2D Array in JAVA | Java Programming by TechWithCode

 

Hello Everyone Welcome to TechWithCode, In the Series of Learning Java Programming, Today we will learn everything about 2D Array in java with source code. we write a simple program of the 2D array in java which helps us to learn easily. After reading this, there is no doubt remains in your mind about 2D Array in Java. So Let start...

Following are the topics which are covered in this article

  1. What is an Array?
  2. What is Two Dimensional Array?
  3. How to Declare Two Dimensional array?
  4. Example Program of 2D Array

1. What is an Array?

An array is a group of variables (called elements or components) containing values that all have the same type. Arrays are objects, so they’re considered reference types.

The elements of an array can be either primitive types or reference types

Declaration of an Array:

 Like other objects, arrays are created with keyword " new ". To create an array object, you specify the type of the array elements and the number of elements as part of an array-creation expression that uses keyword new.

Example:  
int[] arr = new int[12];

Other way to declare an array:

                                           int[] c; // declare the array variable

                                          c = new int[12]; // create the array; assign to array variable


2.What is Two Dimensional Array?

Multidimensional arrays with two dimensions are often used to represent tables of values with data arranged in rows and columns. 

To identify a particular table element, you specify two indices. By convention, the first identifies the element’s row and the second its column.

Arrays that require two indices to identify each element are called two-dimensional arrays.

3. Declaration of 2D Array:

                              int[][] arr = new int[3][4];



4. Example Program of 2D Array

Problem Statement:⤵️

Write a java program to practice:- 

(A) Use of single dimensional array 

(B) Use of two-dimensional array 

Java Program⤵️

import java.io.DataInputStream; 

public class SecondP { 

 public static void main(String args[]) { 

 int arr[]=new int[5];// DECLARATION OF 1D 

ARRAY 

 int arr2[][]=new int[3][3];// DECLARATION OF 2D 

ARRAY 

 int sum=0; 

System.out.println("ONE DIMENTIONAL ARRAY:----------

--------------->"); 

 DataInputStream in=new 

DataInputStream(System.in); 

 try {  System.out.println("Enter Five integers"); 

 for(int i=0;i<5;i++) 

 arr[i]=Integer.parseInt(in.readLine()); 

 }catch(Exception e) { } 

 for(int i=0;i<5;i++) 

 sum=sum+arr[i]; 

 System.out.println("Sum of above numbers = 

"+sum); 

System.out.println("\nTWO DIMENTIONAL ARRAY:------

------------------->"); 

 try { 

 System.out.println("Enter nine integers"); 

 for(int i=0;i<3;i++) 

 for(int j=0;j<3;j++) 

 arr2[i][j]=Integer.parseInt(in.readLine()); 

 }catch(Exception e) { } 

 System.out.println("Matrics format of given 

integer:-"); 

 for(int i=0;i<3;i++) { 

 System.out.println();  for(int j=0;j<3;j++)

 System.out.print(" "+arr2[i][j]);

 } 

 } 

}

The Output of Above code⤵️





No comments:

Powered by Blogger.