JAVA

Java Array In Detail

images - Java Array In Detail

Arrays:

description of array

Arrays are the process to store the information at particular variable on different index position. It means the data are stored on the array, a variable can access with the help of index position. It contains similar data at the continuous memory location. It provides static memory that can not be changed at program execution time.

Single Dimensional Arrays

single array

The single dimensional array is a type of array that store multiple information with different columns in the single row. We define the array variable and column index position to read or write the data into the array.

Syntax :-
Data-type array var[] = new data-type [cols size]
Example :- 
Class A
{
     public static void main (String s[])
     {
        int ar = new int[20];
        int a;
        for (a=1; a<10; a++)
        {
          System.out.print (" Enter data ");
          ar[a] = new java util.Scanner(System.in).nextInt();
            }
          for (a=0; a<10; a++)
           {
              System.out.print (ar[a] + "/t");
               }
}

 

Multi-Dimensional Arrays

demo of Multi-dimension arrays

A multi-dimensional array is a type of array that allows arranging the data into rows and columns format. It is basically used to store maximum information at the minimum range of memory. The data are accessible with the help of array variable and index position.

Syntax :- 
Data-type array var[][] = new data-type [row][cols]
Example :-
import java.util.*;
Class B
{
     public static void main (String s[])
     {
          Scanner sc = new Scanner ( System.in);
          int ar[][] = new int[20][20];
          int a, b;
          for (a=0; a<3; a++)
           {
                for (b=0; b<3; b++)
                  {
                        System.out.print (" Enter value");
                         ar [a][b] = sc.nextInt();
                            }
                       }
                 for (a=0; a<3; a++)
                  {
                       for (b=0; b<3; b++)
                         {
                              System.out.print (ar [a][b] + "/t");
                                  }
                            System.out.println();
                               }
}
}

 

Program to sum arrays elements

import java.util.*;
Class B
{
   public static void main (String s[])
   {
      Scanner sc = new Scanner (System.in);
      int ar [][] = new int [20] [20];
      int a, b, c = 0;
      for (a=0; a<3; a++)
      {
          for (b=0; b<3; b++)
           {
               System.out.print (" Enter value ");
                ar [a] [b] = sc.nextInt();
                    }
              }
      for (a=0; a<3; a++)
      {
          for (b=0; b<3; b++)
           {
                System.out.print ( ar [a] [b] + "/t");
                    }
             System.out.println();
                }
       for (a=0; a<3; a++)
        {
             for (b=0; b<3; b++)
              {
                  c = ar [a] [b] + c;
                     }
            System.out.print (c);
             }
}

 

About the author

Udit Mutreja

Add Comment

Click here to post a comment