Friday, April 28, 2017

Array Creation and Initialization

Array Creation.

Every array in java is an object only, hence we can create array by using new operator.

Int []  x= new int[4];

Every array type corresponding classes are available and these classes  are part of java Language But not available for Programmer label.
To know the class of array use getClass() and getName() method on array reference variable.

System.out.Println(x.getClass().getName());


List of Corresponding classes of Array

Array Type
Corresponding Classes
int[]
[I
int[][]
[[I
int[][][]
[[[I
byte[]
[B
short[]
[S
long[]
[L
boolean[]
[Z
char[]
[C
float[]
[F
double[]
[D

1)At the time of array creation compulsory we must specify the size of the array otherwise we will get compile Time Error.

                                 int [] x= new int [4];
2  2) It legal to have the size of array as zero (0) in java.

                                int [] x = new int[0];
     
3)we try to specify the array size as some negative int value than we will get Run Time Exception Saying “negative Array Size Exception”.

                                int[] x = new int[-4];
          Run-Time.Error : java.lang.NegativeArraySizeException

 4) To specify the array size allowed data types are (byte, short, int and char). If we try to specify the other data type we will get compile type error.
                                Ex-1:-
                int[] x = new int[a];

           Ex-2:-

byte b = 3;
int[]y= new int[b];

     5)Maximum allowed Size of the array in java is 2147483648. Because array Class construction takes int data value and max value for int data type is 231-1.

               int[] x= new int[2147483648];

                           C.E : integer number too large

             int[] x= new int[2147483647];

                     Runtime.Error :  java.lang.OutOfMemoryError

Note:-In second case we may also get Run Time Exception saying  “java.lang.OutOfMemoryError: Requested array size exceeds Virtual Machine limit”. Because of memory size allowed to Virtual Machine


2-D Array Creation/Multi-D Array creation




In java 2-D array or multi-dimension array not implemented by using Matrix implementation. SUN people followed array of array approach for multi-Dimensional Array Creation.
The main Advantage of this approach is memory utilization will be improved.

Example 1

int [][] x= new int [2][3]








Note: during creation of multi-dimensional array at least base size we have to specify. Another level  size will be given  latter.

We use when next level array size are vary from each other

Example 2

int [][] x= new [2][];
X[0]= new int [3];
X[1]=new int[2];









Array initialization

Once we create an array every array by default initialized with the default value , if we are not satisfied with default value than we can override this value with our customized value.

int[] x= new int[6];
X[0]=10;
0
10
0
10
0
20
0
130
0
40
0
60
X[1]=10;
X[2]=20;
X[3]=130;
X[4]=40;
X[5]=60;

X[6]=50  R.T.Error :  java.lang.ArrayIndexOutBoundException

X[-6]=50;R.T.Error :  java.lang.ArrayIndexOutBoundException

X[2.5]=230
C.E :  possible loss of precision
required:int
found:double

Note :- if we try to print reference variable of array (suppose x) or when ever we try to print any reference variable by default internally toString() method will be executed . Which is implemented by  default to print the string in the following form:

ClassName @hashcode

Here, hashcode is in hexdecimal memory address representation

Example 1:

int [] x=new int[3];
System.out.println(x);
 System.out.println(x[0]);

Output
[I@xxxx
0

Here , xxxx is hashcode hexadecimal value and 0 is defaut value for int type. 

Example 2:-

int [][] x= new int[2][3];

System.out.println(x);
System.out.println(x[0]);
System.out.println(x[0][1]);

Output
[[I@xxxx
[I@xxxx
0


Example 3:-

int [][] x= new int[2][ ];
System.out.println(x);
System.out.println(x[0]);
System.out.println(x[0][1]);➜Runtime Error


Output
[[I@xxxx
Null
Run time Error: java.lang.NullPointerException

Array declaration, creation, initialization in a single line.


similarly,

char []  vowel = {‘a’,’e’,’i’,’u’,’o’};
String[]  s= {“hello”, “welcome” , “bye”};

We can declare, create and initialize the array in a single line (shortcut representation).
This shortcut is also applicable for multi-dimensional array.

Ex-

int[][] x= { {20,30},{20,10,15}};

Note: - if you want to use this shortcut compulsory you should perform in a single line, if you try to break or divide in multiple line we will get compile time error.

int [] x;
X={10,20,30};    ➜  Compile time error




No comments:

Post a Comment