forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from zhaog/master
425044891的第一次作业 上海平常心
- Loading branch information
Showing
7 changed files
with
493 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
* ArrayList | ||
* | ||
* @author Guang | ||
* @date 2017年3月12日 下午1:55:47 | ||
*/ | ||
public class ArrayList implements List { | ||
|
||
/** | ||
* 数组中元素个数 | ||
*/ | ||
private int size = 0; | ||
|
||
/** | ||
* 数组 | ||
*/ | ||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o) { | ||
if (size >= 100 || size < 0) { | ||
throw new IndexOutOfBoundsException("越界."); | ||
} | ||
if (null == o) { | ||
return; | ||
} | ||
elementData[size] = o; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
checkRangeForAdd(index); | ||
if (null == o) { | ||
return; | ||
} | ||
for (int i = size; i > index; i--) { | ||
elementData[i] = elementData[i - 1]; | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
private void checkRangeForAdd(int index) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("越界."); | ||
} | ||
} | ||
|
||
private void checkRangeForGet(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException("越界."); | ||
} | ||
} | ||
|
||
public Object get(int index) { | ||
checkRangeForGet(index); | ||
return elementData[index]; | ||
} | ||
|
||
/** | ||
* 移除第index位置的元素,后续元素前移 | ||
*/ | ||
public Object remove(int index) { | ||
checkRangeForGet(index); | ||
int i = index; | ||
Object o = elementData[index]; | ||
for (; i < size - 1; i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
elementData[i] = null; | ||
size--; | ||
return o; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Iterator() { | ||
int index = 0; | ||
@Override | ||
public Object next() { | ||
checkRangeForGet(index); | ||
Object o = elementData[index]; | ||
index++; | ||
return o; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return index < size && index >= 0; | ||
} | ||
}; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
group27/425044891/src/com/coding/basic/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode<T extends Comparable<T>> { | ||
|
||
private T data; | ||
private BinaryTreeNode<T> left; | ||
private BinaryTreeNode<T> right; | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T data) { | ||
this.data = data; | ||
} | ||
|
||
public BinaryTreeNode<T> getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(BinaryTreeNode<T> left) { | ||
this.left = left; | ||
} | ||
|
||
public BinaryTreeNode<T> getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(BinaryTreeNode<T> right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode<T> insert(T o) { | ||
if (o.compareTo(data) <= 0) { | ||
return getLeft().insert(o); | ||
} else { | ||
return getRight().insert(o); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.coding.basic; | ||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
Oops, something went wrong.