Quantcast
Viewing latest article 2
Browse Latest Browse All 30

Answer by Java-Dev for In detail, how does the 'for each' loop work in Java?

The code would be:

import java.util.ArrayList;import java.util.List;public class ForLoopDemo {  public static void main(String[] args) {    List<String> someList = new ArrayList<String>();    someList.add("monkey");    someList.add("donkey");    someList.add("skeleton key");    // Iteration using For Each loop    System.out.println("Iteration using a For Each loop:");    for (String item : someList) {      System.out.println(item);    }    // Iteration using a normal For loop    System.out.println("\nIteration using normal For loop: ");    for (int index = 0; index < someList.size(); index++) {      System.out.println(someList.get(index));    }  }}

Viewing latest article 2
Browse Latest Browse All 30

Trending Articles