Austin
Austin

Reputation: 3080

Objects and ArrayLists

So rather than describe what I have tried, I will just simply lay out what I am trying to do, as every method thus far has failed me, and I am sure it is something simple I am missing.

What I need to do is make an array of objects. Whether it is an arraylist that holds objects, or an object array that holds objects I do not care.

I have to make a banking program, I have 8 data fields, each set makes up an account and thus an object. I first add all 8 data fields into an object, then I have been adding that object to either an arraylist or array of objects.

My issue, is when I try to recall a specific account. Let's say I call up the arraylist or object array, when I try to call the original objects within either of those, they are empty or incompatible. It's almost as if the data just getting mushed together.

here is some code I have to help explain.

static ArrayList AccountList = new ArrayList();
    static Object[] User = new Object[8];

where I add the data into the object User

type = AddAccount.jComboBox1.getSelectedItem().toString();
    User[0] = type;
    number = AddAccount.jTextField1.getText();
    User[1] = number;
    owner = AddAccount.jTextField2.getText();
    User[2] = owner;
    date = AddAccount.jTextField3.getText();
    User[3] = date;
    balance = AddAccount.jTextField4.getText();
    User[4] = balance;
    fee = AddAccount.jTextField5.getText();
    User[5] = fee;
    rate = AddAccount.jTextField6.getText();
    User[6] = rate;
    minBalance = AddAccount.jTextField7.getText();
    User[7] = minBalance;
       AccountList.add(counter,User);
    counter++;

I call the following when I want data, int selection is just referring the object I want

 public static void displayData(int selection) {
            int row = selection;
            Object temp = AccountList.get(selection);
            System.out.println(temp[0].toString); //<--WHY WONT THAT WORK?

        }

Any help appreciated, or alternative methods.

Upvotes: 0

Views: 367

Answers (1)

amit
amit

Reputation: 178451

You have some issues I can note here:

  1. You keep using the same Object[], before each insertion you should create a new Object[] - otherwise you will override existing data. [remember, the ArrayList contains references, if you modify an object in it, it will be modified in the ArrayList as well.
  2. You need to cast temp to an Object[] - or even better, declare AccountList as ArrayList<Object[]>
  3. After all of it is done - it should be toString(), and not toString [it is a method]

Not related, but important for readability: in java, the convention is that variables are starting with lower-case characters. It helps fellow programmers understand when reading accountList and not AccountList that it is a variable, and not a class.

Also, as @GuillaumePolet mentioned, you might want to reconsider the desing, instead of using an Object[], and storing it in the ArrayList, you can build your own class [MyBankAccount for examples] with 8 fields, and have AccountList of type ArrayList<MyBankAccount>. You will find it much easier to follow the logic and maintain your code this way!

Upvotes: 4

Related Questions