Introduction
In Java, an array is a way to store many values of the same type — like a list of numbers or words.
When we loop through an array, we go through each value one by one, so we can do something with it — like print it or use it in a calculation.
In this lesson, we will learn 4 ways to loop through arrays:
forloopwhileloop- Improved
for-eachloop - A modern way used by professional programmers: Stream API
1. The for Loop
The for loop uses indexes to go through the array.
Example:
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
iis the index — it starts at 0.numbers.lengthmeans how many elements are in the array.- The loop runs as long as
iis smaller than the length of the array.
2. The while Loop
while is useful when we don’t know how many steps we need, but we know the condition to stop.
Example:
int[] numbers = {10, 20, 30, 40, 50};
int i = 0;
while (i < numbers.length) {
System.out.println("Element at index " + i + ": " + numbers[i]);
i++;
}
The loop continues while i < numbers.length is true.
3. The Improved for-each Loop
for-each is a simple loop that doesn’t use indexes.
We use it when we want to do something with every element.
Example:
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
System.out.println("Current element: " + number);
}
numberis a temporary variable with the current value.- It’s shorter and easier than using a normal
forloop.
Practice Tasks
- Sum of all elements
Create an array with 10 numbers and find the total sum using aforloop. - Find the biggest and smallest number
Create an array with 8 numbers. Use awhileloop to find the largest and smallest numbers. - Longest string
Create an array of city names. Use afor-eachloop to find the longest name. - Check for duplicate numbers
Check if the array contains two of the same numbers. Use twoforloops. - Create a new array with doubled values
Create a number array. Make a new array where every number is double the value of the original one.
Solutions
1. Sum of elements:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
System.out.println("Total sum: " + sum);
2. Max and Min values:
int[] numbers = {45, 12, 78, 4, 89, 23, 56, 31};
int max = numbers[0];
int min = numbers[0];
int i = 1;
while (i < numbers.length) {
if (numbers[i] > max) max = numbers[i];
if (numbers[i] < min) min = numbers[i];
i++;
}
System.out.println("Max: " + max);
System.out.println("Min: " + min);
3. Longest string:
String[] cities = {"Stockholm", "London", "Tokyo", "New York", "Madrid"};
String longestCity = "";
for (String city : cities) {
if (city.length() > longestCity.length()) {
longestCity = city;
}
}
System.out.println("Longest city name: " + longestCity);
4. Check for duplicates:
int[] numbers = {10, 20, 30, 20, 50};
boolean hasDuplicates = false;
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] == numbers[j]) {
hasDuplicates = true;
break;
}
}
if (hasDuplicates) break;
}
if (hasDuplicates) {
System.out.println("There are duplicates.");
} else {
System.out.println("No duplicates found.");
}
5. New array with doubled values:
int[] original = {2, 4, 6, 8, 10, 12};
int[] doubled = new int[original.length];
for (int i = 0; i < original.length; i++) {
doubled[i] = original[i] * 2;
}
System.out.print("New array: ");
for (int num : doubled) {
System.out.print(num + " ");
}
4. How Professionals Do It
Advanced Java programmers use Stream API to write clean and powerful code.
Example:
import java.util.Arrays;
int[] numbers = {10, 20, 30, 40, 50};
Arrays.stream(numbers)
.forEach(number -> System.out.println("Current element: " + number));
To sort before printing:
int[] numbers = {40, 10, 50, 30, 20};
Arrays.stream(numbers)
.sorted()
.forEach(number -> System.out.println("Current element: " + number));
Using Stream API makes code shorter and more powerful — and it’s great to learn it later.
But first, it’s important to understand simple loops like for, while, and for-each.
They help you build a strong foundation in Java programming.
