Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Multidimensional Array

In Java, a multidimensional array is an array of arrays, meaning that it can have multiple levels of arrays. The most common type of multidimensional array is the two-dimensional array (2D array), which can be thought of as a table with rows and columns. This tutorial will cover the basics of creating and using multidimensional arrays in Java.

  • Declare and create a two-dimensional array:

To create a two-dimensional array, you need to specify the number of rows and columns within two pairs of square brackets ([][]).

int[][] matrix = new int[3][4]; // Creates a 3x4 (3 rows, 4 columns) integer array
  • Initialize a two-dimensional array:

You can initialize a two-dimensional array by specifying the values for each row within curly braces ({}).

int[][] matrix = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
  • Access elements in a two-dimensional array:

To access elements in a two-dimensional array, use two indices within square brackets, where the first index represents the row, and the second index represents the column.

int value = matrix[1][2]; // Retrieves the value at row 1, column 2 (7 in this example)
  • Modify elements in a two-dimensional array:

You can modify the elements in a two-dimensional array using assignment statements and indices.

matrix[0][0] = 42; // Changes the value at row 0, column 0 to 42
  • Iterate over a two-dimensional array:

You can use nested loops to iterate over a two-dimensional array. The outer loop iterates over the rows, while the inner loop iterates over the columns.

for (int row = 0; row < matrix.length; row++) {
    for (int col = 0; col < matrix[row].length; col++) {
        System.out.print(matrix[row][col] + " ");
    }
    System.out.println();
}
  • Declare and create a jagged array:

A jagged array is a type of multidimensional array in which the length of each row can be different. To create a jagged array, you only need to specify the number of rows when you create the array. Then, you can create each row with a different length.

int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[4];
jaggedArray[1] = new int[2];
jaggedArray[2] = new int[3];
  • Initialize a jagged array:

You can initialize a jagged array by specifying the values for each row within curly braces ({}). Each row can have a different number of values.

int[][] jaggedArray = {
    {1, 2, 3, 4},
    {5, 6},
    {7, 8, 9}
};

This tutorial introduced the basics of creating and using multidimensional arrays in Java, focusing on two-dimensional arrays and jagged arrays. You can use the same concepts to create arrays with more dimensions, although they are less common in practice.

  1. Accessing elements in a 2D array in Java:

    • Elements in a 2D array are accessed using row and column indices.
    int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int element = twoDArray[1][2]; // Accesses the element at row 1, column 2 (value 6)
    
  2. Working with 3D arrays in Java:

    • Similarly, 3D arrays extend the concept to three dimensions (rows, columns, and depth).
    int[][][] threeDArray = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
    int element = threeDArray[1][0][1]; // Accesses the element at layer 1, row 0, column 1 (value 6)
    
  3. Iterating through multidimensional arrays in Java:

    • Nested loops are commonly used for iterating through multidimensional arrays.
    for (int i = 0; i < twoDArray.length; i++) {
        for (int j = 0; j < twoDArray[i].length; j++) {
            System.out.print(twoDArray[i][j] + " ");
        }
        System.out.println(); // Move to the next row
    }
    
  4. Common operations and manipulations on multidimensional arrays:

    • Operations include finding the length of rows and columns, copying arrays, and transposing.
    int numRows = twoDArray.length;
    int numCols = twoDArray[0].length;
    
    int[][] copyArray = Arrays.copyOf(twoDArray, twoDArray.length);
    
  5. Converting between 1D and 2D arrays in Java:

    • You can flatten a 2D array to a 1D array and vice versa.
    int[] oneDArray = twoDArray[1];
    int[][] newTwoDArray = {oneDArray, {10, 11, 12}};
    
  6. Initializing and populating a jagged array in Java:

    • A jagged array is an array of arrays with varying lengths.
    int[][] jaggedArray = new int[3][];
    jaggedArray[0] = new int[]{1, 2, 3};
    jaggedArray[1] = new int[]{4, 5};
    jaggedArray[2] = new int[]{6, 7, 8, 9};