0
ArrayList<Customer> customerList = new ArrayList<Customer>();
// add object to arraylist
customerList.add(customer);

// get objects from arraylist
customerlist.get(0);

But how do I get the value from a method that is inside the class like if I have a method like getCustomerName() inside and I want the name in return or perhaps I want to change or add something to a method. How do I write the call? Preciate some help! Thanks!

0

2 Answers 2

3
// Single reference.
customerlist.get(0).getCustomerName();

// Or...
Customer c = customerlist.get(0);
c.getCustomerName();

// Or looping.
for (Customer c : customerList) {
    c.getCustomerName();
}

Once you have a reference to the Customer object you can do whatever things you normally do to customers.

Sign up to request clarification or add additional context in comments.

9 Comments

Aha! That easy! So if I want to set some information I can just call the method like customerlist.get(0).setCustomerName("Sandra"); ??
@3D-kreativ Yep :) Although for something like that, it seems like it would be better to do all that work on the customer before adding it to the list, just to avoid confusion.
Hmmm, well I'm doing a task that is like a simple bank system where you create objects like Customers and SavingsAccount. And my idea was to store all Customer objects in one arraylist and all SavingsAccount objects in one arraylist. It must be possible to change the balance and name in the objects, thats why I wondered how to add new values to the objects in the arraylist. I'm new to this, so it's a little bit confusing, but I'm learning.
@3D-kreativ, Perhaps write a singleton manager class that's responsible for adding/retrieving accounts?
@3D-kreativ Hmm. To me it seems like a Customer should have a collection of accounts, or a single account. Things get interesting once you realize that an account could be associated with multiple customers.
|
0

you can implement that using Map

as you have to locate some certain object using the id

map.get(id).setBalance(map.get(id).getBalance() + 100000);

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.