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 foreach loop or new version of for loop with arrays and we can loop through all the elements without using variables such as index variables.

Also read – how are arrays stored in memory

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

Syntax:

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

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 foreach loop or new version of for loop. First we have to create an array of type string and variable name “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”.

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

Foreach loop 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 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 the 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 foreach loop 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.

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.

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 is only forward and in single steps.
  • Foreach loop 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 cannot obtain index. Hence can’t keep track of index.

Related Posts