I edit the question again. I take the first column, from top to bottom. First position like this and compare if they are the same as the one below. This second position, compares if they are the same with the one above and the one below. and so on.
Asked
Active
Viewed 860 times
-1
-
Well, this does no longer have much to do with the original question. – luk2302 Dec 01 '17 at 15:43
-
Because I would like to do it myself, but if I see that I try and it does not work out for me as the last remedy to ask for help. I thought that printing the matrix would help me, but not. – Fernando Dec 01 '17 at 15:57
2 Answers
0
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
System.out.print(matrix[j][i] + " ");
}
System.out.println();
}
matrix[j][i]
in place of matrix [i][j]
should do the trick.
and yeah remember arrays start from index 0.

Shivendra Agarwal
- 648
- 6
- 17
-
-
1@FernandoBenavidel: So you want only column traversal.? and the output should be same as input? – Shivendra Agarwal Nov 30 '17 at 19:32
-
Print the matrix by columns. From top to bottom First print column 1, then column 2, then column 3... – Fernando Nov 30 '17 at 19:36
-
The code that I put before was to print the matrix by rows, from left to right, row 1, row 2, row 3 ... – Fernando Nov 30 '17 at 19:37
-
@FernandoBenavidel: you do understand that once we come down a line by println, we cannot go back to previous line? output is sequential.. best i can tell for your need is to change above code as: System.out.println(matrix[j][i] + " "); This will bring everything in new row. – Shivendra Agarwal Nov 30 '17 at 19:47
-
@FernandoBenavide: you can try using \b character to go back a character.. never used it though.. see for: https://stackoverflow.com/questions/8090155/java-carriage-to-previous-line – Shivendra Agarwal Nov 30 '17 at 19:59
0
I may not totally understand the question, but maybe consider using Streams rather than looping.
int[][] matr = { { 1,2,3,4,5 }, { 6,7,8,9,10}, { 11,12,13,14,15}, { 16,17,18,19,20}, {21,22,23,24,25} };
Arrays.stream(matr).forEach(row->
System.out.println(Arrays.stream(row).mapToObj(i -> String.format("%2d",i)).collect(Collectors.joining(" ")))
);
This outputs:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

darkpbj
- 2,892
- 4
- 22
- 32