Syntax For Creating A Two-Dimensional Array In Java

Here we will discuss Syntax For Creating A Two-Dimensional Array In Java where we need to provide two square brackets before the array variable name and just after the data type in java where it was somewhat different in the case of C and C++. So let’s see how to initialize a two -dimensional array with some examples.

Syntax

 

Two Dimensional Array

Two -dimensional array is nothing but a metric where we have rows and columns to store the data. where we have to enter the data in a way that it should satisfy the R*C row multiply column criteria and the number of elements it can accommodate is R*C.

Syntax Of Two Dimensional Array

we have to specify the size, data type, and variable name in two -dimensional array to initialize for the same following the syntax given below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<data Type> [][] <Variable name> = new <Data type> [n][m]
where n and m are the size of rows and columns
for example
int[][] arr = new int[3][3];
<data Type> [][] <Variable name> = new <Data type> [n][m] where n and m are the size of rows and columns for example int[][] arr = new int[3][3];
<data Type> [][] <Variable name> = new <Data type> [n][m]
where n and m are the size of rows  and columns 
for example
int[][] arr = new int[3][3];

In the given example the metric of 3X3 will be created that we call as two -dimensional array

We required a quadratic complexity that is O(n*m) for entering the value in it, Iterating the elements, and even searching

Let’s take an example to explain it in a better way

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class ABC {
public static void main(String[] args)
{
int rows = 80, columns = 5;
int[][] marks = new int[rows][columns];
// initializing the array elements using for loop
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
marks[i][j] = i + j;
}
}
// printing the first three rows of marks array
System.out.println("First three rows are: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < columns; j++) {
System.out.printf(marks[i][j] + " ");
}
System.out.println();
}
}
}
class ABC { public static void main(String[] args) { int rows = 80, columns = 5; int[][] marks = new int[rows][columns]; // initializing the array elements using for loop for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { marks[i][j] = i + j; } } // printing the first three rows of marks array System.out.println("First three rows are: "); for (int i = 0; i < 3; i++) { for (int j = 0; j < columns; j++) { System.out.printf(marks[i][j] + " "); } System.out.println(); } } }
class ABC {
    public static void main(String[] args)
    {
        int rows = 80, columns = 5;
        int[][] marks = new int[rows][columns];
  
        // initializing the array elements using for loop
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                marks[i][j] = i + j;
            }
        }
  
        // printing the first three rows of marks array
        System.out.println("First three rows are: ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.printf(marks[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Here we have used two for loops for accepting the values and similarly 2 for loops for printing the values which imply that the complexity of the program will be m*n which is quadratic.

 

 

Learn more about Syntax For Creating A Two-Dimensional Array In Java at: Two Dimensional Array

And also visit Java Tutorials to solve more problems related to array and java and also visit How To Reverse A String In Python?for more solutions of python and other programming languages.

Leave a Comment

%d bloggers like this:
Python4U
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.