how to declare n row and 2 column in java code example
Example 1: java two dimensional arrays
int[][] multiples = new int[4][2];
and 2 columns
String[][] cities = new String[3][3];
and 3 columns
Example 2: adding elements in a specified column or row in a two dimensional array java
for (int row = 0; row < matrix.length; row++) {
int rowSum = 0;
for (int col = 0; col < matrix[row].length; col++) {
rowSum += matrix[row][col];
}
System.out.println("Sum of the elements at row " + row + " is: " + rowSum);
}
for (int col = 0; col < matrix[0].length; col++) {
int colSum = 0;
for (int row = 0; row < matrix.length; row++) {
colSum += matrix[row][col];
}
System.out.println("Sum of the elements at col " + col + " is: " + colSum);
}