You can do an enhanced for loop (for java 5 and higher) for iteration on array's elements:

String[] elements = {"a", "a", "a", "a"};   
for (String s: elements) {           
    //Do your stuff here
    System.out.println(s); 
}
Answer from Michal on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_arrays_loop.asp
Java Loop Through an Array
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run. This example creates an array of strings and then uses a for loop to print each element, one by one:
🌐
Guru99
guru99.com › home › java tutorials › for-each loop in java
For-each loop in Java
November 8, 2024 - For(<DataType of array/List><Temp variable name> : <Array/List to be iterated>){ System.out.println(); //Any other operation can be done with this temp variable. } Let us take the example using a String array that you want to iterate over without ...
🌐
Java67
java67.com › 2013 › 08 › how-to-iterate-over-array-in-java-15.html
How to iterate over an Array in Java using foreach loop Example Tutorial | Java67
Though Java 1.5 foreach loop * is most elegant way of iterating over array, it doesn't provide any * counter, which is available in classic for loop. So, depending upon, whether * you need a counter or not, you can decide between Java 1.5 foreach or traditional * for loop. * * @author Javin */ public class JavaForEachOverArray { public static void main(String args[]) { String[] languages = {"Java", "Scala", "C++", "Ruby", "Python", "Perl"}; // looping over array using foreach loop System.out.println("Iterating over String array using Java 1.5 foreach loop"); for(String str : languages){ System
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit7-Arrays › topic-7-3-arrays-with-foreach.html
7.3. Enhanced For-Loop (For-Each) for Arrays — CS Java
for (type item: array) { //statements using item; } See the examples below in Java that loop through an int and a String array.
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-array-with-enhanced-for-loop-in-java
String Array with Enhanced For Loop in Java - GeeksforGeeks
April 8, 2023 - Enhanced for loop(for-each loop) was introduced in java version 1.5 and it is also a control flow statement that iterates a part of the program multiple times. This for-loop provides another way for traversing the array or collections and hence ...
🌐
Baeldung
baeldung.com › home › java › core java › guide to the java foreach loop
Guide to the Java forEach Loop | Baeldung
June 17, 2025 - The enhanced for-loop is an external iterator, whereas the new forEach method is internal. This type of iterator manages the iteration in the background and leaves the programmer to just code what is meant to be done with the elements of the collection. The iterator instead manages the iteration and makes sure to process the elements one by one. ... List<String> names = List.of("Larry", "Steve", "James", "Conan", "Ellen"); names.forEach(name -> LOG.info(name));
🌐
TutorialKart
tutorialkart.com › java › java-array › java-array-foreach
Java Array Foreach - Examples
November 23, 2020 - ForEach statement is also called enhanced for loop in Java. The advantage of for-each statement is that there is no need to know the length of the loop nor use index to access element during each iteration.
Find elsewhere
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit6-Arrays › topic-6-3-arrays-with-foreach.html
6.3. Enhanced For-Loop (For-Each) for Arrays — CSAwesome v1
To set up a for-each loop, use for (type variable : arrayname) where the type is the type for elements in the array, and read it as “for each variable value in arrayname”. You may have used a similar loop in AP CSP Pseudocode or App Inventor with lists like below.
🌐
ZetCode
zetcode.com › java › foreach
Java forEach - forEach on Java lists, maps, sets
Lambda expressions in Java provide a succinct way to implement functional interfaces, such as Consumer, by defining their behavior inline. They are constructed using the -> operator, which separates the parameters from the body of the expression, enhancing code readability and reducing boilerplate. ... void main() { List<String> items = new ArrayList<>(); items.add("coins"); items.add("pens"); items.add("keys"); items.add("sheets"); items.forEach((String name) -> { System.out.println(name); }); }
🌐
Alvin Alexander
alvinalexander.com › java › java-string-array-reference-java-5-for-loop-syntax
Java String array examples (with Java 5 for loop syntax) | alvinalexander.com
Before Java 5, the way to loop through an array involved (a) getting the number of elements in the array, and then (b) looping through the array with a for loop. Here’s a complete source code example that demonstrates the syntax prior to Java 5: public class JavaStringArrayTests1 { private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"}; // our constructor; print out the String array here public JavaStringArrayTests1() { // old `for` loop int size = toppings.length; for (int i=0; i<size; i++) { System.out.println(toppings[i]); } } // main kicks everything off.
Top answer
1 of 16
1310
for (Iterator<String> i = someIterable.iterator(); i.hasNext();) {
    String item = i.next();
    System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for ( : ) idiom, since the actual iterator is merely inferred.

As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.

If the right-hand side of the for (:) idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array.length instead. See the Java Language Specification.

for (int i = 0; i < someArray.length; i++) {
    String item = someArray[i];
    System.out.println(item);
}
2 of 16
559

The construct for each is also valid for arrays. e.g.

String[] fruits = new String[] { "Orange", "Apple", "Pear", "Strawberry" };

for (String fruit : fruits) {
    // fruit is an element of the `fruits` array.
}

which is essentially equivalent of

for (int i = 0; i < fruits.length; i++) {
    String fruit = fruits[i];
    // fruit is an element of the `fruits` array.
}

So, overall summary:
[nsayer] The following is the longer form of what is happening:

for(Iterator<String> i = someList.iterator(); i.hasNext(); ) {
  String item = i.next();
  System.out.println(item);
}

Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for( : ) idiom, since the actual Iterator is merely inferred.

[Denis Bueno]

It's implied by nsayer's answer, but it's worth noting that the OP's for(..) syntax will work when "someList" is anything that implements java.lang.Iterable -- it doesn't have to be a list, or some collection from java.util. Even your own types, therefore, can be used with this syntax.

🌐
Codecademy
codecademy.com › docs › java › arraylist › .foreach()
Java | ArrayList | .forEach() | Codecademy
April 13, 2025 - The .forEach() method performs a specified action on each element of the ArrayList one by one. This method is part of the Java Collections Framework and was introduced in Java 8 as part of the Iterable interface, which ArrayList implements.
🌐
CodeGym
codegym.cc › java blog › java arrays › string array in java
String Array in Java
May 11, 2023 - In Java you can iterate through a String array using a loop. It could be for or a foreach construction.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-string-array
Java String Array | DigitalOcean
August 3, 2022 - We can iterate over string array using java for loop or java foreach loop.
🌐
Javatpoint
javatpoint.com › for-each-loop
Java For-each Loop | Enhanced For Loop - javatpoint
Java For-each loop | Java Enhanced For Loop: The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.
🌐
Delft Stack
delftstack.com › home › howto › java › for each char in string java
How to Iterate Over Characters of String in Java | Delft Stack
February 2, 2024 - import java.util.stream.IntStream; ...stem.out::print); } } Output: Happy Coding · The String.toCharArray() method converts the given string into a sequence of characters....
🌐
W3Schools
w3schools.com › java › java_foreach_loop.asp
Java For-Each Loop
Strings Concatenation Numbers and Strings Special Characters Code Challenge Java Math Java Booleans · Booleans Real-Life Example Code Challenge Java If...Else · if else else if Short Hand If...Else Nested If Logical Operators Real-Life Examples Code Challenge Java Switch ... For Loop Nested Loops For-Each Loop Real-Life Examples Code Challenge Java Break/Continue Java Arrays