Skip to content

Arrays – Storing Multiple Values in One Place

Seanti edited this page Mar 24, 2025 · 1 revision

Imagine you have a box of gumballs, each with a different color.

If you only had one variable, you could only store one gumball at a time:

String gumball1 = "Red";
String gumball2 = "Blue";
String gumball3 = "Green";

This works, but it’s too much work if you have 100 gumballs!

➡️ Instead of creating many variables, we use an array to store multiple values in one place.


1. What is an Array?

📌 An array is like a box that holds multiple values of the same type.

Example: A Box of Gumballs

public class GumballBox {
    public static void main(String[] args) {
        String[] gumballs = {"Red", "Blue", "Green", "Yellow", "Pink"};
        System.out.println(gumballs[0]); // Get the first gumball
        System.out.println(gumballs[3]); // Get the fourth gumball
    }
}

Output:

Red
Yellow

🔹 gumballs[0] → First item (Red)
🔹 gumballs[3] → Fourth item (Yellow)

➡️ Arrays use "index numbers" to access values.
➡️ The first item always starts at index 0, NOT 1!


2. Declaring and Initializing an Array

There are two ways to create an array:

1️⃣ Directly Assign Values

String[] gumballs = {"Red", "Blue", "Green", "Yellow"};
  • The array automatically knows there are 4 items.

2️⃣ Create an Empty Array and Add Values Later

String[] gumballs = new String[4]; // A box that holds 4 items
gumballs[0] = "Red";
gumballs[1] = "Blue";
gumballs[2] = "Green";
gumballs[3] = "Yellow";
  • We reserve space for 4 items and add them later.
  • If we don’t add values, Java fills them with default values (like null for Strings, 0 for numbers).

3. Getting the Length of an Array

To check how many items are in an array, use .length:

System.out.println(gumballs.length);

Example:

String[] gumballs = {"Red", "Blue", "Green", "Yellow"};
System.out.println("Total gumballs: " + gumballs.length);

Output:

Total gumballs: 4

➡️ .length tells us how many items are inside the array.


4. Looping Through an Array

Instead of printing each item one by one, we use a loop.

1️⃣ Using a For Loop

public class LoopExample {
    public static void main(String[] args) {
        String[] gumballs = {"Red", "Blue", "Green", "Yellow"};

        for (int i = 0; i < gumballs.length; i++) {
            System.out.println(gumballs[i]);
        }
    }
}

Output:

Red
Blue
Green
Yellow

🔹 The loop starts at i = 0 and goes up to gumballs.length - 1.
🔹 It prints each gumball in the array.


2️⃣ Using an Enhanced For Loop

This loop is simpler and works directly on arrays:

public class EnhancedLoopExample {
    public static void main(String[] args) {
        String[] gumballs = {"Red", "Blue", "Green", "Yellow"};

        for (String gumball : gumballs) {
            System.out.println(gumball);
        }
    }
}

Output:

Red
Blue
Green
Yellow

➡️ This loop automatically goes through all items in the array! 🎉


5. Changing Values in an Array

String[] gumballs = {"Red", "Blue", "Green"};
gumballs[1] = "Purple"; // Change Blue to Purple
System.out.println(gumballs[1]);

Output:

Purple

🔹 We replaced "Blue" with "Purple".


6. Multi-Dimensional Arrays (Arrays Inside Arrays!)

What if we need rows and columns, like a gumball vending machine?

public class MultiArray {
    public static void main(String[] args) {
        String[][] gumballMachine = {
            {"Red", "Blue", "Green"},
            {"Yellow", "Purple", "Pink"}
        };

        System.out.println(gumballMachine[1][2]); // Get "Pink"
    }
}

Output:

Pink

🔹 gumballMachine[1][2]Row 1, Column 2 (Pink)

➡️ Think of it as rows and columns, like a table!


7. Summary of Arrays

Feature Explanation Example
Store multiple values An array holds multiple values in one variable String[] colors = {"Red", "Blue", "Green"};
Use index numbers First item is 0, second is 1, etc. colors[0]"Red"
Find the length Use .length to check how many items colors.length3
Loop through an array Use for loop or for-each loop for (String color : colors) {}
Change values Assign a new value to an index colors[1] = "Purple";

8. What’s Next?

Now that we can store multiple values in arrays, we’ll learn about ArrayLists, which are like super flexible arrays that can grow and shrink! 🚀

Clone this wiki locally