how to iterate through list code example
Example 1: java loop through arraylist
ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") );
for(String name : namesList)
{
System.out.println(name);
}
Example 2: how to loop over list
list = [1, 2, 3, 4, 5, 6]
for item in list:
print(item)
for index in range(len(list)-1):
print(list[index])
for index, value in enumerate(list):
print(value)
Example 3: java loop through list
for (E element : list) {
. . .
}
Example 4: iterate through a list
my_list = ["Hello", "World"]
for i in my_list:
print(i)
Example 5: traversal of list in java
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorSample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("JavaFx");
list.add("Java");
list.add("WebGL");
list.add("OpenCV");
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Example 6: iterate over a list python
list = [1, 3, 5, 7, 9]
for i in list:
print(i)