• Collection集合的常见使用(Java)


    一、Collection本身是接口,不能被实例化,被实现后,可实例化使用

    (1)Collection的常用子接口的实现类:

    1、List体系:ArrayList、Vector、LinkedList

    2、Set体系:HashSet、TreeSet

    二:Collection常用方法:

    判断功能:

    boolean contain(Object o)判断集合中是否包含某元素

    boolean containsAll(Collection c)判断集合中是否包含某集合c的所有元素

    boolean isEmpty()判断集合是否为空

    添加元素:

    boolean add(object obj)添加一个元素

    boolean addAll(Collection c)将集合c的全部元素添加到原集合

    添加功能的返回值为布尔值,如果成功将返回true

    删除功能:

    void chear()移除所有元素

    boolean remove(Object o)移除一个元素

    boolean removeAll(Collection c)移除一个集合的元素,其效果为移除原集合中与c中相同的元素

    int size()返回元素的个数

    Iterator迭代器使用案例:

    1. import java.util.ArrayList;
    2. import java.util.Collection;
    3. import java.util.Iterator;
    4. public class MyIterator {
    5. public static void main(String[] args) {
    6. Collection c = new ArrayList();
    7. c.add(1);
    8. c.add(2);
    9. c.add(3);
    10. c.add(4);
    11. Iterator iterator = c.iterator();
    12. /*Iterator iterator()方法:获取集合对应的迭代器*/
    13. while (iterator.hasNext()){
    14. /*boolean hasNext()方法:判断当前位置是否有元素*/
    15. Integer integer = (Integer) iterator.next();
    16. /*Object next()方法:获取当前位置的元素,并移动到下一个位置*/
    17. System.out.println(integer);
    18. }
    19. }
    20. }
  • 相关阅读:
    课程2 第3周 TensorFlow入门
    项目管理系统(Java+Web+MySQL)
    zookeeper最基础教程
    MyBatis 配置与测试方式
    Go Goroutine 究竟可以开多少?(详细介绍)
    【C语言】指针那些事之数组传参和指针传参的区别
    02-SpringBoot基础
    远程服务器的Docker环境遇到问题,无法调试
    Linux串口断帧和连帧处理
    PhpSpreadsheet读写Excel文件
  • 原文地址:https://blog.csdn.net/zhan_qian/article/details/126315234