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 barrywangmeng/master
为.gitignore添加空格。
- Loading branch information
Showing
8 changed files
with
638 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,46 @@ | ||
# Class files | ||
*.class | ||
|
||
# Package Files | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# Virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
# Ignore web-site project | ||
*web-site/ | ||
|
||
# Temporary files | ||
.DS_STORE | ||
*.log | ||
|
||
# Maven related | ||
/*/target/ | ||
target | ||
|
||
# Netbeans related | ||
nb-configuration.xml | ||
nbactions.xml | ||
nbproject | ||
|
||
# Eclipse related | ||
*.classpath | ||
*.project | ||
.settings | ||
|
||
# IntelliJ related | ||
.idea | ||
*.iml | ||
*.ipr | ||
*.iws | ||
|
||
# Jrebel related | ||
rebel.xml | ||
rebel-remote.xml | ||
|
||
# design model | ||
*.eab | ||
|
||
.idea/workspace.xml |
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,152 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
import java.util.ConcurrentModificationException; | ||
|
||
public class ArrayList implements List { | ||
/** | ||
* 当数组进行add/remove时, 对modCount进行++ | ||
*/ | ||
protected transient int modCount = 0; | ||
/** | ||
* 数组的大小 | ||
*/ | ||
private int size = 0; | ||
|
||
/** | ||
* 数组,用来存放ArrayList的内容。 | ||
*/ | ||
private Object[] elementData; | ||
|
||
public ArrayList() { | ||
this(10); | ||
} | ||
|
||
public ArrayList(int intialSize) { | ||
elementData = new Object[intialSize]; | ||
} | ||
|
||
public void add(Object o) { | ||
modCount++; | ||
// 检测是否要扩容,当添加的元素大于数组的长度后, 扩容 | ||
increment(size + 1); | ||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
modCount++; | ||
increment(size + 1); | ||
/** | ||
* @param src | ||
* 源数组 | ||
* @param srcPos | ||
* 源数组要复制的起始位置 | ||
* @param dest | ||
* 目的数组 | ||
* @param destPos | ||
* 目的数组放置的起始位置 | ||
* @param length | ||
* 复制的长度 从index位置开始copy, | ||
*/ | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = o; | ||
} | ||
|
||
/** | ||
* 验证是否要扩容。 | ||
* | ||
* @param capacity | ||
*/ | ||
private void increment(int capacity) { | ||
if (capacity - elementData.length > 0) { | ||
grow(capacity); | ||
} | ||
} | ||
|
||
/** | ||
* 扩容,扩容规则为:oldCapacity + oldCapacity/2 | ||
* | ||
* @param capacity | ||
*/ | ||
private void grow(int capacity) { | ||
int oldCapacity = elementData.length; | ||
int newCapacity = oldCapacity + oldCapacity / 2; | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
|
||
public Object get(int index) throws Exception { | ||
checkSize(index); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) throws Exception { | ||
modCount++; | ||
checkSize(index); | ||
Object oldValue = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
//回收多出来的内存。 | ||
elementData[size--] = null; | ||
return oldValue; | ||
} | ||
|
||
/** | ||
* 验证给定的数组下标是否小于数组的长度。 | ||
* | ||
* @param index | ||
* @return | ||
*/ | ||
private void checkSize(int index) throws Exception { | ||
if (index > size) { | ||
// 数组下标越界异常。 | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator { | ||
int cursor;//记录下一个元素的索引 | ||
int lastReturn = -1;//记录最后一个元素的索引 | ||
int expectCount = modCount; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return (cursor != size); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
checkForComodification(); | ||
int i = cursor; | ||
Object[] elementData = ArrayList.this.elementData; | ||
cursor = i+ 1; | ||
return elementData[lastReturn = i]; | ||
} | ||
|
||
/** | ||
* 核心方法, 这里remove可以避免fail-fast快速失败原则。 | ||
* @throws Exception | ||
*/ | ||
public void remove() throws Exception { | ||
checkForComodification(); | ||
ArrayList.this.remove(lastReturn); | ||
cursor = lastReturn; | ||
lastReturn = -1; | ||
expectCount = modCount; | ||
} | ||
|
||
/** | ||
* 验证fail-fast规则。 | ||
*/ | ||
final void checkForComodification() { | ||
if (modCount != expectCount) | ||
throw new ConcurrentModificationException(); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
group03/510782645/src/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,83 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
static class Node { | ||
Integer data; | ||
Node parent; | ||
Node left; | ||
Node right; | ||
|
||
public Node(Integer data, Node parent, Node left, Node right) { | ||
this.data = data; | ||
this.parent = parent; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
public String toString(){ | ||
return "[data=" + data + "]"; | ||
} | ||
|
||
public boolean equals(Object obj){ | ||
if(this == obj){ | ||
return true; | ||
} | ||
|
||
if(obj.getClass() == Node.class){ | ||
Node target = (Node) obj; | ||
return data.equals(target.data) && left == target.left | ||
&& right == target.right && parent == target.parent; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
private Node root; | ||
|
||
BinaryTreeNode() { | ||
root = null; | ||
} | ||
|
||
BinaryTreeNode(Integer data) { | ||
root = new Node(data, null, null, null); | ||
} | ||
|
||
/** | ||
* 暂且使用Intenger作为节点数据。 | ||
* @param o | ||
*/ | ||
public void insert(Integer o) { | ||
if (root == null) { | ||
root = new Node(o, null, null, null); | ||
} else { | ||
Node current = root; | ||
Node parent = null; | ||
int cmp; | ||
|
||
//搜索合适的叶子节点,以该叶子节点为父节点添加新节点 | ||
do { | ||
parent = current; | ||
cmp = o.compareTo(current.data); | ||
|
||
//如果新节点的值大于当前节点的值 | ||
if (cmp > 0) { | ||
//以当前节点的右子节点作为当前节点 | ||
current = current.right; | ||
} else { | ||
current = current.left; | ||
} | ||
} while (current != null); | ||
|
||
//创建新节点 | ||
Node newNode = new Node(o, parent, null, null); | ||
|
||
//如果新节点的值大于父节点的值 | ||
if (cmp > 0) { | ||
parent.right = newNode; | ||
} else { | ||
parent.left = newNode; | ||
} | ||
} | ||
} | ||
} |
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(); | ||
|
||
} |
Oops, something went wrong.