Day 20 : Double Dimensional Array

 Double Dimensional Array

In Java 2D Array are used in those situations where we have to stored multiple groups of data.
For Example : 
    Suppose we want to record marks of three student, where each student has five subject. In this case we have two solution -

We can declare Three 1D array each of size 5 But the code will become difficult because we will have to handle 3 Arrays individually. 
A better solution would be to take 2D array of three rows and five column. The benefit will be we will have to handle 1 array which can be handle using only 1 Loop.

Type of 2D Array

In Java we have two types of 2D Array.
  1. Rectangular  2D Array
  2. Jagged 2D Array

Rectangular 2D Array

These are those 2D array where every row contains same number of columns i.e., Length of every row contains same number of column. See Fig1.




Jagged 2D Array

These are those 2D Array where each rows can be of different length. See Fig2.

Creating a Rectangular 2D Array

1. Creating the Array Reference

<data_type> [ ] [ ] <array_ref>; //most preferred
OR
<data type><array_ref> [ ] [ ];
OR
<data type> [ ] <array_ref> [ ];

2-Creating the Actual Array

<array_ref> = new <data_type>[row_size][col_size]

int [ ] [ ] arr;
arr = new int[3] [4];
System.out.println(arr.length); //3
System.out.println(arr[0].length); // 4
System.out.println(arr[0] [0].length); //Will not compile
System.out.println(arr[3].length); //Will give Exception


Total taking 80 Byte
        Size in Memory 16 + 16 + 16 + (8*3) + 8 = 80 Byte

Problem - Write a program to create 2D Integer Array of the Row and Column size given by the user. Now accept input from the user and finally display array values in matrix form(Ex: 3 Row 4  Column) also display sum and average of all array values.

Initializing A Rectangular 2D Array

(1) int [ ] arr = new int [2] [3]
      arr[0][0] = 10
      arr[0][1] = 20
      arr[0][2] = 30
      arr[1][0] = 40
      arr[1][1] = 50
      arr[1][2] = 60
(2) int [ ] [ ] arr = new int [ ] [ ]{{10,20,30},{40,50,60}}
(3) int [ ] [ ] arr = {{10,20,30},{30,40,50}}

Creating Jagged Array

(1) Creating Ref
        <datatype> [ ] [ ] <ref> = new < data_type>[arr][ ] ;
(2) Creating Actual Array
        <ref>[row_index] = new <data_type> [size];
        Example :
        int [ ] [ ] arr = new int [3] [ ];
        arr[0] = new int [4]
        arr[1] = new int [3]
        arr[2] = new int[5]

        System.out.println(arr.length);
        System.out.println(arr[0].length);
        System.out.println(arr[1].length);
        System.out.println(arr[2].length);


class JaggedArrayDemo
{
public static void main(String [ ] args)
{
int [ ] [ ] arr = new int [3] [ ];
Scanner kb = new Scanner(System.in);
arr[0] = new int[4];
arr[1] = new int[3];
arr[2] = new int[5];

for(int i =0; i<arr.length; i++)
{
for(int j = 0; j<arr[i].length; j++)
{
System.out.println("Enter value ");
arr[i][j] = kb.nextInt();
}
}
int sum = 0;
int count = 0;
for(int i = 0; i<arr.length;i++)
{
for(int j = 0; j<arr[i].length; j++)
{
System.out.println(arr[i][j] + " ");
sum = sum + arr[i][j];
++count;
}
System.out.println();
}
System.out.println("Sum is "+sum);
System.out.println("Avg is "+(float)sum/count);
}
}



Try Your Self:
Problem : Write a program o calculate and print the average dale made by every salesmen of the company. Number of salesman, their number of sales and their sale value are to be accepted from the user.
Sample Output :
How many salesman?  4
How many sales by salesmen1? 3
Enter sale value: 20 6 12
How many sales.

Initializing A jagged Array

(1) int [ ] [ ] arr = new int [ 3] [ ] 
    arr [ 0 ] = new int [ 3]
    arr[1] = new int [5]
    arr[2] = new int [4]
    arr [ 0] [0] = 10;
    arr[0][1] = 20;
(2)int [ ] [ ]arr = new int[3] [ ] 
    arr[0] = new int[] {10,20,30}
    arr[1] = new int[ ] 40


Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?