一、字符串的遍历
String a = "abcdefg";
for (int i = 0; i < a.length(); i++) {
System.out.println(a.charAt(i));
}
二、数组的遍历
1、一维数组的遍历
int [] a = {1,2,3,4,5,6};
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
2、二维数组的遍历
int[] a = {1,2,3};
int[] b = {2,3,4};
int[] c = {3,4,5};
int[][] arrs = {a,b,c};
for (int i = 0; i < arrs.length; i++) {
for (int j = 0; j < arrs[i].length; j++) {
System.out.println(arrs[i][j]);
}
}
三、集合的遍历
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
// 方法一:for循环遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 方法二:加强for循环遍历
for(Integer i : list){
System.out.println(i);
}
// 方法三:迭代器遍历
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}