Iterate Over Array Using Foreach Loop In Java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn how to iterate over array using foreach loop in java.

We can use this for each loop java or you know the new version of for loop with arrays and we can loop through all the elements without using variables such as index variables.

Also Read – Two Dimensional Array Java Program

For each loop in java optimizes loops and reduces the code hence saving time in typing code. The data type declared in java foreach loop must match datatype of the array or list that we are iterating.

Syntax of foreach loop in java :

for(<DataType of array><temporary variable name> : <Array to be iterated>)
{
   System.out.println(variable name);
}

So let’s get started with for each loop java example,

Example : iterate over array using foreach loop in java

public class ForEachDemo
{
   public static void main(String[] args)
   {
      String numbers[] = {"Eleven","Twelve","Thirteen","Fourteen"};
 
      System.out.println("---------------------------------------");
      System.out.println("Iterating over array using foreach loop");
      System.out.println("---------------------------------------");

      for(String num : numbers)
      {
         System.out.println(num);
      }

      System.out.println("---------------------------------------------");
      System.out.println("Iterating over array using classical for loop");
      System.out.println("---------------------------------------------");

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

Now let us see how to use this foreach loop or new version of for loop. First we have to create an array of type string and variable name will be numbers.

Since it’s going to be numbers array we need to use square brackets and then initialize the array.

So the first number is going to be “Eleven”, second “Twelve”, third “Thirteen” and “Fourteen”.

Alright, then we are going to use new version of for loop which is java foreach. First we need to write the keyword “for” and then a pair of parentheses and then the curly braces or body of the loop.

Also Read – 2d Array Java

For each loop java is going to loop through each element of the array that we pass.

For example, if you pass numbers array then this loop is going to loop through for each element of numbers array.

First thing we need to do is we need to create a variable which is going to contain one of element of numbers array.

Here our array elements are of type string so I have created a string variable and let’s call it as “num”.

Then add colon ( : ) and then the array name, that is, numbers array. Inside for each loop java we need to print out the value that will be present in “num” variable.

Also Read – Array Java

Now what happens in foreach loop is, for the first iteration “num” variable contain the first element of numbers array.

Or it will contain “Eleven” in this case. In the second iteration “num” variable will contain “Twelve”. Similarly foreach loop will loop through all the elements of the array.

Save above file and you will get this output,

Output:

Iterate Over Array Using Foreach Loop In Java

As you can see in the above output I have also used the classical for loop to loop(counter, condition, and increment) through “numbers” array.

NOTE

  • Iterating through characters in a string java is only forward and in single steps.
  • For each loop java is not suitable when you want to modify an array.
  • In enhanced for loop we cannot process another decision making statements (if else statement).
  • As we learnt earlier enhanced for loop java cannot obtain index. Hence can’t keep track of index.

conclusion

Well guys, that’s it about how to iterate over array using foreach loop in java. I hope you have understood the concept of for each loop java.

You can subscribe to my blog flower brackets, if you haven’t already.

If you found this post useful share it across social networks.

You May Also Like