length > 0) grow(minCapacity); // 进行扩容 } // 数组允许的最大长度 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; private void grow(int minCapacity) { // 数组扩容 // overflow-conscious code int oldCapacity =
获取长度: arr.length 举例: int[] arr = new int[5]; int[] arr = new int[] {11, 22, 33}; String[] arr = new String[4]; 数组的特点: 1.数组长度固定 2.数组即可以存储基本数据类型,也可以存储引用数据类型 二、ArrayList的简介 查API 1.看包 java.util 2.看类的说明 ArrayList也是一个...
// optimization that results in faster sorts for nearly ordered lists. if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) { System.arraycopy(src, low, dest, destLow, length); return; } // Merge sorted halves (now in src) into dest for(int i = destLow, p = low, q = ...
代码语言:java AI代码解释 privatevoidgrow(intminCapacity){// overflow-conscious codeintoldCapacity=elementData.length;intnewCapacity=oldCapacity+(oldCapacity>>1);if(newCapacity-minCapacity<0)newCapacity=minCapacity;if(newCapacity-MAX_ARRAY_SIZE>0)newCapacity=hugeCapacity(minCapacity);// 将原数组元素拷...
import java.util.ArrayList; public class RunoobTest { public static void main(String[] args) { ArrayList<String> sites = new ArrayList<String>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); sites.add("Weibo"); sites.remove(3); // 删除第四个元素 System.out.print...
modCount++;//这里就是add方法中注释的Increments modCount//溢出if(minCapacity - elementData.length >0) grow(minCapacity);//这里就是执行扩容的方法} 下面来看一下扩容的主要方法grow。 grow方法分析 privatestaticfinalintMAX_ARRAY_SIZE=Integer.MAX_VALUE -8;privatevoidgrow(intminCapacity){// old...
throws java.io.IOException{ // Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject(); // Write out array length s.writeInt(elementData.length); // Write out all elements in the proper order. ...
// ArrayList.javapublic void ensureCapacity(int minCapacity) {if (minCapacity > elementData.length ...
length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) // 如果c.toArray()返回的数组类型不是Object[]类型的 // 则利用Arrays.copyOf()创建一个大小为size的、类型为Object[]的数组, 并将elementData中的元素复制到新的数组中 // 最后让elementData 指向新的数组 if ...
int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); ...