Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

Thursday, October 2, 2008

Arrays in Java Programming

Array is the most important thing in any programming language. By definition, array is the static memory allocation. It allocates the memory for the same data type in sequence. It contains multiple values of same types. It also store the values in memory at the fixed size. Multiple types of arrays are used in any programming language such as: one - dimensional, two - dimensional or can say multi - dimensional.

An array is a sequence of memory locations for storing data set out in such a way that any one of the individual locations can be accessed by quoting its index number.


Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

Declaring Java Arrays

To declare an array, write the data type, followed by a set of square brackets [ ], followed by the identifier name.

ElementType [ ] arrayName;

Example:

int [ ] age;
float grades [ ];
String name [ ];

Initializing Array Variables

To initialize an array, you use the new operator to allocate memory for objects. The new operator is followed by the data type with the number of elements to allocate specified the number of elements to be allocated is placed within the [ ] operator.

Example:

age = new int [5];
grades = new float [10];
name = new String [100];

Like other variables, an array variable can be initialized when it is declared.

ElementType[ ] arrayName = new ElementType [ sizeOfArray];

Example:

int [ ] age = new int [ 5 ];
float [ ] grades = new float [10];
String name [ ] = new String [100];

Sometimes user declares an array and it's size simultaneously. You may or may not be define the size in the declaration time. An array can be also created by directly initializing it with data.

Example:

int [ ] arr = {1, 2, 3, 4, 5};

This statement declares and creates an array of integers with five elements, and initializes this with the values 1, 2, 3, 4, and 5.

String [ ] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

This statement declares and creates an array of string with identifier days and initialized. This array contains 7 elements.

Accessing an Array Element

To access an array element, or a part of the array, use a number called an index or a subscript.

An index number or subscript assigned to each member of the array, to allow the program to access an individual member of the array.

It is an integer beginning at zero and progresses sequentially by whole numbers to the end of the array. Index is from 0 to (sizeOfArray - 1).

Example:

//assigns 5 to the first element in the array
age [0] = 5;

// prints the last element in the array
System.out.println(age[4]);

Here is the code of the program:

import java.io.*;

public class sampleArray
{
public static void main (String args []) throws IOException
{
BufferedReader x=new BufferedReader (new InputStreamReader (System.in));
int a;
int num [] = new int[10];

for ( a=0; a<10;>> ");
num[a] = Integer.parseInt(x.readLine());
}

System.out.print("\n\n The inputted numbers are : \n");

for(a=0; a<10; style="font-weight: bold;">Sample Output:

Sample Array Output
Try to create a program that will display the sum of 5 numbers using one dimensional array.