Java Series: Part 5

Table of Contents

📦 Part 5: Java Arrays – Store and Access Multiple Values

Ever wanted your code to remember a list of things? Like scores in a game, names of friends, or numbers from a quiz? That’s where arrays come in!

In Java, an array is like a row of boxes where each box holds a value — and each box has a number (called an index).


🧠 What Is an Array in Java?

An array lets you store multiple values in a single variable. Instead of writing this:

int score1 = 80;
int score2 = 90;
int score3 = 70;

You can write this:

int[] scores = {80, 90, 70};

Now you have all your scores in one place! Easy to manage. Easy to loop through.


🔢 How Do You Access an Array?

You use the index number. In Java, counting starts at 0. So:

System.out.println(scores[0]); // prints 80
System.out.println(scores[1]); // prints 90
System.out.println(scores[2]); // prints 70

⚠️ Important: If you try to access something that doesn’t exist (like scores[3]), you’ll get an error.


📥 Declaring Arrays in Java

There are two main ways to declare arrays:

// Method 1: Declare and assign values
String[] names = {"Rawisha", "Anna", "Leo"};

// Method 2: Declare first, then assign
int[] numbers = new int[3]; // makes room for 3 numbers
numbers[0] = 100;
numbers[1] = 200;
numbers[2] = 300;

🔁 Looping Through an Array

You can use a loop to print all values in an array:

for (int i = 0; i < scores.length; i++) {
    System.out.println("Score: " + scores[i]);
}

This works with any array! Just use array.length to get the number of items.


🎯 Real Example: Display Favorite Foods

String[] foods = {"Pizza", "Tacos", "Sushi"};

for (int i = 0; i < foods.length; i++) {
    System.out.println("I love " + foods[i]);
}

Output:

I love Pizza
I love Tacos
I love Sushi

👀 Common Mistakes to Avoid

  • ❌ Forgetting arrays start at 0
  • ❌ Trying to access an index that doesn’t exist
  • ❌ Mixing up array types (int[] ≠ String[])

🧪 Mini Challenge: Store & Print 5 Favorite Numbers

Create an array with 5 numbers and print them using a loop.

int[] myNumbers = {7, 14, 21, 28, 35};

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

🧭 What’s Next?

In Part 6, we’ll explore:

  • How to create your own methods (aka reusable code blocks)
  • What void and return mean
  • Why breaking code into pieces makes everything easier

✨ You’re now storing and using data like a real coder. High five!


📌 Java Array Cheat Sheet

// Declare and assign
int[] ages = {21, 25, 30};

// Access item
System.out.println(ages[0]);

// Get array length
System.out.println(ages.length);

// Loop through array
for (int i = 0; i < ages.length; i++) {
    System.out.println(ages[i]);
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Want to keep up with our blog?

Get our most valuable tips right inside your inbox, once per month!

Related Posts

0
Would love your thoughts, please comment.x
()
x