-
Notifications
You must be signed in to change notification settings - Fork 0
Arrays – Storing Multiple Values in One Place
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.
📌 An array is like a box that holds multiple values of the same type.
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
}
}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!
There are two ways to create an array:
String[] gumballs = {"Red", "Blue", "Green", "Yellow"};- The array automatically knows there are 4 items.
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
nullfor Strings,0for numbers).
To check how many items are in an array, use .length:
System.out.println(gumballs.length);String[] gumballs = {"Red", "Blue", "Green", "Yellow"};
System.out.println("Total gumballs: " + gumballs.length);Total gumballs: 4
➡️ .length tells us how many items are inside the array.
Instead of printing each item one by one, we use a 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]);
}
}
}Red
Blue
Green
Yellow
🔹 The loop starts at i = 0 and goes up to gumballs.length - 1.
🔹 It prints each gumball in the array.
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);
}
}
}Red
Blue
Green
Yellow
➡️ This loop automatically goes through all items in the array! 🎉
String[] gumballs = {"Red", "Blue", "Green"};
gumballs[1] = "Purple"; // Change Blue to Purple
System.out.println(gumballs[1]);Purple
🔹 We replaced "Blue" with "Purple".
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"
}
}Pink
🔹 gumballMachine[1][2] → Row 1, Column 2 (Pink)
➡️ Think of it as rows and columns, like a table!
| 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.length → 3
|
| 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"; |
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! 🚀