Sunday, 29 January 2017

2D ARRAY IN JAVA

Suppose if we want to represent our record in the form of table, then we always think what we can do to represent our record in the form of table which consists of rows and columns. Then array plays a vital role. Here to overcome with this problem we will use 2D array which consists of multiple rows and columns. Syntax –
int num[][] = new int[][];
int num[][]={{1,2,3,4},{4,5,6,7}};

Let us take an example –
int a[] ={1,2,3};
int b[]={3,4,5}
int c[]={4,5,7}

Now suppose if we want to store the values of all the above array in one array then the structure would be something like this –
int d[][] = {
                    {1,2,3},
                   {3,4,5},
                   {4,5,7}
                   }
Where the elements are represented in the form of rows and columns. So the first row first value will be in the position 0th row and 0th column, second element will be 0th row and 1st column and so on. For the second row the first value will be in the position 1st row and 0th column, second value will in position 1st row and 1st column.

Now how to work with this kind of an array then we have to take two loops where the first loop will count for the number of rows and the second loop will keep track for columns.

Example –
public class Array6 {
          public static void main(String[] args) {
                   int num[][]={
                                                          {1,2,3},
                                                          {3,4,5},
                                                          {4,5,7}
                                                };
                   for(int i=0;i<num.length;i++)
                   {
                             for(int j=0;j<num.length;j++)
                             {
                                      System.out.print(num[i][j]+" ");
                             }
                             System.out.println();
                   }
          }
}

Output –
1 2 3
3 4 5
4 5 7

Example –
import java.util.Scanner;
public class Array7 {
          public static void main(String[] args) {
                   Scanner sc = new Scanner(System.in);
                   int num[][]=new int[3][4];
                   System.out.println("Enter values in 2D array");
                   // assigning values to 2D array
                   for(int i=0;i<num.length;i++)
                   {
                             for(int j=0;j<num.length;j++)
                             {
                                      num[i][j]=sc.nextInt();
                             }
                   }
                   //fetching the values
                   System.out.println("Fetched records are");
                   for(int i=0;i<num.length;i++)
                   {
                             for(int j=0;j<num.length;j++)
                             {
                                      System.out.print(num[i][j]+" ");
                             }
                             System.out.println();
                   }
          }
}

Output –
Enter values in 2D array
1
2
3
4
5
6
7
8
9
Fetched records are
1 2 3
4 5 6
7 8 9

Example –
import java.util.Scanner;
public class Array8 {
          public static void main(String[] args) {
                   Scanner sc = new Scanner(System.in);
                   System.out.print("Enter number of rows : ");
                   int row = sc.nextInt();
                   System.out.print("Enter number of columns : ");
                   int col = sc.nextInt();
                   int num[][]=new int[row][col];
                   System.out.println("Enter values in 2D array");
                   // assigning values to 2D array
                   for(int i=0;i<num.length;i++)
                   {
                             for(int j=0;j<num.length;j++)
                             {
                                      num[i][j]=sc.nextInt();
                             }
                   }
                   //fetching the values
                   System.out.println("Fetched records are");
                   for(int i=0;i<num.length;i++)
                   {
                             for(int j=0;j<num.length;j++)
                             {
                                      System.out.print(num[i][j]+" ");
                             }
                             System.out.println();
                   }
          }
}

Output –
Enter number of rows : 3
Enter number of columns : 3
Enter values in 2D array
1
2
3
4
5
6
7
8
9
Fetched records are
1 2 3
4 5 6
7 8 9

No comments:

Post a Comment