ZC

In-Depth Notes on Arrays of Objects in Java

Overview of Object Arrays in Java

  • Definition of Arrays: An array is a collection of similar types of data stored in contiguous memory locations. Java allows creating arrays of objects, which can be of any type, including custom objects like Student or Rectangle.

Creating an Array of Objects

  • Steps to Create an Array of Objects:
    • Define an object class (e.g., Student).
    • Create an array of that object type.
    • Initialize the array with instances of the object.

Example: Array of Students

  1. Defining the Student Class:
    • Define properties like name, ID, and GPA.
    • Include a constructor to initialize these properties.
   class Student {
       String name;
       int ID;
       double GPA;

       Student(String name, int ID, double GPA) {
           this.name = name;
           this.ID = ID;
           this.GPA = GPA;
       }

       // ... other methods like toString() and getters
   }
  1. Creating an Array of Students:
    • Declare an array: Student[] students = new Student[3];
    • The memory for three Student objects is allocated.
   students[0] = new Student("Janet", 1, 3.5);
  1. Accessing and Printing Student Information:
    • Print using a loop:
   for (int i = 0; i < students.length; i++) {
       System.out.println(students[i]); // implicitly calls toString()
   }
  • Without a toString() method, printing will show the class name and memory address.

Importance of toString() Method

  • Automatic Call: When printing an object, the toString() method is called automatically.
  • Enhancing Readability: Overriding toString() provides a meaningful string representation of the object.

Example 2: Creating an Array of Rectangles

  1. Defining the Rectangle Class:
    • Class with properties such as length, width and methods like getArea() and overridden toString().
   class Rectangle {
       double length;
       double width;

       Rectangle(double length, double width) {
           this.length = length;
           this.width = width;
       }

       double getArea() {
           return length * width;
       }

       // ... add toString() method here
   }
  1. Creating an Array of Rectangles:
    • Declare and initialize objects in the array:
   Rectangle[] rectangles = new Rectangle[3];
   rectangles[0] = new Rectangle(2, 3);
   rectangles[1] = new Rectangle(4, 5);
   rectangles[2] = new Rectangle(10, 20);
  1. Printing Rectangle Information:
    • Use a loop to print results:
   for (int i = 0; i < rectangles.length; i++) {
       System.out.println(rectangles[i].getArea());
   }

Summary of Important Concepts

  • Constructor: Initializes object properties when creating an instance.
  • Memory Management: Arrays store references to objects, and Java handles memory allocation.
  • Inheritance and Object-Oriented Programming (OOP): The significance of methods like toString() showcases OOP principles like encapsulation and inheritance. Understanding these concepts helps in designing robust applications.
  • Practical Application: Understanding how to manipulate and utilize arrays of objects is crucial for software design, especially in fields like architecture (e.g., representing rooms as rectangles).