Processing Two-Dimensional Arrays
Tags: java, programming, learning, beginners
Published: 2024-05-29 14:05:10

Nested **for** loops are often used to process a two-dimensional array.
Suppose an array **matrix** is created as follows:

`int[][] matrix = new int[10][10];`

The following are some examples of processing two-dimensional arrays.

_Initializing arrays with input values_. The following loop initializes the array with user input values:

`java.util.Scanner input = new Scanner(System.in);
System.out.println("Enter " + matrix.length + " rows and " +
 matrix[0].length + " columns: ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
 matrix[row][column] = input.nextInt();
 }
}`

_Initializing arrays with random values_. The following loop initializes the array with random values between **0** and **99**:

`for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
 matrix[row][column] = (int)(Math.random() * 100);
 }
}`

_Printing arrays_. To print a two-dimensional array, you have to print each element in the array using a loop like the following:

`for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
 System.out.print(matrix[row][column] + " ");
 }
 System.out.println();
}`

_Summing all elements_. Use a variable named **total** to store the sum. Initially **total** is **0**. Add each element in the array to total using a loop like this:

`int total = 0;
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
 total += matrix[row][column];
 }
}`

_Summing elements by column_. For each column, use a variable named **total** to store its sum. Add each element in the column to **total** using a loop like this:

```
for (int column = 0; column < matrix[0].length; column++) {
int total = 0;
for (int row = 0; row < matrix.length; row++)
 total += matrix[row][column];
 System.out.println("Sum for column " + column + " is "
 + total);
}
```

_Which row has the largest sum?_ Use variables **maxRow** and **indexOfMaxRow** to track the largest sum and index of the row. For each row, compute its sum and update **maxRow** and **indexOfMaxRow** if the new sum is greater.

```
int maxRow = 0;
int indexOfMaxRow = 0;
// Get sum of the first row in maxRow
for (int column = 0; column < matrix[0].length; column++) {
 maxRow += matrix[0][column];
}
for (int row = 1; row < matrix.length; row++) {
int totalOfThisRow = 0;
for (int column = 0; column < matrix[row].length; column++)
 totalOfThisRow += matrix[row][column];
if (totalOfThisRow > maxRow) {
 maxRow = totalOfThisRow;
 indexOfMaxRow = row;
 }
}
System.out.println("Row " + indexOfMaxRow
 + " has the maximum sum of " + maxRow);
```

_Random shuffling_. How do you shuffle all the elements in a two-dimensional array? To accomplish this, for each element **matrix[i][j]**, randomly generate indices **i1** and **j1** and swap **matrix[i][j]** with **matrix[i1][j1]**, as follows:

`for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
int i1 = (int)(Math.random() * matrix.length);
int j1 = (int)(Math.random() * matrix[i].length);
// Swap matrix[i][j] with matrix[i1][j1]
int temp = matrix[i][j];
 matrix[i][j] = matrix[i1][j1];
 matrix[i1][j1] = temp;
 }
}`