forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
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 #8 from likeleaf/master
first commit
- Loading branch information
Showing
9 changed files
with
728 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,5 @@ | ||
/.settings/ | ||
/bin/ | ||
.classpath | ||
.project | ||
|
106 changes: 106 additions & 0 deletions
106
group24/893022254/task1-list/src/com/oneflyingleaf/util/ArrayList.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,106 @@ | ||
package com.oneflyingleaf.util; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
//非线程安全 | ||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
|
||
checkLength(1); | ||
|
||
elementData[ size ++] = o; | ||
} | ||
|
||
|
||
public void add(int index, Object o){ | ||
checkBound(index); | ||
|
||
if(index != size){ | ||
checkLength(1); | ||
} | ||
|
||
int temp = index; | ||
|
||
//index == size 不移动,直接放 | ||
while(index < size){ | ||
elementData[ ++ index ] = elementData[index]; | ||
} | ||
|
||
size ++ ; | ||
elementData[temp] = o; | ||
} | ||
|
||
public Object get(int index){ | ||
checkBound(index); | ||
|
||
return elementData [index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
checkBound(index); | ||
|
||
Object ret = elementData[index]; | ||
while(index < size){ | ||
elementData[index] = elementData[ ++ index]; | ||
} | ||
|
||
-- size ; | ||
|
||
return ret; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(); | ||
} | ||
|
||
/** | ||
* 判断扩容 | ||
* @param length | ||
*/ | ||
private void checkLength(int length){ | ||
if( (size + length) > elementData.length){ | ||
//默认扩大一倍 | ||
Arrays.copyOf(elementData, elementData.length << 1); | ||
} | ||
} | ||
|
||
/** | ||
* 校验是否超出 | ||
* @param index | ||
*/ | ||
private void checkBound(int index){ | ||
if(index < 0 || index > size){ | ||
throw new IndexOutOfBoundsException("size : " + size +" , index : " + index); | ||
} | ||
} | ||
|
||
|
||
private class ArrayListIterator implements Iterator{ | ||
|
||
private int count = -1; | ||
|
||
private ArrayListIterator(){ | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return (count+1) < size?true:false; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
count ++ ; | ||
return elementData[count]; | ||
} | ||
|
||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
group24/893022254/task1-list/src/com/oneflyingleaf/util/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,57 @@ | ||
package com.oneflyingleaf.util; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
if(! (o instanceof Comparable)){ | ||
throw new RuntimeException("未实现Comparable接口"); | ||
} | ||
Comparable<Object> temp = (Comparable<Object>)o; | ||
|
||
if(temp.compareTo(data) > 0){ | ||
if(right == null){ | ||
right= new BinaryTreeNode(); | ||
right.data = o; | ||
}else{ | ||
right.insert(o); | ||
} | ||
} | ||
|
||
if(temp.compareTo(data) <= 0){ | ||
if(left == null){ | ||
|
||
left = new BinaryTreeNode(); | ||
left.data = o; | ||
}else{ | ||
left.insert(o); | ||
} | ||
} | ||
|
||
|
||
return this; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
group24/893022254/task1-list/src/com/oneflyingleaf/util/Iterator.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,7 @@ | ||
package com.oneflyingleaf.util; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
Oops, something went wrong.