欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

干了这杯Java之ArrayList存储一个有序元素合集,javaarraylist

来源: javaer 分享于  点击 12063 次 点评:283

干了这杯Java之ArrayList存储一个有序元素合集,javaarraylist


  List接口的实现类有: ArrayList,LinkedList,Vector,Stack

  ArrayList一个数组型的List

  默认容量为10

  private static final int DEFAULT_CAPACITY = 10;

  扩容

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

  }

  扩容的大小为原长度+1/2的原长度成都金牛区哪个医院做阴茎延长术好

  如果扩容长度比传入的最小容量小,则使用最小容量,如果扩容长度超过设定的最大容量,则实用最大正整数

  初始化默认长度为10,当添加到11个长度时,容量为15

  add方法

  public boolean add(E e) {

  ensureCapacityInternal(size + 1); // Increments modCount!!

  elementData[size++] = e;

  return true;

  }

  ensureCapacityInternal(size + 1);确保内部容量,不够则扩容

  elementData[size++] = e;赋值

  remove方法

  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; // clear to let GC do its work

  return oldValue;

  }

  subList内部类

  生成subList对象

  public List subList(int fromIndex, int toIndex) {

  //检查边界男性尿黄混浊有异味怎么回事

  subListRangeCheck(fromIndex, toIndex, size);

  //生成的时SubList对象,注意this

  return new SubList(this, 0, fromIndex, toIndex);

  }

  subList继承自AbstractList

  private final AbstractList parent;

  private final int parentOffset;

  private final int offset;

  int size;

  SubList(AbstractList parent,

  int offset, int fromIndex, int toIndex) {

  this.parent = parent;

  this.parentOffset = fromIndex;

  this.offset = offset + fromIndex;

  this.size = toIndex - fromIndex;

  this.modCount = ArrayList.this.modCount;

  }

  在构造方法中this.parent = parent,意味着对象为原始list

  this.parentOffset = fromIndex;和this.offset = offset + fromIndex;为原始索引

  public void add(int index, E e) {

  rangeCheckForAdd(index);

  checkForComodification();

  parent.add(parentOffset + index, e);

  this.modCount = parent.modCount;

  this.size++;男性性欲下降会影响生育吗?

  }

  parent.add(parentOffset + index, e);原始list将被添加一个元素

  remove方法中E result = parent.remove(parentOffset + index);将在原始list中移除

  结论:

  在操作sublist的添加、移除等方法的时候,原始list将会被修改

  sublist是一个list的视图

相关文章

    暂无相关文章

用户点评