2022-11-27 20:05:28 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: array.js
|
|
|
|
|
* Created Time: 2022-11-27
|
|
|
|
|
* Author: IsChristina (christinaxia77@foxmail.com)
|
|
|
|
|
*/
|
2022-11-27 18:33:53 +08:00
|
|
|
|
|
2023-08-17 05:13:19 +08:00
|
|
|
|
/* 随机访问元素 */
|
2022-12-12 21:13:12 +08:00
|
|
|
|
function randomAccess(nums) {
|
2022-11-27 18:33:53 +08:00
|
|
|
|
// 在区间 [0, nums.length) 中随机抽取一个数字
|
2022-12-12 21:05:36 +08:00
|
|
|
|
const random_index = Math.floor(Math.random() * nums.length);
|
2022-11-27 18:33:53 +08:00
|
|
|
|
// 获取并返回随机元素
|
2022-12-12 21:13:12 +08:00
|
|
|
|
const random_num = nums[random_index];
|
2022-12-12 21:05:36 +08:00
|
|
|
|
return random_num;
|
2022-11-27 18:33:53 +08:00
|
|
|
|
}
|
2022-11-27 19:52:48 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 扩展数组长度 */
|
2022-12-12 21:33:30 +08:00
|
|
|
|
// 请注意,JavaScript 的 Array 是动态数组,可以直接扩展
|
|
|
|
|
// 为了方便学习,本函数将 Array 看作是长度不可变的数组
|
2022-12-12 21:13:12 +08:00
|
|
|
|
function extend(nums, enlarge) {
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
const res = new Array(nums.length + enlarge).fill(0);
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2022-12-12 21:05:36 +08:00
|
|
|
|
res[i] = nums[i];
|
2022-12-12 21:13:12 +08:00
|
|
|
|
}
|
2022-11-27 18:33:53 +08:00
|
|
|
|
// 返回扩展后的新数组
|
2022-12-12 21:05:36 +08:00
|
|
|
|
return res;
|
2022-11-27 18:33:53 +08:00
|
|
|
|
}
|
2022-11-27 19:52:48 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2022-12-12 21:13:12 +08:00
|
|
|
|
function insert(nums, num, index) {
|
2022-11-27 18:33:53 +08:00
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 13:20:20 +08:00
|
|
|
|
for (let i = nums.length - 1; i > index; i--) {
|
2022-11-27 18:33:53 +08:00
|
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 删除索引 index 处元素 */
|
2022-12-12 21:13:12 +08:00
|
|
|
|
function remove(nums, index) {
|
2022-11-27 18:33:53 +08:00
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for (let i = index; i < nums.length - 1; i++) {
|
2022-12-12 21:05:36 +08:00
|
|
|
|
nums[i] = nums[i + 1];
|
2022-12-12 21:13:12 +08:00
|
|
|
|
}
|
2022-11-27 18:33:53 +08:00
|
|
|
|
}
|
2022-12-12 21:13:12 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 遍历数组 */
|
2022-12-12 21:13:12 +08:00
|
|
|
|
function traverse(nums) {
|
2022-12-12 21:05:36 +08:00
|
|
|
|
let count = 0;
|
2022-11-27 18:33:53 +08:00
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += nums[i];
|
2022-11-27 18:33:53 +08:00
|
|
|
|
}
|
2023-11-14 21:27:35 +08:00
|
|
|
|
// 直接遍历数组元素
|
2023-08-17 05:00:35 +08:00
|
|
|
|
for (const num of nums) {
|
2023-11-14 21:27:35 +08:00
|
|
|
|
count += num;
|
2022-12-12 21:13:12 +08:00
|
|
|
|
}
|
2022-11-27 18:33:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 在数组中查找指定元素 */
|
2022-12-12 21:13:12 +08:00
|
|
|
|
function find(nums, target) {
|
2022-11-27 18:33:53 +08:00
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2023-08-03 14:44:49 +08:00
|
|
|
|
if (nums[i] === target) return i;
|
2022-11-27 18:33:53 +08:00
|
|
|
|
}
|
2022-12-12 21:05:36 +08:00
|
|
|
|
return -1;
|
2022-11-27 18:33:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 17:39:56 +08:00
|
|
|
|
/* Driver Code */
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 初始化数组 */
|
2022-12-13 00:06:18 +08:00
|
|
|
|
const arr = new Array(5).fill(0);
|
2023-04-17 21:58:11 +08:00
|
|
|
|
console.log('数组 arr =', arr);
|
2022-12-13 00:10:29 +08:00
|
|
|
|
let nums = [1, 3, 2, 5, 4];
|
2023-04-17 21:58:11 +08:00
|
|
|
|
console.log('数组 nums =', nums);
|
2022-11-27 18:33:53 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 随机访问 */
|
2022-12-13 00:10:29 +08:00
|
|
|
|
let random_num = randomAccess(nums);
|
2023-04-17 21:58:11 +08:00
|
|
|
|
console.log('在 nums 中获取随机元素', random_num);
|
2022-11-27 18:33:53 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 长度扩展 */
|
2022-12-12 21:05:36 +08:00
|
|
|
|
nums = extend(nums, 3);
|
2023-04-17 21:58:11 +08:00
|
|
|
|
console.log('将数组长度扩展至 8 ,得到 nums =', nums);
|
2022-11-27 18:33:53 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 插入元素 */
|
2022-12-12 21:05:36 +08:00
|
|
|
|
insert(nums, 6, 3);
|
2023-04-17 21:58:11 +08:00
|
|
|
|
console.log('在索引 3 处插入数字 6 ,得到 nums =', nums);
|
2022-11-27 18:33:53 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 删除元素 */
|
2022-12-12 21:05:36 +08:00
|
|
|
|
remove(nums, 2);
|
2023-04-17 21:58:11 +08:00
|
|
|
|
console.log('删除索引 2 处的元素,得到 nums =', nums);
|
2022-11-27 18:33:53 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 遍历数组 */
|
2022-12-12 21:05:36 +08:00
|
|
|
|
traverse(nums);
|
2022-11-27 18:33:53 +08:00
|
|
|
|
|
2022-11-27 20:05:28 +08:00
|
|
|
|
/* 查找元素 */
|
2022-12-13 00:10:29 +08:00
|
|
|
|
let index = find(nums, 3);
|
2023-04-17 21:58:11 +08:00
|
|
|
|
console.log('在 nums 中查找元素 3 ,得到索引 =', index);
|