-
Notifications
You must be signed in to change notification settings - Fork 641
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 Wrecksoul/master
First homework,at 17/2/26
- Loading branch information
Showing
11 changed files
with
417 additions
and
1 deletion.
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 @@ | ||
/.metadata/ | ||
/RemoteSystemsTempFiles/ | ||
|
||
/JavaUtil/.settings/ | ||
|
||
.classpath | ||
.project |
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 @@ | ||
/target/ |
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,27 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.coding.basic</groupId> | ||
<artifactId>JavaUtil</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.7</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
100 changes: 100 additions & 0 deletions
100
group17/82427129/JavaUtil/src/main/java/com/coding/basic/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,100 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData; | ||
|
||
private static Object[] EMPTY_ELEMENTDATA = {}; | ||
|
||
private static int INITIALCAPACITY = 10; | ||
|
||
public ArrayList(){ | ||
elementData = EMPTY_ELEMENTDATA; | ||
} | ||
|
||
public ArrayList(int initialCapacity){ | ||
elementData = new Object[INITIALCAPACITY]; | ||
} | ||
|
||
public void add(Object o){ | ||
ensureCapacity(size+1); | ||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o){ | ||
rangeCheckForAdd(index); | ||
|
||
ensureCapacity(size+1); | ||
System.arraycopy(elementData, index, elementData, index+1, size-index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object set(int index, Object o){ | ||
rangeCheck(index); | ||
|
||
Object oldValue = elementData[index]; | ||
elementData[index] = o; | ||
return oldValue; | ||
} | ||
|
||
public Object get(int index){ | ||
rangeCheck(index); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
rangeCheck(index); | ||
Object oldValue = elementData[index]; | ||
int movedLength = size - index - 1; | ||
if(movedLength > 0)//当要删除最后一个元素时,不需要移动数组,只需要把最后一个元素置null | ||
System.arraycopy(elementData, index+1, elementData, index, size-index-1); | ||
elementData[--size] = null; | ||
return oldValue; | ||
} | ||
|
||
private void rangeCheckForAdd(int index){ | ||
if( index > size || index<0 ){ | ||
throw new IndexOutOfBoundsException(outofIndex(index)); | ||
} | ||
} | ||
private void rangeCheck(int index){ | ||
if( index >= size || index < 0){ | ||
throw new IndexOutOfBoundsException(outofIndex(index)); | ||
} | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
private void ensureCapacity(int minCapacity){ | ||
if(elementData == EMPTY_ELEMENTDATA){ | ||
minCapacity = Math.max(minCapacity, INITIALCAPACITY);//针对addall首次增加的数量就比INITIALCAPACITY多 | ||
} | ||
if(minCapacity - elementData.length > 0){ | ||
grow(minCapacity); | ||
} | ||
} | ||
|
||
private void grow(int minCapcity){ | ||
int oldCapacity = elementData.length; | ||
int newCapcity = oldCapacity + (oldCapacity>>1); | ||
if(newCapcity - minCapcity < 0){ | ||
newCapcity = minCapcity; | ||
} | ||
elementData = Arrays.copyOf(elementData, newCapcity); | ||
} | ||
|
||
private String outofIndex(int index){ | ||
return "Index: "+index+", Size: "+size; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
group17/82427129/JavaUtil/src/main/java/com/coding/basic/BinaryTreeNode.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,32 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
return null; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
group17/82427129/JavaUtil/src/main/java/com/coding/basic/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,7 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
180 changes: 180 additions & 0 deletions
180
group17/82427129/JavaUtil/src/main/java/com/coding/basic/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,180 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class LinkedList implements List { | ||
private int size = 0; | ||
|
||
private Node first; | ||
|
||
private Node last; | ||
|
||
public void add(Object o){ | ||
add(size,o); | ||
} | ||
public void add(int index , Object o){ | ||
rangeCheck(index); | ||
|
||
if(index == size){ | ||
linkLast(o); | ||
}else{ | ||
linkBefore(o, indexOf(index)); | ||
} | ||
} | ||
private void linkBefore(Object o ,Node succ){ | ||
final Node prev = succ.prev; | ||
final Node newNode = new Node(prev, o, succ); | ||
succ.prev = newNode; | ||
if(prev == null){ | ||
first = newNode; | ||
}else{ | ||
prev.next = newNode; | ||
} | ||
size++; | ||
} | ||
private void linkLast(Object o){ | ||
final Node succ = last; | ||
final Node newNode = new Node(succ, o, null); | ||
last = newNode; | ||
if(succ == null){ | ||
first = newNode; | ||
}else{ | ||
succ.next = newNode; | ||
} | ||
size++; | ||
} | ||
private void rangeCheck(int index) { | ||
if(index > size|| index < 0 ) | ||
throw new IndexOutOfBoundsException("Size"+size+":index"+index); | ||
} | ||
private void elementIndexCheck(int index){ | ||
if(index >=size||index < 0) | ||
throw new IndexOutOfBoundsException("Size"+size+":index"+index); | ||
} | ||
/** | ||
* 获取“下标”为index的值, | ||
* index为size时返回null | ||
* @param index | ||
* @return | ||
*/ | ||
private Node indexOf(int index){ | ||
if(index < (this.size>>1) ){ | ||
Node x = first; | ||
for (int i = 0; i < index; i++) { | ||
x = x.next; | ||
} | ||
return x; | ||
}else{ | ||
Node x = last; | ||
for (int i = this.size-1; i > index; i--) { | ||
x = x.prev; | ||
} | ||
return x; | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
elementIndexCheck(index); | ||
|
||
return indexOf(index); | ||
} | ||
public Object remove(int index){ | ||
elementIndexCheck(index); | ||
|
||
if(index == 0){ | ||
return removeFirst(); | ||
}else if(index == size) { | ||
return removeLast(); | ||
}else{ | ||
return unlinkNode(indexOf(index)); | ||
} | ||
} | ||
|
||
private Object unlinkNode(Node node) { | ||
final Node next = node.next; | ||
final Node prev = node.prev; | ||
final Object element = node.data; | ||
if(next == null){ | ||
last = node; | ||
}else{ | ||
next.prev = node; | ||
node.next = next; | ||
} | ||
if(prev == null){ | ||
first = node; | ||
}else{ | ||
prev.next = node; | ||
node.prev = prev; | ||
} | ||
size--; | ||
node.data = null; | ||
|
||
return element; | ||
} | ||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
linkBefore(o, first); | ||
} | ||
|
||
public void addLast(Object o){ | ||
linkLast(o); | ||
} | ||
|
||
public Object removeFirst(){ | ||
if(first == null) | ||
throw new NoSuchElementException("first is null"); | ||
|
||
Object oldData = first.data; | ||
final Node next = first.next; | ||
first.data = null; | ||
first.next = null;//GC | ||
first = next; | ||
|
||
if(next == null){ | ||
last = null; | ||
}else{ | ||
next.prev = null; | ||
} | ||
size--; | ||
|
||
return oldData; | ||
} | ||
|
||
public Object removeLast(){ | ||
if(last == null) | ||
throw new NoSuchElementException("last is null"); | ||
|
||
Object oldData = last.data; | ||
final Node prev = last.prev; | ||
last.prev = null; | ||
last.data = null;//GC | ||
last = prev; | ||
|
||
if(prev == null){ | ||
first = null; | ||
}else{ | ||
prev.next = null; | ||
} | ||
size--; | ||
|
||
return oldData; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
Node prev; | ||
Node(Node prev,Object data,Node next){ | ||
this.data = data; | ||
this.next = next; | ||
this.prev = prev; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group17/82427129/JavaUtil/src/main/java/com/coding/basic/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 com.coding.basic; | ||
|
||
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(); | ||
} |
21 changes: 21 additions & 0 deletions
21
group17/82427129/JavaUtil/src/main/java/com/coding/basic/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,21 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
private LinkedList elementData = new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
elementData.addLast(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
return elementData.removeLast(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
} |
Oops, something went wrong.