Google+

48. Arrays







An array is a group of like typed variables that are referred to by a common name. So, unlike variables which hold a single value, arrays hold more than one fixed number of values.

Example -> int a variable -> holds a single integer value

Example -> int a[10] variable -> holds 10 integer values

 The above array example can store 10 values, by assigning the values to the variable indexes 0 to 9. i.e. we can assing 10 values like  a[0] = 1, a[1] = 5, a[2] = 8, ........a[9]=3

How a normal variable holds a value ?

Suppose there is an integer variable a which hold a value 5 as shown below

int a = 5;

Lets use the below diagram to understand how an integer variable stores a value -

How an array variable holds the value ?

Suppose there is an integer array variable which stores the 3 values 5, 7 and 9 as shown below

int a[] = new int[3]; //can assign three values

a[0] = 5;   //first value
a[1] = 7;  //second value
a[2] = 9;  //third value

Lets use the below diagram to understand how an integer array variable stores more than one value  -


Single Dimension Arrays -

All the array variable which store the values using single index i.e.. [] are knows as Single Dimension Arrays.

Lets understand the single dimension array using the below diagram -


Example of a Single Dimension array -

int a[] = new int[3];  
a[0] = 5;
a[1] = 7;
a[2] = 9;

Lets break the above statements and understand -

int a[] -> Defines the variable a[] as an integer variable. So this variable stores only the integer values.
int[3] -> Stores 3 values using a[0], a[1] and a[2]
a[0] = 5; -> 0 is the index value of the array and stores the first value 5
a[1] = 7; -> 1 is the index value of the array and stores the second value 7
a[2] = 9; -> 2 is the index value of the array and stores the third value 9

a[0], a[1] and a[2] are called as array elements i.e. a[0], a[1] and a[2] are the elements of the array a[].


Program to demonstrate a single dimensional array:

class Array1
{
   public static void main(String args[])
   {
       int a[]=new int[4];    // This means the number of values this array can store is 4 i.e. a[0],a[1],a[2],a[3]

       a[0] = 11;
       a[1] = 25;
       a[2] = 36;
       a[3] = 42;

       int total;
       total = a[0]+a[1]+a[2]+a[3];

       System.out.println("Total Sum="+total);

   }
}

Output of this program:

Total Sum=114


Program to demonstrate the single dimensional array by initializing the array during declaration itself

class Array2
{
   public static void main(String args[])
   {
       int a[]={11,25,36,42};   //Initializing the array during its declaration
       int total;
       total = a[0]+a[1]+a[2]+a[3];

       System.out.println("Total Sum="+total);
   }
}

Output of this program:

Total Sum=114




Two Dimensional Arrays -

All the array variables which store the values using two indexes [][] are known as Two Dimension Arrays.

variable[Row Index][Column Index]

Lets understand the two dimension array using the below diagram -


Example of a Two Dimension array -

int a[][] = new a[2][2];

a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;

Understand the below program to understand the two dimensional array concept perfectly.

Pre-requisites to be read to understand the for loop inside the below program -

   49. Arithmetic Operators
   50. Relational Operators
   51. Boolean Logical Operators
   52. Assignment and '?' operator
   53. Operator Precedence
   54. 'if' selection statements
   55. 'switch' selection statements
   56. 'while' iteration statements
   57. 'do-while' iteration statements
   58. 'for' iterative statements
   59. 'break' jump statement
   60. 'continue' jump statements

Program to demonstrate a two dimensional array

class TwoDArray1
{
   public static void main(String args[])
   {
       int a[][]=new int[2][3];
       int i,j,k=0;

        for(i=0;i<2;i++)
        {
           for(j=0;j<3;j++)
           {
            a[i][j]=k;
            k++;
            System.out.print(a[i][j]+ " ");
         }
 System.out.println();
      }

    }
  }

Output of this program:

0 1 2
3 4 5




Program to demonstrate a two dimensional array in which the sizes of the second dimension are not equal

class TwoDArray2
{
   public static void main(String args[])
   {
       int a[][]=new int[4][];
       a[0]=new int[1];
       a[1]=new int[2];
       a[2]=new int[3];
       a[3]=new int[4];

       int i,j,k=0;

        for(i=0;i<4;i++)
        {
           for(j=0;j<i+1;j++)
           {
           a[i][j]=k;
           k++;
           System.out.print(a[i][j]+ " ");
   }
 System.out.println();
    }

   }
}

Output of this program:


1 2
3 4 5
6 7 8 9




Program to demonstrate initializing a two dimensional array during array declaration

class TwoDArray3
{
   public static void main(String args[])
   {
       int a[][]={ {0,1,2}, {3,4,5}, {6,7,8}, {9,10,11} };

       int i,j;

        for(i=0;i<4;i++)
        {
           for(j=0;j<3;j++)
           {
            System.out.print(a[i][j]+ " ");
         }
       System.out.println();
       }

   }
}

Output of this program:

0 1 2
3 4 5
6 7 8
9 10 11



Three Dimensional Arrays -

A three dimensional array contains 3 indexes i.e. [][][] as shown below -

variable[Number Of 2D Arrays][Row index][Column index]

The diagram of three dimension array will be as shown below -



Program to demonstrate a three dimensional array

class TwoDArray4
{
   public static void main(String args[])
   {
       int a[][][]=new int[2][3][4];

       int i,j,k,l=0;

       for(i=0;i<2;i++)
       {
         for(j=0;j<3;j++)
        {
            for(k=0;k<4;k++)
            {
              a[i][j][k]=l;
              l++;
             System.out.print(a[i][j][k]+ " ");
             }
          System.out.println( );
       }
       System.out.println( );
       }
   }
}

Output of this program:

0 1 2 3
4 5 6 7
8 9 10 11

12  13 14 15
16 17 18 19
20 21 22 23







Please comment below to feedback or ask questions.

Arithmetic Operators concept will be explained in the next post.


8 comments:

raghavi said...

ARUN I dint understand the for condition can u please elaborate how it takes and gives outpot

Bharadwaj.P said...

I appreciate your wonderful effort arun and I do respect your views and in addition to your views on post number 47 regarding type conversion it would be better to add Types of Type casting like Implicit, Explicit and Boolean Type Casting for better understanding. Thank you :)

Unknown said...

Hi Arun,

Can you please elaborate on how you get the outputs from the for conditions?

I'm really confused.

Arun Motoori said...

@Everyone - Understand for loop at http://selenium-by-arun.blogspot.in/2013/02/58-for-iterative-statements.html

Unknown said...

What is the diff b/w:

System.out.print(a[i][j]+"");
And
System.out.print(a[i][j]);

I am getting the same results with both the statements.

Arun Motoori said...

@ Aman Gupta - Yes you will get the same result with both the statements.

But if you use the below two statements, you will get a different output -

System.out.print(a[i][j]+"-");
And
System.out.print(a[i][j]);

Assuming a[i][j] is holding a value say 9, the first print statement will print 9- as output and the second print statement will print 9.

Hence when you use +"" in print statements, + operator will concatenate the string value specified between "" with the value in a[i][j].

Hope you understood the concept :)

Unknown said...

class arraytest1
Hi arun,
Can you tell me where i am wrong in this,
I am getting 1 error which i have included at the given below program.

{
public static void main(String args[])
{

int a [] [] = {1,0,0} ,{2,0,0} , {3,0,0} ,{4,0,0} ;

System.out.println("2 dimensional array is " + a[][]);
}

}

On compiling it ,i am getting below given error:-
arraytest1.java:7: error: expected

^

^
arraytest1.java:7: error: expected
int a [] [] = {1,0,0} ,{2,0,0} , {3,0,0} ,{4,0,0} ;
^
arraytest1.java:9: error: class, interface, or enum expected
System.out.println("2 dimensional array is " + a[][]);
^
arraytest1.java:10: error: class, interface, or enum expected
}
^
19 errors

Tool completed with exit code 1

Arun Motoori said...

@Jenny - Please compare your code with the below code to correct the errors. The below program prints all the elements of the array.

public class ArrayDemo {

public static void main(String args[])
{

int a[][] = {{1,0,0} ,{2,0,0} , {3,0,0} ,{4,0,0}} ;

for(int i=0; i<a.length; i++){

for(int j=0; j<a[0].length; j++){

System.out.println("a["+i+"]["+j+"] value is " + a[i][j]);
}

}


}

}