Saturday, 3 January 2015

Java Arrays

An array in Java is an ordered collection of the similar type of variables. Java allows creating arrays of any dimensions. An array in Java is a bit different from C/C++. You can not specify the size of the array at the time of declaration. The memory allocation is always dynamic. Every array in Java is treated as an object with one special attribute length, which specifies the number of elements in the array.

One Dimensional array

Declaration

A one dimensional array is declared as:

type variable_name[];

The type specifies the base type of array elements, which specifies what type of elements can be stored in the array.

For example, the following array can be used to store int elements:

int marks[]; or int[] marks;

Declaration just specifies that array marks can be used to store integers but no memory allocation takes place at point of declaration.

Memory Allocation

Memory allocation for a Java array is done using operator new. The general form of the operator new is as follows:

var = new type[size];

The variable var is the name of an array, declared using the syntax as mentioned above. The type refers to the type of the elements that can be stored in the array. Size refers to the number of elements for which the memory is to be allocated dynamically.

For example, following array can store 20 elements:

marks = new int[20];

Thus obtaining an array in Java is a two step process:

1. declaration
2. memory allocation

A third step may be initializing the array elements.

The above two steps can be combined into one as follow:

int marks[] = new int[20];

Initialization

Third step is to initialize the array elements. For example the array allocated above can be initialized as:

for(int i=0; i<20; i++){
    marks[i] = 0;
}

The initialization with zero is not must as all the arrays of numeric types are initialized with 0.

Note: Array declaration, memory allocation and initialization steps can be combined into one as shown below:

int marks[] = {10, 2, 17, 77, 99, 90, 22, 34};

Array Length

In Java, all arrays store the allocated size in a variable named length. We can access the length of the array marks using the attribute length.

int len = marks.length;

Example: The following example demonstrate the use of attribute length and also demonstrates how to access the command line arguments.

class cmdLineDemo{
    public static void main(String args[]){
        int len = args.length;
        for(int i=0;i<len;i++){
            System.out.println(args[i]);
        }
    }
}

The JVM calls the main() and passes the command line arguments to it as an array of Strings. The length of the array (i.e. the number of the command line arguments) is obtained using attribute length in the above example. the for loop displays the command line arguments on the console/monitor.

Two dimensional array

Declaration

A two dimensional array is declared as:

type variable_name[][];

The type specifies the base type of array elements as in the case of one-dimensional array. For example, The following array can be used to store int elements:

int marks[][]; or int[][] marks;

Declaration just specifies that array marks can be used to store integers but no memory allocation takes place at point of declaration.

Memory Allocation

The general form of the operator new is as follows:

var = new type[rows][columns];

The variable var is the name of two dimensional array. the type refers to the type of the elements that can be stored in the array. The first index indicates the number of rows and the second index indicates the number of columns in the two dimensional array to be allocated dynamically.

For example, following array can store a table consisting of 5 rows and 4 elements:

marks = new int[5][4];

The above two steps can be combined into one as follows:

int marks[][] = new int[5][4];

No comments:

Post a Comment