Share on Google+Share on Google+

Iterating java arrays

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

Advertisements

Advertisement

Posted on: October 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.