## Sunday, February 8, 2009

### Arrays Decleration Construction initialization

An array is a data structure that defines an indexed collection of fixed number of homogeneous data elements.Once the size of array is fixed there is no chance of increasing or decreasing its size.Array show wonderful performance when compared with the collections.
Declaration of array:
Become name is clearly saperated form the type,first one is the highly recommended way.
At the time of deceleration we are not allowed to specify the size ,violation leads to compail time error.

Array Example:int [] a; (valid)
int a; (not valid)
For deceleration of 2/3 dimensional array
int [][] a; (valid)
int [][][]a; (valid)

int a[][]; (valid)
int a[][][]; (valid)

int [] a[][]; (valid)
int [] a[][]; (valid)

int [][]a; (valid)
int [][] a[]; (valid)

int[] a. b[]; (valid)
int[] a,[]b; (not valid)

int a,b[]; (valid)
int [][]a[]; (valid)

Example:
int []a[]; ------>2 di
int []a[];-------->2di
int []a[],b[]; ------->a is 2 di,b is 2 di
int []a[],[]b[]; --------> //not valid

Construction of arrays:
At the time of construction we should specify the size ,other wise compail time error occures.
i.e int a =new int ; (not valid)
int [] a=new int []; (not valid)
int s[]=new int; (valid)
It is legal to have an array with size zero.

Example: int [] a=new int [-6]; (not valid)
We are not allowed to specify the size of the array as nagative number,violation leads to runtime exception saying negative array size exception.
char/short /byte/int b=6;
int [] a=new int [b]; (valid)
long/double/float b=6;
int [] a=new int [b]; (not valid)

• If we want to specify the size of the array by using some variable ,the allowed data type for that variable are byte,short,char,int(i.e any data type which can be implicitly promoted to the int type).
• The maximum allowed size for java array is 2147483647.
• Once an array is created ,all the elements will assign with default values (depends on data types)
Example:
int [] a=new int ;
0 0 0 0 0 0
Then a -------------------->For int default 0,for string it is null
int [][] a= new int ;
Then a-----------------------> In java the 2 dimensional array is not implemented as the matrix.it is simply an array of arrays.
int [][] a= new int [];
a =new int;
a=new int;
a=new int;
so,no need to specify the second dimension in 2d,3d arrays.
Array Examples:
int [][] a=new int; (valid)
int [][] a=new int[]; (valid)
int [][] a=new int[]; (not valid)
int [][] a=new int[][]; (not valid)
Base size should be specified mandatory.

int [][][] a new int [][]; (valid)
int [][][] a new int []; (valid)
int [][][] a new int []; (not valid)

Example:
int[] a=new int ;
System.out.println(a); -----------> output: 0
boolean[] b=new booleab;
System.out.println(b); -------------> output: false
String s=new String ;
System.out.println(s); -------------->output: null

Example:
int [][] a=new int ;
System.out.println(a); -----------> output: 0
int [][] a=new int ;
System.out.println(a); -----------> output: null
System.out.println(a); -----------> output: Null Pointer Exception

Initializing an array:
int [] a=new int;
a=20;
a=30;
a=40;
a=50 ------> Leads to array index out of bound Exception
If you are accessing an array with invalid index or some -ve index we will get a RTE saying array out of bound exception.
a[4.0]; ---------> leads to compail time error
Deceleration ,construction and initialization in single line:
int [] a;
a=new int ;
Example:
char[] ch={‘l’,’a’,’x’,’m’,’a’,’n’};
String [] s= {“laxman”,”scjp”};
int [] a;
a ={10,20,30}; ----------------> Leads to compail time error illegal start expression
so Declaration,contraction,initialization must in a single Line.

length Vs length():
length:This is a variable.applicable for only for array objects represents the number of elements or the size of the array.
Ex1:
int a=new int;
System.out.println(a.length); ------------------> output: 6
Can’t apply to String
Ex2:
String s=”Laxman”;
System.out.println(s.length);
Given CTE because length variable applicable only for array objects.
length(): It is a final method applicable for string objects.
It represents the number of characters present in the String Object.
Ex:
String s=”Laxman”;
System.out.println(s.length()); ---------> 4
System.out.println(s.length); ---------->error
Anonymous Arrays:
There are name less arrays.
Purpose of these anonymous arrays is just for instant use.
Example:
class Sample {
public static void main(String arg[]) {
System.out.println(sum(new int[]{10,20,30}));
} }
output: 60
While constructing anonymous arrays we are not allowed to specify the size .violation leads to compile time error.
If we want ,we can assign anonymous array for any reference variable.
i.e int [] a=new int[] {10,20,30,40}; ----------> valid

Array Element Assignments:
As array elements we can take any values which can be implicitly promoted to declared type.
int[] a =new int{3};
a=10; -------> valid
a=’a’; -------->valid because char can be implicitly promoted to int type.
a=b; ---------->valid because byte can implicitly promoted to int type.
a=10l; --------->CTE because long can’t be implicitly promoted to int
Here we can give only String objects.
Object[] o=new Object[b];
• If an array is declared as object reference array we are allowed to assign the objects of either declared type or its child classes.
Array variable Assignments:
Case 1: int [] a={10,20,30};
char [] ch={‘a’,’b’,’c’};
int [] b=ch; -------------->CTE Incompatible types reqired:int[] found:char[]
A char element can be assigned in the place of int element .But we are not allowed to give a char array in the place of int array.
But int [] b=a; ---------> valid
Case 2: int [] a={10,20,30,40};
int [] b={50,60,70};
In this case a=b and b=a, both are valid because while assigning one array to another we have to consider only types irrespective of sizes.
Char ch={‘a’,’b’,’c’,’d’};
a=ch; --------->Incompatable not valid
case 3: int [][] a=new int;
a=new int ; -------> valid
But a=new int  -------> not valid
Int [][] a=new int[];
a=new int; -------> valid
a=10; --------->CTE incompatable types reqired : int[] found : int
a=new int ; -----------> CTE incompatable types reqired : int[] found : int[][]
case 4: int [] a=new int;
System.out.println(a.length); -------------> 6
Int [][] a=new int ;
System.out.println(a.length); -------------> 3
System.out.println(a.length); -----------> 4