您当前的位置: 首页 >  Java

哆啦A梦_i

暂无认证

  • 1浏览

    0关注

    629博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java:Collection 接口(集合添加方法,集合 size()、remove() 和 removeAll() 方法的应用)

哆啦A梦_i 发布时间:2019-05-18 16:18:55 ,浏览量:1

Collection 接口是 List 接口和 Set 接口的父接口,通常情况下不被直接使用。Collection 接口定义了一些通用的方法,通过这些方法可以实现对集合的基本操作。因为 List 接口和 Set 接口继承自 Collection 接口,所以也可以调用这些方法。 本节将介绍 Collection 接口中常用的方法,如表 1 所示。

表1 Collection接口的常用方法 方法名称说明boolean add(E e)向集合中添加一个元素,E 是元素的数据类型boolean addAll(Collection c)向集合中添加集合 c 中的所有元素void clear()删除集合中的所有元素boolean contains(Object o)判断集合中是否存在指定元素boolean containsAll(Collection c)判断集合中是否包含集合 c 中的所有元素boolean isEmpty()判断集合是否为空Iteratoriterator()返回一个 Iterator 对象,用于遍历集合中的元素boolean remove(Object o)从集合中删除一个指定元素boolean removeAll(Collection c)从集合中删除所有在集合 c 中出现的元素boolean retainAll(Collection c)仅仅保留集合中所有在集合 c 中出现的元素int size()返回集合中元素的个数Object[] toArray()返回包含此集合中所有元素的数组

例 1

经过前面的介绍,我们知道 Collection 是非常重要的一个接口,在表 1 中列出了其常用方法。本案例将编写一个简单的程序,演示如何使用 Collection 接口向集合中添加方法。具体实现代码如下:

  1. public static void main(Strmg[] args)
  2. {
  3. ArrayList list1=new ArrayList(); //创建集合 iist1
  4. ArrayList list2=new ArrayList(); //创建集合 Iist2
  5. list1.add("one"); //向 list1 添加一个元素
  6. list1.add("two"); //向 list1 添加一个元素
  7. list2.addAll(list1); //将 list1 的所有元素添加到 list2
  8. list2.add("three"); //向 list2 添加一个元素
  9. System.out.println("list2 集合中的元素如下:");
  10. Iterator it1=list2.iterator();
  11. while(it1.hasNext())
  12. {
  13. System.out.print(it1.next()+"、");
  14. }
  15. }

由于 Collection 是接口,不能对其实例化,所以上述代码中使用了 Collection 接口的 ArrayList 实现类来调用 Collection 的方法。add() 方法可以向 Collection 中添加一个元素,而调用 addAll() 方法可以将指定 Collection 中的所有元素添加到另一个 Collection 中。 代码创建了两个集合 list1 和 list2,然后调用 add() 方法向 list1 中添加了两个元素,再调用 addAll() 方法将这两个元素添加到 list2 中。接下来又向 list2 中添加了一个元素,最后输出 list2 集合中的所有元素。

运行结果如下:

list2 集合中的元素如下: one、two、three、

例 2

创建一个案例,演示 Collection 集合中 size()、remove() 和 removeAll() 方法的应用。具体代码如下:

  1. public static void main(String[] args)
  2. {
  3. ArrayList list1=new ArrayList(); //创建集合 list1
  4. ArrayList list2=new ArrayList(); //创建集合 list2
  5. list1.add("one");
  6. list1.add("two");
  7. list1.add("three");
  8. System.out.println("list1 集合中的元素数量:"+list1.size()); //输出list1中的元素数量
  9. list2.add("two");
  10. list2.add("four");
  11. list2.add("six");
  12. System.out.println("list2 集合中的元素数量:"+list2.size()); //输出list2中的元素数量
  13. list2.remove(2); //删除第 3 个元素
  14. System.out.println("\nremoveAll() 方法之后 list2 集合中的元素数量:"+list2.size());
  15. System.out.println("list2 集合中的元素如下:");
  16. Iterator it1=list2.iterator();
  17. while(it1.hasNext())
  18. {
  19. System.out.print(it1.next()+"、");
  20. }
  21. list1.removeAll(list2);
  22. System.out.println("\nremoveAll() 方法之后 list1 集合中的元素数量:"+list1.size());
  23. System.out.println("list1 集合中的元素如下:");
  24. Iterator it2=list1.iterator();
  25. while(it2.hasNext())
  26. {
  27. System.out.print(it2.next()+"、");
  28. }
  29. }

list2 集合在调用 remove(2) 方法删除第 3 个元素之后剩下了 two 和 four。list1.removeAll(list2) 语句会从 list1 中将 list1 和 list2 中相同的元素删除,即删除 two 元素。

运行结果如下:

list1 集合中的元素数量:3

Iist2 集合中的元素数量:3

remove() 方法之后

list2 集合中的元素数量:2

list2 集合中的元素如下: two、four、 removeAH() 方法之后

list1 集合中的元素数量:2

list1 集合中的元素如下: one、 three、

注意:retainAll() 方法的作用与 removeAll() 方法相反,即保留两个集合中相同的元素,其他全部删除。

关注
打赏
1556978864
查看更多评论
立即登录/注册

微信扫码登录

0.0472s