在普通for循环遍历或取数组元素或做其他操作时,总会以将要超出数组最大索引作为循环结束的标志,也就是说每一次循环都要去判断此条件是否满足
而forEach强就强在它无需这一步流程,所以效率要高
但在今天我无意写出了一种这样的遍历方式,如下
int[] test = new int[500000000];
long begin = System.currentTimeMillis();
try {
for (int i = 0; ; i++) {
int n = test[i];
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(System.currentTimeMillis() - begin + "-------------");
}
谁会想着去捕获运行时异常呢?躲还来不及呢
经洒家测试,这种方式也确实比传统的for效率高那么一点
牛哔!