forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 20
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 #10 from devinyeen/master
284999210 basic container pull
- Loading branch information
Showing
12 changed files
with
615 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"/> | ||
<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,3 @@ | ||
*.class | ||
/.metadata | ||
/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>284999210</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> |
Binary file not shown.
156 changes: 156 additions & 0 deletions
156
group06/284999210/src/com/coding/basic/container/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,156 @@ | ||
package com.coding.basic.container; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private Object[] elementData; | ||
private int size; | ||
private int capacity; | ||
private static final int DEFAULT_CAPACITY = 8; | ||
|
||
public ArrayList() { | ||
elementData = new Object[8]; | ||
size = 0; | ||
capacity = DEFAULT_CAPACITY; | ||
} | ||
|
||
public boolean add(Object element) { | ||
if (size == capacity) { | ||
Object[] tempArray = new Object[capacity]; | ||
for (int i = 0; i < size; i++) { | ||
tempArray[i] = elementData[i]; | ||
} | ||
elementData = new Object[capacity * 2]; | ||
for (int i = 0; i < size; i++) { | ||
elementData[i] = tempArray[i]; | ||
} | ||
elementData[capacity] = element; | ||
capacity = capacity * 2; | ||
} | ||
elementData[size++] = element; | ||
return true; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object element) { | ||
checkIndex(index); | ||
Object[] tempArray = new Object[capacity]; | ||
for (int i = 0; i < size; i++) { | ||
tempArray[i] = elementData[i]; | ||
} | ||
elementData = new Object[capacity * 2]; | ||
for (int i = 0; i < size; i++) { | ||
if (i < index) { | ||
elementData[i] = tempArray[i]; | ||
} else { | ||
elementData[i + 1] = tempArray[i]; | ||
} | ||
} | ||
elementData[index] = element; | ||
capacity = capacity * 2; | ||
size++; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Object remove(int index) { | ||
checkIndex(index); | ||
|
||
Object o = elementData[index]; | ||
for (int i = index; i < size; i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
elementData[size] = null; | ||
size = size - 1; | ||
return o; | ||
} | ||
|
||
private void checkIndex(int index) { | ||
if (index >= capacity) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
public Object set(int index, Object element) { | ||
checkIndex(index); | ||
|
||
Object o = elementData[index]; | ||
elementData[index] = element; | ||
return o; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Object get(int index) { | ||
checkIndex(index); | ||
|
||
return (Object) elementData[index]; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("["); | ||
for (int i = 0; i < size; i++) { | ||
if (i != size - 1) { | ||
sb.append(elementData[i] + ", "); | ||
} else { | ||
sb.append(elementData[i]); | ||
} | ||
} | ||
sb.append("]"); | ||
return sb.toString(); | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
@Override | ||
public Iterator iterator() { | ||
return new IteratorArrayList(); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object element) { | ||
if (element == null) | ||
return false; | ||
int findIndex = -1; | ||
for (int i = 0; i < size; i++) { | ||
if (elementData[i].equals(element)) { | ||
findIndex = i; | ||
break; | ||
} | ||
} | ||
|
||
for (int i = findIndex; i < size - 1; i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
elementData[size - 1] = null; | ||
size--; | ||
return false; | ||
} | ||
|
||
private class IteratorArrayList implements Iterator { | ||
|
||
private int cursor; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor < size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (cursor < size) | ||
return elementData[cursor++]; | ||
else | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
group06/284999210/src/com/coding/basic/container/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,68 @@ | ||
package com.coding.basic.container; | ||
|
||
public class BinaryTreeNode<T extends Comparable> { | ||
private T data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
private BinaryTreeNode root; | ||
|
||
public BinaryTreeNode(T data, BinaryTreeNode left, BinaryTreeNode right) { | ||
this.data = data; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T 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(T o) { | ||
if (null == root) { | ||
root = new BinaryTreeNode(o, null, null); | ||
return root; | ||
} | ||
|
||
return insert(o, root); | ||
} | ||
public BinaryTreeNode insert(T o, BinaryTreeNode node) { | ||
BinaryTreeNode nodeNew = new BinaryTreeNode<Comparable>(o, null, null); | ||
BinaryTreeNode nodeCurrent = node; | ||
if (o.compareTo(nodeCurrent.data) < 0) { | ||
if (nodeCurrent.left == null) { | ||
nodeCurrent.left = nodeNew; | ||
return nodeNew; | ||
} else { | ||
return insert(o, nodeCurrent.left); | ||
} | ||
} else if (o.compareTo(nodeCurrent.data) > 0) { | ||
if (nodeCurrent.right == null) { | ||
nodeCurrent.right = nodeNew; | ||
return nodeNew; | ||
} else { | ||
return insert(o, nodeCurrent.right); | ||
} | ||
} else { | ||
return nodeCurrent; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
group06/284999210/src/com/coding/basic/container/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.container; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
|
||
public Object next(); | ||
} |
Oops, something went wrong.