开发者

Is there a way that I could put a number of data into one element of an array or do I need to create one array per data?

开发者 https://www.devze.com 2023-03-31 01:32 出处:网络
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

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 Maps. They are much easier to use and may have higher performance, depending on your needs.

0

精彩评论

暂无评论...
验证码 换一张
取 消