I'm a Java beginner and have problems understanding what this means:
for( String string : myWord){
Is there another way to write this? Even if it is probably more complicated, it might help me understand!
Thanks for any help.
I'm a Java beginner and have problems understanding what this means:
for( String string : myWord){
Is there another way to write this? Even if it is probably more complicated, it might help me understand!
Thanks for any help.
Well, assuming that myWord
is some sort of collection, such as List
, you could use a stream. Assuming your original code:
List<String> myWord = new ArrayList<>();
myWord.add("Hello");
myWord.add("World");
for (String string : myWord) {
System.out.println(string);
}
Using streams, the above becomes:
myWord.stream().foreach(System.out::println);
From the oracle foreach documentation:
When you see the colon (:) read it as "in."
More details and scenarios are available at the oracle link I provided.