Java 08: Array

Array is a simple data structure used to store and manage data in organized way. A group of related data are collected and represented as one unit. Processing data with array is efficient and easy to maintain data. Array and for loop goes hand in hand in most of the program designs.

Data is stored in sequential order. Accessed using index and size of the array is fixed. This makes array very suitable with for loop.This allows compiler to allocate required space, but at same time makes array static and need extra measures to make array dynamic when it comes to store any additional data.

We already have variable to store data, why array? Sometimes there is need for us to design program that has same name for variable. I mean if the age of a person is age. we could think of other synonym for age so that variable name is unique and not repeated. Lets say that you want to process the value of age for 10 person, you could declare 10 variables like this.

int age,
int year_old;
int years_since_born;
// and so on
or
int age1;
int age2;
int age3;
//...

Processing an individual variable like this might be troublesome, because you need to spell out each variable one by one without making mistake. For example

average_age = age1 + age2 + age3;

This makes more tedious job to even think of if we want to calculate the average age for a group of 100 even 10000 of peoples. The solution is array. Array is a collection or a group of data. The data stored in the array are from same type.

Declaring an Array

Same like variable, an array must be declared before using it. When declaring an array we need to specify data type of the elements that we want to store in this array and also the reference to the array.

Array declaration syntax is:

dataType[] arrayName;

the dataType can be any type data, and all data stored in this array will be same data type. For example to declare an array that stores double value we declare as follows:

double[] anArray;

Now if try to access this array compiler will say there is no array. This is because we did not create the array yet. Declaration of an array only allocates memory to hold the address of the array. If no reference to array is specified it should ne null.

Create an Array

To create an array we need to use new keyword. The name of the keyword is new. When creating the array we need to specify the size of the array. The syntax looks like this:

arrayName = new dataType[size];

lets create array for previous example.

anArray = new double[10];

this statement will do two important thing. First it creates an array of size 10. Later it assigns the address to this array to variable anArray.

Vary often we can see declaring array variable, creating array and assigning the address to the variable in a single statement.The syntax is:

dataType[] arrayName = new dataType[arraySize];

So, for storing age of then person we might write:

int age[] = new int[10];

and if you decided that you want to increase the number of person to 1000, just change the arraySize.

int age[] = new int[1000];

Now we have successfully declare, create and assign array, but the array contains no data. To add data to the array we need to initialize the array using assignment statement.

arrayName[index]  = value;

When initializing the array we use the index number of the element found in the array. The index number always starts from 0 and index number of the last element is arraySize -1.

So to store data to array age, the statement will looks as follows,

age[0] = 23;
age[1] = 40;
age[2] = 33;

// and so on

Array initializer combines the array declaration, creation and initialization in one statement. The syntax looks like this:

dataType[] arrayName = {value1,value2, …, valueN};

Using array initialize method we write like this:

int age[] = {23, 40, 33,25, 27, 31, 36, 21, 26, 30};

This statement will automatically create an integer value with the size depending on how many values we write inside the curly bracket. But once the array is created the size of it cannot be changed during program execution.

After the array is created, it is ready to be processed. One of common array processing is simply display the elements or data of the array. Array are used together with a for loop.

The following will display all the elements contain inside the array.

for(int i = 0; i < 10; i++){
    System.out.println(age[i]);
}

The output is

23
40
33
25
27
31
36
21
26
30

Explanation on the for loop

naturally array starts with 0 so we initialize variable i with 0. Next is the stopping condition. we want to display until the last element of the array. The index number for last element is 9. the i++ operation at the end of the for loop will keep increment value of i by 1. So we need to keep counting until 9. so the value of i reached then to loop is finished. Every time the loop is entered System.out.println() will display element at index i inside array age.

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net