Sunday, February 23, 2014

Creating single array, multidimentional array and array of objects in java



//Creating single array, multidimentional array and array of objects in java
public class Arrayexample {

    public static void main(String[] args) {
        String arr1[]= new String[10]; //Creating String array arr1[]
        arr1[0]="a";
        arr1[1]="1";
        int arr2[]=new int[10]; //creating int array arr2[]
        arr2[0]=1;
        //arr2[1]="1";----cannot convert string to int
        //arr1[2]=1;---cannot convert int to string
        float arr3[] = new float[3]; //creating float array arr3[]
        arr3[0]=1;
        String arr4[][]=new String[2][2]; //Creating String multidimensional array arr4[]
        arr4[0][0]="a";
        Arrayexample obj1= new Arrayexample();
        System.out.println(obj1);
        Arrayexample obj2=new Arrayexample();
        Arrayexample arr5[]=new Arrayexample[2]; //Crating an array which stores objects, arr5[]
        arr5[0]=obj1; //Assigning value of obj1 i,e address to arr5[0]
        arr5[1]=obj2;
        System.out.println(arr5[0]); //here the value of obj1 is displayed i,e one of the address of memory
   
    }

}

No comments:

Post a Comment