Friday, April 28, 2017

Anonymous Array and Array Assignment

Anonymous Array

Array without name is called Anonymous array.
Some time we declare an array without name such type of nameless array is called anonymous array.
The main purpose of this array is just for instant used (one time use).
Example:- suppose  a condition a method taking array as parameter than just to pass the argument as array we create an array.

class Test
{                             
     public static void main(String[] args){
        System.out.println("Sum="+sum(new int[]{10,20,30,40}));                
                }
     public static int sum(int[] numbers){
        int total=0;
        for(int no:numbers){
             total=total+no;
                  }
             return total;
                }
               
}

We can create anonymous array as follow:

new int[]{10,20,30,40}

While creating anonymous array we can’t specify the size of array if  we try to specify than we will get compile time error .
new int[4]{10,20,30,40}   àC.E
new int[]{10,20,30,40}  Correct

we can create multi-dimensional anonymous array also.

new  int [][]{{10,20},{5,8,15}}

Based on our requirement we also provide name to the anonymous array but it is no longer be an anonymous array.

Int[][]  x = new  int [][]{{10,20},{5,8,15}};

Array element assignment

Here we learn which types of element are allowed to assign in which type of array.

Case 1: In the case of Primitive type array as array element we can be assign any type of element which can implicitly promoted to declared type.

Example
                int [] x = new int[6];
                x[0]=10;
                x[1]='a';
                byte b=30;
                x[2]=b;
                char c='d';
                x[3]=c;
                short s=56;

                x[4]=s;
                 long f=10L;

                 x[5]=f; C.E: possible loss of precision 

Case 2: In case of object type array as array element we can assign or provide either declared type or its child type.

Example 1
                Object [] o = new Object [10];
                o [0]=new Thread();
                o[1]= new String(“hello”);
                o[2]= new Integer(10);

Example 2
                Number [] o = new Number [10];
        o[0]= new Integer(10);
        o[1]= new Double(10.45);
        o[2]= new String(“hello”);C.E: incompatible type

Case3: In case of Abstract class type array as array element we can provide only its child type object.

Case 4: In case of interface type array as array element we assign its implemented class object only.


Example
                Runnable [] r = new Runnable [10];
                r [0]=new Thread();
                r[1]= new String(“hello”);
à C.E : incompatible type

Array Type
Allowed Element Type
Primitive Type
Any type which implicitly promoted to declared type.
Object Type or Class Type
Either declared type object or its child class objects.
Abstract Class Type
Only its child class objects.
Interface Type
Only its implementation class objects.


Array Variable Assignments

Case 1: Element level promotion not allowed to array level.

Example:

char element can Promoted to int type But char Type array (char[]) not promoted to int type array(int[]).

char c=’a’;
int x=c;
char[] c={‘a’,’b’,’c’};
int[] y={1,2,3};
int[] x=y; 
int[] z=c;à C.E : incompatible type 
                                             required: int[]                                              found: char[]


*But in case of Object Type Arrays Child class Array can be promoted to parent Class Type array.

Integer[] y={1,2,3};
Number[] n=y;
String[] s ={“aa”,”ab”,”bb”};
Object[] o=s;

Case 2: whenever we are assigned one array to other array internally the elements of array would not copied to the other array just reference variable will be re-assigned.


Example

int [] a={10,20,30,40};
int [] b={50,60};








Case 3: whenever we assigned on array to another than dimension must be same.

Example

If we want to assign one dimension int [] we should provide one dimensional  array only. If  we provide other dimension than get compile time error.
int[] x={10,20,30};

No comments:

Post a Comment