Skip to content

Commit e7357dd

Browse files
committed
新建目录和完成部分作业
1 parent 6a545e0 commit e7357dd

21 files changed

+982
-0
lines changed

group17/102228177/work2_19/.classpath

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"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

group17/102228177/work2_19/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

group17/102228177/work2_19/.project

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>102228177</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: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package data2_19;
2+
3+
import java.util.ConcurrentModificationException;
4+
import java.util.NoSuchElementException;
5+
6+
7+
public class ArrayList implements List{
8+
public static final int defLen = 10;
9+
private Object[] elements;
10+
private int size;
11+
private int maxLen;
12+
13+
public ArrayList(){
14+
size = 0;
15+
maxLen = defLen;
16+
elements = new Object[defLen];
17+
}
18+
19+
/**
20+
* 在ArrayList末尾处追加元素
21+
* @param o 添加的元素
22+
*/
23+
public void add(Object o){
24+
if(size >= maxLen){
25+
grow();
26+
}
27+
elements[size] = o;
28+
size++;
29+
}
30+
31+
/**
32+
* 数组扩容
33+
*/
34+
private void grow(){
35+
maxLen = maxLen + (maxLen >> 1);
36+
Object[] newArr = new Object[maxLen];
37+
System.arraycopy(elements, 0, newArr, 0, size);
38+
elements = newArr;
39+
}
40+
41+
/**
42+
* 在指定索引处添加元素
43+
* @param i 指定索引
44+
* @param o 添加元素
45+
*/
46+
public void add(int i,Object o){
47+
//判断插入位置大于数组实际长度
48+
if(i > size){
49+
size = i;
50+
if(size >= maxLen){//数组大小大于数组最大容量则需要扩容
51+
grow();
52+
}
53+
}
54+
//插入位置不大于数组实际长度时,将插入位置的元素向后移。
55+
for (int j = size; j > i ; j++) {
56+
elements[j] = elements[j-1];
57+
}
58+
elements[i] = o;
59+
size++;
60+
}
61+
62+
/**
63+
* 获取传入索引的元素
64+
* @param index 索引
65+
* @return 返回传入索引的元素
66+
*/
67+
public Object get(int index){
68+
//索引不在实际范围内
69+
if(index < 0||index >= size){
70+
throw new ArrayIndexOutOfBoundsException();
71+
}
72+
for (int i = 0; i < size; i++) {
73+
return elements[index];
74+
}
75+
return null;
76+
}
77+
78+
/**
79+
* 删除指定索引元素并返回
80+
* @param index
81+
* @return 该索引处元素
82+
*/
83+
public Object remove(int index){
84+
//索引不在实际范围内
85+
if(index < 0||index >= size){
86+
throw new ArrayIndexOutOfBoundsException();
87+
}else{
88+
for (int j = index; j < size-1; j++) {
89+
elements[j]=elements[j+1];
90+
}
91+
size--;
92+
return elements[index];
93+
}
94+
}
95+
96+
/**
97+
* 获取大小
98+
* @return
99+
*/
100+
public int size(){
101+
return size;
102+
}
103+
104+
public Iterator iterator(){
105+
return new ArrayListIterator();
106+
}
107+
108+
private class ArrayListIterator implements Iterator{
109+
int cursor;
110+
111+
@Override
112+
public boolean hasNext() {
113+
return cursor != size;
114+
}
115+
116+
@Override
117+
public Object next() {
118+
int i = cursor;
119+
if(i >= size){
120+
throw new NoSuchElementException();
121+
}
122+
if (i >= elements.length){
123+
throw new ConcurrentModificationException();
124+
}
125+
cursor = i+1;
126+
return elements[i];
127+
}
128+
}
129+
130+
public static void main(String[] args) {
131+
ArrayList list = new ArrayList();
132+
list.add(0);
133+
list.add(1);
134+
list.add(2);
135+
list.add(3);
136+
list.add(4);
137+
list.add(6, 6);
138+
list.remove(3);
139+
for (int i = 0; i < list.size(); i++) {
140+
System.out.println(i+":"+list.get(i));
141+
}
142+
143+
Iterator it = list.iterator();
144+
while (it.hasNext()) {
145+
System.out.println(it.next());
146+
}
147+
}
148+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package data2_19;
2+
3+
public class BinaryTreeNode implements Comparable<BinaryTreeNode>{
4+
5+
private Object data;
6+
private BinaryTreeNode left;
7+
private BinaryTreeNode right;
8+
9+
public BinaryTreeNode(Object o) {
10+
this.data = o;
11+
}
12+
13+
public Object getData() {
14+
return data;
15+
}
16+
public void setData(Object data) {
17+
this.data = data;
18+
}
19+
public BinaryTreeNode getLeft() {
20+
return left;
21+
}
22+
public void setLeft(BinaryTreeNode left) {
23+
this.left = left;
24+
}
25+
public BinaryTreeNode getRight() {
26+
return right;
27+
}
28+
public void setRight(BinaryTreeNode right) {
29+
this.right = right;
30+
}
31+
32+
@Override
33+
public int compareTo(BinaryTreeNode o) {
34+
return (this.data.hashCode() < o.data.hashCode()) ? -1 :
35+
((this.data.hashCode() == o.data.hashCode()) ? 0 : 1);
36+
}
37+
38+
public BinaryTreeNode insert(Object o){
39+
BinaryTreeNode node = new BinaryTreeNode(o);
40+
insertNode(this,node);
41+
return node;
42+
}
43+
44+
private void insertNode(BinaryTreeNode parentNode, BinaryTreeNode node) {
45+
//父节点大于添加元素
46+
if(parentNode.compareTo(node) == 1){
47+
if(parentNode.left == null){
48+
parentNode.left = node;
49+
return;
50+
}
51+
insertNode(parentNode.left, node);
52+
}
53+
//父节点小于添加元素
54+
else
55+
if(parentNode.compareTo(node) == -1){
56+
if(parentNode.right == null){
57+
parentNode.right = node;
58+
return;
59+
}
60+
insertNode(parentNode.right, node);
61+
}else{
62+
throw new RuntimeException("No duplicate vertex allowed!");
63+
}
64+
}
65+
66+
public static void main(String[] args) {
67+
BinaryTreeNode tree = new BinaryTreeNode(5);
68+
tree.insert(2);
69+
tree.insert(23);
70+
tree.insert(7);
71+
tree.insert(1);
72+
}
73+
74+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package data2_19;
2+
3+
public interface Iterator {
4+
5+
public boolean hasNext();
6+
public Object next();
7+
8+
}

0 commit comments

Comments
 (0)