yt

Header Ads

Chapter -13 Array || 2-D or Dimensional || Explain in Details

Learn  2-D Arrays with Example 

Introduction 2-Dimensional or Multidimensional: 

  • Multidimensional Arrays can be defined in simple words as array of arrays. Data in multidimensional arrays are stored in tabular form (in row and Column  major order).
  • Multi-Dimensional arrays represent 2D, 3D.....arrays, which are combination of several earlier types of arrays.
  • For Example 2D '[] [] 'arrays is combination of two or more 1D arrays.
  • Similarly 3D '[] [] []', arrays is a combination of two or more 2D arrays.
  • A two dimensional arrays represents several rows and columns of data.
  • For Example, the marks obtained by a group of students in five different subjects can be represented  by a 2-D arrays.
  • If we write the marks of three student as.

50,60,55,67,70

62,65,70,47,85

75,87,98,78,45

The preceding marks represent a 2D arrays since it got 3 rows(no. of students) and in each row 5 columns(no. of subjects). There are totally 3*5= 15 elements .We can take the first rows itself as a 1D arrays. Similarly the second rows is a 1D and the third rows is another 1D arrays.  Hence the preceding  2D arrays three 1D arrays within it.

We can declared a 2D arrays:

int  marks[] [] = { {50,60,55,67,70},  {62,65,70,47,85}, {75,87,98,78,45} };

So We can Say that 2-D or Multi-D arrays are same, here now discussed on most important points.

Multi- Dimensional Arrays:

  • It is collection of data of same datatype.
  • It is used to store group of data simultaneously.
  • It can store data of same data type means an integer array can store only integer value, character arrays can store only character value used so on. 
  • We can’t fetch data from array directly therefore we use index point.
  • The index of arrays always start with 0.
  • Index value is always an integer number.
  • Arrays may be of any data type like int, char, float, etc.

Syntax: 2-D Arrays Diagram

·        Here Now:

  • ·        Arr is the name of arrays.
  • ·        int is the data type of array.
  • ·        Size of arrays is 3*3 means we can store maximum 9 values in this arrays.
  •  So this is called 3D arrays . 


Step 1 Declaration- Syntax: 
datatype array_name[row_index][column_index] = value;
            

Step 1 Initialization -Syntax:

int arr[] []=  {{ 45,89,65},{49,36,74}, {96,54,47)};



Step 2 Initialization -Syntax:

datatype array_name[row_index][column_index] = new datatype array_name[row_index][column_index] ; 

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

arr[0] [0]= 45;

arr[0] [1]= 89;

arr[0] [2]=65;

arr[1] [0]=49;

arr[1] [1]=36;

arr[1] [2]=74;

arr[2] [0]=96;

arr[2] [1]=54;

arr[2] [2]=47;

2- D Arrays Example :- 

Example 1: Print all element using step 1:


class Arr2D{
    public static void main(String arg[]){
        int arr[] []=  {{ 45,89,65},{49,36,74}, {96,54,47}};
        System.out.println("value of arr[0][0]: " +arr[0][0]);
        System.out.println("value of arr[0][1]: " +arr[0][1]);
        System.out.println("value of arr[0][2]: " +arr[0][2]);
        System.out.println("value of arr[1][0]: " +arr[1][0]);
        System.out.println("value of arr[1][1]: " +arr[1][1]);
        System.out.println("value of arr[1][2]: " +arr[1][2]);
        System.out.println("value of arr[2][0]: " +arr[2][0]);
        System.out.println("value of arr[2][1]: " +arr[2][1]);
        System.out.println("value of arr[2][2]: " +arr[2][2]);
    }
}




!!!!!!!   Output  !!!!!



value of arr[0][0]: 45
value of arr[0][1]: 89
value of arr[0][2]: 65
value of arr[1][0]: 49
value of arr[1][1]: 36
value of arr[1][2]: 74
value of arr[2][0]: 96
value of arr[2][1]: 54
value of arr[2][2]: 47



Example 1.1 Print all elements using loop throw.


class Arr2D1{
    public static void main(String arg[]){
        int arr[][]=  {{ 45,89,65},{49,36,74}, {96,54,47}};
        for(int i=0; i<=2; i++){
            for(int j=0; j<=2; j++){
            System.out.println("Value of arr["+i+"] ["+j+"]="+arr[i][j]); // for new line
            System.out.print("Value of arr["+i+"] ["+j+"]="+arr[i][j]);
            
            }
        }
    }
}


!!!!!!!   Output  !!!!!


Value of arr[0] [0]=45
Value of arr[0] [1]=89
Value of arr[0] [2]=65
Value of arr[1] [0]=49
Value of arr[1] [1]=36
Value of arr[1] [2]=74
Value of arr[2] [0]=96
Value of arr[2] [1]=54
Value of arr[2] [2]=47


Example 1.2 print Question in tabular format. 


class Arr2D2{
    public static void main(String arg[]){
        int arr[][]=  {{ 45,89,65},{49,36,74}, {96,54,47}};
        for(int i=0; i<=2; i++){
            for(int j=0; j<=2; j++){
            System.out.print(arr[i][j]+" ");
            }
            System.out.println("  ");
        }
    }
}


!!!!!!!   Output  !!!!!


45 89 65  
49 36 74
96 54 47


Example 1.3 print Question in tabular format 2*3.


class Arr2D3{
    public static void main(String arg[]){
        int arr[][]=  {{ 45,89,65},{49,36,89}};
        for(int i=0; i<2; i++){
            for(int j=0; j<=2; j++){
            System.out.print(arr[i][j]+" ");
            }
            System.out.println("  ");
        }
    }
}



!!!!!!!   Output  !!!!!


45 89 65
49 36 89


Example 1.4 : Print Question in tabular from but Input value by User.


import java.util.Scanner;
class Arr2D4{
    public static void main(String arg[]){
        Scanner sc= new Scanner (System.in);
        int ar[][]= new int [3] [5];
        System.out.println("Enter 15 value one by one:");
        // taking input by user
        int i, j;
        for(i=0; i<3; i++)
            for( j=0; j<5; j++)
            ar[i][j]= sc.nextInt();
            // printing element of array
            System.out.println("All Given Element  is ");
            for(i=0; i<3; i++){
                for(j=0; j<5; j++){
                System.out.print(ar[i][j]+" ");
            }
            System.out.println(" ");
        }
    }
}


!!!!!!!   Output  !!!!!


Enter 15 value one by one:
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

All Given Value is:-
10 11 12 13 14  
15 16 17 18 19
20 21 22 23 24


So Guys I hope all example is useful for your practice. Plz Don't forget follow me.



No comments

Theme images by Dizzo. Powered by Blogger.