×

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array

Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection is the most accessible type of complex exhibit. A two-layered cluster should be visible as a variety of the one-layered display for more straightforward comprehension. A two-layered exhibition is proclaimed with two aspects likewise alluded to as their limits.

Syntax for 2-Dimensional Array

data type[][] array name = new data type[x][y];

Example:

int [][] ar = new int [5][10];

How to Assign Values to a Multi-dimensional Array:

Syntax:

data_type[][] array_name = {{valueR1C1, valueR1C2, ....},
                            {valueR2C1, valueR2C2, ....}
                           };

Example:

//simple program for 2-d arrays
// Allocation of values in a 2-dimensional array
class MDA {        //creating the class with the name MDA
    public static void main(String[] args)
    {
  
        int[][] arr = new int[5][10];   //creating the 2-D array with 5 rows and 10 columns
        arr[0][0] = 5;       //assigning the value 5 to the 2-D array at the 0th row and 0th //column value
        System.out.println(“printing the 2-D array elements”);
        System.out.println("arr[0][0] = " + arr[0][0]);
    }
}

Output:

Printing the 2-D array elements
arr[0][0] = 5

Example 1:

//simple program for 2-d arrays
// Allocation of values in a 2-dimensional array
class MDA{           //creating the class with the name MDA
    public static void main(String[] args)
    {
  
        int[][] arr = { { 10, 20 }, { 30, 40 } }; //creating the 2-D array with 2 rows and 2 //columns
        System. out.println(“Printing the 2-D array elements”);
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)  //printing the 2-D array elements
                System.out.println("arr[" + i + "][" + j+ "] = " + arr[i][j]);
    }
}

Output:

Printing the 2-D array elements
arr[0][0] = 10
arr[0][1] = 20
arr[1][0] = 30
arr[1][1] = 40

How to Determine the Upper Bound of a Two-Dimensional Array

A variety of Exhibits in java is a variety of a one-layered clusters. Each line of a two-layered exhibit has a length field that holds the number of sections.

Syntax

int row bound = arr name.length;       //arr means array
int column bound = arr name[0].length;

Example:

//simple program to determine the Dimension of an Array
class MDA {        //creating a class with the name MDA
    public static void main(String args[])
    {
        int[][] arr = new int[8][15];          //creating the 2-D array of integer type with 8 //rows and 15 columns
        int row_bound = arr.length;       //declaring a integer variable and assigning the //length of the array
        int column_bound = arr[0].length;      //declaring an integer variable and //assigning the length of the 0th index array
         System.out.println(“printing the dimensions of the given array”);
        System. out.println("Dimension 1: " + row_bound);  //prints the dimension of the //rows
        System. out.println("Dimension 2: " + column_bound);     //prints the dimension //of the columns
    }
}

Output:

Printing the dimensions of the given array
Dimension 1: 8
Dimension 2: 15

Example 1:

//simple program to determine the Dimension of an Array
class MDA {       //creating a class with the name MDA
    public static void main(String args[])
    {
        int[][] ar = { { 10, 20 },
                        { 30 },
                        { 40 },
                        { 50, 60, 70, 80 },
                        { 90, 100, 110, 120, 130, 140, 150 } };      //declaring the array of the //integer type with different dimensions at different index values
        int row_bound = ar.length;      //declaring a integer variable and assigning the //length of the given array
        int column_bound1 = ar[0].length;     //declaring an integer variable and //assigning the length of the 0th index of the given array
        int column_bound2 = ar[1].length;      //declaring an integer variable and //assigning the length of the 1st index of the given array
        int column_bound3 = ar[2].length;      //declaring an integer variable and //assigning the length of the 2nd index of the given array
         int column_bound4 = ar[3].length;      //declaring an integer variable and //assigning the length of the 3rd index of the given array
        int column_bound5 = ar[4].length;       //declaring an integer variable and //assigning the length of the 4th index of the given array
        System.out.println("printing the dimensions of the given array”);
        System. out.println("The Dimension 1 is: " + row_bound);
        System. out.println("The Dimension 2 is: " + column_bound1);
        System. out.println("The Dimension 3 is: " + column_bound2);
        System. out.println("The Dimension 4 is: " + column_bound3);
        System. out.println("The Dimension 5 is: " + column_bound4);
        System. out.println("The Dimension 6 is: " + column_bound5);
    }
}

Output:

Printing the dimensions of the given array
The Dimension 1 is: 5
The Dimension 2 is: 2
The Dimension 3 is: 1
The Dimension 4 is: 1
The Dimension 5 is: 4
The Dimension 6 is: 7

Related Topics

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

Sorting Program in Java

Sorting Program in Java: The sorting program in Java is used to sort arrays either in ascending or descending order.  There are two predefined methods available in Java that are...

6 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Java Generics Questions

Introduction We'll walk through a few real-world examples of interview questions and responses for Java generics in this article. Java 5 saw the debut of the fundamental idea of generics. Due to...

9 minutes read.