简体   繁体   中英

Get values from methods in objects that are in arraylist?

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!

// 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM