Friday, December 01, 2006

3 Different Ways of Declaring, Creating and Initialising an Array

public class ArrayExample2 {
public static void main(String[] args) {

// Step 1: Declare
// Step 2: Create the array
// Step 3: Initialise

// Step 1
double temp0[];

// Steps1 +2
double temp1[] = new double[7];

// Step 3: initialising
temp1[0] = 37.5;
temp1[1] = 38.5;

// 3-in-1 method
// Declare,
double temp2[] = {37.5, 38.5, 39.5, 40.5,41.5};

System.out.println(temp2[2]);

// Retrieve the length of an array
System.out.println(temp2.length);

for (int i=0;i<temp2.length;i++){
System.out.println(temp2[i]);
}//end for



} //end main
} //end class

No comments: