本文将以以下几个问题来探讨ArrayList的源码实现

1.ArrayList的大小是如何自动增加的

2.什么情况下你会使用ArrayList?什么时候你会选择LinkedList?

3.如何复制某个ArrayList到另一个ArrayList中去?写出你的代码?

4.在索引中ArrayList的增加或者删除某个对象的运行过程?效率很低吗?解释一下为什么?

5.Interator在ArrayList的实现

关于Java集合的小抄 关于ArrayList的描述:
*以数组实现。节约空间,但数组有容量限制。超出限制时会增加50%容量,用System.arraycopy()复制到新的数组,因此最好能给出数组大小的预估值。默认第一次插入元素时创建大小为10的数组。
按数组下标访问元素--get(i)/set(i,e) 的性能很高,这是数组的基本优势。
直接在数组末尾加入元素--add(e)的性能也高,但如果按下标插入、删除元素--add(i,e), remove(i), remove(e),则要用System.arraycopy()来移动部分受影响的元素,性能就变差了,这是基本劣势。*

1、ArrayList的大小是如何自动增加的

直接上代码吧,每次add的时候都会判断是否需要扩容,以下是扩容的主要方法
```java
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}

  1. private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
  2. private void grow(int minCapacity) {
  3. // overflow-conscious code
  4. int oldCapacity = elementData.length;
  5. //左移1位,相当于除以2,就是容量提高50%
  6. int newCapacity = oldCapacity + (oldCapacity >> 1);
  7. if (newCapacity - minCapacity < 0)
  8. newCapacity = minCapacity;
  9. //最大的阀值MAX_ARRAY_SIZE
  10. if (newCapacity - MAX_ARRAY_SIZE > 0)
  11. newCapacity = hugeCapacity(minCapacity);
  12. // minCapacity is usually close to size, so this is a win:
  13. elementData = Arrays.copyOf(elementData, newCapacity);
  14. }

  1. **2、什么情况下你会使用ArrayList?什么时候你会选择LinkedList?**
  2. <p>我们知道ArrayListLinkedList的数据结构是不同的,ArrayList是以连续的数组进行存储的,所以它的get是常数级别的,LinkedList是双向链表存储的。他的查询最坏情况是n。以为ArrayList是数组存储的,所以当你查找某一指定索引的数据时,它每次删除和指定索引添加都要移动数组的位置,其内部的实现方式是数组复杂用到System.arraycope,是比较影响性能的,而双向链表删除和插入只要找到相应的节点位置,关联下指针,所以性能会更好。
  3. ```java
  4. public void add(int index, E element) {
  5. //判断是否超出了索引
  6. rangeCheckForAdd(index);
  7. //判断是否需要扩容
  8. ensureCapacityInternal(size + 1); // Increments modCount!!
  9. //讲elementData复制到elementData,从index开始复制,从index+1开始粘贴,复制的长度是size-index
  10. System.arraycopy(elementData, index, elementData, index + 1,
  11. size - index);
  12. elementData[index] = element;
  13. size++;
  14. }

以及删除方法

  1. public E remove(int index) {
  2. //判断是否超出索引
  3. rangeCheck(index);
  4. modCount++;
  5. E oldValue = elementData(index);
  6. //线性移动数组
  7. int numMoved = size - index - 1;
  8. if (numMoved > 0)
  9. System.arraycopy(elementData, index+1, elementData, index,
  10. numMoved);
  11. elementData[--size] = null; // clear to let GC do its work
  12. return oldValue;
  13. }

3、如何复制某个ArrayList到另一个ArrayList中去?写出你的代码?

下面就是把某个ArrayList复制到另一个ArrayList中去的几种技术:

使用clone()方法,比如ArrayList newArray = oldArray.clone()

使用ArrayList构造方法,比如:ArrayList myObject = new ArrayList(myTempObject)

其他
```java
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(e);
}
}
```
```java
public ArrayList(Collection 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);
}
```

综上所述,最终的复制方式都是调用Arrays.copyOf,而Arrays.copyOf是调用System.arrayscopy
```java
public static T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}

  1. public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
  2. @SuppressWarnings("unchecked")
  3. T[] copy = ((Object)newType == (Object)Object[].class)
  4. ? (T[]) new Object[newLength]
  5. : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  6. System.arraycopy(original, 0, copy, 0,
  7. Math.min(original.length, newLength));
  8. return copy;
  9. }
  1. **4、在索引中ArrayList的增加或者删除某个对象的运行过程?效率很低吗?**
  2. <p>这个问题同2,因为添加删除某个索引的数据时,需要整体移动数组,所以效率比较低。
  3. **5InteratorArrayList的实现**
  4. <p>因为这个实现的代码比较简单这里就不多解释了,特别说明下forEachRemaining,这个方法是jdk1.8加上的,支持lamdba表达式,主要是遍历游标后面的数据,看while循环i++
  5. ```java
  6. private class Itr implements Iterator<E> {
  7. int cursor; // index of next element to return
  8. int lastRet = -1; // index of last element returned; -1 if no such
  9. int expectedModCount = modCount;
  10. public boolean hasNext() {
  11. return cursor != size;
  12. }
  13. @SuppressWarnings("unchecked")
  14. public E next() {
  15. checkForComodification();
  16. int i = cursor;
  17. if (i >= size)
  18. throw new NoSuchElementException();
  19. Object[] elementData = ArrayList.this.elementData;
  20. if (i >= elementData.length)
  21. throw new ConcurrentModificationException();
  22. //游标的位置加1
  23. cursor = i + 1;
  24. return (E) elementData[lastRet = i];
  25. }
  26. public void remove() {
  27. if (lastRet < 0)
  28. throw new IllegalStateException();
  29. checkForComodification();
  30. try {
  31. ArrayList.this.remove(lastRet);
  32. cursor = lastRet;
  33. lastRet = -1;
  34. expectedModCount = modCount;
  35. } catch (IndexOutOfBoundsException ex) {
  36. throw new ConcurrentModificationException();
  37. }
  38. }
  39. @Override
  40. @SuppressWarnings("unchecked")
  41. public void forEachRemaining(Consumer<? super E> consumer) {
  42. Objects.requireNonNull(consumer);
  43. final int size = ArrayList.this.size;
  44. int i = cursor;
  45. if (i >= size) {
  46. return;
  47. }
  48. final Object[] elementData = ArrayList.this.elementData;
  49. if (i >= elementData.length) {
  50. throw new ConcurrentModificationException();
  51. }
  52. while (i != size && modCount == expectedModCount) {
  53. consumer.accept((E) elementData[i++]);
  54. }
  55. // update once at end of iteration to reduce heap write traffic
  56. cursor = i;
  57. lastRet = i - 1;
  58. checkForComodification();
  59. }
  60. final void checkForComodification() {
  61. if (modCount != expectedModCount)
  62. throw new ConcurrentModificationException();
  63. }
  64. }

参考

ArrayList实现源码分析的更多相关文章

  1. ArrayList迭代器源码分析

    集合的遍历 Java集合框架中容器有很多种类,如下图中: 对于有索引的List集合可以通过for循环遍历集合: List<String> list = new ArrayList<& ...

  2. ArrayList的源码分析

    在项目中经常会用到list集合来存储数据,而其中ArrayList是用的最多的的一个集合,这篇博文主要简单介绍ArrayList的源码分析,基于JDK1.7: 这里主要介绍 集合 的属性,构造器,和方 ...

  3. 数据结构——ArrayList的源码分析(你所有的疑问,都会被解答)

    一.首先来看一下ArrayList的类图: 1,实现了RandomAccess接口,可以达到随机访问的效果. 2,实现了Serializable接口,可以用来序列化或者反序列化. 3,实现了List接 ...

  4. ArrayList方法源码分析

    本文将从ArrayList类的存储结构.初始化.增删数据.扩容处理以及元素迭代等几个方面,分析该类常用方法的源码. 数据存储设计 该类用一个Object类型的数组存储容器的元素.对于容量为空的情况,提 ...

  5. 集合之ArrayList的源码分析

    转载请注明出处 一.介绍 对于ArrayList,可以说诸位绝不陌生,可以说是在诸多集合中运用的最多一个类之一,那么它是怎样构成,怎样实现的呢,相信很多人都知道数组构成的,没毛病,如果遇到面试的时候, ...

  6. Java——ArrayList底层源码分析

    1.简介 ArrayList 是最常用的 List 实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔, 当数组大小不满足时需要增加存储能力,就要将已经有数 ...

  7. 迎难而上ArrayList,源码分析走一波

    先看再点赞,给自己一点思考的时间,思考过后请毫不犹豫微信搜索[沉默王二],关注这个长发飘飘却靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有技术大佬整理 ...

  8. ArrayList<E>源码分析

    ArrayList是按照线性表结构实现的 ArrayList的主要继承结构 public class ArrayList<E> extends AbstractList<E> ...

  9. ArrayList构造方法源码分析

    首先看一下无参的构造方法: private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; transient Object ...

随机推荐

  1. 阿里云centos试用

    今天体验了下阿里云的centos,起初用的官方推荐的putty方式来管理,全部使用命令行管理起来还是很别扭的. 后来通过百度了解到winscp,有了这个工具,管理起来就爽很多啦.整个centos的管理 ...

  2. 循环a数组(值代表b的下标)删除b数组中存在的记录,从后往前删

    for (var j = adelete.Count-1; j >= 0; --j)                     {                         aAttachm ...

  3. 做最好的日期控件,My97 DatePicker 4.8 Beta4

    4.8更新的内容 [新增]preload预载选项 [新增]时分秒选择菜单的定制功能,详见hmsMenuCfg属性[beta4] [新增]$dp.unbind函数,允许用户手动接触日期控件绑定[beta ...

  4. Python基本语句

    x = 1 y = 1 dataf = 1.0 def fun1(): print("fun1") class ClassB(): def __init__(self): prin ...

  5. 如何在终端实时展现git分支

    在微博上看到ghosTM55在终端可以实时展现出当前运行的分支,觉得很奇特,于是google了一把.这里面存在两个内容,第一个bash,第二个是git bash基础: 了解到linux的shell存在 ...

  6. csv大文件分割以及添加表头

    注:这里说的大文件也不是太大,只有60多M而已(70多万条数据),相对比较大而已. 为了减轻编辑的工作,某种情况下网站上可能用会到csv格式的文件进行数据导入,但一般网站除了有上传文件大小限制以外,还 ...

  7. IIS崩溃时自动抓取Dump

    背景:在客户现场,IIS有时会崩溃,开发环境没法重现这个bug,唯有抓取IIS的崩溃是的Dump文件分析. IIS崩溃时自动抓取Dump,需要满足下面几个条件 1.启动 Windows Error R ...

  8. [转]C# dataGridview 报“索引-1没有值”的解决办法

    很多WINFORM的开发人员在DataGridView的开发当中,都会出现“索引-1没有值”这个烦人的问题,其实较早之前,我已经大概知道问题的所在,也找到了解决方法,不过一直没有时间去深入研究一下,今 ...

  9. css3背景颜色渐变

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  10. (转)ubuntu安装opengl

    $ sudo apt-get install build-essential 安装OpenGL Library接下来要把我们会用到的 Library 装上去,首先安装 OpenGL Library $ ...