2D Array in Java
Java Programming

2D Array in Java

 

In Java, multidimensional arrays are also applied as arrays of arrays. 2D(two-dimension i.e tabular form) array is the generally used and simplest multi-dimensional array(like excel sheet(grids)). 2D arrays are represented in a row-columns([3][4]) form, and the terms “rows”[3]  and  “columns”[4] are used in computing.

Syntax….

Declaration – Syntax:

               Data_Type[][] Array_Name = new int[Row_Size][Cols_Size];

      For example: int[][] arr = new int[10][20];

  • Row_Size: Number of Row elements an array can store. In this example, Row_Size = 10, then the array will have ten rows.
  • Cols_Size: Number of Column elements an array can store. In this example, Cols_Size = 20, then the array will have 6 Columns.
  • int is the data type
  • arr is the name of the array

How to Initialize Array in Java

Initialization – Syntax:( Accessing Elements of Two-Dimensional Arrays)

array_name[row_index][column_index] = value;

        For example: arr[0][0] = 1;

The above example represents the element present in the first row and first column.

For example: int[][] arr_values = {{11, 12}, {13, 14}};
  • Data_type(int): It decides the type of elements it will accept. For illustration, If we want to store integer values( without decimal like 4,6,80), then we use (int) data type. If we want to store Float values(decimal values like 3.45,7.89f), then we use (float)data type. If we want to store character values(‘s’,’u’) then we use the (char) data type.
  • Array_Name(arr_values): This is the name(here arr_values is the name of the array) to give it to this Java two dimensional array(arr_values[][]).
  • How to initialize 2d array in Java

        arr_values [0][0]=11;

        arr_values [0][1]=12;

        arr_values [1][0]=13;

        arr_values [1][1]=14;

Two-Dimensional array in Java Example

class twodim
{
public static void main(String ana[])
{
int aa,bb;
int table[][]=new int[4][5];
for(aa=0;aa<4;++aa)
{
for(bb=0;bb<5;++bb)
{
table[aa][bb]=(aa*5)+bb+1;
System.out.print(table[aa][bb]+” “);
}
System.out.println();
}
}
}

Two Dimensional Array in Java

Explanation…

In this example, a class two dim (name of the class)is created. In the main method, two integer variables aa, and bb are declared, which are used for the two-dimensional array. Then, a two-dimensional array is created (int table[][]=new int[4][5]),  which is linked to a reference variable table of the array. After this, first for loop is used, where in variable aa is initialized to 0, and aa condition aa<4 is checked.

If the condition aa<4 is true(outer loop) , second for loop starts(inner loop). (for(aa=0;aa<4;++aa)

for(bb=0;bb<5;++bb))

In this for loop

for(bb=0;bb<5;++bb)

bb=0    initialization

bb<5   condition

++bb  increment

first, the value of bb is initialized to 0(bb=0)

Then, the condition bb<5 is checked.

If the condition(inner loop) bb<5 is true, table[aa][bb] is assigned a value that comes as a result of (aa*5)+bb+1;

Then the System.out.print(table[aa][bb]+” “); statement is used to print the value(aa is for rows and bb is for cols) that is assigned to table[aa][bb].

How to print a 2d array in Java

public class AccessTwoDimensionalArray
{
public static void main(String[] args)
{
int[][] StudMarks = { {12, 22, 33},{45, 65, 95},{442, 444, 446},{785, 786, 787}};

System.out.println("Element at StudMarks [0][0] = " + StudMarks [0][0]);
System.out.println("Element at StudMarks [0][1] = " + StudMarks [0][1]);
System.out.println("Element at StudMarks [0][2] = " + StudMarks [0][2]);
System.out.println("Element at StudMarks [1][0] = " + StudMarks [1][0]);
System.out.println("Element at StudMarks [1][1] = " + StudMarks [1][1]);
System.out.println("Element at StudMarks [1][2] = " + StudMarks [1][2]);
System.out.println("Element at StudMarks [2][0] = " + StudMarks [2][0]);
System.out.println("Element at StudMarks [2][1] = " + StudMarks [2][1]);
System.out.println("Element at StudMarks [2][2] = " + StudMarks [2][2]);
}

}


			

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment