循环
迭代器
Iterator<Student> iterator=list.listIterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
使用迭代器遍历细节
迭代器遍历完毕,指针不会复位
循环只能出现一次 next()
迭代器循环集合不能使用集合原生删除方法,必须使用迭代器提供的 remove() 方法删除
增强for
for (Student student : list) {
student=new Student("11","22");
System.out.println(student);
}
idea快速生成 集合名称+for
student 表示当前遍历的对象
修改当前循环对象不会对 原list集合数据造成影响
lambda表达式循环(java8 新增特性)
list.forEach(Student::toS);
list.forEach(Student, ->
System.out.println("只有一行代码")
);
list.forEach(Student->{
System.out.println("多行代码");
System.out.println(Student.getName());
});
tos 表示Student 公开的成员方法
第二种只有一行代码可以简写
for 循环
String arr[] = {"a","b","c"};
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
for (循环变量类型 循环变量名称;循环条件;更新语句) 循环体
while循环
int j=0;
while(j<=5) {
j++;
System.out.println("HelloWorld");
}
while (条件判断语句) {条件控制语句;循环体语句};
for循环先判断条件是否成立,然后决定是否执行循环体(先判断后执行)
do-while循环
int j=0;
do {
System.out.println("HelloWorld");
j++;
}while(j<=5);
do…while循环先执行一次循环体,然后判断条件是否成立,是否继续执行循环体(先执行后判断)