Iterating java arrays


 

Iterating java arrays

Learn How to iterate java arrays

Learn How to iterate java arrays
Iterating java Arrays
    • arrays can be iterated by the for, for each loop.
    • array elements can be added to the list and then it can be iterated by the iterator() method.

Example 1
public class array1 {
    public static void main(String[] args) {
     int ar[]={4,5,6,88,9,1};
     for(int x:ar)
         System.out.print(x+"\t");
    }
}


Output
4 5 6 88 9 1

Example2
import java.util.*;

public class array2 {
    public static void main(String[] args) {
     int ar[]={4,5,6,88,9,1};
     List list=new ArrayList();
     
        for (int i = 0; i < ar.length; i++) {
            list.add(ar[i]);
        }
     Iterator i=list.iterator();
     while(i.hasNext())
         System.out.print(i.next()+"\t");
    }
}

Output
4 5 6 88 9 1

Ads