1、什么是迭代器
- 一种设计模式,主要用于遍历Collection中的元素、
- GOF定义:提供一种方法访问一个容器对象中各个元素,二又不需要暴露对象的内部细节。
- 类似于火车上得乘务员、公交车上得售票员、空姐
- Collection 继承java.lang.Iterable 接口、此接口有一个iterator()方法
- 所有实现了Collection接口的集合的类都有一个iterator()方法,用于返回一个实现了Iterable接口的对象
- Iterator 仅用于遍历集合,本身不提供装在对象的能力。要创建一个Iterator对象,必须有一个被迭代的集合
2、迭代器的使用
遍历时两个方法配合使用哦
0000000000000000000000000000000000000000000000000000000000
理解两个方法的作用
next():先指针下移,后输出数据
hasNext():判断下一个方法的指针是不是指空。
看代码
package my.test;
import learn.clection.Person;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestIterator {
@Test
public void test (){
//造一个集合(存好元素)
Collection collectiont = new ArrayList();
collectiont.add(123);
collectiont.add(new Person());
collectiont.add(new Person("Tom","程序员",23));
collectiont.add(false);
//创造一个迭代器对象
Iterator iterator = collectiont.iterator();
System.out.println("输出的是地址(堆内存):"+iterator);
//输出容器(集合)中的一个对象
System.out.println(iterator.next());
//再输出容器(集合)中的一个对象
System.out.println(iterator.next());
System.out.println("***************");
//1、遍历的第一种方式 =============一般不用
System.out.println("遍历方式一结果为:");
// for (int i = 0; i < collectiont.size(); i++) {
// System.out.println(iterator.next());
// }
//强烈推荐(常用)
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
输出的是地址(堆内存):java.util.ArrayList$Itr@4dcbadb4
123
Person{name='null', profession='null', age=0}
***************
遍历方式一结果为:
Person{name='Tom', profession='程序员', age=23}
false
3、迭代器中remove()
知识点在代码中
package my.test;
import learn.clection.Person;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class TestCollection {
@Test
public void test(){
Collection collectiont = new ArrayList();
collectiont.add(123);
collectiont.add(new Person());
collectiont.add(new Person("Tom","程序员",23));
System.out.println(collectiont);
collectiont.add(false);
System.out.println("*************");
System.out.println(collectiont.contains(123));
System.out.println("*************");
System.out.println(new String("Tom"));
System.out.println("*************");
//没有重写equals()方法,为 false
//重写equals() 后,只比较姓名 所以为true
Person person1 = new Person("Tom","产品经理",25);
System.out.println(collectiont.contains(person1)); //false
//Collection coll = new Arrays.asList(123,456);
//collectiont.containsAll(coll);
//System.out.println(collectiont.containsAll(coll));
//迭代器模式
//作用:遍历
Iterator iterator = collectiont.iterator();
System.out.println("迭代器遍历结果:"+iterator);
System.out.println(iterator.next());
}
}
运行结果
输出的是地址(堆内存):java.util.ArrayList$Itr@4dcbadb4
***************
遍历方式一结果为:
123
Person{name='null', profession='null', age=0}
Tom
Person{name='Tom', profession='程序员', age=23}
false
移除Tom后遍历结果:
123
Person{name='null', profession='null', age=0}
Person{name='Tom', profession='程序员', age=23}
false
4、补充:增强for循环
@Test
public void test2(){
//造一个集合(存好元素)
Collection collectiont = new ArrayList();
collectiont.add(123);
collectiont.add(new Person());
collectiont.add("Tom");
collectiont.add(new Person("Tom","程序员",23));
collectiont.add(false);
//增强for循环
//(集合中元素类型 局部变量 :集合对象)
for (Object obj:collectiont) {
System.out.println(obj);
}
}