Back to Arrays
Q2Question 2 of 4

What are multi-dimensional and jagged arrays?

Key Concepts
  • A 2D array is an array of arrays: int[][] matrix = new int[3][4]
  • A jagged array has rows of different lengths: rows[0] = new int[3], rows[1] = new int[5]
  • Java has no true multi-dimensional arrays – just arrays of arrays
  • Access: matrix[row][col], iterate with nested loops
  • Use cases: matrices, grids, transaction tables by month and day

Definition

A 2D array is an array of arrays. A jagged array has rows of different lengths. Java has no true multi-dimensional arrays.

Multi-dimensional arrays:

  • 2D array: int[][] matrix = new int[3][4] – 3 rows, 4 columns. Each row is a separate array.
  • Jagged array: Rows have different lengths. int[][] jagged = new int[3][]; then jagged[0] = new int[2]; jagged[1] = new int[5];.
  • Internally: An array of references to row arrays. Not a contiguous 2D block.

A 2D array is a table of bank lockers with uniform rows. A jagged array is a table where each row has a different number of lockers – row 1 has 3, row 2 has 5.

Understanding the Concepts

2D array

JAVA
int[][] monthlyTxns = new int[12][31];  // 12 months, up to 31 days
monthlyTxns[0][0] = 5;  // January 1st: 5 transactions
monthlyTxns[11][30] = 3; // December 31st: 3 transactions

for (int month = 0; month < monthlyTxns.length; month++) {
    for (int day = 0; day < monthlyTxns[month].length; day++) {
        // process monthlyTxns[month][day]
    }
}

Jagged array

JAVA
// Quarterly report: Q1=3 months, Q2=3, Q3=3, Q4=3 but different data lengths
BigDecimal[][] quarterly = new BigDecimal[4][];
quarterly[0] = new BigDecimal[]{new BigDecimal("100000"), new BigDecimal("120000")};
quarterly[1] = new BigDecimal[]{new BigDecimal("90000")};
// quarterly[0].length = 2, quarterly[1].length = 1

What should a senior engineer know?

  • Memory layout: Each row is a separate object on the heap. Not cache-friendly for large matrices.
  • For matrices in finance, consider dedicated libraries (Apache Commons Math, ojAlgo) that use flat arrays internally for performance.

What are common mistakes?

A common mistake is not understanding the concept fully before using it in production banking applications.

Wrong approach:

JAVA
package com.bank.service;

public class TransferService {
    public void transfer(String fromAccount, String toAccount, double amount) {
        // WRONG: using double for money
        double balance = getBalance(fromAccount);
        balance = balance - amount;
    }
    private double getBalance(String id) { return 10000.0; }
}

Correct approach:

JAVA
package com.bank.service;

import java.math.BigDecimal;

public class TransferService {
    public void transfer(String fromAccount, String toAccount, BigDecimal amount) {
        // CORRECT: using BigDecimal for money
        BigDecimal balance = getBalance(fromAccount);
        balance = balance.subtract(amount);
    }
    private BigDecimal getBalance(String id) { return new BigDecimal("10000.00"); }
}

Always use BigDecimal for monetary calculations in banking systems to avoid floating-point precision errors.

What are the edge cases?

Edge cases can cause unexpected behavior in banking applications. Here are some to watch for.

JAVA
package com.bank.edge;

public class EdgeCaseDemo {
    public static void main(String[] args) {
        // Edge case 1: null handling
        String accountId = null;
        try {
            System.out.println(accountId.length());
        } catch (NullPointerException e) {
            System.out.println("Null account ID");
        }

        // Edge case 2: empty vs null
        String emptyId = "";
        System.out.println(emptyId.isEmpty());  // true
        System.out.println(emptyId == null);     // false

        // Edge case 3: boundary values
        java.math.BigDecimal zero = java.math.BigDecimal.ZERO;
        java.math.BigDecimal negativeBalance = new java.math.BigDecimal("-0.01");
        System.out.println(zero.compareTo(negativeBalance) > 0);  // true
    }
}
TEXT
Null account ID
true
false
true

Always test with null values, empty strings, boundary values, and negative amounts in banking applications.

What are common interview follow-up questions?

Interviewers often follow up with these related questions:

  • How does this concept apply to a high-throughput banking transaction system?
  • What are the performance implications in a production environment?
  • How would you debug issues related to this concept?
  • What tools would you use to monitor this in production?
  • How does this behave differently in Java 8 vs Java 17+?

When answering follow-up questions, always relate your answer back to practical experience. For example, at National Bank of India, Rajesh debugged a production issue related to this concept by using JFR recordings and heap dumps. Providing concrete examples from banking systems shows the interviewer that you understand both the theory and the practice.

Visual Overview

DIAGRAM
  int[][] matrix = new int[3][4];
  → matrix[0] → [_, _, _, _]   (4 elements)
  → matrix[1] → [_, _, _, _]
  → matrix[2] → [_, _, _, _]

  Jagged: int[][] jagged = new int[3][];
  → jagged[0] = new int[2]  → [_, _]
  → jagged[1] = new int[5]  → [_, _, _, _, _]
  → jagged[2] = new int[1]  → [_]

Key Takeaway

A 2D array in Java is an array of arrays. Each row is a separate object. A jagged array has rows of different lengths. Access with matrix[row][col] and iterate with nested loops.

One sentence to remember: Java 2D arrays are arrays of arrays – each row is independent, so rows can have different lengths (jagged).

---

Share
Use ← → arrow keys to navigate