forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
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 houtaijun/master
再添加第一次作业,提交第二次作业,读取structs.xml和实现ArrayUtil,并都写了单元测试
- Loading branch information
Showing
14 changed files
with
869 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
group13/1392221554/lesson01/src/com/tiaozaoj/NewArrayList.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,90 @@ | ||
package com.tiaozaoj; | ||
import java.util.Arrays; | ||
|
||
public class NewArrayList { | ||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
//直接在后面添加 | ||
public void add(Object o){ | ||
//先判断该数组是否已满 | ||
if(this.size == elementData.length){ | ||
this.grow(elementData,5); | ||
} | ||
this.elementData[size] = o; | ||
this.size++; | ||
} | ||
|
||
private void grow(Object[] src,int length){ | ||
this.elementData = Arrays.copyOf(src, src.length+length); | ||
} | ||
|
||
public void add(int index,Object o){ | ||
this.verifyIndex(index); | ||
//先判断该数组是否已满 | ||
if(this.size == elementData.length){ | ||
this.grow(elementData,5); | ||
} | ||
for(int i=size;i>index;i--){ | ||
this.elementData[i+1] = this.elementData[i]; | ||
} | ||
this.elementData[index] = o; | ||
this.size++; | ||
} | ||
|
||
private void verifyIndex(int index){ | ||
try{ | ||
if(index <0 || index > this.size) | ||
throw new Exception("下标越界"); | ||
}catch(Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
this.verifyIndex(index); | ||
return this.elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
Object o = elementData[index]; | ||
this.verifyIndex(index); | ||
for(int i=index;i<size;i++){ | ||
this.elementData[i] = this.elementData[i+1]; | ||
} | ||
this.size--; | ||
return o; | ||
} | ||
|
||
public int size(){ | ||
return this.size; | ||
} | ||
|
||
public NewIterator iterator(){ | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
private class ArrayListIterator implements NewIterator{ | ||
NewArrayList l = null; | ||
int pos = 0; | ||
|
||
private ArrayListIterator(NewArrayList l){ | ||
this.l = l; | ||
} | ||
|
||
public boolean hasNext() { | ||
// TODO 自动生成的方法存根 | ||
return pos < l.size(); | ||
} | ||
|
||
public void remove(){ | ||
this.l.remove(pos); | ||
} | ||
|
||
public Object next() { | ||
// TODO 自动生成的方法存根 | ||
return l.get(pos++); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
group13/1392221554/lesson01/src/com/tiaozaoj/NewArrayListTest.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,58 @@ | ||
package com.tiaozaoj; | ||
|
||
import java.awt.List; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public class NewArrayListTest extends TestCase { | ||
private NewArrayList list = new NewArrayList(); | ||
|
||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
} | ||
|
||
protected void tearDown() throws Exception { | ||
super.tearDown(); | ||
} | ||
|
||
public void testAddObject() { | ||
list.add("0"); | ||
list.add("2"); | ||
assertEquals("0", list.get(0)); | ||
assertEquals("2", list.get(1)); | ||
} | ||
|
||
public void testAddIntObject() { | ||
list.add(0,"0"); | ||
assertEquals("0", list.get(0)); | ||
} | ||
|
||
public void testRemove() { | ||
list.add(0,"0"); | ||
list.add(1,"1"); | ||
list.add(2,"2"); | ||
assertEquals("1", list.remove(1)); | ||
} | ||
|
||
public void testSize() { | ||
list.add(0,"0"); | ||
list.add(1,"1"); | ||
list.add(2,"2"); | ||
assertEquals(3, list.size()); | ||
} | ||
|
||
public void testIterator() { | ||
int i = 0; | ||
for(NewIterator It = list.iterator();It.hasNext();){ | ||
Object str = (Object) It.next(); | ||
assertEquals(list.get(i++), str); | ||
} | ||
|
||
int j = list.size(); | ||
for(NewIterator It = list.iterator();It.hasNext();){ | ||
It.next(); | ||
It.remove(); | ||
assertEquals(--j, list.size()); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
group13/1392221554/lesson01/src/com/tiaozaoj/NewBinaryTree.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,61 @@ | ||
package com.tiaozaoj; | ||
|
||
public class NewBinaryTree { | ||
// Root node pointer. Will be null for an empty tree. | ||
private Node root ; | ||
|
||
private static class Node { | ||
Node left ; | ||
Node right ; | ||
int data ; | ||
|
||
Node( int newData) { | ||
left = null ; | ||
right = null ; | ||
data = newData; | ||
} | ||
} | ||
|
||
public NewBinaryTree() { | ||
root = null ; | ||
} | ||
|
||
public void insert( int data) { | ||
root = insert( root , data); | ||
} | ||
|
||
private Node insert(Node node, int data) { | ||
if (node== null ) { | ||
node = new Node(data); | ||
} | ||
else { | ||
if (data <= node. data ) { | ||
node. left = insert(node. left , data); | ||
} | ||
else { | ||
node. right = insert(node. right , data); | ||
} | ||
} | ||
|
||
return (node); // in any case, return the new pointer to the caller | ||
} | ||
|
||
public void buildTree( int [] data){ | ||
for ( int i=0;i<data.length ;i++){ | ||
insert(data[i]); | ||
} | ||
} | ||
|
||
public void printTree() { | ||
printTree( root ); | ||
System. out .println(); | ||
} | ||
|
||
private void printTree(Node node) { | ||
if (node == null ) return ; | ||
// left, node itself, right | ||
printTree(node. left ); | ||
System. out .print(node. data + " " ); | ||
printTree(node. right ); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
group13/1392221554/lesson01/src/com/tiaozaoj/NewIterator.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.tiaozaoj; | ||
|
||
public interface NewIterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
public void remove(); | ||
} |
91 changes: 91 additions & 0 deletions
91
group13/1392221554/lesson01/src/com/tiaozaoj/NewLinkedList.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,91 @@ | ||
package com.tiaozaoj; | ||
|
||
public class NewLinkedList { | ||
|
||
private Node head; | ||
private int size = 0; | ||
|
||
//构造方法 | ||
public NewLinkedList(){ | ||
//初始化的时候,申请一个头结点,不存储数据 | ||
head = new Node("0x666"); | ||
head.next = null; | ||
} | ||
|
||
public void add(Object o){ | ||
Node newNode = new Node(o); | ||
head.next = newNode; | ||
newNode = head; | ||
this.size++; | ||
} | ||
|
||
private void verifyIndex(int index){ | ||
try{ | ||
if(index<0 || index>size) | ||
throw new Exception("越界异常"); | ||
}catch(Exception e){ | ||
e.printStackTrace(); | ||
return; | ||
} | ||
} | ||
|
||
public void add(int index,Object o){ | ||
this.verifyIndex(index); | ||
int j = -1; | ||
Node newNode = new Node(o); | ||
//遍历结点 | ||
for(Node p = head.next;p.next != null;p = p.next){ | ||
if((index) == j+1){ | ||
Node q = p.next; | ||
p.next = newNode; | ||
newNode.next = q; | ||
break; | ||
} | ||
j++; | ||
} | ||
this.size++; | ||
} | ||
|
||
public Object get(int index){ | ||
this.verifyIndex(index); | ||
int j = 0; | ||
//遍历结点 | ||
Node p = head.next; | ||
for(;p.next != null;p = p.next){ | ||
if((index) == j){ | ||
break; | ||
} | ||
j++; | ||
} | ||
return p; | ||
} | ||
|
||
public Object remove(int index){ | ||
this.verifyIndex(index); | ||
int j = -1; | ||
//遍历结点 | ||
Node p = head.next; | ||
for(;p.next != null;p = p.next){ | ||
if((index) == j+1){ | ||
break; | ||
} | ||
j++; | ||
} | ||
Node toRemoveNode = p.next; | ||
p.next = toRemoveNode.next; | ||
return toRemoveNode; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
private static class Node{ | ||
public Object data; | ||
public Node next; | ||
|
||
public Node(Object o){ | ||
this.data = o; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
group13/1392221554/lesson01/src/com/tiaozaoj/NewQueue.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,24 @@ | ||
package com.tiaozaoj; | ||
|
||
public class NewQueue { | ||
NewLinkedList linkedList = new NewLinkedList(); | ||
private int size = 0; | ||
|
||
public void enQueue(Object o){ | ||
linkedList.add(o); | ||
} | ||
|
||
public Object deQueue(Object o){ | ||
Object toDel = linkedList.get(0); | ||
linkedList.remove(0); | ||
return toDel; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return linkedList.size()>0?false:true; | ||
} | ||
|
||
public int size(){ | ||
return linkedList.size(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
group13/1392221554/lesson01/src/com/tiaozaoj/NewStack.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,24 @@ | ||
package com.tiaozaoj; | ||
|
||
public class NewStack { | ||
private NewArrayList elementData = new NewArrayList(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
Object o = elementData.get(elementData.size()); | ||
elementData.remove(elementData.size()); | ||
return o; | ||
} | ||
|
||
//Ö»»ñÈ¡Õ»¶¥ÔªËØ | ||
public Object peek(){ | ||
return elementData.get(elementData.size()); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return elementData.size()>0?false:true; | ||
} | ||
} |
Oops, something went wrong.