Iteractor 迭代器使用:
1.调用 iterator( ) 方法,创建迭代器对象;
Iterator iterator=collection.iterator();
2.调用 迭代器 . hasNext ( )
//判断迭代器指向的元素是否还有下一个元素(创建迭代器时,默认迭代器的下一个指向的元素才是集合中的第一个元素);
while (iterator.hasNext()){ Object obj=iterator.next(); System.out.println(obj); }
Collection 常用方法:
1.boolean add(Object o) //向集合中添加元素
collection.add(1);
2.void clear( ) //清空集合
collection.clear();
3.boolean contains(Object o) //判断集合中是否有该元素
System.out.println(collection.contains(1));
4.boolean isEmpty( ) //判断集合中元素个数是否为 0
System.out.println(collection.isEmpty());
5.boolean remove(Object o) //删除集合中某个元素
collection.remove(4);
6.int size( ) //返回集合中元素个数
System.out.println(collection.size());
7.Object [ ] toArray ( ) //将集合转换为数组 (作为了解,使用不多)
Object [] object=collection.toArray();
方法易错点:
1.Collection . remove ( ) 和 Collection . contains ( ) :
//它们匹配元素的准则是根据对象的 equals 方法来定的,所以在定义类时,一定一定要重写 equals 方法(之前的笔记也强调过);
( String 类中的 equals 方法已经被重写了)
eg:没有重写 equals 方法的情况:
package com.bjpowernode.javase.day2; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class practice { public static void main(String[] args) { Collection collection=new ArrayList(); People p1=new People(10,"张三"); People p2=new People(10,"张三"); collection.add(p1); System.out.println(collection.contains(p2)); collection.remove(p2); Iterator iterator=collection.iterator(); while (iterator.hasNext()){ Object obj=iterator.next(); System.out.println(obj); } } } class People{ private int old; private String name; public People() { } public People(int old, String name) { this.old = old; this.name = name; } @Override public String toString() { return "People{" + "old=" + old + ", name='" + name + '\'' + '}'; } } 运行结果: -------------------------------- false People{old=10, name='张三'} Process finished with exit code 0
重写 equals 方法的情况:
package com.bjpowernode.javase.day2; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Objects; public class practice { public static void main(String[] args) { Collection collection=new ArrayList(); People p1=new People(10,"张三"); People p2=new People(10,"张三"); collection.add(p1); System.out.println(collection.contains(p2)); collection.remove(p2); Iterator iterator=collection.iterator(); while (iterator.hasNext()){ Object obj=iterator.next(); System.out.println(obj); } } } class People{ private int old; private String name; public People() { } public People(int old, String name) { this.old = old; this.name = name; } @Override public String toString() { return "People{" + "old=" + old + ", name='" + name + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; People people = (People) o; return old == people.old && Objects.equals(name, people.name); } } 运行结果; ------------------------------- true Process finished with exit code 0
2.Collection .remove ( ):
1)在迭代集合元素的过程中,不能通过调用 Collection 的方法来改变集合结构,否则会出现:
java . util . ConcurrentModificationException;
2)注意:不能调用集合对象的 remove 方法来删除元素,一定要使用迭代器 Iterator 的 remove 方法,删除元素;
Collection 的 remove 方法是删除集合中的元素,迭代器 . remove 方法是删除集合中的元素和迭代器中的 “ 同一元素 ” ;
3)栗子老师:
package com.bjpowernode.javase.day2; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class pra { public static void main(String[] args) { Collection collection=new ArrayList(); collection.add(1); collection.add(2); Iterator iterator=collection.iterator(); while (iterator.hasNext()){ //在元素被删除之前提前获取元素 Object obj=iterator.next(); //删除迭代器当前指向的元素 iterator.remove(); //输出obj变量储存的元素 System.out.println(obj); } //集合中的元素被删没了 System.out.println(collection.size()); } } 运行结果: ------------------------------ 1 2 0 Process finished with exit code 0
由于博主目前只是一只猿宝宝,所以有些地方可能说的有些片面,若前辈们能够指点一二就更好了 (~ ̄(OO) ̄)ブ