ArrayList

  • 是什么,定义?

这是动态的数组,它提供了动态的增加和减少元素,实现了List接口(List实现Collection,所以也实现Collection接口)灵活的设置数组的大小等好处

  

  • 内部如何实现
     /**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient Object[] elementData;//内部是数组 /**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;//定义大小 /**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {//带参数的初始化
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
} /**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {//默认初始化,大小是10
this(10);
}
  • 添加如何操作
   public boolean add(E e) {
ensureCapacityInternal(size + 1); // 进行长度的判断与修改(将检查与扩容放在一个方法调用,代码阅读性高)
elementData[size++] = e;
return true;
}
  
private void ensureCapacityInternal(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)//进行判断
grow(minCapacity);
} 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);
}
  • 添加重要的一部,扩容,——这是为什么实际应用中要定义数组大小的原因,
    //Array的源代码
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
} public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> 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;
}
  • 制定位置的添加
     public void add(int index, E element) {
rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);//仅仅是当前位置以后的对象进行复制
elementData[index] = element;
size++;
}
  • 删除对象
    public E remove(int index) {
rangeCheck(index); modCount++;
E oldValue = 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;
}
  • 怎么遍历呢?

数组,通过数组的脚标,遍历;

for each 因为实现Iterator,也可以使用迭代器

  

使用中改注意:

1、初始化最好定义都用的长度,避免Arrarlist内部复制的操作;

2、如果删除的,尽量从后往前删除

3、不是线程安全,全部代码中都没有synchronized ,

  如果有需要,使用Vector,两者的区别3点(其他基本是一样):

  线程安全,

  多了方法indexOf,

  扩容的打大小是:1倍

      int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
      capacityIncrement : oldCapacity);

  

java基础,集合,Arraylist,源码解析(基础)的更多相关文章

  1. Java集合-ArrayList源码解析-JDK1.8

    ◆ ArrayList简介 ◆ ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAcc ...

  2. 集合-ArrayList 源码解析

    ArrayList是一种以数组实现的List,与数组相比,它具有动态扩展的能力,因此也可称之为动态数组. 类图 ArrayList实现了List, RandomAccess, Cloneable, j ...

  3. java学习笔记之集合—ArrayList源码解析

    1.ArrayList简介 ArrayList是一个数组队列,与java中的数组的容量固定不同,它可以动态的实现容量的增涨.所以ArrayList也叫动态数组.当我们知道有多少个数据元素的时候,我们用 ...

  4. Java中的容器(集合)之ArrayList源码解析

    1.ArrayList源码解析 源码解析: 如下源码来自JDK8(如需查看ArrayList扩容源码解析请跳转至<Java中的容器(集合)>第十条):. package java.util ...

  5. Collection集合重难点梳理,增强for注意事项和三种遍历的应用场景,栈和队列特点,数组和链表特点,ArrayList源码解析, LinkedList-源码解析

    重难点梳理 使用到的新单词: 1.collection[kəˈlekʃn] 聚集 2.empty[ˈempti] 空的 3.clear[klɪə(r)] 清除 4.iterator 迭代器 学习目标: ...

  6. ArrayList源码解析

    ArrayList简介 ArrayList定义 1 public class ArrayList<E> extends AbstractList<E> implements L ...

  7. 【java提高】---ArrayList源码

    ArrayList源码 一.定义 public class ArrayList<E> extends AbstractList<E> implements List<E& ...

  8. 面试必备:ArrayList源码解析(JDK8)

    面试必备:ArrayList源码解析(JDK8) https://blog.csdn.net/zxt0601/article/details/77281231 概述很久没有写博客了,准确的说17年以来 ...

  9. ArrayList源码解析--值得深读

    ArrayList源码解析 基于jdk1.8 ArrayList的定义 类注释 允许put null值,会自动扩容: size isEmpty.get.set.add等方法时间复杂度是O(1): 是非 ...

  10. 【源码解析】- ArrayList源码解析,绝对详细

    ArrayList源码解析 简介 ArrayList是Java集合框架中非常常用的一种数据结构.继承自AbstractList,实现了List接口.底层基于数组来实现动态容量大小的控制,允许null值 ...

随机推荐

  1. 数值函数ROUND(四舍五入),TRUNC(不四舍五入),MOD

  2. Swift MD5加密 所需桥接文件

    Swift MD5加密在github有一个非常好的第三方库,使用也比较简单,还有很多加密方法,如果需要,点击这里下载 对于那些不需要太多的加密,只需要MD5加密的同学,我建议还是不要用第三方库. 因为 ...

  3. Python学习日记day10------函数的命名空间、作用域与闭合函数

    1,参数陷阱 如果默认参数的只是一个可变数据类型,那么每一次调用的时候,如果不传值就共用这个数据类型的资源. 2,三元运算 c=a if a>b else b#如果a>b返回a,否则,返回 ...

  4. ABP .Net Core API和Angular前端APP集成部署

    前言:在ABP官网(https://aspnetboilerplate.com)生成的.Net Core + Angular项目前后端是两个独立的项目,我们可以分开部署,也可以将前端和Web API一 ...

  5. 【Zookeeper】源码分析之服务器(一)

    一.前言 前面已经介绍了Zookeeper中Leader选举的具体流程,接着来学习Zookeeper中的各种服务器. 二.总体框架图 对于服务器,其框架图如下图所示 说明: ZooKeeperServ ...

  6. python写入文本报错TypeError: expected a string or other character buffer object

    今天用python写入文本, file_object2 = open('result.txt', 'w') file_object2.write(bookid_list) file_object2.c ...

  7. <tangmuchw>之新手vue项目小记--新建.vue文件,运行项目,出现error:This dependency was not found...

    错误码: This dependency was not found: * !!vue-style-loader!css-loader?{"minimize":false,&quo ...

  8. Unix 文件系统读写时权限校验

    文件系统中的所有文件都是在读出或写入时进行权限校验 一个问题,如果一个用户对一个普通文件有读写权限,在使用vim编辑时,管理员撤销掉此用户对此文件的写入权限 那么,这个普通用户还可以将修改写入文件吗?

  9. RepeatMasker使用中的问题

    RepeatMasker在运行时会先产生如下一个中间文件夹如RM_23346.WedAug301137422017,最后生成结果文件,例如.out,.masked,.tbl等 软件特性:软件运行很慢, ...

  10. C语言中static关键字的用法

    C记得还是大一时学的,现在觉得好久没用了,又捧起来看看.今天刚看到有关static关键字,仔细地看了一遍<C和指针>这本书中的解释,现在觉得清楚多了. 首先,我们将static关键字,修饰 ...