1ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") );
2
3for(String name : namesList)
4{
5 System.out.println(name);
6}
7
1// Using Classic For Loop
2for (int i=0; i<myArrayList.size(); i++) {
3 System.out.println(myArrayList.get(i));
4}
5/*****************************************************/
6//Advanced For Loop - ArrayList<String>
7for(String str : myArrayList) {
8 System.out.println(str);
9}
10/*****************************************************/
11//Using Iterator
12Iterator arrayListIterator = myArrayList.iterator();
13while (arrayListIterator.hasNext()) {
14 System.out.println(arrayListIterator.next());
15}
16/*****************************************************/
17//ForEach - java 8 - ArrayList<String>
18myArrayList.forEach( str -> System.out.println(str));
19
20
21
22
1for (int i = 0; i < crunchifyList.size(); i++) {
2 System.out.println(crunchifyList.get(i));
3 }
1 for (int counter = 0; counter < arrlist.size(); counter++) {
2 System.out.println(arrlist.get(counter));
3 }
1// will iterate through each index of the array list
2// using the size of the array list as the max.
3// (the last index is the size of the array list - 1)
4
5for (int i = 0; i < myArrayList.size(); i++) {
6
7 System.out.println(myArrayList.get(i));
8 // will print each index as it loops
9}
1Iterator<String> crunchifyIterator = crunchifyList.iterator();
2 while (crunchifyIterator.hasNext()) {
3 System.out.println(crunchifyIterator.next());
4 }