Skip to content
Computer ApplicationsICSE Class 10 · Arrays

Double Dimensional Arrays: Rows, Columns and Diagonals (ICSE Class 10 Java)

Updated 17 October 2026 · Fact-checked

A double dimensional array int[][] m = new int[r][c] has r rows and c columns; m[i][j] is the element in row i and column j, with indices starting from 0. m.length gives the number of rows and m[0].length the number of columns. For a square matrix the left diagonal has i equal to j and the right diagonal has i + j equal to n - 1. Nested for loops visit the elements row by row.

Row sums run across (orange), column sums run down (green); left diagonal has i = j, right diagonal has i + j = 2, and both cross at the centre 5.

Illustration drawn for QuestionBuddy.

Understand Double Dimensional Arrays: Row, Column and Diagonal Sums

Declaration and creation.

int[][] m = new int[3][4];          // 3 rows, 4 columns, all elements 0
int[][] t = {{1, 2, 3}, {4, 5, 6}}; // initialised: 2 rows, 3 columns

m.length is the number of rows; m[0].length is the number of columns. Valid row indices are 0 to m.length - 1; m[3][0] for 3 rows is an ArrayIndexOutOfBoundsException.

Accepting data and displaying in matrix format.

Scanner in = new Scanner(System.in);
for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
        m[i][j] = in.nextInt();

for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++)
        System.out.print(m[i][j] + " ");
    System.out.println();
}

The outer loop is the row, the inner loop is the column, and println() after the inner loop gives the matrix shape.

Sums.

  • Row i: s = 0; then s += m[i][j] over j.
  • Column j: s = 0; then s += m[i][j] over i (the inner loop runs over rows).
  • Left (main) diagonal of an n by n matrix: elements m[i][i].
  • Right diagonal: elements m[i][n - 1 - i], where i + j = n - 1.

Diagonals exist as such only for square matrices. Reset the sum to 0 for every new row or column.

Example matrix {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}: row sums 6, 15, 24; column sums 12, 15, 18; left diagonal 1 + 5 + 9 = 15; right diagonal 3 + 5 + 7 = 15.

Sorting and searching are required only for single-dimensional arrays; the syllabus for double dimensional arrays covers declaration, initialisation, accepting data, sums and display.

Double Dimensional Arrays: Row, Column and Diagonal Sums: key facts

Size of a 2D array
Rowsm.length
Columnsm[0].length
Elementsrows multiplied by columns
Last elementm[rows - 1][columns - 1]
Indices start at 0.
Element access
  • m[i][j]: row i, column j
  • i from 0 to rows - 1
  • j from 0 to columns - 1
Row first, column second.
Diagonals of an n by n matrix
Left diagonalm[i][i]
Right diagonalm[i][n - 1 - i]
Right diagonali + j = n - 1
Only for square matrices.
Row and column sums
  • Row sum: outer loop i, inner loop j
  • Column sum: outer loop j, inner loop i
  • Reset the sum to 0 each time
Swap the loops for columns.

How to answer Double Dimensional Arrays: Row, Column and Diagonal Sums questions

Use this routine for 2D array questions.

  1. 1Write the matrix as a grid with row and column indices.
  2. 2Decide the loop order: row sums and display use rows outside; column sums use columns outside.
  3. 3Pick the elements asked for with the index rule (i equals j, or i + j equals n - 1).
  4. 4Reset accumulators to 0 at the start of each row or column.
  5. 5Check every index stays within 0 to length - 1.

Double Dimensional Arrays: Row, Column and Diagonal Sums in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Seating chart as a matrix

Suppose a class seating chart has 3 rows and 4 columns, and the number of present students is stored for every seat (1 for present, 0 for absent).

Which array declaration fits, and how many elements does it hold?

Try it first, then show the solutionSolution
  1. Rows and columns: int[][] seat = new int[3][4];
  2. Elements: 3 multiplied by 4 = 12.

Answer: int[][] seat = new int[3][4]; 12 elements.

Memory hook: Rows first, then columns.

Tricks to remember Double Dimensional Arrays: Row, Column and Diagonal Sums

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Row then column

    m[row][column]: read it like a street address, street first, house number second.

    Use it when: Indexing.

  • i equals j is the main road

    The left diagonal has equal indices; the right diagonal indices add up to n - 1.

    Use it when: Diagonal elements.

  • Reset the bucket

    Empty the sum variable before each new row or column.

    Use it when: Avoiding running totals.

Double Dimensional Arrays: Row, Column and Diagonal Sums: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Size of an array · 2 marks · Short answerBoard level

For int[][] a = new int[3][4]; state a.length, a[0].length, the total number of elements and the index of the last element.

Try it first, then show the solutionSolution
  1. a.length = 3 (rows) and a[0].length = 4 (columns). (1 mark)
  2. Total elements: 3 x 4 = 12; the last element is a[2][3]. (1 mark)

Answer: 3, 4, 12 elements, last element a[2][3]

Example 2: Row sums and column sums · 3 marks · Short answerHarder board level

Write the output:

int[][] m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < 3; i++) {
    int s = 0;
    for (int j = 0; j < 3; j++)
        s += m[i][j];
    System.out.print(s + " ");
}
System.out.println();
for (int j = 0; j < 3; j++) {
    int s = 0;
    for (int i = 0; i < 3; i++)
        s += m[i][j];
    System.out.print(s + " ");
}
Try it first, then show the solutionSolution
  1. Row sums: 1 + 2 + 3 = 6; 4 + 5 + 6 = 15; 7 + 8 + 9 = 24. (1 mark)
  2. Column sums: 1 + 4 + 7 = 12; 2 + 5 + 8 = 15; 3 + 6 + 9 = 18. (1 mark)
  3. println() separates the two lines; each number is followed by a space. (1 mark)

Answer: 6 15 24
12 15 18

Example 3: Both diagonals · 3 marks · Short answerHarder board level

Write the output:

int[][] m = {{2, 4, 6}, {1, 3, 5}, {9, 7, 8}};
int left = 0, right = 0;
for (int i = 0; i < 3; i++) {
    left += m[i][i];
    right += m[i][2 - i];
}
System.out.println(left + " " + right);
Try it first, then show the solutionSolution
  1. Left diagonal: m[0][0] + m[1][1] + m[2][2] = 2 + 3 + 8 = 13. (1 mark)
  2. Right diagonal: m[0][2] + m[1][1] + m[2][0] = 6 + 3 + 9 = 18. (1 mark)
  3. Output: 13 18. (1 mark)

Answer: 13 18

Example 4: Case study: transposed display · 4 marks · Case-basedHOTS / competency

A program displays a 2 by 3 array a = {{1, 2, 3}, {4, 5, 6}} like this:

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

(i) How many rows and columns does a have? (1)
(ii) Write the output. (2)
(iii) What happens if the statement System.out.println(a[2][0]); is executed? (1)

Try it first, then show the solutionSolution
  1. (i) 2 rows and 3 columns.
  2. (ii) j = 0 prints a[0][0] and a[1][0]: 1 4. j = 1 prints 2 5. j = 2 prints 3 6. The output is a 3 by 2 arrangement: 1 4 / 2 5 / 3 6.
  3. (iii) a has only rows 0 and 1, so a[2][0] throws ArrayIndexOutOfBoundsException at run time.

Answer: (i) 2 rows, 3 columns (ii) 1 4 / 2 5 / 3 6 (iii) run-time ArrayIndexOutOfBoundsException

Example 5: Accepting and displaying a matrix · 4 marks · Short answerHarder board level

Complete the code to accept a 2 by 2 matrix with Scanner in and display it in matrix format. Show the output when the input is 1 2 3 4.

Try it first, then show the solutionSolution
  1. int[][] m = new int[2][2]; and two nested loops reading m[i][j] = in.nextInt(); (2 marks)
  2. Two nested loops printing m[i][j] + " " with println() after the inner loop. (1 mark)
  3. The values fill row by row: first row 1 2, second row 3 4. (1 mark)

Answer: int[][] m = new int[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
m[i][j] = in.nextInt();
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
System.out.print(m[i][j] + " ");
System.out.println();
}
Output:
1 2
3 4

Where students lose marks in Double Dimensional Arrays: Row, Column and Diagonal Sums

The slips examiners see most often, and what to write instead.

  • Mistake: Not resetting the sum

    If s is declared outside the outer loop, each row adds to the previous total.

    Wrong working

    int s = 0; before the loops: row sums 6, 21, 45

    Right working

    int s = 0; inside the outer loop: 6, 15, 24

    Do this: Reset for each row or column.

    Usually costs: 1 to 2 marks

  • Mistake: Writing m[j][i] for a row traversal

    The first index is the row; swapping them gives the transpose.

    Wrong working

    row sum: s += m[j][i]

    Right working

    row sum: s += m[i][j]

    Do this: Row index first.

    Usually costs: 1 mark

  • Mistake: Using 3 as the last index of a 3 by 3 array

    Valid indices are 0 to 2.

    Wrong working

    m[3][3]

    Right working

    m[2][2]

    Do this: Last index = length - 1.

    Usually costs: 1 mark

Exam tips

  • Draw the matrix with its values and tick the elements you are adding.
  • Remember println() after the inner loop to get the matrix shape.
  • For the right diagonal use m[i][n - 1 - i].
  • State "array size" as rows x columns in your declaration.

Practice questions on Double Dimensional Arrays: Row, Column and Diagonal Sums

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    For int[][] x = new int[4][5]; the value of x.length is:
    (a) 5 (b) 4 (c) 20 (d) 9

    Hint

    Number of rows.

    Answer

    (b) 4

  2. Q2 · 1 mark · Very short answerBoard level

    What is the index of the last element of a 3 by 3 array?

    Hint

    Both indices start at 0.

    Answer

    [2][2]

  3. Q3 · 2 marks · Short answerHarder board level

    Write the output:

    int[][] b = {{1, 2}, {3, 4}};
    System.out.println(b[0][1] + b[1][0] + " " + b[0][0] * b[1][1]);
    Hint

    Multiply before the string joining; evaluate left to right.

    Answer

    5 4 (2 + 3 = 5 first, then a space, then 1 x 4 = 4)

  4. Q4 · 4 marks · Short answerHarder board level

    Write loops to display the array {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} in matrix format and to find the sum of the elements of its second column (index 1).

    Hint

    Column sum: one loop over i with j fixed at 1.

    Answer

    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++)
    System.out.print(m[i][j] + " ");
    System.out.println();
    }
    int s = 0;
    for (int i = 0; i < 3; i++)
    s += m[i][1];
    The sum is 2 + 5 + 8 = 15.

  5. Q5 · 1 mark · Assertion-reasonHOTS / competency

    Assertion (A): In a square matrix the elements of the left diagonal have equal row and column indices.
    Reason (R): The left diagonal elements are m[0][0], m[1][1], m[2][2] and so on, that is m[i][i].
    (a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.

    Hint

    Does R restate A with an example?

    Answer

    (a) Both are true and R explains A.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.What does m.length give for a 2D array?Show answer

    The number of rows.

  2. 2.What does m[0].length give?Show answer

    The number of columns.

  3. 3.Which elements form the left diagonal?Show answer

    m[i][i].

  4. 4.Which elements form the right diagonal of an n by n matrix?Show answer

    m[i][n - 1 - i].

  5. 5.What is the value of an unassigned int element?Show answer

    0

  6. 6.Which loop is outside for a column sum?Show answer

    The loop over the columns.

Double Dimensional Arrays: Row, Column and Diagonal Sums: frequently asked questions

Do diagonals exist for a non-square matrix?

The Class X programs use square matrices for diagonals, where rows equal columns.

How do I print a matrix in matrix format?

Use print() inside the inner loop and println() after it.

Is a 2D array an array of arrays?

Yes: each row is itself a one-dimensional array, which is why m[0].length gives the columns.