Skip to content

Commit 2ef14ac

Browse files
authored
Merge pull request onlyliuxin#7 from maishihang/master
complete
2 parents 9f44c5e + 6e39ef6 commit 2ef14ac

File tree

7 files changed

+555
-0
lines changed

7 files changed

+555
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
*
7+
* arrayList集合-数组
8+
*
9+
* @ClassName ArrayList
10+
* @author msh
11+
* @date 2017年2月21日 下午3:49:24
12+
*/
13+
public class ArrayList implements List {
14+
// 记录ArrayList集合大小
15+
private int size = 0;
16+
// 初始化存储数组
17+
private Object[] elementData = new Object[100];
18+
/**
19+
*
20+
* 向最后插入元素
21+
*
22+
* @Method add 添加
23+
* @param o 元素
24+
* @see com.coding.basic.List#add(java.lang.Object)
25+
*/
26+
public void add(Object o){
27+
// 数组不够时增长
28+
growOrNot(size + 1);
29+
elementData[size] = o;
30+
++size;
31+
}
32+
/**
33+
*
34+
* 向指定位置插入元素
35+
*
36+
* @Method add 添加
37+
* @param index 下标
38+
* @param o 元素
39+
* @see com.coding.basic.List#add(int, java.lang.Object)
40+
*/
41+
public void add(int index, Object o){
42+
validate(index);
43+
growOrNot(size + 1);
44+
System.arraycopy(elementData, index, elementData, index + 1, size - index);
45+
elementData[index] = o;
46+
++size;
47+
}
48+
/**
49+
*
50+
* 取得元素
51+
*
52+
* @Method get 取得
53+
* @param index 下标
54+
* @return
55+
* @see com.coding.basic.List#get(int)
56+
*/
57+
public Object get(int index){
58+
validate(index);
59+
return elementData[index];
60+
}
61+
/**
62+
*
63+
* 删除元素
64+
*
65+
* @Method remove 删除
66+
* @param index 下标
67+
* @return 删除的元素
68+
* @see com.coding.basic.List#remove(int)
69+
*/
70+
public Object remove(int index){
71+
Object oldValue = elementData[index];
72+
validate(index);
73+
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
74+
elementData[size] = null;
75+
--size;
76+
return oldValue;
77+
}
78+
/**
79+
*
80+
* 取得集合大小
81+
*
82+
* @Method size 集合大小
83+
* @return 集合大小
84+
* @see com.coding.basic.List#size()
85+
*/
86+
public int size(){
87+
return size;
88+
}
89+
90+
public Iterator iterator(){
91+
return null;
92+
}
93+
/**
94+
*
95+
* 判断是否需要增长数组
96+
*
97+
* @MethodName growOrNot
98+
* @author msh
99+
* @date 2017年2月21日 下午3:53:29
100+
* @param minCapacity
101+
*/
102+
private void growOrNot(int minCapacity) {
103+
// 当增加长度大于数组长度时,增长
104+
if (minCapacity > elementData.length) {
105+
elementData = Arrays.copyOf(elementData, elementData.length * 2);
106+
}
107+
}
108+
/**
109+
*
110+
* 验证
111+
*
112+
* @MethodName validate 下标
113+
* @author msh
114+
* @date 2017年2月21日 下午3:54:21
115+
* @param index
116+
*/
117+
private void validate(int index) {
118+
if (index < 0 || index >= size)
119+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
120+
}
121+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.coding.basic;
2+
3+
4+
public class BinaryTreeNode {
5+
private Object data;
6+
private BinaryTreeNode left;
7+
private BinaryTreeNode right;
8+
private BinaryTreeNode( Object data,BinaryTreeNode left,BinaryTreeNode right){
9+
this.data = data;
10+
this.left = left;
11+
this.right = right;
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+
public BinaryTreeNode insert(BinaryTreeNode tree,Object o){
33+
if(null== tree){
34+
tree = new BinaryTreeNode(o, null, null);
35+
}
36+
if(Integer.valueOf(o.toString())>Integer.valueOf(tree.data.toString())){
37+
tree.right =insert(tree.right,o);
38+
}else{
39+
tree.left = insert(tree.left,o);
40+
}
41+
return tree;
42+
}
43+
44+
}
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+
}

0 commit comments

Comments
 (0)