Home >Java >javaTutorial >Java program to iterate over arrays using for and foreach loop

Java program to iterate over arrays using for and foreach loop

Susan Sarandon
Susan SarandonOriginal
2025-02-07 11:51:10598browse

Java program to iterate over arrays using for and foreach loop

Java offers two primary methods for iterating through arrays: the traditional for loop and the enhanced for-each loop. Both efficiently process array elements, but their applications differ slightly. This article demonstrates how to use each loop type for array traversal.

Scenario 1:

<code>Input: String[] names = {"Ravi", "Riya", "Ashish"};
Output: Ravi, Riya, Ashish</code>

Scenario 2:

<code>Input: int[] numbers = {2, 4, 5, 7};
Output: {2, 4, 5, 7}</code>

Iterating with a for Loop

The for loop is ideal when you need precise control over the iteration process, such as accessing element indices or performing conditional operations within the loop based on the index.

Syntax:

<code class="language-java">for (initialization; condition; increment) {
   // Statements
}</code>

Example:

This Java code uses a for loop to iterate through a string array:

<code class="language-java">public class ArrayIteration {
   public static void main(String[] args) {
      String[] companies = {"Microsoft", "Google", "Facebook", "Oracle"};
      System.out.println("Using a for loop:");
      for (int i = 0; i < companies.length; i++) {
         System.out.println(companies[i]);
      }
   }
}</code>

Output:

<code>Using a for loop:
Microsoft
Google
Facebook
Oracle</code>

Iterating with a for-each Loop (Enhanced for Loop)

The for-each loop simplifies iteration, eliminating the need for explicit index management. It's preferred when you only need to access each element's value without needing its index.

Syntax:

<code class="language-java">for (data_type element : array) {
   // Statements
}</code>

Example:

This example uses a for-each loop to iterate through an integer array:

<code class="language-java">public class ArrayIteration {
   public static void main(String[] args) {
      int[] values = {2, 34, 51, 8, 56, 90};
      System.out.println("\nUsing a for-each loop:");
      for (int value : values) {
         System.out.println(value);
      }
   }
}</code>

Output:

<code>Using a for-each loop:
2
34
51
8
56
90</code>

Conclusion

Both for and for-each loops provide effective ways to iterate through Java arrays. Choose the for loop when index access is crucial, and the for-each loop for simpler, index-free iteration.

The above is the detailed content of Java program to iterate over arrays using for and foreach loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Compiler Class In JavaNext article:Compiler Class In Java