## How to declare a two-dimensional array?

A two-dimensional array stores data in tabular form.

It needs the number of both rows and columns.

Here is an example:

`int a[][];`

a = new int[3][4];

In the above code, the first line declares a two-dimensional array variable of integer type.

The second line creates a 2-D array object with 3 rows and 4 columns.

This object is referred to by the variable named ‘a’ that was created in the first line.

Also note that Java will automatically initialize each block in the array for you.

In the above code, the array is filled with zeroes automatically.

The same can be done in one line as follows:

`int a[][] = new int[3][4];`

When you already know the values beforehand that you want to store in a 2D array, you may use the following technique:

`int a[][] = {{1, 2, 3}, {7, 8, 9}};`

The above code will create an array object of 2 rows and 3 columns, and will fill it with the values provided in the code.

## How to accept data in a double-dimensional array?

To input data in a 2D array, we need to access each block of the array using both row and column indices.

For this purpose, we need to use a nested loop:

```
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][] = new int[3][4];
for(int i = 0; i < 3; i++){
for(int j = 0; j < 4; j++){
a[i][j] = Integer.parseInt(br.readLine());
}
}
```

## How to display the elements of a two-dimensional array in a matrix format?

A matrix format displays the data in tabular format.

Here also, we need to access each block of the array, for which we need nested loops.

Also note that while printing, we need to take the cursor to the next line only when we have finished printing one row.

And individual values/elements must be separated with gaps using horizontal tab character so that the matrix format is followed.

Here is the code to display the array created earlier in matrix format:

```
for(int i = 0; i < 3; i++){
for(int j = 0; j < 4; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
```

## How to find the sum of individual rows in a double-dimensional array?

To find the sum of the individual rows in the matrix, we need an accumulator to store the result.

This accumulator needs to be re-initialized each time we are done with one row.

We need to access the elements row-wise, find the sum for each row and display it as follows:

```
for(int i = 0; i < 3; i++){
int sum = 0;
for(int j = 0; j < 4; j++){
sum += a[i][j];
}
System.out.println("Sum of row " + (i + 1) + ": " + sum);
}
```

## How to find the sum of individual columns in a double-dimensional array?

For this task also we need an accumulator, but now we need to access the matrix column-wise.

Here is the code that demonstrates this:

```
for(int i = 0; i < 4; i++){
int sum = 0;
for(int j = 0; j < 3; j++){
sum += a[j][i];
}
System.out.println("Sum of column " + (i + 1) + ": " + sum);
}
```

## How to find the sum of the diagonal elements?

It is important to note that diagonals are only possible in a square matrix.

A square matrix is one which has the same number of rows and columns.

There are two possible diagonals in a matrix: left and right.

The left diagonal ranges from upper left corner to lower right corner in a straight line.

Notice that every block has the same index for the row and column when we access the elements in the left diagonal.

Code to find the sum of the elements in the left diagonal:

```
//assuming square matrix of size 3
int sum = 0;
for(int i = 0; i < 3; i++){
sum += a[i][i];
}
System.out.println("Sum = " + sum);
```

The right diagonal ranges from upper right corner to lower left corner in a straight line.

Notice that we start accessing the elements from the first row and last column.

Code to find the sum of the elements in the right diagonal:

```
//assuming square matrix of size 3
int sum = 0;
for(int i = 0, j = 2; i < 3; i++, j--){
sum += a[i][j];
}
System.out.println("Sum = " + sum);
}
```