forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #2 from BaymaxGithub/master
Master
- Loading branch information
Showing
10 changed files
with
316 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.8"/> | ||
<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>20170224-01-ArrayList</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> |
11 changes: 11 additions & 0 deletions
11
group07/1280157271/20170224-01-ArrayList/.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,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
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.8 |
9 changes: 9 additions & 0 deletions
9
group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.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,9 @@ | ||
package firstHomework.fan; | ||
|
||
public interface List { | ||
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(); | ||
} |
60 changes: 60 additions & 0 deletions
60
group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.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,60 @@ | ||
package firstHomework.fan; | ||
|
||
public class myArrayList implements List { | ||
|
||
private int size = 0; | ||
private int initLength=10; | ||
private Object[] elementData = new Object[initLength]; | ||
|
||
public void add(Object o){ | ||
ensureCapacity(size+1); | ||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o){ | ||
ensureCapacity(size+1); | ||
if(index<0||index>size){ | ||
System.out.println("index应该在0-size之间!!!"); | ||
}else{ | ||
System.arraycopy(elementData, index, elementData, index+1, size-index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
if(index<0||index>size){ | ||
System.out.println("index应该在0-size之间!!!"); | ||
}else{ | ||
return elementData[index]; | ||
} | ||
return null; | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index<0||index>size){ | ||
System.out.println("index应该在0-size之间!!!"); | ||
return null; | ||
} | ||
Object obj = get(index); | ||
System.arraycopy(elementData, index+1, elementData, index, size-index-1); | ||
size--; | ||
return obj; | ||
} | ||
|
||
public int size(){ | ||
return this.size; | ||
} | ||
|
||
|
||
|
||
public void ensureCapacity(int x){ | ||
int oldCapacity = elementData.length; | ||
if(x>oldCapacity){ | ||
Object newEle[] = new Object[oldCapacity+initLength]; | ||
System.arraycopy(elementData, 0, newEle, 0, size); | ||
elementData = newEle; | ||
} | ||
} | ||
|
||
} |
129 changes: 129 additions & 0 deletions
129
group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.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,129 @@ | ||
package firstHomework.fan; | ||
|
||
|
||
|
||
public class myLinkedList implements List { | ||
private static class Node{//链表的节点结构体 | ||
Object data;//数据域 | ||
Node next; //Node引用,相当于指针,指向下一个节点 | ||
|
||
Node(Object e, Node next) { | ||
this.data = e; | ||
this.next = next; | ||
} | ||
} | ||
|
||
private Node head,last=null;//分别指向第一个和最后一个节点 | ||
private int size; | ||
|
||
|
||
public void add(Object o){//在尾部添加,就相当于添加尾节点 | ||
creatLastNode(o); | ||
} | ||
public void add(int index , Object o){//在index前面插入 | ||
if(index == 0){//说明传过来的位置为0,那么就添加头结点 | ||
createFirstNode(o); | ||
}else{//就去找到指定位置 | ||
Node indexBeforeNode = getNode(index-1);//这里返回的是index的前一个节点 | ||
Node newIndex =new Node(o,indexBeforeNode.next) ;//x新节点保存indexBefore的指向 | ||
indexBeforeNode.next = newIndex; | ||
size++; | ||
} | ||
} | ||
public Object get(int index){ | ||
return getNode(index).data;//返回的是节点中的数据对象 | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index==0){//移除头结点 | ||
removeFirst(); | ||
}else{//找到指定节点的前一个节点 | ||
Node removeNode = getNode(index-1); | ||
removeNode.next = removeNode.next.next;//移除了index | ||
size--; | ||
return removeNode.next.data;//返回移除节点的数据对象 | ||
} | ||
return null; | ||
} | ||
|
||
public int size(){ | ||
return this.size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
createFirstNode(o); | ||
} | ||
public void addLast(Object o){ | ||
creatLastNode(o); | ||
} | ||
public Object removeFirst(){ | ||
if(size>0){//列表不为空,即一定有头 | ||
Node removeHead = head; | ||
head = head.next; | ||
size--; | ||
return removeHead.data;//返回头结点的数据对象 | ||
}else{ | ||
System.out.println("链表为空!"); | ||
} | ||
return null; | ||
} | ||
public Object removeLast(){ | ||
if(size>0){ | ||
Node removeLastBefore = getNode(size-2);//找到last节点的上一个节点 | ||
Object returnObj = removeLastBefore.next.data; | ||
removeLastBefore.next = null; | ||
last = removeLastBefore; | ||
size--; | ||
return returnObj; | ||
}else{ | ||
System.out.println("链表为空!"); | ||
} | ||
return null; | ||
} | ||
|
||
/* | ||
* 添加头结点 | ||
* */ | ||
private void createFirstNode(Object e){ | ||
Node oldHead = head; | ||
Node newHead = new Node(e,oldHead);//传进来的节点作为head节点的前一节点 | ||
head = newHead;//不管空不空,head要指向新的头节点 | ||
if(head == null){//如果链表为空,head和last都指向新节点(不为空last就不能乱赋值,因为不确定链表最后在哪) | ||
last = newHead; | ||
}else{//头结点已经存在,新节点作为头结点,原head变第二,last还是指向它的last | ||
newHead.next = head; | ||
} | ||
size++; | ||
} | ||
/* | ||
* 添加尾结点 | ||
* */ | ||
private void creatLastNode(Object e){ | ||
Node oldLast = last; | ||
Node newLast = new Node(e,null);//新的尾节点下一个节点为空 | ||
last = newLast;//不管空不空,last是要指向新的尾节点 | ||
if(head == null){//链表为空 | ||
head = newLast; | ||
}else{ | ||
oldLast.next = newLast; | ||
} | ||
size++; | ||
} | ||
/* | ||
* 寻找指定结点 | ||
* */ | ||
private Node getNode(int index){ | ||
if(index<0||index>=size){ | ||
System.out.println("index越界!!!"); | ||
}else{ | ||
Node node=head; | ||
while(index != 0){ | ||
node = node.next; | ||
index--; | ||
} | ||
return node; | ||
} | ||
return null; | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.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,51 @@ | ||
package firstHomework.fan; | ||
|
||
public class myQueue { | ||
private int iniLength = 10; | ||
private Object[] array = new Object[iniLength]; | ||
private int size = 0; | ||
|
||
public void enQueue(Object o){ | ||
if(size>=array.length){//需要扩容 | ||
Object[] newArray = new Object[iniLength+array.length];//array数组原长再加上10 | ||
System.arraycopy(array, 0, newArray, 0, size); | ||
array = newArray; | ||
} | ||
array[size++] = o; | ||
} | ||
|
||
public Object deQueue(){//移除第一个元素,后面的元素整体向前位移 | ||
Object deQueue = array[0]; | ||
System.arraycopy(array, 1, array, 0, size-1); | ||
size--; | ||
return deQueue; | ||
|
||
} | ||
|
||
public boolean isEmpty(){ | ||
return size==0; | ||
} | ||
|
||
public int size(){ | ||
return this.size; | ||
} | ||
public static void main(String[] args) { | ||
myQueue queue = new myQueue(); | ||
queue.enQueue("A"); | ||
queue.enQueue("B"); | ||
queue.enQueue("C"); | ||
|
||
queue.enQueue("D"); | ||
queue.enQueue("E"); | ||
queue.enQueue("F"); | ||
queue.enQueue("G"); | ||
|
||
while(!queue.isEmpty()){ | ||
System.out.println(queue.deQueue());//出队 | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
} |
31 changes: 31 additions & 0 deletions
31
group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.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,31 @@ | ||
package firstHomework.fan; | ||
|
||
|
||
|
||
public class myStack { | ||
private myArrayList array = new myArrayList();//myArrayList对象里有个动态数组 | ||
|
||
public void push(Object o){ //入栈 | ||
//长度不够时myArrayList会自己扩容 | ||
array.add(o);//新对象放在数组后面 | ||
} | ||
|
||
public Object pop(){//出栈,即数组尾上的元素, 还要删除他 | ||
Object pop = array.get(array.size()-1);//下标要比size小1 | ||
array.remove(array.size()-1); | ||
return pop; | ||
} | ||
|
||
public Object peek(){//只是弹出栈顶的值,不删除 | ||
return array.get(array.size()-1); | ||
} | ||
public boolean isEmpty(){ | ||
return array.size()==0; | ||
} | ||
public int size(){ | ||
return array.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 @@ | ||
http://blog.csdn.net/stromcloud/article/details/56678442 |