Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Created by john on 2017/3/8.
*
* @// TODO: 2017/4/1 实现Iterator 接口
*/

Expand All @@ -23,7 +24,7 @@ public ArrayList() {
/**
* Constructs an list with the specified initial capacity.
*
* @param initialCapacity
* @param initialCapacity capacity of arrayList.
* @throws IllegalArgumentException if the specified initial capacity
* is negative or zero
*/
Expand Down Expand Up @@ -70,7 +71,7 @@ public void add(E element) {
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
rangeCheckForAdd(index);
rangeCheck(index);
ensureCapacityInternal(size + 1);
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
Expand All @@ -85,10 +86,10 @@ public void add(int index, E element) {
*
* @param index the index of the element to be removed
* @return the element that was removed from the list
* @throws IndexOutOfBoundsException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc} index out of B
*/
public E remove(int index) {
rangeCheckForAdd(index);
rangeCheck(index);
Object oldValue = elementData[index];
int numMoved = size() - index - 1;
if (numMoved > 0) {
Expand Down Expand Up @@ -167,14 +168,6 @@ public Object[] toArray() {
return Arrays.copyOf(elementData, size());
}

/**
* A version of rangeCheck used by add and addAll.
*/
private void rangeCheckForAdd(int index) {
if (index > size() - 1 || index < 0) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
}

/**
* Constructs an IndexOutOfBoundsException detail message.
Expand All @@ -192,7 +185,7 @@ private String outOfBoundsMsg(int index) {
* which throws an ArrayIndexOutOfBoundsException if index is negative.
*/
private void rangeCheck(int index) {
if (index >= size()) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.johnChnia.coding2017.basic;

import com.johnChnia.coding2017.basic.linklist.LinkedList;

import java.util.EmptyStackException;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.johnChnia.coding2017.basic.linklist;

/**
* Created by john on 2017/4/6.
*/
public class LRUPageFrame {

private static class Node {

Node prev;
Node next;
int pageNum;

Node() {
}
}

private int capacity;


private Node first;// 链表头
private Node last;// 链表尾


public LRUPageFrame(int capacity) {

this.capacity = capacity;

}

/**
* 获取缓存中的对象
*
* @param pageNum 对象值
*/
public void access(int pageNum) {
if (first == null) {
Node node = createNode(pageNum);
first = last = node;
capacity--;
} else if (getNode(pageNum) == null) {
if (capacity == 0) {
Node lastNode = first;
while (lastNode.next != null) {
lastNode = lastNode.next;
}
lastNode.prev.next = null;
last = lastNode.prev;
delete(lastNode);
capacity++;
}
Node node = createNode(pageNum);
node.next = first;
node.prev = null;
first.prev = node;
first = node;
capacity--;

} else {
if (first.pageNum != pageNum) {
Node node = getNode(pageNum);
if (node.next != null) {
node.prev.next = node.next;
node.next.prev = node.prev;
} else {
node.prev.next = null;
last = node.prev;
}
node.next = first;
node.prev = null;
first.prev = node;
first = node;
}
}

}

/**
* 删除节点
*/
private void delete(Node node) {
node.pageNum = 0;
node.next = null;
node.prev = null;
}

/**
* @param pageNum 页号
* @return 节点
*/
private Node createNode(int pageNum) {
Node node = new Node();
node.pageNum = pageNum;
node.next = null;
node.prev = null;
return node;
}


/**
* @param pageNum 页号
* @return 如果LRUPageFrame包含该pageNum就返回该节点,否则返回null
*/
private Node getNode(int pageNum) {
for (Node node = first; node != null; node = node.next) {
if (node.pageNum == pageNum) {
return node;
}
}
return null;
}


public String toString() {
StringBuilder buffer = new StringBuilder();
Node node = first;
while (node != null) {
buffer.append(node.pageNum);

node = node.next;
if (node != null) {
buffer.append(",");
}
}
return buffer.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.johnChnia.coding2017.basic.linklist;

import org.junit.Assert;
import org.junit.Test;


public class LRUPageFrameTest {

@Test
public void testAccess() {
LRUPageFrame frame = new LRUPageFrame(3);
frame.access(7);
frame.access(0);
frame.access(1);
Assert.assertEquals("1,0,7", frame.toString());
frame.access(2);
Assert.assertEquals("2,1,0", frame.toString());
frame.access(0);
Assert.assertEquals("0,2,1", frame.toString());
frame.access(0);
Assert.assertEquals("0,2,1", frame.toString());
frame.access(3);
Assert.assertEquals("3,0,2", frame.toString());
frame.access(0);
Assert.assertEquals("0,3,2", frame.toString());
frame.access(4);
Assert.assertEquals("4,0,3", frame.toString());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.johnChnia.coding2017.basic;
package com.johnChnia.coding2017.basic.linklist;

import com.johnChnia.coding2017.basic.List;

import java.util.NoSuchElementException;

Expand Down Expand Up @@ -345,7 +347,7 @@ public int[] getElements(LinkedList list) {
valueNode = valueNode.next;
indexOfArray++;
} else {
mapNode = mapNode.next;
valueNode = valueNode.next;
}
indexOfList++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public void testRemoveElementByIndex() {
arrayList4.add(i);
}
Object removed = arrayList4.remove(4);
System.out.println(arrayList4);
assertThat(removed, equalTo(4));
assertThat(arrayList4.size(), equalTo(5));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.Before;
import org.junit.Test;

import com.johnChnia.coding2017.basic.linklist.LinkedList;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.containsString;
Expand Down