Skip to content

Commit 66b58e9

Browse files
committed
test
1 parent dc22720 commit 66b58e9

File tree

31 files changed

+909
-6
lines changed

31 files changed

+909
-6
lines changed

Collection/Collection.iml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
</component>
15+
</module>
16+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
import java.util.Random;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: Loon
8+
* Date: 13-4-2
9+
* Time: 下午10:14
10+
* To change this template use File | Settings | File Templates.
11+
*/
12+
public class ArraysDemo {
13+
public static void main(String[] args) {
14+
15+
// aslist使用
16+
asList();
17+
18+
// 复制数组
19+
copyingArrays();
20+
21+
// 数组排序
22+
sort();
23+
24+
// 查找
25+
binarySearch();
26+
}
27+
28+
public static void copyingArrays() {
29+
30+
int[] i = new int[7];
31+
int[] j = new int[10];
32+
33+
// 插入元素
34+
Arrays.fill(i, 47);
35+
Arrays.fill(j, 99);
36+
37+
System.out.println(Arrays.toString(i));
38+
System.out.println(Arrays.toString(j));
39+
// 比较数据
40+
System.out.println(Arrays.equals(i,j));
41+
42+
// 复制元素
43+
System.arraycopy(i,0,j,0,i.length);
44+
System.out.println(Arrays.toString(j));
45+
}
46+
47+
48+
public static void asList() {
49+
50+
List list = Arrays.asList(1, 2, 3);
51+
52+
for (Object o : list) {
53+
System.out.println(o);
54+
}
55+
}
56+
57+
58+
public static void hasCode() {
59+
60+
Integer[] integer = {4, 5, 6};
61+
// 生产散列码
62+
System.out.println(integer.hashCode());
63+
}
64+
65+
public static void sort() {
66+
67+
String[] strings =new String[10];
68+
69+
for (int i= 0;i<strings.length;i++){
70+
strings[i] =getRandomString(5);
71+
}
72+
73+
System.out.println("Before sort:" + Arrays.toString(strings));
74+
// 按照词典编排顺序 大写在前
75+
Arrays.sort(strings,String.CASE_INSENSITIVE_ORDER);
76+
// Arrays.sort(strings);
77+
System.out.println("After sort:" + Arrays.toString(strings));
78+
79+
}
80+
81+
public static void binarySearch() {
82+
83+
String[] strings =new String[10];
84+
85+
for (int i= 0;i<strings.length;i++){
86+
strings[i] =getRandomString(5);
87+
}
88+
89+
System.out.println(Arrays.toString(strings));
90+
91+
int index = Arrays.binarySearch(strings,strings[2],String.CASE_INSENSITIVE_ORDER);
92+
93+
System.out.println(index+" "+strings[index]);
94+
}
95+
96+
97+
// 获取随机字符串
98+
public static String getRandomString(int length) {
99+
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
100+
Random random = new Random();
101+
StringBuffer sb = new StringBuffer();
102+
for (int i = 0; i < length; i++) {
103+
int number = random.nextInt(base.length());
104+
sb.append(base.charAt(number));
105+
}
106+
return sb.toString();
107+
}
108+
109+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Created with IntelliJ IDEA.
3+
* User: Loon
4+
* Date: 13-4-2
5+
* Time: 下午10:35
6+
* To change this template use File | Settings | File Templates.
7+
*/
8+
public class ListDemo {
9+
public static void main(String[] args) {
10+
11+
}
12+
}

Concurrency/Concurrency.iml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
<component name="NewModuleRootManager" inherit-compiler-output="false">
44
<output url="file://$MODULE_DIR$/target/classes" />
55
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6-
<exclude-output />
76
<content url="file://$MODULE_DIR$">
87
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
9-
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
108
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
1110
<excludeFolder url="file://$MODULE_DIR$/target" />
1211
</content>
1312
<orderEntry type="inheritedJdk" />
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.ArrayList;
2+
import java.util.concurrent.*;
3+
4+
/**
5+
* Created with IntelliJ IDEA.
6+
* User: Loon
7+
* Date: 13-4-8
8+
* Time: 下午10:47
9+
* To change this template use File | Settings | File Templates.
10+
*/
11+
public class CallableDemo {
12+
13+
public static void main(String[] args) {
14+
ExecutorService executorService = Executors.newCachedThreadPool();
15+
16+
ArrayList<Future<String>> results = new ArrayList<Future<String>>();
17+
for (int i = 0; i < 5; i++) {
18+
results.add(executorService.submit(new TaskWithResult(i)));
19+
}
20+
for (Future<String> str : results) {
21+
try {
22+
System.out.println(str.get());
23+
} catch (InterruptedException e) {
24+
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
25+
} catch (ExecutionException e) {
26+
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
27+
} finally {
28+
executorService.shutdown();
29+
}
30+
31+
}
32+
33+
}
34+
}
35+
36+
class TaskWithResult implements Callable<String> {
37+
private int id;
38+
39+
public TaskWithResult(int id) {
40+
this.id = id;
41+
}
42+
43+
@Override
44+
public String call() throws Exception {
45+
return "result of TaskWithResult :" + id;
46+
}
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Created with IntelliJ IDEA.
3+
* User: Loon
4+
* Date: 13-4-7
5+
* Time: 下午9:02
6+
* To change this template use File | Settings | File Templates.
7+
*/
8+
public class LiftOff implements Runnable {
9+
protected int countDown = 10;
10+
private static int taskCount = 0;
11+
private final int id = taskCount++;
12+
13+
public LiftOff() {
14+
}
15+
16+
public LiftOff(int countDown) {
17+
this.countDown = countDown;
18+
19+
}
20+
21+
public String stastus() {
22+
return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!") + ").";
23+
24+
}
25+
26+
@Override
27+
public void run() {
28+
while (countDown -- >0){
29+
System.out.println(stastus());
30+
Thread.yield();
31+
}
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
4+
/**
5+
* Created with IntelliJ IDEA.
6+
* User: Loon
7+
* Date: 13-4-7
8+
* Time: 下午9:12
9+
* To change this template use File | Settings | File Templates.
10+
*/
11+
public class MainThread {
12+
13+
public static void main(String[] args) {
14+
// LiftOff lifrOff = new LiftOff();
15+
// lifrOff.run();
16+
17+
18+
// 1.5新特性,启动线程
19+
// newCachedThreadPool newSingleThreadExecutor newScheduledThreadPool newFixedThreadPool
20+
ExecutorService executorService = Executors.newCachedThreadPool();
21+
22+
for (int i = 0; i < 5; i++)
23+
executorService.submit(new LiftOff());
24+
executorService.shutdown();
25+
}
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: Loon
8+
* Date: 13-4-8
9+
* Time: 下午10:59
10+
* To change this template use File | Settings | File Templates.
11+
*/
12+
public class SleepingTask extends LiftOff {
13+
public void run() {
14+
while (countDown-- > 0) {
15+
System.out.println(stastus());
16+
17+
try {
18+
// Old style
19+
// Thread.sleep(200);
20+
21+
// Java SE5 style
22+
TimeUnit.MILLISECONDS.sleep(200);
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
System.err.println("Interrupted");
26+
}
27+
28+
}
29+
}
30+
31+
public static void main(String[] args) {
32+
ExecutorService executorService = Executors.newCachedThreadPool();
33+
34+
for (int i = 0; i < 5; i++) {
35+
executorService.submit(new SleepingTask());
36+
}
37+
executorService.shutdown();
38+
}
39+
}

DesignPatterns/DesignPatterns.iml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
</component>
15+
</module>
16+

0 commit comments

Comments
 (0)