I’m learning Java programming and right now I’m exploring the use of objects in arralist. I know how to get a single value out of a object that are in a arraylist like this:
customerList.get(0).getAccountOwnerName()
EDIT: This how I have done and this is what my question is about. Perhaps there is a better way too do this?
for(int i=0;i<customerList.size();i++){
System.out.println(customerList.get(i).getAccountOwnerName());
System.out.println(customerList.get(i).getAccountOwnerPersonalNumber());
}
THIS IS MY OLD QUESTION: But know I have a problem and I have searched for a solution to iterate through an arraylist and get each value from the objects methods like getAccountOwnerName and getAccountNumber. I thought this code could be a start, but I need some help to develop it further or perhaps there is some better way to do this? Thanks!
System.out.print("List of customer");
Iterator<String> itr = customerList.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.println(element + " ");
}
All objects that implement
CollectionlikeArrayListsupport the newforloop as of Java 1.5. Really anything that implementsIterabledoes. This means you can do something like:This should be more efficient that doing repeated
get(i). This uses the iterator method internally but is a lot easier to code to. Here’s a good link of information:You can also iterate through arrays although they don’t implement
Iterable: