Java中的List

来源:互联网 发布:openstack 数据库服务 编辑:程序博客网 时间:2024/06/02 05:30

原创与转载

首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList、Vector和LinkedList。List用于存放多个元素,能够维护元素的次序,并且允许元素的重复。3个具体实现类的相关区别如下:

  1. ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问。数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要讲已经有数组的数据复制到新的存储空间中。当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制、移动、代价比较高。因此,它适合随机查找和遍历,不适合插入和删除。
  2. Vector与ArrayList一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。
  3. LinkedList是用链表结构存储数据的,很适合数据的动态插入和删除,随机访问和遍历速度比较慢。另外,他还提供了List接口中没有定义的方法,专门用于操作表头和表尾元素,可以当作堆栈、队列和双向队列使用。

     查看Java源代码,发现当数组的大小不够的时候,需要重新建立数组,然后将元素拷贝到新的数组内,ArrayList和Vector的扩展数组的大小不同。

ArrayList中:

复制代码
1 public boolean add(E e) { 2 3 ensureCapacity(size + 1); // 增加元素,判断是否能够容纳。不能的话就要新建数组 4   5 elementData[size++] = e; 6 7 return true; 8 9 }10 11  public void ensureCapacity(int minCapacity) {12 13 modCount++; 14 15 int oldCapacity = elementData.length;16 17 if (minCapacity > oldCapacity) {18 19 Object oldData[] = elementData; // 此行没看出来用处,不知道开发者出于什么考虑20  21 int newCapacity = (oldCapacity * 3)/2 + 1; // 增加新的数组的大小22  23 if (newCapacity < minCapacity)24 25 newCapacity = minCapacity;26 27 // minCapacity is usually close to size, so this is a win:28  29 elementData = Arrays.copyOf(elementData, newCapacity);30 31 }32 33 }34 35  
复制代码

 

 

Vector中:

复制代码
1 private void ensureCapacityHelper(int minCapacity) { 2 3 int oldCapacity = elementData.length; 4 5 if (minCapacity > oldCapacity) { 6 7 Object[] oldData = elementData; 8 9 int newCapacity = (capacityIncrement > 0) ?10 11 (oldCapacity + capacityIncrement) : (oldCapacity * 2);12 13 if (newCapacity < minCapacity) {14 15 newCapacity = minCapacity;16 17 }18 19 elementData = Arrays.copyOf(elementData, newCapacity);20 21 }22 23 }24 25
复制代码

 

关于ArrayList和Vector区别如下:

  1. ArrayList在内存不够时默认是扩展50% + 1个,Vector是默认扩展1倍。
  2. Vector提供indexOf(obj, start)接口,ArrayList没有。
  3. Vector属于线程安全级别的,但是大多数情况下不使用Vector,因为线程安全需要更大的系统开销。

Stack:

java.util.Stack类实现了堆栈数据结构,即按照先进后出的原则存放数据。创建时只能为空.

常用方法:

        Stack<String> stack = new Stack<String>();        stack.pop();//Removes the object at the top of this stack and returns that object as the value of this function.        stack.peek();//Looks at the object at the top of this stack without removing it from the stack.        stack.push("item");//Looks at the object at the top of this stack without removing it from the stack.

直接用sysout输出:

Stack<String> stack = new Stack<String>();stack.push("a");stack.push("b");stack.push("c");stack.push("d");System.out.println(stack); //[a, b, c, d]
输出顺序明显不复合栈的要求,看看JAVA7源码

文件:AbstractCollection<E>, toString方法如下:    public String toString() {        Iterator<E> it = iterator();        if (! it.hasNext())            return "[]";        StringBuilder sb = new StringBuilder();        sb.append('[');        for (;;) {            E e = it.next();            sb.append(e == this ? "(this Collection)" : e);            if (! it.hasNext())                return sb.append(']').toString();            sb.append(',').append(' ');        }    }
说明直接用迭代器迭代出的数据是不复合数据结构要求的,我们还是需要用pop, peek等方法来输出

public static void main(String[] args) {Stack<String> stack = new Stack<String>();stack.push("a");stack.push("b");stack.push("c");stack.push("d");for(Iterator<String> iterator = stack.iterator(); iterator.hasNext();){System.out.print(stack.pop());  //dcba}}


0 0
原创粉丝点击