I have the following matrix
1 2 3 4 5 6
2 3 4 5 6 1
3 4 5 6 1 2
4 5 6 1 2 3
5 6 1 2 3 4
6 1 2 3 4 5
…and I want to have it in a table
| 1 2 3 4 5 6
---------------
1| 1 2 3 4 5 6
2| 2 3 4 5 6 1
3| 3 4 5 6 1 2
4| 4 5 6 1 2 3
5| 5 6 1 2 3 4
6| 6 1 2 3 4 5
Having n elements is it possible to print such a table dynamically?
public static void main ( String [ ] args ) {
int n = Integer.parseInt(args[0]);
for(int w = 1; w <= n; w++){
System.out.println("");
for(int p = w; p <= n + w - 1; p++){
System.out.print((p-1)%n+1 + " ");
}
}
}
Depends on the array, assuming that you know the array size beforehand sure. In Java arrays can be jigged aka subarrays do not have to be same size, what in mind:
For test case:
Output:
Run online: link