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 superfish17/master
第一次作业
- Loading branch information
Showing
11 changed files
with
455 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,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>test</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> |
6 changes: 6 additions & 0 deletions
6
group14/352504906/test/.settings/org.eclipse.core.resources.prefs
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 @@ | ||
#Sun Feb 26 15:56:36 CST 2017 | ||
eclipse.preferences.version=1 | ||
encoding//src/com/coding/basic=UTF-8 | ||
encoding//src/com/coding/basic/SimpleArrayList.java=UTF-8 | ||
encoding/<project>=UTF-8 | ||
encoding/src=UTF-8 |
12 changes: 12 additions & 0 deletions
12
group14/352504906/test/.settings/org.eclipse.jdt.core.prefs
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,12 @@ | ||
#Sun Feb 26 15:31:53 CST 2017 | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
118 changes: 118 additions & 0 deletions
118
group14/352504906/test/src/com/coding/basic/SimpleArrayList.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,118 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
|
||
/** | ||
* @Description 简单实现ArrayList | ||
*/ | ||
public class SimpleArrayList implements SimpleList{ | ||
private int size = 0; | ||
private Object[] elementData = new Object[10]; | ||
/** | ||
* 插入元素o | ||
* @param o 待插入元素 | ||
*/ | ||
public void add(Object o){ | ||
//扩容 | ||
if(elementData.length < size + 1){ | ||
grow(size+1); | ||
} | ||
elementData[size++] = o; | ||
} | ||
/** | ||
* 数组扩容 | ||
* @param capacity 数组实际长度 | ||
*/ | ||
private void grow(int capacity) { | ||
int oldCapacity = elementData.length; | ||
int newCapacity = oldCapacity + (oldCapacity >> 1);//扩容50% | ||
if(capacity > newCapacity){ | ||
newCapacity = capacity; | ||
} | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
/** | ||
* 在指定索引初插入元素 | ||
* @param index 索引 | ||
* @param o 待插入元素 | ||
*/ | ||
public void add(int index,Object o){ | ||
rangeCheckForAdd(index); | ||
if(elementData.length<size+1){ | ||
//扩容 | ||
grow(size+1); | ||
} | ||
elementData[index] = o; | ||
size++; | ||
} | ||
/** | ||
* 插入位置越界处理 | ||
* @param index 索引 | ||
*/ | ||
private void rangeCheckForAdd(int index) { | ||
if(index > size || index <0){ | ||
throw new IndexOutOfBoundsException("数组越界异常"); | ||
} | ||
} | ||
/** | ||
* 索引越界处理 | ||
* @param index 索引 | ||
*/ | ||
private void rangeCheck(int index) { | ||
if(index >= size || index <0) | ||
throw new IndexOutOfBoundsException("数组越界异常"); | ||
} | ||
/** | ||
* 移除该索引处元素 | ||
* @param index 索引位置 | ||
* @return 移除元素 | ||
*/ | ||
public Object remove(int index){ | ||
rangeCheck(index); | ||
Object oldObject = elementData[index]; | ||
if(size > index +1){ | ||
System.arraycopy(elementData, index +1 , elementData, index, size-index-1); | ||
} | ||
elementData[--size] = null; | ||
return oldObject; | ||
} | ||
/** | ||
* 返回集合长度 | ||
* @return 长度 | ||
*/ | ||
public int size(){ | ||
return this.size; | ||
} | ||
/** | ||
* 返回指定索引元素 | ||
* @param index 索引 | ||
* @return Object 元素 | ||
*/ | ||
public Object get(int index){ | ||
rangeCheck(index); | ||
return elementData[index]; | ||
} | ||
private class ArrayListIterator implements SimpleIterator{ | ||
SimpleArrayList l = null; | ||
private int iteratorIndex = 0; | ||
|
||
private ArrayListIterator(SimpleArrayList arrayList){ | ||
this.l = arrayList; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return iteratorIndex < size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (iteratorIndex >= size) { | ||
throw new RuntimeException("数组越界异常"); | ||
} | ||
return elementData[iteratorIndex++]; | ||
} | ||
|
||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
group14/352504906/test/src/com/coding/basic/SimpleIterator.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,11 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
*简单迭代器接口 | ||
* | ||
*/ | ||
public interface SimpleIterator { | ||
public Object next(); | ||
public boolean hasNext(); | ||
|
||
} |
126 changes: 126 additions & 0 deletions
126
group14/352504906/test/src/com/coding/basic/SimpleLinkedList.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,126 @@ | ||
package com.coding.basic; | ||
/** | ||
* @Description 简单实现linkedList | ||
*/ | ||
public class SimpleLinkedList implements SimpleList{ | ||
private Node head; | ||
|
||
private int size = 0; | ||
/** | ||
* 返回集合长度 | ||
* @return 长度 | ||
*/ | ||
public int size(){ | ||
return this.size; | ||
} | ||
/** | ||
* 返回指定索引出元素 | ||
* @param index 索引 | ||
* @return Object 元素 | ||
*/ | ||
public Object get(int index){ | ||
rangeCheck(index); | ||
Node current = head; | ||
for(int i =0;i<index;i++){ | ||
current = current.next; | ||
} | ||
return current.o; | ||
} | ||
/** | ||
* 插入元素o,默认是往头部插入 | ||
* @param o 待插入元素 | ||
*/ | ||
public void add(Object o){ | ||
addFirst(o); | ||
} | ||
/** | ||
* 在指定索引处插入元素 | ||
* @param index 索引 | ||
* @param o 待插入元素 | ||
*/ | ||
public void add(int index,Object o){ | ||
rangeCheck(index); | ||
if(index == 0){ | ||
addFirst(0); | ||
return; | ||
} | ||
if(index == size){ | ||
addLast(o); | ||
return; | ||
} | ||
Node newNode = new Node(o); | ||
Node current = head; | ||
for(int i = 0;i < index -1;i++){ | ||
current = current.next; | ||
} | ||
newNode.next = current.next; | ||
current.next = newNode; | ||
size ++; | ||
|
||
} | ||
/** | ||
* 索引越界处理 | ||
* @param index 索引 | ||
*/ | ||
private void rangeCheck(int index) { | ||
if(index > size || index < 0){ | ||
throw new RuntimeException("找不到该节点"); | ||
} | ||
} | ||
//头插法 | ||
public void addFirst(Object o){ | ||
Node newNode = new Node(o); | ||
newNode.next = head; | ||
head = newNode; | ||
size++; | ||
} | ||
//尾插法 | ||
public void addLast(Object o){ | ||
Node newNode = new Node(o); | ||
Node current = head;//设定一个当前节点,便于遍历 | ||
while(current.next!=null){ | ||
current = current.next; | ||
} | ||
if(current.next == null)//尾节点 | ||
{ | ||
current.next = newNode; | ||
size++; | ||
} | ||
} | ||
/** | ||
* 移除该索引处元素 | ||
* @param index 索引位置 | ||
* @return 移除元素 | ||
*/ | ||
public Object remove(int index){ | ||
rangeCheck(index+1); | ||
Node current = head; | ||
if(index == 0){//头部删除 | ||
Object removeObj = head.o; | ||
head = head.next; | ||
size --; | ||
return removeObj; | ||
} | ||
int i; | ||
for(i=0;i<index-1;i++){ | ||
current = current.next; | ||
} | ||
Object removeObject = get(i); | ||
if(index == size-1){//尾部删除 | ||
current.next = null; | ||
size--; | ||
return removeObject; | ||
} | ||
current.next = current.next.next; | ||
size--; | ||
return removeObject; | ||
} | ||
//内部类,声明为static 与实例无关 | ||
private static class Node{ | ||
private Object o;//单链表的数据域 | ||
private Node next;//单链表的指针域 | ||
private Node(Object o){ | ||
this.o = o; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
group14/352504906/test/src/com/coding/basic/SimpleList.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,14 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
* 简单list接口 | ||
* | ||
*/ | ||
public interface SimpleList { | ||
public void add(Object o); | ||
public void add(int index,Object o ); | ||
public Object get(int index); | ||
public Object remove(int index); | ||
public int size(); | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
group14/352504906/test/src/com/coding/basic/SimpleQueue.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,68 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @Description 简单实现队列 | ||
*/ | ||
public class SimpleQueue { | ||
private Object[] elementData = new Object[10]; | ||
private int size; | ||
|
||
/** | ||
* 往队尾添加新的元素 | ||
* @param o 要添加的新元素 | ||
*/ | ||
public void enQueue(Object o){ | ||
if(elementData.length < size +1){ | ||
//扩容 | ||
grow(size+1); | ||
} | ||
elementData[size++] = o; | ||
} | ||
/** | ||
* 数组扩容 | ||
* @param capacity 数组实际长度 | ||
*/ | ||
private void grow(int capacity) { | ||
int oldCapacity = elementData.length; | ||
int newCapacity = oldCapacity * 2; | ||
if(capacity > newCapacity){ | ||
newCapacity = capacity; | ||
} | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
/** | ||
* 移除并返回队列头部元素 | ||
* @return 队列头部元素 | ||
*/ | ||
public Object deQueue(){ | ||
Object o = elementData[size-1]; | ||
removeElement(0); | ||
return o; | ||
} | ||
/** | ||
* 删除指定索引处元素 | ||
* @param index 索引 | ||
*/ | ||
private void removeElement(int index) { | ||
if(index >= 0){ | ||
System.arraycopy(elementData, index+1, elementData, 0, size-index-1); | ||
elementData[--size] = null; | ||
} | ||
} | ||
/** | ||
* 判断队列是否为空 | ||
* @return Boolean | ||
*/ | ||
public boolean isEmpty(){ | ||
return size==0; | ||
} | ||
/** | ||
* 返回队列长度 | ||
* @return int 队列长度 | ||
*/ | ||
public int size(){ | ||
return this.size; | ||
} | ||
} |
Oops, something went wrong.