Home / JAVA / Java Array In Detail

Java Array In Detail

Arrays:

description of array
image source:-http://javatutorial.net

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
image source:-http://file.mrbool.com

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
image source:-https://i.ytimg.com

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 Udit Mutreja

Check Also

Inheritance in Java

Inheritance in Java Inheritance is the feature of object-oriented programming concept for extending the data member …

Leave a Reply

Your email address will not be published. Required fields are marked *