diff --git a/group21/315752375/homework20170226/.classpath b/group21/315752375/.classpath similarity index 100% rename from group21/315752375/homework20170226/.classpath rename to group21/315752375/.classpath diff --git a/group21/315752375/.gitignore b/group21/315752375/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group21/315752375/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group21/315752375/homework20170226/.project b/group21/315752375/.project similarity index 91% rename from group21/315752375/homework20170226/.project rename to group21/315752375/.project index b571c262ea..b4bd3f32d4 100644 --- a/group21/315752375/homework20170226/.project +++ b/group21/315752375/.project @@ -1,6 +1,6 @@ - homework20170226 + homework diff --git a/group21/315752375/.settings/org.eclipse.core.resources.prefs b/group21/315752375/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..00b5777565 --- /dev/null +++ b/group21/315752375/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding//src/com/coderising/array/ArrayUtil.java=UTF-8 +encoding/=UTF-8 diff --git a/group21/315752375/homework20170219/.gitignore b/group21/315752375/homework20170219/.gitignore deleted file mode 100644 index ff3a56dc08..0000000000 --- a/group21/315752375/homework20170219/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/bin/ - -*.classpath -*.project \ No newline at end of file diff --git a/group21/315752375/homework20170219/src/com/coding/basic/LinkedList.java b/group21/315752375/homework20170219/src/com/coding/basic/LinkedList.java deleted file mode 100644 index 34f50a48a7..0000000000 --- a/group21/315752375/homework20170219/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head;//头结点 - private int size;//单链表中元素个数 - public LinkedList() { - head = new Node(); - head.next=null; - size=0; - } - public void add(Object o){ - addLast(o); - - } - public void add(int index , Object o){ - rangeCheckAdd(index); - Node preNode=getNode(index-1); - Node node=new Node(); - node.data=o; - node.next=preNode.next; - preNode.next=node; - size++; - } - private Node getNode(int index) { - rangeCheck(index); - int count=-1; - Node node=head; - while(count!=index){ - node=node.next; - count++; - } - return node; - } - public Object get(int index){ - return getNode(index).data; - } - public Object remove(int index){ - rangeCheck(index); - Node preNode=getNode(index-1); - Node node=preNode.next; - preNode.next=preNode.next.next; - size--; - return node.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node node=new Node(); - node.data=o; - node.next=head.next; - head.next=node; - size++; - } - public void addLast(Object o){ - add(size, o); - } - public Object removeFirst(){ - return remove(0); - } - public Object removeLast(){ - return remove(size-1); - } - public Iterator iterator(){ - return new LinkedIterator(); - } - private void rangeCheck(int index){//头结点为-1,用于get - if(index<-1||index>size-1)throw new IndexOutOfBoundsException(); - } - private void rangeCheckAdd(int index){//用于add - if(index<0||index>size)throw new IndexOutOfBoundsException(); - } - - private static class Node{ - Object data; - Node next; - } - private class LinkedIterator implements Iterator{ - Node nextNode=head.next; - @Override - public boolean hasNext() { - // TODO Auto-generated method stub - return nextNode!=null; - } - - @Override - public Object next() { - // TODO Auto-generated method stub - Object object=nextNode.data; - nextNode=nextNode.next; - return object; - } - - } -} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/LinkedListTest.java b/group21/315752375/homework20170219/src/com/coding/basic/LinkedListTest.java deleted file mode 100644 index e692aa7615..0000000000 --- a/group21/315752375/homework20170219/src/com/coding/basic/LinkedListTest.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.coding.basic; - -import static org.junit.Assert.*; - -import org.junit.Test; - - - - -public class LinkedListTest { - - @Test - public void testLinkedList() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - } - - @Test - public void testAddObject() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - linkedList.add("1"); - } - - @Test - public void testAddIntObject() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - linkedList.add(0,"2"); - } - @Test - public void testGet() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - linkedList.add(0,"2"); - System.out.println(linkedList.get(0)); - } - @Test - public void testRemove() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - linkedList.add(0,"2"); - System.out.println(linkedList.size()); - linkedList.remove(0); - System.out.println(linkedList.size()); - } - - @Test - public void testSize() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - System.out.println(linkedList.size()); - linkedList.add(0,"2"); - linkedList.add(0,"2"); - linkedList.add(0,"2"); - linkedList.add(0,"2"); - linkedList.add(0,"2"); - linkedList.add(0,"2"); - linkedList.add(0,"2"); - linkedList.add(0,"2"); - System.out.println(linkedList.size()); - linkedList.remove(0); - System.out.println(linkedList.size()); - } - - @Test - public void testAddFirst() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - linkedList.add(0,"1"); - linkedList.add(1,"2"); - linkedList.addFirst("3"); - System.out.println(linkedList.get(0)); - System.out.println(linkedList.get(1)); - System.out.println(linkedList.get(2)); - } - - @Test - public void testAddLast() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - linkedList.add(0,"1"); - linkedList.add(1,"2"); - linkedList.addLast("3"); - System.out.println(linkedList.get(0)); - System.out.println(linkedList.get(1)); - System.out.println(linkedList.get(2)); - - } - - @Test - public void testRemoveFirst() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - linkedList.add(0,"1"); - linkedList.add(1,"2"); - linkedList.addLast("3"); - System.out.println(linkedList.get(0)); - System.out.println(linkedList.get(1)); - System.out.println(linkedList.get(2)); - linkedList.removeFirst(); - System.out.println(linkedList.get(0)); - System.out.println(linkedList.get(1)); - } - - @Test - public void testRemoveLast() { - fail("Not yet implemented"); - LinkedList linkedList=new LinkedList(); - linkedList.add(0,"1"); - linkedList.add(1,"2"); - linkedList.addLast("3"); - System.out.println(linkedList.get(0)); - System.out.println(linkedList.get(1)); - System.out.println(linkedList.get(2)); - linkedList.removeLast(); - System.out.println(linkedList.get(0)); - System.out.println(linkedList.get(1)); - } - - @Test - public void testIterator() { - LinkedList linkedList=new LinkedList(); - linkedList.add(0,"1"); - linkedList.add(1,"2"); - linkedList.add(2,"3"); - linkedList.add(3,"4"); - linkedList.add(4,"5"); - linkedList.add(5,"6"); - linkedList.add(6,"7"); - linkedList.add(7,"8"); - Iterator iterator=linkedList.iterator(); - while(iterator.hasNext()){ - System.out.println(iterator.next()); - } - } - -} diff --git a/group21/315752375/homework20170226/.gitignore b/group21/315752375/homework20170226/.gitignore deleted file mode 100644 index dbc5ccaae7..0000000000 --- a/group21/315752375/homework20170226/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/bin/ -/.settings diff --git a/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtil.java b/group21/315752375/src/com/coderising/array/ArrayUtil.java similarity index 75% rename from group21/315752375/homework20170226/src/com/coderising/array/ArrayUtil.java rename to group21/315752375/src/com/coderising/array/ArrayUtil.java index 5355821118..4a3c98e57a 100644 --- a/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtil.java +++ b/group21/315752375/src/com/coderising/array/ArrayUtil.java @@ -28,10 +28,10 @@ public static void reverseArray(int[] origin) { */ public static int[] removeZero(int[] oldArray) { - int last = 0;// 璁板綍鏈鍚庝竴涓厓绱犵殑浣嶇疆 + int last = 0;// 鐠佹澘缍嶉張锟芥倵娑擄拷閲滈崗鍐閻ㄥ嫪缍呯純锟 int length = oldArray.length; - int zero = 0;// 璁板綍鏁扮粍涓涓涓0鐨勪綅缃 - int nonzero = 1;// 璁板綍zero鍚庨潰绗竴涓潪闆剁殑浣嶇疆 + int zero = 0;// 鐠佹澘缍嶉弫鎵矋娑擃厾顑囨稉锟介嚋0閻ㄥ嫪缍呯純锟 + int nonzero = 1;// 鐠佹澘缍峼ero閸氬酣娼扮粭顑跨娑擃亪娼梿鍓佹畱娴e秶鐤 while (zero < length - 1) { if (oldArray[zero] == 0) { // System.out.println("zero:" + zero); @@ -67,12 +67,12 @@ public static int[] removeZero(int[] oldArray) { */ public static int[] merge(int[] array1, int[] array2) { - int index1 = 0;// 鎸囧悜涓嬩竴涓厓绱 - int index2 = 0;// 鎸囧悜涓嬩竴涓厓绱 + int index1 = 0;// 閹稿洤鎮滄稉瀣╃娑擃亜鍘撶槐锟 + int index2 = 0;// 閹稿洤鎮滄稉瀣╃娑擃亜鍘撶槐锟 int len1 = array1.length; int len2 = array2.length; int[] target = new int[len1 + len2]; - int index = 0;// 鎸囧悜涓嬩竴涓┖浣嶇疆 + int index = 0;// 閹稿洤鎮滄稉瀣╃娑擃亞鈹栨担宥囩枂 int min = 0; while (index1 < len1 && index2 < len2) { if (array1[index1] < array2[index2]) { @@ -87,7 +87,6 @@ public static int[] merge(int[] array1, int[] array2) { System.arraycopy(array1, index1, target, index, len1 - index1); return target; } - /** * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 @@ -115,13 +114,14 @@ public static int[] grow(int[] oldArray, int size) { * @return */ public static int[] fibonacci(int max) { - int first = 0;// 璁板綍绗竴涓暟 + long first = 0;// 鐠佹澘缍嶇粭顑跨娑擃亝鏆 int[] target = new int[max]; int count = 0; - int i = 1;// 璁板綍鍚庝竴涓暟 - while (i < max) { - int tmp = i; - target[count++] = i; + long i = 1;// 鐠佹澘缍嶉崥搴濈娑擃亝鏆 + long max1 = max; + while (i < max1) { + long tmp = i; + target[count++] = (int) i; i = i + first; first = tmp; } @@ -136,21 +136,21 @@ public static int[] fibonacci(int max) { * @param max * @return */ - public static int[] getPrimes(int max) {// 绱犳暟绛涢夋硶 + public static int[] getPrimes(int max) {// 缁辩姵鏆熺粵娑拷濞夛拷 boolean[] primes = new boolean[max]; int[] target = new int[max / 2]; int count = 0; - for (int i = 1; i < max; i += 2) {// 鍏ㄩ儴鍋舵暟榛樿false锛屽鏁拌涓簍rue + for (int i = 1; i < max; i += 2) {// 閸忋劑鍎撮崑鑸垫殶姒涙ǹ顓籪alse閿涘苯顨岄弫鎷岊啎娑撶皪rue primes[i] = true; } - primes[2] = true;// 2涓哄伓鏁颁絾鏄槸绱犳暟 - for (int i = 3; i < (int) Math.sqrt(max); i += 2) {// 0锛1闈炵礌鏁伴潪鍚堟暟锛岃繕鏈2鏄礌鏁帮紝鎵浠ヤ粠3寮濮 + primes[2] = true;// 2娑撳搫浼撻弫棰佺稻閺勵垱妲哥槐鐘虫殶 + for (int i = 3; i < (int) Math.sqrt(max); i += 2) {// 0閿涳拷闂堢偟绀岄弫浼存姜閸氬牊鏆熼敍宀冪箷閺堬拷閺勵垳绀岄弫甯礉閹碉拷浜掓禒锟藉锟筋潗 if (primes[i]) - for (int j = i + i; j < max; j += i) {// 灏唅鐨勬暣鍊嶆暟涓嶆槸绱犳暟锛岃涓篺alse + for (int j = i + i; j < max; j += i) {// 鐏忓攨閻ㄥ嫭鏆i崐宥嗘殶娑撳秵妲哥槐鐘虫殶閿涘矁顔曟稉绡篴lse primes[j] = false; } } - for (int i = 2; i < max; i++) {// 鏈鍚庢墍鏈変笅鏍囦粠2寮濮嬬殑true涓虹礌鏁 + for (int i = 2; i < max; i++) {// 閺堬拷鎮楅幍锟芥箒娑撳鐖f禒锟藉锟筋潗閻ㄥ墖rue娑撹櫣绀岄弫锟 if (primes[i]) target[count++] = i; } @@ -165,31 +165,33 @@ public static int[] getPrimes(int max) {// 绱犳暟绛涢夋硶 * @param max * @return */ + public static int[] getPerfectNumbers(int max) { long start = System.currentTimeMillis(); - int sum = 0; + long sum = 0; + long max1=max; int[] array = new int[50]; int count = 0; int count1 = 5; - for (int i = 1; i < max;) { + for (long i = 1; i < max1;) { try { -// System.out.println("i:" + i + " sum:" + sum); + // System.out.println("i:" + i + " sum:" + sum); if (i % 10 != 6 && i % 10 != 8) { continue; } else if (i != 6) { -// System.out.println(i + " " + i % 3 + " " + i % 9); + // System.out.println(i + " " + i % 3 + " " + i % 9); if (i % 3 != 1 || i % 9 != 1) { continue; } } - sum = 1;// 1宸蹭竴瀹氫负鍥犲瓙 - // System.out.print("鏁"+i+"鐨勫洜瀛愶細"); - // System.out.print(1+" "); - for (int j = 2; j < Math.sqrt(i); j++) { + sum = 1;// 1瀹歌弓绔寸规矮璐熼崶鐘茬摍 +// System.out.print("閺侊拷+i+"閻ㄥ嫬娲滅涙劧绱"); +// System.out.print(1+" "); + for (long j = 2; j < Math.sqrt(i); j++) { if (i % j == 0) { sum += j; sum += i / j; - // System.out.print(j+" "+i/j+" "); +// System.out.print(j+" "+i/j+" "); } } @@ -197,24 +199,24 @@ public static int[] getPerfectNumbers(int max) { sum += (int) Math.sqrt(i); } // System.out.println(); -// System.out.println("i:" + i + " sum:" + sum); + System.out.println("i:" + i + " sum:" + sum); if (sum == i) - array[count++] = i; + array[count++] = (int)i; } finally { if (i < 28) i++; else { i = i + (int) Math.pow(count1, 3); count1 += 2; -// System.out.println(i); + System.out.println(i); } } } int[] answer = new int[count]; System.arraycopy(array, 0, answer, 0, count); - long cost = System.currentTimeMillis()-start; - System.out.println("cost time: "+cost+"ms"); + long cost = System.currentTimeMillis() - start; + System.out.println("cost time: " + cost + "ms"); return answer; } diff --git a/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtilTest.java b/group21/315752375/src/com/coderising/array/ArrayUtilTest.java similarity index 85% rename from group21/315752375/homework20170226/src/com/coderising/array/ArrayUtilTest.java rename to group21/315752375/src/com/coderising/array/ArrayUtilTest.java index 9943556a30..14fde0da00 100644 --- a/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtilTest.java +++ b/group21/315752375/src/com/coderising/array/ArrayUtilTest.java @@ -1,16 +1,14 @@ package com.coderising.array; - import java.util.Arrays; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; - @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ArrayUtilTest { - + @Test public void testReverseArray() { System.out @@ -44,7 +42,8 @@ public void testMerge() { System.out.println("sorted array2:" + Arrays.toString(merge2)); int[] target = new int[merge1.length + merge2.length]; target = ArrayUtil.merge(merge1, merge2); - System.out.println("after merging and sorting:" + Arrays.toString(target)); + System.out.println("after merging and sorting:" + + Arrays.toString(target)); } @Test @@ -65,8 +64,8 @@ public void testFibonacci() { System.out .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); System.out.println("test fibonacci(int max)"); - int num=19; - System.out.print("灏忎簬 "+num+" 鐨勬枑娉㈤偅濂戞暟鍒:"); + int num = 19; + System.out.print("灏忎簬 " + num + " 鐨勬枑娉㈤偅濂戞暟鍒:"); System.out.println(Arrays.toString(ArrayUtil.fibonacci(15))); } @@ -85,8 +84,9 @@ public void testGetPerfectNumbers() { System.out .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); System.out.println("test getPrimes(int max)"); - int num=33550337; - System.out.println("灏忎簬 "+num+" 鐨勫畬鏁版湁锛"+Arrays.toString(ArrayUtil.getPerfectNumbers(num))); + int num = 2147483647; + System.out.println("灏忎簬 " + num + " 鐨勫畬鏁版湁锛" + + Arrays.toString(ArrayUtil.getPerfectNumbers(num))); } @Test @@ -95,10 +95,10 @@ public void testJoin() { .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); System.out.println("test join(int[] array, String seperator)"); int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; - String seperator="+"; - System.out.println("use "+"\""+seperator+"\""+" to join array"); - System.out.println("old array:"+Arrays.toString(array)); - System.out.println("after joining:"+ArrayUtil.join(array, "-")); + String seperator = "+"; + System.out.println("use " + "\"" + seperator + "\"" + " to join array"); + System.out.println("old array:" + Arrays.toString(array)); + System.out.println("after joining:" + ArrayUtil.join(array, "-")); } } diff --git a/group21/315752375/src/com/coderising/jvm/loader/ClassFileLoader.java b/group21/315752375/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..1d46972a03 --- /dev/null +++ b/group21/315752375/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,89 @@ +package com.coderising.jvm.loader; + +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.Iterator; +import java.util.List; + + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + String path=""; + File file=null; + Iterator iterator=clzPaths.iterator(); + className=className.replace(".", File.separator); + + while(iterator.hasNext()){ + path=iterator.next().replace("\\","/"); + path+=File.separator+className+".class"; + file=new File(path); + if(file.exists())break; + } + InputStream inputStream=null; + try { + inputStream=new FileInputStream(file); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + byte[] tmp=new byte[1024]; + byte[]answer=new byte[(int)file.length()]; + int len=0; + int index=0; + try { + while((len=inputStream.read(tmp))!=-1){ + System.arraycopy(tmp, 0, answer, index, len); + index+=len; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + try { + inputStream.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + return answer; + + + } + + + public void addClassPath(String path) { + if(clzPaths.contains(path)){ + return; + } + clzPaths.add(path); + } + + + + public String getClassPath(){ + String answer=""; + Iterator iterator=clzPaths.iterator(); + while(iterator.hasNext()){ + answer+=iterator.next()+";"; + } + int len=answer.length(); + if(len>0)answer=answer.substring(0, len-1); + return answer; + } + + + + + +} diff --git a/group21/315752375/src/com/coderising/jvm/test/ClassFileloaderTest.java b/group21/315752375/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..e8f8c6c66e --- /dev/null +++ b/group21/315752375/src/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,92 @@ +package com.coderising.jvm.test; + +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 = "H:\\璇句欢\\缂栫▼鎻愰珮鐩稿叧\\coding2017\\group21\\315752375\\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 0) { + randomAccessFile.write(tmp, 0, length); + total += length; + rFile.seek(0); + rFile.write((startIndex + total + "").getBytes("UTF-8")); + } + rFile.close(); + inputStream.close(); + randomAccessFile.close(); + cleanTemp(tmpFile); + System.out.println("thread "+id+"涓嬭浇瀹屾垚"); + }else{ + System.out.println("鍝嶅簲鍚楋細"+code+"--涓嶆敮鎸佸绾跨▼涓嬭浇"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static String getFileName(URL url) { + String name = url.getFile(); + name = name.substring(name.lastIndexOf("/") + 1); + return name; + } + private synchronized void cleanTemp(File file){ + file.delete(); + } +} diff --git a/group21/315752375/src/com/coderising/multiThreadDownloader/MultiThreadDownloader.java b/group21/315752375/src/com/coderising/multiThreadDownloader/MultiThreadDownloader.java new file mode 100644 index 0000000000..8f1a43c71f --- /dev/null +++ b/group21/315752375/src/com/coderising/multiThreadDownloader/MultiThreadDownloader.java @@ -0,0 +1,58 @@ +package com.coderising.multiThreadDownloader; + +import java.io.File; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.URL; + +public class MultiThreadDownloader { + private String downloadAdress = "http://img.xmpig.com/forum/201610/20/201816z4zvp3zov8vtpp5g.jpg"; + private String storePathString = "f:/"; + private int threadNum = 4; + + public MultiThreadDownloader() { + } + + public MultiThreadDownloader(String downloadAdress, String storePathString, + int threadNum) { + this.downloadAdress = downloadAdress; + this.storePathString = storePathString; + this.threadNum = threadNum; + } + + public void download() { + try { + URL url = new URL(downloadAdress); + HttpURLConnection httpURLConnection = (HttpURLConnection) url + .openConnection(); + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setReadTimeout(10000); + int code=httpURLConnection.getResponseCode(); + if (code== 200) { + int len = httpURLConnection.getContentLength(); + RandomAccessFile rFile = new RandomAccessFile(new File(storePathString, DownloadThread.getFileName(url)), "rw"); + rFile.setLength(len); + int threadSize = len / threadNum; + for (int id = 0; id < threadNum; id++) { + int startIndex = id * threadSize; + int endIndex = (id + 1) * threadSize - 1; + if (id == threadNum - 1) { + endIndex = len - 1; + } + Thread thread = new DownloadThread(id, downloadAdress, + storePathString, startIndex, endIndex); + thread.start(); + } + Thread.sleep(3000); + System.out.println("download success"); + } + }catch (Exception e) { + e.printStackTrace(); + } + + } + public static void main(String[] args) { + MultiThreadDownloader mt=new MultiThreadDownloader(); + mt.download(); + } +} diff --git a/group21/315752375/src/com/coderising/multiThreadDownloader/MultiThreadDownloaderTest.java b/group21/315752375/src/com/coderising/multiThreadDownloader/MultiThreadDownloaderTest.java new file mode 100644 index 0000000000..f076106c78 --- /dev/null +++ b/group21/315752375/src/com/coderising/multiThreadDownloader/MultiThreadDownloaderTest.java @@ -0,0 +1,15 @@ +package com.coderising.multiThreadDownloader; + + +import org.junit.Test; + +public class MultiThreadDownloaderTest { + + @Test + public void testDownload() { + MultiThreadDownloader downloader=new MultiThreadDownloader(); + downloader.download(); +// System.out.println(this.getClass().getResource("").getPath()); + } + +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/ArrayList.java b/group21/315752375/src/com/coding/basic/ArrayList.java similarity index 100% rename from group21/315752375/homework20170219/src/com/coding/basic/ArrayList.java rename to group21/315752375/src/com/coding/basic/ArrayList.java diff --git a/group21/315752375/homework20170219/src/com/coding/basic/ArrayListTest.java b/group21/315752375/src/com/coding/basic/ArrayListTest.java similarity index 100% rename from group21/315752375/homework20170219/src/com/coding/basic/ArrayListTest.java rename to group21/315752375/src/com/coding/basic/ArrayListTest.java diff --git a/group21/315752375/homework20170219/src/com/coding/basic/BinaryTree.java b/group21/315752375/src/com/coding/basic/BinaryTree.java similarity index 100% rename from group21/315752375/homework20170219/src/com/coding/basic/BinaryTree.java rename to group21/315752375/src/com/coding/basic/BinaryTree.java diff --git a/group21/315752375/homework20170219/src/com/coding/basic/BinaryTreeTest.java b/group21/315752375/src/com/coding/basic/BinaryTreeTest.java similarity index 100% rename from group21/315752375/homework20170219/src/com/coding/basic/BinaryTreeTest.java rename to group21/315752375/src/com/coding/basic/BinaryTreeTest.java diff --git a/group21/315752375/homework20170219/src/com/coding/basic/Iterator.java b/group21/315752375/src/com/coding/basic/Iterator.java similarity index 100% rename from group21/315752375/homework20170219/src/com/coding/basic/Iterator.java rename to group21/315752375/src/com/coding/basic/Iterator.java diff --git a/group21/315752375/src/com/coding/basic/LinkedList.java b/group21/315752375/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..58b8dfecff --- /dev/null +++ b/group21/315752375/src/com/coding/basic/LinkedList.java @@ -0,0 +1,297 @@ +package com.coding.basic; + +import java.util.Arrays; + + +public class LinkedList> { + + private Node head;//澶寸粨鐐 + private int size;//鍗曢摼琛ㄤ腑鍏冪礌涓暟 + public LinkedList() { + head = new Node<>(); + head.next=null; + size=0; + } + public void add(T o){ + addLast(o); + + } + public void add(int index , T o){ + rangeCheckAdd(index); + Node preNode=getNodeByIndex(index-1); + Node node=new Node<>(); + node.data=o; + node.next=preNode.next; + preNode.next=node; + size++; + } + private Node getNodeByIndex(int index) { + rangeCheck(index); + int count=-1; + Node node=head; + while(count!=index){ + node=node.next; + count++; + } + return node; + } + public T get(int index){ + return (T)getNodeByIndex(index).data; + } + public Node getNodeByData(T object){ + Node node=head; + while(node.next!=null){ + node=node.next; + if(node.data.equals(object))return node; + } + return null; + } + public Node getPreNodeByData(T object){ + Node preNode=head; + while(preNode.next!=null){ + if(preNode.next.data.equals(object))return preNode; + preNode=preNode.next; + } + return null; + } + public T remove(int index){ + rangeCheck(index); + Node preNode=getNodeByIndex(index-1); + Node node=preNode.next; + preNode.next=preNode.next.next; + size--; + return (T)node.data; + } + + public int size(){ + return size; + } + + public void addFirst(T o){ + Node node=new Node(); + node.data=o; + node.next=head.next; + head.next=node; + size++; + } + public void addLast(T o){ + add(size, o); + } + public T removeFirst(){ + return remove(0); + } + public T removeLast(){ + return remove(size-1); + } + private class LinkedIterator implements Iterator{ + Node nextNode=head.next; + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return nextNode!=null; + } + + @Override + public T next() { + // TODO Auto-generated method stub + T object=(T)nextNode.data; + nextNode=nextNode.next; + return object; + } + + } + public Iterator iterator(){ + return new LinkedIterator(); + } + private void rangeCheck(int index){//澶寸粨鐐逛负-1锛岀敤浜巊et + if(index<-1||index>size-1)throw new IndexOutOfBoundsException(); + } + private void rangeCheckAdd(int index){//鐢ㄤ簬add + if(index<0||index>size)throw new IndexOutOfBoundsException(); + } + + private static class Node>{ + T data; + Node next; + } + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + Node headNode=new Node<>(); + Node curNode=head.next; + while(curNode!=null){ + Node nextNode=curNode.next; + curNode.next=headNode.next; + headNode.next=curNode; + curNode=nextNode; + } + head=headNode; + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + int len=(int)size/2; + int count=0; + while(count++ first=getNodeByIndex(i-1); + while(count101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] tmp=new int[list.size]; + Iterator iterator=list.iterator(); + Node curNode=head.next; + int count=0; + int curIndex=0; + int arraysIndex=0; + while(iterator.hasNext()){ + count=(int)iterator.next(); + while(count!=curIndex){ + if(curNode==null)break; + curNode=curNode.next; + curIndex++; + } + if(curNode!=null){ + tmp[arraysIndex++]=(Integer)curNode.data; + } + else break; + } + int[] answer=Arrays.copyOf(tmp, arraysIndex); + return answer; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + Iterator iterator=list.iterator(); + Node preNode=head; + while (iterator.hasNext()) { + if(preNode==null)return; + T tmp=(T)iterator.next(); + while(preNode.next!=null){ + if(preNode.next.data.compareTo(tmp)==0){ + preNode.next=preNode.next.next; + size--; + break; + } + else if(preNode.next.data.compareTo(tmp)<0) + preNode=preNode.next; + else break; + } + } + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + Node node=head; + if(node.next==null)return; + node=node.next; + while(node!=null){ + if(node.next==null)return; + else if(node.data.compareTo(node.next.data)==0){ + node.next=node.next.next; + size--; + } + node=node.next; + } + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(T min, T max){ + Node preNode=head; + Node lastNode=null; + if(preNode.next==null)return; + while(preNode.next!=null){ + if(preNode.next.data.compareTo(min)>0)break; + preNode=preNode.next; + size--; + } + lastNode=preNode; + if(lastNode.next==null)return; + while (lastNode.next!=null) { + if(lastNode.next.data.compareTo(max)>=0)break; + lastNode=lastNode.next; + size--; + } + preNode.next=lastNode; + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList linkedList=new LinkedList<>(); + Node node1=head; + Node node2=list.head; + if(node1.next==null||node2.next==null)return linkedList; + node1=node1.next; + node2=node2.next; + int count=0; + while(node1!=null&&node2!=null){ +// System.out.print("node1: "+node1.data+"node2: "+node2.data);; + + if(node1.data.compareTo(node2.data)==0){ + linkedList.add(node2.data); + node1=node1.next; + node2=node2.next; + + } + else if(node1.data.compareTo(node2.data)>0){ + node2=node2.next; + } + else { + node1=node1.next; + } + } + return linkedList; + } +} diff --git a/group21/315752375/src/com/coding/basic/LinkedListTest.java b/group21/315752375/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..85968db544 --- /dev/null +++ b/group21/315752375/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,444 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class LinkedListTest { + + @Test + public void testLinkedList() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test linkedList Contructor"); + LinkedList linkedList = new LinkedList(); + } + + @Test + public void testAddObject() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test addObject(T o)"); + LinkedList linkedList = new LinkedList(); + linkedList.add("1"); + System.out.println(linkedList.get(0)); + } + + @Test + public void testAddIntObject() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test addObject(int index,T o)"); + LinkedList linkedList = new LinkedList(); + linkedList.add(0, "2"); + System.out.println(linkedList.get(0)); + } + + @Test + public void testGet() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test get(int index)"); + LinkedList linkedList = new LinkedList(); + linkedList.add(0, "2"); + System.out.println(linkedList.get(0)); + } + + @Test + public void testRemove() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test remove(int index)"); + LinkedList linkedList = new LinkedList(); + linkedList.add(0, "2"); + System.out.println("size before removing" + linkedList.size()); + linkedList.remove(0); + System.out.println("size after removing" + linkedList.size()); + } + + @Test + public void testSize() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test size()"); + LinkedList linkedList = new LinkedList(); + System.out.println(linkedList.size()); + linkedList.add(0, "2"); + linkedList.add(0, "2"); + linkedList.add(0, "2"); + linkedList.add(0, "2"); + linkedList.add(0, "2"); + linkedList.add(0, "2"); + linkedList.add(0, "2"); + linkedList.add(0, "2"); + System.out.println(linkedList.size()); + linkedList.remove(0); + System.out.println(linkedList.size()); + } + + @Test + public void testAddFirst() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test addFirst()"); + LinkedList linkedList = new LinkedList(); + linkedList.add(0, "1"); + linkedList.add(1, "2"); + linkedList.addFirst("3"); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + System.out.println(linkedList.get(2)); + } + + @Test + public void testAddLast() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test addLast()"); + LinkedList linkedList = new LinkedList(); + linkedList.add(0, "1"); + linkedList.add(1, "2"); + linkedList.addLast("3"); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + System.out.println(linkedList.get(2)); + + } + + @Test + public void testRemoveFirst() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test removeFirst()"); + LinkedList linkedList = new LinkedList(); + linkedList.add(0, "1"); + linkedList.add(1, "2"); + linkedList.addLast("3"); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + System.out.println(linkedList.get(2)); + linkedList.removeFirst(); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + } + + @Test + public void testRemoveLast() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test removeLast()"); + LinkedList linkedList = new LinkedList(); + linkedList.add(0, "1"); + linkedList.add(1, "2"); + linkedList.addLast("3"); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + System.out.println(linkedList.get(2)); + linkedList.removeLast(); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + } + + @Test + public void testIterator() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test iterator()"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(0, "1"); + linkedList.add(1, "2"); + linkedList.add(2, "3"); + linkedList.add(3, "4"); + linkedList.add(4, "5"); + linkedList.add(5, "6"); + linkedList.add(6, "7"); + linkedList.add(7, "8"); + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next()); + } + } + + @Test + public void testReverse() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test reverse()"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(01); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + linkedList.add(7); + linkedList.add(8); + linkedList.reverse(); + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + } + + @Test + public void testRemoveFirstHalf() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test removeFirstHalf()"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + linkedList.add(7); + linkedList.add(8); + linkedList.add(9); + System.out.println(linkedList.size()); + Iterator iterator = linkedList.iterator(); + System.out.print("before removeFirstHalf():"); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + System.out.println(); + linkedList.removeFirstHalf(); + iterator = linkedList.iterator(); + System.out.print("after removeFirstHalf():"); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + } + + @Test + public void testRemoveIntInt() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test remove(int index,int length)"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(0, 1); + linkedList.add(1, 2); + linkedList.add(2, 3); + linkedList.add(3, 4); + linkedList.add(4, 5); + linkedList.add(5, 6); + linkedList.add(6, 7); + linkedList.add(7, 8); + linkedList.add(8, 9); + Iterator iterator = linkedList.iterator(); + int index = 1; + int len = 16; + System.out.print("before remove:"); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + System.out.println("size:"+linkedList.size()); + System.out.println(); + linkedList.remove(index, len); + iterator = linkedList.iterator(); + System.out.print("after remove(" + index + "," + len + ")"); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + System.out.println("size:"+linkedList.size()); + + } + + @Test + public void testGetElements() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test getElements(LinkedList list)"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(0, 1); + linkedList.add(1, 2); + linkedList.add(2, 3); + linkedList.add(3, 4); + linkedList.add(4, 5); + linkedList.add(5, 6); + linkedList.add(6, 7); + linkedList.add(7, 8); + linkedList.add(8, 9); + LinkedList list = new LinkedList<>(); + list.add(1); + list.add(3); + list.add(5); + list.add(7); + list.add(9); + System.out.print("before testing:"); + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + System.out.println(); + iterator = list.iterator(); + String listvalue = ""; + while (iterator.hasNext()) { + listvalue += iterator.next() + ","; + } + System.out.print("get elements in indexs: " + "[" + listvalue + "]" + + Arrays.toString(linkedList.getElements(list))); + + } + + @Test + public void testSubtract() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test subtract(LinkedList list)"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(0, 1); + linkedList.add(1, 2); + linkedList.add(2, 3); + linkedList.add(3, 4); + linkedList.add(4, 5); + linkedList.add(5, 6); + linkedList.add(6, 7); + linkedList.add(7, 8); + linkedList.add(8, 9); + LinkedList list = new LinkedList<>(); + list.add(1); + list.add(3); + list.add(5); + list.add(7); + list.add(9); + list.add(12); + list.add(13); + System.out.print("before testing:"); + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + System.out.println(); + iterator = list.iterator(); + String listvalue = ""; + while (iterator.hasNext()) { + listvalue += iterator.next() + "->"; + } + System.out.println("after subtract Linkedlist:" + listvalue); + linkedList.subtract(list); + iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + } + + @Test + public void testRemoveDuplicateValues() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test removeDuplicateValues()"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + linkedList.add(6); + linkedList.add(7); + linkedList.add(8); + linkedList.add(8); + linkedList.add(9); + System.out.print("before testing: "); + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + System.out.println(); + System.out.println("after test: "); + iterator = linkedList.iterator(); + linkedList.removeDuplicateValues(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + } + + @Test + public void testRemoveRange() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test removeRange(int min,int max)"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + linkedList.add(7); + linkedList.add(8); + linkedList.add(9); + linkedList.add(10); + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + System.out.print("before testing: "); + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + int min = 1; + int max = 4; + linkedList.remove(min, max); + System.out.println(); + System.out.println("after removing index form " + min + " to " + max); + iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + + } + + @Test + public void testIntersection() { + System.out + .println("-----------------------杩欐槸鍗庝附鐨勫垎鍓茬嚎------------------------"); + System.out.println("test intersection(LinkedList list)"); + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.add(7); + linkedList.add(9); + linkedList.add(10); + LinkedList list = new LinkedList<>(); + list.add(1); + list.add(2); + list.add(4); + list.add(5); + list.add(6); + list.add(7); + list.add(10); + System.out.println("before testing:"); + System.out.print("linkedList:"); + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + System.out.println(); + System.out.print("list:"); + iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + System.out.println(); + linkedList=linkedList.intersection(list); + System.out.println("after linkedList.intersection(list):"); + iterator = linkedList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + "->"); + } + + } + +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/List.java b/group21/315752375/src/com/coding/basic/List.java similarity index 100% rename from group21/315752375/homework20170219/src/com/coding/basic/List.java rename to group21/315752375/src/com/coding/basic/List.java diff --git a/group21/315752375/homework20170219/src/com/coding/basic/Queue.java b/group21/315752375/src/com/coding/basic/Queue.java similarity index 79% rename from group21/315752375/homework20170219/src/com/coding/basic/Queue.java rename to group21/315752375/src/com/coding/basic/Queue.java index 5eaf69ce40..465abcd25c 100644 --- a/group21/315752375/homework20170219/src/com/coding/basic/Queue.java +++ b/group21/315752375/src/com/coding/basic/Queue.java @@ -1,8 +1,8 @@ package com.coding.basic; -public class Queue { +public class Queue> { LinkedList linkedList=new LinkedList(); - public void enQueue(Object o){ + public void enQueue(T o){ linkedList.addLast(o); } diff --git a/group21/315752375/homework20170219/src/com/coding/basic/Stack.java b/group21/315752375/src/com/coding/basic/Stack.java similarity index 100% rename from group21/315752375/homework20170219/src/com/coding/basic/Stack.java rename to group21/315752375/src/com/coding/basic/Stack.java diff --git a/group21/315752375/src/com/coding/basic/linklist/LRUPageFrame.java b/group21/315752375/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..24b9d8b155 --- /dev/null +++ b/group21/315752375/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,164 @@ +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(); + } + + + +} diff --git a/group21/315752375/src/com/coding/basic/linklist/LRUPageFrameTest.java b/group21/315752375/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/group21/315752375/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +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()); + + } + +} diff --git a/group21/315752375/homework20170226/src/dom4j-1.6.1.jar b/group21/315752375/src/dom4j-1.6.1.jar similarity index 100% rename from group21/315752375/homework20170226/src/dom4j-1.6.1.jar rename to group21/315752375/src/dom4j-1.6.1.jar diff --git "a/group21/315752375/\344\273\213\347\273\215.txt" "b/group21/315752375/\344\273\213\347\273\215.txt" deleted file mode 100644 index 68a4528a66..0000000000 --- "a/group21/315752375/\344\273\213\347\273\215.txt" +++ /dev/null @@ -1 +0,0 @@ -this is a test file \ No newline at end of file