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[6] 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)

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 [6]; (not valid)
int [] a=new int []; (not valid)
int s[]=new int[0]; (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)
int [] a=new int [6];
0 0 0 0 0 0
Then a -------------------->For int default 0,for string it is null
int [][] a= new int [3][2];
Then a-----------------------> In java the 2 dimensional array is not implemented as the is simply an array of arrays.
int [][] a= new int [3][];
a[0] =new int[2];
a[1]=new int[3];
a[2]=new int[4];
so,no need to specify the second dimension in 2d,3d arrays.
Array Examples:
int [][] a=new int[3][2]; (valid)
int [][] a=new int[3][]; (valid)
int [][] a=new int[][2]; (not valid)
int [][] a=new int[][]; (not valid)
Base size should be specified mandatory.

int [][][] a new int [2][][]; (valid)
int [][][] a new int [2][3][]; (valid)
int [][][] a new int [][3][4]; (not valid)

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

int [][] a=new int [2][3];
System.out.println(a[0]); -----------> output: Garbage value[I@add234]
System.out.println(a[0][0]); -----------> output: 0
int [][] a=new int [2];
System.out.println(a[0]); -----------> output: null
System.out.println(a[0][0]); -----------> output: Null Pointer Exception

Initializing an array:
int [] a=new int[3];
a[3]=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 [3];
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.
int a=new int[6];
System.out.println(a.length); ------------------> output: 6
Can’t apply to String
String s=”Laxman”;
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.
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.
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[0]=10; -------> valid
a[0]=’a’; -------->valid because char can be implicitly promoted to int type.
a[0]=b; ---------->valid because byte can implicitly promoted to int type.
a[0]=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[2][3];
a[0]=new int [6]; -------> valid
But a[0]=new int [2][3] -------> not valid
Int [][] a=new int[2][];
a[0]=new int[6]; -------> valid
a[0]=10; --------->CTE incompatable types reqired : int[] found : int
a[0]=new int [2][3]; -----------> CTE incompatable types reqired : int[] found : int[][]
case 4: int [] a=new int[6];
System.out.println(a.length); -------------> 6
Int [][] a=new int [3][4];
System.out.println(a.length); -------------> 3
System.out.println(a[0].length); -----------> 4


Post a Comment