Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

How to iterate over a list in Java?

// Use for/while loop to iterate a List and get element by index.
for(int i= 0; i < list.size(); i++) {
   System.out.println(list.get(i));
}
//Use foreach loop to iterate list of elements. 
for(int i= 0; i < list.size(); i++) {
   System.out.println(list.get(i));
}
//Use iterator from the list to iterate through its elements.
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()) {
   System.out.print(iterator.next() + " ");
}
// Use listIterator from the list to iterate through its elements.
Iterator<Integer> iterator = list.listIterator();
while(iterator.hasNext()) {
   System.out.print(iterator.next() + " ");
}
// Use forEach of the list to iterate through its elements.
list.forEach(i -> {System.out.print(i + " ");});
//Use forEach of the stream of list to iterate through its elements.
list.stream().forEach(i -> {System.out.print(i + " ");});
Source by www.tutorialspoint.com #
 
PREVIOUS NEXT
Tagged: #How #iterate #list
ADD COMMENT
Topic
Name
1+3 =