Sunday 7 June 2015

First Array Demo

public class firstArrayDemo {

        /* An array is like a box of boxes with each box containing
         * an item or value.
         */

        public static void main(String[] args) {
                // 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("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]);
        }
}

No comments:

Post a Comment

Translate