Java Array Iterator with Example


 

Java Array Iterator with Example

In this tutorial we will learn how to use the Java Iterator interface as Array. We will create an example print the multiple ArrayList at a time using the Iterator

In this tutorial we will learn how to use the Java Iterator interface as Array. We will create an example print the multiple ArrayList at a time using the Iterator

  • Java Array Iterator is an interface in the collection framework.
  • Java ArrayList is a collection class and implements the List Interface.
  • All the elements of the ArrayList can be traversed by the Iterator.
  • There can be array of the Iterator interface to manipulate more than one ArrayList


Java Array Iterator with Example
import java.util.*;

public class arrayIterator {

	public static void main(String[] args) {
		int ar[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
				{ 13, 14, 15, 16 } };

		ArrayList list[] = { new ArrayList(), new ArrayList(), new ArrayList(),
				new ArrayList() };
		for (int i = 0; i <= 3; i++) {
			for (int j = 0; j <= 3; j++) {
				list[i].add(ar[i][j]);
			}
		}
		Iterator it[] = { list[0].iterator(), list[1].iterator(),
				list[2].iterator(), list[3].iterator() };
		for (int k = 0; k <= 3; k++) {
			while (it[k].hasNext()) {
				System.out.print(it[k].next() + "\t");
			}
			System.out.println();
		}
	}
}

Output :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Ads