Monday, October 3, 2011

Arrays in Java

Background:

Suppose we have here three variables of type int with different identifiers for each variable.
int number1;
int number2;
int number3;

number1 = 1;
number2 = 2;
number3 = 3;

As you can see, it seems like a tedious task in order to just initialize and use the variables especially if they are used for the same purpose.

Definition:
  • An array is a systematic arrangement of objects, usually in rows and columns.
  • An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.
  • An array is a very common type of data structure where in all elements must be of the same data type. Once defined , the size of an array is fixed and cannot increase to accommodate more elements. The first element of an array starts with zero.
  • In Java and other programming languages, there is one capability wherein we can use one variable to store a list of data and manipulate them more efficiently. This type of variable is called an array. An array stores multiple data items of the same data type, in a contiguous block of memory, divided into a number of slots.
Declaring Arrays:

Like all other variables in Java, an array must have a specific type like byte, int, String or double. Only variables of the appropriate type can be stored in an array. One array cannot store both ints and Strings, for instance.

Like all other variables in Java an array must be declared. To declare an array, write the data type, followed by a set of square brackets[], followed by the identifier name.

Syntax:

<elementType>[] <arrayName>;
                   or
<elementType> <arrayName>[];

Here are some examples:

int[] k;
float[] yt;
String[] names;

This says that k is an array of ints, yt is an array of floats and names is an array of Strings. In other words you declare an array like you declare any other variable except that you append brackets to the end of the type.

You also have the option to append the brackets to the variable instead of the type.

int k[];
float yt[];
String names[];

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:

byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.

Array Instantiation:

After declaring, we must create the array and specify its length with a constructor statement. Declaring arrays merely says what kind of values the array will hold. It does not create them. Java arrays are objects, and like any other object you use the new keyword to create them. When you create an array, you must tell the compiler how many components will be stored in it.

Note:
Instantiation: In Java, this means creation
Constructor: In order to instantiate an object, we need to use a constructor for this. A constructor is a method that is called to create a certain object.

Arrays can be created using the new operator or by declaring a static initializer. The new operator specifies the size of the array.

Syntax:

<arrayName> = new <elementType>[<noOfElements>];

Example:

intArray = new int[10];     // Defines that intArray will store 10 integer values
int[] c, d;         // declare references to array c and d
c = new int [ 4 ];         // allocate memory for the 4 elements in array c
d = new int [ 125 ];     // allocate memory for the 125 elements in array d
byte[] b = new byte [ 100 ]; // declare and allocate memory for array b

An array intializer may be any arbitrary expression.

int c = 15;
int[] primes = { 1, 2, 3, 5, 7, 9, 11 };
int[] a = { 1, primes[2], c, (int)Math.pow(2,2) };

When memory is allocated for an array, the elements are automatically initialized to their default values: zero for all numeric primitive data types, false for boolean variables and null for references.

As with all objects, if the array reference (object reference) is assigned a null value, then the garbage collector will reclaim the dereferenced (unused) memory.

    No comments:

    Post a Comment