java使用技巧积累


java使用技巧积累

休眠固定时间

1
Thread.sleep((1));

java生成文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static void generate_141M() throws IOException {
String fileName = "D:\\s_name_141.dat";
System.out.println("start: 开始生成141M文件->" + fileName);
BufferedWriter bfw = new BufferedWriter(new FileWriter(fileName));
int num = 0;
while (num < 8221592) {
bfw.write("zhangsan beijing");
bfw.newLine();
num++;
if (num % 10000 == 0) {
bfw.flush();
}
}
}

字符串变整数

1
2
Integer.parseInt("2")
生成long型的Long.parseLong("2")

制表符

1
/t

for循环变量范围

1
2
3
4
5
6
7
for(int i=0;i<xxx;i++){
xx
xx
xx
}

这里用i会报错
1
2
3
4
5
6
7
8
int i;
for(i=0;i<xxx;i++){
xx
xx
xx
}

这里用i不会报错

main方法里使用其它方法

1
mian方法是静态方法,它里面引用的方法也要是静态方法

&和&&

1
2
1.都是逻辑运算符,两边都为true时是true。但&&是短路运算,左边是false时则不再计算右边
2.&可当位运算符

for循环

1
2
int high=arr.length-1;
for(;arr[high]>=temp&low<high; high--);

排序算法

交换排序

冒泡排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void BubbleSort(int[] arr){
int temp;
boolean flag;
for(int i=0;i<arr.length;i++){
flag=false;
for(int j=0;j< arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
flag=true;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
if(!flag){
break;
}
}
for(int a:arr){
System.out.println(a);
}
}

快速排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
快速排序
*/
public static void quickSort(int[] arr, int low, int high) {
if(low<high){
int mid = partition(arr, low, high);
quickSort(arr,low,mid-1);
quickSort(arr,mid+1,high);
}
}

public static int partition(int[] arr, int low, int high){
int temp=arr[low];
while(low<high){
for(;arr[high]>=temp&low<high; high--);
arr[low]=arr[high];
for(;arr[low]<=temp&low<high;low++);
arr[high]=arr[low];
}
arr[low]=temp;
return low;
}

插入排序

直接插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void insertSort(int[] arr){//
int temp;
for(int i=1;i<arr.length;i++){
if(arr[i]<arr[i-1]) {
temp = arr[i];
int j;//
for (j = i - 1; j >= 0; j--) {
if (arr[j] > temp) {
arr[j + 1] = arr[j];
} else {
break;
}
}
arr[j+1] = temp;
}
}
for(int a:arr){
System.out.println(a);
}
}

折半插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static void insertSort1(int[] arr){//
int temp;
for(int i=1;i<arr.length;i++){
if(arr[i]<arr[i-1]) {
temp = arr[i];
int low=0;
int high=i-1;
int mid;
while(low<=high){
mid = (low + high)/2;
if(arr[mid]>temp){
high=mid-1;
}else{
low=mid+1;
}
}
for(int j=i-1;j>=low;j--){
arr[j+1]=arr[j];
}
arr[low]=temp;
}
}
for(int a:arr){
System.out.println(a);
}
}

希尔排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
希尔排序
*/
public static void shellSort(int[] arr){
int temp,j;
for(int d=arr.length/2;d>=1;d=d/2){
for(int i=d;i<arr.length;i++){
if(arr[i]<arr[i-d]){
temp=arr[i];
for(j=i-d;j>=0;j=j-d){
if(arr[j]>temp){
arr[j+d]=arr[j];
}else{
break;
}
}
arr[j+d]=temp;
}
}
}
for(int m:arr)
System.out.println(m);
}

选择排序

简单选择排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
~~~



## 查找算法

### 折半查找

~~~java
public static int BinarySearch(int[] arr,int des){
int low=0,high=arr.length-1,mid;
while(low<=high){
mid=(low+high)/2;
if(arr[mid]==des){
return mid;
}else if(arr[mid]>des){
high=mid-1;
}else{
low=mid+1;
}
}
return -1;
}

分块查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
分块查找
{5,9,45}
{0,5,10}
{2,1,5,4,3/,7,9,6,8,7/,10,17,15,45,21}
*/
public static int splitBlockSearch(int[] maxValueArr,int[] indexArr,int[] arr,int des){
int low=0,high=maxValueArr.length-1,mid,index;
while(low<=high){
mid = (low + high) / 2;
if(maxValueArr[mid]==des){
index=mid;
break;
}else if(maxValueArr[mid]<des){
low=mid+1;
}else{
high=mid-1;
}
}
if(low<maxValueArr.length){
index=low;
}else{
System.out.println("队列里无目标值(low超过索引表最大下标)");
return -1;
}
index=indexArr[index];
for(int j=index;j<index+5;j++){
if(arr[j]==des){
return j;
}
}
System.out.println("队列里无目标值(low未超过索引表最大下标)");
return -1;
}

本文标题:java使用技巧积累

文章作者:TTYONG

发布时间:2022年02月14日 - 21:02

最后更新:2023年05月17日 - 16:05

原始链接:http://tianyong.fun/java%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7%E7%A7%AF%E7%B4%AF.html

许可协议: 转载请保留原文链接及作者。

多少都是爱
0%