for-Each循环: 增强的for循环,写起来比较简洁,其实跟普通的for循环性能上没啥区别
所以到现在遍历的方式有三种:
for :当需要数组的下标信息时用它
for-Each :
Iterator
// 对于一位数组的遍历
int[] array={1,2,3,4,5};
// for(对象的类型type element :array)
for(int a:array){
System.out.println(a);
}
//对于二维数组的遍历
int[][] array2={{1,2,3},{4,5,6},{7,8,9}};
for(int[] row : array2){
for(int element:row){
System.out.println(element);
}
}