服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java 集合系列(二)ArrayList详解

Java 集合系列(二)ArrayList详解

2021-07-26 11:29那一叶随风 Java教程

这篇文章主要介绍了Java集合系列ArrayList,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

arraylist

arraylist 是通过一个数组来实现的,因此它是在连续的存储位置存放对象的引用,只不过它比 array 更智能,能够根据集合长度进行自动扩容。

假设让我们来实现一个简单的能够自动扩容的数组,我们最容易想到的点就是:

  1. add()的时候需要判断当前数组size+1是否等于此时定义的数组大小;
  2. 若小于直接添加即可;否则,需要先扩容再进行添加。

实际上,arraylist的内部实现原理也是这样子,我们可以来研究分析一下arraylist的源码

add(e e) 源码分析

?
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
/**
   * appends the specified element to the end of this list.
   *
   * @param e element to be appended to this list
   * @return <tt>true</tt> (as specified by {@link collection#add})
   */
  public boolean add(e e) {
    ensurecapacityinternal(size + 1);  // 进行扩容校验
    elementdata[size++] = e;      // 将值添加到数组后面,并将 size+1
    return true;
  }
 
 
 
  /**
   * the array buffer into which the elements of the arraylist are stored.
   * the capacity of the arraylist is the length of this array buffer. any
   * empty arraylist with elementdata == defaultcapacity_empty_elementdata
   * will be expanded to default_capacity when the first element is added.
   */
  transient object[] elementdata; // non-private to simplify nested class access
  
  private void ensurecapacityinternal(int mincapacity) {
    ensureexplicitcapacity(calculatecapacity(elementdata, mincapacity));  // elementdata 数组
  }
 
 
 
  /**
   * default initial capacity.
   */
  private static final int default_capacity = 10;
  
  /**
   * shared empty array instance used for default sized empty instances. we
   * distinguish this from empty_elementdata to know how much to inflate when
   * first element is added.
   */
  private static final object[] defaultcapacity_empty_elementdata = {};
 
  // 返回最大的 index
  private static int calculatecapacity(object[] elementdata, int mincapacity) {
    if (elementdata == defaultcapacity_empty_elementdata) {  // 与空数组实例对比
      return math.max(default_capacity, mincapacity);
    }
    return mincapacity;
  }
 
 
 
  private void ensureexplicitcapacity(int mincapacity) {
    modcount++;
 
    // overflow-conscious code
    if (mincapacity - elementdata.length > 0)
      grow(mincapacity);
  }

扩容调用方法,实际也就是数组复制的过程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
   * increases the capacity to ensure that it can hold at least the
   * number of elements specified by the minimum capacity argument.
   *
   * @param mincapacity the desired minimum capacity
   */
  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);
  }

add(int index, e element) 源码分析

?
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
/**
   * inserts the specified element at the specified position in this
   * list. shifts the element currently at that position (if any) and
   * any subsequent elements to the right (adds one to their indices).
   *
   * @param index index at which the specified element is to be inserted
   * @param element element to be inserted
   * @throws indexoutofboundsexception {@inheritdoc}
   */
  public void add(int index, e element) {
    rangecheckforadd(index);  // 校验index是否超过当前定义的数组大小范围,超过则抛出 indexoutofboundsexception
 
    ensurecapacityinternal(size + 1); // increments modcount!!
    system.arraycopy(elementdata, index, elementdata, index + 1,
             size - index);   // 复制,向后移动
    elementdata[index] = element;
    size++;
  }
  
 
  /**
   * a version of rangecheck used by add and addall.
   */
  private void rangecheckforadd(int index) {
    if (index > size || index < 0)
      throw new indexoutofboundsexception(outofboundsmsg(index));
  }

从上面的源码分析可知,扩容和随机插入元素的消耗比较大,因此在实际开发中,应尽量指定arraylist大小,减少在随机插入操作。

优缺点

优点

  1. 封装了一个动态再分配的对象数组
  2. 使用索引进行随机访问效率高

缺陷

  1. 在数组中增删一个元素,所有元素都要往后往前移动,效率低下

知识脑图

Java 集合系列(二)ArrayList详解

以上所述是小编给大家介绍的java集合系列arraylist详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/phpstudy2015-6/p/10618707.html

延伸 · 阅读

精彩推荐