mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 08:34:28 +00:00
Format the Java codes with the Reat Hat extension.
This commit is contained in:
@@ -13,8 +13,7 @@ public class array {
|
||||
/* 随机返回一个数组元素 */
|
||||
static int randomAccess(int[] nums) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
int randomIndex = ThreadLocalRandom.current().
|
||||
nextInt(0, nums.length);
|
||||
int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);
|
||||
// 获取并返回随机元素
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
@@ -79,15 +78,15 @@ public class array {
|
||||
System.out.println("数组 arr = " + Arrays.toString(arr));
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
System.out.println("数组 nums = " + Arrays.toString(nums));
|
||||
|
||||
|
||||
/* 随机访问 */
|
||||
int randomNum = randomAccess(nums);
|
||||
System.out.println("在 nums 中获取随机元素 " + randomNum);
|
||||
|
||||
|
||||
/* 长度扩展 */
|
||||
nums = extend(nums, 3);
|
||||
System.out.println("将数组长度扩展至 8 ,得到 nums = " + Arrays.toString(nums));
|
||||
|
||||
|
||||
/* 插入元素 */
|
||||
insert(nums, 6, 3);
|
||||
System.out.println("在索引 3 处插入数字 6 ,得到 nums = " + Arrays.toString(nums));
|
||||
@@ -95,10 +94,10 @@ public class array {
|
||||
/* 删除元素 */
|
||||
remove(nums, 2);
|
||||
System.out.println("删除索引 2 处的元素,得到 nums = " + Arrays.toString(nums));
|
||||
|
||||
|
||||
/* 遍历数组 */
|
||||
traverse(nums);
|
||||
|
||||
|
||||
/* 查找元素 */
|
||||
int index = find(nums, 3);
|
||||
System.out.println("在 nums 中查找元素 3 ,得到索引 = " + index);
|
||||
|
||||
Reference in New Issue
Block a user