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
Release: 2025-02-07 11:51:10
Original
719 people have browsed it

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>
Copy after login

Scenario 2:

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

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:

for (initialization; condition; increment) {
   // Statements
}
Copy after login

Example:

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

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]);
      }
   }
}
Copy after login

Output:

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

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:

for (data_type element : array) {
   // Statements
}
Copy after login

Example:

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

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);
      }
   }
}
Copy after login

Output:

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

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!

Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template