diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java b/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java index d5cca25bf4..613163b38a 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java @@ -4,367 +4,236 @@ import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array; -public class ArrayUtil -{ - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin) - { - if(origin == null || origin.length == 0) - { - return ; - } - - for(int i=0, j = origin.length-1; i array2[j]) - { - newArray[count++] = array2[j++]; - } - else if(array1[i] == array2[j]) - { - newArray[count++] = array2[j++]; - i++; - } - } - while(i==array1.length && j= max) - { - break; - } - else - { - count++; - } - } - - return Arrays.copyOf(a, count); + // 将非零元素copy到新数组 + return Arrays.copyOf(b, count); + + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + if (array1 == null) { + return array2; } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max) - { - /* - * 思路:先生成素数数组,数组的最大值小于max - */ - //max小于3时,返回空数组 - if(max < 3) - { - return new int[0]; - } - int[] array = new int[max]; - int count = 0; + if (array2 == null) { + return array1; + } + int[] newArray = new int[array1.length + array2.length]; + // 应该让a1,a2两个数组先进行比较 比较后插入元素 + int i = 0; // array1下标 + int j = 0; // array2下标 + int count = 0; // array3下标 + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) { + newArray[count++] = array1[i++]; + } else if (array1[i] > array2[j]) { + newArray[count++] = array2[j++]; + } else if (array1[i] == array2[j]) { + newArray[count++] = array1[i++] = array2[j++]; + } + } + while (j == array2.length && i < array1.length) { + newArray[count++] = array1[i++]; + } + while (i == array1.length && j < array2.length) { + newArray[count++] = array2[j++]; + } + return Arrays.copyOf(newArray, count); + } - for(int n = 2; n < max; n++) - { - if(isPrime(n)) - { - array[count++] = n; - } - } - - return Arrays.copyOf(array, count); + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + * @throws Exception + */ + public int[] grow(int[] oldArray, int size) { + if (oldArray == null) { + return null; } + if (size < 0) { + throw new IndexOutOfBoundsException("size小于0"); + } + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } - private boolean isPrime(int n) - { - //判断当前n是不是素数 - int i = 2; - while(i < n) - { - if(n % i == 0) - break; - if(n % i != 0) - i++; - } - return i == n; + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max == 1) { + return new int[0]; } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max) - { - if(max < 0) - { - return null; - } - int[] array = new int[max]; - int count = 0; - - for(int n = 2; n < max; n++) - { - int sum = 0; - for(int i=1; i= max) { + break; + } else { + count++; + } } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return + + return Arrays.copyOf(a, count); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + /* + * 思路:先生成素数数组,数组的最大值小于max */ - public String join(int[] array, String seperator) - { - if(array == null ) - { - return null; - } - if(array.length == 0) - { - return ""; - } - - StringBuilder buffer = new StringBuilder(); - for(int i=0; i5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - @Test - public void testRemoveFirstHalf() { - aLinkedList.removeFirstHalf(); - assertEquals(0, aLinkedList.size()); - - aLinkedList.add(2); - aLinkedList.add(5); - aLinkedList.add(7); - aLinkedList.add(8); // [2,5,7,8] - - aLinkedList.removeFirstHalf(); // [7,8] - assertEquals(2, aLinkedList.size()); - assertEquals(7, aLinkedList.get(0)); - assertEquals(8, aLinkedList.get(1)); + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testReverse() { + // 测试当aLinkedList为空时的情况 + aLinkedList.reverse(); + assertEquals(0, aLinkedList.size()); + + // 测试当aLinkedList长度为1时的情况 + aLinkedList.add(4); + aLinkedList.reverse(); + assertEquals(1, aLinkedList.size()); + assertEquals(4, aLinkedList.get(0)); + + for (int i = 1; i < 4; i++) { + aLinkedList.add(i); // [4,1,2,3] } - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - @Test - public void testRemoveIntInt() { - - for (int i=0; i<4; i++) { - aLinkedList.add(i); // [0,1,2,3] - } - - aLinkedList.remove(0, 2); // [2,3] - assertEquals(2, aLinkedList.get(0)); - assertEquals(3, aLinkedList.get(1)); - assertEquals(2, aLinkedList.size()); - - aLinkedList.remove(1, 0); - aLinkedList.remove(0, 0); - assertEquals(2, aLinkedList.size()); - - aLinkedList.remove(1, 1); // [2] - assertEquals(1, aLinkedList.size()); - assertEquals(2, aLinkedList.get(0)); - - aLinkedList.remove(0, 1); // [] - assertEquals(0, aLinkedList.size()); - - expectedEx.expect(Exception.class); - aLinkedList.remove(1, 3); + aLinkedList.reverse(); + assertEquals(4, aLinkedList.size()); + assertEquals(3, aLinkedList.get(0)); + assertEquals(2, aLinkedList.get(1)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + * + */ + @Test + public void testRemoveFirstHalf() { + aLinkedList.removeFirstHalf(); + assertEquals(0, aLinkedList.size()); + + aLinkedList.add(2); + aLinkedList.add(5); + aLinkedList.add(7); + aLinkedList.add(8); // [2,5,7,8] + + aLinkedList.removeFirstHalf(); // [7,8] + assertEquals(2, aLinkedList.size()); + assertEquals(7, aLinkedList.get(0)); + assertEquals(8, aLinkedList.get(1)); + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + @Test + public void testRemoveIntInt() { + + for (int i = 0; i < 4; i++) { + aLinkedList.add(i); // [0,1,2,3] } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - @Test - public void testGetElements() { - for (int i=0; i<4; i++) { - aLinkedList.add(i * i); // [0,1,4,9] - } - - MyLinkedList bLinkedList = new MyLinkedList(); - int[] z1 = aLinkedList.getElements(bLinkedList); // [] - assertArrayEquals( z1, new int[0]); - - bLinkedList.add(1); - bLinkedList.add(3); // [1, 3] - - z1 = aLinkedList.getElements(bLinkedList); // [1, 9] - assertArrayEquals(new int[] {1,9}, z1); - - bLinkedList.add(1, 2); // bLinkedList = [1, 2, 3] - z1 = aLinkedList.getElements(bLinkedList); // [1, 4, 9] - assertArrayEquals(new int[] {1,4,9}, z1); - - bLinkedList.add(0, 0); // bLinkedList = [0, 1, 2, 3] - z1 = aLinkedList.getElements(bLinkedList); // [0, 1, 4, 9] - assertArrayEquals(new int[] {0,1,4,9}, z1); - - // aLinkedList不应该变化 - assertEquals(4, aLinkedList.size()); - for (int i=0; i<4; i++) { - assertEquals(i*i, aLinkedList.get(i)); // [0,1,4,9] - } - - // Exception - bLinkedList.add(5); // bLinkedList = [0, 1, 2, 3, 5] - expectedEx.expect(Exception.class); - z1 = aLinkedList.getElements(bLinkedList); + aLinkedList.remove(0, 2); // [2,3] + assertEquals(2, aLinkedList.get(0)); + assertEquals(3, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + + aLinkedList.remove(1, 0); + aLinkedList.remove(0, 0); + assertEquals(2, aLinkedList.size()); + + aLinkedList.remove(1, 1); // [2] + assertEquals(1, aLinkedList.size()); + assertEquals(2, aLinkedList.get(0)); + + aLinkedList.remove(0, 1); // [] + assertEquals(0, aLinkedList.size()); + + expectedEx.expect(Exception.class); + aLinkedList.remove(1, 3); + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + @Test + public void testGetElements() { + for (int i = 0; i < 4; i++) { + aLinkedList.add(i * i); // [0,1,4,9] } - - @Test - public void TestSubtract() - { - //传进的list为null,什么都不干 - MyLinkedList list = null; - for (int i=0; i<6; i++) - { - aLinkedList.add(i); //[0,1,2,3,4,5] - } - aLinkedList.subtract(list); - assertEquals(6, aLinkedList.size()); - - //传进的list为空 - list = new MyLinkedList(); - aLinkedList.subtract(list); - assertEquals(6, aLinkedList.size()); - - aLinkedList.add(1, 1); //[0,1,1,2,3,4,5] - aLinkedList.add(4, 3); //[0,1, 1, 2, 3, 3, 4, 5] - - // list添加元素[0, 1, 3, 7] - list.add(0); - list.add(1); - list.add(3); - list.add(7); - - aLinkedList.subtract(list); //[ 2, 4, 5] - - assertEquals(2, aLinkedList.get(0)); - assertEquals(4, aLinkedList.get(1)); - assertEquals(5, aLinkedList.get(2)); - assertEquals(3, aLinkedList.size()); + + MyLinkedList bLinkedList = new MyLinkedList(); + int[] z1 = aLinkedList.getElements(bLinkedList); // [] + assertArrayEquals(z1, new int[0]); + + bLinkedList.add(1); + bLinkedList.add(3); // [1, 3] + + z1 = aLinkedList.getElements(bLinkedList); // [1, 9] + assertArrayEquals(new int[] { 1, 9 }, z1); + + bLinkedList.add(1, 2); // bLinkedList = [1, 2, 3] + z1 = aLinkedList.getElements(bLinkedList); // [1, 4, 9] + assertArrayEquals(new int[] { 1, 4, 9 }, z1); + + bLinkedList.add(0, 0); // bLinkedList = [0, 1, 2, 3] + z1 = aLinkedList.getElements(bLinkedList); // [0, 1, 4, 9] + assertArrayEquals(new int[] { 0, 1, 4, 9 }, z1); + + // aLinkedList不应该变化 + assertEquals(4, aLinkedList.size()); + for (int i = 0; i < 4; i++) { + assertEquals(i * i, aLinkedList.get(i)); // [0,1,4,9] } - @Test - public void testRemoveDuplicateValues() - { - aLinkedList.removeDuplicateValues(); - assertEquals(0, aLinkedList.size()); - - aLinkedList.add(3); - aLinkedList.add(3); - aLinkedList.add(4); - aLinkedList.add(4); - aLinkedList.add(5); - aLinkedList.add(6); - aLinkedList.add(6); //[3, 3, 4, 4, 5, 6, 6] - - aLinkedList.removeDuplicateValues(); //[3, 4, 5, 6] - - assertEquals(3, aLinkedList.get(0)); - assertEquals(4, aLinkedList.get(1)); - assertEquals(5, aLinkedList.get(2)); - assertEquals(6, aLinkedList.get(3)); - assertEquals(4, aLinkedList.size()); - + + // Exception + bLinkedList.add(5); // bLinkedList = [0, 1, 2, 3, 5] + expectedEx.expect(Exception.class); + z1 = aLinkedList.getElements(bLinkedList); + } + + @Test + public void TestSubtract() { + // 传进的list为null,什么都不干 + MyLinkedList list = null; + for (int i = 0; i < 6; i++) { + aLinkedList.add(i); // [0,1,2,3,4,5] } - @Test - public void testRemoveRange() - { - for (int i=0; i<6; i++) - { - aLinkedList.add(i); // [0, 1, 2, 3, 4, 5] //考虑重复元素 - } - aLinkedList.addFirst(0); // [0, 0, 1, 2, 3, 4, 5] - aLinkedList.add(3, 2); // [0, 0, 1, 2, 2, 3, 4, 5] - - aLinkedList.removeRange(1, 4); // 大于1小于4 [0, 0, 1, 4, 5] - - assertEquals(0, aLinkedList.get(0)); - assertEquals(0, aLinkedList.get(1)); - assertEquals(1, aLinkedList.get(2)); - assertEquals(4, aLinkedList.get(3)); - assertEquals(5, aLinkedList.get(4)); - assertEquals(5, aLinkedList.size()); - - //若出现 min >= max的情况,什么都不做 - aLinkedList.removeRange(4, 1); - assertEquals(0, aLinkedList.get(0)); - assertEquals(0, aLinkedList.get(1)); - assertEquals(1, aLinkedList.get(2)); - assertEquals(4, aLinkedList.get(3)); - assertEquals(5, aLinkedList.get(4)); - assertEquals(5, aLinkedList.size()); - - //将整个链表中的元素删除 - aLinkedList.removeRange(-1, 8); - assertEquals(0, aLinkedList.size()); + aLinkedList.subtract(list); + assertEquals(6, aLinkedList.size()); + + // 传进的list为空 + list = new MyLinkedList(); + aLinkedList.subtract(list); + assertEquals(6, aLinkedList.size()); + + aLinkedList.add(1, 1); // [0,1,1,2,3,4,5] + aLinkedList.add(4, 3); // [0,1, 1, 2, 3, 3, 4, 5] + + // list添加元素[0, 1, 3, 7] + list.add(0); + list.add(1); + list.add(3); + list.add(7); + + aLinkedList.subtract(list); // [ 2, 4, 5] + + assertEquals(2, aLinkedList.get(0)); + assertEquals(4, aLinkedList.get(1)); + assertEquals(5, aLinkedList.get(2)); + assertEquals(3, aLinkedList.size()); + } + + @Test + public void testRemoveDuplicateValues() { + aLinkedList.removeDuplicateValues(); + assertEquals(0, aLinkedList.size()); + + aLinkedList.add(3); + aLinkedList.add(3); + aLinkedList.add(4); + aLinkedList.add(4); + aLinkedList.add(5); + aLinkedList.add(6); + aLinkedList.add(6); // [3, 3, 4, 4, 5, 6, 6] + + aLinkedList.removeDuplicateValues(); // [3, 4, 5, 6] + + assertEquals(3, aLinkedList.get(0)); + assertEquals(4, aLinkedList.get(1)); + assertEquals(5, aLinkedList.get(2)); + assertEquals(6, aLinkedList.get(3)); + assertEquals(4, aLinkedList.size()); + + } + + @Test + public void testRemoveRange() { + for (int i = 0; i < 6; i++) { + aLinkedList.add(i); // [0, 1, 2, 3, 4, 5] //考虑重复元素 } - @Test - public void testIntersection() - { - for (int i=0; i<6; i++) - { - aLinkedList.add(i); - } - aLinkedList.add(6); - aLinkedList.add(7); // [0, 1, 2, 3, 4, 5, 6, 7] - //list为null - MyLinkedList list = null; - MyLinkedList newList1 = aLinkedList.intersection(list); - assertNull(newList1); - - //list为空链表 - list = new MyLinkedList(); - MyLinkedList newList2 = aLinkedList.intersection(list); - assertEquals(0, newList2.size()); - - list.add(0); - list.add(3); - list.add(4); - list.add(6); - list.add(7); // [0, 3, 4, 6, 7] - MyLinkedList newList3 = aLinkedList.intersection(list); - - assertEquals(0, newList3.get(0)); - assertEquals(3, newList3.get(1)); - assertEquals(4, newList3.get(2)); - assertEquals(6, newList3.get(3)); - assertEquals(7, newList3.get(4)); - assertEquals(5, newList3.size()); + aLinkedList.addFirst(0); // [0, 0, 1, 2, 3, 4, 5] + aLinkedList.add(3, 2); // [0, 0, 1, 2, 2, 3, 4, 5] + + aLinkedList.removeRange(1, 4); // 大于1小于4 [0, 0, 1, 4, 5] + + assertEquals(0, aLinkedList.get(0)); + assertEquals(0, aLinkedList.get(1)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + assertEquals(5, aLinkedList.get(4)); + assertEquals(5, aLinkedList.size()); + + // 若出现 min >= max的情况,什么都不做 + aLinkedList.removeRange(4, 1); + assertEquals(0, aLinkedList.get(0)); + assertEquals(0, aLinkedList.get(1)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(4, aLinkedList.get(3)); + assertEquals(5, aLinkedList.get(4)); + assertEquals(5, aLinkedList.size()); + + // 将整个链表中的元素删除 + aLinkedList.removeRange(-1, 8); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testIntersection() { + for (int i = 0; i < 6; i++) { + aLinkedList.add(i); } - + aLinkedList.add(6); + aLinkedList.add(7); // [0, 1, 2, 3, 4, 5, 6, 7] + // list为null + MyLinkedList list = null; + MyLinkedList newList1 = aLinkedList.intersection(list); + assertNull(newList1); + + // list为空链表 + list = new MyLinkedList(); + MyLinkedList newList2 = aLinkedList.intersection(list); + assertEquals(0, newList2.size()); + + list.add(0); + list.add(3); + list.add(4); + list.add(6); + list.add(7); // [0, 3, 4, 6, 7] + MyLinkedList newList3 = aLinkedList.intersection(list); + + assertEquals(0, newList3.get(0)); + assertEquals(3, newList3.get(1)); + assertEquals(4, newList3.get(2)); + assertEquals(6, newList3.get(3)); + assertEquals(7, newList3.get(4)); + assertEquals(5, newList3.size()); + } + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java index f2299e8e83..e73700ec53 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/List.java @@ -1,14 +1,16 @@ package com.github.HarryHook.coding2017.basic; +public interface List { -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(); - - public Iterator iterator(); -} + 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(); + + public Iterator iterator(); +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java index 92f84b687c..113b32f4d2 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/ListTest.java @@ -10,113 +10,109 @@ public class ListTest { - protected static List aList; - - @Test - public void testFunctional() { - aList.add(1); - aList.add(2); - assertEquals(1, aList.get(0)); - assertEquals(2, aList.get(1)); - - aList.add(3); - aList.add(0, 5); - aList.add(2, 11); - assertEquals(5, aList.get(0)); - assertEquals(11, aList.get(2)); - - aList.add("hi"); - assertEquals("hi", aList.get(5)); - assertEquals(6, aList.size()); - - aList.remove(1); - assertEquals(11, aList.get(1)); - assertEquals(2, aList.get(2)); - - assertEquals(5, aList.size()); - } - - @Test - public void testAdd() { - for (int i=0; i<100; i++) - aList.add(i); - assertEquals(0, aList.get(0)); - assertEquals(99, aList.get(99)); - assertEquals(44, aList.get(44)); - } - - @Test - public void testRemove() { - aList.add(1); - aList.add(2); - aList.add(3); - int u = (Integer)aList.remove(2); - assertEquals(3, u); - assertEquals(2, aList.size()); - - aList.add(1, 5); - u = (Integer)aList.remove(0); - assertEquals(1, u); - assertEquals(5, aList.get(0)); - assertEquals(2, aList.get(1)); - assertEquals(2, aList.size()); - - aList.remove(0); - aList.remove(0); - assertEquals(0, aList.size()); - - - } - - @Test - public void testSize() { - for (int i=0; i<10; i++) - aList.add(i*2); - assertEquals(10, aList.size()); - } - - @Rule - public ExpectedException expectedEx = ExpectedException.none(); - - @Test - public void testException() { - expectedEx.expect(Exception.class); - aList.remove(1); - aList.add(3); - aList.add(2, 5); - expectedEx.expect(Exception.class); - } - - @Test - - public void testIterator() - { - Iterator it = aList.iterator(); - assertEquals(false, it.hasNext()); - - aList.add(1); - aList.add(2); - aList.add(3); - - it = aList.iterator(); - assertEquals(true, it.hasNext()); - assertEquals(1, it.next()); - assertEquals(2, it.next()); - assertEquals(3, it.next()); - assertEquals(false, it.hasNext()); - - aList.remove(1); - it = aList.iterator(); - assertEquals(true, it.hasNext()); - assertEquals(1, it.next()); - assertEquals(3, it.next()); - assertEquals(false, it.hasNext()); - - expectedEx.expect(Exception.class); - it.next(); - - } - - + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i = 0; i < 100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer) aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer) aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + } + + @Test + public void testSize() { + for (int i = 0; i < 10; i++) + aList.add(i * 2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + aList.remove(1); + aList.add(3); + aList.add(2, 5); + expectedEx.expect(Exception.class); + } + + @Test + + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java index dbfd8aae19..e8cc041978 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyArrayList.java @@ -7,161 +7,136 @@ import java.util.Arrays; import java.util.NoSuchElementException; -public class MyArrayList implements List -{ - private int size = 0; //数组元素个数 - - private Object[] elementData = new Object[10]; //初始化数组大小为10 - - //将元素添加到数组尾部 - public void add(Object o) - { //需要判断数组空间是否够用 - ensureCapacity(size + 1); - elementData[size++] = o; - } - //在指定位置添加元素 - public void add(int index, Object o) - { - //判断下标记是否越界 - if (index > size || index < 0) - throw new IndexOutOfBoundsException( - "Index: " + index + ", Size: " + size); - ensureCapacity(size + 1); - //判断当前位置是否有元素,没有元素添加到当前位置;若有,当前元素及之后元素向右移 - if(elementData[index] == null) - { - elementData[index] = o; - } - else - { - for(int i=elementData.length-1; i>index; i--) - { - elementData[i] = elementData[i-1]; - } - elementData[index] = o; - } - size++; - - /* - //判断索引位置是否正确 - if (index > size || index < 0) - throw new IndexOutOfBoundsException( - "Index: " + index + ", Size: " + size); - //扩容检测 - ensureCapacity(size+1); - /* - * 对源数组进行复制处理(位移),从index + 1到size-index。 - * 主要目的就是空出index位置供数据插入, - * 即向右移动当前位于该位置的元素以及所有后续元素。 - - System.arraycopy(elementData, index, elementData, index + 1, - size - index); - //在指定位置赋值 - elementData[index] = 0; - size++; - - */ - } - - public Object get(int index) - { - //若index超出size应该抛出异常 - if(index >= size) - throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); - return elementData[index]; - - } - - public Object remove(int index) - { //涉及到元素移位 - Object oldValue = elementData[index]; - for(int i=index; i oldCapacity) - { - //Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 - int newCapacity = (oldCapacity * 3)/2 + 1; //增加50%+1 - if (newCapacity < minCapacity) - newCapacity = minCapacity; - // minCapacity is usually close to size, so this is a win: - elementData = Arrays.copyOf(elementData, newCapacity); - } - } - - //返回数组的大小 - public int size() - { - return size; - } - - public Iterator iterator() - { - return new MyArrayListIterator(); +public class MyArrayList implements List { + private int size = 0; // 数组元素个数 + + private Object[] elementData = new Object[10]; // 初始化数组大小为10 + + // 将元素添加到数组尾部 + public void add(Object o) { // 需要判断数组空间是否够用 + ensureCapacity(size + 1); + elementData[size++] = o; + } + + // 在指定位置添加元素 + public void add(int index, Object o) { + // 判断下标记是否越界 + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + ensureCapacity(size + 1); + // 判断当前位置是否有元素,没有元素添加到当前位置;若有,当前元素及之后元素向右移 + if (elementData[index] == null) { + elementData[index] = o; + } else { + for (int i = elementData.length - 1; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; } - - private class MyArrayListIterator implements Iterator - { - private int cursor = 0; //记录索引位置 - public boolean hasNext() - { - return cursor != size; - } - public Object next() - { - try { - Object next = get(cursor); - cursor++; - return next; - - } catch (IndexOutOfBoundsException e) - { - throw new NoSuchElementException(); - } - - } + size++; + + /* + * //判断索引位置是否正确 if (index > size || index < 0) throw new + * IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); + * //扩容检测 ensureCapacity(size+1); /* 对源数组进行复制处理(位移),从index + + * 1到size-index。 主要目的就是空出index位置供数据插入, 即向右移动当前位于该位置的元素以及所有后续元素。 + * + * System.arraycopy(elementData, index, elementData, index + 1, size - + * index); //在指定位置赋值 elementData[index] = 0; size++; + * + */ + } + + public Object get(int index) { + // 若index超出size应该抛出异常 + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + return elementData[index]; + + } + + public Object remove(int index) { // 涉及到元素移位 + Object oldValue = elementData[index]; + for (int i = index; i < elementData.length - 1; i++) + elementData[i] = elementData[i + 1]; + elementData[--size] = null; + + return oldValue; + } + + // 判断是否需要给数组扩容 + public void ensureCapacity(int minCapacity) { + + int oldCapacity = elementData.length; + if (minCapacity > oldCapacity) { + // Object oldData[] = elementData; + // //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 + int newCapacity = (oldCapacity * 3) / 2 + 1; // 增加50%+1 + if (newCapacity < minCapacity) + newCapacity = minCapacity; + // minCapacity is usually close to size, so this is a win: + elementData = Arrays.copyOf(elementData, newCapacity); } - - public static void main(String[] args) - { - MyArrayList myArrays = new MyArrayList(); - myArrays.add(3); - myArrays.add(0, 11); - myArrays.add(1, 2); - myArrays.add(3, 5); - myArrays.add(2, 1); - myArrays.add(7); - Print(myArrays); - - for(int i = 0; i < 19; i++) - myArrays.add(i, 55); - - System.out.println("获取指定位置元素: " + myArrays.get(2)); - System.out.println("删除指定位置元素: " + myArrays.remove(1)); - System.out.println("当前元素个数:" + myArrays.size()); - - Print(myArrays); - + } + + // 返回数组的大小 + public int size() { + return size; + } + + public Iterator iterator() { + return new MyArrayListIterator(); + } + + private class MyArrayListIterator implements Iterator { + private int cursor = 0; // 记录索引位置 + + public boolean hasNext() { + return cursor != size; } - public static void Print(MyArrayList myArrays) - { - Iterator it = myArrays.iterator(); - System.out.println("对链表中的元素进行打印:"); - while(it.hasNext()) - System.out.print(it.next() + " "); - System.out.println(""); - System.out.println("当前元素个数: " + myArrays.size()); + + public Object next() { + try { + Object next = get(cursor); + cursor++; + return next; + + } catch (IndexOutOfBoundsException e) { + throw new NoSuchElementException(); + } } - -} + } + + public static void main(String[] args) { + MyArrayList myArrays = new MyArrayList(); + myArrays.add(3); + myArrays.add(0, 11); + myArrays.add(1, 2); + myArrays.add(3, 5); + myArrays.add(2, 1); + myArrays.add(7); + Print(myArrays); + for (int i = 0; i < 19; i++) + myArrays.add(i, 55); + + System.out.println("获取指定位置元素: " + myArrays.get(2)); + System.out.println("删除指定位置元素: " + myArrays.remove(1)); + System.out.println("当前元素个数:" + myArrays.size()); + + Print(myArrays); + + } + + public static void Print(MyArrayList myArrays) { + Iterator it = myArrays.iterator(); + System.out.println("对链表中的元素进行打印:"); + while (it.hasNext()) + System.out.print(it.next() + " "); + System.out.println(""); + System.out.println("当前元素个数: " + myArrays.size()); + + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java index 9a668faaf0..e1c3478907 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java @@ -5,549 +5,409 @@ package com.github.HarryHook.coding2017.basic; -public class MyLinkedList implements List -{ - private Node head = null; //头指针 - private int size = 0; - private static class Node - { - Object data; - Node next; +public class MyLinkedList implements List { + private Node head = null; // 头指针 + private int size = 0; + + private static class Node { + Object data; + Node next; + } + + public void add(Object o) { + addLast(o); + } + + // 在指定位置添加元素 + public void add(int index, Object o) { + + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } - public void add(Object o) - { - addLast(o); + + // 存在插入头结点的情况 + if (index == 0) { + addFirst(o); + } else { // 即 index != 0 的情况 + // p保存待插入节点的前一节点,x指向要插入的节点 + Node x = head; + Node p = null; + int i = 0; + while (i < index) { + p = x; + x = x.next; + i++; + } + Node n = new Node(); + p.next = n; + n.next = x; + n.data = o; + size++; } - //在指定位置添加元素 - public void add(int index , Object o) - { - - if (index > size || index < 0) - { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - } - - //存在插入头结点的情况 - if(index == 0) - { - addFirst(o); - } - else - { //即 index != 0 的情况 - // p保存待插入节点的前一节点,x指向要插入的节点 - Node x = head; - Node p = null; - int i = 0; - while(i < index) - { - p = x; - x = x.next; - i++; - } - Node n = new Node(); - p.next = n; - n.next = x; - n.data = o; - size++; - } - + + } + + // 返回指定位置元素 + public Object get(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } - //返回指定位置元素 - public Object get(int index) - { - if(index >= size) - { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - } - Node x = head; - int i = 0; - while(i < index && x != null) - { - x = x.next; - i++; - } - return x.data; + Node x = head; + int i = 0; + while (i < index && x != null) { + x = x.next; + i++; } - - //移除指定位置节点 - public Object remove(int index) - { - if (index > size || index < 0) - { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - } - //先判断是否是头节点 - if( index == 0) - { - return removeFirst(); - } - else - { - Node x = head; - Node pre = null; - int i = 0; - while(i < index) - { - pre = x; - x = x.next; - i++; - } - Object Data = pre.next.data; - pre.next = x.next; - x = null; - size--; - return Data; - } + return x.data; + } + // 移除指定位置节点 + public Object remove(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } - //头部添加节点 - public void addFirst(Object o) - { - Node n = new Node(); - n.next = head; - head = n; - n.data = o; - size++; + // 先判断是否是头节点 + if (index == 0) { + return removeFirst(); + } else { + Node x = head; + Node pre = null; + int i = 0; + while (i < index) { + pre = x; + x = x.next; + i++; + } + Object Data = pre.next.data; + pre.next = x.next; + x = null; + size--; + return Data; } - //尾部添加节点 - public void addLast(Object o) - { - if (head == null) - { - head = new Node(); - head.data = o; - } - else - { - Node x = head; - while(x.next != null) - { - x = x.next; - } - Node n = new Node(); - x.next = n; - n.next = null; - n.data = o; - } - size++; + + } + + // 头部添加节点 + public void addFirst(Object o) { + Node n = new Node(); + n.next = head; + head = n; + n.data = o; + size++; + } + + // 尾部添加节点 + public void addLast(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + } else { + Node x = head; + while (x.next != null) { + x = x.next; + } + Node n = new Node(); + x.next = n; + n.next = null; + n.data = o; } - //移除第一个节点 - public Object removeFirst() - { - Node n = head; - Object Data = n.data; - head = head.next; - n = null; - size--; - return Data; + size++; + } + + // 移除第一个节点 + public Object removeFirst() { + Node n = head; + Object Data = n.data; + head = head.next; + n = null; + size--; + return Data; + } + + // 移除最后一个节点 + public Object removeLast() { + Node x = head; + Node p = null; + if (x.next == null) { + return removeFirst(); + } else { + while (x.next != null) { + p = x; + x = x.next; + } + Object Data = x.data; + p.next = null; + x = null; // 删除最后一个节点 + size--; + return Data; } - - //移除最后一个节点 - public Object removeLast() - { - Node x = head; - Node p = null; - if(x.next == null) - { - return removeFirst(); - } - else - { - while(x.next != null) - { - p = x; - x = x.next; - } - Object Data = x.data; - p.next = null; - x = null; //删除最后一个节点 - size--; - return Data; - } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyLinkedListIterator(); + } + + private class MyLinkedListIterator implements Iterator { + private int cursor = 0; // 记录索引位置 + + public boolean hasNext() { + return cursor != size; } - public int size() - { - return size; + + public Object next() { + Object next = get(cursor); + cursor++; + return next; } - public Iterator iterator() - { - return new MyLinkedListIterator(); + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (head == null) { + return; } - private class MyLinkedListIterator implements Iterator - { - private int cursor = 0; //记录索引位置 - public boolean hasNext() - { - return cursor != size; - } - public Object next() - { - Object next = get(cursor); - cursor ++; - return next; - } + Node p1, p2, p3; + p1 = head; + p2 = p1.next; + while (p2 != null) { + p3 = p2.next; + p2.next = p1; + p1 = p2; + p2 = p3; } - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse() - { - if(head == null) - { - return ; - } - Node p1, p2, p3; - p1 = head; - p2 = p1.next; - while(p2 != null) - { - p3 = p2.next; - p2.next = p1; - p1 = p2; - p2 = p3; - } - head.next = null; - head = p1; + head.next = null; + head = p1; + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + * + */ + public void removeFirstHalf() { + if (size == 0) { + return; } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf() + Node p = head; + Node q = null; + int i = size / 2; + int j = 0; + while (j < i) { + j++; + q = p; + p = p.next; + } + head = p; + q.next = null; + size = size - i; + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + Node p = head; + Node q = null; + int index = 0; + int j = 0; + + while (index < i) { + q = p; + p = p.next; + index++; + } + + while (p != null && j < length) { + p = p.next; + j++; + } + if (i == 0) // 从头开始移除元素 { - if(size == 0) - { - return ; - } - Node p = head; - Node q = null; - int i = size / 2; - int j = 0; - while(j < i) - { - j++; - q = p; - p = p.next; - } + if (p == null) // 元素全被删光 + { + head = null; + size = 0; + } else // 从头删length个元素 + { head = p; + size = size - length; + } + } else // 从中间开始移除元素 + { + if (p == null) { q.next = null; - size = size - i; + size = size - j; + } else { + q.next = p; + size = size - length; + } + } + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(MyLinkedList list) { + if (list == null) { + return new int[0]; } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length) - { - Node p = head; - Node q = null; - int index = 0; - int j = 0; - - while(index < i) - { - q = p; - p = p.next; - index ++; - } + int i = 0; + int[] array = new int[list.size()]; + while (i < list.size()) { + array[i] = (int) this.get((int) list.get(i)); + i++; + } + return array; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(MyLinkedList list) { + int i = 0; + if (list == null) { + return; + } + while (i < list.size()) { + for (int index = 0; index < this.size(); index++) { + Node p = head; // 当前节点 + Node q = null; // 前驱节点 + while (list.get(i) != p.data && p.next != null) { + q = p; + p = p.next; + } + if (p.data == list.get(i)) { // 删除找到的节点 - while(p != null && j < length) - { - p = p.next; - j++; - } - if(i==0) //从头开始移除元素 - { - if(p == null) //元素全被删光 - { - head = null; - size = 0; - } - else //从头删length个元素 - { - head = p; - size = size - length; - } - } - else //从中间开始移除元素 - { - if(p == null) - { - q.next = null; - size = size - j ; - } - else - { - q.next = p; - size = size - length; - } - } - + if (p == head) { + head = head.next; + } else { + q.next = p.next; + } + size--; + } + } + + i++; } - - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public int[] getElements(MyLinkedList list) - { - if(list == null) - { - return new int[0]; - } - int i = 0 ; - int[] array = new int[list.size()]; - while(i < list.size()) - { - array[i] = (int) this.get((int)list.get(i)); - i++; - } - return array; + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + if (head == null) { + return; } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 + Node p = head; + Node q = head; // 前驱 + while (p.next != null) { + p = p.next; - * @param list - */ - - public void subtract(MyLinkedList list) - { - int i = 0; - if(list == null) - { - return ; - } - while(i < list.size()) - { - for(int index = 0; index < this.size(); index++) - { - Node p = head; //当前节点 - Node q = null; //前驱节点 - while(list.get(i) != p.data && p.next != null) - { - q = p; - p = p.next; - } - if(p.data == list.get(i)) //删除找到的节点 - { - if(p == head) - head = head.next; - else - { - q.next = p.next; - } - size--; - } - } - - i++; - } + while (p.data == q.data) { + size--; + if (p.next == null) { + q.next = null; + break; + } + q.next = p.next; + p = p.next; + if (p == null) + break; + } + q = q.next; } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues() - { - if(head == null) - { - return ; - } - Node p = head; - Node q = head; //前驱 - while(p.next != null) - { - p = p.next; - - while(p.data == q.data) - { - size--; - if(p.next == null) - { - q.next = null; - break; - } - q.next = p.next; - p = p.next; - if(p == null) - break; - } - q = q.next; - } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + Node p = head; // 当前节点 + Node q = head; // 前驱节点 + while (p.next != null && ((int) p.data <= min || (int) p.data >= max)) { // 未找到继续遍历 + q = p; + p = p.next; } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max) - { - - Node p = head; //当前节点 - Node q = head; //前驱节点 - while(p.next != null && ((int)p.data <= min || (int)p.data >= max)) //未找到继续遍历 - { - q = p; - p = p.next; - } - while((int)p.data > min && (int)p.data < max) //删除找到的节点 - { - p = p.next; - size--; - - if(size == 0) //删完所有元素 - break ; - } - if(q == head) //头结点被删掉 - { - head = p; - } - else - { - q.next = p; - } - + while ((int) p.data > min && (int) p.data < max) { // 删除找到的节点 + p = p.next; + size--; + + if (size == 0) // 删完所有元素 + break; } + if (q == head) { // 头结点被删掉 - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public MyLinkedList intersection(MyLinkedList list) - { - if(list == null || this == null) - { - return null; - } - Node p1 = list.head; - Node p2 = this.head; - MyLinkedList newList = new MyLinkedList(); - - while(p1 != null && p2 != null) - { - while(((int) p1.data < (int) p2.data) && p1.next != null) - { - p1 = p1.next; - } - while(((int) p1.data > (int) p2.data) && p2.next != null) - { - p2 = p2.next; - } - if(p1.data == p2.data) - { - newList.add(p1.data); - p1 = p1.next; - p2 = p2.next; - } - - if(p1 == null && p2 == null) //若最后两个元素相等 - { - break; - } - } - return newList; + head = p; + } else { + q.next = p; } - - - public static void main(String[] args) - { - MyLinkedList myList = new MyLinkedList(); - myList.add(1); - myList.add(1); - myList.add(3); - myList.add(4); - myList.add(5); - myList.add(5); - myList.add(7); - myList.add(8); - myList.add(8); - myList.add(9); - Print(myList); - /* - MyLinkedList list = new MyLinkedList(); - list.add(0); - list.add(3); - list.add(5); - list.add(6); - list.add(9); - list.add(19); - //Print(list); - //myList.removeDuplicateValues(); - //myList.subtract(list); - */ - - myList.removeRange(-11, 10); - Print(myList); - /* - MyLinkedList list1 = myList.intersection(list); - System.out.println("打印交集:"); - Print(list1); - /* reverse() - myList.reverse(); - Print(myList); - */ - - //System.out.println("从第二个元素开始,移除三个元素"); - //myList.remove(1, 1); - //Print(myList); - - // getElements() - MyLinkedList list = new MyLinkedList(); - - int[] array = myList.getElements(list); - System.out.println("输出:"+array[0]); - for(int i=0; i (int) p2.data) && p2.next != null) { + p2 = p2.next; + } + if (p1.data == p2.data) { + newList.add(p1.data); + p1 = p1.next; + p2 = p2.next; + } + + if (p1 == null && p2 == null) { // 若最后两个元素相等 + break; + } } + return newList; + } + + public static void Print(MyLinkedList myList) { + Iterator it = myList.iterator(); + System.out.println("对链表中的元素进行打印:"); + while (it.hasNext()) + System.out.print(it.next() + " "); + System.out.println(""); + System.out.println("当前元素个数: " + myList.size()); + System.out.println(""); + } - } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java index 9b713eaea9..3d6cd9df0a 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java @@ -3,52 +3,38 @@ * 实现简单的队列 */ package com.github.HarryHook.coding2017.basic; + import java.util.*; -public class MyQueue -{ - private MyArrayList elementData = new MyArrayList(); - private int size = 0; - //入队 - public void enQueue(Object o) - { - elementData.add(o); - size++; - } - //出队 - public Object deQueue() - { - if(isEmpty()) - throw new NoSuchElementException(); - Object Data = elementData.remove(0); - size--; - return Data; - } - //判断队列是否为空 - public boolean isEmpty() - { - return size() == 0; - } - //队列中元素个数 - public int size() - { - return size; - } - public static void main(String[] args) - { - MyQueue mq = new MyQueue(); - mq.enQueue(1); - mq.enQueue(2); - mq.enQueue(3); - mq.enQueue(4); - System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - System.out.println("队列中元素个数: " + mq.size()); - System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - System.out.println("队列中元素个数: " + mq.size()); - System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - System.out.println("队列中元素个数: " + mq.size()); - System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - System.out.println("队列中元素个数: " + mq.size()); - //System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); - //System.out.println("队列中元素个数: " + mq.size()); + +public class MyQueue { + private MyArrayList elementData = new MyArrayList(); + private int size = 0; + + // 入队 + public void enQueue(Object o) { + elementData.add(o); + size++; + } + + // 出队 + public Object deQueue() { + if (isEmpty()) { + throw new NoSuchElementException(); } + Object Data = elementData.remove(0); + size--; + return Data; + } + + // 判断队列是否为空 + public boolean isEmpty() { + return size() == 0; + } + + // 队列中元素个数 + public int size() { + return size; + } + + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java index c7f87c04e6..2702c66a07 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java @@ -6,54 +6,39 @@ import java.util.*; -public class MyStack -{ - private MyArrayList elementData = new MyArrayList(); - private int size = 0; - - //入栈操作 - public void push(Object o) - { - elementData.add(o); - size++; - } - //出栈操作 - public Object pop() - { - Object obj = peek(); - elementData.remove(size() - 1); - size--; - return obj; - } - //获取当前栈顶元素,不用出栈 - public Object peek() - { - if(isEmpty()) - throw new EmptyStackException(); - return elementData.get(size() - 1); - } - //判断栈是否为空 - public boolean isEmpty() - { - return size() == 0; - } - //返回栈内元素个数 - public int size(){ - return size; - } - - public static void main(String[] args) - { - MyStack ms = new MyStack(); - - ms.push(1); - ms.push(2); - ms.push(13); - System.out.println("当前栈顶元素是: " + ms.peek()); - System.out.println("出栈元素是: " + ms.pop()); - System.out.println("出栈元素是: " + ms.pop()); - ms.push(12); - System.out.println("出栈元素是: " + ms.pop()); - System.out.println("当前栈顶元素是: " + ms.peek()); +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + private int size = 0; + + // 入栈操作 + public void push(Object o) { + elementData.add(o); + size++; + } + + // 出栈操作 + public Object pop() { + Object obj = peek(); + elementData.remove(size() - 1); + size--; + return obj; + } + + // 获取当前栈顶元素,不用出栈 + public Object peek() { + if (isEmpty()) { + throw new EmptyStackException(); } + return elementData.get(size() - 1); + } + + // 判断栈是否为空 + public boolean isEmpty() { + return size() == 0; + } + + // 返回栈内元素个数 + public int size() { + return size; + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java index 340f79d240..0b89de7c0a 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java @@ -6,28 +6,28 @@ import com.github.HarryHook.coding2017.basic.MyQueue; public class QueueTest { - private MyQueue queue; - - @Before - public void setUpQueue() { - queue = new MyQueue(); - } - - @Test - public void testQueueFunctional() { - assertEquals(true, queue.isEmpty()); - queue.enQueue(4); - queue.enQueue(2); - assertEquals(2, queue.size()); - assertEquals(false, queue.isEmpty()); - - int i = (Integer)queue.deQueue(); - assertEquals(4, i); - i = (Integer)queue.deQueue(); - assertEquals(2, i); - - assertEquals(0, queue.size()); - assertEquals(true, queue.isEmpty()); - } + private MyQueue queue; + + @Before + public void setUpQueue() { + queue = new MyQueue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer) queue.deQueue(); + assertEquals(4, i); + i = (Integer) queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java index 26160faef6..89f56d8006 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java @@ -9,32 +9,32 @@ public class StackTest { - private MyStack stack; - - @Before - public void setUpStack() { - stack = new MyStack(); - } - - @Test - public void testStackFunctional() { - assertEquals(true, stack.isEmpty()); - stack.push(4); - stack.push(2); - assertEquals(2, stack.size()); - assertEquals(false, stack.isEmpty()); - - int i = (Integer)stack.pop(); - assertEquals(2, i); - - i = (Integer)stack.peek(); - assertEquals(4, i); - - i = (Integer)stack.pop(); - assertEquals(4, i); - - assertEquals(0, stack.size()); - assertEquals(true, stack.isEmpty()); - } + private MyStack stack; + + @Before + public void setUpStack() { + stack = new MyStack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer) stack.pop(); + assertEquals(2, i); + + i = (Integer) stack.peek(); + assertEquals(4, i); + + i = (Integer) stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/download/DownloadThread.java b/group02/727171008/src/com/github/HarryHook/coding2017/download/DownloadThread.java index c7b05e46c0..122c71eae6 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/download/DownloadThread.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/download/DownloadThread.java @@ -6,46 +6,46 @@ public class DownloadThread extends Thread { - Connection conn; - int startPos; - int endPos; - boolean downloadFinsh = false; - public DownloadThread( Connection conn, int startPos, int endPos) { - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - } - - public void run() { - - RandomAccessFile outFile = null; + Connection conn; + int startPos; + int endPos; + boolean downloadFinsh = false; + + public DownloadThread(Connection conn, int startPos, int endPos) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + public void run() { + + RandomAccessFile outFile = null; + try { + + byte[] buffer = conn.read(startPos, endPos); + // 在本地创建一个与服务器大小一致的可随机写入文件 + outFile = new RandomAccessFile("bd_logo.png", "rwd"); + outFile.seek(startPos); + outFile.write(buffer); + FileDownloader fileloader = new FileDownloader(""); + fileloader.getListener(); + + } catch (Exception e) { + + e.printStackTrace(); + + } finally { + try { - - byte[] buffer = conn.read(startPos, endPos); - // 在本地创建一个与服务器大小一致的可随机写入文件 - outFile = new RandomAccessFile("bd_logo.png", "rwd"); - outFile.seek(startPos); - outFile.write(buffer); - FileDownloader fileloader = new FileDownloader(""); - fileloader.getListener(); - - }catch(Exception e) { - + outFile.close(); + + } catch (Exception e) { + e.printStackTrace(); - - }finally { - - try { - outFile.close(); - - } catch(Exception e) { - - e.printStackTrace(); - } - - } + } - + } + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/download/FileDownloader.java b/group02/727171008/src/com/github/HarryHook/coding2017/download/FileDownloader.java index ac5b6606d4..1bb64c1050 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/download/FileDownloader.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/download/FileDownloader.java @@ -5,87 +5,86 @@ import com.github.HarryHook.coding2017.download.api.ConnectionManager; import com.github.HarryHook.coding2017.download.api.DownloadListener; - public class FileDownloader { - - String url; - - DownloadListener listener; - - ConnectionManager cm; - - - public FileDownloader(String _url) { - - this.url = _url; - - } - - public void execute() { - - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - - Connection conn = null; - - try { - - conn = cm.open(this.url); - int fileLength = conn.getContentLength(); - int threadCount = 3; - int blockSize = fileLength / 1024 / threadCount; - - for (int threadId=1; threadId<=threadCount; threadId++) { - - int startPos = (threadId-1) * blockSize; - int endPos = threadId * blockSize - 1; - if (threadId == threadCount) { - endPos = fileLength; - } - - new DownloadThread(conn, startPos, endPos).start(); + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + public FileDownloader(String _url) { + + this.url = _url; + + } + + public void execute() { + + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, + // endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, + // 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + Connection conn = null; + + try { + + conn = cm.open(this.url); + int fileLength = conn.getContentLength(); + int threadCount = 3; + int blockSize = fileLength / 1024 / threadCount; + + for (int threadId = 1; threadId <= threadCount; threadId++) { + + int startPos = (threadId - 1) * blockSize; + int endPos = threadId * blockSize - 1; + if (threadId == threadCount) { + endPos = fileLength; } - - } catch(ConnectionException e) { - - e.printStackTrace(); - - }finally { - - if(conn != null) { - + + new DownloadThread(conn, startPos, endPos).start(); + } + + } catch (ConnectionException e) { + + e.printStackTrace(); + + } finally { + + if (conn != null) { + conn.close(); - - } + } - - } - - public void setListener(DownloadListener listener) { - - this.listener = listener; } - public void setConnectionManager(ConnectionManager ucm) { - - this.cm = ucm; - } - - public DownloadListener getListener() { - - return this.listener; - } - - + } + + public void setListener(DownloadListener listener) { + + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm) { + + this.cm = ucm; + } + + public DownloadListener getListener() { + + return this.listener; + } + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/download/FileDownloaderTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/download/FileDownloaderTest.java index 91ddbfa6f8..adf7dad676 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/download/FileDownloaderTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/download/FileDownloaderTest.java @@ -9,52 +9,52 @@ import com.github.HarryHook.coding2017.download.impl.ConnectionManagerImpl; public class FileDownloaderTest { - - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - - } - @After - public void tearDown() throws Exception { - - } + boolean downloadFinished = false; + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testDownload() { + + String url = "https://www.baidu.com/img/bd_logo.png"; - @Test - public void testDownload() { - - String url = "https://www.baidu.com/img/bd_logo.png"; - - FileDownloader downloader = new FileDownloader(url); - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - - downloadFinished = true; - } - }); - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - - try { - System.out.println("还没有下载完成,休眠两秒"); - //休眠2秒 - Thread.sleep(2000); - } catch (InterruptedException e) { - e.printStackTrace(); - } + FileDownloader downloader = new FileDownloader(url); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + + downloadFinished = true; } - System.out.println("下载完成!"); - + }); + + downloader.execute(); + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + + try { + System.out.println("还没有下载完成,休眠两秒"); + // 休眠2秒 + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } } + System.out.println("下载完成!"); + + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/download/api/Connection.java b/group02/727171008/src/com/github/HarryHook/coding2017/download/api/Connection.java index 92b2fe1050..7f6a13f3e0 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/download/api/Connection.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/download/api/Connection.java @@ -3,22 +3,26 @@ import java.io.IOException; public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * @param startPos 开始位置, 从0开始 - * @param endPos 结束位置 - * @return - */ - public byte[] read(int startPos,int endPos) throws IOException; - - /** - * 得到数据内容的长度 - * @return - */ - public int getContentLength(); - - /** - * 关闭连接 - */ - public void close(); + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * + * @param startPos + * 开始位置, 从0开始 + * @param endPos + * 结束位置 + * @return + */ + public byte[] read(int startPos, int endPos) throws IOException; + + /** + * 得到数据内容的长度 + * + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/download/api/ConnectionManager.java b/group02/727171008/src/com/github/HarryHook/coding2017/download/api/ConnectionManager.java index 57368f7b35..be85791a50 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/download/api/ConnectionManager.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/download/api/ConnectionManager.java @@ -1,10 +1,11 @@ package com.github.HarryHook.coding2017.download.api; public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; + /** + * 给定一个url , 打开一个连接 + * + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/download/impl/ConnectionImpl.java b/group02/727171008/src/com/github/HarryHook/coding2017/download/impl/ConnectionImpl.java index 2932ec1ddb..84a55784f7 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/download/impl/ConnectionImpl.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/download/impl/ConnectionImpl.java @@ -9,57 +9,57 @@ import com.github.HarryHook.coding2017.download.api.Connection; public class ConnectionImpl implements Connection { - - private String url; - - public ConnectionImpl(String url) { - - this.url = url; - } - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - URL url = new URL(this.url); - HttpURLConnection conn = (HttpURLConnection)url.openConnection(); - //conn.setRequestMethod("GET"); - // 设置500毫秒为超时值 - // conn.setReadTimeout(5000); - conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); - BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); - ByteArrayOutputStream out = new ByteArrayOutputStream(1024); - - byte[] buffer = new byte[1024]; - int size = 0; - while ((size = in.read(buffer)) != -1) { - out.write(buffer, 0, size); - } - byte[] b = out.toByteArray(); - out.close(); - in.close(); - - return b; - } + private String url; + + public ConnectionImpl(String url) { + + this.url = url; + } - @Override - public int getContentLength() { - - try { - URL url1 = new URL(url); - HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); - return conn.getContentLength(); - - }catch(Exception e) { - - e.printStackTrace(); - } - - return -1; + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + URL url = new URL(this.url); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + // conn.setRequestMethod("GET"); + // 设置500毫秒为超时值 + // conn.setReadTimeout(5000); + conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); + ByteArrayOutputStream out = new ByteArrayOutputStream(1024); + + byte[] buffer = new byte[1024]; + int size = 0; + while ((size = in.read(buffer)) != -1) { + out.write(buffer, 0, size); } + byte[] b = out.toByteArray(); + out.close(); + in.close(); + + return b; + } - @Override - public void close() { - + @Override + public int getContentLength() { + + try { + URL url1 = new URL(url); + HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); + return conn.getContentLength(); + + } catch (Exception e) { + + e.printStackTrace(); } + return -1; + } + + @Override + public void close() { + + } + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/download/impl/ConnectionManagerImpl.java b/group02/727171008/src/com/github/HarryHook/coding2017/download/impl/ConnectionManagerImpl.java index 2ce38b1f79..ee40d1e943 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/download/impl/ConnectionManagerImpl.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/download/impl/ConnectionManagerImpl.java @@ -6,9 +6,9 @@ public class ConnectionManagerImpl implements ConnectionManager { - @Override - public Connection open(String url) throws ConnectionException { - return new ConnectionImpl(url); - } + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/jvm/loader/ClassFileLoader.java b/group02/727171008/src/com/github/HarryHook/coding2017/jvm/loader/ClassFileLoader.java index 5c52c492a3..c0d992bda8 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/jvm/loader/ClassFileLoader.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/jvm/loader/ClassFileLoader.java @@ -1,6 +1,5 @@ package com.github.HarryHook.coding2017.jvm.loader; - import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -8,67 +7,60 @@ 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 class ClassFileLoader { + public byte[] readBinaryCode(String className) { - private List clzPaths = new ArrayList(); - - public byte[] readBinaryCode(String className) { - - - String fileName = clzPaths.get(0) + File.separatorChar + - className.replace('.', File.separatorChar) + ".class"; - - InputStream in = null; - try { - in = new FileInputStream(fileName); - ByteArrayOutputStream out = new ByteArrayOutputStream(1024); - - byte[] buffer = new byte[1024]; - int length = 0; - while((length = in.read(buffer)) != -1) { - out.write(buffer, 0, length); - } - return out.toByteArray(); - } catch(IOException e) { - e.printStackTrace(); - } finally { - if(in != null) { - try{ - in.close(); - }catch(IOException e) { - e.printStackTrace(); - } + String fileName = clzPaths.get(0) + File.separatorChar + + className.replace('.', File.separatorChar) + ".class"; + + InputStream in = null; + try { + in = new FileInputStream(fileName); + ByteArrayOutputStream out = new ByteArrayOutputStream(1024); + + byte[] buffer = new byte[1024]; + int length = 0; + while ((length = in.read(buffer)) != -1) { + out.write(buffer, 0, length); + } + return out.toByteArray(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); } } - return null; - - } - - - public void addClassPath(String path) { - - this.clzPaths.add(path); } - - - - public String getClassPath(){ - - StringBuilder buffer = new StringBuilder(); - - for(int i=0; i capacity) { + // 若缓存已满,移除最后一个节点 + if (currentSize > capacity) { last = last.prev; last.next = null; } - }else { - moveToFirst(iteratorNode); + } else { + moveToFirst(iteratorNode); } } - } + } } + // 将节点/缓存页添加到fitrst public void addToFirst(int pageNum) { Node node = new Node(); @@ -88,7 +88,8 @@ public void addToFirst(int pageNum) { first = node; currentSize++; } - //将last节点移动到first + + // 将last节点移动到first public void moveLastToFirst() { last.next = first; first.prev = last; @@ -97,7 +98,8 @@ public void moveLastToFirst() { last.next = null; first.prev = null; } - //将最近使用的已有缓存页移动到first + + // 将最近使用的已有缓存页移动到first public void moveToFirst(Node iteratorNode) { iteratorNode.prev.next = iteratorNode.next; iteratorNode.next.prev = iteratorNode.prev; @@ -106,19 +108,18 @@ public void moveToFirst(Node iteratorNode) { first.prev = iteratorNode; first = iteratorNode; } - + public String toString() { StringBuilder buffer = new StringBuilder(); Node node = first; - while(node != null) { - buffer.append(node.pageNum); - node = node.next; - if(node != null) { + while (node != null) { + buffer.append(node.pageNum); + node = node.next; + if (node != null) { buffer.append(","); } } return buffer.toString(); } - - + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrameTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrameTest.java index 004b99eac4..f25cb5d5e9 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrameTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/linklist/LRUPageFrameTest.java @@ -1,35 +1,34 @@ package com.github.HarryHook.coding2017.linklist; -import org.junit.Assert; +import org.junit.Assert; import org.junit.Test; - public class LRUPageFrameTest { - - @Test - public void testAccess() { - LRUPageFrame frame = new LRUPageFrame(3); - frame.access(7); - Assert.assertEquals("7", frame.toString()); - frame.access(0); - Assert.assertEquals("0,7", frame.toString()); - frame.access(7); - Assert.assertEquals("7,0", frame.toString()); - frame.access(1); - Assert.assertEquals("1,7,0", frame.toString()); - frame.access(2); - Assert.assertEquals("2,1,7", 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()); - } + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + Assert.assertEquals("7", frame.toString()); + frame.access(0); + Assert.assertEquals("0,7", frame.toString()); + frame.access(7); + Assert.assertEquals("7,0", frame.toString()); + frame.access(1); + Assert.assertEquals("1,7,0", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,7", 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/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Configuration.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Configuration.java index c44457c58f..f2db5581f5 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Configuration.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Configuration.java @@ -10,88 +10,93 @@ import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; - public class Configuration { - Map actions = new HashMap<>(); - - public Configuration(String fileName) { - String packageName = this.getClass().getPackage().getName(); - - packageName = packageName.replace('.', '/'); - - InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); - - parseXML(is); - - try { - is.close(); - }catch(IOException e) { - throw new ConfigurationException(e); - } + Map actions = new HashMap<>(); + + public Configuration(String fileName) { + String packageName = this.getClass().getPackage().getName(); + + packageName = packageName.replace('.', '/'); + + InputStream is = this.getClass().getResourceAsStream("/" + packageName + "/" + fileName); + + parseXML(is); + + try { + is.close(); + } catch (IOException e) { + throw new ConfigurationException(e); } - private void parseXML(InputStream is) { - SAXBuilder builder = new SAXBuilder(); - - try { - Document doc = builder.build(is); - - Element root = doc.getRootElement(); - - for(Element actionElement : root.getChildren("action")) { - String actionName = actionElement.getAttributeValue("name"); - String clzName = actionElement.getAttributeValue("class"); - - ActionConfig ac = new ActionConfig(actionName, clzName); - - for(Element resultElement : actionElement.getChildren("result")) { - String resultName = resultElement.getAttributeValue("name"); - String viewName = resultElement.getText().trim(); - - ac.addViewResult(resultName, viewName); - } - this.actions.put(actionName, ac); - } - }catch(JDOMException e) { - throw new ConfigurationException(e); - - }catch(IOException e) { - throw new ConfigurationException(e); - + } + + private void parseXML(InputStream is) { + SAXBuilder builder = new SAXBuilder(); + + try { + Document doc = builder.build(is); + + Element root = doc.getRootElement(); + + for (Element actionElement : root.getChildren("action")) { + String actionName = actionElement.getAttributeValue("name"); + String clzName = actionElement.getAttributeValue("class"); + + ActionConfig ac = new ActionConfig(actionName, clzName); + + for (Element resultElement : actionElement.getChildren("result")) { + String resultName = resultElement.getAttributeValue("name"); + String viewName = resultElement.getText().trim(); + + ac.addViewResult(resultName, viewName); } + this.actions.put(actionName, ac); + } + } catch (JDOMException e) { + throw new ConfigurationException(e); + + } catch (IOException e) { + throw new ConfigurationException(e); + } + } - public String getClassName(String action) { - ActionConfig ac = this.actions.get(action); - if(ac == null) { - return null; - } - return ac.getClassName(); + public String getClassName(String action) { + ActionConfig ac = this.actions.get(action); + if (ac == null) { + return null; } - public String getResultView(String action, String resultName) { - ActionConfig ac = this.actions.get(action); - if(ac == null) { - return null; - } - return ac.getViewName(resultName); + return ac.getClassName(); + } + + public String getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if (ac == null) { + return null; } - private static class ActionConfig { - String name; - String clzName; - Map viewResult = new HashMap<>(); - - public ActionConfig(String actionName, String clzName) { - this.clzName = clzName; - this.name = actionName; - } - public String getClassName() { - return clzName; - } - public void addViewResult(String name, String viewName) { - viewResult.put(name, viewName); - } - public String getViewName(String resultName) { - return viewResult.get(resultName); - } - + return ac.getViewName(resultName); + } + + private static class ActionConfig { + String name; + String clzName; + Map viewResult = new HashMap<>(); + + public ActionConfig(String actionName, String clzName) { + this.clzName = clzName; + this.name = actionName; + } + + public String getClassName() { + return clzName; + } + + public void addViewResult(String name, String viewName) { + viewResult.put(name, viewName); + } + + public String getViewName(String resultName) { + return viewResult.get(resultName); } + + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ConfigurationException.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ConfigurationException.java index df763a0ab4..83ab2dbe85 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ConfigurationException.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ConfigurationException.java @@ -1,18 +1,19 @@ package com.github.HarryHook.coding2017.litestruts; - import java.io.IOException; import org.jdom2.JDOMException; public class ConfigurationException extends RuntimeException { - public ConfigurationException(String msg) { - super(msg); - } - public ConfigurationException(JDOMException e) { - super(e); - } - public ConfigurationException(IOException e) { - super(e); - } + public ConfigurationException(String msg) { + super(msg); + } + + public ConfigurationException(JDOMException e) { + super(e); + } + + public ConfigurationException(IOException e) { + super(e); + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ConfigurationTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ConfigurationTest.java index 9e25a7974e..114104c87e 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ConfigurationTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ConfigurationTest.java @@ -6,38 +6,39 @@ import org.junit.Test; public class ConfigurationTest { - - Configuration cfg = new Configuration("struts.xml"); - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testGetClassName() { - String clzName = cfg.getClassName("login"); - Assert.assertEquals("com.github.HarryHook.coding2017.litestruts.LoginAction", clzName); - - clzName = cfg.getClassName("logout"); - Assert.assertEquals("com.github.HarryHook.coding2017.litestruts.LogoutAction", clzName); - } - - @Test - public void testGetResultView() { - String jsp = cfg.getResultView("login", "success"); - Assert.assertEquals("/jsp/homepage.jsp", jsp); - - jsp = cfg.getResultView("login", "fail"); - Assert.assertEquals("/jsp/showLogin.jsp", jsp); - - jsp = cfg.getResultView("logout", "success"); - Assert.assertEquals("/jsp/welcome.jsp", jsp); - - jsp = cfg.getResultView("logout", "error"); - Assert.assertEquals("/jsp/error.jsp", jsp); - } + + Configuration cfg = new Configuration("struts.xml"); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetClassName() { + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.github.HarryHook.coding2017.litestruts.LoginAction", clzName); + + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.github.HarryHook.coding2017.litestruts.LogoutAction", clzName); + } + + @Test + public void testGetResultView() { + String jsp = cfg.getResultView("login", "success"); + Assert.assertEquals("/jsp/homepage.jsp", jsp); + + jsp = cfg.getResultView("login", "fail"); + Assert.assertEquals("/jsp/showLogin.jsp", jsp); + + jsp = cfg.getResultView("logout", "success"); + Assert.assertEquals("/jsp/welcome.jsp", jsp); + + jsp = cfg.getResultView("logout", "error"); + Assert.assertEquals("/jsp/error.jsp", jsp); + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/LoginAction.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/LoginAction.java index d42f7b0cd6..120d72f1e0 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/LoginAction.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/LoginAction.java @@ -2,41 +2,42 @@ /** * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * * @author liuxin * */ public class LoginAction { - - private String name ; + + private String name; private String password; private String message; - public String getName() { - return name; + public String getName() { + return name; } public String getPassword() { - return password; + return password; } public String execute() { - if("test".equals(name) && "1234".equals(password)) { - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; } - + public void setName(String name) { - this.name = name; + this.name = name; } - + public void setPassword(String password) { - this.password = password; + this.password = password; } - + public String getMessage() { - return this.message; + return this.message; } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ReflectionUtil.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ReflectionUtil.java index 1efcb21574..850f644a2b 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ReflectionUtil.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ReflectionUtil.java @@ -7,76 +7,74 @@ import java.util.List; import java.util.Map; +public class ReflectionUtil { -public class ReflectionUtil -{ + public static List getSetterMethods(Class clz) { - public static List getSetterMethods(Class clz) { - - return getMethods(clz, "set"); - } + return getMethods(clz, "set"); + } + + public static void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + + for (String name : params.keySet()) { + + String methodName = "set" + name; + + for (Method m : methods) { - public static void setParameters(Object o, Map params) { - - List methods = getSetterMethods(o.getClass()); - - for(String name : params.keySet()) { - - String methodName = "set" + name; - - for(Method m : methods) { - - if(m.getName().equalsIgnoreCase(methodName)) { - try { - m.invoke(o, params.get(name)); - - }catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - e.printStackTrace(); - } + if (m.getName().equalsIgnoreCase(methodName)) { + try { + m.invoke(o, params.get(name)); + + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); } } } - } - public static List getGetterMethods(Class clz) { - - return getMethods(clz, "get"); - } + } - public static List getMethods(Class clz, String startWithName) { - - List methods = new ArrayList<>(); - for(Method m : clz.getDeclaredMethods()) { + public static List getGetterMethods(Class clz) { - if(m.getName().startsWith(startWithName)) { - methods.add(m); - } + return getMethods(clz, "get"); + } + + public static List getMethods(Class clz, String startWithName) { + + List methods = new ArrayList<>(); + for (Method m : clz.getDeclaredMethods()) { + + if (m.getName().startsWith(startWithName)) { + methods.add(m); } - - return methods; } - public static Map getParameterMap(Object o) { - - Map params = new HashMap<>(); - - List methods = getGetterMethods(o.getClass()); - - for(Method m : methods) { - - String methodName = m.getName(); - String name = methodName.replaceFirst("get", "").toLowerCase(); - - try { - Object value = m.invoke(o); - params.put(name, value); - - }catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - e.printStackTrace(); - } + return methods; + } + + public static Map getParameterMap(Object o) { + + Map params = new HashMap<>(); + + List methods = getGetterMethods(o.getClass()); + + for (Method m : methods) { + + String methodName = m.getName(); + String name = methodName.replaceFirst("get", "").toLowerCase(); + + try { + Object value = m.invoke(o); + params.put(name, value); + + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); } - - return params; } + + return params; + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ReflectionUtilTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ReflectionUtilTest.java index ff4c30f05d..043b2d3050 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ReflectionUtilTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/ReflectionUtilTest.java @@ -14,100 +14,99 @@ import org.junit.Before; import org.junit.Test; - public class ReflectionUtilTest { - @Before - public void setUp() throws Exception { - } + @Before + public void setUp() throws Exception { + } - @After - public void tearDown() throws Exception { - } + @After + public void tearDown() throws Exception { + } - @Test - public void testGetSetterMethod() throws Exception { - - String name = "com.github.HarryHook.coding2017.litestruts.LoginAction"; - Class clz = Class.forName(name); - List methods = ReflectionUtil.getSetterMethods(clz); - - Assert.assertEquals(2, methods.size()); - - List expectedNames = new ArrayList<>(); - expectedNames.add("setName"); - expectedNames.add("setPassword"); - - Set acctualNames = new HashSet<>(); - for(Method m : methods) { - - acctualNames.add(m.getName()); - } - - Assert.assertTrue(acctualNames.containsAll(expectedNames)); - } - - @Test - public void testSetParameters() throws Exception { - - String name = "com.github.HarryHook.coding2017.litestruts.LoginAction"; - Class clz = Class.forName(name); - Object o = clz.newInstance(); - - Map params = new HashMap(); - params.put("name", "test"); - params.put("password", "1234"); - - ReflectionUtil.setParameters(o, params); - - Field f = clz.getDeclaredField("name"); - - f.setAccessible(true); - Assert.assertEquals("test", f.get(o)); - - f = clz.getDeclaredField("password"); - f.setAccessible(true); - Assert.assertEquals("1234", f.get(o)); - } - - @Test - public void testGetGetterMethod() throws Exception { - - String name = "com.github.HarryHook.coding2017.litestruts.LoginAction"; - Class clz = Class.forName(name); - List methods = ReflectionUtil.getGetterMethods(clz); - - Assert.assertEquals(3, methods.size()); - - List expectedNames = new ArrayList<>(); - expectedNames.add("getMessage"); - expectedNames.add("getName"); - expectedNames.add("getPassword"); - - Set acctualNames = new HashSet<>(); - for(Method m : methods) { - - acctualNames.add(m.getName()); - } - - Assert.assertTrue(acctualNames.containsAll(expectedNames)); - + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.github.HarryHook.coding2017.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getSetterMethods(clz); + + Assert.assertEquals(2, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("setName"); + expectedNames.add("setPassword"); + + Set acctualNames = new HashSet<>(); + for (Method m : methods) { + + acctualNames.add(m.getName()); } - - @Test - public void testGetParameters() throws Exception { - - String name = "com.github.HarryHook.coding2017.litestruts.LoginAction"; - Class clz = Class.forName(name); - LoginAction action = (LoginAction)clz.newInstance(); - action.setName("test"); - action.setPassword("1234"); - - Map params = ReflectionUtil.getParameterMap(action); - Assert.assertEquals(3, params.size()); - Assert.assertEquals(null, params.get("message")); - Assert.assertEquals("test", params.get("name")); - Assert.assertEquals("1234", params.get("password")); + + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + } + + @Test + public void testSetParameters() throws Exception { + + String name = "com.github.HarryHook.coding2017.litestruts.LoginAction"; + Class clz = Class.forName(name); + Object o = clz.newInstance(); + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + ReflectionUtil.setParameters(o, params); + + Field f = clz.getDeclaredField("name"); + + f.setAccessible(true); + Assert.assertEquals("test", f.get(o)); + + f = clz.getDeclaredField("password"); + f.setAccessible(true); + Assert.assertEquals("1234", f.get(o)); + } + + @Test + public void testGetGetterMethod() throws Exception { + + String name = "com.github.HarryHook.coding2017.litestruts.LoginAction"; + Class clz = Class.forName(name); + List methods = ReflectionUtil.getGetterMethods(clz); + + Assert.assertEquals(3, methods.size()); + + List expectedNames = new ArrayList<>(); + expectedNames.add("getMessage"); + expectedNames.add("getName"); + expectedNames.add("getPassword"); + + Set acctualNames = new HashSet<>(); + for (Method m : methods) { + + acctualNames.add(m.getName()); } + Assert.assertTrue(acctualNames.containsAll(expectedNames)); + + } + + @Test + public void testGetParameters() throws Exception { + + String name = "com.github.HarryHook.coding2017.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction action = (LoginAction) clz.newInstance(); + action.setName("test"); + action.setPassword("1234"); + + Map params = ReflectionUtil.getParameterMap(action); + Assert.assertEquals(3, params.size()); + Assert.assertEquals(null, params.get("message")); + Assert.assertEquals("test", params.get("name")); + Assert.assertEquals("1234", params.get("password")); + } + } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Struts.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Struts.java index 270e26a57d..4a55b6c2d1 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Struts.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Struts.java @@ -4,45 +4,43 @@ import java.util.Map; public class Struts { - + private final static Configuration cfg = new Configuration("struts.xml"); - - public static View runAction(String actionName, Map parameters) { - + + public static View runAction(String actionName, Map parameters) { + String clzName = cfg.getClassName(actionName); - - if(clzName == null) { - + + if (clzName == null) { + return null; - } - - + } + try { - - Class clz = Class.forName(clzName); + + Class clz = Class.forName(clzName); Object action = clz.newInstance(); - + ReflectionUtil.setParameters(action, parameters); - + Method m = clz.getDeclaredMethod("execute"); - String resultName = (String)m.invoke(action); + String resultName = (String) m.invoke(action); - Map params = ReflectionUtil.getParameterMap(action); String resultView = cfg.getResultView(actionName, resultName); - + View view = new View(); view.setParameters(params); view.setJsp(resultView); return view; - - }catch(Exception e) { - + + } catch (Exception e) { + e.printStackTrace(); } - + return null; - + } } \ No newline at end of file diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java index 23dd0a0845..26b12d610c 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java @@ -8,43 +8,43 @@ public class StrutsTest { - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view; + + view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + + } + + @Test + public void testLoginActionFailed() { + + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); // 密码和预设的不一致 + + try { + View view; - + view = Struts.runAction(actionName, params); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - + } catch (Exception e) { + e.printStackTrace(); } - @Test - public void testLoginActionFailed() { - - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - try { - - View view; - - view = Struts.runAction(actionName,params); - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - - }catch(Exception e) { - e.printStackTrace(); - } - - } + } } diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java index 3e35a849c3..258b376ae2 100644 --- a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java @@ -2,30 +2,31 @@ import java.util.Map; -public class View -{ - private String jsp; - private Map parameters; - - //对应action获取Jsp - public String getJsp() { - - return jsp; - } - public View setJsp(String jsp) { - - this.jsp = jsp; - return this; - } - - //execute()获取对应参数 - public Map getParameters() { - - return parameters; - } - public View setParameters(Map parameters) { - - this.parameters = parameters; - return this; - } +public class View { + private String jsp; + private Map parameters; + + // 对应action获取Jsp + public String getJsp() { + + return jsp; + } + + public View setJsp(String jsp) { + + this.jsp = jsp; + return this; + } + + // execute()获取对应参数 + public Map getParameters() { + + return parameters; + } + + public View setParameters(Map parameters) { + + this.parameters = parameters; + return this; + } }