Search
 
SCRIPT & CODE EXAMPLE
 

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 + " ");});
Comment

java iterate over list

for (int i = 0; i < crunchifyList.size(); i++) {
            System.out.println(crunchifyList.get(i));
        }
Comment

java loop through list

for (E element : list) {
    . . .
}
Comment

iterate list in java

Iterator itr=list.iterator();
while(itr.hasNext)
{
 system.out.println(itr.next); 
}
Comment

Iterate List> in java

List<List<Int>> arrayList = new ArrayList(); //Java usually infers type parameters in cases as these
for(int i = 0; i < desiredSize; i++){
    List<Int> listAtI = new ArrayList ();
    for(int j = 0; j < rowLength; j++){
        listAtI.set(j, 0);  //sets the element at j to be  0, notice the values are Int not int, this is dues to Javas generics having to work with classes not simple types, the values are (mostly) automatically boxed/unboxed
    }
    arrayList.set(i, listAtI);
}

arrayList.get(5); //returns the list at index 5
arrayList.get(5).get(5) // returns values from column 5 in row 5 
Comment

PREVIOUS NEXT
Code Example
Java :: java string lowercase 
Java :: solucion var java 
Java :: cannot set char field to null value java 
Java :: absolute method in jdbc 
Java :: HOW TO SUPRESS sonar warning in java code 
Java :: Java get list of keys Hashmap 
Java :: how to add animation to nivation component android 
Java :: java swing jtable zebra stripes 
Java :: actionlistener java 
Java :: how to use ? in java without using if 
Java :: sum and array list java 
Java :: how to converet negative byte value to postive int value in java 
Java :: stream sum java 
Java :: print line number java 
Java :: java only close socket outputstream 
Java :: why java is popular 
Java :: writing to a text file java 
Java :: java max integer 
Java :: keytool error: java.io.IOException: keystore password was incorrect java.io.IOException: keystore password was incorrect flutter 
Java :: java eliminate numbers from string 
Java :: java pre increment 
Java :: get value textfield java 
Java :: fibunacci java 
Java :: how to find the highest power of 2 that divides a number? 
Java :: font type javafx button css 
Java :: how to count lines from txt java 
Java :: java write 
Java :: java random char 
Java :: unparseable date error in java 
Java :: Java program to find the sum of first 100 numbers 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =