2d Array Java | Multidimensional Array example | Matrix

As per Array’s definition, it holds the same type of variables it’s also called One Dimensional Array. 2d Array Java is 2 Dimensional Array which stores data in Row and column or can say matrix formate. This matric or Array can have int, Strings, and other Java data types.

Pre-requested must read the – Array Initialization | Types with Examples Tutorial 

2d Array Java Multidimensional Array example Matrix

This Tutorial you will learn all about Java Multidimensional Array with multiple examples.

Syntax

A syntax of 2d Array with “3 column” and “3 rows” and How to 2d array declaration in Java. This Array can hold up to 9 elements.

Thinking about Array indexing? It’s same as 2d Array start with 0, so the first element index is m[0][0].

int[][] m = new int[3][3];
Syntax 2d two dimensional array in java example

Example of 2d array java

Let’s start with an example of “Print 2d array java” for better understanding.

Here is an example of how to declaring and initializing a 2D array, also printing the 2D Array. For Printing a Multidimensional Array we are using a Java for-loop.

public class Java2DArray {

    public static void main(String args[]) {
        //declaring and initializing 2D array
        int arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        //printing 2D array
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output: 

1 2 3
4 5 6
7 8 9

Two-dimensional string array in java example

It’s very easy to make a 2d String Array in Java. Here is an example of Dimensional string array code.

public class Java2DArray {

    public static void main(String args[]) {
        //declaring and initializing 2D array
        String arr[][] = {{"A","B","C"}, {"P","Q","R" }, {"X","Y","Z"}};
        //printing 2D array
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output screenshot:

Print Two-dimensional string array in java example 2d

Get The 2D Array Java Length

Each component of the 2D Array is an array in itself, and the length of each row is also different.

public class Java2DArray {

    public static void main(String args[]) {
        //declaring and initializing 2D array
        int arr[][] = {{1,2}, {4, 5}, {7, 8, 9}};

        System.out.println("Length of row 1: " + arr[0].length);
        System.out.println("Length of row 2: " + arr[1].length);
        System.out.println("Length of row 3: " + arr[2].length);

    }
}

Output: Length of row 1: 2
Length of row 2: 2
Length of row 3: 3

QA: How to get a single Item(element) of Two Dimensional Array in Java?

It’s easy to get a single item from 2D Array in Java, just pass the index like this – array[index][index].

Here is an example of how to select one or many items by indexing in Java Multidimensional Array.

String arr[][] = {{"A", "B", "C"}, {"P", "Q", "R"}, {"X", "Y", "Z"}};
        //printing single elements

        System.out.print(arr[1][1]);

Output: Q

More understanding sees below diagram of  Multidimensional Array.

How to get an single Item of Two Dimensional Array in Java?

Do comment in below comment section if any doubt and suggestion.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Two Dimensional Array in Java examples (Print 2d Array) are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.