Arrays in Java: Creation, Manipulation, and Traversal

Arrays are fundamental data structures in Java that allow storing multiple values of the same type in a single variable. They are fixed in size and allow indexed access to their elements.

1. Array Creation

In Java, an array is declared by specifying the type of elements it will hold, followed by square brackets. The array can be initialized at the time of declaration or later.

Array Declaration Syntax:

Example:

2. Array of User-Defined Objects

In Java, you can create an array that holds user-defined objects. Below is an example where we create an array of Person objects:

Example: Array of Objects

Let's create a Person class and use it to define an array of Person objects:


public class Person {
    String name;
    int age;

    // Constructor to initialize the object
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method to display person details
    public void displayDetails() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class ArrayOfObjects {
    public static void main(String[] args) {
        // Creating an array of Person objects
        Person[] people = new Person[3];

        // Initializing the objects in the array
        people[0] = new Person("Alice", 25);
        people[1] = new Person("Bob", 30);
        people[2] = new Person("Charlie", 35);

        // Traversing the array and calling displayDetails on each person
        for (Person person : people) {
            person.displayDetails();
        }
    }
}
      

The output of this example will be:


Name: Alice, Age: 25
Name: Bob, Age: 30
Name: Charlie, Age: 35
      

3. Array Manipulation

Arrays in Java are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Example of accessing and manipulating array elements:

4. Traversing Arrays

Traversing an array means accessing each element of the array. You can use a loop, such as a for loop or foreach loop, to iterate over the elements.

Using a for loop to traverse:

Using a foreach loop to traverse:

5. Example: Array Declaration, Manipulation, and Traversal

Here’s an example that demonstrates array creation, manipulation, and traversal in Java:


public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        
        // Modify an element
        numbers[2] = 10; // Change the third element to 10
        
        // Traverse the array using a for loop
        System.out.println("Using for loop:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
        
        // Traverse the array using a foreach loop
        System.out.println("Using foreach loop:");
        for (int num : numbers) {
            System.out.println(num);
        }
    }
}
      

The output of this example will be:


Using for loop:
1
2
10
4
5
Using foreach loop:
1
2
10
4
5
      

6. Array Length

The length property of an array returns the number of elements in the array. It’s important to note that length is a property (not a method) and doesn't require parentheses.

Example:

7. Multidimensional Arrays

In Java, you can also work with multidimensional arrays (arrays of arrays). Here’s an example of a two-dimensional array:


public class MultiDimensionalArrayExample {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        // Traverse the 2D array
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}
      

The output of this 2D array example will be:


1 2 3 
4 5 6 
7 8 9