Array of objects

Introduction

An array of objects is a collection that stores references to instances of a class. This allows you to manage and manipulate multiple objects in a structured manner. Arrays of objects are useful when you need to handle a fixed number of related objects efficiently.

Creating an Array of Objects

Creating an array of objects involves two main steps:

  1. Declare the array
  2. Instantiate the array elements

Step 1: Declare the Array

First, you declare an array that can hold references to objects of a specific class.

ClassName[] arrayName;

Step 2: Instantiate the Array

Next, you create the array and instantiate its elements.

arrayName = new ClassName[arraySize];

You can combine both steps in a single line:

ClassName[] arrayName = new ClassName[arraySize];

Example: Array of Objects

Let’s create an array of Person objects, where the Person class has two attributes: name and age.

Defining the Person Class

public class Person {
    private String name;
    private int age;

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

    // Getter for name
    public String getName() {
        return name;
    }

    // Getter for age
    public int getAge() {
        return age;
    }

    // Overriding toString() method for easy display
    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}

Creating and Using an Array of Person Objects

public class Main {
    public static void main(String[] args) {
        // Declare and instantiate an array of Person objects
        Person[] people = new Person[3];

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

        // Access and display the array elements
        for (Person person : people) {
            System.out.println(person);
        }
    }
}

Dynamic Initialization of Array Elements

You can also dynamically initialize the elements of an array of objects, perhaps based on user input or data from a file.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Declare and instantiate an array of Person objects
        Person[] people = new Person[2];

        // Dynamically initialize the array elements
        for (int i = 0; i < people.length; i++) {
            System.out.println("Enter name for person " + (i + 1) + ":");
            String name = scanner.nextLine();
            System.out.println("Enter age for person " + (i + 1) + ":");
            int age = scanner.nextInt();
            scanner.nextLine(); // Consume newline
            people[i] = new Person(name, age);
        }

        // Access and display the array elements
        for (Person person : people) {
            System.out.println(person);
        }

        scanner.close();
    }
}

Advantages of Using Arrays of Objects

  1. Efficiency: Arrays provide an efficient way to store and access multiple objects.
  2. Organization: Arrays help organize related objects in a single structure, making the code easier to manage.
  3. Iteration: You can easily iterate over the elements of an array using loops.

Limitations of Using Arrays of Objects

  1. Fixed Size: Arrays have a fixed size, which must be specified at the time of creation. This can be a limitation if the number of objects is not known beforehand.
  2. Homogeneous Elements: Arrays can only store objects of the same type. If you need to store different types of objects, you would need to use a more flexible data structure like a List.
  3. Memory Management: You need to manually manage memory for array elements, ensuring that objects are properly initialized and accessed.