If I have an array for a bank client's transaction number, would I need a different array to store their balance and another for their names, etc.?开发者_如何学C
Is there a way that I could put all of those data into one element of the array? So that when I call the first element of the array, all the data (that is, the transaction number, balance, and name) would be printed out.
Put those fields into a class and make an array of objects.
private class Transaction {
public String name;
public int balance;
public int number;
public Foo etc;
}
private Transaction[] transactions = new Transaction[transactionCount];
Now you can say, e.g.:
int thatGuysBalance = transactions[thatGuysIndex].balance;
Or:
Transaction t = new Transaction();
t.balance = 1000000;
t.number = 3;
t.name = "That Guy";
transactions[thatGuysIndex] = t;
Look into the use of java.util.List
and ArrayList
and Map
s. They are much easier to use and may have higher performance, depending on your needs.
精彩评论