I am trying to create a three dimensional array that outputs as:
[1,2,3], [1,2,3], [1,2,3], [1,2,3]
[1,2,3], [1,2,3], [1,2,3], [1,2,3]
[1,2,3], [1,2,3], [1,2,3], [1,2,3]
[1,2,3], [1,2,3], [1,2,3], [1,2,3]
[1,2,3], [1,2,3], [1,2,3], [1,2,3]
Here is the code I have come up with:
public class Triples {
public static void main(String[] args) {
int[][][] triplet = new int[5][4][3];
for (int i = 0; i < triplet.length; i++)
{
for (int j = 0; j < triplet[i].length; j++)
{
System.out.print("[");
for (int k = 0; k < triplet[i][j].length; k++)
{
triplet[i][j][k] = i+1;
System.out.print(triplet[i][j][k] + "," + "");
}
System.out.print("]");
}
System.out.println();
}
}
}
The result I have looks like:
[1,1,1,][1,1,1,][1,1,1,][1,1,1,]
[2,2,2,][2,2,2,][2,2,2,][2,2,2,]
[3,3,3,][3,3,3,][3,3,3,][3,3,3,]
[4,4,4,][4,4,4,][4,4,4,][4,4,4,]
[5,5,5,][5,5,5,][5,5,5,][5,5,5,]
My issue is I am not sure how to increment the values in the third for loop to go as 1, 2, and 3. I have tried different combinations, but they have resulted with either the same or incorrect result. Any tips?