Answer the question
In order to leave comments, you need to log in
Is the "for each" loop the same as the "for" loop in Java?
We have an array of strings:
String[] fruits = new String[] { "Orange", "Apple", "Pear", "Strawberry" };
for (String fruit : fruits) {
// выводим fruit
}
for (int i = 0; i < fruits.length; i++) {
String fruit = fruits[i];
// выводим fruit
}
for each
is guaranteed to go in order? Answer the question
In order to leave comments, you need to log in
First, you don't remember well. In Python, lists are an ordered collection and, accordingly, they are iterated sequentially in order.
Secondly, the foreach loop in Java is syntactic sugar, which will be expanded into
for (Iterator i = fruits.iterator(); i.hasNext();) {
String fruit = i.next();
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question