8000 Update ArrayList源码+扩容机制分析.md · tinytang/JavaGuide@86b2ddf · GitHub
[go: up one dir, main page]

Skip to content

Commit 86b2ddf

Browse files
authored
Update ArrayList源码+扩容机制分析.md
对 ArrayList 中的 System.arraycopy() 和 Arrays.copyOf() 添加说明
1 parent 8b1ffa7 commit 86b2ddf

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/java/collection/ArrayList源码+扩容机制分析.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,24 @@ public class ArrayList<E> extends AbstractList<E>
733733

734734
#### 3.3.1. `System.arraycopy()` 方法
735735

736+
源码:
737+
738+
```java
739+
// 我们发现 arraycopy 是一个 native 方法,接下来我们解释一下各个参数的具体意义
740+
/**
741+
* 复制数组
742+
* @param src 源数组
743+
* @param srcPos 源数组中的起始位置
744+
* @param dest 目标数组
745+
* @param destPos 目标数组中的起始位置
746+
* @param length 要复制的数组元素的数量
747+
*/
748+
public static native void arraycopy(Object src, int srcPos,
749+
Object dest, int destPos,
750+
int length);
751+
```
752+
753+
场景:
736754
```java
737755
/**
738756
* 在此列表中的指定位置插入指定的元素。
@@ -781,6 +799,21 @@ public class ArraycopyTest {
781799

782800
#### 3.3.2. `Arrays.copyOf()`方法
783801

802+
源码:
803+
804+
```java
805+
public static int[] copyOf(int[] original, int newLength) {
806+
// 申请一个新的数组
807+
int[] copy = new int[newLength];
808+
// 调用System.arraycopy,将源数组中的数据进行拷贝,并返回新的数组
809+
System.arraycopy(original, 0, copy, 0,
810+
Math.min(original.length, newLength));
811+
return copy;
812+
}
813+
```
814+
815+
场景:
816+
784817
```java
785818
/**
786819
以正确的顺序返回一个包含此列表中所有元素的数组(从第一个到最后一个元素); 返回的数组的运行时类型是指定数组的运行时类型。

0 commit comments

Comments
 (0)
0