0Pricing
Java Academy · Lesson

2D Array Basics & Nested Iteration

Understand 2D arrays (jagged allowed), rows vs columns, m.length and m[0].length, and nested for iteration.

2D Array Basics & Nested Iteration is a free Java Academy lesson on CoddyKit — lesson 1 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is 2D?

2D array is an array of arrays. It may be rectangular or jagged (rows can have different lengths).

Declare & Create

Declare and create:

public class Main {
  public static void main(String[] args) {
    // Way 1: Create a 2D array with fixed size
    int[][] a = new int[2][3]; 
    // 2 rows, 3 columns → default values = 0
    // [
    //   [0, 0, 0],
    //   [0, 0, 0]
    // ]

    // Way 2: Create and initialize directly
    int[][] b = {
      {1, 2, 3},
      {4, 5, 6}
    };
    // [
    //   [1, 2, 3],
    //   [4, 5, 6]
    // ]

    // Print array a
    System.out.println("Array a:");
    for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < a[i].length; j++) {
        System.out.print(a[i][j] + " ");
      }
      System.out.println();
    }

    // Print array b
    System.out.println("Array b:");
    for (int i = 0; i < b.length; i++) {
      for (int j = 0; j < b[i].length; j++) {
        System.out.print(b[i][j] + " ");
      }
      System.out.println();
    }
  }
}

Rows vs Columns

Rows: m.length. Columns (first row): m[0].length. Beware of jagged rows—check each row's length.

Nested Iteration

Use nested loops to visit all items:

public class Main {
  public static void main(String[] args) {
    // Example 2D array (matrix)
    int[][] m = {
      {1, 2, 3},
      {4, 5, 6}
    };

    // Loop over rows
    for (int r = 0; r < m.length; r = r + 1) {
      // Loop over columns in the current row
      for (int c = 0; c < m[r].length; c = c + 1) {
        int value = m[r][c]; // get element at row r, column c
        System.out.print(value + " "); // work: here we print
      }
      System.out.println(); // move to next row
    }
  }
}

Row/Col Sums

Compute sums:

public class Main {
  public static void main(String[] args) {
    // Example 2D array (matrix)
    int[][] m = {
      {1, 2, 3},
      {4, 5, 6},
      {7, 8, 9}
    };

    // Sum of row 1 (second row, index = 1)
    int rowSum = 0;
    for (int c = 0; c < m[1].length; c = c + 1) {
      rowSum = rowSum + m[1][c];
    }
    System.out.println("Sum of row 1 = " + rowSum);

    // Sum of column 2 (third column, index = 2)
    int colSum = 0;
    for (int r = 0; r < m.length; r = r + 1) {
      colSum = colSum + m[r][2];
    }
    System.out.println("Sum of column 2 = " + colSum);
  }
}

2D Demo

Run it: See totals and per-row sums printed.

public class Main {
  static int total(int[][] m) {
    int s = 0;
    for (int r = 0; r < m.length; r = r + 1) {
      for (int c = 0; c < m[r].length; c = c + 1) {
        s = s + m[r][c];
      }
    }
    return s;
  }

  static int[] rowSums(int[][] m) {
    int[] sums = new int[m.length];
    for (int r = 0; r < m.length; r = r + 1) {
      int s = 0;
      for (int c = 0; c < m[r].length; c = c + 1) s = s + m[r][c];
      sums[r] = s;
    }
    return sums;
  }

  static String join(int[] a) {
    String s = "";
    for (int i = 0; i < a.length; i = i + 1) {
      s = s + a[i] + (i + 1 < a.length ? " " : "");
    }
    return s;
  }

  public static void main(String[] args) {
    int[][] m = {
      {1, 2, 3},
      {4, 5, 6}
    };

    System.out.println("rows = " + m.length);
    System.out.println("cols of row 0 = " + m[0].length);
    System.out.println("total = " + total(m));

    int[] rs = rowSums(m);
    System.out.println("row sums = " + join(rs));
  }
}

Columns Count

Quick check: For int[][] m = {{1,2,3},{4,5,6}}, how many columns are in the first row?

Recap & Next

Recap: You declared 2D arrays, iterated with nested loops, and computed totals.

Next: Practice with jagged arrays and formatting tables.

Frequently asked questions

Is the “2D Array Basics & Nested Iteration” lesson free?

Yes — the full text of “2D Array Basics & Nested Iteration” is free to read here on the web, and the Java Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “2D Array Basics & Nested Iteration”?

Understand 2D arrays (jagged allowed), rows vs columns, m.length and m[0].length, and nested for iteration. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 6, so you can start here or from the beginning and move at your own pace.

How long does the “2D Array Basics & Nested Iteration” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. 2D Array Basics & Nested Iteration
  2. Jagged Arrays & Table Formatting
  3. Row/Column Maxima, Transpose & Formatting
  4. 2D Mini-Project: Gradebook
  5. 2D Utilities as a Helper Class (Refactor)
  6. 2D Data Cleaning & Validation
← Back to Java Academy