Sunday, January 13, 2013

4 Array

4-2
Same thing
int a[ ][ ] = new int [5][4] ;
int[ ][ ] a = new int[5][4] ;
int[ ] a[ ] = new int [5][4] ;



int[ ] a[ ] = new int[4][ ] ;
This will create an array of length 4. Each element of this array will be null. But you can assign an array of ints of any length to any of the elements. For example:a[0] = new int[10];//valid
a[1] = new int[4];//valid
a[2] = new int[]; //invalid because you must specify the length
a[3] = new Object[] //invalid because a[3] can only refer to an array of ints.

This shows that while creating a one dimensional array, the length must be specified but while creating multidimensional arrays, the length of the last dimension can be left unspecified. Thus,  multidimensional arrays do not have to be symmetrical.





   int[] ia, ba;  // here ia and ba both are int arrays.
   int ia[], ba; //here only ia is int array and ba is an int.


4-3 
If you give the elements explicitly you can't give the size. So it should be just int[] { 1, 2 } or just { 1, 2 }   
 right:   String[] sA = new String[] { "aaa"}; 
 wrong:  String[] sA = new String[1] { "aaa"}; 

4-6
 
    In an array access, the expression to the left of the brackets appears to 
    be fully evaluated before any part of the expression within the brackets 
    is evaluated.
  
4-9
 b = (B[]) a1; // 3

the cast sign lie to compiler, so there is not compile time error here. It is gonna be a run time error though.

  

No comments:

Post a Comment