diff --git a/group23/601689050/ArrayList.java b/group23/601689050/1List&BinaryTreeNode/ArrayList.java similarity index 100% rename from group23/601689050/ArrayList.java rename to group23/601689050/1List&BinaryTreeNode/ArrayList.java diff --git a/group23/601689050/BinaryTreeNode.java b/group23/601689050/1List&BinaryTreeNode/BinaryTreeNode.java similarity index 100% rename from group23/601689050/BinaryTreeNode.java rename to group23/601689050/1List&BinaryTreeNode/BinaryTreeNode.java diff --git a/group23/601689050/Iterator.java b/group23/601689050/1List&BinaryTreeNode/Iterator.java similarity index 100% rename from group23/601689050/Iterator.java rename to group23/601689050/1List&BinaryTreeNode/Iterator.java diff --git a/group23/601689050/LinkedList.java b/group23/601689050/1List&BinaryTreeNode/LinkedList.java similarity index 100% rename from group23/601689050/LinkedList.java rename to group23/601689050/1List&BinaryTreeNode/LinkedList.java diff --git a/group23/601689050/List.java b/group23/601689050/1List&BinaryTreeNode/List.java similarity index 100% rename from group23/601689050/List.java rename to group23/601689050/1List&BinaryTreeNode/List.java diff --git a/group23/601689050/Queue.java b/group23/601689050/1List&BinaryTreeNode/Queue.java similarity index 100% rename from group23/601689050/Queue.java rename to group23/601689050/1List&BinaryTreeNode/Queue.java diff --git a/group23/601689050/Stack.java b/group23/601689050/1List&BinaryTreeNode/Stack.java similarity index 100% rename from group23/601689050/Stack.java rename to group23/601689050/1List&BinaryTreeNode/Stack.java diff --git a/group23/601689050/4LRU&JVM/JVM/ClassFileLoader.java b/group23/601689050/4LRU&JVM/JVM/ClassFileLoader.java new file mode 100644 index 0000000000..6a7ab61bc0 --- /dev/null +++ b/group23/601689050/4LRU&JVM/JVM/ClassFileLoader.java @@ -0,0 +1,63 @@ +package com.loader; + +import java.io.*; +import java.util.ArrayList; +import java.util.Dictionary; +import java.util.List; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList<>(); + + public byte[] readBinaryCode(String className) throws ClassNotFoundException { + + String fileName = className.replace('.',File.separatorChar)+".class"; + InputStream is = null; + try{ + is = new FileInputStream(fileName); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length = 0; + while((length = is.read(buffer)) != -1){ + baos.write(buffer,0,length); + } + return baos.toByteArray(); + } catch (IOException e){ + e.printStackTrace(); + } finally { + if(is!=null){ + try{ + is.close(); + } catch (IOException e){ + e.printStackTrace(); + } + } + } + return null; + } + + + + + public void addClassPath(String path) { + + StringBuilder str = new StringBuilder(path); + + } + + + + public String getClassPath(){ + + String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath(); + + return path; + } + + + + + +} diff --git a/group23/601689050/4LRU&JVM/JVM/ClassFileloaderTest.java b/group23/601689050/4LRU&JVM/JVM/ClassFileloaderTest.java new file mode 100644 index 0000000000..eae77d6dd4 --- /dev/null +++ b/group23/601689050/4LRU&JVM/JVM/ClassFileloaderTest.java @@ -0,0 +1,88 @@ +package com.test; + +import com.loader.ClassFileLoader; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + + +public class ClassFileloaderTest { + + + public static String path1 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + public 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() throws ClassNotFoundException { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length-1); + + } + + + @Test + public void testMagicNumber() throws ClassNotFoundException { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "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 capacity) { + last = last.prev; + last.next = null; + } + } else { + moveToFirst(iteratorNode); + } + } + } + + } + + private void moveLastToFirst() { + last.next = first; + first.prev = last; + first = last; + last = last.prev; + last.next = null; + first.prev = null; + } + private void moveToFirst(Node iteratorNode){ + iteratorNode.prev.next = iteratorNode.next; + iteratorNode.next.prev = iteratorNode.prev; + iteratorNode.prev = null; + iteratorNode.next = first; + first.prev = iteratorNode; + first = iteratorNode; + } + + private void addToFirst(int pageNum) { + Node node = new Node(); + node.pageNum = pageNum; + node.prev = null; + node.next = first; + first.prev = node; + first = node; + size++; + } + + + 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(); + } + +} diff --git a/group23/601689050/4LRU&JVM/LRU/LRUPageFrameTest.java b/group23/601689050/4LRU&JVM/LRU/LRUPageFrameTest.java new file mode 100644 index 0000000000..a7dd99ec58 --- /dev/null +++ b/group23/601689050/4LRU&JVM/LRU/LRUPageFrameTest.java @@ -0,0 +1,29 @@ +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()); + } + +} diff --git a/group23/601689050/4LRU&JVM/LRU/LinkedList.java b/group23/601689050/4LRU&JVM/LRU/LinkedList.java new file mode 100644 index 0000000000..4a71816e5d --- /dev/null +++ b/group23/601689050/4LRU&JVM/LRU/LinkedList.java @@ -0,0 +1,313 @@ +import java.util.Objects; +import java.util.Stack; + +public class LinkedList implements List { + + private Node head; + private Node rear; + public boolean isEmpty(){ + return true; + } + + public void add(Object o){ + if(isEmpty()) + addFirst(o); + else + addLast(o); + } + public void add(int index , Object o){ + if(index<0 || o ==null){ + throw new IllegalArgumentException("不合法"); + } else if (index == 0 && head == null){ + addFirst(o); + }else if(index>0 && head == null){ + throw new IllegalArgumentException("不合法"); + }else{ + Node srcNode = (Node) this.get(index); + Node newNode = new Node(); + newNode.data = o; + newNode.next = srcNode.next; + srcNode.next.previous = newNode; + srcNode.next = newNode; + newNode.previous = srcNode; + } + } + public Object get(int index){ + Node newNode = new Node(); + for(int i = 0;i7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + Node p1,p2 = null; + p1 = head; + while (head.next != null){ + p2 = head.next; + head.next = p2.next; + p2.next = p1; + p1 = p2; + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int size = this.size(); + Node newNode = new Node(); + newNode = head; + if(size%2 == 0){ + int length = size/2; + for(int i = 0;i101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] listB = new int[list.size()-1]; + int[] listA = new int[list.size()-1]; + int size = list.size(); + for(int i=0;i min && (int)newNode.data min && (int)newNode.data > max){ + break; + } + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList listA = this; + LinkedList listB = list; + LinkedList listC = new LinkedList(); + if(listA == null){ + return list; + } + if(listB == null){ + return this; + } + Node p1 = listA.head; + Node p2 = listB.head; + Node p3 = listC.head; + while(p1 != null && p2 !=null) { + if ((int) p1.data <= (int) p2.data) { + p3.next = p1; + p1 = p1.next; + } else { + p3.next = p2; + p2 = p2.next; + } + p3 = p3.next; + } + if(p1 == null){ + p3.next = p2; + } + if(p2 == null){ + p3.next = p1; + } + return listC; + } +} diff --git a/group23/601689050/4LRU&JVM/LRU/List.java b/group23/601689050/4LRU&JVM/LRU/List.java new file mode 100644 index 0000000000..4f7bcc71a8 --- /dev/null +++ b/group23/601689050/4LRU&JVM/LRU/List.java @@ -0,0 +1,7 @@ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +}