public static void main(String[] args) {
// Integer array from first array demo.
// Declare an array of integers.
int[] anIntegerArray;
// Allocate memory for 5 integers.
anIntegerArray = new int[5];
// Initialise the array elements.
anIntegerArray[0] = 100;
anIntegerArray[1] = 200;
anIntegerArray[2] = 300;
anIntegerArray[3] = 400;
anIntegerArray[4] = 500;
// Print out the contents of the array.
System.out.println("Integer array defined using the first method.");
System.out.println("Element at index 0: " + anIntegerArray[0]);
System.out.println("Element at index 1: " + anIntegerArray[1]);
System.out.println("Element at index 2: " + anIntegerArray[2]);
System.out.println("Element at index 3: " + anIntegerArray[3]);
System.out.println("Element at index 4: " + anIntegerArray[4]);
System.out.println("\n"); // Adds a blank line to the output.
/* Alternative method for declaring an array.
* The number of items in the array are determined by the
* number of items provided between the braces.
*
* NOTE: An "end of line" semi-colon has been used after the
* closing brace.
*/
int[] integerArray2 = {
500, 600, 700, 800, 900
};
System.out.println("Integer array defined using the second method.");
System.out.println("Element at index 0: " + integerArray2[0]);
System.out.println("Element at index 1: " + integerArray2[1]);
System.out.println("Element at index 2: " + integerArray2[2]);
System.out.println("Element at index 3: " + integerArray2[3]);
System.out.println("Element at index 4: " + integerArray2[4]);
}
}
No comments:
Post a Comment