|
| 1 | +# Array of Objects |
| 2 | + |
| 3 | +``` |
| 4 | +Let’s revisit Array! |
| 5 | +``` |
| 6 | +### Array |
| 7 | + |
| 8 | +``` |
| 9 | +Array is a linear data structure, which store similar kind of data! |
| 10 | +
|
| 11 | +It stores multiple values of the same data type in contiguous memory locations. |
| 12 | +
|
| 13 | +It allows data access efficiently using an index. |
| 14 | +
|
| 15 | +Arrays are non-primitive. They are type safe. |
| 16 | +
|
| 17 | +``` |
| 18 | +```java |
| 19 | +int[] a = new int[3]; |
| 20 | +``` |
| 21 | +``` |
| 22 | +int is a primitive type. |
| 23 | +
|
| 24 | +Java creates an array of 3 int slots in heap memory. |
| 25 | +
|
| 26 | +The reference variable a is stored in the stack. |
| 27 | +
|
| 28 | +a[0] = 100; // adding values to array |
| 29 | +a[1] = 101; |
| 30 | +a[2] = 103; |
| 31 | +``` |
| 32 | +``` |
| 33 | + Stack Heap |
| 34 | + --------- -------------------------------- |
| 35 | +| a | ---> | [100] [101] [103] | |
| 36 | + --------- -------------------------------- |
| 37 | +``` |
| 38 | + |
| 39 | +## What is Array of Objects ? |
| 40 | +``` |
| 41 | +An array of objects is an array that stores references to many objects of the same class. |
| 42 | +
|
| 43 | +Instead of creating each object one by one, we can group them in a single array and manage them together. |
| 44 | +
|
| 45 | +Array of objects is ideal for: |
| 46 | +
|
| 47 | +Students in a school → Student[] |
| 48 | +
|
| 49 | +Products in a cart → Product[] |
| 50 | +
|
| 51 | +Movies in a library → Movie[] |
| 52 | +
|
| 53 | +We can scale from 1 object to hundreds — all with the same logic. |
| 54 | +
|
| 55 | +``` |
| 56 | + |
| 57 | +## How to Create Array Of Objects? |
| 58 | + |
| 59 | +## Step 1: Declare array of references |
| 60 | + |
| 61 | +```java |
| 62 | + |
| 63 | +Person[] p = new Person[3]; // p = reference variable (lives in stack). |
| 64 | + // Person = user-defined class (type of object the array can hold). |
| 65 | + // new Person[3] → allocates an array of 3 references in the heap, each slot is initialized to null. |
| 66 | + // Here each memory location is going to act as reference variable |
| 67 | + // 3 memory locations created in heap act as reference variable |
| 68 | +``` |
| 69 | +``` |
| 70 | + Stack Heap |
| 71 | + --------- ----------------------------------------- |
| 72 | +| p | ---> | [null] [null] [null] | |
| 73 | + --------- ----------------------------------------- |
| 74 | +
|
| 75 | +``` |
| 76 | + |
| 77 | +``` |
| 78 | +Person is a user-defined class (non-primitive type). |
| 79 | +
|
| 80 | +Java creates an array of 3 null references in the heap. |
| 81 | +
|
| 82 | +The reference variable p is stored in the stack. |
| 83 | +
|
| 84 | +``` |
| 85 | + |
| 86 | +## Step 2: Create and assign objects |
| 87 | + |
| 88 | +```java |
| 89 | +p[0] = new Person("Joe", "S101", "A"); |
| 90 | +p[1] = new Person("Jack", "S102", "B"); |
| 91 | +p[2] = new Person("Jen", "S103", "C"); |
| 92 | + |
| 93 | +``` |
| 94 | + |
| 95 | +### What Does This Mean? |
| 96 | + |
| 97 | +``` |
| 98 | +Each element in the array p is a reference to a Person object: |
| 99 | +
|
| 100 | +p[0] → Person("Joe", "S101", "A") |
| 101 | +
|
| 102 | +p[1] → Person("Jack", "S102", "B") |
| 103 | +
|
| 104 | +p[2] → Person("Jen", "S103", "C") |
| 105 | +
|
| 106 | +Each one is a separate object in memory, but all are of type Person. |
| 107 | +
|
| 108 | +``` |
| 109 | +``` |
| 110 | + Stack Heap |
| 111 | + --------- ----------------------------------------- |
| 112 | +| p | ---> | [ref0] [ref1] [ref2] |. Now, each reference in the array points to an actual Person object in the heap. |
| 113 | + --------- ----------------------------------------- |
| 114 | + | | | |
| 115 | + v v v |
| 116 | + Joe Jack Jen |
| 117 | + (p[0]) (p[1]) (p[2]) |
| 118 | +``` |
| 119 | +## Using for-each Loop in Array of Objects |
| 120 | +``` |
| 121 | +Here, Person is the class type used for each element. |
| 122 | +
|
| 123 | +``` |
| 124 | +```java |
| 125 | +for (Person per : p) { |
| 126 | + System.out.println(per); |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | + |
| 131 | +## Revisit: for-each with Primitive Array ! |
| 132 | + |
| 133 | +```java |
| 134 | +int[] arr = new int[3]; |
| 135 | +for (int a : arr) { |
| 136 | + System.out.print(a); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Summary Table: Array vs Array of Objects: |
| 141 | +``` |
| 142 | +| Concept | Array | Array of Objects | |
| 143 | +| --------------- | --------------------------- | ------------------------------ | |
| 144 | +| Data Type Used | Primitive (`int`, `double`) | Class/Object (`Person`) | |
| 145 | +| Memory (Heap) | Stores actual values | Stores references to objects | |
| 146 | +| Default Values | `0`, `0.0`, `false`, etc. | `null` | |
| 147 | +| Object Creation | Direct via index | Must use `new` for each object | |
| 148 | +``` |
| 149 | +## CONCEPT CHECK |
| 150 | + |
| 151 | + |
| 152 | +## 1. What is an array of objects in Java? |
| 153 | +``` |
| 154 | +An array of objects is an array that stores references to many objects of the same class. |
| 155 | +
|
| 156 | +Instead of creating each object one by one, we can group them in a single array and manage them together. |
| 157 | +
|
| 158 | +``` |
| 159 | + |
| 160 | + |
| 161 | +## 2. How does memory allocation work in an array of objects? |
| 162 | + |
| 163 | +``` |
| 164 | +
|
| 165 | +- The array of references is created in heap memory. |
| 166 | +
|
| 167 | +- Each object must be created separately using `new`. |
| 168 | +
|
| 169 | +- Until initialized, the references are `null`. |
| 170 | +
|
| 171 | +``` |
| 172 | + |
| 173 | +## 3. If you declare Person[] p = new Person[3]; but don’t initialize with new - what happens when you try to access p[0]? |
| 174 | + |
| 175 | +``` |
| 176 | +We’ll get a NULL POINTER EXCEPTION because the array only holds NULL references until objects are assigned. |
| 177 | +
|
| 178 | +``` |
| 179 | + |
| 180 | +## 4. How do you iterate over an array of objects? |
| 181 | + |
| 182 | +Using for or for-each loop: |
| 183 | + |
| 184 | +```java |
| 185 | +for (Person person : p) { |
| 186 | + System.out.println(person.getName()); |
| 187 | +} |
| 188 | +``` |
| 189 | +## 5. Can you sort an array of objects? |
| 190 | +``` |
| 191 | +Yes - Using Arrays.sort() along with: Comparable (natural ordering) or Comparator (custom ordering) |
| 192 | +``` |
| 193 | +## 6. Can an array of objects hold different types? |
| 194 | +``` |
| 195 | +No, All elements must be of the declared type or its subclasses. |
| 196 | +Example: Animal[] can hold Dog and Cat objects (since both extend Animal). |
| 197 | +``` |
| 198 | +## 7. What's the difference between Person[] and ArrayList<Person>? |
| 199 | +``` |
| 200 | +Array → Fixed size, cannot grow dynamically. |
| 201 | +
|
| 202 | +ArrayList → Resizable, provides more flexibility with dynamic data management. |
| 203 | +
|
| 204 | +``` |
| 205 | + |
0 commit comments