Add Java codes, and license.
This commit is contained in:
		
							
								
								
									
										100
									
								
								codes/java/chapter_array_and_linkedlist/array.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								codes/java/chapter_array_and_linkedlist/array.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,100 @@ | ||||
| package chapter_array_and_linkedlist; | ||||
|  | ||||
| import java.util.*; | ||||
| import java.util.concurrent.ThreadLocalRandom; | ||||
|  | ||||
| public class array { | ||||
|     /* 随机返回一个数组元素 */ | ||||
|     static int randomAccess(int[] nums) { | ||||
|         // 在区间 [0, nums.length) 中随机抽取一个数字 | ||||
|         int randomIndex = ThreadLocalRandom.current(). | ||||
|                           nextInt(0, nums.length); | ||||
|         // 获取并返回随机元素 | ||||
|         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 以及之后的所有元素向后移动一位 | ||||
|         for (int i = nums.length - 1; i >= index; i--) { | ||||
|             nums[i] = nums[i - 1]; | ||||
|         } | ||||
|         // 将 num 赋给 index 处元素 | ||||
|         nums[index] = num; | ||||
|     } | ||||
|  | ||||
|     /* 删除索引 index 处元素 */ | ||||
|     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++) { | ||||
|             count++; | ||||
|         } | ||||
|         // 直接遍历数组 | ||||
|         for (int num : nums) { | ||||
|             count++; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /* 在数组中查找指定元素 */ | ||||
|     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)); | ||||
|          | ||||
|         /* 随机访问 */ | ||||
|         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)); | ||||
|  | ||||
|         /* 删除元素 */ | ||||
|         remove(nums, 2); | ||||
|         System.out.println("删除索引 2 处的元素,得到 nums = " + Arrays.toString(nums)); | ||||
|          | ||||
|         /* 遍历数组 */ | ||||
|         traverse(nums); | ||||
|          | ||||
|         /* 查找元素 */ | ||||
|         int index = find(nums, 3); | ||||
|         System.out.println("在 nums 中查找元素 3 ,得到索引 = " + index); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										80
									
								
								codes/java/chapter_array_and_linkedlist/linked_list.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								codes/java/chapter_array_and_linkedlist/linked_list.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,80 @@ | ||||
| package chapter_array_and_linkedlist; | ||||
|  | ||||
| import include.*; | ||||
|  | ||||
| public class linked_list { | ||||
|     /* 在链表的结点 n0 之后插入结点 P */ | ||||
|     static void insert(ListNode n0, ListNode P) { | ||||
|         ListNode n1 = n0.next; | ||||
|         n0.next = P; | ||||
|         P.next = n1; | ||||
|     } | ||||
|  | ||||
|     /* 删除链表的结点 n0 之后的首个结点 */ | ||||
|     static void remove(ListNode n0) { | ||||
|         if (n0.next == null) | ||||
|             return; | ||||
|         // n0 -> P -> n1 | ||||
|         ListNode P = n0.next; | ||||
|         ListNode n1 = P.next; | ||||
|         n0.next = n1; | ||||
|     } | ||||
|  | ||||
|     /* 访问链表中索引为 index 的结点 */ | ||||
|     static ListNode access(ListNode head, int index) { | ||||
|         for (int i = 0; i < index; i++) { | ||||
|             head = head.next; | ||||
|             if (head == null) | ||||
|                 return null; | ||||
|         } | ||||
|         return head; | ||||
|     } | ||||
|  | ||||
|     /* 在链表中查找值为 target 的首个结点 */ | ||||
|     static int find(ListNode head, int target) { | ||||
|         int index = 0; | ||||
|         while (head != null) { | ||||
|             if (head.val == target) | ||||
|                 return index; | ||||
|             head = head.next; | ||||
|             index++; | ||||
|         } | ||||
|         return -1; | ||||
|     } | ||||
|  | ||||
|     /* Driver Code */ | ||||
|     public static void main(String[] args) { | ||||
|         /* 初始化链表 */ | ||||
|         // 初始化各个结点  | ||||
|         ListNode n0 = new ListNode(1); | ||||
|         ListNode n1 = new ListNode(3); | ||||
|         ListNode n2 = new ListNode(2); | ||||
|         ListNode n3 = new ListNode(5); | ||||
|         ListNode n4 = new ListNode(4); | ||||
|         // 构建引用指向 | ||||
|         n0.next = n1; | ||||
|         n1.next = n2; | ||||
|         n2.next = n3; | ||||
|         n3.next = n4; | ||||
|         System.out.println("初始化的链表为"); | ||||
|         PrintUtil.printLinkedList(n0); | ||||
|  | ||||
|         /* 插入结点 */ | ||||
|         insert(n0, new ListNode(0)); | ||||
|         System.out.println("插入结点后的链表为"); | ||||
|         PrintUtil.printLinkedList(n0); | ||||
|  | ||||
|         /* 删除结点 */ | ||||
|         remove(n0); | ||||
|         System.out.println("删除结点后的链表为"); | ||||
|         PrintUtil.printLinkedList(n0); | ||||
|  | ||||
|         /* 访问结点 */ | ||||
|         ListNode node = access(n0, 3); | ||||
|         System.out.println("链表中索引 3 处的结点的值 = " + node.val); | ||||
|  | ||||
|         /* 查找结点 */ | ||||
|         int index = find(n0, 2); | ||||
|         System.out.println("链表中值为 2 的结点的索引 = " + index); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										62
									
								
								codes/java/chapter_array_and_linkedlist/list.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								codes/java/chapter_array_and_linkedlist/list.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | ||||
| package chapter_array_and_linkedlist; | ||||
|  | ||||
| import java.util.*; | ||||
|  | ||||
| public class list { | ||||
|     public static void main(String[] args) { | ||||
|         /* 初始化列表 */ | ||||
|         // 注意数组的元素类型是 int[] 的包装类 Integer[] | ||||
|         Integer[] numbers = new Integer[] { 1, 3, 2, 5, 4 }; | ||||
|         List<Integer> list = new ArrayList<>(Arrays.asList(numbers)); | ||||
|         System.out.println("列表 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 访问元素 */ | ||||
|         int num = list.get(1); | ||||
|         System.out.println("访问索引 1 处的元素,得到 num = " + num); | ||||
|  | ||||
|         /* 更新元素 */ | ||||
|         list.set(1, 0); | ||||
|         System.out.println("将索引 1 处的元素更新为 0 ,得到 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 清空列表 */ | ||||
|         list.clear(); | ||||
|         System.out.println("清空列表后 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 尾部添加元素 */ | ||||
|         list.add(1); | ||||
|         list.add(3); | ||||
|         list.add(2); | ||||
|         list.add(5); | ||||
|         list.add(4); | ||||
|         System.out.println("添加元素后 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 中间插入元素 */ | ||||
|         list.add(3, 6); | ||||
|         System.out.println("在索引 3 处插入数字 6 ,得到 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 删除元素 */ | ||||
|         list.remove(3); | ||||
|         System.out.println("删除索引 3 处的元素,得到 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 通过索引遍历列表 */ | ||||
|         int count = 0; | ||||
|         for (int i = 0; i < list.size(); i++) { | ||||
|             count++; | ||||
|         } | ||||
|  | ||||
|         /* 直接遍历列表元素 */ | ||||
|         count = 0; | ||||
|         for (int n : list) { | ||||
|             count++; | ||||
|         } | ||||
|  | ||||
|         /* 拼接两个列表 */ | ||||
|         List<Integer> list1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 })); | ||||
|         list.addAll(list1); | ||||
|         System.out.println("将列表 list1 拼接到 list 之后,得到 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 排序列表 */ | ||||
|         Collections.sort(list); | ||||
|         System.out.println("排序列表后 list = " + Arrays.toString(list.toArray())); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										136
									
								
								codes/java/chapter_array_and_linkedlist/my_list.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										136
									
								
								codes/java/chapter_array_and_linkedlist/my_list.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,136 @@ | ||||
| package chapter_array_and_linkedlist; | ||||
|  | ||||
| import java.util.*; | ||||
|  | ||||
| /* 列表类简易实现 */ | ||||
| class MyList { | ||||
|     int[] nums;               // 数组(存储列表元素) | ||||
|     int initialCapacity = 10; // 列表初始容量 | ||||
|     int size = 0;             // 列表长度(即当前元素数量) | ||||
|     int extendRatio = 2;      // 每次列表扩容的倍数 | ||||
|  | ||||
|     /* 构造函数 */ | ||||
|     public MyList() { | ||||
|         nums = new int[initialCapacity]; | ||||
|     } | ||||
|  | ||||
|     /* 获取列表容量 */ | ||||
|     public int size() { | ||||
|         return size; | ||||
|     } | ||||
|  | ||||
|     /* 获取列表长度(即当前元素数量) */ | ||||
|     public int capacity() { | ||||
|         return nums.length; | ||||
|     } | ||||
|  | ||||
|     /* 访问元素 */ | ||||
|     public int get(int index) { | ||||
|         // 索引如果越界则抛出异常,下同 | ||||
|         if (index >= size) | ||||
|             throw new IndexOutOfBoundsException("索引越界"); | ||||
|         return nums[index]; | ||||
|     } | ||||
|  | ||||
|     /* 更新元素 */ | ||||
|     public void set(int index, int num) { | ||||
|         if (index >= size) | ||||
|             throw new IndexOutOfBoundsException("索引越界"); | ||||
|         nums[index] = num; | ||||
|     } | ||||
|  | ||||
|     /* 尾部添加元素 */ | ||||
|     public void add(int num) { | ||||
|         // 元素数量超出容量时,触发扩容机制 | ||||
|         if (size == nums.length) | ||||
|             extendCapacity(); | ||||
|         nums[size] = num; | ||||
|         // 更新元素数量 | ||||
|         size++; | ||||
|     } | ||||
|  | ||||
|     /* 中间插入元素 */ | ||||
|     public void add(int index, int num) { | ||||
|         if (index >= size) | ||||
|             throw new IndexOutOfBoundsException("索引越界"); | ||||
|         // 元素数量超出容量时,触发扩容机制 | ||||
|         if (size == nums.length) | ||||
|             extendCapacity(); | ||||
|         // 索引 i 以及之后的元素都向后移动一位 | ||||
|         for (int j = size - 1; j >= index; j--) { | ||||
|             nums[j + 1] = nums[j]; | ||||
|         } | ||||
|         nums[index] = num; | ||||
|         // 更新元素数量 | ||||
|         size++; | ||||
|     } | ||||
|  | ||||
|     /* 删除元素 */ | ||||
|     public void remove(int index) { | ||||
|         if (index >= size) | ||||
|             throw new IndexOutOfBoundsException("索引越界"); | ||||
|         // 索引 i 之后的元素都向前移动一位 | ||||
|         for (int j = index; j < size - 1; j++) { | ||||
|             nums[j] = nums[j + 1]; | ||||
|         } | ||||
|         // 更新元素数量 | ||||
|         size--; | ||||
|     } | ||||
|  | ||||
|     /* 列表扩容 */ | ||||
|     public void extendCapacity() { | ||||
|         // 新建一个长度为 size 的数组,并将原数组拷贝到新数组 | ||||
|         nums = Arrays.copyOf(nums, nums.length * extendRatio); | ||||
|     } | ||||
|  | ||||
|     /* 将列表转换为数组 */ | ||||
|     public int[] toArray() { | ||||
|         int size = size(); | ||||
|         // 仅转换有效长度范围内的列表元素 | ||||
|         int[] nums = new int[size]; | ||||
|         for (int i = 0; i < size; i++) { | ||||
|             nums[i] = get(i); | ||||
|         } | ||||
|         return nums; | ||||
|     } | ||||
| } | ||||
|  | ||||
| public class my_list { | ||||
|     /* Driver Code */ | ||||
|     public static void main(String[] args) { | ||||
|         /* 初始化列表 */ | ||||
|         MyList list = new MyList(); | ||||
|         /* 尾部添加元素 */ | ||||
|         list.add(1); | ||||
|         list.add(3); | ||||
|         list.add(2); | ||||
|         list.add(5); | ||||
|         list.add(4); | ||||
|         System.out.println("列表 list = " + Arrays.toString(list.toArray()) + | ||||
|                            " ,容量 = " + list.capacity() + " ,长度 = " + list.size()); | ||||
|  | ||||
|         /* 中间插入元素 */ | ||||
|         list.add(3, 6); | ||||
|         System.out.println("在索引 3 处插入数字 6 ,得到 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 删除元素 */ | ||||
|         list.remove(3); | ||||
|         System.out.println("删除索引 3 处的元素,得到 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 访问元素 */ | ||||
|         int num = list.get(1); | ||||
|         System.out.println("访问索引 1 处的元素,得到 num = " + num); | ||||
|  | ||||
|         /* 更新元素 */ | ||||
|         list.set(1, 0); | ||||
|         System.out.println("将索引 1 处的元素更新为 0 ,得到 list = " + Arrays.toString(list.toArray())); | ||||
|  | ||||
|         /* 测试扩容机制 */ | ||||
|         for (int i = 0; i < 10; i++) { | ||||
|             // 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制 | ||||
|             list.add(i); | ||||
|         } | ||||
|         System.out.println("扩容后的列表 list = " + Arrays.toString(list.toArray()) + | ||||
|                            " ,容量 = " + list.capacity() + " ,长度 = " + list.size()); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user