Skip to content

Commit d69973d

Browse files
committed
code: add java arrays demo
1 parent 78c287c commit d69973d

2 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.wdbyte.collection;
2+
3+
import java.util.EnumMap;
4+
5+
/**
6+
* @author niulang
7+
* @date 2023/10/20
8+
*/
9+
public class EnumMapTest {
10+
public static void main(String[] args) {
11+
EnumMap<Week, String> enumMap = new EnumMap(Week.class);
12+
enumMap.put(Week.a,null);
13+
System.out.println(enumMap.get(Week.a));
14+
}
15+
}
16+
17+
enum Week {
18+
a,
19+
b,
20+
c,
21+
d,
22+
e,
23+
f,
24+
g
25+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.wdbyte.collection;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Random;
6+
import java.util.function.IntFunction;
7+
import java.util.function.ToIntFunction;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
/**
12+
* @author niulang
13+
* @date 2024/03/04
14+
*/
15+
public class JavaArrays {
16+
17+
@Test
18+
public void print() {
19+
String[] arr = new String[] {"a", "b", "c", "d"};
20+
System.out.println(Arrays.toString(arr));
21+
}
22+
23+
@Test
24+
public void init() {
25+
String[] arr = new String[] {"a", "b", "c", "d"};
26+
String[] copyOf2 = Arrays.copyOf(arr, 2);
27+
String[] copyOfRange = Arrays.copyOfRange(arr, 1, 3);
28+
29+
System.out.println(Arrays.toString(arr));
30+
System.out.println(Arrays.toString(copyOf2));
31+
System.out.println(Arrays.toString(copyOfRange));
32+
33+
// 目标大小大于原始大小,则copyOf 会用null填充数组 。
34+
String[] copyOf10 = Arrays.copyOf(arr, 10);
35+
System.out.println(Arrays.toString(copyOf10));
36+
}
37+
38+
@Test
39+
public void fill() {
40+
String[] arr = new String[5];
41+
Arrays.fill(arr, "java");
42+
System.out.println(Arrays.toString(arr));
43+
44+
// 生成 100以内的 随机数
45+
IntFunction<Integer> intFunction = i -> new Random().nextInt(100);
46+
Integer[] intArr = new Integer[5];
47+
Arrays.setAll(intArr, intFunction);
48+
System.out.println(Arrays.toString(intArr));
49+
}
50+
51+
@Test
52+
public void diff() {
53+
String[] arr = new String[] {"a", "b", "c", "d"};
54+
Object[] arr1 = new Object[] {arr, new String[] {"a", "b", "c", "d"}};
55+
Object[] arr2 = new Object[] {arr, arr};
56+
57+
System.out.println(Arrays.equals(arr1, arr2));
58+
System.out.println(Arrays.deepEquals(arr1, arr2));
59+
60+
// hashCode
61+
System.out.println(Arrays.hashCode(arr2));
62+
System.out.println(Arrays.deepHashCode(arr2));
63+
64+
arr[0] = null;
65+
System.out.println(Arrays.hashCode(arr2));
66+
System.out.println(Arrays.deepHashCode(arr2));
67+
}
68+
69+
@Test
70+
public void sort() {
71+
// 生成 100以内的 随机数
72+
IntFunction<Integer> intFunction = i -> new Random().nextInt(100);
73+
Integer[] intArr = new Integer[5];
74+
Arrays.setAll(intArr, intFunction);
75+
System.out.println(Arrays.toString(intArr));
76+
77+
Arrays.sort(intArr);
78+
System.out.println(Arrays.toString(intArr));
79+
80+
//long cost = 0;
81+
//for (int ii = 0; ii < 10; ii++) {
82+
// IntFunction<Integer> intFunction = i -> new Random().nextInt(100000);
83+
// Integer[] intArr = new Integer[100000];
84+
// Arrays.setAll(intArr, intFunction);
85+
//
86+
// long start = System.currentTimeMillis();
87+
// Arrays.sort(intArr);
88+
// long end = System.currentTimeMillis();
89+
// cost = cost + (end - start);
90+
//}
91+
//System.out.println(cost / 10);
92+
//
93+
//cost = 0;
94+
//for (int ii = 0; ii < 10; ii++) {
95+
// IntFunction<Integer> intFunction = i -> new Random().nextInt(100000);
96+
// Integer[] intArr = new Integer[100000];
97+
// Arrays.setAll(intArr, intFunction);
98+
//
99+
// long start = System.currentTimeMillis();
100+
// Arrays.parallelSort(intArr);
101+
// long end = System.currentTimeMillis();
102+
// cost = cost + (end - start);
103+
//}
104+
//System.out.println(cost / 10);
105+
}
106+
107+
@Test
108+
public void search() {
109+
Integer[] intArr = new Integer[] {2, 3, 4, 5, 6, 7, 8, 9};
110+
int index = Arrays.binarySearch(intArr, 3);
111+
System.out.println("index:"+index);
112+
System.out.println(intArr[index]);
113+
}
114+
115+
@Test
116+
public void stream() {
117+
Integer[] intArr = new Integer[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
118+
System.out.println(Arrays.stream(intArr).count());
119+
ToIntFunction toIntFunction = i -> (int)i;
120+
System.out.println(Arrays.stream(intArr).mapToInt(toIntFunction).sum());
121+
}
122+
123+
@Test
124+
public void cast() {
125+
String[] arr = new String[] {"a", "b", "c", "d"};
126+
Object[] arr2 = new Object[] {arr, arr};
127+
128+
System.out.println(Arrays.toString(arr));
129+
System.out.println(Arrays.toString(arr2));
130+
131+
System.out.println(Arrays.deepToString(arr));
132+
System.out.println(Arrays.deepToString(arr2));
133+
134+
List<String> list = Arrays.asList(arr);
135+
System.out.println(list);
136+
System.out.println(list.getClass());
137+
// list.add("e"); 报错
138+
}
139+
140+
@Test
141+
public void prefix() {
142+
Integer[] intArr = new Integer[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
143+
Arrays.parallelPrefix(intArr, (left, right) -> left + right);
144+
System.out.println(Arrays.toString(intArr));
145+
}
146+
}

0 commit comments

Comments
 (0)