There are several ways to iterate over an ArrayList<Type> of a refrence type
I found the following ways useful, you could try out yourself.
A. The most common/simple way of doing it, by using a counter
to list its elements
ArrayList<Person> persons = new ArrayList<Person>();
//Add some person objects to the list
for(int i=0;i<persons.size();i++){
System.out.println(persons.get(i));
}
B. Using an iterator
for (Iterator i = persons.iterator(); i.hasNext();) {
Object listElement = i.next();
System.out.println(listElement);
}
C. A For/in loop
for (Person listElement : persons) {
System.out.println(listElement);
}
You could possibly use any of the 3 methods to get your work done,
but it is recommended to use the last method which is the For/in loop
since you won't have to use a counter (usually called i or count) or a Iterator
ArrayListof some reference typeAand you want to iterate over yourArrayListso you can access each of theseAobjects ?