考虑:
List<String> someList = new ArrayList<String>();
// add "monkey", "donkey", "skeleton key" to someList
Run Code Online (Sandbox Code Playgroud)
for (String item : someList) {
System.out.println(item);
}
Run Code Online (Sandbox Code Playgroud)
如果for不使用for each语法,等效循环会是什么样的?
正如标题所说,有些人向我倾斜,如果我想在数组数组中打印所有内容的总和,我应该使用上述参数进行for循环(如果需要进一步说明,代码将会跟随) .但具体的确切定义是什么呢?: - 我的意思是.是吗; 对于阵列中的每个数字我都很高?
import java.util.*;
class Uke36{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
int[] tall=new int[5];
for (int i=0; i<=4; i++){
System.out.println("Vennligst oppgi det " + (i+1) + ". tallet: ");
tall[i]=input.nextInt();
}
int sum = 0;
for(int i : tall){
sum+=;
}
}
}
Run Code Online (Sandbox Code Playgroud) 这篇文章解释了foreach循环直接对应于使用迭代器.如果我写一个foreach循环,它真的会转换成for for iterator?特别是给定的循环:
for(Integer i : createList()){
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
createList()无论如何,我保证总是只打一次电话吗?它被重写为:
for(Iterator<Integer> i = createList().iterator(); i.hasNext(); ) {
System.out.println(i.next());
}
Run Code Online (Sandbox Code Playgroud)
在某种中间步骤或恰好产生与上述相同的字节码?