Skip to main content

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.



Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...