MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

DATA STRUCTURES

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
How do you write a for each loop that iterates through an ArrayList of Strings?
A
for each (String s in list) {}
B
for each (String s:list) {}
C
for (Integer i:list) {}
D
for (String s:list) {}
Explanation: 

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 .

There is 1 question to complete.