Skip to content

Commit 71d6757

Browse files
authored
Merge pull request onlyliuxin#3 from 240094626/master
workspace of 240094626
2 parents f88a48f + 2cb6d5c commit 71d6757

File tree

9 files changed

+619
-0
lines changed

9 files changed

+619
-0
lines changed

group17/240094626/.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
*.class
2+
3+
# Mobile Tools for Java (J2ME)
4+
.mtj.tmp/
5+
6+
# Package Files #
7+
*.jar
8+
*.war
9+
*.ear
10+
11+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12+
hs_err_pid*
13+
14+
#ide config
15+
.metadata
16+
.recommenders
17+
18+
*.settings
19+
*.project
20+
*.classpath
21+
*/.settings
22+
/**/target/**/*

group17/240094626/warm-up/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/bin/
2+
*.classpath
3+
*.project
4+
/.settings/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coding.basic;
2+
3+
public interface List {
4+
public void add(Object o);
5+
public void add(int index, Object o);
6+
public Object get(int index);
7+
public Object remove(int index);
8+
public int size();
9+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package com.coding.basic.impl;
2+
3+
import java.util.Arrays;
4+
5+
import com.coding.basic.Iterator;
6+
import com.coding.basic.List;
7+
8+
/**
9+
*
10+
* @描述: ArrayList简单实现
11+
* @作者:240094626
12+
* @创建日期:2017-2-20
13+
*/
14+
public class ArrayList implements List {
15+
16+
17+
/**
18+
* @comment:元素数组
19+
*/
20+
private Object data[] = null;
21+
22+
/**
23+
* @comment:数组元素个数
24+
*/
25+
private int size = 0;
26+
27+
/**
28+
* 无参构造函数,初始化容量为10的空列表
29+
*/
30+
public ArrayList(){
31+
this(10);
32+
}
33+
34+
/**
35+
* @param length
36+
* 构造函数,初始化容量为length的空列表
37+
*/
38+
public ArrayList(int length){
39+
if(length < 0){
40+
throw new IllegalArgumentException("初始容量参数非法:"+length);
41+
}
42+
data = new Object[length];
43+
}
44+
45+
46+
/**
47+
* @createTime: 2017-2-21 下午1:32:28
48+
* @param length
49+
* @return:void
50+
* @comment:列表结构扩展容量,每次增加原来的1/2容量
51+
*/
52+
private void grow(int length){
53+
int oldLength = data.length;
54+
if(length > oldLength){
55+
Object oldData[] = data;
56+
int newLength = oldLength*3/2 + 1;
57+
if(newLength < length){
58+
newLength = length;
59+
}
60+
data = new Object[newLength];
61+
System.arraycopy(oldData, 0, data, 0, oldLength);
62+
}
63+
}
64+
65+
/**
66+
* @createTime: 2017-2-21 下午1:32:05
67+
* @param index
68+
* @return:void
69+
* @comment:检验下标参数是否超限
70+
*/
71+
private void check(int index) {
72+
if( index >= size){
73+
throw new IndexOutOfBoundsException("Index:"+index+",size:"+size);
74+
}
75+
}
76+
77+
@Override
78+
public void add(Object o) {
79+
grow(size+1);
80+
data[size++]=o;
81+
}
82+
83+
@Override
84+
public void add(int index, Object o) {
85+
if( index > size || index < 0){
86+
throw new IndexOutOfBoundsException("Index:"+index);
87+
}
88+
grow(size+1);
89+
System.arraycopy(data, index, data, index+1, size-index);
90+
data[index] = o;
91+
size++;
92+
93+
}
94+
95+
@Override
96+
public Object get(int index) {
97+
check(index);
98+
return data[index];
99+
}
100+
101+
102+
103+
@Override
104+
public Object remove(int index) {
105+
check(index);
106+
Object remove = data[index];
107+
System.arraycopy(data, index+1, data, index, size-index);
108+
data[--size] = null;
109+
return remove;
110+
}
111+
112+
@Override
113+
public int size() {
114+
return size;
115+
}
116+
117+
118+
@Override
119+
public String toString() {
120+
return "ArrayList [data=" + Arrays.toString(data) + ", size=" + size
121+
+ "]";
122+
}
123+
124+
public Iterator iterator(){
125+
return new ArrayListIterator();
126+
}
127+
128+
/**
129+
* @描述: 简单实现迭代器
130+
* @作者:240094626
131+
* @创建日期:2017-2-21
132+
*/
133+
private class ArrayListIterator implements Iterator{
134+
135+
/**
136+
* @column:index
137+
* @comment:当前位置下标
138+
*/
139+
private int index;
140+
141+
/**
142+
* 无参构造,初始化迭代器的下标为0
143+
*/
144+
public ArrayListIterator(){
145+
index = 0;
146+
}
147+
148+
@Override
149+
public boolean hasNext() {
150+
if(index < size){
151+
return true;
152+
}
153+
return false;
154+
}
155+
156+
@Override
157+
public Object next() {
158+
Object o = get(index++);
159+
return o;
160+
}
161+
162+
}
163+
164+
}
165+
166+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.coding.basic.impl;
2+
3+
4+
/**
5+
* 二叉树简单实现(key为int类型)
6+
* @author 240094626
7+
*
8+
*/
9+
public class BinaryTree {
10+
private Node rootNode = null;
11+
12+
public Node insert(int key){
13+
return insert(key,null);
14+
}
15+
16+
public Node insert(int key ,Object o){
17+
Node newNode = new Node(key, o);
18+
if(rootNode == null){
19+
rootNode = newNode;
20+
return rootNode;
21+
}
22+
Node fatherNode = rootNode;
23+
Node currentNode = rootNode;
24+
while(currentNode != null){
25+
fatherNode = currentNode;
26+
if(key < currentNode.key){
27+
currentNode = currentNode.left;
28+
}else{
29+
currentNode = currentNode.right;
30+
}
31+
}
32+
if(key < fatherNode.key){
33+
fatherNode.left = newNode;
34+
}else{
35+
fatherNode.right = newNode;
36+
}
37+
return newNode;
38+
}
39+
40+
public Node getNode(int key){
41+
return get(rootNode, key);
42+
}
43+
44+
private Node get(Node n,int key){
45+
if(n == null){
46+
return null;
47+
}
48+
if(key < n.key){
49+
return get(n.left, key);
50+
}else if(key > n.key){
51+
return get(n.left, key);
52+
}
53+
return null;
54+
}
55+
56+
57+
58+
59+
private static class Node{
60+
61+
int key;
62+
Object data;
63+
Node left;
64+
Node right;
65+
66+
public Node(int key, Object data) {
67+
this.key = key;
68+
this.data = data;
69+
this.left = null;
70+
this.right = null;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return "Node [key=" + key + ", data=" + data + "]";
76+
}
77+
78+
79+
}
80+
81+
82+
}

0 commit comments

Comments
 (0)