forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 8
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 dongqihust/master
完成程序作业
- Loading branch information
Showing
11 changed files
with
515 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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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 @@ | ||
/bin/ |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>DongqiHomeWork</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
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,109 @@ | ||
package com.dong.week1; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
/** | ||
* 先判断数组是否已经满了,如果已经满了做扩容处理 | ||
*/ | ||
if(elementData.length==size){ | ||
elementData = Arrays.copyOf(elementData, elementData.length*2+1); | ||
} | ||
elementData[size++]=o; | ||
|
||
} | ||
public void add(int index, Object o){ | ||
if(index >size || index < 0 ){ | ||
throw new ArrayIndexOutOfBoundsException("数组越界,当前数组长度是"+size+",但是请求元素的索引是:"+index); | ||
} | ||
/** | ||
* 数组已经满了,做扩容 | ||
*/ | ||
if(size==elementData.length){ | ||
elementData = Arrays.copyOf(elementData, elementData.length*2+1); | ||
} | ||
Object[] elementDataClone = elementData.clone(); | ||
System.arraycopy(elementData, index, elementDataClone, index+1, size-index); | ||
elementDataClone[index++]=o; | ||
size++; | ||
elementData = elementDataClone; | ||
} | ||
|
||
|
||
public Object get(int index){ | ||
if(index >=size || index < 0 ){ | ||
throw new ArrayIndexOutOfBoundsException("数组越界,当前数组长度是"+size+",但是请求元素的索引是:"+index); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index >=size || index < 0 ){ | ||
throw new ArrayIndexOutOfBoundsException("数组越界,当前数组长度是"+size+",但是删除元素的索引是:"+index); | ||
} | ||
elementData[index]=null; | ||
size--; | ||
Object[] elementDataClone = elementData.clone(); | ||
System.arraycopy(elementData, index+1, elementDataClone, index, size-index-1); | ||
elementData = elementDataClone; | ||
return elementData[index]; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new IteratorArrayList(this); | ||
} | ||
@Override | ||
public String toString() { | ||
return "ArrayList [size=" + size + ", elementData=" + Arrays.toString(elementData) + "]"; | ||
} | ||
|
||
|
||
private class IteratorArrayList implements Iterator{ | ||
|
||
private ArrayList arrayList; | ||
private int index=0; | ||
|
||
|
||
public IteratorArrayList(ArrayList arrayList) { | ||
super(); | ||
this.arrayList = arrayList; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
// TODO Auto-generated method stub | ||
return this.arrayList.size() >index; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
// TODO Auto-generated method stub | ||
if(hasNext()){ | ||
return this.arrayList.get(index++); | ||
} | ||
return null; | ||
} | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList arrayList= new ArrayList(); | ||
|
||
Iterator iterator= arrayList.iterator(); | ||
while(iterator.hasNext()){ | ||
System.out.println(iterator.next()); | ||
} | ||
} | ||
|
||
|
||
} |
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,63 @@ | ||
package com.dong.week1; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
public class ArrayListTest { | ||
|
||
//@Test | ||
public void testAddObject() { | ||
ArrayList arrayList = new ArrayList(); | ||
for(int i=0;i<=200;i++){ | ||
arrayList.add(i); | ||
} | ||
System.out.println(arrayList); | ||
} | ||
|
||
//@Test | ||
public void testAddIntObject() { | ||
ArrayList arrayList = new ArrayList(); | ||
for(int i=0;i<=2;i++){ | ||
arrayList.add(i); | ||
} | ||
arrayList.add(1,100); | ||
arrayList.add(1, 1000); | ||
System.out.println(arrayList); | ||
} | ||
|
||
// @Test | ||
public void testGet() { | ||
ArrayList arrayList = new ArrayList(); | ||
for(int i=0;i<=200;i++){ | ||
arrayList.add(i); | ||
} | ||
//System.out.println(arrayList.get(-1)); | ||
//System.out.println(arrayList.get(50)); | ||
System.out.println(arrayList.get(200)); | ||
//System.out.println(arrayList.get(300)); | ||
|
||
|
||
} | ||
|
||
@Test | ||
public void testRemove() { | ||
ArrayList arrayList = new ArrayList(); | ||
for(int i=0;i<=10;i++){ | ||
arrayList.add(i); | ||
} | ||
arrayList.remove(1); | ||
arrayList.remove(1); | ||
System.out.println(arrayList); | ||
} | ||
|
||
// @Test | ||
public void testSize() { | ||
ArrayList arrayList = new ArrayList(); | ||
for(int i=0;i<=10;i++){ | ||
arrayList.add(i); | ||
} | ||
System.out.println(arrayList.size()); | ||
} | ||
|
||
} |
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,39 @@ | ||
package com.dong.week1; | ||
|
||
public class BinaryTreeNode { | ||
private TreeNode node; | ||
|
||
private static class TreeNode{ | ||
private int key=0; | ||
private TreeNode leftChild=null; | ||
private TreeNode rightChild=null; | ||
|
||
public TreeNode(){} | ||
|
||
/** | ||
* @param key ²ãÐò±àÂë | ||
* @param data Êý¾ÝÓò | ||
*/ | ||
public TreeNode(int key){ | ||
this.key=key; | ||
this.leftChild=null; | ||
this.rightChild=null; | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
public TreeNode insert(TreeNode o){ | ||
if(node == null){ | ||
return o; | ||
} | ||
if(node.key > o.key){ | ||
return insert(o.leftChild); | ||
}else{ | ||
return insert(node.leftChild); | ||
} | ||
} | ||
|
||
} |
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.dong.week1; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
Oops, something went wrong.