3D-kreativ
3D-kreativ

Reputation: 9301

Get values from arraylist inside object inside arraylist in Java?

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>();

Upvotes: 2

Views: 31110

Answers (4)

Surasin Tancharoen
Surasin Tancharoen

Reputation: 5850

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.

Upvotes: 0

VirtualTroll
VirtualTroll

Reputation: 3091

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

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533472

do you mean?

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

Upvotes: 0

Romczyk
Romczyk

Reputation: 361

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

Upvotes: 0

Related Questions