# Processing Two Dimensional Array

Nest for loops are often used to process a two dimensional array.

Suppose an array `matrix` is made as follows:

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

The following are some examples of processing two dimensional arrays:

## 1. Initializing arrays values by User Input.

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();
}
}```

## 2. 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);
}
}```

## 3. 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();
}```

## 4. 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];
}
}```

## 5. 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 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);
}```

*Notice that column & row switched top & bottom order between examples 4. & 5. (this one)

## 6. Which row has the largest sum?

Us variables `maxRow` and `indexOfMaxRow` to track the largest sum and the 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];
}

//Finding the total value of a row, for all rows
for (int row = 1; row < matrix.length; row++) {
int totalOfThisRow = 0;
for (int column = 0; column < matrix[row].length; column++)
totalOfThisRow += matrix[row][column];

//Compare totalOfThisRow to maxRow so that row with the largest total sum is selected after evaluation
if (totalOfThisRow > maxRow) {
maxRow = totalOfThisRow;
indexOfMaxRow = row;
}
}

System.out.println("Row " + indexOfMaxRow + " has the madimum sum of " + maxRow);```

## 7. Random shuffling.

Shuffling the elements in a one-dimensional array was introduced in ___ . 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;
}
}```

# Passing Two Dimensional Arrays to Methods

When passing a two dimensional array to a method, the reference of the array is passed to the method.

You can pass a two dimensional array to a method just as you pass a one dimensional array. You can also return an array from a method. The example below shows 2 methods. The first method, `getArray()`, returns a two dimensional array, and the second method, `sum(int[][] m)`, returns the sum of all the elements in a matrix.

```import java.util.Scanner;

public class PassTwoDimensionalArray {
public static void main(String[] args) {
int[][] m = getArray(); //Get an array

// Display sum of elements
System.out.println("\nSum of all elements is " + sum(m));
}

public static int[][] getArray() {
// Make a Scanner
Scanner input = new Scanner(System.in);

// Enter array values
int[][] m = new int[3][4];
System.out.println("Enter " + m.length + " rows and " + m[0].length + " columns: ");
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
m[i][j] = input.nextInt();
return m;
}

public static int sum(int[][] m) {
int total = 0;
for (int row = 0; row < m.length; row++) {
for (int column = 0; column < m[row].length; column++) {
total += m[row][column];
}
}

}
}```

## Further Explanation

The methods `getArray` prompts the user to enter values for the array:

```public static int[][] getArray() {
// Make a Scanner
Scanner input = new Scanner(System.in);

// Enter array values
int[][] m = new int[3][4];
System.out.println("Enter " + m.length + " rows and " + m[0].length + " columns: ");
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
m[i][j] = input.nextInt();
return m;
}```

and returns the array:

`        return m;`

The method `sum`:

```public static int sum(int[][] m) {
int total = 0;
for (int row = 0; row < m.length; row++) {
for (int column = 0; column < m[row].length; column++) {
total += m[row][column];
}
}

has a two dimensional array argument. You can obtain the number of rows using `m.length`:
`        for (int row = 0; row < m.length; row++)`
and the number of columns in a specified row using `m[row].length`:
`            for (int column = 0; column < m[row].length; column++)`