Friday, March 13, 2015

Arrays in java

Posted by Sona Mathews


1.     An array is a group of variables that have a common name.
2.     An array is an abstract data type.
3.     Arrays are stored in contiguous memory locations.
4.     They are fixed length entities i.e. they remain the same once they are created.
5.     Arrays of any type can have one or more than one dimensions.
6.     The element of an array can be accessed by its index also known as subscript.
7.     With the help of arrays we can group related information.
8.     Array is a collection of homogeneous elements with same name.

Example:
1.     The array name is c.
2.     The elements in the array are referred to as c [0], c [1], to c [11]. This means 12 elements from 0-11.

Arrays in java

Creating an array:
In Java arrays are implemented as objects.
In order to create an array we must first declare an array and then instantiate it using new operator.
Declaring/Instantiating an array:
     int c[] = new int[12];

This declaration and array creation create an array containing 12 int elements and store the reference in variable c.

The task can be performed in two steps:
            int c [];                       //declare the array variable
            c = new int [12];     //create the array; assign to array variable
            float [] salary = new float [100];           
1.     A salary is an array variable which stores the reference to the array which contains 100 float type values.
2.     The default values for an array of numeric elements are 0. For Boolean arrays are false. For char arrays are ‘\u0000’ (space). For an array of class type are null.

Initializing arrays:
Once an array is created, each element of an array is set to its default initial value according to its type.
User can also provide value other than the default value by using array initialization.
           data type [] array name = {value1, value2 …};
           int [] num = {5,10,15,20};

The above initialization is also equivalent:
int [] num = new int [5];
num [0] = 5;
num [1] = 10;
num [2] = 15;
num [3] = 20;
num [4] = 25;

How to get length of an array:
1.     The user can access the length of an array i.e. the number of elements it holds.
2.     By using its length field which is public and final implicitly.
3.     The length field is associated with each and every array which has been instantiated.
4.     Length field can be directly accessed using the array name and a dot operator because it is a public member.
        num.length;    //to access the number of elements in an array called num.

1.     Array with length zero is said to be an empty array.
2.     Length field of an array can also be used to control the numerical for loop which iterates over the elements of an array.

One dimensional array: They are a list of like-typed variables which have only one index or subscript.
Multidimensional array: Arrays of Arrays
1.     These are the arrays which contain more than one index and can be stored in more than multiple dimensions.
2.     The information is arranged in rows and columns.
3.     But Java does not directly support multidimensional arrays.

Two dimensional arrays: these arrays require two indexes to identify a particular element.
Left index refer to rows.
[0][0]
[0][1]
[0][2]
[0][3]
[0][4]
[1][0]
[1][1]
[1][2]
[1][3]
[1][4]
[2][0]
[2][1]
[2][2]
[2][3]
[2][4]
[3][0]
[3][1]
[3][2]
[3][3]
[3][4]

Right index refers to columns.
int two [] [] = new int [4] [5];
This allocates a 4 by 5 two dimensional array and this matrix is implemented as an array of arrays of int.

0 comments:

Post a Comment