Java Tutorial

Java 2D Arrays

A 2D array is an array of arrays — rows of columns. Index with grid[row][col] and nest loops to visit every cell.

Declare and initialize

Write int[][] grid for a table of ints. You can size it with new int[rows][cols] or fill it with a nested initializer. First index is the row; second is the column.

Example

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

Walk every cell

Use nested loops: outer over rows, inner over columns. grid.length is the number of rows;grid[r].length is the length of that row.

Example

public class Main {
  public static void main(String[] args) {
    int[][] grid = {
      {1, 2, 3},
      {4, 5, 6}
    };
    for (int r = 0; r < grid.length; r++) {
      for (int c = 0; c < grid[r].length; c++) {
        System.out.print(grid[r][c] + " ");
      }
      System.out.println();
    }
  }
}

for-each over rows

You can also for-each each row, then for-each the values in that row — handy when you do not need indices.

Example

public class Main {
  public static void main(String[] args) {
    int[][] grid = {{8, 1}, {3, 7}};
    int sum = 0;
    for (int[] row : grid) {
      for (int n : row) sum += n;
    }
    System.out.println(sum);   // 19
  }
}

Rows can have different lengths (a “jagged” array). Always use grid[r].length for that row instead of assuming every row matches grid[0].length.

Update a cell

Assignment uses the same indexing. Bounds still matter: going past a row or column length throwsArrayIndexOutOfBoundsException.

Example

public class Main {
  public static void main(String[] args) {
    int[][] scores = new int[2][3];
    scores[0][0] = 90;
    scores[1][2] = 85;
    System.out.println(scores[0][0] + " " + scores[1][2]);
  }
}

grid[r][c] — row first, then column. Mixing them up is a silent logic bug until a value looks wrong.

Try It Yourself

Exercise: Create a 3×3 int[][] filled with 1 on the diagonal and 0 elsewhere, then print it.

Show solution
int[][] id = new int[3][3];
for (int i = 0; i < 3; i++) id[i][i] = 1;
for (int[] row : id) {
  for (int n : row) System.out.print(n + " ");
  System.out.println();
}

Setting id[i][i] writes the main diagonal; other cells stay the default 0.

Key Takeaways

  • A 2D array is an array of rows; index with grid[row][col].
  • grid.length is rows; grid[r].length is that row’s length.
  • Nested loops (or nested for-each) visit every cell.
  • Rows may differ in length — do not assume a perfect rectangle.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Statistics

Sum a data matrix

A 2D array is rows of columns. Walk every cell to get a grand total of counts.

Example

public class Main {
  public static void main(String[] args) {
    int[][] grid = {{1, 2, 3}, {4, 5, 6}};
    int sum = 0;
    for (int[] row : grid) for (int n : row) sum += n;
    System.out.println(sum);
  }
}

FAQ: Java 2D Arrays

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java 2d arrays examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java 2d arrays in this Java Java lesson (Java 2D Arrays).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.