Arrays Declaration 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.
Each item in an array is called
an element, and each element is accessed by its numerical index. As
shown in the preceding illustration, numbering begins with 0. The 9th element,
for example, would therefore be accessed at index 8.
Declaration of array: (Declaring a Variable
to Refer to an Array)
// declares an array of integers
int[] anArray;
An array declaration has two
components: the array's type and the array's name
An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this
variable holds an array.The size of the array is not part of its type (which is
why the brackets are empty).
An array's name can be anything you want, provided that it followed
java identifier rules.
At the time of deceleration we are not allowed to specify the size, violation leads to compile time error.
Array Example:int [] a; (valid)
int[6] a; (not valid)
At the time of deceleration we are not allowed to specify the size, violation leads to compile time error.
Array Example:int [] a; (valid)
int[6] a; (not valid)
For deceleration of 2/3 dimensional array
int [][] a1;// (valid)
int [][][]a2; //(valid)
int a3[][]; //(valid)
int a4[][][]; //(valid)
int [] a5[][];// (valid)
int [] a6[][];// (valid)
int [][]a7; ///(valid)
int [][] a8[]; //(valid)
//int[]
a10,[]b10; //(not valid)
int[] a10,b8;
int a11,b3[];
///(valid)
int [][]a12[]; //(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 ,otherwise compile time error occurs.
int[] a13 =new int[0]; //valid and it islegal to have an array with size zero
int a14 = new int[10];//not valid .can
not convert int[] to int
int[] a15 = new int[];//not 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 negative 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)
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 negative 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 [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 matrix.it 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)
Example:
int[] a=new int [6];
System.out.println(a[0]); -----------> output: 0
boolean[] b=new boolean[3];
System.out.println(b[0]); -------------> output: false
String s=new String [6];
System.out.println(s[0]); -------------->output: null
Example:
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[0]=20;
a[1]=30;
a[2]=40;
a[3]=50 ------> Leads to ArrayIndexOutOfBoundsException
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 matrix.it 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)
Example:
int[] a=new int [6];
System.out.println(a[0]); -----------> output: 0
boolean[] b=new boolean[3];
System.out.println(b[0]); -------------> output: false
String s=new String [6];
System.out.println(s[0]); -------------->output: null
Example:
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[0]=20;
a[1]=30;
a[2]=40;
a[3]=50 ------> Leads to ArrayIndexOutOfBoundsException
If you are accessing an array with invalid index or some -ve index
we will get a RTE saying array out of bound exception.
Deceleration ,construction and initialization in single line:
int [] a;
a=new int [3];
Example:
char[] ch={‘l’,’a’,’x’,’m’,’a’,’n’};
String [] s= {“laxman”,”scjp”};
int [] a;
a ={10,20,30}; ----------------> Leads to compile time error illegal start expression
so Declaration, contraction ,initialization must in a single Line.
length Vs length():
length: This is a variable. Applicable only for array objects represents the number of elements or the size of the array.
Ex1:
int a=new int[6];
System.out.println(a.length); ------------------> output: 6
Can’t apply to String
Ex2:
String s=”ChandraSekhar Sannamuri”;
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=” ChandraSekhar”;
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[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];
Deceleration ,construction and initialization in single line:
int [] a;
a=new int [3];
Example:
char[] ch={‘l’,’a’,’x’,’m’,’a’,’n’};
String [] s= {“laxman”,”scjp”};
int [] a;
a ={10,20,30}; ----------------> Leads to compile time error illegal start expression
so Declaration, contraction ,initialization must in a single Line.
length Vs length():
length: This is a variable. Applicable only for array objects represents the number of elements or the size of the array.
Ex1:
int a=new int[6];
System.out.println(a.length); ------------------> output: 6
Can’t apply to String
Ex2:
String s=”ChandraSekhar Sannamuri”;
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=” ChandraSekhar”;
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[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; --------->Incompatible 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 incompatible types required : int[] found : int
a[0]=new int [2][3]; -----------> CTE incompatible types required : 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
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; --------->Incompatible 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 incompatible types required : int[] found : int
a[0]=new int [2][3]; -----------> CTE incompatible types required : 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
Comments
Post a Comment