java values()_Java:for( String value: values)简单运用

1 public classProgram {2 public static voidmain(String[] args) {3

4 String[] values = new String[3];5 values[0] = "Dot";6 values[1] = "Net";7 values[2] = "Perls";8

9 for(String value : values) {10 System.out.println(value);11 }12 }13 }

For each. This is a simple syntax form. If we loop over a collection, we use a colon, not an index variable. This enumerates each element in the collection (array, ArrayList).Array

Info: This is called a foreach-loop statement. The Java language does not support a "foreach" keyword. Please use the "for" keyword.

对于每一个。 这是一个简单的语法形式。 如果我们在一个集合上循环,我们使用冒号,而不是索引变量。 这将枚举集合中的每个元素(数组、 ArrayList)。 Array Info: 这称为 foreach 循环语句。 Java 语言不支持“ foreach”关键字。 请使用“ for”关键字

public classProgram {static intcount;static int[] getElements() {//Set array elements based on a static field.

int[] array = new int[3];

array[0] = count++;

array[1] = count++;

array[2] = count++;returnarray;

}public static voidmain(String[] args) {//The method is called once and not many times in the for-loop.

for (intvalue : getElements()) {

System.out.println(value);

}

}

}

/*

output:

0

1

2

*/

Method, for-each. A method can be called in the for-loop. This method is evaluated once and then the results of it are accessed in the loop iteration variable.

Here: This program shows that the method getElements is only called once. It is not called three times.

So: When calling a variable or method in a for-each loop, we can see that the result is cached in a local and not evaluated more than once.

方法,每个。 可以在 for 循环中调用方法。 这个方法被评估一次,然后在循环迭代变量中访问它的结果。 这里: 这个程序显示方法 getelementes 只被调用一次。 它不叫三次。 因此: 当在 for-each 循环中调用变量或方法时,我们可以看到结果被缓存在局部中,并且不会进行多次计算

1 public classProgram {2 public static voidmain(String[] args) {3 4 String[] values = new String[3];5 values[0] = "Dot";6 values[1] = "Net";7 values[2] = "Perls";8 9 for(String value : values) {10 System.out.println(value);11 }12 }13 } For each. This is a simple syntax form. If we loop over a collection, we use a colon, not an index variable. This enumerates each element in the collection (array, ArrayList).Array Info: This is called a foreach-loop statement. The Java language does not support a "foreach" keyword. Please use the "for" keyword. 对于每一个。 这是一个简单的语法形式。 如果我们在一个集合上循环,我们使用冒号,而不是索引变量。 这将枚举集合中的每个元素(数组、 ArrayList)。 Array Info: 这称为 foreach 循环语句。 Java 语言不支持“ foreach”关键字。 请使用“ for”关键字 public classProgram {static intcount;static int[] getElements() {//Set array elements based on a static field. int[] array = new int[3]; array[0] = count++; array[1] = count++; array[2] = count++;returnarray; }public static voidmain(String[] args) {//The method is called once and not many times in the for-loop. for (intvalue : getElements()) { System.out.println(value); } } } /* output: 0 1 2 */ Method, for-each. A method can be called in the for-loop. This method is evaluated once and then the results of it are accessed in the loop iteration variable. Here: This program shows that the method getElements is only called once. It is not called three times. So: When calling a variable or method in a for-each loop, we can see that the result is cached in a local and not evaluated more than once. 方法,每个。 可以在 for 循环中调用方法。 这个方法被评估一次,然后在循环迭代变量中访问它的结果。 这里: 这个程序显示方法 getelementes 只被调用一次。 它不叫三次。 因此: 当在 for-each 循环中调用变量或方法时,我们可以看到结果被缓存在局部中,并且不会进行多次计算
经验分享 程序员 微信小程序 职场和发展