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
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