Thursday, November 25, 2010

Array in JAVA

Array is a group data which store same variable data type, Array has fixed length. It is declare syntax is below.

Example : int[] aArray = new int[10];

Here int[] aArray is an array declaration.

new int[10] means an array length is fixed and its allocate memory for 10 elements type int. Each item of array is called element. Array element accessed at index.

package example;

public class ExampleArray {

public static void main(String[] args) {

// Declare array of integer

int[] aArrays;

// Allocate memory for 10 Integer

aArrays = new int[10];

aArrays[0] = 100; // initialize first element

aArrays[1] = 101; // initialize second element

aArrays[2] = 102;

aArrays[3] = 103;

aArrays[4] = 104; // initialize fifth element

aArrays[5] = 105;

aArrays[6] = 106;

aArrays[7] = 107;

aArrays[8] = 108;

aArrays[9] = 109; // initialize 10th element

System.out.println("Element at 1st index is : "+aArrays[0]);

// -----

System.out.println("Element at 2nd index is : "+aArrays[2]);

// -----

System.out.println("Element at 9th index is : "+aArrays[9]);

}

}

Output is :

Element at 1st index is : 100

Element at 2nd index is : 102

Element at 9th index is : 109

No comments:

Post a Comment