A two dimensional array is an array of an array. It can represent a matrix or a table.
For example, the following table describe the distance between the cities can be represented using a two dimensional array.
The syntax to declare an array:
datatype[][] arrayVar;
Example:
int[][] matrix;
And to create a two dimensional array of 4 by 4 double values and assign it to matrix using syntax:
matrix = new int[4][4];
Two subscripts are used in a two-dimensional array, one for the row and the other for the column. As in one dimensional array, the index for each subscript is of the int type and starts from 0.
To declare and create a two dimensional array of 4 by 4:
int[][] matrix = new int[4][4];
matrix
We can also use an array initializer to declare, create, and initialize a two-dimensional array.
For example:
int[][] table = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
A two dimensional array is actually an array in which each element is a one dimensional array. The length of an array x is the number of elements in the array, which can be obtained using x.length.
For example:
x = new int[3][4];
x.length is 3
x[0].length is 4
x[1].length is 4
x[2].length is 4
Each row in a two dimensional array is itself an array. Thus the rows can have different length.
For example:
int[][] table = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5},
}
Suppose an array matrix is declared as follows:
int [][] matrix = new int[10][10];
// declare the array 2 dimensional
a. Initializing arrays with 0.
for (int row=0; row< matrix.length; row++){
for (int col=0; col<matrix[row].length; col++) {
matrix[row][col] = 0;
}
}
b. Read an array.
Scanner sc = new Scanner(System.in);
for (int row=0; row< matrix.length; row++){
for (int col=0; col<matrix[row].length; col++) {
matrix[row][col] = sc.nextInt();
}
}
b. Printing a two dimensional array.
for (int row=0; row< matrix.length; row++){
for (int col=0; col<matrix[row].length; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println();
}
c. Summing all elements.
int sum = 0;
for (int row=0; row< matrix.length; row++){
for (int col=0; col<matrix[row].length; col++) {
sum += matrix[row][col];
}
}
d. Summing elements by row.
for (int row=0; row< matrix.length; row++){
int sum = 0;
for (int col=0; col<matrix[row].length; col++) {
sum += matrix[row][col];
}
System.out.println("sum row " + row + " = " + sum);
}
e. Summing elements by column.
for (int col=0; col< matrix.length; col++){
int sum = 0;
for (int row=0; row<matrix[row].length; row++) {
sum += matrix[row][col];
}
System.out.println("sum colum " + col + " = " + sum);
}
f. find the row with largest sum.
int maxRow = 0;
int indexOfMaxRow = 0;
for(int col=0; col<matrix[0].length; col++){
maxRow += matrix[0][col];
}
for (int row=0; row< matrix.length; row++){
int totalOfThisRow = 0;
for (int col=0; col<matrix[row].length; col++) {
totalOfThisRow += matrix[row][col];
if (totalOfThisRow > maxRow){
maxRow = totalOfThisRow;
indexOfMaxRow = row;
}
}
}
System.out.println("Row " + indexOfMaxRow + " has the maximum sum " + " of " + maxRow);
Write a program that will read an array of size 5 and find the sum of all elements.
public class FindSumAll{
public static void main(String[] args){
int [][] matrix = new int[5][5];
ReadList(matrix);
PrintList(matrix);
System.out.println("total = " + FindSum(matrix));
}
// static method ReadList
public static void ReadList(int [][] arr){
for (int row=0; row< arr.length; row++){
for (int col=0; col<arr[row].length; col++) {
arr[row][col] = row * col;
}
}
}
// static method PrintList
public static void PrintList(int [][] arr){
for (int row=0; row< arr.length; row++){
for (int col=0; col<arr[row].length; col++) {
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}
// static method for summing all elements
public static int FindSum(int[][] arr){
int sum = 0;
for (int row=0; row< arr.length; row++){
for (int col=0; col<arr[row].length; col++) {
sum += arr[row][col];
}
}
return sum;
}
}