Skip to content

Commit dcac31c

Browse files
AdministratorAdministrator
authored andcommitted
第一次作业
1 parent 9382b6c commit dcac31c

File tree

11 files changed

+594
-0
lines changed

11 files changed

+594
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>coding2017-01</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/**
2+
*
3+
*/
4+
package com.coding.basic;
5+
6+
/**
7+
* @author patchouli
8+
*
9+
*/
10+
public class ArrayList implements List {
11+
12+
/**
13+
* 数组列表初始默认容量100.
14+
*/
15+
private static final int DEFAULT_CAPACITY=100;
16+
17+
/**
18+
* 数组列表中元素的数量。
19+
*/
20+
private int size = 0;
21+
22+
/**
23+
* 数组列表当前容量
24+
*/
25+
private int capacity;
26+
27+
/**
28+
* 存放元素的数组。
29+
*/
30+
private Object[] elementData;
31+
32+
/**
33+
* 默认构造函数,构造一个初始容量为100的数组列表
34+
*/
35+
public ArrayList() {
36+
capacity=DEFAULT_CAPACITY;
37+
elementData=new Object[DEFAULT_CAPACITY];
38+
}
39+
40+
/*
41+
* (non-Javadoc)
42+
*
43+
* @see nusub.coding2017.basic.List#add(java.lang.Object)
44+
*/
45+
@Override
46+
public void add(Object o) {
47+
expand();
48+
elementData[size]=o;
49+
size=size++;
50+
}
51+
52+
/*
53+
* (non-Javadoc)
54+
*
55+
* @see nusub.coding2017.basic.List#add(int, java.lang.Object)
56+
*/
57+
@Override
58+
public void add(int index, Object o) throws ListIndexException {
59+
if (index==size) {
60+
add(o);
61+
return;
62+
}
63+
if (0<=index&&index<size) {
64+
expand();
65+
for (int i = size; i>index; i--) {
66+
elementData[i]=elementData[i-1];
67+
}
68+
elementData[index]=o;
69+
return;
70+
}
71+
throw new ListIndexException("index不在[0,size]之间");
72+
}
73+
74+
/**
75+
* index在[0,size)之间返回通过数组下标访问的方式获取位置index处的元素,index超出这个范围抛出ListIndexException。
76+
* @param index 元素的位置
77+
* @return 获取的对象
78+
* @throws ListIndexException
79+
*/
80+
@Override
81+
public Object get(int index) throws ListIndexException {
82+
if (0<=index&&index<size) {
83+
return elementData[index];
84+
}
85+
throw new ListIndexException("index不在[0,size)之间");
86+
}
87+
88+
/**
89+
* 通过访问数组下标删除[0,size)之间的元素,被删除的元素之后的元素向前移动。如果index超出这个范围,抛出ListIndexException。
90+
* @param index 要删除大额元素的位置
91+
* @return 被删除的对象
92+
* @throws ListIndexException
93+
*/
94+
@Override
95+
public Object remove(int index) throws ListIndexException {
96+
if (0<=index&&index<size) {
97+
throw new ListIndexException("index不在[0,size)之间");
98+
}
99+
Object object=elementData[index];
100+
for(int i=index;i<size;i++){
101+
elementData[i]=elementData[i+1];
102+
}
103+
shrink();
104+
return object;
105+
}
106+
107+
/*
108+
* (non-Javadoc)
109+
*
110+
* @see nusub.coding2017.basic.List#size()
111+
*/
112+
@Override
113+
public int size() {
114+
return size;
115+
}
116+
117+
/**
118+
* 空间不足时扩容,容量变为原来的2倍,在添加元素前调用。
119+
*/
120+
private void expand(){
121+
if (size<DEFAULT_CAPACITY) {
122+
return;
123+
}
124+
capacity=capacity*2;
125+
Object[] oldElementData=elementData;
126+
elementData=new Object[capacity];
127+
for (int i = 0; i < size; i++) {
128+
elementData[i]=oldElementData[i];
129+
}
130+
}
131+
132+
/**
133+
* 装填因子小于25%时收缩,容量变为原来的50%,但不小于初始容量。在移除元素后使用。
134+
*/
135+
private void shrink(){
136+
if (capacity<DEFAULT_CAPACITY*2) {
137+
return;
138+
}
139+
if (size*4<capacity) {
140+
capacity=capacity/2;
141+
Object[] oldElement=elementData;
142+
elementData=new Object[capacity];
143+
for (int i = 0; i < size; i++) {
144+
elementData[i]=oldElement[i];
145+
}
146+
}
147+
}
148+
149+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
*
3+
*/
4+
package com.coding.basic;
5+
6+
/**
7+
* @author patchouli
8+
* @param <T>
9+
*
10+
*/
11+
public class BinaryTreeNode {
12+
13+
private Object data;
14+
private BinaryTreeNode left;
15+
private BinaryTreeNode right;
16+
17+
public BinaryTreeNode(){}
18+
19+
public BinaryTreeNode(Object data){
20+
this.data=data;
21+
left=null;
22+
right=null;
23+
}
24+
25+
public Object getData() {
26+
return data;
27+
}
28+
29+
public void setData(Object data) {
30+
this.data = data;
31+
}
32+
33+
public BinaryTreeNode getLeft() {
34+
return left;
35+
}
36+
37+
public void setLeft(BinaryTreeNode left) {
38+
this.left = left;
39+
}
40+
41+
public BinaryTreeNode getRight() {
42+
return right;
43+
}
44+
45+
public void setRight(BinaryTreeNode right) {
46+
this.right = right;
47+
}
48+
49+
public BinaryTreeNode insert(Object o) {
50+
if (o.toString().compareTo(data.toString())<0) {
51+
if (left==null) {
52+
left=new BinaryTreeNode(o);
53+
return left;
54+
}
55+
insert(o);
56+
}
57+
else {
58+
if (right==null) {
59+
right=new BinaryTreeNode(o);
60+
return right;
61+
}
62+
insert(o);
63+
}
64+
return null;
65+
}
66+
67+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*
3+
*/
4+
package com.coding.basic;
5+
6+
/**
7+
* @author patchouli
8+
*
9+
*/
10+
public interface Iterator {
11+
/**
12+
* 集合中存在下一个元素返回true,不存在下一个元素返回false。
13+
* @return
14+
*/
15+
public boolean hasNext();
16+
17+
/**
18+
* 返回集合中下一个元素
19+
* @return
20+
*/
21+
public Object next();
22+
}

0 commit comments

Comments
 (0)