Java Array - Create, Declare, Initialize and Print Examples

The following program demonstrates the usage of Arrays in Java. Learn complete Arrays in Java at https://www.javaguides.net/2018/08/java-array-basics-guide.html.

Java Array - Create, Declare, Initialize and Print Examples

Refer the comments in the below program are self-descriptive.

/**
 * Class demonstrate the basics of Array
 * @author sourcecodeexamples.net
 *
 */
public class JavaArrayDemonstration {

        public static void main(String[] args) {
                // initialize primitive one dimensional array
                int[] anArray = new int[5];

                anArray[0] = 10; // initialize first element
                anArray[1] = 20; // initialize second element
                anArray[2] = 30; // and so forth
                anArray[3] = 40;
                anArray[4] = 50;

                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + anArray[0]);
                System.out.println("Element 2 at index 1: " + anArray[1]);
                System.out.println("Element 3 at index 2: " + anArray[2]);
                System.out.println("Element 4 at index 3: " + anArray[3]);
                System.out.println("Element 5 at index 4: " + anArray[4]);

                // initialize Object one dimensional array
                String[] anArrayOfStrings = new String[5];
                anArrayOfStrings[0] = "abc"; // initialize first element
                anArrayOfStrings[1] = "xyz"; // initialize second element
                anArrayOfStrings[2] = "name"; // and so forth
                anArrayOfStrings[3] = "address";
                anArrayOfStrings[4] = "id";

                
                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + anArrayOfStrings[0]);
                System.out.println("Element 2 at index 1: " + anArrayOfStrings[1]);
                System.out.println("Element 3 at index 2: " + anArrayOfStrings[2]);
                System.out.println("Element 4 at index 3: " + anArrayOfStrings[3]);
                System.out.println("Element 5 at index 4: " + anArrayOfStrings[4]);

                Employee[] anArrayOfEmployees = new Employee[2];
                anArrayOfEmployees[0] = new Employee(10, "ramesh");
                anArrayOfEmployees[1] = new Employee(11, "john");

                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + anArrayOfEmployees[0].getName());
                System.out.println("Element 2 at index 1: " + anArrayOfEmployees[1].getName());

                
                // shortcut syntax to create and initialize an array
                int[] array = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };
                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + array[0]);
                System.out.println("Element 2 at index 1: " + array[1]);
                System.out.println("Element 3 at index 2: " + array[2]);
                System.out.println("Element 4 at index 3: " + array[3]);
                System.out.println("Element 5 at index 4: " + array[4]);
                
                // shortcut syntax to create and initialize an array
                String[] arrayOfStrings = {"abc", "xyz", "name", "address", "id"};
                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + arrayOfStrings[0]);
                System.out.println("Element 2 at index 1: " + arrayOfStrings[1]);
                System.out.println("Element 3 at index 2: " + arrayOfStrings[2]);
                System.out.println("Element 4 at index 3: " + arrayOfStrings[3]);
                System.out.println("Element 5 at index 4: " + arrayOfStrings[4]);
                
                
                // shortcut syntax to create and initialize an array
                Employee[] employees = {new Employee(10, "ramesh"),new Employee(11, "john")};
                // Each array element is accessed by its numerical index:
                System.out.println("Element 1 at index 0: " + employees[0].getName());
                System.out.println("Element 2 at index 1: " + employees[1].getName());
                
        }
}

class Employee {
        private int id;
        private String name;

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public Employee(int id, String name) {
                super();
                this.id = id;
                this.name = name;
        }
}
Output:
Element 1 at index 0: 10
Element 2 at index 1: 20
Element 3 at index 2: 30
Element 4 at index 3: 40
Element 5 at index 4: 50
Element 1 at index 0: abc
Element 2 at index 1: xyz
Element 3 at index 2: name
Element 4 at index 3: address
Element 5 at index 4: id
Element 1 at index 0: ramesh
Element 2 at index 1: john
Element 1 at index 0: 100
Element 2 at index 1: 200
Element 3 at index 2: 300
Element 4 at index 3: 400
Element 5 at index 4: 500
Element 1 at index 0: abc
Element 2 at index 1: xyz
Element 3 at index 2: name
Element 4 at index 3: address
Element 5 at index 4: id
Element 1 at index 0: ramesh
Element 2 at index 1: john

Reference

Comments