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 循环中调用变量或方法时,我们可以看到结果被缓存在局部中,并且不会进行多次计算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值