java array

Java array
       An array is used to store collection of same data type variables.
Java arrays can hold Homogeneous data only. In arrays by using single reference we can store data. Arrays have fixed size.
In java arrays are index based, the first element stored at 0th index location in the array.



Array declaration:
                To declare an we need reference variable of array, and you must specify the type of array.
                dataType[] arrayRefVar; (or)
                dataType []arrayRefVar; (or)
                dataType arrayRefVar[];
Example:
Int[] anArray;

There are two types of arrays in java
Single Dimensional array
Multidimensional array

Single Dimensional array:
                In single Dimensional array elements are stored from 0th location and size is fixed
Example:
Creating, Initializing, and Accessing an Array:
There are two types to create a single dimensional array
By using new keyword,
class sample{
                public static void main(String args[]){
                int myArray[]= new int[3];   //declaration
                myArray[0]=1;   //initialization
                myArray[1]=2;
                myArray[2]=3;
                for(int i=0;i<myArray.length;i++)    //printing of array
                System.out.println(myArray[i]);
}
}
In another Way
class sample2{
                public static void main(String args[]){
                int myArray[]= {1,2,3};   //declaration, initialization
                for(int i=0;i<myArray.length;i++)           //printing of array
                System.out.println(myArray[i]);
}
}


                 

Multidimensional array:             
                In multidimensional array data is stored in row and column based index.
Example:  matrix form
                Int[][] arr = new int[2][2];
Syntax:
                dataType[][] arrayRefVar; (or)
                dataType []arrayRefVar[]; (or)
                dataType [][]arrayRefVar; (or)
                dataType arrayRefVar[][];
Program:
class Sample3{
                public static void main(String args[]){
                int myArray[][]= new int[3][3];   //declaration 
                myArray[0][0]=1;   //initialization
                myArray[0][1]=2;
                myArray[0][2]=3;
                myArray[1][0]=4;
                myArray[1][1]=5;
                myArray[1][2]=6;
                myArray[2][0]=7;
                myArray[2][1]=8;
                myArray[2][2]=9;
                for(int i=0;i<3;i++)
                {
                                for(int j=0;j<3;j++) {
                System.out.print(myArray[i][j]+" ");   //printing of array
                }              System.out.println();
}
}
}

Another Way
class Sample4{ 
public static void main(String args[]){ 

int myArray[][]={{1,2,3},{4,5,6},{7,8,9}};  //declaring and initializing

for(int i=0;i<3;i++){ 
 for(int j=0;j<3;j++){ 
   System.out.print(myArray[i][j]+" ");  //printing

 } 
 System.out.println(); 

}}

Jagged array:
                The Jagged array is like multidimensional arrays. In the jagged array, we can allocate dimensions separately.
Example:
0
1 2 3
4 5
6 7 8 9

int cse[][]= new int[4][];   ([] optional)

cse[0]=new int[1];
cse[1]=new int[3];
cse[2]=new int[2];
cse[3]=new int[4];             

Advantages of arrays:
                If we know size better to go arrays only because fast access and random access.
                Code optimization, by using single reference we can store data.
Disadvantages of java arrays:
                Java arrays are fixed size once we are allocated size we can’t change it.
Example: If we are declared array size is  10 but we are stored 5 values in this case memory is not reusable.

1 comments:


EmoticonEmoticon