/** * @author 冀帅 * @date 2020/8/7-15:31 */ public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; if (age != person.age) return false; return name != null ? name.equals(person.name) : person.name == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } public Person(String name, int age) { this.name = name; this.age = age; } public Person() { } }
import org.junit.Test; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /** * 集合元素的变量操作,使用迭代器Iterator接口 * 1.内部的方法:hasNext()和next() * 2.集合对象每次调用iterator()都会得到一个全新的迭代器对象, * 默认游标都在第一个元素之前。 * 3.内部定义了一个remove()方法,可以在遍历的时候删除集合中的元素 * 此方法不同于集合直接调用remove() * @author 冀帅 * @date 2020/8/7-17:31 */ public class ItertorTest { @Test public void test1(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("jerry",20)); coll.add(new String("Tom")); coll.add(false); Iterator iterator = coll.iterator(); // 方式一: // System.out.println(iterator.next()); // System.out.println(iterator.next()); // System.out.println(iterator.next()); // System.out.println(iterator.next()); // System.out.println(iterator.next()); // //报异常:NoSuchElementException 五个元素 只能next()5次 // System.out.println(iterator.next()); //方式二:不推荐 // for (int i = 0; i < coll.size(); i++) { // System.out.println(iterator.next()); // } //方式三:推荐 while (iterator.hasNext()){ System.out.println(iterator.next());//next:1.指针下移。2.将下移以后集合位置上的元素返回 } } @Test public void test2(){ Collection cool = new ArrayList(); cool.add(123); cool.add(456); cool.add(new Person("jerry",20)); cool.add(new String("Tom")); cool.add(false); //错误方式一: // Iterator iterator = cool.iterator(); // while (iterator.next()!=null){ // System.out.println(iterator.next());//456 Tom // // // } // //错误方式二:死循环一直输出123(第一个数)集合对象每次调用iterator()都会得到 // 一个全新的迭代器对象,默认游标都在第一个元素之前。 // while (cool.iterator().hasNext()){ // System.out.println(cool.iterator().next()); // } } //测试Iterator中的remove() //如果还未调用next(),或在上一次调用next方法之后已经调用了remove方法, //再调用remove()[写两次remove()]就会报IllegalStateException @Test public void test3(){ Collection cool = new ArrayList(); cool.add(123); cool.add(465); cool.add(new Person("jerry",22)); cool.add(new String("Tom")); cool.add(false); //删除集合中"Tom"的数据 Iterator iterator = cool.iterator(); while (iterator.hasNext()){ Object obj = iterator.next();//调用remove()前必须调用next()方法,得让指针指下来。不然报异常 if ("Tom".equals(obj)){ iterator.remove(); } } //遍历集合 iterator = cool.iterator();//上一个迭代器指针已到最后,所以重新创建一个 while (iterator.hasNext()){ System.out.println(iterator.next()); } } }