Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways.
Method 1: Using for loop:
This is the simplest of all where we just have to use a for loop where a counter variable accesses each element one by one.
1 2 3 4 5 6 7 8
Method 2: Using for each loop :
For each loop optimizes the code, save typing and time.
1 2 3 4 5 6 7 8
This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Iterating over ArrayLists in Java
- Iterating over Enum Values in Java
- Remove an Entry using key from HashMap while Iterating over it
- Remove an Entry using value from HashMap while Iterating over it
- Java.util.Arrays.parallelSetAll(), Arrays.setAll() in Java
- Why to use char array over a string for storing passwords in Java?
- Program to Iterate over a Stream with Indices in Java 8
- How to loop over TreeSet in Java
- How to iterate over a TreeMap in Java?
- How to iterate over a 2D list (list of lists) in Java
- When to use StringJoiner over StringBuilder?
- Sending a Text Message Over the Phone Using SmsManager in Android
- Java.util.Arrays.equals() in Java with Examples
- Java.util.Arrays.deepEquals() in Java
- Java.util.Arrays.copyOfRange() in Java
- Java.util.Arrays.parallelPrefix in Java 8
- Arrays in Java
- Final arrays in Java
- How to compare two arrays in Java?
- Arrays class in Java