Collection接口为最顶层集合接口

Collection接口常用的子接口有:List接口、Set接口

List接口常用的子类有:ArrayList类、LinkedList类

Set接口常用的子类有:HashSet类、LinkedHashSet类

Collection创建集合的格式:

1.Collection<元素类型> 变量名 = new ArrayList<元素类型>();

2.Collection 变量名 = new ArrayList();

Collection的常用方法:add(E e)、clear()、contians(Object o)、remove(Object o)、size()、toArray()

 

 

Iterator迭代器

hasNext()方法:用来判断集合中是否有下一个元素可以迭代。如果返回true,说明可以迭代。

next()方法:用来返回迭代的下一个元素,并把指针向后移动一位。

//遍历
        Iterator<String> it=set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

Iterator<String> it = coll.iterator();

while (it.hasNext()) {

    String str = it.next();

//当使用Iterator<String>控制元素类型后,就不需要强转了。获取到的元素直接就是String类型

posted on 2020-10-16 16:11  心灯不夜  阅读(98)  评论(0)    收藏  举报