Multidimensional Arrays in Java — Java Technical Interview Questions & Answers

Multidimensional Arrays in Java

Multidimensional Arrays in Java

  • A 2D array is an array of arrays. int[][] m = {{1, 2}, {3, 4}}; m[1][0] is row 1, column 0 → 3. Indexes start at 0.
  • Rows can have different lengths (jagged): new int[3][]; then m[0] = new int[2]; m[1] = new int[5];. Nested loops visit every cell: for i, for j.
  • Length: m.length is rows. m[i].length is columns in that row. Out of range still throws ArrayIndexOutOfBoundsException.
  • Let's take this on the board with one tiny Main class — no extra files. Multidimensional Arrays in Java — m[1][0] is row 1, column 0 → 3. Start from main, go line by line, and stop at each print. That output is the proof for Multidimensional Arrays.
  index →  0    1    2
  array → [10,  20,  30]
           ↑         ↑
         first   length-1

Technical Interview Tip: Say array of arrays. Show m[1][0]. Mention jagged if asked.

Code Examples for Multidimensional Arrays in Java

Example

public class Main {
  public static void main(String[] args) {
    int[][] m = {{1, 2}, {3, 4}};
    System.out.println(m[1][0]); // 3
  }
}

Multidimensional Arrays in Java — m[1][0] is row 1, column 0 → 3.

How to Explain Multidimensional Arrays in Java to an Interviewer

What is a 2D array?

Array of arrays m[row][col]

What is jagged?

Rows of different length