Collection
并不是所有的集合都有索引,以前使用for结合索引遍历集合的方式,就不通用了 jdk提供了一种通用的遍历集合的方式: 迭代器 java.util.Iterator<E> 接口 抽象方法: public abstract boolean hasNext(): 判断集合中是否有下一个元素,有: true,没有: false public abstract E next(): 获取集合中的下一个元素。 java.util.Collection<E> 接口 ,定义了获取迭代器Iterator实现类对象的方法 public abstract Iterator<E> iterator() 返回在此 collection 的元素上进行迭代的迭代器。 ArrayList集合,是Collection的实现类,必然覆盖重写iterator()方法,用来获取迭代器对象 迭代器遍历集合的使用步骤: 1.集合对象调用iterator()方法获取,迭代器对象(Iterator<E> 接口的实现类对象) 2.迭代器对象调用hasNext()方法判读是否具有下一个元素 3.如果hasNext()方法返回true,说明有下一个元素,迭代器对象调用next()获取下一个元素
java.util.Collection接口: 单列集合的根接口,里面定义的方法, 子接口/实现类中都有 java.util.List<E>子接口 1.有序(存入和取出的顺序是一致的) 2.有索引 3.允许重复 java.util.Set<E>子接口 1.无序(不保证存入和取出的顺序一致) 2.没有索引 3.不允许重复
-
在进行集合元素获取时,如果集合中已经没有元素了,还继续使用迭代器的next方法,将会抛出java.util.NoSuchElementException没有集合元素异常。
-
在进行集合元素获取时,如果添加或移除集合中的元素 , 将无法继续迭代 , 将会抛出ConcurrentModificationException并发修改异常
常用方法
java.util.Collection接口常用方法: 1.public boolean add(E e): 把给定的对象添加到当前集合中 。 2.public boolean contains(Object obj): 判断当前集合中是否包含给定的对象。 3.public boolean isEmpty(): 判断当前集合是否为空。 4.public int size(): 返回集合中元素的个数。 5.public boolean remove(E e): 把给定的对象在当前集合中删除。 6.public Object[] toArray(): 把集合中的元素,存储到数组中 7.public void clear() :清空集合中所有的元素。
代码示例:
public static void main(String[] args) { //使用多态的方式创建对象 Collection<String> coll = new ArrayList<>(); //添加元素: add coll.add("hello"); coll.add("world"); coll.add("java"); System.out.println(coll);//[hello, world, java] //判断是否包含某个元素: contains //判断是否包含"java" boolean result = coll.contains("java"); System.out.println("是否包含java? "+result);//true //判断是否包含"c++" System.out.println("是否包含java? "+coll.contains("c++"));//false //3.public boolean isEmpty(): 判断当前集合是否为空。 System.out.println("集合是否为空? "+coll.isEmpty());//false //4.public int size(): 返回集合中元素的个数。 System.out.println("集合中元素的数量: "+coll.size());//3 //5.public boolean remove(E e): 把给定的对象在当前集合中删除。 System.out.println("删除world是否成功? "+coll.remove("world"));//true System.out.println("删除world后集合内容: "+coll);//[hello, java] System.out.println("删除Android是否成功? "+coll.remove("Android"));//false System.out.println("删除Android后集合内容: "+coll);//[hello, java] System.out.println("----------------"); //遍历Collection集合: 以前使用for+索引遍历集合的方式,就不能用了 for (int i = 0; i < coll.size(); i++) { //获取当前 元素 //String str = coll.get(i); } //6.public Object[] toArray(): 把集合中的元素,存储到数组中 Object[] objs = coll.toArray(); for (int i = 0; i < objs.length; i++) { //获取当前 元素 String str = (String)objs[i]; //System.out.println(str+"长度: "+str.length()); System.out.println(str); } System.out.println("----------------"); //7.public void clear() :清空集合中所有的元素。 coll.clear(); System.out.println("集合是否为空? "+coll.isEmpty());//true System.out.println("集合中元素的数量: "+coll.size());//0 System.out.println(coll);//[] }
Collection集合存储自定义Student对象并遍历 1.创建标准的Student类 2.创建Collection集合对象 3.创建Student类的对象 4.集合对象调用add方法添加元素 5.集合对象调用iterator方法,获取迭代器对象 6.迭代器对象调用hasNext判断是否有下一个元素 7.hasNext方法返回true,说明有下一个元素,迭代器对象调用next,获取下一个元素
示例
public static void main(String[] args) { //1.创建标准的Student类 //2.创建Collection集合对象 Collection<Student> coll = new ArrayList<>(); //3.创建Student类的对象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("张曼玉", 35); Student s3 = new Student("王祖贤", 33); //4.集合对象调用add方法添加元素: 地址值 coll.add(s1); coll.add(s2); coll.add(s3); //5.集合对象调用iterator方法,获取迭代器对象 Iterator<Student> it = coll.iterator(); //6.迭代器对象调用hasNext判断是否有下一个元素 while (it.hasNext()) { //7.hasNext方法返回true,说明有下一个元素,迭代器对象调用next,获取下一个元素 Student stu = it.next(); //System.out.println(stu);//地址值 System.out.println(stu.getName()+"::"+stu.getAge()); } }
Iterator迭代器
迭代的概念
迭代:Collection集合元素的通用获取方式.在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续判断,如果还有就再取出来.直到将集合中所有的元素全部取出来称为迭代.
获取迭代
public Iterator iterator() //获取集合对应的迭代器,用来遍历集合中的元素
常用方法
public E next() //返回迭代的下一个元素 public boolean hasNext() //如果1仍有元素可以迭代,则返回true