Skip to content

Java Arrays 🏨¢

Prerequisites: Java Loops

Mentor's Note: An Array is just a Big Building with multiple Rooms inside. Instead of having 100 separate boxes in your storage room, you have one giant shelf where everything is organized by a number! πŸ’‘


🌟 The Scenario: The Hotel Rooms 🏨¢

Imagine you are the manager of a luxury hotel.

  • The Array (The Hotel): You have One Big Building (One variable: cars) with multiple Rooms (Values) inside. 🏨
  • The Index (The Room Number): Each room has a Number, but the reception starts counting from 0. So the first room is #0, the second is #1, etc. πŸ”’
  • The Length (The Capacity): Once the hotel is built, you Cannot add more rooms. It is a Fixed Size. 🧱
  • The Result: You can quickly find, change, or list every "Guest" in the building using just their room number. βœ…

🎨 Visual Logic: The Hotel Map¢

Index 0 1 2 3
Guest (Value) Volvo BMW Ford Mazda
graph LR
    A[Room 0: Volvo πŸš—] --- B[Room 1: BMW πŸš—]
    B --- C[Room 2: Ford πŸš—]
    C --- D[Room 3: Mazda πŸš—]

πŸ“– Concept ExplanationΒΆ

1. Declaring & Initializing πŸ—οΈΒΆ

In Java, we use square brackets [] to say: "This is a collection, not a single box." - Syntax: String[] cars = {"Volvo", "BMW"};

2. Accessing Elements πŸ”’ΒΆ

You use the index number inside brackets to "Knock on the door" of a specific room. - System.out.println(cars[0]);

3. Array Length πŸ“ΒΆ

The length property tells you how many rooms are in the building. - Rule: Indexes always go from 0 to length - 1.

4. Multi-Dimensional Arrays 🏒¢

Think of this as a building with Floors. - hotel[1][2] means: Floor 1, Room 2.


πŸ’» Implementation: The Hotel LabΒΆ

// πŸ›’ Scenario: Managing a Car Garage
// πŸš€ Action: Storing and looping through an array

public class Main {
    public static void main(String[] args) {
        // πŸ“¦ 1. Create a "Hotel" of cars
        String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

        // 🏷️ 2. Change a guest in Room 0
        cars[0] = "Tesla"; 

        // πŸ“ 3. Find the total number of rooms
        System.out.println("Total Cars: " + cars.length);

        // πŸ”„ 4. List every guest (Loop)
        System.out.println("\n--- Guest List ---");
        for (int i = 0; i < cars.length; i++) {
            System.out.println("Room #" + i + ": " + cars[i]);
        }
    }
}

πŸ“Š Sample Dry Run (Logic)ΒΆ

Step Action Computer's Logic Result
1 cars[0] Go to the very first room "Volvo" 🏨
2 cars[0] = "Tesla" Replace the guest in Room 0 "Tesla" in Room 0 βœ…
3 cars[4] Go to Room 4 (Out of Bounds!) ERROR (Crash!) 🚫

πŸ“ˆ Technical Analysis: Fixed Size 🧠¢

A key rule in Java is that an Array cannot be resized. If you create an array of size 5, it will ALWAYS be size 5. If you want to add a 6th item later, you have to build a New Hotel! πŸ›‘οΈ - Alternative: For flexible sizes, professionals use ArrayList.


🎯 Practice Lab πŸ§ͺΒΆ

Task: The Score Calculator

Task: Create an int[] of 5 exam scores. Calculate the Sum and Average of these scores using a loop. Goal: Master the index and the .length property. πŸ’‘


πŸ’‘ Interview Tip πŸ‘”ΒΆ

"Interviewers often ask: 'What is the difference between length and length()?' Answer: length is a Property used for Arrays, but length() is a Method used for Strings. Don't mix them up! ⚠️"


πŸ’‘ Pro Tip: "Arrays are the most efficient way to store data because they are contiguous in memory. Use them whenever you know the fixed size of your data!" - Vishnu Damwala


← Back: Break & Continue | Next: Module 4 - Methods β†’