博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDK源码 ArrayList
阅读量:7077 次
发布时间:2019-06-28

本文共 5503 字,大约阅读时间需要 18 分钟。

java.lang.Object

    java.util.AbstractCollection<E>
        java.util.AbstractList<E>
            java.util.ArrayList<E>

所有已实现的接口:
, , <E>, <E>, <E>,
直接已知子类:
, ,

1.Iterator方法

ArrayList l = new ArrayList();

Iterator iterator = l.iterator();

调用父类AbstractList的iterator()方法,得到Itr实例(AbstractList的内部类,实现了Iterator接口),利用cursor游标实现hashNext(),及next()方法

public abstract class AbstractList
extends AbstractCollection
implements List
{ protected AbstractList() { } public Iterator
iterator() { return new Itr(); } private class Itr implements Iterator
{ /** * Index of element to be returned by subsequent call to next. */ int cursor = 0; /** * Index of element returned by most recent call to next or * previous. Reset to -1 if this element is deleted by a call * to remove. */ int lastRet = -1; /** * The modCount value that the iterator believes that the backing * List should have. If this expectation is violated, the iterator * has detected concurrent modification. */ int expectedModCount = modCount; public boolean hasNext() { return cursor != size(); } public E next() { checkForComodification(); try { E next = get(cursor); lastRet = cursor++; return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } public void remove() { if (lastRet == -1) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.remove(lastRet); if (lastRet < cursor) cursor--; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } } }

2.Add方法

//添加元素 public boolean add(E e) {    ensureCapacity(size + 1);  // Increments modCount!!    elementData[size++] = e; //elementData为缓存数组,若不设置list的初始大小,此数组默认大小为10    return true;    }   //当添加的元素数量超出数组初始值时,进行扩容操作    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);    }    }

 3.addAll方法

public boolean addAll(Collection
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; }

4.clear方法

public void clear() {        //操作次数加一    modCount++;    // Let gc do its work    for (int i = 0; i < size; i++)        elementData[i] = null;    size = 0;    }

5.clone方法

public Object clone() {    try {        ArrayList
v = (ArrayList
) 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(); } } public static
T[] copyOf(T[] original, int newLength) { return (T[]) copyOf(original, newLength, original.getClass()); } public static
T[] copyOf(U[] original, int newLength, Class
newType) { T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }

6.get方法

public E get(int index) {    RangeCheck(index);    return (E) elementData[index];    }    //检测数组越界    private void RangeCheck(int index) {    if (index >= size)        throw new IndexOutOfBoundsException(        "Index: "+index+", Size: "+size);    }

7.indexOf方法

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;    }

8.remove方法

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;    }

 9.removeAll方法

public boolean removeAll(Collection
c) { boolean modified = false; Iterator
e = iterator(); while (e.hasNext()) { if (c.contains(e.next())) { e.remove(); modified = true; } } return modified; } //contains方法调用了indexOf方法 public boolean contains(Object o) { return indexOf(o) >= 0; }

 

 

 

转载于:https://www.cnblogs.com/nsxqf/p/6973953.html

你可能感兴趣的文章
ThinkSNS开源社交系统安装问题及解决措施
查看>>
2018年微信小程序风口最新发展趋势分析
查看>>
ThinkSNS+ 移动端1.8.2.0704 版本更新简要说明
查看>>
redis开启远程访问
查看>>
真正能支撑高并发以及高可用的复杂系统中的缓存架构有哪些东西?
查看>>
Oracle 12c:ORA-28040
查看>>
我的友情链接
查看>>
img 样式单和属性
查看>>
RAID磁盘阵列种类及区别
查看>>
spring MVC自定义视图实现jsonp
查看>>
面向对象之继承时的关键词
查看>>
H3C的CDP——NDP
查看>>
inspect a service on the swarm
查看>>
spark shell的学习
查看>>
安卓模拟器BlueStacks+TCPdump对APP抓包分析
查看>>
maven scope含义的说明
查看>>
Javac编译器源代码分析
查看>>
我的友情链接
查看>>
JAVA中的类型转换 int和String
查看>>
计划任务不能启动
查看>>