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 ronanhardiman/master
init 2-26 test
- Loading branch information
Showing
6 changed files
with
408 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,92 @@ | ||
package net.iyouqu.bruceretrofit.util.java; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by liq on 2017/2/25. | ||
*/ | ||
|
||
public class CustomArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private final static int DEFAULT_CAPACITY = 10; | ||
private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8; | ||
|
||
private Object[] elementData = new Object[DEFAULT_CAPACITY]; | ||
private String desc = "index超过界限"; | ||
|
||
@Override | ||
public void add(Object o) { | ||
isCapacityEnough(size + 1); | ||
elementData[size++] = o; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
checkRangeForAdd(index); | ||
isCapacityEnough(size + 1); | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
checkRange(index); | ||
return elementData[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
Object value = get(index); | ||
int moveSize = size - index - 1; | ||
if (moveSize > 0){ | ||
System.arraycopy(elementData,index + 1, elementData,index,size - index - 1); | ||
} | ||
elementData[--size] = null; | ||
return value; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
private void checkRange(int index) { | ||
if (index >= size || index < 0) { | ||
throw new IndexOutOfBoundsException(desc); | ||
} | ||
} | ||
|
||
private void checkRangeForAdd(int index) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(desc); | ||
} | ||
} | ||
|
||
private void explicitCapacity(int capacity) { | ||
int newLength = elementData.length * 2; | ||
if (newLength - capacity < 0) { | ||
newLength = capacity; | ||
} | ||
if (newLength > (MAX_ARRAY_LENGTH)) { | ||
newLength = (capacity > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH); | ||
} | ||
elementData = Arrays.copyOf(elementData, newLength); | ||
} | ||
|
||
private void isCapacityEnough(int size) { | ||
if (size > DEFAULT_CAPACITY) { | ||
explicitCapacity(size); | ||
} | ||
if (size < 0) { | ||
throw new OutOfMemoryError(); | ||
} | ||
} | ||
|
||
public Iterator iterator() { | ||
return null; | ||
} | ||
|
||
} |
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,172 @@ | ||
package net.iyouqu.bruceretrofit.util.java; | ||
|
||
/** | ||
* Created by liq on 2017/2/25. | ||
*/ | ||
|
||
public class CustomLinkedList<E> implements List { | ||
|
||
//链表长度 | ||
private int size = 0; | ||
//链表头指针 | ||
private Node<Object> first; | ||
//链表尾部指针 | ||
private Node<Object> last; | ||
//操作次数 | ||
private int modCount; | ||
|
||
@Override | ||
public void add(Object o) { | ||
linkLast(o); | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
checkPositionIndex(index); | ||
if (index == size) { | ||
linkLast(o); | ||
} else { | ||
linkBefore(o, node(index)); | ||
} | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
checkPositionIndex(index); | ||
return node(index).data; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
checkPositionIndex(index); | ||
return unlink(node(index)); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
/** | ||
* 添加节点到链表尾部 | ||
*/ | ||
public void addLast(Object e) { | ||
linkLast(e); | ||
} | ||
|
||
/** | ||
* 解除传入节点的属性,并且将传入节点的上一个和下一个节点 链接。使传入节点的属性 全部为 null | ||
*/ | ||
private Object unlink(Node<Object> node) { | ||
//获取当前节点node的属性 | ||
final Object element = node.data; | ||
final Node<Object> next = node.next; | ||
final Node<Object> prev = node.prev; | ||
if (prev == null) { | ||
//上一个节点为null将首节点设置为下一个节点 | ||
first = next; | ||
} else { | ||
//上一个节点有 将上一个节点的下一个节点 设置为当前节点的下一个节点 | ||
prev.next = next; | ||
//将当前节点的上一个节点设置为null | ||
node.prev = null; | ||
} | ||
if (next == null) { | ||
//下一个节点为null将末尾节点设置为上一个节点 | ||
last = prev; | ||
} else { | ||
//将下一个节点的上一个节点 设置为当前节点的上一个节点 | ||
next.prev = prev; | ||
node.next = null; | ||
} | ||
node.data = null; | ||
size--; | ||
modCount++; | ||
return element; | ||
} | ||
|
||
/** | ||
* 获取一个节点 | ||
* 判断index 在前半区间还是后半区间。而不是一直从头到尾搜索 | ||
* 将节点访问复杂度从O(n)变为O(n/2) | ||
*/ | ||
private Node<Object> node(int index) { | ||
checkPositionIndex(index); | ||
if (index < (size / 2)) { | ||
Node<Object> x = first; | ||
for (int i = 0; i < index; i++) { | ||
x = x.next; | ||
} | ||
return x; | ||
} else { | ||
Node<Object> x = last; | ||
for (int i = size - 1; i > index; i--) { | ||
x = x.prev; | ||
} | ||
return x; | ||
} | ||
} | ||
|
||
/** | ||
* 在参数节点之前插入一个节点 | ||
*/ | ||
private void linkBefore(Object element, Node<Object> node) { | ||
//获取添加节点的上一个节点 | ||
final Node<Object> pred = node.prev; | ||
//创建一个新节点 | ||
final Node<Object> newNode = new Node<>(pred, element, node); | ||
//添加节点的上一个节点为 新节点 | ||
node.prev = newNode; | ||
//判断上一个节点是否为null | ||
if (pred == null) { | ||
//首节点设置为新创建的节点 | ||
first = newNode; | ||
} else { | ||
//上个节点不为null。将其下个节点设置为新创建的节点。 | ||
pred.next = newNode; | ||
} | ||
size++; | ||
modCount++; | ||
} | ||
|
||
/** | ||
* 链接 节点到 last | ||
*/ | ||
private void linkLast(Object e) { | ||
final Node<Object> l = last; | ||
final Node<Object> newNode = new Node<>(l, e, null); | ||
last = newNode; | ||
//判断链表last是否为null | ||
if (l == null) { | ||
//链表first指向新添加的 节点 | ||
first = newNode; | ||
} else { | ||
//链表last不为null将链表last节点的的next设置为新节点 | ||
l.next = newNode; | ||
} | ||
size++; | ||
modCount++; | ||
} | ||
|
||
/** | ||
* 检查index是否越界 | ||
*/ | ||
private void checkPositionIndex(int index) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("index超过界限"); | ||
} | ||
} | ||
|
||
private static class Node<E> { | ||
Object data; | ||
//下一个节点 | ||
Node<Object> next; | ||
//上一个节点 | ||
Node<Object> prev; | ||
public Node(Node<Object> prev, Object item, Node<Object> next) { | ||
this.data = item; | ||
this.next = next; | ||
this.prev = prev; | ||
} | ||
} | ||
} |
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 net.iyouqu.bruceretrofit.util.java; | ||
|
||
/** | ||
* Created by liq on 2017/2/25. | ||
*/ | ||
|
||
public class CustomQueue<E> { | ||
|
||
Object[] data = null; | ||
//容量 | ||
private int capacity; | ||
//队尾指针 | ||
private int tail; | ||
|
||
CustomQueue(int initSize) { | ||
if (initSize >= 0) { | ||
this.capacity = initSize; | ||
data = new Object[initSize]; | ||
tail = 0; | ||
} else { | ||
throw new RuntimeException("初始化大小不能小于0" + initSize); | ||
} | ||
} | ||
|
||
public void enQueue(E o){ | ||
ensureCapacity(); | ||
data[tail] = o; | ||
tail++; | ||
} | ||
|
||
public E deQueue(){ | ||
return (E) data[0]; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return tail == 0; | ||
} | ||
|
||
public int size(){ | ||
return tail; | ||
} | ||
|
||
private void ensureCapacity() { | ||
if (tail == capacity) { | ||
capacity *= 2; | ||
Object[] newData = new Object[capacity]; | ||
System.arraycopy(data, 0, newData, 0, tail); | ||
data = newData; | ||
} | ||
} | ||
} |
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,70 @@ | ||
package net.iyouqu.bruceretrofit.util.java; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by liq on 2017/2/25. | ||
*/ | ||
|
||
public class CustomStack<E> { | ||
|
||
//重载因子 | ||
private static final float LOAD_FACTOR = 0.75f; | ||
//需要扩充容量时的大小 | ||
private int resizeCapacity; | ||
private Object[] data = null; | ||
//栈容量 | ||
private int capacity; | ||
//栈顶 | ||
private int top; | ||
|
||
public CustomStack(int initSize) { | ||
if (initSize >= 0) { | ||
this.capacity = initSize; | ||
data = new Object[initSize]; | ||
top = 0; | ||
this.resizeCapacity = (int) (capacity * LOAD_FACTOR); | ||
} else { | ||
throw new RuntimeException("初始化大小不能小于0:" + initSize); | ||
} | ||
} | ||
|
||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(E o){ | ||
checkStackCapacity(); | ||
data[top] = o; | ||
top++; | ||
} | ||
|
||
public E pop(){ | ||
if(top<=0) | ||
throw new RuntimeException("没有元素不能弹出"); | ||
E e = (E) data[top - 1]; | ||
data[top-1] = null; | ||
--top; | ||
return e; | ||
} | ||
|
||
public E peek(){ | ||
|
||
return (E) data[top - 1]; | ||
|
||
} | ||
public boolean isEmpty(){ | ||
return top == 0; | ||
} | ||
public int size(){ | ||
return top; | ||
} | ||
|
||
private void checkStackCapacity() { | ||
if (top == resizeCapacity) { | ||
capacity = capacity * 2; | ||
Object[] newData = new Object[capacity]; | ||
System.arraycopy(data, 0, newData, 0, top); | ||
data = newData; | ||
} | ||
} | ||
|
||
} |
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,10 @@ | ||
package net.iyouqu.bruceretrofit.util.java; | ||
|
||
/** | ||
* Created by liq on 2017/2/25. | ||
*/ | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
} |
Oops, something went wrong.