Friday, 23 October 2015

Core Java with OCJP_SCJP Language Fundamentals Part-6 __ Arrays Part-1


Core Java with OCJP_SCJP Language Fundamentals Part-6 __ Arrays Part-1

Arrays

Introduction-
                          An array is a data structure that represents an index collection of fixed no of homogeneous data  elements. The main advantage of arrays is we can represent a group of values with single name  hence readability of the code will be improved. The main limitation of arrays is they are fixed in size. i.e once we constructed an array there is no chance of increasing or decreasing bases on our requirement hence with respect to memory arrays shows worst performance we can overcome this problem by using collections.

Array Declaration-

The following are the ways to declare an array.
      1)  int [] a;
      2)  int a[];
      3)  int [] a;

The first one is recommended to use because the type is clearly separated from the name.
At the first time of declarations we are not allowed to specify the size. Violation leads to C.E.
 Ex:.

Declaring Multidimensional Arrays

 The following are the valid declarations for multidimensional arrays.  
                               int[][] a;
                               int a[][];
                               int [][]a;
                               int[] a[];
                               int[] []a; 
we can specify the dimension before name of variable also, but this facility is available only for the first variable.

Which of the following declarations are valid? 


Construction of Arrays

Single Dimension: Arrays are internally implemented as object hence by using new operator we can construct an array.

Compulsory at the time of construction we should specify the size otherwise compile time error.
 Ex:-
                    we will get R.E saying NegativeArraySizeException.

The only allowed Data type to allow the size are byte, short, char, int. if we are using any other datatype we will get a C.E.


The maximum allowed Array Size in java is 2147483648.

4 comments:

  1. excellent >>>>>>>>>

    ReplyDelete
  2. I need next part ... but I couldn't find it :(

    ReplyDelete
  3. Excellent explanation sir.

    ReplyDelete
  4. recommended to all java learners

    ReplyDelete