Java Array Simple Program

Learn complete Arrays in Java at https://www.javaguides.net/2018/08/java-array-basics-guide.html

Java Array Simple Program Example

The following program, ArrayDemo, creates an array of integers, puts some values in the array, and prints each value to standard output.
public class ArrayBasics {
        public static void main(String[] args) {
                // declares an array of integers
            int[] anArray;
   
           // allocates memory for 10 integers
            anArray = new int[10];
            
            // initialize first element
            anArray[0] = 100;
            // initialize second element
            anArray[1] = 200;
            // and so forth
            anArray[2] = 300;
            anArray[3] = 400;
            anArray[4] = 500;
            anArray[5] = 600;
            anArray[6] = 700;
            anArray[7] = 800;
            anArray[8] = 900;
            anArray[9] = 1000;
 
            for (int i = 0; i < anArray.length; i++) {
                 System.out.println("Element at index " + i + ": " +anArray[i]);
            }
       }
}
Output:
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000

Reference


Comments