From b7e88aff387c98b8c4e2dc8f79c17542c68e3f11 Mon Sep 17 00:00:00 2001 From: xiongrui233 Date: Tue, 4 Apr 2017 16:02:58 +0800 Subject: [PATCH 1/5] copy templet code --- .../src/com/coding/basic/BinaryTreeNode.java | 10 +- .../627559964/src/com/coding/basic/Queue.java | 4 +- .../coding/basic/{ => array}/ArrayList.java | 33 ++-- .../basic}/array/ArrayUtil.java | 2 +- .../coding/basic/linklist/LRUPageFrame.java | 151 ++++++++++++++++++ .../basic/{ => linklist}/LinkedList.java | 5 +- .../com/coding/basic/{ => stack}/Stack.java | 18 ++- .../src/com/coding/basic/stack/StackUtil.java | 45 ++++++ .../src/com/jvm/ClassFileLoader.java | 24 +++ group22/627559964/src/com/jvm/EmployeeV1.java | 30 ++++ group22/627559964/src/demo/Demo.java | 4 +- .../com/coderising/array/TestArrayUtil.java | 1 + .../basic/{ => array}/TestArrayList.java | 6 +- .../basic/linklist/LRUPageFrameTest.java | 33 ++++ .../test/com/jvm/ClassFileloaderTest.java | 78 +++++++++ 15 files changed, 410 insertions(+), 34 deletions(-) rename group22/627559964/src/com/coding/basic/{ => array}/ArrayList.java (85%) rename group22/627559964/src/com/{coderising => coding/basic}/array/ArrayUtil.java (99%) create mode 100644 group22/627559964/src/com/coding/basic/linklist/LRUPageFrame.java rename group22/627559964/src/com/coding/basic/{ => linklist}/LinkedList.java (98%) rename group22/627559964/src/com/coding/basic/{ => stack}/Stack.java (68%) create mode 100644 group22/627559964/src/com/coding/basic/stack/StackUtil.java create mode 100644 group22/627559964/src/com/jvm/ClassFileLoader.java create mode 100644 group22/627559964/src/com/jvm/EmployeeV1.java rename group22/627559964/test/com/coding/basic/{ => array}/TestArrayList.java (88%) create mode 100644 group22/627559964/test/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 group22/627559964/test/com/jvm/ClassFileloaderTest.java diff --git a/group22/627559964/src/com/coding/basic/BinaryTreeNode.java b/group22/627559964/src/com/coding/basic/BinaryTreeNode.java index 9c416d3ad3..28212adfe8 100644 --- a/group22/627559964/src/com/coding/basic/BinaryTreeNode.java +++ b/group22/627559964/src/com/coding/basic/BinaryTreeNode.java @@ -1,18 +1,18 @@ package com.coding.basic; /** - * 自定义二叉树 + * 鑷畾涔変簩鍙夋爲 * * @author xiongrui233 * */ public class BinaryTreeNode { - //节点值 + //鑺傜偣鍊 private Object data; - //左子树 + //宸﹀瓙鏍 private BinaryTreeNode left; - //右子树 + //鍙冲瓙鏍 private BinaryTreeNode right; public Object getData() { @@ -40,7 +40,7 @@ public void setRight(BinaryTreeNode right) { } /** - * 插入元素 + * 鎻掑叆鍏冪礌 * @param o * @return BinaryTreeNode */ diff --git a/group22/627559964/src/com/coding/basic/Queue.java b/group22/627559964/src/com/coding/basic/Queue.java index 965894a9ea..9ed8e0b4fa 100644 --- a/group22/627559964/src/com/coding/basic/Queue.java +++ b/group22/627559964/src/com/coding/basic/Queue.java @@ -1,7 +1,9 @@ package com.coding.basic; +import com.coding.basic.linklist.LinkedList; + /** - * 自定义队列 + * 鑷畾涔夐槦鍒 * * @author xiongrui233 * diff --git a/group22/627559964/src/com/coding/basic/ArrayList.java b/group22/627559964/src/com/coding/basic/array/ArrayList.java similarity index 85% rename from group22/627559964/src/com/coding/basic/ArrayList.java rename to group22/627559964/src/com/coding/basic/array/ArrayList.java index 59d055dfa7..0070419e3d 100644 --- a/group22/627559964/src/com/coding/basic/ArrayList.java +++ b/group22/627559964/src/com/coding/basic/array/ArrayList.java @@ -1,23 +1,26 @@ -package com.coding.basic; +package com.coding.basic.array; import java.util.Arrays; +import com.coding.basic.Iterator; +import com.coding.basic.List; + /** - * 自定义ArrayList + * 鑷畾涔堿rrayList * * @author xiongrui233 * */ public class ArrayList implements List { - // list长度 + // list闀垮害 private int size = 0; - // list的元素集合 + // list鐨勫厓绱犻泦鍚 private Object[] elementData = new Object[10]; /** - * 合并数组 + * 鍚堝苟鏁扮粍 * * @param arrays1 * @param arrays2 @@ -31,7 +34,7 @@ private Object[] concat(Object[] arrays1, Object[] arrays2) { } /** - * 分割数组 + * 鍒嗗壊鏁扮粍 * * @param arrays * @param from @@ -47,8 +50,8 @@ private Object[] subArrays(Object[] arrays, int from, int index) { } /** - * 动态增长list长度 - * 策略为:newSize = oldSize * 1.5 + * 鍔ㄦ佸闀縧ist闀垮害 + * 绛栫暐涓:newSize = oldSize * 1.5 * * @param oldSize */ @@ -57,7 +60,7 @@ private void grow(int oldSize) { } /** - * 检查在插入新元素时,list长度是否足够 + * 妫鏌ュ湪鎻掑叆鏂板厓绱犳椂,list闀垮害鏄惁瓒冲 * * @param newSize */ @@ -69,7 +72,7 @@ private void checkSize(int newSize) { } /** - * 新增元素 + * 鏂板鍏冪礌 * * @param Object */ @@ -79,7 +82,7 @@ public void add(Object o) { } /** - * 新增元素 + * 鏂板鍏冪礌 * * @param index * @param Object @@ -96,7 +99,7 @@ public void add(int index, Object o) { } /** - * 获得编号为index的元素 + * 鑾峰緱缂栧彿涓篿ndex鐨勫厓绱 * * @param int * @return Object @@ -106,7 +109,7 @@ public Object get(int index) { } /** - * 删除编号为index的元素 + * 鍒犻櫎缂栧彿涓篿ndex鐨勫厓绱 * * @param int * @return Object @@ -122,7 +125,7 @@ public Object remove(int index) { } /** - * 返回list长度 + * 杩斿洖list闀垮害 * * @return int */ @@ -131,7 +134,7 @@ public int size() { } /** - * 重写迭代器 + * 閲嶅啓杩唬鍣 * * @return IteratorImpl */ diff --git a/group22/627559964/src/com/coderising/array/ArrayUtil.java b/group22/627559964/src/com/coding/basic/array/ArrayUtil.java similarity index 99% rename from group22/627559964/src/com/coderising/array/ArrayUtil.java rename to group22/627559964/src/com/coding/basic/array/ArrayUtil.java index e97b5f7a7c..07336908e0 100644 --- a/group22/627559964/src/com/coderising/array/ArrayUtil.java +++ b/group22/627559964/src/com/coding/basic/array/ArrayUtil.java @@ -1,4 +1,4 @@ -package com.coderising.array; +package com.coding.basic.array; import java.util.ArrayList; import java.util.Arrays; diff --git a/group22/627559964/src/com/coding/basic/linklist/LRUPageFrame.java b/group22/627559964/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..f46c602b7f --- /dev/null +++ b/group22/627559964/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,151 @@ +package com.coding.basic.linklist; + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 閾捐〃澶 + private Node last;// 閾捐〃灏 + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 鑾峰彇缂撳瓨涓璞 + * + * @param key + * @return + */ + public void access(int pageNum) { + + Node node = find(pageNum); + // 鍦ㄨ闃熷垪涓瓨鍦紝 鍒欐彁鍒伴槦鍒楀ご + if (node != null) { + + moveExistingNodeToHead(node); + + } else { + + node = new Node(); + node.pageNum = pageNum; + + // 缂撳瓨瀹瑰櫒鏄惁宸茬粡瓒呰繃澶у皬. + if (currentSize >= capacity) { + removeLast(); + + } + + addNewNodetoHead(node); + + } + } + + private void addNewNodetoHead(Node node) { + + if (isEmpty()) { + + node.prev = null; + node.next = null; + first = node; + last = node; + + } else { + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize++; + } + + private Node find(int data) { + + Node node = first; + while (node != null) { + if (node.pageNum == data) { + return node; + } + node = node.next; + } + return null; + + } + + /** + * 鍒犻櫎閾捐〃灏鹃儴鑺傜偣 琛ㄧず 鍒犻櫎鏈灏戜娇鐢ㄧ殑缂撳瓨瀵硅薄 + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize--; + } + + /** + * 绉诲姩鍒伴摼琛ㄥご锛岃〃绀鸿繖涓妭鐐规槸鏈鏂颁娇鐢ㄨ繃鐨 + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + + if (node == first) { + + return; + } else if (node == last) { + // 褰撳墠鑺傜偣鏄摼琛ㄥ熬锛 闇瑕佹斁鍒伴摼琛ㄥご + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + + } else { + // node 鍦ㄩ摼琛ㄧ殑涓棿锛 鎶妌ode 鐨勫墠鍚庤妭鐐硅繛鎺ヨ捣鏉 + Node prevNode = node.prev; + prevNode.next = node.next; + + Node nextNode = node.next; + nextNode.prev = prevNode; + + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + + } + + private boolean isEmpty() { + return (first == null) && (last == 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(); + } + +} \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/LinkedList.java b/group22/627559964/src/com/coding/basic/linklist/LinkedList.java similarity index 98% rename from group22/627559964/src/com/coding/basic/LinkedList.java rename to group22/627559964/src/com/coding/basic/linklist/LinkedList.java index 39b0250ae3..843495caa4 100644 --- a/group22/627559964/src/com/coding/basic/LinkedList.java +++ b/group22/627559964/src/com/coding/basic/linklist/LinkedList.java @@ -1,4 +1,7 @@ -package com.coding.basic; +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; /** * 鑷畾涔塋inkList diff --git a/group22/627559964/src/com/coding/basic/Stack.java b/group22/627559964/src/com/coding/basic/stack/Stack.java similarity index 68% rename from group22/627559964/src/com/coding/basic/Stack.java rename to group22/627559964/src/com/coding/basic/stack/Stack.java index 84e90bfb75..ce602f94d9 100644 --- a/group22/627559964/src/com/coding/basic/Stack.java +++ b/group22/627559964/src/com/coding/basic/stack/Stack.java @@ -1,18 +1,20 @@ -package com.coding.basic; +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; /** - * 自定义stack + * 鑷畾涔塻tack * * @author xiongrui233 * */ public class Stack { - //元素集合 + //鍏冪礌闆嗗悎 private ArrayList elementData = new ArrayList(); /** - * 向栈顶压入元素 + * 鍚戞爤椤跺帇鍏ュ厓绱 * @param o */ public void push(Object o) { @@ -20,7 +22,7 @@ public void push(Object o) { } /** - * 获得栈顶元素,并移除栈里该元素 + * 鑾峰緱鏍堥《鍏冪礌,骞剁Щ闄ゆ爤閲岃鍏冪礌 * @return obj */ public Object pop() { @@ -30,7 +32,7 @@ public Object pop() { } /** - * 获得栈顶元素,不移除栈里该元素 + * 鑾峰緱鏍堥《鍏冪礌,涓嶇Щ闄ゆ爤閲岃鍏冪礌 * @return obj */ public Object peek() { @@ -38,7 +40,7 @@ public Object peek() { } /** - * 判断该栈是否为空 + * 鍒ゆ柇璇ユ爤鏄惁涓虹┖ * @return true/false */ public boolean isEmpty() { @@ -49,7 +51,7 @@ public boolean isEmpty() { } /** - * 获得栈的大小 + * 鑾峰緱鏍堢殑澶у皬 * @return size */ public int size() { diff --git a/group22/627559964/src/com/coding/basic/stack/StackUtil.java b/group22/627559964/src/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..51186c5e25 --- /dev/null +++ b/group22/627559964/src/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack; + +public class StackUtil { + + /** + * 鍋囪鏍堜腑鐨勫厓绱犳槸Integer, 浠庢爤椤跺埌鏍堝簳鏄 : 5,4,3,2,1 璋冪敤璇ユ柟娉曞悗锛 鍏冪礌娆″簭鍙樹负: 1,2,3,4,5 + * 娉ㄦ剰锛氬彧鑳戒娇鐢⊿tack鐨勫熀鏈搷浣滐紝鍗硃ush,pop,peek,isEmpty锛 鍙互浣跨敤鍙﹀涓涓爤鏉ヨ緟鍔 + */ + public static void reverse(Stack s) { + + } + + /** + * 鍒犻櫎鏍堜腑鐨勬煇涓厓绱 娉ㄦ剰锛氬彧鑳戒娇鐢⊿tack鐨勫熀鏈搷浣滐紝鍗硃ush,pop,peek,isEmpty锛 鍙互浣跨敤鍙﹀涓涓爤鏉ヨ緟鍔 + * + * @param o + */ + public static void remove(Stack s, Object o) { + + } + + /** + * 浠庢爤椤跺彇寰條en涓厓绱, 鍘熸潵鐨勬爤涓厓绱犱繚鎸佷笉鍙 娉ㄦ剰锛氬彧鑳戒娇鐢⊿tack鐨勫熀鏈搷浣滐紝鍗硃ush,pop,peek,isEmpty锛 + * 鍙互浣跨敤鍙﹀涓涓爤鏉ヨ緟鍔 + * + * @param len + * @return + */ + public static Object[] getTop(Stack s, int len) { + return null; + } + + /** + * 瀛楃涓瞫 鍙兘鍖呭惈杩欎簺瀛楃锛 ( ) [ ] { }, a,b,c... x,yz 浣跨敤鍫嗘爤妫鏌ュ瓧绗︿覆s涓殑鎷彿鏄笉鏄垚瀵瑰嚭鐜扮殑銆 渚嬪s = + * "([e{d}f])" , 鍒欒瀛楃涓蹭腑鐨勬嫭鍙锋槸鎴愬鍑虹幇锛 璇ユ柟娉曡繑鍥瀟rue 濡傛灉 s = "([b{x]y})", + * 鍒欒瀛楃涓蹭腑鐨勬嫭鍙蜂笉鏄垚瀵瑰嚭鐜扮殑锛 璇ユ柟娉曡繑鍥瀎alse; + * + * @param s + * @return + */ + public static boolean isValidPairs(String s) { + return false; + } + +} \ No newline at end of file diff --git a/group22/627559964/src/com/jvm/ClassFileLoader.java b/group22/627559964/src/com/jvm/ClassFileLoader.java new file mode 100644 index 0000000000..4fb8b7b30e --- /dev/null +++ b/group22/627559964/src/com/jvm/ClassFileLoader.java @@ -0,0 +1,24 @@ +package com.jvm; + +import java.util.ArrayList; +import java.util.List; + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + return null; + + } + + public void addClassPath(String path) { + + } + + public String getClassPath() { + return null; + } + +} \ No newline at end of file diff --git a/group22/627559964/src/com/jvm/EmployeeV1.java b/group22/627559964/src/com/jvm/EmployeeV1.java new file mode 100644 index 0000000000..6d56f64652 --- /dev/null +++ b/group22/627559964/src/com/jvm/EmployeeV1.java @@ -0,0 +1,30 @@ +package com.jvm; + +public class EmployeeV1 { + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + + public static void main(String[] args) { + EmployeeV1 p = new EmployeeV1("Andy", 29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/group22/627559964/src/demo/Demo.java b/group22/627559964/src/demo/Demo.java index 4fbb5c6b40..833babbe20 100644 --- a/group22/627559964/src/demo/Demo.java +++ b/group22/627559964/src/demo/Demo.java @@ -3,10 +3,10 @@ import java.util.Arrays; import java.util.PriorityQueue; -import com.coding.basic.ArrayList; import com.coding.basic.Iterator; -import com.coding.basic.LinkedList; import com.coding.basic.List; +import com.coding.basic.array.ArrayList; +import com.coding.basic.linklist.LinkedList; public class Demo { public static void main(String[] args) { diff --git a/group22/627559964/test/com/coderising/array/TestArrayUtil.java b/group22/627559964/test/com/coderising/array/TestArrayUtil.java index f597900492..a7ec1e9e1e 100644 --- a/group22/627559964/test/com/coderising/array/TestArrayUtil.java +++ b/group22/627559964/test/com/coderising/array/TestArrayUtil.java @@ -7,6 +7,7 @@ import org.junit.Assert; import org.junit.Test; +import com.coding.basic.array.ArrayUtil; import com.sun.scenario.effect.Merge; public class TestArrayUtil { diff --git a/group22/627559964/test/com/coding/basic/TestArrayList.java b/group22/627559964/test/com/coding/basic/array/TestArrayList.java similarity index 88% rename from group22/627559964/test/com/coding/basic/TestArrayList.java rename to group22/627559964/test/com/coding/basic/array/TestArrayList.java index 68dabbb042..436dc6e376 100644 --- a/group22/627559964/test/com/coding/basic/TestArrayList.java +++ b/group22/627559964/test/com/coding/basic/array/TestArrayList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package com.coding.basic.array; import java.util.Arrays; @@ -6,6 +6,10 @@ import org.junit.Before; import org.junit.Test; +import com.coding.basic.Iterator; +import com.coding.basic.List; +import com.coding.basic.array.ArrayList; + public class TestArrayList { private List list = null; diff --git a/group22/627559964/test/com/coding/basic/linklist/LRUPageFrameTest.java b/group22/627559964/test/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..0153a1d40d --- /dev/null +++ b/group22/627559964/test/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,33 @@ +package com.coding.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()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} \ No newline at end of file diff --git a/group22/627559964/test/com/jvm/ClassFileloaderTest.java b/group22/627559964/test/com/jvm/ClassFileloaderTest.java new file mode 100644 index 0000000000..7eb602dcab --- /dev/null +++ b/group22/627559964/test/com/jvm/ClassFileloaderTest.java @@ -0,0 +1,78 @@ +package com.jvm; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class ClassFileloaderTest { + + static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 娉ㄦ剰锛氳繖涓瓧鑺傛暟鍙兘鍜屼綘鐨凧VM鐗堟湰鏈夊叧绯伙紝 浣犲彲浠ョ湅鐪嬬紪璇戝ソ鐨勭被鍒板簳鏈夊澶 + Assert.assertEquals(1056, byteCodes.length); + + } + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], + byteCodes[3] }; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} \ No newline at end of file From ffae32d40ff97265d7c2ca5f8c519942d07928db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E7=86=8A=E7=86=8A?= Date: Wed, 5 Apr 2017 14:31:05 +0800 Subject: [PATCH 2/5] complete StackUtil --- .../src/com/coding/basic/stack/StackUtil.java | 71 ++++++++++++++++--- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/group22/627559964/src/com/coding/basic/stack/StackUtil.java b/group22/627559964/src/com/coding/basic/stack/StackUtil.java index 51186c5e25..cb69d39c58 100644 --- a/group22/627559964/src/com/coding/basic/stack/StackUtil.java +++ b/group22/627559964/src/com/coding/basic/stack/StackUtil.java @@ -6,8 +6,12 @@ public class StackUtil { * 鍋囪鏍堜腑鐨勫厓绱犳槸Integer, 浠庢爤椤跺埌鏍堝簳鏄 : 5,4,3,2,1 璋冪敤璇ユ柟娉曞悗锛 鍏冪礌娆″簭鍙樹负: 1,2,3,4,5 * 娉ㄦ剰锛氬彧鑳戒娇鐢⊿tack鐨勫熀鏈搷浣滐紝鍗硃ush,pop,peek,isEmpty锛 鍙互浣跨敤鍙﹀涓涓爤鏉ヨ緟鍔 */ - public static void reverse(Stack s) { - + public static Stack reverse(Stack s) { + Stack s2 = new Stack(); + while(!s.isEmpty()) { + s2.push(s.pop()); + } + return s2; } /** @@ -15,8 +19,20 @@ public static void reverse(Stack s) { * * @param o */ - public static void remove(Stack s, Object o) { - + public static Stack remove(Stack s, Object o) { + Stack s2 = new Stack(); + while(!s.isEmpty()) { + if(!o.equals(s.peek())) { + s2.push(s.pop()); + } else { + s.pop(); + break; + } + } + while(!s2.isEmpty()) { + s.push(s2.pop()); + } + return s; } /** @@ -27,11 +43,21 @@ public static void remove(Stack s, Object o) { * @return */ public static Object[] getTop(Stack s, int len) { - return null; + Object[] obj = new Object[len]; + Stack s2 = new Stack(); + for (int i = 0; i < obj.length; i++) { + obj[i] = s.peek(); + s2.push(s.pop()); + } + while (!s2.isEmpty()) { + s.push(s2.pop()); + } + s2 = null; + return obj; } /** - * 瀛楃涓瞫 鍙兘鍖呭惈杩欎簺瀛楃锛 ( ) [ ] { }, a,b,c... x,yz 浣跨敤鍫嗘爤妫鏌ュ瓧绗︿覆s涓殑鎷彿鏄笉鏄垚瀵瑰嚭鐜扮殑銆 渚嬪s = + * 瀛楃涓瞫 鍙兘鍖呭惈杩欎簺瀛楃锛 ( ) [ ] { }, a,b,c... x,y,z 浣跨敤鍫嗘爤妫鏌ュ瓧绗︿覆s涓殑鎷彿鏄笉鏄垚瀵瑰嚭鐜扮殑銆 渚嬪s = * "([e{d}f])" , 鍒欒瀛楃涓蹭腑鐨勬嫭鍙锋槸鎴愬鍑虹幇锛 璇ユ柟娉曡繑鍥瀟rue 濡傛灉 s = "([b{x]y})", * 鍒欒瀛楃涓蹭腑鐨勬嫭鍙蜂笉鏄垚瀵瑰嚭鐜扮殑锛 璇ユ柟娉曡繑鍥瀎alse; * @@ -39,7 +65,36 @@ public static Object[] getTop(Stack s, int len) { * @return */ public static boolean isValidPairs(String s) { - return false; + Stack left = new Stack(); + Stack right = new Stack(); + char[] arr = new char[s.length()]; + arr = s.toCharArray(); + for (int i = 0; i < arr.length; i++) { + if ('('==(arr[i]) || '['==(arr[i]) || '{'==(arr[i])) { + left.push(arr[i]); + } + if (')'==(arr[i]) || ']'==(arr[i]) || '}'==(arr[i])) { + right.push(arr[i]); + } + } + right = StackUtil.reverse(right); + List list = new ArrayList(); + list.add("()"); + list.add("[]"); + list.add("{}"); + while (!left.isEmpty() && !right.isEmpty()) { + String temp = left.peek().toString() + right.peek().toString(); + if (list.contains(temp)) { + left.pop(); + right.pop(); + } else { + return false; + } + } + if (!left.isEmpty() || !right.isEmpty()) { + return false; + } + return true; } -} \ No newline at end of file +} From f9a2cad819d1aa1a40259e2990e34c5f5a3579f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E7=86=8A=E7=86=8A?= Date: Wed, 5 Apr 2017 14:33:26 +0800 Subject: [PATCH 3/5] complete ClassFileLoader --- .../src/com/jvm/ClassFileLoader.java | 52 ++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/group22/627559964/src/com/jvm/ClassFileLoader.java b/group22/627559964/src/com/jvm/ClassFileLoader.java index 4fb8b7b30e..1b0d6c0206 100644 --- a/group22/627559964/src/com/jvm/ClassFileLoader.java +++ b/group22/627559964/src/com/jvm/ClassFileLoader.java @@ -1,5 +1,11 @@ package com.jvm; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; @@ -7,18 +13,50 @@ public class ClassFileLoader { private List clzPaths = new ArrayList(); - public byte[] readBinaryCode(String className) { - - return null; - + public byte[] readBinaryCode(String className){ + File file = null; + for (String string : clzPaths) { + file = new File(string + className.replace(".", "/") + ".class"); + //TODO 宸叉壘鍒板氨閫鍑,鏄笉鏄湁闂锛 + if (file.exists()){ + break; + } + } + if (file == null) { + new FileNotFoundException("classpath涓湭鎵惧埌璇ユ枃浠"); + } + byte[] bt = new byte[(int) file.length()]; + byte[] buf = new byte[1024]; + + try { + InputStream in = new FileInputStream(file); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int n; + while ((n=in.read(buf)) != -1) { + out.write(buf, 0, n); + } + bt = out.toByteArray(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return bt; } public void addClassPath(String path) { - + clzPaths.add(path); } public String getClassPath() { - return null; + StringBuilder paths = new StringBuilder(); + for (int i = 0; i < clzPaths.size(); i++) { + paths.append(clzPaths.get(i)); + if(i != clzPaths.size() - 1) { + paths.append(";"); + } + } + return paths.toString(); } -} \ No newline at end of file +} From ed44638953825dee54d7a8ea86681d3edd5b09a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E7=86=8A=E7=86=8A?= Date: Wed, 5 Apr 2017 14:36:38 +0800 Subject: [PATCH 4/5] commit ClassFileloaderTest --- .../627559964/src/com/jvm/ClassFileloaderTest | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 group22/627559964/src/com/jvm/ClassFileloaderTest diff --git a/group22/627559964/src/com/jvm/ClassFileloaderTest b/group22/627559964/src/com/jvm/ClassFileloaderTest new file mode 100644 index 0000000000..02ace52171 --- /dev/null +++ b/group22/627559964/src/com/jvm/ClassFileloaderTest @@ -0,0 +1,78 @@ +package com.jvm; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.jvm.loader.ClassFileLoader; + +public class ClassFileloaderTest { + + static String path1 = "F:/workStu/ClassLoaderTest/bin/"; + static String path2 = "C:/temp"; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1 + ";" + path2, clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 娉ㄦ剰锛氳繖涓瓧鑺傛暟鍙兘鍜屼綘鐨凧VM鐗堟湰鏈夊叧绯伙紝 浣犲彲浠ョ湅鐪嬬紪璇戝ソ鐨勭被鍒板簳鏈夊澶 + Assert.assertEquals(1226, byteCodes.length); + + } + + @Test + public void testMagicNumber() { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[] { byteCodes[0], byteCodes[1], byteCodes[2], byteCodes[3] }; + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + private String byteToHexString(byte[] codes) { + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < codes.length; i++) { + byte b = codes[i]; + int value = b & 0xFF; + String strHex = Integer.toHexString(value); + if (strHex.length() < 2) { + strHex = "0" + strHex; + } + buffer.append(strHex); + } + return buffer.toString(); + } + +} From 38345e82d1f1b1d7b335852e331a2db1c778f38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E7=86=8A=E7=86=8A?= Date: Thu, 6 Apr 2017 09:04:49 +0800 Subject: [PATCH 5/5] Create LRUPageFrameTest --- .../coding/basic/linklist/LRUPageFrameTest | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 group22/627559964/src/com/coding/basic/linklist/LRUPageFrameTest diff --git a/group22/627559964/src/com/coding/basic/linklist/LRUPageFrameTest b/group22/627559964/src/com/coding/basic/linklist/LRUPageFrameTest new file mode 100644 index 0000000000..5e50d719c4 --- /dev/null +++ b/group22/627559964/src/com/coding/basic/linklist/LRUPageFrameTest @@ -0,0 +1,35 @@ +package com.coding.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()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +}