2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-11-25 02:04:38 +08:00
|
|
|
* File: array.java
|
|
|
|
* Created Time: 2022-11-25
|
2024-02-07 22:21:18 +08:00
|
|
|
* Author: krahets (krahets@163.com)
|
2022-11-25 02:04:38 +08:00
|
|
|
*/
|
|
|
|
|
2022-11-08 02:58:42 +08:00
|
|
|
package chapter_array_and_linkedlist;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
|
|
public class array {
|
2023-08-17 05:13:19 +08:00
|
|
|
/* 随机访问元素 */
|
2022-11-08 02:58:42 +08:00
|
|
|
static int randomAccess(int[] nums) {
|
|
|
|
// 在区间 [0, nums.length) 中随机抽取一个数字
|
2023-04-14 00:12:10 +08:00
|
|
|
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
|
2022-11-08 02:58:42 +08:00
|
|
|
// 获取并返回随机元素
|
|
|
|
int randomNum = nums[randomIndex];
|
|
|
|
return randomNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
static int[] extend(int[] nums, int enlarge) {
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
int[] res = new int[nums.length + enlarge];
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
for (int i = 0; i < nums.length; i++) {
|
|
|
|
res[i] = nums[i];
|
|
|
|
}
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
|
|
|
static void insert(int[] nums, int num, int index) {
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 13:20:20 +08:00
|
|
|
for (int i = nums.length - 1; i > index; i--) {
|
2022-11-08 02:58:42 +08:00
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
}
|
2023-12-02 06:21:34 +08:00
|
|
|
// 将 num 赋给 index 处的元素
|
2022-11-08 02:58:42 +08:00
|
|
|
nums[index] = num;
|
|
|
|
}
|
|
|
|
|
2023-12-02 06:21:34 +08:00
|
|
|
/* 删除索引 index 处的元素 */
|
2022-11-08 02:58:42 +08:00
|
|
|
static void remove(int[] nums, int index) {
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
for (int i = index; i < nums.length - 1; i++) {
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 遍历数组 */
|
|
|
|
static void traverse(int[] nums) {
|
|
|
|
int count = 0;
|
|
|
|
// 通过索引遍历数组
|
|
|
|
for (int i = 0; i < nums.length; i++) {
|
2023-11-14 21:27:35 +08:00
|
|
|
count += nums[i];
|
2022-11-08 02:58:42 +08:00
|
|
|
}
|
2023-11-14 21:27:35 +08:00
|
|
|
// 直接遍历数组元素
|
2022-11-08 02:58:42 +08:00
|
|
|
for (int num : nums) {
|
2023-11-14 21:27:35 +08:00
|
|
|
count += num;
|
2022-11-08 02:58:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 在数组中查找指定元素 */
|
|
|
|
static int find(int[] nums, int target) {
|
|
|
|
for (int i = 0; i < nums.length; i++) {
|
|
|
|
if (nums[i] == target)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
public static void main(String[] args) {
|
|
|
|
/* 初始化数组 */
|
|
|
|
int[] arr = new int[5];
|
|
|
|
System.out.println("数组 arr = " + Arrays.toString(arr));
|
|
|
|
int[] nums = { 1, 3, 2, 5, 4 };
|
|
|
|
System.out.println("数组 nums = " + Arrays.toString(nums));
|
2023-04-14 00:12:10 +08:00
|
|
|
|
2022-11-08 02:58:42 +08:00
|
|
|
/* 随机访问 */
|
|
|
|
int randomNum = randomAccess(nums);
|
|
|
|
System.out.println("在 nums 中获取随机元素 " + randomNum);
|
2023-04-14 00:12:10 +08:00
|
|
|
|
2022-11-08 02:58:42 +08:00
|
|
|
/* 长度扩展 */
|
|
|
|
nums = extend(nums, 3);
|
|
|
|
System.out.println("将数组长度扩展至 8 ,得到 nums = " + Arrays.toString(nums));
|
2023-04-14 00:12:10 +08:00
|
|
|
|
2022-11-08 02:58:42 +08:00
|
|
|
/* 插入元素 */
|
|
|
|
insert(nums, 6, 3);
|
|
|
|
System.out.println("在索引 3 处插入数字 6 ,得到 nums = " + Arrays.toString(nums));
|
|
|
|
|
|
|
|
/* 删除元素 */
|
|
|
|
remove(nums, 2);
|
|
|
|
System.out.println("删除索引 2 处的元素,得到 nums = " + Arrays.toString(nums));
|
2023-04-14 00:12:10 +08:00
|
|
|
|
2022-11-08 02:58:42 +08:00
|
|
|
/* 遍历数组 */
|
|
|
|
traverse(nums);
|
2023-04-14 00:12:10 +08:00
|
|
|
|
2022-11-08 02:58:42 +08:00
|
|
|
/* 查找元素 */
|
|
|
|
int index = find(nums, 3);
|
|
|
|
System.out.println("在 nums 中查找元素 3 ,得到索引 = " + index);
|
|
|
|
}
|
|
|
|
}
|