MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Unit 0: The First Program
  • Unit 1: Using Objects and Methods
  • Unit 2: Selection and Iteration
  • Unit 3: Class Creation
  • Unit 4: Data Collections
    • Part 1: Arrays
    • Part 2: ArrayList
    • Part 3: 2D Arrays and Recursion
      • 2D Array Creation and Access
      • 2D Array Traversals
      • Implementing 2D Array Algorithms
      • Searching Algorithms
      • Sorting Algorithms
      • Recursion
      • Recursive Searching and Sorting
      • Unit 4 Part 3 Slides

Unit 4.12: Traversing 2D Arrays

Nested Loop Traversal

To visit every element in a 2D array, nested iteration statements (loops) are used. The order in which elements are accessed depends on which dimension is controlled by the outer loop.

Row-Major Order
  • Traversal occurs across each row. The outer loop iterates through the rows, and the inner loop iterates through the columns.

Task: Printing all elements of a 2D array in row-major order.

// In the main method ...
int[][] grid = {
    {1, 2, 3},
    {4, 5, 6}
};

// Row-Major: Outer loop is Rows, Inner is Columns
for (int r = 0; r < grid.length; r++) {
    for (int c = 0; c < grid[0].length; c++) {
        System.out.print(grid[r][c] + " ");
    }
    System.out.println(); // New line after each row
}
Column-Major Order
  • Traversal occurs down each column. The outer loop iterates through the columns, and the inner loop iterates through the rows.

Task: Printing all elements of a 2D array in column-major order.

// In the main method ...
int[][] grid = {
    {1, 2, 3},
    {4, 5, 6}
};

// Column-Major: Outer loop is Columns, Inner is Rows
for (int c = 0; c < grid[0].length; c++) {
    for (int r = 0; r < grid.length; r++) {
        System.out.print(grid[r][c] + " ");
    }
    System.out.println(); // New line after each column
}
2D Array Traversal Diagram

Enhanced for Loop Traversal

You can also use nested enhanced for loops to traverse a 2D array. This is often cleaner but only allows for row-major access and does not provide index variables.

Nested Enhanced Loops
  • The outer loop variable must be a 1D array (representing a single row). The inner loop variable must be the same data type as the elements stored in that 1D array.

Task: Traversing a 2D array using nested enhanced for loops.

// In the main method ...
int[][] grid = {{1, 2, 3}, {4, 5, 6}};

// Outer loop gets each row (1D array)
for (int[] row : grid) {
    // Inner loop gets each element in that row
    for (int val : row) {
        System.out.print(val + " ");
    }
}
Value Immutability
  • Assigning a new value to the enhanced for loop variable (the inner element variable) does not change the value stored in the 2D array.

Task: Demonstrating that modifying an enhanced for loop variable does not affect the 2D array.

// In the main method ...
for (int[] row : grid) {
    for (int val : row) {
        val = 0; // This ONLY changes the local variable 'val', NOT the array
    }
}
// The grid still contains its original values.
Privacy Policy | Terms & Conditions