Sunday, October 27, 2013

Arrays

An array is a group of like typed variables that are referred by a common name, Arrays of any type can be created and may have one or more dimensions, A specific element in an array is accessed by its index, Arrays offer a convenient means of grouping related information.

One - Dimensional Arrays

A one dimensional array is essentially, a list of variables of the same type. To create an array, you first must create an array variable of the desired type. the general form of a one dimensional array declaration is
Type variable_Name [];
Here, type declares the base type of the array, the base type determines the data type of each element in the array.
Example ti declares an array named montDays with the type "array of int"
int mothDays [];
Although the declaration says that monthDays is an array variable, no array actually exists, That is, the array monthdays is set to null, which represents an array with no value. To link montDays with an actual Physical array of integers, you must allocate one using new and assign it to mothDays, new is a special operator that allocates memory
variable_Name = new type [size];
Here type specifies the type of data being allocated, size specifies the number of elements in the array, and variable_Name is the array variable that is linked to the array. That is, to use new to allocate an array, you must specify the type and number of elements to allocate, The elements in the array allocated by new will automatically be initialized to zero. The following example allocates a 12 element array of integers and links them to month_days.
class MyFirstArray{

    public static void main(String[] args) {
     
        int moth_days [] = new int[12];

    }
}
After this statement executes, moth_days will refer to an array of 12 integers,Also all elements in the array will be initialized to zero.

Obtaining an array is a two way process.

  1. Declare a variable of the desired array type.
  2. allocate the memory that will hold the array. using new and assign it to the array variable.
Once you have allocated an array, you can access a specific element in the array by specifying its index within square brackets, All array indexes start at zero
class MyFirstArray{

    public static void main(String[] args) {
     
        int moth_days [];
        moth_days = new int[12];
     
        moth_days[0] = 31;
        moth_days[1] = 28;
        moth_days[2] = 31;
        moth_days[3] = 30;
        moth_days[4] = 31;
        moth_days[5] = 30;
        moth_days[6] = 31;
        moth_days[7] = 31;
        moth_days[8] = 31;
        moth_days[9] = 31;
        moth_days[10] = 30;
        moth_days[11] = 31;
        System.out.println("April has : "+moth_days[3]+" Day's");
    }
}
The above Program creates an array of the number of days in each month.

When you run this program, it prints the number of days in April. Java array indexes start with zero, so the number of days in April is month_days[3] or 30.
It is also possible to combine the declaration of the array variable with the allocation of the array itself as shown here.
class MyFirstArray{

    public static void main(String[] args) {
     
        int moth_days [] = new int[12];
 
    }
}
Array can be initialized when they are declared, the process is much the same as that used to initialize the simple types, An array initialize is a list of comma separated expressions surrounded by curly braces, the commas separated the values of the array elements, the array will automatically be created large enough to hold the number of elements you specify in the array initialize. There is no need to user new.

Example 
class MyFirstArray{

    public static void main(String[] args) {
       
        int moth_days [] = {30,28,31,30,31,30,31,31,31,31,30,31};
        System.out.println("April has : "+ moth_days[3]+" Day's");
 
    }
}
Java strictly checks to make sure that you do not accidentally try to store or refer to values outside of the range of the array, The Java run time system will check that all array indexes are in the correct range.If you try to access elements outside the range of the array, you will cause a run time error.

No comments:

Post a Comment