一、数组是否可以变长?
我们都知道,数组时定长的,初始化时一定要给定长度,由于这个长度的问题,我们在实际的开发中,会更倾向于使用容器,如ArrayList等,使用容器类时,无需考虑长度问题,因为容器已经帮我们处理了,那么数组就没有办法变长了吗?当然不是,ArrayList就是基于数组实现的,我们可以看看ArrayList是如何处理的
二、ArrayList的实现原理
ArrayList用一个Object数组作为其内部操作,并有一个成员变量size代表容器的长度
<span style="color:#000000"><code> <span style="color:#000088">private</span> <span style="color:#000088">transient</span> Object[] elementData;
<span style="color:#000088">private</span> <span style="color:#000088">int</span> size;</code></span>
添加数据时,调用add()方法:
<span style="color:#000000"><code><span style="color:#000088">public</span> <span style="color:#000088">boolean</span> <span style="color:#009900">add</span>(E e) {
ensureCapacityInternal(size + <span style="color:#006666">1</span>); <span style="color:#880000">// Increments modCount!!</span>
elementData[size++] = e;
<span style="color:#000088">return</span> <span style="color:#000088">true</span>;
}</code></span>
<span style="color:#000000"><code> <span style="color:#000088">private</span> <span style="color:#000088">void</span> <span style="color:#009900">ensureCapacityInternal</span>(<span style="color:#000088">int</span> minCapacity) {
modCount++;
<span style="color:#880000">// 如果添加了新元素后的长度超出了原来的长度,则需要扩容</span>
<span style="color:#000088">if</span> (minCapacity - elementData.length > <span style="color:#006666">0</span>)
grow(minCapacity);
}</code></span>
可以看到扩容的核心代码在grow方法中:
<span style="color:#000000"><code> <span style="color:#000088">private</span> <span style="color:#000088">void</span> <span style="color:#009900">grow</span>(<span style="color:#000088">int</span> minCapacity) {
<span style="color:#000088">int</span> oldCapacity = elementData.length;
<span style="color:#880000">//扩容,生成新容量,为原来的2倍</span>
<span style="color:#000088">int</span> newCapacity = oldCapacity + (oldCapacity >> <span style="color:#006666">1</span>);
<span style="color:#000088">if</span> (newCapacity - minCapacity < <span style="color:#006666">0</span>)
newCapacity = minCapacity;
<span style="color:#000088">if</span> (newCapacity - MAX_ARRAY_SIZE > <span style="color:#006666">0</span>)
newCapacity = hugeCapacity(minCapacity);
<span style="color:#880000">// 把数组扩大到新长度</span>
elementData = Arrays.copyOf(elementData, newCapacity);
}</code></span>
elementData = Arrays.copyOf(elementData, newCapacity)
API文档的解释是:复制指定的数组,截取或用 null 或 0 填充(如有必要),以使副本具有指定的长度
也就是说,通过Arrays.copyOf,将数组elementData的长度扩大到newCapacity,扩大的部分填充由数组类型决定
例如:
<span style="color:#000000"><code> int[] a = new int[]{<span style="color:#006666">1</span>,<span style="color:#006666">2</span>}<span style="color:#880000">;</span>
System<span style="color:#009900">.out</span><span style="color:#009900">.println</span>(<span style="color:#009900">"扩容前长度:"</span> + a<span style="color:#009900">.length</span>)<span style="color:#880000">;</span>
a = Arrays<span style="color:#009900">.copyOf</span>(a, <span style="color:#006666">3</span>)<span style="color:#880000">;</span>
System<span style="color:#009900">.out</span><span style="color:#009900">.println</span>(<span style="color:#009900">"扩容后长度:"</span> + a<span style="color:#009900">.length</span>)<span style="color:#880000">;</span>
System<span style="color:#009900">.out</span><span style="color:#009900">.println</span>(<span style="color:#009900">"填充的数据:"</span> + a[<span style="color:#006666">2</span>])<span style="color:#880000">;</span></code></span>
输出:
扩容前长度:2
扩容后长度:3
填充的数据:0
我们来查看一下Arrays.copyOf源代码,看看它做了什么:
<span style="color:#000000"><code> <span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">int</span>[] <span style="color:#009900">copyOf</span>(<span style="color:#000088">int</span>[] original, <span style="color:#000088">int</span> newLength) {
<span style="color:#000088">int</span>[] copy = <span style="color:#000088">new</span> <span style="color:#000088">int</span>[newLength];
System.arraycopy(original, <span style="color:#006666">0</span>, copy, <span style="color:#006666">0</span>,
Math.min(original.length, newLength));
<span style="color:#000088">return</span> copy;
}</code></span>
Arrays.copyOf方法中,重新创建了一个新长度的数组,并且通过System.arraycopy方法把原来的数组复制到新数组中,最后返回。System.arraycopy是一个本地的系统方法,采用的是直接对内存中的数据块进行复制,是一块一块复制的(操作系统中有讲到),所以比我们平时for循环一个一个复制要快得多。
三、效率
值得注意这些问题,什么时候会扩容?扩容会影响效率吗?如果影响要怎么避免?
当增加元素后的size 大于 原来的size时,就需要扩容,而扩容需要复制数组,小数据量还好,而大数据量的时候肯定很影响效率!注意到,ArrayList中有两个和size相关构造函数:
<span style="color:#000000"><code> <span style="color:#000088">public</span> <span style="color:#009900">ArrayList</span>() {
<span style="color:#000088">this</span>(<span style="color:#006666">10</span>);
}
<span style="color:#000088">public</span> <span style="color:#009900">ArrayList</span>(<span style="color:#000088">int</span> initialCapacity) {
<span style="color:#000088">super</span>();
<span style="color:#000088">if</span> (initialCapacity < <span style="color:#006666">0</span>)
<span style="color:#000088">throw</span> <span style="color:#000088">new</span> IllegalArgumentException(<span style="color:#009900">"Illegal Capacity: "</span>+
initialCapacity);
<span style="color:#000088">this</span>.elementData = <span style="color:#000088">new</span> Object[initialCapacity];
}</code></span>
第一个构造函数给定默认容量size为10,而第二个函数默认size自定义,那么我们看这两个构造函数在大数据量的情况:
<span style="color:#000000"><code><span style="color:#000088">public</span> <span style="color:#000088">abstract</span> <span style="color:#000088">class</span> Array1 {
<span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">void</span> <span style="color:#009900">main</span>(String[] args) {
<span style="color:#000088">long</span> b1 = System.currentTimeMillis();
List<Integer> list1 = <span style="color:#000088">new</span> ArrayList<Integer>();
<span style="color:#000088">for</span>(<span style="color:#000088">int</span> i =<span style="color:#006666">0</span>; i < <span style="color:#006666">10000000</span>; i++){
list1.add(i);
}
<span style="color:#000088">long</span> e1 = System.currentTimeMillis();
System.<span style="color:#000088">out</span>.println(<span style="color:#009900">"时间为 : "</span> + (e1-b1));
<span style="color:#000088">long</span> b2 = System.currentTimeMillis();
List<Integer> list2 = <span style="color:#000088">new</span> ArrayList<Integer>(<span style="color:#006666">10000000</span>);
<span style="color:#000088">for</span>(<span style="color:#000088">int</span> i =<span style="color:#006666">0</span>; i < <span style="color:#006666">10000000</span>; i++){
list2.add(i);
}
<span style="color:#000088">long</span> e2 = System.currentTimeMillis();
System.<span style="color:#000088">out</span>.println(<span style="color:#009900">"时间为 : "</span> + (e2-b2));
}
}</code></span>
结果如下:
时间为 : 7878
时间为 : 283
所以:请为你的ArrayList指定初始容量!
三、Arrays类的使用
Arrays类包含用来操作数组(比如排序和搜索)的各种方法,这个类是必须熟练使用的!
该类的常用方法有:
方法 | 解释 |
---|---|
asList(T… a) | 返回一个受指定数组支持的固定大小的列表 |
copyOf(int[] original,int newLength) | 复制指定的数组,填充的数据由数组类型决定,以使副本具有指定的长度 |
copyOfRange(long[] original, int from, int to) | 将指定数组的指定范围复制到一个新数组 |
equals(int[] a, int[] a2) | 如果两个指定的 int 型数组彼此相等,则返回 true |
fill(int[] a, int val) | 将指定的 int 值分配给指定 int 型数组的每个元素 |
sort(int[] a) | 对指定的 int 型数组按数字升序进行排序 |
<span style="color:#000000"><code>以上只用int类型数组做为example,同理其他类型数组同样的操作
</code></span>
四、copyOf的陷阱,数组的浅拷贝
当数组类型不是基本数据类型时,数组内存放的是对象的引用,因此在copyOf复制对象数组时,千万要注意,复制的对象的引用而不是对象本身!
<span style="color:#000000"><code>class Person{
<span style="color:#000088">int</span> age;
<span style="color:#000088">public</span> <span style="color:#000088">int</span> <span style="color:#009900">getAge</span>() {
<span style="color:#000088">return</span> age;
}
<span style="color:#000088">public</span> <span style="color:#000088">void</span> <span style="color:#009900">setAge</span>(<span style="color:#000088">int</span> age) {
<span style="color:#000088">this</span>.age = age;
}
}
<span style="color:#000088">public</span> <span style="color:#000088">class</span> Array2 {
<span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">void</span> <span style="color:#009900">main</span>(String[] args) {
Person p = <span style="color:#000088">new</span> Person();
p.setAge(<span style="color:#006666">20</span>);
Person[] a = <span style="color:#000088">new</span> Person[]{p};
Person[] b = Arrays.copyOf(a, a.length);
a[<span style="color:#006666">0</span>].setAge(<span style="color:#006666">30</span>);
System.<span style="color:#000088">out</span>.println(<span style="color:#009900">"a的年龄:"</span> + a[<span style="color:#006666">0</span>].getAge());
System.<span style="color:#000088">out</span>.println(<span style="color:#009900">"b的年龄:"</span> + b[<span style="color:#006666">0</span>].getAge());
}
}</code></span>
输出:
a的年龄:30
b的年龄:30
可以看见,a改变其值后,连b的值都改变了,这就是浅拷贝问题