Pages

Friday 9 August 2019

Arrays

Agenda:
  • Introduction
  • Array declaration
  • Array construction
  • Array initialization
  • Array declaration construction initialization in a single line.
  • length vs length() 
  • Anonymous arrays
  • Array element assignments
  • Array variable assignments
Introduction:
  • An array is a collection of homogeneous elements.
  • It can represent multiple values with the same name.
  • It is fixed in size. Means once you  created an array you can't change the size of the array
  • It is an object in java.
Array Declaration:
Single Dimensional array declaration
int[] a;
int a[];
int []a;

Double dimensional array declaration
int[][] a;
int [][]a;
int[]a[];
int a[][];
int a[] [];
int[] a[];

Three dimensional array declaration
int[][][] a;
int [][][]a;
int[][]a[];
int[]a[][];
int a[][][];
int a[] [][];
int a[][] [];
int a [][][];

Note: If you specify the [](dimension) before the variable this rule is applicable only for the first variable. The second variable onward you can't apply in the same declaration.
e.g int[]a,[]b;//invalid
Array construction:
  • Every array in java is an object so, you can create an object using the new operator.
     E.g.: int[] a = new int[3];
  • For every array type corresponding classes are available but these classes are part of java language and not available to the programmer level.
Points to remember:
  • You must specify the size at array creation otherwise you will get a compiler error.
    E.g.: int[] a = new int[];//compiler error.
  • It is valid to have an array with size zero in java e.g int a = new int[0];
  • You can't take array size with -ve int value, otherwise, you will get runtime exception NegativeArraySizeException
  • The allowed data types to specify array size are a byte, short, char, int only.
  • The maximum allowed array size in java is the maximum value of int size[2147483647]
Two-dimensional array creation:
  • In java, multidimensional arrays are implemented as an array of arrays approach but not matrix form.
  • The main advantage of this approach is to improve memory utilization.
Array initialization:
Whenever you are creating an array every element is initialized with default value automatically.

Declaration  and initialization of an array in a single line:
int[] a = {10,20,30,40};
int[][] a ={{10,20},{30}.{40,50}};
int[][][] a ={{{10,20},{30}},{{40,50}},{{60},{70,80},{90}}};

length vs length() method:
length:
It is the final variable applicable only for arrays.
It represents the size of the array.
length() method:
It is the final method applicable to String objects.
It returns the no of characters present in the String.
Anonymous Arrays:
  • Sometimes you can create an array without name such type of nameless arrays are called anonymous arrays.
  • At the time of anonymous array creation, we can't specify the size otherwise you will get a compiler error e.g new int[]{10};//valid, new int[3]{10,20};//invalid
  • You can give a name for anonymous array if required then it will be no longer anonymous e.g int[] a = new int[]{10,20};

Array element assignments:
Case 1: In the case of a primitive array, as array elements, any type is allowed which can be promoted to declared type.
Example 1: For the int type arrays the allowed array element types are a byte, short, char, int
Example 2:  For the float type arrays the allowed array element types are a byte, short, char, int, long, float. e.g. int[] a = new int[10]; a[0]=97;//valid, a[1]=10L;//invalid

Case 2: In the case of a Class type arrays, as array elements, you can provide either declared type objects or its child class objects.
Example:
Object[] obj = new Object[10];
obj[0] = new Object();//valid
obj[1] = new String("anu");
Number[] numbers = new Numbers[10];
numbers[0] = new Integer(10);//valid
numbers[1] = new String("kalawati");//invalid because String is not the child class of Number class.

Case 3: In the case of interface type arrays, as array elements, you can provide its implemented class objects.
Example:
Runnable[] r = new Runnable[10];
r[0] = new Thread();//valid
r[1] = new String("Amit");// invalid, because String is not implemented class of Runnable interface.
Array variable assignments:
  • Element level promotions are not applicable at the array level.
  • Example: A char value can be promoted to int type but char array can't be promoted to an int array. int[] a ={10,20}; char[] ch ={'a','d'}; int[] b =a;//valid, an int[] c = ch;//invalid C.E
  • Whenever you are assigning one array to another array internal elements won't copy just reference variables will be reassigned, so sizes are not important but types must be matched.Example: int[] a = {10,20,30,40}; int[] b ={10,20}; a=b;//valid  b=a;//valid
  • Whenever you are assigning one array to another array, dimensions must be matched, i.e in the place of a one-dimensional array, you must provide the same type only otherwise you will get a compiler error. int[][] a = new int[3][];  a[0] = new int[4][5];//invalid
  • I explained earlier that every array is an object. This means you can assign an array object to a variable of type Object. Like this:
    int[] intArray = new int[3]; Object obj = intArray; This is valid because an array of ints is an Object. Object[] obj2 = new int[3];//Invalid It will give you a compilation error because elements of the array pointed to by int array are not objects. They are ints. Therefore, you can't assign an array of ints to a variable of type array of objects. Each element of the array created using new int[2][3] is an array of ints. An array of ints is an object and therefore, an array of array of ints is an array of objects.
    Object[][] obj3 = new int[3][2];//Invalid,
    Object[] obj4 = new int[3][2];//valid
Note: Whenever you are performing array assignments the types and dimensions must be matched but sizes are not important.

Memory Representation of Arrays:
Fig -1









Fig-2

Fig-3


Next Topic: METHOD & ENCAPSULATION