forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 GitMori/master
作业作业交作业
- Loading branch information
Showing
8 changed files
with
490 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,119 @@ | ||
package com.coding.basic; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList<E> implements List<E> { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData; | ||
|
||
//默认容量 | ||
private static final int DEFAULT_CAPACITY = 10; | ||
|
||
public ArrayList(int capacity){ | ||
if(capacity >= 0){ | ||
elementData = new Object[capacity]; | ||
}else { | ||
throw new IllegalArgumentException("Illegal Capacity: " + | ||
capacity); | ||
} | ||
|
||
} | ||
public ArrayList(){ | ||
this(DEFAULT_CAPACITY); | ||
} | ||
|
||
/** | ||
* 保证集合容量 | ||
* @param minCapacity | ||
*/ | ||
private void ensureCapacity(int minCapacity){ | ||
int oldCapacity = elementData.length; | ||
if(minCapacity > oldCapacity){ | ||
//扩容 | ||
int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; | ||
if(minCapacity - newCapacity > 0){ | ||
newCapacity = minCapacity; | ||
} | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
} | ||
private void checkIndexRange(int index) | ||
{ | ||
if(index >= size || index < 0) | ||
{ | ||
throw new IndexOutOfBoundsException("Index out of bounds, index : " + index); | ||
} | ||
} | ||
public void add(E o){ | ||
ensureCapacity(size+1); | ||
elementData[size++] = o; | ||
} | ||
public void add(int index, E o){ | ||
checkIndexRange(index);//检查下标 | ||
ensureCapacity(size+1);//保证数组容量 | ||
System.arraycopy(elementData,index,elementData,index + 1,size-index);//数组复制,把index后的元素全部向后移一位 | ||
elementData[index] = o;//插入元素值 | ||
size++;//元素size加一 | ||
} | ||
|
||
@Override | ||
public E get(int index) { | ||
checkIndexRange(index);//检查下标 | ||
return (E)elementData[index]; | ||
} | ||
|
||
@Override | ||
public E remove(int index) { | ||
E e = this.get(index); | ||
int numMoved = size - index - 1; | ||
if(numMoved > 0) | ||
{ | ||
System.arraycopy(elementData, index+1, elementData, index, numMoved);//数组复制,把index后的元素全部向前移一位 | ||
} | ||
elementData[--size] = null;//最后一位赋值为null,size-1 | ||
return e; | ||
} | ||
|
||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
private class ArrayListIterator<E> implements Iterator{ | ||
|
||
private Object [] array; | ||
private int endIndex = 0; | ||
private int index = 0; | ||
|
||
public ArrayListIterator(ArrayList list){ | ||
this.array=list.elementData; | ||
this.endIndex = list.size(); | ||
} | ||
@Override | ||
public boolean hasNext() { | ||
return this.index < this.endIndex; | ||
} | ||
|
||
@Override | ||
public E next() { | ||
if(!this.hasNext()) { | ||
throw new NoSuchElementException();//没有元素了 | ||
} else { | ||
return (E)Array.get(this.array, this.index++); | ||
} | ||
} | ||
} | ||
} |
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; | ||
|
||
|
||
public class BinaryTreeNode implements Comparable<BinaryTreeNode> { | ||
public BinaryTreeNode(Object data) { | ||
this.data = data; | ||
} | ||
|
||
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) { | ||
//左子节点的值永远比父节点的值小 | ||
//右子节点的值永远比父节点的值大 | ||
BinaryTreeNode node = new BinaryTreeNode(o); | ||
insertNode(node); | ||
return node; | ||
} | ||
private void insertNode(BinaryTreeNode node){ | ||
insertNode(this,node); | ||
} | ||
private void insertNode(BinaryTreeNode parentNode, BinaryTreeNode node) { | ||
if (parentNode.compareTo(node) <= 0) {//数字大于父节点 | ||
if (parentNode.right == null) { | ||
parentNode.right = node; | ||
return; | ||
} | ||
insertNode(parentNode.right, node); | ||
} else { | ||
if (parentNode.left == null) { | ||
parentNode.left = node; | ||
return; | ||
} | ||
insertNode(parentNode.left, node); | ||
} | ||
} | ||
|
||
@Override | ||
public int compareTo(BinaryTreeNode o) { | ||
Integer i = (Integer) this.data; | ||
return i.compareTo((Integer) o.data); | ||
} | ||
} |
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 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator<E> { | ||
public boolean hasNext(); | ||
public E next(); | ||
} |
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,62 @@ | ||
package com.coding.basic; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by Mori on 2017/2/21. | ||
*/ | ||
public class JavaTest { | ||
@Test | ||
public void testBinaryTreeNode(){ | ||
BinaryTreeNode node = new BinaryTreeNode(5); | ||
node.insert(4);//左 | ||
node.insert(7);//右 | ||
node.insert(2);//左左 | ||
node.insert(6);//右左 | ||
node.insert(5);//右左左 | ||
node.insert(6);//右左右 | ||
System.out.println(node.getData()); | ||
System.out.println(node.getLeft().getData()); | ||
System.out.println(node.getRight().getData()); | ||
System.out.println(node.getLeft().getLeft().getData()); | ||
System.out.println(node.getRight().getLeft().getData()); | ||
System.out.println(node.getRight().getLeft().getLeft().getData()); | ||
System.out.println(node.getRight().getLeft().getRight().getData()); | ||
} | ||
@Test | ||
public void testArrayList(){ | ||
ArrayList<Integer> list =new ArrayList<>(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(5); | ||
Assert.assertEquals((Object) list.get(2),3); | ||
Assert.assertEquals((Object) list.remove(2),3); | ||
Assert.assertEquals((Object) list.get(2),5); | ||
Iterator listIterator = list.iterator(); | ||
while (listIterator.hasNext()){ | ||
System.out.println(listIterator.next()); | ||
} | ||
} | ||
@Test | ||
public void testLinkedList(){ | ||
LinkedList<Integer> linkedList = new LinkedList<>(); | ||
linkedList.add(1); | ||
linkedList.add(2); | ||
linkedList.add(5); | ||
linkedList.addFirst(10); | ||
linkedList.add(1,6); | ||
// linkedList.removeLast(); | ||
//linkedList.removeFirst(); | ||
Iterator linkedListIterator = linkedList.iterator(); | ||
while (linkedListIterator.hasNext()){ | ||
System.out.println(linkedListIterator.next()); | ||
} | ||
System.out.println("----"); | ||
System.out.println(linkedList.remove(0)); | ||
System.out.println(linkedList.remove(2)); | ||
//System.out.println(linkedList.get(3)); | ||
//System.out.println(linkedList.get(4)); | ||
} | ||
} |
Oops, something went wrong.