List实现类

  • ArrayList(重点):

    • 数组结构实现,查询快,增删慢
    • jdk1.2版本,运行效率快,线程不安全
    • 源码分析:
      • 默认容量大小:DEFAULT_CAPACITY = 10(如果没有向集合中添加任何元素容量为0,添加一个元素容量为10,每次扩容大小为原来的1.5倍)
      • 存放元素的数组:elementData
      • 实际的元素个数:size
      • 添加元素:add()
    //创建集合
    ArrayList arrayList = new ArrayList();
    
    //添加元素
    Student s1 = new Student("张三",20);
    Student s2 = new Student("李四",21);
    Student s3 = new Student("王五",22);
    ArrayList.add(s1);
    ArrayList.add(s2);
    ArrayList.add(s3);
    
    //遍历(重点)
    //迭代器
    Iterator it = arrayList.iterator();
    while(it.hasNext()){
        Student s = (Student)it.next();
        System.out.println(s.toString());
    }
    //列表迭代器
    ListIterator lit = arrayList.listIterator();
    //正序
    while(lit.hasNext()){
        Student s = (Student)lit.next();
        System.out.println(s.toString());
    }
    //逆序
    while(lit.hasPrevious()){
        Student s = (Student)lit.previous();
        System.out.println(s.toString());
    }
    
    //判断
    System.out.println(arrayList.contains(new Student("李四",21))); //true重写equals方法可判断
    System.out.println(arrayList.isEmpty()); //false
    
    //查找
    System.out.println(arrayList.indexOf(new Student("李四",21)); //1
    
    //删除元素
    arrayList.remove(s1);
    arrayList.remove(new Student("王五",22)); //重写equals方法后可删除
    
  • Vector:

    • 数组结构实现,查询快,增删慢
    • jdk1.0版本,运行效率慢,线程安全
    //创建集合
    Vector vector = new Vector();
    
    //添加元素
    vector.add("apple");
    vector.add("orange");
    vector.add("pear");
    
    //遍历
    //for 增强for 迭代器也可以
    //枚举器
    Enumeration en = vector.elements();
    while(en.hasMoreElements()){
        String o = (String)en.nextElement();
        System.out.println(o);
    }
    
    //firstElement()、lastElement()、elementAt();方法
    System.out.println(vector.firstElement()); //apple
    System.out.println(vector.lastElement()); //pear
    System.out.println(vector.elementAt(1)); //orange
    
    //判断
    System.out.println(vector.contains("apple")); //true
    System.out.println(vector.isEmpty()); //false
    
    //删除元素
    collection.remove("pear");
    System.out.println("删除之后:" + vector.size()); //删除之后:2
    collection.clear();
    System.out.println("删除之后:" + vector.size()); //删除之后:0
    
  • LinkedList:

    • 链表结构实现,增删快,查询慢(双向链表)
    • 源码分析:
      • 集合的大小:size
      • 链表的头节点:Node first
      • 链表的尾节点:Node last
    //创建集合
    LinkedList linkedList = new LinkedList();
    
    //添加元素
    Student s1 = new Student("张三",20);
    Student s2 = new Student("李四",21);
    Student s3 = new Student("王五",22);
    linkedList.add(s1);
    linkedList.add(s2);
    linkedList.add(s3);
    
    //遍历(重点)
    //for遍历
    for(int i = 0; i < linkedList.size(); i++){
        System.out.println(linkedList.get(i));
    }
    //增强for
    for(Object o : linkedList){
        Student s = (Student)o;
        System.out.println(s.toString());
    }
    //迭代器
    Iterator it = linkedList.iterator();
    while(it.hasNext()){
        Student s = (Student)it.next();
        System.out.println(s.toString());
    }
    //列表迭代器
    ListIterator lit = linkedList.listIterator();
    //正序
    while(lit.hasNext()){
        Student s = (Student)lit.next();
        System.out.println(s.toString());
    }
    //逆序
    while(lit.hasPrevious()){
        Student s = (Student)lit.previous();
        System.out.println(s.toString());
    }
    
    //判断
    System.out.println(linkedList.contains(new Student("李四",21))); //true重写equals方法可判断
    System.out.println(linkedList.isEmpty()); //false
    
    //查找
    System.out.println(linkedList.indexOf(new Student("李四",21)); //1
    System.out.println(linkedList.indexOf(s1); //0
                       
    //删除元素
    linkedList.remove(s1);
    linkedList.remove(new Student("王五",22)); //重写equals方法后可删除
    
  • ArrayList和LinkedList的区别

    • ArrayList:必须开辟连续空间,查询快,增删慢。LinkedList:无需开辟连续的空间,查询慢,增删快。
      ArrayList和LinkedList对比
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Remote_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值