Skip to content

Commit 66d3e47

Browse files
committed
1.格式化代码
2.pom添加jdk1.8编译支持
1 parent dabaece commit 66d3e47

5 files changed

Lines changed: 130 additions & 5 deletions

File tree

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,18 @@
2828
<scope>provided</scope>
2929
</dependency>
3030
</dependencies>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-compiler-plugin</artifactId>
37+
<version>3.1</version>
38+
<configuration>
39+
<source>1.8</source>
40+
<target>1.8</target>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
3145
</project>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package datastructure;
2+
3+
import java.io.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* @author yangqc
9+
*/
10+
public class IndexTree {
11+
static private int Sn = -1;
12+
static private Node root;
13+
14+
static private class Node implements Serializable {
15+
int data;
16+
transient Node left;
17+
transient Node right;
18+
int l = -1, r = -1;
19+
20+
public Node(int data, Node l, Node r) {
21+
this.data = data;
22+
this.left = l;
23+
this.right = r;
24+
}
25+
26+
public int write(ObjectOutputStream out) throws IOException {
27+
if (left != null) {
28+
l = left.write(out);
29+
}
30+
if (right != null) {
31+
r = right.write(out);
32+
}
33+
Sn++;
34+
out.writeObject(this);
35+
return Sn;
36+
}
37+
38+
private void init(List<Node> list) {
39+
if (l != -1) {
40+
left = list.get(l);
41+
left.init(list);
42+
}
43+
if (r != -1) {
44+
right = list.get(r);
45+
right.init(list);
46+
}
47+
}
48+
49+
@Override
50+
public String toString() {
51+
StringBuilder sb = new StringBuilder();
52+
sb.append(data + " ");
53+
if (left != null) {
54+
sb.append(left);
55+
}
56+
if (right != null) {
57+
sb.append(right);
58+
}
59+
return sb.toString();
60+
}
61+
}
62+
63+
static public void read(ObjectInputStream in) {
64+
List<Node> list = new ArrayList<>();
65+
Node n;
66+
try {
67+
while (((n = (Node) in.readObject()) != null)) {
68+
list.add(n);
69+
}
70+
} catch (Exception e) {
71+
e.printStackTrace();
72+
}
73+
root = list.get(list.size() - 1);
74+
root.init(list);
75+
}
76+
77+
public static void main(String[] args) throws IOException {
78+
// 构造一棵二叉树
79+
/*
80+
* 1 2 3 4 5 6
81+
*/
82+
Node n6 = new Node(6, null, null);
83+
Node n4 = new Node(4, n6, null);
84+
Node n5 = new Node(5, null, null);
85+
Node n2 = new Node(2, n4, n5);
86+
Node n3 = new Node(3, null, null);
87+
Node n1 = new Node(1, n2, n3);
88+
root = n1;
89+
System.out.println(root);
90+
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("abc.ser"));
91+
root.write(out);
92+
out.close();
93+
ObjectInputStream in = new ObjectInputStream(new FileInputStream("abc.ser"));
94+
read(in);
95+
in.close();
96+
System.out.println(root);
97+
}
98+
}

src/main/java/jvm/classloader/ClassLoaderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
/**
77
* 自定义ClassLoader加载当前包下的.class文件
88
* 2018/3/18
9+
*
10+
* @author yangqc
911
*/
1012
public class ClassLoaderTest {
1113
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {

src/main/java/jvm/classloader/FileSystemClassLoader.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* (3)如果父类没有装载,再调用本类加载器的findClass(…)方法,试图获取对应的字节码,如果获取得到,则调用defineClass(…)导入类型到方法区;
1111
* (4)如果获取不到对应的字节码或者其他原因失败,返回异常给loadclass(…),loadclass(…)转抛异常,终止加载过程。
1212
* 2018/3/19
13+
*
14+
* @author yangqc
1315
*/
1416
public class FileSystemClassLoader extends ClassLoader {
1517
//com.lgd.User --> d:/myjava/ com/lgd/User.class
@@ -19,20 +21,25 @@ public FileSystemClassLoader(String rootDir) {
1921
this.rootDir = rootDir;
2022
}
2123

22-
//重写方法
24+
/**
25+
* 重写方法
26+
*/
2327
@Override
2428
protected Class<?> findClass(String name) throws ClassNotFoundException {
25-
Class<?> c = findLoadedClass(name);//查找已加载的类
29+
//查找已加载的类
30+
Class<?> c = findLoadedClass(name);
2631
//应该要先查询有没有加载过这个类。如果已经加载,不为空,则直接返回加载好的类。
2732
//如果没有,则加载新的类。
2833
if (c != null) {
2934
return c;
3035
} else {
3136
//获得他的父类,让父类去加载去加载
32-
ClassLoader parent = this.getParent(); //获得appclassloader
37+
//获得appclassloader
38+
ClassLoader parent = this.getParent();
3339
//采用的是双亲委派机制
3440
try {
35-
c = parent.loadClass(name);//委派给父类加载
41+
//委派给父类加载
42+
c = parent.loadClass(name);
3643
} catch (Exception e) {
3744
e.printStackTrace();
3845
}

src/main/java/jvm/classloader/FileSystemLoaderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package jvm.classloader;
22

3+
/**
4+
* @author yangqc
5+
*/
36
public class FileSystemLoaderTest {
47
public static void main(String[] args) throws ClassNotFoundException {
58
FileSystemClassLoader loader1 = new FileSystemClassLoader("/home/yangqc/IdeaProjects/java-basic");
@@ -22,6 +25,7 @@ public static void main(String[] args) throws ClassNotFoundException {
2225
Class<?> c5 = loader2.loadClass("JVMProcess.Demo");
2326
System.out.println(c5 + "-->" + c5.hashCode());
2427
//应用类加载器
25-
System.out.println(c5.getClassLoader());//系统默认的类加载器
28+
//系统默认的类加载器
29+
System.out.println(c5.getClassLoader());
2630
}
2731
}

0 commit comments

Comments
 (0)