mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-05 21:19:41 +08: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);
|
||||
|
@ -51,7 +51,7 @@ public class linked_list {
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
/* 初始化链表 */
|
||||
// 初始化各个节点
|
||||
// 初始化各个节点
|
||||
ListNode n0 = new ListNode(1);
|
||||
ListNode n1 = new ListNode(3);
|
||||
ListNode n2 = new ListNode(2);
|
||||
|
@ -10,17 +10,17 @@ import java.util.*;
|
||||
|
||||
/* 列表类简易实现 */
|
||||
class MyList {
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
public MyList() {
|
||||
nums = new int[capacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(即当前元素数量) */
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
@ -118,7 +118,7 @@ public class my_list {
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
System.out.println("列表 list = " + Arrays.toString(list.toArray()) +
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(3, 6);
|
||||
@ -142,6 +142,6 @@ public class my_list {
|
||||
list.add(i);
|
||||
}
|
||||
System.out.println("扩容后的列表 list = " + Arrays.toString(list.toArray()) +
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user