Java中的foreach

int[] arr = {1,2,3,4,5,6,8,5,9};

//常规for循环
for (int i = 0; i < arr.length; i++) {
  System.out.print(arr[i]+" ");
}

System.out.println();
//JDK5 之后的高级for循环,它只能使用在仅仅为了遍历数组
//或集合,如果需要做更复杂的操作,还是需要常规for循环
for (int i : arr) {
  System.out.print(i+" ");

posted @ 2015-02-06 08:42  一种武器  Views(142)  Comments(0)    收藏  举报