Iterate Over Arrays Using For and Foreach Loop in Java



In Java, both for loop and the for-each loop are used to iterate over each element of a stream or collection like arrays and ArrayList in order to perform desired operations.

In this article, we will learn how to iterate over elements of an array using for and for-each loop. Here, the array is a data structure which stores a fixed-size sequential collection of elements of the same data type.

Example Scenario 1

Input: String[] designations={"Ravi", "Riya", "Ashish"};
Output: Ravi, Riya, Ashish

Example Scenario 2

Input: int[] designations = {2, 4, 5, 7};
Output: {2,4,5,7}

Iterating Over Array Using for Loop

A for loop is used when we know the number of repetition of the loop. It consist of three parts namely initialization, boolean expression and increment.

The initialization statement contains starting value of loop variable, the boolean expression checks the condition and increment updates the loop variable after each iteration.

Syntax

Following is the syntax of for loop ?

for(initialization; boolean_expression; increment) {
   // Statements
}

Generally, size of an array is fixed. Therefore, for loop can be used to iterate through the elements of the given array.

Example

In this Java program, we use for loop to iterate over an array.

public class Main{
   public static void main(String[] args){
      // given array
      String[] mnc = {"Microsoft","Google","Facebook","Oracle"};
      // using for loop to iterate the array
      System.out.println("Printing array using for loop: ");
      for(int p = 0; p < mnc.length; p++){
         System.out.println(mnc[p]);
      }
   }
} 

On executing the above code, it will produce the following result ?

Printing array using for loop: 
Microsoft
Google
Facebook
Oracle

Iterating Over Array Using for-each Loop

The for-each loop is considered as an alternate and enhanced version of the for loop. It makes the iteration operation easier and less error prone. Unlike a for loop, the for-each loop does not use an index variable for iteration. Instead, it uses a separator named colon (:) to separate and traverse the collection and current element.

To control the number of iterations in for-each, we can use the return or break statement otherwise, it will iterate over each element of the given array.

Syntax

The for-each loop has the following Syntax ?

for(type var: array) {
   // statements
}

Example

The following example shows how to use for-each loop to iterate over a given array.

public class Main{
   public static void main(String[] args){
      // given array
      int[] item = {2, 34, 51, 8, 56, 90};
      // using for loop to iterate the array
      System.out.println("Printing array elements using for-each loop: ");
      for(int varbl : item){
         System.out.println(varbl);
      }
   }
} 

When you run the code, following output will be displayed ?

Printing array elements using for-each loop: 
2
34
51
8
56
90

Conclusion

In this article, the users explored the approaches to iterate over arrays for each and for loop using Java programming language. Whether, the users want any type of index so the users may utilize it for loop, or else it will be better to utilize it for each loop.

Updated on: 2024-08-01T11:48:10+05:30

452 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements