• Collection体系集合


    一、Collection体系结构

    Collection体系结构的根接口,代表一组对象,称为“集合”。

    List接口的特点:有序、有下标、元素可重复。

    Set 接口的特点:无序、无下标、元素不能重复。

    二、Collection父接口

    特点:代表一组任意类型的对象,无序、无下标、不能重复。

    方法:

    1. 添加一个对象

    boolean add(Object obj)

    2. 将一个集合中的所有对象添加到此集合中。

    boolean addAll(Collection c)

    3. 清空此集合中的所有元素

    void clear()

    4. 检查此集合中是否包含  o  对象

    boolean contains(Object o)

    5. 比较此集合是否与指定对象相等

    boolean equals(Object o)

    6. 此集合是否为空

    boolean isEmpty()

    7. 在此集合中移除 o 对象

    boolean remove (Object o)

    8. 返回此集合中的元素个数

    int size()

    9. 将此集合转换成数组

    Object[] toArray()

    三、Collection接口的使用

    详看代码注释:

    ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 

    1. 接口的使用(1):添加元素   删除元素   遍历元素   判断

    1. import java.util.ArrayList;
    2. import java.util.Collection;
    3. import java.util.Iterator;
    4. /**
    5. * Collection接口的使用:
    6. * (1)添加元素
    7. * (2)删除元素
    8. * (3)遍历元素
    9. * (4)判断
    10. */
    11. public class Demo1 {
    12. public static void main(String[] args) {
    13. //创建集合 不能实例化,但可以创建对象
    14. Collection collection = new ArrayList();
    15. //1.添加元素
    16. collection.add("西瓜");
    17. collection.add("苹果");
    18. collection.add("香蕉");
    19. collection.add("榴莲");
    20. collection.add("葡萄");
    21. System.out.println("元素个数:"+collection.size());//size()指元素长度
    22. System.out.println(collection);
    23. System.out.println("------------------");
    24. //2.删除
    25. collection.remove("苹果");
    26. System.out.println("删除之后:"+collection.size());
    27. System.out.println("------------------");
    28. //3.遍历元素
    29. //(1)使用增强for
    30. for (Object object:collection) {
    31. System.out.println(object);
    32. }
    33. System.out.println("------------------");
    34. //(2)使用迭代器 Interator
    35. //先用hasNext()判断有没有下一个元素
    36. //next()取出下一个元素,在继续往后移一位
    37. //remove()删除元素
    38. Iterator it = collection.iterator();
    39. while(it.hasNext()){
    40. String str = (String)it.next();
    41. System.out.println(str);
    42. //在使用叠加器遍历过程中不允许使用其他并发修改集合的方法
    43. //collection.remove(str); 不能collection删除方法
    44. it.remove(); // 可以,属于叠加器的清除
    45. }
    46. System.out.println("元素个数:"+collection.size());
    47. System.out.println("------------------");
    48. //4.判断
    49. System.out.println(collection.contains("西瓜"));//判断该元素是否存在
    50. System.out.println(collection.isEmpty()); //判断是否为空 空(true) 不为空(false)
    51. System.out.println("------------------");
    52. //5.清空
    53. collection.clear();
    54. System.out.println("清空之后:"+collection.size());
    55. System.out.println(collection);
    56. }
    57. }

    简单粗暴!

    感谢ლ(°◕‵ƹ′◕ლ)!!!

  • 相关阅读:
    nginx 配置~~~本身就是一个静态资源的服务器
    图像处理: ImageKit.NET 3.0.10704 Crack
    Springboot美发店会员管理系统y71a1计算机毕业设计-课程设计-期末作业-毕设程序代做
    C和指针 第14章 预处理器 14.1 预定义符号
    4.1 Windows驱动开发:内核中进程与句柄互转
    深度学习中的图像处理(基本介绍+示例代码)
    java 大厂面试指南:性能优化 + 微服务 + 并发编程 + 开源框架 + 分布式
    LeetCode 101. 对称二叉树
    驱动开发day8
    docker desktop无法启动问题
  • 原文地址:https://blog.csdn.net/yao_yaoya/article/details/128013693