There is a matrix, you need to display each element and also display its row and column.

public static void main(String[] args) { int[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int i = 0; int j; for (j = 0; j < mat.length; j++) { System.out.println("Element: " + mat[i][j] + "\n Ряд: " + i + "\n Столбец: " + j); i++; } } 

Displays only for 1, 5 and 9, since i and j are increased by 1, I do not know how to apply for each element.

  • We need another nested cycle - Oleksiy Morenets

1 answer 1

Everything is easier!

 public static void main(String[] args) { int[][] mass = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int i = 0; i < mass.length; i++) { for (int j = 0; j < mass.length; j++) { System.out.println(mass[i][j] + "\n Ряд: " + i + "\n Столбец: " + j); } System.out.println(""); //каждый раз переходим на новую строчку вывода } } 

Conclusion:

 1 Ряд: 0 Столбец: 0 2 Ряд: 0 Столбец: 1 3 Ряд: 0 Столбец: 2 4 Ряд: 1 Столбец: 0 5 Ряд: 1 Столбец: 1 6 Ряд: 1 Столбец: 2 7 Ряд: 2 Cтолбец: 0 8 Ряд: 2 Столбец: 1 9 Ряд: 2 Столбец: 2 
  • 3
    j < mass[i].length - Igor
  • what for? still works - michael_best
  • one
    This will only work in this case. But if the array is uneven, then it will not work - michael_best
  • one
    "You ruin yourself, my young friend!" Alexander Dumas - Igor
  • how to draw a nice draw? - michael_best 4:04 pm