This commit is contained in:
krahets
2023-02-07 17:17:14 +08:00
parent ecbf2d1560
commit 1ca5c731f7
19 changed files with 501 additions and 192 deletions

View File

@@ -122,6 +122,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
// 在区间 [0, nums.length) 中随机抽取一个数字
int randomIndex = ThreadLocalRandom.current().
nextInt(0, nums.length);
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;
}
@@ -417,7 +418,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
void remove(int[] nums, int index) {
// 把索引 index 之后的所有元素向前移动一位

View File

@@ -331,7 +331,7 @@ comments: true
n0.next = P;
P.next = n1;
}
/* 删除链表的结点 n0 之后的首个结点 */
void remove(ListNode n0) {
if (n0.next == null)

View File

@@ -801,6 +801,17 @@ comments: true
// 更新列表容量
capacity = nums.length;
}
/* 将列表转换为数组 */
public int[] toArray() {
int size = size();
// 仅转换有效长度范围内的列表元素
int[] nums = new int[size];
for (int i = 0; i < size; i++) {
nums[i] = get(i);
}
return nums;
}
}
```