迭代器模式
调用iterator方法,获取迭代器Iterator

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestCollection {
public static void main(String[] args) {
Collection pers=new ArrayList();
pers.add(new Person("jarvis",18));
pers.add(new Person("robin",18));
pers.add(new Person("wzb",18));
Iterator iterator=pers.iterator();
// 判断,如果返回true,则进行下一步
// Way1
while (true) {
if (iterator.hasNext()) {
System.out.println("Way1" + iterator.next());
} else {
break;
}
}
//Way 2
while (iterator.hasNext()) {
System.out.println("Way2" + iterator.next());
}
//为了简化Iterator的语法,jdk5.0出现增强for,本质上就是Iterator,只是语法简化了
//Way3
for (Object o : pers) {
System.out.println("Way3" + o);
}
}
}
foreach 方法来遍历
从 JDK 1.5 之后可以使用 foreach 方法来遍历实现了 Iterable 接口的聚合对象
for(元素类型 元素名:集合或数组名){
}

问题:Way2没有输出,为什么呢?
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestCollection {
public static void main(String[] args) {
Collection pers = new ArrayList();
pers.add(new Person("jarvis", 18));
pers.add(new Person("robin", 18));
pers.add(new Person("wzb", 18));
Iterator iterator = pers.iterator();
while (iterator.hasNext()) {
pers.add(new Person("xiaoyu",17));
System.out.println(iterator.next());
}
}
}

报错:java.util.ConcurrentModificationException 非同步的更新异常
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestCollection {
public static void main(String[] args) {
Collection pers = new ArrayList();
pers.add(new Person("jarvis", 20));
pers.add(new Person("robin", 18));
pers.add(new Person("wzb", 21));
Iterator iterator = pers.iterator();
while (iterator.hasNext()) {
Person next=(Person)iterator.next();
if(next.age<19){
pers.remove(next);
}
System.out.println(iterator.next());
}
}
}

还是错,这次是NoSuchElementException
每次都会去比较记录的实际的个数
为了避免出现信息不安全,影响自己本身的功能,设立了一个变量,记录实际有几个
使用迭代器过程,不适合做增删,容易报异常ConCurrentModificationException
使用迭代器过程。可以做修改,但如果修改地址,没有效果
使用迭代器过程,如非要删除,可以使用迭代器本身的remove方法
适配器模式
java.util.Arrays#asList() 可以把数组类型转换为 List 类型。
应该注意的是 asList() 的参数为泛型的变长参数,不能使用基本类型数组作为参数,只能使用相应的包装类型数组。
import java.util.Arrays;
import java.util.List;
public class dasdsa {
public static void main(String[] args) {
Student[] students=new Student[3];
students[0]=new Student();
students[1]=new Student();
students[2]=new Student();
students[0].setAge(20);
System.out.println("students[0] 的年纪 = " + students[0].age);
System.out.println("students数组的长度 = " + students.length);
/*
复制数组
*/
//先新建一个数组
Student[] newstudents=new Student[students.length+1];
//再复制过去
for (int i = 0; i <students.length ; i++) {
newstudents[i]=students[i];
}
//也可使用arraycopy进行复制
//System.arraycopy(students,0,newstudents,0,3);
System.out.println("newstudents[0] 的年纪 = " + newstudents[0].age);
System.out.println("newstudents数组的长度 = " + newstudents.length);
List list= Arrays.asList(newstudents);
System.out.println("list = " + list);
System.out.println("list size = " + list.size());
for (int i = 0; i <list.size() ; i++) {
System.out.println(list.get(i).toString());
}
}
}
class Student {
String name;
int age;
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

students数组转为List成功,但是newstudents数组转化失败
也可以使用以下方式调用 asList():
import java.util.Arrays;
import java.util.List;
public class dasdsa {
public static void main(String[] args) {
List list = Arrays.asList(1, 2, 3);
System.out.println("list = " + list);
}
}

博客主要介绍了迭代器模式和适配器模式。迭代器模式可通过调用 iterator 方法获取迭代器,JDK 1.5 后可用 foreach 方法遍历实现 Iterable 接口的聚合对象,使用迭代器时增删易报错,修改地址可能无效,删除可用迭代器的 remove 方法。适配器模式中,Arrays#asList() 可将数组转 List,但有参数类型要求。
1310

被折叠的 条评论
为什么被折叠?



