- For Loop
- Advance For Loop
- While Loop
- Iterator
See Example :
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
public class LoopArrays {
public static void main(String[] args) {
ArrayList<Integer> arrlist = getArrayList();
}
public static ArrayList<Integer> getArrayList(){
ArrayList<Integer> arrayListLoop = new ArrayList<Integer>();
arrayListLoop.add(20);
arrayListLoop.add(11);
arrayListLoop.add(23);
arrayListLoop.add(34);
arrayListLoop.add(56);
/* For Loop for iterating ArrayList */
System.out.println("For Loop");
for (int counter = 0; counter < arrayListLoop.size(); counter++) {
System.out.println(arrayListLoop.get(counter));
}
/* Advanced For Loop*/
System.out.println("Advanced For Loop");
for (Integer num : arrayListLoop) {
System.out.println(num);
}
/* While Loop for iterating ArrayList*/
System.out.println("While Loop");
int count = 0;
while (arrayListLoop.size() > count) {
System.out.println(arrayListLoop.get(count));
count++;
}
/*Looping Array List using Iterator*/
System.out.println("Iterator");
Iterator iter = arrayListLoop.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
// Get the Enumeration object
Enumeration<Integer> e = Collections.enumeration(arrayListLoop);
// Enumerate through the ArrayList elements
System.out.println("ArrayList elements Enumeration: ");
while(e.hasMoreElements())
System.out.println(e.nextElement());
return arrayListLoop;
}
}
For Loop
20
11
23
34
56
Advanced For Loop
20
11
23
34
56
While Loop
20
11
23
34
56
Iterator
20
11
23
34
56
ArrayList elements Enumeration:
20
11
23
34
56
EmoticonEmoticon