Jan 24, 2026
## Chapter 9: Arrays (A Row of Boxes)
Imagine you want to store marks of 50 students. Creating `marks1`, `marks2`... is a nightmare. An **Array** is one variable that holds many values of the same type. Think of it as a row of lockers.
### 1. The "0" Confusion (Indexing)
In Java, we count from **0**, not 1.
**Example 1: Creating and Filling (Basic Example)**
```java
int[] marks = new int[5]; // 5 boxes
marks[0] = 90; // 1st box
marks[4] = 85; // 5th (last) box
```
**Example 2: The Manager's Scream (Common Mistake)**
```java
int[] arr = new int[5];
System.out.println(arr[5]); // CRASH! ArrayIndexOutOfBoundsException
```
*Teacher's Note:* If a hotel has 5 rooms numbered 0 to 4, and you ask for room 5, the manager (Java) screams! This is the most common error in Chapter 9.
### 2. The `length` Trap
**Example 3: No Brackets! (Explanation Focused)**
```java
int size = arr.length; // Correct
int size = arr.length(); // ERROR!
```
*Teacher's Note:* Arrays use `.length`, but Strings use `.length()`. Students mix this up for months. Remember: Arrays are simple "boxes," so they have a simple property. Strings are "tools," so they use a method.
### 3. Using Loops with Arrays
**Example 4: The Classroom Roll Call (Real-Life Example)**
```java
String[] students = {"Ravi", "Neha", "Aman"};
for (int i = 0; i < students.length; i++) {
System.out.println("Present: " + students[i]);
}
```
**Example 5: 2D Arrays (Future Hint)**
```java
int[][] matrix = new int[3][3];
```
*Teacher's Note:* Later, we'll learn about "Arrays of Arrays" for things like maps, grids, or Tic-Tac-Toe boards.