JAVA中的ArrayList用法

List接口下一共实现了三个类:ArrayList,Vector,LinkedList。 
LinkedList主要保持数据的插入顺序的时候使用,采用链表结构。

ArrayList,Vector主要区别为以下几点: 
(1):Vector是线程安全的,源码中有很多的synchronized可以看出,而ArrayList不是。导致Vector效率无法和ArrayList相比; 
(2):ArrayList和Vector都采用线性连续存储空间,当存储空间不足的时候,ArrayList默认增加为原来的50%,Vector默认增加为原来的一倍; 
(3):Vector可以设置capacityIncrement,而ArrayList不可以,从字面理解就是capacity容量,Increment增加,容量增长的参数。

在Java中()表示参数,[]表示数组,那么特定泛型被固定为<>。
List<?> ?表示不确定类型,其实表示的是Object类型。
List<E> E是自己定义的,相当于参数临时名而已,例如public <T> void add(T t){}
List<? extends Person> 泛型向上限定,也就是说传入的?必须是Person的子类。
List<? super Person> 泛型向下限定,也就是传入的?必须是Person的父类。
高级篇:泛型只作用域javac编译,其实就是编译时作为一个检测,比如List<String>编译完毕后,我们使用反射得到getClass(),调用add方法,插入int,这是测试成功的。由此而建,泛型只是一个编译类型检测功能。

swap(List<?>, int, int) 方法被用于交换在指定列表中的指定位置的元素。

声明

以下是java.util.Collections.swap()方法的声明。

public static void swap(List<?> list,int i,int j)

参数

  • list-- 在该列表中的调剂元素。

  • i-- 要交换的一个元素的索引。

  • j-- 要交换的其它元素的索引。

返回值

  • NA

异常

  • IndexOutOfBoundsException-- 如果不是i或j超出这个范围(i < 0 || i >= list.size() || j < 0 || j >= list.size()),则会引发。

例子

下面的例子显示java.util.Collections.swap()方法的使用

package com.yiibai;

import java.util.*;

public class CollectionsDemo {
   public static void main(String[] args) {
      // create vector object 
      Vector<String< vector = new Vector<String<();
      
      // populate the vector
      vector.add("1");
      vector.add("2");
      vector.add("3");
      vector.add("4");
      vector.add("5");

      System.out.println("Before swap: "+vector);
      
      // swap the elements
      Collections.swap(vector, 0, 4);
      
      System.out.println("After swap: "+vector);
   }
}   

现在编译和运行上面的代码示例,将产生以下结果。

Before swap: [1, 2, 3, 4, 5]
After swap: [5, 2, 3, 4, 1]

import java.util.Collections;

 private ArrayList<ProgramInfo> progInfoList = new ArrayList<ProgramInfo>();

 Collections.swap(progInfoList,0,1); //交换元素的位置

一:ArrayList结构图

这里写图片描述

简单说明:

1、上图中虚线且无依赖字样、说明是直接实现的接口

2、虚线但是有依赖字样、说明此类依赖与接口、但不是直接实现接口

3、实线是继承关系、类继承类、接口继承接口

二:ArrayList类简介:

1、ArrayList是内部是以动态数组的形式来存储数据的、知道数组的可能会疑惑:数组不是定长的吗?

这里的动态数组不是意味着去改变原有内部生成的数组的长度、而是保留原有数组的引用、将其指向新生成的数组对象、

这样会造成数组的长度可变的假象。

        2、ArrayList具有数组所具有的特性、通过索引支持随机访问、所以通过随机访问ArrayList中的元素效率非常高、

但是执行插入、删除时效率比较地下、具体原因后面有分析。

3、ArrayList实现了AbstractList抽象类、List接口、所以其更具有了AbstractList和List的功能、

前面我们知道AbstractList内部已经实现了获取Iterator和ListIterator的方法、所以ArrayList只需关心对数组操作的方法的实现。

4、ArrayList实现了RandomAccess接口、此接口只有声明、没有方法体、表示ArrayList支持随机访问。

5、ArrayList实现了Cloneable接口、此接口只有声明、没有方法体、表示ArrayList支持克隆。

6、ArrayList实现了Serializable接口、此接口只有声明、没有方法体、表示ArrayList支持序列化、

即可以将ArrayList以流的形式通过ObjectInputStream/ObjectOutputStream来写/读。

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

三:ArrayList API

// Collection中定义的API
boolean             add(E object)
boolean             addAll(Collection<? extends E> collection)
void                clear()
boolean             contains(Object object)
boolean             containsAll(Collection<?> collection)
boolean             equals(Object object)
int                 hashCode()
boolean             isEmpty()
Iterator<E>         iterator()
boolean             remove(Object object)
boolean             removeAll(Collection<?> collection)
boolean             retainAll(Collection<?> collection)
int                 size()
<T> T[]             toArray(T[] array)
Object[]            toArray()
// AbstractList中定义的API
void                add(int location, E object)
boolean             addAll(int location, Collection<? extends E> collection)
E                   get(int location)
int                 indexOf(Object object)
int                 lastIndexOf(Object object)
ListIterator<E>     listIterator(int location)
ListIterator<E>     listIterator()
E                   remove(int location)
E                   set(int location, E object)
List<E>             subList(int start, int end)
// ArrayList新增的API
Object               clone()
void                 ensureCapacity(int minimumCapacity)
void                 trimToSize()
void                 removeRange(int fromIndex, int toIndex)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

总结:相对与AbstractCollection而言、多实现了List中新增的通过索引操作元素的方法。

四:ArrayList源码分析

package com.chy.collection.core;

import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.RandomAccess;
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {

    private static final long serialVersionUID = 8683452581122892189L;

    /** 保存ArrayList中元素的数组*/
    private transient Object[] elementData;

    /** 保存ArrayList中元素的数组的容量、即数组的size*/
    private int size;

    /** 使用指定的大小创建ArrayList*/
    public ArrayList(int initialCapacity) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
        this.elementData = new Object[initialCapacity];
    }

    /** 使用默认的大小创建ArrayList*/
    public ArrayList() {
        this(10);
    }

    /**
     * 使用指定的Collection构造ArrayList、构造之后的ArrayList中包含Collection中的元素、
     * 这些元素的排序方式是按照ArrayList的Iterator返回他们时候的顺序排序的
     */
    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        size = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    }

    /**
     * 将此 ArrayList 实例的容量调整为列表的当前大小
     */
    public void trimToSize() {
        //此集合总共被修改的次数
        modCount++;
        int oldCapacity = elementData.length;
        if (size < oldCapacity) {
                elementData = Arrays.copyOf(elementData, size);
        }
    }

    /**
     * 确保此ArrayList的最小容量能容纳下参数minCapacity指定的容量、
     * 1、minCapacity大于原来容量、则将原来的容量增加(oldCapacity * 3)/2 + 1;
     * 2、若minCapacity仍然大于增加后的容量、则使用minCapacity作为ArrayList容量
     * 3、若minCapacity不大于增加后的容量、则使用增加后的容量。
     */
    public void ensureCapacity(int minCapacity) {
        modCount++;
        int oldCapacity = elementData.length;
        if (minCapacity > oldCapacity) {
            Object oldData[] = elementData;
            int newCapacity = (oldCapacity * 3)/2 + 1;
                if (newCapacity < minCapacity)
                    newCapacity = minCapacity;
                // minCapacity is usually close to size, so this is a win:
                elementData = Arrays.copyOf(elementData, newCapacity);
        }
    }

    /** 返回此列表中的元素的个数*/
    public int size() {
        return size;
    }

    /** 如果此列表中没有元素,则返回 true*/
    public boolean isEmpty() {
        return size == 0;
    }

    /**  如果此列表中包含指定的元素,则返回 true。*/
    public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }

    /** 返回指定对象在ArrayList中存放的第一个位置索引、注意空值的处理和Object.equals(? extends Object o)的返回值、不存在的话返回-1*/
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

    /** 返回指定对象在ArrayList中存放最后一个位置的索引、注意空值的处理和Object.equals(? extends Object o)的返回值、不存在的话返回-1*/
    public int lastIndexOf(Object o) {
        if (o == null) {
            for (int i = size-1; i >= 0; i--)
            if (elementData[i]==null)
                return i;
        } else {
            for (int i = size-1; i >= 0; i--)
            if (o.equals(elementData[i]))
                return i;
        }
        return -1;
    }

    /** 返回一个当前集合的浅clone对象*/
    public Object clone() {
        try {
            ArrayList<E> v = (ArrayList<E>) super.clone();
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError();
        }
    }

    /** 将当前ArrayList转换成Object数组、注意操作使用此方法转换后的数组有可能抛异常*/
    public Object[] toArray() {
        return Arrays.copyOf(elementData, size);
    }

    /** 
     * 将当前ArrayList转换成与传入的T类型相同的数组、当传入的a的length小于ArrayList的size的时候、方法内部会生成一个新的T[]返回
     *  如果传入的T[]的length大于ArrayList的size、则T[]从下标size开始到最后的元素都自动用null填充。 
     */
    public <T> T[] toArray(T[] a) {
        if (a.length < size)
            // Make a new array of a's runtime type, but my contents:
            return (T[]) Arrays.copyOf(elementData, size, a.getClass());
        System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }

    // Positional Access Operations

    /** 获取ArrayList中索引为index位置的元素*/
    public E get(int index) {
        RangeCheck(index);

        return (E) elementData[index];
    }

    /** 将ArrayList的索引为index处的元素使用指定的E元素替换、返回被替换的原来的元素值*/
    public E set(int index, E element) {
        RangeCheck(index);

        E oldValue = (E) elementData[index];
        elementData[index] = element;
        return oldValue;
    }

    /** 将指定元素E添加到ArrayList的结尾处*/
    public boolean add(E e) {
        //确保ArrayList的容量能够添加新的的元素
        ensureCapacity(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

    /** 将指定元素添加到指定的索引处 、
     *  注意:
     *  1、如果指定的index大于Object[] 的size或者小于0、则抛IndexOutOfBoundException
     *  2、检测Object[]是否需要扩容
     *  3、 将从index开始到最后的元素后移一个位置、
     *  4、将新添加的元素添加到index去。
     */
    public void add(int index, E element) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(
            "Index: "+index+", Size: "+size);

        ensureCapacity(size+1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                 size - index);
        elementData[index] = element;
        size++;
    }

    /** 与add类似、
     *  1、将指定index处的元素删除、
     *  2、将index之后的所有元素前一一个位置、最后一个
     *  3、将最后一个元素设置为null、--size
     *
     *  返回被删除的元素。
     */
    public E remove(int index) {
        RangeCheck(index);

        modCount++;
        E oldValue = (E) elementData[index];

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                     numMoved);
        elementData[--size] = null; // Let gc do its work

        return oldValue;
    }

    /** 删除Object[]中指定的元素Object 类似与contains方法与remove的结合体、只不过这里使用的是fastRemove方法去移除指定元素、移除成功则返回true*/
    public boolean remove(Object o) {
        if (o == null) {
                for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
            }
        return false;
    }

    /* 删除指定索引处的元素、不返回被删除的元素*/
    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // Let gc do its work
    }

    /** 清空ArrayList*/
    public void clear() {
        modCount++;

        // Let gc do its work
        for (int i = 0; i < size; i++)
            elementData[i] = null;
        size = 0;
    }

    /** 将指定集合中的所有元素追加到ArrayList中(从最后开始追加)*/
    public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacity(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }

    /** 将指定集合中的所有元素插入到idnex开始的后面位置处、原有的元素往后排*/
    public boolean addAll(int index, Collection<? extends E> c) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(
            "Index: " + index + ", Size: " + size);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacity(size + numNew);  // Increments modCount

        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew,
                     numMoved);

            System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

    /** 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。
     *  1、将Object[] 从toIdnex开始之后的元素(包括toIndex处的元素)移到Object[]下标从fromIndex开始之后的位置
     *  2、若有Object[]尾部要有剩余的位置则用null填充 
     */
    protected void removeRange(int fromIndex, int toIndex) {
        modCount++;
        int numMoved = size - toIndex;
            System.arraycopy(elementData, toIndex, elementData, fromIndex,
                             numMoved);

        // Let gc do its work
        int newSize = size - (toIndex-fromIndex);
        while (size != newSize)
            elementData[--size] = null;
    }

    /** 检测下标是否越界*/
    private void RangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(
        "Index: "+index+", Size: "+size);
    }

    /** 将此ArrayList写入到ObjectOutputStream流中、先写ArrayList存放元素的Object[]长度、再将Object[]中的每个元素写入到ObjectOutputStream流中*/
    private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{
        // Write out element count, and any hidden stuff
        int expectedModCount = modCount;
        s.defaultWriteObject();

            // Write out array length
            s.writeInt(elementData.length);

        // Write out all elements in the proper order.
        for (int i=0; i<size; i++)
                s.writeObject(elementData[i]);

        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
    }

    /** 从ObjectInputStream中读取ArrayList、先读取ArrayList中Object[]的长度、再读取每个元素放入Object []中对应的位置*/
    private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
    // Read in size, and any hidden stuff
    s.defaultReadObject();

        // Read in array length and allocate array
        int arrayLength = s.readInt();
        Object[] a = elementData = new Object[arrayLength];

    // Read in all elements in the proper order.
    for (int i=0; i<size; i++)
            a[i] = s.readObject();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
        总结:从ArrayList源码可以看出、ArrayList内部是通过动态数组来存储数据、从中我们也可以很容易的找到ArrayList的几个特性:

     1、有序:如果不指定元素存放位置、则元素将依次从Object数组的第一个位置开始放、如果指定插入位置、则会将元素插入指定位置、后面的所有元素都后移

     2、可重复:从源码中没有看到对存放的元素的校验

     3、随机访问效率高:可以直接通过索引定位到我们要找的元素

     4、自动扩容:ensureCapacity(int minCapacity)方法中会确保数组的最小size、当不够时会将原来的容量扩增到:(oldCapacity * 3) / 2 + 1。

     5、变动数组元素个数(即添加、删除数组元素)效率低、在增删的操作中我们常见的一个函数: System.arraycopy()、他是将删除、或者添加之后、原有的元素进行移位、这是需要较大代价的。

     6、 ArrayList不是线程安全的、即当使用多线程操作ArrayList时会有可能出错、后面总结会有。

猜你喜欢

转载自blog.csdn.net/weixin_38451161/article/details/80364702
今日推荐