Following Code will read 2D and 3D arrays using for each loop
Code:
public class Java3
{
public static void main(String[] nt)
{
int[][] nti={
{7,6,2,3},
{6,7},
{2,3},
{7,6},
{7,5},
{7,5,6,7}
};
System.out.println("nti :"+nti); //address of nti[0]
System.out.println("nti length :"+nti.length); // length of array i.e. no of arrays inside
for(int[] i : nti)
{
for(int j : i)
{
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("---------------------");
int[][][] ntii={
{
{7,6,2,3},
{6,7},
{2,3},
{7,6},
{7,5,6,7}
},
{ {7,6,2,3},
{6,7},
{2,3},
{7,6},
{7,5,6,7}
}
};
System.out.println("ntii :"+ntii);
System.out.println("ntii length :"+ntii.length);
for(int[][] i : ntii)
{
for(int[] j : i)
{
for(int k : j)
System.out.print(k+" ");
System.out.println();
}
System.out.println();
}
}
}
Output:
Code:
public class Java3
{
public static void main(String[] nt)
{
int[][] nti={
{7,6,2,3},
{6,7},
{2,3},
{7,6},
{7,5},
{7,5,6,7}
};
System.out.println("nti :"+nti); //address of nti[0]
System.out.println("nti length :"+nti.length); // length of array i.e. no of arrays inside
for(int[] i : nti)
{
for(int j : i)
{
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("---------------------");
int[][][] ntii={
{
{7,6,2,3},
{6,7},
{2,3},
{7,6},
{7,5,6,7}
},
{ {7,6,2,3},
{6,7},
{2,3},
{7,6},
{7,5,6,7}
}
};
System.out.println("ntii :"+ntii);
System.out.println("ntii length :"+ntii.length);
for(int[][] i : ntii)
{
for(int[] j : i)
{
for(int k : j)
System.out.print(k+" ");
System.out.println();
}
System.out.println();
}
}
}
Output: