Iterate Over Array Using Foreach Loop In Java

Sharing is healthy !!

Hey guys!! Welcome to flower brackets. 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 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.

So let’s get started.

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 lastly “Fourteen”.

Alright, then we are going to use new version of for loop which is foreach loop.

First we need to write the keyword “for” and then a pair of parentheses and then the curly braces or body of the loop.

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 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.

Also Read – Java For Loop Example

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.

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 through “numbers” array.


conclusion

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

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

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

Sharing is healthy !!

Leave a Reply

Your email address will not be published.