Core Java

OCA Java 8 Preparation : Arrays

An array in Java is itself an ‘Object’ which is used to store collection of values. You can choose to store a collection of primitives or a collection of objects in an array. When storing primitives, the actual values are stored whereas when storing the objects, array elements hold the references to those objects.

A contiguous space is allocated for an array based on its size .This helps in accessing the elements quickly.

Above are few declarations used to create arrays. Arrays array1 and array2 hold primitive type values, array1 specifies only the size and later fills up the elements in those spaces whereas array2 specifies  its elements directly.

The third array declared above , ‘strArray‘ (size=3), is the one which hold the references to the objects and not the values. This is because String is an object, not a primitive type.

Index for an array always starts at 0. Arrays can be either one or multi-dimensional.

Array Declaration :

In OCA exam you might be tested to identify valid and invalid array declarations as part of some complex code presented to you which might seem requiring some other complex knowledge, for instance, complex looping on arrays. Remember below rules for array declaration :

  1. Size of an array can never be specified during an array declaration.
  2. Square brackets can follow either the variable name or its type.For multidimensional array , it can follow both of them.

Array Allocation :

As we have already told you that array is an object in Java, so you now understand that new keyword will be used to allocate memory to arrays.However there are some basic rules to remember related to array space allocation for your OCA Exam.

  1. It is must to specify the array size when trying to allocate memory to it.
  2. The size of the array must be evaluated to int value. You can’t create an array with its size specified as a floating-point number.
  3. For a multidimensional(2D) array, it is possible to specify the size only in the first square bracket.

One important point to understand is that once the array is allocated memory, all the array elements store the default values unless specified. Elements of type primitive types stores 0 for integral types(byte,short, int, long), 0.0 for decimal types(float, double), false for boolean and ‘\u000’ for char data. Elements in an array which store objects default to null. Try printing the values on your favourite IDE to grasp this concept by heart.

For multidimensional array , if you specify  the size only in first square bracket, the values of that 2D array is defaulted to null, considering it to be holding object values. For example, try executing below code .

Array Initialization

Initializing arrays is easiest of all. You just need to access any particular element using its index value.

When trying to access an array element using an invalid index( index < 0 or index >= arr.length ), the code compiles fine but a runtime exception – ArrayIndexOutOfBoundException is thrown. Moreover, compiler expects you to pass an int value (or any type that can be auto-typecasted to int) as an index while trying to access its elements. Any attempt to pass long or float without explicit typecasting will fail to compile.

Combining it all together :

It is also possible to declare, allocate and initialize an array all in one line.However , remember the following rules :

  1. You are not allowed to specify the size of the array as it is computed based on count of the values that are assigned to it.
  2. You can either use curly braces {} or  a new keyword with values passed[referred to as anonymous array].
  3. It’s relevant to understand that if you choose to declare and initialize an array using two separate lines of code, you’ll use a new keyword to initialize the values.

Important Members of Array :

  1. Array has a property length, which returns the length of the array. Note : String has a method length() whereas for arrays, length is a property.( i.e arr.length)
  2. clone() : This method helps us to create a clone of an array.

Sorting & Searching Arrays :

The class java.util.Arrays provides methods to perform sort and search operations on an array.

Sorting is done based on the natural ordering of elements in an array. Also, it is must to have all the elements of an array to be mutually comparable or else a ClassCastException is thrown.

Binary search is always performed on a sorted array and Java API helps us to do that with a method binarySearch() in java.util.Arrays class. This method takes an array of almost any type along with a key to look for and returns the index of the key, if it exists.The array passed as an input must be sorted in natural ascending order or the results will be unpredictable.

If the key is not found in the array, this method returns a value corresponding to -(insertionPoint +1) where insertionPoint is the index of the element where the key would have been found if it would have been present in the array.

If there are duplicate elements in an array with same value as the key, then there is no guarantee of which element will be found first.

Asymmetric Arrays :

Since an array is of type object in Java so it is possible to define an asymmetric array. Refer the code below to understand it better.


Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *