Show Mobile Navigation

Webring

Powered by WebRing.

Complete guide on arrays in Java

Sachin R Kukale - 20:35
Arrays
Arrays are set of homogeneous elements which are stored contiguous memory locations. The one of advantage of array is that we can access these all the elements of same type, with a single reference variable.
Syntax to declare an array in Java:
[modifier(s)] [];
[modifier(s)] [] ;
[modifier(s)] [] ;
Ex:
int[] ai;
short[][] as;
It is important to note that size of array (number of elements that can be stored in an array.) cannot be specified at the time of declaration of an array.
Also arrays are treated as an object in Java but there is no special class for an array. The class for an array in Java will be created at runtime. All the variables in array are referenced by the non-negative index values. If array has ‘n’ elements then we can say that length of array is ‘n’ and these ‘n’ elements are referenced by the index starting from ‘0’ to ‘n-1’. Variables in array do not have any name and their data type is same as that of data type of array. For ex. if an array is declared as ‘float’ then all the elements in that array will be of type ‘float’.

Anonymous Array:
Anonymous array is an array whose size is not specified as integer value. Such an array can be created at the time array reference variable declaration along with the elements of array enclosed in curly brackets.
Ex:
int s[]={10,20,30,40,50};
In this case number elements within curly brackets will be length of an array, where each element is separated by comma.

Multi-Dimensional Array:
In Java multi-dimensional arrays are created as array of arrays. Internally they will be treated as array of arrays by machine. In the multidimensional array only last dimension will hold the actual value and all the other arrays will have some reference to other arrays.
Syntax to declare multi-dimensional array in Java:
[modifier(s)] [][];
[modifier(s)] [][] ;
[modifier(s)] [] [];
Ex:
int []  a,b[],c;
In above declaration ‘a’ is one dimensional array, ‘b’ is two dimensional array and ‘c’ is one dimensional array.
Syntax to declare two-dimensional array in Java:
=new [size][size];
‘Size’ should be of type ‘int’ or it should be assignment compatible with int.
While declaring multi-dimensional array only first dimension is mandatory to declare and other dimensional are optional.
When you create an array by declaring only first dimension then it is called as ‘Jagged array’.
Exceptions, meaning and causes of exceptions in array in Java:
        i.            java.lang.NullPointerException: This causes when you try to access members of array using null array reference.
      ii.            java.lang.ArrayIndexOutOfBoundException: This exception causes when you try to access the element of array for which index is not present.
    iii.            java.lang.NegativeArraySizeException: This exception is caused when array size declared as negative integer. We cannot provide negative ‘int’ size to an array.
   iv.            java.lang.OutOfError: This error causes when size of array exceeds memory limit of the virtual machine i.e. when size of array is greater than ‘integer.MAX_VALUE’.



0 comments:

Post a Comment