🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #53

Loop Through an Array


Loop Through an Array

You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.

This example creates an array of strings and then uses a for loop to print each element, one by one:

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

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

Here is a similar example with numbers. We create an array of integers and use a for loop to print each value:

Example

int[] numbers = {10, 20, 30, 40};

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

Calculate the Sum of Elements

Now that you know how to work with arrays and loops, let's use them together to calculate the sum of all elements in an array:

Example

int[] numbers = {1, 5, 10, 25};
int sum = 0;

// Loop through the array and add each element to sum
for (int i = 0; i < numbers.length; i++) {
  sum += numbers[i];
}

System.out.println("The sum is: " + sum);

Loop Through an Array with For-Each

There is also a "for-each" loop, which is used exclusively to loop through elements in an array (or other data structures):

Syntax

for (type variable : arrayname) {
  // code block to be executed
}

The colon (:) is read as "in". So you can read the loop as: "for each variable in array".

The following example uses a for-each loop to print all elements in the cars array:

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

for (String car : cars) {
  System.out.println(car);
}

This means: for each String in the cars array (here called car), print its value.

Compared to a regular for loop, the for-each loop is easier to write and read because it does not need a counter (like i < cars.length). However, it only gives you the values, not their positions (indexes) in the array.

So, if you need both the position (index) of each element and its value, a regular for loop is the right choice. For example, when printing seat numbers in a theater row, you need to show both the seat number (the index) and who is sitting there (the value):

Example

String[] seats = {"John", "Jenny", "Liam", "Bo"};

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

Note: The for-each loop is great when you only need to read elements. If you want to change the elements later, or keep track of their index, use a regular for loop instead.

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Loop Through an Array – FAQs

Quick answers about learning Loop Through an Array in Java.

This free note from CodingNow 2.0 explains Loop Through an Array in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Loop Through an Array, is 100% free with no signup required.
With focused practice, most students grasp Loop Through an Array in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now