关于List实现类的学习

List实现类

  • ArrayList

    • 数组结构实现,查询快、增删慢;

    • JDK1.2版本,运行效率快、线程不安全。

    • 源码分析:

      • DEFAULT_CAPACITY = 10 默认容量

        注意:如果没有向集合中添加任何元素时,容量为0;添加任意一个元素之后,容量为10.

      • elementData 存放元素的数组

      • size 实际元素个数

      • add() 添加元素

        部分源码:

         public boolean add(E e) {
                 ensureCapacityInternal(size + 1);  // Increments modCount!!
                 elementData[size++] = e;
                 return true;
             }
         private void ensureCapacityInternal(int minCapacity) {
                 ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
             }
         ​
             private void ensureExplicitCapacity(int minCapacity) {
                 modCount++;
         ​
                 // overflow-conscious code
                 if (minCapacity - elementData.length > 0)
                     grow(minCapacity);
             }
         private void grow(int minCapacity) {
                 // overflow-conscious code
                 int oldCapacity = elementData.length;
                 int newCapacity = oldCapacity + (oldCapacity >> 1);
                 if (newCapacity - minCapacity < 0)
                     newCapacity = minCapacity;
                 if (newCapacity - MAX_ARRAY_SIZE > 0)
                     newCapacity = hugeCapacity(minCapacity);
                 // minCapacity is usually close to size, so this is a win:
                 elementData = Arrays.copyOf(elementData, newCapacity);
             }
  • Vector

    • 数组结构实现,查询快、增删慢;

    • JDK1.0版本,运行效率慢、线程安全。

    • 代码示例:

      /**
        * 演示Vector集合的使用
        * 存储结构:数组
        * @author zyhls
        * */
       public class Demo01 {
           public static void main(String[] args) {
               //创建集合
               Vector vector = new Vector<>();
               //1.添加元素
               vector.add("0");
               vector.add("1");
               vector.add("2");
               vector.add("3");
               vector.add("4");
               System.out.println("元素个数:"+vector.size());
               System.out.println(vector.toString());
               //2.删除元素
               vector.remove(0);
               //3.遍历元素
               //使用枚举器
               Enumeration en = vector.elements();
               while (en.hasMoreElements()){
                   String o = (String) en.nextElement();
                   System.out.println(o);
               }
               //4.判断
               System.out.println(vector.contains("1"));
               System.out.println(vector.isEmpty());
           }
       }

  • LinkedList

    • 链表结构实现,增删快,查询慢。

    • 代码示例:

      /**
        * LinkedList的使用
        * 存储结构:双向链表
        * @author zyhls
        **/
       public class Demo02 {
           public static void main(String[] args) {
               //创建集合
               LinkedList linkedList = new LinkedList<>();
               //1.添加元素
               Student s1 = new Student("刘一",20);
               Student s2 = new Student("王二",20);
               Student s3 = new Student("张三",20);
               Student s4 = new Student("李四",20);
               linkedList.add(s1);
               linkedList.add(s2);
               linkedList.add(s3);
               linkedList.add(s4);
               System.out.println("元素个数:"+linkedList.size());
               System.out.println(linkedList.toString());
               //2.删除
               linkedList.remove(s1);
               System.out.println("删除之后:"+linkedList.size());
               //3.遍历
               //3.1for遍历
               System.out.println("-----for遍历-----");
               for (int i = 0; i < linkedList.size(); i++) {
                   System.out.println(linkedList.get(i));
               }
               //3.2增强for
               System.out.println("-----增强for-----");
               for (Object object : linkedList){
                   Student s = (Student) object;
                   System.out.println(s.toString());
               }
               //3.3迭代器
               System.out.println("-----迭代器-----");
               Iterator it = linkedList.iterator();
               while (it.hasNext()){
                   Student s = (Student) it.next();
                   System.out.println(s.toString());
               }
               //3.4列表迭代器
               System.out.println("-----列表迭代器-----");
               ListIterator lit = linkedList.listIterator();
               while (lit.hasNext()){
                   Student s = (Student) lit.next();
                   System.out.println(s.toString());
               }
               //4.判断
               System.out.println(linkedList.contains(s2));
               System.out.println(linkedList.isEmpty());
               //5.获取
               System.out.println(linkedList.indexOf(s3));
           }
       }

      源码分析:

      • int size 集合的大小

      • Node first 链表的头节点

      • Node last 链表的尾节点

        void linkLast(E e) {
                 final Node<E> l = last;
                 final Node<E> newNode = new Node<>(l, e, null);
                 last = newNode;
                 if (l == null)
                     first = newNode;
                 else
                     l.next = newNode;
                 size++;
                 modCount++;
             }
         private static class Node<E> {
                 E item;
                 Node<E> next;
                 Node<E> prev;
         ​
                 Node(Node<E> prev, E element, Node<E> next) {
                     this.item = element;
                     this.next = next;
                     this.prev = prev;
                 }
             }
  • ArrayList与LinkedList的区别

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值