这篇文章记录了一下Java知识点,后续会持续增加!~~~
一、位运算中的与运算
package com;
/**
* @author tzp
* @created 2020年6月17日 下午7:28:52
* @类描述:位运算中与运算(&),运用
*/
public class ANDOperation {
public static void main(String[] args) {
//判断奇偶性
judgeOdevity(12);
}
/**
* @author tzp
* @created 2020年6月17日 下午7:43:31
* @param param
* @return
* @方法描述:判断奇偶性,偶数的二进制最低位为0,奇数的二进制最低位为1,判断过程如:a = 6 ---> 110, 1 ---> 001, 110 & 001 = 000 --->偶数
*/
public static void judgeOdevity(int a) {
if((a & 1) == 1) {
System.out.println("传递数据为奇数!");
}
if((a & 1) == 0) {
System.out.println("传递数据为偶数!");
}
}
}
二、位运算中的异或运算
package com;
/**
* @author tzp
* @created 2020年6月17日 下午3:10:47
* @类描述:位运算中的异或运算(^),运用
*/
public class XOROperation {
public static void main(String[] args) {
//异或(^)实现数据交换
swapData(10, 20);
}
/**
* @author tzp
* @created 2020年6月17日 下午3:13:26
* @param param
* @return
* @方法描述:交换两个变量的数值,不需要中间变量,异或运算
*/
public static void swapData(int a, int b) {
System.out.println("交换前数据:" + a + ":" + b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("交换后数据:" + a + ":" + b);
}
}
三、ArrayList、Vector元素可为空且可重复
package com;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
* @author tzp
* @created 2020年6月15日 下午5:28:01
* @类描述:ArrayList、Vector元素可为空且可重复
*/
public class ArrayListAndVectorCanNull {
public static void main(String[] args) {
List<String> strs = new ArrayList<>();
strs.add(null);
strs.add(null);
System.out.println(strs);
Vector<String> v = new Vector<>();
v.add(null);
v.add(null);
System.out.println(v);
}
}
四、数组有最大长度限制?(不考虑空间大小)
package com;
/**
* @author tzp
* @created 2020年6月16日 下午2:48:33
* @类描述:数组有最大长度限制?
*/
public class ArrayMaxLenth {
/**
* @author tzp
* @created 2020年6月16日 下午7:59:32
* @param param
* @return
* @方法描述:可以看到数组的长度是有限制的,姑且认为大小不得超过Integer.MAX_VALUE,也就是2的16次幂。
*/
public static void main(String[] args) {
// int[] len = new int[Integer.MAX_VALUE-1];//java.lang.OutOfMemoryError: Requested array size exceeds VM limit
int[] len = new int[Integer.MAX_VALUE-2];//java.lang.OutOfMemoryError: Java heap space
System.out.println("数组长度:"+len.length);
}
}
五、遍历集合元素时的fail-fast
package com;
import java.util.ArrayList;
import java.util.List;
/**
* @author tzp
* @created 2020年6月15日 下午5:29:14
* @类描述:遍历过程中如果删除元素报ConcurrentModificationException错误-fail-fast
*/
public class FailFast {
public static void main(String[] args) {
List<String> strs = new ArrayList<>();
strs.add("a");
strs.add("b");
strs.add("c");
for(String str : strs) {
System.out.println(str);
strs.remove("b");
}
}
}
六、查看CPU的数量
package com;
/**
* @author tzp
* @created 2020年6月24日 上午9:23:10
* @类描述:查看CPU的数量
*/
public class CPUNumber {
public static void main(String[] args) {
System.out.println("CPU数量:"+Runtime.getRuntime().availableProcessors());
}
}
七、排序之插入排序
package com.sort;
/**
* @author tzp
* @created 2020年6月22日 上午9:12:00
* @类描述:插入排序
*/
public class InsertSort {
public static void main(String[] args) {
int[] arr = { 8, 3, 6, 9, 2, 1, 3 };
insertSort(arr);
print(arr);
}
/**
* @author tzp
* @created 2020年6月22日 上午10:30:59
* @param param
* @return
* @方法描述:主要逻辑:将元素插入到前面已经排好序的数组中
*/
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int tmp = arr[i];
int j = i - 1;
for (; j >= 0 && arr[j] > tmp; j--) {
arr[j + 1] = arr[j];
}
arr[++j] = tmp;
}
}
private static void print(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
}
}
八、排序之希尔排序
package com.sort;
/**
* @author tzp
* @created 2020年6月22日 上午10:38:41 @类描述:希尔排序(增量排序)
*/
public class ShellSort {
public static void main(String[] args) {
int[] arr = { 8, 3, 6, 9, 2, 1, 3 };
shellSort(arr);
print(arr);
}
/**
* @author tzp
* @created 2020年6月22日 上午10:39:07
* @param param
* @return
* @方法描述:主要逻辑:在插入排序的基础上,添加了一个增量
*/
public static void shellSort(int[] arr) {
for (int grp = arr.length / 2; grp > 0; grp /= 2) {// 遍历增量
for (int i = grp; i < arr.length; i++) {
int tmp = arr[i];
int j = i - grp;
for (; j >= 0 && arr[j] > tmp; j -= grp) {
arr[j + grp] = arr[j];
}
arr[j + grp] = tmp;
}
}
}
private static void print(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
}
}
九、排序之冒泡排序
package com.sort;
/**
* @author tzp
* @created 2020年6月22日 上午11:22:52
* @类描述:冒泡排序
*/
public class BubbleSort {
public static void main(String[] args) {
int[] arr = { 8, 3, 6, 9, 2, 1, 3 };
bubbleSort(arr);
print(arr);
}
/**
* @author tzp
* @created 2020年6月22日 上午11:23:08
* @param param
* @return
* @方法描述:主要逻辑:每趟遍历选出最大的元素移到一边
*/
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {// n-1趟
for (int j = i + 1; j < arr.length; j++) {
if(arr[i]>arr[j]) {//如果arr[i]大于arr[j]交换两个值(通过位运算的异或运算实现,不需要额外的临时变量)
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}
}
}
private static void print(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
}
}
十、排序之堆排序
package com.sort;
/**
* @author tzp
* @created 2020年6月22日 下午2:59:01
* @类描述:堆排序
*/
public class HeapSort {
public static void main(String[] args) {
int[] arr = { 8, 3, 6, 9, 2, 1, 3 };
heapSort(arr);
print(arr);
}
/**
* @author tzp
* @created 2020年6月22日 下午3:05:42
* @param param
* @return
* @方法描述:主要逻辑:二叉堆实现
*/
public static void heapSort(int[] arr) {
for(int i = arr.length / 2 -1; i >= 0; i--)
percDown(arr, i, arr.length);
for(int i = arr.length - 1; i > 0; i--) {
swapReferences(arr, 0, i);
percDown(arr, 0, i);
}
}
/**
* @author tzp
* @created 2020年6月22日 下午4:20:40
* @param param
* @return
* @方法描述:下滤
*/
public static void percDown(int[] arr, int i, int n) {
int child = 0;
int tmp = arr[i];
for(;leftChild(i) < n; i = child) {
child = leftChild(i);
if(child != n - 1 && arr[child] < arr[child+1])
child ++;
if(tmp < arr[child]) {
arr[i] = arr[child];
}else {
break;
}
}
arr[i] = tmp;
}
/**
* @author tzp
* @created 2020年6月22日 下午4:20:02
* @param param
* @return
* @方法描述:获取左子树,二叉堆的结构性质
*/
public static int leftChild(int i) {
return 2 * i + 1;
}
/**
* @author tzp
* @created 2020年6月22日 下午4:19:27
* @param param
* @return
* @方法描述:用位运算交换两个元素
*/
public static void swapReferences(int[] arr, int i, int n) {
arr[i] = arr[i] ^ arr[n];
arr[n] = arr[i] ^ arr[n];
arr[i] = arr[i] ^ arr[n];
}
private static void print(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
}
}
十一、排序之归并排序
package com.sort;
/**
* @author tzp
* @created 2020年6月22日 下午4:23:04
* @类描述:归并排序
*/
public class MergeSort {
public static void main(String[] args) {
int[] arr = { 8, 3, 6, 9, 2, 1, 3 };
mergeSort(arr);
print(arr);
}
/**
* @author tzp
* @created 2020年6月22日 下午4:24:20
* @param param
* @return
* @方法描述:主要逻辑:将两个已经排好序的数组进行合并
*/
public static void mergeSort(int[] arr) {
int[] tmparr = new int[arr.length];
mergeSort(arr, tmparr, 0, arr.length - 1);
}
/**
* @author tzp
* @created 2020年6月22日 下午5:24:04
* @param param
* @return
* @方法描述:递归的将两个已经排好序的数组合并
*/
public static void mergeSort(int[] arr, int[] tmparr, int left, int right) {
if (left < right) {
int center = (left + right) / 2;
mergeSort(arr, tmparr, left, center);
mergeSort(arr, tmparr, center + 1, right);
merge(arr, tmparr, left, center + 1, right);
}
}
/**
* @author tzp
* @created 2020年6月22日 下午5:25:34
* @param param
* @return
* @方法描述:合并两个已经排好序的数组
*/
public static void merge(int[] arr, int[] tmparr, int leftpos, int rightpos, int rightend) {
int leftend = rightpos - 1;
int tmppos = leftpos;
int numele = rightend - leftpos + 1;
while (leftpos <= leftend && rightpos <= rightend)
if (arr[leftpos] <= arr[rightpos])
tmparr[tmppos++] = arr[leftpos++];
else
tmparr[tmppos++] = arr[rightpos++];
while (leftpos <= leftend)
tmparr[tmppos++] = arr[leftpos++];
while (rightpos <= rightend)
tmparr[tmppos++] = arr[rightpos++];
for (int i = 0; i < numele; i++, rightend--)
arr[rightend] = tmparr[rightend];
}
private static void print(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
}
}
十二、排序之快速排序
package com.sort;
/**
* @author tzp
* @created 2020年6月22日 下午9:09:32
* @类描述:快速排序
*/
public class QuickSort {
public static void main(String[] args) {
int[] arr = { 8, 3, 6, 9, 2, 1, 3 };
quickSort(arr);
print(arr);
}
/**
* @author tzp
* @created 2020年6月22日 下午9:10:27
* @param param
* @return
* @方法描述:主要逻辑:找到枢纽元(可以理解为中值)把小于枢纽元的元素集合、枢纽元、大于枢纽元的元素集合进行组装
*/
public static void quickSort(int[] arr) {
quickSort(arr, 0, arr.length - 1);
}
/**
* @author tzp
* @created 2020年6月22日 下午9:15:33
* @param param
* @return
* @方法描述:递归的组合
*/
public static void quickSort(int[] arr, int left, int right) {
if (left + 3 <= right) {
int pivot = median(arr, left, right);
int i = left, j = right - 1;
for (;;) {
while (arr[++i] < pivot) {
}
while (arr[--j] > pivot) {
}
if (i < j)
swapReferences(arr, i, j);
else
break;
}
swapReferences(arr, i, right - 1);
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
} else
insertSort(arr, left, right);
}
/**
* @author tzp
* @created 2020年6月22日 下午9:15:53
* @param param
* @return
* @方法描述:数组元素小于一定数量时,直接使用插入排序
*/
public static void insertSort(int[] arr, int left, int right) {
for (int p = left + 1; p <= right; p++) {
int tmp = arr[p];
int j;
for (j = p; j > left && tmp < arr[j - 1]; j--)
arr[j] = arr[j - 1];
arr[j] = tmp;
}
}
/**
* @author tzp
* @created 2020年6月22日 下午9:16:42
* @param param
* @return
* @方法描述:获取枢纽元(中值)
*/
public static int median(int[] arr, int left, int right) {
int center = (left + right) / 2;
if (arr[center] < arr[left])
swapReferences(arr, left, center);
if (arr[right] < arr[left])
swapReferences(arr, left, right);
if (arr[right] < arr[center])
swapReferences(arr, center, right);
swapReferences(arr, center, right - 1);
return arr[right - 1];
}
/**
* @author tzp
* @created 2020年6月22日 下午4:19:27
* @param param
* @return
* @方法描述:用位运算交换两个元素
*/
public static void swapReferences(int[] arr, int i, int n) {
int tmp = arr[i];
arr[i] = arr[n];
arr[n] = tmp;
}
private static void print(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
}
}
持续中~~~