2

This is complicated, but I will do my best to explain my question. Object customer from the Customer class has a arraylist inside the object that store different account numbers. And all of the customer objects are stored in a arraylist. So my problem and question is how to get the account numbers from the arraylist, customerAccountsList, inside the object customer that is inside the arraylist customerList!?

With some help from previous questions here I have learned how to get values from objects inside an arraylist like this:

customerList.get(index).getAccountOwnername();

But now I'm looking for something like this:

customerList.get(index).getAccountNumbers(????);

Help is preciated! Thanks!

EDIT:

customerList.get(index).getAccountNumbers(arraylist that holds all account numbers)

I want to either get a single account number or all account numbers that are inside the arraylist. I don't want to do it to complicated since I rather new in java programming

EDIT 2:

Inside the Customer class I have this code:

ArrayList<String> customerAccountsList = new ArrayList<String>();

Inside the main class I have this code:

// create an arraylist to store customer objects
ArrayList<Customer> customerList = new ArrayList<Customer>();
1
  • 1
    Should work exactly as you wrote. Customer class should have a method: ArrayList<String> getAccountNumbers() {} which will give you an ArrayList of String objects. Commented Nov 24, 2011 at 18:50

4 Answers 4

3

i guess what you are trying to do is

customerList.get(indexCustomer).getAccountNumbers().get(indexAccount)

Depending on your needs, you might have to some re-factoring that is :

  1. Create an object AccountNumbersList having an arrayList
  2. Add the appropriate methods to the previous object getFirstAccount, getLastAccount, getSavingAccount etc.
  3. Change the reference of Customer : refer to AccountNumbersList object
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, this looks promising, but if there are several account numbers inside the arraylist, and i want to pick one of the accounts, how would I know which indexAccount to use?
@ 3D-kreativ: which account will you need ? what criteria ?
I need to get all the account numbers that I have added to the arraylist accountNumbersList
@ 3D-kreativ: in order to display then as a string or you just need the arraylist object ?
Hmm, I guess I want the content! All numbers are added to the arraylist as strings, is that a help?
|
0

Just add method

ArrayList<String> getAccountNumbers()

to your Customer class and so you can get it via

customerList.get(index).getAccountNumbers();

you do not need ???? here, since you will get the whole list directly

Comments

0

do you mean?

List<Account> accounts = new ArrayList<Account>();
for(Customer c: customerList) accounts.addAll(a.getAccounts());

Comments

0

I thing you know not much about Java. So, I have to guess what you want. However, I strongly recommend you to read basic java programming otherwise it will waste your own time. It takes too much time in here than you read java programming book if you know not much about Java.

I guess that customerAccountList comes from something like this code

ArrayList<CustomerAccount> customerAccountList = new ArrayList<CustomerAccount>(); //or something

Then you can get a customer account by:

CustomerAccount oneCustomerAccount = customerAccountList.get(index); //index is integer

Then from oneCustomerAccount , you can get accounts which belong to customer by:

ArrayList<Account> accountList = oneCustomerAccount.getAccountNumbers();

then in accountList, you can get an account by:

Account oneAccount = accountList.get(indexOfAccountYouWant); // indexOfAccountYouWant is integer

then in oneAccount, you can call any methods which have in Account class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.