Home Tutorial Java Collections Arrays Iterating java arrays

 
 

Share on Google+Share on Google+
Iterating java arrays
Posted on: October 22, 2009 at 12:00 AM
Advertisement
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
Advertisement

Related Tags for Iterating java arrays:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 22, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com