COMPUTER SCIENCE AND ENGINEERING
DATA STRUCTURES
Question
[CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
|
|
for each (String s in list) {}
|
|
for each (String s:list) {}
|
|
for (Integer i:list) {}
|
|
for (String s:list) {}
|
Detailed explanation-1: -ArrayList: [1, 3, 2] Iterating over ArrayList: 1, 3, 2, In the above example, we have used the listIterator() method to iterate over the arraylist. Here, hasNext()-returns true if there is next element in the arraylist.
Detailed explanation-2: -Syntax: For(<DataType of array/List><Temp variable name> : <Array/List to be iterated>) System. out. println(); //Any other operation can be done with this temp variable.
Detailed explanation-3: -Iterate ArrayList using Stream API Java program to iterate through an ArrayList of objects with Java 8 stream API. Create a stream of elements from the list with the method stream. foreach() and get elements one by one. ArrayList<String> namesList = new ArrayList<String>(Arrays.
Detailed explanation-4: -The enhanced for loop (sometimes called a “for each” loop) can be used with any class that implements the Iterable interface, such as ArrayList .