workplace-blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

集合

1.集合的概念

  • 对象的容器,实现了对对象的常用操作,类似数组功能
  • 位置:java.util.*

2.集合和数组的区别

  • 集合长度不固定,数组长度固定
  • 集合只能存储引用数据类型,数组可以存储基本数据类型和引用数据类型

Collection体系结构

1.collection父接口

  • 代表一组任意类型的对象,无序,无下标,可存放重复元素(List)也可以存放不重复元素(Set)
  • 方法:

  • A.removeAll(Collection c):在A集合中移除A,B的交集,即A-B
  • iterator( ):返回在此元素上进行迭代的迭代器(遍历集合)
    • hasNext():是否有下一个元素
    • next(): 获取下一个元素
    • remove():获取当前元素

2.Collection使用

/*
Collection接口的使用1
(1).增加元素
(2).删除元素
(3).遍历集合
(4).判断(是否为空,是否含有某元素)
 */
public class demo01 {
    public static void main(String[] args) {
        //1.增加
        Collection collection = new ArrayList();
        collection.add("melon");
        collection.add("peach");
        collection.add("apple");
        System.out.println("集合长度为" + collection.size());
        System.out.println(collection);

        //2.删除
        collection.remove("melon");
        System.out.println("集合长度为" + collection.size());

        //3.遍历(important!!)
        //3.1使用增强for
        System.out.println("--------增强for--------");
        for (Object object:collection) {
            System.out.println(object);
        }
        //3.2使用iterator迭代器(返回一个Object类型数据)
        /*
        迭代器中的三个方法:
        hasNext():是否有下一个元素
        next(): 获取下一个元素
        remove():获取当前元素
         */
        System.out.println("---------迭代器----------");
        Iterator it = collection.iterator();
        while (it.hasNext()){
            Object ob = it.next();
            System.out.println(ob);
            /*不可以在迭代器内使用collection的方法,出现并发修改异常
            collection.remove(ob);
             */
            it.remove();
        }
        System.out.println(collection);

        //4.判断
        System.out.println(collection.contains("apple"));
        System.out.println(collection.isEmpty());
    }
}



//输出结果:
集合长度为3
[melon, peach, apple]
集合长度为2
--------增强for--------
peach
apple
---------迭代器----------
peach
apple
[]
false
true
/*
Collection接口的使用2(对象)
 */
public class demo02 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        student s1 = new student("赵美延",26);
        student s2 = new student("ssw",29);
        student s3 = new student("pjh",32);

        //1.添加对象
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println(collection);

        //2.删除
        collection.remove(s1);
        System.out.println(collection);

        //3.遍历
        //增强for循环
        for (Object s:collection
             ) {
            student stu = (student)s;
            System.out.println(stu.toString());
        }
        //iterator:返回一个Object类型数据
        Iterator it = collection.iterator();
        while (it.hasNext()){
            student student =(student)it.next();
            System.out.println(student);
        }

        //4.判断
        System.out.println(collection.isEmpty());
        System.out.println(collection.contains(s2));
    }
}

//输出结果:
[student{name='赵美延', age=26}, student{name='ssw', age=29}, student{name='pjh', age=32}]
[student{name='ssw', age=29}, student{name='pjh', age=32}]
student{name='ssw', age=29}
student{name='pjh', age=32}
student{name='ssw', age=29}
student{name='pjh', age=32}
false
true
posted on 2022-08-26 19:20  多巴胺LLL  阅读(34)  评论(0)    收藏  举报