Arrays in Java: Storing Multiple Values Together
A Simple Thought
Imagine keeping a list of your friends’ names. You could create one variable for each name, but that would be a lot of typing.
Computers face the same problem when they need to store many related items.
Instead of using many separate variables, Java lets you store all of them together in one container called an array.
In this lesson, you will learn how arrays help programs organize and manage information efficiently.
The Project: Grocery List Tracker
You will create a small program that stores and displays the names of five grocery items.
The program will print them one by one using a loop.
Here’s how your output should look:
1 2 3 4 5 6 7 8======================== GROCERY LIST 1. Milk 2. Bread 3. Eggs 4. Butter 5. Sugar ========================
You will store the items in an array and then use a loop to display them.
The Concept: Arrays
An array is like a shelf with boxes, each holding one piece of data.
All boxes in the array must hold the same type of data, such as all strings or all numbers.
Here is how you declare an array in Java:
1String[] items = {"Milk", "Bread", "Eggs", "Butter", "Sugar"};
In this example:
- String[] tells Java that this array will hold text values.
- The values inside {} are the items stored in the array.
- The square brackets [] show that this variable can store more than one value.
You can access each item by its position number, which is called an index.
Indexes start at 0, so items[0] means the first element, which is "Milk".
Why This Matters
Arrays are used everywhere in programming.
They help you manage groups of related data efficiently.
Think about how many lists exist in real life and in software:
- A list of students in a class.
- A list of scores in a game.
- A list of products in an online store.
Without arrays, you would have to create a separate variable for each one, which would quickly become messy and hard to manage.
Let’s Build the Grocery List
Here’s the complete program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15public class GroceryList { public static void main(String[] args) { String[] items = {"Milk", "Bread", "Eggs", "Butter", "Sugar"}; System.out.println("========================"); System.out.println(" GROCERY LIST"); for (int i = 0; i < items.length; i++) { System.out.println(" " + (i + 1) + ". " + items[i]); } System.out.println("========================"); } }
When you run this, the program will print each grocery item along with its number in the list.
The loop repeats as many times as there are items in the array, which is controlled by items.length.
How It Works
- The array items stores five grocery names.
- The loop starts at i = 0, which refers to the first element.
- The loop continues while i is less than items.length, meaning while there are items left in the list.
- Each time the loop runs, it prints the current item.
- The (i + 1) adds one to the index to make the list start from 1 instead of 0 for better readability.
This way, your program can easily handle multiple values with just one variable.
Learn Together with AI
If you are using an AI Copilot, you can quickly generate and explore variations of this code.
Try this prompt:
“Write a Java program that stores five grocery items in an array and prints them with numbers.”
Then ask it for changes:
- “Add the prices for each item and print both name and price.”
- “Sort the list alphabetically.”
- “Print the items in reverse order.”
Each variation will help you understand new ways to use arrays with loops.
Practice Challenge
Create a program that stores the names of five favorite movies in an array and prints them in this format:
1 2 3 4 5 6 7 8======================== FAVORITE MOVIES 1. Inception 2. Interstellar 3. Avatar 4. The Dark Knight 5. Titanic ========================
Use a loop to print the movies one by one.
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15public class FavoriteMovies { public static void main(String[] args) { String[] movies = {"Inception", "Interstellar", "Avatar", "The Dark Knight", "Titanic"}; System.out.println("========================"); System.out.println(" FAVORITE MOVIES"); for (int i = 0; i < movies.length; i++) { System.out.println(" " + (i + 1) + ". " + movies[i]); } System.out.println("========================"); } }
When you run it, Java will print your movie list cleanly. You can add more items to the array at any time, and the loop will automatically adjust to the new size.
Coming Up Next
You now know how to store and print multiple values using arrays.
Next, you will take this idea one step further by using loops with arrays to perform calculations and summaries, such as finding the total or average of a set of numbers.