×

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

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

StringBuilder in Java

StringBuilder in Java Java StringBuilder class is introduced since JDK 1.5. The StringBuilder class is mainly used to create modifiable or mutable strings. Note that the StringBuilder class is not synchronized....

7 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.