forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #11 from wxyjwxyj/master
第一周作业
- Loading branch information
Showing
13 changed files
with
759 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>homework</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> |
15 changes: 15 additions & 0 deletions
15
group06/1730798243/src/com/coding/basic/first/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,15 @@ | ||
package com.coding.basic.first; | ||
|
||
public interface Iterator { | ||
/** | ||
* 查看是否有下一个元素 | ||
* @return 有的话返回true,否则返回false | ||
*/ | ||
public boolean hasNext(); | ||
/** | ||
* 获得下一个元素,也就是当前元素 | ||
* @return 获取当前位置的元素 | ||
*/ | ||
public Object 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,37 @@ | ||
package com.coding.basic.first; | ||
/** | ||
* 数组的接口 | ||
* @author zap | ||
* | ||
*/ | ||
|
||
public interface List { | ||
/** | ||
* 向数组中添加一个元素 | ||
* @param o 添加的元素 | ||
*/ | ||
public void add(Object o); | ||
/** | ||
* 向数组中某一个位置添加一个元素 | ||
* @param index 添加元素的位置 | ||
* @param o 添加的元素 | ||
*/ | ||
public void add(int index,Object o); | ||
/** | ||
* 从数组中根据位置获取一个元素 | ||
* @param index 想要获取的元素的位置 | ||
* @return 想获取的元素 | ||
*/ | ||
public Object get(int index); | ||
/** | ||
* 根据位置移除数组中的一个元素 | ||
* @param index 被移除元素的位置 | ||
* @return 被移除的元素 | ||
*/ | ||
public Object remove(int index); | ||
/** | ||
* 获取数组的长度 | ||
* @return 返回数组的长度 | ||
*/ | ||
public int size(); | ||
} |
87 changes: 87 additions & 0 deletions
87
group06/1730798243/src/com/coding/basic/first/impl/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,87 @@ | ||
package com.coding.basic.first.impl; | ||
|
||
import com.coding.basic.first.List; | ||
|
||
/** | ||
* 实现一个ArrayList | ||
* @author zap | ||
* | ||
*/ | ||
|
||
public class ArrayList implements List{ | ||
|
||
private int size; | ||
private Object[] elementData = null; | ||
private static final int DEFAULT_SIZE=100; | ||
|
||
public ArrayList() { | ||
this(DEFAULT_SIZE); | ||
} | ||
|
||
public ArrayList(int size) { | ||
if(size < 0) { | ||
System.out.println("size 必须大于0"); | ||
} else { | ||
this.elementData = new Object[size]; | ||
} | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
judgeSize(size+1); | ||
elementData[size++] = o; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
judgeIndexRangge(index); | ||
System.arraycopy(elementData, index, elementData, index + 1, | ||
size - index);//整体往后挪移--当前往后挪 | ||
elementData[index] = o; | ||
size++; | ||
|
||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
judgeIndexRangge(index); | ||
return elementData[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
judgeIndexRangge(index); | ||
Object e = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, | ||
size - index - 1);//整体往前挪移--后一位往前挪 | ||
size--; | ||
return e; | ||
} | ||
|
||
private void judgeIndexRangge(int index){ | ||
|
||
if (index > size || index < 0) | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
|
||
private String outOfBoundsMsg(int index) { | ||
return "Index: "+index+", Size: "+size; | ||
} | ||
|
||
|
||
private void judgeSize(int size) { | ||
if(size > elementData.length){ | ||
Object[] newarr = new Object[elementData.length + DEFAULT_SIZE]; | ||
System.arraycopy(elementData, 0, newarr, 0, elementData.length); | ||
this.elementData = newarr; | ||
} | ||
} | ||
|
||
|
||
|
||
} |
143 changes: 143 additions & 0 deletions
143
group06/1730798243/src/com/coding/basic/first/impl/LinkedList.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,143 @@ | ||
package com.coding.basic.first.impl; | ||
|
||
import com.coding.basic.first.List; | ||
|
||
/** | ||
* 链表 | ||
* @author zap | ||
* LinkedList | ||
*/ | ||
public class LinkedList implements List { | ||
|
||
|
||
|
||
private Node head = new Node(); | ||
private int size ;// | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
public Node() { | ||
} | ||
|
||
public Node(Object data) { | ||
this.data = data; | ||
} | ||
|
||
} | ||
|
||
|
||
@Override | ||
public void add(Object o) { | ||
|
||
if(size==0){ | ||
Node node = new Node(o); | ||
head = node; | ||
// head = tail = node; | ||
}else{ | ||
Node current = getCurrentNode(size); | ||
Node node = new Node(o); | ||
current.next = node; | ||
// tail = node; | ||
} | ||
size ++; | ||
|
||
|
||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
judgeIndexRangge(index); | ||
if(size == 0 ){ | ||
Node node = new Node(o); | ||
head = node; | ||
// head = tail = node; | ||
}else if(index == 0){ | ||
Node node = new Node(o); | ||
node.next = head; | ||
head = node; | ||
}else{ | ||
Node prev = getCurrentNode(index); | ||
Node temp = prev.next; | ||
Node node = new Node(o); | ||
node.next = temp; | ||
prev.next = node; | ||
} | ||
size ++; | ||
|
||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
judgeGetIndexRangge(index); | ||
Node current = getCurrentNode(index + 1); | ||
return current.data; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
judgeIndexRangge(index);//下标 | ||
Object obj = null; | ||
if(index == 0){ | ||
Node node = head.next; | ||
obj = head.data; | ||
head = node; | ||
}else{ | ||
Node prev = getCurrentNode(index); | ||
Node temp = prev.next; | ||
Node after =temp.next ; | ||
prev.next = after; | ||
obj = temp.data; | ||
} | ||
size -- ; | ||
return obj; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
|
||
|
||
private void judgeIndexRangge(int index){ | ||
|
||
if (index > size || index < 0) | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
|
||
private void judgeGetIndexRangge(int index){ | ||
|
||
if (index >= size || index < 0) | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
|
||
private String outOfBoundsMsg(int index) { | ||
return "Index: "+index+", Size: "+size; | ||
} | ||
|
||
private Node getCurrentNode(int index){ | ||
|
||
Node temp = head; | ||
Node prev = head; | ||
for(int i = 0 ;i < index;i++){//找到该个元素 | ||
prev = temp;// | ||
temp = temp.next;// | ||
} | ||
return prev; | ||
|
||
} | ||
|
||
|
||
public void addLast(Object o){ | ||
add(o); | ||
} | ||
|
||
public Object removeFirst(){ | ||
Object o = remove(0); | ||
return o; | ||
} | ||
|
||
|
||
} |
46 changes: 46 additions & 0 deletions
46
group06/1730798243/src/com/coding/basic/first/impl/Queue.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,46 @@ | ||
package com.coding.basic.first.impl; | ||
|
||
/** | ||
* 基本数据结构-队列 | ||
* @author zap | ||
* | ||
*/ | ||
|
||
public class Queue { | ||
|
||
private LinkedList linkedList = new LinkedList(); | ||
|
||
/** | ||
* 入队操作 | ||
* @param o 入队的元素 | ||
*/ | ||
public void enQueue(Object o){ | ||
linkedList.addLast(o); | ||
} | ||
|
||
/** | ||
* 出队操作 | ||
* @return 返回出队的元素 | ||
*/ | ||
public Object deQueue(){ | ||
if(!isEmpty()) | ||
return linkedList.removeFirst(); | ||
return null; | ||
} | ||
|
||
/** | ||
* 判断队列是否为空 | ||
* @return 为空返回true,否则返回false | ||
*/ | ||
public boolean isEmpty(){ | ||
return linkedList.size() == 0 ? true : false; | ||
} | ||
|
||
/** | ||
* 返回队列的长度 | ||
* @return | ||
*/ | ||
public int size(){ | ||
return linkedList.size(); | ||
} | ||
} |
Oops, something went wrong.