From 4656c624a3b1b1ba1404617d9548f1e54f45b2c8 Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Mon, 6 Mar 2017 11:37:43 +0800 Subject: [PATCH 001/155] second homework --- .../ArrayList.java | 0 .../ArrayUtil.java | 83 +++++++++++++++++++ 2 files changed, 83 insertions(+) rename group11/1310368322/GitHub/src/{ => Day_2017_2_26_FirstHomework}/ArrayList.java (100%) create mode 100644 group11/1310368322/GitHub/src/day_2017_2_26_SecondHomework/ArrayUtil.java diff --git a/group11/1310368322/GitHub/src/ArrayList.java b/group11/1310368322/GitHub/src/Day_2017_2_26_FirstHomework/ArrayList.java similarity index 100% rename from group11/1310368322/GitHub/src/ArrayList.java rename to group11/1310368322/GitHub/src/Day_2017_2_26_FirstHomework/ArrayList.java diff --git a/group11/1310368322/GitHub/src/day_2017_2_26_SecondHomework/ArrayUtil.java b/group11/1310368322/GitHub/src/day_2017_2_26_SecondHomework/ArrayUtil.java new file mode 100644 index 0000000000..81bc431679 --- /dev/null +++ b/group11/1310368322/GitHub/src/day_2017_2_26_SecondHomework/ArrayUtil.java @@ -0,0 +1,83 @@ +package day_2017_2_26_SecondHomework; + +import java.util.Arrays; + +import javax.management.RuntimeErrorException; + +public class ArrayUtil { + + /* * + * 给定一个整形数组 a ,对该数组的值进行置换 + * 例如: a = [7, 9, 30, 3], 置换后为 [3, 30, 9, 7] + * */ + + /*public ArrayUtil(int[] a2) { + this.a = a2; + }*/ + public void reverseArray(int [] a){ + if(null == a){ + System.out.println("空指针----"); + return; + } + int temp; + int last = a.length-1; + for (int i = 0; i < a.length/2; i++) { + temp = a[i]; + a[i] = a[last]; + a[last--] = temp; + } + } + public void print(int [] a){ + if(null == a){ + System.out.println("空指针----"); + return; + } + for (int i = 0; i < a.length; i++) { + System.out.print(a[i] + " "); + } + System.out.println(); + } + + /* * + * 现在有如下的一个数组, int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中的值为 0 的项去掉,将不为 0 的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + public int [] removeZero(int [] oldArray){ + if(null == oldArray){ + return null; + } + int count = 0; + int oldArrayLength = oldArray.length; + for(int i = 0; i < oldArrayLength;){ + if(oldArray[i]==0){ + for(int j = i; j < oldArrayLength -1; j++){ + oldArray[j] = oldArray[j+1]; + } + oldArrayLength--; + count++; + }else{ + i++; + } + } + int [] target = new int[oldArray.length-count]; + System.arraycopy(oldArray, 0, target, 0, oldArray.length-count); + return target; + } + + + + + + + + + + + + + + +} From 7af7f85a0b7244b65342eab9a6fb38d6766fbbb4 Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Mon, 6 Mar 2017 11:55:22 +0800 Subject: [PATCH 002/155] new --- group11/1310368322/src/ArrayList.java | 98 ++++++++++++++++++ group11/1310368322/src/LinkedList.java | 113 +++++++++++++++++++++ group11/1310368322/test/ArrayUtilTest.java | 101 ++++++++++++++++++ 3 files changed, 312 insertions(+) create mode 100644 group11/1310368322/src/ArrayList.java create mode 100644 group11/1310368322/src/LinkedList.java create mode 100644 group11/1310368322/test/ArrayUtilTest.java diff --git a/group11/1310368322/src/ArrayList.java b/group11/1310368322/src/ArrayList.java new file mode 100644 index 0000000000..b5024f1dfb --- /dev/null +++ b/group11/1310368322/src/ArrayList.java @@ -0,0 +1,98 @@ +package Day_2017_2_26_FirstHomework; + +public class ArrayList { + + private static final int DEFAULT_SIZE = 10; + private static final int MAX_VALUE = 2147483647; + private Object[] elementData = new Object[DEFAULT_SIZE]; + private Exception Exception; + private int size = 0; + + public ArrayList(){ + this(DEFAULT_SIZE); + } + public ArrayList(int defaultSize) { + rangCheckForConstructor(defaultSize); + elementData = new Object[defaultSize]; + } + + + private void rangCheckForConstructor(int defaultSize) { + if(defaultSize<0 || defaultSize>MAX_VALUE){ + throw new IndexOutOfBoundsException("数值不合理"); + } + + } + + public void add(Object o){ + ensureCapacity(); + for(int i = 0; i < elementData.length; i++){ + if(null == elementData[i]){ + elementData[i] = o; + break; + } + } + size++; + } + private void ensureCapacity() { + if(size>elementData.length){ + elementData = ArrayList.grow(elementData, 10); + } + } + public void add(int index, Object o){ + rangeCheckForAdd(index); + ensureCapacity(); + int k = -1; + for(int i = index; i < elementData.length; i++){ + if(null==elementData[i]){ + k = i-1; + break; + } + } + for(int i = k; i >= index;i--){ + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + } + private void rangeCheckForAdd(int index) { + if(index < 0 || index > this.size){// add 的元素只能在 [0,size](可以给size位置插元素,但不可以给size后插元素) + throw new IndexOutOfBoundsException("下标越界"); + } + + } + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + while(true){ + elementData[index] = elementData[index+++1]; + if(elementData[index]==null){ + break; + } + } + size--; + return null; + } + public int size(){ + return -1; + } + public void getElementData(){ + for(int i = 0; i < elementData.length; i++){ + System.out.println(elementData[i]); + + } + } + public static Object[] grow(Object[] elementData2, int size){ + Object []target = new Object[elementData2.length+size]; + System.arraycopy(elementData2, 0, target, 0, elementData2.length); + return target; + } + + public static void main(String[] args) { + ArrayList a = new ArrayList(); + a.getElementData(); + System.out.println(a.size); + } +} diff --git a/group11/1310368322/src/LinkedList.java b/group11/1310368322/src/LinkedList.java new file mode 100644 index 0000000000..488f2a22a6 --- /dev/null +++ b/group11/1310368322/src/LinkedList.java @@ -0,0 +1,113 @@ +package Day_2017_2_26_FirstHomework; + +public class LinkedList{ + private Node head; + static int size = 0; + public void add(Object o){ + if(null == head){ + head = new Node(); + head.data = o; + head.next = null; + }else{ + Node p = head; + while(null != p.next){ + p = p.next; + } + Node newNode = new Node(); + newNode.data = o; + p.next = newNode; + newNode.next =null; + } + size++; + } + public int size(){ + return size; + } + public void add(int index,Object o){ + if(index < 0){ + throw new RuntimeException("下标不能为负数"); + } + if(index == 0){ + addFirst(o); + size++; + return; + } + if(index > size){ + throw new RuntimeException(""); + } + int i = 0; + Node p = head; + Node q = null; + + while(i!=index){ + q = p; + p = p.next; + i++; + } + Node r = new Node(); + r.data = o; + r.next =null; + q.next = r; + r.next = p; + size++; + return; + } + + public Object get(int index){ + int i = 0; + Node p = head; + while(i != index){ + p = p.next; + i++; + } + return p.data; + } + public Object remove(int index){ + if(index < 0){ + throw new RuntimeException("下标不能为负数"); + } + if(index == 1){ + size--; + return head.data; + } + int i = 0; + Node p = head; + Node q = null; + while(i != index){ + q = p; + p = p.next; + i++; + } + q.next = p.next; + size--; + return p.data; + } + public void addFirst(Object o){ + Node p = new Node(); + p.next = head; + p.data = o; + head = p; + size++; + } + public Object removeFirst(){ + head = head.next; + size--; + return null; + } + public static class Node{ + Object data; + Node next; + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add("a"); + linkedList.add("b"); + linkedList.add("c"); + linkedList.add("d"); + linkedList.add(5, "f"); + System.out.println(linkedList.get(5)); + System.out.println(linkedList.size()); + } + +} \ No newline at end of file diff --git a/group11/1310368322/test/ArrayUtilTest.java b/group11/1310368322/test/ArrayUtilTest.java new file mode 100644 index 0000000000..aaf3a80e7e --- /dev/null +++ b/group11/1310368322/test/ArrayUtilTest.java @@ -0,0 +1,101 @@ +package day_2017_2_26_SecondHomework; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + ArrayUtil arrayUtil; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray_a() { + int [] actuals = {}; + int [] expected = {}; + arrayUtil.reverseArray(actuals); + Assert.assertArrayEquals(expected, actuals); + } + @Test + public void testReverseArray_b() { + int [] actuals = null; + int [] expected = null; + arrayUtil.reverseArray(actuals); + Assert.assertArrayEquals(expected, actuals); + } + @Test + public void testReverseArray_c() { + int [] actuals = {1,2,3,4}; + int [] expected = {4,3,2,1}; + arrayUtil.reverseArray(actuals); + Assert.assertArrayEquals(expected, actuals); + } + + + @Test + public void testRemoveZero_1(){ + int [] actuals = null; + int [] expected = null; + int [] actual = arrayUtil.removeZero(actuals); + Assert.assertArrayEquals(expected, actual); + } + @Test + public void testRemoveZero_2(){ + int [] actuals = {}; + int [] expected = {}; + int [] actual = arrayUtil.removeZero(actuals); + Assert.assertArrayEquals(expected, actual); + } + @Test + public void testRemoveZero_3(){ + int [] actuals = {0,0,0,0,0,0}; + int [] expected = {}; + int [] actual = arrayUtil.removeZero(actuals); + Assert.assertArrayEquals(expected, actual); + } + @Test + public void testRemoveZero_4(){ + int [] actuals = {1,2,3,4,5,6}; + int [] expected = {1,2,3,4,5,6}; + int [] actual = arrayUtil.removeZero(actuals); + Assert.assertArrayEquals(expected, actual); + } + @Test + public void testRemoveZero_5(){ + int [] actuals = {1,2,0,0,5,6}; + int [] expected = {1,2,5,6}; + int [] actual = arrayUtil.removeZero(actuals); + Assert.assertArrayEquals(expected, actual); + } + @Test + public void testRemoveZero_6(){ + int [] actuals = {0,0,4,2}; + int [] expected = {4,2}; + int [] actual = arrayUtil.removeZero(actuals); + Assert.assertArrayEquals(expected, actual); + }@Test + public void testRemoveZero_7(){ + int [] actuals = {4,2,0,0,0}; + int [] expected = {4,2}; + int [] actual = arrayUtil.removeZero(actuals); + Assert.assertArrayEquals(expected, actual); + } + public void testRemoveZero_8(){ + int [] actuals = {0,0,4,0,0,2,0,0,0}; + int [] expected = {4,2}; + int [] actual = arrayUtil.removeZero(actuals); + Assert.assertArrayEquals(expected, actual); + } + + +} From c7f3d11a592e37a418ffcf5d1934b686c4d09a29 Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Mon, 6 Mar 2017 12:11:32 +0800 Subject: [PATCH 003/155] second --- group11/1310368322/src/ArrayUtil.java | 83 +++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 group11/1310368322/src/ArrayUtil.java diff --git a/group11/1310368322/src/ArrayUtil.java b/group11/1310368322/src/ArrayUtil.java new file mode 100644 index 0000000000..81bc431679 --- /dev/null +++ b/group11/1310368322/src/ArrayUtil.java @@ -0,0 +1,83 @@ +package day_2017_2_26_SecondHomework; + +import java.util.Arrays; + +import javax.management.RuntimeErrorException; + +public class ArrayUtil { + + /* * + * 给定一个整形数组 a ,对该数组的值进行置换 + * 例如: a = [7, 9, 30, 3], 置换后为 [3, 30, 9, 7] + * */ + + /*public ArrayUtil(int[] a2) { + this.a = a2; + }*/ + public void reverseArray(int [] a){ + if(null == a){ + System.out.println("空指针----"); + return; + } + int temp; + int last = a.length-1; + for (int i = 0; i < a.length/2; i++) { + temp = a[i]; + a[i] = a[last]; + a[last--] = temp; + } + } + public void print(int [] a){ + if(null == a){ + System.out.println("空指针----"); + return; + } + for (int i = 0; i < a.length; i++) { + System.out.print(a[i] + " "); + } + System.out.println(); + } + + /* * + * 现在有如下的一个数组, int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中的值为 0 的项去掉,将不为 0 的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + public int [] removeZero(int [] oldArray){ + if(null == oldArray){ + return null; + } + int count = 0; + int oldArrayLength = oldArray.length; + for(int i = 0; i < oldArrayLength;){ + if(oldArray[i]==0){ + for(int j = i; j < oldArrayLength -1; j++){ + oldArray[j] = oldArray[j+1]; + } + oldArrayLength--; + count++; + }else{ + i++; + } + } + int [] target = new int[oldArray.length-count]; + System.arraycopy(oldArray, 0, target, 0, oldArray.length-count); + return target; + } + + + + + + + + + + + + + + +} From 87d2cfef839eeca434ef57bcf7207bbfd36d6aa9 Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Mon, 6 Mar 2017 14:33:56 +0800 Subject: [PATCH 004/155] remove GitHub --- group11/1310368322/GitHub/.gitignore | 1 - .../ArrayList.java | 79 ------------------ .../ArrayUtil.java | 83 ------------------- group11/1310368322/GitHub/src/testGitHub.java | 7 -- 4 files changed, 170 deletions(-) delete mode 100644 group11/1310368322/GitHub/.gitignore delete mode 100644 group11/1310368322/GitHub/src/Day_2017_2_26_FirstHomework/ArrayList.java delete mode 100644 group11/1310368322/GitHub/src/day_2017_2_26_SecondHomework/ArrayUtil.java delete mode 100644 group11/1310368322/GitHub/src/testGitHub.java diff --git a/group11/1310368322/GitHub/.gitignore b/group11/1310368322/GitHub/.gitignore deleted file mode 100644 index 5e56e040ec..0000000000 --- a/group11/1310368322/GitHub/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin diff --git a/group11/1310368322/GitHub/src/Day_2017_2_26_FirstHomework/ArrayList.java b/group11/1310368322/GitHub/src/Day_2017_2_26_FirstHomework/ArrayList.java deleted file mode 100644 index 68b42b1c03..0000000000 --- a/group11/1310368322/GitHub/src/Day_2017_2_26_FirstHomework/ArrayList.java +++ /dev/null @@ -1,79 +0,0 @@ -package Day_2017_2_20_DateStructure; - -public class ArrayList { - - - private int size = 0; - - private Object[] elementData = new Object[10]; - - private Exception Exception; - - public void add(Object o){ - if(size>elementData.length){ - elementData = ArrayList.grow(elementData, 10); - } - for(int i = 0; i < elementData.length; i++){ - if(null == elementData[i]){ - elementData[i] = o; - break; - } - } - size++; - } - public void add(int index, Object o){ - if(size>elementData.length){ - elementData = ArrayList.grow(elementData, 10); - } - if(index<0){ - System.out.println("您插入的位置有误"); - } - int k = -1; - for(int i = index; i < elementData.length; i++){ - if(null==elementData[i]){ - k = i-1; - break; - } - } - for(int i = k; i >= index;i--){ - elementData[i+1] = elementData[i]; - } - elementData[index] = o; - size++; - } - public Object get(int index){ - return elementData[index]; - } - - public Object remove(int index){ - while(true){ - elementData[index] = elementData[index+++1]; - if(elementData[index]==null){ - break; - } - } - size--; - return null; - } - public int size(){ - return -1; - } - public void getElementData(){ - for(int i = 0; i < elementData.length; i++){ - System.out.println(elementData[i]); - - } - } - public static Object[] grow(Object[] elementData2, int size){ - Object []target = new Object[elementData2.length+size]; - System.arraycopy(elementData2, 0, target, 0, elementData2.length); - return target; - } - - public static void main(String[] args) { - ArrayList a = new ArrayList(); - a.add("a"); - a.getElementData(); - System.out.println(a.size); - } -} diff --git a/group11/1310368322/GitHub/src/day_2017_2_26_SecondHomework/ArrayUtil.java b/group11/1310368322/GitHub/src/day_2017_2_26_SecondHomework/ArrayUtil.java deleted file mode 100644 index 81bc431679..0000000000 --- a/group11/1310368322/GitHub/src/day_2017_2_26_SecondHomework/ArrayUtil.java +++ /dev/null @@ -1,83 +0,0 @@ -package day_2017_2_26_SecondHomework; - -import java.util.Arrays; - -import javax.management.RuntimeErrorException; - -public class ArrayUtil { - - /* * - * 给定一个整形数组 a ,对该数组的值进行置换 - * 例如: a = [7, 9, 30, 3], 置换后为 [3, 30, 9, 7] - * */ - - /*public ArrayUtil(int[] a2) { - this.a = a2; - }*/ - public void reverseArray(int [] a){ - if(null == a){ - System.out.println("空指针----"); - return; - } - int temp; - int last = a.length-1; - for (int i = 0; i < a.length/2; i++) { - temp = a[i]; - a[i] = a[last]; - a[last--] = temp; - } - } - public void print(int [] a){ - if(null == a){ - System.out.println("空指针----"); - return; - } - for (int i = 0; i < a.length; i++) { - System.out.print(a[i] + " "); - } - System.out.println(); - } - - /* * - * 现在有如下的一个数组, int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中的值为 0 的项去掉,将不为 0 的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - public int [] removeZero(int [] oldArray){ - if(null == oldArray){ - return null; - } - int count = 0; - int oldArrayLength = oldArray.length; - for(int i = 0; i < oldArrayLength;){ - if(oldArray[i]==0){ - for(int j = i; j < oldArrayLength -1; j++){ - oldArray[j] = oldArray[j+1]; - } - oldArrayLength--; - count++; - }else{ - i++; - } - } - int [] target = new int[oldArray.length-count]; - System.arraycopy(oldArray, 0, target, 0, oldArray.length-count); - return target; - } - - - - - - - - - - - - - - -} diff --git a/group11/1310368322/GitHub/src/testGitHub.java b/group11/1310368322/GitHub/src/testGitHub.java deleted file mode 100644 index af74e99b53..0000000000 --- a/group11/1310368322/GitHub/src/testGitHub.java +++ /dev/null @@ -1,7 +0,0 @@ - -public class testGitHub { - private void mian() { - System.out.print("Hello GitHub"); - } - -} From ecafb62bacf99c75b0c7af64ec957dedd356a20d Mon Sep 17 00:00:00 2001 From: Dev_yang <501294009@qq.com> Date: Mon, 6 Mar 2017 23:31:39 +0800 Subject: [PATCH 005/155] commit first --- .../JavaProject/.idea/uiDesigner.xml | 124 ++++++++++++++++++ .../501294009/JavaProject/module1/README.md | 1 + group11/501294009/JavaProject/module1/pom.xml | 25 ++++ .../501294009/JavaProject/module2/README.md | 15 +++ group11/501294009/JavaProject/module2/pom.xml | 47 +++++++ .../src/main/java/com/yang/LoginAction.java | 39 ++++++ .../src/main/java/com/yang/Struts.java | 124 ++++++++++++++++++ .../src/main/java/com/yang/StrutsTest.java | 43 ++++++ .../module2/src/main/java/com/yang/View.java | 27 ++++ .../src/main/java/com/yang/bean/Action.java | 58 ++++++++ .../src/main/java/com/yang/bean/Result.java | 40 ++++++ .../src/main/java/com/yang/bean/Struts.java | 34 +++++ .../module2/src/main/resources/struts.xml | 11 ++ 13 files changed, 588 insertions(+) create mode 100644 group11/501294009/JavaProject/.idea/uiDesigner.xml create mode 100644 group11/501294009/JavaProject/module1/README.md create mode 100644 group11/501294009/JavaProject/module1/pom.xml create mode 100644 group11/501294009/JavaProject/module2/README.md create mode 100644 group11/501294009/JavaProject/module2/pom.xml create mode 100644 group11/501294009/JavaProject/module2/src/main/java/com/yang/LoginAction.java create mode 100644 group11/501294009/JavaProject/module2/src/main/java/com/yang/Struts.java create mode 100644 group11/501294009/JavaProject/module2/src/main/java/com/yang/StrutsTest.java create mode 100644 group11/501294009/JavaProject/module2/src/main/java/com/yang/View.java create mode 100644 group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Action.java create mode 100644 group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Result.java create mode 100644 group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Struts.java create mode 100644 group11/501294009/JavaProject/module2/src/main/resources/struts.xml diff --git a/group11/501294009/JavaProject/.idea/uiDesigner.xml b/group11/501294009/JavaProject/.idea/uiDesigner.xml new file mode 100644 index 0000000000..e96534fb27 --- /dev/null +++ b/group11/501294009/JavaProject/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group11/501294009/JavaProject/module1/README.md b/group11/501294009/JavaProject/module1/README.md new file mode 100644 index 0000000000..933a3ef67e --- /dev/null +++ b/group11/501294009/JavaProject/module1/README.md @@ -0,0 +1 @@ +module1 is achieve ArrayList, LinkedList, Queue, Stack BinaryTree 鍜孖terator \ No newline at end of file diff --git a/group11/501294009/JavaProject/module1/pom.xml b/group11/501294009/JavaProject/module1/pom.xml new file mode 100644 index 0000000000..98bfd43a2c --- /dev/null +++ b/group11/501294009/JavaProject/module1/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + com.yang + module1 + 1.0-SNAPSHOT + jar + + module1 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + diff --git a/group11/501294009/JavaProject/module2/README.md b/group11/501294009/JavaProject/module2/README.md new file mode 100644 index 0000000000..9bf528719a --- /dev/null +++ b/group11/501294009/JavaProject/module2/README.md @@ -0,0 +1,15 @@ +0. 璇诲彇閰嶇疆鏂囦欢struts.xml + +1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 +鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 +("name"="test" , "password"="1234") , +閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + +2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + +3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, +閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , +鏀惧埌View瀵硅薄鐨刾arameters + +4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 +鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 diff --git a/group11/501294009/JavaProject/module2/pom.xml b/group11/501294009/JavaProject/module2/pom.xml new file mode 100644 index 0000000000..aee6c06186 --- /dev/null +++ b/group11/501294009/JavaProject/module2/pom.xml @@ -0,0 +1,47 @@ + + 4.0.0 + + com.yang + module2 + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + jar + + module2 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + junit + junit + RELEASE + + + junit + junit + RELEASE + + + diff --git a/group11/501294009/JavaProject/module2/src/main/java/com/yang/LoginAction.java b/group11/501294009/JavaProject/module2/src/main/java/com/yang/LoginAction.java new file mode 100644 index 0000000000..377ae9c629 --- /dev/null +++ b/group11/501294009/JavaProject/module2/src/main/java/com/yang/LoginAction.java @@ -0,0 +1,39 @@ +package com.yang; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git a/group11/501294009/JavaProject/module2/src/main/java/com/yang/Struts.java b/group11/501294009/JavaProject/module2/src/main/java/com/yang/Struts.java new file mode 100644 index 0000000000..c28148d584 --- /dev/null +++ b/group11/501294009/JavaProject/module2/src/main/java/com/yang/Struts.java @@ -0,0 +1,124 @@ +package com.yang; + +import com.yang.bean.Action; +import com.yang.bean.Result; +import org.junit.Test; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/* + +0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 +鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 +("name"="test" , "password"="1234") , +閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + +2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + +3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, +閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , +鏀惧埌View瀵硅薄鐨刾arameters + +4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 +鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + +*/ +public class Struts { + + private static View view; + + public static View runAction(String actionName, Map parameters) { + view = null; + + try { + com.yang.bean.Struts struts = (com.yang.bean.Struts) getBean(com.yang.bean.Struts.class, "struts.xml"); + + List actions = struts.getActions(); + actions.forEach((Action action) -> { + try { + renderView(actionName, parameters, action); + } catch (Exception e) { + e.printStackTrace(); + } + + + }); + + } catch (Exception e) { + e.printStackTrace(); + } + + return view; + } + + private static void renderView(String actionName, Map parameters, Action action) throws Exception { + if (!action.getName().equalsIgnoreCase(actionName)) { + return;//same as continue + } + String packageName = action.getPackageName(); + Class aClass; + aClass = Class.forName(packageName); + Object instance = aClass.newInstance(); + + Method[] methods = aClass.getDeclaredMethods(); + Map methodMap = new HashMap(); + for (Method method : methods) { + method.setAccessible(true); + methodMap.put(method.getName(), method); + } + + invokeMethod("setName", parameters.get("name"), instance, methodMap); + invokeMethod("setPassword", parameters.get("password"), instance, methodMap); + String jsp = invokeMethod("execute", null, instance, methodMap); + + String message = invokeMethod("getMessage", null, instance, methodMap); + + Map params = new HashMap(); + params.put("message", message); + view = new View(); + view.setParameters(params); + List results = action.getResults(); + results.forEach(temp -> { + if (temp.getName().equalsIgnoreCase(jsp)) { + view.setJsp(temp.getValue()); + } + + }); + } + + + + private static Object getBean(Class clz, String fileName) throws JAXBException { + JAXBContext context = JAXBContext.newInstance(clz); + Unmarshaller unmarshaller = context.createUnmarshaller(); + InputStream inputStream = Struts.class.getClassLoader().getResourceAsStream(fileName); + return unmarshaller.unmarshal(inputStream); + } + + + private static String invokeMethod(String methodName, String param, Object instance, Map map) throws IllegalAccessException, InvocationTargetException { + Method setNameMethod = map.get(methodName); + String invoke = null; + if (setNameMethod != null) { + if (param == null) { + invoke = (String) setNameMethod.invoke(instance); + return invoke; + } + invoke = (String) setNameMethod.invoke(instance, param); + } + + + return invoke; + } + +} \ No newline at end of file diff --git a/group11/501294009/JavaProject/module2/src/main/java/com/yang/StrutsTest.java b/group11/501294009/JavaProject/module2/src/main/java/com/yang/StrutsTest.java new file mode 100644 index 0000000000..68f204e252 --- /dev/null +++ b/group11/501294009/JavaProject/module2/src/main/java/com/yang/StrutsTest.java @@ -0,0 +1,43 @@ +package com.yang; + +import junit.framework.Assert; +import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} \ No newline at end of file diff --git a/group11/501294009/JavaProject/module2/src/main/java/com/yang/View.java b/group11/501294009/JavaProject/module2/src/main/java/com/yang/View.java new file mode 100644 index 0000000000..e443b9c759 --- /dev/null +++ b/group11/501294009/JavaProject/module2/src/main/java/com/yang/View.java @@ -0,0 +1,27 @@ +package com.yang; + +import java.util.Map; + +/** + * Created by Dev_yang on 2017/3/5. + */ +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public void setJsp(String jsp) { + this.jsp = jsp; + } + + public Map getParameters() { + return parameters; + } + + public void setParameters(Map parameters) { + this.parameters = parameters; + } +} diff --git a/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Action.java b/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Action.java new file mode 100644 index 0000000000..f79864106a --- /dev/null +++ b/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Action.java @@ -0,0 +1,58 @@ +package com.yang.bean; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import java.io.Serializable; +import java.util.List; + +/** + * Created by Dev_yang on 2017/3/5. + */ +public class Action implements Serializable{ + + private List results; + + private String name; + + private String packageName; + + + @XmlElement(name = "result") + public List getResults() { + return results; + } + + public void setResults(List results) { + this.results = results; + } + + + @XmlAttribute(name="name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + @XmlAttribute(name = "class") + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + + @Override + public String toString() { + return "Action{" + + "results=" + results + + ", name='" + name + '\'' + + ", packageName='" + packageName + '\'' + + '}'; + } +} diff --git a/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Result.java b/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Result.java new file mode 100644 index 0000000000..9dd66cac04 --- /dev/null +++ b/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Result.java @@ -0,0 +1,40 @@ +package com.yang.bean; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlValue; +import java.io.Serializable; + +/** + * Created by Dev_yang on 2017/3/5. + */ +public class Result implements Serializable{ + + private String name; + private String value; + + @XmlAttribute(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return "Result{" + + "name='" + name + '\'' + + ", value='" + value + '\'' + + '}'; + } +} diff --git a/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Struts.java b/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Struts.java new file mode 100644 index 0000000000..ad16ccc057 --- /dev/null +++ b/group11/501294009/JavaProject/module2/src/main/java/com/yang/bean/Struts.java @@ -0,0 +1,34 @@ + +package com.yang.bean; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; +import java.util.List; + + + +@XmlRootElement(name = "struts") +public class Struts implements Serializable{ + + private List actions; + + + @XmlElement(name = "action") + public List getActions() { + return actions; + } + + + public void setActions(List actions) { + this.actions = actions; + } + + + @Override + public String toString() { + return "Struts{" + + "actions=" + actions + + '}'; + } +} diff --git a/group11/501294009/JavaProject/module2/src/main/resources/struts.xml b/group11/501294009/JavaProject/module2/src/main/resources/struts.xml new file mode 100644 index 0000000000..9c989a77e3 --- /dev/null +++ b/group11/501294009/JavaProject/module2/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From ec4aa08fe5b4a3878c722fb9193d1d42d15d44a3 Mon Sep 17 00:00:00 2001 From: Dev_yang <501294009@qq.com> Date: Mon, 6 Mar 2017 23:46:49 +0800 Subject: [PATCH 006/155] delete .idea folder --- .../JavaProject/.idea/uiDesigner.xml | 124 ------------------ 1 file changed, 124 deletions(-) delete mode 100644 group11/501294009/JavaProject/.idea/uiDesigner.xml diff --git a/group11/501294009/JavaProject/.idea/uiDesigner.xml b/group11/501294009/JavaProject/.idea/uiDesigner.xml deleted file mode 100644 index e96534fb27..0000000000 --- a/group11/501294009/JavaProject/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From c232163a721bd174a4213cbcc82feef4942092d2 Mon Sep 17 00:00:00 2001 From: xiongrui Date: Tue, 7 Mar 2017 13:28:47 +0800 Subject: [PATCH 007/155] Basic framework --- group22/627559964/.classpath | 6 + group22/627559964/.gitignore | 1 + group22/627559964/.project | 17 +++ .../src/com/coding/basic/ArrayList.java | 33 +++++ .../src/com/coding/basic/BinaryTreeNode.java | 37 ++++++ .../src/com/coding/basic/Iterator.java | 9 ++ .../src/com/coding/basic/LinkedList.java | 125 ++++++++++++++++++ .../627559964/src/com/coding/basic/List.java | 13 ++ .../627559964/src/com/coding/basic/Queue.java | 19 +++ .../627559964/src/com/coding/basic/Stack.java | 24 ++++ 10 files changed, 284 insertions(+) create mode 100644 group22/627559964/.classpath create mode 100644 group22/627559964/.gitignore create mode 100644 group22/627559964/.project create mode 100644 group22/627559964/src/com/coding/basic/ArrayList.java create mode 100644 group22/627559964/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group22/627559964/src/com/coding/basic/Iterator.java create mode 100644 group22/627559964/src/com/coding/basic/LinkedList.java create mode 100644 group22/627559964/src/com/coding/basic/List.java create mode 100644 group22/627559964/src/com/coding/basic/Queue.java create mode 100644 group22/627559964/src/com/coding/basic/Stack.java diff --git a/group22/627559964/.classpath b/group22/627559964/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group22/627559964/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group22/627559964/.gitignore b/group22/627559964/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group22/627559964/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group22/627559964/.project b/group22/627559964/.project new file mode 100644 index 0000000000..870f65f4a0 --- /dev/null +++ b/group22/627559964/.project @@ -0,0 +1,17 @@ + + + 627559964 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group22/627559964/src/com/coding/basic/ArrayList.java b/group22/627559964/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..0ba3cb15a8 --- /dev/null +++ b/group22/627559964/src/com/coding/basic/ArrayList.java @@ -0,0 +1,33 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + + } + + public void add(int index, Object o) { + + } + + public Object get(int index) { + return null; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return -1; + } + + public Iterator iterator() { + return null; + } + +} \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/BinaryTreeNode.java b/group22/627559964/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..981812cf59 --- /dev/null +++ b/group22/627559964/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + return null; + } + +} \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/Iterator.java b/group22/627559964/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..c4c1725d21 --- /dev/null +++ b/group22/627559964/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface Iterator { + + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/LinkedList.java b/group22/627559964/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..8aa70d53bd --- /dev/null +++ b/group22/627559964/src/com/coding/basic/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o) { + + } + + public void add(int index, Object o) { + + } + + public Object get(int index) { + return null; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return -1; + } + + public void addFirst(Object o) { + + } + + public void addLast(Object o) { + + } + + public Object removeFirst() { + return null; + } + + public Object removeLast() { + return null; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + } + + /** + * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + + } + + /** + * 删除一个单链表的前半部分 例如:list = 2->5->7->8 , 删除以后的值为 7->8 如果list = 2->5->7->8->10 + * ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 假定当前链表和list均包含已升序排列的整数 从当前链表中取出那些list所指定的元素 例如当前链表 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public static int[] getElements(LinkedList list) { + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 从当前链表中中删除在list中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } +} \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/List.java b/group22/627559964/src/com/coding/basic/List.java new file mode 100644 index 0000000000..8815065b99 --- /dev/null +++ b/group22/627559964/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +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(); +} \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/Queue.java b/group22/627559964/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a487336ffe --- /dev/null +++ b/group22/627559964/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o) { + } + + public Object deQueue() { + return null; + } + + public boolean isEmpty() { + return false; + } + + public int size() { + return -1; + } +} \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/Stack.java b/group22/627559964/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b68067ee75 --- /dev/null +++ b/group22/627559964/src/com/coding/basic/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + } + + public Object pop() { + return null; + } + + public Object peek() { + return null; + } + + public boolean isEmpty() { + return false; + } + + public int size() { + return -1; + } +} \ No newline at end of file From 618193789ccb9eaafbc97e59ca56aa8e65fc798d Mon Sep 17 00:00:00 2001 From: xiongrui Date: Wed, 8 Mar 2017 08:41:57 +0800 Subject: [PATCH 008/155] Complete Arraylist --- group22/627559964/.classpath | 1 + group22/627559964/.gitignore | 1 + .../src/com/coding/basic/ArrayList.java | 153 +++++++++++++++++- .../627559964/src/com/coding/basic/List.java | 3 + group22/627559964/src/demo/Demo.java | 24 +++ 5 files changed, 175 insertions(+), 7 deletions(-) create mode 100644 group22/627559964/src/demo/Demo.java diff --git a/group22/627559964/.classpath b/group22/627559964/.classpath index fb5011632c..83b76b7922 100644 --- a/group22/627559964/.classpath +++ b/group22/627559964/.classpath @@ -1,6 +1,7 @@ + diff --git a/group22/627559964/.gitignore b/group22/627559964/.gitignore index ae3c172604..3727029200 100644 --- a/group22/627559964/.gitignore +++ b/group22/627559964/.gitignore @@ -1 +1,2 @@ /bin/ +.metadata \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/ArrayList.java b/group22/627559964/src/com/coding/basic/ArrayList.java index 0ba3cb15a8..8a1eded430 100644 --- a/group22/627559964/src/com/coding/basic/ArrayList.java +++ b/group22/627559964/src/com/coding/basic/ArrayList.java @@ -1,33 +1,172 @@ package com.coding.basic; +import java.util.Arrays; + +/** + * 自定义ArrayList + * + * @author xiongrui233 + * + */ public class ArrayList implements List { + //list长度 private int size = 0; - private Object[] elementData = new Object[100]; + //list的元素集合 + private Object[] elementData = new Object[10]; - public void add(Object o) { + /** + * 合并数组 + * + * @param arrays1 + * @param arrays2 + * @return Object[] + */ + private Object[] concat(Object[] arrays1, Object[] arrays2) { + Object[] newArrays = new Object[arrays1.length + arrays2.length]; + System.arraycopy(arrays1, 0, newArrays, 0, arrays1.length); + System.arraycopy(arrays2, 0, newArrays, arrays1.length, arrays2.length); + return newArrays; + } + + /** + * 分割数组 + * + * @param arrays + * @param index + * @return Object[] + */ + private Object[] subArrays(Object[] arrays, int from, int index) { + Object[] tempArrays = new Object[index - from]; + for (int i = from, j = 0; i < index; i++, j++) { + tempArrays[j] = arrays[i]; + } + return tempArrays; + } + + /** + * 动态增长list长度 策略为:newSize = oldSize * 1.5 + * + * @param oldSize + */ + private void grow(int oldSize) { + elementData = Arrays.copyOf(elementData, oldSize + oldSize / 2); + } + /** + * 检查在插入新元素时,list长度是否足够 + * + * @param newSize + */ + private void checkSize(int newSize) { + int oldSize = elementData.length; + if (newSize > oldSize) { + grow(oldSize); + } } + /** + * 新增元素 + * + * @param Object + */ + public void add(Object o) { + checkSize(size + 1); + elementData[size++] = o; + } + + /** + * 新增元素 + * + * @param index, + * @param Object + */ public void add(int index, Object o) { + checkSize(size + 1); + Object[] arrays1 = subArrays(elementData, 0, index); + Object[] arrays2 = subArrays(elementData, index, elementData.length); + arrays1 = Arrays.copyOf(arrays1, arrays1.length + 1); + arrays1[index] = o; + size++; + elementData = concat(arrays1, arrays2); } + /** + * 获得编号为index的元素 + * + * @param int + * @return Object + */ public Object get(int index) { - return null; + return elementData[index]; } + /** + * 删除编号为index的元素 + * + * @param int + * @return Object + */ public Object remove(int index) { - return null; + Object[] arrays1 = subArrays(elementData, 0, index); + Object[] arrays2 = subArrays(elementData, index + 1, elementData.length); + Object obj = elementData[index]; + + size --; + elementData = concat(arrays1, arrays2); + return obj; } - + + /** + * 返回list长度 + * + * @return int + */ public int size() { - return -1; + return size; } + /** + * 重写迭代器 + * + * @return IteratorImpl + */ public Iterator iterator() { - return null; + + class IteratorImpl implements Iterator { + + private int point = 0; + + @Override + public boolean hasNext() { + if (elementData[point] != null) { + return true; + } + return false; + } + + @Override + public Object next() { + return elementData[point++]; + } + + } + return new IteratorImpl(); } + @Override + public String toString() { + StringBuffer list = new StringBuffer(); + list.append("["); + for (int i = 0; i < elementData.length; i++) { + list.append(elementData); + if (i != elementData.length - 1) { + list.append(","); + } + } + list.append("]"); + return list.toString(); + } } \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/List.java b/group22/627559964/src/com/coding/basic/List.java index 8815065b99..d04fcb7df6 100644 --- a/group22/627559964/src/com/coding/basic/List.java +++ b/group22/627559964/src/com/coding/basic/List.java @@ -1,6 +1,7 @@ package com.coding.basic; public interface List { + public void add(Object o); public void add(int index, Object o); @@ -10,4 +11,6 @@ public interface List { public Object remove(int index); public int size(); + + public Iterator iterator(); } \ No newline at end of file diff --git a/group22/627559964/src/demo/Demo.java b/group22/627559964/src/demo/Demo.java new file mode 100644 index 0000000000..de5ec1a19c --- /dev/null +++ b/group22/627559964/src/demo/Demo.java @@ -0,0 +1,24 @@ +package demo; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class Demo { + public static void main(String[] args) { + + List list = new ArrayList(); + for (int i = 0; i < 12; i++) { + list.add(new Integer(123)); + } + list.add(3, new Integer(233)); + list.add(3, new Double(233.33)); + list.remove(6); + Double kk = (Double) list.get(3); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + System.out.println("End"); + } +} From ce7562ce7fc216a5ea25518fc9fcd74004173ed7 Mon Sep 17 00:00:00 2001 From: cjl1407 <1158477486@qq.com> Date: Wed, 8 Mar 2017 09:58:12 +0800 Subject: [PATCH 009/155] this is text --- group22/1158477486/src/my.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 group22/1158477486/src/my.txt diff --git a/group22/1158477486/src/my.txt b/group22/1158477486/src/my.txt new file mode 100644 index 0000000000..605f61fe72 --- /dev/null +++ b/group22/1158477486/src/my.txt @@ -0,0 +1 @@ +这是我的一个Git \ No newline at end of file From 0cdb2b2e8f08cf7f0fa81772aafdc5575d4e9aee Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 8 Mar 2017 09:59:06 +0800 Subject: [PATCH 010/155] java se --- group22/1158477486/src/myFist.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 group22/1158477486/src/myFist.java diff --git a/group22/1158477486/src/myFist.java b/group22/1158477486/src/myFist.java new file mode 100644 index 0000000000..136079fbac --- /dev/null +++ b/group22/1158477486/src/myFist.java @@ -0,0 +1,6 @@ + +public class myFist { +public static void main(String[] args) { + System.out.println("hello my love"); +} +} From 222e80b065e3b2555ace12ef5806b67e9f860488 Mon Sep 17 00:00:00 2001 From: cjl1407 <1158477486@qq.com> Date: Wed, 8 Mar 2017 12:59:34 +0800 Subject: [PATCH 011/155] this a java demo --- group22/1158477486/src/Test.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 group22/1158477486/src/Test.java diff --git a/group22/1158477486/src/Test.java b/group22/1158477486/src/Test.java new file mode 100644 index 0000000000..f106f0d3df --- /dev/null +++ b/group22/1158477486/src/Test.java @@ -0,0 +1,9 @@ + +public class Test { + + public static void main(String[] args) { + // TODO Auto-generated method stub +System.out.println("测试中------------"); + } + +} From eee5017c54c8b4d7010110760321a2f0233c7920 Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Wed, 8 Mar 2017 17:13:38 +0800 Subject: [PATCH 012/155] 17457741 --- group22/17457741/src/ArrayList.java | 110 +++++++++++++++++ group22/17457741/src/BinaryTreeNode.java | 53 ++++++++ group22/17457741/src/LinkedList.java | 149 +++++++++++++++++++++++ group22/17457741/src/Queue.java | 29 +++++ group22/17457741/src/Stack.java | 33 +++++ 5 files changed, 374 insertions(+) create mode 100644 group22/17457741/src/ArrayList.java create mode 100644 group22/17457741/src/BinaryTreeNode.java create mode 100644 group22/17457741/src/LinkedList.java create mode 100644 group22/17457741/src/Queue.java create mode 100644 group22/17457741/src/Stack.java diff --git a/group22/17457741/src/ArrayList.java b/group22/17457741/src/ArrayList.java new file mode 100644 index 0000000000..345538755c --- /dev/null +++ b/group22/17457741/src/ArrayList.java @@ -0,0 +1,110 @@ +import java.util.Iterator锛 +import java.util.NoSuchElementException; + +public class ArrayList implements Iterable{ + private static final int CAPACITY = 5; + + private int size; + + private T [] items; + + public ArrayList(){doClear();} + + public void clear(){ + doClear(); + } + + private void doClear(){ + this.size=0;ensureCapacity(CAPACITY); + } + + public int size(){ + return size; + } + + public boolean isEmpty(){ + return size==0; + } + + public void trimToSize(){ + ensureCapacity(size()); + } + + public T get( int a){ + return items[a]; + } + + public T set(int a,T b){ + T old = items[a]; + items[a]=b; + return old; + } + + public void ensureCapacity(int newCapacity){ + if(newCapacitya;i--){ + items[i]=items[i-1]; + } + items[a]=b; + size++; + } + + public T remove(int a){ + T removedItem=items[a]; + for(int i=a;i iterator() { + // TODO Auto-generated method stub + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int current =0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return current implements Iterable{ + private Nodebegin; + private Nodeend; + private int size; + private int modCount=0; + + public LinkedList(){ + doClear(); + } + public void clear(){ + doClear(); + } + private void doClear(){ + begin=new Node(null,null,null); + end=new Node(null,begin,null); + begin.next=end; + } + + public int size(){ + return size; + } + public boolean isEmpty(){ + return size==0; + } + + public boolean add( T x){ + add(size(),x); + return true; + } + public void add(int a,T x){ + addBefore(getNode(a,0,size()),x); + } + + public T get(int a){ + return getNode(a).data; + } + public T set(int a,T newVal){ + Nodep=getNode(a); + T old=p.data; + p.data=newVal; + return old; + } + + public T remove(int a){ + return remove(getNode(a)); + } + + private void addBefore(Nodep,T x){ + Node newNode=new Node<>(x,p.prev,p); + newNode.prev.next=newNode; + p.prev=newNode; + size++; + modCount--; + } + + private T remove(Nodep){ + p.next.prev=p.prev; + p.prev.next=p.next; + size--; + modCount++; + + return p.data; + } + + private NodegetNode(int a){ + return getNode(a,0,size()-1); + } + + private NodegetNode(int a,int lower,int upper){ + Nodep; + + if(aupper) + throw new IndexOutOfBoundsException(); + if(aa;i--) + p=p.prev; + } + return p; + } + @Override + public Iterator iterator() { + // TODO Auto-generated method stub + return new LinkedListIterator(); + } + private class LinkedListIterator implements Iterator{ + private Node current=begin.next; + private int expectedModCount=modCount; + private boolean toRemove=false; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return current!=end; + } + @Override + public T next() { + // TODO Auto-generated method stub + if(modCount!=expectedModCount) + throw new java.util.ConcurrentModificationException(); + if(!toRemove) + throw new IllegalStateException(); + + T nextItem=current.data; + current=current.next; + toRemove=true; + return nextItem; + } + + public void remove(){ + if(modCount!=expectedModCount) + throw new java.util.ConcurrentModificationException(); + if(!toRemove) + throw new IllegalStateException(); + + LinkedList.this.remove(current.prev); + expectedModCount++; + toRemove=false; + } + } + + private static class Node{ + + public T data; + public Node prev; + public Node next; + + public Node(T d, Node p,Node n) { + // TODO Auto-generated constructor stub + data=d;prev=p;next=n; + } + + } + +} + diff --git a/group22/17457741/src/Queue.java b/group22/17457741/src/Queue.java new file mode 100644 index 0000000000..b98d8e2043 --- /dev/null +++ b/group22/17457741/src/Queue.java @@ -0,0 +1,29 @@ + + +public class Queue { + + private ArrayList list = new ArrayList(); + + public void enQueue(Object o) { + list.add(o); + } + + public Object deQueue() { + final int size = list.size(); + if (0 == size) + return null; + Object o = list.remove(size); + return o; + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public int size() { + return list.size(); + } + +} + + diff --git a/group22/17457741/src/Stack.java b/group22/17457741/src/Stack.java new file mode 100644 index 0000000000..12f870566f --- /dev/null +++ b/group22/17457741/src/Stack.java @@ -0,0 +1,33 @@ + + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + + elementData.add(o); + } + + public Object peek(){ + return elementData.get(0); + } + + public Object pop(){ + Object a = null; + if(elementData.size() > 0) { + + a = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + } + return a; + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } + +} From 1cd0c34be6d711ad95386830796611e94d2915ee Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Wed, 8 Mar 2017 17:14:54 +0800 Subject: [PATCH 013/155] 17457741 --- group22/17457741/src/ArrayList.java | 2 +- group22/17457741/src/LinkedList.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/group22/17457741/src/ArrayList.java b/group22/17457741/src/ArrayList.java index 345538755c..45b583d13c 100644 --- a/group22/17457741/src/ArrayList.java +++ b/group22/17457741/src/ArrayList.java @@ -1,4 +1,4 @@ -import java.util.Iterator锛 +import java.util.Iterator; import java.util.NoSuchElementException; public class ArrayList implements Iterable{ diff --git a/group22/17457741/src/LinkedList.java b/group22/17457741/src/LinkedList.java index d7c4fe3415..c098978572 100644 --- a/group22/17457741/src/LinkedList.java +++ b/group22/17457741/src/LinkedList.java @@ -1,7 +1,6 @@ import java.util.Iterator; -import LinkedList.LinkedListIterator; -import LinkedList.Node; + From 009358e1f49f5057ea678ebb9774c0bc00cfd873 Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 8 Mar 2017 17:15:07 +0800 Subject: [PATCH 014/155] =?UTF-8?q?list=20=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group22/1158477486/src/TestCollection/List.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 group22/1158477486/src/TestCollection/List.java diff --git a/group22/1158477486/src/TestCollection/List.java b/group22/1158477486/src/TestCollection/List.java new file mode 100644 index 0000000000..1b14c3ebe6 --- /dev/null +++ b/group22/1158477486/src/TestCollection/List.java @@ -0,0 +1,9 @@ +package TestCollection; + +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(); +} From ffc07a4ae94ca5232e51a91821bd76ce0a06fa58 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Thu, 9 Mar 2017 10:52:45 +0800 Subject: [PATCH 015/155] template --- group22/2622819383/Task1/ArrayList.java | 30 +++++ group22/2622819383/Task1/BinaryTreeNode.java | 30 +++++ group22/2622819383/Task1/Iterator.java | 5 + group22/2622819383/Task1/LinkedList.java | 120 +++++++++++++++++++ group22/2622819383/Task1/List.java | 7 ++ group22/2622819383/Task1/Queue.java | 17 +++ group22/2622819383/Task1/Stack.java | 20 ++++ 7 files changed, 229 insertions(+) create mode 100644 group22/2622819383/Task1/ArrayList.java create mode 100644 group22/2622819383/Task1/BinaryTreeNode.java create mode 100644 group22/2622819383/Task1/Iterator.java create mode 100644 group22/2622819383/Task1/LinkedList.java create mode 100644 group22/2622819383/Task1/List.java create mode 100644 group22/2622819383/Task1/Queue.java create mode 100644 group22/2622819383/Task1/Stack.java diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java new file mode 100644 index 0000000000..ca9faa3291 --- /dev/null +++ b/group22/2622819383/Task1/ArrayList.java @@ -0,0 +1,30 @@ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group22/2622819383/Task1/BinaryTreeNode.java b/group22/2622819383/Task1/BinaryTreeNode.java new file mode 100644 index 0000000000..1f07869939 --- /dev/null +++ b/group22/2622819383/Task1/BinaryTreeNode.java @@ -0,0 +1,30 @@ +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group22/2622819383/Task1/Iterator.java b/group22/2622819383/Task1/Iterator.java new file mode 100644 index 0000000000..96a43dbe0a --- /dev/null +++ b/group22/2622819383/Task1/Iterator.java @@ -0,0 +1,5 @@ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java new file mode 100644 index 0000000000..d431e25fdb --- /dev/null +++ b/group22/2622819383/Task1/LinkedList.java @@ -0,0 +1,120 @@ +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group22/2622819383/Task1/List.java b/group22/2622819383/Task1/List.java new file mode 100644 index 0000000000..4f7bcc71a8 --- /dev/null +++ b/group22/2622819383/Task1/List.java @@ -0,0 +1,7 @@ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group22/2622819383/Task1/Queue.java b/group22/2622819383/Task1/Queue.java new file mode 100644 index 0000000000..08f1e8ba29 --- /dev/null +++ b/group22/2622819383/Task1/Queue.java @@ -0,0 +1,17 @@ +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group22/2622819383/Task1/Stack.java b/group22/2622819383/Task1/Stack.java new file mode 100644 index 0000000000..6c8c49bb10 --- /dev/null +++ b/group22/2622819383/Task1/Stack.java @@ -0,0 +1,20 @@ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} From aa9a515649cce4260ed37acdb85cf1fe330104e7 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Thu, 9 Mar 2017 12:26:19 +0800 Subject: [PATCH 016/155] done with ArrayList.java --- group22/2622819383/Task1/ArrayList.java | 136 ++++++++++++++++++++++-- 1 file changed, 125 insertions(+), 11 deletions(-) diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java index ca9faa3291..1c060ffe3a 100644 --- a/group22/2622819383/Task1/ArrayList.java +++ b/group22/2622819383/Task1/ArrayList.java @@ -1,30 +1,144 @@ public class ArrayList implements List { - private int size = 0; + private int size; + + private int capacity; + + private static final int DEFAULT_CAPACITY = 10; - private Object[] elementData = new Object[100]; + private Object[] elementData; + + //add()时用于扩容 + private void expand() { + if (size < capacity) return;//尚未满员,不必扩容 + + if (capacity < DEFAULT_CAPACITY) capacity = DEFAULT_CAPACITY;//不低于最小容量 + + Object[] oldElem = elementData; + elementData = new Object[capacity <<= 1]; + for (int i = 0; i < size; i++) + elementData[i] = oldElem[i]; + } + + //remove()时用于缩容 + private void shrink() { + if (capacity < DEFAULT_CAPACITY << 1) return;//不致收缩至DEFAULT_CAPACITY以下 + + if (capacity >> 2 < size) return; //以25%为界 + + Object[] oldElem = elementData; elementData = new Object[capacity >>= 1]; + for (int i = 0; i < size; i++) + elementData[i] = oldElem[i]; + + } + + public ArrayList() { + clear(); + } + + public ArrayList(Object ...args) { + this(); + for (Object o : args) + add(o); + } + + public void clear() { + size = 0; + elementData = new Object[capacity = DEFAULT_CAPACITY]; + } + public int size() { return size; } + + public int capacity() { return capacity; }//用于测试shrink()&expand() + + public boolean isEmpty() { return size == 0; } + public void add(Object o){ - + add(size(), o); } public void add(int index, Object o){ - + if (index < 0 || size < index) throw new IndexOutOfBoundsException(); + expand(); + + for (int i = size; i > index; i--) + elementData[i] = elementData[i - 1]; + elementData[index] = o; + + size++; } + public void add(Object ...args) { + for (Object o : args) + add(o); + } public Object get(int index){ - return null; + if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); + return elementData[index]; } public Object remove(int index){ - return null; - } - - public int size(){ - return -1; + if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); + + Object removed = elementData[index]; + + for (int i = index; i < size - 1; i++) + elementData[i] = elementData[i + 1]; + size--; + shrink(); + return removed; } + + public void removeElems(int ...args) { + for (int i : args) + remove(i); + } public Iterator iterator(){ - return null; + return new ArrayListIterator(); } + private class ArrayListIterator implements Iterator { + private int current; + public boolean hasNext() { return current != size; } + public Object next() { + if (!hasNext()) throw new java.util.NoSuchElementException(); + + return elementData[current++]; + } + } + public static void showElements(ArrayList list) { + System.out.print("当前list中元素:"); + Iterator iter = list.iterator(); + while (iter.hasNext()) + System.out.print(iter.next() + " "); + System.out.println(); + } + + public static void test(ArrayList list) { + System.out.println("--------基本方法测试---------"); + System.out.println("当前list.isEmpty(): " + list.isEmpty()); + System.out.println("当前list.size(): " + list.size()); + System.out.println("当前list.capacity(): " + list.capacity()); + showElements(list); + + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(1, 2, 3, 4, 5); + test(list); + list.add(6, 7, 8, 9, 10); + test(list); + list.add(3, 11); + list.get(3); + test(list); + list.remove(3); + test(list); + list.add(11,12,13,14,15,16,17,18,19,20,21,22,23,24); + test(list); + + list.removeElems(1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1); + test(list); + + + } } From f1691151953a645e9e602ed2111434a611b981df Mon Sep 17 00:00:00 2001 From: laoheihei Date: Thu, 9 Mar 2017 12:32:42 +0800 Subject: [PATCH 017/155] add ignore file --- group22/2622819383/Task1/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 group22/2622819383/Task1/.gitignore diff --git a/group22/2622819383/Task1/.gitignore b/group22/2622819383/Task1/.gitignore new file mode 100644 index 0000000000..5241a7220a --- /dev/null +++ b/group22/2622819383/Task1/.gitignore @@ -0,0 +1 @@ +*.class \ No newline at end of file From e2d14dc6809168c62a49e5c14750feeeba76f6d7 Mon Sep 17 00:00:00 2001 From: xiongrui Date: Thu, 9 Mar 2017 17:16:21 +0800 Subject: [PATCH 018/155] simple complete Stack --- .../src/com/coding/basic/ArrayList.java | 24 ++++++++++--------- .../627559964/src/com/coding/basic/Stack.java | 15 ++++++++---- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/group22/627559964/src/com/coding/basic/ArrayList.java b/group22/627559964/src/com/coding/basic/ArrayList.java index 8a1eded430..e20bd4ca45 100644 --- a/group22/627559964/src/com/coding/basic/ArrayList.java +++ b/group22/627559964/src/com/coding/basic/ArrayList.java @@ -10,10 +10,10 @@ */ public class ArrayList implements List { - //list长度 + // list长度 private int size = 0; - //list的元素集合 + // list的元素集合 private Object[] elementData = new Object[10]; /** @@ -34,6 +34,7 @@ private Object[] concat(Object[] arrays1, Object[] arrays2) { * 分割数组 * * @param arrays + * @param from * @param index * @return Object[] */ @@ -46,7 +47,8 @@ private Object[] subArrays(Object[] arrays, int from, int index) { } /** - * 动态增长list长度 策略为:newSize = oldSize * 1.5 + * 动态增长list长度 + * 策略为:newSize = oldSize * 1.5 * * @param oldSize */ @@ -79,7 +81,7 @@ public void add(Object o) { /** * 新增元素 * - * @param index, + * @param index * @param Object */ public void add(int index, Object o) { @@ -113,12 +115,12 @@ public Object remove(int index) { Object[] arrays1 = subArrays(elementData, 0, index); Object[] arrays2 = subArrays(elementData, index + 1, elementData.length); Object obj = elementData[index]; - - size --; + + size--; elementData = concat(arrays1, arrays2); return obj; } - + /** * 返回list长度 * @@ -129,16 +131,16 @@ public int size() { } /** - * 重写迭代器 + * 重写迭代器 * * @return IteratorImpl */ public Iterator iterator() { - + class IteratorImpl implements Iterator { private int point = 0; - + @Override public boolean hasNext() { if (elementData[point] != null) { @@ -151,7 +153,7 @@ public boolean hasNext() { public Object next() { return elementData[point++]; } - + } return new IteratorImpl(); } diff --git a/group22/627559964/src/com/coding/basic/Stack.java b/group22/627559964/src/com/coding/basic/Stack.java index b68067ee75..0d93aabb47 100644 --- a/group22/627559964/src/com/coding/basic/Stack.java +++ b/group22/627559964/src/com/coding/basic/Stack.java @@ -1,24 +1,31 @@ package com.coding.basic; public class Stack { + private ArrayList elementData = new ArrayList(); public void push(Object o) { + elementData.add(o); } public Object pop() { - return null; + Object obj = elementData.get(0); + elementData.remove(0); + return obj; } public Object peek() { - return null; + return elementData.get(0); } public boolean isEmpty() { - return false; + if (elementData.size() != 0) { + return false; + } + return true; } public int size() { - return -1; + return elementData.size(); } } \ No newline at end of file From bee5ae231502b9acd178949b0d3c31e23f3ab6c1 Mon Sep 17 00:00:00 2001 From: Fred Qin Date: Thu, 9 Mar 2017 01:34:34 -0800 Subject: [PATCH 019/155] complete linkedlist exercise --- .../coderising/download/DownloadThread.java | 20 + .../coderising/download/FileDownloader.java | 73 ++++ .../download/FileDownloaderTest.java | 59 +++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 ++ .../download/impl/ConnectionManagerImpl.java | 15 + .../src/com/coding/basic/LinkedList.java | 343 ++++++++++++++++-- .../src/com/coding/basic/LinkedListTest.java | 184 +++++++++- 11 files changed, 728 insertions(+), 36 deletions(-) create mode 100644 group11/395443277/src/com/coderising/download/DownloadThread.java create mode 100644 group11/395443277/src/com/coderising/download/FileDownloader.java create mode 100644 group11/395443277/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group11/395443277/src/com/coderising/download/api/Connection.java create mode 100644 group11/395443277/src/com/coderising/download/api/ConnectionException.java create mode 100644 group11/395443277/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group11/395443277/src/com/coderising/download/api/DownloadListener.java create mode 100644 group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group11/395443277/src/com/coderising/download/impl/ConnectionManagerImpl.java diff --git a/group11/395443277/src/com/coderising/download/DownloadThread.java b/group11/395443277/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/group11/395443277/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/group11/395443277/src/com/coderising/download/FileDownloader.java b/group11/395443277/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/group11/395443277/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).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; + } + +} diff --git a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group11/395443277/src/com/coderising/download/api/Connection.java b/group11/395443277/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group11/395443277/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +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(); +} diff --git a/group11/395443277/src/com/coderising/download/api/ConnectionException.java b/group11/395443277/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group11/395443277/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group11/395443277/src/com/coderising/download/api/ConnectionManager.java b/group11/395443277/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group11/395443277/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group11/395443277/src/com/coderising/download/api/DownloadListener.java b/group11/395443277/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group11/395443277/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/group11/395443277/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group11/395443277/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/group11/395443277/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/group11/395443277/src/com/coding/basic/LinkedList.java b/group11/395443277/src/com/coding/basic/LinkedList.java index fb9fbd81ca..df5d62aed8 100644 --- a/group11/395443277/src/com/coding/basic/LinkedList.java +++ b/group11/395443277/src/com/coding/basic/LinkedList.java @@ -3,84 +3,99 @@ import java.util.NoSuchElementException; public class LinkedList implements List { + private Node head; - - public void add(Object o){ + + public void add(Object o) { Node newNode = new Node(); newNode.data = o; newNode.next = null; - + if (head == null) { head = newNode; } else { Node curr = head; - while(curr.next != null) { + while (curr.next != null) { curr = curr.next; } curr.next = newNode; } } - public void add(int index , Object o){ + + public void add(int index, Object o) { Node newNode = new Node(); newNode.data = o; newNode.next = null; - + + if (index == 0) { + this.addFirst(o); + return; + } + if (head == null) { head = newNode; } else { Node curr = head; Node prev = curr; - while(index >0 && curr.next != null) { + while (index > 0 && curr.next != null) { prev = curr; curr = curr.next; index--; } - + prev.next = newNode; newNode.next = curr; } } - public Object get(int index){ + + public Object get(int index) { + if (index >= this.size()) { + return null; + } + Node curr = head; - while(index > 0) { + while (index > 0) { curr = curr.next; index--; - } + } return curr.data; } - public Object remove(int index){ - if (index ==0) { + + public Object remove(int index) { + if (index == 0) { return this.removeFirst(); } - + Node curr = head; Node prev = curr; - while(index >0 && curr.next != null) { + while (index > 0 && curr.next != null) { prev = curr; curr = curr.next; index--; } - + Object target = curr.data; prev.next = curr.next; curr.next = null; - + return target; } - public int size(){ + + public int size() { int size = 0; Node curr = head; - while(curr != null) { + while (curr != null) { size++; curr = curr.next; } return size; } - public void addFirst(Object o){ + + public void addFirst(Object o) { Node newNode = new Node(); newNode.data = o; newNode.next = null; - + if (head == null) { head = newNode; } else { @@ -88,37 +103,40 @@ public void addFirst(Object o){ head.next = newNode; } } - public void addLast(Object o){ + + public void addLast(Object o) { Node newNode = new Node(); newNode.data = o; newNode.next = null; - + Node curr = head; if (head == null) { head = newNode; } else { - while(curr.next != null) { + while (curr.next != null) { curr = curr.next; } curr.next = newNode; } } - public Object removeFirst(){ + + public Object removeFirst() { if (head == null) { return null; } - + Object target = head.data; head = head.next; return target; } - public Object removeLast(){ + + public Object removeLast() { if (head == null) { return null; } Node curr = head; Node prev = curr; - while(curr.next != null) { + while (curr.next != null) { prev = curr; curr = curr.next; } @@ -126,13 +144,23 @@ public Object removeLast(){ prev.next = null; return target; } - public Iterator iterator(){ + + public void print() { + Node curr = head; + + while (curr != null) { + System.out.println(curr.data); + curr = curr.next; + } + } + + public Iterator iterator() { return new SeqIterator(); } - + private class SeqIterator implements Iterator { Node curr = head; - + @Override public boolean hasNext() { return curr != null; @@ -140,17 +168,262 @@ public boolean hasNext() { @Override public Object next() { - if (!hasNext()) throw new NoSuchElementException(); + if (!hasNext()) + throw new NoSuchElementException(); Object target = curr.data; curr = curr.next; return target; } } - - private static class Node{ + + private static class Node { Object data; Node next; - + } + + /** + * 鎶婅閾捐〃閫嗙疆 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse() { + if (head != null) { + Node curr = head; + Node nextNode = null; + Node prev = null; + + // move curr node link + while (curr != null) { + // move link + nextNode = curr.next; + curr.next = prev; + + // move forward + prev = curr; + curr = nextNode; + } + + head = prev; + } + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 濡傛灉list = 2->5->7->8->10 + * ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + * + */ + public void removeFirstHalf() { + if (head != null) { + int listSize = this.size(); + int half = (int) Math.ceil(listSize / 2); + + Node curr = head; + while (half > 0) { + curr = curr.next; + head = curr; + half--; + } + } + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (head == null) { + return; + } + + if (i < 0) { + return; + } + + if (i > this.size() - 1) { + return; + } + + Node curr = head; + Node prev = head; + // move to index i + while (i > 0 && curr != null) { + prev = curr; + curr = curr.next; + i--; + } + + // if curr is out of bound return + if (curr == null) { + return; + } + + // else move length + while (length > 0 && curr != null) { + curr = curr.next; + length--; + } + + // special case to head + if (prev == head) { + head = curr; + } else { + prev.next = curr; + } + } + + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 渚嬪褰撳墠閾捐〃 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + if (list.size() == 0) { + return null; + } + + if (head == null) { + return null; + } + + // TODO: remove list which not existed in the original list + // special case: 1->3->4->20 + + int[] newArr = new int[list.size()]; + int i = 0; + Iterator it = list.iterator(); + while (it.hasNext()) { + int id = (int) it.next(); + + // if element is not existed + if (this.get(id) != null) { + int listElement = (int) this.get(id); + newArr[i] = listElement; + i++; + } + } + + return newArr; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * + * @param list + */ + + public void subtract(LinkedList list) { + if (list.size() == 0) { + return; + } + + if (head == null) { + return; + } + + Iterator it = list.iterator(); + while (it.hasNext()) { + int id = (int) it.next(); + this.remove(id); + } + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues() { + if (head == null) { + return; + } + + Node curr = head; + + // note: + // duplicate value in the first or last + // all values the same + while (curr.next != null) { + if (curr.data.equals(curr.next.data)) { + curr.next = curr.next.next; + } else { + curr = curr.next; + } + } + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + if (head == null) { + return; + } + + if (min > max) { + return; + } + + // only works for integer + Node curr = head; + Node prev = head; + // include last node + while (curr != null) { + if ((int) curr.data > min && (int) curr.data < max) { + // special case for head + if (curr == head) { + head = curr.next; + } else { + prev.next = curr.next; + curr = curr.next; + } + } else { + prev = curr; + curr = curr.next; + } + } + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + if (list.size() == 0) { + return null; + } + + if (head == null) { + return null; + } + + // find size + int l1Size = this.size(); + int l2Size = list.size(); + + // runner + int i = 0; + int j = 0; + + LinkedList rtnList = new LinkedList(); + + while (i < l1Size && j < l2Size) { + if (this.get(i).equals(list.get(j))) { + rtnList.add(this.get(i)); + i++; + j++; + } else if ((int) this.get(i) < (int) list.get(j)) { + i++; + } else { + j++; + } + } + + return rtnList; } } diff --git a/group11/395443277/src/com/coding/basic/LinkedListTest.java b/group11/395443277/src/com/coding/basic/LinkedListTest.java index af5aa2d3a2..47418e188d 100644 --- a/group11/395443277/src/com/coding/basic/LinkedListTest.java +++ b/group11/395443277/src/com/coding/basic/LinkedListTest.java @@ -1,6 +1,7 @@ package com.coding.basic; -import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -124,4 +125,185 @@ public void testIterator() { assertEquals(3, it.next()); assertEquals(2, it.next()); } + + @Test + public void testReverse() { + LinkedList list = new LinkedList(); + list.add(3); + list.add(7); + list.add(10); + list.add(15); + + list.reverse(); + assertEquals(15, list.get(0)); + + LinkedList list2 = new LinkedList(); + list2.reverse(); + } + + @Test + public void testRemoveFirstHalf() { + LinkedList list = new LinkedList(); + list.add(2); + list.add(5); + list.add(7); + list.add(8); + list.add(10); + + list.removeFirstHalf(); + } + + + @Test + public void testRemoveDuplicateValues() { + LinkedList list = new LinkedList(); + list.add(1); + + // with single element + assertEquals(1, list.get(0)); + + // add another one with 1->1 + list.add(1); + list.removeDuplicateValues(); + assertEquals(1, list.get(0)); + assertEquals(1, list.size()); + + // test the duplicate value is the last one + for(int i=2; i<6; i++) { + list.add(i); + } + list.add(5); + list.removeDuplicateValues(); + assertEquals(5, list.get(4)); + assertEquals(5, list.size()); + + // regular + for(int i=0; i<6; i++) { + list.add(5); + } + list.add(6); + + list.removeDuplicateValues(); + assertEquals(6, list.get(5)); + assertEquals(6, list.size()); + } + + @Test + public void testRemoveRange() { + LinkedList list = new LinkedList(); + + // regular + for(int i=1; i<7; i++) { + list.add(i); + } + + list.removeRange(2, 4); + assertEquals(5, list.size()); + + // head case + list.add(1, 1); + list.removeRange(0, 3); + assertEquals(3, list.size()); + + // tail case + list.add(6); + list.add(7); + list.removeRange(5, 20); + assertEquals(2, list.size()); + } + + @Test + public void testRemoveLength() { + LinkedList list = new LinkedList(); + + // regular + for(int i=0; i<9; i++) { + list.add(i); + } + + // regular + list.remove(4, 2); + + // head + LinkedList list2 = new LinkedList(); + for(int i=0; i<9; i++) { + list2.add(i); + } + list2.remove(0, 3); + + // tail + LinkedList list3 = new LinkedList(); + for(int i=0; i<10; i++) { + list3.add(i); + } + list3.remove(9, 3); + assertEquals(9, list3.size()); + } + + @Test + public void testGetElements() { + LinkedList list1 = new LinkedList(); + // 11->101->201->301->401->501->601->701 + list1.add(11); + list1.add(101); + list1.add(201); + list1.add(301); + list1.add(401); + list1.add(501); + list1.add(601); + list1.add(701); + + LinkedList list2 = new LinkedList(); + // 1->3->4->6 + list2.add(1); + list2.add(3); + list2.add(4); + list2.add(6); + + int[] newArr = list1.getElements(list2); + assertArrayEquals(new int[]{101,301,401,601}, newArr); + } + + @Test + public void testSubtract() { + LinkedList list1 = new LinkedList(); + // 11->101->201->301->401->501->601->701 + list1.add(11); + list1.add(101); + list1.add(201); + list1.add(301); + list1.add(401); + list1.add(501); + list1.add(601); + list1.add(701); + + LinkedList list2 = new LinkedList(); + // 1->3->4->6 + list2.add(1); + list2.add(3); + list2.add(4); + list2.add(6); + + list1.subtract(list2); + // 11->201->501->701 + assertEquals(4, list1.size()); + } + + @Test + public void testIntersection() { + LinkedList list1 = new LinkedList(); + list1.add(1); + list1.add(2); + list1.add(4); + list1.add(5); + + LinkedList list2 = new LinkedList(); + list2.add(2); + list2.add(3); + list2.add(4); + list2.add(7); + + LinkedList l3 = list1.intersection(list2); + assertEquals(2, l3.size()); + } } From 032ee7251204241df8035e6a351b93b6924473d9 Mon Sep 17 00:00:00 2001 From: xiongrui Date: Fri, 10 Mar 2017 12:22:41 +0800 Subject: [PATCH 020/155] add Stack notes and complete TestArrayList --- group22/627559964/.classpath | 1 + .../src/com/coding/basic/ArrayList.java | 11 +--- .../627559964/src/com/coding/basic/Queue.java | 2 + .../627559964/src/com/coding/basic/Stack.java | 29 ++++++++- group22/627559964/src/demo/Demo.java | 3 + .../test/com/coding/basic/TestArrayList.java | 59 +++++++++++++++++++ 6 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 group22/627559964/test/com/coding/basic/TestArrayList.java diff --git a/group22/627559964/.classpath b/group22/627559964/.classpath index 83b76b7922..e72ef7c0d4 100644 --- a/group22/627559964/.classpath +++ b/group22/627559964/.classpath @@ -3,5 +3,6 @@ + diff --git a/group22/627559964/src/com/coding/basic/ArrayList.java b/group22/627559964/src/com/coding/basic/ArrayList.java index e20bd4ca45..59d055dfa7 100644 --- a/group22/627559964/src/com/coding/basic/ArrayList.java +++ b/group22/627559964/src/com/coding/basic/ArrayList.java @@ -160,15 +160,6 @@ public Object next() { @Override public String toString() { - StringBuffer list = new StringBuffer(); - list.append("["); - for (int i = 0; i < elementData.length; i++) { - list.append(elementData); - if (i != elementData.length - 1) { - list.append(","); - } - } - list.append("]"); - return list.toString(); + return Arrays.toString(Arrays.copyOf(elementData, size)); } } \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/Queue.java b/group22/627559964/src/com/coding/basic/Queue.java index a487336ffe..0cc28034fe 100644 --- a/group22/627559964/src/com/coding/basic/Queue.java +++ b/group22/627559964/src/com/coding/basic/Queue.java @@ -1,6 +1,8 @@ package com.coding.basic; public class Queue { + + private Object[] queue; public void enQueue(Object o) { } diff --git a/group22/627559964/src/com/coding/basic/Stack.java b/group22/627559964/src/com/coding/basic/Stack.java index 0d93aabb47..84e90bfb75 100644 --- a/group22/627559964/src/com/coding/basic/Stack.java +++ b/group22/627559964/src/com/coding/basic/Stack.java @@ -1,23 +1,46 @@ package com.coding.basic; +/** + * 自定义stack + * + * @author xiongrui233 + * + */ public class Stack { + //元素集合 private ArrayList elementData = new ArrayList(); + /** + * 向栈顶压入元素 + * @param o + */ public void push(Object o) { - elementData.add(o); + elementData.add(0,o); } + /** + * 获得栈顶元素,并移除栈里该元素 + * @return obj + */ public Object pop() { Object obj = elementData.get(0); elementData.remove(0); return obj; } + /** + * 获得栈顶元素,不移除栈里该元素 + * @return obj + */ public Object peek() { return elementData.get(0); } + /** + * 判断该栈是否为空 + * @return true/false + */ public boolean isEmpty() { if (elementData.size() != 0) { return false; @@ -25,6 +48,10 @@ public boolean isEmpty() { return true; } + /** + * 获得栈的大小 + * @return size + */ public int size() { return elementData.size(); } diff --git a/group22/627559964/src/demo/Demo.java b/group22/627559964/src/demo/Demo.java index de5ec1a19c..156d8ec61a 100644 --- a/group22/627559964/src/demo/Demo.java +++ b/group22/627559964/src/demo/Demo.java @@ -1,5 +1,7 @@ package demo; +import java.util.PriorityQueue; + import com.coding.basic.ArrayList; import com.coding.basic.Iterator; import com.coding.basic.List; @@ -14,6 +16,7 @@ public static void main(String[] args) { list.add(3, new Integer(233)); list.add(3, new Double(233.33)); list.remove(6); + System.out.println("List:" + list); Double kk = (Double) list.get(3); Iterator it = list.iterator(); while (it.hasNext()) { diff --git a/group22/627559964/test/com/coding/basic/TestArrayList.java b/group22/627559964/test/com/coding/basic/TestArrayList.java new file mode 100644 index 0000000000..68dabbb042 --- /dev/null +++ b/group22/627559964/test/com/coding/basic/TestArrayList.java @@ -0,0 +1,59 @@ +package com.coding.basic; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestArrayList { + + private List list = null; + + @Before + public void init() { + list = new ArrayList(); + + list.add(0); + list.add(1); + list.add(3); + } + + @Test + public void addTest () { + list.add(2,2); +// System.out.println(list.toString()); + Assert.assertEquals("[0, 1, 2, 3]", list.toString()); + } + + @Test + public void getTest () { + Assert.assertEquals(3, list.get(2)); + } + + @Test + public void removeTest () { + list.remove(0); +// System.out.println(list.toString()); + Assert.assertEquals("[1, 3]", list.toString()); + } + + @Test + public void sizeTest () { + Assert.assertEquals(3, list.size()); + } + + @Test + public void iteratorTest () { + Object[] obj = new Object[list.size()]; + Iterator it = list.iterator(); + int i = 0; + while (it.hasNext()) { + obj[i] = it.next(); + i ++; + } +// System.out.println(Arrays.toString(obj)); + Assert.assertEquals(Arrays.toString(obj), list.toString()); + } + +} From c05ed934d5749224aa15c0f060be4063f28e64da Mon Sep 17 00:00:00 2001 From: xiongrui Date: Fri, 10 Mar 2017 13:37:19 +0800 Subject: [PATCH 021/155] simple complete BinaryTreeNode --- .../src/com/coding/basic/BinaryTreeNode.java | 30 +++++++++++++++- group22/627559964/src/demo/Demo.java | 34 ++++++++++++------- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/group22/627559964/src/com/coding/basic/BinaryTreeNode.java b/group22/627559964/src/com/coding/basic/BinaryTreeNode.java index 981812cf59..9c416d3ad3 100644 --- a/group22/627559964/src/com/coding/basic/BinaryTreeNode.java +++ b/group22/627559964/src/com/coding/basic/BinaryTreeNode.java @@ -1,9 +1,18 @@ package com.coding.basic; +/** + * 自定义二叉树 + * + * @author xiongrui233 + * + */ public class BinaryTreeNode { + //节点值 private Object data; + //左子树 private BinaryTreeNode left; + //右子树 private BinaryTreeNode right; public Object getData() { @@ -30,8 +39,27 @@ public void setRight(BinaryTreeNode right) { this.right = right; } + /** + * 插入元素 + * @param o + * @return BinaryTreeNode + */ public BinaryTreeNode insert(Object o) { - return null; + BinaryTreeNode node = null; + if (this.data == null) { + this.data = o; + node = this; + } else { + if (this.left.data == null) { + this.left.data = o; + node = this.left; + } + if (this.right.data == null) { + this.right.data = o; + node = this.right; + } + } + return node; } } \ No newline at end of file diff --git a/group22/627559964/src/demo/Demo.java b/group22/627559964/src/demo/Demo.java index 156d8ec61a..10e4fbe402 100644 --- a/group22/627559964/src/demo/Demo.java +++ b/group22/627559964/src/demo/Demo.java @@ -4,24 +4,32 @@ import com.coding.basic.ArrayList; import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; import com.coding.basic.List; public class Demo { public static void main(String[] args) { - List list = new ArrayList(); - for (int i = 0; i < 12; i++) { - list.add(new Integer(123)); - } - list.add(3, new Integer(233)); - list.add(3, new Double(233.33)); - list.remove(6); - System.out.println("List:" + list); - Double kk = (Double) list.get(3); - Iterator it = list.iterator(); - while (it.hasNext()) { - System.out.println(it.next()); - } +// List list = new ArrayList(); +// for (int i = 0; i < 12; i++) { +// list.add(new Integer(123)); +// } +// list.add(3, new Integer(233)); +// list.add(3, new Double(233.33)); +// list.remove(6); +// System.out.println("List:" + list); +// Double kk = (Double) list.get(3); +// Iterator it = list.iterator(); +// while (it.hasNext()) { +// System.out.println(it.next()); +// } + + List list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); +// list.add(0, 0); + System.out.println("End"); } } From 0c73e93c22f217a253f42e509fe57d8bffad6b2a Mon Sep 17 00:00:00 2001 From: xiongrui Date: Fri, 10 Mar 2017 13:38:20 +0800 Subject: [PATCH 022/155] doing LinkedList --- .../src/com/coding/basic/LinkedList.java | 95 ++++++++++++++++--- 1 file changed, 82 insertions(+), 13 deletions(-) diff --git a/group22/627559964/src/com/coding/basic/LinkedList.java b/group22/627559964/src/com/coding/basic/LinkedList.java index 8aa70d53bd..0296f1019b 100644 --- a/group22/627559964/src/com/coding/basic/LinkedList.java +++ b/group22/627559964/src/com/coding/basic/LinkedList.java @@ -1,27 +1,102 @@ package com.coding.basic; +/** + * 自定义LinkList + * + * @author xiongrui233 + * + */ public class LinkedList implements List { + + /** + * 定义链表节点结构 + * @author xiongrui233 + * + */ + private static class Node { + Object data; + Node next; + } + //链表节点 + private Node head = new Node(); - private Node head; - + /** + * 添加元素 + * + * @param o + */ public void add(Object o) { - + Node node = head; + while (node.data != null) { + node = node.next; + } + node.data = o; } + /** + * 添加元素 + * + * @param index + * @param o + */ public void add(int index, Object o) { - + Node node = head; + Node oldNode = head; + Node newNode = new Node(); + for (int i = 0; i <= index; i++) { + if (i == index - 1) { + oldNode = node.next; + } + node = node.next; + } + newNode.data = o; + newNode.next = node; + oldNode.next = newNode; } + /** + * 获取元素 + * + * @param index + */ public Object get(int index) { - return null; + Node node = head; + for (int i = 0; i <= index; i++) { + node = node.next; + } + return node.data; } + /** + * 删除元素 + * + * @param index + */ public Object remove(int index) { - return null; + Node node = head; + Node oldNode = head; + Node newNode = new Node(); + for (int i = 0; i <= index; i++) { + if (i == index - 1) { + oldNode = node.next; + } + node = node.next; + } + if (node.next != null) { + newNode = node.next; + } else { + newNode = node; + } + oldNode.next = newNode; + return node.data; } public int size() { - return -1; + int size = 0; + while (head.next != null) { + size ++; + } + return size; } public void addFirst(Object o) { @@ -44,12 +119,6 @@ public Iterator iterator() { return null; } - private static class Node { - Object data; - Node next; - - } - /** * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ From d9c4b4e47e300a8e6beea48060f290156434c814 Mon Sep 17 00:00:00 2001 From: xiongrui Date: Fri, 10 Mar 2017 17:31:30 +0800 Subject: [PATCH 023/155] =?UTF-8?q?simple=20complete=20LinkedList=EF=BC=8C?= =?UTF-8?q?last=20TODO=20intersection()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coding/basic/LinkedList.java | 218 ++++++++++++++---- .../627559964/src/com/coding/basic/Queue.java | 11 +- group22/627559964/src/demo/Demo.java | 55 ++++- 3 files changed, 235 insertions(+), 49 deletions(-) diff --git a/group22/627559964/src/com/coding/basic/LinkedList.java b/group22/627559964/src/com/coding/basic/LinkedList.java index 0296f1019b..dcae1fb835 100644 --- a/group22/627559964/src/com/coding/basic/LinkedList.java +++ b/group22/627559964/src/com/coding/basic/LinkedList.java @@ -7,9 +7,10 @@ * */ public class LinkedList implements List { - + /** * 定义链表节点结构 + * * @author xiongrui233 * */ @@ -17,20 +18,23 @@ private static class Node { Object data; Node next; } - //链表节点 + + // 链表节点 private Node head = new Node(); + private int size = 0; + + public LinkedList() { + head.next = head; + } + /** * 添加元素 * * @param o */ public void add(Object o) { - Node node = head; - while (node.data != null) { - node = node.next; - } - node.data = o; + addLast(o); } /** @@ -40,18 +44,23 @@ public void add(Object o) { * @param o */ public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new ArrayIndexOutOfBoundsException(); + } Node node = head; - Node oldNode = head; - Node newNode = new Node(); - for (int i = 0; i <= index; i++) { - if (i == index - 1) { - oldNode = node.next; - } + Node temp = new Node(); + for (int i = 0; i < index; i++) { node = node.next; } - newNode.data = o; - newNode.next = node; - oldNode.next = newNode; + temp.data = o; + if (index == size -1) { + node.next = temp; + temp.next = head; + } else { + temp.next = node.next; + node.next = temp; + } + size ++; } /** @@ -73,57 +82,117 @@ public Object get(int index) { * @param index */ public Object remove(int index) { + if (index < 0 || index > size) { + throw new ArrayIndexOutOfBoundsException(); + } Node node = head; - Node oldNode = head; - Node newNode = new Node(); - for (int i = 0; i <= index; i++) { - if (i == index - 1) { - oldNode = node.next; - } + Node temp = new Node(); + for (int i = 0; i <= index - 1; i++) { node = node.next; } - if (node.next != null) { - newNode = node.next; + if (index == size -1) { + temp = node.next; + node.next = head; } else { - newNode = node; + temp = node.next; + node.next = node.next.next; } - oldNode.next = newNode; - return node.data; + size --; + return temp.data; } + /** + * 返回LinkedList的大小 + * + * @return size + */ public int size() { - int size = 0; - while (head.next != null) { - size ++; - } return size; } + /** + * 在LinkedList第一的位置添加元素 + * + * @param o + */ public void addFirst(Object o) { - + add(0, o); } + /** + * 在LinkedList最后添加元素 + * @param o + */ public void addLast(Object o) { - + Node node = head; + Node temp = new Node(); + for (int i = 0; i < size; i++) { + node = node.next; + } + temp.data = o; + node.next = temp; + size ++; } + /** + * 移除链表第一位元素 + * + * @return obj + */ public Object removeFirst() { - return null; + return remove(0); } + /** + * 移除链表最后一位元素 + * + * @return obj + */ public Object removeLast() { - return null; + return remove(size - 1); } + /** + * 实现Iterator接口 + * + * @return Iterator + */ public Iterator iterator() { - return null; + + class IteratorImpl implements Iterator { + + private Node node = head.next; + + private Object temp = null; + + @Override + public boolean hasNext() { + if (node != null && node.data != null) { + temp = node.data; + node = node.next; + return true; + } + return false; + } + + @Override + public Object next() { + return temp; + } + + } + return new IteratorImpl(); } /** * 把该链表逆置 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ - public void reverse() { - + public LinkedList reverse() { + LinkedList lis = new LinkedList(); + for (int i = this.size - 1; i >= 0; i--) { + lis.add(this.get(i)); + } + return lis; } /** @@ -131,7 +200,10 @@ public void reverse() { * ,删除以后的值为7,8,10 */ public void removeFirstHalf() { - + int mid = size/2; + for (int i = 0; i < mid; i++) { + remove(0); + } } /** @@ -141,7 +213,15 @@ public void removeFirstHalf() { * @param length */ public void remove(int i, int length) { - + if (i > length) { + throw new IllegalArgumentException(); + } + if (i < 0 || i > size) { + throw new ArrayIndexOutOfBoundsException(); + } + for (int j = i; j <= length; j++) { + remove(i); + } } /** @@ -151,8 +231,15 @@ public void remove(int i, int length) { * * @param list */ - public static int[] getElements(LinkedList list) { - return null; + public int[] getElements(LinkedList list) { + if (this.size < (Integer)list.get(list.size - 1)) { + throw new IllegalArgumentException(); + } + int[] elements = new int[list.size]; + for (int i = 0; i < elements.length; i++) { + elements[i] = (Integer) this.get((Integer)list.get(i)); + } + return elements; } /** @@ -162,14 +249,30 @@ public static int[] getElements(LinkedList list) { */ public void subtract(LinkedList list) { - + Iterator it = list.iterator(); + while (it.hasNext()) { + Object obj = it.next(); + for (int i = 0; i < this.size; i++) { + if (obj.equals(this.get(i))) { + this.remove(i); + } + } + } } /** * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues() { - + for (int i = 0; i < this.size; i++) { + if (i + 1 >= this.size) { + return; + } + if (this.get(i).equals(this.get(i+1))) { + remove(i+1); + i--; + } + } } /** @@ -179,16 +282,39 @@ public void removeDuplicateValues() { * @param max */ public void removeRange(int min, int max) { - + if (min >= max) { + throw new IllegalArgumentException(); + } + for (int i = 0; i < this.size; i++) { + if ((Integer)this.get(i) > max) { + remove(i, this.size-1); + } + } } /** * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * + * TODO * @param list */ public LinkedList intersection(LinkedList list) { return null; } + + @Override + public String toString() { + StringBuffer list = new StringBuffer(); + list.append("List:["); + Node node = head.next; + for (int i = 0; i < size; i++) { + list.append(node.data); + node = node.next; + if (i != size -1) { + list.append(", "); + } + } + list.append("]"); + return list.toString(); + } } \ No newline at end of file diff --git a/group22/627559964/src/com/coding/basic/Queue.java b/group22/627559964/src/com/coding/basic/Queue.java index 0cc28034fe..90f845e6cd 100644 --- a/group22/627559964/src/com/coding/basic/Queue.java +++ b/group22/627559964/src/com/coding/basic/Queue.java @@ -1,10 +1,19 @@ package com.coding.basic; +/** + * 自定义队列 + * + * @author xiongrui233 + * + */ public class Queue { + + private Object[] queue = new Object[10]; - private Object[] queue; + private int size = 0; public void enQueue(Object o) { + } public Object deQueue() { diff --git a/group22/627559964/src/demo/Demo.java b/group22/627559964/src/demo/Demo.java index 10e4fbe402..4fbb5c6b40 100644 --- a/group22/627559964/src/demo/Demo.java +++ b/group22/627559964/src/demo/Demo.java @@ -1,5 +1,6 @@ package demo; +import java.util.Arrays; import java.util.PriorityQueue; import com.coding.basic.ArrayList; @@ -24,12 +25,62 @@ public static void main(String[] args) { // System.out.println(it.next()); // } - List list = new LinkedList(); + LinkedList list = new LinkedList(); list.add(1); list.add(2); list.add(3); -// list.add(0, 0); + list.add(5); + list.add(0, 0); + list.add(2, 4); + + System.out.println(list); + + System.out.println(list.size()); + + System.out.println(list.get(4)); + + System.out.println(list.remove(4)); + + System.out.println(list); + + System.out.println(list.reverse()); + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.print(it.next() + " "); + } + System.out.println(); + + list.removeFirstHalf(); + System.out.println(list); + + list.remove(0, 1); + System.out.println(list); + + LinkedList ls = new LinkedList(); + list.add(6); + list.add(7); + list.add(8); + ls.add(1); + ls.add(2); + System.out.println(list); + System.out.println(ls); + System.out.println(Arrays.toString(list.getElements(ls))); + + ls.add(6); + list.subtract(ls); + System.out.println(list); + + list.add(1, 6); +// list.add(1, 6); + System.out.println(list); +// list.removeDuplicateValues(); +// System.out.println(list); System.out.println("End"); + + list.removeRange(4, 7); + System.out.println(list); + } } From 170ad340bc4866dda0f7649bda50764345c1861f Mon Sep 17 00:00:00 2001 From: cjl1407 <115847486@qq.com> Date: Fri, 10 Mar 2017 18:37:33 +0800 Subject: [PATCH 024/155] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/TestCollection/ArrayList.java | 42 ++++ .../src/TestCollection/LinkedList.java | 184 ++++++++++++++++++ .../1158477486/src/TestCollection/Queue.java | 38 ++++ .../1158477486/src/TestCollection/Stack.java | 36 ++++ 4 files changed, 300 insertions(+) create mode 100644 group22/1158477486/src/TestCollection/ArrayList.java create mode 100644 group22/1158477486/src/TestCollection/LinkedList.java create mode 100644 group22/1158477486/src/TestCollection/Queue.java create mode 100644 group22/1158477486/src/TestCollection/Stack.java diff --git a/group22/1158477486/src/TestCollection/ArrayList.java b/group22/1158477486/src/TestCollection/ArrayList.java new file mode 100644 index 0000000000..b097267d50 --- /dev/null +++ b/group22/1158477486/src/TestCollection/ArrayList.java @@ -0,0 +1,42 @@ +package TestCollection; + +public class ArrayList implements List { + + + + private Object[] elementData = new Object[100]; + private int size ; + public void add(Object o){ + if(size==elementData.length){ + Object[]newData=new Object[size*2+1];//将数组扩容 + System.arraycopy(elementData, 0, newData, 0, elementData.length); + elementData=newData; + } + elementData[size++]=o; + + } + public void add(int index, Object o){ + System.arraycopy(elementData, index, elementData,index+1 , size-index); + elementData[index]=o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData[--size]=null; + return null; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group22/1158477486/src/TestCollection/LinkedList.java b/group22/1158477486/src/TestCollection/LinkedList.java new file mode 100644 index 0000000000..6e1feda532 --- /dev/null +++ b/group22/1158477486/src/TestCollection/LinkedList.java @@ -0,0 +1,184 @@ +package TestCollection; + + + +public class LinkedList implements List { + + private Node head=new Node();; + private int size; + public Node node(int a){//鍒涘缓寰楀埌a浣嶇疆鑺傜偣鐨勬柟娉 + Node teamp=null; + + if(head!=null){ + if(a<=size ){ + teamp=head; + for(int i=0;i7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} + + \ No newline at end of file diff --git a/group22/1158477486/src/TestCollection/Queue.java b/group22/1158477486/src/TestCollection/Queue.java new file mode 100644 index 0000000000..4699235e98 --- /dev/null +++ b/group22/1158477486/src/TestCollection/Queue.java @@ -0,0 +1,38 @@ +package TestCollection; + + ; + +public class Queue { + private LinkedList list=new LinkedList(); + + public void enQueue(Object o){ + list.addLast ( o) ;//问题用add(index, o)出错 + } + + public Object deQueue(){ + Object o=list.get(1); + list.remove(1); + return o; + } + + public boolean isEmpty(){ + if(list.size()!=0){ + return false;}else + return true; + } + + public int size(){ + return list.size(); + } + public static void main(String[] args) { + Queue q=new Queue(); + q.enQueue("1"); + q.enQueue("2"); + q.enQueue("3"); + System.out.println(q.size()); + Object o=q.deQueue(); + System.out.println(o); + System.out.println(q.size()); + + } +} diff --git a/group22/1158477486/src/TestCollection/Stack.java b/group22/1158477486/src/TestCollection/Stack.java new file mode 100644 index 0000000000..35fdc6f31b --- /dev/null +++ b/group22/1158477486/src/TestCollection/Stack.java @@ -0,0 +1,36 @@ +package TestCollection; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.get(elementData.size()-1) ; + } + + public Object peek(){ + elementData.remove(elementData.size()-1); + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + if(elementData.size()!=0){ + return false;} + return true; + } + public int size(){ + return elementData.size(); + } + public static void main(String[] args) { + Stack s=new Stack(); + s.push("111"); + s.push("211"); + s.push("311"); + System.out.println(s.size());//3 + System.out.println(s.pop()); + System.out.println(s.size()); + System.out.println(s.peek()); + System.out.println(s.size()); + }} From f0284b7cde7b9eea489136e82776cf7f3776f5ca Mon Sep 17 00:00:00 2001 From: cjl1407 <2832829901@qq.com> Date: Fri, 10 Mar 2017 18:42:13 +0800 Subject: [PATCH 025/155] Delete Test.java --- group22/1158477486/src/Test.java | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 group22/1158477486/src/Test.java diff --git a/group22/1158477486/src/Test.java b/group22/1158477486/src/Test.java deleted file mode 100644 index f106f0d3df..0000000000 --- a/group22/1158477486/src/Test.java +++ /dev/null @@ -1,9 +0,0 @@ - -public class Test { - - public static void main(String[] args) { - // TODO Auto-generated method stub -System.out.println("测试中------------"); - } - -} From a2a3d252f1dbb1b17420f50a20d181162129a364 Mon Sep 17 00:00:00 2001 From: cjl1407 <2832829901@qq.com> Date: Fri, 10 Mar 2017 18:42:33 +0800 Subject: [PATCH 026/155] Delete my.txt --- group22/1158477486/src/my.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 group22/1158477486/src/my.txt diff --git a/group22/1158477486/src/my.txt b/group22/1158477486/src/my.txt deleted file mode 100644 index 605f61fe72..0000000000 --- a/group22/1158477486/src/my.txt +++ /dev/null @@ -1 +0,0 @@ -这是我的一个Git \ No newline at end of file From 1203ca2ef020d297e5cf221c911fb8dcb7ce5f4f Mon Sep 17 00:00:00 2001 From: cjl1407 <2832829901@qq.com> Date: Fri, 10 Mar 2017 18:42:49 +0800 Subject: [PATCH 027/155] Delete myFist.java --- group22/1158477486/src/myFist.java | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 group22/1158477486/src/myFist.java diff --git a/group22/1158477486/src/myFist.java b/group22/1158477486/src/myFist.java deleted file mode 100644 index 136079fbac..0000000000 --- a/group22/1158477486/src/myFist.java +++ /dev/null @@ -1,6 +0,0 @@ - -public class myFist { -public static void main(String[] args) { - System.out.println("hello my love"); -} -} From ac67112337be177b648af74242d19aad27e2c566 Mon Sep 17 00:00:00 2001 From: PengyuanWei Date: Sat, 11 Mar 2017 15:57:53 +0800 Subject: [PATCH 028/155] =?UTF-8?q?week01=20-=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../910725683/RemoteSystemsTempFiles/.project | 12 + group22/910725683/week01/.classpath | 8 + group22/910725683/week01/.gitignore | 4 + group22/910725683/week01/.project | 17 + .../src/com/coding/basic/ArrayList.java | 98 ++++++ .../src/com/coding/basic/BinaryTreeNode.java | 110 ++++++ .../week01/src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 327 ++++++++++++++++++ .../week01/src/com/coding/basic/List.java | 10 + .../week01/src/com/coding/basic/Queue.java | 26 ++ .../week01/src/com/coding/basic/Stack.java | 27 ++ .../test/com/coding/basic/ArrayListTest.java | 53 +++ .../com/coding/basic/BinaryTreeNodeTest.java | 47 +++ .../test/com/coding/basic/LinkedListTest.java | 137 ++++++++ .../test/com/coding/basic/QueueTest.java | 40 +++ .../test/com/coding/basic/StackTest.java | 48 +++ 16 files changed, 971 insertions(+) create mode 100644 group22/910725683/RemoteSystemsTempFiles/.project create mode 100644 group22/910725683/week01/.classpath create mode 100644 group22/910725683/week01/.gitignore create mode 100644 group22/910725683/week01/.project create mode 100644 group22/910725683/week01/src/com/coding/basic/ArrayList.java create mode 100644 group22/910725683/week01/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group22/910725683/week01/src/com/coding/basic/Iterator.java create mode 100644 group22/910725683/week01/src/com/coding/basic/LinkedList.java create mode 100644 group22/910725683/week01/src/com/coding/basic/List.java create mode 100644 group22/910725683/week01/src/com/coding/basic/Queue.java create mode 100644 group22/910725683/week01/src/com/coding/basic/Stack.java create mode 100644 group22/910725683/week01/test/com/coding/basic/ArrayListTest.java create mode 100644 group22/910725683/week01/test/com/coding/basic/BinaryTreeNodeTest.java create mode 100644 group22/910725683/week01/test/com/coding/basic/LinkedListTest.java create mode 100644 group22/910725683/week01/test/com/coding/basic/QueueTest.java create mode 100644 group22/910725683/week01/test/com/coding/basic/StackTest.java diff --git a/group22/910725683/RemoteSystemsTempFiles/.project b/group22/910725683/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group22/910725683/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group22/910725683/week01/.classpath b/group22/910725683/week01/.classpath new file mode 100644 index 0000000000..1b83178c00 --- /dev/null +++ b/group22/910725683/week01/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group22/910725683/week01/.gitignore b/group22/910725683/week01/.gitignore new file mode 100644 index 0000000000..6a59c3a991 --- /dev/null +++ b/group22/910725683/week01/.gitignore @@ -0,0 +1,4 @@ +/bin/ +/.settings/ +.classpatch +.prject \ No newline at end of file diff --git a/group22/910725683/week01/.project b/group22/910725683/week01/.project new file mode 100644 index 0000000000..3b3c4fa7ee --- /dev/null +++ b/group22/910725683/week01/.project @@ -0,0 +1,17 @@ + + + week01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group22/910725683/week01/src/com/coding/basic/ArrayList.java b/group22/910725683/week01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..acc0e9f8f4 --- /dev/null +++ b/group22/910725683/week01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,98 @@ +package com.coding.basic; +import java.util.Arrays; + +public class ArrayList implements List { + + //数组初始容量// + private final int DEFAULT_CAPICITY=7; + + //数组元素个数// + private int size = 0; + + private Object[] elementData = new Object[DEFAULT_CAPICITY]; + + public void add(Object o){ + ensureCapcity(size+1); + elementData[size++]=o; + } + public void add(int index, Object o){ + //index要连续的增加// + checkIndex(index); + ensureCapcity(size+1); + /* index及后面的元素左移一位,即从index开始移动,注意index从0开始, + * 即还要+1,则长度为size-((index)+1)+1 + */ + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index]=o; + size++; + } + + public Object get(int index){ + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + checkIndex(index); + Object temp=elementData[index]; + /* index后面的元素左移一位,即从index+1开始移动,注意index从0开始, + * 即还要+1,则长度为size-((index+1)+1)+1 + */ + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + size--; + return temp; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator(); + } + + private class Iterator{ + private int index=0; + public boolean hasNext(){ + return index=size){ + throw new IndexOutOfBoundsException("get " + index+" in "+size); + } + } + + //判断是否需要扩容并完成扩容// + private void ensureCapcity(int size){ + int oldLength=elementData.length; + if (size>=oldLength){ + //util.ArrayList中的公式,源代码使用的右移1,即除2// + int newLength=oldLength/2+oldLength; + if (newLength7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + /*两个指针,开始的时候指向头跟头后面的一个(前指针、后指针) + *循环:每次向后移动步长1,至后指针移到链表尾,size-1个节点需要移动(size-1)-1次 + *先保留前指针的值temp,即当前逆序链表的头,然后再移动前、后指针 + *移动后,将前指针的节点连接到逆序链表的头,开始下一次移动 + *循环结束后,注意到实际重连的只有旧链表的第二个节点到倒数第个节点,需要单独处理旧链表的头尾节点 + *旧链表的尾节点需要链接到逆序链表的头,旧链表的头节点的指针置空,不然会1<->2 + *维护头尾标记 + */ + Node current=head; + Node currentAfter=current.next; + Node temp; + for (int i=0;i5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + //int截断,不会有小数// + int removeLength = size / 2; + for (int i=1;i<=removeLength;i++){ + removeFirst(); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + checkIndex(i); + length=length+i-1; + if (i+length-1>size){ + length=size-i; + } + //从后往前删除,防止越界// + for (int k=length;k>=i;k--){ + remove(k); + } + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int indexLength=list.size(); + int[] result=new int[indexLength]; + for (int i=0;isize){ + result[i]=0; + }else{ + result[i]=(int)list.get(index); + } + } + return result; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + * @param list + */ + //注意到递增,在外层循环list的时候,保留内层循环的被比较链表的节点的下标并递增即可// + public void subtract(LinkedList list){ + int startIndex=0; + Iterator iter=list.iterator(); + while(iter.hasNext()){ + int src =(int) iter.next(); + while(startIndexmax){ + if (isHeadNoed){ + current=current.next; + removeFirst(); + }else{ + temp.next=current.next; + current=current.next; + size--; + } + }else{ + temp=current; + current=current.next; + isHeadNoed=false; + } + + } + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + //注意到递增,保留内循环下标位置递增即可// + public LinkedList intersection( LinkedList list){ + LinkedList result = new LinkedList(); + int startIndex = 0; + for (Iterator iter = list.iterator();iter.hasNext();){ + int src = (int) iter.next(); + while (startIndex=size){ + throw new IndexOutOfBoundsException("get " + index+" in "+size); + } + } + + public String toString(){ + StringBuilder sb = new StringBuilder(); + Node current = head; + for (int i=0;i"); + } + current=current.next; + } + return sb.toString(); + } +} \ No newline at end of file diff --git a/group22/910725683/week01/src/com/coding/basic/List.java b/group22/910725683/week01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..29caa79f69 --- /dev/null +++ b/group22/910725683/week01/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +package com.coding.basic; + + +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(); +} \ No newline at end of file diff --git a/group22/910725683/week01/src/com/coding/basic/Queue.java b/group22/910725683/week01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..6e1b288ebc --- /dev/null +++ b/group22/910725683/week01/src/com/coding/basic/Queue.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList queue = new LinkedList(); + + public void enQueue(Object o){ + queue.add(o); + } + + public Object deQueue(){ + return queue.removeFirst(); + } + + public boolean isEmpty(){ + return queue.size()==0; + } + + public int size(){ + return queue.size(); + } + + public String toString() { + return queue.toString(); + } +} \ No newline at end of file diff --git a/group22/910725683/week01/src/com/coding/basic/Stack.java b/group22/910725683/week01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a8d071b707 --- /dev/null +++ b/group22/910725683/week01/src/com/coding/basic/Stack.java @@ -0,0 +1,27 @@ +package com.coding.basic; + + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return (elementData.size())==0; + } + public int size(){ + return elementData.size(); + } + public String toString() { + return elementData.toString(); + } +} \ No newline at end of file diff --git a/group22/910725683/week01/test/com/coding/basic/ArrayListTest.java b/group22/910725683/week01/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..e0940e8acb --- /dev/null +++ b/group22/910725683/week01/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,53 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayListTest { + + ArrayList al = new ArrayList(); + + @Test + public void testAddObject() { + for (int i=1;i<=5;i++){ + al.add(i); + } + } + + @Test + public void testAddIntObject() { + testAddObject(); + al.add(3, 12); + System.out.println("inser index 3 value 12 : "+al.toString()); + } + + @Test + public void testGet() { + testAddObject(); + System.out.println("get index 4 : "+al.get(4)); + } + + @Test + public void testRemove() { + testAddObject(); + System.out.println("remove index 3 : "+al.remove(3)); + } + + @Test + public void testSize() { + testAddObject(); + System.out.println("get size : "+al.size()); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + + @Test + public void testToString() { + fail("Not yet implemented"); + } + +} diff --git a/group22/910725683/week01/test/com/coding/basic/BinaryTreeNodeTest.java b/group22/910725683/week01/test/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..b32b779091 --- /dev/null +++ b/group22/910725683/week01/test/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,47 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + BinaryTreeNode btn = new BinaryTreeNode(); + + @Test + public void testPreOrder() { + testInsert(); + System.out.print("preOrde : "); + btn.preOrder(btn); + System.out.println(""); + } + + @Test + public void testInOrder() { + testInsert(); + System.out.print("inOrder : "); + btn.inOrder(btn); + System.out.println(""); + } + + @Test + public void testPostOrder() { + testInsert(); + System.out.print("postOrder : "); + btn.postOrder(btn); + System.out.println(""); + } + + @Test + public void testInsert() { + btn.insert(45); + btn.insert(24); + btn.insert(53); + btn.insert(12); + btn.insert(37); + btn.insert(93); + } + +} diff --git a/group22/910725683/week01/test/com/coding/basic/LinkedListTest.java b/group22/910725683/week01/test/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..2d22e2e92d --- /dev/null +++ b/group22/910725683/week01/test/com/coding/basic/LinkedListTest.java @@ -0,0 +1,137 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.LinkedList; + +public class LinkedListTest { + LinkedList ll =new LinkedList(); + @Test + public void testAddObject() { + for (int i=0;i<9;i++){ + ll.add(i); + } + } + + @Test + public void testAddIntObject() { + testAddObject(); + ll.add(4, 22); + ll.add(0, 23); + System.out.println("add int : " + ll.toString()); + } + + @Test + public void testGet() { + testAddObject(); + System.out.println("get index 3 : "+ll.get(3)); + } + + @Test + public void testRemoveInt() { + testAddObject(); + System.out.println("remove index 5 : "+ll.get(5)); + } + + @Test + public void testSize() { + testAddObject(); + System.out.println("get size : "+ll.size()); + } + + @Test + public void testAddFirst() { + testAddObject(); + ll.addFirst(12); + System.out.println("add first : "+ll.toString()); + } + + @Test + public void testAddLast() { + testAddObject(); + ll.addLast(23); + System.out.println("add first : "+ll.toString()); + } + + @Test + public void testRemoveFirst() { + testAddObject(); + ll.removeFirst(); + System.out.println("remove first : "+ll.toString()); + } + + @Test + public void testRemoveLast() { + testAddObject(); + ll.removeLast(); + System.out.println("remove last : "+ll.toString()); + } + + @Test + public void testReverse() { + testAddObject(); + ll.reverse(); + System.out.println("reverse : "+ll.toString()); + } + + @Test + public void testRemoveFirstHalf() { + testAddObject(); + ll.removeFirstHalf(); + System.out.println("remove first half : "+ll.toString()); + } + + @Test + public void testRemoveIntInt() { + testAddObject(); + ll.remove(2, 4); + System.out.println("remove index 2 length 4 : "+ll.toString()); + } + + @Test + public void testGetElements() { + testAddObject(); + System.out.println("get index 2 : "+ll.get(2)); + } + + @Test + public void testSubtract() { + testAddObject(); + LinkedList test1 =new LinkedList(); + for (int i=2;i<5;i++){ + test1.add(i); + } + ll.subtract(test1); + System.out.println("subtract "+test1.toString()+" : "+ll.toString()); + } + + @Test + public void testRemoveDuplicateValues() { + testAddObject(); + for (int i=6;i>2;i--){ + ll.add(i,i); + } + ll.removeDuplicateValues(); + System.out.println("remove dupl : "+ll.toString()); + } + + @Test + public void testRemoveRange() { + testAddObject(); + ll.removeRange(3, 6); + System.out.println("remove range[3,6] : "+ll.toString()); + } + + @Test + public void testIntersection() { + testAddObject(); + LinkedList test2 =new LinkedList(); + for (int i=4;i<14;i=i+2){ + test2.add(i); + } + System.out.println("intersection "+test2.toString()+" : "+ll.intersection(test2)); + } + +} diff --git a/group22/910725683/week01/test/com/coding/basic/QueueTest.java b/group22/910725683/week01/test/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..18cba75d07 --- /dev/null +++ b/group22/910725683/week01/test/com/coding/basic/QueueTest.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Queue; + +public class QueueTest { + Queue queue = new Queue(); + + @Test + public void testEnQueue() { + for (int i=0;i<10;i++){ + queue.enQueue(i); + } + } + + @Test + public void testDeQueue() { + testEnQueue(); + for (int i=0;i<3;i++){ + System.out.println("deQueue : " + queue.deQueue()); + } + } + + @Test + public void testIsEmpty() { + System.out.println("is empty(true) : "+queue.isEmpty()); + testEnQueue(); + System.out.println("is empty(false) : "+queue.isEmpty()); + } + + @Test + public void testSize() { + testEnQueue(); + System.out.println("size : "+queue.size()); + } + +} diff --git a/group22/910725683/week01/test/com/coding/basic/StackTest.java b/group22/910725683/week01/test/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..0c8095d79c --- /dev/null +++ b/group22/910725683/week01/test/com/coding/basic/StackTest.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Stack; + +public class StackTest { + Stack stack = new Stack(); + + @Test + public void testPush() { + for (int i = 0; i < 10; i++) { + stack.push(i); + } + } + + @Test + public void testPop() { + testPush(); + for (int i = 0; i < 3; i++) { + System.out.println("pop : "+stack.pop()); + } + } + + @Test + public void testPeek() { + testPush(); + for (int i = 0; i < 3; i++) { + System.out.println("peek : "+stack.peek()); + } + } + + @Test + public void testIsEmpty() { + System.out.println("is empty(true) : "+stack.isEmpty()); + testPush(); + System.out.println("is empty(false) : "+stack.isEmpty()); + } + + @Test + public void testSize() { + testPush(); + System.out.println("size : "+stack.size()); + } + +} From 27102f7976c974d7209a70bcd643e34c33585872 Mon Sep 17 00:00:00 2001 From: Kewy2017 Date: Sat, 11 Mar 2017 17:12:13 +0800 Subject: [PATCH 029/155] Mywork for first week data structure: ArrayList, LinkedList, Stack, Queue, BinaryTree unit test: creat a class for student test score, and use Junit to call method written. --- group22/1014331282/Mywork_LX/.classpath | 7 + group22/1014331282/Mywork_LX/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../Mywork_LX/src/week1_0306/ArrayList.java | 114 +++++++++++ .../Mywork_LX/src/week1_0306/BinaryTree.java | 135 +++++++++++++ .../Mywork_LX/src/week1_0306/Iterator.java | 9 + .../Mywork_LX/src/week1_0306/LinkedList.java | 181 ++++++++++++++++++ .../Mywork_LX/src/week1_0306/List.java | 18 ++ .../Mywork_LX/src/week1_0306/Queue.java | 35 ++++ .../Mywork_LX/src/week1_0306/ScoreRecord.java | 180 +++++++++++++++++ .../Mywork_LX/src/week1_0306/Stack.java | 36 ++++ .../src/week1_0306/Test_ArrayList.java | 56 ++++++ .../Mywork_LX/src/week1_0306/Test_BTree.java | 41 ++++ .../Mywork_LX/src/week1_0306/Test_Queue.java | 45 +++++ .../Mywork_LX/src/week1_0306/Test_Stack.java | 48 +++++ 15 files changed, 933 insertions(+) create mode 100644 group22/1014331282/Mywork_LX/.classpath create mode 100644 group22/1014331282/Mywork_LX/.project create mode 100644 group22/1014331282/Mywork_LX/.settings/org.eclipse.jdt.core.prefs create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/ArrayList.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/BinaryTree.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/Iterator.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/LinkedList.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/List.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/Queue.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/ScoreRecord.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/Stack.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/Test_ArrayList.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/Test_BTree.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/Test_Queue.java create mode 100644 group22/1014331282/Mywork_LX/src/week1_0306/Test_Stack.java diff --git a/group22/1014331282/Mywork_LX/.classpath b/group22/1014331282/Mywork_LX/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group22/1014331282/Mywork_LX/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group22/1014331282/Mywork_LX/.project b/group22/1014331282/Mywork_LX/.project new file mode 100644 index 0000000000..1290d27942 --- /dev/null +++ b/group22/1014331282/Mywork_LX/.project @@ -0,0 +1,17 @@ + + + Mywork_LX + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group22/1014331282/Mywork_LX/.settings/org.eclipse.jdt.core.prefs b/group22/1014331282/Mywork_LX/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group22/1014331282/Mywork_LX/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group22/1014331282/Mywork_LX/src/week1_0306/ArrayList.java b/group22/1014331282/Mywork_LX/src/week1_0306/ArrayList.java new file mode 100644 index 0000000000..5446c9b557 --- /dev/null +++ b/group22/1014331282/Mywork_LX/src/week1_0306/ArrayList.java @@ -0,0 +1,114 @@ +package week1_0306; + + + +public class ArrayList implements List +{ + + private int size; + + private Object[] elementData ; + + public ArrayList() + { + elementData = new Object[3]; + size=-1; + } + + public void add(Object o) + { + if(size >= elementData.length-1) + { + Object[] replaceData=new Object[elementData.length+5]; + System.arraycopy(elementData, 0, replaceData, 0, elementData.length); + elementData = replaceData; + } + elementData[++size] = o; + } + + public void add(int index, Object o) + { + Object[] replaceData=new Object[11]; + for(int i=0;i=0 && size>=0) + return elementData[index]; + else return null; + } + + + public Object remove(int index) + { + size--; + Object o=elementData[index]; + if (index0) + { + for(int i=index;i=0) + return size+1; + else + return 0; + } + + public Iterator iterator() + { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator + { + ArrayList l=null; + int pos = 0; + private ArrayListIterator(ArrayList l) + { + this.l=l; + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + pos++; + if(pos > size) + return false; + else return true; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return elementData[pos]; + + } + + public Object remove() + { + return this.l.remove(pos); + } + + } + +} diff --git a/group22/1014331282/Mywork_LX/src/week1_0306/BinaryTree.java b/group22/1014331282/Mywork_LX/src/week1_0306/BinaryTree.java new file mode 100644 index 0000000000..e5b23d37ce --- /dev/null +++ b/group22/1014331282/Mywork_LX/src/week1_0306/BinaryTree.java @@ -0,0 +1,135 @@ +package week1_0306; + +import java.util.Comparator; + +import week1_0306.BinaryTree.BinaryTreeNode; + +public class BinaryTree +{ + private BinaryTreeNode root; + + private BinaryTreeNode pointer; + + public BinaryTreeNode getRoot() { + return root; + } + + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + + public BinaryTreeNode getPointer() { + return pointer; + } + + public void setPointer(BinaryTreeNode pointer) { + this.pointer = pointer; + } + + public BinaryTree() + { + root=new BinaryTreeNode(); + pointer=root; + } + + public BinaryTreeNode insert(Object o,Comparator c) + { + + pointer= root.insert(o, c); + return pointer; + } + + public void printTree() + { + root.printNode(); + } + + public class BinaryTreeNode + { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + pointer=left; + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + pointer=right; + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + + + public BinaryTreeNode insert(Object o, Comparator c) //建有规律的树+插入 + { + if(this.data == null) + { + this.data = o; + return this; + } + + int i = c.compare(this.data,o); + + if( i > 0 ) + { + if(this.left == null) + { + this.left=new BinaryTreeNode(); + this.left.data=o; + return this.left; + } + else + return this.left.insert(o, c); + } + else if(i < 0) + { + if(this.right == null) + { + this.right=new BinaryTreeNode(); + this.right.data = o; + return this.right; + } + + else + return this.right.insert(o, c); + } + else + { + return this; + } + + } + + public void printNode() + { + ScoreRecord s=(ScoreRecord)(this.getData()); + System.out.println(s.getName()+" "+s.getId()); + if(this.getLeft()!=null) + this.getLeft().printNode(); + if(this.getRight()!=null) + this.getRight().printNode(); + else return; + } + + } + +} diff --git a/group22/1014331282/Mywork_LX/src/week1_0306/Iterator.java b/group22/1014331282/Mywork_LX/src/week1_0306/Iterator.java new file mode 100644 index 0000000000..26471af1cd --- /dev/null +++ b/group22/1014331282/Mywork_LX/src/week1_0306/Iterator.java @@ -0,0 +1,9 @@ +package week1_0306; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + public Object remove(); + +} diff --git a/group22/1014331282/Mywork_LX/src/week1_0306/LinkedList.java b/group22/1014331282/Mywork_LX/src/week1_0306/LinkedList.java new file mode 100644 index 0000000000..e540a7c115 --- /dev/null +++ b/group22/1014331282/Mywork_LX/src/week1_0306/LinkedList.java @@ -0,0 +1,181 @@ +package week1_0306; + + + +public class LinkedList implements List +{ + private Node head; + + private static int size = 0; + + public LinkedList() + { + head=new Node(); + + } + + + private static class Node + { + Object data; + Node next; + } + + public void add(Object o) + { + Node h = head; + while(h.next!= null) + { + h=h.next; + } + Node p = new Node(); + p.data=o; + p.next=null; + h.next= p; + + size++; + } + + public void add(int index, Object o) + { + Node h=head.next; + int i=0; + while(i size) + return false; + else return true; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return l.get(pos); + + } + + public Object remove() + { + return this.l.remove(pos); + } + + } + + +} + diff --git a/group22/1014331282/Mywork_LX/src/week1_0306/List.java b/group22/1014331282/Mywork_LX/src/week1_0306/List.java new file mode 100644 index 0000000000..7ec758f832 --- /dev/null +++ b/group22/1014331282/Mywork_LX/src/week1_0306/List.java @@ -0,0 +1,18 @@ +package week1_0306; + + + +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(); +} diff --git a/group22/1014331282/Mywork_LX/src/week1_0306/Queue.java b/group22/1014331282/Mywork_LX/src/week1_0306/Queue.java new file mode 100644 index 0000000000..e9c32b8e68 --- /dev/null +++ b/group22/1014331282/Mywork_LX/src/week1_0306/Queue.java @@ -0,0 +1,35 @@ +package week1_0306; + +public class Queue { + + private LinkedList queue = new LinkedList(); + + public void enQueue(Object o)//进队列 + { + queue.addFirst(o); + } + + public Object deQueue()//出队列 + { + return queue.removeLast(); + } + + public boolean isEmpty() + { + if(queue.get(0)==null) + return true; + else return false; + } + + public int size() + { + return queue.size(); + } + + public Queue() { + // TODO Auto-generated constructor stub + } + + + +} diff --git a/group22/1014331282/Mywork_LX/src/week1_0306/ScoreRecord.java b/group22/1014331282/Mywork_LX/src/week1_0306/ScoreRecord.java new file mode 100644 index 0000000000..f3bce47950 --- /dev/null +++ b/group22/1014331282/Mywork_LX/src/week1_0306/ScoreRecord.java @@ -0,0 +1,180 @@ +package week1_0306; +/* +提示录入几个学生信息 +录入学号姓名三门成绩 +打印学生中语文数学英语成绩最高的那个人 +统计每门课的平均成绩及总成绩的平均值 +封装处理 +*/ +import java.util.Scanner; +import java.util.Comparator; + +public class ScoreRecord implements Comparator +{ + + private int id; + private String name; + private float C_Score; + private float M_Score; + private float E_Score; + + public ScoreRecord(){}; + + public ScoreRecord(int id,String name,float C_Score, float M_Score, float E_Score) + { + this.id=id; + this.name=name; + this.C_Score=C_Score; + this.E_Score=E_Score; + this.M_Score=M_Score; + } + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public float getC_Score() { + return C_Score; + } + + public void setC_Score(float c_Score) { + C_Score = c_Score; + } + + public float getM_Score() { + return M_Score; + } + + public void setM_Score(float m_Score) { + M_Score = m_Score; + } + + public float getE_Score() { + return E_Score; + } + + public void setE_Score(float e_Score) { + E_Score = e_Score; + } + + public int compare(Object o1, Object o2) + { + ScoreRecord s1 = (ScoreRecord) o1; + ScoreRecord s2 = (ScoreRecord) o2; + if(s1.id>s2.id) + return 1; + else if(s1.id==s2.id) + return 0; + else + return -1; + } + + public int compareTo(Object o1) + { + ScoreRecord s = (ScoreRecord) o1; + if(this.id>s.id) + return 1; + else if(this.id==s.id) + return 0; + else return -1; + } + + public static void readData(ScoreRecord[] arr) + { + Scanner a=new Scanner(System.in); + for(int i=0;i Date: Sat, 11 Mar 2017 19:01:42 +0800 Subject: [PATCH 030/155] done with LinkedList&Queue&Stack no Test --- group22/2622819383/Task1/LinkedList.java | 127 +++++++++++++++++------ group22/2622819383/Task1/Queue.java | 10 +- group22/2622819383/Task1/Stack.java | 11 +- 3 files changed, 105 insertions(+), 43 deletions(-) diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index d431e25fdb..70ddd5d7a0 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -1,59 +1,118 @@ public class LinkedList implements List { - private Node head; + private Node header; + + private Node trailer; + + private int theSize; + + public LinkedList() { + header = new Node(null, null, null); + trailer = new Node(null, header, null); + header.succ = trailer; + theSize = 0; + } public void add(Object o){ - + add(size(), o); } public void add(int index , Object o){ - + if (index < 0 || theSize < index) throw new IndexOutOfBoundsException(); + Node p = header; + while (0 < index--) p = p.succ(); + p.insertAsSucc(o); + theSize++; } public Object get(int index){ - return null; + if (index < 0 || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); + while (0 < index--) p = p.succ(); + + return p.data(); } public Object remove(int index){ - return null; + if (0 < index || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); + while (0 < index--) p = p.succ(); + Object removed = p.data(); + p.pred().succ = p.succ(); + p.succ().pred = p.pred(); + theSize--; + return removed; } public int size(){ - return -1; + return theSize; } public void addFirst(Object o){ - + header.insertAsSucc(o); } public void addLast(Object o){ - + trailer.insertAsPred(o); } public Object removeFirst(){ - return null; + return remove(0); } public Object removeLast(){ - return null; + return remove(theSize - 1); } public Iterator iterator(){ - return null; - } - + return new LinkedListIterator(); + } + private class LinkedListIterator implements Iterator { + private Node current = header.succ(); + public boolean hasNext() { + return current != trailer; + } + public Object next() { + if (!hasNext()) throw new java.util.NoSuchElementException(); + Object item = current.data(); + current = current.succ(); + return item; + } + } - private static class Node{ - Object data; - Node next; + private static class Node{ + private Object data; + private Node pred; + private Node succ; + public Node(Object d, Node p, Node s) { + data = d; + pred = p; + succ = s; + } + public Object data() { return data; } + public Node succ() { return succ; } + public Node pred() { return pred; } + //插入前驱节点,返回新节点 + public Node insertAsPred(Object data) { + Node p = new Node(data, pred, this); + pred = pred.succ = p; + return p; + } + public Node insertAsSucc(Object data) { + Node p = new Node(data, this, succ); + succ = succ.pred = p; + return p; + } + + } /** - * 鎶婅閾捐〃閫嗙疆 - * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 */ public void reverse(){ } /** - * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 - * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 - * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 */ public void removeFirstHalf(){ @@ -61,7 +120,7 @@ public void removeFirstHalf(){ } /** - * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 * @param i * @param length */ @@ -69,11 +128,11 @@ public void remove(int i, int length){ } /** - * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 - * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 - * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 * listB = 1->3->4->6 - * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * 返回的结果应该是[101,301,401,601] * @param list */ public static int[] getElements(LinkedList list){ @@ -81,8 +140,8 @@ public static int[] getElements(LinkedList list){ } /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 * @param list */ @@ -92,16 +151,16 @@ public void subtract(LinkedList list){ } /** - * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) */ public void removeDuplicateValues(){ } /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) * @param min * @param max */ @@ -110,8 +169,8 @@ public void removeRange(int min, int max){ } /** - * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 - * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 * @param list */ public LinkedList intersection( LinkedList list){ diff --git a/group22/2622819383/Task1/Queue.java b/group22/2622819383/Task1/Queue.java index 08f1e8ba29..654a1e3e38 100644 --- a/group22/2622819383/Task1/Queue.java +++ b/group22/2622819383/Task1/Queue.java @@ -1,17 +1,19 @@ public class Queue { + private LinkedList elementData = new LinkedList(); - public void enQueue(Object o){ + public void enQueue(Object o){ + elementData.addLast(o); } public Object deQueue(){ - return null; + return elementData.removeFirst(); } public boolean isEmpty(){ - return false; + return size() == 0; } public int size(){ - return -1; + return elementData.size(); } } diff --git a/group22/2622819383/Task1/Stack.java b/group22/2622819383/Task1/Stack.java index 6c8c49bb10..17a4d0bd0f 100644 --- a/group22/2622819383/Task1/Stack.java +++ b/group22/2622819383/Task1/Stack.java @@ -1,20 +1,21 @@ public class Stack { private ArrayList elementData = new ArrayList(); - public void push(Object o){ + public void push(Object o){ + elementData.add(o); } public Object pop(){ - return null; + return elementData.remove(size() - 1); } public Object peek(){ - return null; + return elementData.get(size() - 1); } public boolean isEmpty(){ - return false; + return size() == 0; } public int size(){ - return -1; + return elementData.size(); } } From ee94cd7ce5ff84826a35d360a631f4cb09e8110e Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 19:25:05 +0800 Subject: [PATCH 031/155] change from GB2312 to UTF-8 --- group22/2622819383/Task1/ArrayList.java | 103 ++++++++++++++---------- 1 file changed, 60 insertions(+), 43 deletions(-) diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java index 1c060ffe3a..19b0b6ca53 100644 --- a/group22/2622819383/Task1/ArrayList.java +++ b/group22/2622819383/Task1/ArrayList.java @@ -8,11 +8,10 @@ public class ArrayList implements List { private Object[] elementData; - //add()时用于扩容 + //add()鏃剁敤浜庡湪蹇呰鏃跺埢鎵╁厖搴曞眰鏁扮粍瀹归噺 private void expand() { - if (size < capacity) return;//尚未满员,不必扩容 - - if (capacity < DEFAULT_CAPACITY) capacity = DEFAULT_CAPACITY;//不低于最小容量 + if (size < capacity) return;//灏氭湭婊″憳锛屼笉蹇呮墿瀹 + if (capacity < DEFAULT_CAPACITY) capacity = DEFAULT_CAPACITY;//涓嶄綆浜庢渶灏忓閲 Object[] oldElem = elementData; elementData = new Object[capacity <<= 1]; @@ -20,67 +19,65 @@ private void expand() { elementData[i] = oldElem[i]; } - //remove()时用于缩容 + //remove()鏃剁敤浜庡湪蹇呰鏃跺埢缂╁皬搴曞眰鏁扮粍瀹归噺 private void shrink() { - if (capacity < DEFAULT_CAPACITY << 1) return;//不致收缩至DEFAULT_CAPACITY以下 - - if (capacity >> 2 < size) return; //以25%为界 + if (capacity < DEFAULT_CAPACITY << 1) return;//涓嶈嚧鏀剁缉鑷矰EFAULT_CAPACITY浠ヤ笅 + if (capacity >> 2 < size) return; //浠25%涓虹晫 Object[] oldElem = elementData; elementData = new Object[capacity >>= 1]; for (int i = 0; i < size; i++) - elementData[i] = oldElem[i]; - + elementData[i] = oldElem[i]; } public ArrayList() { clear(); } - public ArrayList(Object ...args) { - this(); - for (Object o : args) - add(o); - } public void clear() { size = 0; elementData = new Object[capacity = DEFAULT_CAPACITY]; } - public int size() { return size; } + public int size() { + return size; + } - public int capacity() { return capacity; }//用于测试shrink()&expand() + public int capacity() { //鐢ㄤ簬娴嬭瘯shrink()&expand() + return capacity; + } - public boolean isEmpty() { return size == 0; } + public boolean isEmpty() { + return size == 0; + } public void add(Object o){ add(size(), o); } + public void add(int index, Object o){ - if (index < 0 || size < index) throw new IndexOutOfBoundsException(); - expand(); + if (index < 0 || size < index) + throw new IndexOutOfBoundsException(); + expand(); for (int i = size; i > index; i--) elementData[i] = elementData[i - 1]; elementData[index] = o; - size++; } - public void add(Object ...args) { - for (Object o : args) - add(o); - } - + public Object get(int index){ - if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); - return elementData[index]; + if (index < 0 || size <= index) + throw new IndexOutOfBoundsException(); + + return elementData[index]; } public Object remove(int index){ - if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); + if (index < 0 || size <= index) + throw new IndexOutOfBoundsException(); Object removed = elementData[index]; - for (int i = index; i < size - 1; i++) elementData[i] = elementData[i + 1]; size--; @@ -88,10 +85,7 @@ public Object remove(int index){ return removed; } - public void removeElems(int ...args) { - for (int i : args) - remove(i); - } + public Iterator iterator(){ return new ArrayListIterator(); @@ -99,15 +93,39 @@ public Iterator iterator(){ private class ArrayListIterator implements Iterator { private int current; - public boolean hasNext() { return current != size; } + + public boolean hasNext() { + return current != size; + } + public Object next() { - if (!hasNext()) throw new java.util.NoSuchElementException(); + if (!hasNext()) + throw new java.util.NoSuchElementException(); return elementData[current++]; } } + + + //浠ヤ笅鏂规硶渚夸簬娴嬭瘯 + + public ArrayList(Object ...args) { + this(); + for (Object o : args) + add(o); + } + + public void add(Object ...args) { + for (Object o : args) + add(o); + } + + public void removeElems(int ...args) { + for (int i : args) + remove(i); + } public static void showElements(ArrayList list) { - System.out.print("当前list中元素:"); + System.out.print("褰撳墠list涓厓绱狅細"); Iterator iter = list.iterator(); while (iter.hasNext()) System.out.print(iter.next() + " "); @@ -115,12 +133,11 @@ public static void showElements(ArrayList list) { } public static void test(ArrayList list) { - System.out.println("--------基本方法测试---------"); - System.out.println("当前list.isEmpty(): " + list.isEmpty()); - System.out.println("当前list.size(): " + list.size()); - System.out.println("当前list.capacity(): " + list.capacity()); - showElements(list); - + System.out.println("--------鍩烘湰鏂规硶娴嬭瘯---------"); + System.out.println("褰撳墠list.isEmpty(): " + list.isEmpty()); + System.out.println("褰撳墠list.size(): " + list.size()); + System.out.println("褰撳墠list.capacity(): " + list.capacity()); + showElements(list); } public static void main(String[] args) { From 4b527d28acde98b013b06bbe658bdcd9e749b792 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 19:33:45 +0800 Subject: [PATCH 032/155] change Tab --- group22/2622819383/Task1/ArrayList.java | 62 ++++++++++++------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java index 19b0b6ca53..55c8c616d0 100644 --- a/group22/2622819383/Task1/ArrayList.java +++ b/group22/2622819383/Task1/ArrayList.java @@ -1,12 +1,12 @@ public class ArrayList implements List { - private int size; + private int size; private int capacity; private static final int DEFAULT_CAPACITY = 10; - private Object[] elementData; + private Object[] elementData; //add()鏃剁敤浜庡湪蹇呰鏃跺埢鎵╁厖搴曞眰鏁扮粍瀹归噺 private void expand() { @@ -51,29 +51,29 @@ public boolean isEmpty() { return size == 0; } - public void add(Object o){ - add(size(), o); - } - - public void add(int index, Object o){ + public void add(Object o){ + add(size(), o); + } + + public void add(int index, Object o){ if (index < 0 || size < index) throw new IndexOutOfBoundsException(); - expand(); + expand(); for (int i = size; i > index; i--) elementData[i] = elementData[i - 1]; elementData[index] = o; size++; - } - - public Object get(int index){ + } + + public Object get(int index){ if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); - + return elementData[index]; - } - - public Object remove(int index){ + } + + public Object remove(int index){ if (index < 0 || size <= index) throw new IndexOutOfBoundsException(); @@ -82,15 +82,15 @@ public Object remove(int index){ elementData[i] = elementData[i + 1]; size--; shrink(); - return removed; - } - - - - public Iterator iterator(){ - return new ArrayListIterator(); - } - + return removed; + } + + + + public Iterator iterator(){ + return new ArrayListIterator(); + } + private class ArrayListIterator implements Iterator { private int current; @@ -105,21 +105,21 @@ public Object next() { return elementData[current++]; } } - - + + //浠ヤ笅鏂规硶渚夸簬娴嬭瘯 - + public ArrayList(Object ...args) { this(); for (Object o : args) add(o); } - + public void add(Object ...args) { for (Object o : args) add(o); } - + public void removeElems(int ...args) { for (int i : args) remove(i); @@ -131,7 +131,7 @@ public static void showElements(ArrayList list) { System.out.print(iter.next() + " "); System.out.println(); } - + public static void test(ArrayList list) { System.out.println("--------鍩烘湰鏂规硶娴嬭瘯---------"); System.out.println("褰撳墠list.isEmpty(): " + list.isEmpty()); @@ -139,7 +139,7 @@ public static void test(ArrayList list) { System.out.println("褰撳墠list.capacity(): " + list.capacity()); showElements(list); } - + public static void main(String[] args) { ArrayList list = new ArrayList(1, 2, 3, 4, 5); test(list); From b67c212a381e5a8a669b73c8ca721a6a51b25ef0 Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Sat, 11 Mar 2017 19:40:03 +0800 Subject: [PATCH 033/155] 17457741 --- address.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 address.txt diff --git a/address.txt b/address.txt new file mode 100644 index 0000000000..975ea2b26d --- /dev/null +++ b/address.txt @@ -0,0 +1 @@ + http://www.cnblogs.com/xxp17457741/p/6504673.html \ No newline at end of file From 892f436bf02f02df43f92aab723665a23e454615 Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Sat, 11 Mar 2017 19:46:39 +0800 Subject: [PATCH 034/155] 17457741 --- group22/17457741/src/address.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 group22/17457741/src/address.txt diff --git a/group22/17457741/src/address.txt b/group22/17457741/src/address.txt new file mode 100644 index 0000000000..975ea2b26d --- /dev/null +++ b/group22/17457741/src/address.txt @@ -0,0 +1 @@ + http://www.cnblogs.com/xxp17457741/p/6504673.html \ No newline at end of file From d19d48ffff0c04569e98553a5a78a457f85324dd Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Sat, 11 Mar 2017 19:47:44 +0800 Subject: [PATCH 035/155] Delete address.txt --- address.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 address.txt diff --git a/address.txt b/address.txt deleted file mode 100644 index 975ea2b26d..0000000000 --- a/address.txt +++ /dev/null @@ -1 +0,0 @@ - http://www.cnblogs.com/xxp17457741/p/6504673.html \ No newline at end of file From 05f0a9dcc608600e67b50bae9f679ba0b2a799a6 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 19:48:05 +0800 Subject: [PATCH 036/155] change Tab --- group22/2622819383/Task1/Iterator.java | 4 +-- group22/2622819383/Task1/LinkedList.java | 43 ++++++++++++++++++------ group22/2622819383/Task1/List.java | 10 +++--- group22/2622819383/Task1/Queue.java | 30 ++++++++--------- group22/2622819383/Task1/Stack.java | 38 +++++++++++---------- 5 files changed, 74 insertions(+), 51 deletions(-) diff --git a/group22/2622819383/Task1/Iterator.java b/group22/2622819383/Task1/Iterator.java index 96a43dbe0a..f390e63f3a 100644 --- a/group22/2622819383/Task1/Iterator.java +++ b/group22/2622819383/Task1/Iterator.java @@ -1,5 +1,5 @@ public interface Iterator { - public boolean hasNext(); - public Object next(); + public boolean hasNext(); + public Object next(); } diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index 70ddd5d7a0..d1af15765f 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -1,6 +1,6 @@ public class LinkedList implements List { - private Node header; + private Node header; private Node trailer; @@ -12,26 +12,30 @@ public LinkedList() { header.succ = trailer; theSize = 0; } - public void add(Object o){ add(size(), o); } + public void add(int index , Object o){ if (index < 0 || theSize < index) throw new IndexOutOfBoundsException(); + Node p = header; while (0 < index--) p = p.succ(); p.insertAsSucc(o); theSize++; } + public Object get(int index){ if (index < 0 || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); while (0 < index--) p = p.succ(); - return p.data(); } + public Object remove(int index){ if (0 < index || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); while (0 < index--) p = p.succ(); Object removed = p.data(); @@ -48,23 +52,30 @@ public int size(){ public void addFirst(Object o){ header.insertAsSucc(o); } + public void addLast(Object o){ trailer.insertAsPred(o); } + public Object removeFirst(){ return remove(0); } + public Object removeLast(){ return remove(theSize - 1); } + public Iterator iterator(){ return new LinkedListIterator(); } + private class LinkedListIterator implements Iterator { private Node current = header.succ(); + public boolean hasNext() { return current != trailer; } + public Object next() { if (!hasNext()) throw new java.util.NoSuchElementException(); Object item = current.data(); @@ -77,28 +88,38 @@ private static class Node{ private Object data; private Node pred; private Node succ; + public Node(Object d, Node p, Node s) { data = d; pred = p; succ = s; } - public Object data() { return data; } - public Node succ() { return succ; } - public Node pred() { return pred; } - //插入前驱节点,返回新节点 + + public Object data() { + return data; + } + + public Node succ() { + return succ; + } + + public Node pred() { + return pred; + } + + //插入前驱节点,返回插入的新节点 public Node insertAsPred(Object data) { Node p = new Node(data, pred, this); pred = pred.succ = p; return p; } + + //插入后继节点,返回插入的新节点 public Node insertAsSucc(Object data) { Node p = new Node(data, this, succ); succ = succ.pred = p; return p; - } - - - + } } /** diff --git a/group22/2622819383/Task1/List.java b/group22/2622819383/Task1/List.java index 4f7bcc71a8..c8f6da95a8 100644 --- a/group22/2622819383/Task1/List.java +++ b/group22/2622819383/Task1/List.java @@ -1,7 +1,7 @@ public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); + 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(); } diff --git a/group22/2622819383/Task1/Queue.java b/group22/2622819383/Task1/Queue.java index 654a1e3e38..fa916cb089 100644 --- a/group22/2622819383/Task1/Queue.java +++ b/group22/2622819383/Task1/Queue.java @@ -1,19 +1,19 @@ public class Queue { private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o){ + + public void enQueue(Object o){ elementData.addLast(o); - } - - public Object deQueue(){ - return elementData.removeFirst(); - } - - public boolean isEmpty(){ - return size() == 0; - } - - public int size(){ - return elementData.size(); - } + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } } diff --git a/group22/2622819383/Task1/Stack.java b/group22/2622819383/Task1/Stack.java index 17a4d0bd0f..3072c65370 100644 --- a/group22/2622819383/Task1/Stack.java +++ b/group22/2622819383/Task1/Stack.java @@ -1,21 +1,23 @@ public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ elementData.add(o); - } - - public Object pop(){ - return elementData.remove(size() - 1); - } - - public Object peek(){ - return elementData.get(size() - 1); - } - public boolean isEmpty(){ - return size() == 0; - } - public int size(){ - return elementData.size(); - } + } + + public Object pop(){ + return elementData.remove(size() - 1); + } + + public Object peek(){ + return elementData.get(size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } } From 6fc5ae0ccfe9d2f3d4862dd808cea4eec35b25ce Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 20:07:43 +0800 Subject: [PATCH 037/155] change Tab --- group22/2622819383/Task1/LinkedList.java | 263 ++++++++++++----------- 1 file changed, 132 insertions(+), 131 deletions(-) diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index d1af15765f..0d87d2d72a 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -12,30 +12,31 @@ public LinkedList() { header.succ = trailer; theSize = 0; } - public void add(Object o){ - add(size(), o); - } - public void add(int index , Object o){ - if (index < 0 || theSize < index) throw new IndexOutOfBoundsException(); - + public void add(Object o) { + add(size(), o); + } + + public void add(int index , Object o) { + if (index < 0 || theSize < index) throw new IndexOutOfBoundsException(); + Node p = header; while (0 < index--) p = p.succ(); p.insertAsSucc(o); theSize++; - } - - public Object get(int index){ + } + + public Object get(int index) { if (index < 0 || theSize <= index) throw new IndexOutOfBoundsException(); - - Node p = header.succ(); + + Node p = header.succ(); while (0 < index--) p = p.succ(); return p.data(); - } - - public Object remove(int index){ - if (0 < index || theSize <= index) throw new IndexOutOfBoundsException(); - + } + + public Object remove(int index) { + if (0 < index || theSize <= index) throw new IndexOutOfBoundsException(); + Node p = header.succ(); while (0 < index--) p = p.succ(); Object removed = p.data(); @@ -43,39 +44,39 @@ public Object remove(int index){ p.succ().pred = p.pred(); theSize--; return removed; - } - - public int size(){ - return theSize; - } - - public void addFirst(Object o){ - header.insertAsSucc(o); - } - - public void addLast(Object o){ - trailer.insertAsPred(o); - } - - public Object removeFirst(){ - return remove(0); - } - - public Object removeLast(){ - return remove(theSize - 1); - } - - public Iterator iterator(){ - return new LinkedListIterator(); - } - - private class LinkedListIterator implements Iterator { + } + + public int size() { + return theSize; + } + + public void addFirst(Object o) { + header.insertAsSucc(o); + } + + public void addLast(Object o) { + trailer.insertAsPred(o); + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(theSize - 1); + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { private Node current = header.succ(); - + public boolean hasNext() { return current != trailer; } - + public Object next() { if (!hasNext()) throw new java.util.NoSuchElementException(); Object item = current.data(); @@ -83,118 +84,118 @@ public Object next() { return item; } } - - private static class Node{ - private Object data; - private Node pred; + + private static class Node { + private Object data; + private Node pred; private Node succ; - + public Node(Object d, Node p, Node s) { data = d; pred = p; succ = s; } - + public Object data() { return data; } - + public Node succ() { return succ; } - + public Node pred() { return pred; } - + //插入前驱节点,返回插入的新节点 public Node insertAsPred(Object data) { Node p = new Node(data, pred, this); pred = pred.succ = p; return p; } - + //插入后继节点,返回插入的新节点 public Node insertAsSucc(Object data) { Node p = new Node(data, this, succ); succ = succ.pred = p; return p; } - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 + } - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } } From 6f45e392fe43e5a043cd976034ec706eacf66056 Mon Sep 17 00:00:00 2001 From: xiongrui233 Date: Sat, 11 Mar 2017 20:17:26 +0800 Subject: [PATCH 038/155] complete simple queue --- group22/.gitignore | 1 + group22/627559964/src/com/coding/basic/Queue.java | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 group22/.gitignore diff --git a/group22/.gitignore b/group22/.gitignore new file mode 100644 index 0000000000..d1945e125c --- /dev/null +++ b/group22/.gitignore @@ -0,0 +1 @@ +/627559964/ diff --git a/group22/627559964/src/com/coding/basic/Queue.java b/group22/627559964/src/com/coding/basic/Queue.java index 90f845e6cd..965894a9ea 100644 --- a/group22/627559964/src/com/coding/basic/Queue.java +++ b/group22/627559964/src/com/coding/basic/Queue.java @@ -8,23 +8,28 @@ */ public class Queue { - private Object[] queue = new Object[10]; + private LinkedList elements = new LinkedList();; private int size = 0; public void enQueue(Object o) { - + elements.add(o); + size ++; } public Object deQueue() { - return null; + size --; + return elements.removeLast(); } public boolean isEmpty() { + if (size == 0) { + return true; + } return false; } public int size() { - return -1; + return size; } } \ No newline at end of file From 5ad297828ae916ee3c50eccbfcb6c69341af2890 Mon Sep 17 00:00:00 2001 From: xiongrui233 Date: Sat, 11 Mar 2017 20:20:32 +0800 Subject: [PATCH 039/155] delete .classpath .project --- group22/627559964/.gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/group22/627559964/.gitignore b/group22/627559964/.gitignore index 3727029200..6ef2139e00 100644 --- a/group22/627559964/.gitignore +++ b/group22/627559964/.gitignore @@ -1,2 +1,4 @@ /bin/ -.metadata \ No newline at end of file +.metadatas +.classpath +.project \ No newline at end of file From df1b1fe9a3236a754a587674a8ac2d489ea3d93d Mon Sep 17 00:00:00 2001 From: xiongrui233 Date: Sat, 11 Mar 2017 20:22:27 +0800 Subject: [PATCH 040/155] delete ignore file --- group22/627559964/.classpath | 8 -------- group22/627559964/.gitignore | 4 ---- group22/627559964/.project | 17 ----------------- 3 files changed, 29 deletions(-) delete mode 100644 group22/627559964/.classpath delete mode 100644 group22/627559964/.gitignore delete mode 100644 group22/627559964/.project diff --git a/group22/627559964/.classpath b/group22/627559964/.classpath deleted file mode 100644 index e72ef7c0d4..0000000000 --- a/group22/627559964/.classpath +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/group22/627559964/.gitignore b/group22/627559964/.gitignore deleted file mode 100644 index 6ef2139e00..0000000000 --- a/group22/627559964/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/bin/ -.metadatas -.classpath -.project \ No newline at end of file diff --git a/group22/627559964/.project b/group22/627559964/.project deleted file mode 100644 index 870f65f4a0..0000000000 --- a/group22/627559964/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - 627559964 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - From 9af937c194a2d0dfc73dfb7578fc81b31140124b Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 11 Mar 2017 22:26:27 +0800 Subject: [PATCH 041/155] no test --- group22/2622819383/Task1/ArrayList.java | 1 + group22/2622819383/Task1/BinaryTreeNode.java | 78 +++++++++++++------- group22/2622819383/Task1/LinkedList.java | 6 +- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/group22/2622819383/Task1/ArrayList.java b/group22/2622819383/Task1/ArrayList.java index 55c8c616d0..d438fba17d 100644 --- a/group22/2622819383/Task1/ArrayList.java +++ b/group22/2622819383/Task1/ArrayList.java @@ -1,3 +1,4 @@ +//浠g爜鍙傝冭嚜銆婃暟鎹粨鏋勪笌绠楁硶鍒嗘瀽銆 public class ArrayList implements List { private int size; diff --git a/group22/2622819383/Task1/BinaryTreeNode.java b/group22/2622819383/Task1/BinaryTreeNode.java index 1f07869939..8c2b4492d2 100644 --- a/group22/2622819383/Task1/BinaryTreeNode.java +++ b/group22/2622819383/Task1/BinaryTreeNode.java @@ -1,30 +1,54 @@ +//代码参考自《数据结构与算法分析》 public class BinaryTreeNode { - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + private BinaryTreeNode parrent; + private BinaryTreeNode hot; //表示search(Object o)方法返回的命中节点的父亲 + + public BinaryTreeNode(Object o, BinaryTreeNode p) { + data = o; + parrent = p; + } + //在以v为根的二叉树中查找关键码o,返回命中的节点(真实存在得或者虚拟存在的) + public static BinaryTreeNode search(BinaryTreeNode v, Object o, BinaryTreeNode hot) { + int vData = (int)v.getData(); + int searched = (int)o; + if (v == null || vData == searched) return v; + + hot = v; + return search(searched < vData ? v.getLeft() : v.getRight(), o, hot); + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = search(this, o, this.parrent); + if (node != null) return node; + + node = new BinaryTreeNode(o, hot); + if ((int)o < (int)hot.getData()) hot.setLeft(node); + else hot.setRight(node); + return node; + } + } diff --git a/group22/2622819383/Task1/LinkedList.java b/group22/2622819383/Task1/LinkedList.java index 0d87d2d72a..81c1db409c 100644 --- a/group22/2622819383/Task1/LinkedList.java +++ b/group22/2622819383/Task1/LinkedList.java @@ -1,3 +1,4 @@ +//代码参考自《数据结构与算法分析》 public class LinkedList implements List { private Node header; @@ -86,6 +87,7 @@ public Object next() { } private static class Node { + //pred、succ代表属性;pred()、succ()代表Node节点 private Object data; private Node pred; private Node succ; @@ -111,14 +113,14 @@ public Node pred() { //插入前驱节点,返回插入的新节点 public Node insertAsPred(Object data) { Node p = new Node(data, pred, this); - pred = pred.succ = p; + pred = pred().succ = p; return p; } //插入后继节点,返回插入的新节点 public Node insertAsSucc(Object data) { Node p = new Node(data, this, succ); - succ = succ.pred = p; + succ = succ().pred = p; return p; } } From 7d13bef6e864b63b1f9beac119905865146ede7c Mon Sep 17 00:00:00 2001 From: wenGQ Date: Sat, 11 Mar 2017 22:54:12 +0800 Subject: [PATCH 042/155] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/mrwengq/first/ArrayList.java | 104 ++++++++ .../github/mrwengq/first/BinaryTreeNode.java | 77 ++++++ .../com/github/mrwengq/first/Iterator.java | 10 + .../com/github/mrwengq/first/LinkedList.java | 244 ++++++++++++++++++ .../src/com/github/mrwengq/first/List.java | 16 ++ .../src/com/github/mrwengq/first/Queue.java | 29 +++ .../src/com/github/mrwengq/first/Stack.java | 40 +++ 7 files changed, 520 insertions(+) create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/Iterator.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/List.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/Queue.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/Stack.java diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java new file mode 100644 index 0000000000..f627f38abb --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java @@ -0,0 +1,104 @@ +package com.github.mrwengq.first; + +public class ArrayList implements List { + + private int size = 0; + private Object elementData[] = new Object[5]; + + public void add(Object o) { + if (size >= elementData.length) + elementData = copyAddArray(elementData); + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + if (index > size - 1 || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } else { + elementData = addUpdateArray(elementData, index); + elementData[index] = o; + size++; + return; + } + } + + public Object get(int index) { + if (index > size - 1 || index < 0) + throw new ArrayIndexOutOfBoundsException(); + else + return elementData[index]; + } + + public Object remove(int index) { + if (index > size - 1 || index < 0) + throw new ArrayIndexOutOfBoundsException(); + if (index == size - 1) { + elementData[index] = null; + size--; + return null; + } else { + delUpdateArray(elementData, index); + size--; + return null; + } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Iterator() { + + int index; + + public boolean hasNext() { + index++; + Object ob = elementData[index]; + return ob != null; + } + + public Object next() { + return elementData[index]; + } + + }; + } + + private Object[] copyAddArray(Object elementData[]) { + Object ob[] = new Object[(elementData.length * 3) / 4]; + System.arraycopy(((Object) (elementData)), 0, ((Object) (ob)), 0, + elementData.length); + return ob; + } + + private Object[] addUpdateArray(Object elementData[], int index) { + Object temp = null; + for (int i = 0; i < size; i++) + if (i > index) { + temp = elementData[index]; + elementData[index] = elementData[i]; + elementData[i] = temp; + if (i == size - 1) { + if (size > elementData.length) + elementData = copyAddArray(elementData); + elementData[size] = elementData[index]; + } + } + + return elementData; + } + + private void delUpdateArray(Object elementData[], int index) { + Object temp = null; + for (int i = 0; i < size; i++) + if (i > index && i < size - 1) + elementData[i - 1] = elementData[i]; + else if (i == size - 1) + elementData[i] = null; + + } + + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java new file mode 100644 index 0000000000..37bc363cdb --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java @@ -0,0 +1,77 @@ +package com.github.mrwengq.first; + +import java.util.Comparator; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private Comparator cpt; + public BinaryTreeNode() { + cpt = new Comparator() { + + public int compare(Object o1, Object o2) { + int ob1 = ((Integer) o1).intValue(); + int ob2 = ((Integer) o2).intValue(); + if (ob1 > ob2){ + return 1; + } + if(ob1 0) { + if (getLeft() == null) { + this.setLeft(tree); + return null; + } + left.insert(o); + } else if (comValue < 0) { + if (getRight() == null) { + this.setRight(tree); + return null; + } + right.insert(o); + } + return null; + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/Iterator.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/Iterator.java new file mode 100644 index 0000000000..2947813ffd --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/Iterator.java @@ -0,0 +1,10 @@ +package com.github.mrwengq.first; + + +public interface Iterator +{ + + public abstract boolean hasNext(); + + public abstract Object next(); +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java new file mode 100644 index 0000000000..a8ef6f6413 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java @@ -0,0 +1,244 @@ +package com.github.mrwengq.first; + +public class LinkedList implements List { + private Node head; + private int size =0; + private static class Node { + + Object data; + Node next; + + public Node(Object o) { + data = o; + next = null; + } + } + + + public void add(Object o) { + if (size == 0) { + head = new Node(o); + } else { + Node node = new Node(o); + Node lastNode = findNode(size); + lastNode.next = node; + } + size++; + } + + private Node findNode(int index) { + Node no = head; + for (; index - 1 > 0; index--) + no = no.next; + + return no; + } + + public void add(int index, Object o) { + if (index < 0 || index > size - 1) + throw new ArrayIndexOutOfBoundsException(); + Node node = new Node(o); + Node indexNode = findNode(index); + if (index - 1 < 0) { + node.next = indexNode; + head = node; + size++; + return; + } else { + Node lastNode = findNode(index - 1); + lastNode.next = node; + node.next = indexNode; + size++; + return; + } + } + + public Object get(int index) { + if (index < 0 || index > size - 1) + throw new ArrayIndexOutOfBoundsException(); + else + return findNode(index).data; + } + + public Object remove(int index) { + if (index < 0 || index > size - 1 || size == 0) + throw new ArrayIndexOutOfBoundsException(); + Node indexNode = findNode(index); + if (size == 1) { + head = null; + size = 0; + return indexNode.data; + } + Node nextNode = null; + Node lastNode = null; + if (index + 1 < size - 1) + nextNode = findNode(index + 1); + if (index - 1 > 0) + lastNode = findNode(index - 1); + if (lastNode == null) { + head = nextNode; + size--; + return indexNode.data; + } + if (nextNode == null) { + lastNode.next = null; + size--; + return indexNode.data; + } else { + lastNode.next = nextNode; + size--; + return indexNode.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + if (size == 0) { + head = node; + size++; + return; + } else { + node.next = head; + head = node; + size++; + return; + } + } + + public void addLast(Object o) { + Node node = new Node(o); + if (size == 0) { + head = node; + size++; + return; + } else { + Node lastNode = findNode(size); + lastNode.next = node; + size++; + return; + } + } + + public Object removeFirst() { + if (size == 0) { + return null; + } else { + Node nextNode = head.next; + Object ob = head.data; + head = nextNode; + size--; + return ob; + } + } + + public Object removeLast() { + if (size == 0) { + return null; + } else { + Node node = findNode(size); + Node lastNode = findNode(size - 1); + lastNode.next = null; + size--; + return node.data; + } + } + + public Iterator iterator() { + return new Iterator() { + + int index = -1; + + public boolean hasNext() { + index++; + return findNode(index) != null; + } + + public Object next() { + return findNode(index).data; + } + + + }; + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/List.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/List.java new file mode 100644 index 0000000000..d36d04b8b6 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/List.java @@ -0,0 +1,16 @@ +package com.github.mrwengq.first; + + +public interface List +{ + + public abstract void add(Object obj); + + public abstract void add(int i, Object obj); + + public abstract Object get(int i); + + public abstract Object remove(int i); + + public abstract int size(); +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/Queue.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/Queue.java new file mode 100644 index 0000000000..004ac39e6b --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/Queue.java @@ -0,0 +1,29 @@ +package com.github.mrwengq.first; + +public class Queue { + + private ArrayList elementData; + public Queue() { + elementData = new ArrayList(); + } + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + Object ob = null; + ob = elementData.get(0); + elementData.remove(0); + return ob; + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/Stack.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/Stack.java new file mode 100644 index 0000000000..f0614a13df --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/Stack.java @@ -0,0 +1,40 @@ +package com.github.mrwengq.first; + +public class Stack { + + + private ArrayList elementData; + public Stack() { + elementData = new ArrayList(); + } + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (elementData.size() == 0) { + return null; + } else { + Object ob = null; + ob = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + return ob; + } + } + + public Object peek() { + if (elementData.size() == 0) + return null; + else + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} From 9e1a6ebeccad0fd3bfc8059c48b7b0d589ce3f7c Mon Sep 17 00:00:00 2001 From: 240094626 Date: Sun, 12 Mar 2017 11:36:08 +0800 Subject: [PATCH 043/155] 240094626-work0305-linkedlist --- .../com/coderising/linkedlist/Iterator.java | 8 + .../com/coderising/linkedlist/LinkedList.java | 293 ++++++++++++++++++ .../coderising/linkedlist/LinkedListTest.java | 163 ++++++++++ .../src/com/coderising/linkedlist/List.java | 9 + .../coderising/linkedlist/SLinkedList.java | 119 +++++++ 5 files changed, 592 insertions(+) create mode 100644 group17/240094626/work_0305/src/com/coderising/linkedlist/Iterator.java create mode 100644 group17/240094626/work_0305/src/com/coderising/linkedlist/LinkedList.java create mode 100644 group17/240094626/work_0305/src/com/coderising/linkedlist/LinkedListTest.java create mode 100644 group17/240094626/work_0305/src/com/coderising/linkedlist/List.java create mode 100644 group17/240094626/work_0305/src/com/coderising/linkedlist/SLinkedList.java diff --git a/group17/240094626/work_0305/src/com/coderising/linkedlist/Iterator.java b/group17/240094626/work_0305/src/com/coderising/linkedlist/Iterator.java new file mode 100644 index 0000000000..30ea750ecc --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/linkedlist/Iterator.java @@ -0,0 +1,8 @@ +package com.coderising.linkedlist; + +public interface Iterator { + + public boolean hasNext(); + + public E next(); +} diff --git a/group17/240094626/work_0305/src/com/coderising/linkedlist/LinkedList.java b/group17/240094626/work_0305/src/com/coderising/linkedlist/LinkedList.java new file mode 100644 index 0000000000..feddaedd23 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/linkedlist/LinkedList.java @@ -0,0 +1,293 @@ +package com.coderising.linkedlist; + +import java.util.HashMap; +import java.util.Map; + +public class LinkedList implements List { + + transient Node head; + transient int size; + + public LinkedList(){ + head = new Node(null,head); + size = 0; + } + public void addFirst(E e){ + head.next = new Node(e,head.next); + size++; + } + + + public void addAfter(Node n, E e){ + if(n == head){ + addFirst(e); + }else{ + Node curr = new Node(e,n.next); + n.next = curr; + size++; + } + } + + + private void checkRange(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + } + } + + private Node getNode(int index){ + checkRange(index); + Node curr = head; + for(; index >= 0 ; index--){ + curr = curr.next; + } + return curr; + } + + public void add(E e) { + add(size,e); + } + + public void add(int index, E e) { + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + } + if(index == 0){ + addFirst(e); + }else{ + addAfter(getNode(index-1), e); + } + } + + public E get(int index) { + Node n = getNode(index); + return n.e; + } + + + public E remove(int index) { + checkRange(index); + Node preN = null, + currN = null; + if(index == 0){ + preN = head; + }else{ + preN = getNode(index-1); + } + currN = preN.next; + preN.next = currN.next; + E e = currN.e; + currN.e = null; + currN.next = null; + size--; + return e; + + } + + public int size() { + return size; + } + + + private static class Node{ + E e ; + Node next; + + public Node(E e,Node next){ + this.e = e; + this.next = next; + } + } + + private Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + + int index; + public LinkedListIterator(){ + index = 0; + } + + @Override + public boolean hasNext() { + if(index < size){ + return true; + } + return false; + } + + @Override + public E next() { + Node curr = (Node) getNode(index++); + return curr.e; + } + + } + + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + if(size > 1){ + int index = 0; + Node a = null, + preA = null, + b = null, + preB = null; + for(;index < size/2; index++){ + if(index == 0){ + preA = head; + a = head.next; + b = getNode(size-1); + preB = getNode(size-2); + + head.next = b; + preB.next = a; + b.next = a.next; + a.next = head; + }else{ + preA = getNode(index-1); + a = preA.next; + preB = getNode(size-2-index); + b = preB.next; + + preA.next = b; + preB.next = a; + Node tmp = a.next; + a.next = b.next; + b.next = tmp; + } + + } + } + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + remove(0, size/2); + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + while(length > 0){ + remove(i); + length--; + } + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] a = new int[list.size]; + for(int i = 0; i < list.size; i++){ + Node curr = getNode((int)list.get(i)); + a[i] = (int) curr.e; + } + return a; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * @param list + */ + + public void subtract(LinkedList list){ + int i = 0, + j = 0; + for(; i < list.size; i++){ + E a = (E) list.get(i); + while(j < size){ + E b = get(j); + if(a.equals(b)){ + remove(j); + break; + } + j++; + } + } + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + Map map = new HashMap(size); + for(int i = 0; i < size; ){ + if(map.get(getNode(i).e) == null){ + map.put(getNode(i).e, i); + i++; + }else{ + remove(i); + } + } + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if(min >= max){ + return ; + } + for(int i = 0; i < size; ){ + int a = (int)get(i); + if(min < a && max > a){ + remove(i); + continue; + } + i++; + } + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList c = new LinkedList(); + if(list.size == 0){ + return c; + } + Map map = new HashMap(); + for(int i = 0; i < list.size; i++){ + E a = list.get(i); + if(null == map.get(a)){ + map.put(a, true); + } + } + for(int i = 0; i < size; i++){ + E a = get(i); + if(null == map.get(a)){ + map.put(a, true); + }else if(map.get(a)){ + c.add(get(i)); + } + } + + return c; + } +} diff --git a/group17/240094626/work_0305/src/com/coderising/linkedlist/LinkedListTest.java b/group17/240094626/work_0305/src/com/coderising/linkedlist/LinkedListTest.java new file mode 100644 index 0000000000..b245851f2a --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/linkedlist/LinkedListTest.java @@ -0,0 +1,163 @@ +package com.coderising.linkedlist; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class LinkedListTest { + LinkedList list = new LinkedList(); + @Before + public void setUp() throws Exception { + list = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + list = new LinkedList(); + } + + @Test + public void testReverse() { + list.add(3); + list.add(5); + list.add(6); + list.add(7); + list.add(9); + list.add(8); + Assert.assertEquals(3, list.get(0)); + Assert.assertEquals(5, list.get(1)); + Assert.assertEquals(6, list.get(2)); + Assert.assertEquals(7, list.get(3)); + Assert.assertEquals(9, list.get(4)); + Assert.assertEquals(8, list.get(5)); + list.reverse(); + Assert.assertEquals(8, list.get(0)); + Assert.assertEquals(9, list.get(1)); + Assert.assertEquals(7, list.get(2)); + Assert.assertEquals(6, list.get(3)); + Assert.assertEquals(5, list.get(4)); + Assert.assertEquals(3, list.get(5)); + } + @Test + public void testRemove(){ + list.add(3); + list.add(5); + list.add(6); + list.add(7); + list.add(9); + list.remove(0); + Assert.assertEquals(5, list.get(0)); + list.remove(1); + Assert.assertEquals(7, list.get(1)); + } + @Test + public void testRemoveFirstHalf(){ + list.add(3); + list.add(5); + list.add(6); + list.add(7); + list.add(9); + list.add(8); + list.removeFirstHalf(); + Assert.assertEquals(7, list.get(0)); + Assert.assertEquals(9, list.get(1)); + Assert.assertEquals(8, list.get(2)); + } + + @Test + public void testGetElements(){ + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + list.add(801); + list.add(901); + list.add(1001); + LinkedList list1 = new LinkedList(); + list1.add(0); + list1.add(2); + list1.add(4); + list1.add(6); + int[] a = list.getElements(list1); + Assert.assertEquals(301, a[0]); + Assert.assertEquals(501, a[1]); + Assert.assertEquals(701, a[2]); + Assert.assertEquals(901, a[3]); + } + @Test + public void testSubtract(){ + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(701); + list.add(801); + list.add(901); + list.add(1001); + LinkedList list1 = new LinkedList(); + list1.add(401); + list1.add(701); + list1.add(801); + list.subtract(list1); + Assert.assertEquals(301, list.get(0)); + Assert.assertEquals(501, list.get(1)); + Assert.assertEquals(901, list.get(3)); + } + @Test + public void testRemoveDuplicateValues(){ + list.add(301); + list.add(401); + list.add(401); + list.add(601); + list.add(601); + list.add(801); + list.add(901); + list.add(1001); + list.removeDuplicateValues(); + Assert.assertEquals(6, list.size); + } + + @Test + public void testRemoveRange(){ + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(801); + list.add(901); + list.removeRange(500, 600); + Assert.assertEquals(301, list.get(0)); + Assert.assertEquals(401, list.get(1)); + Assert.assertEquals(601, list.get(2)); + } + + @Test + public void testIntersection(){ + list.add(301); + list.add(401); + list.add(501); + list.add(601); + list.add(801); + list.add(901); + LinkedList list1 = new LinkedList(); + list1.add(401); + list1.add(601); + list1.add(701); + LinkedList c = list.intersection(list1); + Assert.assertEquals(401,c.get(0)); + Assert.assertEquals(601,c.get(1)); + } + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(3); + list.add(5); + list.add(6); + list.add(8); +// list.reverse(); + java.util.LinkedList list1 =null; + } +} diff --git a/group17/240094626/work_0305/src/com/coderising/linkedlist/List.java b/group17/240094626/work_0305/src/com/coderising/linkedlist/List.java new file mode 100644 index 0000000000..f1587b17c5 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/linkedlist/List.java @@ -0,0 +1,9 @@ +package com.coderising.linkedlist; + +public interface List { + public void add(E e); + public void add(int index, E e); + public E get(int index); + public E remove(int index); + public int size(); +} diff --git a/group17/240094626/work_0305/src/com/coderising/linkedlist/SLinkedList.java b/group17/240094626/work_0305/src/com/coderising/linkedlist/SLinkedList.java new file mode 100644 index 0000000000..cef0226bb3 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/linkedlist/SLinkedList.java @@ -0,0 +1,119 @@ +package com.coderising.linkedlist; + + +/** + * 鍗曞悜閾捐〃绠鍗曞疄鐜 + * @author 240094626 + * + */ +public class SLinkedList implements List{ + transient Node head; + transient Node last; + transient int size; + + public SLinkedList(){ + head = new Node(null,last); + last = null; + size = 0; + } + + + private void addAfter(Node n , E e){ + if(n == null){ + last = new Node(e,head); + head.next = last; + }else{ + n.next = new Node(e,n.next); + } + size++; + } + private void checkRange(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + } + } + + private Node getNode(int index){ + checkRange(index); + Node curr = head; + for(; index >= 0 ; index--){ + curr = curr.next; + } + return curr; + } + + public void add(E e) { + addAfter(last,e); + } + + public void add(int index, E e) { + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + } + addAfter(getNode(index-1), e); + } + + public E get(int index) { + Node n = getNode(index); + return n.e; + } + + + public E remove(int index) { + checkRange(index); + Node preN = null, + currN = null; + if(index == 0){ + preN = head; + }else{ + preN = getNode(index-1); + } + currN = preN.next; + preN.next = currN.next; + size--; + return currN.e=null; + + } + + public int size() { + return size; + } + + + private static class Node{ + E e ; + Node next; + + public Node(E e,Node next){ + this.e = e; + this.next = next; + } + } + + private Iterator iterator(){ + return new SLinkedListIterator(); + } + + private class SLinkedListIterator implements Iterator{ + + int index; + public SLinkedListIterator(){ + index = 0; + } + + @Override + public boolean hasNext() { + if(index < size){ + return true; + } + return false; + } + + @Override + public E next() { + Node curr = (Node) getNode(index++); + return curr.e; + } + + } +} From c282825848976630b39046f4f5c52dc0b3678622 Mon Sep 17 00:00:00 2001 From: sjlv <1258890344@qq.com> Date: Sun, 12 Mar 2017 13:18:22 +0800 Subject: [PATCH 044/155] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group22/1258890344/.gitignore | 48 +++++ .../src/com/coding/basic/ArrayList.java | 76 +++++++ .../src/com/coding/basic/BinaryTreeNode.java | 41 ++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 203 ++++++++++++++++++ .../1258890344/src/com/coding/basic/List.java | 9 + .../src/com/coding/basic/Queue.java | 27 +++ .../src/com/coding/basic/Stack.java | 27 +++ 8 files changed, 438 insertions(+) create mode 100644 group22/1258890344/.gitignore create mode 100644 group22/1258890344/src/com/coding/basic/ArrayList.java create mode 100644 group22/1258890344/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group22/1258890344/src/com/coding/basic/Iterator.java create mode 100644 group22/1258890344/src/com/coding/basic/LinkedList.java create mode 100644 group22/1258890344/src/com/coding/basic/List.java create mode 100644 group22/1258890344/src/com/coding/basic/Queue.java create mode 100644 group22/1258890344/src/com/coding/basic/Stack.java diff --git a/group22/1258890344/.gitignore b/group22/1258890344/.gitignore new file mode 100644 index 0000000000..ea7fd75f1f --- /dev/null +++ b/group22/1258890344/.gitignore @@ -0,0 +1,48 @@ +/bin/ +# Class files +*.class + +# Package Files +*.jar +*.war +*.ear + +# Virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# Ignore web-site project +*web-site/ + +# Temporary files +.DS_STORE +*.log + +# Maven related +/*/target/ +target + +# Netbeans related +nb-configuration.xml +nbactions.xml +nbproject + +# Eclipse related + +.settings +*.classpath +*.project + +# IntelliJ related +.idea +*.iml +*.ipr +*.iws + +# Jrebel related +rebel.xml +rebel-remote.xml + +# design model +*.eab + +.idea/workspace.xml \ No newline at end of file diff --git a/group22/1258890344/src/com/coding/basic/ArrayList.java b/group22/1258890344/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..b61c1d833c --- /dev/null +++ b/group22/1258890344/src/com/coding/basic/ArrayList.java @@ -0,0 +1,76 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(this.size==elementData.length){ + elementData=Arrays.copyOf(elementData, size+1); + elementData[size]=o; + }else{ + elementData[size]=o; + } + size++; + + } + public void add(int index, Object o){ + if(index<0||index>=this.size){ + throw new ArrayIndexOutOfBoundsException("鏁扮粍瓒婄晫寮傚父"); + }else{ + if(indexelementData.length){ + throw new ArrayIndexOutOfBoundsException("鏁扮粍瓒婄晫寮傚父"); + } + return elementData[index]; + + } + + public Object remove(int index){ + if(index<0||index>=elementData.length){ + throw new ArrayIndexOutOfBoundsException("鏁扮粍瓒婄晫寮傚父"); + }else{ + Object deletedElement=elementData[index]; + if(index7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group22/1258890344/src/com/coding/basic/List.java b/group22/1258890344/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group22/1258890344/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +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(); +} diff --git a/group22/1258890344/src/com/coding/basic/Queue.java b/group22/1258890344/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..1c76d442ef --- /dev/null +++ b/group22/1258890344/src/com/coding/basic/Queue.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class Queue { + private LinkedList list=new LinkedList(); + private int size=0; + public void enQueue(Object o){ + list.addLast(o); + size++; + } + + public Object deQueue(){ + Object o= list.removeFirst(); + size--; + return o; + } + + public boolean isEmpty(){ + if(size==0){ + return true; + } + return false; + } + + public int size(){ + return size; + } +} diff --git a/group22/1258890344/src/com/coding/basic/Stack.java b/group22/1258890344/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..8b2ee6c7e2 --- /dev/null +++ b/group22/1258890344/src/com/coding/basic/Stack.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class Stack { + private ArrayList list = new ArrayList(); + private int size=0; + public void push(Object o){ + list.add(o); + size++; + } + + public Object pop(){ + Object o=list.remove(size-1); + size--; + return o; + } + + public Object peek(){ + Object o=list.get(size-1); + return o; + } + public boolean isEmpty(){ + return size==0?true:false; + } + public int size(){ + return size; + } +} From b7bffbf0a783fab856f53613edf2305c90376e2e Mon Sep 17 00:00:00 2001 From: wenGQ Date: Sun, 12 Mar 2017 14:00:11 +0800 Subject: [PATCH 045/155] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + .../com/github/mrwengq/first/ArrayList.java | 28 ++- .../github/mrwengq/first/ArrayListTest.java | 112 +++++++++ .../github/mrwengq/first/BinaryTreeNode.java | 21 ++ .../mrwengq/first/BinaryTreeNodeTest.java | 37 +++ .../com/github/mrwengq/first/LinkedList.java | 30 ++- .../github/mrwengq/first/LinkedListTest.java | 216 ++++++++++++++++++ .../com/github/mrwengq/first/QueueTest.java | 76 ++++++ .../src/com/github/mrwengq/first/Stack.java | 13 +- .../com/github/mrwengq/first/StackTest.java | 88 +++++++ 10 files changed, 598 insertions(+), 26 deletions(-) create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayListTest.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNodeTest.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedListTest.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/QueueTest.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/first/StackTest.java diff --git a/.gitignore b/.gitignore index d749cfbd62..3d01d810e2 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ rebel.* .rebel.* target +*.classpath +group22/1193590951/githubitem/.project +*.prefs diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java index f627f38abb..0974b8e1dc 100644 --- a/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java @@ -51,12 +51,16 @@ public int size() { public Iterator iterator() { return new Iterator() { - int index; + int index=-1; public boolean hasNext() { index++; - Object ob = elementData[index]; - return ob != null; + if(index index && i < size - 1) - elementData[i - 1] = elementData[i]; - else if (i == size - 1) - elementData[i] = null; + for (int i = 0; i < size; i++){ + + if (i > index && i < size ){ + elementData[i - 1] = elementData[i]; + if (i == size - 1){ + elementData[i] = null; + } + } + } } - } diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayListTest.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayListTest.java new file mode 100644 index 0000000000..000ad6e813 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayListTest.java @@ -0,0 +1,112 @@ +package com.github.mrwengq.first; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + ArrayList list = new ArrayList(); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + list.add(7); + int[] o = new int[] { 1, 2, 3, 4, 5, 6, 7 }; + for (int i = 0; i < list.size(); i++) { + assertEquals(o[i], o[i]); + } + + } + + @Test + public void testAddIntObject() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + list.add(7); + list.add(5, 9); + int[] o = new int[] { 1, 2, 3, 4, 5, 9, 6, 7 }; + for (int i = 0; i < list.size(); i++) { + assertEquals(o[i], o[i]); + } + } + + @Test + public void testGet() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + list.add(7); + assertEquals(list.get(5), 6); + assertEquals(list.get(2), 3); + assertEquals(list.get(4), 5); + assertEquals(list.get(6), 7); + } + + @Test + public void testRemove() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.remove(3); + assertEquals(list.get(3), 5); + } + + @Test + public void testSize() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + assertEquals(list.size(), 5); + } + + @Test + public void testIterator() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + Iterator iter = list.iterator(); + int i = 0; + int[] o = new int[] { 1, 2, 3, 4, 5}; + + while(iter.hasNext()){ + + assertEquals(iter.next(),o[i]); + i++; + } + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java index 37bc363cdb..f832e10521 100644 --- a/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java @@ -73,5 +73,26 @@ public BinaryTreeNode insert(Object o) { } return null; } + //鏂板姞鍏ョ殑鏂规硶鐢ㄤ簬鑾峰彇鑺傜偣锛屽府鍔╂祴璇昳nsert + public BinaryTreeNode findNode(Object o){ + + + if((int)this.data == (int)o){ + return this; + + }else if((int)this.data > (int)o){ + if(this.left!=null){ + return this.left.findNode(o); + } + return null; + }else if((int)this.data <(int)o){ + if(this.right!=null){ + return this.right.findNode(o); + } + return null; + } + return null; + + } } diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNodeTest.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..44fad39594 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNodeTest.java @@ -0,0 +1,37 @@ +package com.github.mrwengq.first; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class BinaryTreeNodeTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testInsert() { + BinaryTreeNode btnt = new BinaryTreeNode(); + btnt.insert(15); + btnt.insert(16); + btnt.insert(23); + btnt.insert(10); + btnt.insert(18); + btnt.insert(9); + + assertEquals(btnt.getData(), 15); + assertEquals(btnt.getLeft().getData(),10); + assertEquals(btnt.getRight().getData(),16); + assertEquals(btnt.findNode(16).getRight().getData(),23); + assertEquals(btnt.findNode(23).getLeft().getData(),18); + assertEquals(btnt.findNode(10).getLeft().getData(),9); + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java index a8ef6f6413..846634287e 100644 --- a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java @@ -20,7 +20,7 @@ public void add(Object o) { head = new Node(o); } else { Node node = new Node(o); - Node lastNode = findNode(size); + Node lastNode = findNode(size-1); lastNode.next = node; } size++; @@ -28,8 +28,8 @@ public void add(Object o) { private Node findNode(int index) { Node no = head; - for (; index - 1 > 0; index--) - no = no.next; + for (; index > 0; index--) + no = no.next; return no; } @@ -71,16 +71,15 @@ public Object remove(int index) { } Node nextNode = null; Node lastNode = null; - if (index + 1 < size - 1) + if (index + 1 <= size - 1) //鍒ゆ柇鏄惁鏈変笅涓涓 nextNode = findNode(index + 1); - if (index - 1 > 0) + if (index - 1 > 0) //鍒ゆ柇鏄惁鏈変笂涓涓 lastNode = findNode(index - 1); if (lastNode == null) { head = nextNode; size--; return indexNode.data; - } - if (nextNode == null) { + }else if (nextNode == null) { lastNode.next = null; size--; return indexNode.data; @@ -116,7 +115,7 @@ public void addLast(Object o) { size++; return; } else { - Node lastNode = findNode(size); + Node lastNode = findNode(size-1); lastNode.next = node; size++; return; @@ -139,9 +138,11 @@ public Object removeLast() { if (size == 0) { return null; } else { - Node node = findNode(size); - Node lastNode = findNode(size - 1); - lastNode.next = null; + Node node = findNode(size-1); + if(size-2>=0){ + Node lastNode = findNode(size - 2); + lastNode.next = null; + } size--; return node.data; } @@ -154,7 +155,12 @@ public Iterator iterator() { public boolean hasNext() { index++; - return findNode(index) != null; + if(indexelementData.size()? null:elementData.get(elementData.size() - index); + if(ob==null){ + index = 0; + } + return ob; + } } public boolean isEmpty() { diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/StackTest.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/StackTest.java new file mode 100644 index 0000000000..7cea2aa985 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/StackTest.java @@ -0,0 +1,88 @@ +package com.github.mrwengq.first; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + + @Test + public void testPush() { + Stack st =new Stack(); + st.push("1"); + st.push("2"); + st.push("3"); + st.push("4"); + st.push("5"); + + } + + @Test + public void testPop() { + Stack st =new Stack(); + st.push("1"); + st.push("2"); + st.push("3"); + st.push("4"); + st.push("5"); + assertEquals(st.pop(), "5"); + assertEquals(st.pop(), "4"); + assertEquals(st.pop(), "3"); + assertEquals(st.pop(), "2"); + assertEquals(st.pop(), "1"); + assertEquals(st.isEmpty(),true); + } + + @Test + public void testPeek() { + Stack st =new Stack(); + st.push("1"); + st.push("2"); + st.push("3"); + st.push("4"); + st.push("5"); + assertEquals(st.peek(), "5"); + assertEquals(st.peek(), "4"); + assertEquals(st.peek(), "3"); + assertEquals(st.peek(), "2"); + assertEquals(st.peek(), "1"); + assertEquals(st.isEmpty(),false); + } + + @Test + public void testIsEmpty() { + Stack st =new Stack(); + assertEquals(st.isEmpty(),true); + st.push("1"); + st.push("2"); + st.push("3"); + st.push("4"); + st.push("5"); + assertEquals(st.isEmpty(),false); + + } + + @Test + public void testSize() { + Stack st =new Stack(); + assertEquals(st.size(),0); + st.push("1"); + st.push("2"); + st.push("3"); + st.push("4"); + st.push("5"); + assertEquals(st.size(),5); + } + +} From e448e2538cb1dfa65934b563e9627df935d4cc7a Mon Sep 17 00:00:00 2001 From: wenGQ Date: Sun, 12 Mar 2017 14:04:00 +0800 Subject: [PATCH 046/155] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../src/\346\226\207\347\253\240\345\234\260\345\235\200.txt" | 0 2 files changed, 1 insertion(+) create mode 100644 "group22/1193590951/githubitem/src/\346\226\207\347\253\240\345\234\260\345\235\200.txt" diff --git a/.gitignore b/.gitignore index 3d01d810e2..793b2e270c 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ target *.classpath group22/1193590951/githubitem/.project *.prefs +group22/1193590951/githubitem/bin/鏂囩珷鍦板潃.txt diff --git "a/group22/1193590951/githubitem/src/\346\226\207\347\253\240\345\234\260\345\235\200.txt" "b/group22/1193590951/githubitem/src/\346\226\207\347\253\240\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..e69de29bb2 From ddf87e3bafe3068d50e9b32fc5ef2b541015422f Mon Sep 17 00:00:00 2001 From: wenGQ Date: Sun, 12 Mar 2017 14:19:35 +0800 Subject: [PATCH 047/155] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/mrwengq/first/ArrayList.java | 14 +++---- .../com/github/mrwengq/first/LinkedList.java | 42 +++++++++---------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java index 0974b8e1dc..d9d8356b67 100644 --- a/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java @@ -70,21 +70,21 @@ public Object next() { }; } - private Object[] copyAddArray(Object elementData[]) { + private Object[] copyAddArray(Object elementData[]) { //瀵规暟缁勬墿瀹 澧炲姞閲忎负鍘熼暱搴3/4 Object ob[] = new Object[elementData.length+(elementData.length * 3) / 4]; System.arraycopy(((Object) (elementData)), 0, ((Object) (ob)), 0, elementData.length); return ob; } - private Object[] addUpdateArray(Object elementData[], int index) { - Object temp = null; + private Object[] addUpdateArray(Object elementData[], int index) {//娣诲姞鏃朵慨鏀规暟缁勭储寮 + Object temp = null; //涓棿鍙橀噺 for (int i = 0; i < size; i++) - if (i > index) { + if (i > index) {//鍒ゆ柇鍙楀奖鍝嶇储寮 temp = elementData[index]; elementData[index] = elementData[i]; elementData[i] = temp; - if (i == size - 1) { + if (i == size - 1) { //鍒ゆ柇涓烘渶鍚庝竴浣 if (size > elementData.length) elementData = copyAddArray(elementData); elementData[size] = elementData[index]; @@ -94,10 +94,10 @@ private Object[] addUpdateArray(Object elementData[], int index) { return elementData; } - private void delUpdateArray(Object elementData[], int index) { + private void delUpdateArray(Object elementData[], int index) {//鍒犻櫎鏃朵慨鏀圭储寮 for (int i = 0; i < size; i++){ - if (i > index && i < size ){ + if (i > index && i < size ){//鍒ゆ柇鍙楀奖鍝嶇储寮 elementData[i - 1] = elementData[i]; if (i == size - 1){ elementData[i] = null; diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java index 846634287e..1dc4dd4342 100644 --- a/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/first/LinkedList.java @@ -71,9 +71,9 @@ public Object remove(int index) { } Node nextNode = null; Node lastNode = null; - if (index + 1 <= size - 1) //鍒ゆ柇鏄惁鏈変笅涓涓 + if (index + 1 <= size - 1) //鍒ゆ柇鏄惁鏈変笅涓浣 nextNode = findNode(index + 1); - if (index - 1 > 0) //鍒ゆ柇鏄惁鏈変笂涓涓 + if (index - 1 > 0) //鍒ゆ柇鏄惁鏈変笂涓浣 lastNode = findNode(index - 1); if (lastNode == null) { head = nextNode; @@ -138,7 +138,7 @@ public Object removeLast() { if (size == 0) { return null; } else { - Node node = findNode(size-1); + Node node = findNode(size-1); //size -1 涓烘渶鍚庝竴浣 -2涓哄墠涓浣 if(size-2>=0){ Node lastNode = findNode(size - 2); lastNode.next = null; @@ -172,17 +172,17 @@ public Object next() { } /** - * 鎶婅閾捐〃閫嗙疆 - * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + * 閹跺﹨顕氶柧鎹愩冮柅鍡欑枂 + * 娓氬顩ч柧鎹愩冩稉锟3->7->10 , 闁棛鐤嗛崥搴″綁娑擄拷 10->7->3 */ public void reverse(){ } /** - * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 - * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 - * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + * 閸掔娀娅庢稉锟介嚋閸楁洟鎽肩悰銊ф畱閸撳秴宕愰柈銊ュ瀻 + * 娓氬顩ч敍姝璱st = 2->5->7->8 , 閸掔娀娅庢禒銉ユ倵閻ㄥ嫬锟芥稉锟7->8 + * 婵″倹鐏塴ist = 2->5->7->8->10 ,閸掔娀娅庢禒銉ユ倵閻ㄥ嫬锟芥稉锟,8,10 */ public void removeFirstHalf(){ @@ -190,7 +190,7 @@ public void removeFirstHalf(){ } /** - * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * 娴犲海顑噄娑擃亜鍘撶槐鐘茬磻婵绱 閸掔娀娅巐ength 娑擃亜鍘撶槐锟介敍锟藉▔銊﹀壈i娴狅拷瀵拷顫 * @param i * @param length */ @@ -198,11 +198,11 @@ public void remove(int i, int length){ } /** - * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 - * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 - * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * 閸嬪洤鐣捐ぐ鎾冲闁炬崘銆冮崪瀹璱st閸у洤瀵橀崥顐㈠嚒閸楀洤绨幒鎺戝灙閻ㄥ嫭鏆i弫锟 + * 娴犲骸缍嬮崜宥夋懠鐞涖劋鑵戦崣鏍у毉闁絼绨簂ist閹碉拷瀵氱规氨娈戦崗鍐 + * 娓氬顩цぐ鎾冲闁炬崘銆 = 11->101->201->301->401->501->601->701 * listB = 1->3->4->6 - * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * 鏉╂柨娲栭惃鍕波閺嬫粌绨茬拠銉︽Ц[101,301,401,601] * @param list */ public static int[] getElements(LinkedList list){ @@ -210,8 +210,8 @@ public static int[] getElements(LinkedList list){ } /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * 瀹歌尙鐓¢柧鎹愩冩稉顓犳畱閸忓啰绀屾禒銉ワ拷闁帒顤冮張澶婄碍閹烘帒鍨敍灞借嫙娴犮儱宕熼柧鎹愩冩担婊冪摠閸屻劎绮ㄩ弸鍕╋拷 + * 娴犲骸缍嬮崜宥夋懠鐞涖劋鑵戞稉顓炲灩闂勩倕婀猯ist娑擃厼鍤悳鎵畱閸忓啰绀 * @param list */ @@ -221,16 +221,16 @@ public void subtract(LinkedList list){ } /** - * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + * 瀹歌尙鐓¤ぐ鎾冲闁炬崘銆冩稉顓犳畱閸忓啰绀屾禒銉ワ拷闁帒顤冮張澶婄碍閹烘帒鍨敍灞借嫙娴犮儱宕熼柧鎹愩冩担婊冪摠閸屻劎绮ㄩ弸鍕╋拷 + * 閸掔娀娅庣悰銊よ厬閹碉拷婀侀崐鑲╂祲閸氬瞼娈戞径姘稇閸忓啰绀岄敍鍫滃▏瀵版鎼锋担婊冩倵閻ㄥ嫮鍤庨幀褑銆冩稉顓熷閺堝鍘撶槐鐘垫畱閸婄厧娼庢稉宥囨祲閸氬矉绱 */ public void removeDuplicateValues(){ } /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * 瀹歌尙鐓¢柧鎹愩冩稉顓犳畱閸忓啰绀屾禒銉ワ拷闁帒顤冮張澶婄碍閹烘帒鍨敍灞借嫙娴犮儱宕熼柧鎹愩冩担婊冪摠閸屻劎绮ㄩ弸鍕╋拷 + * 鐠囨洖鍟撴稉锟界彯閺佸牏娈戠粻妤佺《閿涘苯鍨归梽銈堛冩稉顓熷閺堝锟芥径褌绨琺in娑撴柨鐨禍宸慳x閻ㄥ嫬鍘撶槐鐙呯礄閼汇儴銆冩稉顓炵摠閸︺劏绻栭弽椋庢畱閸忓啰绀岄敍锟 * @param min * @param max */ @@ -239,8 +239,8 @@ public void removeRange(int min, int max){ } /** - * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 - * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * 閸嬪洩顔曡ぐ鎾冲闁炬崘銆冮崪灞藉棘閺佺櫦ist閹稿洤鐣鹃惃鍕懠鐞涖劌娼庢禒銉ュ帗缁辩姳绶烽崐濂革拷婢х偞婀佹惔蹇斿笓閸掓绱欓崥灞肩鐞涖劋鑵戦惃鍕帗缁辩姴锟介崥鍕瑝閻╃ǹ鎮撻敍锟 + * 閻滄媽顩﹀Ч鍌滄晸閹存劖鏌婇柧鎹愩僀閿涘苯鍙鹃崗鍐娑撳搫缍嬮崜宥夋懠鐞涖劌鎷發ist娑擃厼鍘撶槐鐘垫畱娴溿倝娉﹂敍灞肩瑬鐞涒撴稉顓犳畱閸忓啰绀岄張澶夌贩閸婂ジ锟芥晶鐐存箒鎼村繑甯撻崚锟 * @param list */ public LinkedList intersection( LinkedList list){ From 038541c90114f204558447b8cbed410eb29013ba Mon Sep 17 00:00:00 2001 From: chaoszcy <15651180283@163.com> Date: Sun, 12 Mar 2017 15:17:41 +0800 Subject: [PATCH 048/155] homework --- group22/592864103/ArrayList.java | 123 +++++++++++++ group22/592864103/BinaryTreeNode.java | 59 ++++++ group22/592864103/Iterator.java | 7 + group22/592864103/LinkedList.java | 249 ++++++++++++++++++++++++++ group22/592864103/List.java | 9 + group22/592864103/Queue.java | 34 ++++ group22/592864103/Stack.java | 22 +++ 7 files changed, 503 insertions(+) create mode 100644 group22/592864103/ArrayList.java create mode 100644 group22/592864103/BinaryTreeNode.java create mode 100644 group22/592864103/Iterator.java create mode 100644 group22/592864103/LinkedList.java create mode 100644 group22/592864103/List.java create mode 100644 group22/592864103/Queue.java create mode 100644 group22/592864103/Stack.java diff --git a/group22/592864103/ArrayList.java b/group22/592864103/ArrayList.java new file mode 100644 index 0000000000..cf716f728e --- /dev/null +++ b/group22/592864103/ArrayList.java @@ -0,0 +1,123 @@ +import java.util.Arrays; +//import java.util.Objects; + +public class ArrayList implements List { + + //private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (o == null) { + System.out.println("浼犲叆瀵硅薄涓嶈兘涓虹┖锛"); + return; + } else { + if (size() == elementData.length) { + Object[] elementData2 = Arrays.copyOf(elementData, ((int) (elementData.length * 1.2))); + elementData2[size()] = o; + elementData = elementData2; + } else { + elementData[size()] = o; + } + } + } + + public void add(int index, Object o) { + if (o == null) { + System.out.println("浼犲叆瀵硅薄涓嶈兘涓虹┖锛"); + return; + } + if ((index > size())) { + System.out.println("瓒呭嚭鏁扮粍闀垮害锛"); + return; + } else { + if (size() == elementData.length) { + Object[] elementData2 = Arrays.copyOf(elementData, ((int) (elementData.length * 1.2))); + rightShift(elementData2, index); + elementData2[index] = o; + elementData = elementData2; + return; + } else { + rightShift(elementData, index); + elementData[size()] = o; + return; + } + } + } + + public Object get(int index) { + if ((index > size() - 1)) { + System.out.println("瓒呭嚭鏁扮粍闀垮害锛"); + return null; + } else { + return elementData[index]; + } + } + + public Object remove(int index) { + if (index > size() - 1) { + System.out.println("瓒呭嚭鏁扮粍闀垮害锛"); + return null; + }else{ + Object o = elementData[index]; + elementData[index]=null; + leftShift(elementData,index); + return o; + } + } + + public int size() { + int n; + for (n = 0; n < elementData.length; n++) { + if (elementData[n] == null) { + break; + } + } + return n; + } + + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + public class ArrayListIterator implements Iterator{ + ArrayList list = new ArrayList(); + int pos = 0; + ArrayListIterator(ArrayList list){ + this.list = list; + } + + @Override + public boolean hasNext() { + if (pos index; k--) { + o[k] = o[k - 1]; + } + } + + public void leftShift(Object[] o, int index) { + for (int k = index; k < size(); k++) { + o[k] = o[k + 1]; + } + } +} diff --git a/group22/592864103/BinaryTreeNode.java b/group22/592864103/BinaryTreeNode.java new file mode 100644 index 0000000000..b6aa3100b6 --- /dev/null +++ b/group22/592864103/BinaryTreeNode.java @@ -0,0 +1,59 @@ +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Integer getData() { + return data; + } + public void setData(Integer data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Integer o){ + if(o>getData()){ + if(this.right==null){ + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + node.left = null; + node.right = null; + this.right = node; + return node; + }else{ + getRight(); + insert(o); + return null; + } + }else if(o size())) { + System.out.println("瓒呭嚭鏁扮粍闀垮害锛"); + return; + } else if (index == 0) { + Node temp = new Node(); + temp.data = head.data; + temp.next = head.next; + head.data = o; + head.next = temp; + } else { + Node cursor = head; + Node temp = new Node(); + for (int i = 0; i < index - 1; i++) { + cursor = cursor.next; + } + temp.data = o; + temp.next = cursor.next; + cursor.next = temp; + } + } + + public Object get(int index) { + if ((index > size())) { + System.out.println("瓒呭嚭鏁扮粍闀垮害锛"); + return null; + } else { + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor; + } + } + + public Object remove(int index) { + if ((index > size())) { + System.out.println("瓒呭嚭鏁扮粍闀垮害锛"); + return null; + } else if (index == 0) { + Node temp = new Node(); + temp.data = head.data; + temp.next = head.next; + head = head.next; + return temp; + } else { + Node cursor = head; + for (int i = 0; i < index - 1; i++) { + cursor = cursor.next; + } + Node temp = new Node(); + Node target = cursor.next; + temp.data = target.data; + temp.next = target.next; + cursor.next = target.next; + return temp; + } + } + + + public int size() { + Node cursor = head; + int size = 0; + while (cursor != null) { + cursor = cursor.next; + size++; + } + return size; + } + + public void addFirst(Object o) { + Node temp = new Node(); + temp.data = head.data; + temp.next = head.next; + head.data = o; + head.next = temp; + } + + public void addLast(Object o) { + if (o == null) { + System.out.println("浼犲叆瀵硅薄涓嶈兘涓虹┖锛"); + } else if (head == null) { + head.data = o; + head.next = null; + } else { + Node cursor = head; + for (int i = 0; i < size(); i++) { + cursor = cursor.next; + } + cursor.data = o; + cursor.next = null; + } + } + + public Object removeFirst() { + Node temp = new Node(); + temp.data = head.data; + temp.next = head.next; + head = head.next; + return temp; + } + + public Object removeLast() { + Node temp = new Node(); + Node cursor = head; + for (int i = 0; i < size() - 2; i++) { + cursor = cursor.next; + } + Node last = cursor.next; + temp.data = last.data; + temp.next = null; + cursor.next = null; + return temp; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + public class LinkedListIterator implements Iterator{ + int pos = 0; + LinkedList list = new LinkedList(); + LinkedListIterator(LinkedList list){ + this.list = list; + } + @Override + public boolean hasNext() { + if (pos7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse() { + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + */ + public void removeFirstHalf() { + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * + * @param list + */ + public static int[] getElements(LinkedList list) { + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues() { + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } +} diff --git a/group22/592864103/List.java b/group22/592864103/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group22/592864103/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +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(); +} diff --git a/group22/592864103/Queue.java b/group22/592864103/Queue.java new file mode 100644 index 0000000000..764d265c1e --- /dev/null +++ b/group22/592864103/Queue.java @@ -0,0 +1,34 @@ +public class Queue { + + LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + if (o == null) { + System.out.println("浼犲叆瀵硅薄涓嶈兘涓虹┖"); + } else { + elementData.add(o); + } + } + + public Object deQueue() { + if (isEmpty()==true){ + System.out.println("璇ラ槦鍒椾负绌洪槦鍒楋紒"); + return null; + }else{ + Object o = elementData.removeFirst(); + return o; + } + } + + public boolean isEmpty() { + if (elementData.size()==0){ + return true; + }else { + return false; + } + } + + public int size() { + return elementData.size(); + } +} diff --git a/group22/592864103/Stack.java b/group22/592864103/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group22/592864103/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} From 36fa18679ad9979375d51a75249eb94df065eef9 Mon Sep 17 00:00:00 2001 From: wenGQ Date: Sun, 12 Mar 2017 15:20:07 +0800 Subject: [PATCH 049/155] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/\346\226\207\347\253\240\345\234\260\345\235\200.txt" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "group22/1193590951/githubitem/src/\346\226\207\347\253\240\345\234\260\345\235\200.txt" diff --git "a/group22/1193590951/githubitem/src/\346\226\207\347\253\240\345\234\260\345\235\200.txt" "b/group22/1193590951/githubitem/src/\346\226\207\347\253\240\345\234\260\345\235\200.txt" deleted file mode 100644 index e69de29bb2..0000000000 From 26177079c59fa620faafa65b27d88c8506cb1cc0 Mon Sep 17 00:00:00 2001 From: Kewy2017 Date: Sun, 12 Mar 2017 15:59:28 +0800 Subject: [PATCH 050/155] Blog for first week MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduction about CPU锛孧emory锛宒isk and instructions --- .../\345\215\232\345\256\242\345\234\260\345\235\200.txt" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "group22/1014331282/\345\215\232\345\256\242\345\234\260\345\235\200.txt" diff --git "a/group22/1014331282/\345\215\232\345\256\242\345\234\260\345\235\200.txt" "b/group22/1014331282/\345\215\232\345\256\242\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..4fba7167a8 --- /dev/null +++ "b/group22/1014331282/\345\215\232\345\256\242\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +http://www.cnblogs.com/kk9518/p/6538126.html \ No newline at end of file From 5fba770f425477461b4a53cddc1f22fbd1c8f7c5 Mon Sep 17 00:00:00 2001 From: Fred Qin Date: Sun, 12 Mar 2017 00:29:28 -0800 Subject: [PATCH 051/155] =?UTF-8?q?downloader=20=E5=A4=9A=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E4=BB=8D=E7=84=B6=E6=9C=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/395443277/.gitignore | 3 +- group11/395443277/pom.xml | 35 ++++++++ .../coderising/download/DownloadThread.java | 29 +++++-- .../coderising/download/FileDownloader.java | 80 ++++++++++++------- .../download/FileDownloaderTest.java | 4 +- .../coderising/download/api/Connection.java | 2 +- .../download/impl/ConnectionImpl.java | 45 +++++++++-- .../download/impl/ConnectionManagerImpl.java | 4 +- .../src/com/coderising/litestruts/struts.xml | 16 ++-- 9 files changed, 160 insertions(+), 58 deletions(-) create mode 100644 group11/395443277/pom.xml diff --git a/group11/395443277/.gitignore b/group11/395443277/.gitignore index e02fdb6730..ad2c4db410 100644 --- a/group11/395443277/.gitignore +++ b/group11/395443277/.gitignore @@ -108,4 +108,5 @@ local.properties .scala_dependencies -.worksheet \ No newline at end of file +.worksheet +/target/ diff --git a/group11/395443277/pom.xml b/group11/395443277/pom.xml new file mode 100644 index 0000000000..becb81d519 --- /dev/null +++ b/group11/395443277/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + 2017Learning + 2017Learning + 0.0.1-SNAPSHOT + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + + + com.google.guava + guava + 21.0 + + + \ No newline at end of file diff --git a/group11/395443277/src/com/coderising/download/DownloadThread.java b/group11/395443277/src/com/coderising/download/DownloadThread.java index 900a3ad358..8428d2ada8 100644 --- a/group11/395443277/src/com/coderising/download/DownloadThread.java +++ b/group11/395443277/src/com/coderising/download/DownloadThread.java @@ -1,20 +1,35 @@ package com.coderising.download; -import com.coderising.download.api.Connection; +import java.io.IOException; +import java.io.RandomAccessFile; -public class DownloadThread extends Thread{ +import com.coderising.download.api.Connection; +public class DownloadThread extends Thread { Connection conn; int startPos; int endPos; + String filePath; - public DownloadThread( Connection conn, int startPos, int endPos){ - - this.conn = conn; + public DownloadThread(Connection conn, int startPos, int endPos, String filePath) { + this.conn = conn; this.startPos = startPos; this.endPos = endPos; + this.filePath = filePath; } - public void run(){ - + + public void run() { + try { + synchronized (filePath.getClass()) { + byte[] bytes = conn.read(startPos, endPos); + RandomAccessFile RAFile = new RandomAccessFile(filePath, "rw"); + RAFile.seek(startPos); + RAFile.write(bytes, 0, bytes.length); + RAFile.close(); + } + + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/group11/395443277/src/com/coderising/download/FileDownloader.java b/group11/395443277/src/com/coderising/download/FileDownloader.java index c3c8a3f27d..7cd70f4b81 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloader.java +++ b/group11/395443277/src/com/coderising/download/FileDownloader.java @@ -1,73 +1,91 @@ package com.coderising.download; +import java.io.File; +import java.io.RandomAccessFile; + import com.coderising.download.api.Connection; import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; import com.coderising.download.api.DownloadListener; - public class FileDownloader { - + String url; - + DownloadListener listener; - + ConnectionManager cm; - public FileDownloader(String _url) { this.url = _url; - } - - public void execute(){ + + public void execute() { // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 - // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, + // endPos鏉ユ寚瀹氾級 // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 - // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 // 鍏蜂綋鐨勫疄鐜版濊矾锛 - // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 - // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 + // 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 - + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; try { - conn = cm.open(this.url); + + int length = conn.getContentLength(); + + // Separate length to 3 + int partLen = (int) Math.ceil(length / 3); + + // create a file + String filePath = "D://java_learning//test.jpg"; + + // create three threads +// DownloadThread t1 = new DownloadThread(cm.open(this.url), 0, partLen - 1, filePath); +// t1.start(); +// t1.join(); +// +// DownloadThread t2 = new DownloadThread(cm.open(this.url), partLen, partLen * 2 - 1, filePath); +// t2.start(); +// t2.join(); +// +// DownloadThread t3 =new DownloadThread(cm.open(this.url), partLen * 2, length - 1, filePath); +// t3.start(); +// t3.join(); + + new DownloadThread(cm.open(this.url), 0, length-1, filePath).start(); - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).start(); +// getListener().notifyFinished(); - } catch (ConnectionException e) { + } catch (ConnectionException e) { e.printStackTrace(); - }finally{ - if(conn != null){ + } finally { + if (conn != null) { conn.close(); } } - - - - + } - + public void setListener(DownloadListener listener) { this.listener = listener; } - - - public void setConnectionManager(ConnectionManager ucm){ + public void setConnectionManager(ConnectionManager ucm) { this.cm = ucm; } - - public DownloadListener getListener(){ + + public DownloadListener getListener() { return this.listener; } - + } diff --git a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java index 4ff7f46ae0..7e4d8d789d 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java +++ b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java @@ -21,7 +21,9 @@ public void tearDown() throws Exception { @Test public void testDownload() { - String url = "http://localhost:8080/test.jpg"; +// String url = "https://s-media-cache-ak0.pinimg.com/564x/8e/bd/00/8ebd00b1f2ef862b6c80d57c2b45d129.jpg"; + // http://wallpapercraze.com/images/wallpapers/dota2_nevermore_w1.jpeg + String url = "https://i.ytimg.com/vi/xMV2s5f3f0E/hqdefault.jpg"; FileDownloader downloader = new FileDownloader(url); diff --git a/group11/395443277/src/com/coderising/download/api/Connection.java b/group11/395443277/src/com/coderising/download/api/Connection.java index 0957eaf7f4..5443fca61e 100644 --- a/group11/395443277/src/com/coderising/download/api/Connection.java +++ b/group11/395443277/src/com/coderising/download/api/Connection.java @@ -2,7 +2,7 @@ import java.io.IOException; -public interface Connection { +public interface Connection{ /** * 缁欏畾寮濮嬪拰缁撴潫浣嶇疆锛 璇诲彇鏁版嵁锛 杩斿洖鍊兼槸瀛楄妭鏁扮粍 * @param startPos 寮濮嬩綅缃紝 浠0寮濮 diff --git a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java index 36a9d2ce15..cde4246282 100644 --- a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java @@ -1,27 +1,58 @@ package com.coderising.download.impl; import java.io.IOException; - +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; import com.coderising.download.api.Connection; -public class ConnectionImpl implements Connection{ +public class ConnectionImpl implements Connection { + HttpURLConnection urlcon = null; + InputStream is; + + public ConnectionImpl(String url) { + try { + URL imgUrl = new URL(url); + urlcon = (HttpURLConnection) imgUrl.openConnection(); + is = urlcon.getInputStream(); + } catch (Exception e) { + System.out.println(e); + } + } @Override public byte[] read(int startPos, int endPos) throws IOException { - - return null; + byte[] bytes = new byte[endPos - startPos + 1]; + + // Read in the bytes + int offset = startPos; + int numRead = 0; + while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { + offset += numRead; + } + + return bytes; } @Override public int getContentLength() { - + String contentLength = urlcon.getHeaderField("content-Length"); + + if (contentLength != null) { + return Integer.parseInt(contentLength); + } + return 0; } @Override public void close() { - - + try { + is.close(); + System.out.println("one connection is closed"); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/group11/395443277/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group11/395443277/src/com/coderising/download/impl/ConnectionManagerImpl.java index 172371dd55..21ae4b4f08 100644 --- a/group11/395443277/src/com/coderising/download/impl/ConnectionManagerImpl.java +++ b/group11/395443277/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -8,8 +8,8 @@ public class ConnectionManagerImpl implements ConnectionManager { @Override public Connection open(String url) throws ConnectionException { - - return null; + Connection cn = new ConnectionImpl(url); + return cn; } } diff --git a/group11/395443277/src/com/coderising/litestruts/struts.xml b/group11/395443277/src/com/coderising/litestruts/struts.xml index 7f8d558286..1b5028b0bd 100644 --- a/group11/395443277/src/com/coderising/litestruts/struts.xml +++ b/group11/395443277/src/com/coderising/litestruts/struts.xml @@ -1,11 +1,11 @@ - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + \ No newline at end of file From 900ab47bcba5deb10925c528e5fbd528865a4fa4 Mon Sep 17 00:00:00 2001 From: GZ-RXP <283091182@qq.com> Date: Sun, 12 Mar 2017 18:00:30 +0800 Subject: [PATCH 052/155] 20170305 Assignment- LinkedList and MultiThreadDownload --- .../coderising/download/DownloadThread.java | 46 ++ .../coderising/download/FileDownloader.java | 137 +++++ .../download/FileDownloaderTest.java | 59 ++ .../coderising/download/api/Connection.java | 23 + .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 74 +++ .../download/impl/ConnectionManagerImpl.java | 14 + .../src/com/coding/basic2/Iterator.java | 7 + .../src/com/coding/basic2/LinkedList.java | 541 ++++++++++++++++++ .../src/com/coding/basic2/LinkedListTest.java | 368 ++++++++++++ .../283091182/src/com/coding/basic2/List.java | 9 + 13 files changed, 1298 insertions(+) create mode 100644 group11/283091182/src/com/coderising/download/DownloadThread.java create mode 100644 group11/283091182/src/com/coderising/download/FileDownloader.java create mode 100644 group11/283091182/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group11/283091182/src/com/coderising/download/api/Connection.java create mode 100644 group11/283091182/src/com/coderising/download/api/ConnectionException.java create mode 100644 group11/283091182/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group11/283091182/src/com/coderising/download/api/DownloadListener.java create mode 100644 group11/283091182/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group11/283091182/src/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 group11/283091182/src/com/coding/basic2/Iterator.java create mode 100644 group11/283091182/src/com/coding/basic2/LinkedList.java create mode 100644 group11/283091182/src/com/coding/basic2/LinkedListTest.java create mode 100644 group11/283091182/src/com/coding/basic2/List.java diff --git a/group11/283091182/src/com/coderising/download/DownloadThread.java b/group11/283091182/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..f8533373a4 --- /dev/null +++ b/group11/283091182/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,46 @@ +package com.coderising.download; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + String filePath = null; + private boolean finished = false; + + public boolean isFinished() { + return finished; + } + public DownloadThread( Connection conn, int startPos, int endPos,String filePath){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.filePath = filePath; + } + public void run(){ + File f = new File(this.filePath); + + try { + if(!f.exists()){ + f.createNewFile(); + } + RandomAccessFile raf = new RandomAccessFile(f, "rw"); + byte[] ba = conn.read(startPos, endPos); + System.out.println("["+this.getName()+"]ByteArray length="+ba.length+",endPos-startPos+1="+(endPos-startPos+1)); + raf.seek(startPos); + raf.write(ba, 0, endPos-startPos+1); + finished = true; + System.out.println("["+this.getName()+"]DownloadThread-startPos="+startPos+"-endPos="+endPos+":download completed"); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/group11/283091182/src/com/coderising/download/FileDownloader.java b/group11/283091182/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..0deb9a3cc4 --- /dev/null +++ b/group11/283091182/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,137 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + private static int DOWNLOAD_SIZE_PER_THREAD = 1024*1000; + + private String destFilePath; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public FileDownloader(String _url,String destFilePath) { + this.url = _url; + this.destFilePath = destFilePath; + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + System.out.println("GetLength ="+length); + + File dest = new File(destFilePath); + dest.createNewFile(); + //fill the file with byte array to ensure the size +// byte[] ba = new byte[length]; +// FileOutputStream fos = new FileOutputStream(dest); +// fos.write(ba); +// fos.flush(); +// fos.close(); + + ArrayList threadPool = new ArrayList(); + int numOfThread = length/DOWNLOAD_SIZE_PER_THREAD; + if(length%DOWNLOAD_SIZE_PER_THREAD!=0) + { + numOfThread++; + } + + for(int i=0;ilength-1) + { + endPos = length-1; + } + DownloadThread t = new DownloadThread(cm.open(url),startPos,endPos,destFilePath); + t.setName("downloadThread_"+i); + System.out.println("Initializing Thread:"+t.getName()); + threadPool.add(t); + t.start(); + } + //pooling + boolean flag; + do{ + Iterator it = threadPool.iterator(); + flag=true; + DownloadThread t; + while(it.hasNext()){ + t = it.next(); + System.out.println("Thread ="+t.getName()+" has completed?"+t.isFinished()); + flag = flag && t.isFinished(); + } + if(!flag){ + System.out.println("=====================AllCompleted="+flag); + Thread.sleep(10*1000); + continue; + } + }while(!flag); + + } catch (Exception e) { + throw new RuntimeException("Error occured during download,",e); + }finally{ + if(conn != null){ + conn.close(); + } + } + getListener().notifyFinished(); + } + + 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/group11/283091182/src/com/coderising/download/FileDownloaderTest.java b/group11/283091182/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..583f3cb7d8 --- /dev/null +++ b/group11/283091182/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://download.theworld.cn/ver/TWInst_7.0.0.108.exe";//"http://src.onlinedown.net/Public/images/bigsoftimg/120000/q113222.jpg";// + String dest = "D:\\bt\\JavaDownload_TWInst_7.0.0.108.exe"; + FileDownloader downloader = new FileDownloader(url,dest); + + + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group11/283091182/src/com/coderising/download/api/Connection.java b/group11/283091182/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..9710e270e1 --- /dev/null +++ b/group11/283091182/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +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(); +} diff --git a/group11/283091182/src/com/coderising/download/api/ConnectionException.java b/group11/283091182/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..8dbfe95dda --- /dev/null +++ b/group11/283091182/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group11/283091182/src/com/coderising/download/api/ConnectionManager.java b/group11/283091182/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..fb44ede457 --- /dev/null +++ b/group11/283091182/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group11/283091182/src/com/coderising/download/api/DownloadListener.java b/group11/283091182/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..4cd0b3eab1 --- /dev/null +++ b/group11/283091182/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group11/283091182/src/com/coderising/download/impl/ConnectionImpl.java b/group11/283091182/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..8750ac6b8d --- /dev/null +++ b/group11/283091182/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,74 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private URL url; + + private int length; + + private InputStream is; + + public ConnectionImpl(String url){ + try { + this.url = new URL(url); + this.length = getConnection(this.url).getContentLength(); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Error opening connection.",e); + } + } + + private HttpURLConnection getConnection(URL url) throws MalformedURLException, IOException, ProtocolException { + HttpURLConnection conn = (HttpURLConnection)this.url.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(30*1000); + return conn; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + byte[] buffer = new byte[1024]; + int tempLen = 0; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + HttpURLConnection conn = getConnection(url); + conn.setRequestProperty("Range", "bytes="+startPos+"-"+endPos); + this.is = conn.getInputStream(); + + while((tempLen = this.is.read(buffer))!=-1){ + baos.write(buffer, 0, tempLen); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + return this.length; + } + + @Override + public void close() { + if(this.is!=null) + { + try { + this.is.close(); + this.url = null; + } catch (IOException e) { + // Safe to ignore + e.printStackTrace(); + } + + } + + } + +} diff --git a/group11/283091182/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group11/283091182/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..2d6768697e --- /dev/null +++ b/group11/283091182/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} diff --git a/group11/283091182/src/com/coding/basic2/Iterator.java b/group11/283091182/src/com/coding/basic2/Iterator.java new file mode 100644 index 0000000000..25895dc589 --- /dev/null +++ b/group11/283091182/src/com/coding/basic2/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic2; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group11/283091182/src/com/coding/basic2/LinkedList.java b/group11/283091182/src/com/coding/basic2/LinkedList.java new file mode 100644 index 0000000000..4dd531e41b --- /dev/null +++ b/group11/283091182/src/com/coding/basic2/LinkedList.java @@ -0,0 +1,541 @@ +package com.coding.basic2; + +import static org.junit.Assert.assertEquals; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size=0; + + private void checkIndex(int index){ + if(index<0 || index>=this.size) + { + throw new IndexOutOfBoundsException("Error!Invalid index:"+index); + } + } + + private void checkSize(){ + if(size==0) + { + throw new RuntimeException("Empty LinkedList."); + } + } + + public void add(Object o){ + Node temp = new Node(o,null); + if(this.head==null){ + this.head = temp; + this.tail = head; + }else{ + this.tail.next = temp; + this.tail = temp; + } + this.size++; + } + + /* (non-Javadoc) + * @see com.coding.basic2.List#add(int, java.lang.Object) + */ + public void add(int index , Object o){ + checkIndex(index); + if(index==0) + { + Node newNode = new Node(o,head); + head = newNode; + }else{ + Node temp = head; + for(int i=1;i0){ + Node temp = head; + sb.append(temp.data); + while(temp.hasNext()){ + temp = temp.next; + sb.append(","); + sb.append(temp.data); + } + } + return sb.toString(); + } + + public Object remove(int index){ + checkIndex(index); + Node temp = head; + Node pre = head; + Object result; + if(index == 0) + { + result = head.data; + head = head.next; + }else{ + for(int i=0;i0) + { + pre=pre.next; + } + temp=temp.next; + } + result = temp.data; + pre.next=temp.next; + temp = null; + + + if(index == size-1) + { + tail = pre; + } + } + size--; + + return result; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node temp = new Node(o,head); + head = temp; + size++; + + } + public void addLast(Object o){ + Node temp = new Node(o,null); + tail.next = temp; + tail = temp; + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator + { + private Node current = head; + + @Override + public boolean hasNext() + { + return current!=null; + } + + @Override + public Object next() { + if(current==null) + { + throw new RuntimeException("Current element has not next."); + } + + Object result = current.data; + current = current.next; + return result; + } + + } + + + private static class Node{ + Object data; + Node next; + public boolean hasNext(){ + return (this.next!=null); + } + + public Node(Object data,Node next){ + this.data=data; + this.next=next; + } + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + checkSize(); + int tempSize = size; + Node temp = new Node(removeFirst(),null); + tail = temp; + while(size>0){ + temp = new Node(removeFirst(),temp); + } + head = temp; + size = tempSize; + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + checkSize(); + if(size>1) + { + int temp = size; + for(int i=0;isize-i) + { + throw new RuntimeException("No enough size to remove from index:"+i); + }else{ + for(int j=0;j101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if(size==0||list.size==0){ + return new int[0]; + }else{ + int[] result = new int[list.size()]; +//--Method One +// for(int i=0;ival){ + break; + }else if(tempVal==val){ + remove(j); + break; + }else{ + continue; + } + } + } + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + Node temp = head; + while(temp!=null){ + while(temp.hasNext()&&temp.data.equals(temp.next.data)) + { + temp.next = temp.next.next; + size--; + } + temp = temp.next; + } + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + int headVal = (int)head.data; + int tailVal = (int)tail.data; + //if all the values in linkedList fall into the range, clean up; + if(min<=headVal && max>=tailVal) + { + System.out.println("min<=headVal && max>=tailVal"); + head = null; + tail = null; + size = 0; + }else{ + Node preRange = null; + Node sufRange = null; + Node temp = head; + int counter = 0; + while(temp!=null){ + if((int)temp.data=min) + { + preRange = temp; + System.out.println("Found preRange node, val="+temp.data+",next val="+temp.next.data); + } + if((int)temp.data>max){ + sufRange = temp; + System.out.println("Found sufRange node, val="+temp.data+",next val="+(temp.hasNext()?temp.next.data:null)); + break; + } + if((int)temp.data>=min && (int)temp.data<=max) + { + counter++; + } + System.out.println("Counter="+counter); + temp = temp.next; + } + if(min<=headVal){ + head = sufRange; + } + if(max>=tailVal){ + tail = preRange; + } + if(preRange!=null){ + preRange.next = sufRange; + } + size -= counter; + } + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + if(size==0 || list==null || list.size()==0) + { + return new LinkedList(); + }else{ + int pos1=0; + int pos2=0; + LinkedList result = new LinkedList(); + while(pos1val2) + { + if(pos2 0){ + System.arraycopy(elementData, index+1, elementData, index, numMoved); + } + elementData[--size] = null; //缃┖鏈熬鍏冪礌 + return removeparam; + } + + @Override + public int size() { + return size; + } + + /** + * 妫鏌ユ槸鍚﹁秺鐣 + * @param index + */ + public void checkIndex(int index){ + if(index > size || index < 0){ + throw new IndexOutOfBoundsException("current:"+index+" size:"+size); + } + } + + /** + * 鍒ゆ柇褰撳墠瀹归噺鏄惁瓒冲 + * @param minCapacity + */ + private void ensureCapacity(int minCapacity){ + if(minCapacity > elementData.length){ + grow(minCapacity); + } + } + + /** + * 鎵╁ + * @param minCapacity + */ + private void grow(int minCapacity){ + Object [] target = new Object [minCapacity+10]; + System.arraycopy(elementData, 0, target, 0, elementData.length); + elementData = target; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int current = 0; + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + int i = current; + if(current >= size){ + throw new IndexOutOfBoundsException("current:"+current+" size:"+size); + } + current++; + return elementData[i]; + } + + } + + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(elementData, size)); + } + +} diff --git a/group22/1335499238/week01/src/basic/BinaryTreeNode.java b/group22/1335499238/week01/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..92ca37ef18 --- /dev/null +++ b/group22/1335499238/week01/src/basic/BinaryTreeNode.java @@ -0,0 +1,81 @@ +package basic; + +public class BinaryTreeNode >{ + + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T o){ + this.data = o; + this.left = null; + this.right = null; + } + + public Object getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(T o){ + BinaryTreeNode current = this; + BinaryTreeNode addTreeNode = new BinaryTreeNode<>(o); + while(true){ + //濡傛灉浼犲叆鐨勫兼瘮浣嗗墠鑺傜偣鐨勫煎皬 + if(o.compareTo(current.data) < 0){ + if(current.left != null){ + current = current.left; + }else { + current.left = addTreeNode; + break; + } + }else{ + if(current.right != null){ + current = current.right; + }else{ + current.right =addTreeNode; + break; + } + } + } + return addTreeNode; + } + + public LinkedList prevOrder(BinaryTreeNode binaryTreeNode){ + LinkedList linkedList = new LinkedList(); + preOrder(binaryTreeNode, linkedList); + return linkedList; + } + + private void preOrder(BinaryTreeNode binaryTreeNode,LinkedList linkedList){ + if(binaryTreeNode.left != null){ + preOrder(binaryTreeNode.left, linkedList); + + } + linkedList.add(binaryTreeNode.data); + if(binaryTreeNode.right != null){ + preOrder(binaryTreeNode.right, linkedList); + } + } + +} diff --git a/group22/1335499238/week01/src/basic/Iterator.java b/group22/1335499238/week01/src/basic/Iterator.java new file mode 100644 index 0000000000..576b1a4af4 --- /dev/null +++ b/group22/1335499238/week01/src/basic/Iterator.java @@ -0,0 +1,9 @@ +package basic; + +public interface Iterator { + + public boolean hasNext(); + + public Object next(); + +} diff --git a/group22/1335499238/week01/src/basic/LinkedList.java b/group22/1335499238/week01/src/basic/LinkedList.java new file mode 100644 index 0000000000..9af1471bfb --- /dev/null +++ b/group22/1335499238/week01/src/basic/LinkedList.java @@ -0,0 +1,206 @@ +package basic; + + +public class LinkedList implements List{ + + private Node head; + + private int size; + + @Override + public void add(Object o) { + addLast(o); + } + + @Override + public void add(int index, Object o) { + checkIndex(index); + Node current = findByIndex(index); + Node newNode = new Node(o, current); + if(index == 0){ + head = newNode; + }else{ + Node perv = findByIndex(index-1); + perv.next = newNode; + } + size++; + } + + @Override + public Object get(int index) { + checkIndex(index); + return findByIndex(index).data; + } + + @Override + public Object remove(int index) { + Node remove = null; + checkIndex(index); + Node next = findByIndex(index+1); + if(index == 0){ + remove = head; + if(next == null){ + head = null; + }else { + head = next; + } + }else{ + Node perv = findByIndex(index-1); + remove = perv.next; + perv.next = next; + } + size--; + return remove.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o){ + head = new Node(o, head); + size++; + } + + public void addLast(Object o){ + Node nextNode = new Node(o, null); + if(head == null){ + head = nextNode; + }else{ + Node lastNode = findByIndex(size-1); + lastNode.next = nextNode; + } + size++; + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + + int current = 0; + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + Node findByIndex = findByIndex(current); + if(current >= size){ + throw new IndexOutOfBoundsException("current:"+current+" size:"+size); + } + current++; + return findByIndex.data; + } + + } + + private static class Node{ + Object data; + Node next; + + Node(Object data, Node next){ + this.data = data; + this.next = next; + } + } + + private Node findByIndex(int index){ + Node lastNode = head; + for (int i = 0; i < index; i++) { + lastNode = lastNode.next; + } + return lastNode; + } + + private void checkIndex(int index){ + if(index > size || index < 0){ + throw new IndexOutOfBoundsException("current:"+index+" size:"+size); + } + } + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + * + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺listB鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪listB涓嚭鐜扮殑鍏冪礌 + * @param list + */ + + public void subtract(LinkedList list){ + + } + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + +} diff --git a/group22/1335499238/week01/src/basic/List.java b/group22/1335499238/week01/src/basic/List.java new file mode 100644 index 0000000000..82612a4487 --- /dev/null +++ b/group22/1335499238/week01/src/basic/List.java @@ -0,0 +1,15 @@ +package basic; + +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(); + +} diff --git a/group22/1335499238/week01/src/basic/Queue.java b/group22/1335499238/week01/src/basic/Queue.java new file mode 100644 index 0000000000..81eb073387 --- /dev/null +++ b/group22/1335499238/week01/src/basic/Queue.java @@ -0,0 +1,21 @@ +package basic; + +public class Queue { + + private LinkedList linkList = new LinkedList(); + + public void enQueue(Object o){ + linkList.add(o); + } + + public Object deQueue(){ + return linkList.removeFirst(); + } + + public boolean isEmpty(){ + return linkList.size() == 0; + } + public int size(){ + return linkList.size(); + } +} diff --git a/group22/1335499238/week01/src/basic/Stack.java b/group22/1335499238/week01/src/basic/Stack.java new file mode 100644 index 0000000000..38baac269a --- /dev/null +++ b/group22/1335499238/week01/src/basic/Stack.java @@ -0,0 +1,29 @@ +package basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + private int size = 0; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + return elementData.remove(size-1); + } + + public Object peek(){ + return elementData.get(size-1); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group22/1335499238/week01/src/test/ArrayListTest.java b/group22/1335499238/week01/src/test/ArrayListTest.java new file mode 100644 index 0000000000..bf54c307dc --- /dev/null +++ b/group22/1335499238/week01/src/test/ArrayListTest.java @@ -0,0 +1,40 @@ +package test; + + +import org.junit.Assert; +import org.junit.Test; + +import basic.ArrayList; +import basic.Iterator; + +public class ArrayListTest { + + @Test + public void test01(){ + ArrayList arrayList = new ArrayList(); + arrayList.add("612"); + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("5"); + arrayList.add("6"); + Assert.assertEquals("[612, 1, 2, 5, 6]", arrayList.toString()); + + Object remove = arrayList.remove(2); + Assert.assertEquals("2", remove); + + arrayList.add(2, "13"); + Assert.assertEquals("[612, 1, 13, 5, 6]", arrayList.toString()); + + Object object = arrayList.get(2); + Assert.assertEquals("13", object); + + Assert.assertEquals(5, arrayList.size()); + + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next()+" "); + + } + } + +} diff --git a/group22/1335499238/week01/src/test/BinaryTreeNodeTest.java b/group22/1335499238/week01/src/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..18f3b7897b --- /dev/null +++ b/group22/1335499238/week01/src/test/BinaryTreeNodeTest.java @@ -0,0 +1,26 @@ +package test; + +import org.junit.Test; + +import basic.BinaryTreeNode; +import basic.Iterator; +import basic.LinkedList; + +public class BinaryTreeNodeTest { + + @Test + public void test01(){ + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(20); + binaryTreeNode.insert(5); + binaryTreeNode.insert(40); + binaryTreeNode.insert(30); + binaryTreeNode.insert(10); + binaryTreeNode.insert(15); + LinkedList prevOrder = binaryTreeNode.prevOrder(binaryTreeNode); + Iterator iterator = prevOrder.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next()+" "); + } + } + +} diff --git a/group22/1335499238/week01/src/test/LinkedListTest.java b/group22/1335499238/week01/src/test/LinkedListTest.java new file mode 100644 index 0000000000..8a7fa7f444 --- /dev/null +++ b/group22/1335499238/week01/src/test/LinkedListTest.java @@ -0,0 +1,52 @@ +package test; + +import org.junit.Assert; +import org.junit.Test; + +import basic.Iterator; +import basic.LinkedList; + +public class LinkedListTest { + + @Test + public void test01(){ + LinkedList linkedList = new LinkedList(); + linkedList.add(122); + linkedList.add("qwe"); + linkedList.add(133); + iterator(linkedList); + + linkedList.add(1, "asd"); + iterator(linkedList); + + linkedList.addFirst("1"); + iterator(linkedList); + + linkedList.addLast("zxc"); + iterator(linkedList); + + Object remove = linkedList.remove(2); + Assert.assertEquals("asd", remove); + + Object removeFirst = linkedList.removeFirst(); + Assert.assertEquals("1", removeFirst); + + Object removeLast = linkedList.removeLast(); + Assert.assertEquals("zxc", removeLast); + + int size = linkedList.size(); + Assert.assertEquals(3, size); + + + + } + + public static void iterator(LinkedList linkedList){ + Iterator iterator = linkedList.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next()+" "); + } + System.out.println(); + } + +} diff --git a/group22/1335499238/week01/src/test/QueueTest.java b/group22/1335499238/week01/src/test/QueueTest.java new file mode 100644 index 0000000000..61d4eb91c9 --- /dev/null +++ b/group22/1335499238/week01/src/test/QueueTest.java @@ -0,0 +1,30 @@ +package test; + +import org.junit.Assert; +import org.junit.Test; + +import basic.Queue; + +public class QueueTest { + + @Test + public void test01(){ + + Queue queue = new Queue(); + boolean empty1 = queue.isEmpty(); + Assert.assertEquals(true, empty1); + queue.enQueue("111"); + queue.enQueue("222"); + queue.enQueue("333"); + Object deQueue = queue.deQueue(); + Assert.assertEquals("111", deQueue); + + boolean empty2 = queue.isEmpty(); + Assert.assertEquals(false, empty2); + + int size = queue.size(); + Assert.assertEquals(2, size); + } + + +} diff --git a/group22/1335499238/week01/src/test/StackTest.java b/group22/1335499238/week01/src/test/StackTest.java new file mode 100644 index 0000000000..95c440b8af --- /dev/null +++ b/group22/1335499238/week01/src/test/StackTest.java @@ -0,0 +1,32 @@ +package test; + +import org.junit.Assert; +import org.junit.Test; + +import basic.Stack; + +public class StackTest { + + @Test + public void test1(){ + Stack stack = new Stack(); + + Assert.assertEquals(true, stack.isEmpty()); + + stack.push(123); + stack.push("qwe"); + stack.push(456); + + Assert.assertEquals(false, stack.isEmpty()); + + int size = stack.size(); + Assert.assertEquals(3, size); + + Object peek = stack.peek(); + Assert.assertEquals(456, peek); + + Object pop = stack.pop(); + Assert.assertEquals(456, pop); + } + +} From 46fb00afe02ddb4b9ff503dd190b4f5e1cba1970 Mon Sep 17 00:00:00 2001 From: Yanbo Date: Sun, 12 Mar 2017 19:03:50 +0800 Subject: [PATCH 054/155] =?UTF-8?q?'=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E6=A5=AD'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group23/609041842/src/code/ArrayList.java | 49 ++++++++++++ group23/609041842/src/code/LinkedList.java | 87 ++++++++++++++++++++++ group23/609041842/src/code/Queue.java | 17 +++++ group23/609041842/src/code/Stack.java | 30 ++++++++ 4 files changed, 183 insertions(+) create mode 100644 group23/609041842/src/code/ArrayList.java create mode 100644 group23/609041842/src/code/LinkedList.java create mode 100644 group23/609041842/src/code/Queue.java create mode 100644 group23/609041842/src/code/Stack.java diff --git a/group23/609041842/src/code/ArrayList.java b/group23/609041842/src/code/ArrayList.java new file mode 100644 index 0000000000..e24f37e35a --- /dev/null +++ b/group23/609041842/src/code/ArrayList.java @@ -0,0 +1,49 @@ +package code; + +import java.util.Arrays; + +public class ArrayList { + + private Object[] obj = new Object[0]; + + public void add(Object o) { + Object[] tar = new Object[obj.length + 1]; + System.arraycopy(obj, 0, tar, 0, obj.length); + tar[tar.length - 1] = o; + obj = tar; + System.out.println(Arrays.toString(obj)); + } + + public void add(int index, Object o) { + Object[] tar = new Object[obj.length + 1]; + System.arraycopy(obj, 0, tar, 0, index); + tar[index] = o; + System.arraycopy(obj, index, tar, index + 1, obj.length - index); + obj = tar; + } + + public Object get(int index) { + return obj[index]; + } + + public int size(){ + return obj.length; + } + + public Object remove(int index){ + Object[] tar = new Object[obj.length-1]; + System.arraycopy(obj, 0, tar, 0, index); + System.arraycopy(obj, index+1, tar, index, obj.length-index-1); + Object o = obj[index]; + obj = tar; + return o;//返回被删元素 + } + public static void main(String[] args) { + ArrayList al = new ArrayList(); + al.add("hello"); + al.add("java"); + al.add(2, "addm"); + System.out.println(al.remove(1)); + } + +} diff --git a/group23/609041842/src/code/LinkedList.java b/group23/609041842/src/code/LinkedList.java new file mode 100644 index 0000000000..6f2ac5158f --- /dev/null +++ b/group23/609041842/src/code/LinkedList.java @@ -0,0 +1,87 @@ +package code; + +public class LinkedList { + private static Node head; + private Node last; + public int size; + + public void add(Object o) { + Node l = last; + Node newNode = new Node(l, o, null); + last = newNode; + if (head == null) { + head = newNode; + size = 1; + } else { + l.next = newNode; + size++; + } + } + + public void add(int index, Object o) { + Node n = node(index); + System.out.println(n.data); + Node pred = n.prev; + Node newNode = new Node(pred, o, n); + if (pred == null) { + head = newNode; + } else { + pred.next = newNode; + } + size++; + } + + + public Node get(int index){ + return node(index); + } + public Node node(int index) { + Node n = head; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + public Node remove(int index){ + Node del = node(index); + Node after = del.next; + Node before = del.prev; + before.next = after; + after.prev = before; + size--; + return del; + } + private static class Node { + Node next; + Object data; + Node prev; + + private Node(Node prev, Object data, Node next) { + this.data = data; + this.next = next; + this.prev = prev; + } + } + + public static void main(String[] arg) { + LinkedList ll = new LinkedList(); + ll.add("hello"); + ll.add("java"); + ll.add("jvm"); + ll.add("jvmd"); + // System.out.println(ll.get(2)); +// ll.add(1, "ds"); + System.out.println(ll.get(0).data); + System.out.println(ll.get(1).data); + System.out.println(ll.get(2).data); + System.out.println(ll.get(3).data); + System.out.println(ll.size); + System.out.println(ll.remove(1).data); + System.out.println(ll.get(0).data); + System.out.println(ll.get(1).data); + System.out.println(ll.get(2).data); + System.out.println(ll.size); + } + +} diff --git a/group23/609041842/src/code/Queue.java b/group23/609041842/src/code/Queue.java new file mode 100644 index 0000000000..4f18db0129 --- /dev/null +++ b/group23/609041842/src/code/Queue.java @@ -0,0 +1,17 @@ +package code; + +public class Queue { + + private LinkedList lk = new LinkedList(); + public void enQueue(Object o){ + lk.add(o); + } + public void deQueue(){ + lk.remove(lk.size-1); + } + public boolean isEmpty(){ + if(lk.size == 0) + return true; + return false; + } +} diff --git a/group23/609041842/src/code/Stack.java b/group23/609041842/src/code/Stack.java new file mode 100644 index 0000000000..315b07f5a8 --- /dev/null +++ b/group23/609041842/src/code/Stack.java @@ -0,0 +1,30 @@ +package code; + +public class Stack { + + private ArrayList array = new ArrayList(); + + public void push(Object o){ + array.add(o); + } + public Object pop(){ + return array.remove(array.size()-1); + } + + public boolean isEmpty(){ + if(array.size()<=0) + return true; + return false; + } + + public int size(){ + return array.size(); + } + public static void main(String[] args) { + Stack sc = new Stack(); + sc.push("hello world"); + sc.push("java"); + sc.push("jvm"); + } + +} From e90e40ae6493031a9384f0abf670c3c98752b5de Mon Sep 17 00:00:00 2001 From: xmt <542194147@qq.com> Date: Sun, 12 Mar 2017 20:16:02 +0800 Subject: [PATCH 055/155] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/download/DownloadThread.java | 26 ++++++ .../coderising/download/FileDownloader.java | 79 ++++++++++++++++++ .../download/FileDownloaderTest.java | 59 +++++++++++++ .../coderising/download/api/Connection.java | 23 +++++ .../download/api/ConnectionException.java | 5 ++ .../download/api/ConnectionManager.java | 10 +++ .../download/api/DownloadListener.java | 5 ++ .../download/impl/ConnectionImpl.java | 46 ++++++++++ .../download/impl/ConnectionManagerImpl.java | 23 +++++ .../src/com/coding/basic/MyLinkedList.java | 83 +++++++++++++++++++ 10 files changed, 359 insertions(+) create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/DownloadThread.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/FileDownloader.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/api/Connection.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/api/ConnectionException.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/api/DownloadListener.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group11/542194147/myDataStructure/src/com/coderising/download/impl/ConnectionManagerImpl.java diff --git a/group11/542194147/myDataStructure/src/com/coderising/download/DownloadThread.java b/group11/542194147/myDataStructure/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..b12ad98379 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,26 @@ +package com.coderising.download; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + try { + conn.read(startPos, endPos); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group11/542194147/myDataStructure/src/com/coderising/download/FileDownloader.java b/group11/542194147/myDataStructure/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..75ade93046 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,79 @@ +package com.coderising.download; + +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + int downloadThreadNum=3; + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + try { + + conn = cm.open(this.url); + int length = conn.getContentLength(); + int downloadBlock=length/3; + int appendBlock=length%3; + RandomAccessFile rdaFile=new RandomAccessFile("downloadFile","wr"); + for(int i=0;i7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + Node p=head; Node q=null; Node front=null; + while(p!=null){ + q=p.next; + p.next=front; + front=p; + p=q; + } + head=front; + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public static int[] getElements(MyLinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(MyLinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public MyLinkedList intersection( MyLinkedList list){ + return null; + } } From 03ee290fd02e44e718dc637f386ca49fae5615cd Mon Sep 17 00:00:00 2001 From: 240094626 Date: Sun, 12 Mar 2017 21:26:25 +0800 Subject: [PATCH 056/155] =?UTF-8?q?3=E6=9C=8815=E6=97=A5=20=E6=96=87?= =?UTF-8?q?=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group17/article/20170305-20170312.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group17/article/20170305-20170312.md b/group17/article/20170305-20170312.md index 3d45ad0516..ad486c3dc7 100644 --- a/group17/article/20170305-20170312.md +++ b/group17/article/20170305-20170312.md @@ -20,7 +20,7 @@ 1059107701 -240094626 +240094626 http://note.youdao.com/noteshare?id=2a29168e06f3942719478584968505a6 82427129 From 5b179ddd7893cbcd803b4dd72c7fce0c8d789eb1 Mon Sep 17 00:00:00 2001 From: PengyuanWei Date: Sun, 12 Mar 2017 22:06:47 +0800 Subject: [PATCH 057/155] =?UTF-8?q?week01=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\347\232\204\345\205\263\347\263\273.md" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "group22/910725683/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" diff --git "a/group22/910725683/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group22/910725683/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000..04e93eb38e --- /dev/null +++ "b/group22/910725683/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,27 @@ +##CPU锛屽唴瀛橈紝纭洏锛屾寚浠や互鍙婂畠浠箣闂寸殑鍏崇郴(2017.03.12) + +>瀵硅繖鍧椾篃璇翠笉澶繁锛屽ぇ浣撳啓鐐瑰惂 + +### CPU + +CPU锛屽氨鏄腑澶鐞嗗櫒锛圕entral Processing Unit锛夛紝灏辨槸涓澶у潡闆嗘垚鐢佃矾 :) + +杩欓噷闈㈠憿锛屾湁杩欎箞鍑犱釜閲嶈鐨勪笢瑗匡細ALU銆佸瘎瀛樺櫒銆佹椂閽熴 + +ALU锛氬氨鏄畻鏁伴昏緫鍗曞厓锛屾垜鐨勭悊瑙o紝鍔熻兘涓婂氨鏄妸鍑犱釜鍩烘湰鐨勯昏緫璁$畻銆佹暟瀛﹁绠椾互鐢佃矾褰㈠紡瀹炵幇鍑烘潵銆傛瘮濡傚姞娉曞晩銆佷笌鎴栭潪闂ㄥ晩銆佷綅绉诲晩浠涔堢殑銆 + +瀵勫瓨鍣細鍚悕瀛楋紝灏辨槸瀛樺偍鐢ㄧ殑锛屼繚瀛樿骞茬殑浠诲姟銆佺粰ALU鎻愪緵杈撳叆銆佷繚瀛楢LU鐨勮緭鍑恒 + +鏃堕挓锛氳繖涓傘傘傦紝灏辨槸涓涓鏃跺櫒銆備负鍟ラ渶瑕佽繖涓鏃跺櫒鍛紙鎸夌収涓婇潰璁茬殑锛岃繍绠楀凡缁忛兘澶熷畬鎴愪簡锛夛紵鍙互鐞嗚В涓轰箰闃熺殑鎸囨尌銆侀緳鑸熶笂鐨勯紦鎵嬶紝浣滅敤灏辨槸浜х敓涓涓鐜囦俊鍙凤紝鐢ㄦ潵鎸囨尌涓婇潰閭d咯鎸夌収涓涓妭濂忔湁搴忕殑宸ヤ綔銆備负浜嗗揩閫熺殑瀹屾垚浠诲姟锛岃繖涓鐜囬渶瑕侀潪甯稿揩锛屾湁澶氬揩鍛紵鐩墠3.5GHz宸茬粡寰堝父瑙佷簡锛屼篃灏辨槸璇1s杩愮畻350,000,000娆° + +### 鍐呭瓨 + +鍐呭瓨鍛紝涔熸槸鐢ㄦ潵瀛樺偍鏁版嵁鐨勩傚墠闈㈣浜嗭紝CPU閲岄潰宸茬粡鏈変簡瀵勫瓨鍣ㄨ繖涔堜釜鐢ㄦ潵瀛樺偍鐨勪笢瑗匡紝涓哄暐杩樿涓唴瀛樺憿锛熶富瑕佸師鍥犲氨鏄瘎瀛樺櫒鍙堝皬鍙堣吹锛屽皬鍒颁笉鑳藉瓨鍌ㄦ墍鏈夌殑鍐呭锛堝熀鏈笂瀛樹笉浜嗕粈涔堬級锛岃吹鍒颁笉濂戒换鎬х殑鎵╁锛堣婊¤冻CPU閭d箞蹇殑閫熷害杩樻槸鏈夐毦搴︾殑锛夈傛湁浜哄氨鎯冲嚭浜嗗彟涓绉嶆柟妗堬紝鎶婁笉閭d箞甯哥敤鐨勬暟鎹憿锛屾斁鍦ㄤ笉閭d箞蹇紝浣嗘槸鏇村ぇ鐨勫瓨鍌ㄥ師浠堕噷闈紝涔熷氨鏄唴瀛樸傚钩甯稿瓨鍌ㄧ偣鍫嗘爤鍟娿佺紦鍐蹭粈涔堢殑銆傝繖鏍凤紝瀵勫瓨鍣ㄥ彧瑕佷繚瀛樻暟鎹湪鍐呭瓨涓殑浣嶇疆锛岀敤鐨勬椂鍊欏幓鍐呭瓨鍙栧嚭鏉ュ氨濂戒簡銆傚唴瀛樿繕鍙互鍒嗕负RAM涓嶳OM锛屼笉鍐嶇粏璇淬 + +### 纭洏 + +瑕佺‖鐩橈紝鏄洜涓哄唴瀛樹篃涓嶅鐢ㄤ簡銆傘傘傚綋鐒讹紝纭洏姣斿唴瀛樿繕瑕佹參锛屼竴鑸篃浼氭瘮鍐呭瓨澶с傜敤鐨勬椂鍊欙紝鍏堜粠纭洏璇诲嚭鏉ユ斁鍒板唴瀛樺氨濂戒簡銆 + +### 鎸囦护 + +鎸囦护锛屽氨鏄粰CPU鐢ㄧ殑Java锛堝綋鐒跺啓璧锋潵杩滄病鏈塉ava鏂逛究锛夛紝鐢ㄦ潵鍛婅瘔CPU瑕佸共浠涔堛 \ No newline at end of file From 2de36679f4794880c8fceb4feeff55ffdc2aafb5 Mon Sep 17 00:00:00 2001 From: yanshanliang <573535234@qq.com> Date: Sun, 12 Mar 2017 22:24:51 +0800 Subject: [PATCH 058/155] arraylist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鏆備笖瀹炵幇ArrayList --- .../573535234/RemoteSystemsTempFiles/.project | 12 ++ group22/573535234/githubTutorial/.classpath | 6 + group22/573535234/githubTutorial/.gitignore | 1 + group22/573535234/githubTutorial/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../com/lys/coding/week2/basic/ArrayList.java | 76 ++++++++ .../lys/coding/week2/basic/LinkedList.java | 164 ++++++++++++++++++ .../src/com/lys/coding/week2/basic/List.java | 9 + 8 files changed, 296 insertions(+) create mode 100644 group22/573535234/RemoteSystemsTempFiles/.project create mode 100644 group22/573535234/githubTutorial/.classpath create mode 100644 group22/573535234/githubTutorial/.gitignore create mode 100644 group22/573535234/githubTutorial/.project create mode 100644 group22/573535234/githubTutorial/.settings/org.eclipse.jdt.core.prefs create mode 100644 group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/ArrayList.java create mode 100644 group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/LinkedList.java create mode 100644 group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/List.java diff --git a/group22/573535234/RemoteSystemsTempFiles/.project b/group22/573535234/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group22/573535234/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group22/573535234/githubTutorial/.classpath b/group22/573535234/githubTutorial/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group22/573535234/githubTutorial/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group22/573535234/githubTutorial/.gitignore b/group22/573535234/githubTutorial/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group22/573535234/githubTutorial/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group22/573535234/githubTutorial/.project b/group22/573535234/githubTutorial/.project new file mode 100644 index 0000000000..ad0a1d9490 --- /dev/null +++ b/group22/573535234/githubTutorial/.project @@ -0,0 +1,17 @@ + + + githubTutorial + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group22/573535234/githubTutorial/.settings/org.eclipse.jdt.core.prefs b/group22/573535234/githubTutorial/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group22/573535234/githubTutorial/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/ArrayList.java b/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/ArrayList.java new file mode 100644 index 0000000000..feeaaa2d94 --- /dev/null +++ b/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/ArrayList.java @@ -0,0 +1,76 @@ +package com.lys.coding.week2.basic; + +import java.util.Arrays; + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData = new Object[100]; + + @Override + public void add(Object o) { + //先检查数组容量是否充足,不足则要扩增 + if(size>=elementData.length){ + int newLength = elementData.length*3/2+1; + Arrays.copyOf(elementData, newLength); + } + //往数组里面增添数据 + elementData[size+1]=o; + //数组容量size加一 + size++; + } + + @Override + public void add(int index, Object o) { + //先检查数组容量是否充足,不足则要扩增 + if(size>=elementData.length){ + int newLength = elementData.length*3/2+1; + Arrays.copyOf(elementData, newLength); + } + if(index!=size-1){ + //把当前数组index后面的数据往后挪 + System.arraycopy(elementData, index, elementData, index+1, size-index); + } + //往index位置放 + elementData[index]=o; + //数组容量size加一 + size++; + } + + @Override + public Object get(int index) { + //检查index是否非法 + if(index>=size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + }else if(index<0){ + throw new IllegalArgumentException("Index: "+index+",<0!"); + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + Object o = elementData[index]; + //检查index是否非法 + if(index>=size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + }else if(index<0){ + throw new IllegalArgumentException("Index: "+index+",<0!"); + } + if(size!=index+1){ + //把当前index后面的数据往前挪 + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + } + elementData[size]=null; + //数组容量size减一 + size--; + return o; + } + + @Override + public int size() { + return size; + } + +} diff --git a/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/LinkedList.java b/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/LinkedList.java new file mode 100644 index 0000000000..f46f218e60 --- /dev/null +++ b/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/LinkedList.java @@ -0,0 +1,164 @@ +package com.lys.coding.week2.basic; + +import java.util.Iterator; + +public class LinkedList implements List{ + + private Node head; + + public void add(Object o){ + if(head==null){ + head = new Node(); + head.data = o; + }else{ + Node nodes = head; + while(nodes.next!=null){ + nodes = nodes.next; + } + nodes.next = new Node(); + nodes.next.data = o; + } + } + public void add(int index , Object o){ + int len = size(); + if(len7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/List.java b/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/List.java new file mode 100644 index 0000000000..a92e7cabf5 --- /dev/null +++ b/group22/573535234/githubTutorial/src/com/lys/coding/week2/basic/List.java @@ -0,0 +1,9 @@ +package com.lys.coding.week2.basic; + +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(); +} From 6330a15958d61829fb86478bedbde9c9fede9061 Mon Sep 17 00:00:00 2001 From: DDCV587 Date: Sun, 12 Mar 2017 23:17:13 +0800 Subject: [PATCH 059/155] 2.28 --- group11/171535320/DataStruct/ArrayList.java | 2 +- group11/171535320/DataStruct/LinkedList.java | 123 ++++++++++++++++++- 2 files changed, 122 insertions(+), 3 deletions(-) diff --git a/group11/171535320/DataStruct/ArrayList.java b/group11/171535320/DataStruct/ArrayList.java index 4340deecf4..5bed84f633 100644 --- a/group11/171535320/DataStruct/ArrayList.java +++ b/group11/171535320/DataStruct/ArrayList.java @@ -3,7 +3,7 @@ import java.util.Arrays; import java.util.Objects; -public class ArrayList implements List { +public class ArrayList implements List { private int size = 0; diff --git a/group11/171535320/DataStruct/LinkedList.java b/group11/171535320/DataStruct/LinkedList.java index 24a99466ca..33ae66ba65 100644 --- a/group11/171535320/DataStruct/LinkedList.java +++ b/group11/171535320/DataStruct/LinkedList.java @@ -1,7 +1,5 @@ package DataStruct; -import DataStruct.List; - public class LinkedList implements List { private Node head; @@ -151,4 +149,125 @@ private static class Node{ Node next; } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + Node first = head; + Node last = head.next; + while(last != null) { + Node temp = last; + last = last.next; + temp.next = first; + first = temp; + } + head = first; + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + Node temp = head; + int len = 1; + while(temp.next != null) { + temp = temp.next; + len++; + } + + int num = len / 2; + + for(int i = 0; i < num; ++i) { + head = head.next; + } + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + if(i == 0) { + head = null; + } + Node temp = head; + for(int n = 1; n < i; ++n) { + temp = temp.next; + } + Node last = temp; + for(int n = 0; n < length; ++n) { + if(last != null) { + last = last.next; + } + } + temp.next = last; + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + ArrayList tempList = new ArrayList(); + Node t = head; + int i = 0; + while(t != null) { + tempList.add(t.data); + i++; + } + int k = 0; + int[] result = new int[i]; + for(int j = 0; j < list.size(); ++j) { + result[k++] = (Integer)tempList.get((Integer) list.get(j)); + } + return result; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } } From bcecf63d6c2d8f768c6144a123469616c6eabca6 Mon Sep 17 00:00:00 2001 From: liyiliang <735371210@qq.com> Date: Sun, 12 Mar 2017 23:32:45 +0800 Subject: [PATCH 060/155] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../735371210/src/task1/basic/Iterator.java | 8 + .../735371210/src/task1/basic/LinkedList.java | 140 ++++++++++++++++++ group22/735371210/src/task1/basic/List.java | 11 ++ .../src/task1/basic/MyArrayList.java | 120 +++++++++++++++ group22/735371210/src/task1/basic/Queue.java | 45 ++++++ group22/735371210/src/task1/basic/Stack.java | 40 +++++ 6 files changed, 364 insertions(+) create mode 100644 group22/735371210/src/task1/basic/Iterator.java create mode 100644 group22/735371210/src/task1/basic/LinkedList.java create mode 100644 group22/735371210/src/task1/basic/List.java create mode 100644 group22/735371210/src/task1/basic/MyArrayList.java create mode 100644 group22/735371210/src/task1/basic/Queue.java create mode 100644 group22/735371210/src/task1/basic/Stack.java diff --git a/group22/735371210/src/task1/basic/Iterator.java b/group22/735371210/src/task1/basic/Iterator.java new file mode 100644 index 0000000000..ceed20b8ae --- /dev/null +++ b/group22/735371210/src/task1/basic/Iterator.java @@ -0,0 +1,8 @@ +package task1.basic; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group22/735371210/src/task1/basic/LinkedList.java b/group22/735371210/src/task1/basic/LinkedList.java new file mode 100644 index 0000000000..02177df4ea --- /dev/null +++ b/group22/735371210/src/task1/basic/LinkedList.java @@ -0,0 +1,140 @@ +package task1.basic; + +public class LinkedList implements List{ + + private Node head = new Node(); + private int size; + + private static class Node{ + Object data; + Node next; + } + + + public void add(Object o){ + addLast(o); + + } + public void add(int index ,Object o){ + if(index<0 || index>size -1){ + throw new ArrayIndexOutOfBoundsException(); + } + + Node node=head; + + Node newNode=new Node(); + + for(int i =0;ielementData.length){ + grow(minLength); + + } + } + + public void grow(int minLength){ + + int oldLength = elementData.length; + int newLength = oldLength + oldLength>>1; + + if(newLength < minLength){ + newLength=minLength; + } + + elementData= Arrays.copyOf(elementData, newLength); + + + + } + public void add(int index ,Object o){ + checkLength(size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index]=o; + size++; + + } + public Object get(int index){ + + return elementData[index]; + + + } + public Object remove(int index){ + + Object element=elementData[index]; + + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + + elementData[size-1]=null; + + size--; + + return element; + } + + public int size(){ + + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + class ArrayListIterator implements Iterator{ + + int pos=0; + + public boolean hasNext(){ + + if(elementData[pos]!=null){ + return true; + } + + return false; + + } + + public Object next(){ + + return elementData[pos++]; + } + + } + public static void main(String[] args){ + MyArrayList my=new MyArrayList(); + my.add(0); + my.add(1); + my.add(2,10); + my.add(1,11); + my.add(3,32); + + Object ele=my.remove(2); + + System.out.println(ele); + System.out.println(my.get(1)); + System.out.println(my.size()); + + System.out.println("---------"); + + Iterator it=my.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + + + + } + +} diff --git a/group22/735371210/src/task1/basic/Queue.java b/group22/735371210/src/task1/basic/Queue.java new file mode 100644 index 0000000000..7ade2226c4 --- /dev/null +++ b/group22/735371210/src/task1/basic/Queue.java @@ -0,0 +1,45 @@ +package task1.basic; + +import java.util.LinkedList; + +public class Queue { + + private LinkedList q=new LinkedList(); + + public void enQueue(Object o){ + + q.addLast(o); + } + + public Object deQueue(){ + return q.removeFirst(); + + } + + public int size(){ + + return q.size(); + } + + public static void main(String[] args){ + Queue testQ= new Queue(); + + testQ.enQueue(11); + testQ.enQueue(12); + + testQ.enQueue(13); + + System.out.println(testQ.size()); + + Object s1=testQ.deQueue(); + System.out.println(s1); + + Object s2=testQ.deQueue(); + System.out.println(s2); + + + + + } + +} diff --git a/group22/735371210/src/task1/basic/Stack.java b/group22/735371210/src/task1/basic/Stack.java new file mode 100644 index 0000000000..8aa7d2c5c1 --- /dev/null +++ b/group22/735371210/src/task1/basic/Stack.java @@ -0,0 +1,40 @@ +package task1.basic; + +import java.util.ArrayList; +public class Stack { + + private ArrayList elementData =new ArrayList(); + + public void push(Object o){ + + elementData.add(o); + } + + public Object pop(){ + + return elementData.remove(size()-1); + + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + + } + + public Object peek(){ + + return elementData.get(size()-1); + + + } + + public int size(){ + return elementData.size(); + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From a32fead86f379ab285266964344a404d5276bc45 Mon Sep 17 00:00:00 2001 From: liyiliang <735371210@qq.com> Date: Sun, 12 Mar 2017 23:43:20 +0800 Subject: [PATCH 061/155] datastruct --- group22/735371210/.gitignore | 1 + group22/735371210/.project | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 group22/735371210/.gitignore create mode 100644 group22/735371210/.project diff --git a/group22/735371210/.gitignore b/group22/735371210/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group22/735371210/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group22/735371210/.project b/group22/735371210/.project new file mode 100644 index 0000000000..1e6bc81917 --- /dev/null +++ b/group22/735371210/.project @@ -0,0 +1,17 @@ + + + task1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + From 210e65158ac0f0fd3e761df67fe39ff85ce3ed08 Mon Sep 17 00:00:00 2001 From: yyglider Date: Mon, 13 Mar 2017 09:32:25 +0800 Subject: [PATCH 062/155] code01 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit code01锛屾暟鎹粨鏋勭浉鍏 --- .../src/main/java/code01/ArrayList.java | 139 +++++++++ .../src/main/java/code01/BinaryTree.java | 77 +++++ .../coding/src/main/java/code01/Iterator.java | 7 + .../src/main/java/code01/LinkedList.java | 291 ++++++++++++++++++ .../coding/src/main/java/code01/List.java | 9 + .../coding/src/main/java/code01/Queue.java | 24 ++ .../coding/src/main/java/code01/Stack.java | 31 ++ .../src/test/java/code01/ArrayListTest.java | 63 ++++ .../src/test/java/code01/BinaryTreeTest.java | 22 ++ .../src/test/java/code01/LinkedListTest.java | 149 +++++++++ .../src/test/java/code01/QueueTest.java | 24 ++ .../src/test/java/code01/StackTest.java | 27 ++ 12 files changed, 863 insertions(+) create mode 100644 group23/769232552/coding/src/main/java/code01/ArrayList.java create mode 100644 group23/769232552/coding/src/main/java/code01/BinaryTree.java create mode 100644 group23/769232552/coding/src/main/java/code01/Iterator.java create mode 100644 group23/769232552/coding/src/main/java/code01/LinkedList.java create mode 100644 group23/769232552/coding/src/main/java/code01/List.java create mode 100644 group23/769232552/coding/src/main/java/code01/Queue.java create mode 100644 group23/769232552/coding/src/main/java/code01/Stack.java create mode 100644 group23/769232552/coding/src/test/java/code01/ArrayListTest.java create mode 100644 group23/769232552/coding/src/test/java/code01/BinaryTreeTest.java create mode 100644 group23/769232552/coding/src/test/java/code01/LinkedListTest.java create mode 100644 group23/769232552/coding/src/test/java/code01/QueueTest.java create mode 100644 group23/769232552/coding/src/test/java/code01/StackTest.java diff --git a/group23/769232552/coding/src/main/java/code01/ArrayList.java b/group23/769232552/coding/src/main/java/code01/ArrayList.java new file mode 100644 index 0000000000..976193d759 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code01/ArrayList.java @@ -0,0 +1,139 @@ +package code01; + +/** + * Created by yaoyuan on 2017/3/6. + */ +public class ArrayList implements List { + + private int max_size = 0;//鎬婚暱搴 + private int current_size = 0; //褰撳墠闀垮害 + private float extendPercent = 2; //鎵╁睍绯绘暟 + + private Object[] elementData; + + /** + * 榛樿鏋勯犲嚱鏁帮紝鍒濆鍖栨暟缁勯暱搴︿负100 + */ + public ArrayList(){ + this.elementData = new Object[100]; + this.max_size = 100; + } + /** + * 鏋勯犲嚱鏁 + * @param size锛屽垵濮嬪寲鏁扮粍闀垮害 + */ + public ArrayList(int size){ + this.elementData = new Object[size]; + this.max_size = size; + } + + /** + * 椤哄簭娣诲姞鍏冪礌锛岃秴鍑哄師濮嬬晫闄愭椂锛屾暟缁勮嚜鍔ㄦ墿灞 + */ + public void add(Object o) { + //濡傛灉瓒婄晫浜嗭紝闇瑕佸鍒跺師鏈夌殑鏁扮粍鍒版墿鍏呭悗鐨勬暟缁勪腑 + if(this.current_size + 1 > this.max_size) { + this.elementData = copyToNew(this.elementData,this.max_size * 2); + this.max_size = (int) Math.floor(this.max_size * this.extendPercent); + } + this.elementData[this.current_size] = o; + this.current_size ++; + + } + + /** + * 鎸囧畾浣嶇疆娣诲姞鍏冪礌 + * 涓绉嶆槸鍦ㄤ腑闂达紝涓绉嶆槸褰撳墠鎻掑叆鐨勪綅缃熬閮(濡傛灉瓒呰繃灏鹃儴鍒欓粯璁ゆ坊鍔犲埌灏鹃儴) + */ + public void add(int index, Object o){ + assert(index>=0); + //濡傛灉瓒婄晫浜嗭紝闇瑕佸鍒跺師鏈夌殑鏁扮粍鍒版墿鍏呭悗鐨勬暟缁勪腑 + if(this.current_size + 1 > this.max_size) { + //濡傛灉瓒婄晫浜嗭紝闇瑕佸鍒跺師鏈夌殑鏁扮粍鍒版墿鍏呭悗鐨勬暟缁勪腑 + this.elementData = copyToNew(this.elementData,this.max_size * 2); + this.max_size = (int) Math.floor(this.max_size * this.extendPercent); + } + //鏁扮粍涓棿鎻掑叆 + if(index < this.current_size){ + //闇瑕佹妸褰撳墠浣嶇疆鐨勫厓绱犲線鍚庣Щ鍔 + for (int i = this.current_size - 1; i >= index; i--) { + this.elementData[i+1] = this.elementData[i]; + } + this.elementData[index] = o; + }else { + //鍚庨潰鍔犲叆 + this.elementData[this.current_size] = o; + } + this.current_size ++; + } + + public Object get(int index){ + if(index >= 0 && index <= this.current_size-1){ + return this.elementData[index]; + }else { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + /** + * 鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱 + * @param index + * @return + */ + public Object remove(int index){ + Object result = null; + if(index >= 0 && index <= current_size-1){ + result = elementData[index]; + //鍒犻櫎鎿嶄綔 + if(index == current_size - 1){ + elementData[index] = null; + }else { + //闇瑕佹妸褰撳墠浣嶇疆鍚庨潰鐨勫厓绱犲線鍓嶇Щ鍔 + for (int i = index; i < this.current_size-1 ; i++) { + this.elementData[i] = this.elementData[i+1]; + } + this.elementData[this.current_size-1] = null; + } + this.current_size --; + }else { + throw new ArrayIndexOutOfBoundsException(index); + } + return result; + } + + public int size(){ + return this.current_size; + } + + public Iterator iterator(){ + return new Iterator() { + int cursor = 0; + int before_cursor = -1; + @Override + public boolean hasNext() { + return cursor != ArrayList.this.size(); + } + + @Override + public Object next() { + Object next = ArrayList.this.get(cursor); + before_cursor = cursor++; + return next; + } + + @Override + public void remove(){ + ArrayList.this.remove(before_cursor); + } + }; + } + + private Object[] copyToNew(Object[] oldArray, int extendSize){ + Object[] newArray = new Object[extendSize]; + for (int i = 0; i < size(); i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + +} \ No newline at end of file diff --git a/group23/769232552/coding/src/main/java/code01/BinaryTree.java b/group23/769232552/coding/src/main/java/code01/BinaryTree.java new file mode 100644 index 0000000000..27524b6552 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code01/BinaryTree.java @@ -0,0 +1,77 @@ +package code01; + +/** + * Created by yaoyuan on 2017/3/10. + */ +public class BinaryTree>{ + + private BinaryTreeNode root = null; + private int size = 0; + + public BinaryTreeNode createBinaryTree(T[] array){ + for(T data : array){ + this.insert(data); + } + return this.root; + } + + public void insert(T data){ + if(this.root == null){ + BinaryTreeNode node = new BinaryTreeNode(data); + this.root = node; + this.size ++; + return; + } + BinaryTreeNode cursor = this.root; + while (cursor != null){ + if(data.compareTo((T) cursor.data) <= 0){ + if(cursor.left == null) { + cursor.left = new BinaryTreeNode(data); + return; + } + cursor = cursor.left; + } + if(data.compareTo((T) cursor.data) > 0){ + if(cursor.right == null) { + cursor.right = new BinaryTreeNode(data); + return; + } + cursor = cursor.right; + } + } + this.size ++; + } + + public void leftOrderScan(BinaryTreeNode cursor){ + if(cursor == null){ + return; + } + leftOrderScan(cursor.left); + System.out.println(cursor.data.toString() + " "); + leftOrderScan(cursor.right); + } + + public BinaryTreeNode getRoot(){ + return this.root; + } + + class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T data, BinaryTreeNode left, BinaryTreeNode right) { + this.left = right; + this.right = left; + this.data = data; + } + + public BinaryTreeNode(T data) { + this.left = null; + this.right = null; + this.data = data; + } + + } +} diff --git a/group23/769232552/coding/src/main/java/code01/Iterator.java b/group23/769232552/coding/src/main/java/code01/Iterator.java new file mode 100644 index 0000000000..b4074067bb --- /dev/null +++ b/group23/769232552/coding/src/main/java/code01/Iterator.java @@ -0,0 +1,7 @@ +package code01; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public void remove(); +} diff --git a/group23/769232552/coding/src/main/java/code01/LinkedList.java b/group23/769232552/coding/src/main/java/code01/LinkedList.java new file mode 100644 index 0000000000..7f6d826609 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code01/LinkedList.java @@ -0,0 +1,291 @@ +package code01; + + +public class LinkedList implements List { + + private Node head; + private Node tail; //鎸囧悜閾捐〃鏈鍚庝竴涓厓绱犵殑寮曠敤 + + private int size; //鎬婚暱搴 + + public LinkedList() { + this.head = null; + this.tail = null; + this.size = 0; + } + + /** + * 鏂板椤哄簭娣诲姞涓涓厓绱 + * @param o + */ + public void add(Object o){ + Node node = new Node(); + node.data = o; + node.next = null; + //绌洪摼琛 + if(head == null){ + this.head = node; + this.tail = node; + }else { //闈炵┖閾捐〃锛岃鍏堟壘鍒伴摼琛ㄥ熬閮紝鍐嶅姞鍏ユ柊瑙g瓟 + this.tail.next = node; + this.tail = node; + } + this.size ++; + } + + /** + * 鎸囧畾绱㈠紩澶勬坊鍔爊ode + */ + public void add(int index, Object o) { + assert(index >= 0); + Node node = new Node(); + node.data = o; + node.next = null; + + if(index == 0){ + //娣诲姞鍦ㄥご閮 + node.next = head; + head = node; + }else if(index >= this.size){ + //娣诲姞鍦ㄥ熬閮 + this.tail.next = node; + }else { + //娣诲姞鍦ㄤ腑闂 + Node cursor = this.head; + for (int i = 0; i < index - 1; i++) { + cursor = cursor.next; + } + node.next = cursor.next; + cursor.next = node; + } + this.size ++; + } + + public Object get(int index){ + assert(index < this.size); + Node cursor = this.head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor.data; + } + + public Object remove(int index){ + assert(index >= 0 && index < this.size); + Object result = null; + //鍒犻櫎鐨勬槸閾捐〃灏鹃儴鐨勫厓绱 + if(index == this.size - 1){ + Node cursor = this.head; + for (int i = 0; i < index - 1; i++) { + cursor = cursor.next; + } + result = cursor.next.data; + tail = cursor; + cursor.next = null; + }else if(index == 0){ + //鍒犻櫎鐨勬槸澶撮儴鍏冪礌 + result = head.data; + head = head.next; + }else { + //鍒犻櫎鐨勬槸閾捐〃涓棿鐨勫厓绱 + Node cursor = this.head; + for (int i = 0; i < index - 1; i++) { + cursor = cursor.next; + } + result = cursor.next.data; + cursor.next = cursor.next.next; + } + this.size --; + return result; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = null; + if(this.head == null){ + this.head = node; + this.tail = node; + }else { + node.next = head; + this.head = node; + } + this.size ++; + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o; + node.next = null; + if(this.head == null){ + this.head = node; + this.tail = node; + }else { + this.tail.next = node; + this.tail = node; + } + this.size ++; + } + + public Object removeFirst(){ + Object first = null; + if(this.head != null){ + first = this.head.data; + head = head.next; + this.size --; + } + return first; + } + + public Object removeLast(){ + Object last = null; + if(this.tail != null){ + if(this.head != this.tail){ + Node cursor; + for (cursor = head;cursor.next!=tail;cursor=cursor.next); + last = this.tail.data; + this.tail = cursor; + this.tail.next = null; + }else { + last = this.tail.data; + this.head = null; + this.tail = null; + } + this.size --; + } + return last; + } + public Iterator iterator(){ + return null; + } + + /** + * 鑺傜偣绫 + */ + private static class Node{ + Object data; + Node next; + + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + if(this.head == null || this.head == this.tail){ + return; + } + + Node pre_cursor = null; + Node cursor = this.head; + Node after_cursor = cursor.next; + + while(cursor != null){ + cursor.next = pre_cursor; + pre_cursor = cursor; + cursor = after_cursor; + if(after_cursor != null){ + after_cursor = after_cursor.next; + } + } + + Node tmpNode = this.head; + this.head = this.tail; + this.tail = tmpNode; + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + if(this.head == null || this.head.next == null){ + return; + } + if(this.head.next.next == null){ + this.head = this.head.next; + } + + Node stepOne = this.head; + Node stepTwo = this.head; + + while (stepTwo.next != null){ + stepOne = stepOne.next; + stepTwo = stepTwo.next.next; + } + this.head = stepOne; + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + + /** + * 閬嶅巻鍒楄〃 + */ + public void printList(){ + for (Node cursor = this.head;cursor!=null;cursor=cursor.next){ + System.out.println(cursor.data+" "); + } + } +} diff --git a/group23/769232552/coding/src/main/java/code01/List.java b/group23/769232552/coding/src/main/java/code01/List.java new file mode 100644 index 0000000000..95bc37d172 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code01/List.java @@ -0,0 +1,9 @@ +package code01; + +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(); +} diff --git a/group23/769232552/coding/src/main/java/code01/Queue.java b/group23/769232552/coding/src/main/java/code01/Queue.java new file mode 100644 index 0000000000..d9956deb9a --- /dev/null +++ b/group23/769232552/coding/src/main/java/code01/Queue.java @@ -0,0 +1,24 @@ +package code01; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o){ + linkedList.addFirst(o); + } + + public Object deQueue(){ + Object result = linkedList.removeLast(); + return result; + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } + +} diff --git a/group23/769232552/coding/src/main/java/code01/Stack.java b/group23/769232552/coding/src/main/java/code01/Stack.java new file mode 100644 index 0000000000..dbaeb91a48 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code01/Stack.java @@ -0,0 +1,31 @@ +package code01; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object result = null; + if(elementData.size()!=0) { + result = elementData.remove(elementData.size() - 1); + } + return result; + } + + public Object peek(){ + Object result = null; + if(elementData.size()!=0) { + result = elementData.get(elementData.size() - 1); + } + return result; + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group23/769232552/coding/src/test/java/code01/ArrayListTest.java b/group23/769232552/coding/src/test/java/code01/ArrayListTest.java new file mode 100644 index 0000000000..fc4c3dc8ea --- /dev/null +++ b/group23/769232552/coding/src/test/java/code01/ArrayListTest.java @@ -0,0 +1,63 @@ +package code01; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by yaoyuan on 2017/3/8. + */ +public class ArrayListTest { + + @Test + public void testAdd() throws Exception { + + ArrayList arrayList = new ArrayList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + arrayList.add(str); + } + + // size() + Assert.assertEquals(array.length,arrayList.size()); + + //add(),get() + for (int i = 0; i < arrayList.size(); i++){ + Assert.assertEquals(array[i],arrayList.get(i)); + } + } + + @Test + public void testAddWithIndex() throws Exception { + ArrayList arrayList = new ArrayList(3);//鑷姩鎵╁ + String[] array = new String[]{"a","b","c","d","e"}; + for (int i = 0; i < array.length; i++){ + arrayList.add(i,array[i]); + } + //add(),get() + for (int i = 0; i < arrayList.size(); i++){ + Assert.assertEquals(array[i],arrayList.get(i)); + } + arrayList.add(3,"new"); + Assert.assertEquals("new",arrayList.get(3)); + + + } + + @Test + public void testRemove() throws Exception { + + ArrayList arrayList = new ArrayList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + arrayList.add(str); + } + arrayList.remove(0); + arrayList.remove(0); + + + for (int i = 0; i < arrayList.size(); i++) { + Assert.assertEquals(array[i+2],arrayList.get(i)); + } + + } +} \ No newline at end of file diff --git a/group23/769232552/coding/src/test/java/code01/BinaryTreeTest.java b/group23/769232552/coding/src/test/java/code01/BinaryTreeTest.java new file mode 100644 index 0000000000..aa1df37c13 --- /dev/null +++ b/group23/769232552/coding/src/test/java/code01/BinaryTreeTest.java @@ -0,0 +1,22 @@ +package code01; + +import org.junit.Test; + +/** + * Created by yaoyuan on 2017/3/10. + */ +public class BinaryTreeTest { + + @Test + public void testCreateBinaryTree(){ + + BinaryTree binaryTree1 = new BinaryTree(); + BinaryTree binaryTree2 = new BinaryTree(); + Integer[] array1 = new Integer[]{3,4,1,2,5}; + Integer[] array2 = new Integer[]{3,1,4,5,2}; + binaryTree1.createBinaryTree(array1); + binaryTree2.createBinaryTree(array2); + binaryTree1.leftOrderScan(binaryTree1.getRoot()); + binaryTree2.leftOrderScan(binaryTree2.getRoot()); + } +} \ No newline at end of file diff --git a/group23/769232552/coding/src/test/java/code01/LinkedListTest.java b/group23/769232552/coding/src/test/java/code01/LinkedListTest.java new file mode 100644 index 0000000000..1c6d095f60 --- /dev/null +++ b/group23/769232552/coding/src/test/java/code01/LinkedListTest.java @@ -0,0 +1,149 @@ +package code01; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by yaoyuan on 2017/3/8. + */ +public class LinkedListTest { + + @Test + public void testAdd() throws Exception { + + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + + // size() + Assert.assertEquals(array.length,linklist.size()); + + //add(),get() + for (int i = 0; i < linklist.size(); i++){ + Assert.assertEquals(array[i],linklist.get(i)); + } + + } + + @Test + public void testAddWithIndex() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + + //add(),get() + for (int i = 0; i < linklist.size(); i++){ + Assert.assertEquals(array[i],linklist.get(i)); + } + + String str = "new"; + linklist.add(0,str); + Assert.assertEquals(str,linklist.get(0)); + + linklist.add(3,str); + Assert.assertEquals(str,linklist.get(3)); + + linklist.add(linklist.size() ,str); + Assert.assertEquals(str,linklist.get(linklist.size()-1)); + } + + @Test + public void testRemove() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + + //remove(),get() + Assert.assertEquals(linklist.remove(0),array[0]); + Assert.assertEquals(linklist.size(),array.length - 1); + + Assert.assertEquals(linklist.remove(linklist.size() - 1),array[array.length-1]); + Assert.assertEquals(linklist.size(),array.length - 2); + + } + + @Test + public void testAddFirst() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + //addFirst(),get() + String str = "new"; + linklist.addFirst(str); + Assert.assertEquals(str,linklist.get(0)); + Assert.assertEquals(linklist.size(),array.length + 1); + } + + @Test + public void testAddLast() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + //addLast(),get() + String str = "new"; + linklist.addLast(str); + Assert.assertEquals(str,linklist.get(linklist.size()-1)); + Assert.assertEquals(linklist.size(),array.length + 1); + } + + @Test + public void testRemoveFirst() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + //removeFirst(),get() + Assert.assertEquals(linklist.removeFirst(),array[0]); + Assert.assertEquals(linklist.size(),array.length - 1); + } + + @Test + public void testRemoveLast() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + //removeLast(),get() + Assert.assertEquals(linklist.removeLast(),array[array.length-1]); + Assert.assertEquals(linklist.size(),array.length - 1); + + } + + @Test + public void testReverse(){ + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + linklist.reverse(); + for(int i=0; i Date: Mon, 13 Mar 2017 10:02:48 +0800 Subject: [PATCH 063/155] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/171535320/DataStruct/LinkedList.java | 21 +++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/group11/171535320/DataStruct/LinkedList.java b/group11/171535320/DataStruct/LinkedList.java index 33ae66ba65..1b57d15b50 100644 --- a/group11/171535320/DataStruct/LinkedList.java +++ b/group11/171535320/DataStruct/LinkedList.java @@ -222,7 +222,7 @@ public int[] getElements(LinkedList list){ Node t = head; int i = 0; while(t != null) { - tempList.add(t.data); + tempList.add((Integer) t.data); i++; } int k = 0; @@ -236,12 +236,27 @@ public int[] getElements(LinkedList list){ /** * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 - * @param list */ - public void subtract(LinkedList list){ + public void subtract(java.util.LinkedList list){ + Node temp = head; + Node temp2 = head; + int i = 0; + while(i < list.size() || temp != null) { + if(i == list.size() || temp == null) { + break; + } + if(list.get(i) > (Integer)temp.data) { + temp2 = temp; + temp = temp.next; + } else if(list.get(i) < (Integer)temp.data) { + i++; + } else { + temp2.next = temp.next; + } + } } /** From 1a754801adefc010fc06ad1069d2a853702db418 Mon Sep 17 00:00:00 2001 From: peter <729245768@qq.com> Date: Mon, 13 Mar 2017 11:23:25 +0800 Subject: [PATCH 064/155] =?UTF-8?q?=E6=B7=BB=E5=8A=A0170309=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/coding_170309/DownloadThread.java | 37 +++ .../main/coding_170309/FileDownloader.java | 69 +++++ .../src/main/coding_170309/Hello.java | 22 ++ .../src/main/coding_170309/LinkedList.java | 271 ++++++++++++++++++ .../src/main/coding_170309/List.java | 12 + .../main/coding_170309/api/Connection.java | 30 ++ .../api/ConnectionException.java | 7 + .../coding_170309/api/ConnectionManager.java | 13 + .../coding_170309/api/DownloadListener.java | 8 + .../coding_170309/impl/ConnectionImpl.java | 52 ++++ .../impl/ConnectionManagerImpl.java | 16 ++ .../impl/DownloadListenerImpl.java | 25 ++ 12 files changed, 562 insertions(+) create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/DownloadThread.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/FileDownloader.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/Hello.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/LinkedList.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/List.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/api/Connection.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/api/ConnectionException.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/api/ConnectionManager.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/api/DownloadListener.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/impl/ConnectionImpl.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/impl/ConnectionManagerImpl.java create mode 100644 group11/729245768/DataStructure/src/main/coding_170309/impl/DownloadListenerImpl.java diff --git a/group11/729245768/DataStructure/src/main/coding_170309/DownloadThread.java b/group11/729245768/DataStructure/src/main/coding_170309/DownloadThread.java new file mode 100644 index 0000000000..6380e5d9c1 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/DownloadThread.java @@ -0,0 +1,37 @@ +package main.coding_170309; + +import main.coding_170309.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * Created by peter on 2017/3/9. + */ +public class DownloadThread implements Runnable { + Connection conn; + int startPos; + int endPos; + public DownloadThread(Connection conn,int startPos,int endPos){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + @Override + public void run() { + try { + try { + String fileName = "d://liying"+conn.getURL().substring(conn.getURL().lastIndexOf('.')); + byte[] data= conn.read(startPos,endPos); + RandomAccessFile randomAccessFile = new RandomAccessFile(fileName,"rw"); + randomAccessFile.seek(startPos); + randomAccessFile.write(data,0,data.length); + randomAccessFile.close(); + } finally { + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/FileDownloader.java b/group11/729245768/DataStructure/src/main/coding_170309/FileDownloader.java new file mode 100644 index 0000000000..ac4d38a365 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/FileDownloader.java @@ -0,0 +1,69 @@ +package main.coding_170309; + +import main.coding_170309.api.Connection; +import main.coding_170309.api.ConnectionException; +import main.coding_170309.api.ConnectionManager; +import main.coding_170309.api.DownloadListener; +import main.coding_170309.impl.ConnectionManagerImpl; +import main.coding_170309.impl.DownloadListenerImpl; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Created by peter on 2017/3/9. + */ +public class FileDownloader { + String url; + ConnectionManager cm; + DownloadListener listener; + public FileDownloader(String url){ + this.url = url; + } + + public void execute() throws ConnectionException, IOException { + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + cm = new ConnectionManagerImpl(); + Connection conn = cm.open(url); + HttpURLConnection urlConnection =(HttpURLConnection) new URL(url).openConnection(); + int length = urlConnection.getContentLength(); + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + ExecutorService executor = Executors.newFixedThreadPool(3); + if(length>0){ + executor.execute(new DownloadThread(conn,0,length/3-1)); + executor.execute(new DownloadThread(conn,length/3,length/3*2-1)); + executor.execute(new DownloadThread(conn,length/3*2,length)); + }else{ + executor.execute(new DownloadThread(conn,0,1024*150)); + executor.execute(new DownloadThread(conn,1024*150,1024*300)); + executor.execute(new DownloadThread(conn,1024*300,1024*450)); + } + + + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + setListener(new DownloadListenerImpl(executor)); + listener.notifyFinshed(); + + } + public void setListener(DownloadListener listener) { + this.listener = listener; + } + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener() { + return listener; + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/Hello.java b/group11/729245768/DataStructure/src/main/coding_170309/Hello.java new file mode 100644 index 0000000000..9a9ebaf7a9 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/Hello.java @@ -0,0 +1,22 @@ +package main.coding_170309; + +import main.coding_170309.api.ConnectionException; + +import java.io.IOException; + +/** + * Created by peter on 2017/3/9. + */ +public class Hello { + public static void main(String[] args) { + String url = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4162473607,1717166705&fm=23&gp=0.jpg"; + FileDownloader downloader = new FileDownloader(url); + try { + downloader.execute(); + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/LinkedList.java b/group11/729245768/DataStructure/src/main/coding_170309/LinkedList.java new file mode 100644 index 0000000000..8784d252f3 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/LinkedList.java @@ -0,0 +1,271 @@ +package main.coding_170309; + +/** + * Created by peter on 2017/3/10. + */ +public class LinkedList implements List { + private Node head; + public LinkedList(){ + head = new Node(); + head.data = null; + head.next = null; + } + @Override + public void add(Object o) { + Node p = head; + while (p.next!=null){ + p=p.next; + } + Node node = new Node(); + node.data = o; + node.next = null; + p.next = node; + + } + + @Override + public void add(Object o, int index) { + if(index<0||index>getSize()){ + throw new ArrayIndexOutOfBoundsException("鎻掑叆浣嶇疆涓嶅悎娉"); + } + int i =0; + Node p =head; + while (i=getSize()){ + throw new ArrayIndexOutOfBoundsException("鏁扮粍瓒婄晫"); + } + int i =0 ; + Node p = head.next; + while (i=getSize()){ + throw new ArrayIndexOutOfBoundsException("涓嶅悎娉曠殑index"); + } + int i = 0; + Node p =head; + while (i7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + Node p = head.next; + head.next = null; + while (p!=null){ + add(p.data,0); + p = p.next; + } + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + int removeSize = getSize()/2; + for(int i=0;i=getSize()){ + throw new ArrayIndexOutOfBoundsException("涓嬫爣瓒婄晫"); + } + if(length<0){ + throw new IllegalArgumentException("闀垮害涓嶈兘涓鸿礋"); + } + for(int index = i;index101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int length = list.getSize(); + int[] data= new int[length]; + for(int i=0;i=max){ + throw new IllegalArgumentException("max蹇呴』涓嶈兘姣攎in灏"); + } + int length = 0;//婊¤冻鏉′欢鐨勪釜鏁 + Node p =head.next; + Node q = head; + Node startPos=null,endPos=null; + boolean isStarted=false; + while (p!=null){ + if((int)p.data>min&&!isStarted){ + startPos = q; + isStarted = true; + } + if((int)p.data>=max){ + endPos = p; + break; + } + q = p; + p = p.next; + } + //瀛樺湪涓夌鎯呭喌:startPos鍜宔ndPos鍧囦负null,startPos涓嶄负null,endPos涓簄ull,startPos涓嶄负null鍜宔ndPos涓嶄负null + startPos.next = endPos; + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + Node p =this.head.next; + Node q = list.head.next; + LinkedList newList = new LinkedList(); + while (p!=null&&q!=null){ + if((int)p.data>(int)q.data){ + q = q.next; + }else if((int)p.data==(int)q.data){ + newList.add(p.data); + p = p.next; + q = q.next; + }else { + p = p.next; + } + } + while (p!=null){ + newList.add(p.data); + p = p.next; + } + while (q!=null){ + newList.add(q.data); + q = q.next; + } + return newList; + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/List.java b/group11/729245768/DataStructure/src/main/coding_170309/List.java new file mode 100644 index 0000000000..78a8ce41a7 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/List.java @@ -0,0 +1,12 @@ +package main.coding_170309; + +/** + * Created by peter on 2017/3/10. + */ +public interface List { + public void add(Object o); + public void add(Object o ,int index); + public Object get(int index); + public Object remove(int index); + public int getSize(); +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/api/Connection.java b/group11/729245768/DataStructure/src/main/coding_170309/api/Connection.java new file mode 100644 index 0000000000..c67a52b299 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/api/Connection.java @@ -0,0 +1,30 @@ +package main.coding_170309.api; + +import java.io.IOException; + +/** + * Created by peter on 2017/3/9. + */ +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(); + /** + * 鑾峰彇url + */ + public String getURL(); +} + diff --git a/group11/729245768/DataStructure/src/main/coding_170309/api/ConnectionException.java b/group11/729245768/DataStructure/src/main/coding_170309/api/ConnectionException.java new file mode 100644 index 0000000000..8d4624647e --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/api/ConnectionException.java @@ -0,0 +1,7 @@ +package main.coding_170309.api; + +/** + * Created by peter on 2017/3/9. + */ +public class ConnectionException extends Exception { +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/api/ConnectionManager.java b/group11/729245768/DataStructure/src/main/coding_170309/api/ConnectionManager.java new file mode 100644 index 0000000000..e3746ebfb6 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/api/ConnectionManager.java @@ -0,0 +1,13 @@ +package main.coding_170309.api; + +/** + * Created by peter on 2017/3/9. + */ +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/api/DownloadListener.java b/group11/729245768/DataStructure/src/main/coding_170309/api/DownloadListener.java new file mode 100644 index 0000000000..324eabba73 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/api/DownloadListener.java @@ -0,0 +1,8 @@ +package main.coding_170309.api; + +/** + * Created by peter on 2017/3/9. + */ +public interface DownloadListener { + public void notifyFinshed(); +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/impl/ConnectionImpl.java b/group11/729245768/DataStructure/src/main/coding_170309/impl/ConnectionImpl.java new file mode 100644 index 0000000000..ed015618bc --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/impl/ConnectionImpl.java @@ -0,0 +1,52 @@ +package main.coding_170309.impl; + +import main.coding_170309.api.Connection; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Arrays; + +/** + * Created by peter on 2017/3/9. + */ +public class ConnectionImpl implements Connection { + private String url; + private HttpURLConnection urlConnection; + + public ConnectionImpl(String url) { + this.url = url; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + urlConnection = (HttpURLConnection) new URL(url).openConnection(); + urlConnection.setRequestProperty("Range","bytes="+startPos+"-"+endPos);//浠庢寚瀹氫綅缃紑濮 + InputStream in = urlConnection.getInputStream(); + byte[] data = new byte[endPos-startPos+1]; + byte[] temp = new byte[1024]; + int pointer=0;//琛ㄧずdata姣忔鍋忕Щ閲 + int length ;//琛ㄧず涓娆¤兘璇诲彇鐨刡it鏁 + while ((length=in.read(temp,0,temp.length))!=-1){ + System.arraycopy(temp,0,data,pointer,length); + pointer+=length; + } + return Arrays.copyOf(data,pointer); + } + + @Override + public int getContentLength() { + return urlConnection.getContentLength(); + } + + @Override + public String getURL() { + return url; + } + + @Override + public void close() { + urlConnection.disconnect(); + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/impl/ConnectionManagerImpl.java b/group11/729245768/DataStructure/src/main/coding_170309/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..7b97153076 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/impl/ConnectionManagerImpl.java @@ -0,0 +1,16 @@ +package main.coding_170309.impl; + +import main.coding_170309.api.Connection; +import main.coding_170309.api.ConnectionException; +import main.coding_170309.api.ConnectionManager; + +/** + * Created by peter on 2017/3/9. + */ +public class ConnectionManagerImpl implements ConnectionManager { + @Override + public Connection open(String url) throws ConnectionException { + Connection conn = new ConnectionImpl(url); + return conn; + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170309/impl/DownloadListenerImpl.java b/group11/729245768/DataStructure/src/main/coding_170309/impl/DownloadListenerImpl.java new file mode 100644 index 0000000000..f496a0e126 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170309/impl/DownloadListenerImpl.java @@ -0,0 +1,25 @@ +package main.coding_170309.impl; + +import main.coding_170309.api.DownloadListener; + +import java.util.concurrent.ExecutorService; + +/** + * Created by peter on 2017/3/9. + */ +public class DownloadListenerImpl implements DownloadListener { + private ExecutorService executor; + public DownloadListenerImpl(ExecutorService executor){ + this.executor = executor; + } + @Override + public void notifyFinshed() { + executor.shutdown(); + while (true){ + if(executor.isTerminated()){ + break; + } + } + System.out.println("缃戠粶鏂囦欢涓嬭浇瀹屾垚"); + } +} From 259e1bfda72feebfba1b190ea54d1e73be9a60a4 Mon Sep 17 00:00:00 2001 From: peter <729245768@qq.com> Date: Mon, 13 Mar 2017 11:24:44 +0800 Subject: [PATCH 065/155] =?UTF-8?q?=E6=B7=BB=E5=8A=A0170309=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=B5=8B=E8=AF=95=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/coding_170309/LinkedListTest.java | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 group11/729245768/DataStructure/tests/main/coding_170309/LinkedListTest.java diff --git a/group11/729245768/DataStructure/tests/main/coding_170309/LinkedListTest.java b/group11/729245768/DataStructure/tests/main/coding_170309/LinkedListTest.java new file mode 100644 index 0000000000..797e9d5f41 --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170309/LinkedListTest.java @@ -0,0 +1,167 @@ +package main.coding_170309; + +import junit.framework.TestCase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by peter on 2017/3/11. + */ +public class LinkedListTest extends TestCase { + LinkedList linkedList; + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @org.junit.Test + public void testAdd() throws Exception { + linkedList.add(1); + linkedList.add(2); + System.out.println(linkedList); + } + + @Test + public void testAdd1() throws Exception { + linkedList.add(12,0); + linkedList.add(15,1); + linkedList.add(20,1); + System.out.println(linkedList); + } + + @Test + public void testGet() throws Exception { + linkedList.add(12); + Assert.assertEquals(12,(int)linkedList.get(0)); + } + + @Test + public void testRemove() throws Exception { + linkedList.add(15); + linkedList.add(20); + linkedList.remove(0); + System.out.println(linkedList.getSize()); + } + + @Test + public void testGetSize() throws Exception { + linkedList.add(15); + linkedList.add(25); + linkedList.add(30); + linkedList.add(45); + System.out.println(linkedList.getSize()); + } + + @Test + public void testReverse() throws Exception { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + System.out.println(linkedList); + linkedList.reverse(); + System.out.println(linkedList); + } + + @Test + public void testRemoveFirstHalf() throws Exception { + linkedList.add(10); + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(15); + System.out.println(linkedList); + linkedList.removeFirstHalf(); + System.out.println(linkedList); + } + + @Test + public void testRemove1() throws Exception { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.remove(1,2); + System.out.println(linkedList); + } + + @Test + public void testGetElements() throws Exception { + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(15); + linkedList.add(16); + LinkedList list = new LinkedList(); + list.add(2); + list.add(3); + list.add(4); + int[] data = linkedList.getElements(list); + System.out.println(Arrays.toString(data)); + } + + @Test + public void testSubtract() throws Exception { + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(15); + linkedList.add(16); + LinkedList list = new LinkedList(); + list.add(12); + list.add(14); + list.add(15); + linkedList.subtract(list); + System.out.println(linkedList); + } + + @Test + public void testRemoveDuplicateValues() throws Exception { + linkedList.add(10); + linkedList.add(11); + linkedList.add(11); + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(14); + } + + @Test + public void testRemoveRange() throws Exception { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.removeRange(2,4); + System.out.println(linkedList); + } + + @Test + public void testIntersection() throws Exception { + linkedList.add(12); + linkedList.add(13); + linkedList.add(14); + linkedList.add(15); + linkedList.add(16); + LinkedList list = new LinkedList(); + list.add(12); + list.add(14); + list.add(15); + list.add(17); + list.add(18); + System.out.println(linkedList.intersection(list)); + + } + +} \ No newline at end of file From 7e3bb418bf11dfcb0bbabb4ccf4808a2795779c0 Mon Sep 17 00:00:00 2001 From: Vito Date: Mon, 13 Mar 2017 12:20:00 +0800 Subject: [PATCH 066/155] update readme --- group23/729693763/readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/group23/729693763/readme.md b/group23/729693763/readme.md index 25bc95c472..a8a9503559 100644 --- a/group23/729693763/readme.md +++ b/group23/729693763/readme.md @@ -1 +1,3 @@ Data Struct contain: ArrayList, LinkedList, BinaryTreeNode, Stack, Queue, Iterator +--- --- +Add Blog link: http://www.cnblogs.com/CodeSaveMe/p/6523745.html From 1c647592ec4b430f6091155237ce9bc0dcd86065 Mon Sep 17 00:00:00 2001 From: em14Vito Date: Mon, 13 Mar 2017 12:23:13 +0800 Subject: [PATCH 067/155] update LinkedList --- .../First_Homework1/src/com/danny/hw1/LinkedList.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/group23/729693763/First_Homework1/src/com/danny/hw1/LinkedList.java b/group23/729693763/First_Homework1/src/com/danny/hw1/LinkedList.java index 445cef7686..64c9b9bbca 100644 --- a/group23/729693763/First_Homework1/src/com/danny/hw1/LinkedList.java +++ b/group23/729693763/First_Homework1/src/com/danny/hw1/LinkedList.java @@ -41,12 +41,11 @@ public Object remove(int index) { //delete all node if ( size() == 1 ) { + deleteNode = this.Head; this.Head = null; this.Tail = null; - } - - //Remove Head - if ( index == 0 ) { + } else if ( index == 0 ) { + //Remove Head deleteNode = this.Head; this.Head = this.Head.next; } else if ( index == size() - 1) { From b42cc06fec73b0d258aa4375d403f7f041de6e49 Mon Sep 17 00:00:00 2001 From: anxinJ <1028361767@qq.com> Date: Mon, 13 Mar 2017 12:25:28 +0800 Subject: [PATCH 068/155] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鏁版嵁缁撴瀯浣滀笟 --- group23/.DS_Store | Bin 0 -> 6148 bytes group23/1028361767/1028361767.md | 1 + .../src/com/coding/basic/ArrayList.java | 79 ++++++ .../src/com/coding/basic/BinaryTreeNode.java | 97 +++++++ .../src/com/coding/basic/Iterator.java | 8 + .../src/com/coding/basic/LinkedList.java | 244 ++++++++++++++++++ .../src/com/coding/basic/List.java | 13 + .../src/com/coding/basic/Queue.java | 22 ++ .../src/com/coding/basic/Stack.java | 35 +++ .../test/com/coding/basic/TestArrayList.java | 60 +++++ .../com/coding/basic/TestBinaryTreeNode.java | 37 +++ 11 files changed, 596 insertions(+) create mode 100644 group23/.DS_Store create mode 100644 group23/1028361767/1028361767.md create mode 100644 group23/1028361767/Week1DataStructure/src/com/coding/basic/ArrayList.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coding/basic/Iterator.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coding/basic/LinkedList.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coding/basic/List.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coding/basic/Queue.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coding/basic/Stack.java create mode 100644 group23/1028361767/Week1DataStructure/src/test/com/coding/basic/TestArrayList.java create mode 100644 group23/1028361767/Week1DataStructure/src/test/com/coding/basic/TestBinaryTreeNode.java diff --git a/group23/.DS_Store b/group23/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7e3e6cfac417bbd05dee75c3fbd8899a17aaca99 GIT binary patch literal 6148 zcmeHLK}th05S`I3if+1enIrfWtjkcoC+G!)e$a)bUD$mt=+0Ys3E#_%@;{9r2qH2A znKwyhlIeq=WGW)MIt-o2LPX|hM5WOX4mYPx+<6Any2iEKNGD6^Wn-bgIKD z1Guv(^K-+{!9Xw&416-c^C6)Tvtu!=M+a0b0f2l)SAor1f-%W4I~GICK-5Bk7OGxi zsD)!ZxnFiHh88aB#fSRNAH|E--LZbE?xNW+bTAMMOc^+}?t=IK9X^?6kv|QIUN8_0 z{4)l0)~|d1b>(;K(a+;uo6zpjDD*2dAh0Ko0CaecT+~UMPqg8e9g87X;dl)P#zUYI L5-J$@1qR*$%DN@j literal 0 HcmV?d00001 diff --git a/group23/1028361767/1028361767.md b/group23/1028361767/1028361767.md new file mode 100644 index 0000000000..741976ad6e --- /dev/null +++ b/group23/1028361767/1028361767.md @@ -0,0 +1 @@ +This is 1028361767's project. \ No newline at end of file diff --git a/group23/1028361767/Week1DataStructure/src/com/coding/basic/ArrayList.java b/group23/1028361767/Week1DataStructure/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..a0d0e50e49 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coding/basic/ArrayList.java @@ -0,0 +1,79 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private int HALF_MAX_VALUE = Integer.MAX_VALUE; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (noSpace()) { + elementData = grow(); + } + elementData[size++] = o; + } + + public void add(int index, Object o) { + if (index < 0) { + throw new IllegalArgumentException("index must be positive integer"); + } + if (index > size) { + throw new IndexOutOfBoundsException("size is" + size); + } + if (noSpace()) { + elementData = grow(); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[size++] = o; + } + + public Object get(int index) { + if (index < 0) { + throw new IllegalArgumentException("index must be positive integer"); + } + if (index > (size - 1)) { + throw new IndexOutOfBoundsException("size is" + size); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index < 0) { + throw new IllegalArgumentException("index must be positive integer"); + } + if (index > (size - 1)) { + throw new IndexOutOfBoundsException("size is" + size); + } + Object obj = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size--; + return obj; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private boolean noSpace() { + return size == elementData.length; + } + + private Object[] grow() { + int newSize; + if (size < HALF_MAX_VALUE) { + newSize = size * 2; + } else { + newSize = Integer.MAX_VALUE; + } + return Arrays.copyOf(elementData, newSize); + } + +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coding/basic/BinaryTreeNode.java b/group23/1028361767/Week1DataStructure/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..dced34e873 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode parent; + + public BinaryTreeNode(Object data) { + this.data = data; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public void setParent(BinaryTreeNode parent) { + this.parent = parent; + } + + public BinaryTreeNode getParent() { + return parent; + } + + public BinaryTreeNode insert(Object o) { + BinaryTreeNode newNode = new BinaryTreeNode(o); + BinaryTreeNode root = findRoot(this); + if (root.data == null) { + root.data = newNode; + } else { + int newVal = getNodeIntVal(newNode); + insert(root, newNode, newVal); + } + return newNode; + } + + private void insert(BinaryTreeNode node, BinaryTreeNode newNode, int newVal) { + int nodeVal = getNodeIntVal(node); + if (newVal < nodeVal) { + if (node.left == null) { + newNode.parent = node; + node.left = newNode; + } else { + insert(node.left, newNode, newVal); + } + } else { + if (node.right == null) { + newNode.parent = node; + node.right = newNode; + } else { + insert(node.right, newNode, newVal); + } + } + } + + private BinaryTreeNode findRoot(BinaryTreeNode binaryTreeNode) { + while (binaryTreeNode.parent != null) { + binaryTreeNode = binaryTreeNode.parent; + } + return binaryTreeNode; + } + + private int getNodeIntVal(BinaryTreeNode node) { + if (node.data instanceof Integer) { + return ((Integer) node.data).intValue(); + } + return 0; + } + + public int getDataIntVal() { + if (data instanceof Integer) { + return ((Integer) data).intValue(); + } + return 0; + } +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coding/basic/Iterator.java b/group23/1028361767/Week1DataStructure/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..96adcd6d3a --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coding/basic/LinkedList.java b/group23/1028361767/Week1DataStructure/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..5e241db172 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coding/basic/LinkedList.java @@ -0,0 +1,244 @@ +package com.coding.basic; + + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + Node newNode = new Node(o, null); + if (head == null) { + head = newNode; + } else { + Node tmp = head; + while (tmp.next != null) { + tmp = tmp.next; + } + tmp.next = newNode; + } + size++; + } + + public void add(int index, Object o) { + checkMinBound(index); + checkMaxBound(index, size); + Node newNode = new Node(o, null); + if (index == 0) { + newNode.next = head; + head = newNode; + } else { + int pos = 1; + Node tmp = head; + while (pos != index) { + tmp = tmp.next; + pos++; + } + newNode.next = tmp.next; + tmp.next = newNode; + } + size++; + + } + + private void checkMinBound(int index) { + if (index < 0) { + throw new IllegalArgumentException(); + } + } + + private void checkMaxBound(int index, int max) { + if (index > max) { + throw new IndexOutOfBoundsException(); + } + } + + + public Object get(int index) { + checkMinBound(index); + checkMaxBound(index, size - 1); + Node cur = head; + if (index != 0) { + int pos = 0; + do { + cur = cur.next; + pos++; + } while (pos != index); + } + return cur; + } + + public Object remove(int index) { + checkMinBound(index); + checkMaxBound(index, size - 1); + Node cur = head; + if (index == 0) { + head = cur.next; + } else { + int pos = 1; + Node prev = cur; + while (pos != index) { + prev = prev.next; + pos++; + } + cur = prev.next; + prev.next = cur.next; + } + size--; + return cur; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, null); + newNode.next = head; + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o, null); + if (head == null) { + head = newNode; + } else { + Node tmp = head; + while (tmp.next != null) { + tmp = tmp.next; + } + tmp.next = newNode; + } + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + Node ret = head; + head = head.next; + size--; + return ret; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + Node ret; + if (head.next == null) { + ret = head; + head = null; + } else { + Node prev = head; + ret = head.next; + while (ret.next != null) { + prev = ret; + ret = ret.next; + } + prev.next = null; + } + size--; + return ret; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse() { + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + */ + public void removeFirstHalf() { + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * + * @param list + */ + public static int[] getElements(LinkedList list) { + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues() { + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coding/basic/List.java b/group23/1028361767/Week1DataStructure/src/com/coding/basic/List.java new file mode 100644 index 0000000000..01398944e6 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +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(); +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coding/basic/Queue.java b/group23/1028361767/Week1DataStructure/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..bb24e2132e --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o) { + linkedList.add(o); + } + + public Object deQueue() { + return linkedList.removeFirst(); + } + + public boolean isEmpty() { + return linkedList.size() == 0; + } + + public int size() { + return linkedList.size(); + } +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coding/basic/Stack.java b/group23/1028361767/Week1DataStructure/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..55c96985a9 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coding/basic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + checkBound(); + return elementData.get(size() - 1); + } + + public Object peek() { + checkBound(); + return elementData.remove(size() - 1); + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return elementData.size(); + } + + private void checkBound() { + if (isEmpty()) { + throw new EmptyStackException(); + } + } +} diff --git a/group23/1028361767/Week1DataStructure/src/test/com/coding/basic/TestArrayList.java b/group23/1028361767/Week1DataStructure/src/test/com/coding/basic/TestArrayList.java new file mode 100644 index 0000000000..61562d2a39 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/test/com/coding/basic/TestArrayList.java @@ -0,0 +1,60 @@ +package test.com.coding.basic; + +import com.coding.basic.ArrayList; +import org.junit.Assert; +import org.junit.Test; + +public class TestArrayList { + + @Test + public void testAdd() { + ArrayList list = new ArrayList(); + int i = 0; + for (; i < 1000; i++) { + list.add(new Object()); + } + Assert.assertTrue(list.size() == i); + } + + @Test + public void testGet() { + ArrayList list = new ArrayList(); + int i = 0; + for (; i < 10; i++) { + list.add(new Object()); + } + Assert.assertFalse(list.get(5) == null); + try { + list.get(10); + Assert.assertTrue(false); + } catch (IndexOutOfBoundsException e) { + Assert.assertTrue(true); + } + } + + @Test + public void testAddWithIndex() { + ArrayList list = new ArrayList(); + int i = 0; + for (; i < 10; i++) { + list.add(new Object()); + } + Object obj = list.get(5); + list.add(5, new Object()); + Assert.assertTrue(list.size() == (i + 1)); + Assert.assertTrue(obj == list.get(6)); + } + + @Test + public void testRemove() { + ArrayList list = new ArrayList(); + int i = 0; + for (; i < 10; i++) { + list.add(i); + } + Object tempObj = list.get(5); + Assert.assertTrue(tempObj == list.remove(5)); + Assert.assertTrue(list.size() == (i - 1)); + Assert.assertTrue((int) list.get(list.size() - 1) == (i - 1)); + } +} diff --git a/group23/1028361767/Week1DataStructure/src/test/com/coding/basic/TestBinaryTreeNode.java b/group23/1028361767/Week1DataStructure/src/test/com/coding/basic/TestBinaryTreeNode.java new file mode 100644 index 0000000000..a137ceffb8 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/test/com/coding/basic/TestBinaryTreeNode.java @@ -0,0 +1,37 @@ +package test.com.coding.basic; + +import com.coding.basic.BinaryTreeNode; +import org.junit.Test; + +public class TestBinaryTreeNode { + + @Test + public void testInsert() { + BinaryTreeNode binaryTree = new BinaryTreeNode(5); + binaryTree.insert(2); + binaryTree.insert(7); + binaryTree.insert(1); + binaryTree.insert(6); + + printNode(binaryTree); + + binaryTree.insert(4); + binaryTree.insert(8); + + System.out.println("*************************"); + printNode(binaryTree); + } + + private void printNode(BinaryTreeNode node) { + System.out.print("node's data is " + node.getDataIntVal()); + System.out.println(" ,node's parent' data is " + (node.getParent() == null ? "null" : node.getParent().getDataIntVal())); + if (node.getLeft() != null) { + System.out.println("find left child node."); + printNode(node.getLeft()); + } + if (node.getRight() != null) { + System.out.println("find right child node."); + printNode(node.getRight()); + } + } +} From 5a04708aa559d9a7ce0d447fd68bbd2c2972492c Mon Sep 17 00:00:00 2001 From: bananaloveapple <810181789@qq.com> Date: Mon, 13 Mar 2017 21:05:44 +0800 Subject: [PATCH 069/155] datastructure --- group23/810181789/.classpath | 6 + group23/810181789/.gitignore | 1 + group23/810181789/.project | 17 ++ .../810181789/src/firstday/ArrayListt.java | 86 +++++++++ group23/810181789/src/firstday/LinkListt.java | 167 ++++++++++++++++++ group23/810181789/src/firstday/Queue.java | 43 +++++ group23/810181789/src/firstday/Stack.java | 43 +++++ 7 files changed, 363 insertions(+) create mode 100644 group23/810181789/.classpath create mode 100644 group23/810181789/.gitignore create mode 100644 group23/810181789/.project create mode 100644 group23/810181789/src/firstday/ArrayListt.java create mode 100644 group23/810181789/src/firstday/LinkListt.java create mode 100644 group23/810181789/src/firstday/Queue.java create mode 100644 group23/810181789/src/firstday/Stack.java diff --git a/group23/810181789/.classpath b/group23/810181789/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group23/810181789/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group23/810181789/.gitignore b/group23/810181789/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group23/810181789/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group23/810181789/.project b/group23/810181789/.project new file mode 100644 index 0000000000..d78dba00af --- /dev/null +++ b/group23/810181789/.project @@ -0,0 +1,17 @@ + + + basicstructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group23/810181789/src/firstday/ArrayListt.java b/group23/810181789/src/firstday/ArrayListt.java new file mode 100644 index 0000000000..229c588f33 --- /dev/null +++ b/group23/810181789/src/firstday/ArrayListt.java @@ -0,0 +1,86 @@ +package firstday; + +import java.util.Arrays; + +public class ArrayListt { + private Object arr[]=new Object[10]; + private int pos=0; + public boolean add(Object o) + { + if(pos=arr.length) + { + arr=Arrays.copyOf(arr, arr.length+1); + arr[pos] = o; + pos++; + return true; + } + return false; + } + public boolean add(Object o, int index) + { + if(pos=arr.length) + { + arr=Arrays.copyOf(arr, arr.length+1); + for(int i=arr.length-2;i>=index;i--) + { + arr[i+1] = arr[i]; + } + arr[index] = o; + return true; + } + return false; + } + public Object get(int i) + { + Object o = arr[i]; + return o; + } + public Object remove(int index) + { + Object var=arr[index]; + for(int i=index+1;i Date: Mon, 13 Mar 2017 23:55:12 +0800 Subject: [PATCH 070/155] struts homework resultParams --- .../bin/com/coderising/litestruts/struts.xml | 11 ++ .../src/com/coderising/array/ArrayUtil.java | 96 ++++++++++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../src/com/coderising/litestruts/Struts.java | 181 ++++++++++++++++++ .../com/coderising/litestruts/StrutsTest.java | 43 +++++ .../src/com/coderising/litestruts/View.java | 23 +++ .../src/com/coderising/litestruts/struts.xml | 11 ++ 7 files changed, 404 insertions(+) create mode 100644 group23/1028361767/Week1DataStructure/bin/com/coderising/litestruts/struts.xml create mode 100644 group23/1028361767/Week1DataStructure/src/com/coderising/array/ArrayUtil.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/LoginAction.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/Struts.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/View.java create mode 100644 group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/struts.xml diff --git a/group23/1028361767/Week1DataStructure/bin/com/coderising/litestruts/struts.xml b/group23/1028361767/Week1DataStructure/bin/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/bin/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group23/1028361767/Week1DataStructure/src/com/coderising/array/ArrayUtil.java b/group23/1028361767/Week1DataStructure/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..e5ddb476a6 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + return null; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + return null; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/LoginAction.java b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/Struts.java b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..7757668834 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,181 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + private final static Map actions = new HashMap(); + + @SuppressWarnings("rawtypes") + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + //璇诲彇xml锛屾妸xml淇℃伅鏀惧埌actions + readStrutsXML(); + + Action action = actions.get(actionName); + View view = new View(); + if(action != null){ + Class clazz = Class.forName(action.getClassName()); + Object obj = clazz.newInstance(); + + //鏍规嵁parameters璋冪敤set鏂规硶 + initClass(clazz, obj, action, parameters); + + //璋冪敤execute鏂规硶 + String result = execute(clazz, obj); + //璋冪敤鎵鏈塯et鏂规硶 + Map resultParameters = getResultParameters(clazz, obj); + + view.setJsp(action.getResults().get(result)); + view.setParameters(resultParameters); + } + + return view; + } + + @SuppressWarnings("rawtypes") + private static Map getResultParameters(Class clazz, Object obj) throws Exception { + Map resultParameters = new HashMap(); + Method[] methods = clazz.getMethods(); + String methodName; + String propName; + String propValue; + for(Method method : methods){ + methodName = method.getName(); + if(methodName.startsWith("get") && !"getClass".equals(methodName)){ + propName = firstLetterLowerCase(methodName.substring(3, methodName.length())); + propValue = (String) method.invoke(obj); + resultParameters.put(propName, propValue); + } + } + return resultParameters; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private static String execute(Class clazz, Object obj) throws Exception{ + Method method = clazz.getMethod("execute"); + return (String)method.invoke(obj); + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private static void initClass(Class clazz, Object obj, Action action, Map parameters) throws Exception { + String setMethodName; + Method method = null; + for (String parameter : parameters.keySet()) { + setMethodName = "set" + firstLetterUpperCase(parameter); + method = clazz.getMethod(setMethodName, String.class); + method.invoke(obj, parameters.get(parameter)); + } + } + + private static String firstLetterLowerCase(String string){ + char[] cs = string.toCharArray(); + cs[0] += 32; + return String.valueOf(cs); + } + + private static String firstLetterUpperCase(String string){ + char[] cs = string.toCharArray(); + cs[0] -= 32; + return String.valueOf(cs); + } + + @SuppressWarnings("unchecked") + private static void readStrutsXML() { + SAXReader saxReader = new SAXReader(); + Document document; + try { + document = saxReader.read(new File("src/com/coderising/litestruts/struts.xml")); + } catch (DocumentException e) { + System.out.println("error:file not found"); + return ; + } + Element root = document.getRootElement(); + Iterator actionItr = root.elementIterator(); + Element action; + Action act; + Iterator resultItr; + Element result; + Map results; + while(actionItr.hasNext()){ + action = actionItr.next(); + + resultItr = action.elementIterator(); + results = new HashMap(); + while(resultItr.hasNext()){ + result = resultItr.next(); + results.put(result.attributeValue("name"), result.getStringValue()); + } + + act = new Action(); + act.setName(action.attributeValue("name")); + act.setClassName(action.attributeValue("class")); + act.setResults(results); + + actions.put(act.getName(), act); + } + } + + static class Action { + + private String name; + + private String className; + + private Map results = new HashMap<>(); + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + } + +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/StrutsTest.java b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..92a6758a69 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/View.java b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/struts.xml b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group23/1028361767/Week1DataStructure/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From a319d115027b9623ae3f34afe205970d3618a050 Mon Sep 17 00:00:00 2001 From: Fred Qin Date: Mon, 13 Mar 2017 09:50:42 -0700 Subject: [PATCH 071/155] update to downloader --- .../coderising/download/DownloadThread.java | 33 +++++++--- .../coderising/download/FileDownloader.java | 62 +++++++++++++------ .../download/FileDownloaderTest.java | 2 +- .../download/impl/ConnectionImpl.java | 25 +++++--- 4 files changed, 85 insertions(+), 37 deletions(-) diff --git a/group11/395443277/src/com/coderising/download/DownloadThread.java b/group11/395443277/src/com/coderising/download/DownloadThread.java index 8428d2ada8..e977d0ef8b 100644 --- a/group11/395443277/src/com/coderising/download/DownloadThread.java +++ b/group11/395443277/src/com/coderising/download/DownloadThread.java @@ -2,6 +2,8 @@ import java.io.IOException; import java.io.RandomAccessFile; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; import com.coderising.download.api.Connection; @@ -10,26 +12,43 @@ public class DownloadThread extends Thread { int startPos; int endPos; String filePath; + CyclicBarrier barrier; + byte[] totalBytes; - public DownloadThread(Connection conn, int startPos, int endPos, String filePath) { + public DownloadThread(Connection conn, int startPos, int endPos, String filePath, CyclicBarrier barrier, + byte[] totalBytes) { this.conn = conn; this.startPos = startPos; this.endPos = endPos; this.filePath = filePath; + this.barrier = barrier; + this.totalBytes = totalBytes; } public void run() { try { - synchronized (filePath.getClass()) { - byte[] bytes = conn.read(startPos, endPos); - RandomAccessFile RAFile = new RandomAccessFile(filePath, "rw"); - RAFile.seek(startPos); - RAFile.write(bytes, 0, bytes.length); - RAFile.close(); + byte[] bytes = conn.read(startPos, endPos); + System.out.println(bytes.length); + + int i = 0; + while (startPos <= endPos) { + totalBytes[startPos] = bytes[i]; + i++; + startPos++; } + // RandomAccessFile RAFile = new RandomAccessFile(filePath, "rw"); + // RAFile.seek(startPos); + // RAFile.write(bytes, 0, bytes.length); + // RAFile.close(); + + barrier.await(); } catch (IOException e) { e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); } } } diff --git a/group11/395443277/src/com/coderising/download/FileDownloader.java b/group11/395443277/src/com/coderising/download/FileDownloader.java index 7cd70f4b81..09095f5d85 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloader.java +++ b/group11/395443277/src/com/coderising/download/FileDownloader.java @@ -1,7 +1,11 @@ package com.coderising.download; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; import com.coderising.download.api.Connection; import com.coderising.download.api.ConnectionException; @@ -39,33 +43,51 @@ public void execute() { Connection conn = null; try { + final int NUM_THREADS = 2; + conn = cm.open(this.url); int length = conn.getContentLength(); + System.out.println("total file size is: " + length); // Separate length to 3 - int partLen = (int) Math.ceil(length / 3); + int partLen = (int) Math.ceil(length / NUM_THREADS); // create a file - String filePath = "D://java_learning//test.jpg"; - - // create three threads -// DownloadThread t1 = new DownloadThread(cm.open(this.url), 0, partLen - 1, filePath); -// t1.start(); -// t1.join(); -// -// DownloadThread t2 = new DownloadThread(cm.open(this.url), partLen, partLen * 2 - 1, filePath); -// t2.start(); -// t2.join(); -// -// DownloadThread t3 =new DownloadThread(cm.open(this.url), partLen * 2, length - 1, filePath); -// t3.start(); -// t3.join(); - - new DownloadThread(cm.open(this.url), 0, length-1, filePath).start(); - -// getListener().notifyFinished(); - + String filePath = "D://java_learning//test1.jpg"; + byte[] totalBytes = new byte[length]; + + Runnable barrierAction = new Runnable() { + @Override + public void run() { + FileOutputStream fos; + try { + fos = new FileOutputStream(filePath); + fos.write(totalBytes); + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + getListener().notifyFinished(); + } + }; + + CyclicBarrier barrier = new CyclicBarrier(3, barrierAction); + + new DownloadThread(conn, 0, partLen - 1, filePath, barrier, totalBytes).start(); + new DownloadThread(conn, partLen, partLen * 2 - 1, filePath, barrier, totalBytes).start(); + new DownloadThread(conn, partLen * 2, length - 1, filePath, barrier, totalBytes).start(); + + // CyclicBarrier barrier = new CyclicBarrier(2, barrierAction); + // new DownloadThread(conn, 0, partLen - 1, filePath, barrier, + // totalBytes).start(); + // new DownloadThread(conn, partLen, length - 1, filePath, barrier, + // totalBytes).start(); + + // new DownloadThread(cm.open(this.url), 0, length - 1, filePath, + // barrier).start(); + } catch (ConnectionException e) { e.printStackTrace(); } finally { diff --git a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java index 7e4d8d789d..dc3d322a00 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java +++ b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java @@ -23,7 +23,7 @@ public void testDownload() { // String url = "https://s-media-cache-ak0.pinimg.com/564x/8e/bd/00/8ebd00b1f2ef862b6c80d57c2b45d129.jpg"; // http://wallpapercraze.com/images/wallpapers/dota2_nevermore_w1.jpeg - String url = "https://i.ytimg.com/vi/xMV2s5f3f0E/hqdefault.jpg"; + String url = "https://s-media-cache-ak0.pinimg.com/736x/7c/21/ac/7c21acd1cb970f372ef763c08522b860.jpg"; FileDownloader downloader = new FileDownloader(url); diff --git a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java index cde4246282..af15057fdc 100644 --- a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java @@ -1,5 +1,6 @@ package com.coderising.download.impl; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; @@ -8,13 +9,11 @@ public class ConnectionImpl implements Connection { HttpURLConnection urlcon = null; - InputStream is; public ConnectionImpl(String url) { try { URL imgUrl = new URL(url); urlcon = (HttpURLConnection) imgUrl.openConnection(); - is = urlcon.getInputStream(); } catch (Exception e) { System.out.println(e); } @@ -24,13 +23,21 @@ public ConnectionImpl(String url) { public byte[] read(int startPos, int endPos) throws IOException { byte[] bytes = new byte[endPos - startPos + 1]; + InputStream is = new BufferedInputStream(urlcon.getInputStream()); + // Read in the bytes int offset = startPos; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } - + + System.out.println("start is: " + startPos); + +// for (int i=0; i Date: Tue, 14 Mar 2017 01:00:08 +0800 Subject: [PATCH 072/155] =?UTF-8?q?ArrayUtil=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homework0313/litestruts/ArrayUtil.java | 238 ++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0313/litestruts/ArrayUtil.java diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0313/litestruts/ArrayUtil.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0313/litestruts/ArrayUtil.java new file mode 100644 index 0000000000..5523a35536 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0313/litestruts/ArrayUtil.java @@ -0,0 +1,238 @@ +package me.lzb.homework0313.litestruts; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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) { + int[] target = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + target[i] = origin[origin.length - 1 - i]; + } + origin = target; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int l = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + l = l + 1; + } + } + + int[] target = new int[l]; + + int a = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + target[a] = oldArray[i]; + a = a + 1; + } + } + + return target; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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) { + + + int[] tmp = new int[array1.length + array2.length]; + + + int mini = 0; + int a1 = array1[0]; + int a2 = array2[0]; + + if(a1 < a2){ + mini = a1; + }else { + mini = a2; + } + + tmp[0] = mini; + + int l3 = 0; + + for (int i = 1; i < array1.length + array2.length; i++) { + +// if(mini >= array1[l1 - 1] && mini >= array2[l2 - 1]){ +// l3 = i; +// break; +// } + + int oldMin = mini; + + + + int aa1 = mini; + if(mini < array1[array1.length - 1] ){ + for (int j = 0; j < array1.length; j++) { + if(array1[j] > mini){ + aa1 = array1[j]; + break; + } + } + + } + + int aa2 = mini; + if(mini < array2[array2.length - 1] ){ + for (int j = 0; j < array2.length; j++) { + if(array2[j] > mini){ + aa2 = array2[j]; + break; + } + } + } + + + if(aa1 != oldMin && aa2 != oldMin){ + if(aa1 < aa2){ + mini = aa1; + }else { + mini = aa2; + } + }else if(aa1 != oldMin){ + mini = aa1; + }else { + mini = aa2; + } + + + if(oldMin == mini){ + l3 = i; + break; + } + + tmp[i] = mini; + } + + int[] result = new int[l3]; + + System.arraycopy(tmp, 0, result, 0, l3); + + + + return result; + } + + + + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int newArray[] = new int[oldArray.length + size]; + + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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]; + } + + int[] result = {1, 1}; + + + int i = 2; + + int n = 0; + + while (n < max){ + int[] t = new int[result.length + 1]; + System.arraycopy(result, 0, t, 0, result.length); + n = t[i-1] + t[i - 2]; + + if(n >= max){ + return result; + } + + t[i] = n; + + result = t; + i = i + 1; + } + + return null; + } + + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @return + */ + public String join(int[] array, String seperator) { + return null; + } + + +} From 22245ac32561f68c0e971d4c74d3ec1bd2f1831a Mon Sep 17 00:00:00 2001 From: Fred Qin Date: Mon, 13 Mar 2017 10:36:45 -0700 Subject: [PATCH 073/155] try to add range to get byte --- .../coderising/download/DownloadThread.java | 24 +++++++++---------- .../coderising/download/FileDownloader.java | 24 +++++++++---------- .../download/FileDownloaderTest.java | 2 +- .../download/impl/ConnectionImpl.java | 2 +- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/group11/395443277/src/com/coderising/download/DownloadThread.java b/group11/395443277/src/com/coderising/download/DownloadThread.java index e977d0ef8b..3d9bd4b921 100644 --- a/group11/395443277/src/com/coderising/download/DownloadThread.java +++ b/group11/395443277/src/com/coderising/download/DownloadThread.java @@ -28,18 +28,18 @@ public DownloadThread(Connection conn, int startPos, int endPos, String filePath public void run() { try { byte[] bytes = conn.read(startPos, endPos); - System.out.println(bytes.length); - - int i = 0; - while (startPos <= endPos) { - totalBytes[startPos] = bytes[i]; - i++; - startPos++; - } - // RandomAccessFile RAFile = new RandomAccessFile(filePath, "rw"); - // RAFile.seek(startPos); - // RAFile.write(bytes, 0, bytes.length); - // RAFile.close(); +// System.out.println(bytes.length); +// +// int i = 0; +// while (startPos <= endPos) { +// totalBytes[startPos] = bytes[i]; +// i++; +// startPos++; +// } + RandomAccessFile RAFile = new RandomAccessFile(filePath, "rw"); + RAFile.seek(startPos); + RAFile.write(bytes, 0, bytes.length); + RAFile.close(); barrier.await(); diff --git a/group11/395443277/src/com/coderising/download/FileDownloader.java b/group11/395443277/src/com/coderising/download/FileDownloader.java index 09095f5d85..60c80aacf6 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloader.java +++ b/group11/395443277/src/com/coderising/download/FileDownloader.java @@ -54,20 +54,20 @@ public void execute() { int partLen = (int) Math.ceil(length / NUM_THREADS); // create a file - String filePath = "D://java_learning//test1.jpg"; + String filePath = "D://java_learning//test1.jpeg"; byte[] totalBytes = new byte[length]; Runnable barrierAction = new Runnable() { @Override public void run() { - FileOutputStream fos; - try { - fos = new FileOutputStream(filePath); - fos.write(totalBytes); - fos.close(); - } catch (Exception e) { - e.printStackTrace(); - } +// FileOutputStream fos; +// try { +// fos = new FileOutputStream(filePath); +// fos.write(totalBytes); +// fos.close(); +// } catch (Exception e) { +// e.printStackTrace(); +// } getListener().notifyFinished(); } @@ -75,9 +75,9 @@ public void run() { CyclicBarrier barrier = new CyclicBarrier(3, barrierAction); - new DownloadThread(conn, 0, partLen - 1, filePath, barrier, totalBytes).start(); - new DownloadThread(conn, partLen, partLen * 2 - 1, filePath, barrier, totalBytes).start(); - new DownloadThread(conn, partLen * 2, length - 1, filePath, barrier, totalBytes).start(); + new DownloadThread(cm.open(this.url), 0, partLen - 1, filePath, barrier, totalBytes).start(); + new DownloadThread(cm.open(this.url), partLen, partLen * 2 - 1, filePath, barrier, totalBytes).start(); + new DownloadThread(cm.open(this.url), partLen * 2, length - 1, filePath, barrier, totalBytes).start(); // CyclicBarrier barrier = new CyclicBarrier(2, barrierAction); // new DownloadThread(conn, 0, partLen - 1, filePath, barrier, diff --git a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java index dc3d322a00..80e751805a 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java +++ b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java @@ -23,7 +23,7 @@ public void testDownload() { // String url = "https://s-media-cache-ak0.pinimg.com/564x/8e/bd/00/8ebd00b1f2ef862b6c80d57c2b45d129.jpg"; // http://wallpapercraze.com/images/wallpapers/dota2_nevermore_w1.jpeg - String url = "https://s-media-cache-ak0.pinimg.com/736x/7c/21/ac/7c21acd1cb970f372ef763c08522b860.jpg"; + String url = "http://wallpapercraze.com/images/wallpapers/dota2_nevermore_w1.jpeg"; FileDownloader downloader = new FileDownloader(url); diff --git a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java index af15057fdc..ae98b63f77 100644 --- a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java @@ -22,7 +22,7 @@ public ConnectionImpl(String url) { @Override public byte[] read(int startPos, int endPos) throws IOException { byte[] bytes = new byte[endPos - startPos + 1]; - + urlcon.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); InputStream is = new BufferedInputStream(urlcon.getInputStream()); // Read in the bytes From 9ebaf5bdfb97617096062a6aee9490021ae50924 Mon Sep 17 00:00:00 2001 From: ddcv587 Date: Tue, 14 Mar 2017 09:14:37 +0800 Subject: [PATCH 074/155] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/171535320/DataStruct/LinkedList.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/group11/171535320/DataStruct/LinkedList.java b/group11/171535320/DataStruct/LinkedList.java index 1b57d15b50..2b1c2a25ae 100644 --- a/group11/171535320/DataStruct/LinkedList.java +++ b/group11/171535320/DataStruct/LinkedList.java @@ -264,7 +264,17 @@ public void subtract(java.util.LinkedList list){ * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 */ public void removeDuplicateValues(){ - + Node temp1 = head; + Node temp2 = head.next; + while(temp2 != null) { + if(temp1.data == temp2.data) { + temp2 = temp2.next; + } else { + temp1.next = temp2; + temp1 = temp2; + temp2 = temp2.next; + } + } } /** From 18ad855f2b55d230141acccaac3091256a1d7c1d Mon Sep 17 00:00:00 2001 From: unknown <绠滄兂> Date: Tue, 14 Mar 2017 14:18:47 +0800 Subject: [PATCH 075/155] =?UTF-8?q?77253242=5F=E6=8F=90=E4=BA=A4=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../77253242/coding2017/src/ArrayList.java | 74 ++++++ .../coding2017/src/BinaryTreeNode.java | 33 +++ group22/77253242/coding2017/src/Iterator.java | 9 + .../77253242/coding2017/src/LinkedList.java | 238 ++++++++++++++++++ group22/77253242/coding2017/src/List.java | 10 + group22/77253242/coding2017/src/Main.java | 9 + group22/77253242/coding2017/src/Queue.java | 25 ++ group22/77253242/coding2017/src/Stack.java | 34 +++ 9 files changed, 433 insertions(+) create mode 100644 group22/77253242/coding2017/src/ArrayList.java create mode 100644 group22/77253242/coding2017/src/BinaryTreeNode.java create mode 100644 group22/77253242/coding2017/src/Iterator.java create mode 100644 group22/77253242/coding2017/src/LinkedList.java create mode 100644 group22/77253242/coding2017/src/List.java create mode 100644 group22/77253242/coding2017/src/Main.java create mode 100644 group22/77253242/coding2017/src/Queue.java create mode 100644 group22/77253242/coding2017/src/Stack.java diff --git a/.gitignore b/.gitignore index 793b2e270c..a697b49249 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ target group22/1193590951/githubitem/.project *.prefs group22/1193590951/githubitem/bin/鏂囩珷鍦板潃.txt +!.gitignore \ No newline at end of file diff --git a/group22/77253242/coding2017/src/ArrayList.java b/group22/77253242/coding2017/src/ArrayList.java new file mode 100644 index 0000000000..c911129893 --- /dev/null +++ b/group22/77253242/coding2017/src/ArrayList.java @@ -0,0 +1,74 @@ +import javax.swing.plaf.basic.BasicTreeUI; +import java.security.SignatureException; +import java.util.Arrays; + +/** + * Created by fantasy on 2017-03-13. + */ + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o) { + if(size >= elementData.length) + { + elementData= Arrays.copyOf(elementData, size+10); + } + elementData [++size] = o; + } + + public void add(int index, Object o) { + if(size >= elementData.length) + { + elementData = new Object[size + 10]; + } + if(size > index) + { + elementData[++size] = o; + } + else + { + for(int i = size;i>index;i--) + { + elementData [i + 1] = elementData [i]; + } + } + elementData [index] = o; + size++; + } + + public Object get(int index) { + if(index < 0 || index > size) + { + throw new IndexOutOfBoundsException("size:" + size + ",index:"+index); + } + return elementData[size]; + } + + public Object remove(int index) { + if(size > index) + { + Object OldValue = elementData[index]; + for(int i = index;i<= size;) + { + elementData[i] = elementData[++i]; + } + return OldValue; + } + throw new IndexOutOfBoundsException("size:"+ size + ",index:" + index); + } + + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + +} \ No newline at end of file diff --git a/group22/77253242/coding2017/src/BinaryTreeNode.java b/group22/77253242/coding2017/src/BinaryTreeNode.java new file mode 100644 index 0000000000..20b7dd99fb --- /dev/null +++ b/group22/77253242/coding2017/src/BinaryTreeNode.java @@ -0,0 +1,33 @@ +/** + * Created by fantasy on 2017-03-13. + */ +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group22/77253242/coding2017/src/Iterator.java b/group22/77253242/coding2017/src/Iterator.java new file mode 100644 index 0000000000..5d970abec8 --- /dev/null +++ b/group22/77253242/coding2017/src/Iterator.java @@ -0,0 +1,9 @@ +/** + * Created by fantasy on 2017-03-13. + */ + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group22/77253242/coding2017/src/LinkedList.java b/group22/77253242/coding2017/src/LinkedList.java new file mode 100644 index 0000000000..e9fd8547d7 --- /dev/null +++ b/group22/77253242/coding2017/src/LinkedList.java @@ -0,0 +1,238 @@ +import java.net.IDN; + +public class LinkedList implements List { + + private Node head; + + private Node firstNode; + private Node lastNode; + private int size = 0; + + + public void add(Object o){ + linkLast(o); + + } + public void add(int index , Object o){ + if(index > size()) + { + throw new IndexOutOfBoundsException("index:"+ index + ",size:"+ size()); + } + if(size == 0) + { + linkLast(o); + } + else + { + Node node = node(index); + Node newNode = new Node(o,node.prev,node); + node.setPrev(newNode); + } + ++size; + } + public Object get(int index){ + return node(index).getData(); + } + public Object remove(int index){ + if(index >size()) + { + throw new IndexOutOfBoundsException("index:"+ index + ",size:"+ size()); + } + Node node = node(index); + node.prev.next = node.next; + node.next.prev = node.prev; + --size; + return node; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + linkLast(o); + } + public void addLast(Object o){ + if(lastNode == null) + { + lastNode = new Node(o,firstNode,null); + } + else + { + Node node = lastNode; + node.next = new Node(o,node,null); + lastNode = node.next; + } + ++size; + } + public Object removeFirst(){ + if(size > 0 ) + { + Node node = firstNode; + firstNode = node.next; + --size; + return node.getData(); + } + return null; + } + public Object removeLast(){ + if(size > 0 ) + { + Node node = lastNode; + lastNode = node.prev; + --size; + return node.getData(); + } + return null; + } + public Iterator iterator(){ + return null; + } + + LinkedList.Node node(int index) + { + if(index < size()) + { + Node node = firstNode; + for(int i=0;i7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + for(int i=0;i5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺listB鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪listB涓嚭鐜扮殑鍏冪礌 + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} \ No newline at end of file diff --git a/group22/77253242/coding2017/src/List.java b/group22/77253242/coding2017/src/List.java new file mode 100644 index 0000000000..c4e9df138a --- /dev/null +++ b/group22/77253242/coding2017/src/List.java @@ -0,0 +1,10 @@ +/** + * Created by fantasy on 2017-03-13. + */ +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(); +} diff --git a/group22/77253242/coding2017/src/Main.java b/group22/77253242/coding2017/src/Main.java new file mode 100644 index 0000000000..1f5e6bdd9c --- /dev/null +++ b/group22/77253242/coding2017/src/Main.java @@ -0,0 +1,9 @@ +import java.util.*; +import java.util.LinkedList; + +public class Main { + + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/group22/77253242/coding2017/src/Queue.java b/group22/77253242/coding2017/src/Queue.java new file mode 100644 index 0000000000..4e0fe62925 --- /dev/null +++ b/group22/77253242/coding2017/src/Queue.java @@ -0,0 +1,25 @@ +import java.awt.image.AreaAveragingScaleFilter; + +/** + * Created by fantasy on 2017-03-13. + */ +public class Queue { + ArrayList arrayList = new ArrayList(); + + + public void enQueue(Object o){ + arrayList.add(o); + } + + public Object deQueue(){ + return arrayList.remove(0); + } + + public boolean isEmpty(){ + return !(arrayList.size() >0); + } + + public int size(){ + return arrayList.size(); + } +} \ No newline at end of file diff --git a/group22/77253242/coding2017/src/Stack.java b/group22/77253242/coding2017/src/Stack.java new file mode 100644 index 0000000000..d203b6ba29 --- /dev/null +++ b/group22/77253242/coding2017/src/Stack.java @@ -0,0 +1,34 @@ +import java.util.EmptyStackException; + +/** + * Created by fantasy on 2017-03-13. + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size() == 0) + { + return elementData.remove(elementData.size() - 1 ); + } + return null; + } + + public Object peek(){ + if(elementData.size() == 0 ) + { + return elementData.get(elementData.size() - 1); + } + throw new EmptyStackException(); + } + public boolean isEmpty(){ + return !(elementData.size()>0); + } + public int size(){ + return elementData.size(); + } +} From 55cd2d45d17c43c1dc9a3b86d64ac602a1801c7f Mon Sep 17 00:00:00 2001 From: johnChnia Date: Tue, 14 Mar 2017 17:42:14 +0800 Subject: [PATCH 076/155] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E5=AE=9E=E7=8E=B0=E5=8F=8A=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=EF=BC=8C=E4=BF=AE=E6=94=B9ArrayList?= =?UTF-8?q?=E3=80=81LinkList=20=E5=AE=9E=E7=8E=B0=20List=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../johnChnia/coding2017/basic/ArrayList.java | 19 +-- .../coding2017/basic/BinarySearchTree.java | 124 ++++++++++++++++++ .../coding2017/basic/LinkedList.java | 29 ++-- .../com/johnChnia/coding2017/basic/List.java | 16 +++ .../com/johnChnia/coding2017/basic/Queue.java | 10 +- .../com/johnChnia/coding2017/basic/Stack.java | 4 +- .../basic/{test => }/ArrayListTest.java | 6 +- .../basic/BinarySearchTreeTest.java | 37 ++++++ .../basic/{test => }/LinkedListTest.java | 3 +- .../basic/{test => }/QueueTest.java | 3 +- .../basic/{test => }/StackTest.java | 2 +- 11 files changed, 221 insertions(+), 32 deletions(-) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java rename group24/315863321/src/test/java/com/johnChnia/coding2017/basic/{test => }/ArrayListTest.java (92%) create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/BinarySearchTreeTest.java rename group24/315863321/src/test/java/com/johnChnia/coding2017/basic/{test => }/LinkedListTest.java (95%) rename group24/315863321/src/test/java/com/johnChnia/coding2017/basic/{test => }/QueueTest.java (93%) rename group24/315863321/src/test/java/com/johnChnia/coding2017/basic/{test => }/StackTest.java (97%) diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java index 4881c6518c..800f89d6ab 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -7,8 +7,8 @@ * @// TODO: 2017/3/15 鏀寔娉涘瀷 */ -public class ArrayList { - private int[] elementData; +public class ArrayList implements List{ + private Object[] elementData; private int size = 0; /** @@ -20,7 +20,7 @@ public class ArrayList { */ public ArrayList(int initialCapacity) { if (initialCapacity > 0) { - elementData = new int[initialCapacity]; + elementData = new Object[initialCapacity]; } else { throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); @@ -34,7 +34,7 @@ public ArrayList(int initialCapacity) { * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ - public int get(int index) { + public Object get(int index) { rangeCheck(index); rangeCheckForAdd(index); return elementData[index]; @@ -46,7 +46,7 @@ public int get(int index) { * * @param element element to be appended to this list */ - public void add(int element) { + public void add(Object element) { ensureCapacityInternal(size + 1); elementData[size++] = element; } @@ -61,7 +61,7 @@ public void add(int element) { * @param index index at which the specified element is to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ - public void add(int element, int index) { + public void add(int index, Object element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); System.arraycopy(elementData, index, elementData, index + 1, @@ -79,9 +79,9 @@ public void add(int element, int index) { * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ - public int remove(int index) { + public Object remove(int index) { rangeCheckForAdd(index); - int oldValue = elementData[index]; + Object oldValue = elementData[index]; int numMoved = size() - index - 1; if (numMoved > 0) { System.arraycopy(elementData, index + 1, elementData, index, @@ -115,7 +115,8 @@ public int size() { * number of elements specified by the double length of list. */ private void grow() { - elementData = Arrays.copyOf(elementData, 2 * elementData.length); + elementData = Arrays.copyOf(elementData, + 2 * elementData.length); } public String toString() { diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java new file mode 100644 index 0000000000..48d3e41b12 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java @@ -0,0 +1,124 @@ +package com.johnChnia.coding2017.basic; + +/** + * Created by john on 2017/3/13. + */ +public class BinarySearchTree { + + + /** + * The root node of tree. + */ + public BstNode root; + + private Queue q = new Queue(); + + + private static class BstNode { + private int data; + private BstNode left; + private BstNode right; + + @Override + public String toString() { + return String.valueOf(data); + } + } + + /** + * create an BinarySearchTree. + * + * @param root root node of tree + * @param data stored element + * @return binarySearchTree + */ + public BstNode insert(BstNode root, int data) { + if (root == null) { + root = getNewBstNode(data); + if (this.root == null) { + this.root = root; + } + return root; + } + if (data <= root.data) { + root.left = insert(root.left, data); + } else { + root.right = insert(root.right, data); + } + return root; + } + + private BstNode getNewBstNode(int data) { + BstNode node = new BstNode(); + node.data = data; + node.left = null; + node.right = null; + return node; + } + + /** + * Returns the minimum value in the tree. + * + * @param root root node of the tree銆 + * @return the minimum value in the tree + * @throws IllegalArgumentException if root is null. + */ + public int findMin(BstNode root) { + int min; + if (root == null) { + throw new IllegalArgumentException("tree is empty"); + } else if (root.left == null) { + min = root.data; + } else { + min = findMin(root.left); + } + return min; + } + + /** + * Returns the maximum value in the tree. + * + * @param root root node of the tree銆 + * @return the maximum value in the tree + * @throws IllegalArgumentException if root is null. + */ + public int findMax(BstNode root) { + int max; + if (root == null) { + throw new IllegalArgumentException("tree is empty"); + } else if (root.right == null) { + max = root.data; + } else { + max = findMax(root.right); + } + return max; + } + + + /** + * Traverse each node from left to right. + * + * @param root root node of the tree + */ + public void LevelOrder(BstNode root) { + if (root == null) { + return; + } + q.add(root); + while (!q.empty()) { + BstNode current = (BstNode) q.peek(); + if (current.left != null) { + q.add(current.left); + } + if (current.right != null) { + q.add(current.right); + } + q.remove(); + } + } + + public BstNode getRoot() { + return root; + } + +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java index f2825659b9..79109cc5c8 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java @@ -8,7 +8,7 @@ * @// TODO: 2017/3/15 鏀寔娉涘瀷 */ -public class LinkedList { +public class LinkedList implements List { private Node first = null; private int size = 0; @@ -21,7 +21,7 @@ public LinkedList() { } private static class Node { - int element; + Object element; Node next; Node prev; } @@ -31,7 +31,7 @@ private static class Node { * * @param element element to be appended to this list */ - public void add(int element) { + public void add(Object element) { Node newNode = new Node(); if (first == null) { addWhenListIsEmpty(newNode, element); @@ -47,7 +47,7 @@ public void add(int element) { size++; } - private void addWhenListIsEmpty(Node newNode, int element) { + private void addWhenListIsEmpty(Node newNode, Object element) { first = newNode; first.element = element; first.next = null; @@ -60,7 +60,7 @@ private void addWhenListIsEmpty(Node newNode, int element) { * * @param element the element to add */ - public void addFirst(int element) { + public void addFirst(Object element) { Node newNode = new Node(); if (first == null) { addWhenListIsEmpty(newNode, element); @@ -85,7 +85,7 @@ public void addFirst(int element) { * @param element element to be inserted. * @throws RuntimeException if list size less than 2. */ - public void add(int index, int element) { + public void add(int index, Object element) { if (size() < 2) throw new RuntimeException("list size should greater than or equal to 2"); isElementIndex(index); @@ -132,16 +132,25 @@ public void remove() { } + /** + * @param index + * @return + * @// TODO: 2018/3/14 if i am happy, i will implement it right now! + */ + public Object remove(int index) { + return null; + } + /** * Removes and returns the first element from this list. * * @return the first element from this list */ - public int removeFirst() { + public Object removeFirst() { Node f = first; if (f == null) throw new NoSuchElementException(); - int element = f.element; + Object element = f.element; Node next = first.next; first.element = 0; first.next = null; // help GC @@ -160,7 +169,7 @@ public int removeFirst() { * @param index index of the element to return * @return the element at the specified position in this list */ - public int get(int index) { + public Object get(int index) { checkElementIndex(index); Node node = first; if (index == 0) { @@ -178,7 +187,7 @@ public int get(int index) { * @return the first element in this list * @throws NoSuchElementException if this list is empty */ - public int getFirst() { + public Object getFirst() { final Node f = first; if (f == null) throw new NoSuchElementException(); diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java new file mode 100644 index 0000000000..5616a17e70 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java @@ -0,0 +1,16 @@ +package com.johnChnia.coding2017.basic; + +/** + * Created by john on 2017/3/12. + */ +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(); +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java index 4fd2558ea3..2551b7a243 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java @@ -26,7 +26,7 @@ public Queue() { * @param element the element to add * @return {@code true} */ - public boolean add(int element) { + public boolean add(Object element) { arrayList.add(element); return true; } @@ -38,7 +38,7 @@ public boolean add(int element) { * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ - public int remove() { + public Object remove() { if (arrayList.empty()) throw new NoSuchElementException(emptyMsg()); return arrayList.remove(0); @@ -52,7 +52,7 @@ public int remove() { * * @return the head of this queue, or {@code 0} if this queue is empty */ - public int peek() { + public Object peek() { if (arrayList.empty()) return 0; return arrayList.get(0); @@ -80,4 +80,8 @@ public int size() { private String emptyMsg() { return "Size: " + size(); } + + public boolean empty() { + return arrayList.size() == 0; + } } diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java index e11be853f9..d6a496217d 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java @@ -33,7 +33,7 @@ public void push(int element) { * @return The object at the top of this stack. * @throws EmptyStackException if this stack is empty. */ - public int pop() { + public Object pop() { if (empty()) { throw new EmptyStackException(); } @@ -47,7 +47,7 @@ public int pop() { * @return the object at the top of this stack. * @throws EmptyStackException if this stack is empty. */ - public int peek() { + public Object peek() { if (empty()) { throw new EmptyStackException(); } diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java similarity index 92% rename from group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java rename to group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java index 13c81724ad..a80003b85e 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java @@ -1,4 +1,4 @@ -package com.johnChnia.coding2017.basic.test; +package com.johnChnia.coding2017.basic; import com.johnChnia.coding2017.basic.ArrayList; import org.junit.Before; @@ -44,7 +44,7 @@ public void testAddElementByIndex() { for (int i = 0; i < 3; i++) { arrayList3.add(10); } - arrayList3.add(1000, 1); + arrayList3.add(1, 1000); assertThat(arrayList3.get(1), equalTo(1000)); } @@ -53,7 +53,7 @@ public void testRemoveElementByIndex() { for (int i = 0; i < 6; i++) { arrayList4.add(i); } - int removed = arrayList4.remove(4); + Object removed = arrayList4.remove(4); System.out.println(arrayList4); assertThat(removed, equalTo(4)); assertThat(arrayList4.size(), equalTo(5)); diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/BinarySearchTreeTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/BinarySearchTreeTest.java new file mode 100644 index 0000000000..d756c31198 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/BinarySearchTreeTest.java @@ -0,0 +1,37 @@ +package com.johnChnia.coding2017.basic; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/14. + */ +public class BinarySearchTreeTest { + + private BinarySearchTree bst; + + @Before + public void setUp() throws Exception { + bst = new BinarySearchTree(); + bst.insert(bst.getRoot(), 10); + bst.insert(bst.getRoot(), 20); + bst.insert(bst.getRoot(), 9); + bst.insert(bst.getRoot(), 11); + bst.insert(bst.getRoot(), 12); + } + + @Test + public void testFindMin() throws Exception { + assertThat(bst.findMin(bst.getRoot()), equalTo(9)); + + } + + @Test + public void testFindMax() throws Exception { + assertThat(bst.findMax(bst.getRoot()), equalTo(20)); + } + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java similarity index 95% rename from group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java rename to group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java index bd8ad5ac4c..b92defffd8 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java @@ -1,6 +1,5 @@ -package com.johnChnia.coding2017.basic.test; +package com.johnChnia.coding2017.basic; -import com.johnChnia.coding2017.basic.LinkedList; import org.junit.Before; import org.junit.Test; diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java similarity index 93% rename from group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java rename to group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java index 54cd7f9385..ebe017b2d6 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java @@ -1,6 +1,5 @@ -package com.johnChnia.coding2017.basic.test; +package com.johnChnia.coding2017.basic; -import com.johnChnia.coding2017.basic.Queue; import org.junit.Before; import org.junit.Test; diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java similarity index 97% rename from group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java rename to group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java index 7fb4a35757..43a7d858d9 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java @@ -1,4 +1,4 @@ -package com.johnChnia.coding2017.basic.test; +package com.johnChnia.coding2017.basic; import com.johnChnia.coding2017.basic.Stack; import org.junit.Before; From 75018e04b984364e4fe3f7ba2d0a3f263e2354ce Mon Sep 17 00:00:00 2001 From: JiaMing Q <809203042@qq.com> Date: Tue, 14 Mar 2017 17:43:55 +0800 Subject: [PATCH 077/155] 809203042first homework --- group24/809203042/.classpath | 6 + group24/809203042/.gitignore | 1 + group24/809203042/.project | 17 ++ .../basic/structures/MyArrayList.java | 99 ++++++++++ .../basic/structures/MyBinaryTree.java | 154 ++++++++++++++++ .../basic/structures/MyLinkedList.java | 172 ++++++++++++++++++ .../coding2017/basic/structures/MyList.java | 23 +++ .../coding2017/basic/structures/MyQueue.java | 30 +++ .../coding2017/basic/structures/MyStack.java | 41 +++++ .../basic/structurestest/MyArrayListTest.java | 32 ++++ .../structurestest/MyBinaryTreeTest.java | 23 +++ .../structurestest/MyLinkedListTest.java | 35 ++++ .../basic/structurestest/MyQueueTest.java | 9 + .../basic/structurestest/MyStackTest.java | 25 +++ 14 files changed, 667 insertions(+) create mode 100644 group24/809203042/.classpath create mode 100644 group24/809203042/.gitignore create mode 100644 group24/809203042/.project create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyLinkedList.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyList.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyQueue.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyStack.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyBinaryTreeTest.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyLinkedListTest.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyQueueTest.java create mode 100644 group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyStackTest.java diff --git a/group24/809203042/.classpath b/group24/809203042/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group24/809203042/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group24/809203042/.gitignore b/group24/809203042/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group24/809203042/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group24/809203042/.project b/group24/809203042/.project new file mode 100644 index 0000000000..28fba6c7a5 --- /dev/null +++ b/group24/809203042/.project @@ -0,0 +1,17 @@ + + + 809203042Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java new file mode 100644 index 0000000000..711dbfd0e9 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java @@ -0,0 +1,99 @@ +package com.github.qq809203042.coding2017.basic.structures; + +import java.util.Arrays; + +/* + * 鏍规嵁api涓璦rraylist鐨勬柟娉曟弿杩帮紝瀹炵幇涓涓嚜宸辩殑arraylist + * 鍩轰簬鏁扮粍瀹炵幇 + * + */ +public class MyArrayList implements MyList { + // 瀹氫箟涓涓鏈夋暟缁勶紝鍒濆闀垮害涓10 + private Object[] elementData = new Object[10]; + // 瀹氫箟鍙橀噺璁板綍ArrayList鐨勯暱搴 + private int size = 0; + + // 鑾峰彇鎸囧畾绱㈠紩涓婄殑鍏冪礌鐨勫 + @Override + public Object get(int index) { + if (index >= 0 && index < size) { + return elementData[index]; + } else { + throw new IndexOutOfBoundsException("鏌ヨ鐨勭储寮曚笉瀛樺湪"); + } + } +// 鍚戝垪琛ㄥ熬閮ㄦ坊鍔犲厓绱 + @Override + public boolean add(Object obj) { + if (size >= elementData.length) {// 鑻ize澶т簬绛変簬鐜版湁鏁扮粍闀垮害锛屽垯灏嗘暟缁勬墿瀹瑰悗鍐嶈繘琛屾搷浣 + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + elementData[size] = obj; + size++; + return true; +// 浠涔堟椂鍊欎細杩斿洖false锛屽浣曡繑鍥瀎alse锛 + } +// 鍚戝垪琛ㄦ寚瀹氫綅缃坊鍔犲厓绱 + @Override + public boolean add(Object obj,int index) { + if (size >= elementData.length) {// 鑻ize澶т簬绛変簬鐜版湁鏁扮粍闀垮害锛屽垯灏嗘暟缁勬墿瀹瑰悗鍐嶈繘琛屾搷浣 + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } +// 灏嗕粠index寮濮嬬殑鎵鏈夊厓绱犲悜鍚庣Щ鍔ㄤ竴浣 + moveBackward(elementData,index,size-1,1); +// 灏嗗厓绱犳坊鍔犲埌鎸囧畾浣嶇疆 + elementData[index] = obj; + size++; + return true; + } + +// 鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱 + @Override + public Object remove(int index) { + if(size == 0){ + throw new IndexOutOfBoundsException("鍒楄〃涓虹┖"); + } + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("鎯宠鍒犻櫎鐨勫厓绱犵储寮曚笉瀛樺湪"); + } + Object removeElement = elementData[index]; + moveForward(elementData,index+1,size-1,1); + size--; + return removeElement; + } + + @Override + public int size() { + + return size; + } +// 鍒ゆ柇鍒楄〃鏄惁涓虹┖ + @Override + public boolean isEmpty() { + + return size == 0; + } +// 灏嗘暟缁勪粠startPos浣嶇疆鍒癳ndPos浣嶇疆鐨勫厓绱犲悜鍚庣Щ鍔╯tep姝ラ暱 + private void moveBackward(Object[] elementData, int startPos, int endPos, int step) { + for(int i = endPos + step; i >= startPos + step; i--){ + elementData[i] = elementData[i-step]; + } + + } +// 灏嗘暟缁勪粠startPos浣嶇疆鍒癳ndPos浣嶇疆鐨勫厓绱犲悜鍓嶇Щ鍔╯tep姝ラ暱 + private void moveForward(Object[] elementData, int startPos, int endPos, int step) { + for(int i = startPos - step; i <= endPos - step; i++){ + elementData[i] = elementData[i+step]; + } + + } + @Override + public String toString() { +// return Arrays.toString(elementData); + Object[] myArrayList = Arrays.copyOf(elementData, size); + return Arrays.toString(myArrayList); + + } + + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java new file mode 100644 index 0000000000..c5109cb954 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java @@ -0,0 +1,154 @@ +package com.github.qq809203042.coding2017.basic.structures; + + +/* + * 瀹炵幇浜屽弶鏍, + * 鍙瓨鍌╥nt绫诲瀷 + * 鍐呴儴绫讳唬琛ㄦ瘡涓妭鐐癸細姣忎釜鑺傜偣鍚湁涓涓敭锛屼竴涓硷紝涓鏉″乏閾炬帴锛屼竴鏉″彸閾炬帴 + * + * 瀹炵幇鎻掑叆鍊兼搷浣滐細褰搑oot涓虹┖鏃舵彃鍦╮oot涓婏細纭畾鏌愪竴鑺傜偣涓婃槸鍚︿负绌 + * + */ +public class MyBinaryTree { + private BinaryTreeNode root ; + private int size; + +// 鏋勯犲嚱鏁 + public MyBinaryTree(){ + + } + + public boolean getValue(Integer key){ + return getValue(root,key); + } + + private boolean getValue(BinaryTreeNode node,Integer key){ + if(node == null){//濡傛灉涓虹┖锛屽垯涓虹┖锛屾病鏈夎鍏冪礌 + return false; + } + Integer value = node.getValue(); + if(value == key){//鎵惧埌 + return true; + } + else if(value.compareTo(key) > 0){//濡傛灉灏忎簬璇ヨ妭鐐瑰璞★紝姣旇緝宸﹁妭鐐 + return getValue(node.left,key); + }else{ + return getValue(node.right,key);//濡傛灉灏忎簬璇ヨ妭鐐瑰璞★紝姣旇緝鍙宠妭鐐 + } + + } + + public void add(Integer key){ + root = add(root, key); + + } + + private BinaryTreeNode add(BinaryTreeNode node, Integer key) { + if(node == null){//鑻ユ病鏈夎鑺傜偣锛屽垯鍒涘缓骞舵坊鍔犳暟鎹嚦鑺傜偣涓 + node = new BinaryTreeNode(key); + size++; + return node; + } + Integer value = node.getValue(); + if(value.compareTo(key) > 0){ + node.left =add(node.left,key); + }else if(value.compareTo(key) < 0){ + node.right=add(node.right,key); + } + return node; + } + + public int size(){ + return size; + } + + +// 鍓嶅簭閬嶅巻 + public void preOrder(){ + preOrder(root); + System.out.println(); + } +// 涓簭閬嶅巻 + public void midOrder(){ + midOrder(root); + System.out.println(); + } +// 鍚庡簭閬嶅巻 + public void aftOrder(){ + aftOrder(root); + System.out.println(); + } + +// 鍓嶅簭閬嶅巻 + private void preOrder(BinaryTreeNode node){ + if(node == null){ + return; + } + System.out.print(node.value + " "); + preOrder(node.left); + preOrder(node.right); + + } +// 涓簭閬嶅巻 + private void midOrder(BinaryTreeNode node){ + if(node == null){ + return; + } + midOrder(node.left); + System.out.print(node.value + " "); + midOrder(node.right); + + } +// 鍚庡簭閬嶅巻 + private void aftOrder(BinaryTreeNode node){ + if(node == null){ + return; + } + aftOrder(node.left); + aftOrder(node.right); + System.out.print(node.value + " "); + + } + + + + // 浜屽弶鏍戠殑鑺傜偣瀵硅薄锛氭瘡涓妭鐐瑰惈鏈変竴涓敭锛屼竴涓硷紝涓鏉″乏閾炬帴锛屼竴鏉″彸閾炬帴鍜屼竴涓妭鐐硅鏁板櫒 + private static class BinaryTreeNode { + private Integer value; + + private BinaryTreeNode left; + private BinaryTreeNode right; +// 鏋勯犲嚱鏁 + BinaryTreeNode(Integer value){ + this.value = value; + } + + + + public Integer getValue() { + return value; + } + public void setValue(Integer value) { + this.value = value; + } + + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Integer value) { + this.value = value; + return null; + } + + } +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyLinkedList.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyLinkedList.java new file mode 100644 index 0000000000..79f80df63e --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyLinkedList.java @@ -0,0 +1,172 @@ +package com.github.qq809203042.coding2017.basic.structures; + +/* + * 鏍规嵁API瀹炵幇鑷繁鐨凩inkedList鏁版嵁缁撴瀯 + */ +public class MyLinkedList implements MyList { + // 澶磋妭鐐瑰璞★紝涓嶅瓨鍌ㄥ厓绱狅紝鐢ㄤ簬鎸囩ず閾捐〃鐨勮捣濮嬩綅缃 + private Node head = new Node(null,null); + // 灏捐妭鐐瑰璞 + private Node last = new Node(null,null); + // 閾捐〃闀垮害 + int size = 0; + + @Override + public Object get(int index) { + Node node = node(index); + return node.data; + } + + @Override + public boolean add(Object obj) { + addLast(obj); + return true; + } + + @Override + public boolean add(Object obj, int index) { + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("涓嬫爣瓒婄晫"); + } + if(index == size){//绛夊悓浜庢坊鍔犲埌鏈熬 + addLast(obj); + return true; + } + Node nodeIndex = node(index);//鑾峰彇index澶勭殑鑺傜偣 + Node newNode = new Node(obj,nodeIndex);//鏂板鍏冪礌鐨刵ext鎸囧悜鍘熸潵index澶 + if(index == 0){ + head.next = newNode; + }else{ + Node nodeBeforeIndex = node(index-1); + nodeBeforeIndex.next = newNode; + } + size++; + return true; + } + + @Override + public Object remove(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("涓嬫爣瓒婄晫"); + } + if(index == 0){ + return removeFirst(); + }else{ + Node nodeIndex = node(index); + Object removeData = nodeIndex.data; + node(index-1).next = nodeIndex.next; + size--; + return removeData; + } + } + + @Override + public int size() { + + return size; + } + + @Override + public boolean isEmpty() { + + return size == 0; + } + + public void addFirst(Object obj) { + // 鍒涘缓涓涓复鏃惰妭鐐瑰瓨鍌ㄧ涓涓妭鐐 + Node tempFirst = head.next; + // 鍒涘缓鎵闇娣诲姞鍏冪礌鐨勮妭鐐瑰璞 + Node newNode = new Node(obj, tempFirst); + head.next = newNode; + if (tempFirst == null) {// 濡傛灉閾捐〃涓病鏈夊厓绱狅紝鍒欏皢娣诲姞鐨勮妭鐐瑰悓鏃惰祴鍊间负灏捐妭鐐 + last = newNode; + } + size++; + } + + public void addLast(Object obj) { + // 鍒涘缓涓涓复鏃惰妭鐐瑰瓨鍌ㄥ熬鑺傜偣 + Node tempLast = last; + // 鍒涘缓鎵闇娣诲姞鍏冪礌鐨勮妭鐐瑰璞 + Node newNode = new Node(obj, null); + last = newNode; + if (size == 0) {// 濡傛灉閾捐〃涓病鏈夊厓绱狅紝鍒欏皢娣诲姞鐨勮妭鐐瑰悓鏃惰祴鍊间负澶磋妭鐐 + head.next = newNode; + } else {// 濡傛灉閾捐〃涓師鍏堝瓨鍦ㄥ厓绱狅紝鍒欏皢鍘熸潵灏捐妭鐐圭殑next鎸囧悜鐜板湪鐨勮妭鐐 + tempLast.next = newNode; + } + size++; + + } + + public Object removeFirst() { + if(size == 0){ + throw new NullPointerException("閾捐〃涓虹┖"); + } + Node removeNode =head.next; + Object removeData = removeNode.data; + head.next = removeNode.next; + size--; + return removeData; + } + + public Object removeLast() { + if(size <= 1){ + return removeFirst(); + }else{ + Object removeData = last.data; + last = node(size-2); + last.next = null; + size--; + return removeData; + } + } + + + + + @Override + public String toString() { + if(size == 0){ + return "[]"; + } + StringBuffer listToString = new StringBuffer(); + listToString.append("["); + Node node = head; + for(int i = 0; i < size;i++){ + node = node.next; + listToString.append(node.data); + if(i= size){ + throw new IndexOutOfBoundsException("涓嬫爣瓒婄晫"); + } + Node node = head.next; + for(int i = 1; i <= index; i++){ + node = node.next; + } + return node; + } +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyList.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyList.java new file mode 100644 index 0000000000..13bd0920c5 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyList.java @@ -0,0 +1,23 @@ +package com.github.qq809203042.coding2017.basic.structures; +/* + * 鑷畾涔塴ist鎺ュ彛 + */ +public interface MyList { +// 鏌 + public abstract Object get(int index); + +// 澧 + public abstract boolean add(Object obj); +// 鍦ㄦ寚瀹氫綅缃 + public abstract boolean add(Object obj,int index); + +// 鍒犻櫎 + public abstract Object remove(int index); + +// 鍒ゆ柇澶у皬 + public abstract int size(); +// 鍒ゆ柇鏄惁涓虹┖ + public abstract boolean isEmpty(); + + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyQueue.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyQueue.java new file mode 100644 index 0000000000..cced80f1e8 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyQueue.java @@ -0,0 +1,30 @@ +package com.github.qq809203042.coding2017.basic.structures; + +import java.util.LinkedList; + +/* + * 瀹炵幇闃熷垪鐨勭粨鏋勶紝浣跨敤閾捐〃缁撴瀯 + */ +public class MyQueue { + private LinkedList queueList = new LinkedList<>(); + private int size; +// 杩涘叆闃熷垪 + public void enQueue(Object obj){ + queueList.addLast(obj); + size++; + } + + public Object deQueue(){ + Object removeElement = queueList.removeFirst(); + size--; + return removeElement; + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyStack.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyStack.java new file mode 100644 index 0000000000..5657c91b50 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyStack.java @@ -0,0 +1,41 @@ +package com.github.qq809203042.coding2017.basic.structures; + +import java.util.ArrayList; + +public class MyStack { + private ArrayList elementData = new ArrayList(); +// 鏍堟寚閽 + private int pos; +// 鍘嬫爤 + public Object push(Object obj) { + elementData.add(obj); + pos++; + return obj; + } +// 寮规爤 + public Object pop() { + if(isEmpty()){ + throw new StackOverflowError("鏍堟孩鍑"); + } + return elementData.remove(--pos); + } +// 杩斿洖鏍堥《瀵硅薄 + public Object peek() { + int topIndex = pos -1; + return elementData.get(topIndex); + } + + public boolean isEmpty() { + return pos == 0; + } + + public int size() { + return pos; + } + @Override + public String toString() { + return elementData.toString(); + } + + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java new file mode 100644 index 0000000000..cda34461ea --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java @@ -0,0 +1,32 @@ +package com.github.qq809203042.coding2017.basic.structurestest; +import com.github.qq809203042.coding2017.basic.structures.MyArrayList; + + /* + * 鐢ㄤ簬娴嬭瘯MyArrayList + */ + +public class MyArrayListTest { + + public static void main(String[] args) { + MyArrayList mList = new MyArrayList(); + mList.add(new String("hahah")); + mList.add(new String("heihei")); + mList.add(new String("xixi")); + mList.add(new String("papapa")); + mList.add(new String("xiaoqiang")); + mList.add(new String("xiaoming")); + + System.out.println(mList); + System.out.println(mList.get(0)); + System.out.println(mList); + System.out.println(mList.add(new String("鏂板厓绱"),3)); + System.out.println(mList); + System.out.println(mList.remove(0)); + System.out.println(mList); + System.out.println(mList.isEmpty()); + System.out.println(mList); + + System.out.println(mList.size()); + } + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyBinaryTreeTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyBinaryTreeTest.java new file mode 100644 index 0000000000..3e62719570 --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyBinaryTreeTest.java @@ -0,0 +1,23 @@ +package com.github.qq809203042.coding2017.basic.structurestest; + +import com.github.qq809203042.coding2017.basic.structures.MyBinaryTree; + +public class MyBinaryTreeTest { + + public static void main(String[] args) { + MyBinaryTree tree = new MyBinaryTree(); + tree.add(5); + tree.add(2); + tree.add(7); + tree.add(1); + tree.add(6); + tree.add(4); + tree.add(8); + + System.out.println(tree.size()); + tree.preOrder(); + tree.midOrder(); + tree.aftOrder(); + } + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyLinkedListTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyLinkedListTest.java new file mode 100644 index 0000000000..a13b940a3c --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyLinkedListTest.java @@ -0,0 +1,35 @@ +package com.github.qq809203042.coding2017.basic.structurestest; + +import com.github.qq809203042.coding2017.basic.structures.MyLinkedList; + +/* + * 鐢ㄤ簬娴嬭瘯MyLinkedList绫 + */ +public class MyLinkedListTest { + + public static void main(String[] args) { + MyLinkedList mList = new MyLinkedList(); + System.out.println(mList.add(new String("hahah"))); + System.out.println(mList.add(new String("heihei"))); + System.out.println(mList.add(new String("xixi"))); + System.out.println(mList.add(new String("papapa"))); + System.out.println(mList.add(new String("xiaoqiang"))); + System.out.println(mList.add(new String("xiaoming"))); + + System.out.println(mList.size()); + System.out.println(mList); + System.out.println(mList.get(0)); + System.out.println(mList); + System.out.println(mList.add(new String("鏂板厓绱"),0)); + mList.addFirst(new String("鏂板厓绱2")); + mList.addLast(new String("鏂板厓绱3")); + + System.out.println(mList.size()); + System.out.println(mList); + System.out.println(mList.remove(5)); + System.out.println(mList); + + System.out.println(mList.size()); + } + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyQueueTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyQueueTest.java new file mode 100644 index 0000000000..c9d25c9c4b --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyQueueTest.java @@ -0,0 +1,9 @@ +package com.github.qq809203042.coding2017.basic.structurestest; + +public class MyQueueTest { + + public static void main(String[] args) { + + } + +} diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyStackTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyStackTest.java new file mode 100644 index 0000000000..cd32a0a50e --- /dev/null +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyStackTest.java @@ -0,0 +1,25 @@ +package com.github.qq809203042.coding2017.basic.structurestest; + +import com.github.qq809203042.coding2017.basic.structures.MyStack; + +public class MyStackTest { + + public static void main(String[] args) { + MyStack ms = new MyStack(); + ms.push(new String("yi")); + ms.push(new String("er")); + ms.push(new String("san")); + ms.push(new String("si")); + ms.push(new String("wu")); + ms.push(new String("liu")); + + System.out.println(ms); + ms.pop(); + System.out.println(ms); + ms.pop(); + System.out.println(ms); + System.out.println(ms.peek()); + System.out.println(ms.size()); + } + +} From 0e47f49d1c0de8e96f26015affef84ac6cfe841b Mon Sep 17 00:00:00 2001 From: 240094626 Date: Tue, 14 Mar 2017 22:35:22 +0800 Subject: [PATCH 078/155] download-todo --- .../coderising/download/DownloadThread.java | 76 +++++++++++++++ .../coderising/download/FileDownloader.java | 96 +++++++++++++++++++ .../download/FileDownloaderTest.java | 59 ++++++++++++ .../coderising/download/api/Connection.java | 24 +++++ .../download/api/ConnectionException.java | 58 +++++++++++ .../download/api/ConnectionManager.java | 10 ++ .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 49 ++++++++++ .../download/impl/ConnectionManagerImpl.java | 28 ++++++ 9 files changed, 405 insertions(+) create mode 100644 group17/240094626/work_0305/src/com/coderising/download/DownloadThread.java create mode 100644 group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java create mode 100644 group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group17/240094626/work_0305/src/com/coderising/download/api/Connection.java create mode 100644 group17/240094626/work_0305/src/com/coderising/download/api/ConnectionException.java create mode 100644 group17/240094626/work_0305/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group17/240094626/work_0305/src/com/coderising/download/api/DownloadListener.java create mode 100644 group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionManagerImpl.java diff --git a/group17/240094626/work_0305/src/com/coderising/download/DownloadThread.java b/group17/240094626/work_0305/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..a95a4c04f0 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,76 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + int threadId; + private static final int BUFF_LENGTH = 1024; + private final static String TEMP_FILE_PATH = "E:/temp"; + String filePath ; + boolean isFinished = false; + long currSize = 0; + + public DownloadThread( Connection conn, int startPos, int endPos,int threadId){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.threadId = threadId; + filePath = TEMP_FILE_PATH + "/"+threadId+".tmp"; + } + public void run(){ + try { + // create temp file with threadId + createTmepFile(); + + // read temp file + writeTempFile(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + private void createTmepFile() { + //鍒ゆ柇璺緞temp鏄惁瀛樺湪锛屼笉瀛樺湪鍒欏厛鍒涘缓 + File tempDir = new File(TEMP_FILE_PATH); + if(!tempDir.exists() || !tempDir.isDirectory()){ + tempDir.mkdir(); + } + + File file = new File(filePath); + if(file.exists()){ + currSize = file.length(); + }else{ + currSize = 0; + } + } + private int writeTempFile() { + int size = 0; + OutputStream fout = null; + try{ + fout = new FileOutputStream(filePath, true); + for(int i = 0; i < sbLogMsg.size(); i++){ + StringBuffer logMsg = sbLogMsg.get(i); + byte[] tmpBytes = CommUtil.StringToBytes(logMsg.toString()); + fout.write(tmpBytes); + size += tmpBytes.length; + } + }catch(Exception e){ + e.printStackTrace(); + }finally{ + if(fout != null){ + fout.close(); + } + } + return size; + + } +} diff --git a/group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java b/group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..ab4e3f08f8 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,96 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + int threadNum; + + //涓缁勫紑濮嬩笅杞戒綅缃 + private int[] startPos; + //涓缁勭粨鏉熶笅杞戒綅缃 + private int[] endPos; + + private boolean stop = false; + + + public FileDownloader(String _url,int threadNum) { + this.url = _url; + this.threadNum = threadNum; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + if(length > 0){ + for(int i = 0 , len = length/threadNum; i < threadNum; i++){ + int size = i * len; + startPos[i] = size; + if(i == threadNum-1){ + endPos[i] = length - 1; + }else{ + endPos[i] = size + len-1; + } + Connection downConn = cm.open(url); + new DownloadThread(downConn, startPos[i], endPos[i], i+1).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; + } + +} diff --git a/group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java b/group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..20358d21ca --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + int threadNum = Runtime.getRuntime().availableProcessors(); + FileDownloader downloader = new FileDownloader(url,threadNum); + + + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group17/240094626/work_0305/src/com/coderising/download/api/Connection.java b/group17/240094626/work_0305/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..41d308f445 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/api/Connection.java @@ -0,0 +1,24 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 缁欏畾寮濮嬪拰缁撴潫浣嶇疆锛 璇诲彇鏁版嵁锛 杩斿洖鍊兼槸瀛楄妭鏁扮粍 + * @param startPos 寮濮嬩綅缃紝 浠0寮濮 + * @param endPos 缁撴潫浣嶇疆 + * @return + * @throws Exception + */ + public byte[] read(int startPos,int endPos) throws Exception; + /** + * 寰楀埌鏁版嵁鍐呭鐨勯暱搴 + * @return + */ + public int getContentLength(); + + /** + * 鍏抽棴杩炴帴 + */ + public void close(); +} diff --git a/group17/240094626/work_0305/src/com/coderising/download/api/ConnectionException.java b/group17/240094626/work_0305/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..93dbe37805 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,58 @@ +package com.coderising.download.api; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class ConnectionException extends Exception { + + private static final long serialVersionUID = 5581807994119179835L; + private String message; + private Throwable t; + private String stackTrace; + + public Throwable getCause(){ + return this.t; + } + + public String toString(){ + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public ConnectionException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public ConnectionException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public ConnectionException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + this.message = paramString; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + +} diff --git a/group17/240094626/work_0305/src/com/coderising/download/api/ConnectionManager.java b/group17/240094626/work_0305/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group17/240094626/work_0305/src/com/coderising/download/api/DownloadListener.java b/group17/240094626/work_0305/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java b/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..7dc00e4523 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,49 @@ +package com.coderising.download.impl; + +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private static final int BUFF_LENGTH = 1024; + + private HttpURLConnection conn; + + public ConnectionImpl(HttpURLConnection conn) { + this.conn = conn; + } + + @Override + public byte[] read(int startPos, int endPos) throws Exception { + if(startPos > endPos){ + throw new IllegalArgumentException("startPos:"+startPos+",endPos:"+endPos); + } + String property = "bytes="+startPos+"-"+endPos; + conn.setRequestProperty("RANGE", property); + if(conn.getResponseCode() == 206){ + InputStream is = conn.getInputStream(); + byte[] bytes = new byte[endPos-startPos]; + byte[] buff = new byte[BUFF_LENGTH]; + int len,i=0; + while((len = is.read(buff) ) != 0){ + System.arraycopy(buff, 0, bytes, i*len, len); + i++; + } + return bytes; + } + return null; + } + + @Override + public int getContentLength() { + return conn.getContentLength(); + } + + @Override + public void close() { + + } + +} diff --git a/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..95b0d1ec28 --- /dev/null +++ b/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,28 @@ +package com.coderising.download.impl; + +import java.net.HttpURLConnection; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + Connection conn = null; + try { + URL u = new URL(url); + HttpURLConnection hConn = (HttpURLConnection) u.openConnection(); + hConn.setConnectTimeout(5000); + hConn.setReadTimeout(5000); + hConn.setRequestMethod("GET"); + conn = new ConnectionImpl(hConn); + } catch (Exception e) { + throw new ConnectionException("杩炴帴鎵撳紑澶辫触:"+url, e); + } + return conn; + } + +} From e05a0fb5df1455e2ae1e899e75a4cfa8f8ecc337 Mon Sep 17 00:00:00 2001 From: JiaMing Q <809203042@qq.com> Date: Tue, 14 Mar 2017 23:11:08 +0800 Subject: [PATCH 079/155] my first homework --- .../coding2017/basic/structures/MyArrayList.java | 2 ++ .../coding2017/basic/structures/MyBinaryTree.java | 6 ++++-- .../coding2017/basic/structurestest/MyArrayListTest.java | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java index 711dbfd0e9..b5157e1030 100644 --- a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java @@ -58,6 +58,8 @@ public Object remove(int index) { } Object removeElement = elementData[index]; moveForward(elementData,index+1,size-1,1); +// 鏈鍚庝竴浣嶇疆涓簄ull锛涳紒锛 + elementData[size-1] = null; size--; return removeElement; } diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java index c5109cb954..8d57321a1e 100644 --- a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java @@ -51,9 +51,11 @@ private BinaryTreeNode add(BinaryTreeNode node, Integer key) { } Integer value = node.getValue(); if(value.compareTo(key) > 0){ - node.left =add(node.left,key); + node.setLeft(add(node.left,key)); + }else if(value.compareTo(key) < 0){ - node.right=add(node.right,key); + node.setRight(add(node.right,key)); + } return node; } diff --git a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java index cda34461ea..897fc596b2 100644 --- a/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java +++ b/group24/809203042/src/com/github/qq809203042/coding2017/basic/structurestest/MyArrayListTest.java @@ -1,4 +1,8 @@ package com.github.qq809203042.coding2017.basic.structurestest; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + import com.github.qq809203042.coding2017.basic.structures.MyArrayList; /* @@ -8,6 +12,8 @@ public class MyArrayListTest { public static void main(String[] args) { + + MyArrayList mList = new MyArrayList(); mList.add(new String("hahah")); mList.add(new String("heihei")); From 4243d84bd05dd7f1ddb6f3f2a844d0ebf707cebd Mon Sep 17 00:00:00 2001 From: lzb Date: Tue, 14 Mar 2017 23:33:34 +0800 Subject: [PATCH 080/155] =?UTF-8?q?=E5=AE=8C=E6=88=90arrayutil,=E6=95=B4?= =?UTF-8?q?=E7=90=86=E4=BD=9C=E4=B8=9A=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homework0312/{basic => }/ArrayList.java | 19 +-- .../{basic => }/BinaryTreeNode.java | 2 +- .../homework0312/{basic => }/Iterator.java | 2 +- .../homework0312/{basic => }/LinkedList.java | 2 +- .../me/lzb/homework0312/{basic => }/List.java | 2 +- .../lzb/homework0312/{basic => }/Queue.java | 2 +- .../lzb/homework0312/{basic => }/Stack.java | 2 +- .../ArrayUtil.java | 113 ++++++++++++++++-- .../{basic => }/ArrayListTest.java | 26 ++-- .../{basic => }/LinkedListTest.java | 28 ++--- 10 files changed, 143 insertions(+), 55 deletions(-) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{basic => }/ArrayList.java (85%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{basic => }/BinaryTreeNode.java (96%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{basic => }/Iterator.java (77%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{basic => }/LinkedList.java (99%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{basic => }/List.java (84%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{basic => }/Queue.java (93%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{basic => }/Stack.java (96%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/{homework0313/litestruts => homework0319}/ArrayUtil.java (71%) rename group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/{basic => }/ArrayListTest.java (83%) rename group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/{basic => }/LinkedListTest.java (82%) diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/ArrayList.java similarity index 85% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/ArrayList.java index c93b6c76e0..56e3a6ab12 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/ArrayList.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; /** * 绠鏄揂rrayList @@ -12,13 +12,14 @@ public class ArrayList implements List { public void add(Object o) { - if (elementData.length < size + 1) { - Object[] target = new Object[size + 1]; - System.arraycopy(elementData, 0, target, 0, elementData.length); - elementData = target; - } - elementData[size] = o; - size = size + 1; +// if (elementData.length < size + 1) { +// Object[] target = new Object[size + 1]; +// System.arraycopy(elementData, 0, target, 0, elementData.length); +// elementData = target; +// } +// elementData[size] = o; +// size = size + 1; + add(size, o); } @@ -29,7 +30,7 @@ public void add(int index, Object o) throws IndexOutOfBoundsException { int leftSize = index; int rightSize = size - index; - Object[] target = new Object[elementData.length + 1]; + Object[] target = new Object[size + 1]; System.arraycopy(elementData, 0, target, 0, leftSize); target[index] = o; System.arraycopy(elementData, index, target, index + 1, rightSize); diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/BinaryTreeNode.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/BinaryTreeNode.java similarity index 96% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/BinaryTreeNode.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/BinaryTreeNode.java index 053baad0d4..80a7e61027 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/BinaryTreeNode.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; /** * 宸﹁竟姣旂埗鑺傜偣灏忥紝鍙宠竟姣旂埗鑺傜偣澶 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Iterator.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Iterator.java similarity index 77% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Iterator.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Iterator.java index 138e090126..d97e9bc7a2 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Iterator.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Iterator.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; /** * Created by LZB on 2017/3/11. diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/LinkedList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/LinkedList.java similarity index 99% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/LinkedList.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/LinkedList.java index 7b12d27c6c..223983829c 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/LinkedList.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/LinkedList.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; /** diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/List.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/List.java similarity index 84% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/List.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/List.java index bd66593efa..f0408185bd 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/List.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/List.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; /** * list鎺ュ彛 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Queue.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Queue.java similarity index 93% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Queue.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Queue.java index 50ea66f1a2..21bce4591f 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Queue.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Queue.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; /** * 鍏堣繘鍏堝嚭 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Stack.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Stack.java similarity index 96% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Stack.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Stack.java index 4cd8d3dfd9..ea434cbfbc 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Stack.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Stack.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; /** * 鍏堣繘鍚庡嚭 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0313/litestruts/ArrayUtil.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/ArrayUtil.java similarity index 71% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0313/litestruts/ArrayUtil.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/ArrayUtil.java index 5523a35536..640a2e4843 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0313/litestruts/ArrayUtil.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/ArrayUtil.java @@ -1,4 +1,4 @@ -package me.lzb.homework0313.litestruts; +package me.lzb.homework0319; public class ArrayUtil { @@ -208,20 +208,114 @@ public int[] fibonacci(int max) { * @return */ public int[] getPrimes(int max) { - return null; + if (max <= 2){ + return new int[0]; + } + + if (max == 3){ + return new int[]{2}; + } + + + int[] primes = new int[max+1]; + primes[0] = 2; + int count = 1; + for (int i = 3; i < max; i = i + 2) { + + boolean isPrime = true; + for (int j = 3; j < i; j++) { + if(i % j == 0){ + isPrime = false; + break; + } + } + + if(isPrime){ + primes[count] = i; + count = count + 1; + } + } + + int[] result = new int[count]; + System.arraycopy(primes, 0, result, 0, count); + + return result; + + } + + private boolean isPrime(int a){ + if (a < 2) { + return false; + } + + if (a == 2) { + return true; + } + + if(a % 2 == 0){ + return false; + } + + + for (int i = 3; i < a; i = i + 2) { + if(a % i == 0){ + return false; + } + } + + return true; } + + /** - * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勭湡鍥犲瓙涔嬪拰锛屼緥濡6=1+2+3 * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 * * @param max * @return */ public int[] getPerfectNumbers(int max) { - return null; + if (max < 6){ + return new int[0]; + } + + + int[] pns = new int[max]; + + int count = 0; + for (int i = 6; i < max; i++) { + if (isPerfectNumber(i)){ + pns[count] = i; + count = count + 1; + } + } + + + + int[] result = new int[count]; + System.arraycopy(pns, 0, result, 0, count); + return result; } + + private boolean isPerfectNumber(int a){ + if(a < 6){ + return false; + } + + int sum = 0; + for (int i = 1; i < a; i++) { + if(a % i == 0){ + sum = sum + i; + } + } + + return sum == a; + } + + + /** * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 * 渚嬪array= [3,8,9], seperator = "-" @@ -230,9 +324,14 @@ public int[] getPerfectNumbers(int max) { * @param array * @return */ - public String join(int[] array, String seperator) { - return null; - } + public static String join(int[] array, String seperator) { + String result = ""; + for (int i = 0; i < array.length; i++) { + result = result + array[i] + seperator ; + } + result = result.substring(0, result.length() - 1); + return result; + } } diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/ArrayListTest.java similarity index 83% rename from group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java rename to group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/ArrayListTest.java index c35ed8d2b4..2683dbcf75 100644 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/ArrayListTest.java @@ -1,5 +1,7 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; +import me.lzb.homework0312.ArrayList; +import me.lzb.homework0312.Iterator; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -68,29 +70,21 @@ public void addTest() { @Test public void removeTest() throws IndexOutOfBoundsException { - - ArrayList list = new ArrayList(); - list.add("a"); - list.add("b"); - list.add("c"); - list.add("d"); - - - String r1 = list.remove(1).toString(); + String r1 = arrayList.remove(1).toString(); Assert.assertEquals("b", r1); - Assert.assertEquals(3, list.size()); + Assert.assertEquals(3, arrayList.size()); - String r0 = list.remove(0).toString(); + String r0 = arrayList.remove(0).toString(); Assert.assertEquals("a", r0); - Assert.assertEquals(2, list.size()); + Assert.assertEquals(2, arrayList.size()); - String rs = list.remove(list.size() - 1).toString(); + String rs = arrayList.remove(arrayList.size() - 1).toString(); Assert.assertEquals("d", rs); - Assert.assertEquals(1, list.size()); + Assert.assertEquals(1, arrayList.size()); thrown.expect(IndexOutOfBoundsException.class); thrown.expectMessage("index boom"); - list.remove(100); + arrayList.remove(100); } diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/LinkedListTest.java similarity index 82% rename from group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java rename to group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/LinkedListTest.java index 740d8768c7..16e3e5bc8c 100644 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/LinkedListTest.java @@ -1,5 +1,7 @@ -package me.lzb.homework0312.basic; +package me.lzb.homework0312; +import me.lzb.homework0312.Iterator; +import me.lzb.homework0312.LinkedList; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -69,30 +71,22 @@ public void addTest() { @Test public void removeTest() throws IndexOutOfBoundsException { - - LinkedList list = new LinkedList(); - list.add("a"); - list.add("b"); - list.add("c"); - list.add("d"); - - - String r1 = list.remove(1).toString(); + String r1 = linkedList.remove(1).toString(); Assert.assertEquals("b", r1); - Assert.assertEquals(3, list.size()); + Assert.assertEquals(3, linkedList.size()); - String r0 = list.remove(0).toString(); + String r0 = linkedList.remove(0).toString(); Assert.assertEquals("a", r0); - Assert.assertEquals(2, list.size()); + Assert.assertEquals(2, linkedList.size()); - String rs = list.remove(list.size() - 1).toString(); + String rs = linkedList.remove(linkedList.size() - 1).toString(); Assert.assertEquals("d", rs); - Assert.assertEquals(1, list.size()); + Assert.assertEquals(1, linkedList.size()); thrown.expect(IndexOutOfBoundsException.class); thrown.expectMessage("index boom"); - list.remove(100); - list.remove(-1); + linkedList.remove(100); + linkedList.remove(-1); } From 82c0d8297fcee87f5f0e248c8b31158dd661978f Mon Sep 17 00:00:00 2001 From: HaoSong Date: Wed, 15 Mar 2017 11:12:22 +0800 Subject: [PATCH 081/155] 3.15 --- group24/330657387/.project | 2 +- .../src/com/coding/basic/BinaryTreeNode.java | 32 ------ .../330657387/src/com/coding/basic/Queue.java | 19 ---- .../330657387/src/com/coding/basic/Stack.java | 22 ---- .../week01/data_structure}/ArrayList.java | 2 +- .../week01/data_structure/BinaryTreeNode.java | 102 ++++++++++++++++++ .../week01/data_structure}/Iterator.java | 2 +- .../week01/data_structure}/LinkedList.java | 2 +- .../week01/data_structure}/List.java | 3 +- .../src/main/week01/data_structure/Queue.java | 23 ++++ .../src/main/week01/data_structure/Stack.java | 28 +++++ .../week01/data_structure/ArrayListTest.java | 49 +++++++++ .../org.eclipse.core.resources.prefs | 2 + 13 files changed, 210 insertions(+), 78 deletions(-) delete mode 100644 group24/330657387/src/com/coding/basic/BinaryTreeNode.java delete mode 100644 group24/330657387/src/com/coding/basic/Queue.java delete mode 100644 group24/330657387/src/com/coding/basic/Stack.java rename group24/330657387/src/{com/coding/basic => main/week01/data_structure}/ArrayList.java (97%) create mode 100644 group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java rename group24/330657387/src/{com/coding/basic => main/week01/data_structure}/Iterator.java (69%) rename group24/330657387/src/{com/coding/basic => main/week01/data_structure}/LinkedList.java (98%) rename group24/330657387/src/{com/coding/basic => main/week01/data_structure}/List.java (82%) create mode 100644 group24/330657387/src/main/week01/data_structure/Queue.java create mode 100644 group24/330657387/src/main/week01/data_structure/Stack.java create mode 100644 group24/330657387/src/test/week01/data_structure/ArrayListTest.java create mode 100644 liuxin/.settings/org.eclipse.core.resources.prefs diff --git a/group24/330657387/.project b/group24/330657387/.project index fab8d7f04c..e929d098c9 100644 --- a/group24/330657387/.project +++ b/group24/330657387/.project @@ -1,6 +1,6 @@ - 2017Learning + 2017Learning_330657387 diff --git a/group24/330657387/src/com/coding/basic/BinaryTreeNode.java b/group24/330657387/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d7ac820192..0000000000 --- a/group24/330657387/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/group24/330657387/src/com/coding/basic/Queue.java b/group24/330657387/src/com/coding/basic/Queue.java deleted file mode 100644 index 36e516e266..0000000000 --- a/group24/330657387/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.coding.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} diff --git a/group24/330657387/src/com/coding/basic/Stack.java b/group24/330657387/src/com/coding/basic/Stack.java deleted file mode 100644 index a5a04de76d..0000000000 --- a/group24/330657387/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} diff --git a/group24/330657387/src/com/coding/basic/ArrayList.java b/group24/330657387/src/main/week01/data_structure/ArrayList.java similarity index 97% rename from group24/330657387/src/com/coding/basic/ArrayList.java rename to group24/330657387/src/main/week01/data_structure/ArrayList.java index 1728a28291..190402488c 100644 --- a/group24/330657387/src/com/coding/basic/ArrayList.java +++ b/group24/330657387/src/main/week01/data_structure/ArrayList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package main.week01.data_structure; import java.util.Arrays; diff --git a/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java new file mode 100644 index 0000000000..357a7d32a7 --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java @@ -0,0 +1,102 @@ +package main.week01.data_structure; + +import com.sun.org.apache.regexp.internal.recompile; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + + public BinaryTreeNode(){} + + public BinaryTreeNode(T data) + { + this.data=data; + this.left=null; + this.right=null; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + /** + * data不允许重复 + * @param data + * @return + */ + public BinaryTreeNode insert(T data){ + int compareResult = this.data.compareTo(data); + if(compareResult == 0){ + return this; + } + if(compareResult > 0){ + if(this.left == null){ + this.left = new BinaryTreeNode(data); + return this.left; + }else{ + return this.left.insert(data); + } + }else{ + if(this.right == null){ + this.right = new BinaryTreeNode(data); + return this.right; + }else{ + return this.right.insert(data); + } + } + } + + /** + * 允许节点为空 + * @param data + * @return + */ + public BinaryTreeNode search(T data){ + if(this.data == null){ + return null; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + if (this.left == null) { + return null; + } else { + return this.left.search(data); + } + } else if (compareResult < 0) { + if (this.right == null) { + return null; + } else { + return this.right.search(data); + } + } else { + return this; + } + + } + + + +} diff --git a/group24/330657387/src/com/coding/basic/Iterator.java b/group24/330657387/src/main/week01/data_structure/Iterator.java similarity index 69% rename from group24/330657387/src/com/coding/basic/Iterator.java rename to group24/330657387/src/main/week01/data_structure/Iterator.java index 06ef6311b2..194891e56a 100644 --- a/group24/330657387/src/com/coding/basic/Iterator.java +++ b/group24/330657387/src/main/week01/data_structure/Iterator.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package main.week01.data_structure; public interface Iterator { public boolean hasNext(); diff --git a/group24/330657387/src/com/coding/basic/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java similarity index 98% rename from group24/330657387/src/com/coding/basic/LinkedList.java rename to group24/330657387/src/main/week01/data_structure/LinkedList.java index 8357e13254..f4c967ba44 100644 --- a/group24/330657387/src/com/coding/basic/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package main.week01.data_structure; import java.util.NoSuchElementException; diff --git a/group24/330657387/src/com/coding/basic/List.java b/group24/330657387/src/main/week01/data_structure/List.java similarity index 82% rename from group24/330657387/src/com/coding/basic/List.java rename to group24/330657387/src/main/week01/data_structure/List.java index 10d13b5832..6cbc15f8ab 100644 --- a/group24/330657387/src/com/coding/basic/List.java +++ b/group24/330657387/src/main/week01/data_structure/List.java @@ -1,6 +1,7 @@ -package com.coding.basic; +package main.week01.data_structure; public interface List { + public void add(Object o); public void add(int index, Object o); public Object get(int index); diff --git a/group24/330657387/src/main/week01/data_structure/Queue.java b/group24/330657387/src/main/week01/data_structure/Queue.java new file mode 100644 index 0000000000..7da6edf433 --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/Queue.java @@ -0,0 +1,23 @@ +package main.week01.data_structure; + +public class Queue { + + private LinkedList elementData; + private int size = 0; + + public void enQueue(Object o){ + elementData.add(size++,o); + } + + public Object deQueue(){ + return elementData.remove(size---1); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group24/330657387/src/main/week01/data_structure/Stack.java b/group24/330657387/src/main/week01/data_structure/Stack.java new file mode 100644 index 0000000000..5fc5410fde --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/Stack.java @@ -0,0 +1,28 @@ +package main.week01.data_structure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(size++,o); + } + + public Object pop() { + Object temp = elementData.get(size-1); + elementData.remove(size---1); + return temp; + } + + public Object peek() { + return elementData.get(size-1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java new file mode 100644 index 0000000000..a9f00d99c1 --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java @@ -0,0 +1,49 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEnsureCapacity() { + fail("Not yet implemented"); + } + + @Test + public void testAddObject() { + fail("Not yet implemented"); + } + + @Test + public void testAddIntObject() { + fail("Not yet implemented"); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + +} diff --git a/liuxin/.settings/org.eclipse.core.resources.prefs b/liuxin/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/liuxin/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 From 94071e5fe6a7d688e6a138da1531d0bb4ab205af Mon Sep 17 00:00:00 2001 From: HaoSong Date: Wed, 15 Mar 2017 17:11:39 +0800 Subject: [PATCH 082/155] 3.15 --- .../main/week01/data_structure/ArrayList.java | 96 ++++++++++++------- .../week01/data_structure/BinaryTreeNode.java | 19 ++++ .../week01/data_structure/LinkedList.java | 68 +++++++++---- .../week01/data_structure/ArrayListTest.java | 59 ++++++++---- 4 files changed, 170 insertions(+), 72 deletions(-) diff --git a/group24/330657387/src/main/week01/data_structure/ArrayList.java b/group24/330657387/src/main/week01/data_structure/ArrayList.java index 190402488c..93aee4cc92 100644 --- a/group24/330657387/src/main/week01/data_structure/ArrayList.java +++ b/group24/330657387/src/main/week01/data_structure/ArrayList.java @@ -3,60 +3,86 @@ import java.util.Arrays; public class ArrayList implements List { - + private int size = 0; - + private Object[] elementData = new Object[10]; + + private void ensureCapacity(int input) { + if (input > elementData.length) { + growCapacity(); + } + } - public void ensureCapacity(int input) { - if (input > elementData.length) { - growCapacity(); - } - } - - private void growCapacity(){ - elementData = Arrays.copyOf(elementData, size * 2); - } + private void growCapacity() { + elementData = Arrays.copyOf(elementData, size * 2); + } - public void add(Object o){ + private void rangeCheck(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + public void add(Object o) { ensureCapacity(size + 1); elementData[size++] = o; } - - - public void add(int index, Object o){ + + public void add(int index, Object o) { rangeCheck(index); ensureCapacity(size + 1); - System.arraycopy(elementData,index, elementData, index + 1, size - index); + System.arraycopy(elementData, index, elementData, index + 1, size + - index); elementData[index] = o; - size ++; - } - - private void rangeCheck(int index){ - if (index > size || index < 0){ - throw new IndexOutOfBoundsException(); - } + size++; } - - public Object get(int index){ + + public Object get(int index) { rangeCheck(index); return elementData[index]; } - - public Object remove(int index){ + + public Object remove(int index) { rangeCheck(index); Object dest = elementData[index]; - System.arraycopy(elementData, index +1, elementData, index, size-index-1); - size --; + System.arraycopy(elementData, index + 1, elementData, index, size + - index - 1); + size--; return dest; } - - public int size(){ + + public int size() { return size; } - - public Iterator iterator(){ - return null; + + public class ArrayListIterator implements Iterator { + + private ArrayList list; + + private int position = 0; + + private ArrayListIterator() { + } + + private ArrayListIterator(ArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return position + 1 <= list.size; + } + + @Override + public Object next() { + return list.get(position++); + } + } - + + public ArrayListIterator iterator() { + return new ArrayListIterator(this); + } + } diff --git a/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java index 357a7d32a7..296744c188 100644 --- a/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java +++ b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java @@ -74,6 +74,7 @@ public BinaryTreeNode insert(T data){ * @param data * @return */ + @SuppressWarnings("unchecked") public BinaryTreeNode search(T data){ if(this.data == null){ return null; @@ -97,6 +98,24 @@ public BinaryTreeNode search(T data){ } + private BinaryTreeNode findMin() { + if (this.data == null) { + return null; + } + if (this.left == null) { + return this; + } + return this.left.findMin(); + } + private BinaryTreeNode findMax() { + if (this.data == null) { + return null; + } + if (this.right == null) { + return this; + } + return this.right.findMin(); + } } diff --git a/group24/330657387/src/main/week01/data_structure/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java index f4c967ba44..d89842f166 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -2,6 +2,8 @@ import java.util.NoSuchElementException; +import main.week01.data_structure.ArrayList.ArrayListIterator; + public class LinkedList implements List { private Node head; @@ -11,7 +13,7 @@ public void add(Object o) { if (isEmpty()) { head = new Node(o); } else { - Node tail = (Node)get(size-1); + Node tail = (Node) get(size - 1); Node node = new Node(o); tail.next = node; } @@ -24,28 +26,28 @@ public boolean isEmpty() { public void add(int index, Object o) { rangeCheck(index); - if (index ==0) { + if (index == 0) { Node node = new Node(o); node.next = head; head = node; } else { - Node pre = (Node)get(index-1); + Node pre = (Node) get(index - 1); Node node = new Node(o); node.next = pre.next; pre.next = node; } } - - private void rangeCheck(int index){ - if (index >= size || index <0){ - throw new IndexOutOfBoundsException(); + + private void rangeCheck(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); } } - + public Object get(int index) { rangeCheck(index); Node dest = head; - for (int i = 0; i< index; i++){ + for (int i = 0; i < index; i++) { dest = dest.next; } return dest.data; @@ -53,10 +55,10 @@ public Object get(int index) { public Object remove(int index) { rangeCheck(index); - Node pre = (Node)get(index); + Node pre = (Node) get(index); Node dest = pre.next; pre.next = dest.next; - size --; + size--; return dest; } @@ -68,11 +70,11 @@ public void addFirst(Object o) { Node node = new Node(o); node.next = head; head = node; - size ++; + size++; } public void addLast(Object o) { - Node last = (Node)get(size-1); + Node last = (Node) get(size - 1); Node node = new Node(o); last.next = node; size++; @@ -84,7 +86,7 @@ public Object removeFirst() { } Node newhead = head; head = head.next; - size --; + size--; return newhead; } @@ -95,20 +97,16 @@ public Object removeLast() { if (head.next == null) { Node tmp = head; head = null; - size --; + size--; return tmp; } - Node newLastNode = (Node)get(size-2); + Node newLastNode = (Node) get(size - 2); Node oldLastNode = newLastNode.next; newLastNode.next = null; - size --; + size--; return oldLastNode; } - public Iterator iterator() { - return null; - } - private static class Node { Object data; Node next; @@ -119,4 +117,32 @@ private static class Node { } } + public class LinkedListIterator implements Iterator { + + private LinkedList list; + + private int position = 0; + + private LinkedListIterator() { + } + + private LinkedListIterator(LinkedList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return position + 1 <= list.size; + } + + @Override + public Object next() { + return list.get(position++); + } + + } + + public LinkedListIterator iterator() { + return new LinkedListIterator(this); + } } diff --git a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java index a9f00d99c1..dacef0cbcc 100644 --- a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java +++ b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java @@ -1,49 +1,76 @@ package test.week01.data_structure; import static org.junit.Assert.*; +import main.week01.data_structure.ArrayList; +import main.week01.data_structure.ArrayList.ArrayListIterator; -import org.junit.After; import org.junit.Before; import org.junit.Test; public class ArrayListTest { + public static ArrayList list; + @Before public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEnsureCapacity() { - fail("Not yet implemented"); + list = new ArrayList(); } @Test public void testAddObject() { - fail("Not yet implemented"); + list.add(1); + list.add(2); + list.add(2); + assertEquals(3,list.size()); } @Test public void testAddIntObject() { - fail("Not yet implemented"); + list.add(0,1); + list.add(1,2); + list.add(2,2); + list.add(0,2); + assertEquals(2,list.get(0)); } @Test public void testGet() { - fail("Not yet implemented"); + list.add("songhao"); + assertEquals("songhao", list.get(0)); } @Test public void testRemove() { - fail("Not yet implemented"); + list.add("songhao"); + assertEquals("songhao", list.remove(0)); } @Test - public void testSize() { - fail("Not yet implemented"); + public void testSize(){ + list.add(0,1); + list.add(1,2); + list.add(2,2); + list.add(0,2); + assertEquals(4,list.size()); + } + + @Test + public void testIterator() { + list.add(0,1); + list.add(1,2); + list.add(2,3); + list.add(0,4); + ArrayListIterator iter = list.iterator(); + assertTrue(iter.hasNext()); + assertEquals(4, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(1, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(2, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(3, iter.next()); + assertFalse(iter.hasNext()); + } } From fccb1974710239b552bf0f86d0aa73b53347d22b Mon Sep 17 00:00:00 2001 From: yyglider Date: Wed, 15 Mar 2017 18:03:18 +0800 Subject: [PATCH 083/155] code02 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鍙嶅皠鐩稿叧 --- group23/769232552/coding/pom.xml | 35 +++ .../src/main/java/code02/ArrayUtil.java | 257 ++++++++++++++++++ .../java/code02/litestruts/LoginAction.java | 39 +++ .../main/java/code02/litestruts/Struts.java | 171 ++++++++++++ .../src/main/java/code02/litestruts/View.java | 23 ++ .../coding/src/main/resources/struts.xml | 11 + .../src/test/java/code02/ArrayUtilTest.java | 73 +++++ .../java/code02/litestruts/StrutsTest.java | 43 +++ 8 files changed, 652 insertions(+) create mode 100644 group23/769232552/coding/pom.xml create mode 100644 group23/769232552/coding/src/main/java/code02/ArrayUtil.java create mode 100644 group23/769232552/coding/src/main/java/code02/litestruts/LoginAction.java create mode 100644 group23/769232552/coding/src/main/java/code02/litestruts/Struts.java create mode 100644 group23/769232552/coding/src/main/java/code02/litestruts/View.java create mode 100644 group23/769232552/coding/src/main/resources/struts.xml create mode 100644 group23/769232552/coding/src/test/java/code02/ArrayUtilTest.java create mode 100644 group23/769232552/coding/src/test/java/code02/litestruts/StrutsTest.java diff --git a/group23/769232552/coding/pom.xml b/group23/769232552/coding/pom.xml new file mode 100644 index 0000000000..812e591bab --- /dev/null +++ b/group23/769232552/coding/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + com.coding + coding2017 + 1.0-SNAPSHOT + + + + ch.qos.logback + logback-classic + 1.1.1 + + + dom4j + dom4j + 1.6.1 + + + junit + junit + 4.12 + test + + + commons-lang + commons-lang + 2.6 + + + + \ No newline at end of file diff --git a/group23/769232552/coding/src/main/java/code02/ArrayUtil.java b/group23/769232552/coding/src/main/java/code02/ArrayUtil.java new file mode 100644 index 0000000000..23055ef138 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code02/ArrayUtil.java @@ -0,0 +1,257 @@ +package code02; +import org.apache.commons.lang.ArrayUtils; +import java.util.ArrayList; +import java.util.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 <= 1){ + return; + } + + int head = 0; + int tail = origin.length - 1; + int tmp; + while (head != tail){ + //璋冩崲浣嶇疆 + tmp = origin[head]; + origin[head] = origin[tail]; + origin[tail] = tmp; + + head ++; + tail --; + } + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + public int[] removeZero(int[] oldArray){ + if (oldArray == null || oldArray.length < 1){ + return null; + } + + List newList = new ArrayList(); + for(int number : oldArray){ + if(number != 0){ + newList.add(number); + } + } + + Integer[] result = new Integer[newList.size()]; + result = (Integer[]) newList.toArray(result); + return ArrayUtils.toPrimitive(result); + + + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 && array2 == null){ + return null; + } + if(array1 == null){ + return array2; + } + if(array2 == null){ + return array1; + } + int[] newArray = new int[array1.length + array2.length]; + int m = 0,n = 0, k = 0; + while (m < array1.length && n < array2.length){ + if(array1[m] <= array2[n]){ + newArray[k++] = array1[m++]; + }else { + newArray[k++] = array2[n++]; + } + } + if(m >= array1.length){ + while (n < array2.length){ + newArray[k++] = array2[n++]; + } + } + if(n >= array2.length){ + while (m < array1.length){ + newArray[k++] = array1[m++]; + } + } + return newArray; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + int i = 0; + for (; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + for (int j = 0; j < size; j++){ + newArray[i++] = 0; + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + //涔熷氨鏄渶瑕佺敓鎴愪竴涓皬浜巑ax鍊肩殑fibonacci鏁扮粍 + public int[] fibonacci(int max){ + if(max < 2){ + return new int[]{}; + } + if(max < 3){ + return new int[]{1,1}; + } + List list = new ArrayList(); + list.add(0,1); + list.add(1,1); + int i=0; + while (list.get(i) + list.get(i+1) < max){ + list.add(i+2,list.get(i) + list.get(i+1)); + i++; + } + + int[] newArray = new int[list.size()]; + for (int j = 0; j < list.size(); j++) { + newArray[j] = list.get(j).intValue(); + } + return newArray; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + * + * 鍘熺悊锛 + * 锛戯紝鍒ゆ柇涓涓暟瀛楁槸鍚︿负绱犳暟锛屼竴涓暟 n 濡傛灉鏄悎鏁帮紝閭d箞瀹冪殑鎵鏈夌殑鍥犲瓙涓嶈秴杩噑qrt(n) + * 锛掞紝褰搃鏄礌鏁扮殑鏃跺欙紝i鐨勬墍鏈夌殑鍊嶆暟蹇呯劧鏄悎鏁般 + */ + public int[] getPrimes(int max){ + + if(max <= 2){ + return null; + } + boolean[] prime = new boolean[max + 1]; + for (int i = 2; i <= max; i++) { + if(i%2 == 0){ + prime[i] = false; //鍋舵暟 + }else { + prime[i] = true; + } + } + + for (int i = 2; i <= Math.sqrt(max) ; i++) { + if(prime[i]){//濂囨暟 + //濡傛灉i鏄礌鏁帮紝閭d箞鎶奿鐨勫嶆暟鏍囪涓洪潪绱犳暟 + for(int j = i+i; j <= max; j += i){ + prime[j] = false; + } + } + } + + List num = new ArrayList(); + for (int i = 2; i <= max; i++) { + if(prime[i]){ + num.add(i); + } + } + + Integer[] result = new Integer[num.size()]; + result = (Integer[]) num.toArray(result); + return ArrayUtils.toPrimitive(result); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + if(max < 6){ + return null; + } + + List perfectNumlist = new ArrayList(); + + for (int j = 6;j <= max; j++){ + List factorNumlist = new ArrayList(); + factorNumlist.add(1); + for (int i = 2; i < j; i++) { + if(j % i == 0){ + factorNumlist.add(i); + } + } + int sum = 0; + for(Integer num : factorNumlist){ + sum += num; + } + + if(sum == j){ + perfectNumlist.add(j); + } + } + Integer[] result = new Integer[perfectNumlist.size()]; + result = (Integer[]) perfectNumlist.toArray(result); + return ArrayUtils.toPrimitive(result); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length - 1; i++) { + sb.append(array[i]); + sb.append(seperator); + } + sb.append(array[array.length - 1]); + return sb.toString(); + } + + public void printArr(int[] array){ + for(int num : array){ + System.out.print(num + " "); + } + } + +} diff --git a/group23/769232552/coding/src/main/java/code02/litestruts/LoginAction.java b/group23/769232552/coding/src/main/java/code02/litestruts/LoginAction.java new file mode 100644 index 0000000000..0799eae71a --- /dev/null +++ b/group23/769232552/coding/src/main/java/code02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package code02.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group23/769232552/coding/src/main/java/code02/litestruts/Struts.java b/group23/769232552/coding/src/main/java/code02/litestruts/Struts.java new file mode 100644 index 0000000000..f260a9eba1 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code02/litestruts/Struts.java @@ -0,0 +1,171 @@ +package code02.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +public class Struts { + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Struts.class); + + private static final Map> clazzMap = new HashMap>(); + private static final Map actionMap = new HashMap(); + private static final Map pageMap = new HashMap(); + + //瑙f瀽xml鏂囦欢 + private static void parseXML(){ + //璇诲彇鏂囦欢 + File file = new File("src/main/resources/struts.xml"); + SAXReader reader = new SAXReader(); + Document document = null; + try { + document = reader.read(file); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element root = document.getRootElement(); + + for (Iterator iterator = root.elementIterator("action"); iterator.hasNext();) { + Element e = iterator.next(); + String actionKey = e.attributeValue("name"); + String actionValue = e.attributeValue("class"); + actionMap.put(actionKey, actionValue); + for(Iterator childIterator = e.elementIterator();childIterator.hasNext();){ + Element child = childIterator.next(); + String jspKey = actionKey + "-" + child.attributeValue("name"); + String jspValue = child.getTextTrim(); + pageMap.put(jspKey, jspValue); + } + } + } + + //鍔犺浇xml鏂囦欢涓殑绫 + private static void initiateClazz(){ + for (Map.Entry entry : actionMap.entrySet()) { + String actionName = entry.getKey(); //login + String className = entry.getValue(); //code02.litestruts.LoginAction + Class cls; + try { + cls = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); + clazzMap.put(actionName,cls); + } catch (Exception e) { + logger.warn("鍔犺浇绫 " + className + "鍑洪敊锛"); + } + } + } + + static { + parseXML(); + initiateClazz(); + } + + //杩斿洖瀹炰緥瀵硅薄 + private static Object getInstance(String actionName){ + Object instance = null; + for (Map.Entry> entry : clazzMap.entrySet()) { + String action = entry.getKey(); //login + Class cls = entry.getValue(); //code02.litestruts.LoginAction.class + if(actionName.equals(action)){ + try { + instance = cls.newInstance(); + } catch (Exception e) { + logger.error("鐢熸垚瀹炰緥鍑洪敊锛", e); + throw new RuntimeException(e); + } + } + } + return instance; + } + + //杩斿洖浠et寮澶寸殑鏂规硶 + private static List getMethods(String actionName){ + List methodsList = new ArrayList(); + Class cls = clazzMap.get(actionName); + Method[] methods = cls.getDeclaredMethods(); + for (int i = 0; i < methods.length; i++) { + String methodName = methods[i].getName(); + if(methodName.startsWith("get")){ + methodsList.add(methods[i]); + } + } + return methodsList; + } + + + /* + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + + public static View runAction(String actionName, Map parameters) { + LoginAction loginAction = (LoginAction) getInstance(actionName); + loginAction.setName(parameters.get("name")); + loginAction.setPassword(parameters.get("password")); + + String result = loginAction.execute(); + + Map params = new HashMap(); + List methods = getMethods(actionName); + for(Method method : methods){ + String key = method.getName().substring(3); + String value = null; + try { + value = (String) method.invoke(loginAction); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + params.put(key,value); + } + + + View view = new View(); + + view.setParameters(params); + String resultKey = actionName + "-" + result; + view.setJsp(pageMap.get(resultKey)); + + return view; + } + + + + + + + public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { + + String actionName = "login"; + HashMap params = new HashMap(); + params.put("name","test"); + params.put("password","12345"); + + View view = Struts.runAction(actionName,params); + System.out.println(view.getJsp()); + System.out.println(view.getParameters()); + + + } + +} diff --git a/group23/769232552/coding/src/main/java/code02/litestruts/View.java b/group23/769232552/coding/src/main/java/code02/litestruts/View.java new file mode 100644 index 0000000000..c7e630587c --- /dev/null +++ b/group23/769232552/coding/src/main/java/code02/litestruts/View.java @@ -0,0 +1,23 @@ +package code02.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group23/769232552/coding/src/main/resources/struts.xml b/group23/769232552/coding/src/main/resources/struts.xml new file mode 100644 index 0000000000..fd71dc16ff --- /dev/null +++ b/group23/769232552/coding/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group23/769232552/coding/src/test/java/code02/ArrayUtilTest.java b/group23/769232552/coding/src/test/java/code02/ArrayUtilTest.java new file mode 100644 index 0000000000..1277e3dab6 --- /dev/null +++ b/group23/769232552/coding/src/test/java/code02/ArrayUtilTest.java @@ -0,0 +1,73 @@ +package code02; + +import org.junit.Test; + +/** + * Created by yaoyuan on 2017/3/13. + */ +public class ArrayUtilTest { + + @Test + public void testReverseArray() throws Exception { + int[] arr1 = new int[]{1,2,3,4,5,6,7}; + ArrayUtil arrayUtil = new ArrayUtil(); + arrayUtil.reverseArray(arr1); + arrayUtil.printArr(arr1); + + + } + + @Test + public void testRemoveZero() throws Exception { + int[] arr1 = new int[]{1,0,2,3,0,0,4,5,6,0}; + ArrayUtil arrayUtil = new ArrayUtil(); + int[] arr2 = arrayUtil.removeZero(arr1); + arrayUtil.printArr(arr2); + } + + @Test + public void testMerge() throws Exception { + int[] arr1 = new int[]{3, 5, 7, 8}; + int[] arr2 = new int[]{4, 5, 6, 7}; + ArrayUtil arrayUtil = new ArrayUtil(); + int[] arr3 = arrayUtil.merge(arr1,arr2); + arrayUtil.printArr(arr3); + } + + @Test + public void testGrow() throws Exception { + int[] arr1 = new int[]{1,2,3,4,5,6,7}; + ArrayUtil arrayUtil = new ArrayUtil(); + int[] arr2 = arrayUtil.grow(arr1,3); + arrayUtil.printArr(arr2); + } + + @Test + public void testFibonacci() throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] arr1 = arrayUtil.fibonacci(4); + arrayUtil.printArr(arr1); + + int[] arr2 = arrayUtil.fibonacci(20); + arrayUtil.printArr(arr2); + } + + @Test + public void testGetPrimes() throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + arrayUtil.printArr(arrayUtil.getPrimes(30)); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + arrayUtil.printArr(arrayUtil.getPerfectNumbers(1000)); + } + + @Test + public void testJoin() throws Exception { + int[] arr1 = new int[]{1,2,3,4,5,6,7}; + ArrayUtil arrayUtil = new ArrayUtil(); + System.out.println(arrayUtil.join(arr1,"-")); + } +} \ No newline at end of file diff --git a/group23/769232552/coding/src/test/java/code02/litestruts/StrutsTest.java b/group23/769232552/coding/src/test/java/code02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9213702251 --- /dev/null +++ b/group23/769232552/coding/src/test/java/code02/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package code02.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} From f24979ee55106d68fbe1be735aaab1e69c01c4b3 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 16 Mar 2017 01:27:00 +0800 Subject: [PATCH 084/155] test for new dell --- group24/121111914/.gitignore | 1 + .../ipk2015/coding2017/basic/test/TestForDell.java | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 group24/121111914/.gitignore create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/basic/test/TestForDell.java diff --git a/group24/121111914/.gitignore b/group24/121111914/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group24/121111914/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/TestForDell.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/TestForDell.java new file mode 100644 index 0000000000..01d2c8d5fc --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/TestForDell.java @@ -0,0 +1,10 @@ +package com.github.ipk2015.coding2017.basic.test; + +public class TestForDell { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From e9c093b9d15c01c38408d209d7356529a672ed0b Mon Sep 17 00:00:00 2001 From: ddcv587 Date: Thu, 16 Mar 2017 09:25:28 +0800 Subject: [PATCH 085/155] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/171535320/DataStruct/LinkedList.java | 39 +++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/group11/171535320/DataStruct/LinkedList.java b/group11/171535320/DataStruct/LinkedList.java index 2b1c2a25ae..9aa9fbc46c 100644 --- a/group11/171535320/DataStruct/LinkedList.java +++ b/group11/171535320/DataStruct/LinkedList.java @@ -284,7 +284,27 @@ public void removeDuplicateValues(){ * @param max */ public void removeRange(int min, int max){ - + Node first = head; + Node first2 = first; + Node last = head.next; + Node last2 = last; + while(last != null) { + if((Integer)last.data < max && (Integer)first.data < min) { + first2 = first; + first = first.next; + last2 = last; + last = last.next; + } else if((Integer)last.data > max && (Integer)first.data > min) { + break; + } else if((Integer)last.data < max && (Integer)first.data > min) { + last2 = last; + last = last.next; + } else { + first2 = first; + first = first.next; + } + } + first2.next = last; } /** @@ -293,6 +313,21 @@ public void removeRange(int min, int max){ * @param list */ public LinkedList intersection( LinkedList list){ - return null; + LinkedList result = new LinkedList(); + Node temp = head; + Node temp2 = list.head; + while(temp != null && temp2 != null) { + if((Integer)temp.data == (Integer)temp2.data) { + result.add(temp.data); + temp = temp.next; + temp2 = temp2.next; + } else if((Integer)temp.data > (Integer)temp2.data) { + temp2 = temp2.next; + } else { + temp = temp.next; + } + } + + return result; } } From 60424407b836cd5d579e2544bc66a8ec976315ea Mon Sep 17 00:00:00 2001 From: HaoSong Date: Thu, 16 Mar 2017 13:30:53 +0800 Subject: [PATCH 086/155] 3.16 --- .../main/week01/data_structure/ArrayList.java | 2 +- .../week01/data_structure/LinkedList.java | 52 +++++++++--------- .../week01/data_structure/ArrayListTest.java | 8 +++ .../data_structure/BinaryTreeNodeTest.java | 28 ++++++++++ .../week01/data_structure/LinkedListTest.java | 54 +++++++++++++++++++ 5 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java create mode 100644 group24/330657387/src/test/week01/data_structure/LinkedListTest.java diff --git a/group24/330657387/src/main/week01/data_structure/ArrayList.java b/group24/330657387/src/main/week01/data_structure/ArrayList.java index 93aee4cc92..9a2f4d2c8b 100644 --- a/group24/330657387/src/main/week01/data_structure/ArrayList.java +++ b/group24/330657387/src/main/week01/data_structure/ArrayList.java @@ -48,7 +48,7 @@ public Object remove(int index) { Object dest = elementData[index]; System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - size--; + elementData[size---1]=null;//防止内存泄漏 return dest; } diff --git a/group24/330657387/src/main/week01/data_structure/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java index d89842f166..b61df9f254 100644 --- a/group24/330657387/src/main/week01/data_structure/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -2,8 +2,6 @@ import java.util.NoSuchElementException; -import main.week01.data_structure.ArrayList.ArrayListIterator; - public class LinkedList implements List { private Node head; @@ -11,13 +9,10 @@ public class LinkedList implements List { public void add(Object o) { if (isEmpty()) { - head = new Node(o); + addFirst(o); } else { - Node tail = (Node) get(size - 1); - Node node = new Node(o); - tail.next = node; + addLast(o); } - size++; } public boolean isEmpty() { @@ -27,14 +22,15 @@ public boolean isEmpty() { public void add(int index, Object o) { rangeCheck(index); if (index == 0) { - Node node = new Node(o); - node.next = head; - head = node; + addFirst(o); + } else if (index == size) { + addLast(o); } else { - Node pre = (Node) get(index - 1); + Node pre = getNode(index - 1); Node node = new Node(o); node.next = pre.next; pre.next = node; + size++; } } @@ -53,13 +49,27 @@ public Object get(int index) { return dest.data; } + public Node getNode(int index) { + rangeCheck(index); + Node dest = head; + for (int i = 0; i < index; i++) { + dest = dest.next; + } + return dest; + } + public Object remove(int index) { rangeCheck(index); - Node pre = (Node) get(index); + if (index == 0) { + return removeFirst(); + }else if(index == size){ + return removeLast(); + } + Node pre = getNode(index - 1); Node dest = pre.next; pre.next = dest.next; size--; - return dest; + return dest.data; } public int size() { @@ -74,7 +84,7 @@ public void addFirst(Object o) { } public void addLast(Object o) { - Node last = (Node) get(size - 1); + Node last = getNode(size - 1); Node node = new Node(o); last.next = node; size++; @@ -85,22 +95,14 @@ public Object removeFirst() { throw new NoSuchElementException(); } Node newhead = head; + Node dest = head; head = head.next; size--; - return newhead; + return dest.data; } public Object removeLast() { - if (head == null) { - throw new NoSuchElementException(); - } - if (head.next == null) { - Node tmp = head; - head = null; - size--; - return tmp; - } - Node newLastNode = (Node) get(size - 2); + Node newLastNode = getNode(size - 2); Node oldLastNode = newLastNode.next; newLastNode.next = null; size--; diff --git a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java index dacef0cbcc..d9d63339b2 100644 --- a/group24/330657387/src/test/week01/data_structure/ArrayListTest.java +++ b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java @@ -31,6 +31,14 @@ public void testAddIntObject() { list.add(2,2); list.add(0,2); assertEquals(2,list.get(0)); + try{ + list.add(-1 , "test"); + fail("-1 can't be index"); + list.add(1000, "test"); + fail("out of range"); + }catch (Exception e){ + + } } @Test diff --git a/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java b/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..515da2e7d4 --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java @@ -0,0 +1,28 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; +import main.week01.data_structure.BinaryTreeNode; + +import org.junit.Before; +import org.junit.Test; + +public class BinaryTreeNodeTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testInsert() { + BinaryTreeNode head=new BinaryTreeNode(); + + head.setData(5); + head.insert(2); + head.insert(7); + head.insert(1); + head.insert(4); + head.insert(3); + assertEquals(3,head.getLeft().getRight().getLeft().getData()); + } + +} diff --git a/group24/330657387/src/test/week01/data_structure/LinkedListTest.java b/group24/330657387/src/test/week01/data_structure/LinkedListTest.java new file mode 100644 index 0000000000..cf374a6dcb --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/LinkedListTest.java @@ -0,0 +1,54 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; +import main.week01.data_structure.LinkedList; +import main.week01.data_structure.LinkedList.LinkedListIterator; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + private LinkedList list; + + @Before + public void setUp() throws Exception { + list = new LinkedList(); + } + + @Test + public void testGet() { + list.add("A"); + list.add("B"); + list.add(0, "C"); + assertEquals("C", list.get(0)); + } + + @Test + public void testRemove() { + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.add(0, "E"); + assertEquals("E", list.remove(0)); + assertEquals("D", list.remove(list.size()-1)); + assertEquals(3, list.size()); + } + + @Test + public void testIterator() { + LinkedListIterator iter = list.iterator(); + list.add("A"); + list.add("B"); + list.add(0, "C"); + assertTrue(iter.hasNext()); + assertEquals("C", iter.next()); + assertTrue(iter.hasNext()); + assertEquals("A", iter.next()); + assertTrue(iter.hasNext()); + assertEquals("B", iter.next()); + assertFalse(iter.hasNext()); + } + +} From a928c8c0684bd5484507f026ca753207d567589a Mon Sep 17 00:00:00 2001 From: "wangxg922@chinaunincom.cn" <996108220@qq.com> Date: Thu, 16 Mar 2017 14:45:59 +0800 Subject: [PATCH 087/155] =?UTF-8?q?Signed-off-by:=20996108220@qq.com?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E5=92=8C=E5=A4=9A=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/download/DownloadThread.java | 48 +++ .../coderising/download/FileDownloader.java | 94 +++++ .../download/FileDownloaderTest.java | 57 +++ .../coderising/download/api/Connection.java | 25 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 62 ++++ .../download/impl/ConnectionManagerImpl.java | 30 ++ .../src/com/coderising/litestruts/Struts.java | 13 +- .../src/com/coding/basic/LinkedList.java | 329 +++++++++++++++++- .../996108220/src/com/coding/basic/Queue.java | 4 +- .../src/com/coding/basic/newLinkedList.java | 5 + 13 files changed, 662 insertions(+), 25 deletions(-) create mode 100644 group11/996108220/src/com/coderising/download/DownloadThread.java create mode 100644 group11/996108220/src/com/coderising/download/FileDownloader.java create mode 100644 group11/996108220/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group11/996108220/src/com/coderising/download/api/Connection.java create mode 100644 group11/996108220/src/com/coderising/download/api/ConnectionException.java create mode 100644 group11/996108220/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group11/996108220/src/com/coderising/download/api/DownloadListener.java create mode 100644 group11/996108220/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group11/996108220/src/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 group11/996108220/src/com/coding/basic/newLinkedList.java diff --git a/group11/996108220/src/com/coderising/download/DownloadThread.java b/group11/996108220/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..79c738753b --- /dev/null +++ b/group11/996108220/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,48 @@ +package com.coderising.download; + +import java.io.IOException; + + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.DownloadListener; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + boolean status=false; + CyclicBarrier barrier; + + public DownloadThread( Connection conn, int startPos, int endPos,CyclicBarrier barrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.barrier=barrier; + } + public void run(){ + try { + + byte[] buffer=conn.read(startPos, endPos); + RandomAccessFile fos = new RandomAccessFile(FileDownloader.SAVE_FILE, "rw"); + fos.seek(startPos); + fos.write(buffer, 0, endPos-startPos); + fos.close(); + barrier.await(); + + } catch (Exception e) { + // TODO Auto-generated catch block + + e.printStackTrace(); + } + + System.out.println("绾跨▼" + this.getId() + "宸茬粡瀹屾垚"); + + } + + +} diff --git a/group11/996108220/src/com/coderising/download/FileDownloader.java b/group11/996108220/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..ef23fa872b --- /dev/null +++ b/group11/996108220/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,94 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coding.basic.newLinkedList; + + +public class FileDownloader { + + public static File SAVE_FILE= new File("f:/abc.jpg");; + String url; + DownloadListener listener; + ConnectionManager cm; + int threadNumber = 3; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + CyclicBarrier barrier=new CyclicBarrier(threadNumber, new Runnable() { + + @Override + public void run() { + // TODO Auto-generated method stub + listener.notifyFinished(); + } + }); + Connection conn =null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + byte[] buf = new byte[8192]; + FileOutputStream fos; + fos = new FileOutputStream(FileDownloader.SAVE_FILE); + long m = length/ buf.length; + for (long i = 0; i < m; i++) { + fos.write(buf, 0, buf.length); + } + fos.write(buf, 0, (int)(conn.getContentLength() % buf.length)); + fos.close(); + //鍚姩涓嬭浇绾跨▼ + int fragment = length / threadNumber; + for (int i = 0; i < threadNumber-1; i++) { + new DownloadThread(conn,i*fragment,(i+1)*fragment-1,barrier).start(); + } + new DownloadThread(conn,(threadNumber-1)*fragment,length-1,barrier).start(); + barrier.await(); + } catch (Exception 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; + } + +} diff --git a/group11/996108220/src/com/coderising/download/FileDownloaderTest.java b/group11/996108220/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..323febec1b --- /dev/null +++ b/group11/996108220/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,57 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://pic17.nipic.com/20111102/3707281_235344313129_2.jpg"; + + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group11/996108220/src/com/coderising/download/api/Connection.java b/group11/996108220/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..39f87cd2fa --- /dev/null +++ b/group11/996108220/src/com/coderising/download/api/Connection.java @@ -0,0 +1,25 @@ +package com.coderising.download.api; + +import java.io.IOException; +import java.net.URLConnection; + +public interface Connection { + /** + * 缁欏畾寮濮嬪拰缁撴潫浣嶇疆锛 璇诲彇鏁版嵁锛 杩斿洖鍊兼槸瀛楄妭鏁扮粍 + * @param startPos 寮濮嬩綅缃紝 浠0寮濮 + * @param endPos 缁撴潫浣嶇疆 + * @return + */ + public URLConnection connection=null; + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 寰楀埌鏁版嵁鍐呭鐨勯暱搴 + * @return + */ + public int getContentLength(); + + /** + * 鍏抽棴杩炴帴 + */ + public void close(); +} diff --git a/group11/996108220/src/com/coderising/download/api/ConnectionException.java b/group11/996108220/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..8dbfe95dda --- /dev/null +++ b/group11/996108220/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group11/996108220/src/com/coderising/download/api/ConnectionManager.java b/group11/996108220/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..fb44ede457 --- /dev/null +++ b/group11/996108220/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group11/996108220/src/com/coderising/download/api/DownloadListener.java b/group11/996108220/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..4cd0b3eab1 --- /dev/null +++ b/group11/996108220/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group11/996108220/src/com/coderising/download/impl/ConnectionImpl.java b/group11/996108220/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..9b76a5efd7 --- /dev/null +++ b/group11/996108220/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,62 @@ +package com.coderising.download.impl; + + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; +import com.coding.basic.newLinkedList; + +public class ConnectionImpl implements Connection{ + URL url; + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + URLConnection connection=url.openConnection(); + InputStream ins =connection.getInputStream(); + byte[] buffer = new byte[endPos-startPos+1]; +// ins.skip(startPos); +// ins.read(buffer,0, endPos-startPos); + int at = startPos; + long amt=0; + while(at > 0) { + amt = ins.skip(at-amt); + if (amt<=0) { + break; + } + at -= amt; + } + int curPos =0; + while(true){ + int readByte = ins.read(buffer,curPos, endPos-startPos-curPos); + if(readByte <=0){ + break; + } + curPos= readByte + curPos; + } + ins.close(); + return buffer; + } + + @Override + public int getContentLength() { + + try { + return url.openConnection().getContentLength(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + + } + +} diff --git a/group11/996108220/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group11/996108220/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..114c095bf2 --- /dev/null +++ b/group11/996108220/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,30 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coding.basic.newLinkedList; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + try { + ConnectionImpl conn=null; + conn=new ConnectionImpl(); + conn.url =new URL(url); + return conn; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + + } + +} diff --git a/group11/996108220/src/com/coderising/litestruts/Struts.java b/group11/996108220/src/com/coderising/litestruts/Struts.java index 8f961f2175..84c7578e91 100644 --- a/group11/996108220/src/com/coderising/litestruts/Struts.java +++ b/group11/996108220/src/com/coderising/litestruts/Struts.java @@ -153,15 +153,18 @@ private static View updaView(View view,ActionConfig actionConfig,String message) // System.out.println(results.item(i).getTextContent()); // } // } -// -//// NamedNodeMap name = beans.item(0).getAttributes(); + +// NamedNodeMap name = beans.item(0).getAttributes(); +// NodeList results = doc.getElementsByTagName("struts"); +// for (int i = 0; i < results.getLength(); i++) { +// System.out.println(results.item(i).getAttributes().getNamedItem("name").getNodeValue()); +// System.out.println(results.item(i).getTextContent()); +// } // // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } - - -// } +// } } diff --git a/group11/996108220/src/com/coding/basic/LinkedList.java b/group11/996108220/src/com/coding/basic/LinkedList.java index be0b354bc2..8f5b928e02 100644 --- a/group11/996108220/src/com/coding/basic/LinkedList.java +++ b/group11/996108220/src/com/coding/basic/LinkedList.java @@ -1,14 +1,16 @@ package com.coding.basic; + import java.util.NoSuchElementException; -public class LinkedList implements List { + +public class LinkedList implements List { private Node head; private int size=0; /** * 澧炲姞鑺傜偣 */ - public void add(Object o){ + public void add(T o){ if(size==0){ head=new Node(); head.data=o; @@ -23,7 +25,7 @@ public void add(Object o){ /** * 鍦╥ndex(0~size)澶勬坊鍔犲厓绱 */ - public void add(int index , Object o){ + public void add(int index , T o){ if(index<0||index>size){ System.out.println("index瓒呭嚭鑼冨洿"+index); @@ -56,6 +58,16 @@ public Object get(int index){ return ptr; } } + public Node getNode(int index) { + if (index<0||index>=size) { + return null; + } + Node ptrNode=head; + for (int i = 0; i < index; i++) { + ptrNode=ptrNode.next; + } + return ptrNode; + } /** * 绉婚櫎index(0~size-1)鐨勫厓绱 */ @@ -79,14 +91,14 @@ public int size(){ return size; } - public void addFirst(Object o){ + public void addFirst(T o){ Node ptr=head; head=new Node(); head.data=o; head.next=ptr; size++; } - public void addLast(Object o){ + public void addLast(T o){ Node ptr=head; for(int i=1;i{ + T data; Node next; } public static void main(String args[]){ LinkedList list=new LinkedList(); - list.add(0); list.add(1); list.add(2); + list.add(2); list.add(3); list.add(4); + list.add(4); list.add(5); list.add(6); -// list.add(7,9); -// System.out.println(list.remove(5)); -// System.out.println(list.remove(5)); -// System.out.println(list.remove(6)); -// System.out.println(list.size()); -// Node ptr=list.head; -// do{ -// System.out.print(ptr.data); -// ptr=ptr.next; -// }while(ptr!=null); + list.add(6); + LinkedList list2=new LinkedList(); + list2.add(0); + list2.add(0); + list2.add(1); + list2.add(2); + list2.add(3); + list2.add(5); + list2.add(6); + list2.add(7); + list.removeRange(5,7); Iterator itr=list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + if (size==0||size==1) { + return ; + } + else if (size==2) { + Node ptr=head.next; + head.next=null; + ptr.next=head; + head=ptr; + } + else{ + Node pre=head; + Node ptr=head.next; + while (ptr.next!=null) { + Node node=ptr.next; + ptr.next=pre; + pre=ptr; + ptr=node; + } + ptr.next=pre; + head.next=null; + head=ptr; + } + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + Node ptr=head; + for (int i = 1; i < size/2; i++) { + ptr=ptr.next; + } + head=ptr.next; + size=size-size/2; + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + if (i>=size||i<0||i+length-1>=size) { + return; + } + Node ptr=head; + if (i==0) { + for (int j = 0; j < length; j++) { + ptr=ptr.next; + } + head=ptr; + } + else { + ptr=head; + Node pre=null; + for (int j = 0; j < i+length; j++) { + if (j==i-1) { + pre=ptr; + } + ptr=ptr.next; + } + pre.next=ptr; + } + size=size-length; + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + Node ptrNode=list.head; + int[] array=new int[list.size()]; + int i=0; + while (ptrNode!=null) { + int index=Integer.parseInt(String.valueOf(ptrNode.data)); + array[i++]=Integer.parseInt(String.valueOf(this.getNode(index).data)); + ptrNode=ptrNode.next; + } + return array; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + Node ptr=list.head; + if (list.size()==0||size==0) { + return; + } + while (ptr!=null) { + delete(ptr.data); + ptr=ptr.next; + } + + } + + private void delete(Comparable data) { + if (size==0) { + return; + } + else if (size==1) { + if (head.data.compareTo(data)==0) { + head=null; + size--; + } + } + else { + if (head.data.compareTo(data)==0) { + size--; + head=size==0?null:head.next; + return; + } + Node pre=head; + Node ptr=head.next; + do{ + if (ptr.data.compareTo(data)==0) { + pre.next=ptr.next; + size--; + break; + } + else { + pre=ptr; + ptr=ptr.next; + } + }while (ptr!=null); + } + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + removeDuplicateValues(head); + } + + private void removeDuplicateValues(Node node) { + if (node.next==null||node==null) { + return; + } + else { + if (node.data.compareTo(node.next.data)==0) { + node.next=node.next.next; + size--; + removeDuplicateValues(node); + } + else { + removeDuplicateValues(node.next); + } + } + + } + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if (size==0) { + return; + } + else if (size==1) { + if (head.data.compareTo(min)>0&&head.data.compareTo(max)<0) { + head=null; + size--; + } + else { + return; + } + } + else { + Node pre=null; + Node ptr=head; + Node tail=null; + int num=0; + + if (ptr==head&&(head.data.compareTo(min)>0&&head.data.compareTo(max)<0)) { + while (ptr!=null) { + if (ptr.data.compareTo(max)>=0) { + head=ptr; + size=size-num; + return; + } + else { + ptr=ptr.next; + num++; + } + } + size=0; + head=null; + return; + } + else if (head.data.compareTo(min)<=0) { + pre=head; + ptr=head.next; + while (ptr!=null) { + if (ptr.data.compareTo(min)<=0) { + pre=ptr; + ptr=ptr.next; + } + else if (ptr.data.compareTo(min)>0&&ptr.data.compareTo(max)<0) { + ptr=ptr.next; + num++; + } + else { + tail=ptr; + break; + } + } + if (pre==head) { + return; + } + else if (pre!=head&&tail!=null) { + size=size-num; + pre.next=tail; + }else { + pre.next=null; + size=size-num; + } + } + + + + } + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList linkedList=new LinkedList(); + Node node1=head; Node node2=list.head; + while (node1!=null&&node2!=null) { + if (node1.data==node2.data) { + linkedList.add(node1.data); + node1=node1.next; + node2=node2.next; + } + else if (Integer.parseInt(String.valueOf(node1.data)) { //鐢ㄩ摼琛ㄥ疄鐜伴槦鍒 private LinkedList elementData = new LinkedList(); - public void enQueue(Object o){ + public void enQueue(T o){ elementData.addLast(o);; } public Object deQueue(){ diff --git a/group11/996108220/src/com/coding/basic/newLinkedList.java b/group11/996108220/src/com/coding/basic/newLinkedList.java new file mode 100644 index 0000000000..e327a7288d --- /dev/null +++ b/group11/996108220/src/com/coding/basic/newLinkedList.java @@ -0,0 +1,5 @@ +package com.coding.basic; + +public class newLinkedList { + +} From a789603fd43b22cbe2918656c851c2089001ca9e Mon Sep 17 00:00:00 2001 From: Fred Qin Date: Thu, 16 Mar 2017 00:57:07 -0700 Subject: [PATCH 088/155] update assignment 1 and fix downloader read function/bug --- .../coderising/download/DownloadThread.java | 24 ++---- .../coderising/download/FileDownloader.java | 45 +++++----- .../download/FileDownloaderTest.java | 6 +- .../download/impl/ConnectionImpl.java | 40 +++++---- .../src/com/coding/basic/ArrayList.java | 82 +++++++++++-------- .../src/com/coding/basic/ArrayListTest.java | 1 - .../src/com/coding/basic/BinaryTreeNode.java | 48 +++++++---- .../395443277/src/com/coding/basic/Queue.java | 22 +++-- 8 files changed, 144 insertions(+), 124 deletions(-) diff --git a/group11/395443277/src/com/coderising/download/DownloadThread.java b/group11/395443277/src/com/coderising/download/DownloadThread.java index 3d9bd4b921..a02d9dad18 100644 --- a/group11/395443277/src/com/coderising/download/DownloadThread.java +++ b/group11/395443277/src/com/coderising/download/DownloadThread.java @@ -13,33 +13,25 @@ public class DownloadThread extends Thread { int endPos; String filePath; CyclicBarrier barrier; - byte[] totalBytes; - public DownloadThread(Connection conn, int startPos, int endPos, String filePath, CyclicBarrier barrier, - byte[] totalBytes) { + public DownloadThread(Connection conn, int startPos, int endPos, String filePath, CyclicBarrier barrier) { this.conn = conn; this.startPos = startPos; this.endPos = endPos; this.filePath = filePath; this.barrier = barrier; - this.totalBytes = totalBytes; } public void run() { try { byte[] bytes = conn.read(startPos, endPos); -// System.out.println(bytes.length); -// -// int i = 0; -// while (startPos <= endPos) { -// totalBytes[startPos] = bytes[i]; -// i++; -// startPos++; -// } - RandomAccessFile RAFile = new RandomAccessFile(filePath, "rw"); - RAFile.seek(startPos); - RAFile.write(bytes, 0, bytes.length); - RAFile.close(); + System.out.println(bytes.length); + + RandomAccessFile RAFile = new RandomAccessFile(filePath, "rw"); + RAFile.seek(startPos); + RAFile.write(bytes, 0, bytes.length); + RAFile.close(); + conn.close(); barrier.await(); diff --git a/group11/395443277/src/com/coderising/download/FileDownloader.java b/group11/395443277/src/com/coderising/download/FileDownloader.java index 60c80aacf6..3bb464b454 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloader.java +++ b/group11/395443277/src/com/coderising/download/FileDownloader.java @@ -1,8 +1,6 @@ package com.coderising.download; -import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.concurrent.CyclicBarrier; @@ -24,7 +22,7 @@ public FileDownloader(String _url) { this.url = _url; } - public void execute() { + public void execute(String filePath) { // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, @@ -43,7 +41,7 @@ public void execute() { Connection conn = null; try { - final int NUM_THREADS = 2; + final int NUM_THREADS = 3; conn = cm.open(this.url); @@ -54,42 +52,27 @@ public void execute() { int partLen = (int) Math.ceil(length / NUM_THREADS); // create a file - String filePath = "D://java_learning//test1.jpeg"; + createPlaceHolderFile(filePath, length); + byte[] totalBytes = new byte[length]; Runnable barrierAction = new Runnable() { @Override public void run() { -// FileOutputStream fos; -// try { -// fos = new FileOutputStream(filePath); -// fos.write(totalBytes); -// fos.close(); -// } catch (Exception e) { -// e.printStackTrace(); -// } - getListener().notifyFinished(); } }; CyclicBarrier barrier = new CyclicBarrier(3, barrierAction); - new DownloadThread(cm.open(this.url), 0, partLen - 1, filePath, barrier, totalBytes).start(); - new DownloadThread(cm.open(this.url), partLen, partLen * 2 - 1, filePath, barrier, totalBytes).start(); - new DownloadThread(cm.open(this.url), partLen * 2, length - 1, filePath, barrier, totalBytes).start(); - - // CyclicBarrier barrier = new CyclicBarrier(2, barrierAction); - // new DownloadThread(conn, 0, partLen - 1, filePath, barrier, - // totalBytes).start(); - // new DownloadThread(conn, partLen, length - 1, filePath, barrier, - // totalBytes).start(); - - // new DownloadThread(cm.open(this.url), 0, length - 1, filePath, - // barrier).start(); + new DownloadThread(cm.open(this.url), 0, partLen - 1, filePath, barrier).start(); + new DownloadThread(cm.open(this.url), partLen, partLen * 2 - 1, filePath, barrier).start(); + new DownloadThread(cm.open(this.url), partLen * 2, length - 1, filePath, barrier).start(); } catch (ConnectionException e) { e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } finally { if (conn != null) { conn.close(); @@ -98,6 +81,16 @@ public void run() { } + private void createPlaceHolderFile(String filePath, int contentLength) throws IOException { + RandomAccessFile file = new RandomAccessFile(filePath, "rw"); + + for (int i = 0; i < contentLength; i++) { + file.write(0); + } + + file.close(); + } + public void setListener(DownloadListener listener) { this.listener = listener; } diff --git a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java index 80e751805a..152edbc664 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloaderTest.java +++ b/group11/395443277/src/com/coderising/download/FileDownloaderTest.java @@ -23,7 +23,7 @@ public void testDownload() { // String url = "https://s-media-cache-ak0.pinimg.com/564x/8e/bd/00/8ebd00b1f2ef862b6c80d57c2b45d129.jpg"; // http://wallpapercraze.com/images/wallpapers/dota2_nevermore_w1.jpeg - String url = "http://wallpapercraze.com/images/wallpapers/dota2_nevermore_w1.jpeg"; + String url = "http://eskipaper.com/images/large-2.jpg"; FileDownloader downloader = new FileDownloader(url); @@ -39,8 +39,8 @@ public void notifyFinished() { }); - - downloader.execute(); + String filePath = "D://java_learning//test.jpg"; + downloader.execute(filePath); // 绛夊緟澶氱嚎绋嬩笅杞界▼搴忔墽琛屽畬姣 while (!downloadFinished) { diff --git a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java index ae98b63f77..c1de2c97b9 100644 --- a/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group11/395443277/src/com/coderising/download/impl/ConnectionImpl.java @@ -1,14 +1,18 @@ package com.coderising.download.impl; import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Arrays; + import com.coderising.download.api.Connection; public class ConnectionImpl implements Connection { HttpURLConnection urlcon = null; + static final int BUFFER_SIZE = 1024; public ConnectionImpl(String url) { try { @@ -21,24 +25,30 @@ public ConnectionImpl(String url) { @Override public byte[] read(int startPos, int endPos) throws IOException { - byte[] bytes = new byte[endPos - startPos + 1]; urlcon.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); - InputStream is = new BufferedInputStream(urlcon.getInputStream()); - // Read in the bytes - int offset = startPos; - int numRead = 0; - while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { - offset += numRead; + InputStream is = urlcon.getInputStream(); + + byte[] buff = new byte[BUFFER_SIZE]; + + int totalLength = endPos - startPos + 1; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while (baos.size() < totalLength) { + int len = is.read(buff); + if (len < 0) { + break; + } + baos.write(buff, 0, len); + } + + if (baos.size() > totalLength) { + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLength); } - - System.out.println("start is: " + startPos); - -// for (int i=0; i elementData.length) { + int newCapacity = Math.max(minCapacity, elementData.length * 2); + Object[] newArray = new Object[newCapacity]; + System.arraycopy(elementData, 0, newArray, 0, elementData.length); + elementData = newArray; + } } - - public void add(int index, Object o){ - // check size - if (size == elementData.length) { - doubleSize(); + + public void add(int index, Object o) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); } - - //shift and add element - System.arraycopy(elementData, index, elementData, index+1, size - index); + + ensureCapacity(size + 1); + // shift and add element + System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = o; size++; } - - public Object get(int index){ + + public Object get(int index) { + // check input + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + return elementData[index]; } - - public Object remove(int index){ + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + if (size == 0) { return null; } - + // remove element and shift Object target = elementData[index]; - System.arraycopy(elementData, index+1, elementData, index, size - index - 1); - + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + // reset last element - elementData[size-1] = null; + elementData[size - 1] = null; size--; return target; } - - public int size(){ + + public int size() { return size; } - - public Iterator iterator (){ + + public Iterator iterator() { return new SeqIterator(); } - + private class SeqIterator implements Iterator { int i = 0; - + @Override public boolean hasNext() { return i < size; @@ -76,7 +86,7 @@ public Object next() { } return elementData[i++]; } - + } - + } diff --git a/group11/395443277/src/com/coding/basic/ArrayListTest.java b/group11/395443277/src/com/coding/basic/ArrayListTest.java index 75bae320f4..ec99bc24f6 100644 --- a/group11/395443277/src/com/coding/basic/ArrayListTest.java +++ b/group11/395443277/src/com/coding/basic/ArrayListTest.java @@ -58,7 +58,6 @@ public void testRemove() { assertEquals(2, list.get(2)); assertEquals(4, list.size()); - assertEquals(null, list.get(4)); list.add(6); assertEquals(6, list.get(4)); diff --git a/group11/395443277/src/com/coding/basic/BinaryTreeNode.java b/group11/395443277/src/com/coding/basic/BinaryTreeNode.java index 907fc23275..7ebd1f2a4e 100644 --- a/group11/395443277/src/com/coding/basic/BinaryTreeNode.java +++ b/group11/395443277/src/com/coding/basic/BinaryTreeNode.java @@ -1,37 +1,49 @@ package com.coding.basic; -public class BinaryTreeNode implements Comparable{ - +public class BinaryTreeNode implements Comparable { + private Object data; private BinaryTreeNode left; private BinaryTreeNode right; - + public Object getData() { return data; } + public void setData(Object data) { this.data = data; } + public BinaryTreeNode getLeft() { return left; } + public void setLeft(BinaryTreeNode left) { this.left = left; } + public BinaryTreeNode getRight() { return right; } + public void setRight(BinaryTreeNode right) { this.right = right; } - - public BinaryTreeNode insert(Object o){ - if (this.compareTo(o)==0) { - return null; + + public BinaryTreeNode insert(Object o) { + // root element + if (this.data == null) { + this.data = o; + return this; + } + + // equals return the node + if (this.compareTo(o) == 0) { + return this; } else { // current value less than inserted value // go right - if (this.compareTo(o)<0) { + if (this.compareTo(o) < 0) { if (this.right == null) { BinaryTreeNode nd = new BinaryTreeNode(); nd.setData(o); @@ -39,10 +51,10 @@ public BinaryTreeNode insert(Object o){ } else { this.getRight().insert(o); } - } + } // greater than // go left - else if(this.compareTo(o)>0) { + else if (this.compareTo(o) > 0) { if (this.left == null) { BinaryTreeNode nd = new BinaryTreeNode(); nd.setData(o); @@ -52,24 +64,24 @@ else if(this.compareTo(o)>0) { } } } - - return null; + + return null; } - + /** * oversimplified implementation: only allows int and string */ @Override - public int compareTo(Object nd) throws ClassCastException{ + public int compareTo(Object nd) throws ClassCastException { if (!(nd instanceof Object)) { throw new ClassCastException("An object expected."); } - + if (nd instanceof String) { - return ((String)this.data).compareTo((String) nd); + return ((String) this.data).compareTo((String) nd); } else { - return ((int) this.data) -((int) nd); + return ((int) this.data) - ((int) nd); } } - + } diff --git a/group11/395443277/src/com/coding/basic/Queue.java b/group11/395443277/src/com/coding/basic/Queue.java index f12e73d46d..4b29108ef9 100644 --- a/group11/395443277/src/com/coding/basic/Queue.java +++ b/group11/395443277/src/com/coding/basic/Queue.java @@ -2,20 +2,24 @@ public class Queue { private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o){ + + public void enQueue(Object o) { elementData.add(o); } - - public Object deQueue(){ + + public Object deQueue() { + if (size() == 0) { + return null; + } + return elementData.removeFirst(); } - - public boolean isEmpty(){ - return elementData.size()==0; + + public boolean isEmpty() { + return elementData.size() == 0; } - - public int size(){ + + public int size() { return elementData.size(); } } From 0516d03c5a461829a7d0314cb21e0b7425d58048 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Thu, 16 Mar 2017 22:10:54 +0800 Subject: [PATCH 089/155] =?UTF-8?q?=E5=AE=8C=E6=88=90ArrayUtil=E5=8F=8A?= =?UTF-8?q?=E5=8D=95=E6=B5=8B=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/315863321/.gitignore | 3 + .../ArrayUtil.java | 234 ++++++++++++++++++ .../johnChnia/coding2017/basic/ArrayList.java | 17 +- .../coderising2017/array/ArrayUtilTest.java | 114 +++++++++ 4 files changed, 366 insertions(+), 2 deletions(-) create mode 100644 group24/315863321/.gitignore create mode 100644 group24/315863321/src/main/java/com.johnChnia.coderising2017.array/ArrayUtil.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coderising2017/array/ArrayUtilTest.java diff --git a/group24/315863321/.gitignore b/group24/315863321/.gitignore new file mode 100644 index 0000000000..5e57f14b91 --- /dev/null +++ b/group24/315863321/.gitignore @@ -0,0 +1,3 @@ +*.xml +*.iml +*.jsp \ No newline at end of file diff --git a/group24/315863321/src/main/java/com.johnChnia.coderising2017.array/ArrayUtil.java b/group24/315863321/src/main/java/com.johnChnia.coderising2017.array/ArrayUtil.java new file mode 100644 index 0000000000..2fb2614eb5 --- /dev/null +++ b/group24/315863321/src/main/java/com.johnChnia.coderising2017.array/ArrayUtil.java @@ -0,0 +1,234 @@ +package com.johnChnia.coderising2017.array; + +import com.johnChnia.coding2017.basic.Queue; + +import java.util.ArrayList; + + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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) { + int length = origin.length; + for (int i = 0; i < length / 2; i++) { + int start = i; + int end = length - 1 - i; + int temp = origin[start]; + origin[start] = origin[end]; + origin[end] = temp; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + Queue notZeroQueue = new Queue(); + for (int element : + oldArray) { + if (element != 0) { + notZeroQueue.add(element); + } + } + int[] newArray = new int[notZeroQueue.size()]; + for (int index = 0; index < newArray.length; index++) { + newArray[index] = (int) notZeroQueue.remove(); + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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) { + ArrayList array1List = new ArrayList<>(); + for (int element : + array1) { + array1List.add(element); + } + for (int i = 0; i < array2.length; i++) { + int inserted = array2[i]; + for (int j = 0; j < array1List.size(); j++) { + if (array1List.indexOf(inserted) != -1) { + break; + } else if (inserted < array1List.get(j)) { + array1List.add(j, inserted); + } else if (j == array1List.size() - 1) { + array1List.add(inserted); + } + } + } + int[] newArray = new int[array1List.size()]; + for (int i = 0; i < array1List.size(); i++) { + newArray[i] = array1List.get(i); + } + + return newArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] copy = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, copy, 0, + Math.min(oldArray.length, copy.length)); + return copy; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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) { + int[] newArray; + if (max == 1) { + newArray = new int[0]; + } else if (max == 2) { + newArray = new int[]{1, 1}; + } else { + int next; + ArrayList arrayList = new ArrayList<>(); + arrayList.add(1); + arrayList.add(1); + for (int i = 0; i < arrayList.size(); i++) { + next = arrayList.get(i) + arrayList.get(i + 1); + if (next < max) { + arrayList.add(next); + } else { + break; + } + } + + newArray = new int[arrayList.size()]; + for (int i = 0; i < arrayList.size(); i++) { + newArray[i] = arrayList.get(i); + } + } + + return newArray; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + ArrayList arrayList = new ArrayList<>(); + for (int i = 0; i < max; i++) { + if (i <= 1) { + continue; + } + boolean flag = true; + for (int j = 2; j <= Math.sqrt(i); j++) { + if (i % j == 0) { + flag = false; + } + } + if (flag) { + arrayList.add(i); + } + } + int[] newArray = new int[arrayList.size()]; + for (int i = 0; i < arrayList.size(); i++) { + newArray[i] = arrayList.get(i); + } + return newArray; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList arrayList = new ArrayList<>(); + for (int i = 1; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + arrayList.add(i); + } + } + + int[] newArray = new int[arrayList.size()]; + for (int i = 0; i < arrayList.size(); i++) { + newArray[i] = arrayList.get(i); + } + + return newArray; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < array.length-1; i++) { + stringBuilder.append(array[i]); + stringBuilder.append(seperator); + } + stringBuilder.append(array[array.length-1]); + return stringBuilder.toString(); + } + + + public void printArray(int[] array) { + for (int element : + array) { + System.out.print(element + ", "); + } + } + + + public static void main(String[] args) { + int[] a = new int[]{2,3,5,7,11,13,17,19}; + ArrayUtil arrayUtil = new ArrayUtil(); + System.out.println(arrayUtil.join(a, "-")); + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java index 800f89d6ab..0be160ec0f 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -10,6 +10,15 @@ public class ArrayList implements List{ private Object[] elementData; private int size = 0; + private static final int DEFAULTCAPACITY = 10; + + + /** + * Constructs an list with the default capacity. + */ + public ArrayList() { + elementData = new Object[DEFAULTCAPACITY]; + } /** * Constructs an list with the specified initial capacity. @@ -135,11 +144,15 @@ private void ensureCapacityInternal(int minCapacity) { grow(); } + public Object[] toArray() { + return Arrays.copyOf(elementData, size()); + } + /** * A version of rangeCheck used by add and addAll. */ private void rangeCheckForAdd(int index) { - if (index > elementData.length - 1 || index < 0) { + if (index > size() - 1 || index < 0) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } } @@ -160,7 +173,7 @@ private String outOfBoundsMsg(int index) { * which throws an ArrayIndexOutOfBoundsException if index is negative. */ private void rangeCheck(int index) { - if (index >= size) { + if (index >= size()) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } } diff --git a/group24/315863321/src/test/java/com/johnChnia/coderising2017/array/ArrayUtilTest.java b/group24/315863321/src/test/java/com/johnChnia/coderising2017/array/ArrayUtilTest.java new file mode 100644 index 0000000000..9b730c19e5 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coderising2017/array/ArrayUtilTest.java @@ -0,0 +1,114 @@ +package com.johnChnia.coderising2017.array; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/16. + */ +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + private int[] array1; + private int[] array2; + private int[] array3; + private int[] array4; + private int[] array5; + private int[] array6; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + array1 = new int[]{7, 9, 30, 3}; + array2 = new int[]{1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + array3 = new int[]{3, 5, 7, 8}; + array4 = new int[]{4, 5, 6, 7}; + array5 = new int[]{2, 3, 6}; + array6 = new int[]{2, 3, 5, 7, 11, 13, 17, 19}; + } + + @Test + public void testReverseArray() throws Exception { + arrayUtil.reverseArray(array1); + assertThat(array1[0], equalTo(3)); + assertThat(array1[1], equalTo(30)); + assertThat(array1[2], equalTo(9)); + assertThat(array1[3], equalTo(7)); + } + + @Test + public void testRemoveZero() throws Exception { + int[] newArray = arrayUtil.removeZero(array2); + for (int element : + newArray) { + assertThat(element, not(0)); + } + } + + @Test + public void testMerge() throws Exception { + int[] newArray = arrayUtil.merge(array3, array4); + assertThat(newArray[0], equalTo(3)); + assertThat(newArray[1], equalTo(4)); + assertThat(newArray[2], equalTo(5)); + assertThat(newArray[3], equalTo(6)); + assertThat(newArray[4], equalTo(7)); + assertThat(newArray[5], equalTo(8)); + } + + @Test + public void testGrow() throws Exception { + int[] newArray = arrayUtil.grow(array5, 3); + assertThat(newArray[0], equalTo(2)); + assertThat(newArray[1], equalTo(3)); + assertThat(newArray[2], equalTo(6)); + assertThat(newArray[3], equalTo(0)); + assertThat(newArray[4], equalTo(0)); + assertThat(newArray[5], equalTo(0)); + } + + @Test + public void testFibonacci() throws Exception { + int[] newArray = arrayUtil.fibonacci(15); + assertThat(newArray[0], equalTo(1)); + assertThat(newArray[1], equalTo(1)); + assertThat(newArray[2], equalTo(2)); + assertThat(newArray[3], equalTo(3)); + assertThat(newArray[4], equalTo(5)); + assertThat(newArray[5], equalTo(8)); + assertThat(newArray[6], equalTo(13)); + } + + @Test + public void testGetPrimes() throws Exception { + int[] newArray = arrayUtil.getPrimes(23); + assertThat(newArray[0], equalTo(2)); + assertThat(newArray[1], equalTo(3)); + assertThat(newArray[2], equalTo(5)); + assertThat(newArray[3], equalTo(7)); + assertThat(newArray[4], equalTo(11)); + assertThat(newArray[5], equalTo(13)); + assertThat(newArray[6], equalTo(17)); + assertThat(newArray[7], equalTo(19)); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] newArray = arrayUtil.getPerfectNumbers(100); + assertThat(newArray.length, equalTo(2)); + assertThat(newArray[0], equalTo(6)); + assertThat(newArray[1], equalTo(28)); + } + + @Test + public void testJoin() throws Exception { + assertThat(arrayUtil.join(array6, "-"), + containsString("2-3-5-7-11-13-17-19")); + } + +} \ No newline at end of file From 4f38030e0a022d032cc15499f1450e39b2436608 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Thu, 16 Mar 2017 22:17:10 +0800 Subject: [PATCH 090/155] =?UTF-8?q?=E5=AE=8C=E6=88=90ArrayUtil=E5=8F=8A?= =?UTF-8?q?=E5=8D=95=E6=B5=8B=E7=94=A8=E4=BE=8B=EF=BC=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=8C=85=E5=90=8D=E5=8F=8A=E5=88=A0=E9=99=A4main=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../johnChnia/coderising2017/array}/ArrayUtil.java | 6 ------ 1 file changed, 6 deletions(-) rename group24/315863321/src/main/java/{com.johnChnia.coderising2017.array => com/johnChnia/coderising2017/array}/ArrayUtil.java (97%) diff --git a/group24/315863321/src/main/java/com.johnChnia.coderising2017.array/ArrayUtil.java b/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java similarity index 97% rename from group24/315863321/src/main/java/com.johnChnia.coderising2017.array/ArrayUtil.java rename to group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java index 2fb2614eb5..bc0e921a85 100644 --- a/group24/315863321/src/main/java/com.johnChnia.coderising2017.array/ArrayUtil.java +++ b/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java @@ -225,10 +225,4 @@ public void printArray(int[] array) { } } - - public static void main(String[] args) { - int[] a = new int[]{2,3,5,7,11,13,17,19}; - ArrayUtil arrayUtil = new ArrayUtil(); - System.out.println(arrayUtil.join(a, "-")); - } } From 653701939373688b24a113941e2d7bd5337c07fc Mon Sep 17 00:00:00 2001 From: johnChnia Date: Thu, 16 Mar 2017 22:20:43 +0800 Subject: [PATCH 091/155] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20rangeCheckForAdd?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/johnChnia/coding2017/basic/ArrayList.java | 1 - 1 file changed, 1 deletion(-) diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java index 0be160ec0f..8daec3782f 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -45,7 +45,6 @@ public ArrayList(int initialCapacity) { */ public Object get(int index) { rangeCheck(index); - rangeCheckForAdd(index); return elementData[index]; } From 4ed9b9679265a1cdda612f2b15bf74f6b4e8430a Mon Sep 17 00:00:00 2001 From: lzb Date: Fri, 17 Mar 2017 01:07:34 +0800 Subject: [PATCH 092/155] =?UTF-8?q?=E5=AE=8C=E6=88=90litestruts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homework0312/{ => basic}/ArrayList.java | 2 +- .../{ => basic}/BinaryTreeNode.java | 2 +- .../homework0312/{ => basic}/Iterator.java | 2 +- .../homework0312/{ => basic}/LinkedList.java | 2 +- .../me/lzb/homework0312/{ => basic}/List.java | 2 +- .../lzb/homework0312/{ => basic}/Queue.java | 2 +- .../lzb/homework0312/{ => basic}/Stack.java | 2 +- .../homework0319/{ => array}/ArrayUtil.java | 2 +- .../homework0319/litestruts/LoginAction.java | 39 ++++++++++ .../lzb/homework0319/litestruts/Struts.java | 71 +++++++++++++++++++ .../me/lzb/homework0319/litestruts/View.java | 23 ++++++ .../lzb/homework0319/litestruts/XmlUtil.java | 45 ++++++++++++ .../src/main/resources/litestruts/struts.xml | 11 +++ .../{ => basic}/ArrayListTest.java | 6 +- .../{ => basic}/LinkedListTest.java | 6 +- .../homework0319/litestruts/StrutsTest.java | 41 +++++++++++ .../src/test/resources/litestruts/struts.xml | 11 +++ group24/1148285693/learning2017/pom.xml | 32 ++++++++- 18 files changed, 286 insertions(+), 15 deletions(-) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{ => basic}/ArrayList.java (98%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{ => basic}/BinaryTreeNode.java (96%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{ => basic}/Iterator.java (77%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{ => basic}/LinkedList.java (99%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{ => basic}/List.java (84%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{ => basic}/Queue.java (93%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/{ => basic}/Stack.java (96%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/{ => array}/ArrayUtil.java (99%) create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/LoginAction.java create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/View.java create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/XmlUtil.java create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/resources/litestruts/struts.xml rename group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/{ => basic}/ArrayListTest.java (95%) rename group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/{ => basic}/LinkedListTest.java (95%) create mode 100644 group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0319/litestruts/StrutsTest.java create mode 100644 group24/1148285693/learning2017/learning-basic/src/test/resources/litestruts/struts.xml diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/ArrayList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java similarity index 98% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/ArrayList.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java index 56e3a6ab12..6fde63911f 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/ArrayList.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; /** * 绠鏄揂rrayList diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/BinaryTreeNode.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/BinaryTreeNode.java similarity index 96% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/BinaryTreeNode.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/BinaryTreeNode.java index 80a7e61027..053baad0d4 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/BinaryTreeNode.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; /** * 宸﹁竟姣旂埗鑺傜偣灏忥紝鍙宠竟姣旂埗鑺傜偣澶 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Iterator.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Iterator.java similarity index 77% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Iterator.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Iterator.java index d97e9bc7a2..138e090126 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Iterator.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Iterator.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; /** * Created by LZB on 2017/3/11. diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/LinkedList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/LinkedList.java similarity index 99% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/LinkedList.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/LinkedList.java index 223983829c..7b12d27c6c 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/LinkedList.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/LinkedList.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; /** diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/List.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/List.java similarity index 84% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/List.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/List.java index f0408185bd..bd66593efa 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/List.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/List.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; /** * list鎺ュ彛 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Queue.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Queue.java similarity index 93% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Queue.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Queue.java index 21bce4591f..50ea66f1a2 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Queue.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Queue.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; /** * 鍏堣繘鍏堝嚭 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Stack.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Stack.java similarity index 96% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Stack.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Stack.java index ea434cbfbc..4cd8d3dfd9 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/Stack.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/Stack.java @@ -1,4 +1,4 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; /** * 鍏堣繘鍚庡嚭 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/ArrayUtil.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/array/ArrayUtil.java similarity index 99% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/ArrayUtil.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/array/ArrayUtil.java index 640a2e4843..94f7b60c64 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/ArrayUtil.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/array/ArrayUtil.java @@ -1,4 +1,4 @@ -package me.lzb.homework0319; +package me.lzb.homework0319.array; public class ArrayUtil { diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/LoginAction.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/LoginAction.java new file mode 100644 index 0000000000..4c4ca22187 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package me.lzb.homework0319.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java new file mode 100644 index 0000000000..96bc0c5036 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java @@ -0,0 +1,71 @@ +package me.lzb.homework0319.litestruts; + +import org.apache.commons.lang3.StringUtils; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +/* + +0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 +鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 +("name"="test" , "password"="1234") , +閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + +2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + +3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, +閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , +鏀惧埌View瀵硅薄鐨刾arameters + +4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 +鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + +*/ + + +public class Struts { + private static final String XML = "struts.xml"; + private static final XmlUtil xmlUtil = XmlUtil.create(XML); + + public static View runAction(String actionName, Map parameters) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { + + Class loginClasss = Class.forName(xmlUtil.getAuctionPathByName(actionName)); + LoginAction loginAction = (LoginAction) loginClasss.newInstance(); + + for (Map.Entry entry : parameters.entrySet()) { + Method mSetter = loginClasss.getDeclaredMethod("set" + StringUtils.capitalize(entry.getKey()), String.class); + mSetter.invoke(loginAction, entry.getValue()); + } + + Method mExectue = loginClasss.getDeclaredMethod("execute"); + String resultName = (String) mExectue.invoke(loginAction); + + + //鑾峰彇鎵鏈夊睘鎬у拰get鏂规硶 + Map resultMap = new HashMap(); + Method[] methods = loginClasss.getDeclaredMethods(); + for(int i =0 ;i + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/ArrayListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java similarity index 95% rename from group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/ArrayListTest.java rename to group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java index 2683dbcf75..4938e6a8ac 100644 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/ArrayListTest.java +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java @@ -1,7 +1,7 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; -import me.lzb.homework0312.ArrayList; -import me.lzb.homework0312.Iterator; +import me.lzb.homework0312.basic.ArrayList; +import me.lzb.homework0312.basic.Iterator; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/LinkedListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java similarity index 95% rename from group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/LinkedListTest.java rename to group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java index 16e3e5bc8c..0d48c290f1 100644 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/LinkedListTest.java +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java @@ -1,7 +1,7 @@ -package me.lzb.homework0312; +package me.lzb.homework0312.basic; -import me.lzb.homework0312.Iterator; -import me.lzb.homework0312.LinkedList; +import me.lzb.homework0312.basic.Iterator; +import me.lzb.homework0312.basic.LinkedList; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0319/litestruts/StrutsTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0319/litestruts/StrutsTest.java new file mode 100644 index 0000000000..027c33eba9 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0319/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package me.lzb.homework0319.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception{ + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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() throws Exception{ + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group24/1148285693/learning2017/learning-basic/src/test/resources/litestruts/struts.xml b/group24/1148285693/learning2017/learning-basic/src/test/resources/litestruts/struts.xml new file mode 100644 index 0000000000..81c153757c --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/test/resources/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/1148285693/learning2017/pom.xml b/group24/1148285693/learning2017/pom.xml index 6493774794..4951c1ff42 100644 --- a/group24/1148285693/learning2017/pom.xml +++ b/group24/1148285693/learning2017/pom.xml @@ -65,13 +65,43 @@ - + junit junit 4.12 + + + dom4j + dom4j + 1.6.1 + + + + jaxen + jaxen + 1.1.6 + + + + + commons-io + commons-io + 2.5 + + + org.apache.commons + commons-lang3 + 3.5 + + + commons-codec + commons-codec + 1.10 + + From 3ec006f63ac063e4e69e36b297ff7a5e3e85dad8 Mon Sep 17 00:00:00 2001 From: em14Vito Date: Fri, 17 Mar 2017 03:21:16 +0800 Subject: [PATCH 093/155] add homework 2 --- group23/729693763/Second_Homework2/.classpath | 9 + group23/729693763/Second_Homework2/.project | 17 ++ .../.settings/org.eclipse.jdt.core.prefs | 11 + .../src/com/danny/hw2/ArrayUtil.java | 221 ++++++++++++++++++ .../src/com/danny/hw2/LoginAction.java | 39 ++++ .../src/com/danny/hw2/Struts.java | 163 +++++++++++++ .../src/com/danny/hw2/View.java | 23 ++ .../src/com/danny/hw2/test/ArrayUtilTest.java | 95 ++++++++ .../src/com/danny/hw2/test/StrutsTest.java | 46 ++++ .../Second_Homework2/xmlFolder/struts.xml | 11 + 10 files changed, 635 insertions(+) create mode 100644 group23/729693763/Second_Homework2/.classpath create mode 100644 group23/729693763/Second_Homework2/.project create mode 100644 group23/729693763/Second_Homework2/.settings/org.eclipse.jdt.core.prefs create mode 100644 group23/729693763/Second_Homework2/src/com/danny/hw2/ArrayUtil.java create mode 100644 group23/729693763/Second_Homework2/src/com/danny/hw2/LoginAction.java create mode 100644 group23/729693763/Second_Homework2/src/com/danny/hw2/Struts.java create mode 100644 group23/729693763/Second_Homework2/src/com/danny/hw2/View.java create mode 100644 group23/729693763/Second_Homework2/src/com/danny/hw2/test/ArrayUtilTest.java create mode 100644 group23/729693763/Second_Homework2/src/com/danny/hw2/test/StrutsTest.java create mode 100644 group23/729693763/Second_Homework2/xmlFolder/struts.xml diff --git a/group23/729693763/Second_Homework2/.classpath b/group23/729693763/Second_Homework2/.classpath new file mode 100644 index 0000000000..aa7b00d27d --- /dev/null +++ b/group23/729693763/Second_Homework2/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group23/729693763/Second_Homework2/.project b/group23/729693763/Second_Homework2/.project new file mode 100644 index 0000000000..fb7175dae6 --- /dev/null +++ b/group23/729693763/Second_Homework2/.project @@ -0,0 +1,17 @@ + + + Second_Homework2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group23/729693763/Second_Homework2/.settings/org.eclipse.jdt.core.prefs b/group23/729693763/Second_Homework2/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group23/729693763/Second_Homework2/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group23/729693763/Second_Homework2/src/com/danny/hw2/ArrayUtil.java b/group23/729693763/Second_Homework2/src/com/danny/hw2/ArrayUtil.java new file mode 100644 index 0000000000..af11509593 --- /dev/null +++ b/group23/729693763/Second_Homework2/src/com/danny/hw2/ArrayUtil.java @@ -0,0 +1,221 @@ +package com.danny.hw2; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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) { + int size = origin.length; + int[] temp = new int[size]; + + for(int i=0;i < size; i++){ + temp[i] = origin[size-i-1]; + } + + for(int i=0;i array2[index2] ){ + result[resultIndex++] = array2[index2++]; + break; + } + } else{ + result[resultIndex++] = array1[index1++]; + } + } + System.out.println(resultIndex); + return Arrays.copyOf(result, resultIndex); + + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int oldSize = oldArray.length; + int[] newArray= new int[oldSize+size]; + + for ( int i = 0; i < newArray.length; i++) { + if ( i < oldSize ) { + newArray[i] = oldArray[i]; + } else{ + newArray[i] = 0; + } + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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]; + } + + int maxIndex = 1000; + int realIndex = 0; + int[] array=new int[maxIndex]; + for (; realIndex <= array.length; realIndex++) { + int fibonacciNum = getFibonacciArray(realIndex+1); + if(fibonacciNum > max){ + break; + } + array[realIndex] = fibonacciNum; + } + + + + return Arrays.copyOf(array, realIndex); + } + + + private int getFibonacciArray(int index){ + + if(index == 0 || index == 1){ + return index; + } + return getFibonacciArray(index - 1) + getFibonacciArray(index - 2); + } + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int maxIndex = 1000; + int realSize = 0; + int[] array = new int[maxIndex]; + for (int i = 0; i < max; i++) { + if(isPrimers(i)){ + array[realSize++] = i; + } + } + return Arrays.copyOf(array, realSize); + } + private static boolean isPrimers(int n){ + if (n <= 3) { + return n > 1; + } + + for(int i=2;i<=Math.sqrt(n);i++){ + if(n%i == 0) + return false; + } + return true; + } + + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int maxIndex = 1000; + int realIndex = 0; + int[] array = new int[maxIndex]; + int sum = 0; + + for (int i = 1; i < max /2 +1; i++) { + if(max % i == 0){ + sum += i; + array[realIndex++] = i; + } + } + if(sum == max){ + return Arrays.copyOf(array, realIndex); + }else{ + return new int[0]; + } + + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String result=""; + for (int i = 0; i < array.length; i++) { + result+= String.valueOf(array[i]) + seperator; + } + //鍘绘帀澶氫綑鐨勪竴涓猻eperator + return result.substring(0, result.length()-1); + } + + + + +} diff --git a/group23/729693763/Second_Homework2/src/com/danny/hw2/LoginAction.java b/group23/729693763/Second_Homework2/src/com/danny/hw2/LoginAction.java new file mode 100644 index 0000000000..340d542dec --- /dev/null +++ b/group23/729693763/Second_Homework2/src/com/danny/hw2/LoginAction.java @@ -0,0 +1,39 @@ +package com.danny.hw2; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group23/729693763/Second_Homework2/src/com/danny/hw2/Struts.java b/group23/729693763/Second_Homework2/src/com/danny/hw2/Struts.java new file mode 100644 index 0000000000..6e1cb4ba96 --- /dev/null +++ b/group23/729693763/Second_Homework2/src/com/danny/hw2/Struts.java @@ -0,0 +1,163 @@ +package com.danny.hw2; + +import java.io.File; +import java.lang.invoke.CallSite; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import org.jaxen.*; + +public class Struts { + + private static Document file = null; + private static String file_path = + new File("").getAbsolutePath()+".\\xmlFolder\\struts.xml"; + + private static String viewPath="com.danny.hw2.View"; + + //0. 璇诲彇閰嶇疆鏂囦欢struts.xml + private static Document parse(String path){ + SAXReader reader = new SAXReader(); + Document document = null; + try { + document = reader.read(path); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return document; + } + + private static Element getAction(Document document,String name){ + + List lElement = (List) file.selectObject("/struts/action"); + Element actionElement = null; + for(Element e:lElement){ + if(e.attributeValue("name").equals(name)){ + actionElement = e; + break; + } + } + return actionElement; + } + + public static View runAction(String actionName, Map parameters) { + + file = parse(file_path); + + //杩斿洖鐨刢lass View; + Object viewClass = null; + + + //0 Get Action Element + Element actionElement = getAction(file, actionName); + + + String actionClassPath = actionElement.attributeValue("class"); + System.out.println(actionClassPath); + + Object action = null; + + try { + /* + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + */ + action = Class.forName(actionClassPath).newInstance(); + + Method setName = action.getClass().getMethod("setName",String.class); + setName.invoke(action, parameters.get("name")); + + Method setPassword = action.getClass().getMethod("setPassword",String.class); + setPassword.invoke(action, parameters.get("password")); + + + //2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + Method execute = action.getClass().getMethod("execute", null); + Object status = execute.invoke(action); + + System.out.println(status.toString()); + + + //3鑾峰緱鎵鏈夌殑Getter瀛楁 + Field[] files = action.getClass().getDeclaredFields(); + + HashMap save = new HashMap<>(); + + //鎵惧埌闇瑕佺殑鏁版嵁锛岀嚎瀛樺埌save涓 + for (int i = 0; i < files.length; i++) { + String name = files[i].getName(); + name = "get" + name.substring(0,1).toUpperCase() + + name.substring(1); + + + Method getter = action.getClass().getMethod(name,null); + save.put(files[i].getName(), (String) getter.invoke(action)); + + } + //濉炶繘viewClass閲岄潰 + viewClass = Class.forName(viewPath).newInstance(); + Method setParameters = viewClass.getClass().getMethod("setParameters", Map.class); + setParameters.invoke(viewClass, save); + + + + //4灏唈sp鏀捐繘鍘 + List resultElement = actionElement.selectNodes("result"); + for(Element e:resultElement){ + if(e.attributeValue("name").equals(status.toString())){ + String jspFields= e.getStringValue(); + viewClass.getClass().getMethod("setJsp", String.class). + invoke(viewClass, jspFields); + } + + } + + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + + throw new RuntimeException(e.getMessage()); + } + + + + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + + return (View) viewClass; + } + + +} diff --git a/group23/729693763/Second_Homework2/src/com/danny/hw2/View.java b/group23/729693763/Second_Homework2/src/com/danny/hw2/View.java new file mode 100644 index 0000000000..67ff0a061b --- /dev/null +++ b/group23/729693763/Second_Homework2/src/com/danny/hw2/View.java @@ -0,0 +1,23 @@ +package com.danny.hw2; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group23/729693763/Second_Homework2/src/com/danny/hw2/test/ArrayUtilTest.java b/group23/729693763/Second_Homework2/src/com/danny/hw2/test/ArrayUtilTest.java new file mode 100644 index 0000000000..a7fce5320a --- /dev/null +++ b/group23/729693763/Second_Homework2/src/com/danny/hw2/test/ArrayUtilTest.java @@ -0,0 +1,95 @@ +package com.danny.hw2.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.danny.hw2.ArrayUtil; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { +// * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] 濡傛灉 a = +// * [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + int[] testData = new int[]{7,9,30,3}; + int[] ans = new int[]{3,30,9,7}; + new ArrayUtil().reverseArray(testData); + + for (int i = 0; i < ans.length; i++) { + assertEquals(ans[i], testData[i]); + } + } + + @Test + public void testRemoveZero() { + int[] testData = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] ans = new int[]{1,3,4,5,6,6,5,4,7,6,7,5}; + + int[] test = new ArrayUtil().removeZero(testData); + for (int i = 0; i < ans.length; i++) { + assertEquals(ans[i], test[i]); + } + } + + @Test + public void testMerge() { +// * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 a1 = +// * [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + int[] a1 = new int[]{3, 5, 7,8}; + int[] a2 = new int[]{4,5,6,7}; + + int[] ans = new int[]{3,4,5,6,7,8}; + + int[] test = new ArrayUtil().merge(a1, a2); + + for (int i = 0; i < test.length; i++) { + assertEquals(ans[i], test[i]); + } + + } + + @Test + public void testGrow() { +// 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size +// * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 +// * [2,3,6,0,0,0] + + int size = 3; + int[] testData = new int[]{2,3,6}; + int[] test = new ArrayUtil().grow(testData, size); + assertEquals(testData.length+size, test.length); + + } + + @Test + public void testFibonacci() { + int[] ans = new int[]{1,1,2,3,5,8,13}; + int[] array = new ArrayUtil().fibonacci(15); + for (int i = 0; i < ans.length; i++) { + assertEquals(ans[i], array[i]); + } + + } + + @Test + public void testGetPerfectNumbers() { + + int[] ans=new int[]{1,2,4,7,14}; + int[] array=new ArrayUtil().getPerfectNumbers(28); + for (int i = 0; i < array.length; i++) { + assertEquals(ans[i], array[i]); + } + } + + @Test + public void testJoin() { + //* 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], + //seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + int[] test = new int[]{3,8,9}; + + String test1 = new ArrayUtil().join(test, "-"); + assertEquals("3-8-9", test1); + } + +} diff --git a/group23/729693763/Second_Homework2/src/com/danny/hw2/test/StrutsTest.java b/group23/729693763/Second_Homework2/src/com/danny/hw2/test/StrutsTest.java new file mode 100644 index 0000000000..49b770f015 --- /dev/null +++ b/group23/729693763/Second_Homework2/src/com/danny/hw2/test/StrutsTest.java @@ -0,0 +1,46 @@ +package com.danny.hw2.test; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.danny.hw2.Struts; +import com.danny.hw2.View; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group23/729693763/Second_Homework2/xmlFolder/struts.xml b/group23/729693763/Second_Homework2/xmlFolder/struts.xml new file mode 100644 index 0000000000..8a788e9b1f --- /dev/null +++ b/group23/729693763/Second_Homework2/xmlFolder/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From 8d310a3b37bf07983bdee22e2e9b6c4e02c116ab Mon Sep 17 00:00:00 2001 From: "wangxg922@chinaunincom.cn" <996108220@qq.com> Date: Fri, 17 Mar 2017 09:36:00 +0800 Subject: [PATCH 094/155] =?UTF-8?q?Signed-off-by:=20=E4=BF=AE=E6=94=B9<996?= =?UTF-8?q?108220@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/download/FileDownloader.java | 1 - .../src/com/coderising/download/impl/ConnectionImpl.java | 2 +- .../com/coderising/download/impl/ConnectionManagerImpl.java | 2 +- group11/996108220/src/com/coding/basic/newLinkedList.java | 5 ----- 4 files changed, 2 insertions(+), 8 deletions(-) delete mode 100644 group11/996108220/src/com/coding/basic/newLinkedList.java diff --git a/group11/996108220/src/com/coderising/download/FileDownloader.java b/group11/996108220/src/com/coderising/download/FileDownloader.java index ef23fa872b..9fb1c8086d 100644 --- a/group11/996108220/src/com/coderising/download/FileDownloader.java +++ b/group11/996108220/src/com/coderising/download/FileDownloader.java @@ -9,7 +9,6 @@ import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; import com.coderising.download.api.DownloadListener; -import com.coding.basic.newLinkedList; public class FileDownloader { diff --git a/group11/996108220/src/com/coderising/download/impl/ConnectionImpl.java b/group11/996108220/src/com/coderising/download/impl/ConnectionImpl.java index 9b76a5efd7..425d1b272f 100644 --- a/group11/996108220/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group11/996108220/src/com/coderising/download/impl/ConnectionImpl.java @@ -7,7 +7,7 @@ import java.net.URLConnection; import com.coderising.download.api.Connection; -import com.coding.basic.newLinkedList; + public class ConnectionImpl implements Connection{ URL url; diff --git a/group11/996108220/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group11/996108220/src/com/coderising/download/impl/ConnectionManagerImpl.java index 114c095bf2..768e2cc250 100644 --- a/group11/996108220/src/com/coderising/download/impl/ConnectionManagerImpl.java +++ b/group11/996108220/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -7,7 +7,7 @@ import com.coderising.download.api.Connection; import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; -import com.coding.basic.newLinkedList; + public class ConnectionManagerImpl implements ConnectionManager { diff --git a/group11/996108220/src/com/coding/basic/newLinkedList.java b/group11/996108220/src/com/coding/basic/newLinkedList.java deleted file mode 100644 index e327a7288d..0000000000 --- a/group11/996108220/src/com/coding/basic/newLinkedList.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coding.basic; - -public class newLinkedList { - -} From de1b1c1bb5870abf99a4bf2d5c2791d147783c31 Mon Sep 17 00:00:00 2001 From: lzb Date: Fri, 17 Mar 2017 16:09:05 +0800 Subject: [PATCH 095/155] =?UTF-8?q?litestruts=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lzb/homework0319/litestruts/Struts.java | 80 ++++++++++++++----- .../lzb/homework0319/litestruts/XmlUtil.java | 11 +-- 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java index 96bc0c5036..aea8d1105d 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java @@ -1,6 +1,7 @@ package me.lzb.homework0319.litestruts; import org.apache.commons.lang3.StringUtils; +import org.dom4j.DocumentException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -30,42 +31,83 @@ public class Struts { + private static final String XML = "struts.xml"; - private static final XmlUtil xmlUtil = XmlUtil.create(XML); + + private static final XmlUtil resource = createResource(XML); + + private static XmlUtil createResource(String xml){ + try { + return new XmlUtil(xml); + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + public static View runAction(String actionName, Map parameters) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { - Class loginClasss = Class.forName(xmlUtil.getAuctionPathByName(actionName)); - LoginAction loginAction = (LoginAction) loginClasss.newInstance(); + Object loginAction = getAuctionByName(actionName); + + invokeSetMethods(loginAction, parameters); - for (Map.Entry entry : parameters.entrySet()) { - Method mSetter = loginClasss.getDeclaredMethod("set" + StringUtils.capitalize(entry.getKey()), String.class); - mSetter.invoke(loginAction, entry.getValue()); + String resultName = invokeExecute(loginAction).toString(); + + View view = new View(); + view.setJsp(resource.getResultJsp(actionName, resultName)); + view.setParameters(invokeGetMethods(loginAction)); + + return view; + } + + private static Object getAuctionByName(String auctionName) throws ClassNotFoundException, IllegalAccessException, InstantiationException { + Class c = Class.forName(resource.getAuctionPathByName(auctionName)); + return c.newInstance(); + } + + + private static Object invokeExecute(Object o) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Class c = o.getClass(); + Method mExectue = c.getDeclaredMethod("execute"); + return mExectue.invoke(o); + } + + + private static void invokeSetMethods(Object o, Map parameteMap) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Class c = o.getClass(); + Method[] methods = c.getDeclaredMethods(); + for (int i = 0; i< methods.length; i++) { + String name = methods[i].getName(); + if(StringUtils.startsWith(name, "set")){ + String key = name.replaceAll("^set", "").toLowerCase(); + if(parameteMap.containsKey(key)){ + methods[i].invoke(o, parameteMap.get(key)); + } + } } - Method mExectue = loginClasss.getDeclaredMethod("execute"); - String resultName = (String) mExectue.invoke(loginAction); +// //杩欐牱鍙傛暟绫诲瀷涓嶅浐瀹氱殑璇濅笉濂芥悶 +// for (Map.Entry entry : parameteMap.entrySet()) { +// Method mSetter = c.getDeclaredMethod("set" + StringUtils.capitalize(entry.getKey()), String.class); +// mSetter.invoke(o, entry.getValue()); +// } + } - //鑾峰彇鎵鏈夊睘鎬у拰get鏂规硶 + private static Map invokeGetMethods(Object o) throws InvocationTargetException, IllegalAccessException { Map resultMap = new HashMap(); - Method[] methods = loginClasss.getDeclaredMethods(); + Class c = o.getClass(); + Method[] methods = c.getDeclaredMethods(); for(int i =0 ;i Date: Fri, 17 Mar 2017 16:54:23 +0800 Subject: [PATCH 096/155] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E7=9A=84?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鍒涘缓浜嗕袱涓猵roject 涓涓槸Java project 娴嬭瘯ArrayUtil鐨勶紝 涓や竴涓槸Dynamic web project 鏄祴璇昐truts鐨 --- .../com/coding/datastructs/ArrayUtil.java" | 141 ++++++++++++++++++ .../src/com/coding/test/ArrayUtilTest.java" | 86 +++++++++++ .../WebContent/WEB-INF/etc/struts.xml" | 11 ++ .../coderising/litestruts/LoginAction.java" | 35 +++++ .../com/coderising/litestruts/Struts.java" | 122 +++++++++++++++ .../coderising/litestruts/StrutsTest.java" | 43 ++++++ .../src/com/coderising/litestruts/Test.java" | 94 ++++++++++++ .../src/com/coderising/litestruts/View.java" | 23 +++ 8 files changed, 555 insertions(+) create mode 100644 "group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/DataStructsTest/src/com/coding/datastructs/ArrayUtil.java" create mode 100644 "group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/DataStructsTest/src/com/coding/test/ArrayUtilTest.java" create mode 100644 "group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/WebContent/WEB-INF/etc/struts.xml" create mode 100644 "group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/LoginAction.java" create mode 100644 "group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/Struts.java" create mode 100644 "group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/StrutsTest.java" create mode 100644 "group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/Test.java" create mode 100644 "group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/View.java" diff --git "a/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/DataStructsTest/src/com/coding/datastructs/ArrayUtil.java" "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/DataStructsTest/src/com/coding/datastructs/ArrayUtil.java" new file mode 100644 index 0000000000..ff773dffcc --- /dev/null +++ "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/DataStructsTest/src/com/coding/datastructs/ArrayUtil.java" @@ -0,0 +1,141 @@ +/** + * + */ +package com.coding.datastructs; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + + +public class ArrayUtil { + + + public void reverseArray(int[] origin){ + for (int i=0; i temp = new HashSet(); + for(Integer i : newArray) + temp.add(i); + for (int i = 0; i < temp.size(); i++) { + newArray[i]=(int)temp.toArray()[i]; + } + Arrays.sort(Arrays.copyOfRange(newArray, 0, temp.size())); + return Arrays.copyOfRange(newArray, 0, temp.size()); + } + + public int[] grow(int [] oldArray, int size){ + int[] target = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, target, 0, oldArray.length); + return target; + } + + + public int[] fibonacci(int max){ + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + if(max<=1){ + return new int[0]; + } + for (int i = 2; i < max; i++) { + int a = list.get(i-1)+list.get(i-2); + if(a list = new ArrayList(); + int j=1; + for(int i=2;i<100;i++){ + j=i; + while(j>0){ + if(i%j==0&&i!=j&&j!=1){ + break; + }else{ + j--; + } + } + if(j==0){ + list.add(i); + } + } + int[] newArray = new int[list.size()]; + for(int i=0;i list = new ArrayList(); + for(int i = 1; i < max; i++) + { + int sum = 0; + for(int j = 1; j < i; j++) + { + if(i % j == 0) + { + sum = sum + j; + } + } + if(sum == i) + { + list.add(i); + } + } + int[] newArray = new int[list.size()]; + for(int i=0;i + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git "a/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/LoginAction.java" "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/LoginAction.java" new file mode 100644 index 0000000000..07d16b45d3 --- /dev/null +++ "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/LoginAction.java" @@ -0,0 +1,35 @@ +package com.coderising.litestruts; + + +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git "a/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/Struts.java" "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/Struts.java" new file mode 100644 index 0000000000..bdcafdff8a --- /dev/null +++ "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/Struts.java" @@ -0,0 +1,122 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.input.SAXBuilder; + +public class Struts { + + public static View runAction(String actionName, + Map parameters) { + + + View view = new View(); + Map> array = new HashMap>(); + Map params = new HashMap(); + Map classData = new HashMap(); + try { + analysisXml(classData,array); + Map jspData = array.get(actionName); + String s = "对不起,您传递过来的actionName并没有对应的class类,所以需要重新传"; + if (!classData.containsKey(actionName)) { + throw new ClassNotFoundException(s); + } + + Class class1 = Class.forName(classData.get(actionName)); + LoginAction login = (LoginAction) class1.newInstance(); + for (String ss : parameters.keySet()) { + Method[] methos1 = class1.getMethods(); + for (int i = 0; i < methos1.length; i++) { + if (("set" + ss.substring(0, 1).toUpperCase() + ss + .substring(1)).equals(methos1[i].getName())) { + methos1[i].invoke(login, parameters.get(ss)); + break; + + } + } + } + + Method method1 = class1.getMethod("execute"); + String result = (String) method1.invoke(login); + if(null!=result){ + view.setJsp(jspData.get(result)); + } + Method[] methos2 = class1.getMethods(); + for (int i = 0; i < methos2.length; i++) { + if(methos2[i].getName().substring(0, 3).equals("get")){ + Object value1 = (Object) (methos2[i].invoke(login)); + String name1 = methos2[i].getName(); + params.put(name1.substring(3, 4).toLowerCase()+name1.substring(4), value1); + } + } + view.setParameters(params); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return view; + } + + + public static void analysisXml(Map xmlData,Map> array) { + try { + + String dirpath = System.getProperty("user.dir"); + String xmlFile = dirpath + "/WebContent/WEB-INF/etc/struts.xml"; + SAXBuilder builder = new SAXBuilder(); + Document doc = builder.build(new File(xmlFile)); + Element xRoot = doc.getRootElement(); + List actions = getChildren(xRoot, "action"); + for (int i = 0; i < actions.size(); i++) { + Element e = (Element) actions.get(i); + String actionName1 = getAttributeValue(e, "name"); + String className = getAttributeValue(e, "class"); + xmlData.put(actionName1, className); + List results = getChildren(e, "result"); + Map jspData = new HashMap(); + for (int j = 0; j < results.size(); j++) { + Element result = (Element) results.get(j); + String jspUrl = getValue(result); + String resultName = getAttributeValue(result, "name"); + jspData.put(resultName, jspUrl); + array.put(actionName1, jspData); + } + } + + // /StrutsDemo/WebContent/WEB-INF/etc/struts.xml + } catch (Exception e) { + e.printStackTrace(); + } + } + + + public static Element getChild(Element element, String sonMark) { + return element == null ? null : element.getChild(sonMark); + } + + + public static List getChildren(Element element, String sonMark) { + return element == null ? null : element.getChildren(sonMark); + } + + + public static String getValue(Element element) { + return element == null ? "" : element.getValue(); + } + + + public static String getAttributeValue(Element element, String attribute) { + return element == null ? null : element.getAttributeValue(attribute); + } + +} \ No newline at end of file diff --git "a/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/StrutsTest.java" "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/StrutsTest.java" new file mode 100644 index 0000000000..52f08f3652 --- /dev/null +++ "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/StrutsTest.java" @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //密码和预设的不一致 + + 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")); + } +} \ No newline at end of file diff --git "a/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/Test.java" "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/Test.java" new file mode 100644 index 0000000000..d7bcd36d42 --- /dev/null +++ "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/Test.java" @@ -0,0 +1,94 @@ +/** + * + */ +package com.coderising.litestruts; + +import java.io.BufferedReader; +import java.io.File; +import java.io.StringReader; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.input.SAXBuilder; + +/** + * Title: + * Description: + * @author HuangLiang + * @2017年3月17日上午10:55:39 + * + */ +public class Test { + + /**Description: + * @param args + * @author HuangLiang 2017年3月17日 上午10:55:39 + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + String name1 = "getName"; + System.out.println(name1.substring(3, 4).toLowerCase()+name1.substring(4)); + try { + Class class1 = Class.forName("com.coderising.litestruts.LoginAction"); + Method[] methos1 = class1.getMethods(); + for(int i=0;i para[] = methos1[i].getParameterTypes(); + throw new ClassNotFoundException(); + } + System.out.println("输出"); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + /** + * 获取XML子标签 + * + * @param element + * 标签节点 + * @param sonMark + * 子标签 + * @return + */ + public static Element getChild(Element element, String sonMark) { + return element == null ? null : element.getChild(sonMark); + } + + /** + * 获取XML子标签们 + * + * @param element + * 标签节点 + * @param sonMark + * 子标签 + * @return + */ + public static List getChildren(Element element, String sonMark) { + return element == null ? null : element.getChildren(sonMark); + } + + /** + * s 获取XML标签值 + * + * @param element + * @return + */ + public static String getValue(Element element) { + return element == null ? "" : element.getValue(); + } + /** + * s 获取XML属性值 + * + * @param element + * @return + */ + public static String getAttributeValue(Element element, String attribute) { + return element == null ? null : element.getAttributeValue(attribute); + } + +} diff --git "a/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/View.java" "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/View.java" new file mode 100644 index 0000000000..f68a8c438b --- /dev/null +++ "b/group23/1246614258/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/StrutsDemo/src/com/coderising/litestruts/View.java" @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} \ No newline at end of file From 7566142eaae72a635a8930dfcb0f569ea152d210 Mon Sep 17 00:00:00 2001 From: wenGQ Date: Sat, 18 Mar 2017 16:44:34 +0800 Subject: [PATCH 097/155] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bin/com/github/mrwengq/sec/struts.xml | 11 + .../src/com/github/mrwengq/sec/ArrayUtil.java | 252 ++++++++++++++++++ .../com/github/mrwengq/sec/ArrayUtilTest.java | 93 +++++++ .../com/github/mrwengq/sec/LoginAction.java | 39 +++ .../src/com/github/mrwengq/sec/Struts.java | 134 ++++++++++ .../com/github/mrwengq/sec/StrutsTest.java | 43 +++ .../src/com/github/mrwengq/sec/View.java | 23 ++ .../src/com/github/mrwengq/sec/struts.xml | 11 + 8 files changed, 606 insertions(+) create mode 100644 group22/1193590951/githubitem/bin/com/github/mrwengq/sec/struts.xml create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtil.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtilTest.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/LoginAction.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/Struts.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/StrutsTest.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/View.java create mode 100644 group22/1193590951/githubitem/src/com/github/mrwengq/sec/struts.xml diff --git a/group22/1193590951/githubitem/bin/com/github/mrwengq/sec/struts.xml b/group22/1193590951/githubitem/bin/com/github/mrwengq/sec/struts.xml new file mode 100644 index 0000000000..404aa778e1 --- /dev/null +++ b/group22/1193590951/githubitem/bin/com/github/mrwengq/sec/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtil.java b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtil.java new file mode 100644 index 0000000000..3b037d2cc9 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/ArrayUtil.java @@ -0,0 +1,252 @@ +package com.github.mrwengq.sec; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ //涓轰簡閬垮厤杩囧浣跨敤绌洪棿锛屾病鏈夐噰鐢ㄧ┖闂村鍒剁殑鏂瑰紡杩涜缃崲 + int len = origin.length; + int temp = 0; + for(int i = len ; i>0;i--){ //len - i 鏄粠灏忓埌澶 + if(len - i > (int) (len-1)/2){ + break; + } + temp = origin[len-i]; + origin[len-i] = origin[i-1]; //i-1鏄粠澶у埌灏 + origin[i-1] = temp; + + } + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int removeNum =0; + for(int i = 0;iarray1.length-1){ + newArray[i] = array2[index2++]; + break; + } + if(index2>array2.length-1){ + newArray[i] = array1[index1++]; + break; + } + if(array1[index1]>array2[index2]){ + newArray[i] = array2[index2++]; + }else if(array1[index1]max){ + break; + } + if(len-1>temp.length){ + temp = copyAddArray(temp); + } + temp[len] = next; + len++; + + } + int[] fbn = new int[len]; + System.arraycopy(temp, 0, fbn, 0, len); + return fbn; + } + private int[] copyAddArray(int elementData[]) { //瀵规暟缁勬墿瀹 澧炲姞閲忎负鍘熼暱搴3/4 + int ob[] = new int[elementData.length+(elementData.length * 3) / 4]; + System.arraycopy(elementData ,0, ob, 0,elementData.length); + return ob; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max<3){ + return null; + } + int[] temp = new int[100]; + int index = 0; + int i = 2;//鏈灏忕礌鏁 + while(itemp.length-1){ //鍒ゆ柇绌洪棿鏄惁鍏呰冻锛屽惁鎵╁ + temp = copyAddArray(temp); + } + temp[index++] = i; + i++; + } + int[] prime = new int[index]; + System.arraycopy(temp, 0, prime, 0, index); + return prime; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <2){ + return null; + } + + int[] temp = new int[100]; + int index = 0; + int i = 2; + while(itemp.length-1){ //鍒ゆ柇绌洪棿鏄惁鍏呰冻锛屽惁鎵╁ + temp = copyAddArray(temp); + } + temp[index++] = i; + i++; + } + if(0==index){ + return null; + } + int[] perfectNum = new int[index]; + System.arraycopy(temp, 0, perfectNum, 0, index); + return perfectNum; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder stb = new StringBuilder(); + for(int i = 0; i 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ +public class Struts { + + public static View runAction(String actionName, Map parameters) { + View view = new View(); + //0.璇诲彇xml鏂囦欢 + Document doc = createDOC("struts.xml"); + Class actionClass= queryClass(actionName, doc); + //1.瀹炰緥鍖栧璞¤皟鐢╯et鏂规硶 + Set paramNames = parameters.keySet(); //鍙栧嚭璋冪敤鏂规硶鎵鐢ㄥ弬鏁板悕绉 + Iterator iter = paramNames.iterator(); + Object ob = null; + try { + ob = actionClass.newInstance(); + while(iter.hasNext()){ + String temp = iter.next(); + String methodName = "set"+temp.replaceFirst( temp.substring(0,1),temp.substring(0,1).toUpperCase());//鏂规硶鍚嶇О + String methodParam = parameters.get(temp); //鏂规硶鍙傛暟 + Method me= actionClass.getMethod(methodName,String.class); + me.invoke(ob,methodParam); + } + //2.璋冪敤execute鍙嶆柟娉曞彇鍑鸿繑鍥炲 + String actionMethod = (String)actionClass.getMethod("execute").invoke(ob); + //3.璋冪敤鎵鏈夌殑get锛屽苟淇濆瓨杩斿洖鍊 + Map map = new HashMap(); + Method[] methods = actionClass.getMethods(); + for(Method method : methods){ + String methodName = method.getName(); + if(methodName.substring(0,3).equals("get")){ + Object value = method.invoke(ob); + String key = methodName.substring(3,methodName.length()).toLowerCase(); + map.put(key, value); + } + } + view.setParameters(map);//灏嗘暟鎹繚瀛樺埌view涓 + //4.璁剧疆杩斿洖缁撴灉瑙嗗浘 + String jsp = queryResult(actionName,doc,actionMethod); + view.setJsp(jsp); + } catch (Exception e) { + e.printStackTrace(); + } + + return view; + } + private static String queryResult(String actionName,Document doc,String actionMethod){ + String reView = null; + Element el = queryActionElement(actionName,doc);//鏌ユ壘action鍏冪礌 + List resultEl = el.elements("result"); + for(Element rel : resultEl){ + Attribute att = rel.attribute("name"); + if(att.getValue().equals(actionMethod)){ + reView = rel.getText(); + } + } + return reView; + } + //鏌ユ壘瀵瑰簲鐨刟ction鍏冪礌 + private static Element queryActionElement(String actionName, Document doc){ + Element root = doc.getRootElement(); + Element actionElement = null; + List list = root.elements("action"); + for(Element el : list){ + Attribute att = el.attribute("name"); + if(att.getValue().equals(actionName)){ + actionElement = el; + } + } + return actionElement; + + } + //鏌ユ壘action绫 + private static Class queryClass(String actionName, Document doc) { + Element el = queryActionElement(actionName,doc);//鏌ユ壘action鍏冪礌 + Attribute att = el.attribute("class"); + String className = att.getValue(); + + Class actionClass = null; + try { + actionClass = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return actionClass; + } + //鑾峰彇document瀵硅薄 + private static Document createDOC(String fileName){ + + //浣跨敤dom4j鐨勮鍙杧ml鏂囦欢 + SAXReader sr = new SAXReader(); + URL fileUrl = Struts.class.getResource(fileName); + Document doc = null; + try { + doc = sr.read(fileUrl); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return doc; + } + +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/sec/StrutsTest.java b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/StrutsTest.java new file mode 100644 index 0000000000..2b848ce991 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.mrwengq.sec; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/sec/View.java b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/View.java new file mode 100644 index 0000000000..1c09f0ba33 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/View.java @@ -0,0 +1,23 @@ +package com.github.mrwengq.sec; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/1193590951/githubitem/src/com/github/mrwengq/sec/struts.xml b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/struts.xml new file mode 100644 index 0000000000..404aa778e1 --- /dev/null +++ b/group22/1193590951/githubitem/src/com/github/mrwengq/sec/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + From b70c5bb71c13c8b347d32908c0e856f17d6b39fd Mon Sep 17 00:00:00 2001 From: Fred Qin Date: Sat, 18 Mar 2017 02:16:28 -0700 Subject: [PATCH 098/155] update downloader --- .../395443277/src/com/coderising/download/DownloadThread.java | 1 + .../395443277/src/com/coderising/download/FileDownloader.java | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/group11/395443277/src/com/coderising/download/DownloadThread.java b/group11/395443277/src/com/coderising/download/DownloadThread.java index a02d9dad18..79be3a0c52 100644 --- a/group11/395443277/src/com/coderising/download/DownloadThread.java +++ b/group11/395443277/src/com/coderising/download/DownloadThread.java @@ -36,6 +36,7 @@ public void run() { barrier.await(); } catch (IOException e) { + // TODO: what if download fail, deal with exception, need to inform main thread. e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); diff --git a/group11/395443277/src/com/coderising/download/FileDownloader.java b/group11/395443277/src/com/coderising/download/FileDownloader.java index 3bb464b454..6d221f8c28 100644 --- a/group11/395443277/src/com/coderising/download/FileDownloader.java +++ b/group11/395443277/src/com/coderising/download/FileDownloader.java @@ -1,6 +1,5 @@ package com.coderising.download; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.util.concurrent.CyclicBarrier; @@ -54,8 +53,6 @@ public void execute(String filePath) { // create a file createPlaceHolderFile(filePath, length); - byte[] totalBytes = new byte[length]; - Runnable barrierAction = new Runnable() { @Override public void run() { From f5fad16fcc57dba7348741cc03c95094b06d4d2c Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sat, 18 Mar 2017 20:22:02 +0800 Subject: [PATCH 099/155] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=87=AA=E5=B7=B1?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9A=84=E6=B3=9B=E5=9E=8BArrayList?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/johnChnia/coderising2017/array/ArrayUtil.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java b/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java index bc0e921a85..3126dafc53 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java +++ b/group24/315863321/src/main/java/com/johnChnia/coderising2017/array/ArrayUtil.java @@ -1,8 +1,7 @@ package com.johnChnia.coderising2017.array; import com.johnChnia.coding2017.basic.Queue; - -import java.util.ArrayList; +import com.johnChnia.coding2017.basic.ArrayList; public class ArrayUtil { From 1f89fb224bba0e46a8839c8a0dc9d820c5b964d8 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sat, 18 Mar 2017 20:23:16 +0800 Subject: [PATCH 100/155] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=B3=9B=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../johnChnia/coding2017/basic/ArrayList.java | 38 +++++++++--- .../coding2017/basic/LinkedList.java | 58 +++++++++---------- .../com/johnChnia/coding2017/basic/List.java | 10 ++-- .../com/johnChnia/coding2017/basic/Queue.java | 12 ++-- .../com/johnChnia/coding2017/basic/Stack.java | 15 +++-- .../coding2017/basic/ArrayListTest.java | 20 ++++--- .../coding2017/basic/LinkedListTest.java | 28 ++++----- .../johnChnia/coding2017/basic/QueueTest.java | 12 ++-- .../johnChnia/coding2017/basic/StackTest.java | 21 ++++--- 9 files changed, 117 insertions(+), 97 deletions(-) diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java index 8daec3782f..8c4b8807be 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -4,10 +4,10 @@ /** * Created by john on 2017/3/8. - * @// TODO: 2017/3/15 鏀寔娉涘瀷 + * @// TODO: 2017/4/1 瀹炵幇Iterator 鎺ュ彛 */ -public class ArrayList implements List{ +public class ArrayList implements List { private Object[] elementData; private int size = 0; private static final int DEFAULTCAPACITY = 10; @@ -43,9 +43,9 @@ public ArrayList(int initialCapacity) { * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ - public Object get(int index) { + public E get(int index) { rangeCheck(index); - return elementData[index]; + return (E) elementData[index]; } @@ -54,7 +54,7 @@ public Object get(int index) { * * @param element element to be appended to this list */ - public void add(Object element) { + public void add(E element) { ensureCapacityInternal(size + 1); elementData[size++] = element; } @@ -69,7 +69,7 @@ public void add(Object element) { * @param index index at which the specified element is to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ - public void add(int index, Object element) { + public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); System.arraycopy(elementData, index, elementData, index + 1, @@ -87,7 +87,7 @@ public void add(int index, Object element) { * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ - public Object remove(int index) { + public E remove(int index) { rangeCheckForAdd(index); Object oldValue = elementData[index]; int numMoved = size() - index - 1; @@ -95,8 +95,28 @@ public Object remove(int index) { System.arraycopy(elementData, index + 1, elementData, index, numMoved); } - elementData[--size] = 0; // let jc to clear - return oldValue; + elementData[--size] = null; // let jc to clear + return (E) oldValue; + } + + /** + * Returns the index of the first occurrence of the specified element + * in this list, or -1 if this list does not contain the element. + * More formally, returns the lowest index i such that + * (o==null ? get(i)==null : o.equals(get(i))), + * or -1 if there is no such index. + */ + public int indexOf(Object o) { + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) + return i; + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) + return i; + } + return -1; } /** diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java index 79109cc5c8..a91f03573a 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java @@ -5,12 +5,12 @@ /** * Created by john on 2017/3/9. * - * @// TODO: 2017/3/15 鏀寔娉涘瀷 + * @// TODO: 2017/4/1 鏀寔Iterator */ -public class LinkedList implements List { +public class LinkedList implements List { - private Node first = null; + private Node first = null; private int size = 0; /** @@ -20,10 +20,10 @@ public LinkedList() { } - private static class Node { - Object element; - Node next; - Node prev; + private static class Node { + T element; + Node next; + Node prev; } /** @@ -31,13 +31,13 @@ private static class Node { * * @param element element to be appended to this list */ - public void add(Object element) { - Node newNode = new Node(); + public void add(E element) { + Node newNode = new Node<>(); if (first == null) { addWhenListIsEmpty(newNode, element); return; } - Node last = first; + Node last = first; while (last.next != null) last = last.next; last.next = newNode; @@ -47,7 +47,7 @@ public void add(Object element) { size++; } - private void addWhenListIsEmpty(Node newNode, Object element) { + private void addWhenListIsEmpty(Node newNode, E element) { first = newNode; first.element = element; first.next = null; @@ -60,8 +60,8 @@ private void addWhenListIsEmpty(Node newNode, Object element) { * * @param element the element to add */ - public void addFirst(Object element) { - Node newNode = new Node(); + public void addFirst(E element) { + Node newNode = new Node<>(); if (first == null) { addWhenListIsEmpty(newNode, element); return; @@ -85,7 +85,7 @@ public void addFirst(Object element) { * @param element element to be inserted. * @throws RuntimeException if list size less than 2. */ - public void add(int index, Object element) { + public void add(int index, E element) { if (size() < 2) throw new RuntimeException("list size should greater than or equal to 2"); isElementIndex(index); @@ -93,8 +93,8 @@ public void add(int index, Object element) { addFirst(element); return; } else { - Node temp = new Node(); - Node temp2 = first; + Node temp = new Node<>(); + Node temp2 = first; for (int i = 0; i < index; i++) { temp2 = temp2.next; } @@ -118,11 +118,11 @@ public void add(int index, Object element) { public void remove() { if (size == 0) throw new RuntimeException("linkList size should greater than or equal to 1"); - Node next = first.next; + Node next = first.next; if (next == null) { first = null; } else { - Node last = first; + Node last = first; while (last.next != null) last = last.next; last.prev.next = null; @@ -137,7 +137,7 @@ public void remove() { * @return * @// TODO: 2018/3/14 if i am happy, i will implement it right now! */ - public Object remove(int index) { + public E remove(int index) { return null; } @@ -146,13 +146,13 @@ public Object remove(int index) { * * @return the first element from this list */ - public Object removeFirst() { - Node f = first; + public E removeFirst() { + Node f = first; if (f == null) throw new NoSuchElementException(); - Object element = f.element; - Node next = first.next; - first.element = 0; + E element = f.element; + Node next = first.next; + first.element = null; first.next = null; // help GC first = next; @@ -169,9 +169,9 @@ public Object removeFirst() { * @param index index of the element to return * @return the element at the specified position in this list */ - public Object get(int index) { + public E get(int index) { checkElementIndex(index); - Node node = first; + Node node = first; if (index == 0) { return first.element; } @@ -187,8 +187,8 @@ public Object get(int index) { * @return the first element in this list * @throws NoSuchElementException if this list is empty */ - public Object getFirst() { - final Node f = first; + public E getFirst() { + final Node f = first; if (f == null) throw new NoSuchElementException(); return f.element; @@ -220,7 +220,7 @@ private boolean isElementIndex(int index) { public String toString() { StringBuilder sb = new StringBuilder(); sb.append(first.element); - Node temp = first; + Node temp = first; while (temp.next != null) { temp = temp.next; sb.append("鈫"); diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java index 5616a17e70..6d5a8b01df 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java @@ -3,14 +3,14 @@ /** * Created by john on 2017/3/12. */ -public interface List { - public void add(Object o); +public interface List { + public void add(E o); - public void add(int index, Object o); + public void add(int index, E o); - public Object get(int index); + public E get(int index); - public Object remove(int index); + public E remove(int index); public int size(); } diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java index 2551b7a243..4d8449a8eb 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java @@ -4,17 +4,17 @@ /** * Created by john on 2017/3/10. - * @// TODO: 2017/3/15 鏀寔娉涘瀷 + * @// TODO: 2017/4/1 瀹炵幇Iterator */ -public class Queue { +public class Queue { - private ArrayList arrayList; + private ArrayList arrayList; /** * Constructs an queue using 10 capacity of ArrayList. */ public Queue() { - arrayList = new ArrayList(10); + arrayList = new ArrayList<>(); } @@ -26,7 +26,7 @@ public Queue() { * @param element the element to add * @return {@code true} */ - public boolean add(Object element) { + public boolean add(E element) { arrayList.add(element); return true; } @@ -38,7 +38,7 @@ public boolean add(Object element) { * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ - public Object remove() { + public E remove() { if (arrayList.empty()) throw new NoSuchElementException(emptyMsg()); return arrayList.remove(0); diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java index d6a496217d..f43ea52397 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java @@ -4,16 +4,15 @@ /** * Created by john on 2017/3/10. - * @// TODO: 2017/3/15 鏀寔娉涘瀷 */ -public class Stack { - private LinkedList linkList; +public class Stack { + private LinkedList linkList; /** * Creates an empty Stack. */ public Stack() { - linkList = new LinkedList(); + linkList = new LinkedList<>(); } @@ -22,7 +21,7 @@ public Stack() { * * @param element the element to be pushed onto this stack. */ - public void push(int element) { + public void push(E element) { linkList.addFirst(element); } @@ -33,7 +32,7 @@ public void push(int element) { * @return The object at the top of this stack. * @throws EmptyStackException if this stack is empty. */ - public Object pop() { + public E pop() { if (empty()) { throw new EmptyStackException(); } @@ -47,7 +46,7 @@ public Object pop() { * @return the object at the top of this stack. * @throws EmptyStackException if this stack is empty. */ - public Object peek() { + public E peek() { if (empty()) { throw new EmptyStackException(); } @@ -58,7 +57,7 @@ public Object peek() { * Tests if this stack is empty. * * @return true if and only if this stack contains - * no elements; false otherwise. + * no elements; false otherwise. */ public boolean empty() { return linkList.size() == 0; diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java index a80003b85e..e0df250c37 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java @@ -1,6 +1,5 @@ package com.johnChnia.coding2017.basic; -import com.johnChnia.coding2017.basic.ArrayList; import org.junit.Before; import org.junit.Test; @@ -12,17 +11,18 @@ */ public class ArrayListTest { - private ArrayList arrayList1; - private ArrayList arrayList2; - private ArrayList arrayList3; - private ArrayList arrayList4; + private ArrayList arrayList1; + private ArrayList arrayList2; + private ArrayList arrayList3; + private ArrayList arrayList4; + private ArrayList arrayList5; @Before public void setUp() throws Exception { - arrayList1 = new ArrayList(3); - arrayList2 = new ArrayList(3); - arrayList3 = new ArrayList(3); - arrayList4 = new ArrayList(3); + arrayList1 = new ArrayList<>(3); + arrayList2 = new ArrayList<>(3); + arrayList3 = new ArrayList<>(3); + arrayList4 = new ArrayList<>(3); } @Test @@ -58,4 +58,6 @@ public void testRemoveElementByIndex() { assertThat(removed, equalTo(4)); assertThat(arrayList4.size(), equalTo(5)); } + + } diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java index b92defffd8..d09362cdf9 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java @@ -3,29 +3,29 @@ import org.junit.Before; import org.junit.Test; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.junit.MatcherAssert.assertThat; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; /** * Created by john on 2017/3/9. */ public class LinkedListTest { - private LinkedList linkList1; - private LinkedList linkList2; - private LinkedList linkList3; - private LinkedList linkList4; - private LinkedList linkList5; - private LinkedList linkList6; + private LinkedList linkList1; + private LinkedList linkList2; + private LinkedList linkList3; + private LinkedList linkList4; + private LinkedList linkList5; + private LinkedList linkList6; @Before public void setUp() throws Exception { - linkList1 = new LinkedList(); - linkList2 = new LinkedList(); - linkList3 = new LinkedList(); - linkList4 = new LinkedList(); - linkList5 = new LinkedList(); - linkList6 = new LinkedList(); + linkList1 = new LinkedList<>(); + linkList2 = new LinkedList<>(); + linkList3 = new LinkedList<>(); + linkList4 = new LinkedList<>(); + linkList5 = new LinkedList<>(); + linkList6 = new LinkedList<>(); } @Test diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java index ebe017b2d6..191c6f568d 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java @@ -10,15 +10,15 @@ * Created by john on 2017/3/11. */ public class QueueTest { - Queue queue1; - Queue queue2; - Queue queue3; + Queue queue1; + Queue queue2; + Queue queue3; @Before public void setUp() throws Exception { - queue1 = new Queue(); - queue2 = new Queue(); - queue3 = new Queue(); + queue1 = new Queue<>(); + queue2 = new Queue<>(); + queue3 = new Queue<>(); } diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java index 43a7d858d9..9d84f7367a 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java @@ -1,29 +1,28 @@ package com.johnChnia.coding2017.basic; -import com.johnChnia.coding2017.basic.Stack; import org.junit.Before; import org.junit.Test; import static junit.framework.TestCase.assertFalse; -import static junit.framework.TestCase.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.junit.MatcherAssert.assertThat; +import static org.junit.Assert.assertTrue; /** * Created by john on 2017/3/10. */ public class StackTest { - Stack stack1; - Stack stack2; - Stack stack3; - Stack stack4; + Stack stack1; + Stack stack2; + Stack stack3; + Stack stack4; @Before public void setUp() throws Exception { - stack1 = new Stack(); - stack2 = new Stack(); - stack3 = new Stack(); - stack4 = new Stack(); + stack1 = new Stack<>(); + stack2 = new Stack<>(); + stack3 = new Stack<>(); + stack4 = new Stack<>(); } @Test From 627485a18acb818436748b4cebc6a064f89170b4 Mon Sep 17 00:00:00 2001 From: liangduoduo666666 <798277403@qq.com> Date: Sat, 18 Mar 2017 20:42:37 +0800 Subject: [PATCH 101/155] =?UTF-8?q?zl=E7=9A=84=E7=AC=AC=E4=BA=8C=E5=91=A8?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouliang/week2/litestruts/struts.xml | 11 + .../798277403/src/week2/array/ArrayUtil.java | 235 ++++++++++++++++++ .../src/week2/array/ArrayUtilTest.java | 92 +++++++ .../src/week2/litestruts/LoginAction.java | 41 +++ .../src/week2/litestruts/Struts.java | 155 ++++++++++++ .../src/week2/litestruts/StrutsTest.java | 43 ++++ .../798277403/src/week2/litestruts/View.java | 23 ++ .../798277403/src/week2/litestruts/struts.xml | 11 + 8 files changed, 611 insertions(+) create mode 100644 group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml create mode 100644 group24/798277403/src/week2/array/ArrayUtil.java create mode 100644 group24/798277403/src/week2/array/ArrayUtilTest.java create mode 100644 group24/798277403/src/week2/litestruts/LoginAction.java create mode 100644 group24/798277403/src/week2/litestruts/Struts.java create mode 100644 group24/798277403/src/week2/litestruts/StrutsTest.java create mode 100644 group24/798277403/src/week2/litestruts/View.java create mode 100644 group24/798277403/src/week2/litestruts/struts.xml diff --git a/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml b/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml new file mode 100644 index 0000000000..54550a4174 --- /dev/null +++ b/group24/798277403/out/production/zhouliang/week2/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/798277403/src/week2/array/ArrayUtil.java b/group24/798277403/src/week2/array/ArrayUtil.java new file mode 100644 index 0000000000..04c03f95e0 --- /dev/null +++ b/group24/798277403/src/week2/array/ArrayUtil.java @@ -0,0 +1,235 @@ +package week2.array; + +import java.util.Arrays; + + +class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin 鍘熸暟缁 + */ + void reverseArray(int[] origin){ + if(origin!=null && origin.length>0){ + int i, n; + for(i=0, n=origin.length-1; i0) { + int index = 0; + int length = 0; + int[] temp = new int[oldArray.length]; + for(int i=0; iarray2[j]){ + merges[index] = array2[j]; + j++; + }else{ + merges[index] = array2[j]; + i++; + j++; + split++; + } + } + while(i0) { + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + stringBuffer.append(array[i]); + if (i != array.length - 1) { + stringBuffer.append(seperator); + } + } + return stringBuffer.toString(); + }else{ + return null; + } + } + +} diff --git a/group24/798277403/src/week2/array/ArrayUtilTest.java b/group24/798277403/src/week2/array/ArrayUtilTest.java new file mode 100644 index 0000000000..77c99242fa --- /dev/null +++ b/group24/798277403/src/week2/array/ArrayUtilTest.java @@ -0,0 +1,92 @@ +package week2.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Random; + +/** + * Created by zhouliang on 2017-03-13. + */ +public class ArrayUtilTest { + private int[] array; + private ArrayUtil arrayUtil ; + private int SIZE = 11; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + array = new int[SIZE]; + Random random = new Random(); + + for(int i=0; i 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 +鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + +*/ +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = null; + Class actionClass = null; + LoginAction loginAction = null; + View view = new View(); + try { + db = documentBuilderFactory.newDocumentBuilder(); + Document document = db.parse("src/week2/litestruts/struts.xml"); + NodeList nodeList = document.getElementsByTagName("action"); + + //閬嶅巻姣忎竴涓猘ction鑺傜偣 + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + //鑾峰彇action鑺傜偣鐨勬墍鏈夊睘鎬ч泦鍚 + NamedNodeMap attrs = node.getAttributes(); + //鑾峰彇name缁撶偣鐨勫 + String nodeName = attrs.getNamedItem("name").getNodeValue(); + + if(nodeName.equals(actionName)){ + //鑾峰彇LoginAction瀹炰緥 + actionClass = Class.forName(attrs.getNamedItem("class").getNodeValue()); + loginAction = (LoginAction) actionClass.newInstance(); + + //璁剧疆鐢ㄦ埛鍚嶅瘑鐮佸睘鎬 + Set> entrySet = parameters.entrySet(); + for (Map.Entry entry : entrySet) { + if (entry.getKey().equals("name")) { + loginAction.setName(entry.getValue()); + } + if (entry.getKey().equals("password")) { + loginAction.setPassword(entry.getValue()); + } + } + + //鎵цexecute()鏂规硶 + String result = loginAction.execute(); + + //灏唌essage灏佽鍒皏iew + String message = loginAction.getMessage(); + Map map = new HashMap(); + map.put("message",message); + view.setParameters(map); + + //瑙f瀽瀵瑰簲鐨剅esult鑺傜偣 + NodeList childNodes = node.getChildNodes(); + //閬嶅巻childNodes鑾峰彇姣忎釜鑺傜偣鐨勮妭鐐瑰悕鍜岃妭鐐瑰 + for (int k = 0; k < childNodes.getLength(); k++) { + Node childNode = childNodes.item(k); + //鍖哄垎鍑簍ext绫诲瀷鐨刵ode浠ュ強element绫诲瀷鐨刵ode + if (childNode.getNodeType() == Node.ELEMENT_NODE) { + NamedNodeMap attributes = childNode.getAttributes(); + String nodeValue = attributes.getNamedItem("name").getNodeValue(); + if(nodeValue.equals(result)){ + view.setJsp(childNode.getTextContent()); + } + } + + } + + } + + } + } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + + return view; + } + +/* DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = null; + Class actionClass = null; + LoginAction loginAction = null; + try { + db = documentBuilderFactory.newDocumentBuilder(); + Document document = db.parse("src/week2/litestruts/struts.xml"); + NodeList nodeList = document.getElementsByTagName("action"); + System.out.println("涓鍏辨湁" + nodeList.getLength() + "涓粨鐐"); + //閬嶅巻姣忎竴涓猘ction鑺傜偣 + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); + //鑾峰彇action鑺傜偣鐨勬墍鏈夊睘鎬ч泦鍚 + NamedNodeMap attrs = node.getAttributes(); + //閬嶅巻action鐨勫睘鎬 + for (int j = 0; j < attrs.getLength(); j++) { + //閫氳繃item(index)鏂规硶鑾峰彇book鑺傜偣鐨勬煇涓涓睘鎬 + Node attr = attrs.item(j); + String name = attrs.getNamedItem("name").getNodeValue(); + System.out.println("++++++++++"+name); + //鑾峰彇灞炴у悕 + System.out.print("灞炴у悕锛" + attr.getNodeName()); + //鑾峰彇灞炴у + System.out.println("--灞炴у" + attr.getNodeValue()); + if(attr.getNodeName().equals(actionName)){ + actionClass = Class.forName(attr.getNodeValue()); + loginAction = (LoginAction) actionClass.newInstance(); + } + } + //瑙f瀽book鑺傜偣鐨勫瓙鑺傜偣 + NodeList childNodes = node.getChildNodes(); + //閬嶅巻childNodes鑾峰彇姣忎釜鑺傜偣鐨勮妭鐐瑰悕鍜岃妭鐐瑰 + for (int k = 0; k < childNodes.getLength(); k++) { + //鍖哄垎鍑簍ext绫诲瀷鐨刵ode浠ュ強element绫诲瀷鐨刵ode + if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) { + //鑾峰彇浜唀lement绫诲瀷鑺傜偣鐨勮妭鐐瑰悕 + System.out.print(childNodes.item(k).getNodeName()); + //鑾峰彇浜唀lement绫诲瀷鑺傜偣鐨勮妭鐐瑰 + System.out.println("--鑺傜偣鍊兼槸锛" + childNodes.item(k).getFirstChild().getNodeValue()); + System.out.println("--鑺傜偣鍊兼槸锛" + childNodes.item(k).getTextContent()); + } + } + } + } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + }*/ +} \ No newline at end of file diff --git a/group24/798277403/src/week2/litestruts/StrutsTest.java b/group24/798277403/src/week2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..5c4379d912 --- /dev/null +++ b/group24/798277403/src/week2/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package week2.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group24/798277403/src/week2/litestruts/View.java b/group24/798277403/src/week2/litestruts/View.java new file mode 100644 index 0000000000..01a422a808 --- /dev/null +++ b/group24/798277403/src/week2/litestruts/View.java @@ -0,0 +1,23 @@ +package week2.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/798277403/src/week2/litestruts/struts.xml b/group24/798277403/src/week2/litestruts/struts.xml new file mode 100644 index 0000000000..54550a4174 --- /dev/null +++ b/group24/798277403/src/week2/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + From e3f885bad62f3bfe95f8942db62595ab0573142b Mon Sep 17 00:00:00 2001 From: anxinJ <1028361767@qq.com> Date: Sat, 18 Mar 2017 22:14:07 +0800 Subject: [PATCH 102/155] ArrayUtil ArrayUtil --- .../src/com/coderising/array/ArrayUtil.java | 136 ++++++++++++++++-- .../src/com/coding/basic/ArrayList.java | 1 + 2 files changed, 126 insertions(+), 11 deletions(-) diff --git a/group23/1028361767/Week1DataStructure/src/com/coderising/array/ArrayUtil.java b/group23/1028361767/Week1DataStructure/src/com/coderising/array/ArrayUtil.java index e5ddb476a6..e497a8e251 100644 --- a/group23/1028361767/Week1DataStructure/src/com/coderising/array/ArrayUtil.java +++ b/group23/1028361767/Week1DataStructure/src/com/coderising/array/ArrayUtil.java @@ -1,5 +1,7 @@ package com.coderising.array; +import java.util.Arrays; + public class ArrayUtil { /** @@ -9,8 +11,14 @@ public class ArrayUtil { * @param origin * @return */ - public void reverseArray(int[] origin){ - + public int[] reverseArray(int[] origin){ + int len = origin.length; + int[] ret = new int[len]; + for(int i=0;i len1 && i2 > len2){ + break; + }else if(i1 > len1){ + ret[i++] = array2[i2++]; + }else{ + ret[i++] = array1[i1++]; + } + } + + } + if(sameNum > 0){ + ret = Arrays.copyOf(ret, ret.length - sameNum); + } + return ret; } + /** * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 @@ -46,7 +103,7 @@ public int[] merge(int[] array1, int[] array2){ * @return */ public int[] grow(int [] oldArray, int size){ - return null; + return Arrays.copyOf(oldArray, oldArray.length + size); } /** @@ -57,7 +114,25 @@ public int[] grow(int [] oldArray, int size){ * @return */ public int[] fibonacci(int max){ - return null; + if(max == 1){ + return new int[0]; + }else{ + int[] tmp = new int[max + 1]; + int x1 = 1, x2 = 1; + int i = 1, j = 0; + tmp[j++] = x1; + tmp[j++] = x2; + while(true){ + i = x1 + x2; + if(i > max){ + break; + } + x1 = x2; + x2 = i; + tmp[j++] = i; + } + return Arrays.copyOf(tmp, j); + } } /** @@ -67,7 +142,22 @@ public int[] fibonacci(int max){ * @return */ public int[] getPrimes(int max){ - return null; + int[] tmp = new int[max/2 + 1]; + boolean isPrime; + int k = 0; + for(int i=2;i Date: Sat, 18 Mar 2017 23:19:50 +0800 Subject: [PATCH 103/155] week02 --- .../java/com/coderising/array/ArrayUtil.java | 239 ++++++++++++++++++ .../com/coderising/array/ArrayUtilTest.java | 84 ++++++ .../coderising/litestruts/LoginAction.java | 39 +++ .../com/coderising/litestruts/Struts.java | 154 +++++++++++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + 7 files changed, 593 insertions(+) create mode 100644 group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtil.java create mode 100644 group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtilTest.java create mode 100644 group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/View.java create mode 100644 group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/struts.xml diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtil.java b/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..a9e5e8bde0 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,239 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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(checkArrayIsNull(origin)){ + return; + } + int size = origin.length; + for (int i = 0; i < size/2; i++) { + int swap = origin[i]; + origin[i] = origin[size - 1 - i]; + origin[size - 1 - i] = swap; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + if(checkArrayIsNull(oldArray)){ + return null; + } + int[] swap = new int [oldArray.length]; + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] == 0){ + continue; + } + swap[size] = oldArray[i]; + ++size; + + } + int[] newArray = new int[size]; + System.arraycopy(swap, 0, newArray, 0, size); + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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(checkArrayIsNull(array1) && checkArrayIsNull(array2)){ + return null; + } + int[] swap = new int[array1.length + array2.length]; + int index1 = 0,index2 = 0,size = 0; + while(index1 < array1.length && index2 < array2.length){ + if(array1[index1] == array2[index2]){ + swap[size++] = array1[index1]; + ++index1; + ++index2; + }else if(array1[index1] < array2[index2]){ + if(size > 0 && swap[size-1] == array1[index1]){ + ++index1; + continue; + } + swap[size++] = array1[index1++]; + }else{ + if(size > 0 && swap[size-1] == array2[index2]){ + ++index2; + continue; + } + swap[size++] = array2[index2++]; + } + + } + while(index1 < array1.length){ + swap[size++] = array1[index1]; + ++index1; + } + while(index2 < array2.length){ + swap[size++] = array2[index2]; + ++index2; + } + int[] newArray = new int [size]; + System.arraycopy(swap, 0, newArray, 0, size); + return newArray; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size){ + if(checkArrayIsNull(oldArray)){ + return null; + } + int length = oldArray.length; + int[] newArray = new int [length + size]; + System.arraycopy(oldArray, 0, newArray, 0, length); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 <= 0){ + return null; + } + if(max == 1){ + return new int[0]; + } + int array[] = null; + if(max < 20 ){ + array = new int [max]; + }else{ + array =new int [max/2]; + } + array[0] = array[1] = 1; + int index = 1; + while(array[index-1] + array[index] < max){ + array[index+1] = array[index-1] + array[index]; + ++index; + } + int[] newArray = new int [index+1]; + System.arraycopy(array, 0, newArray, 0, index+1); + return newArray; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 2){ + return null; + } + int init = 2; + int size = 0; + int array[] = new int[max]; + boolean flag = true; + while(init < max){ + flag = true; + for (int i = 2; i < init; i++) { + if(init % i == 0){ + flag = false; + break; + } + } + if(flag){ + array[size++] = init; + } + ++init; + } + int[] newArray = new int [size]; + System.arraycopy(array, 0, newArray, 0, size); + return newArray; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max < 6){ + return null; + } + int array[] = new int[max]; + int init = 6; + int size = 0,sum = 0; + while(init < max){ + sum = 0; + for (int i = 1; i <= init/2; i++) { + if(init % i == 0){ + sum += i; + } + } + if(sum == init){ + array[size++] = init; + } + ++init; + } + int[] newArray = new int [size]; + System.arraycopy(array, 0, newArray, 0, size); + return newArray; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + if(checkArrayIsNull(array) || seperator == null){ + return null; + } + StringBuilder stringBuilder = new StringBuilder(); + int size = array.length; + for (int i = 0; i < size; i++) { + if(i != size - 1){ + stringBuilder.append(array[i]).append(seperator); + continue; + } + stringBuilder.append(array[i]); + } + return stringBuilder.toString(); + } + + private boolean checkArrayIsNull(int[] array){ + if(array == null){ + return true; + } + return false; + } + +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtilTest.java b/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..d2f4d2b668 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,84 @@ +package com.coderising.array; + +import java.util.Arrays; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ArrayUtilTest { + + public static ArrayUtil arrayUtil; + + @BeforeClass + public static void arrayUtil() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Test + public void reverseArrayTest() { + int[] array = {1,2,3,4,5,6,7}; + int[] swapAfter = {7,6,5,4,3,2,1}; + arrayUtil.reverseArray(array); + Assert.assertEquals(Arrays.toString(swapAfter), Arrays.toString(array)); + + } + + @Test + public void removeZeroTest(){ + int[] array = {0,1,0,0,2,3,4,5,6,0,0,7,0}; + int[] array2 = {1,2,3,4,5,6,7}; + int[] removeZero = arrayUtil.removeZero(array); + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(removeZero)); + } + + @Test + public void mergeTest(){ + int[] array1 = {1,2,3,4,5}; + int[] array2 = {4,5,7,8,13,15}; + int[] array3 = {1,2,3,4,5,7,8,13,15}; + int[] merge = arrayUtil.merge(array1, array2); + Assert.assertEquals(Arrays.toString(array3), Arrays.toString(merge)); + } + + @Test + public void growTest(){ + int[] array = {1,2,3,4,5,6,7}; + int[] array2 = {1,2,3,4,5,6,7,0,0,0}; + int[] grow = arrayUtil.grow( array, 3); + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(grow)); + } + + @Test + public void fibonacciTest(){ + int[] fibonacci = arrayUtil.fibonacci(1000); + int[] array2 = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987}; + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(fibonacci)); + } + + @Test + public void getPrimesTest(){ + int[] primes = arrayUtil.getPrimes(20); + int[] array2 = {2, 3, 5, 7, 11, 13, 17, 19}; + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(primes)); + } + + @Test + public void getPerfectNumbersTest(){ + int[] perfectNumbers = arrayUtil.getPerfectNumbers(1000); + int[] array2 = {6, 28, 496}; + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(perfectNumbers)); + } + + @Test + public void joinTest(){ + int[] join = {2,4,5,6}; + String join2 = arrayUtil.join(join, "-"); + Assert.assertEquals("2-4-5-6", join2); + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/LoginAction.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..818f1e9b03 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,154 @@ +package com.coderising.litestruts; + +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view = null; + SAXReader reader = new SAXReader(); + InputStream resourceAsStream = Struts.class.getResourceAsStream("struts.xml"); + try { + Document document = reader.read(resourceAsStream); + Element rootElement = document.getRootElement(); + + Element element = findElement(rootElement, actionName); + if(element == null){ + throw new RuntimeException("鎸囧畾鑺傜偣涓嶅瓨鍦"); + } + //瀛愯妭鐐规暟鎹俊鎭 + Map elementData = getElementData(element); + Class forName = Class.forName(element.attribute("class").getValue()); + Object classInstance = forName.newInstance(); + PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(forName).getPropertyDescriptors(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + String name = propertyDescriptor.getName(); + Method readMethod = propertyDescriptor.getWriteMethod(); + if(parameters != null && parameters.size() > 0 && parameters.containsKey(name) && readMethod != null){ + readMethod.setAccessible(true); + readMethod.invoke(classInstance, parameters.get(name)); + } + } + Method method = forName.getDeclaredMethod("execute"); + method.setAccessible(true); + Object o = method.invoke(classInstance); + Map viewMap = new HashMap(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + String name = propertyDescriptor.getName(); + Method readMethod = propertyDescriptor.getReadMethod(); + if(readMethod != null && !"class".equals(name)){ + viewMap.put(name, readMethod.invoke(classInstance)); + } + } + view = new View(); + view.setParameters(viewMap); + if(elementData != null && elementData.size() > 0 && elementData.containsKey(o)){ + view.setJsp(elementData.get(o)); + }else{ + view.setJsp(null); + } + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IntrospectionException e) { + e.printStackTrace(); + } + return view; + } + + /** + * 鏍规嵁鍚嶇О鑾峰彇鎸囧畾鑺傜偣 + * @param rootElement 璺熻妭鐐 + * @param actionName action鍚嶇О + * @return + */ + private static Element findElement(Element rootElement,String actionName){ + @SuppressWarnings("unchecked") + List elements = rootElement.elements("action"); + for (Element element : elements) { + Attribute name = element.attribute("name"); + if(name == null){ + return null; + } + String value = name.getValue(); + if(value == null || !value.equals(actionName)){ + continue; + } + Attribute actionClass = element.attribute("class"); + if(actionClass == null || actionClass.getValue() == null){ + return null; + } + return element; + } + return null; + } + + /** + * 鑾峰彇action涓嬬殑result鑺傜偣淇℃伅 + * @param element action鑺傜偣 + * @return + */ + private static Map getElementData(Element element){ + Map map = null; + @SuppressWarnings("unchecked") + List elements = element.elements("result"); + for (Element elementSub : elements) { + if(map == null){ + map = new HashMap(); + } + Attribute attribute = elementSub.attribute("name"); + if(attribute!=null){ + map.put(attribute.getValue(), elementSub.getTextTrim()); + } + } + return map; + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/StrutsTest.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/View.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/struts.xml b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..4c6eeabbd4 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From 0d2aeaf1b54dce54bf7e8d4bbcd0a0f4b214b28b Mon Sep 17 00:00:00 2001 From: stillOnTheWay <1335499238@qq.com> Date: Sat, 18 Mar 2017 23:24:07 +0800 Subject: [PATCH 104/155] week02 --- .../src/main/java/com/coderising/litestruts/Struts.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java index 818f1e9b03..d231ca1634 100644 --- a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java @@ -106,7 +106,7 @@ public static View runAction(String actionName, Map parameters) { /** * 鏍规嵁鍚嶇О鑾峰彇鎸囧畾鑺傜偣 - * @param rootElement 璺熻妭鐐 + * @param rootElement 鏍硅妭鐐 * @param actionName action鍚嶇О * @return */ From 19baaf87d65ddcd7f605761f729e8173d18bfae4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 18 Mar 2017 23:51:43 +0800 Subject: [PATCH 105/155] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=EF=BC=9Aarray=E5=8F=8A=E7=AE=80=E6=98=93struts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/array/ArrayUtil.java | 229 ++++++++++++++++++ .../coderising/array/ArrayUtilTest.java | 73 ++++++ .../coderising/litestruts/LoginAction.java | 41 ++++ .../coderising/litestruts/Struts.java | 107 ++++++++ .../coderising/litestruts/StrutsTest.java | 61 +++++ .../coderising/litestruts/View.java | 25 ++ .../coderising/litestruts/struts.xml | 11 + 7 files changed, 547 insertions(+) create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/coderising/array/ArrayUtil.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/coderising/array/ArrayUtilTest.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/LoginAction.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/Struts.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/StrutsTest.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/View.java create mode 100644 group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/array/ArrayUtil.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..d1ddfb6339 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.github.ipk2015.coding2017.coderising.array; + + + +import java.util.ArrayList; +import java.util.Arrays; + +public class ArrayUtil { + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + int temp=0; + for(int i=0;i list= new ArrayList(); + for(int i=0;i=array[i+1]){ + throw new RuntimeException("涓嶆槸鎺掑簭濂界殑鏁扮粍"); + } + } + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray=new int[oldArray.length+size]; + for(int i=0;i list=new ArrayList(); + list.add(1); + list.add(1); + int size=2; + int lastElement=2; + while(lastElement list=new ArrayList(); + list.add(2); + for(int i=3;i list=new ArrayList(); + for(int i=2;i parameters) throws Exception { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + SAXReader reader=new SAXReader(); + Document document = reader.read("src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml"); + Element rootElement = document.getRootElement(); + + Element actionElement=findElement(rootElement,"action","name",actionName); + Class armClass = Class.forName(actionElement.attributeValue("class")); + Object newInstance = armClass.newInstance(); + + setParameters(armClass,newInstance,parameters); + + Method exectueMethod = armClass.getMethod("execute"); + String exectueMethodResult = (String) exectueMethod.invoke(newInstance); + + View view=new View(); + Map viewParameters = getViewParameters(armClass,newInstance); + view.setParameters(viewParameters); + + Element resultElement=findElement(actionElement,"result","name",exectueMethodResult); + view.setJsp(resultElement.getStringValue()); + + return view; + } + + private static Element findElement(Element parent,String elementTag,String attributeName,String actionName){ + Element actionElement = null; + String nameAttributeValue=""; + List elements = parent.elements(elementTag); + for(Element element : elements){ + nameAttributeValue = element.attributeValue(attributeName); + if(nameAttributeValue.equals(actionName)){ + actionElement=element; + break; + } + } + return actionElement; + } + private static void setParameters(Class armClass,Object newInstance,Map parameters) throws Exception{ + Set> entrySet = parameters.entrySet(); + String entryKey=""; + String firstLetter=""; + Method method=null; + for(Entry entry:entrySet){ + entryKey=entry.getKey(); + firstLetter=entryKey.substring(0, 1); + entryKey=entryKey.replace(firstLetter,firstLetter.toUpperCase()); + method = armClass.getMethod("set"+entryKey, String.class); + method.invoke(newInstance, entry.getValue()); + } + } + private static Map getViewParameters(Class armClass,Object newInstance) throws Exception{ + Map map=new HashMap(); + Object getMethodReturn=null; + String methodName=""; + Method[] methods = armClass.getMethods(); + for(int i=0;i3 && methodName.startsWith("get")){ + getMethodReturn=methods[i].invoke(newInstance); + map.put(methodName.substring(3).toLowerCase(), getMethodReturn.toString()); + } + } + return map; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/StrutsTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..3f5f9bc98e --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/StrutsTest.java @@ -0,0 +1,61 @@ +package com.github.ipk2015.coding2017.coderising.litestruts; + + + +import static org.junit.Assert.fail; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = Struts.runAction(actionName,params); + } catch (Exception e) { + // TODO Auto-generated catch block + fail("鍙戠敓寮傚父"); + e.printStackTrace(); + } + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + View view = null; + try { + view = Struts.runAction(actionName,params); + } catch (Exception e) { + // TODO Auto-generated catch block + fail("鍙戠敓寮傚父"); + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/View.java b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..26f7bddb17 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/View.java @@ -0,0 +1,25 @@ +package com.github.ipk2015.coding2017.coderising.litestruts; + + + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0c1df8c87c --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From fe06bc79f52f5474b7bef5974c0001371cc2e68c Mon Sep 17 00:00:00 2001 From: em14Vito Date: Sun, 19 Mar 2017 02:39:02 +0800 Subject: [PATCH 106/155] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=9A=E5=AE=A2?= =?UTF-8?q?=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group23/729693763/readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/group23/729693763/readme.md b/group23/729693763/readme.md index a8a9503559..2b6766f186 100644 --- a/group23/729693763/readme.md +++ b/group23/729693763/readme.md @@ -1,3 +1,5 @@ Data Struct contain: ArrayList, LinkedList, BinaryTreeNode, Stack, Queue, Iterator --- --- Add Blog link: http://www.cnblogs.com/CodeSaveMe/p/6523745.html + +Add week2 Blog link:http://www.cnblogs.com/CodeSaveMe/p/6571621.html From 4dd832cef6647baf94adaa39eec774bde46204b4 Mon Sep 17 00:00:00 2001 From: sjlv <1258890344@qq.com> Date: Sun, 19 Mar 2017 12:18:12 +0800 Subject: [PATCH 107/155] =?UTF-8?q?ArrayUtil=E5=92=8CStruts=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/array/ArrayUtil.java | 209 ++++++++++++++++++ .../com/coderising/array/ArrayUtilTest.java | 88 ++++++++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../src/com/coderising/litestruts/Struts.java | 136 ++++++++++++ .../com/coderising/litestruts/StrutsTest.java | 47 ++++ .../src/com/coderising/litestruts/View.java | 23 ++ .../src/com/coderising/litestruts/struts.xml | 11 + 7 files changed, 553 insertions(+) create mode 100644 group22/1258890344/src/com/coderising/array/ArrayUtil.java create mode 100644 group22/1258890344/src/com/coderising/array/ArrayUtilTest.java create mode 100644 group22/1258890344/src/com/coderising/litestruts/LoginAction.java create mode 100644 group22/1258890344/src/com/coderising/litestruts/Struts.java create mode 100644 group22/1258890344/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group22/1258890344/src/com/coderising/litestruts/View.java create mode 100644 group22/1258890344/src/com/coderising/litestruts/struts.xml diff --git a/group22/1258890344/src/com/coderising/array/ArrayUtil.java b/group22/1258890344/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..773b66ffb7 --- /dev/null +++ b/group22/1258890344/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,209 @@ +package com.coderising.array; + +import java.util.Arrays; +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + int length=origin.length; + int value=0; + for(int i=0;i<(length/2);i++){ + value=origin[i]; + origin[i]=origin[length-1-i]; + origin[length-1-i]=value; + + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int length=oldArray.length; + int[] newArray = new int[length]; + int j=0; + for(int i=0;iarray2[j]){ + for(int k=length1;k>i;k--){ + array3[k]=array3[k-1]; + } + array3[i]=array2[j]; + j++; + if(j1){ + array[0]=1; + array[1]=1; + for(int i=2;i>1;i++){ + array[i]=array[i-1]+array[i-2]; + if(array[i]>=max){ + array[i]=0; + return array; + } + } + } + return array; + + + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + int[] array=new int[max]; + int k=0; + if(max==2){ + array[0]=2; + } + for(int i=3;i parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view=new View(); + try { + SAXReader reader=new SAXReader(); + Document doc=reader.read("./src/com/coderising/litestruts/struts.xml"); + Element root=doc.getRootElement(); + List list=root.elements();//灏嗘墍鏈夊厓绱犳斁鍒伴泦鍚堜腑 + List> listMap=getElements(list); + int size=listmap.size(); + listMap.remove(size-1); + System.out.println(listMap.toString()); + + String pathName=""; + int index=0; + String name=parameters.get("name"); + String password=parameters.get("password"); + + Map parameter=new HashMap<>(); + + for(int i=0;i> listmap = new ArrayList>(); + private static Map map=new LinkedHashMap(); + private static List> getElements(List list){ + + for(Iterator itera1=list.iterator();itera1.hasNext();){ + String key=""; + String value=""; + //鑾峰彇action銆乺esult + Element element=itera1.next(); + if(element.getName()=="action"){ + map = new LinkedHashMap(); + } + if(element.getText().trim()!=null){ + value=element.getText(); + } + List attributes=element.attributes(); + + + //鑾峰彇action銆乺esult閲岀殑灞炴у悕鍜屽 + for(Iterator itera2=attributes.iterator();itera2.hasNext();){ + Attribute attr=itera2.next(); + + if(attr.getName().equals("name")){ + key=attr.getValue(); + } + if(attr.getName().equals("class")){ + value=attr.getValue(); + + } + if(!key.trim().isEmpty()&&!value.trim().isEmpty()){ + map.put(key, value); + } + } + + List subList=element.elements(); + //閫掑綊action鐨勭殑瀛愬厓绱犱滑 + if(subList.size()!=0){ + getElements(subList); + } + } + listmap.add(map); + return listmap; + } +} diff --git a/group22/1258890344/src/com/coderising/litestruts/StrutsTest.java b/group22/1258890344/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..ff10b5dd33 --- /dev/null +++ b/group22/1258890344/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,47 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + System.out.println( view.getJsp()); + System.out.println( view.getParameters().get("message")); + +// 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + View view = Struts.runAction(actionName,params); + +// System.out.println( view.getJsp()); +// System.out.println( view.getParameters().get("message")); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group22/1258890344/src/com/coderising/litestruts/View.java b/group22/1258890344/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group22/1258890344/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/1258890344/src/com/coderising/litestruts/struts.xml b/group22/1258890344/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..171848ecd1 --- /dev/null +++ b/group22/1258890344/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + From a9c2dd5d32cdc940e1805fba0c9ea85a4f286cbc Mon Sep 17 00:00:00 2001 From: 240094626 Date: Sun, 19 Mar 2017 14:03:36 +0800 Subject: [PATCH 108/155] 240094626 work0315 download --- .../coderising/download/DownloadThread.java | 83 +++++++------------ .../coderising/download/FileDownloader.java | 53 +++++++++--- .../download/FileDownloaderTest.java | 5 +- .../download/impl/ConnectionImpl.java | 52 ++++++++---- .../download/impl/ConnectionManagerImpl.java | 16 +--- 5 files changed, 109 insertions(+), 100 deletions(-) diff --git a/group17/240094626/work_0305/src/com/coderising/download/DownloadThread.java b/group17/240094626/work_0305/src/com/coderising/download/DownloadThread.java index a95a4c04f0..7cb544ba0c 100644 --- a/group17/240094626/work_0305/src/com/coderising/download/DownloadThread.java +++ b/group17/240094626/work_0305/src/com/coderising/download/DownloadThread.java @@ -1,76 +1,51 @@ package com.coderising.download; -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; import com.coderising.download.api.Connection; public class DownloadThread extends Thread{ - Connection conn; - int startPos; - int endPos; - int threadId; - private static final int BUFF_LENGTH = 1024; - private final static String TEMP_FILE_PATH = "E:/temp"; - String filePath ; + private Connection conn; + private int startPos; + private int endPos; + private int threadId; + private String localFile; + private static final int BUFF_LENGTH = 1024 * 4; boolean isFinished = false; - long currSize = 0; + int currSize = 0; + CyclicBarrier barrier; - public DownloadThread( Connection conn, int startPos, int endPos,int threadId){ + public DownloadThread( Connection conn, int startPos, int endPos,int threadId,String localFile,CyclicBarrier barrier){ this.conn = conn; this.startPos = startPos; this.endPos = endPos; this.threadId = threadId; - filePath = TEMP_FILE_PATH + "/"+threadId+".tmp"; + this.localFile = localFile; + this.barrier = barrier; } public void run(){ try { - // create temp file with threadId - createTmepFile(); - - // read temp file - writeTempFile(); - + System.out.println("Thread"+threadId+" begin download bytes range:"+startPos+"-"+endPos); + RandomAccessFile raf = new RandomAccessFile(localFile, "rw"); + int totalLen = endPos - startPos + 1; + while(currSize < totalLen){ + int start = currSize + startPos; + int end = start + BUFF_LENGTH-1; + byte[] data = conn.read(start,(end>endPos?endPos:end)); + + raf.seek(start); + raf.write(data); + currSize += data.length; + + + } + raf.close(); + barrier.await(); //绛夊緟鍒殑绾跨▼瀹屾垚 } catch (Exception e) { e.printStackTrace(); } } - private void createTmepFile() { - //鍒ゆ柇璺緞temp鏄惁瀛樺湪锛屼笉瀛樺湪鍒欏厛鍒涘缓 - File tempDir = new File(TEMP_FILE_PATH); - if(!tempDir.exists() || !tempDir.isDirectory()){ - tempDir.mkdir(); - } - - File file = new File(filePath); - if(file.exists()){ - currSize = file.length(); - }else{ - currSize = 0; - } - } - private int writeTempFile() { - int size = 0; - OutputStream fout = null; - try{ - fout = new FileOutputStream(filePath, true); - for(int i = 0; i < sbLogMsg.size(); i++){ - StringBuffer logMsg = sbLogMsg.get(i); - byte[] tmpBytes = CommUtil.StringToBytes(logMsg.toString()); - fout.write(tmpBytes); - size += tmpBytes.length; - } - }catch(Exception e){ - e.printStackTrace(); - }finally{ - if(fout != null){ - fout.close(); - } - } - return size; - - } } diff --git a/group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java b/group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java index ab4e3f08f8..2081831155 100644 --- a/group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java +++ b/group17/240094626/work_0305/src/com/coderising/download/FileDownloader.java @@ -1,32 +1,38 @@ package com.coderising.download; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; import com.coderising.download.api.DownloadListener; public class FileDownloader { - String url; + private String url; + + private DownloadListener listener; - DownloadListener listener; + private ConnectionManager cm; - ConnectionManager cm; + private String localFile; - int threadNum; + private int threadNum; //涓缁勫紑濮嬩笅杞戒綅缃 private int[] startPos; //涓缁勭粨鏉熶笅杞戒綅缃 private int[] endPos; - private boolean stop = false; - - - public FileDownloader(String _url,int threadNum) { + + public FileDownloader(String _url,int threadNum,String localFile) { this.url = _url; this.threadNum = threadNum; + this.localFile = localFile; + startPos = new int[threadNum]; + endPos = new int[threadNum]; } @@ -44,13 +50,24 @@ public void execute(){ // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + + CyclicBarrier barrier = new CyclicBarrier(threadNum, new Runnable() { + @Override + public void run() { + listener.notifyFinished(); + } + }); + Connection conn = null; try { conn = cm.open(this.url); int length = conn.getContentLength(); + if(length > 0){ + holderFile(localFile,length); + for(int i = 0 , len = length/threadNum; i < threadNum; i++){ int size = i * len; startPos[i] = size; @@ -59,14 +76,18 @@ public void execute(){ }else{ endPos[i] = size + len-1; } - Connection downConn = cm.open(url); - new DownloadThread(downConn, startPos[i], endPos[i], i+1).start(); + new DownloadThread(cm.open(url), + startPos[i], + endPos[i], + i+1, + localFile, + barrier).start(); } } - } catch (ConnectionException e) { + } catch (Exception e) { e.printStackTrace(); }finally{ if(conn != null){ @@ -79,6 +100,14 @@ public void execute(){ } + private void holderFile(String localFile, int length) throws IOException { + RandomAccessFile raf = new RandomAccessFile(localFile, "rw"); + for(int i = 0; i < length; i++){ + raf.write(0); + } + raf.close(); + } + public void setListener(DownloadListener listener) { this.listener = listener; } diff --git a/group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java b/group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java index 20358d21ca..57499dd4b7 100644 --- a/group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java +++ b/group17/240094626/work_0305/src/com/coderising/download/FileDownloaderTest.java @@ -21,9 +21,10 @@ public void tearDown() throws Exception { @Test public void testDownload() { - String url = "http://localhost:8080/test.jpg"; + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + String localFile = "E:\\Users\\2017coding\\temp\\pic.png"; int threadNum = Runtime.getRuntime().availableProcessors(); - FileDownloader downloader = new FileDownloader(url,threadNum); + FileDownloader downloader = new FileDownloader(url,threadNum,localFile); ConnectionManager cm = new ConnectionManagerImpl(); diff --git a/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java b/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java index 7dc00e4523..0692bc6dfb 100644 --- a/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java +++ b/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionImpl.java @@ -1,18 +1,26 @@ package com.coderising.download.impl; +import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; -public class ConnectionImpl implements Connection{ +class ConnectionImpl implements Connection{ - private static final int BUFF_LENGTH = 1024; + private static final int BUFF_SIZE = 1024; - private HttpURLConnection conn; + private URL url; - public ConnectionImpl(HttpURLConnection conn) { - this.conn = conn; + ConnectionImpl(String _url) throws ConnectionException{ + try { + this.url = new URL(_url); + } catch (MalformedURLException e) { + throw new ConnectionException(e); + } } @Override @@ -20,25 +28,35 @@ public byte[] read(int startPos, int endPos) throws Exception { if(startPos > endPos){ throw new IllegalArgumentException("startPos:"+startPos+",endPos:"+endPos); } + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); String property = "bytes="+startPos+"-"+endPos; conn.setRequestProperty("RANGE", property); - if(conn.getResponseCode() == 206){ - InputStream is = conn.getInputStream(); - byte[] bytes = new byte[endPos-startPos]; - byte[] buff = new byte[BUFF_LENGTH]; - int len,i=0; - while((len = is.read(buff) ) != 0){ - System.arraycopy(buff, 0, bytes, i*len, len); - i++; - } - return bytes; + conn.connect(); + int totalLen = endPos - startPos + 1; + InputStream is = conn.getInputStream(); + byte[] bytes = new byte[totalLen]; + byte[] buff = new byte[BUFF_SIZE]; + int len, + count = 0; + while((len = is.read(buff) ) > 0 && count < totalLen){ + + System.arraycopy(buff, 0, bytes,count, len); + count += len; } - return null; + return bytes; } @Override public int getContentLength() { - return conn.getContentLength(); + HttpURLConnection conn; + try { + conn = (HttpURLConnection) url.openConnection(); + return conn.getContentLength(); + } catch (IOException e) { + e.printStackTrace(); + } + + return -1; } @Override diff --git a/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionManagerImpl.java index 95b0d1ec28..c7d47979c8 100644 --- a/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionManagerImpl.java +++ b/group17/240094626/work_0305/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -1,8 +1,5 @@ package com.coderising.download.impl; -import java.net.HttpURLConnection; -import java.net.URL; - import com.coderising.download.api.Connection; import com.coderising.download.api.ConnectionException; import com.coderising.download.api.ConnectionManager; @@ -11,18 +8,7 @@ public class ConnectionManagerImpl implements ConnectionManager { @Override public Connection open(String url) throws ConnectionException { - Connection conn = null; - try { - URL u = new URL(url); - HttpURLConnection hConn = (HttpURLConnection) u.openConnection(); - hConn.setConnectTimeout(5000); - hConn.setReadTimeout(5000); - hConn.setRequestMethod("GET"); - conn = new ConnectionImpl(hConn); - } catch (Exception e) { - throw new ConnectionException("杩炴帴鎵撳紑澶辫触:"+url, e); - } - return conn; + return new ConnectionImpl(url); } } From 0888e398d8c9d5724b954a64a9dc3a584024af2a Mon Sep 17 00:00:00 2001 From: CJ-chen <120509419@qq.com> Date: Sun, 19 Mar 2017 14:41:21 +0800 Subject: [PATCH 109/155] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4=EF=BC=8C=E5=8F=AF=E8=83=BD=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E4=B8=80=E4=BA=9B=E8=B7=AF=E5=BE=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E8=BF=87=E9=83=BD=E9=80=9A=E8=BF=87JUnit?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=80=8C=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../basic => 120509419}/ArrayList.java | 0 .../basic => 120509419}/BinaryTreeNode.java | 0 .../basic => 120509419}/Iterator.java | 0 .../120509419/JUnitTest/ArrayUtilTest.java | 164 ++++++++++++ .../JUnitTest/struts/StrutsTest.java | 46 ++++ .../basic => 120509419}/LinkedList.java | 0 .../coding2017/basic => 120509419}/List.java | 0 .../coding2017/basic => 120509419}/Queue.java | 0 .../coding2017/basic => 120509419}/Stack.java | 0 group24/120509419/javaclass/ArrayUtil.java | 237 ++++++++++++++++++ .../javaclass/struts/LoginAction.java | 44 ++++ .../120509419/javaclass/struts/Struts.java | 109 ++++++++ group24/120509419/javaclass/struts/View.java | 32 +++ group24/120509419/javaclass/struts/struts.xml | 11 + 14 files changed, 643 insertions(+) rename group24/{com/github/CJ-chen/coding2017/basic => 120509419}/ArrayList.java (100%) rename group24/{com/github/CJ-chen/coding2017/basic => 120509419}/BinaryTreeNode.java (100%) rename group24/{com/github/CJ-chen/coding2017/basic => 120509419}/Iterator.java (100%) create mode 100644 group24/120509419/JUnitTest/ArrayUtilTest.java create mode 100644 group24/120509419/JUnitTest/struts/StrutsTest.java rename group24/{com/github/CJ-chen/coding2017/basic => 120509419}/LinkedList.java (100%) rename group24/{com/github/CJ-chen/coding2017/basic => 120509419}/List.java (100%) rename group24/{com/github/CJ-chen/coding2017/basic => 120509419}/Queue.java (100%) rename group24/{com/github/CJ-chen/coding2017/basic => 120509419}/Stack.java (100%) create mode 100644 group24/120509419/javaclass/ArrayUtil.java create mode 100644 group24/120509419/javaclass/struts/LoginAction.java create mode 100644 group24/120509419/javaclass/struts/Struts.java create mode 100644 group24/120509419/javaclass/struts/View.java create mode 100644 group24/120509419/javaclass/struts/struts.xml diff --git a/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java b/group24/120509419/ArrayList.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/ArrayList.java rename to group24/120509419/ArrayList.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java b/group24/120509419/BinaryTreeNode.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java rename to group24/120509419/BinaryTreeNode.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/Iterator.java b/group24/120509419/Iterator.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/Iterator.java rename to group24/120509419/Iterator.java diff --git a/group24/120509419/JUnitTest/ArrayUtilTest.java b/group24/120509419/JUnitTest/ArrayUtilTest.java new file mode 100644 index 0000000000..bef91785bc --- /dev/null +++ b/group24/120509419/JUnitTest/ArrayUtilTest.java @@ -0,0 +1,164 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author CJ + */ +public class ArrayUtilTest { + + public ArrayUtilTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of reverseArray method, of class ArrayUtil. + */ + @Test + public void testReverseArray() { + System.out.println("reverseArray"); + int[] origin = new int[]{1,2,3,4,5}; + int[] expecteds = new int[]{5,4,3,2,1}; + ArrayUtil instance = new ArrayUtil(); + instance.reverseArray(origin); + Assert.assertArrayEquals(expecteds, origin); + // TODO review the generated test code and remove the default call to fail. + //fail("The test case is a prototype."); + } + + /** + * Test of removeZero method, of class ArrayUtil. + */ + @Test + public void testRemoveZero() { + System.out.println("removeZero"); + int[] oldArray = new int[]{1,2,3,4,5,6,0,0,0,0}; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{1,2,3,4,5,6}; + int[] result = instance.removeZero(oldArray); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of merge method, of class ArrayUtil. + */ + @Test + public void testMerge() { + System.out.println("merge"); + int[] array1 = new int[]{3, 5, 7,8}; + int[] array2 = new int[]{4, 5, 6,7}; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{3,4,5,6,7,8}; + int[] result = instance.merge(array1, array2); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of grow method, of class ArrayUtil. + */ + @Test + public void testGrow() { + System.out.println("grow"); + int[] oldArray = new int[]{1,2,3,4,5}; + int size = 5; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{1,2,3,4,5,0,0,0,0,0}; + int[] result = instance.grow(oldArray, size); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of fibonacci method, of class ArrayUtil. + */ + @Test + public void testFibonacci() { + System.out.println("fibonacci"); + int max = 15; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{1,1,2,3,5,8,13}; + int[] result = instance.fibonacci(max); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of getPrimes method, of class ArrayUtil. + */ + @Test + public void testGetPrimes() { + System.out.println("getPrimes"); + int max = 23; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{2,3,5,7,11,13,17,19}; + int[] result = instance.getPrimes(max); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of getPerfectNumbers method, of class ArrayUtil. + */ + @Test + public void testGetPerfectNumbers() { + System.out.println("getPerfectNumbers"); + int max = 10; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{6}; + int[] result = instance.getPerfectNumbers(max); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of join method, of class ArrayUtil. + */ + @Test + public void testJoin() { + System.out.println("join"); + int[] array = new int[]{1,2,3,4,5}; + String seperator = ""; + ArrayUtil instance = new ArrayUtil(); + String expResult = "1-2-3-4-5"; + String result = instance.join(array, seperator); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + +} diff --git a/group24/120509419/JUnitTest/struts/StrutsTest.java b/group24/120509419/JUnitTest/struts/StrutsTest.java new file mode 100644 index 0000000000..ebeafa858f --- /dev/null +++ b/group24/120509419/JUnitTest/struts/StrutsTest.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass.struts; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws IOException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + 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() throws IOException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java b/group24/120509419/LinkedList.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/LinkedList.java rename to group24/120509419/LinkedList.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/List.java b/group24/120509419/List.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/List.java rename to group24/120509419/List.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/Queue.java b/group24/120509419/Queue.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/Queue.java rename to group24/120509419/Queue.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/Stack.java b/group24/120509419/Stack.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/Stack.java rename to group24/120509419/Stack.java diff --git a/group24/120509419/javaclass/ArrayUtil.java b/group24/120509419/javaclass/ArrayUtil.java new file mode 100644 index 0000000000..fc460454be --- /dev/null +++ b/group24/120509419/javaclass/ArrayUtil.java @@ -0,0 +1,237 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import java.util.Arrays; + +/** + * + * @author CJ + */ +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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) { + int totalLen = origin.length; + int[] revIntArr = new int[totalLen]; + for (int i = 0; i < totalLen; i++) { + revIntArr[i] = origin[totalLen - 1 - i]; + } + for (int i = 0; i < totalLen; i++) { + origin[i] = revIntArr[i]; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + public int[] removeZero(int[] oldArray) { + int totalLen = oldArray.length; + + int[] tmpArr = new int[totalLen]; + int nonZeroCount = 0; + for (int i : oldArray) { + if (i != 0) { + tmpArr[nonZeroCount++] = i; + } + } + return Arrays.copyOfRange(tmpArr, 0, nonZeroCount); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { + // 涓嶈冭檻 瀵诲潃 + // 涓嶈冭檻鏁堢巼 + int arrLen1 = array1.length; + int arrLen2 = array2.length; + int maxLen = arrLen1+arrLen2; + int[] mergedArr = new int[maxLen]; + int mergedC = 0; + int i = 0; + int j = 0; + while(i 1; + } + for(int i=2;i<=Math.sqrt(n);i++){ + if(n%i == 0) + return false; + } + return true; + } + + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] perfectArr = new int[max]; + // int growSize = 100; + int Count = 0; + for(int i=2;iinNum){ + return false; + } + } + } + return sum == inNum; + } + + + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + int loopMax = array.length-1; + for(int i=0;i parameters) throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { + + /* + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + String strutsFilePath = getProRealPath(Struts.class) + File.separator + "struts.xml"; + System.out.println("Reading File: " + strutsFilePath); + + BufferedReader br = new BufferedReader(new FileReader(strutsFilePath)); + String inline; + String curActionName = null; + String className = null; + HashMap status2action = new HashMap(); + while ((inline = br.readLine()) != null) { + if (inline.contains("(.*?).*$", "$1\t$2").split("\t"); + status2action.put(info[0], info[1]); + } else if (inline.contains("")) { + if (actionName.equals(curActionName)) { + Class c = Class.forName(className); + Object curInstance = c.newInstance(); + + Method setNameMethod = c.getMethod("setName", String.class); + setNameMethod.invoke(curInstance, parameters.get("name")); + Method setPasswordMethod = c.getMethod("setPassword", String.class); + setPasswordMethod.invoke(curInstance, parameters.get("password")); + + Method excuteMethod = c.getMethod("execute"); + // 杩斿洖 sucess 鎴栬 fail + String status =(String) excuteMethod.invoke(curInstance); + + String curJsp = status2action.get(status); + + Method getMessageMethod = c.getMethod("getMessage"); + + View curView = new View(); + curView.setJsp(curJsp); + Map curMap = new HashMap(); + curMap.put("message",(String) getMessageMethod.invoke(curInstance)); + curView.setParameters(curMap); + return curView; + + } + } + } + System.err.println("No Match Aciton"); + return null; + } + + public static String getProRealPath(Class inClass) { + URL url = inClass.getProtectionDomain().getCodeSource().getLocation(); + + String filePath = null; + try { + filePath = URLDecoder.decode(url.getPath(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } + if (filePath.endsWith(".jar")) { + filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1); + } + File file = new File(filePath); + filePath = file.getAbsolutePath(); + return filePath; + } + +} diff --git a/group24/120509419/javaclass/struts/View.java b/group24/120509419/javaclass/struts/View.java new file mode 100644 index 0000000000..700c376eaa --- /dev/null +++ b/group24/120509419/javaclass/struts/View.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass.struts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/120509419/javaclass/struts/struts.xml b/group24/120509419/javaclass/struts/struts.xml new file mode 100644 index 0000000000..a6cdbfc4a4 --- /dev/null +++ b/group24/120509419/javaclass/struts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From e1edf4b35090ecc8408fa0600c5f9f3db2f12b7d Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Sun, 19 Mar 2017 15:32:52 +0800 Subject: [PATCH 110/155] 17457741 --- .../17457741/src/secondwork/ArrayUtil.java | 157 ++++++++++++++++++ .../src/secondwork/ArrayUtilTest.java | 52 ++++++ .../17457741/src/secondwork/LoginAction.java | 38 +++++ group22/17457741/src/secondwork/Struts.java | 69 ++++++++ .../17457741/src/secondwork/StrutsAction.java | 24 +++ .../17457741/src/secondwork/StrutsParser.java | 77 +++++++++ .../17457741/src/secondwork/TestStrust.java | 40 +++++ group22/17457741/src/secondwork/View.java | 26 +++ group22/17457741/src/secondwork/adress2.txt | 1 + group22/17457741/src/secondwork/struts.xml | 11 ++ 10 files changed, 495 insertions(+) create mode 100644 group22/17457741/src/secondwork/ArrayUtil.java create mode 100644 group22/17457741/src/secondwork/ArrayUtilTest.java create mode 100644 group22/17457741/src/secondwork/LoginAction.java create mode 100644 group22/17457741/src/secondwork/Struts.java create mode 100644 group22/17457741/src/secondwork/StrutsAction.java create mode 100644 group22/17457741/src/secondwork/StrutsParser.java create mode 100644 group22/17457741/src/secondwork/TestStrust.java create mode 100644 group22/17457741/src/secondwork/View.java create mode 100644 group22/17457741/src/secondwork/adress2.txt create mode 100644 group22/17457741/src/secondwork/struts.xml diff --git a/group22/17457741/src/secondwork/ArrayUtil.java b/group22/17457741/src/secondwork/ArrayUtil.java new file mode 100644 index 0000000000..29f1bf31c8 --- /dev/null +++ b/group22/17457741/src/secondwork/ArrayUtil.java @@ -0,0 +1,157 @@ +package secondwork; + + +import java.util.Arrays; + +import org.junit.Test; + +public class ArrayUtil { + public void reverseArray (int [] origin){ + int le =origin.length; + int []changedArray=Arrays.copyOf(origin, le); + for (int i=0;i parameters) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, SecurityException { + Map actionMap = StrutsParser.doParse(); + StrutsAction action = actionMap.get(actionName); + + if (action == null) { + System.out.println("couldn't get action: " + actionName + ", return"); + return null; + } + + try { + // 閫氳繃鍙嶅皠, 鍒涘缓瀹炰緥瀵硅薄 + Class actionClass = Class.forName(action.getActionClassName()); + Object actionObj = actionClass.newInstance(); + + // 璋冪敤 parameters 涓殑 set 鏂规硶 + for (Map.Entry parameterEntry : parameters.entrySet()) { + Method[] methods = actionClass.getMethods(); + for (Method method : methods) { + if (method.getName().equalsIgnoreCase("set" + parameterEntry.getKey())) { + method.invoke(actionObj, parameterEntry.getValue()); + } + } + } + + Method executeMethod = actionClass.getMethod("execute"); + Object executeResult = executeMethod.invoke(actionObj); + + // 鏍规嵁 execute 鏂规硶鐨勭粨鏋, 鑾峰彇 xml 閰嶇疆鐨 jsp 椤甸潰 + String jsp = action.getAttributes().get(Objects.toString(executeResult)); + + + Map actionFieldMap = new HashMap<>(); + Field[] actionFields = actionClass.getDeclaredFields(); + for (Field actionFiled : actionFields) { + Method[] methods = actionClass.getMethods(); + for (Method method : methods) { + if (method.getName().equalsIgnoreCase("get" + actionFiled.getName())) { + method.invoke(actionObj); + actionFieldMap.put(actionFiled.getName(), Objects.toString(method.invoke(actionObj))); + } + } + } + + View view = new View(); + view.setParameters(actionFieldMap); + view.setJsp(jsp); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/group22/17457741/src/secondwork/StrutsAction.java b/group22/17457741/src/secondwork/StrutsAction.java new file mode 100644 index 0000000000..fe30f54363 --- /dev/null +++ b/group22/17457741/src/secondwork/StrutsAction.java @@ -0,0 +1,24 @@ +package secondwork; + +import java.util.Map; + +public class StrutsAction { + private String actionClassName; + private Map attributes; + + public String getActionClassName() { + return actionClassName; + } + + public void setActionClassName(String actionClassName) { + this.actionClassName = actionClassName; + } + + public Map getAttributes() { + return attributes; + } + + public void setAttributes(Map attributes) { + this.attributes = attributes; + } +} diff --git a/group22/17457741/src/secondwork/StrutsParser.java b/group22/17457741/src/secondwork/StrutsParser.java new file mode 100644 index 0000000000..c3342872f7 --- /dev/null +++ b/group22/17457741/src/secondwork/StrutsParser.java @@ -0,0 +1,77 @@ +package secondwork; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.Element; + +import com.sun.xml.internal.txw2.Document; + + + +public class StrutsParser { + + private static final String STRUTS_XML = "struts.xml"; + + public static void main(String[] args) { + Map strutsActions = doParse(); + System.out.println(strutsActions.size()); + } + + public static Map doParse() { + Map resultMap = new HashMap<>(); + + SAXReader reader = new SAXReader(); + InputStream in = getStrutsInputStream(); + try { + Document document = reader.read(in); + Element rootElement = document.getRootElement(); + + + List elementActions = rootElement.elements(); + for (Element elementAction : elementActions) { + StrutsAction action = new StrutsAction(); + + + resultMap.put(elementAction.attribute("name").getValue(), action); + + // parse "class" attribute from action element + action.setActionClassName(elementAction.attribute("class").getValue()); + + // parse sub elements in action element + List elements = elementAction.elements(); + Map map = new HashMap<>(); + for (Element element : elements) { + map.put(element.attribute("name").getValue(), element.getStringValue()); + } + action.setAttributes(map); + } + + return resultMap; + } catch (DocumentException e) { + throw new IllegalStateException("failed to parse " + STRUTS_XML, e); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private static InputStream getStrutsInputStream() { + StrutsParser.class.getPackage().getName(); + InputStream in = StrutsParser.class.getClassLoader() + .getResourceAsStream("org/korben/coderising/litestruts/util/" + STRUTS_XML); + if (in == null) { + throw new IllegalStateException(STRUTS_XML + " doesn't exist"); + } + + return in; + } +} diff --git a/group22/17457741/src/secondwork/TestStrust.java b/group22/17457741/src/secondwork/TestStrust.java new file mode 100644 index 0000000000..6cf4e3da8e --- /dev/null +++ b/group22/17457741/src/secondwork/TestStrust.java @@ -0,0 +1,40 @@ +package secondwork; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + +public class TestStrust { + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + 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() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, SecurityException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group22/17457741/src/secondwork/View.java b/group22/17457741/src/secondwork/View.java new file mode 100644 index 0000000000..44e4754edb --- /dev/null +++ b/group22/17457741/src/secondwork/View.java @@ -0,0 +1,26 @@ +package secondwork; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/17457741/src/secondwork/adress2.txt b/group22/17457741/src/secondwork/adress2.txt new file mode 100644 index 0000000000..b728064ab8 --- /dev/null +++ b/group22/17457741/src/secondwork/adress2.txt @@ -0,0 +1 @@ +http://www.cnblogs.com/xxp17457741/p/6568230.html \ No newline at end of file diff --git a/group22/17457741/src/secondwork/struts.xml b/group22/17457741/src/secondwork/struts.xml new file mode 100644 index 0000000000..9c2574460d --- /dev/null +++ b/group22/17457741/src/secondwork/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file From e449df73fdb1f1d412c1a5c72002055f3caccebf Mon Sep 17 00:00:00 2001 From: xxp17457741 <17457741@qq.com> Date: Sun, 19 Mar 2017 15:33:13 +0800 Subject: [PATCH 111/155] 17457741 --- group22/17457741/src/adress2.txt | 1 + group22/17457741/src/secondwork/TestStrust.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 group22/17457741/src/adress2.txt diff --git a/group22/17457741/src/adress2.txt b/group22/17457741/src/adress2.txt new file mode 100644 index 0000000000..b728064ab8 --- /dev/null +++ b/group22/17457741/src/adress2.txt @@ -0,0 +1 @@ +http://www.cnblogs.com/xxp17457741/p/6568230.html \ No newline at end of file diff --git a/group22/17457741/src/secondwork/TestStrust.java b/group22/17457741/src/secondwork/TestStrust.java index 6cf4e3da8e..7629899c2b 100644 --- a/group22/17457741/src/secondwork/TestStrust.java +++ b/group22/17457741/src/secondwork/TestStrust.java @@ -11,7 +11,7 @@ public class TestStrust { @Test - public void testLoginActionSuccess() { + public void testLoginActionSuccess() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, NoSuchMethodException, SecurityException { String actionName = "login"; From d1f9d2acfa44c9667b3dfd5c9394bc5cc48c69fe Mon Sep 17 00:00:00 2001 From: liyiliang <735371210@qq.com> Date: Sun, 19 Mar 2017 17:34:59 +0800 Subject: [PATCH 112/155] week2 --- .../src/task2/week2/array/ArrayUtil.java | 252 ++++++++++++++++++ .../src/task2/week2/struts/LoginAction.java | 39 +++ .../src/task2/week2/struts/Struts.java | 139 ++++++++++ .../src/task2/week2/struts/StrutsTest.java | 44 +++ .../src/task2/week2/struts/View.java | 23 ++ .../src/task2/week2/struts/struts.out.xml | 0 .../src/task2/week2/struts/struts.xml | 11 + 7 files changed, 508 insertions(+) create mode 100644 group22/735371210/src/task2/week2/array/ArrayUtil.java create mode 100644 group22/735371210/src/task2/week2/struts/LoginAction.java create mode 100644 group22/735371210/src/task2/week2/struts/Struts.java create mode 100644 group22/735371210/src/task2/week2/struts/StrutsTest.java create mode 100644 group22/735371210/src/task2/week2/struts/View.java create mode 100644 group22/735371210/src/task2/week2/struts/struts.out.xml create mode 100644 group22/735371210/src/task2/week2/struts/struts.xml diff --git a/group22/735371210/src/task2/week2/array/ArrayUtil.java b/group22/735371210/src/task2/week2/array/ArrayUtil.java new file mode 100644 index 0000000000..22492679fa --- /dev/null +++ b/group22/735371210/src/task2/week2/array/ArrayUtil.java @@ -0,0 +1,252 @@ +package task2.week2.array; + +public class ArrayUtil { + + public static void main(String[] args){ + ArrayUtil t=new ArrayUtil(); + int[] intArray={1,3,4}; + int[] array1={1,4,5,8}; + int[] array2={3,5,9}; + + String str=t.join(intArray,"-"); + + for(int i:t.merge(array1,array2)){ + + System.out.println(i); + } + + + } + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + + int temp=0; + for(int i=0;i2){ + array[0]=1; + array[1]=1; + int i=2; + + while(array[i-1]=max){ + array[i-1]=0; + } + } + + + return array; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + int[] newArray = new int[max]; + if (max > 2) { + + int size = 0, j = 0; + + for (int i = 2; i < max; i++) { + for (j = 2; j < i / 2 + 1; j++) { + + if (i % j == 0) { + + break; + + } + + } + + if (j == i / 2 + 1) { + newArray[size++] = i; + } + + } + + } + + return newArray; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] prefectArray=new int[max]; + int k=0; + + for(int i=1;i parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + String jsp=""; + + Map viewParams=new HashMap(); + + Element ele=getNode(actionName); + + String className=ele.attributeValue("class"); + + Class actionClass=Class.forName(className); + + Object action=actionClass.newInstance(); + + for(Map.Entry entry :parameters.entrySet()){ + + + Method m=actionClass.getMethod("set"+entry.getKey().substring(0,1).toUpperCase()+entry.getKey().substring(1), String.class); + + m.invoke(action, entry.getValue()); + + + } + + Method execute =actionClass.getMethod("execute"); + String result=(String)execute.invoke(action); + + System.out.println(result); + + Method[] methods=actionClass.getDeclaredMethods(); + + for(Method m : methods){ + if(m.getName().startsWith("get")){ + String value =(String) m.invoke(action); + + String key=m.getName().substring(3); + key=key.substring(0,1).toLowerCase()+key.substring(1); + + viewParams.put(key,value); + + + } + } + + for(Element e:(List) ele.elements("result")){ + + if(e.attributeValue("name").equals(result)){ + + jsp=e.getText(); + } + + } + + View view=new View(); + view.setJsp(jsp); + + view.setParameters(viewParams); + + + return view; + } + + + public static Element getNode(String actionName){ + Element ele=null; + + SAXReader reader =new SAXReader(); + + InputStream in =Struts.class.getResourceAsStream("struts.xml"); + + try { + Document doc =reader.read(in); + Element root =doc.getRootElement(); + + List childNode= root.elements(); + + + for(Element e:childNode){ + + if(e.attributeValue("name").equals(actionName)){ + ele=e; + break; + } + + } + + + } catch (DocumentException ex) { + + ex.printStackTrace(); + } + + return ele; + + + + } + + + +} + diff --git a/group22/735371210/src/task2/week2/struts/StrutsTest.java b/group22/735371210/src/task2/week2/struts/StrutsTest.java new file mode 100644 index 0000000000..4c9ad36231 --- /dev/null +++ b/group22/735371210/src/task2/week2/struts/StrutsTest.java @@ -0,0 +1,44 @@ +package task2.week2.struts; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group22/735371210/src/task2/week2/struts/View.java b/group22/735371210/src/task2/week2/struts/View.java new file mode 100644 index 0000000000..9f5851bc8f --- /dev/null +++ b/group22/735371210/src/task2/week2/struts/View.java @@ -0,0 +1,23 @@ +package task2.week2.struts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/735371210/src/task2/week2/struts/struts.out.xml b/group22/735371210/src/task2/week2/struts/struts.out.xml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group22/735371210/src/task2/week2/struts/struts.xml b/group22/735371210/src/task2/week2/struts/struts.xml new file mode 100644 index 0000000000..b47a193398 --- /dev/null +++ b/group22/735371210/src/task2/week2/struts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + From 3de8c0545baf52db921a583c530488367f61786f Mon Sep 17 00:00:00 2001 From: dsfan Date: Sun, 19 Mar 2017 17:54:23 +0800 Subject: [PATCH 113/155] Update .gitignore --- .gitignore | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index a697b49249..19ccd0286f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ + +build/ +.idea/ +.gradle/ *.class # Mobile Tools for Java (J2ME) .mtj.tmp/ @@ -19,8 +23,6 @@ rebel.* .rebel.* target -*.classpath -group22/1193590951/githubitem/.project -*.prefs -group22/1193590951/githubitem/bin/鏂囩珷鍦板潃.txt -!.gitignore \ No newline at end of file +*.DS_Store +liuxin/.DS_Store +liuxin/src/.DS_Store From 54afebf63804325359c724489801860025981d5c Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sun, 19 Mar 2017 19:33:08 +0800 Subject: [PATCH 114/155] =?UTF-8?q?=E5=AE=8C=E6=88=90litstruts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../litestruts/LoginAction.java | 41 +++++++ .../coderising2017/litestruts/Struts.java | 111 ++++++++++++++++++ .../coderising2017/litestruts/View.java | 23 ++++ .../coderising2017/litestruts/StrutsTest.java | 39 ++++++ 4 files changed, 214 insertions(+) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/LoginAction.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/Struts.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/View.java create mode 100644 group24/315863321/src/test/java/com/johnChnia/coderising2017/litestruts/StrutsTest.java diff --git a/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/LoginAction.java b/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..1bab950b95 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +package com.johnChnia.coderising2017.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * + * @author liuxin + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/Struts.java b/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/Struts.java new file mode 100644 index 0000000000..8c92a3a49b --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/Struts.java @@ -0,0 +1,111 @@ +package com.johnChnia.coderising2017.litestruts; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + SAXReader reader = new SAXReader(); + Document document = null; + document = reader.read(new File("target/classes/struts.xml")); + Element node = document.getRootElement(); + List elements = node.elements("action"); + + Class action = null; + Object actionInstance = null; + Method[] methods = null; + View view = null; + for (Element element : + elements) { + if (element.attribute("name").getValue().equals(actionName)) { + action = Class.forName(element.attribute("class").getValue()); + Constructor constructor = action.getConstructor(null); + actionInstance = constructor.newInstance(null); + + + methods = action.getDeclaredMethods(); + Iterator mapIterator = parameters.entrySet().iterator(); + while (mapIterator.hasNext()) { + Map.Entry entry = (Map.Entry) mapIterator.next(); + Object key = entry.getKey(); + Object value = entry.getValue(); + for (Method method : + methods) { + if (method.getName().equalsIgnoreCase("set" + key)) { + method.invoke(actionInstance, value); + break; + } + } + } + + + Method method = action.getMethod("execute", null); + String resultName = (String) method.invoke(actionInstance, null); + + + Map map = new HashMap<>(); + Field[] fields = action.getDeclaredFields(); + for (Field field : + fields) { + String fieldName = field.getName(); + String methodName = "get" + + fieldName.substring(0, 1).toUpperCase() + + fieldName.substring(1); + map.put(fieldName, + (String) action.getMethod(methodName, null) + .invoke(actionInstance, null)); + } + + String jsp = null; + Iterator xmlIterator = element.elementIterator(); + while (xmlIterator.hasNext()) { + Element e = xmlIterator.next(); + if (e.attribute("name").getValue().equals(resultName)) { + jsp = e.getTextTrim(); + } + } + view = new View(); + view.setParameters(map); + view.setJsp(jsp); + } + } + + + return view; + } + + +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/View.java b/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/View.java new file mode 100644 index 0000000000..b6bab92926 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coderising2017/litestruts/View.java @@ -0,0 +1,23 @@ +package com.johnChnia.coderising2017.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/315863321/src/test/java/com/johnChnia/coderising2017/litestruts/StrutsTest.java b/group24/315863321/src/test/java/com/johnChnia/coderising2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..d333db7d10 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coderising2017/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.johnChnia.coderising2017.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = com.johnChnia.coderising2017.litestruts.Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + View view = com.johnChnia.coderising2017.litestruts.Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} From 3b183e2834dcd9a3178bd2151dd357eed4b72b3e Mon Sep 17 00:00:00 2001 From: xiongrui233 Date: Sun, 19 Mar 2017 20:02:34 +0800 Subject: [PATCH 115/155] complete struts --- .../src/com/coderising/array/ArrayUtil.java | 96 +++++++++++++++ .../coderising/litestruts/LoginAction.java | 39 ++++++ .../src/com/coderising/litestruts/Struts.java | 112 ++++++++++++++++++ .../com/coderising/litestruts/StrutsTest.java | 62 ++++++++++ .../src/com/coderising/litestruts/View.java | 23 ++++ .../src/com/coderising/litestruts/struts.xml | 11 ++ .../627559964/src/com/coding/basic/List.java | 1 + 7 files changed, 344 insertions(+) create mode 100644 group22/627559964/src/com/coderising/array/ArrayUtil.java create mode 100644 group22/627559964/src/com/coderising/litestruts/LoginAction.java create mode 100644 group22/627559964/src/com/coderising/litestruts/Struts.java create mode 100644 group22/627559964/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group22/627559964/src/com/coderising/litestruts/View.java create mode 100644 group22/627559964/src/com/coderising/litestruts/struts.xml diff --git a/group22/627559964/src/com/coderising/array/ArrayUtil.java b/group22/627559964/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f4b5e8649b --- /dev/null +++ b/group22/627559964/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓?涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫?艰繘琛岀疆鎹? + 渚嬪锛? 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){ + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓?涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫?煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛? a1鍜宎2 , 鍒涘缓涓?涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + return null; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃?佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸? + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + return null; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈?澶у?糾ax鐨勬墍鏈夌礌鏁版暟缁? + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 鎵?璋撯?滃畬鏁扳?濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓?涓渶澶у?糾ax锛? 杩斿洖涓?涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁? + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁? array缁欒繛鎺ヨ捣鏉? + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲?间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group22/627559964/src/com/coderising/litestruts/LoginAction.java b/group22/627559964/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..eef38d702e --- /dev/null +++ b/group22/627559964/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 杩欐槸涓?涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩?? + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group22/627559964/src/com/coderising/litestruts/Struts.java b/group22/627559964/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..5eef643ff1 --- /dev/null +++ b/group22/627559964/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,112 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛? 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛? + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛? 渚嬪parameters涓殑鏁版嵁鏄? + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛? 骞惰幏寰楄繑鍥炲?硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡? getMessage锛?, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂?煎拰灞炴?у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲?硷紝 纭畾鍝竴涓猨sp锛? + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓?? + + */ + + //鍒涘缓view瀵硅薄 + View view = new View(); + + //璇诲彇xml鏂囦欢鐨凞ocument瀵硅薄 + SAXReader reader = new SAXReader(); + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + + //鑾峰彇鏍硅妭鐐? + Element root = document.getRootElement(); + //鏍硅妭鐐逛笉鏄痵truts鐨?,缁撴潫鏂规硶 + if (!root.getName().equals("struts")) { + return null; + } + //鑾峰彇action鍖归厤actionName鐨勮妭鐐? + List children = root.elements("action"); + Element targetElement = null; + for (Element element : children) { + System.out.println("name:" + element.attributeValue("name")); + System.out.println("class" + element.attributeValue("class")); + if (element.attributeValue("name").equals(actionName)) { + targetElement = element; + } + } + //娌℃湁name鍙傛暟鏃?,缁撴潫鏂规硶 + if (targetElement.attributeCount() <= 0) { + return null; + } + + Class clazz = Class.forName(targetElement.attributeValue("class")); + Object obj = clazz.newInstance(); + Method setName = clazz.getDeclaredMethod("setName", String.class); + Method setPassword = clazz.getDeclaredMethod("setPassword", String.class); + Method execute = clazz.getDeclaredMethod("execute"); + setName.invoke(obj, parameters.get("name")); + setPassword.invoke(obj, parameters.get("password")); + String remsg = (String) execute.invoke(obj); + System.out.println("缁撴灉锛?" + remsg); + + Map parameter = new HashMap(); + Method[] gets = clazz.getDeclaredMethods(); + for (Method method : gets) { + String methodName = method.getName(); + String name = methodName.substring(0,3); + if (name.equals("get")) { + Method getxxx = clazz.getDeclaredMethod(methodName); + String xxx = methodName.substring(3, methodName.length()).toLowerCase(); + String temp = (String) getxxx.invoke(obj); + parameter.put(xxx, temp); + } + } + List targetChilren = targetElement.elements(); + for (Element element : targetChilren) { + String resultName = element.attributeValue("name"); + System.out.println(resultName); + if ("success".equalsIgnoreCase(resultName)) { + view.setJsp(element.getText()); + continue; + } + if ("fail".equalsIgnoreCase(resultName)) { + view.setJsp(element.getText()); + continue; + } + } + view.setParameters(parameter); + + return view; + } + +} diff --git a/group22/627559964/src/com/coderising/litestruts/StrutsTest.java b/group22/627559964/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..37d6840016 --- /dev/null +++ b/group22/627559964/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,62 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = Struts.runAction(actionName,params); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷? + + View view = null; + try { + view = Struts.runAction(actionName,params); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + System.out.println(view.getJsp()); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group22/627559964/src/com/coderising/litestruts/View.java b/group22/627559964/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group22/627559964/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/627559964/src/com/coderising/litestruts/struts.xml b/group22/627559964/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..171848ecd1 --- /dev/null +++ b/group22/627559964/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group22/627559964/src/com/coding/basic/List.java b/group22/627559964/src/com/coding/basic/List.java index d04fcb7df6..6f8deaac0a 100644 --- a/group22/627559964/src/com/coding/basic/List.java +++ b/group22/627559964/src/com/coding/basic/List.java @@ -13,4 +13,5 @@ public interface List { public int size(); public Iterator iterator(); + } \ No newline at end of file From 17f5a61dd4bd4cd8bf948fd36fbede1d8cb3dd94 Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 20:04:51 +0800 Subject: [PATCH 116/155] finish first week and second week homework --- group24/1525619747/.gitignore | 5 + .../homework_20170311/src/Test.java | 29 ++ .../coding2017/basic/ArrayList.java | 92 ++++++ .../coding2017/basic/BinaryTreeNode.java | 32 ++ .../dengliotng/coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedList.java | 124 ++++++++ .../dengliotng/coding2017/basic/List.java | 9 + .../dengliotng/coding2017/basic/Queue.java | 19 ++ .../dengliotng/coding2017/basic/Stack.java | 22 ++ .../src/com/basic/ArrayList.java | 94 ++++++ .../src/com/basic/BinaryTreeNode.java | 45 +++ .../src/com/basic/Iterator.java | 9 + .../src/com/basic/LinkedList.java | 286 +++++++++++++++++ .../homework_20170312/src/com/basic/List.java | 14 + .../src/com/basic/Queue.java | 24 ++ .../src/com/basic/Stack.java | 30 ++ .../src/testcase/TestArrayList.java | 189 +++++++++++ .../src/testcase/TestLinkedList.java | 190 ++++++++++++ .../src/com/basic/ArrayUtil.java | 293 ++++++++++++++++++ .../src/com/struts/DomXmlHelper.java | 105 +++++++ .../src/com/struts/LoginAction.java | 40 +++ .../src/com/struts/Struts.java | 91 ++++++ .../src/com/struts/View.java | 23 ++ .../src/com/struts/struts.xml | 11 + .../src/testcase/StrutsTest.java | 57 ++++ .../src/testcase/TestArrayUtil.java | 147 +++++++++ 26 files changed, 1987 insertions(+) create mode 100644 group24/1525619747/.gitignore create mode 100644 group24/1525619747/homework_20170311/src/Test.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java create mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/ArrayList.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/Iterator.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/LinkedList.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/List.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/Queue.java create mode 100644 group24/1525619747/homework_20170312/src/com/basic/Stack.java create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestArrayList.java create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java create mode 100644 group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/LoginAction.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/Struts.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/View.java create mode 100644 group24/1525619747/homework_20170319/src/com/struts/struts.xml create mode 100644 group24/1525619747/homework_20170319/src/testcase/StrutsTest.java create mode 100644 group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java diff --git a/group24/1525619747/.gitignore b/group24/1525619747/.gitignore new file mode 100644 index 0000000000..5b4edb7bcb --- /dev/null +++ b/group24/1525619747/.gitignore @@ -0,0 +1,5 @@ +*bin/ + +.project +.classpath +.settings diff --git a/group24/1525619747/homework_20170311/src/Test.java b/group24/1525619747/homework_20170311/src/Test.java new file mode 100644 index 0000000000..9e40820653 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/Test.java @@ -0,0 +1,29 @@ +import com.github.dengliotng.coding2017.basic.ArrayList; +import com.github.dengliotng.coding2017.basic.Iterator; + +/** + * Created by LeonDeng on 2017/3/11. + */ +public class Test { + + public static void main(String[] args) { + ArrayList a = new ArrayList(); + for (int i =0; i<10; i++) { + a.add("hello" + i); + } + a.add(20.56); + System.out.println(a.size()); + a.add(5, 20.56); + System.out.println(a.size()); + System.out.println(a.get(5)); +// a.remove(5); +// a.remove(200); + + Iterator i = a.iterator(); + for ( ; i.hasNext(); ) { + String str = i.next().toString(); + System.out.println(str); + } + } + +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..a588d2845e --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.github.dengliotng.coding2017.basic; + +public class ArrayList implements List { + + private int size = 0; + private static final int maxLength = 100; + + private Object[] elementData = new Object[maxLength]; + + public void add(Object o){ + if (size > maxLength) { + return; + } + elementData[size++] = o; + } + + public void add(int index, Object o){ + if (size > maxLength) { + return; + } + if (elementData[index] == null) { + elementData[index] = o; + ++size; + return; + } else { + int emptyIndex = index + 1; + boolean hasEmpty = false; + for (int i = index; i < maxLength; i++) { + if (elementData[i] == null) { + hasEmpty = true; + emptyIndex = i; + } + } + if (!hasEmpty) { + return; + } + //shift + for (int i = emptyIndex; i > index; --i) { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + ++size; + } + } + + public Object get(int index){ + if (index > maxLength || index < 0) { + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if (index > maxLength || index < 0) { + return null; + } + Object o = elementData[index]; + for (int i = index; i < size - 1; ++i) { + elementData[i] = elementData[i+1]; + } + --size; + return o; + } + + public int size(){ + return size; + } + + public Iterator iterator() { + return new Iterator() { + private int index=0; + @Override + public boolean hasNext() { + if (index < size) { + return true; + } + return false; + } + + @Override + public Object next() { + if (hasNext()) { + return elementData[index++]; + } + return null; + } + }; + } + + + +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..fd3e827346 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.dengliotng.coding2017.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..b40a21f6c4 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.dengliotng.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..e8f8244d46 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java @@ -0,0 +1,124 @@ +package com.github.dengliotng.coding2017.basic; + + + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java new file mode 100644 index 0000000000..fe5f523eea --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.dengliotng.coding2017.basic; + +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(); +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java new file mode 100644 index 0000000000..556e3b4293 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java @@ -0,0 +1,19 @@ +package com.github.dengliotng.coding2017.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java new file mode 100644 index 0000000000..ae0a194504 --- /dev/null +++ b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java @@ -0,0 +1,22 @@ +package com.github.dengliotng.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java new file mode 100644 index 0000000000..6c02e8d4f7 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java @@ -0,0 +1,94 @@ +package com.basic; + +public class ArrayList implements List +{ + + private int size = 0; + + private final int MAXNSIZE = 100; + + private Object[] elementData = new Object[MAXNSIZE]; + + public void add(Object o) + { + if (size > MAXNSIZE) + { + String errorInfo = "Out of max size" + MAXNSIZE; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + elementData[size++] = o; + } + + public void add(int index, Object o) + { + if (index > size) + { + String errorInfo = "Index to add: " + index + + " is out of current size: " + size; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + for (int i = size + 1; i > index + 1; i--) + { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + ++size; + } + + public Object get(int index) + { + if (index < 0 || index >= size) + { + String errorInfo = "Index to get: " + index + + " is invalid, current range: 0 - " + (size - 1); + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + return elementData[index]; + } + + public Object remove(int index) + { + if (index < 0 || index >= size) + { + String errorInfo = "Index to remove: " + index + + " is invalid, current range: 0 - " + (size - 1); + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + for (int i = index; i < size - 1; i++) + { + elementData[i] = elementData[i + 1]; + } + Object o = elementData[size]; + elementData[size--] = null; + return o; + } + + public int size() + { + return size; + } + + public Iterator iterator() + { + return new Iterator() + { + + private int index = 0; + + public boolean hasNext() + { + return (index < size); + } + + public Object next() + { + if (hasNext()) + { + return elementData[index++]; + } + return null; + } + }; + } + +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..9cf6755972 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java @@ -0,0 +1,45 @@ +package com.basic; + +public class BinaryTreeNode +{ + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() + { + return data; + } + + public void setData(Object data) + { + this.data = data; + } + + public BinaryTreeNode getLeft() + { + return left; + } + + public void setLeft(BinaryTreeNode left) + { + this.left = left; + } + + public BinaryTreeNode getRight() + { + return right; + } + + public void setRight(BinaryTreeNode right) + { + this.right = right; + } + + public BinaryTreeNode insert(Object o) + { + return null; + } + +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/Iterator.java b/group24/1525619747/homework_20170312/src/com/basic/Iterator.java new file mode 100644 index 0000000000..402b5346a5 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.basic; + +public interface Iterator +{ + public boolean hasNext(); + + public Object next(); + +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java new file mode 100644 index 0000000000..1e692521e2 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java @@ -0,0 +1,286 @@ +package com.basic; + +public class LinkedList implements List +{ + + private Node head; + + public void add(Object o) + { + if (head == null) { + head = new Node(o, null); + } else { + Node nodePointer = head; + while (nodePointer.next != null) { + nodePointer = nodePointer.next; + } + nodePointer.next = new Node(o, null); + } + + } + + public void add(int index, Object o) + { + int size = size(); + if (index < 0 || index > size) { + String errorInfo = "Invalid index to add:" + index+ " out of range: [0," + size + "]"; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + int step = 0; + Node nodePointer = head; + while (step < index) { + nodePointer = nodePointer.next; + ++step; + } + nodePointer.next = new Node(o, nodePointer.next); + } + + public Object get(int index) + { + int size = size(); + if (index < 0 || index > size) { + String errorInfo = "Invalid index to get:" + index+ " out of range: [0," + size + "]"; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + + int step = 0; + Node nodePointer = head; + while (step < index) { + nodePointer = nodePointer.next; + ++step; + } + + return nodePointer.data; + } + + public Object remove(int index) + { + int size = size(); + if (index < 0 || index > size) { + String errorInfo = "Invalid index to remove:" + index+ " out of range: [0," + size + "]"; + throw new ArrayIndexOutOfBoundsException(errorInfo); + } + + int step = 0; + Node nodePointer = head; + Node lastPointer = head; + + while (step < index) { + lastPointer = nodePointer; + nodePointer = nodePointer.next; + ++step; + } + + Object o = null; + + if (lastPointer == nodePointer) { + Node toDelete = head; + o = toDelete.data; + head = head.next; + toDelete = null; + } else { + o = nodePointer.data; + lastPointer.next = nodePointer.next; + nodePointer = null; + } + + return o; + } + + public int size() + { + int size = 0; + if (head != null) { + ++size; + Node nodePointer = head; + while (nodePointer.next != null) { + ++size; + nodePointer = nodePointer.next; + } + } + return size; + } + + public void addFirst(Object o) + { + if (head == null) { + head = new Node(o, null); + return; + } + head = new Node(o, head); + } + + public void addLast(Object o) + { + if (head == null) { + head = new Node(o, null); + return; + } + + Node nodePointer = head; + while (nodePointer.next != null) { + nodePointer = nodePointer.next; + } + nodePointer.next = new Node(o, null); + } + + public Object removeFirst() + { + if (head == null) { + return null; + } + + Node toDelete = head; + Object o = head.data; + head = head.next; + toDelete = null; + + return o; + } + + public Object removeLast() + { + if (head == null) { + return null; + } + + Node nodePointer = head; + Node lastPointer = head; + + while (nodePointer.next != null) { + lastPointer = nodePointer; + nodePointer = nodePointer.next; + } + lastPointer.next = null; + Object o = nodePointer.data; + nodePointer = null; + + return o; + } + + public Iterator iterator() + { + return new Iterator() { + + private Node nodePointer = head; + + public boolean hasNext() + { + // TODO Auto-generated method stub + return (nodePointer != null); + } + + public Object next() + { + // TODO Auto-generated method stub + if (hasNext()) { + Object o = nodePointer.data; + nodePointer = nodePointer.next; + return o; + } + return null; + } + }; + } + + private static class Node + { + Object data; + Node next; + public Node(Object o, Node n) { + this.data = o; + this.next = n; + } + } + + /** + * 鎶婅閾捐〃閫嗙疆 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse() + { + if (head == null) { + return; + } + + Node reverse = null; + while (size() > 0) { + reverse = new Node(removeFirst(), reverse); + } + + head = reverse; + } + + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 濡傛灉list = 2->5->7->8->10 + * ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + */ + public void removeFirstHalf() + { + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * + * @param i + * @param length + */ + public void remove(int i, int length) + { + + } + + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 渚嬪褰撳墠閾捐〃 = + * 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * + * @param list + */ + public static int[] getElements(LinkedList list) + { + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * + * @param list + */ + + public void subtract(LinkedList list) + { + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues() + { + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * + * @param min + * @param max + */ + public void removeRange(int min, int max) + { + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * + * @param list + */ + public LinkedList intersection(LinkedList list) + { + return null; + } +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/List.java b/group24/1525619747/homework_20170312/src/com/basic/List.java new file mode 100644 index 0000000000..8892e795e9 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/List.java @@ -0,0 +1,14 @@ +package com.basic; + +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(); +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/Queue.java b/group24/1525619747/homework_20170312/src/com/basic/Queue.java new file mode 100644 index 0000000000..786b95a3a4 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/Queue.java @@ -0,0 +1,24 @@ +package com.basic; + +public class Queue +{ + + public void enQueue(Object o) + { + } + + public Object deQueue() + { + return null; + } + + public boolean isEmpty() + { + return false; + } + + public int size() + { + return -1; + } +} diff --git a/group24/1525619747/homework_20170312/src/com/basic/Stack.java b/group24/1525619747/homework_20170312/src/com/basic/Stack.java new file mode 100644 index 0000000000..0226551dcc --- /dev/null +++ b/group24/1525619747/homework_20170312/src/com/basic/Stack.java @@ -0,0 +1,30 @@ +package com.basic; + +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + public void push(Object o) + { + } + + public Object pop() + { + return null; + } + + public Object peek() + { + return null; + } + + public boolean isEmpty() + { + return false; + } + + public int size() + { + return -1; + } +} diff --git a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java new file mode 100644 index 0000000000..471243a83a --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java @@ -0,0 +1,189 @@ +package testcase; + +import static org.junit.Assert.*; + +import com.basic.ArrayList; +import com.basic.Iterator; + +import org.junit.Test; + +public class TestArrayList +{ + + @Test + public void testAdd() + { + ArrayList list = new ArrayList(); + try + { + list.add(3); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + try + { + for (int i = 0; i < 100; i++) + { + list.add(i); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + assertEquals("100", e.getMessage()); + } + + } + + /* + * test add(index, o) + */ + @Test + public void testIndexAdd() + { + ArrayList list = new ArrayList(); + try + { + for (int i = 0; i < 10; i++) + { + list.add(i); + } + list.add(3, 20); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + assertFalse(list.get(3).equals(3)); + assertTrue(list.get(3).equals(20)); + assertTrue(list.get(4).equals(4)); + + try + { + for (int i = 0; i < 100; i++) + { + list.add(i, i); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + assertEquals("100", e.getMessage()); + } + } + + /* + * test get(index) + */ + @Test + public void testGet() + { + ArrayList list = new ArrayList(); + try + { + for (int i = 0; i < 10; i++) + { + list.add(i); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + try + { + list.get(0); + list.get(5); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + try + { + list.get(10); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + System.out.println(list.size()); + String errorInfo = "Index to get: 10 is invalid, current range: 0 - " + + (list.size() - 1); + assertEquals(errorInfo, e.getMessage()); + } + } + + /* + * test remove(index) and size + */ + @Test + public void testRemove() + { + ArrayList list = new ArrayList(); + try + { + for (int i = 0; i < 10; i++) + { + list.add(i); + } + assertTrue(list.size() == 10); + list.remove(3); + assertTrue(list.size() == 9); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + assertFalse(list.get(3).equals(3)); + assertTrue(list.get(3).equals(4)); + + try + { + list.remove(-3); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + } + + try + { + list.remove(20); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNotNull(e); + System.out.println(e.getMessage()); + } + } + + @Test + public void testInterator() + { + ArrayList list = new ArrayList(); + try + { + for (int i = 0; i < 10; i++) + { + list.add(i); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + + Iterator it = list.iterator(); + assertTrue(it.hasNext()); + assertTrue(it.next().equals(0)); + assertTrue(it.next().equals(1)); + assertTrue(it.next().equals(2)); + } + +} diff --git a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java new file mode 100644 index 0000000000..1f855e873c --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java @@ -0,0 +1,190 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.basic.Iterator; +import com.basic.LinkedList; + +public class TestLinkedList +{ + + /* + * test add() and size() + * */ + @Test + public void testAdd() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + assertTrue(linkedList.size() == 2); + } + + + @Test + public void testGet() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + assertTrue(linkedList.get(0).equals(3)); + assertFalse(linkedList.get(0).equals("hello world")); + assertTrue(linkedList.get(1).equals("hello world")); + + try { + linkedList.get(-1); + } catch (ArrayIndexOutOfBoundsException e) { + assertNotNull(e); + String errorInfo = "Invalid index to get:" + -1 + " out of range: [0," + linkedList.size() + "]"; + assertTrue(e.getMessage().equals(errorInfo)); +// System.out.println(e.getMessage()); + } + } + + + @Test + public void testRemove() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + linkedList.add(4); + linkedList.add("Leon Deng"); + + try { + linkedList.remove(-1); + } catch (ArrayIndexOutOfBoundsException e) { + assertNotNull(e); + String errorInfo = "Invalid index to remove:" + -1 + " out of range: [0," + linkedList.size() + "]"; + assertTrue(e.getMessage().equals(errorInfo)); + } + + try { + linkedList.remove(10); + } catch (ArrayIndexOutOfBoundsException e) { + assertNotNull(e); + String errorInfo = "Invalid index to remove:" + 10 + " out of range: [0," + linkedList.size() + "]"; + assertTrue(e.getMessage().equals(errorInfo)); + } + + Object o = null; + try { + o = linkedList.remove(0); + } catch (ArrayIndexOutOfBoundsException e) { + assertNull(e); + } + assertTrue(o.equals(3)); + + try { + o = linkedList.remove(2); + } catch (ArrayIndexOutOfBoundsException e) { + assertNull(e); + } +// System.out.println(o); + assertTrue(o.equals("Leon Deng")); + } + + + @Test + public void testAddFirst() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + + linkedList.addFirst("Leon Deng"); + assertTrue(linkedList.get(0).equals("Leon Deng")); + assertTrue(linkedList.get(1).equals(3)); + } + + @Test + public void testAddLast() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + + linkedList.addLast("Leon Deng"); + assertTrue(linkedList.get(linkedList.size() - 1).equals("Leon Deng")); + } + + @Test + public void testRemoveFirst() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + + Object o = linkedList.removeFirst(); + assertTrue(o.equals(3)); + o = linkedList.removeFirst(); + assertTrue(o.equals("hello world")); + } + + @Test + public void testRemoveLast() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("hello world"); + linkedList.add("Leon Deng"); + + Object o = linkedList.removeLast(); + assertTrue(o.equals("Leon Deng")); + o = linkedList.removeLast(); + assertTrue(o.equals("hello world")); + } + + @Test + public void testInterator() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add("Leon Deng"); + + Iterator it = linkedList.iterator(); + assertTrue(it.hasNext()); + assertTrue(it.next().equals(3)); + assertTrue(it.hasNext()); + assertTrue(it.next().equals("Leon Deng")); + assertFalse(it.hasNext()); + } + + @Test + public void testReverse() { + LinkedList linkedList = new LinkedList(); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); +// +// linkedList.reverse(); +// +// it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + Object o1 = linkedList.get(0); + Object o2 = linkedList.get(linkedList.size() - 1); + + linkedList.reverse(); + + Object o3 = linkedList.get(0); + Object o4 = linkedList.get(linkedList.size() - 1); + + assertEquals(o1, o4); + assertEquals(o2, o3); + + linkedList.reverse(); + Object o5 = linkedList.get(0); + Object o6 = linkedList.get(linkedList.size() - 1); + + assertEquals(o1, o5); + assertEquals(o2, o6); + } + + + +} diff --git a/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java new file mode 100644 index 0000000000..86ba46d8cd --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java @@ -0,0 +1,293 @@ +package com.basic; + +import java.util.Collections; + +public class ArrayUtil +{ + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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) + { + int length = origin.length; + int left = 0; + int right = length - 1; + int a = 0; + int b = 0; + while (left < right) + { + swap(origin, left++, right--); + } + } + + private void swap(int[] origin, int i, int j) + { + // TODO Auto-generated method stub + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) + { + int length = oldArray.length; + int[] newArray = new int[length]; + int[] zeroArray = new int[length]; + + int zIndex = 0; + int nzIndex = 0; + for (int i = 0; i < length; i++) + { + if (oldArray[i] == 0) + { + zeroArray[zIndex++] = oldArray[i]; + } + else + { + newArray[nzIndex++] = oldArray[i]; + } + } + + int[] newArray2 = new int[nzIndex]; + for (int i = 0; i < nzIndex; i++) + { + newArray2[i] = newArray[i]; + } + + return newArray2; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) + { + int length1 = array1.length; + int length2 = array2.length; + int[] array3 = new int[length1 + length2]; + + int index1 = 0; + int index2 = 0; + int index3 = 0; + + while (index1 < length1 && index2 < length2) + { + + + if (index3 > 0) + { + if (array3[index3 - 1] == array1[index1]) + { + ++index1; + continue; + } + if (array3[index3 - 1] == array2[index2]) + { + ++index2; + continue; + } + } + + if (array1[index1] == array2[index2]) + { + array3[index3++] = array1[index1]; + ++index1; + ++index2; + continue; + } + if (array1[index1] < array2[index2]) + { + array3[index3++] = array1[index1]; + ++index1; + continue; + } + if (array1[index1] > array2[index2]) + { + array3[index3++] = array2[index2]; + ++index2; + continue; + } + } + + while (index1 < length1) { + array3[index3++] = array1[index1++]; + } + while (index2 < length2) { + array3[index3++] = array1[index2++]; + } + + int[] newArray = new int[index3]; + for (int i = 0; i < index3; i++) + { + newArray[i] = array3[i]; + } + + return newArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) + { + int length = oldArray.length; + int[] newArr = new int[length + size]; + for (int i = 0; i < length; i++) { + newArr[i] = oldArray[i]; + } + for (int i = length; i < length + size; i++) { + newArr[i] = 0; + } + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 null; + } + int[] arr = new int[max / 2]; + int a = 1; + int b = 1; + int c = 0; + + arr[0] = 1; + arr[1] = 1; + + int index = 2; + while (a + b < max) { + arr[index++] = a + b; + c = b; + b = a + b; + a = c; + } + + int[] newArr = new int[index]; + for (int i = 0; i < index; i++) { + newArr[i] = arr[i]; + } + + return newArr; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) + { + int size = max / 2 + 1; + int[] arr = new int[size]; + int index = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + arr[index++] = i; + } + } + + int[] newArr = new int[index]; + for (int i = 0; i < index; i++) { + newArr[i] = arr[i]; + } + + return newArr; + } + + public boolean isPrime(int i) { + for (int j = 2; j < i / 2; j++) { + if (i % j == 0) { + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) + { + int size = max / 2 + 1; + int[] arr = new int[size]; + int index = 0; + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { + arr[index++] = i; + } + } + + int[] newArr = new int[index]; + for (int i = 0; i < index; i++) { + newArr[i] = arr[i]; + } + + return newArr; + } + + public boolean isPerfectNumber(int num) { + int sum = 0; + for (int i = 1; i <= num / 2; i++) { + if (num % i == 0) { + sum += i; + } + } +// System.out.println("num: " + num + ", sum:" + sum); + return (num == sum); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) + { + String str = ""; + int length = array.length; + for (int a : array) { + str += a + seperator; + } + str = str.substring(0, str.length()-1); + return str; + } + +} diff --git a/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java new file mode 100644 index 0000000000..507a974297 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java @@ -0,0 +1,105 @@ +package com.struts; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * DOM鏂瑰紡瑙f瀽xml + */ +public class DomXmlHelper { + + private Map kv = new HashMap(); + + public DomXmlHelper() throws DocumentException { + fetchAttributes(); + } + + //閬嶅巻褰撳墠鑺傜偣涓嬬殑鎵鏈夎妭鐐 + private void listNodes(Element node, String loop, Map kv){ + +// System.out.println("褰撳墠鑺傜偣鐨勫悕绉帮細" + node.getName()); + if (loop.equals("")) { + loop += node.getName(); + } else { + kv.put(loop, node.getName()); + loop += "-" + node.getName(); + } + + //棣栧厛鑾峰彇褰撳墠鑺傜偣鐨勬墍鏈夊睘鎬ц妭鐐 + List list = node.attributes(); + //閬嶅巻灞炴ц妭鐐 + for(Attribute attribute : list){ +// System.out.println("灞炴 "+attribute.getName() +":" + attribute.getValue()); + kv.put(loop, attribute.getValue()); + loop += "-" + attribute.getValue(); +// System.out.println("loop: " + loop); + } + + //濡傛灉褰撳墠鑺傜偣鍐呭涓嶄负绌猴紝鍒欒緭鍑 + if(!(node.getTextTrim().equals(""))){ +// System.out.println("鍐呭 " + node.getName() + "锛" + node.getText()); + kv.put(loop, node.getText()); + loop += "-" + node.getText(); +// System.out.println("loop: " + loop); + } + + + //鍚屾椂杩唬褰撳墠鑺傜偣涓嬮潰鐨勬墍鏈夊瓙鑺傜偣 + //浣跨敤閫掑綊 + Iterator iterator = node.elementIterator(); + while(iterator.hasNext()){ + Element e = iterator.next(); + listNodes(e, loop, kv); + } + } + + private void fetchAttributes() throws DocumentException { + //鍒涘缓SAXReader瀵硅薄 + SAXReader reader = new SAXReader(); + //璇诲彇鏂囦欢 杞崲鎴怐ocument + Document document = (Document) reader.read(new File("./src/com/struts/struts.xml")); + + //鑾峰彇鏍硅妭鐐瑰厓绱犲璞 + Element root = ((org.dom4j.Document) document).getRootElement(); + + listNodes(root, "", kv); + + for (Map.Entry entity : kv.entrySet()) { +// System.out.println("key: " + entity.getKey() + " , value: " + entity.getValue()); + } + } + + public String getActionView(String action, String method) { + String key = "struts-action-" + action; + String className = kv.get(key); + key += "-" + className + "-result-" + method; + return kv.get(key); + } + + public String getActionClassByName(String action) { + String key = "struts-action-" + action; + return kv.get(key); + } + +// public static void main(String[] args) throws DocumentException { +// DomXmlHelper dm = new DomXmlHelper(); +// System.out.println(dm.getActionClassByName("login")); +// System.out.println(dm.getActionView("login", "success")); +// System.out.println(dm.getActionView("login", "fail")); +// +// System.out.println(dm.getActionClassByName("logout")); +// System.out.println(dm.getActionView("logout", "success")); +// System.out.println(dm.getActionView("logout", "error")); +// } + +} \ No newline at end of file diff --git a/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java new file mode 100644 index 0000000000..24e01dab79 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java @@ -0,0 +1,40 @@ +package com.struts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + System.out.println("name: " + name + " , password: " + password); + 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; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group24/1525619747/homework_20170319/src/com/struts/Struts.java b/group24/1525619747/homework_20170319/src/com/struts/Struts.java new file mode 100644 index 0000000000..1750ce2018 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/Struts.java @@ -0,0 +1,91 @@ +package com.struts; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; + +public class Struts +{ + + public static View runAction(String actionName, + Map parameters) throws DocumentException, + ClassNotFoundException, NoSuchMethodException, SecurityException, + InstantiationException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, NoSuchFieldException + { + + /* + * + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , + * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 + * {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters + * + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + + DomXmlHelper dx = new DomXmlHelper(); + String action = "login"; + String className = dx.getActionClassByName(action); +// System.out.println(className); + + Class class1 = null; + class1 = Class.forName(className); +// System.out.println("绫诲悕绉 " + class1.getName()); + + if (class1 != null) { + // 璋冪敤class1鐨剆etName鏂规硶, 寰呭弬鏁 + //鏍规嵁.class鍙嶅皠鍑烘潵鐨勭被瀹炰緥 + Object instance = class1.newInstance(); + + Method method = class1.getMethod("setName", String.class); + Object res1 =method.invoke(instance, "test"); + + method = class1.getMethod("setPassword", String.class); + Object res2 = method.invoke(instance, "1234"); + + // set attr + for (Map.Entry entity : parameters.entrySet()) { + String attrName = entity.getKey(); + String attrValue = entity.getValue(); + + Field idF = class1.getDeclaredField(attrName); // 鑾峰彇灞炴 + idF.setAccessible(true); // 浣跨敤鍙嶅皠鏈哄埗鍙互鎵撶牬灏佽鎬э紝瀵艰嚧浜唈ava瀵硅薄鐨勫睘鎬т笉瀹夊叏銆 + idF.set(instance, attrValue); // set + } + + View view = new View(); + + method = class1.getMethod("execute"); + Object res3 = method.invoke(instance); +// System.out.println(res3); + String jsp = dx.getActionView(action, res3.toString()); + view.setJsp(jsp); + + method = class1.getMethod("getMessage"); + Object res4 = method.invoke(instance); +// System.out.println(res4); + + Map map = new HashMap(); + map.put("message", res4.toString()); + + view.setParameters(map); + + return view; + } + + return null; + } + +} diff --git a/group24/1525619747/homework_20170319/src/com/struts/View.java b/group24/1525619747/homework_20170319/src/com/struts/View.java new file mode 100644 index 0000000000..5d6a6d9d1f --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/View.java @@ -0,0 +1,23 @@ +package com.struts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/1525619747/homework_20170319/src/com/struts/struts.xml b/group24/1525619747/homework_20170319/src/com/struts/struts.xml new file mode 100644 index 0000000000..a33fbd92bb --- /dev/null +++ b/group24/1525619747/homework_20170319/src/com/struts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java new file mode 100644 index 0000000000..8fd79a3e4e --- /dev/null +++ b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java @@ -0,0 +1,57 @@ +package testcase; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + + + + + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +import com.struts.DomXmlHelper; +import com.struts.Struts; +import com.struts.View; + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DocumentException, NoSuchFieldException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DocumentException, NoSuchFieldException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } + + + +} diff --git a/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java new file mode 100644 index 0000000000..6579308478 --- /dev/null +++ b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java @@ -0,0 +1,147 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.basic.ArrayUtil; + +public class TestArrayUtil +{ + private void print_r(int[] a) + { + for (int i = 0; i < a.length; i++) + { + System.out.print(a[i] + " "); + } + System.out.println(); + + // int index = 0; + // while (a[index] != '\0') { + // System.out.print(a[index] + " "); + // ++index; + // } + // System.out.println(); + } + + @Test + public void testReverseArray() + { + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] a = { 7, 9, 30, 3 }; + // print_r(a); + + arrayUtil.reverseArray(a); + // print_r(a); + assertTrue(a[0] == 3); + assertTrue(a[1] == 30); + assertTrue(a[3] == 7); + + int[] b = { 7, 9, 30, 3, 4 }; + // print_r(b); + + arrayUtil.reverseArray(b); + // print_r(b); + assertTrue(b[0] == 4); + assertTrue(b[1] == 3); + assertTrue(b[3] == 9); + assertTrue(b[2] == 30); + } + + @Test + public void testRemoveZero() + { + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 }; + int[] newArr = arrayUtil.removeZero(oldArr); + // print_r(newArr); + assertFalse(newArr[4] == 0); + assertTrue(newArr[4] == 6); + + } + + @Test + public void testMerge() + { + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] a1 = { 3, 5, 7, 8 }; + int[] a2 = { 4, 5, 6, 7 }; + + int[] newArr = arrayUtil.merge(a1, a2); +// print_r(newArr); + assertTrue(newArr[0] == 3); + assertTrue(newArr[2] == 5); + assertTrue(newArr[3] == 6); + assertTrue(newArr[5] == 8); + } + + @Test + public void testGrow() { + ArrayUtil arrayUtil = new ArrayUtil(); + + int[] a1 = { 3, 5, 7, 8 }; + a1 = arrayUtil.grow(a1, 3); +// print_r(a1); + assertTrue(a1[0] == 3); + assertTrue(a1[2] == 7); + assertTrue(a1[3] == 8); + assertTrue(a1[4] == 0); + assertTrue(a1[5] == 0); + assertTrue(a1[6] == 0); + + } + + @Test + public void testFibonacci() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 100; + int[] arr = arrayUtil.fibonacci(max); +// print_r(arr); + + assertNotNull(arr); + int index = (int) (Math.random() * arr.length); + assertTrue(arr[index] < max); + + arr = arrayUtil.fibonacci(1); + assertNull(arr); + } + + @Test + public void testGetPrimes() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 23; + int[] arr = arrayUtil.getPrimes(max); +// print_r(arr); + + int index = (int) (Math.random() * arr.length); + assertTrue(arr[index] < max); + assertTrue(arrayUtil.isPrime(arr[index])); + + } + + @Test + public void testGetPerfectNumbers() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 300; + int[] arr = arrayUtil.getPerfectNumbers(max); +// print_r(arr); + + int index = (int) (Math.random() * arr.length); + assertTrue(arr[index] < max); + assertTrue(arrayUtil.isPerfectNumber(arr[index])); + + } + + @Test + public void testJoin() { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] a = {3,8,9}; + String str = arrayUtil.join(a, "-"); +// System.out.println(str); + assertTrue(str.equals("3-8-9")); + } + +} From 477d9fe42402fe2e8fbad61901c48d1432c370ba Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 20:07:00 +0800 Subject: [PATCH 117/155] add first week homework untrancked files --- .../src/com/basic/ArrayList.java | 7 +- .../src/com/basic/LinkedList.java | 161 ++++++++++++++- .../src/com/basic/Queue.java | 8 +- .../src/com/basic/Stack.java | 9 +- .../src/testcase/TestArrayList.java | 28 ++- .../src/testcase/TestLinkedList.java | 194 ++++++++++++++++++ .../src/testcase/TestQueue.java | 31 +++ .../src/testcase/TestStack.java | 31 +++ 8 files changed, 442 insertions(+), 27 deletions(-) create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestQueue.java create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestStack.java diff --git a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java index 6c02e8d4f7..bb540052cd 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java @@ -21,13 +21,13 @@ public void add(Object o) public void add(int index, Object o) { - if (index > size) + if (index >= size && size > 0) { String errorInfo = "Index to add: " + index + " is out of current size: " + size; throw new ArrayIndexOutOfBoundsException(errorInfo); } - for (int i = size + 1; i > index + 1; i--) + for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; } @@ -54,11 +54,12 @@ public Object remove(int index) + " is invalid, current range: 0 - " + (size - 1); throw new ArrayIndexOutOfBoundsException(errorInfo); } + + Object o = elementData[index]; for (int i = index; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } - Object o = elementData[size]; elementData[size--] = null; return o; } diff --git a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java index 1e692521e2..2d9943e89b 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java @@ -124,6 +124,7 @@ public void addLast(Object o) nodePointer.next = new Node(o, null); } + @SuppressWarnings ("unused") public Object removeFirst() { if (head == null) { @@ -215,9 +216,18 @@ public void reverse() * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 濡傛灉list = 2->5->7->8->10 * ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 */ + @SuppressWarnings ("unused") public void removeFirstHalf() { - + int removeLength = size() / 2; + int step = 0; + Node toDelete = null; + while (step < removeLength) { + toDelete = head; + head = head.next; + toDelete = null; + ++step; + } } /** @@ -228,7 +238,38 @@ public void removeFirstHalf() */ public void remove(int i, int length) { - + int size = size(); + if (i >= size) { + return; + } + Node nodePointer = head; + Node lastPointer = head; + int step = 0; + while (step < i) { + lastPointer = nodePointer; + nodePointer = nodePointer.next; + ++step; + } + + step = 0; + Node toDelete = null; + if (lastPointer == head) { + while (step < length) { + toDelete = head; + head = head.next; + toDelete = null; + ++step; + } + } else { + while (step < length) { + toDelete = nodePointer; + nodePointer = nodePointer.next; + toDelete = null; + ++step; + } + lastPointer.next = nodePointer; + } + } /** @@ -238,9 +279,15 @@ public void remove(int i, int length) * * @param list */ - public static int[] getElements(LinkedList list) + public int[] getElements(LinkedList list) { - return null; + int[] elements = new int[list.size()]; + Iterator it = list.iterator(); + int index = 0; + for ( ; it.hasNext(); ) { + elements[index++] = (Integer) get((Integer) (it.next())); + } + return elements; } /** @@ -251,7 +298,41 @@ public static int[] getElements(LinkedList list) public void subtract(LinkedList list) { - + if (head == null) { + return; + } + Node nodePointer = head; + Node lastPointer = head; + Node toDelte = null; + while (nodePointer != null) { + if (list.contain(nodePointer.data)) { + if (nodePointer == head) { + toDelte = head; + head = head.next; + toDelte = null; + nodePointer = head; + lastPointer = head; + } else { + toDelte = nodePointer; + lastPointer.next = nodePointer.next; + toDelte = null; + } + } + lastPointer = nodePointer; + nodePointer = nodePointer.next; + } + + } + + private boolean contain(Object o) { + Node nodePointer = head; + while (nodePointer != null) { + if (nodePointer.data.equals(o)) { + return true; + } + nodePointer = nodePointer.next; + } + return false; } /** @@ -259,7 +340,20 @@ public void subtract(LinkedList list) */ public void removeDuplicateValues() { - + Node nodePointer = head; + if (nodePointer.next == null) { + return; + } + + Node toDelete = null; + while (nodePointer.next != null) { + while (nodePointer.next != null && nodePointer.data.equals(nodePointer.next.data)) { // delete nodePointer.next + toDelete = nodePointer.next; + nodePointer.next = nodePointer.next.next; + toDelete = null; + } + nodePointer = nodePointer.next; + } } /** @@ -270,7 +364,30 @@ public void removeDuplicateValues() */ public void removeRange(int min, int max) { - + Node nodePointer = head; + Node lastPointer = head; + if (nodePointer == null) { + return; + } + while (nodePointer != null && ((Integer) nodePointer.data) <= (new Integer(min))) { + lastPointer = nodePointer; + nodePointer = nodePointer.next; + } + Node toDelete = null; + while (nodePointer != null && ((Integer) nodePointer.data) < (new Integer(max))) { + if (nodePointer == head) { + toDelete = head; + head = head.next; + toDelete = null; + nodePointer = head; + lastPointer = head; + } else { + toDelete = nodePointer; + lastPointer.next = nodePointer.next; + nodePointer = nodePointer.next; + toDelete = null; + } + } } /** @@ -281,6 +398,34 @@ public void removeRange(int min, int max) */ public LinkedList intersection(LinkedList list) { - return null; + LinkedList linkedList = new LinkedList(); + Iterator it1 = iterator(); + Iterator it2 = list.iterator(); + Object o1 = null; + Object o2 = null; + + if (size() == 0 || list.size() == 0) { + return null; + } + + o1 = it1.next(); + o2 = it2.next(); + + while (o1 != null && o2 != null) { +// System.out.println(o1 + " " + o2); + if (((Integer) o1) == ((Integer) o2)) { + linkedList.add(o1); + o1 = it1.next(); + o2 = it2.next(); + } else { + if (((Integer) o1) > ((Integer) o2)) { + o2 = it2.next(); + } else { + o1 = it1.next(); + } + } + } + + return linkedList; } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/Queue.java b/group24/1525619747/homework_20170312/src/com/basic/Queue.java index 786b95a3a4..b040c85722 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/Queue.java +++ b/group24/1525619747/homework_20170312/src/com/basic/Queue.java @@ -2,23 +2,25 @@ public class Queue { + private LinkedList list = new LinkedList(); public void enQueue(Object o) { + list.addLast(o); } public Object deQueue() { - return null; + return list.removeFirst(); } public boolean isEmpty() { - return false; + return (list.size() == 0); } public int size() { - return -1; + return list.size(); } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/Stack.java b/group24/1525619747/homework_20170312/src/com/basic/Stack.java index 0226551dcc..db7ada4c53 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/Stack.java +++ b/group24/1525619747/homework_20170312/src/com/basic/Stack.java @@ -6,25 +6,26 @@ public class Stack public void push(Object o) { + elementData.add(0, o); } public Object pop() { - return null; + return elementData.remove(0); } public Object peek() { - return null; + return elementData.get(0); } public boolean isEmpty() { - return false; + return (elementData.size() == 0); } public int size() { - return -1; + return elementData.size(); } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java index 471243a83a..4cf2bf1a62 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java @@ -22,6 +22,7 @@ public void testAdd() { assertNull(e); } + assertTrue(list.get(0).equals(3)); try { for (int i = 0; i < 100; i++) @@ -32,7 +33,7 @@ public void testAdd() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); +// System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); } @@ -52,14 +53,23 @@ public void testIndexAdd() list.add(i); } list.add(3, 20); + list.add(0, 30); + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } - assertFalse(list.get(3).equals(3)); - assertTrue(list.get(3).equals(20)); - assertTrue(list.get(4).equals(4)); + +// Iterator it = list.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + assertTrue(list.get(0).equals(30)); + assertTrue(list.get(4).equals(20)); + assertTrue(list.get(5).equals(3)); try { @@ -71,7 +81,7 @@ public void testIndexAdd() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); +// System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); } } @@ -110,8 +120,8 @@ public void testGet() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); - System.out.println(list.size()); +// System.out.println(e.getMessage()); +// System.out.println(list.size()); String errorInfo = "Index to get: 10 is invalid, current range: 0 - " + (list.size() - 1); assertEquals(errorInfo, e.getMessage()); @@ -149,7 +159,7 @@ public void testRemove() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); +// System.out.println(e.getMessage()); } try @@ -159,7 +169,7 @@ public void testRemove() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); - System.out.println(e.getMessage()); +// System.out.println(e.getMessage()); } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java index 1f855e873c..54cf229718 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java @@ -185,6 +185,200 @@ public void testReverse() { assertEquals(o2, o6); } + @Test + public void testRemoveFirstHalf() { + LinkedList linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + linkedList.removeFirstHalf(); + assertTrue(linkedList.get(0).equals(7)); + + linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + + linkedList.removeFirstHalf(); + assertTrue(linkedList.get(2).equals(10)); + } + + @Test + public void testRemoveIndexLength() { + LinkedList linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + + linkedList.remove(0,2); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + assertTrue(linkedList.get(0).equals(7)); + + linkedList = new LinkedList(); + linkedList.add(2); + linkedList.add(5); + linkedList.add(7); + linkedList.add(8); + linkedList.add(10); + + linkedList.remove(2,2); + assertTrue(linkedList.get(0).equals(2)); + assertTrue(linkedList.get(2).equals(10)); + + } + + @Test + public void testGetElements() { +// 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + + LinkedList indexLinkedList = new LinkedList(); + indexLinkedList.add(1); + indexLinkedList.add(3); + indexLinkedList.add(4); + indexLinkedList.add(6); + + int[] elements = linkedList.getElements(indexLinkedList); + +// for (int i = 0; i < elements.length; i++) { +// System.out.print(elements[i] + " "); +// } +// System.out.println(); + + assertEquals(elements[0], linkedList.get(1)); + assertEquals(elements[1], linkedList.get(3)); + assertEquals(elements[2], linkedList.get(4)); + assertEquals(elements[3], linkedList.get(6)); + } + + + @Test + public void testSubstract() { + LinkedList linkedList = new LinkedList(); + linkedList.add(11); + linkedList.add(101); + linkedList.add(201); + linkedList.add(301); + linkedList.add(401); + linkedList.add(501); + linkedList.add(601); + linkedList.add(701); + + + LinkedList substractList = new LinkedList(); + substractList.add(11); + substractList.add(301); + substractList.add(404); + substractList.add(501); + substractList.add(701); + + linkedList.subtract(substractList); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + assertFalse(linkedList.get(0).equals(11)); + assertTrue(linkedList.get(0).equals(101)); + } + @Test + public void testRemoveDuplicateValues() { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(3); + linkedList.add(4); + linkedList.add(4); + linkedList.add(5); + + linkedList.removeDuplicateValues(); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + + assertTrue(linkedList.get(0).equals(1)); + assertTrue(linkedList.get(1).equals(2)); + assertTrue(linkedList.get(4).equals(5)); + + } + + @Test + public void testRemoveRange() { + LinkedList linkedList = new LinkedList(); + for (int i = 0; i < 10; i++) { + linkedList.add(i); + } + + linkedList.removeRange(3, 7); + +// Iterator it = linkedList.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + assertTrue(linkedList.get(3).equals(3)); + assertTrue(linkedList.get(4).equals(7)); + } + + @Test + public void testIntersection() { + LinkedList linkedList = new LinkedList(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + linkedList.add(6); + + LinkedList secondList = new LinkedList(); + secondList.add(1); + secondList.add(3); + secondList.add(5); + secondList.add(6); + + LinkedList intersection = linkedList.intersection(secondList); +// Iterator it = intersection.iterator(); +// for ( ; it.hasNext(); ) { +// System.out.print(it.next() + " "); +// } +// System.out.println(); + assertTrue(intersection.get(0).equals(1)); + assertTrue(intersection.get(1).equals(3)); + assertTrue(intersection.get(2).equals(5)); + assertTrue(intersection.get(3).equals(6)); + } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestQueue.java b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java new file mode 100644 index 0000000000..a08ee80951 --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java @@ -0,0 +1,31 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.basic.Queue; + +public class TestQueue +{ + /* + * test enQueue() isEmpty() and size() deQueue + * */ + @Test + public void testQueue() { + Queue queue = new Queue(); + + assertTrue(queue.isEmpty()); + + for (int i = 10; i < 20; i++) { + queue.enQueue(i); + } + + assertFalse(queue.isEmpty()); + assertTrue(queue.size() == 10); + + assertTrue(queue.deQueue().equals(10)); + assertTrue(queue.deQueue().equals(11)); + } + +} diff --git a/group24/1525619747/homework_20170312/src/testcase/TestStack.java b/group24/1525619747/homework_20170312/src/testcase/TestStack.java new file mode 100644 index 0000000000..58c7206fea --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestStack.java @@ -0,0 +1,31 @@ +package testcase; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.basic.Stack; + +public class TestStack +{ + @Test + public void testStack() { + Stack stack = new Stack(); + + assertTrue(stack.isEmpty()); + + for (int i = 10; i < 20; i++) { + stack.push(i); + } + + assertFalse(stack.isEmpty()); + assertTrue(stack.size() == 10); + + assertTrue(stack.peek().equals(19)); + assertFalse(stack.peek().equals(10)); + + assertTrue(stack.pop().equals(19)); + assertTrue(stack.pop().equals(18)); + } + +} From 118d6c54c44ae1803ae171e3c75e6acf96c087f0 Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 20:14:07 +0800 Subject: [PATCH 118/155] delete unneed dir --- .../homework_20170311/src/Test.java | 29 ---- .../coding2017/basic/ArrayList.java | 92 ------------- .../coding2017/basic/BinaryTreeNode.java | 32 ----- .../dengliotng/coding2017/basic/Iterator.java | 7 - .../coding2017/basic/LinkedList.java | 124 ------------------ .../dengliotng/coding2017/basic/List.java | 9 -- .../dengliotng/coding2017/basic/Queue.java | 19 --- .../dengliotng/coding2017/basic/Stack.java | 22 ---- 8 files changed, 334 deletions(-) delete mode 100644 group24/1525619747/homework_20170311/src/Test.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java delete mode 100644 group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java diff --git a/group24/1525619747/homework_20170311/src/Test.java b/group24/1525619747/homework_20170311/src/Test.java deleted file mode 100644 index 9e40820653..0000000000 --- a/group24/1525619747/homework_20170311/src/Test.java +++ /dev/null @@ -1,29 +0,0 @@ -import com.github.dengliotng.coding2017.basic.ArrayList; -import com.github.dengliotng.coding2017.basic.Iterator; - -/** - * Created by LeonDeng on 2017/3/11. - */ -public class Test { - - public static void main(String[] args) { - ArrayList a = new ArrayList(); - for (int i =0; i<10; i++) { - a.add("hello" + i); - } - a.add(20.56); - System.out.println(a.size()); - a.add(5, 20.56); - System.out.println(a.size()); - System.out.println(a.get(5)); -// a.remove(5); -// a.remove(200); - - Iterator i = a.iterator(); - for ( ; i.hasNext(); ) { - String str = i.next().toString(); - System.out.println(str); - } - } - -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java deleted file mode 100644 index a588d2845e..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public class ArrayList implements List { - - private int size = 0; - private static final int maxLength = 100; - - private Object[] elementData = new Object[maxLength]; - - public void add(Object o){ - if (size > maxLength) { - return; - } - elementData[size++] = o; - } - - public void add(int index, Object o){ - if (size > maxLength) { - return; - } - if (elementData[index] == null) { - elementData[index] = o; - ++size; - return; - } else { - int emptyIndex = index + 1; - boolean hasEmpty = false; - for (int i = index; i < maxLength; i++) { - if (elementData[i] == null) { - hasEmpty = true; - emptyIndex = i; - } - } - if (!hasEmpty) { - return; - } - //shift - for (int i = emptyIndex; i > index; --i) { - elementData[i] = elementData[i-1]; - } - elementData[index] = o; - ++size; - } - } - - public Object get(int index){ - if (index > maxLength || index < 0) { - return null; - } - return elementData[index]; - } - - public Object remove(int index){ - if (index > maxLength || index < 0) { - return null; - } - Object o = elementData[index]; - for (int i = index; i < size - 1; ++i) { - elementData[i] = elementData[i+1]; - } - --size; - return o; - } - - public int size(){ - return size; - } - - public Iterator iterator() { - return new Iterator() { - private int index=0; - @Override - public boolean hasNext() { - if (index < size) { - return true; - } - return false; - } - - @Override - public Object next() { - if (hasNext()) { - return elementData[index++]; - } - return null; - } - }; - } - - - -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index fd3e827346..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java deleted file mode 100644 index b40a21f6c4..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java deleted file mode 100644 index e8f8244d46..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - - - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - /** - * 鎶婅閾捐〃閫嗙疆 - * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 - */ - public void reverse(){ - - } - - /** - * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 - * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 - * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 - * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 - * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - return null; - } - - /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 - */ - public void removeDuplicateValues(){ - - } - - /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 - * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java deleted file mode 100644 index fe5f523eea..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -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(); -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java deleted file mode 100644 index 556e3b4293..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Queue.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} diff --git a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java b/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java deleted file mode 100644 index ae0a194504..0000000000 --- a/group24/1525619747/homework_20170311/src/com/github/dengliotng/coding2017/basic/Stack.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.github.dengliotng.coding2017.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} From 47d4fe2ded043b43b1af76ae81d0fc884cee1813 Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 20:18:02 +0800 Subject: [PATCH 119/155] format codes --- .../src/com/basic/ArrayUtil.java | 93 +++++---- .../src/com/struts/DomXmlHelper.java | 196 ++++++++++-------- .../src/com/struts/LoginAction.java | 71 ++++--- .../src/com/struts/Struts.java | 91 ++++---- .../src/com/struts/View.java | 20 +- .../src/testcase/StrutsTest.java | 66 +++--- .../src/testcase/TestArrayUtil.java | 55 ++--- 7 files changed, 326 insertions(+), 266 deletions(-) diff --git a/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java index 86ba46d8cd..9b4b81c12d 100644 --- a/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java +++ b/group24/1525619747/homework_20170319/src/com/basic/ArrayUtil.java @@ -88,11 +88,10 @@ public int[] merge(int[] array1, int[] array2) int index1 = 0; int index2 = 0; int index3 = 0; - + while (index1 < length1 && index2 < length2) { - - + if (index3 > 0) { if (array3[index3 - 1] == array1[index1]) @@ -106,7 +105,7 @@ public int[] merge(int[] array1, int[] array2) continue; } } - + if (array1[index1] == array2[index2]) { array3[index3++] = array1[index1]; @@ -128,13 +127,15 @@ public int[] merge(int[] array1, int[] array2) } } - while (index1 < length1) { + while (index1 < length1) + { array3[index3++] = array1[index1++]; } - while (index2 < length2) { + while (index2 < length2) + { array3[index3++] = array1[index2++]; } - + int[] newArray = new int[index3]; for (int i = 0; i < index3; i++) { @@ -157,10 +158,12 @@ public int[] grow(int[] oldArray, int size) { int length = oldArray.length; int[] newArr = new int[length + size]; - for (int i = 0; i < length; i++) { + for (int i = 0; i < length; i++) + { newArr[i] = oldArray[i]; } - for (int i = length; i < length + size; i++) { + for (int i = length; i < length + size; i++) + { newArr[i] = 0; } return newArr; @@ -175,30 +178,33 @@ public int[] grow(int[] oldArray, int size) */ public int[] fibonacci(int max) { - if (max == 1) { + if (max == 1) + { return null; } int[] arr = new int[max / 2]; int a = 1; int b = 1; int c = 0; - + arr[0] = 1; arr[1] = 1; - + int index = 2; - while (a + b < max) { + while (a + b < max) + { arr[index++] = a + b; c = b; b = a + b; a = c; } - + int[] newArr = new int[index]; - for (int i = 0; i < index; i++) { + for (int i = 0; i < index; i++) + { newArr[i] = arr[i]; } - + return newArr; } @@ -213,23 +219,29 @@ public int[] getPrimes(int max) int size = max / 2 + 1; int[] arr = new int[size]; int index = 0; - for (int i = 2; i < max; i++) { - if (isPrime(i)) { + for (int i = 2; i < max; i++) + { + if (isPrime(i)) + { arr[index++] = i; } } - + int[] newArr = new int[index]; - for (int i = 0; i < index; i++) { + for (int i = 0; i < index; i++) + { newArr[i] = arr[i]; } - + return newArr; } - - public boolean isPrime(int i) { - for (int j = 2; j < i / 2; j++) { - if (i % j == 0) { + + public boolean isPrime(int i) + { + for (int j = 2; j < i / 2; j++) + { + if (i % j == 0) + { return false; } } @@ -247,28 +259,34 @@ public int[] getPerfectNumbers(int max) int size = max / 2 + 1; int[] arr = new int[size]; int index = 0; - for (int i = 2; i < max; i++) { - if (isPerfectNumber(i)) { + for (int i = 2; i < max; i++) + { + if (isPerfectNumber(i)) + { arr[index++] = i; } } - + int[] newArr = new int[index]; - for (int i = 0; i < index; i++) { + for (int i = 0; i < index; i++) + { newArr[i] = arr[i]; } - + return newArr; } - - public boolean isPerfectNumber(int num) { + + public boolean isPerfectNumber(int num) + { int sum = 0; - for (int i = 1; i <= num / 2; i++) { - if (num % i == 0) { + for (int i = 1; i <= num / 2; i++) + { + if (num % i == 0) + { sum += i; } } -// System.out.println("num: " + num + ", sum:" + sum); + // System.out.println("num: " + num + ", sum:" + sum); return (num == sum); } @@ -283,10 +301,11 @@ public String join(int[] array, String seperator) { String str = ""; int length = array.length; - for (int a : array) { + for (int a : array) + { str += a + seperator; } - str = str.substring(0, str.length()-1); + str = str.substring(0, str.length() - 1); return str; } diff --git a/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java index 507a974297..31d27b592c 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java +++ b/group24/1525619747/homework_20170319/src/com/struts/DomXmlHelper.java @@ -7,99 +7,115 @@ import java.util.List; import java.util.Map; -import org.dom4j.Attribute; -import org.dom4j.Document; +import org.dom4j.Attribute; +import org.dom4j.Document; import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; /** * DOM鏂瑰紡瑙f瀽xml */ -public class DomXmlHelper { - - private Map kv = new HashMap(); - - public DomXmlHelper() throws DocumentException { +public class DomXmlHelper +{ + + private Map kv = new HashMap(); + + public DomXmlHelper () throws DocumentException + { fetchAttributes(); - } - - //閬嶅巻褰撳墠鑺傜偣涓嬬殑鎵鏈夎妭鐐 - private void listNodes(Element node, String loop, Map kv){ - -// System.out.println("褰撳墠鑺傜偣鐨勫悕绉帮細" + node.getName()); - if (loop.equals("")) { - loop += node.getName(); - } else { - kv.put(loop, node.getName()); - loop += "-" + node.getName(); - } - - //棣栧厛鑾峰彇褰撳墠鑺傜偣鐨勬墍鏈夊睘鎬ц妭鐐 - List list = node.attributes(); - //閬嶅巻灞炴ц妭鐐 - for(Attribute attribute : list){ -// System.out.println("灞炴 "+attribute.getName() +":" + attribute.getValue()); - kv.put(loop, attribute.getValue()); - loop += "-" + attribute.getValue(); -// System.out.println("loop: " + loop); - } - - //濡傛灉褰撳墠鑺傜偣鍐呭涓嶄负绌猴紝鍒欒緭鍑 - if(!(node.getTextTrim().equals(""))){ -// System.out.println("鍐呭 " + node.getName() + "锛" + node.getText()); - kv.put(loop, node.getText()); - loop += "-" + node.getText(); -// System.out.println("loop: " + loop); - } - - - //鍚屾椂杩唬褰撳墠鑺傜偣涓嬮潰鐨勬墍鏈夊瓙鑺傜偣 - //浣跨敤閫掑綊 - Iterator iterator = node.elementIterator(); - while(iterator.hasNext()){ - Element e = iterator.next(); - listNodes(e, loop, kv); - } - } - - private void fetchAttributes() throws DocumentException { - //鍒涘缓SAXReader瀵硅薄 - SAXReader reader = new SAXReader(); - //璇诲彇鏂囦欢 杞崲鎴怐ocument - Document document = (Document) reader.read(new File("./src/com/struts/struts.xml")); - - //鑾峰彇鏍硅妭鐐瑰厓绱犲璞 - Element root = ((org.dom4j.Document) document).getRootElement(); - - listNodes(root, "", kv); - - for (Map.Entry entity : kv.entrySet()) { -// System.out.println("key: " + entity.getKey() + " , value: " + entity.getValue()); - } - } - - public String getActionView(String action, String method) { - String key = "struts-action-" + action; - String className = kv.get(key); - key += "-" + className + "-result-" + method; - return kv.get(key); - } - - public String getActionClassByName(String action) { - String key = "struts-action-" + action; - return kv.get(key); - } - -// public static void main(String[] args) throws DocumentException { -// DomXmlHelper dm = new DomXmlHelper(); -// System.out.println(dm.getActionClassByName("login")); -// System.out.println(dm.getActionView("login", "success")); -// System.out.println(dm.getActionView("login", "fail")); -// -// System.out.println(dm.getActionClassByName("logout")); -// System.out.println(dm.getActionView("logout", "success")); -// System.out.println(dm.getActionView("logout", "error")); -// } - -} \ No newline at end of file + } + + // 閬嶅巻褰撳墠鑺傜偣涓嬬殑鎵鏈夎妭鐐 + private void listNodes(Element node, String loop, Map kv) + { + + // System.out.println("褰撳墠鑺傜偣鐨勫悕绉帮細" + node.getName()); + if (loop.equals("")) + { + loop += node.getName(); + } + else + { + kv.put(loop, node.getName()); + loop += "-" + node.getName(); + } + + // 棣栧厛鑾峰彇褰撳墠鑺傜偣鐨勬墍鏈夊睘鎬ц妭鐐 + List list = node.attributes(); + // 閬嶅巻灞炴ц妭鐐 + for (Attribute attribute : list) + { + // System.out.println("灞炴 "+attribute.getName() +":" + + // attribute.getValue()); + kv.put(loop, attribute.getValue()); + loop += "-" + attribute.getValue(); + // System.out.println("loop: " + loop); + } + + // 濡傛灉褰撳墠鑺傜偣鍐呭涓嶄负绌猴紝鍒欒緭鍑 + if (!(node.getTextTrim().equals(""))) + { + // System.out.println("鍐呭 " + node.getName() + "锛" + + // node.getText()); + kv.put(loop, node.getText()); + loop += "-" + node.getText(); + // System.out.println("loop: " + loop); + } + + // 鍚屾椂杩唬褰撳墠鑺傜偣涓嬮潰鐨勬墍鏈夊瓙鑺傜偣 + // 浣跨敤閫掑綊 + Iterator iterator = node.elementIterator(); + while (iterator.hasNext()) + { + Element e = iterator.next(); + listNodes(e, loop, kv); + } + } + + private void fetchAttributes() throws DocumentException + { + // 鍒涘缓SAXReader瀵硅薄 + SAXReader reader = new SAXReader(); + // 璇诲彇鏂囦欢 杞崲鎴怐ocument + Document document = (Document) reader.read(new File( + "./src/com/struts/struts.xml")); + + // 鑾峰彇鏍硅妭鐐瑰厓绱犲璞 + Element root = ((org.dom4j.Document) document).getRootElement(); + + listNodes(root, "", kv); + + for (Map.Entry entity : kv.entrySet()) + { + // System.out.println("key: " + entity.getKey() + " , value: " + + // entity.getValue()); + } + } + + public String getActionView(String action, String method) + { + String key = "struts-action-" + action; + String className = kv.get(key); + key += "-" + className + "-result-" + method; + return kv.get(key); + } + + public String getActionClassByName(String action) + { + String key = "struts-action-" + action; + return kv.get(key); + } + + // public static void main(String[] args) throws DocumentException { + // DomXmlHelper dm = new DomXmlHelper(); + // System.out.println(dm.getActionClassByName("login")); + // System.out.println(dm.getActionView("login", "success")); + // System.out.println(dm.getActionView("login", "fail")); + // + // System.out.println(dm.getActionClassByName("logout")); + // System.out.println(dm.getActionView("logout", "success")); + // System.out.println(dm.getActionView("logout", "error")); + // } + +} diff --git a/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java index 24e01dab79..6d1d5a2ca6 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java +++ b/group24/1525619747/homework_20170319/src/com/struts/LoginAction.java @@ -2,39 +2,50 @@ /** * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 - * @author - * + * + * @author + * */ -public class LoginAction{ - private String name ; - private String password; - private String message; +public class LoginAction +{ + private String name; + private String password; + private String message; - public String getName() { - return name; - } + public String getName() + { + return name; + } - public String getPassword() { - return password; - } + public String getPassword() + { + return password; + } - public String execute(){ - System.out.println("name: " + name + " , password: " + password); - 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 String execute() + { + System.out.println("name: " + name + " , password: " + password); + 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; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } + public void setName(String name) + { + this.name = name; + } + + public void setPassword(String password) + { + this.password = password; + } + + public String getMessage() + { + return this.message; + } } diff --git a/group24/1525619747/homework_20170319/src/com/struts/Struts.java b/group24/1525619747/homework_20170319/src/com/struts/Struts.java index 1750ce2018..f0ff145909 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/Struts.java +++ b/group24/1525619747/homework_20170319/src/com/struts/Struts.java @@ -15,7 +15,8 @@ public static View runAction(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, - IllegalArgumentException, InvocationTargetException, NoSuchFieldException + IllegalArgumentException, InvocationTargetException, + NoSuchFieldException { /* @@ -34,55 +35,57 @@ public static View runAction(String actionName, * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 */ - + DomXmlHelper dx = new DomXmlHelper(); String action = "login"; String className = dx.getActionClassByName(action); -// System.out.println(className); - + // System.out.println(className); + Class class1 = null; class1 = Class.forName(className); -// System.out.println("绫诲悕绉 " + class1.getName()); - - if (class1 != null) { - // 璋冪敤class1鐨剆etName鏂规硶, 寰呭弬鏁 - //鏍规嵁.class鍙嶅皠鍑烘潵鐨勭被瀹炰緥 - Object instance = class1.newInstance(); - + // System.out.println("绫诲悕绉 " + class1.getName()); + + if (class1 != null) + { + // 璋冪敤class1鐨剆etName鏂规硶, 寰呭弬鏁 + // 鏍规嵁.class鍙嶅皠鍑烘潵鐨勭被瀹炰緥 + Object instance = class1.newInstance(); + Method method = class1.getMethod("setName", String.class); - Object res1 =method.invoke(instance, "test"); - - method = class1.getMethod("setPassword", String.class); - Object res2 = method.invoke(instance, "1234"); - - // set attr - for (Map.Entry entity : parameters.entrySet()) { - String attrName = entity.getKey(); - String attrValue = entity.getValue(); - - Field idF = class1.getDeclaredField(attrName); // 鑾峰彇灞炴 - idF.setAccessible(true); // 浣跨敤鍙嶅皠鏈哄埗鍙互鎵撶牬灏佽鎬э紝瀵艰嚧浜唈ava瀵硅薄鐨勫睘鎬т笉瀹夊叏銆 - idF.set(instance, attrValue); // set - } - - View view = new View(); - - method = class1.getMethod("execute"); - Object res3 = method.invoke(instance); -// System.out.println(res3); - String jsp = dx.getActionView(action, res3.toString()); - view.setJsp(jsp); - - method = class1.getMethod("getMessage"); - Object res4 = method.invoke(instance); -// System.out.println(res4); - - Map map = new HashMap(); - map.put("message", res4.toString()); - - view.setParameters(map); - - return view; + Object res1 = method.invoke(instance, "test"); + + method = class1.getMethod("setPassword", String.class); + Object res2 = method.invoke(instance, "1234"); + + // set attr + for (Map.Entry entity : parameters.entrySet()) + { + String attrName = entity.getKey(); + String attrValue = entity.getValue(); + + Field idF = class1.getDeclaredField(attrName); // 鑾峰彇灞炴 + idF.setAccessible(true); // 浣跨敤鍙嶅皠鏈哄埗鍙互鎵撶牬灏佽鎬э紝瀵艰嚧浜唈ava瀵硅薄鐨勫睘鎬т笉瀹夊叏銆 + idF.set(instance, attrValue); // set + } + + View view = new View(); + + method = class1.getMethod("execute"); + Object res3 = method.invoke(instance); + // System.out.println(res3); + String jsp = dx.getActionView(action, res3.toString()); + view.setJsp(jsp); + + method = class1.getMethod("getMessage"); + Object res4 = method.invoke(instance); + // System.out.println(res4); + + Map map = new HashMap(); + map.put("message", res4.toString()); + + view.setParameters(map); + + return view; } return null; diff --git a/group24/1525619747/homework_20170319/src/com/struts/View.java b/group24/1525619747/homework_20170319/src/com/struts/View.java index 5d6a6d9d1f..b5b9e2802b 100644 --- a/group24/1525619747/homework_20170319/src/com/struts/View.java +++ b/group24/1525619747/homework_20170319/src/com/struts/View.java @@ -2,21 +2,29 @@ import java.util.Map; -public class View { +public class View +{ private String jsp; private Map parameters; - - public String getJsp() { + + public String getJsp() + { return jsp; } - public View setJsp(String jsp) { + + public View setJsp(String jsp) + { this.jsp = jsp; return this; } - public Map getParameters() { + + public Map getParameters() + { return parameters; } - public View setParameters(Map parameters) { + + public View setParameters(Map parameters) + { this.parameters = parameters; return this; } diff --git a/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java index 8fd79a3e4e..61ef07748f 100644 --- a/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java +++ b/group24/1525619747/homework_20170319/src/testcase/StrutsTest.java @@ -1,57 +1,55 @@ package testcase; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; - - - - - import org.dom4j.DocumentException; import org.junit.Assert; import org.junit.Test; -import com.struts.DomXmlHelper; import com.struts.Struts; import com.struts.View; - - -public class StrutsTest { +public class StrutsTest +{ @Test - public void testLoginActionSuccess() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DocumentException, NoSuchFieldException { - + public void testLoginActionSuccess() throws ClassNotFoundException, + NoSuchMethodException, SecurityException, InstantiationException, + IllegalAccessException, IllegalArgumentException, + InvocationTargetException, DocumentException, NoSuchFieldException + { + String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + 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() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, DocumentException, NoSuchFieldException { + public void testLoginActionFailed() throws ClassNotFoundException, + NoSuchMethodException, SecurityException, InstantiationException, + IllegalAccessException, IllegalArgumentException, + InvocationTargetException, DocumentException, NoSuchFieldException + { String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 - - 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")); + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); // 瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); } - - } diff --git a/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java index 6579308478..fffa56eac1 100644 --- a/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java +++ b/group24/1525619747/homework_20170319/src/testcase/TestArrayUtil.java @@ -69,78 +69,83 @@ public void testMerge() int[] a1 = { 3, 5, 7, 8 }; int[] a2 = { 4, 5, 6, 7 }; - + int[] newArr = arrayUtil.merge(a1, a2); -// print_r(newArr); + // print_r(newArr); assertTrue(newArr[0] == 3); assertTrue(newArr[2] == 5); assertTrue(newArr[3] == 6); assertTrue(newArr[5] == 8); } - + @Test - public void testGrow() { + public void testGrow() + { ArrayUtil arrayUtil = new ArrayUtil(); int[] a1 = { 3, 5, 7, 8 }; a1 = arrayUtil.grow(a1, 3); -// print_r(a1); + // print_r(a1); assertTrue(a1[0] == 3); assertTrue(a1[2] == 7); assertTrue(a1[3] == 8); assertTrue(a1[4] == 0); assertTrue(a1[5] == 0); assertTrue(a1[6] == 0); - + } - + @Test - public void testFibonacci() { + public void testFibonacci() + { ArrayUtil arrayUtil = new ArrayUtil(); int max = 100; int[] arr = arrayUtil.fibonacci(max); -// print_r(arr); - + // print_r(arr); + assertNotNull(arr); int index = (int) (Math.random() * arr.length); assertTrue(arr[index] < max); - + arr = arrayUtil.fibonacci(1); assertNull(arr); } - + @Test - public void testGetPrimes() { + public void testGetPrimes() + { ArrayUtil arrayUtil = new ArrayUtil(); int max = 23; int[] arr = arrayUtil.getPrimes(max); -// print_r(arr); - + // print_r(arr); + int index = (int) (Math.random() * arr.length); assertTrue(arr[index] < max); assertTrue(arrayUtil.isPrime(arr[index])); - + } - + @Test - public void testGetPerfectNumbers() { + public void testGetPerfectNumbers() + { ArrayUtil arrayUtil = new ArrayUtil(); int max = 300; int[] arr = arrayUtil.getPerfectNumbers(max); -// print_r(arr); - + // print_r(arr); + int index = (int) (Math.random() * arr.length); assertTrue(arr[index] < max); assertTrue(arrayUtil.isPerfectNumber(arr[index])); - + } - + @Test - public void testJoin() { + public void testJoin() + { ArrayUtil arrayUtil = new ArrayUtil(); - int[] a = {3,8,9}; + int[] a = { 3, 8, 9 }; String str = arrayUtil.join(a, "-"); -// System.out.println(str); + // System.out.println(str); assertTrue(str.equals("3-8-9")); } From 9069dcd66e4005bd668e8370932d6991b0d9bee4 Mon Sep 17 00:00:00 2001 From: Yanbo Date: Sun, 19 Mar 2017 20:55:20 +0800 Subject: [PATCH 120/155] Signed-off-by: Yanbo --- group23/609041842/.classpath | 10 + group23/609041842/.gitignore | 1 + group23/609041842/.project | 17 ++ .../.settings/org.eclipse.jdt.ui.prefs | 60 +++++ .../{code => com/homework01}/ArrayList.java | 4 +- .../{code => com/homework01}/LinkedList.java | 2 +- .../src/{code => com/homework01}/Queue.java | 2 +- .../src/{code => com/homework01}/Stack.java | 2 +- .../src/com/homework02/ArrayUtil.java | 208 ++++++++++++++++++ .../src/com/homework02/LoginAction.java | 42 ++++ .../609041842/src/com/homework02/Struts.java | 121 ++++++++++ .../src/com/homework02/StrutsTest.java | 38 ++++ .../609041842/src/com/homework02/View.java | 32 +++ group23/609041842/src/struts.xml | 11 + group23/609041842/src/test.java | 9 + 15 files changed, 554 insertions(+), 5 deletions(-) create mode 100644 group23/609041842/.classpath create mode 100644 group23/609041842/.gitignore create mode 100644 group23/609041842/.project create mode 100644 group23/609041842/.settings/org.eclipse.jdt.ui.prefs rename group23/609041842/src/{code => com/homework01}/ArrayList.java (90%) rename group23/609041842/src/{code => com/homework01}/LinkedList.java (93%) rename group23/609041842/src/{code => com/homework01}/Queue.java (85%) rename group23/609041842/src/{code => com/homework01}/Stack.java (89%) create mode 100644 group23/609041842/src/com/homework02/ArrayUtil.java create mode 100644 group23/609041842/src/com/homework02/LoginAction.java create mode 100644 group23/609041842/src/com/homework02/Struts.java create mode 100644 group23/609041842/src/com/homework02/StrutsTest.java create mode 100644 group23/609041842/src/com/homework02/View.java create mode 100644 group23/609041842/src/struts.xml create mode 100644 group23/609041842/src/test.java diff --git a/group23/609041842/.classpath b/group23/609041842/.classpath new file mode 100644 index 0000000000..4449a3dae9 --- /dev/null +++ b/group23/609041842/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/group23/609041842/.gitignore b/group23/609041842/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group23/609041842/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group23/609041842/.project b/group23/609041842/.project new file mode 100644 index 0000000000..b2b4094e01 --- /dev/null +++ b/group23/609041842/.project @@ -0,0 +1,17 @@ + + + 609041842coding + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group23/609041842/.settings/org.eclipse.jdt.ui.prefs b/group23/609041842/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000000..b6ec1a71fb --- /dev/null +++ b/group23/609041842/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,60 @@ +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=true +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_functional_interfaces=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.correct_indentation=false +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.insert_inferred_type_arguments=false +sp_cleanup.make_local_variable_final=true +sp_cleanup.make_parameters_final=false +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=false +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=true +sp_cleanup.on_save_use_additional_actions=false +sp_cleanup.organize_imports=true +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_redundant_type_arguments=true +sp_cleanup.remove_trailing_whitespaces=false +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=false +sp_cleanup.remove_unused_imports=false +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_anonymous_class_creation=false +sp_cleanup.use_blocks=false +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_lambda=true +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=false +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=false +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true +sp_cleanup.use_type_arguments=false diff --git a/group23/609041842/src/code/ArrayList.java b/group23/609041842/src/com/homework01/ArrayList.java similarity index 90% rename from group23/609041842/src/code/ArrayList.java rename to group23/609041842/src/com/homework01/ArrayList.java index e24f37e35a..1feb6162ad 100644 --- a/group23/609041842/src/code/ArrayList.java +++ b/group23/609041842/src/com/homework01/ArrayList.java @@ -1,4 +1,4 @@ -package code; +package com.homework01; import java.util.Arrays; @@ -36,7 +36,7 @@ public Object remove(int index){ System.arraycopy(obj, index+1, tar, index, obj.length-index-1); Object o = obj[index]; obj = tar; - return o;//返回被删元素 + return o;//锟斤拷锟截憋拷删元锟斤拷 } public static void main(String[] args) { ArrayList al = new ArrayList(); diff --git a/group23/609041842/src/code/LinkedList.java b/group23/609041842/src/com/homework01/LinkedList.java similarity index 93% rename from group23/609041842/src/code/LinkedList.java rename to group23/609041842/src/com/homework01/LinkedList.java index 6f2ac5158f..f18c110c7c 100644 --- a/group23/609041842/src/code/LinkedList.java +++ b/group23/609041842/src/com/homework01/LinkedList.java @@ -1,4 +1,4 @@ -package code; +package com.homework01; public class LinkedList { private static Node head; diff --git a/group23/609041842/src/code/Queue.java b/group23/609041842/src/com/homework01/Queue.java similarity index 85% rename from group23/609041842/src/code/Queue.java rename to group23/609041842/src/com/homework01/Queue.java index 4f18db0129..afc54a2dda 100644 --- a/group23/609041842/src/code/Queue.java +++ b/group23/609041842/src/com/homework01/Queue.java @@ -1,4 +1,4 @@ -package code; +package com.homework01; public class Queue { diff --git a/group23/609041842/src/code/Stack.java b/group23/609041842/src/com/homework01/Stack.java similarity index 89% rename from group23/609041842/src/code/Stack.java rename to group23/609041842/src/com/homework01/Stack.java index 315b07f5a8..a5bc4488af 100644 --- a/group23/609041842/src/code/Stack.java +++ b/group23/609041842/src/com/homework01/Stack.java @@ -1,4 +1,4 @@ -package code; +package com.homework01; public class Stack { diff --git a/group23/609041842/src/com/homework02/ArrayUtil.java b/group23/609041842/src/com/homework02/ArrayUtil.java new file mode 100644 index 0000000000..9ed26f4d42 --- /dev/null +++ b/group23/609041842/src/com/homework02/ArrayUtil.java @@ -0,0 +1,208 @@ +package com.homework02; + +import java.util.ArrayList; +import java.util.Arrays; + +import org.junit.Test; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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) { + int[] reverse = new int[origin.length]; + for (int i = origin.length - 1; i >= 0; i--) { + reverse[reverse.length - i - 1] = origin[i]; + } + System.out.println("reverseArray-" + Arrays.toString(reverse)); + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + count++; + } + } + int[] newArray = new int[count]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + for (int j = 0; j < newArray.length; j++) { + if (newArray[j] == 0) { + newArray[j] = oldArray[i]; + break; + } + } + } + } + System.out.println("removeZero-" + Arrays.toString(newArray)); + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { + ArrayUtil atl = new ArrayUtil(); + int[] newArray = new int[array1.length + array2.length]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + System.arraycopy(array2, 0, newArray, array1.length, array2.length); + newArray = atl.sort(newArray); + System.out.println(Arrays.toString(newArray)); + for (int i = 0; i < newArray.length; i++) { + for (int j = 0; j < newArray.length; j++) { + } + } + return newArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + System.out.println("grow:" + Arrays.toString(newArray)); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 渚嬪锛 max = 15 , + * 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public ArrayList fibonacci(int max) { + int[] array = new int[max]; + int count = 1; + int a = 1; + int b = 1; + int c = 1; + ArrayList arrayList = new ArrayList(); + if (max <= 1) { + System.out.println("-" + arrayList); + return arrayList; + } else { + arrayList.add(a); + arrayList.add(b); + while (c < max) { + c = a + b; + a = b; + if (c + b > max) { + break; + } + arrayList.add(c); + b = c; + } + System.out.println("-" + arrayList); + } + return arrayList; + } + + @Test + public void tests() { + ArrayUtil s = new ArrayUtil(); + s.fibonacci(1); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String newString = ""; + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) + seperator = ""; + newString = newString + array[i] + seperator; + } + System.out.println("join:" + newString); + return null; + } + + public int[] sort(int[] newArray) { + int count = 0; + for (int i = 0; i < newArray.length; i++) { + for (int j = i; j < newArray.length; j++) { + if (newArray[i] > newArray[j]) { + int mid = newArray[i]; + newArray[i] = newArray[j]; + newArray[j] = mid; + } + } + } + System.out.println(count); + return newArray; + } + + @Test + public void testArray() { + // test reverseArray + ArrayUtil atl = new ArrayUtil(); + int[] origin = { 4, 0, 7, 0, 2, 9, 8 }; + System.out.println("娴嬭瘯鏁版嵁:" + Arrays.toString(origin)); + atl.reverseArray(origin); + // remove zero + atl.removeZero(origin); + // join + atl.join(origin, "-"); + // grow + atl.grow(origin, 4); + // merge + int[] array1 = { 3, 5, 7, 8 }; + int[] array2 = { 1, 7, 4, 8, 2 }; + atl.merge(array1, array2); + } + +} diff --git a/group23/609041842/src/com/homework02/LoginAction.java b/group23/609041842/src/com/homework02/LoginAction.java new file mode 100644 index 0000000000..ed4c5449e0 --- /dev/null +++ b/group23/609041842/src/com/homework02/LoginAction.java @@ -0,0 +1,42 @@ +package com.homework02; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * + * @author liuxin + * + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group23/609041842/src/com/homework02/Struts.java b/group23/609041842/src/com/homework02/Struts.java new file mode 100644 index 0000000000..88dc30bee9 --- /dev/null +++ b/group23/609041842/src/com/homework02/Struts.java @@ -0,0 +1,121 @@ +package com.homework02; + +import java.lang.reflect.Method; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; +import org.dom4j.tree.DefaultElement; +import org.junit.Test; + +public class Struts { + + private static final String ACTIN_NOT_FOUNT = "鏈壘鍒癆ction"; + private static final String RESULT_NOT_FOUNT = "鏈壘鍒癛esult"; + + public static View runAction(String actionName, Map parameters) { + + /* + * + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , + * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 + * {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters + * + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + * + */ + try { + String name = parameters.get("name"); + String password = parameters.get("password"); + String absoluteActionClassPath = getAbsoluteActionClassPath(actionName); + Class clazz; + clazz = Class.forName(absoluteActionClassPath); + + Object obj = clazz.newInstance(); + String setName = "name"; + String setPwd = "password"; + String prefix = "set"; + String setterName = prefix + setName.substring(0, 1).toUpperCase() + setName.substring(1, setName.length()); + String setterPwd = prefix + setPwd.substring(0, 1).toUpperCase() + setPwd.substring(1, setPwd.length()); + Method mnd = clazz.getMethod(setterName, String.class); + Method mtd = clazz.getMethod(setterPwd, String.class); + mnd.invoke(obj, name); + mtd.invoke(obj, password); + // 璋冨彇execute鑾峰彇杩斿洖鍊 + Method md = clazz.getMethod("execute"); + String mdResult = md.invoke(obj).toString(); + Method getMessageMethod = clazz.getMethod("getMessage"); + Object getMessage = getMessageMethod.invoke(obj); + String message = ""; + if (null != getMessage) { + message = getMessage.toString(); + } else { + System.out.println("messge涓虹┖"); + } + // 鑾峰彇xml涓殑result鍊 + String result = getResultValue(actionName, mdResult); + View view = new View(); + System.out.println(result); + view.setJsp(result); + parameters.put("message", message); + view.setParameters(parameters); + System.out.println("鎵撳嵃view缁撴灉-" + view.toString()); + return view; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + + } + + /** + * 鑾峰彇璇锋眰鐨刟ctionName鎵鍦ㄧ被缁濆璺緞 + * + * @param actionName + * @return + * @throws DocumentException + */ + public static String getAbsoluteActionClassPath(String actionName) throws DocumentException { + SAXReader reader = new SAXReader(); + Document doc = reader.read("src/struts.xml"); + List getActions = doc.selectNodes("/struts/action[@name='" + actionName + "']"); + for (DefaultElement e : getActions) { + return e.attributeValue("class"); + } + return ACTIN_NOT_FOUNT; + } + + @Test + public void test() { + try { + System.out.println(getAbsoluteActionClassPath("login")); + // System.out.println(getResultValue("login", "success")); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static String getResultValue(String actionName, String param) throws DocumentException { + SAXReader reader = new SAXReader(); + Document doc = reader.read("src/struts.xml"); + String path = "/struts/action[@name='" + actionName + "']/result[@name='" + param + "']"; + String result = doc.selectSingleNode(path).getText().toString(); + if (null == result) { + return RESULT_NOT_FOUNT; + } + return result; + } + +} diff --git a/group23/609041842/src/com/homework02/StrutsTest.java b/group23/609041842/src/com/homework02/StrutsTest.java new file mode 100644 index 0000000000..89c31f88bd --- /dev/null +++ b/group23/609041842/src/com/homework02/StrutsTest.java @@ -0,0 +1,38 @@ +package com.homework02; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = Struts.runAction(actionName, params); + System.out.println(view.getJsp()); + 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"); // 瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group23/609041842/src/com/homework02/View.java b/group23/609041842/src/com/homework02/View.java new file mode 100644 index 0000000000..2d37570a4f --- /dev/null +++ b/group23/609041842/src/com/homework02/View.java @@ -0,0 +1,32 @@ +package com.homework02; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } + + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } + +} diff --git a/group23/609041842/src/struts.xml b/group23/609041842/src/struts.xml new file mode 100644 index 0000000000..1d6fde9d6c --- /dev/null +++ b/group23/609041842/src/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group23/609041842/src/test.java b/group23/609041842/src/test.java new file mode 100644 index 0000000000..931a307711 --- /dev/null +++ b/group23/609041842/src/test.java @@ -0,0 +1,9 @@ +import org.junit.Test; + +public class test { + + @Test + public void test(){ + System.out.println("Heool"); + } +} From bc25d5ef3383d1077502de7b6019394d91abce1b Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 21:00:45 +0800 Subject: [PATCH 121/155] add BinaryTreeNode and testcase --- .../src/com/basic/BinaryTreeNode.java | 45 ++++++++++++++++++- .../src/testcase/TestBinaryTreeNode.java | 24 ++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java diff --git a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java index 9cf6755972..71c313b68a 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java +++ b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java @@ -6,6 +6,18 @@ public class BinaryTreeNode private Object data; private BinaryTreeNode left; private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + this.data = data; + left = null; + right = null; + } + + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } public Object getData() { @@ -37,9 +49,40 @@ public void setRight(BinaryTreeNode right) this.right = right; } + /* + * 鎺掑簭浜屽弶鏍戠殑鎻掑叆 + * */ public BinaryTreeNode insert(Object o) { - return null; + if ( ((Integer) data) > ((Integer) o) ) { + if (left == null) { + setLeft(new BinaryTreeNode(o)); + } else { + left.insert(o); + } + } else { + if (right == null) { + setRight(new BinaryTreeNode(o)); + } else { + right.insert(o); + } + } + return this; + } + + /* + * 鍓嶅簭閬嶅巻 + * */ + public void preOrderInterator() { + if (left != null) { + left.preOrderInterator(); + } + + System.out.print(data.toString() + " "); + + if (right != null) { + right.preOrderInterator(); + } } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java new file mode 100644 index 0000000000..6434d6880f --- /dev/null +++ b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java @@ -0,0 +1,24 @@ +package testcase; + +import org.junit.Test; + +import com.basic.BinaryTreeNode; + +public class TestBinaryTreeNode +{ + @Test + public void testBinaryTree() { + BinaryTreeNode binNode = new BinaryTreeNode(5); + binNode.insert(1); + binNode.insert(10); + binNode.insert(4); + binNode.insert(6); + binNode.insert(2); + binNode.insert(15); + binNode.insert(8); + + //1 2 4 5 6 8 10 15 + binNode.preOrderInterator(); + } + +} From bec6ad33f840836be1b352edb542477c2c4ada1b Mon Sep 17 00:00:00 2001 From: beyondself <1525619747@qq.com> Date: Sun, 19 Mar 2017 21:01:31 +0800 Subject: [PATCH 122/155] format codes --- .../src/com/basic/ArrayList.java | 2 +- .../src/com/basic/BinaryTreeNode.java | 57 ++-- .../src/com/basic/LinkedList.java | 243 +++++++++----- .../src/testcase/TestArrayList.java | 28 +- .../src/testcase/TestBinaryTreeNode.java | 9 +- .../src/testcase/TestLinkedList.java | 310 ++++++++++-------- .../src/testcase/TestQueue.java | 14 +- .../src/testcase/TestStack.java | 16 +- 8 files changed, 398 insertions(+), 281 deletions(-) diff --git a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java index bb540052cd..008b390255 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/ArrayList.java @@ -54,7 +54,7 @@ public Object remove(int index) + " is invalid, current range: 0 - " + (size - 1); throw new ArrayIndexOutOfBoundsException(errorInfo); } - + Object o = elementData[index]; for (int i = index; i < size - 1; i++) { diff --git a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java index 71c313b68a..63899d38ce 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java +++ b/group24/1525619747/homework_20170312/src/com/basic/BinaryTreeNode.java @@ -6,14 +6,17 @@ public class BinaryTreeNode private Object data; private BinaryTreeNode left; private BinaryTreeNode right; - - public BinaryTreeNode(Object data) { + + public BinaryTreeNode (Object data) + { this.data = data; left = null; right = null; } - - public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) { + + public BinaryTreeNode (Object data, BinaryTreeNode left, + BinaryTreeNode right) + { this.data = data; this.left = left; this.right = right; @@ -51,38 +54,50 @@ public void setRight(BinaryTreeNode right) /* * 鎺掑簭浜屽弶鏍戠殑鎻掑叆 - * */ + */ public BinaryTreeNode insert(Object o) { - if ( ((Integer) data) > ((Integer) o) ) { - if (left == null) { + if (((Integer) data) > ((Integer) o)) + { + if (left == null) + { setLeft(new BinaryTreeNode(o)); - } else { + } + else + { left.insert(o); - } - } else { - if (right == null) { + } + } + else + { + if (right == null) + { setRight(new BinaryTreeNode(o)); - } else { + } + else + { right.insert(o); - } + } } return this; } - + /* * 鍓嶅簭閬嶅巻 - * */ - public void preOrderInterator() { - if (left != null) { + */ + public void preOrderInterator() + { + if (left != null) + { left.preOrderInterator(); } - + System.out.print(data.toString() + " "); - - if (right != null) { + + if (right != null) + { right.preOrderInterator(); - } + } } } diff --git a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java index 2d9943e89b..e741421aad 100644 --- a/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java +++ b/group24/1525619747/homework_20170312/src/com/basic/LinkedList.java @@ -7,11 +7,15 @@ public class LinkedList implements List public void add(Object o) { - if (head == null) { + if (head == null) + { head = new Node(o, null); - } else { + } + else + { Node nodePointer = head; - while (nodePointer.next != null) { + while (nodePointer.next != null) + { nodePointer = nodePointer.next; } nodePointer.next = new Node(o, null); @@ -22,13 +26,16 @@ public void add(Object o) public void add(int index, Object o) { int size = size(); - if (index < 0 || index > size) { - String errorInfo = "Invalid index to add:" + index+ " out of range: [0," + size + "]"; + if (index < 0 || index > size) + { + String errorInfo = "Invalid index to add:" + index + + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); } int step = 0; Node nodePointer = head; - while (step < index) { + while (step < index) + { nodePointer = nodePointer.next; ++step; } @@ -38,62 +45,73 @@ public void add(int index, Object o) public Object get(int index) { int size = size(); - if (index < 0 || index > size) { - String errorInfo = "Invalid index to get:" + index+ " out of range: [0," + size + "]"; + if (index < 0 || index > size) + { + String errorInfo = "Invalid index to get:" + index + + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); } - + int step = 0; Node nodePointer = head; - while (step < index) { + while (step < index) + { nodePointer = nodePointer.next; ++step; } - + return nodePointer.data; } public Object remove(int index) { int size = size(); - if (index < 0 || index > size) { - String errorInfo = "Invalid index to remove:" + index+ " out of range: [0," + size + "]"; + if (index < 0 || index > size) + { + String errorInfo = "Invalid index to remove:" + index + + " out of range: [0," + size + "]"; throw new ArrayIndexOutOfBoundsException(errorInfo); } - + int step = 0; Node nodePointer = head; Node lastPointer = head; - - while (step < index) { + + while (step < index) + { lastPointer = nodePointer; nodePointer = nodePointer.next; ++step; } - + Object o = null; - - if (lastPointer == nodePointer) { + + if (lastPointer == nodePointer) + { Node toDelete = head; o = toDelete.data; head = head.next; toDelete = null; - } else { + } + else + { o = nodePointer.data; lastPointer.next = nodePointer.next; nodePointer = null; - } - + } + return o; } public int size() { int size = 0; - if (head != null) { + if (head != null) + { ++size; Node nodePointer = head; - while (nodePointer.next != null) { + while (nodePointer.next != null) + { ++size; nodePointer = nodePointer.next; } @@ -103,7 +121,8 @@ public int size() public void addFirst(Object o) { - if (head == null) { + if (head == null) + { head = new Node(o, null); return; } @@ -112,13 +131,15 @@ public void addFirst(Object o) public void addLast(Object o) { - if (head == null) { + if (head == null) + { head = new Node(o, null); return; } - + Node nodePointer = head; - while (nodePointer.next != null) { + while (nodePointer.next != null) + { nodePointer = nodePointer.next; } nodePointer.next = new Node(o, null); @@ -127,44 +148,48 @@ public void addLast(Object o) @SuppressWarnings ("unused") public Object removeFirst() { - if (head == null) { + if (head == null) + { return null; } - + Node toDelete = head; Object o = head.data; head = head.next; toDelete = null; - + return o; } public Object removeLast() { - if (head == null) { + if (head == null) + { return null; } Node nodePointer = head; Node lastPointer = head; - - while (nodePointer.next != null) { + + while (nodePointer.next != null) + { lastPointer = nodePointer; nodePointer = nodePointer.next; } lastPointer.next = null; Object o = nodePointer.data; nodePointer = null; - + return o; } public Iterator iterator() { - return new Iterator() { - + return new Iterator() + { + private Node nodePointer = head; - + public boolean hasNext() { // TODO Auto-generated method stub @@ -174,13 +199,14 @@ public boolean hasNext() public Object next() { // TODO Auto-generated method stub - if (hasNext()) { + if (hasNext()) + { Object o = nodePointer.data; nodePointer = nodePointer.next; return o; } return null; - } + } }; } @@ -188,7 +214,9 @@ private static class Node { Object data; Node next; - public Node(Object o, Node n) { + + public Node (Object o, Node n) + { this.data = o; this.next = n; } @@ -199,19 +227,20 @@ public Node(Object o, Node n) { */ public void reverse() { - if (head == null) { + if (head == null) + { return; } - + Node reverse = null; - while (size() > 0) { + while (size() > 0) + { reverse = new Node(removeFirst(), reverse); } - + head = reverse; } - /** * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 濡傛灉list = 2->5->7->8->10 * ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 @@ -222,7 +251,8 @@ public void removeFirstHalf() int removeLength = size() / 2; int step = 0; Node toDelete = null; - while (step < removeLength) { + while (step < removeLength) + { toDelete = head; head = head.next; toDelete = null; @@ -239,29 +269,36 @@ public void removeFirstHalf() public void remove(int i, int length) { int size = size(); - if (i >= size) { + if (i >= size) + { return; } Node nodePointer = head; Node lastPointer = head; int step = 0; - while (step < i) { + while (step < i) + { lastPointer = nodePointer; nodePointer = nodePointer.next; ++step; } - + step = 0; Node toDelete = null; - if (lastPointer == head) { - while (step < length) { + if (lastPointer == head) + { + while (step < length) + { toDelete = head; head = head.next; toDelete = null; ++step; } - } else { - while (step < length) { + } + else + { + while (step < length) + { toDelete = nodePointer; nodePointer = nodePointer.next; toDelete = null; @@ -269,7 +306,7 @@ public void remove(int i, int length) } lastPointer.next = nodePointer; } - + } /** @@ -284,7 +321,8 @@ public int[] getElements(LinkedList list) int[] elements = new int[list.size()]; Iterator it = list.iterator(); int index = 0; - for ( ; it.hasNext(); ) { + for (; it.hasNext();) + { elements[index++] = (Integer) get((Integer) (it.next())); } return elements; @@ -298,36 +336,45 @@ public int[] getElements(LinkedList list) public void subtract(LinkedList list) { - if (head == null) { + if (head == null) + { return; } Node nodePointer = head; Node lastPointer = head; Node toDelte = null; - while (nodePointer != null) { - if (list.contain(nodePointer.data)) { - if (nodePointer == head) { + while (nodePointer != null) + { + if (list.contain(nodePointer.data)) + { + if (nodePointer == head) + { toDelte = head; head = head.next; toDelte = null; nodePointer = head; lastPointer = head; - } else { + } + else + { toDelte = nodePointer; lastPointer.next = nodePointer.next; toDelte = null; } } lastPointer = nodePointer; - nodePointer = nodePointer.next; + nodePointer = nodePointer.next; } - + } - - private boolean contain(Object o) { + + private boolean contain(Object o) + { Node nodePointer = head; - while (nodePointer != null) { - if (nodePointer.data.equals(o)) { + while (nodePointer != null) + { + if (nodePointer.data.equals(o)) + { return true; } nodePointer = nodePointer.next; @@ -341,13 +388,17 @@ private boolean contain(Object o) { public void removeDuplicateValues() { Node nodePointer = head; - if (nodePointer.next == null) { + if (nodePointer.next == null) + { return; } - + Node toDelete = null; - while (nodePointer.next != null) { - while (nodePointer.next != null && nodePointer.data.equals(nodePointer.next.data)) { // delete nodePointer.next + while (nodePointer.next != null) + { + while (nodePointer.next != null + && nodePointer.data.equals(nodePointer.next.data)) + { // delete nodePointer.next toDelete = nodePointer.next; nodePointer.next = nodePointer.next.next; toDelete = null; @@ -366,22 +417,30 @@ public void removeRange(int min, int max) { Node nodePointer = head; Node lastPointer = head; - if (nodePointer == null) { + if (nodePointer == null) + { return; } - while (nodePointer != null && ((Integer) nodePointer.data) <= (new Integer(min))) { + while (nodePointer != null + && ((Integer) nodePointer.data) <= (new Integer(min))) + { lastPointer = nodePointer; nodePointer = nodePointer.next; } Node toDelete = null; - while (nodePointer != null && ((Integer) nodePointer.data) < (new Integer(max))) { - if (nodePointer == head) { + while (nodePointer != null + && ((Integer) nodePointer.data) < (new Integer(max))) + { + if (nodePointer == head) + { toDelete = head; head = head.next; toDelete = null; nodePointer = head; lastPointer = head; - } else { + } + else + { toDelete = nodePointer; lastPointer.next = nodePointer.next; nodePointer = nodePointer.next; @@ -403,28 +462,36 @@ public LinkedList intersection(LinkedList list) Iterator it2 = list.iterator(); Object o1 = null; Object o2 = null; - - if (size() == 0 || list.size() == 0) { + + if (size() == 0 || list.size() == 0) + { return null; } - + o1 = it1.next(); o2 = it2.next(); - - while (o1 != null && o2 != null) { -// System.out.println(o1 + " " + o2); - if (((Integer) o1) == ((Integer) o2)) { + + while (o1 != null && o2 != null) + { + // System.out.println(o1 + " " + o2); + if (((Integer) o1) == ((Integer) o2)) + { linkedList.add(o1); o1 = it1.next(); o2 = it2.next(); - } else { - if (((Integer) o1) > ((Integer) o2)) { + } + else + { + if (((Integer) o1) > ((Integer) o2)) + { o2 = it2.next(); - } else { + } + else + { o1 = it1.next(); } - } - } + } + } return linkedList; } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java index 4cf2bf1a62..134a541265 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestArrayList.java @@ -33,7 +33,7 @@ public void testAdd() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); } @@ -54,19 +54,19 @@ public void testIndexAdd() } list.add(3, 20); list.add(0, 30); - + } catch (ArrayIndexOutOfBoundsException e) { assertNull(e); } - -// Iterator it = list.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); - + + // Iterator it = list.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); + assertTrue(list.get(0).equals(30)); assertTrue(list.get(4).equals(20)); assertTrue(list.get(5).equals(3)); @@ -81,7 +81,7 @@ public void testIndexAdd() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); assertEquals("100", e.getMessage()); } } @@ -120,8 +120,8 @@ public void testGet() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); -// System.out.println(list.size()); + // System.out.println(e.getMessage()); + // System.out.println(list.size()); String errorInfo = "Index to get: 10 is invalid, current range: 0 - " + (list.size() - 1); assertEquals(errorInfo, e.getMessage()); @@ -159,7 +159,7 @@ public void testRemove() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); } try @@ -169,7 +169,7 @@ public void testRemove() catch (ArrayIndexOutOfBoundsException e) { assertNotNull(e); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); } } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java index 6434d6880f..472b9a7fb0 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestBinaryTreeNode.java @@ -7,7 +7,8 @@ public class TestBinaryTreeNode { @Test - public void testBinaryTree() { + public void testBinaryTree() + { BinaryTreeNode binNode = new BinaryTreeNode(5); binNode.insert(1); binNode.insert(10); @@ -16,9 +17,9 @@ public void testBinaryTree() { binNode.insert(2); binNode.insert(15); binNode.insert(8); - - //1 2 4 5 6 8 10 15 + + // 1 2 4 5 6 8 10 15 binNode.preOrderInterator(); } - + } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java index 54cf229718..05ac118a90 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestLinkedList.java @@ -12,126 +12,149 @@ public class TestLinkedList /* * test add() and size() - * */ - @Test - public void testAdd() { + */ + @Test + public void testAdd() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); assertTrue(linkedList.size() == 2); } - @Test - public void testGet() { + public void testGet() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); assertTrue(linkedList.get(0).equals(3)); assertFalse(linkedList.get(0).equals("hello world")); assertTrue(linkedList.get(1).equals("hello world")); - - try { + + try + { linkedList.get(-1); - } catch (ArrayIndexOutOfBoundsException e) { + } + catch (ArrayIndexOutOfBoundsException e) + { assertNotNull(e); - String errorInfo = "Invalid index to get:" + -1 + " out of range: [0," + linkedList.size() + "]"; + String errorInfo = "Invalid index to get:" + -1 + + " out of range: [0," + linkedList.size() + "]"; assertTrue(e.getMessage().equals(errorInfo)); -// System.out.println(e.getMessage()); + // System.out.println(e.getMessage()); } } - @Test - public void testRemove() { + public void testRemove() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); linkedList.add(4); linkedList.add("Leon Deng"); - try { + try + { linkedList.remove(-1); - } catch (ArrayIndexOutOfBoundsException e) { + } + catch (ArrayIndexOutOfBoundsException e) + { assertNotNull(e); - String errorInfo = "Invalid index to remove:" + -1 + " out of range: [0," + linkedList.size() + "]"; + String errorInfo = "Invalid index to remove:" + -1 + + " out of range: [0," + linkedList.size() + "]"; assertTrue(e.getMessage().equals(errorInfo)); } - - try { + + try + { linkedList.remove(10); - } catch (ArrayIndexOutOfBoundsException e) { + } + catch (ArrayIndexOutOfBoundsException e) + { assertNotNull(e); - String errorInfo = "Invalid index to remove:" + 10 + " out of range: [0," + linkedList.size() + "]"; + String errorInfo = "Invalid index to remove:" + 10 + + " out of range: [0," + linkedList.size() + "]"; assertTrue(e.getMessage().equals(errorInfo)); } - - Object o = null; - try { + + Object o = null; + try + { o = linkedList.remove(0); - } catch (ArrayIndexOutOfBoundsException e) { - assertNull(e); - } + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } assertTrue(o.equals(3)); - - try { + + try + { o = linkedList.remove(2); - } catch (ArrayIndexOutOfBoundsException e) { - assertNull(e); - } -// System.out.println(o); + } + catch (ArrayIndexOutOfBoundsException e) + { + assertNull(e); + } + // System.out.println(o); assertTrue(o.equals("Leon Deng")); } - @Test - public void testAddFirst() { + public void testAddFirst() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); - + linkedList.addFirst("Leon Deng"); assertTrue(linkedList.get(0).equals("Leon Deng")); assertTrue(linkedList.get(1).equals(3)); } @Test - public void testAddLast() { + public void testAddLast() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); - + linkedList.addLast("Leon Deng"); assertTrue(linkedList.get(linkedList.size() - 1).equals("Leon Deng")); } - + @Test - public void testRemoveFirst() { + public void testRemoveFirst() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); - + Object o = linkedList.removeFirst(); assertTrue(o.equals(3)); o = linkedList.removeFirst(); assertTrue(o.equals("hello world")); } - + @Test - public void testRemoveLast() { + public void testRemoveLast() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("hello world"); linkedList.add("Leon Deng"); - + Object o = linkedList.removeLast(); assertTrue(o.equals("Leon Deng")); o = linkedList.removeLast(); assertTrue(o.equals("hello world")); } - + @Test - public void testInterator() { + public void testInterator() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add("Leon Deng"); @@ -143,78 +166,81 @@ public void testInterator() { assertTrue(it.next().equals("Leon Deng")); assertFalse(it.hasNext()); } - + @Test - public void testReverse() { + public void testReverse() + { LinkedList linkedList = new LinkedList(); linkedList.add(3); linkedList.add(4); linkedList.add(5); linkedList.add(6); -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); -// -// linkedList.reverse(); -// -// it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); - + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); + // + // linkedList.reverse(); + // + // it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); + Object o1 = linkedList.get(0); Object o2 = linkedList.get(linkedList.size() - 1); - + linkedList.reverse(); - + Object o3 = linkedList.get(0); Object o4 = linkedList.get(linkedList.size() - 1); assertEquals(o1, o4); assertEquals(o2, o3); - + linkedList.reverse(); Object o5 = linkedList.get(0); Object o6 = linkedList.get(linkedList.size() - 1); - + assertEquals(o1, o5); assertEquals(o2, o6); } - + @Test - public void testRemoveFirstHalf() { + public void testRemoveFirstHalf() + { LinkedList linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); linkedList.add(7); linkedList.add(8); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); - + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); + linkedList.removeFirstHalf(); assertTrue(linkedList.get(0).equals(7)); - + linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); linkedList.add(7); linkedList.add(8); linkedList.add(10); - + linkedList.removeFirstHalf(); assertTrue(linkedList.get(2).equals(10)); } - + @Test - public void testRemoveIndexLength() { + public void testRemoveIndexLength() + { LinkedList linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); @@ -222,15 +248,15 @@ public void testRemoveIndexLength() { linkedList.add(8); linkedList.add(10); - linkedList.remove(0,2); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + linkedList.remove(0, 2); + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertTrue(linkedList.get(0).equals(7)); - + linkedList = new LinkedList(); linkedList.add(2); linkedList.add(5); @@ -238,15 +264,16 @@ public void testRemoveIndexLength() { linkedList.add(8); linkedList.add(10); - linkedList.remove(2,2); + linkedList.remove(2, 2); assertTrue(linkedList.get(0).equals(2)); assertTrue(linkedList.get(2).equals(10)); - + } - + @Test - public void testGetElements() { -// 11->101->201->301->401->501->601->701 listB = 1->3->4->6 + public void testGetElements() + { + // 11->101->201->301->401->501->601->701 listB = 1->3->4->6 LinkedList linkedList = new LinkedList(); linkedList.add(11); linkedList.add(101); @@ -256,29 +283,29 @@ public void testGetElements() { linkedList.add(501); linkedList.add(601); linkedList.add(701); - + LinkedList indexLinkedList = new LinkedList(); indexLinkedList.add(1); indexLinkedList.add(3); indexLinkedList.add(4); indexLinkedList.add(6); - + int[] elements = linkedList.getElements(indexLinkedList); - -// for (int i = 0; i < elements.length; i++) { -// System.out.print(elements[i] + " "); -// } -// System.out.println(); + + // for (int i = 0; i < elements.length; i++) { + // System.out.print(elements[i] + " "); + // } + // System.out.println(); assertEquals(elements[0], linkedList.get(1)); assertEquals(elements[1], linkedList.get(3)); assertEquals(elements[2], linkedList.get(4)); - assertEquals(elements[3], linkedList.get(6)); + assertEquals(elements[3], linkedList.get(6)); } - - + @Test - public void testSubstract() { + public void testSubstract() + { LinkedList linkedList = new LinkedList(); linkedList.add(11); linkedList.add(101); @@ -288,7 +315,6 @@ public void testSubstract() { linkedList.add(501); linkedList.add(601); linkedList.add(701); - LinkedList substractList = new LinkedList(); substractList.add(11); @@ -296,21 +322,22 @@ public void testSubstract() { substractList.add(404); substractList.add(501); substractList.add(701); - + linkedList.subtract(substractList); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertFalse(linkedList.get(0).equals(11)); assertTrue(linkedList.get(0).equals(101)); } - + @Test - public void testRemoveDuplicateValues() { + public void testRemoveDuplicateValues() + { LinkedList linkedList = new LinkedList(); linkedList.add(1); linkedList.add(1); @@ -320,41 +347,44 @@ public void testRemoveDuplicateValues() { linkedList.add(4); linkedList.add(4); linkedList.add(5); - + linkedList.removeDuplicateValues(); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertTrue(linkedList.get(0).equals(1)); assertTrue(linkedList.get(1).equals(2)); assertTrue(linkedList.get(4).equals(5)); - + } - + @Test - public void testRemoveRange() { + public void testRemoveRange() + { LinkedList linkedList = new LinkedList(); - for (int i = 0; i < 10; i++) { - linkedList.add(i); + for (int i = 0; i < 10; i++) + { + linkedList.add(i); } linkedList.removeRange(3, 7); - -// Iterator it = linkedList.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + + // Iterator it = linkedList.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertTrue(linkedList.get(3).equals(3)); - assertTrue(linkedList.get(4).equals(7)); + assertTrue(linkedList.get(4).equals(7)); } - + @Test - public void testIntersection() { + public void testIntersection() + { LinkedList linkedList = new LinkedList(); linkedList.add(1); linkedList.add(2); @@ -362,23 +392,23 @@ public void testIntersection() { linkedList.add(4); linkedList.add(5); linkedList.add(6); - + LinkedList secondList = new LinkedList(); secondList.add(1); secondList.add(3); secondList.add(5); secondList.add(6); - + LinkedList intersection = linkedList.intersection(secondList); -// Iterator it = intersection.iterator(); -// for ( ; it.hasNext(); ) { -// System.out.print(it.next() + " "); -// } -// System.out.println(); + // Iterator it = intersection.iterator(); + // for ( ; it.hasNext(); ) { + // System.out.print(it.next() + " "); + // } + // System.out.println(); assertTrue(intersection.get(0).equals(1)); assertTrue(intersection.get(1).equals(3)); assertTrue(intersection.get(2).equals(5)); assertTrue(intersection.get(3).equals(6)); } - + } diff --git a/group24/1525619747/homework_20170312/src/testcase/TestQueue.java b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java index a08ee80951..390238efc9 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestQueue.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestQueue.java @@ -10,17 +10,19 @@ public class TestQueue { /* * test enQueue() isEmpty() and size() deQueue - * */ + */ @Test - public void testQueue() { + public void testQueue() + { Queue queue = new Queue(); - + assertTrue(queue.isEmpty()); - - for (int i = 10; i < 20; i++) { + + for (int i = 10; i < 20; i++) + { queue.enQueue(i); } - + assertFalse(queue.isEmpty()); assertTrue(queue.size() == 10); diff --git a/group24/1525619747/homework_20170312/src/testcase/TestStack.java b/group24/1525619747/homework_20170312/src/testcase/TestStack.java index 58c7206fea..2091577ec1 100644 --- a/group24/1525619747/homework_20170312/src/testcase/TestStack.java +++ b/group24/1525619747/homework_20170312/src/testcase/TestStack.java @@ -9,21 +9,23 @@ public class TestStack { @Test - public void testStack() { + public void testStack() + { Stack stack = new Stack(); - + assertTrue(stack.isEmpty()); - - for (int i = 10; i < 20; i++) { + + for (int i = 10; i < 20; i++) + { stack.push(i); } - + assertFalse(stack.isEmpty()); assertTrue(stack.size() == 10); - + assertTrue(stack.peek().equals(19)); assertFalse(stack.peek().equals(10)); - + assertTrue(stack.pop().equals(19)); assertTrue(stack.pop().equals(18)); } From 8b04bbf2649dab58be0476fd3903ac35346bd294 Mon Sep 17 00:00:00 2001 From: xiongrui233 Date: Sun, 19 Mar 2017 22:13:02 +0800 Subject: [PATCH 123/155] complete ArrayUtil --- .../src/com/coderising/array/ArrayUtil.java | 164 ++++++++++++++---- .../com/coderising/array/TestArrayUtil.java | 69 ++++++++ 2 files changed, 202 insertions(+), 31 deletions(-) create mode 100644 group22/627559964/test/com/coderising/array/TestArrayUtil.java diff --git a/group22/627559964/src/com/coderising/array/ArrayUtil.java b/group22/627559964/src/com/coderising/array/ArrayUtil.java index f4b5e8649b..e97b5f7a7c 100644 --- a/group22/627559964/src/com/coderising/array/ArrayUtil.java +++ b/group22/627559964/src/com/coderising/array/ArrayUtil.java @@ -1,96 +1,198 @@ package com.coderising.array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + public class ArrayUtil { /** - * 缁欏畾涓?涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫?艰繘琛岀疆鎹? - 渚嬪锛? a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ - + public static int[] reverseArray(int[] origin){ + int size = origin.length; + int[] result = new int[size]; + for (int i = 0; i < size; i++) { + int temp = origin[size-1-i]; + result[i] = temp; + } + return result; } /** - * 鐜板湪鏈夊涓嬬殑涓?涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫?煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 * {1,3,4,5,6,6,5,4,7,6,7,5} * @param oldArray * @return */ - public int[] removeZero(int[] oldArray){ - return null; + public static int[] removeZero(int[] oldArray){ + int size = oldArray.length; + int countZero = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + countZero ++; + } + } + int[] result = new int[size - countZero]; + for (int i = 0, j = 0; i < size; i++) { + int temp = oldArray[i]; + if (temp != 0) { + result[j] = temp; + j ++; + } + } + return result; } /** - * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛? a1鍜宎2 , 鍒涘缓涓?涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 - * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒? a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛? 宸茬粡娑堥櫎浜嗛噸澶? + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ - return null; + public static int[] merge(int[] array1, int[] array2){ + int[] result = new int[array1.length + array2.length]; + System.arraycopy(array1, 0, result, 0, array1.length); + System.arraycopy(array2, 0, result, array1.length, array2.length); + Arrays.sort(result); + return result; } /** * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size - * 娉ㄦ剰锛岃?佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸? + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 * [2,3,6,0,0,0] * @param oldArray * @param size * @return */ - public int[] grow(int [] oldArray, int size){ - return null; + public static int[] grow(int[] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; } /** - * 鏂愭尝閭e鏁板垪涓猴細1锛?1锛?2锛?3锛?5锛?8锛?13锛?21...... 锛岀粰瀹氫竴涓渶澶у?硷紝 杩斿洖灏忎簬璇ュ?肩殑鏁板垪 - * 渚嬪锛? max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓? [1锛?1锛?2锛?3锛?5锛?8锛?13] + * 鏂愭尝閭e鏁板垪涓猴細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){ - return null; + public static int[] fibonacci(int max){ + List resultList = new ArrayList(); + int temp = 0; + for (int i = 1; i <= max; i++) { + temp = getFibo(i); + if (temp > max) { + break; + } + resultList.add(temp); + } + return getArrayFromList(resultList); + } + /** + * 璁$畻鏂愭尝閭e鏁板垪 + * @param i + * @return + */ + private static int getFibo(int i) { + if (i == 1 || i == 2) { + return 1; + } else { + return getFibo(i - 1) + getFibo(i - 2); + } + } + + /** + * 閫氳繃list杩囧緱int[]鏁扮粍 + * @param list + * @return + */ + private static int[] getArrayFromList(List list) { + int[] result = new int[list.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = list.get(i); + } + return result; } /** - * 杩斿洖灏忎簬缁欏畾鏈?澶у?糾ax鐨勬墍鏈夌礌鏁版暟缁? + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] * @param max * @return */ - public int[] getPrimes(int max){ - return null; + public static int[] getPrimes(int max){ + List result = new ArrayList(); + boolean isPrime = true; + for (int i = 2; i < max; i++) { + for (int j = 2; j < i; j++) { + if (i % j == 0) { + isPrime = false; + break; + } + } + if (isPrime) { + result.add(i); + } + isPrime = true; + } + + return getArrayFromList(result); } /** - * 鎵?璋撯?滃畬鏁扳?濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 - * 缁欏畾涓?涓渶澶у?糾ax锛? 杩斿洖涓?涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁? + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 * @param max * @return */ - public int[] getPerfectNumbers(int max){ - return null; + public static int[] getPerfectNumbers(int max){ + List result = new ArrayList(); + for (int i = 1; i <= max; i++) { + int temp = 0; + for (int j = 1; j < i/2 + 1; j++) { + if(i%j == 0) { + temp += j; + } + } + if (temp == i) { + result.add(i); + } + } + return getArrayFromList(result); } /** - * 鐢╯eperator 鎶婃暟缁? array缁欒繛鎺ヨ捣鏉? + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 * 渚嬪array= [3,8,9], seperator = "-" - * 鍒欒繑鍥炲?间负"3-8-9" + * 鍒欒繑鍥炲间负"3-8-9" * @param array * @param s * @return */ - public String join(int[] array, String seperator){ - return null; + public static String join(int[] array, String seperator){ + List result = Arrays.asList(array); + int size = array.length; + StringBuffer rs = new StringBuffer(); + for (int i = 0; i < size; i++) { + rs.append(result.get(0)[i]); + if (i != size-1) { + rs.append(seperator); + } + } + return rs.toString(); } -} +} \ No newline at end of file diff --git a/group22/627559964/test/com/coderising/array/TestArrayUtil.java b/group22/627559964/test/com/coderising/array/TestArrayUtil.java new file mode 100644 index 0000000000..f597900492 --- /dev/null +++ b/group22/627559964/test/com/coderising/array/TestArrayUtil.java @@ -0,0 +1,69 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Test; + +import com.sun.scenario.effect.Merge; + +public class TestArrayUtil { + + @Test + public void testReverseArray() { + try { + int[] number = {1,2,3}; + Assert.assertEquals("[3, 2, 1]", Arrays.toString(ArrayUtil.reverseArray(number))); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testRemoveZero() { + int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + Assert.assertEquals("[1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5]", Arrays.toString(ArrayUtil.removeZero(oldArr))); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + Assert.assertEquals("[3, 4, 5, 5, 6, 7, 7, 8]", Arrays.toString(ArrayUtil.merge(a1, a2))); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int size = 3; + Assert.assertEquals("[2, 3, 6, 0, 0, 0]", Arrays.toString(ArrayUtil.grow(oldArray, size))); + } + + @Test + public void testFibonacci() { + int max = 15; + Assert.assertEquals("[1, 1, 2, 3, 5, 8, 13]", Arrays.toString(ArrayUtil.fibonacci(max))); + } + + @Test + public void testGetPrimes() { + int max = 23; + System.out.println(Arrays.toString(ArrayUtil.getPrimes(max))); + Assert.assertEquals("[2, 3, 5, 7, 11, 13, 17, 19]", Arrays.toString(ArrayUtil.getPrimes(max))); + } + + @Test + public void testGetPerfectNumbers() { + Assert.assertEquals("[6, 28, 496]", Arrays.toString(ArrayUtil.getPerfectNumbers(1000))); + } + + @Test + public void testJoin() { + int[] array= {3,8,9}; + String seperator = "-"; + Assert.assertEquals("3-8-9", ArrayUtil.join(array, seperator)); + } +} From 771b304c9b96300e2673deeb414f92f9220104ef Mon Sep 17 00:00:00 2001 From: DonaldY <448641125@qq.com> Date: Sun, 19 Mar 2017 22:38:50 +0800 Subject: [PATCH 124/155] Second Week --- .../com/donaldy/litestruts/struts.xml | 11 + .../src/com/donaldy/basic/ArrayUtil.java | 206 ++++++++++++++++++ .../com/donaldy/litestruts/LoginAction.java | 43 ++++ .../src/com/donaldy/litestruts/Struts.java | 138 ++++++++++++ .../com/donaldy/litestruts/StrutsTest.java | 39 ++++ .../src/com/donaldy/litestruts/View.java | 24 ++ .../src/com/donaldy/litestruts/struts.xml | 11 + .../src/com/donaldy/test/ArrayUtilTest.java | 92 ++++++++ 8 files changed, 564 insertions(+) create mode 100644 group24/448641125/out/production/448641125/com/donaldy/litestruts/struts.xml create mode 100644 group24/448641125/src/com/donaldy/basic/ArrayUtil.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/LoginAction.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/Struts.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/StrutsTest.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/View.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/struts.xml create mode 100644 group24/448641125/src/com/donaldy/test/ArrayUtilTest.java diff --git a/group24/448641125/out/production/448641125/com/donaldy/litestruts/struts.xml b/group24/448641125/out/production/448641125/com/donaldy/litestruts/struts.xml new file mode 100644 index 0000000000..823895e9bc --- /dev/null +++ b/group24/448641125/out/production/448641125/com/donaldy/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/448641125/src/com/donaldy/basic/ArrayUtil.java b/group24/448641125/src/com/donaldy/basic/ArrayUtil.java new file mode 100644 index 0000000000..bbe0f57db3 --- /dev/null +++ b/group24/448641125/src/com/donaldy/basic/ArrayUtil.java @@ -0,0 +1,206 @@ +package com.donaldy.basic; + +import java.util.ArrayList; + +public class ArrayUtil { + + private java.util.ArrayList arrayList; + private int[] newArr; + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + makeSure(origin); + int length = origin.length - 1; + int [] newArr = new int[length + 1]; + + for (int i = 0; i <= length; ++i) + newArr[i] = origin[length - i]; + + for (int i = 0 ; i <= length; ++i) + origin[i] = newArr[i]; + + } + + private void makeSure(int [] arr) { + if (arr.length == 0 || arr == null) + throw new RuntimeException(); + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + makeSure(oldArray); + int length = oldArray.length; + arrayList = new ArrayList(length); + for (int i = 0; i < length ; ++i) + if (oldArray[i] != 0) + arrayList.add(oldArray[i]); + + return toArray(arrayList.size()); + } + + private int[] toArray(int length) { + newArr = new int[length]; + for (int i = 0; i < length ; ++i) + newArr[i] = (int)arrayList.get(i); + return newArr; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 + * 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + makeSure(array1); + makeSure(array2); + int length1 = array1.length; + int length2 = array2.length; + arrayList = new ArrayList(length1 + length2); + int i, j, cnt = 0, temp; + boolean flag = false; + for (i = 0, j = 0; i < length1 && j < length2; ) { + if (array1[i] < array2[j]) { + temp = array1[i]; + i ++; + } else { + temp = array2[j]; + j ++; + } + if (flag && temp != (int)arrayList.get(cnt)) { + arrayList.add(temp); + cnt ++; + } else if (!flag){ + arrayList.add(temp); + flag = true; + } + + } + for (i += 1; i < length1; ++i) + arrayList.add(array1[i]); + for (j += 1; j < length2; ++j) + arrayList.add(array2[j]); + + return toArray(arrayList.size()); + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size) { + int length = oldArray.length + size; + newArr = new int[length]; + int i = 0; + for (; i < oldArray.length ; ++i) + newArr[i] = oldArray[i]; + for (; i < length; ++i) + newArr[i] = 0; + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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) { + arrayList = new ArrayList(); + arrayList.add(1); + arrayList.add(1); + int maxn = 2; + int i = 1; + while (maxn < max) { + arrayList.add(maxn); + i ++; + maxn = (int)arrayList.get(i) + (int)arrayList.get(i - 1); + } + ; + return toArray(i + 1); + } + + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + arrayList = new ArrayList(); + boolean prime[] = new boolean[max + 1]; + int p = 0; + for (int i = 0; i < max; ++i) + prime[i] = true; + prime[0] = prime[1] = false; + for (int i = 2; i < max; ++i) { + if (prime[i]) { + arrayList.add(p ++, i); + for (int j = 2 * i; j < max; j += i) + prime[j] = false; + } + } + return toArray(arrayList.size()); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + arrayList = new ArrayList(); + int sum; + for (int i = 1; i < max; ++i) { + sum = 0; + for (int j = 1; j < i; ++j) { + if (i % j == 0) + sum += j; + } + if (sum == i) + arrayList.add(sum); + } + return toArray(arrayList.size()); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(array[0]); + for (int i = 1; i < array.length; ++i) { + stringBuilder.append(seperator + array[i]); + } + return stringBuilder.toString(); + } + + +} diff --git a/group24/448641125/src/com/donaldy/litestruts/LoginAction.java b/group24/448641125/src/com/donaldy/litestruts/LoginAction.java new file mode 100644 index 0000000000..17d1842b09 --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/LoginAction.java @@ -0,0 +1,43 @@ +package com.donaldy.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public LoginAction() { + + } + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group24/448641125/src/com/donaldy/litestruts/Struts.java b/group24/448641125/src/com/donaldy/litestruts/Struts.java new file mode 100644 index 0000000000..48cb164edc --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/Struts.java @@ -0,0 +1,138 @@ +package com.donaldy.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.regex.Pattern; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + Element rootElement = null; + try { + /** + * 0.璇诲彇閰嶇疆鏂囦欢 + */ + rootElement = readStrutsXml().getRootElement(); + } catch (DocumentException e) { + e.printStackTrace(); + } + + /** + * 1.鏍规嵁actionName鎵惧埌class + * 骞惰缃 + */ + String classPath = findClass(actionName, rootElement); + + return handle(classPath, parameters, rootElement); + } + + private static Document readStrutsXml() throws DocumentException { + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read( + new File("D:\\tools\\Code\\Y_Repository\\coding2017\\group24\\448641125\\src\\com\\donaldy" + + "\\litestruts\\struts.xml")); + + return document; + } + + private static String findClass(String actionName, Element root) { + String classPath = null; + for (Iterator i = root.elementIterator(); i.hasNext(); ) { + Element action = (Element) i.next(); + if (actionName.equals(action.attribute("name").getText())) { + classPath = action.attribute("class").getText(); + break; + } + } + return classPath; + } + + private static View handle(String classPath, Map parameters + , Element rootElement) { + View view = new View(); + + Class newClass = getClass(classPath); + Object action = getObject(newClass); + Element element = rootElement.element("action"); + + + if (action instanceof LoginAction) { + LoginAction loginAction = (LoginAction) getAction(action, parameters); + String answer = loginAction.execute(); + String page = getPage(element, answer); + + view.setJsp(page); + view.setParameters(getMap(newClass, action)); + } + + return view; + } + + private static Class getClass(String classPath) { + try { + return Class.forName(classPath); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } + + private static Object getObject(Class newClass) { + try { + return newClass.newInstance(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + + private static Object getAction(Object action, Map parameters) { + LoginAction loginAction = (LoginAction) action; + loginAction.setName(parameters.get("name")); + loginAction.setPassword(parameters.get("password")); + return loginAction; + } + + private static String getPage(Element element, String answer) { + for (Iterator i = element.elementIterator(); i.hasNext(); ) { + Element result = (Element) i.next(); + if (answer.equals(result.attribute("name").getText())) { + return result.getText(); + } + } + return ""; + } + + private static Map getMap(Class newClass, Object action) { + Map map = new HashMap<>(); + Method[] methods = newClass.getDeclaredMethods(); + String getterMethod; + for (Method method : methods) { + getterMethod = method.getName(); + if (Pattern.matches("get(\\w+)", getterMethod)) { + try { + map.put(getterMethod.substring(3).toLowerCase(), + method.invoke(action).toString()); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + } + return map; + } + +} diff --git a/group24/448641125/src/com/donaldy/litestruts/StrutsTest.java b/group24/448641125/src/com/donaldy/litestruts/StrutsTest.java new file mode 100644 index 0000000000..6e17b7483c --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.donaldy.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group24/448641125/src/com/donaldy/litestruts/View.java b/group24/448641125/src/com/donaldy/litestruts/View.java new file mode 100644 index 0000000000..1676cf90da --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/View.java @@ -0,0 +1,24 @@ +package com.donaldy.litestruts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/448641125/src/com/donaldy/litestruts/struts.xml b/group24/448641125/src/com/donaldy/litestruts/struts.xml new file mode 100644 index 0000000000..823895e9bc --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/448641125/src/com/donaldy/test/ArrayUtilTest.java b/group24/448641125/src/com/donaldy/test/ArrayUtilTest.java new file mode 100644 index 0000000000..b457e108da --- /dev/null +++ b/group24/448641125/src/com/donaldy/test/ArrayUtilTest.java @@ -0,0 +1,92 @@ +package com.donaldy.test; + +import com.donaldy.basic.ArrayUtil; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +/** + * Created by donal on 2017/3/13. + */ +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + + @Before + public void before() { + arrayUtil = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int []A = {7, 9, 30, 3}; + int []B = {7, 9, 30, 3, 4}; + arrayUtil.reverseArray(A); + assertArrayEquals(new int[] {3, 30, 9, 7}, A); + arrayUtil.reverseArray(B); + assertArrayEquals(new int[] {4, 3, 30 , 9, 7}, B); + } + + @Test + public void testRuntimeException() { + thrown.expect(RuntimeException.class); + arrayUtil.reverseArray(null); + } + + @Test + public void testRemoveZero() { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int newArr[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + assertArrayEquals(newArr, arrayUtil.removeZero(oldArr)); + } + + @Test + public void testMerge() { + int [] arr1 = {3, 5, 7, 8}; + int [] arr2 = {4, 5, 6, 7}; + int [] answer = new int[]{3,4,5,6,7,8}; + assertArrayEquals(answer, arrayUtil.merge(arr1, arr2)); + } + + @Test + public void testGrow() { + int [] oldArray = {2, 3, 6}; + int [] newArray = {2, 3, 6, 0, 0, 0}; + assertArrayEquals(newArray, arrayUtil.grow(oldArray, 3)); + } + + @Test + public void testFibonacci() { + int [] testArr = {1, 1, 2, 3, 5, 8, 13}; + int [] testArr2 = {1, 1}; + assertArrayEquals(testArr, arrayUtil.fibonacci(15)); + assertArrayEquals(testArr2, arrayUtil.fibonacci(2)); + } + + @Test + public void testGetPrimes() { + int [] testArr = {2, 3, 5, 7, 11, 13, 17, 19}; + assertArrayEquals(testArr, arrayUtil.getPrimes(23)); + } + + @Test + public void testGetPerfectNumbers() { + int [] testArr = {6}; + assertArrayEquals(testArr, arrayUtil.getPerfectNumbers(10)); + } + + @Test + public void testJoin() { + int [] testArr = {3, 8, 9}; + assertEquals("3-8-9", arrayUtil.join(testArr, "-")); + } +} From b1fdd8a016f7a1ff565a8bd9508be94ed24c78f0 Mon Sep 17 00:00:00 2001 From: Yanbo Date: Sun, 19 Mar 2017 23:11:38 +0800 Subject: [PATCH 125/155] =?UTF-8?q?'=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E6=A5=AD'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/homework02/ArrayUtil.java | 208 ------------------ .../src/com/homework02/LoginAction.java | 42 ---- .../609041842/src/com/homework02/Struts.java | 121 ---------- .../src/com/homework02/StrutsTest.java | 38 ---- .../609041842/src/com/homework02/View.java | 32 --- group23/609041842/src/struts.xml | 11 - 6 files changed, 452 deletions(-) delete mode 100644 group23/609041842/src/com/homework02/ArrayUtil.java delete mode 100644 group23/609041842/src/com/homework02/LoginAction.java delete mode 100644 group23/609041842/src/com/homework02/Struts.java delete mode 100644 group23/609041842/src/com/homework02/StrutsTest.java delete mode 100644 group23/609041842/src/com/homework02/View.java delete mode 100644 group23/609041842/src/struts.xml diff --git a/group23/609041842/src/com/homework02/ArrayUtil.java b/group23/609041842/src/com/homework02/ArrayUtil.java deleted file mode 100644 index 9ed26f4d42..0000000000 --- a/group23/609041842/src/com/homework02/ArrayUtil.java +++ /dev/null @@ -1,208 +0,0 @@ -package com.homework02; - -import java.util.ArrayList; -import java.util.Arrays; - -import org.junit.Test; - -public class ArrayUtil { - - /** - * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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) { - int[] reverse = new int[origin.length]; - for (int i = origin.length - 1; i >= 0; i--) { - reverse[reverse.length - i - 1] = origin[i]; - } - System.out.println("reverseArray-" + Arrays.toString(reverse)); - - } - - /** - * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 {1,3,4,5,6,6,5,4,7,6,7,5} - * - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray) { - - int count = 0; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] != 0) { - count++; - } - } - int[] newArray = new int[count]; - for (int i = 0; i < oldArray.length; i++) { - if (oldArray[i] != 0) { - for (int j = 0; j < newArray.length; j++) { - if (newArray[j] == 0) { - newArray[j] = oldArray[i]; - break; - } - } - } - } - System.out.println("removeZero-" + Arrays.toString(newArray)); - return newArray; - } - - /** - * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { - ArrayUtil atl = new ArrayUtil(); - int[] newArray = new int[array1.length + array2.length]; - System.arraycopy(array1, 0, newArray, 0, array1.length); - System.arraycopy(array2, 0, newArray, array1.length, array2.length); - newArray = atl.sort(newArray); - System.out.println(Arrays.toString(newArray)); - for (int i = 0; i < newArray.length; i++) { - for (int j = 0; j < newArray.length; j++) { - } - } - return newArray; - } - - /** - * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size - * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 - * [2,3,6,0,0,0] - * - * @param oldArray - * @param size - * @return - */ - public int[] grow(int[] oldArray, int size) { - int[] newArray = new int[oldArray.length + size]; - System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); - System.out.println("grow:" + Arrays.toString(newArray)); - return newArray; - } - - /** - * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 渚嬪锛 max = 15 , - * 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] - * - * @param max - * @return - */ - public ArrayList fibonacci(int max) { - int[] array = new int[max]; - int count = 1; - int a = 1; - int b = 1; - int c = 1; - ArrayList arrayList = new ArrayList(); - if (max <= 1) { - System.out.println("-" + arrayList); - return arrayList; - } else { - arrayList.add(a); - arrayList.add(b); - while (c < max) { - c = a + b; - a = b; - if (c + b > max) { - break; - } - arrayList.add(c); - b = c; - } - System.out.println("-" + arrayList); - } - return arrayList; - } - - @Test - public void tests() { - ArrayUtil s = new ArrayUtil(); - s.fibonacci(1); - } - - /** - * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] - * - * @param max - * @return - */ - public int[] getPrimes(int max) { - return null; - } - - /** - * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 - * - * @param max - * @return - */ - public int[] getPerfectNumbers(int max) { - return null; - } - - /** - * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" - * - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator) { - String newString = ""; - for (int i = 0; i < array.length; i++) { - if (i == array.length - 1) - seperator = ""; - newString = newString + array[i] + seperator; - } - System.out.println("join:" + newString); - return null; - } - - public int[] sort(int[] newArray) { - int count = 0; - for (int i = 0; i < newArray.length; i++) { - for (int j = i; j < newArray.length; j++) { - if (newArray[i] > newArray[j]) { - int mid = newArray[i]; - newArray[i] = newArray[j]; - newArray[j] = mid; - } - } - } - System.out.println(count); - return newArray; - } - - @Test - public void testArray() { - // test reverseArray - ArrayUtil atl = new ArrayUtil(); - int[] origin = { 4, 0, 7, 0, 2, 9, 8 }; - System.out.println("娴嬭瘯鏁版嵁:" + Arrays.toString(origin)); - atl.reverseArray(origin); - // remove zero - atl.removeZero(origin); - // join - atl.join(origin, "-"); - // grow - atl.grow(origin, 4); - // merge - int[] array1 = { 3, 5, 7, 8 }; - int[] array2 = { 1, 7, 4, 8, 2 }; - atl.merge(array1, array2); - } - -} diff --git a/group23/609041842/src/com/homework02/LoginAction.java b/group23/609041842/src/com/homework02/LoginAction.java deleted file mode 100644 index ed4c5449e0..0000000000 --- a/group23/609041842/src/com/homework02/LoginAction.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.homework02; - -/** - * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 - * - * @author liuxin - * - */ -public class LoginAction { - private String name; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - 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"; - } - - public void setName(String name) { - this.name = name; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getMessage() { - return this.message; - } -} diff --git a/group23/609041842/src/com/homework02/Struts.java b/group23/609041842/src/com/homework02/Struts.java deleted file mode 100644 index 88dc30bee9..0000000000 --- a/group23/609041842/src/com/homework02/Struts.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.homework02; - -import java.lang.reflect.Method; -import java.util.List; -import java.util.Map; - -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.io.SAXReader; -import org.dom4j.tree.DefaultElement; -import org.junit.Test; - -public class Struts { - - private static final String ACTIN_NOT_FOUNT = "鏈壘鍒癆ction"; - private static final String RESULT_NOT_FOUNT = "鏈壘鍒癛esult"; - - public static View runAction(String actionName, Map parameters) { - - /* - * - * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml - * - * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 - * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , - * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 - * - * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" - * - * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 - * {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters - * - * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 - * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 - * - */ - try { - String name = parameters.get("name"); - String password = parameters.get("password"); - String absoluteActionClassPath = getAbsoluteActionClassPath(actionName); - Class clazz; - clazz = Class.forName(absoluteActionClassPath); - - Object obj = clazz.newInstance(); - String setName = "name"; - String setPwd = "password"; - String prefix = "set"; - String setterName = prefix + setName.substring(0, 1).toUpperCase() + setName.substring(1, setName.length()); - String setterPwd = prefix + setPwd.substring(0, 1).toUpperCase() + setPwd.substring(1, setPwd.length()); - Method mnd = clazz.getMethod(setterName, String.class); - Method mtd = clazz.getMethod(setterPwd, String.class); - mnd.invoke(obj, name); - mtd.invoke(obj, password); - // 璋冨彇execute鑾峰彇杩斿洖鍊 - Method md = clazz.getMethod("execute"); - String mdResult = md.invoke(obj).toString(); - Method getMessageMethod = clazz.getMethod("getMessage"); - Object getMessage = getMessageMethod.invoke(obj); - String message = ""; - if (null != getMessage) { - message = getMessage.toString(); - } else { - System.out.println("messge涓虹┖"); - } - // 鑾峰彇xml涓殑result鍊 - String result = getResultValue(actionName, mdResult); - View view = new View(); - System.out.println(result); - view.setJsp(result); - parameters.put("message", message); - view.setParameters(parameters); - System.out.println("鎵撳嵃view缁撴灉-" + view.toString()); - return view; - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - - } - - /** - * 鑾峰彇璇锋眰鐨刟ctionName鎵鍦ㄧ被缁濆璺緞 - * - * @param actionName - * @return - * @throws DocumentException - */ - public static String getAbsoluteActionClassPath(String actionName) throws DocumentException { - SAXReader reader = new SAXReader(); - Document doc = reader.read("src/struts.xml"); - List getActions = doc.selectNodes("/struts/action[@name='" + actionName + "']"); - for (DefaultElement e : getActions) { - return e.attributeValue("class"); - } - return ACTIN_NOT_FOUNT; - } - - @Test - public void test() { - try { - System.out.println(getAbsoluteActionClassPath("login")); - // System.out.println(getResultValue("login", "success")); - } catch (DocumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public static String getResultValue(String actionName, String param) throws DocumentException { - SAXReader reader = new SAXReader(); - Document doc = reader.read("src/struts.xml"); - String path = "/struts/action[@name='" + actionName + "']/result[@name='" + param + "']"; - String result = doc.selectSingleNode(path).getText().toString(); - if (null == result) { - return RESULT_NOT_FOUNT; - } - return result; - } - -} diff --git a/group23/609041842/src/com/homework02/StrutsTest.java b/group23/609041842/src/com/homework02/StrutsTest.java deleted file mode 100644 index 89c31f88bd..0000000000 --- a/group23/609041842/src/com/homework02/StrutsTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.homework02; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name", "test"); - params.put("password", "1234"); - - View view = Struts.runAction(actionName, params); - System.out.println(view.getJsp()); - 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"); // 瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 - - 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")); - } -} diff --git a/group23/609041842/src/com/homework02/View.java b/group23/609041842/src/com/homework02/View.java deleted file mode 100644 index 2d37570a4f..0000000000 --- a/group23/609041842/src/com/homework02/View.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.homework02; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - - public Map getParameters() { - return parameters; - } - - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } - - @Override - public String toString() { - return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; - } - -} diff --git a/group23/609041842/src/struts.xml b/group23/609041842/src/struts.xml deleted file mode 100644 index 1d6fde9d6c..0000000000 --- a/group23/609041842/src/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file From 6212acf96c0068d952f9d45321612019b54c9b70 Mon Sep 17 00:00:00 2001 From: sargeles <330657387@qq.com> Date: Sun, 19 Mar 2017 23:41:41 +0800 Subject: [PATCH 126/155] 3.19 --- .../src/main/week02/practice/ArrayUtil.java | 115 ++++++++++++++++++ .../test/week02/practice/ArrayUtilTest.java | 79 ++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 group24/330657387/src/main/week02/practice/ArrayUtil.java create mode 100644 group24/330657387/src/test/week02/practice/ArrayUtilTest.java diff --git a/group24/330657387/src/main/week02/practice/ArrayUtil.java b/group24/330657387/src/main/week02/practice/ArrayUtil.java new file mode 100644 index 0000000000..3ab82a31e8 --- /dev/null +++ b/group24/330657387/src/main/week02/practice/ArrayUtil.java @@ -0,0 +1,115 @@ +package main.week02.practice; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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.length <= 1) { + return; + } + int i = 0, j = origin.length - 1; + int temp; + while (j > i) { + origin[i] = origin[i] ^ origin[j]; + origin[j] = origin[i] ^ origin[j]; + origin[i] = origin[i] ^ origin[j]; + i++; + j--; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int i = 0; + for (int j = 0; j < oldArray.length; j++) { + if (0 != oldArray[j]) { + oldArray[i++] = oldArray[j]; + } + } + int[] newArray = new int[i]; + System.arraycopy(oldArray, 0, newArray, 0,newArray.length); + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { + return null; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + return null; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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) { + return null; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + return null; + } + +} diff --git a/group24/330657387/src/test/week02/practice/ArrayUtilTest.java b/group24/330657387/src/test/week02/practice/ArrayUtilTest.java new file mode 100644 index 0000000000..1bf97fced3 --- /dev/null +++ b/group24/330657387/src/test/week02/practice/ArrayUtilTest.java @@ -0,0 +1,79 @@ +package test.week02.practice; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import main.week02.practice.ArrayUtil; + +import org.junit.Before; +import org.junit.Test; + + +public class ArrayUtilTest { + + ArrayUtil util; + + @Before + public void setUp() throws Exception { + util = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[][] origin = {{1,20,5,3,65,4,6,9,7}, + {1}, + {1,2,3}, + {}, + {23,32}}; + for(int[] a : origin){ + System.out.println("鍓嶏細"+Arrays.toString(a)); + util.reverseArray(a); + System.out.println("鍚庯細"+Arrays.toString(a)); + } + } + + @Test + public void testRemoveZero() { + int[][] origin = {{1,20,0,0,5,3,65,4,0,6,9,7}, + {1,0}, + {1,0,2,3,0}, + {}, + {23,0,0,32}}; + for(int[] a : origin){ + System.out.println("鍓嶏細"+Arrays.toString(a)); + System.out.println("鍚庯細"+Arrays.toString(util.removeZero(a))); + } + } + + @Test + public void testMerge() { + fail("Not yet implemented"); + } + + @Test + public void testGrow() { + fail("Not yet implemented"); + } + + @Test + public void testFibonacci() { + fail("Not yet implemented"); + } + + @Test + public void testGetPrimes() { + fail("Not yet implemented"); + } + + @Test + public void testGetPerfectNumbers() { + fail("Not yet implemented"); + } + + @Test + public void testJoin() { + fail("Not yet implemented"); + } + +} From bd88b5011c12821626175f4969a4f62e34430429 Mon Sep 17 00:00:00 2001 From: skomefen <1072760797@qq.com> Date: Sun, 19 Mar 2017 23:51:59 +0800 Subject: [PATCH 127/155] day3-19 --- .../src/com/coderising/array/ArrayUtil.java | 117 ++++++++++++++++++ .../com/coderising/litestruts/StrutsTest.java | 54 ++++++++ 2 files changed, 171 insertions(+) create mode 100644 group23/1072760797-skomefen/day3-19/src/com/coderising/array/ArrayUtil.java create mode 100644 group23/1072760797-skomefen/day3-19/src/com/coderising/litestruts/StrutsTest.java diff --git a/group23/1072760797-skomefen/day3-19/src/com/coderising/array/ArrayUtil.java b/group23/1072760797-skomefen/day3-19/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f1d0af35b9 --- /dev/null +++ b/group23/1072760797-skomefen/day3-19/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,117 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + int[] reverse = new int[origin.length]; + for(int i=0;i params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴锟? + + + 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")); + } +} From eb7574e152ed96c6c14a8252c5996eb5d32cd864 Mon Sep 17 00:00:00 2001 From: sdnb Date: Mon, 20 Mar 2017 00:27:42 +0800 Subject: [PATCH 128/155] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/494800949/build.gradle | 2 + .../com/coding/week2/array/ArrayUtil.java | 217 ++++++++++++++++++ .../coding/week2/litestruts/LoginAction.java | 39 ++++ .../com/coding/week2/litestruts/Struts.java | 121 ++++++++++ .../week2/litestruts/StrutsXmlUtil.java | 92 ++++++++ .../com/coding/week2/litestruts/View.java | 23 ++ .../494800949/src/main/resources/struts.xml | 11 + .../com/coding/week2/array/ArrayUtilTest.java | 91 ++++++++ .../coding/week2/litestruts/StrutsTest.java | 36 +++ .../week2/litestruts/StrutsXmlUtilTest.java | 35 +++ 10 files changed, 667 insertions(+) create mode 100644 group24/494800949/src/main/java/com/coding/week2/array/ArrayUtil.java create mode 100644 group24/494800949/src/main/java/com/coding/week2/litestruts/LoginAction.java create mode 100644 group24/494800949/src/main/java/com/coding/week2/litestruts/Struts.java create mode 100644 group24/494800949/src/main/java/com/coding/week2/litestruts/StrutsXmlUtil.java create mode 100644 group24/494800949/src/main/java/com/coding/week2/litestruts/View.java create mode 100644 group24/494800949/src/main/resources/struts.xml create mode 100644 group24/494800949/src/test/java/com/coding/week2/array/ArrayUtilTest.java create mode 100644 group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsTest.java create mode 100644 group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsXmlUtilTest.java diff --git a/group24/494800949/build.gradle b/group24/494800949/build.gradle index e88b275214..7930ebfca5 100644 --- a/group24/494800949/build.gradle +++ b/group24/494800949/build.gradle @@ -18,6 +18,8 @@ repositories { dependencies{ compile group: 'junit', name: 'junit', version: '4.11' + compile files("lib/dom4j-1.6.1.jar") + compile files("lib/jaxen-1.1.1.jar") } gradle.projectsEvaluated { tasks.withType(JavaCompile) { diff --git a/group24/494800949/src/main/java/com/coding/week2/array/ArrayUtil.java b/group24/494800949/src/main/java/com/coding/week2/array/ArrayUtil.java new file mode 100644 index 0000000000..f454b99d94 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/array/ArrayUtil.java @@ -0,0 +1,217 @@ +package com.coding.week2.array; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + int[] newArr = new int[origin.length]; + for(int i = origin.length - 1,j = 0; i >= 0; i--,j++){ + newArr[j] = origin[i]; + } + System.arraycopy(newArr, 0, origin, 0, origin.length); + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] newArr = new int[oldArray.length]; + int j = 0; + for(int i = 0; i < oldArray.length; i++){ + if(oldArray[i] != 0) + newArr[j++] = oldArray[i]; + } + int[] newArr1 = new int[j]; + System.arraycopy(newArr, 0, newArr1, 0, j); + return newArr1; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + int len = array1.length > array2.length ? array1.length : array2.length; + int[] newArr = new int[len]; + for(int i = 0; i < array1.length; i++){ + for(int j : array2){ + if(array1[i] == j){ + newArr[i] = array1[i]; + } + } + } + int[] newArr2 = new int[array1.length - newArr.length]; + for(int i = 0; i < array1.length; i++){ + for(int j : newArr){ + if(array1[i] == j) + continue; + } + } + int[] newArr1 = new int[array1.length + array2.length]; + System.arraycopy(array2, 0, newArr1, 0, array2.length); + System.arraycopy(newArr, 0, newArr1, array2.length-1, newArr.length); + bubuleSort(newArr1); + return newArr1; + } + + public void bubuleSort(int[] newArr1){ + for(int i = 0; i < newArr1.length; i++){ + for(int j = 0; j < newArr1.length; j++) + if(newArr1[i] < newArr1[j]){ + int temp = newArr1[i]; + newArr1[i] = newArr1[j] ; + newArr1[j] = temp; + } + } + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArr = new int[oldArray.length + size]; + for(int i = 0; i < newArr.length; i++){ + if (i < oldArray.length){ + newArr[i] = oldArray[i]; + }else + newArr[i] = 0; + } + + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + //1 寰楀埌灏忎簬max鐨勬枑娉㈤偅濂戞暟 + //2 + + if( max == 1) + return new int[]{}; + int[] arr = new int[3]; + arr[0] = 1; + arr[1] = 1; + arr[2] = arr[0] + arr[1]; + if(max == 2) + return arr; + int i = 2; + while (max > arr[i]){ + if(i+1 >= arr.length){ + int capacity = arr.length + (arr.length >> 1); + int[] newArr = new int[capacity]; + System.arraycopy(arr, 0, newArr, 0, arr.length); + arr = newArr; + } + arr[++i] = arr[i - 1] + arr[i - 2]; + if(arr[i] < 0){ + System.out.println(Arrays.toString(arr)); + throw new OutOfMemoryError(); + } + } + + return removeZero(arr); + } + + + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 2) + return new int[]{}; + int[] arr = new int[max]; + for(int i = max; i >= 2; i--){ + if(isPrime(i)){ + arr[i] = i; + } + } + return removeZero(arr); + } + + private boolean isPrime(int value){ + for(int i = 2; i < Math.sqrt(value); i++){ + if(value % i == 0) + return false; + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] ints = new int[max+1]; + for(int i = 1; i <= max; i++){ + if(isPerfectNum(i)){ + ints[i] = i; + } + } + return removeZero(ints); + } + + + private boolean isPerfectNum(int value){ + if(value == 1) + return false; + int sum = 0; + for(int i = 1; i <= Math.sqrt(value); i++){ + if(value % i == 0){ + sum += i + value / i; + } + } + return sum-value == value; + } + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder builder = new StringBuilder(); + for(int i = 0; i < array.length; i++){ + builder.append(array[i]); + if(i != array.length - 1) + builder.append(seperator); + } + return builder.toString(); + } + + +} diff --git a/group24/494800949/src/main/java/com/coding/week2/litestruts/LoginAction.java b/group24/494800949/src/main/java/com/coding/week2/litestruts/LoginAction.java new file mode 100644 index 0000000000..1c90d49ccb --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.week2.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group24/494800949/src/main/java/com/coding/week2/litestruts/Struts.java b/group24/494800949/src/main/java/com/coding/week2/litestruts/Struts.java new file mode 100644 index 0000000000..6448434800 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/litestruts/Struts.java @@ -0,0 +1,121 @@ +package com.coding.week2.litestruts; + +import org.dom4j.DocumentException; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view = new View(); + try { + //0 + //寰楀埌action绫诲悕 + String actionClassName = StrutsXmlUtil.getActionClassName(actionName); + + //寰楀埌action鐨勭粨鏋滃強jsp璺緞 + Map jspMap = StrutsXmlUtil.getResultOfAction(actionName); + + //鍔犺浇绫 + Class clazz = Class.forName(actionClassName); + //瀹炰緥鍖栧璞 + Object obj = clazz.newInstance(); + Method[] methods = clazz.getDeclaredMethods(); + + //1 娉ㄥ叆action瀹炰緥鍙橀噺 + setFields(obj, methods, parameters); + + //2 鎵цaction + String result = excute(obj, methods); + + + //3 鑾峰彇杩斿洖鐨勫弬鏁 + Map viewParams = buildReturnParams(obj, methods); + view.setParameters(viewParams); + + //4 璁剧疆杩斿洖鐨刯sp + String jsp = jspMap.get(result); + view.setJsp(jsp); + + } catch (DocumentException | ClassNotFoundException + | InstantiationException | IllegalAccessException + | InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } + + private static String excute(Object obj, Method[] methods ) + throws InvocationTargetException, IllegalAccessException { + for(Method method : methods){ + String methodName = method.getName(); + if("execute".equals(methodName)){ + return (String) method.invoke(obj); + } + } + return ""; + } + + + private static void setFields(Object obj, Method[] methods, Map parameters) + throws InvocationTargetException, IllegalAccessException { + Set> entities = parameters.entrySet(); + for(Map.Entry entry : entities){ + for(Method method : methods){ + if(method.getName().equals("set" + upperFirstChar(entry.getKey()))){ + method.invoke(obj, entry.getValue()); + } + } + } + } + + private static Map buildReturnParams(Object obj, Method[] methods) throws InvocationTargetException, IllegalAccessException { + Map viewParams = new HashMap<>(); + for(Method method : methods){ + String methodName = method.getName(); + if(methodName.startsWith("get")){ + Object ret = method.invoke(obj); + String field = methodName.substring(3); + viewParams.put(lowerFirstChar(field), ret); + } + } + return viewParams; + } + private static String upperFirstChar(String str){ + char[] cs = str.toCharArray(); + cs[0] -= 32; + return new String(cs); + } + + private static String lowerFirstChar(String str){ + char[] cs = str.toCharArray(); + cs[0] += 32; + return new String(cs); + } +} diff --git a/group24/494800949/src/main/java/com/coding/week2/litestruts/StrutsXmlUtil.java b/group24/494800949/src/main/java/com/coding/week2/litestruts/StrutsXmlUtil.java new file mode 100644 index 0000000000..e4cbee7480 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/litestruts/StrutsXmlUtil.java @@ -0,0 +1,92 @@ +package com.coding.week2.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.util.*; + +/** + * Created by Administrator on 2017/3/19 0019. + */ +public class StrutsXmlUtil { + + private static final String RESOURCE_PATH = "src/main/resources"; + private static final String STRUTS_CONFIG_FILE_NAME = "struts.xml"; + + private StrutsXmlUtil() { + } + + public static Document readXml(String path) throws DocumentException { + try { + SAXReader reader = new SAXReader(); + return reader.read(new File(path)); + } catch (DocumentException e) { + e.printStackTrace(); + throw e; + } + } + + + public static Iterator actionIterator(String path) throws DocumentException { + Document doc = readXml(path); + Element root = doc.getRootElement(); + return root.elementIterator("action"); + } + + public static String getActionClassName(String actionName) throws DocumentException { + Iterator iterator = actionIterator(getPathOfStrutsConfigurtion()); + if(actionName == null || "".equals(actionName)){ + throw new IllegalArgumentException("actionName can't be empty"); + } + while (iterator.hasNext()){ + Element e =(Element) iterator.next(); + if(actionName.equals(e.attributeValue("name"))){ + return e.attributeValue("class"); + } + } + return null; + } + + + public static String getPathOfStrutsConfigurtion(){ + File file = new File(RESOURCE_PATH); + List fileList = new ArrayList<>(); + + if (file.isDirectory()){ + File[] files = file.listFiles((dir, name) -> { + return name.equals(STRUTS_CONFIG_FILE_NAME); + }); + fileList.addAll(Arrays.asList(files)); + if(fileList.size() > 1){ + throw new RuntimeException("閰嶇疆鏂囦欢涓嶆涓涓"); + }else if(fileList.size() == 0){ + throw new RuntimeException("鎵句笉鍒皊truts閰嶇疆鏂囦欢"); + } + } + return fileList.get(0).getPath(); + } + + public static Map getResultOfAction(String actionName) throws DocumentException { + Iterator iterator = actionIterator(getPathOfStrutsConfigurtion()); + Map res = new HashMap<>(); + while (iterator.hasNext()){ + Element e =(Element) iterator.next(); + System.out.println(e.getName()); + if(actionName.equals(e.attributeValue("name"))){ + Iterator resItr = e.elementIterator("result"); + while (resItr.hasNext()){ + Element result = (Element)resItr.next(); + System.out.println(result.attribute("name").getValue()); + System.out.println(result.getData()); + res.put(result.attribute("name").getValue(), result.getData().toString()); + } + + } + } + return res; + } + +} diff --git a/group24/494800949/src/main/java/com/coding/week2/litestruts/View.java b/group24/494800949/src/main/java/com/coding/week2/litestruts/View.java new file mode 100644 index 0000000000..bf7bd0d6c8 --- /dev/null +++ b/group24/494800949/src/main/java/com/coding/week2/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.week2.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/494800949/src/main/resources/struts.xml b/group24/494800949/src/main/resources/struts.xml new file mode 100644 index 0000000000..4133f6a2fb --- /dev/null +++ b/group24/494800949/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/494800949/src/test/java/com/coding/week2/array/ArrayUtilTest.java b/group24/494800949/src/test/java/com/coding/week2/array/ArrayUtilTest.java new file mode 100644 index 0000000000..b402701691 --- /dev/null +++ b/group24/494800949/src/test/java/com/coding/week2/array/ArrayUtilTest.java @@ -0,0 +1,91 @@ +package com.coding.week2.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by Administrator on 2017/3/19 0019. + */ +public class ArrayUtilTest { + + + private ArrayUtil arrayUtil; + + private int[] ints = new int[]{4, 9, 10, 3, 5, 0, 10, 12, 0, 9}; + + @Before + public void setup(){ + arrayUtil = new ArrayUtil(); + } + + @Test + public void testReverseArray() throws Exception { + arrayUtil.reverseArray(ints); + Assert.assertEquals(ints[0], 9); + Assert.assertEquals(ints[1], 0); + Assert.assertEquals(ints[2], 12); + Assert.assertEquals(ints[3], 10); + Assert.assertEquals(ints[4], 0); + } + + @Test + public void testRemoveZero() throws Exception { + int[] newInts = ints.clone(); + int[] newArr = arrayUtil.removeZero(newInts); + System.out.println(Arrays.toString(newArr)); + } + + @Test + public void testMerge() throws Exception { + int[] ints1 = new int[]{3, 4, 9, 20}; + int[] ints2 = new int[]{4, 6, 7, 12}; + int[] mergeArr = arrayUtil.merge(ints1, ints2); + System.out.println(Arrays.toString(mergeArr)); + } + + @Test + public void testGrow() throws Exception { + int[] newInts = arrayUtil.grow(ints, 5); + + Assert.assertEquals(newInts.length, 15); + Assert.assertEquals(newInts[14], 0); + Assert.assertEquals(newInts[13], 0); + Assert.assertEquals(newInts[12], 0); + Assert.assertEquals(newInts[11], 0); + Assert.assertEquals(newInts[10], 0); + } + + @Test + public void testFibonacci() throws Exception { + int[] ints = arrayUtil.fibonacci(100); + Assert.assertArrayEquals(ints, new int[]{1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144}); + } + + @Test + public void testGetPrimes() throws Exception { + int[] ints = arrayUtil.getPrimes(100); + System.out.println(Arrays.toString(arrayUtil.getPrimes(100))); + Assert.assertArrayEquals(ints, new int[]{2, 3, 4, 5, 7, 9, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41, 43, 47, 49, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] ints = arrayUtil.getPerfectNumbers(10000); + long start = System.currentTimeMillis(); + System.out.println(Arrays.toString(arrayUtil.getPerfectNumbers(100000))); + long end = System.currentTimeMillis(); + System.out.println(end - start); + Assert.assertArrayEquals(ints, new int[]{6, 28, 496, 8128}); + } + + @Test + public void testJoin() throws Exception { + System.out.println(arrayUtil.join(ints, "|")); + Assert.assertEquals("4+9+10+3+5+0+10+12+0+9", arrayUtil.join(ints, "+")); + Assert.assertEquals("4|9|10|3|5|0|10|12|0|9", arrayUtil.join(ints, "|")); + } + +} \ No newline at end of file diff --git a/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsTest.java b/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..bf16557d9c --- /dev/null +++ b/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsTest.java @@ -0,0 +1,36 @@ +package com.coding.week2.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } + +} diff --git a/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsXmlUtilTest.java b/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsXmlUtilTest.java new file mode 100644 index 0000000000..bedff77fe6 --- /dev/null +++ b/group24/494800949/src/test/java/com/coding/week2/litestruts/StrutsXmlUtilTest.java @@ -0,0 +1,35 @@ +package com.coding.week2.litestruts; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; + +/** + * Created by Administrator on 2017/3/19 0019. + */ +public class StrutsXmlUtilTest { + + @Test + public void testGetClassNameOfAction() throws Exception { + String path = "src\\main\\java\\com\\coding\\week2\\litestruts\\struts.xml"; + + String actionCLass = StrutsXmlUtil.getActionClassName( "login"); + Assert.assertEquals(actionCLass, "com.coderising.litestruts.LoginAction"); + actionCLass = StrutsXmlUtil.getActionClassName("logout"); + Assert.assertEquals(actionCLass, "com.coderising.litestruts.LogoutAction"); + + } + + + @Test + public void testGetResultOfAction() throws DocumentException { + String path = "src\\main\\java\\com\\coding\\week2\\litestruts\\struts.xml"; + Map res = StrutsXmlUtil.getResultOfAction("login"); + Assert.assertNotNull(res.get("success"), "/jsp/homepage.jsp"); + Assert.assertNotNull(res.get("fail"), "/jsp/showLogin.jsp"); + } + + +} \ No newline at end of file From 6e7de6c17e06508b907e90896902785948e3dc2c Mon Sep 17 00:00:00 2001 From: xiaozi Date: Mon, 20 Mar 2017 12:52:50 +0800 Subject: [PATCH 129/155] This is my second week's work. --- group24/1054283210/.classpath | 1 + .../coding2017/secondWork/.gitignore | 27 +++ .../coding2017/secondWork/ArrayUtil.java | 201 ++++++++++++++++++ .../coding2017/secondWork/LoginAction.java | 41 ++++ .../coding2017/secondWork/Struts.java | 145 +++++++++++++ .../coding2017/secondWork/StrutsTest.java | 46 ++++ .../xiaozi123/coding2017/secondWork/View.java | 23 ++ .../coding2017/secondWork/struts.xml | 11 + ...7\347\253\240\345\234\260\345\235\200.txt" | 2 + 9 files changed, 497 insertions(+) create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/.gitignore create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/ArrayUtil.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/LoginAction.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/Struts.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/StrutsTest.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/View.java create mode 100644 group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/struts.xml create mode 100644 "group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/\346\226\207\347\253\240\345\234\260\345\235\200.txt" diff --git a/group24/1054283210/.classpath b/group24/1054283210/.classpath index 2d7497573f..26651e8716 100644 --- a/group24/1054283210/.classpath +++ b/group24/1054283210/.classpath @@ -3,5 +3,6 @@ + diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/.gitignore b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/.gitignore new file mode 100644 index 0000000000..2c93a035dc --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/.gitignore @@ -0,0 +1,27 @@ +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ +*.iml +rebel.* +.rebel.* + +# Idea +*.iml +*.ipr +*.iws +.idea + +target diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/ArrayUtil.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/ArrayUtil.java new file mode 100644 index 0000000000..149f196397 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/ArrayUtil.java @@ -0,0 +1,201 @@ +package com.github.xiaozi123.coding2017.secondWork; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +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){ + int[] reverseArray=new int[origin.length]; + for (int i = 0; i < reverseArray.length/2; i++) { + reverseArray[i]=origin[origin.length-i-1]; + } + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + + int[] newArray=new int[oldArray.length]; + + for (int i = 0,j=0; i < oldArray.length; i++,j++) { + if (oldArray[i]==0) { + i++; + } + newArray[j]=oldArray[i]; + } + return newArray; + + + } + + /** + * 给定两个已经排序好的整形数组, 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){ + ArrayList list1 = new ArrayList(Arrays.asList(array1)); + ArrayList list2 = new ArrayList(Arrays.asList(array2)); + list1.removeAll(list2); + Integer[] integers=(Integer[]) list1.toArray(); + int[] intArray = new int[integers.length]; + for(int i=0; i < integers.length; i ++) + { + intArray[i] = integers[i].intValue(); + } + return intArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + if (oldArray==null||size>=0) { + throw new IndexOutOfBoundsException("出错."); + + } + int[] resultArray=new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, resultArray, 0,oldArray.length); + return resultArray; + } + + /** + * 斐波那契数列为: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){ + int[] array=new int[max]; + for (int i = 0; i < max; i++) { + array[i]=getFibo(i);//第i项 + if (array[i]>=max) { + break; + } + } + return array; + } + + // 获取第i项 + private static int getFibo(int i) { + if (i == 1 || i == 2) + return 1; + else + return getFibo(i - 1) + getFibo(i - 2);} + + + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] array=new int[max]; + for (int i = 0,j=0; i < max; i++) { + if (isPrime(i)) { + array[j]=i; + j++; + } + + } + return array; + } + public static boolean isPrime(int a) { + boolean flag = true; + + if (a < 2) {// 素数不小于2 + return false; + } else { + + for (int i = 2; i <= Math.sqrt(a); i++) { + + if (a % i == 0) {// 若能被整除,则说明不是素数,返回false + + flag = false; + break;// 跳出循环 + } + } + } + return flag; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] array=new int[max]; + for (int i = 0,j=0; i < max; i++) { + if (isPrime(i)) { + array[j]=i; + j++; + } + + } + return array; + } + + public static boolean isPerfectNumber(int i) { + int s=0; + for(int j=1;j0&&array!=null) { + StringBuffer stringBuffer=new StringBuffer(); + stringBuffer.append(array[0]); + for (int i = 1; i < array.length; i++) { + stringBuffer.append(seperator).append(array[i]); + } + return stringBuffer.toString(); + } + return null; + } + + +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/LoginAction.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/LoginAction.java new file mode 100644 index 0000000000..6c8e4026ce --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/LoginAction.java @@ -0,0 +1,41 @@ +package com.github.xiaozi123.coding2017.secondWork; + + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} + diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/Struts.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/Struts.java new file mode 100644 index 0000000000..2f267a2491 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/Struts.java @@ -0,0 +1,145 @@ +package com.github.xiaozi123.coding2017.secondWork; + +import java.util.Map; + +import java.io.File; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import com.sun.corba.se.impl.oa.poa.ActiveObjectMap.Key; + + + +public class Struts { + + + + public static View runAction(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + //读取xml文件 + +// String path="src/com/github/xiaozi123.coding2017.secondWork/struct.xml"; + + + + return initView(actionName, parameters); + } + + public static View initView(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + View view=new View(); + int i=0; + String[] methodNames=new String[parameters.size()]; + + for (String string : parameters.keySet()) { + methodNames[i++]="set" + +string.substring(0, 1).toUpperCase()+string.substring(1); + } + + Struts.class.getResourceAsStream("/structs.xml"); + Element element=getTargetElement(actionName); + + //通过反射实例化 + String className=element.attribute(1).getValue(); + Class clazz=Class.forName(className); + + Object object=clazz.newInstance(); + + invokeObjectSetter(parameters, methodNames, clazz, object); + + view.setParameters(createGetterMap(clazz, object)); + setViewJsp(view, element, clazz, object); + return view; + + } + + + private static void setViewJsp(View view, Element element, Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + view.setJsp(getJsp(element, executeToGetResult(clz, obj))); + } + + + private static Map createGetterMap(Class clz, Object obj) throws IllegalAccessException, InvocationTargetException { + Map map = new HashMap(); + Method[] methods = clz.getMethods(); + for (Method item : methods) { + if (item.getName().contains("get")) { + String key = item.getName().substring(3).toLowerCase(); + Object value = item.invoke(obj); + map.put(key, value); + } + } + return map; + } + + private static String executeToGetResult(Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + // 反射获取方法 并且执行 + Method method = clz.getMethod("execute"); + String result = (String) method.invoke(obj); + return result; + } + + private static void invokeObjectSetter(Map parameters, + String[] methodNames, Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for (String key : methodNames) { + Method method = clz.getMethod(key, String.class); + method.invoke(obj, parameters.get(key)); + } + } + + private static Element getTargetElement(String actionName) throws DocumentException { + SAXReader reader = new SAXReader(); + InputStream inputStream =Struts.class.getResourceAsStream("/struts.xml"); + Document document = reader.read(inputStream); + Element rootNode = document.getRootElement(); + List elements = rootNode.elements(); + for (Element item : elements) { + if (actionName.equals(item.attribute(0).getValue())) { + return item; + } + } + return null; + } + + private static String getJsp(Element element, String result) { + List elements = element.elements(); + for (Element e : elements) { + if (result.equals(e.attribute(0).getValue())) { + return e.getTextTrim(); + } + } + return null; + } + +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/StrutsTest.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/StrutsTest.java new file mode 100644 index 0000000000..7e61cb97d9 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/StrutsTest.java @@ -0,0 +1,46 @@ +package com.github.xiaozi123.coding2017.secondWork; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, DocumentException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, DocumentException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); ////密码和预设的不一致 + + + 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")); + } +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/View.java b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/View.java new file mode 100644 index 0000000000..5d13e67df3 --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/View.java @@ -0,0 +1,23 @@ +package com.github.xiaozi123.coding2017.secondWork; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/struts.xml b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/struts.xml new file mode 100644 index 0000000000..0582b7d4ea --- /dev/null +++ b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git "a/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/\346\226\207\347\253\240\345\234\260\345\235\200.txt" "b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/\346\226\207\347\253\240\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..4aa14355dc --- /dev/null +++ "b/group24/1054283210/src/com/github/xiaozi123/coding2017/secondWork/\346\226\207\347\253\240\345\234\260\345\235\200.txt" @@ -0,0 +1,2 @@ +简书:http://www.jianshu.com/p/8a15d1c12bc0 +CSDN:http://blog.csdn.net/qq_23038639/article/details/63252328 \ No newline at end of file From 54c47c7516c8bd39bda2ddd906a4a6b5a3062a3d Mon Sep 17 00:00:00 2001 From: DonaldY <448641125@qq.com> Date: Mon, 20 Mar 2017 19:39:12 +0800 Subject: [PATCH 130/155] update1 --- group24/448641125/src/com/donaldy/basic/ArrayUtil.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/group24/448641125/src/com/donaldy/basic/ArrayUtil.java b/group24/448641125/src/com/donaldy/basic/ArrayUtil.java index bbe0f57db3..73f956258d 100644 --- a/group24/448641125/src/com/donaldy/basic/ArrayUtil.java +++ b/group24/448641125/src/com/donaldy/basic/ArrayUtil.java @@ -15,14 +15,20 @@ public class ArrayUtil { */ public void reverseArray(int[] origin){ makeSure(origin); - int length = origin.length - 1; + /*int length = origin.length - 1; int [] newArr = new int[length + 1]; for (int i = 0; i <= length; ++i) newArr[i] = origin[length - i]; for (int i = 0 ; i <= length; ++i) - origin[i] = newArr[i]; + origin[i] = newArr[i];*/ + + for (int i = 0, j = origin.length - 1; i < j; ++i, --j) { + int t = origin[i]; + origin[i] = origin[j]; + origin[j] = t; + } } From 18aeca00d7045ca227997789e9ba62921cee39ef Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Mon, 20 Mar 2017 22:17:39 +0800 Subject: [PATCH 131/155] Third Homework --- .../src/Download/DownloadThread.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 group11/1310368322/src/Download/DownloadThread.java diff --git a/group11/1310368322/src/Download/DownloadThread.java b/group11/1310368322/src/Download/DownloadThread.java new file mode 100644 index 0000000000..af248b72b2 --- /dev/null +++ b/group11/1310368322/src/Download/DownloadThread.java @@ -0,0 +1,39 @@ +package day_2017_3_8_ThreadHomework; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier; + String localFile; + public DownloadThread(Connection conn, int startPos, int endPos,String localFile,CyclicBarrier barrier){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + public void run(){ + try { + System.out.println("Begin to read [" + startPos + "-" + endPos + "]"); + byte [] data = conn.read(startPos, endPos); + System.out.println("创建一个随机读取文件的对象"); + RandomAccessFile file = new RandomAccessFile(localFile,"rw"); + file.seek(startPos); + System.out.println("要写数据了"); + file.write(data); + file.close(); + conn.close(); + System.out.println(this.currentThread().getName()+"once over"); + barrier.await(); + } catch (Exception e) { + // TODO: handle exception + } + } +} From 4dbf500189c70d857197770a035fe1e8b8f0273b Mon Sep 17 00:00:00 2001 From: Mengxz Date: Tue, 21 Mar 2017 15:09:00 +0800 Subject: [PATCH 132/155] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group17/article/20170305-20170312.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/group17/article/20170305-20170312.md b/group17/article/20170305-20170312.md index f43f5b5fb2..f4ccc3e67f 100644 --- a/group17/article/20170305-20170312.md +++ b/group17/article/20170305-20170312.md @@ -22,7 +22,7 @@ 240094626 -82427129 +82427129 http://blog.csdn.net/walk_er/article/details/60570932 296910598 @@ -53,4 +53,3 @@ 1368331120 517970312 - From 94c851866b86571a374ac38817c4d5060002a492 Mon Sep 17 00:00:00 2001 From: James <1310368322@qq.com> Date: Tue, 21 Mar 2017 15:31:02 +0800 Subject: [PATCH 133/155] Third Homework --- .../src/Download/FileDownloader.java | 114 ++++++++++++++++++ .../src/Download/api/Connection.java | 22 ++++ .../src/Download/api/ConnectionException.java | 7 ++ .../src/Download/api/ConnectionManager.java | 10 ++ .../src/Download/api/DownloadListener.java | 5 + .../src/Download/impl/ConnectionImpl.java | 73 +++++++++++ .../Download/impl/ConnectionManagerImpl.java | 16 +++ .../1310368322/test/FileDownloaderTest.java | 44 +++++++ 8 files changed, 291 insertions(+) create mode 100644 group11/1310368322/src/Download/FileDownloader.java create mode 100644 group11/1310368322/src/Download/api/Connection.java create mode 100644 group11/1310368322/src/Download/api/ConnectionException.java create mode 100644 group11/1310368322/src/Download/api/ConnectionManager.java create mode 100644 group11/1310368322/src/Download/api/DownloadListener.java create mode 100644 group11/1310368322/src/Download/impl/ConnectionImpl.java create mode 100644 group11/1310368322/src/Download/impl/ConnectionManagerImpl.java create mode 100644 group11/1310368322/test/FileDownloaderTest.java diff --git a/group11/1310368322/src/Download/FileDownloader.java b/group11/1310368322/src/Download/FileDownloader.java new file mode 100644 index 0000000000..445ede2e3d --- /dev/null +++ b/group11/1310368322/src/Download/FileDownloader.java @@ -0,0 +1,114 @@ +package day_2017_3_8_ThreadHomework; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + +public class FileDownloader { + private String url; + private String localFile; + DownloadListener listener; + ConnectionManager cm; + + private static final int DOWNLOAD_THREAD_NUM = 3; + public FileDownloader(String _url,String localFile){ + this.url = _url; + this.localFile = localFile; + } + + 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 方法 + + // 下面的代码是实例代码,也就是说只有一个线程,你需要改造成多线程的 + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_THREAD_NUM,new Runnable() {// 当所有的Thread都调用 await方法时,会执行后面的 barrierAction,调用后面这个线程 + @Override + public void run() { + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength();// 拿到将要下载文件的长度 + createPlaceHolderFile(this.localFile,length);//占位 + System.out.println("占位完毕"); + int [][] ranges = allocateDownloadRange(DOWNLOAD_THREAD_NUM,length);// 给每个线程分配开始位置和结束位置 + // 开始下载文件 + System.out.println("开始下载文件"); + for(int i = 0; i < DOWNLOAD_THREAD_NUM; i++){ + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + thread.start(); + System.out.println("第" + (i+1) + "个线程已经启动"); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + System.out.println("即将关闭连接"); + if(conn != null){ + conn.close(); + System.out.println("关闭连接成功"); + } + } + } + + public void setListener(DownloadListener listener){ + this.listener = listener; + } + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + public DownloadListener getListener(){ + return this.listener; + } + private void createPlaceHolderFile(String fileName,int contentLen) throws IOException{ + RandomAccessFile file = new RandomAccessFile(fileName,"rw"); + for(int i = 0; i < contentLen; i++){ + file.write(0); + } + file.close(); + } + /** + * 给出线程数和文件长度,返回一个二维数组,里面存的是每个线程下载的开始位置和结束位置 + * @param threadNum + * @param contentLen + * @return + */ + private int [][] allocateDownloadRange(int threadNum, int contentLen){ + int [][] ranges = new int[threadNum][2];// 用二维数组存下每个线程的开始位置和结束位置 + + int eachThreadSize = contentLen / threadNum;// 每个线程需要下载的文件大小 + int left = contentLen % threadNum;// 剩下的归最后一个线程来处理 + + for(int i = 0; i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + URLConnection con; + try { + con = url.openConnection(); + return con.getContentLength(); + } catch (Exception e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + } + +} diff --git a/group11/1310368322/src/Download/impl/ConnectionManagerImpl.java b/group11/1310368322/src/Download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..ff92aa77fe --- /dev/null +++ b/group11/1310368322/src/Download/impl/ConnectionManagerImpl.java @@ -0,0 +1,16 @@ +package com.coderising.download.impl; + +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager{ + + @Override + public Connection open(String url) throws ConnectionException { + return new ConnectionImpl(url); + } + +} diff --git a/group11/1310368322/test/FileDownloaderTest.java b/group11/1310368322/test/FileDownloaderTest.java new file mode 100644 index 0000000000..f337778812 --- /dev/null +++ b/group11/1310368322/test/FileDownloaderTest.java @@ -0,0 +1,44 @@ +package testDownload; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +import day_2017_3_8_ThreadHomework.FileDownloader; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Test + public void test() { + String url = "http://pic33.nipic.com/2013 0916/3420027_192919547000_2.jpg"; + FileDownloader downloader = new FileDownloader(url,"d:/test.jpg"); + 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("还没下载完成,休眠五秒"); + // 休眠 5 秒 + Thread.sleep(5000); + } catch (Exception e) { + e.printStackTrace(); + } + } + System.out.println("下载完成"); + } + + +} From b36c1cb3f577932fd13b0c5824920b6ab01c68a9 Mon Sep 17 00:00:00 2001 From: Pan Date: Wed, 22 Mar 2017 00:15:45 +0800 Subject: [PATCH 134/155] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/pan/coding2017/basic/List.java | 14 +- .../org/pan/coding2017/basic2/LinkedList.java | 429 ++++++++++++++++++ .../multThreadDownload/DownloadThread.java | 40 ++ .../multThreadDownload/FileDownloader.java | 93 ++++ .../FileDownloaderTest.java | 44 ++ .../multThreadDownload/api/Connection.java | 23 + .../api/ConnectionException.java | 7 + .../api/ConnectionManager.java | 10 + .../api/DownloadListener.java | 5 + .../api/impl/ConnectionImpl.java | 67 +++ .../api/impl/ConnectionManagerImpl.java | 23 + .../org/pan/coding2017/utils/Dom4JUtil.java | 41 -- .../pan/coding2017/utils/JaxpSAXPUtil.java | 89 ---- .../pan/coding2017/basic/LinkedListTest.java | 4 +- 14 files changed, 745 insertions(+), 144 deletions(-) create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic2/LinkedList.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/DownloadThread.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/FileDownloader.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/FileDownloaderTest.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/Connection.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/ConnectionException.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/ConnectionManager.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/DownloadListener.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/impl/ConnectionImpl.java create mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/impl/ConnectionManagerImpl.java delete mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java delete mode 100644 group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java index daa8253313..82e72e1080 100644 --- a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java @@ -6,19 +6,9 @@ */ public interface List { - boolean add(Object o); - - boolean add(int index, Object o); - - Object set(int index, Object element); - + void add(Object o); + void add(int index, Object o); Object get(int index); - Object remove(int index); - int size(); - - boolean isEmpty(); - - Iterator iterator(); } diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic2/LinkedList.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic2/LinkedList.java new file mode 100644 index 0000000000..5286f27746 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic2/LinkedList.java @@ -0,0 +1,429 @@ +package org.pan.coding2017.basic2; + +import org.pan.coding2017.basic.Iterator; +import org.pan.coding2017.basic.List; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size=0; + + private void checkIndex(int index){ + if(index<0 || index>=this.size) + { + throw new IndexOutOfBoundsException("Error!Invalid index:"+index); + } + } + + private void checkSize(){ + if(size==0) + { + throw new RuntimeException("Empty LinkedList."); + } + } + + public void add(Object o){ + Node temp = new Node(o,null); + if(this.head==null){ + this.head = temp; + this.tail = head; + }else{ + this.tail.next = temp; + this.tail = temp; + } + this.size++; + } + + /* (non-Javadoc) + * @see com.coding.basic2.List#add(int, java.lang.Object) + */ + public void add(int index , Object o){ + checkIndex(index); + if(index==0) + { + Node newNode = new Node(o,head); + head = newNode; + }else{ + Node temp = head; + for(int i=1;i0){ + Node temp = head; + sb.append(temp.data); + while(temp.hasNext()){ + temp = temp.next; + sb.append(","); + sb.append(temp.data); + } + } + return sb.toString(); + } + + public Object remove(int index){ + checkIndex(index); + Node temp = head; + Node pre = head; + Object result; + if(index == 0) + { + result = head.data; + head = head.next; + }else{ + for(int i=0;i0) + { + pre=pre.next; + } + temp=temp.next; + } + result = temp.data; + pre.next=temp.next; + temp = null; + + + if(index == size-1) + { + tail = pre; + } + } + size--; + + return result; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node temp = new Node(o,head); + head = temp; + size++; + + } + public void addLast(Object o){ + Node temp = new Node(o,null); + tail.next = temp; + tail = temp; + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + private Node current = head; + + @Override + public boolean hasNext() + { + return current!=null; + } + + @Override + public Object next() { + if(current==null) + { + throw new RuntimeException("Current element has not next."); + } + + Object result = current.data; + current = current.next; + return result; + } + + @Override + public void remove() { + + } + + } + + + private static class Node{ + Object data; + Node next; + public boolean hasNext(){ + return (this.next!=null); + } + + public Node(Object data,Node next){ + this.data=data; + this.next=next; + } + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + checkSize(); + int tempSize = size; + Node temp = new Node(removeFirst(),null); + tail = temp; + while(size>0){ + temp = new Node(removeFirst(),temp); + } + head = temp; + size = tempSize; + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + checkSize(); + if(size>1) + { + int temp = size; + for(int i=0;isize-i) + { + throw new RuntimeException("No enough size to remove from index:"+i); + }else{ + for(int j=0;j101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + if(size==0||list.size==0){ + return new int[0]; + }else{ + int[] result = new int[list.size()]; + Node temp = head; + int currentPos = 0; + for(int i=0;ival){ + break; + }else if(tempVal==val){ + remove(j); + break; + }else{ + continue; + } + } + } + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + Node temp = head; + while(temp!=null){ + while(temp.hasNext()&&temp.data.equals(temp.next.data)) + { + temp.next = temp.next.next; + size--; + } + temp = temp.next; + } + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + int headVal = (int)head.data; + int tailVal = (int)tail.data; + //if all the values in linkedList fall into the range, clean up; + if(min<=headVal && max>=tailVal) + { + System.out.println("min<=headVal && max>=tailVal"); + head = null; + tail = null; + size = 0; + }else{ + Node preRange = null; + Node sufRange = null; + Node temp = head; + int counter = 0; + while(temp!=null){ + if((int)temp.data=min) + { + preRange = temp; + System.out.println("Found preRange node, val="+temp.data+",next val="+temp.next.data); + } + if((int)temp.data>max){ + sufRange = temp; + System.out.println("Found sufRange node, val="+temp.data+",next val="+(temp.hasNext()?temp.next.data:null)); + break; + } + if((int)temp.data>=min && (int)temp.data<=max) + { + counter++; + } + System.out.println("Counter="+counter); + temp = temp.next; + } + if(min<=headVal){ + head = sufRange; + } + if(max>=tailVal){ + tail = preRange; + } + if(preRange!=null){ + preRange.next = sufRange; + } + size -= counter; + } + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + if(size==0 || list==null || list.size()==0) + { + return new LinkedList(); + }else{ + int pos1=0; + int pos2=0; + LinkedList result = new LinkedList(); + while(pos1val2) + { + if(pos2 downloadFinished = true); + downloader.execute(); + + // 绛夊緟澶氱嚎绋嬩笅杞界▼搴忔墽琛屽畬姣 + while (!downloadFinished) { + try { + System.out.println("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/Connection.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/Connection.java new file mode 100644 index 0000000000..98046b6c8f --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/Connection.java @@ -0,0 +1,23 @@ +package org.pan.coding2017.multThreadDownload.api; + +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(); +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/ConnectionException.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/ConnectionException.java new file mode 100644 index 0000000000..cb3a1b6d0a --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/ConnectionException.java @@ -0,0 +1,7 @@ +package org.pan.coding2017.multThreadDownload.api; + +public class ConnectionException extends Exception { + public ConnectionException(Exception e){ + super(e); + } +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/ConnectionManager.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/ConnectionManager.java new file mode 100644 index 0000000000..4ac353f33d --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package org.pan.coding2017.multThreadDownload.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/DownloadListener.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/DownloadListener.java new file mode 100644 index 0000000000..0de8c742d0 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/DownloadListener.java @@ -0,0 +1,5 @@ +package org.pan.coding2017.multThreadDownload.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/impl/ConnectionImpl.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/impl/ConnectionImpl.java new file mode 100644 index 0000000000..d8639a5b6b --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/impl/ConnectionImpl.java @@ -0,0 +1,67 @@ +package org.pan.coding2017.multThreadDownload.api.impl; + +import org.pan.coding2017.multThreadDownload.api.Connection; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.Arrays; + + +public class ConnectionImpl implements Connection { + + private URL url; + private static final int BUFFER_SIZE = 1024; + + ConnectionImpl(){} + + ConnectionImpl(String url) throws MalformedURLException { + this.url = new URL(url); + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream inputStream = urlConnection.getInputStream(); + + byte[] buff = new byte[BUFFER_SIZE]; + int totalSize = endPos - startPos + 1; + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + while (byteArrayOutputStream.size() < totalSize) { + int readSize = inputStream.read(buff); + if (readSize < 0) { + break; + } + byteArrayOutputStream.write(buff, 0, readSize); + } + byte[] data = byteArrayOutputStream.toByteArray(); + if (byteArrayOutputStream.size() > totalSize) { + return Arrays.copyOf(data, totalSize); + } + return data; + } + + @Override + public int getContentLength() { + URLConnection con; + try { + con = url.openConnection(); + return con.getContentLength(); + } catch (Exception e) { + e.printStackTrace(); + } + return -1; + } + + @Override + public void close() { + + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/impl/ConnectionManagerImpl.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..5a555652fc --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/multThreadDownload/api/impl/ConnectionManagerImpl.java @@ -0,0 +1,23 @@ +package org.pan.coding2017.multThreadDownload.api.impl; + + +import org.pan.coding2017.multThreadDownload.api.Connection; +import org.pan.coding2017.multThreadDownload.api.ConnectionManager; + +import java.io.IOException; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) { + try { + ConnectionImpl conn=null; + conn=new ConnectionImpl(url); + return conn; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java deleted file mode 100644 index f1679f6bef..0000000000 --- a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.pan.coding2017.utils; - -import java.io.FileWriter; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.io.OutputFormat; -import org.dom4j.io.SAXReader; -import org.dom4j.io.XMLWriter; - -public class Dom4JUtil { - - public static Document getDocument(String xmlPath) { - - try { - //鍒涘缓瑙f瀽鍣 - SAXReader saxReader = new SAXReader(); - //寰楀埌Documment - Document document = saxReader.read(xmlPath); - return document; - } catch (DocumentException e) { - e.printStackTrace(); - } - - return null; - } - - public static void xmlWrite(Document document,String xmlPath){ - - try { - OutputFormat format = OutputFormat.createPrettyPrint(); - XMLWriter xmlWriter = new XMLWriter(new FileWriter(xmlPath),format); - xmlWriter.write(document); - xmlWriter.close(); - } catch (Exception e) { - e.printStackTrace(); - } - - - } - -} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java deleted file mode 100644 index 8756c8ab95..0000000000 --- a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.pan.coding2017.utils; - -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; - -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; -import org.xml.sax.helpers.DefaultHandler; - -public class JaxpSAXPUtil { - public static void main(String[] args) { - /* - * 1銆佸垱寤鸿В鏋愬櫒宸ュ巶 - * 2銆佸垱寤鸿В鏋愬櫒 - * 3銆佹墽琛 parse 鏂规硶 - * - * 4銆佽嚜宸卞垱寤轰竴涓被銆佺户鎵緿efaultHandler - * 5銆侀噸鍐欑被閲岄潰鐨勪笁涓柟娉 - */ - - try { - SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); - SAXParser saxParser = saxParserFactory.newSAXParser(); - saxParser.parse("src/p1.xml",new MyDeafultHandler2() ); - } catch (Exception e) { - e.printStackTrace(); - } - - - } - -} - -class MyDeafultHandler1 extends DefaultHandler{ - - @Override - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - System.out.println("<"+qName+">"); - } - - @Override - public void characters(char[] ch, int start, int length) - throws SAXException { - System.out.println(new String(ch,start,length)); - } - - @Override - public void endElement(String uri, String localName, String qName) - throws SAXException { - System.out.println("<"+qName+"/>"); - } - -} - -//瀹炵幇鑾峰彇鎵鏈夌殑name鍏冪礌鐨勫 -class MyDeafultHandler2 extends DefaultHandler{ - - boolean flag = false; - int index = 1; - - @Override - public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException { - //鍒ゆ柇qName 鏄惁涓 name 鍏冪礌 - if("name".equals(qName) && index == 2){ - flag = true; - } - } - - @Override - public void characters(char[] ch, int start, int length) - throws SAXException { - if(flag == true){ - System.out.println(new String(ch, start, length)); - } - } - - @Override - public void endElement(String uri, String localName, String qName) - throws SAXException { - if("name".equals(qName)){ - flag = false; - index++ ; - } - } - - -} diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java index d10e2a1d48..135e052218 100644 --- a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java @@ -8,11 +8,11 @@ */ public class LinkedListTest { - LinkedList linkedList; + org.pan.coding2017.basic2.LinkedList linkedList; @Before public void setUp() throws Exception { - linkedList = new LinkedList(); + linkedList = new org.pan.coding2017.basic2.LinkedList(); linkedList.add(0); linkedList.add(1); linkedList.add(2); From 960309815940befbb304476fea4e103cbff79eae Mon Sep 17 00:00:00 2001 From: mengxz <82427129@qq.com> Date: Wed, 22 Mar 2017 14:18:01 +0800 Subject: [PATCH 135/155] homework -17/3/12 lastcommit,complished linkedlist's some method --- .../java/com/coding/basic/LinkedList.java | 252 +++++++++++++++--- .../java/com/coding/basic/LinkedListTest.java | 240 ++++++++++++++--- 2 files changed, 425 insertions(+), 67 deletions(-) diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java index 1996e43e91..efacab7da8 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java @@ -1,46 +1,149 @@ package com.coding.basic; -public class LinkedList implements List { +import java.util.LinkedHashSet; + +public class LinkedList implements List { - private Node head; + private Node head; + private int size; - public void add(Object o){ - + public LinkedList(){} + + public void add(E o){ + add(size, o); } - public void add(int index , Object o){ + public void add(int index , E o){ + rangeCheckforAdd(index); + if(index == 0){ + addFirst(o); + }else{ + addNext(index, o); + } } - public Object get(int index){ - return null; + + private void addNext(int index, E o) { + Node newNode = new Node(o, null); + Node prev = indexOf(index-1); + Node next = prev.next; + newNode.next = next; + prev.next = newNode; + + size++; } - public Object remove(int index){ - return null; + + public void addFirst(E o) { + Node newNode = new Node(o, null); + Node next = head; + head = newNode; + newNode.next = next; + + size++; } - public int size(){ - return -1; + public void addLast(E o){ + add(o); + } + private Node indexOf(int index){ + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + private void rangeCheck(int index){ + if(index >= size || index < 0) + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); + } + private void rangeCheckforAdd(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); } - public void addFirst(Object o){ + public E get(int index){ + rangeCheck(index); + return indexOf(index).data; } - public void addLast(Object o){ + public E remove(int index){ + rangeCheck(index); + if(index == 0){ + return removeFirst(); + }else{ + return removeNext(index); + } } - public Object removeFirst(){ - return null; + public void clear(){ + for (Node x = head; x!= null; ) { + Node next = x.next; + x.data = null; + x.next = null; + x = next; + } + size = 0; + head = null; } - public Object removeLast(){ - return null; + + private E removeNext(int index) { + final Node rv = indexOf(index); + final E element = rv.data; + final Node prev = indexOf(index-1); + prev.next = rv.next; + rv.next = null; + size--; + return element; + } + + public int size(){ + return size; + } + + public E removeFirst(){ + final E element = head.data; + final Node rv = head; + final Node next = rv.next; + head = next; + rv.next = null; + size --; + return element; + } + public E removeLast(){ + E e = remove(size-1); + return e; } public Iterator iterator(){ return null; } + @Override + public String toString() { + StringBuilder s = new StringBuilder(); + for (int i = 0; i < size; i++) { + s.append(get(i)).append(","); + } + return s.toString(); + } - - private static class Node{ - Object data; - Node next; + private static class Node{ + E data; + Node next; + public Node(E data,Node next) { + this.data = data; + this.next = next; + } + } + public class ListItr implements Iterator { + + + @Override + public boolean hasNext() { + return false; + } + + @Override + public Object next() { + return null; + } } @@ -48,8 +151,16 @@ private static class Node{ * 鎶婅閾捐〃閫嗙疆 * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 */ - public void reverse(){ - + public void reverse(){ + int i = 1; + if(size == 1 || size == 0){ + return; + } + while(i < size){ + E e = remove(i); + addFirst(e); + i++; + } } /** @@ -58,8 +169,11 @@ public void reverse(){ * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 */ - public void removeFirstHalf(){ - + public void removeFirstHalf(){ + int size = this.size>>1; + for (int i = 0; i < size; i++) { + removeFirst(); + } } /** @@ -67,8 +181,15 @@ public void removeFirstHalf(){ * @param i * @param length */ - public void remove(int i, int length){ + public void remove(int i, int length){ + if(i < 0 ) + throw new IndexOutOfBoundsException("i requested >= 0 ,i:"+i); + if(length < 0) + throw new IndexOutOfBoundsException("length requested > 0 ,length:"+length); + for (int j = 0; j < length&&j list){ + int[] r = new int[list.size]; + for (int i = 0; i < r.length; i++) { + int index = list.get(i); + if(index >= this.size()){ + r[i] = 0; + break; + }else{ + r[i] = (int) this.get(index); + } + } + return r; } /** @@ -89,16 +220,42 @@ public static int[] getElements(LinkedList list){ * @param list */ - public void subtract(LinkedList list){ - + public void subtract(LinkedList list){ + int i = 0;//this list's index + while(i < this.size){ + boolean flag = false; + int j = 0;//parameter list's index + E e = get(i); + + while(j< list.size){ + if(e.equals(list.get(j))){ + remove(i); + flag = true; + break; + }else{ + j++; + } + } + if(!flag){ + i++; + } + } } /** * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 */ - public void removeDuplicateValues(){ - + public void removeDuplicateValues(){ + LinkedHashSet set = new LinkedHashSet(); + for (int i = 0; i < size; i++) { + set.add(get(i)); + } + clear(); + java.util.Iterator iterator = set.iterator(); + while(iterator.hasNext()){ + add(iterator.next()); + } } /** @@ -107,8 +264,16 @@ public void removeDuplicateValues(){ * @param min * @param max */ - public void removeRange(int min, int max){ - + public void removeRange(int min, int max){ + int i = 0; + while(i < size){ + int e = (int)get(i); + if(e > min && e < max){ + remove(i); + }else{ + i++; + } + } } /** @@ -116,7 +281,22 @@ public void removeRange(int min, int max){ * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 * @param list */ - public LinkedList intersection( LinkedList list){ - return null; + public LinkedList intersection( LinkedList list){ + LinkedList linkedList = new LinkedList(); + int i = 0;//this list's index + int j = 0;//parameter list's index + int j2= 0; + while( i < this.size ){ + E e = get(i++); + j = j2+1; + while(j < list.size ){ + if(e.equals(list.get(j))){ + linkedList.add(e); + j2 = j; + } + j++; + } + } + return linkedList; } } diff --git a/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java index 304367a0a4..fa76fe64b1 100644 --- a/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java +++ b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java @@ -1,81 +1,259 @@ package com.coding.basic; -import static org.junit.Assert.*; - import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class LinkedListTest { - private DoubleNodeLinkedList list; + private LinkedList l =null; @Before public void setUp() throws Exception { - list = new DoubleNodeLinkedList(); + l = new LinkedList() ; } @After public void tearDown() throws Exception { - list = null; + l = null; } @Test - public void testAddObject() { - list.add(100); - Assert.assertEquals(1, list.size()); - list.add(100); - Assert.assertEquals(2, list.size()); - list.add(100); - Assert.assertEquals(3, list.size()); + public void testAddE() { + l.add("1"); + Assert.assertTrue(l.size() == 1); + l.add("1"); + Assert.assertTrue(l.size() == 2); + l.add("1"); + Assert.assertTrue(l.size() == 3); } @Test - public void testAddIntObject() { - for (int i = 0; i < 5; i++) { - list.add(i, i); - } - Assert.assertEquals(5, list.size()); + public void testAddIntE() { + l.add("1"); + l.add("1"); + l.add("1"); + l.add("1"); + l.add(2,"2"); + Assert.assertTrue(l.size() == 5); } @Test - public void testGet() { - + public void testAddFirst() { + l.add("1"); + l.add("1"); + l.add("1"); + l.add("1"); + l.addFirst("first"); + Assert.assertTrue(l.size() == 5); } @Test - public void testRemove() { - fail("Not yet implemented"); + public void testAddLast() { + l.add("1"); + l.add("1"); + l.add("1"); + l.add("1"); + l.addLast("first"); + Assert.assertTrue(l.size() == 5); } @Test - public void testSize() { - fail("Not yet implemented"); + public void testGet() { + l.add("1"); + l.add("1"); + l.add("1"); + l.add("1"); + l.addLast("first"); + Assert.assertTrue(l.get(4).equals("first")); + Assert.assertTrue(l.get(3).equals("1")); } @Test - public void testAddFirst() { - fail("Not yet implemented"); + public void testRemoveInt() { + l.add("1"); + l.add("2"); + l.add("3"); + l.add("4"); + Assert.assertTrue(l.size()==4); + l.remove(2); + Assert.assertTrue(l.size()==3); + Assert.assertTrue(l.get(2).equals("4")); + } + @Test + public void testClear() { + l.add("1"); + l.add("2"); + l.add("3"); + l.add("4"); + Assert.assertTrue(l.size()==4); + l.clear(); + Assert.assertTrue(l.size()==0); } @Test - public void testAddLast() { - fail("Not yet implemented"); + public void testSize() { + Assert.assertTrue(l.size()==0); + l.add("1"); + l.add("2"); + l.add("3"); + l.add("4"); + Assert.assertTrue(l.size()==4); } @Test public void testRemoveFirst() { - fail("Not yet implemented"); + l.add("1"); + l.add("2"); + l.add("3"); + l.add("4"); + l.removeFirst(); + Assert.assertTrue(l.get(0).equals("2")); } @Test public void testRemoveLast() { - fail("Not yet implemented"); + l.add("1"); + l.add("2"); + l.add("3"); + l.add("4"); + Assert.assertTrue(l.size()==4); + Assert.assertTrue(l.get(2).equals("3")); + l.removeLast(); + Assert.assertTrue(l.size()==3); + Assert.assertTrue(l.get(0).equals("1")); + Assert.assertTrue(l.get(1).equals("2")); + Assert.assertTrue(l.get(2).equals("3")); + } + + @Test + public void testReverse() { + l.add("1"); + l.add("2"); + l.add("3"); + l.add("4"); + Assert.assertTrue(l.get(0).equals("1")); + Assert.assertTrue(l.get(1).equals("2")); + Assert.assertTrue(l.get(2).equals("3")); + Assert.assertTrue(l.get(3).equals("4")); + l.reverse(); + Assert.assertTrue(l.get(3).equals("1")); + Assert.assertTrue(l.get(2).equals("2")); + Assert.assertTrue(l.get(1).equals("3")); + Assert.assertTrue(l.get(0).equals("4")); + } + + @Test + public void testRemoveFirstHalf() { + l.add("2"); + l.add("5"); + l.add("7"); + l.add("8"); + l.add("9"); + Assert.assertTrue(l.size()==5); + l.removeFirstHalf(); + Assert.assertTrue(l.size()==3); + Assert.assertTrue(l.get(0).equals("7")); + Assert.assertTrue(l.get(1).equals("8")); + Assert.assertTrue(l.get(2).equals("9")); + } + + @Test + public void testRemoveIntInt() { + l.add("2"); + l.add("5"); + l.add("7"); + l.add("8"); + l.add("9"); + l.remove(2, 2); + Assert.assertTrue(l.get(0).equals("2")); + Assert.assertTrue(l.get(1).equals("5")); + Assert.assertTrue(l.get(2).equals("9")); + Assert.assertTrue(l.size()==3); + } + + @Test + public void testGetElements() { + LinkedList l = new LinkedList(); + l.add(2); + l.add(5); + l.add(7); + l.add(8); + l.add(9); + LinkedList l2 = new LinkedList(); + l2.add(1); + l2.add(2); + l2.add(4); + int[] elements = l.getElements(l2); + int[] exp = {5,7,9}; + Assert.assertArrayEquals(exp, elements); + } + + @Test + public void testSubtract() { + l.add("2"); + l.add("5"); + l.add("7"); + l.add("8"); + l.add("9"); + LinkedList l2 = new LinkedList(); + l2.add("2"); + l2.add("9"); + l2.add("8"); + l.subtract(l2); + Assert.assertTrue(l.size()==2); + Assert.assertTrue(l.get(0).equals("5")); + Assert.assertTrue(l.get(1).equals("7")); + } + + @Test + public void testRemoveDuplicateValues() { + l.add("2"); + l.add("5"); + l.add("5"); + l.add("8"); + l.add("8"); + l.removeDuplicateValues(); + Assert.assertTrue(l.size()==3); + Assert.assertTrue(l.get(0).equals("2")); + Assert.assertTrue(l.get(1).equals("5")); + Assert.assertTrue(l.get(2).equals("8")); + } + + @Test + public void testRemoveRange() { + LinkedList l = new LinkedList(); + l.add(2); + l.add(5); + l.add(7); + l.add(8); + l.add(9); + System.out.println(l); + l.removeRange(3, 7); + System.out.println(l); + Assert.assertTrue(l.size()==4); + Assert.assertTrue(l.get(0)==2); + Assert.assertTrue(l.get(1)==7); + Assert.assertTrue(l.get(2)==8); + Assert.assertTrue(l.get(3)==9); } @Test - public void testIterator() { - fail("Not yet implemented"); + public void testIntersection() { + l.add("2"); + l.add("5"); + l.add("7"); + l.add("8"); + l.add("9"); + LinkedList l2 = new LinkedList(); + l2.add("1"); + l2.add("2"); + l2.add("3"); + l2.add("5"); + LinkedList intersection = l.intersection(l2); + Assert.assertTrue(intersection.size()==2); + Assert.assertTrue(intersection.get(0).equals("2")); + Assert.assertTrue(intersection.get(1).equals("5")); } } From 83174b37f8d19fb5f0cac9007edb5a60c34f06f5 Mon Sep 17 00:00:00 2001 From: DonaldY <448641125@qq.com> Date: Wed, 22 Mar 2017 15:31:25 +0800 Subject: [PATCH 136/155] use TDD --- .../com/donaldy/litestruts/Configuration.java | 116 +++++++++++++++++ .../litestruts/ConfigurationException.java | 21 +++ .../donaldy/litestruts/ConfigurationTest.java | 35 +++++ .../litestruts/ReflectionUtiilTest.java | 99 ++++++++++++++ .../donaldy/litestruts/ReflectionUtil.java | 121 ++++++++++++++++++ .../src/com/donaldy/litestruts/Struts.java | 57 ++++++++- 6 files changed, 444 insertions(+), 5 deletions(-) create mode 100644 group24/448641125/src/com/donaldy/litestruts/Configuration.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/ConfigurationException.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/ConfigurationTest.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/ReflectionUtiilTest.java create mode 100644 group24/448641125/src/com/donaldy/litestruts/ReflectionUtil.java diff --git a/group24/448641125/src/com/donaldy/litestruts/Configuration.java b/group24/448641125/src/com/donaldy/litestruts/Configuration.java new file mode 100644 index 0000000000..69415bf1fc --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/Configuration.java @@ -0,0 +1,116 @@ +package com.donaldy.litestruts; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by donal on 2017/3/21. + */ +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); + } + } + + 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 getResultView(String action, String resultName) { + ActionConfig ac = this.actions.get(action); + if(ac == null){ + return null; + } + return ac.getViewName(resultName); + } + + //涔嬫墍浠ヤ负鍐呴儴绫伙紝鑷冲皯鐜板湪鐪嬫潵澶栫晫骞朵笉闇瑕併 + private static class ActionConfig { + String name; + String clzName; + Map viewResult = new HashMap<>(); + + public ActionConfig (String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + + 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/group24/448641125/src/com/donaldy/litestruts/ConfigurationException.java b/group24/448641125/src/com/donaldy/litestruts/ConfigurationException.java new file mode 100644 index 0000000000..fed8e00df5 --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/ConfigurationException.java @@ -0,0 +1,21 @@ +package com.donaldy.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); + } + +} diff --git a/group24/448641125/src/com/donaldy/litestruts/ConfigurationTest.java b/group24/448641125/src/com/donaldy/litestruts/ConfigurationTest.java new file mode 100644 index 0000000000..4ef2386762 --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/ConfigurationTest.java @@ -0,0 +1,35 @@ +package com.donaldy.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by donal on 2017/3/21. + */ +public class ConfigurationTest { + + Configuration cfg = new Configuration("struts.xml"); + + @Test + public void testGetClassName() { + String clzName = cfg.getClassName("login"); + Assert.assertEquals("com.donaldy.litestruts.LoginAction", clzName); + clzName = cfg.getClassName("logout"); + Assert.assertEquals("com.donaldy.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/group24/448641125/src/com/donaldy/litestruts/ReflectionUtiilTest.java b/group24/448641125/src/com/donaldy/litestruts/ReflectionUtiilTest.java new file mode 100644 index 0000000000..9c51567276 --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/ReflectionUtiilTest.java @@ -0,0 +1,99 @@ +package com.donaldy.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.*; + +/** + * Created by donal on 2017/3/21. + */ +public class ReflectionUtiilTest { + + @Test + public void testGetSetterMethod() throws Exception { + + String name = "com.coderising.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.coderising.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.coderising.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 testGetParamters() throws Exception{ + String name = "com.donaldy.litestruts.LoginAction"; + Class clz = Class.forName(name); + LoginAction action = (LoginAction)clz.newInstance(); + action.setName("test"); + action.setPassword("123456"); + + + + + Map params = ReflectionUtil.getParamterMap(action); + + Assert.assertEquals(3, params.size()); + + Assert.assertEquals(null, params.get("messaage") ); + Assert.assertEquals("test", params.get("name") ); + Assert.assertEquals("123456", params.get("password") ); + } +} diff --git a/group24/448641125/src/com/donaldy/litestruts/ReflectionUtil.java b/group24/448641125/src/com/donaldy/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..764f1dd09f --- /dev/null +++ b/group24/448641125/src/com/donaldy/litestruts/ReflectionUtil.java @@ -0,0 +1,121 @@ +package com.donaldy.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by donal on 2017/3/21. + */ +public class ReflectionUtil { + public static List getSetterMethods(Class clz) { + + 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){ + + 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"); + } + + private 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 getParamterMap(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; + } + + ////////////////////////Backup /////////////////////////////////// + + public static List getGetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("get")){ + + methods.add(m); + + } + + } + + return methods; + } + + public static List getSetterMethods_V1(Class clz) { + + List methods = new ArrayList<>(); + + for(Method m : clz.getDeclaredMethods()){ + + if(m.getName().startsWith("set")){ + + methods.add(m); + + } + + } + + return methods; + + } +} diff --git a/group24/448641125/src/com/donaldy/litestruts/Struts.java b/group24/448641125/src/com/donaldy/litestruts/Struts.java index 48cb164edc..38d150ebb8 100644 --- a/group24/448641125/src/com/donaldy/litestruts/Struts.java +++ b/group24/448641125/src/com/donaldy/litestruts/Struts.java @@ -1,5 +1,6 @@ package com.donaldy.litestruts; +import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; @@ -15,21 +16,67 @@ public class Struts { + + private final static Configuration cfg = new Configuration("struts.xml"); + public static View runAction(String actionName, Map parameters) { + + String clzName = cfg.getClassName(actionName); + + if (clzName == null) { + return null; + } + + try { + 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 jsp = cfg.getResultView(actionName, resultName); + + Map params = ReflectionUtil.getParamterMap(action); + + View view = new View(); + view.setJsp(jsp); + view.setParameters(params); + + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + + return null; + } + + /*public static View runAction(String actionName, Map parameters) { Element rootElement = null; try { - /** + *//** * 0.璇诲彇閰嶇疆鏂囦欢 - */ + *//* rootElement = readStrutsXml().getRootElement(); } catch (DocumentException e) { e.printStackTrace(); } - /** + *//** * 1.鏍规嵁actionName鎵惧埌class * 骞惰缃 - */ + *//* String classPath = findClass(actionName, rootElement); return handle(classPath, parameters, rootElement); @@ -133,6 +180,6 @@ private static Map getMap(Class newClass, Object action) { } return map; - } + }*/ } From 4781ae6b86f43c8b7e3bfa5b445c1aab14c15e74 Mon Sep 17 00:00:00 2001 From: fzon0902 <815591664@qq.com> Date: Wed, 22 Mar 2017 16:49:24 +0800 Subject: [PATCH 137/155] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/array/ArrayUtil.java | 348 ++++++++++++++++++ .../coderising/litestruts/LoginAction.java | 39 ++ .../src/com/coderising/litestruts/Struts.java | 137 +++++++ .../com/coderising/litestruts/StrutsTest.java | 43 +++ .../src/com/coderising/litestruts/View.java | 23 ++ .../src/com/coding/basic/ArrayList.java | 134 +++++++ .../src/com/coding/basic/BinaryTree.java | 35 ++ .../src/com/coding/basic/BinaryTreeNode.java | 109 ++++++ .../src/com/coding/basic/Iterator.java | 9 + .../src/com/coding/basic/LinkedList.java | 223 +++++++++++ .../src/com/coding/basic/List.java | 19 + .../src/com/coding/basic/Queue.java | 27 ++ .../src/com/coding/basic/Stack.java | 29 ++ .../src/com/coding/basic/TestArrayList.java | 84 +++++ 14 files changed, 1259 insertions(+) create mode 100644 group24/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java create mode 100644 group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java create mode 100644 group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java create mode 100644 group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group24/815591664/2017Learning/src/com/coderising/litestruts/View.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/BinaryTree.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/Iterator.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/List.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/Queue.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/Stack.java create mode 100644 group24/815591664/2017Learning/src/com/coding/basic/TestArrayList.java diff --git a/group24/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java b/group24/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7c30103d89 --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,348 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; + +import java.util.List; + + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + + public static void main(String[] args) { + int[] a = {7, 9, 30, 3, 4}; + reverseArray(a); + System.out.println(Arrays.toString(a)); + + + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5,0} ; + System.out.println(Arrays.toString(removeZero(oldArr))); + + + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + + System.out.println(Arrays.toString(merge(a1,a2))); + + + int[] b = { 2,3,6}; + System.out.println(Arrays.toString(grow(b,5))); + + System.out.println(genFibonacci(5)); + + System.out.println(Arrays.toString(fibonacci(30))); + + System.out.println(Arrays.toString(getPrimes(10000))); + + System.out.println(getFactor(10)); + + System.out.println(isPerfectNum(1000)); + +// System.out.println(); + System.out.println(Arrays.toString(getPerfectNumbers(100))); + + System.out.println(join(a,"&")); + + + } + public static void reverseArray(int[] origin){ + + if(origin.length==0){ + return; + + } + int[] copy = new int[origin.length]; + System.arraycopy(origin, 0, copy, 0, origin.length); + + for (int i = 0; i < copy.length; i++) { + + origin[i] = copy[copy.length-1-i]; + } + + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + int newSize = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + newSize++; + } + } + int index = 0; + int[] newArr = new int[newSize]; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + newArr[index] = oldArray[i]; + index++; + } + } + return newArr; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + Arrays.sort(array1); + Arrays.sort(array2); + + int[] newArr = new int[array1.length+array2.length]; + + System.arraycopy(array1, 0, newArr, 0,array1.length ); + System.arraycopy(array2, 0, newArr, array1.length, array2.length); + Arrays.sort(newArr); + List list = new ArrayList(); + for(int i=0;i list = new ArrayList(); + for(int i =0;i list = new ArrayList(); + for(int i=2;i<=max;i++){ + if(isPrime(i)){ + list.add(i); + } + } + + return listToArray(list); + + + } + + public static int[] listToArray(List list){ + if(list == null){ + return null; + } + + int[] arr = new int[list.size()]; + + for(int i=0;i list = new ArrayList(); + for(int i=1;i<=max;i++){ + if(isPerfectNum(i)){ + list.add(i); + } + } + + return listToArray(list); + } + + + public static boolean isPerfectNum(int num){ + if(num <=1){ + return false; + } + + List factors = getFactor(num); + int sum = 0; + for (Integer integer : factors) { + sum = integer+sum; + } + if(sum == num){ + return true; + } + return false; + } + + public static List getFactor(int num){ + List list = new ArrayList(); + list.add(1); + + + for(int i=2;i getFactor(int num){ + List list = new ArrayList(); + list.add(1); + int temp = num; + + while(!isPrime(temp)){ + if(temp ==1){ + break; + } + for(int i=2;i<=temp;i++){ + if(temp % i ==0){ + list.add(i); + temp = temp/i; + break; + } + } + + } + list.add(temp); + + return list; + }*/ + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i); + sb.append(seperator); + + } + + return sb.subSequence(0, sb.length()-1).toString(); + } + + +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..550d47ec2b --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,137 @@ +package com.coderising.litestruts; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.input.SAXBuilder; +import org.jdom2.output.XMLOutputter; + +import com.sun.istack.internal.Builder; + + + +public class Struts { + + @SuppressWarnings("deprecation") + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view = new View(); + String xmlpath = Struts.class.getResource("struts.xml").getFile(); + SAXBuilder builder = null; + try { + xmlpath = URLDecoder.decode(xmlpath, "utf-8"); + builder = new SAXBuilder(false); + Document doc = builder.build(xmlpath); + Element root = doc.getRootElement(); + Element action = root.getChild("action"); + String className = action.getAttributeValue("class"); + Class clazz = Class.forName(className); + Object logAction = clazz.newInstance(); + for(String key :parameters.keySet()){ + String methodName = "set"+ Struts.captureName(key); + Method method = null; + try { + method = clazz.getMethod(methodName, String.class); + } catch (SecurityException e) { + e.printStackTrace(); + break; + } catch (NoSuchMethodException e) { + break; + } + method.invoke(logAction, parameters.get(key)); + + } + Method executeMethod = clazz.getMethod("execute", (Class[])null); + String result = (String)executeMethod.invoke(logAction, (Object[])null); + Method[] declareMtds = clazz.getDeclaredMethods(); + Map paramForView = new HashMap(); + for (Method method : declareMtds) { + String methodName = method.getName(); + if(methodName.startsWith("get")){ + paramForView.put(methodName.substring(3).toLowerCase(), (String)method.invoke(logAction, (Object[])null)); + } + + } + view.setParameters(paramForView); + List results = action.getChildren("result"); + for (Element element : results) { + String resultName = element.getAttributeValue("name"); + if(result.equals(resultName)){ + view.setJsp(element.getText()); + } + } + + } catch (JDOMException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return view; + } + + + public static String captureName(String name) { + // name = name.substring(0, 1).toUpperCase() + name.substring(1); +// return name; + char[] cs=name.toCharArray(); + cs[0]-=32; + return String.valueOf(cs); + + + } + +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/group24/815591664/2017Learning/src/com/coderising/litestruts/View.java b/group24/815591664/2017Learning/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java b/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..e5fec6fceb --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,134 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.LinkedList; + +/** + * @author hugaoqing + * created on 2017-3-8 + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[3]; + + /* + * 娣诲姞鍏冪礌 + * + */ + public void add(Object o){ + /*if(elementData.length == size){ + Object[] buffer = new Object[size+15]; + System.arraycopy(elementData, 0, buffer, 0, size); + elementData = buffer; + elementData[size] = o; + size++; + }else { + + elementData[size] = o; + size++; + }*/ + add(size, o); + + } + + + /* + * + * 鎸囧畾浣嶇疆娣诲姞鍏冪礌 + */ + public void add(int index, Object o){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException(); + } + if(size+1=size){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException(); + } + Object out = elementData[index]; + Object[] temp = new Object[size-index-1]; + System.arraycopy(elementData, index+1, temp, 0, size-index-1); + System.arraycopy(temp, 0, elementData,index, size-index-1); + //灏嗙Щ闄ょ殑鍏冪礌鐨勬寚閽堝幓鎺夛紝閬垮厤鍐呭瓨娉勬紡 + elementData[size-1] = null; + size--; + return out; + } + + public int size(){ + return this.size; + } + + @Override + public String toString() { + Object[] objs = new Object[size]; + System.arraycopy(elementData, 0,objs , 0, size); + return Arrays.toString(objs); + + } + public Iterator iterator(){ + return new Iterator() { + int cursor = 0; + public Object next() throws Exception { + cursor++; + return get(cursor-1); + } + + public boolean hasNext() { + + return this.cursor0){ + while(curNode.getRight()!=null){ + curNode = curNode.getRight(); + } + curNode = node; + + }else{ + while(curNode.getLeft()!=null){ + curNode = curNode.getLeft(); + } + curNode = node; + } + + } + return null; + } + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java b/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4bd92572c9 --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,109 @@ +package com.coding.basic; + + + +/** + * @author hugaoqing + * created on 2017-3-11 + */ +public class BinaryTreeNode { + + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Comparable data2) { + // TODO Auto-generated constructor stub + } + public BinaryTreeNode() { + // TODO Auto-generated constructor stub + } + /*public BinaryTreeNode(Comparable data) { + super(); + this.data = data; + }*/ + public Comparable getData() { + return data; + } + public void setData(Comparable data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Comparable data){ + /*if(this.data==null){ + return new BinaryTreeNode(o); + } + + BinaryTreeNode curNode = this; + while(curNode != null){ + if(curNode.data.compareTo(o) == 0){ + return curNode; + } + else if(o.compareTo(curNode.data) < 0){ + BinaryTreeNode node = curNode; + curNode = curNode.left; + if(curNode == null){ + curNode = new BinaryTreeNode(o); + node.left = curNode; + return curNode; + } + + + }else if(o.compareTo(curNode.data) > 0){ + BinaryTreeNode node = curNode; + curNode = curNode.right; + if(curNode == null){ + curNode = new BinaryTreeNode(o); + node.right = curNode; + return curNode; + } + } + } + return curNode;*/ + BinaryTreeNode curNode = this; + BinaryTreeNode insertNode = new BinaryTreeNode(); + insertNode.setData(data); + + while(curNode != null){ + if(null == curNode.data){ + curNode.setData(data); + break; + }else{ + Comparable dataOfNode = curNode.getData(); + if(dataOfNode.compareTo(data) == 0){ + break; + }else if(dataOfNode.compareTo(data) < 0){ + BinaryTreeNode leftNode = curNode.left; + if(null == leftNode){ + curNode.setLeft(insertNode); + break; + } + curNode = leftNode; + }else{ + BinaryTreeNode rightNode = curNode.right; + if(null == rightNode){ + curNode.setRight(insertNode); + break; + } + curNode = rightNode; + } + } + } + return insertNode; + + + } + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java b/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d4656a7daf --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface Iterator { + //接口里的成员变量默认都是final static的 +// int cursor = 0; + public boolean hasNext(); + public Object next() throws Exception; + +} diff --git a/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java b/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..3d7e06e19f --- /dev/null +++ b/group24/815591664/2017Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,223 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + + public void add(Object o){ + this.addLast(o); + + } + public void add(int index , Object o){ + if(index<0 || index>size){ + throw new IndexOutOfBoundsException(); + } + if(index==0){ + this.addFirst(o); + size++; + return; + }else if(index==size){ + this.addLast(o); + size++; + return; + } + Node preNode = this.getNode(index-1); + Node curNode = this.getNode(index); + Node newNode = new Node(o, curNode); + preNode.next = newNode; + + + size++; + + + } + + private Node getNode(int index){ + if(index <0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + if(index ==0){ + return head; + } + Node curNode = head; + for(int i=1;i<=index;i++){ + curNode = head.next; + } + return curNode; + } + + public Object get(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + + Node temp = head; + for(int i =1;i<=index;i++){ + temp = temp.next; + } + return temp.data; + } + public Object remove(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); + } + Object o = null; + if(size == 1){ + o = head.data; + size--; + return o; + } + if(index==0){ + o = head.data; + Node afterHead = head.next; + head = afterHead; + + }else if(index==size-1){ + Node preTail = getNode(index-1); + Node tail = preTail.next; + o = tail.data; + preTail.next = null; + }else{ + Node preCur = getNode(index-1); + Node cur = preCur.next; + Node nextCur = cur.next; + o = cur.data; + preCur.next = nextCur; + + } + size--; + return o; + + + + + + + + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node node = new Node(o,null); + + if(head == null){ + head = node; + size++; + return; + } + head = new Node(o, head); + size++; + + } + public void addLast(Object o){ + //新节点的next指针指向tail + Node add = new Node(o, null); + if(head==null){ + head = add; + size++; + return; + } + Node curNode = head; + while(curNode.next != null){ + curNode = curNode.next; + } + + curNode.next = add; + size++; + } + + + public Object removeFirst() throws Exception{ + return this.remove(0); + } + public Object removeLast() throws Exception{ + return this.remove(size-1); + } + + private class Itr implements Iterator{ + int cursor = 0; + public boolean hasNext() { + return cursor Date: Wed, 22 Mar 2017 23:37:30 +0800 Subject: [PATCH 138/155] refactor litestructs --- .../com/coderising/{ => litestruts}/action/LoginAction.java | 2 +- .../code/homework/coderising/src/main/resources/struts.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename group17/1204187480/code/homework/coderising/src/main/java/com/coderising/{ => litestruts}/action/LoginAction.java (95%) diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/action/LoginAction.java similarity index 95% rename from group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java rename to group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/action/LoginAction.java index 5496d8084d..85ae4dc47c 100644 --- a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/action/LoginAction.java @@ -1,4 +1,4 @@ -package com.coderising.action; +package com.coderising.litestruts.action; /** * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 diff --git a/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml b/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml index dd598a3664..c7ee86e436 100644 --- a/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml +++ b/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml @@ -1,10 +1,10 @@ - + /jsp/homepage.jsp /jsp/showLogin.jsp - + /jsp/welcome.jsp /jsp/error.jsp From baa210ca7c73bf0e5773adc142c24766bc1363eb Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Wed, 22 Mar 2017 23:52:25 +0800 Subject: [PATCH 139/155] add new LinkedList --- .../java/com/coding/basic/LinkedList.java | 294 +++++++++--------- .../coding/basic/algorithm/LinkedList.java | 125 ++++++++ 2 files changed, 272 insertions(+), 147 deletions(-) create mode 100644 group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/algorithm/LinkedList.java diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java index 7174cb9cdf..175d3adb74 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java @@ -1,147 +1,147 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private int size = 0; - private Iterator iterator = new LinkedListIterator(); - - - public void add(Object o) { - Node newNode = new Node(o, null); - if (head == null) { - head = newNode; - } else { - node(size - 1).next = newNode; - } - size++; - } - - public void add(int index, Object o) { - checkForAdd(index); - if (index == size) { - add(o); - }else { - Node newNode = new Node(o, null); - if (index == 0){ - addFirst(o); - } else { - Node preNode = node(index - 1); - Node now = preNode.next; - preNode.next = newNode; - newNode.next = now; - size++; - } - } - - } - - private Node node(int index) { - Node x = head; - for (int i = 0; i < index; i++) { - x = x.next; - } - return x; - } - - public Object get(int index) { - checkIndex(index); - return node(index).data; - } - - /** - * 璁╄鍒犻櫎鐨勫紩鐢ㄧ殑鎸佹湁鑰呮寚鍚戜笅涓涓妭鐐 - * @param index - * @return - */ - public Object remove(int index) { - final Object ret; - checkIndex(index); - if (index == 0) { - Node removeNode = head; - ret = head.data; - head = removeNode.next; - } else { - Node pre = node(index - 1); - Node removeNode = pre.next; - ret = removeNode.data; - pre.next = removeNode.next; - } - size--; - return ret; - } - - public int size() { - return size; - } - - public void addFirst(Object o) { - head = new Node(o, head);; - size++; - } - - public void addLast(Object o) { - add(o); - } - - public Object removeFirst() { - if (size == 0){ - return null; - }else { - return remove(0); - } - } - - public Object removeLast() { - return remove(size - 1); - } - - public Iterator iterator() { - return iterator; - } - - private void checkIndex(int index) { - if (index < 0 || index >= size) { - throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); - } - } - - private void checkForAdd(int index) { - if (index < 0 || index > size) { - throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); - } - } - - private static class Node { - Object data; - Node next; - - public Node() { - } - - public Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - - private class LinkedListIterator implements Iterator{ - - private Node next; - - @Override - public boolean hasNext() { - return next != null; - } - - @Override - public Object next() { - if (next == null) { - throw new IndexOutOfBoundsException("there is no node in list"); - } - Node ret = next; - next = next.next; - return ret.data; - } - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + private Iterator iterator = new LinkedListIterator(); + + + public void add(Object o) { + Node newNode = new Node(o, null); + if (head == null) { + head = newNode; + } else { + node(size - 1).next = newNode; + } + size++; + } + + public void add(int index, Object o) { + checkForAdd(index); + if (index == size) { + add(o); + }else { + Node newNode = new Node(o, null); + if (index == 0){ + addFirst(o); + } else { + Node preNode = node(index - 1); + Node now = preNode.next; + preNode.next = newNode; + newNode.next = now; + size++; + } + } + + } + + private Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } + + public Object get(int index) { + checkIndex(index); + return node(index).data; + } + + /** + * 璁╄鍒犻櫎鐨勫紩鐢ㄧ殑鎸佹湁鑰呮寚鍚戜笅涓涓妭鐐 + * @param index + * @return + */ + public Object remove(int index) { + final Object ret; + checkIndex(index); + if (index == 0) { + Node removeNode = head; + ret = head.data; + head = removeNode.next; + } else { + Node pre = node(index - 1); + Node removeNode = pre.next; + ret = removeNode.data; + pre.next = removeNode.next; + } + size--; + return ret; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + head = new Node(o, head);; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + if (size == 0){ + return null; + }else { + return remove(0); + } + } + + public Object removeLast() { + return remove(size - 1); + } + + public Iterator iterator() { + return iterator; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + private void checkForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + private static class Node { + Object data; + Node next; + + public Node() { + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator{ + + private Node next; + + @Override + public boolean hasNext() { + return next != null; + } + + @Override + public Object next() { + if (next == null) { + throw new IndexOutOfBoundsException("there is no node in list"); + } + Node ret = next; + next = next.next; + return ret.data; + } + } +} diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/algorithm/LinkedList.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/algorithm/LinkedList.java new file mode 100644 index 0000000000..de2a1a15b9 --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/algorithm/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.algorithm; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺listB鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪listB涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} From d647ebd3b6d2523d6ab6b7edd18450db2dcd267b Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Wed, 22 Mar 2017 23:55:39 +0800 Subject: [PATCH 140/155] add new LinkedList2 --- .../java/com/coding/basic/LinkedList.java | 96 +++++++++++++- .../coding/basic/algorithm/LinkedList.java | 125 ------------------ 2 files changed, 89 insertions(+), 132 deletions(-) delete mode 100644 group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/algorithm/LinkedList.java diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java index 175d3adb74..f2e6b4f77a 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java @@ -21,10 +21,10 @@ public void add(int index, Object o) { checkForAdd(index); if (index == size) { add(o); - }else { + } else { Node newNode = new Node(o, null); - if (index == 0){ - addFirst(o); + if (index == 0) { + addFirst(o); } else { Node preNode = node(index - 1); Node now = preNode.next; @@ -51,6 +51,7 @@ public Object get(int index) { /** * 璁╄鍒犻櫎鐨勫紩鐢ㄧ殑鎸佹湁鑰呮寚鍚戜笅涓涓妭鐐 + * * @param index * @return */ @@ -76,7 +77,8 @@ public int size() { } public void addFirst(Object o) { - head = new Node(o, head);; + head = new Node(o, head); + ; size++; } @@ -85,9 +87,9 @@ public void addLast(Object o) { } public Object removeFirst() { - if (size == 0){ + if (size == 0) { return null; - }else { + } else { return remove(0); } } @@ -112,6 +114,86 @@ private void checkForAdd(int index) { } } + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse() { + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + */ + public void removeFirstHalf() { + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + + } + + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺listB鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪listB涓嚭鐜扮殑鍏冪礌 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues() { + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } + private static class Node { Object data; Node next; @@ -125,7 +207,7 @@ public Node(Object data, Node next) { } } - private class LinkedListIterator implements Iterator{ + private class LinkedListIterator implements Iterator { private Node next; diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/algorithm/LinkedList.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/algorithm/LinkedList.java deleted file mode 100644 index de2a1a15b9..0000000000 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/algorithm/LinkedList.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.coding.basic.algorithm; - -import com.coding.basic.Iterator; -import com.coding.basic.List; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - /** - * 鎶婅閾捐〃閫嗙疆 - * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 - */ - public void reverse(){ - - } - - /** - * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 - * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 - * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 - * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺listB鎵鎸囧畾鐨勫厓绱 - * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - return null; - } - - /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪listB涓嚭鐜扮殑鍏冪礌 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 - */ - public void removeDuplicateValues(){ - - } - - /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 - * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} From 6ea628e149d1d5e85ac00ed5f8565e8729a61308 Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Thu, 23 Mar 2017 00:29:53 +0800 Subject: [PATCH 141/155] add test for arrayList --- .../java/com/coding/basic/LinkedList.java | 18 +++++++++++++ .../java/com/coding/basic/LinkedListTest.java | 27 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/LinkedListTest.java diff --git a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java index f2e6b4f77a..6a94ded0a9 100644 --- a/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java +++ b/group17/1204187480/code/homework/basic/src/main/java/com/coding/basic/LinkedList.java @@ -119,6 +119,24 @@ private void checkForAdd(int index) { * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 */ public void reverse() { + if (size == 0) { + return; + } + Node[] nodes = new Node[size]; + int i = 0; + // 杩唬閾捐〃鐨勬暟鎹敓鎴愭暟缁 + while (iterator.hasNext()) { + nodes[i++] = (Node) iterator.next(); + } + // 閬嶅巻鏁扮粍瓒婄敓鎴愭柊鐨 閾捐〃 + Node newHead = nodes[--i]; + Node next = newHead.next; + for (int j = --i; j >= 0; j--) { + next.next = nodes[j]; + next = next.next; + + } + this.head = newHead; } diff --git a/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/LinkedListTest.java b/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..06efea8aa0 --- /dev/null +++ b/group17/1204187480/code/homework/basic/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by luoziyihao on 3/23/17. + */ +public class LinkedListTest { + @Test + public void iterator() throws Exception { + + } + + @Test + public void reverse() throws Exception { + LinkedList linkedList = new LinkedList(); + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + linkedList.add("4"); + linkedList.reverse(); + System.out.println(linkedList); + } + +} \ No newline at end of file From f1e9c53dc90148dddce11449be9f050ee6386156 Mon Sep 17 00:00:00 2001 From: yyglider Date: Fri, 24 Mar 2017 09:28:00 +0800 Subject: [PATCH 142/155] update update --- group23/769232552/coding/pom.xml | 12 ++ .../src/main/java/code01/ArrayList.java | 23 ++- .../src/main/java/code01/BinaryTree.java | 20 +++ .../src/main/java/code01/LinkedList.java | 48 +++++- .../java/code02/litestruts/ActionConfig.java | 28 ++++ .../java/code02/litestruts/Configuration.java | 64 ++++++++ .../code02/litestruts/ReflectionUtil.java | 119 +++++++++++++++ .../main/java/code02/litestruts/Struts.java | 139 +++--------------- .../src/main/java/code03/DownloadThread.java | 47 ++++++ .../src/main/java/code03/FileDownloader.java | 109 ++++++++++++++ .../src/main/java/code03/api/Connection.java | 23 +++ .../java/code03/api/ConnectionException.java | 9 ++ .../java/code03/api/ConnectionManager.java | 10 ++ .../java/code03/api/DownloadListener.java | 5 + .../main/java/code03/impl/ConnectionImpl.java | 107 ++++++++++++++ .../code03/impl/ConnectionManagerImpl.java | 36 +++++ .../src/test/java/code01/ArrayListTest.java | 20 ++- .../src/test/java/code01/BinaryTreeTest.java | 5 + .../src/test/java/code01/LinkedListTest.java | 25 ++++ .../test/java/code03/FileDownloaderTest.java | 59 ++++++++ 20 files changed, 765 insertions(+), 143 deletions(-) create mode 100644 group23/769232552/coding/src/main/java/code02/litestruts/ActionConfig.java create mode 100644 group23/769232552/coding/src/main/java/code02/litestruts/Configuration.java create mode 100644 group23/769232552/coding/src/main/java/code02/litestruts/ReflectionUtil.java create mode 100644 group23/769232552/coding/src/main/java/code03/DownloadThread.java create mode 100644 group23/769232552/coding/src/main/java/code03/FileDownloader.java create mode 100644 group23/769232552/coding/src/main/java/code03/api/Connection.java create mode 100644 group23/769232552/coding/src/main/java/code03/api/ConnectionException.java create mode 100644 group23/769232552/coding/src/main/java/code03/api/ConnectionManager.java create mode 100644 group23/769232552/coding/src/main/java/code03/api/DownloadListener.java create mode 100644 group23/769232552/coding/src/main/java/code03/impl/ConnectionImpl.java create mode 100644 group23/769232552/coding/src/main/java/code03/impl/ConnectionManagerImpl.java create mode 100644 group23/769232552/coding/src/test/java/code03/FileDownloaderTest.java diff --git a/group23/769232552/coding/pom.xml b/group23/769232552/coding/pom.xml index 812e591bab..5b9701ed4d 100644 --- a/group23/769232552/coding/pom.xml +++ b/group23/769232552/coding/pom.xml @@ -7,6 +7,18 @@ com.coding coding2017 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + diff --git a/group23/769232552/coding/src/main/java/code01/ArrayList.java b/group23/769232552/coding/src/main/java/code01/ArrayList.java index 976193d759..6746de2a50 100644 --- a/group23/769232552/coding/src/main/java/code01/ArrayList.java +++ b/group23/769232552/coding/src/main/java/code01/ArrayList.java @@ -33,8 +33,8 @@ public ArrayList(int size){ public void add(Object o) { //濡傛灉瓒婄晫浜嗭紝闇瑕佸鍒跺師鏈夌殑鏁扮粍鍒版墿鍏呭悗鐨勬暟缁勪腑 if(this.current_size + 1 > this.max_size) { - this.elementData = copyToNew(this.elementData,this.max_size * 2); this.max_size = (int) Math.floor(this.max_size * this.extendPercent); + this.elementData = copyToNew(this.elementData,this.max_size); } this.elementData[this.current_size] = o; this.current_size ++; @@ -50,8 +50,8 @@ public void add(int index, Object o){ //濡傛灉瓒婄晫浜嗭紝闇瑕佸鍒跺師鏈夌殑鏁扮粍鍒版墿鍏呭悗鐨勬暟缁勪腑 if(this.current_size + 1 > this.max_size) { //濡傛灉瓒婄晫浜嗭紝闇瑕佸鍒跺師鏈夌殑鏁扮粍鍒版墿鍏呭悗鐨勬暟缁勪腑 - this.elementData = copyToNew(this.elementData,this.max_size * 2); this.max_size = (int) Math.floor(this.max_size * this.extendPercent); + this.elementData = copyToNew(this.elementData,this.max_size); } //鏁扮粍涓棿鎻掑叆 if(index < this.current_size){ @@ -107,23 +107,22 @@ public int size(){ public Iterator iterator(){ return new Iterator() { - int cursor = 0; - int before_cursor = -1; - @Override + int next_pos = 0; + int pos = -1; public boolean hasNext() { - return cursor != ArrayList.this.size(); + if(max_size <= 0){ + return false; + } + return next_pos < ArrayList.this.size(); } - @Override public Object next() { - Object next = ArrayList.this.get(cursor); - before_cursor = cursor++; + Object next = ArrayList.this.get(next_pos); + pos = next_pos ++; return next; } - - @Override public void remove(){ - ArrayList.this.remove(before_cursor); + ArrayList.this.remove(pos); } }; } diff --git a/group23/769232552/coding/src/main/java/code01/BinaryTree.java b/group23/769232552/coding/src/main/java/code01/BinaryTree.java index 27524b6552..b29fb960cb 100644 --- a/group23/769232552/coding/src/main/java/code01/BinaryTree.java +++ b/group23/769232552/coding/src/main/java/code01/BinaryTree.java @@ -15,6 +15,26 @@ public BinaryTreeNode createBinaryTree(T[] array){ return this.root; } + // recursive way, + // t is the node that roots the subtree. + public BinaryTreeNode insert(T data, BinaryTreeNode t){ + if(t == null){ + return new BinaryTreeNode(data); + } + int comparator = ((T) t.data).compareTo(data); + if(comparator > 0){ + t.left = insert(data,t.right); + }else if(comparator < 0){ + t.right = insert(data,t.left); + }else { + // do nothing + } + return t; + + } + + + //loop way public void insert(T data){ if(this.root == null){ BinaryTreeNode node = new BinaryTreeNode(data); diff --git a/group23/769232552/coding/src/main/java/code01/LinkedList.java b/group23/769232552/coding/src/main/java/code01/LinkedList.java index 7f6d826609..f7bbc970a9 100644 --- a/group23/769232552/coding/src/main/java/code01/LinkedList.java +++ b/group23/769232552/coding/src/main/java/code01/LinkedList.java @@ -227,9 +227,31 @@ public void removeFirstHalf(){ * @param i * @param length */ - public void remove(int i, int length){ + public void remove(int i, int length){ + Node current = head; + Node firstHalf = null; + for (int k = 0; k < i; k ++){ + if(current == null){ + return; + } + firstHalf = current; //璁板綍寰呭垹闄よ妭鐐圭殑鍓嶄竴涓妭鐐 + current = current.next; + } - } + //绉诲姩length闀垮害 + for (int j = 0; j < length; j++) { + if(current == null){ + return; + } + current = current.next; + } + + if(i == 0){ + head = current; + }else { + firstHalf.next = current; + } + } /** * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 @@ -257,8 +279,21 @@ public void subtract(LinkedList list){ * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 */ - public void removeDuplicateValues(){ - + public void removeDuplicateValues(){ + if(this.head == null){ + return; + } + Node current = this.head; + Node current_next = this.head; + while (current_next != null){ + current_next = current_next.next; //濡傛灉鏀惧埌涓嬩釜while寰幆鍚庨潰鍐欙紝灏遍渶瑕佸垽鏂竴娆urrent_next鏄笉鏄痭ull浜 + while(current_next != null && current_next.data.equals(current.data)){ + //鍒犻櫎閲嶅鑺傜偣 + current.next = current_next.next; + current_next = current_next.next; + } + current = current_next; + } } /** @@ -268,7 +303,7 @@ public void removeDuplicateValues(){ * @param max */ public void removeRange(int min, int max){ - + //鎬庝箞鎵嶈兘楂樻晥鍛 } /** @@ -284,8 +319,9 @@ public LinkedList intersection( LinkedList list){ * 閬嶅巻鍒楄〃 */ public void printList(){ + System.out.println(); for (Node cursor = this.head;cursor!=null;cursor=cursor.next){ - System.out.println(cursor.data+" "); + System.out.print(cursor.data+" "); } } } diff --git a/group23/769232552/coding/src/main/java/code02/litestruts/ActionConfig.java b/group23/769232552/coding/src/main/java/code02/litestruts/ActionConfig.java new file mode 100644 index 0000000000..b5e077e7a5 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code02/litestruts/ActionConfig.java @@ -0,0 +1,28 @@ +package code02.litestruts; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by yaoyuan on 2017/3/22. + */ +public class ActionConfig { + String name; + String clzName; + Map viewResult = new HashMap(); + + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + 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/group23/769232552/coding/src/main/java/code02/litestruts/Configuration.java b/group23/769232552/coding/src/main/java/code02/litestruts/Configuration.java new file mode 100644 index 0000000000..85d3d98a1f --- /dev/null +++ b/group23/769232552/coding/src/main/java/code02/litestruts/Configuration.java @@ -0,0 +1,64 @@ +package code02.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +/** + * Created by yaoyuan on 2017/3/21. + */ +public class Configuration { + + + private String path; + private final Map actionMap = new HashMap(); + + Configuration(String path){ + parseXML(path); + } + + //瑙f瀽xml鏂囦欢 + private void parseXML(String path){ + //璇诲彇鏂囦欢 + File file = new File(path); + SAXReader reader = new SAXReader(); + Document document = null; + try { + document = reader.read(file); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element root = document.getRootElement(); + + for (Iterator iterator = root.elementIterator("action"); iterator.hasNext();) { + Element e = iterator.next(); + String actionName = e.attributeValue("name"); + String clazName = e.attributeValue("class"); + ActionConfig actionConfig = new ActionConfig(actionName,clazName); + for(Iterator childIterator = e.elementIterator();childIterator.hasNext();){ + Element child = childIterator.next(); + String jspKey = child.attributeValue("name"); + String jspValue = child.getTextTrim(); + actionConfig.addViewResult(jspKey,jspValue); + } + actionMap.put(actionName,actionConfig); + } + } + + public String getView(String actionName, String result){ + String jspKey = actionName + "." + result; + return actionMap.get(actionName).getViewName(result); + } + + + public Map getActionMap() { + return actionMap; + } + +} diff --git a/group23/769232552/coding/src/main/java/code02/litestruts/ReflectionUtil.java b/group23/769232552/coding/src/main/java/code02/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..2a499f104b --- /dev/null +++ b/group23/769232552/coding/src/main/java/code02/litestruts/ReflectionUtil.java @@ -0,0 +1,119 @@ +package code02.litestruts; + +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by yaoyuan on 2017/3/21. + */ +public class ReflectionUtil { + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ReflectionUtil.class); + + private static final Map> clazzMap = new HashMap>(); + + //鍔犺浇xml鏂囦欢涓殑绫 + public void initiateClazz(Configuration cfg){ + Map actionMap = cfg.getActionMap(); + + for (Map.Entry entry : actionMap.entrySet()) { + String actionName = entry.getKey(); //login + ActionConfig actionConfig =entry.getValue(); + String className = actionConfig.getClassName(); //code02.litestruts.LoginAction + Class cls; + try { + cls = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); + clazzMap.put(actionName,cls); + } catch (Exception e) { + logger.warn("鍔犺浇绫 " + className + "鍑洪敊锛"); + } + } + } + + + //杩斿洖瀹炰緥瀵硅薄 + public Object getInstance(String actionName){ + Object instance = null; + for (Map.Entry> entry : clazzMap.entrySet()) { + String action = entry.getKey(); //login + Class cls = entry.getValue(); //code02.litestruts.LoginAction.class + if(actionName.equals(action)){ + try { + instance = cls.newInstance(); + } catch (Exception e) { + logger.error("鐢熸垚瀹炰緥鍑洪敊锛", e); + throw new RuntimeException(e); + } + } + } + return instance; + } + + + //鍙傛暟璧嬪 + public 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 (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + } + } + + //杩愯鏃犲弬鏂规硶 + public Object runMethodWithoutParams(Object o , String methodName){ + Class clz = o.getClass(); + Object result = null; + try { + Method method = clz.getDeclaredMethod(methodName); + try { + result = method.invoke(o); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + return result; + } + + //杩斿洖浠et寮澶寸殑鏂规硶 + public List getSetterMethods(Object o){ + return getMethods(o.getClass(),"set"); + } + + //杩斿洖浠et寮澶寸殑鏂规硶 + public List getGetterMethods(Object o){ + return getMethods(o.getClass(),"get"); + } + + private List getMethods(Class clz, String startWithName){ + List methodsList = new ArrayList(); + Method[] methods = clz.getDeclaredMethods(); + for (int i = 0; i < methods.length; i++) { + String methodName = methods[i].getName(); + if(methodName.startsWith(startWithName)){ + methodsList.add(methods[i]); + } + } + return methodsList; + } + +} diff --git a/group23/769232552/coding/src/main/java/code02/litestruts/Struts.java b/group23/769232552/coding/src/main/java/code02/litestruts/Struts.java index f260a9eba1..b954f25bd5 100644 --- a/group23/769232552/coding/src/main/java/code02/litestruts/Struts.java +++ b/group23/769232552/coding/src/main/java/code02/litestruts/Struts.java @@ -1,104 +1,16 @@ package code02.litestruts; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; import org.slf4j.LoggerFactory; -import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class Struts { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Struts.class); - - private static final Map> clazzMap = new HashMap>(); - private static final Map actionMap = new HashMap(); - private static final Map pageMap = new HashMap(); - - //瑙f瀽xml鏂囦欢 - private static void parseXML(){ - //璇诲彇鏂囦欢 - File file = new File("src/main/resources/struts.xml"); - SAXReader reader = new SAXReader(); - Document document = null; - try { - document = reader.read(file); - } catch (DocumentException e) { - e.printStackTrace(); - } - Element root = document.getRootElement(); - - for (Iterator iterator = root.elementIterator("action"); iterator.hasNext();) { - Element e = iterator.next(); - String actionKey = e.attributeValue("name"); - String actionValue = e.attributeValue("class"); - actionMap.put(actionKey, actionValue); - for(Iterator childIterator = e.elementIterator();childIterator.hasNext();){ - Element child = childIterator.next(); - String jspKey = actionKey + "-" + child.attributeValue("name"); - String jspValue = child.getTextTrim(); - pageMap.put(jspKey, jspValue); - } - } - } - - //鍔犺浇xml鏂囦欢涓殑绫 - private static void initiateClazz(){ - for (Map.Entry entry : actionMap.entrySet()) { - String actionName = entry.getKey(); //login - String className = entry.getValue(); //code02.litestruts.LoginAction - Class cls; - try { - cls = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); - clazzMap.put(actionName,cls); - } catch (Exception e) { - logger.warn("鍔犺浇绫 " + className + "鍑洪敊锛"); - } - } - } - - static { - parseXML(); - initiateClazz(); - } - - //杩斿洖瀹炰緥瀵硅薄 - private static Object getInstance(String actionName){ - Object instance = null; - for (Map.Entry> entry : clazzMap.entrySet()) { - String action = entry.getKey(); //login - Class cls = entry.getValue(); //code02.litestruts.LoginAction.class - if(actionName.equals(action)){ - try { - instance = cls.newInstance(); - } catch (Exception e) { - logger.error("鐢熸垚瀹炰緥鍑洪敊锛", e); - throw new RuntimeException(e); - } - } - } - return instance; - } - - //杩斿洖浠et寮澶寸殑鏂规硶 - private static List getMethods(String actionName){ - List methodsList = new ArrayList(); - Class cls = clazzMap.get(actionName); - Method[] methods = cls.getDeclaredMethods(); - for (int i = 0; i < methods.length; i++) { - String methodName = methods[i].getName(); - if(methodName.startsWith("get")){ - methodsList.add(methods[i]); - } - } - return methodsList; - } - - /* 0. 璇诲彇閰嶇疆鏂囦欢struts.xml @@ -107,30 +19,31 @@ private static List getMethods(String actionName){ ("name"="test" , "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 - 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" - 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, - 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , - 鏀惧埌View瀵硅薄鐨刾arameters - 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 - 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 - */ + */ public static View runAction(String actionName, Map parameters) { - LoginAction loginAction = (LoginAction) getInstance(actionName); - loginAction.setName(parameters.get("name")); - loginAction.setPassword(parameters.get("password")); - - String result = loginAction.execute(); - + View view = new View(); + Configuration cfg = new Configuration("src/main/resources/struts.xml"); + ReflectionUtil reflectionUtil = new ReflectionUtil(); + reflectionUtil.initiateClazz(cfg); + /* 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛*/ + Object o = reflectionUtil.getInstance(actionName); + /*2. 鏍规嵁parameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") ,閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶*/ + reflectionUtil.setParameters(o,parameters); + /*3. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success"*/ + String result = (String) reflectionUtil.runMethodWithoutParams(o,"execute"); + /* 4. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛,閫氳繃鍙嶅皠鏉ヨ皟鐢紝 + 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} ,鏀惧埌View瀵硅薄鐨刾arameters*/ Map params = new HashMap(); - List methods = getMethods(actionName); + List methods = reflectionUtil.getGetterMethods(o); for(Method method : methods){ String key = method.getName().substring(3); String value = null; try { - value = (String) method.invoke(loginAction); + value = (String) method.invoke(o); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { @@ -138,22 +51,14 @@ public static View runAction(String actionName, Map parameters) } params.put(key,value); } - - - View view = new View(); - + /*5. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝纭畾鍝竴涓猨sp锛屾斁鍒癡iew瀵硅薄鐨刯sp瀛楁涓*/ + String jsp = cfg.getView(actionName,result); view.setParameters(params); - String resultKey = actionName + "-" + result; - view.setJsp(pageMap.get(resultKey)); + view.setJsp(jsp); return view; } - - - - - public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { String actionName = "login"; diff --git a/group23/769232552/coding/src/main/java/code03/DownloadThread.java b/group23/769232552/coding/src/main/java/code03/DownloadThread.java new file mode 100644 index 0000000000..7bf8a8e765 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code03/DownloadThread.java @@ -0,0 +1,47 @@ +package code03; + +import code03.api.Connection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; + +/** + * 瀹氫箟绾跨▼绫 + */ + + +public class DownloadThread extends Thread{ + + private static final Logger logger = LoggerFactory.getLogger(DownloadThread.class); + + private Connection conn; + private int startPos; + private int endPos; + private static final String fileName = "D://test.png"; + + + public DownloadThread(Connection conn, int startPos, int endPos){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + @Override + public void run(){ + logger.debug("thread {} begin to download from start {} to end {} ",Thread.currentThread().getName(),startPos,endPos); + + try { + byte[] data = conn.read(startPos,endPos); + RandomAccessFile rfile = new RandomAccessFile(fileName,"rw"); + rfile.seek(startPos); + rfile.write(data,0,data.length); + rfile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + logger.debug("thread {} end to download from start {} to end {} ",Thread.currentThread().getName(),startPos,endPos); + + + } +} diff --git a/group23/769232552/coding/src/main/java/code03/FileDownloader.java b/group23/769232552/coding/src/main/java/code03/FileDownloader.java new file mode 100644 index 0000000000..6814c49f9c --- /dev/null +++ b/group23/769232552/coding/src/main/java/code03/FileDownloader.java @@ -0,0 +1,109 @@ +package code03; + +import code03.api.Connection; +import code03.api.ConnectionException; +import code03.api.ConnectionManager; +import code03.api.DownloadListener; +import code03.impl.ConnectionManagerImpl; + +import java.util.ArrayList; +import java.util.List; + +/** + * 绾跨▼鍚姩绫 + */ + +public class FileDownloader { + + private String url; + private DownloadListener listener; + private ConnectionManager cm; + private static boolean downloadFinished = false; + + private final static int THREAD_NUM = 5; + + public FileDownloader(String _url) { + this.url = _url; + } + + /* + (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆*/ + public void execute(){ + Connection conn = null; + try { + //鍚姩绾跨▼ + int startPos = 0, endPos = 0; + List threads = new ArrayList(); + for (int i = 0; i < THREAD_NUM; i++) { + conn = cm.open(this.url); //姣忔閮借閲嶆柊鑾峰彇涓涓猚onnection.imputstream + int length = conn.getContentLength(); + startPos = length/THREAD_NUM * i; + endPos = length/THREAD_NUM * (i + 1)- 1; + DownloadThread downloadThread = new DownloadThread(conn,startPos,endPos); + threads.add(downloadThread); + downloadThread.start(); + } + + //璋冪敤join鏂规硶锛岀‘淇濇墍鏈夌嚎绋嬬殑宸ヤ綔宸茬粡瀹屾垚 + for (int i = 0; i < THREAD_NUM; i++) { + try { + threads.get(i).join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + listener.notifyFinished(); + } 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 static void main(String[] args) { + + String url = "http://litten.me/assets/blogImg/litten.png"; + FileDownloader fileDownloader = new FileDownloader(url); + ConnectionManager cm = new ConnectionManagerImpl(); + fileDownloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + fileDownloader.setConnectionManager(cm); + fileDownloader.execute(); + + + while (!downloadFinished){ + try { + System.out.println("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("download finished ! "); + + + } + +} diff --git a/group23/769232552/coding/src/main/java/code03/api/Connection.java b/group23/769232552/coding/src/main/java/code03/api/Connection.java new file mode 100644 index 0000000000..1f2e4e1d39 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code03/api/Connection.java @@ -0,0 +1,23 @@ +package code03.api; + +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(); +} diff --git a/group23/769232552/coding/src/main/java/code03/api/ConnectionException.java b/group23/769232552/coding/src/main/java/code03/api/ConnectionException.java new file mode 100644 index 0000000000..77e886e987 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code03/api/ConnectionException.java @@ -0,0 +1,9 @@ +package code03.api; + +public class ConnectionException extends Exception { + + public ConnectionException(String message,Throwable e){ + super(message,e); + } + +} diff --git a/group23/769232552/coding/src/main/java/code03/api/ConnectionManager.java b/group23/769232552/coding/src/main/java/code03/api/ConnectionManager.java new file mode 100644 index 0000000000..4cbd46445a --- /dev/null +++ b/group23/769232552/coding/src/main/java/code03/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package code03.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group23/769232552/coding/src/main/java/code03/api/DownloadListener.java b/group23/769232552/coding/src/main/java/code03/api/DownloadListener.java new file mode 100644 index 0000000000..f5e7e146a7 --- /dev/null +++ b/group23/769232552/coding/src/main/java/code03/api/DownloadListener.java @@ -0,0 +1,5 @@ +package code03.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group23/769232552/coding/src/main/java/code03/impl/ConnectionImpl.java b/group23/769232552/coding/src/main/java/code03/impl/ConnectionImpl.java new file mode 100644 index 0000000000..069f21625b --- /dev/null +++ b/group23/769232552/coding/src/main/java/code03/impl/ConnectionImpl.java @@ -0,0 +1,107 @@ +package code03.impl; + +import code03.api.Connection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URLConnection; + + +public class ConnectionImpl implements Connection{ + + private static final Logger logger = LoggerFactory.getLogger(ConnectionImpl.class); + + + private URLConnection urlConnection; + private int length = -1; + + public ConnectionImpl(URLConnection urlConnection){ + this.urlConnection = urlConnection; + } + + /** + * 璇诲彇urlConnection.getInputStream()涓殑鏁版嵁锛岃繑鍥瀊yte[] + */ + @Override + public byte[] read(int startPos, int endPos) throws IOException { + int contentLength = getContentLength(); + if(startPos < 0 || endPos > contentLength || contentLength <= 0){ + logger.info("index out of range !"); + return null; + } + + InputStream raw = null; + BufferedInputStream in = null; + int size = endPos - startPos + 1; + + byte[] data = new byte[size]; + try{ + raw = urlConnection.getInputStream(); + in = new BufferedInputStream(raw); + in.skip(startPos); + + int offset = 0; + while(offset < size){ + int bytesRead = in.read(data, offset, size - offset); + while (bytesRead == -1){break;} + offset += bytesRead; + } + } catch (IOException e) { + e.printStackTrace(); + }finally { + raw.close(); + in.close(); + } + return data; + } + + @Override + public int getContentLength() { + if(length != -1){ + return length; + } + length = urlConnection.getContentLength(); + + //if without content-length header + if(length == -1) { + int offset = 0; + InputStream raws = null; + BufferedInputStream ins = null; + try { + raws = urlConnection.getInputStream(); + ins = new BufferedInputStream(raws); + + int max_size = 1024 * 1024;//1M + byte[] data = new byte[max_size]; + + int bytesRead = 0; + while (bytesRead != -1) { + ins.read(data, offset, max_size - offset); + offset += bytesRead; + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + raws.close(); + ins.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + length = offset; + } + return length; + } + + @Override + public void close() { + if(urlConnection != null){ + urlConnection = null; + } + } + +} diff --git a/group23/769232552/coding/src/main/java/code03/impl/ConnectionManagerImpl.java b/group23/769232552/coding/src/main/java/code03/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..5078c7608c --- /dev/null +++ b/group23/769232552/coding/src/main/java/code03/impl/ConnectionManagerImpl.java @@ -0,0 +1,36 @@ +package code03.impl; + +import code03.api.Connection; +import code03.api.ConnectionException; +import code03.api.ConnectionManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +/** + * 鑾峰彇Connection瀹炰緥 + */ + +public class ConnectionManagerImpl implements ConnectionManager { + private static final Logger logger = LoggerFactory.getLogger(ConnectionManagerImpl.class); + + @Override + public Connection open(String url) throws ConnectionException { + Connection connection = null; + try { + URL _url = new URL(url); + URLConnection urlConnection = _url.openConnection(); + connection = new ConnectionImpl(urlConnection); + } catch (MalformedURLException e) { + logger.error("url {} format error",url); + } catch (IOException e) { + e.printStackTrace(); + } + return connection; + } + +} diff --git a/group23/769232552/coding/src/test/java/code01/ArrayListTest.java b/group23/769232552/coding/src/test/java/code01/ArrayListTest.java index fc4c3dc8ea..2bfd7f52e1 100644 --- a/group23/769232552/coding/src/test/java/code01/ArrayListTest.java +++ b/group23/769232552/coding/src/test/java/code01/ArrayListTest.java @@ -1,17 +1,22 @@ package code01; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; /** * Created by yaoyuan on 2017/3/8. */ public class ArrayListTest { + ArrayList arrayList; + @Before + public void setUp(){ + arrayList = new ArrayList(); + } @Test public void testAdd() throws Exception { - ArrayList arrayList = new ArrayList(); String[] array = new String[]{"a","b","c","d","e"}; for (String str : array){ arrayList.add(str); @@ -28,17 +33,17 @@ public void testAdd() throws Exception { @Test public void testAddWithIndex() throws Exception { - ArrayList arrayList = new ArrayList(3);//鑷姩鎵╁ + ArrayList arrayList2 = new ArrayList(3);//鑷姩鎵╁ String[] array = new String[]{"a","b","c","d","e"}; for (int i = 0; i < array.length; i++){ - arrayList.add(i,array[i]); + arrayList2.add(i,array[i]); } //add(),get() - for (int i = 0; i < arrayList.size(); i++){ - Assert.assertEquals(array[i],arrayList.get(i)); + for (int i = 0; i < arrayList2.size(); i++){ + Assert.assertEquals(array[i],arrayList2.get(i)); } - arrayList.add(3,"new"); - Assert.assertEquals("new",arrayList.get(3)); + arrayList2.add(3,"new"); + Assert.assertEquals("new",arrayList2.get(3)); } @@ -46,7 +51,6 @@ public void testAddWithIndex() throws Exception { @Test public void testRemove() throws Exception { - ArrayList arrayList = new ArrayList(); String[] array = new String[]{"a","b","c","d","e"}; for (String str : array){ arrayList.add(str); diff --git a/group23/769232552/coding/src/test/java/code01/BinaryTreeTest.java b/group23/769232552/coding/src/test/java/code01/BinaryTreeTest.java index aa1df37c13..8c1f4e8e09 100644 --- a/group23/769232552/coding/src/test/java/code01/BinaryTreeTest.java +++ b/group23/769232552/coding/src/test/java/code01/BinaryTreeTest.java @@ -19,4 +19,9 @@ public void testCreateBinaryTree(){ binaryTree1.leftOrderScan(binaryTree1.getRoot()); binaryTree2.leftOrderScan(binaryTree2.getRoot()); } + + @Test + public void testInsert(){ + BinaryTree binaryTree3 = new BinaryTree(); + } } \ No newline at end of file diff --git a/group23/769232552/coding/src/test/java/code01/LinkedListTest.java b/group23/769232552/coding/src/test/java/code01/LinkedListTest.java index 1c6d095f60..5481783932 100644 --- a/group23/769232552/coding/src/test/java/code01/LinkedListTest.java +++ b/group23/769232552/coding/src/test/java/code01/LinkedListTest.java @@ -146,4 +146,29 @@ public void testremoveFirstHalf(){ Assert.assertEquals(array[i+array.length/2], linklist.get(i)); } } + + @Test + public void testRemoveDuplicateValues() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","b","b","d","d","d"}; + for (String str : array){ + linklist.add(str); + } + linklist.printList(); + linklist.removeDuplicateValues(); + linklist.printList(); + + } + + @Test + public void testRemove1() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e","f","g"}; + for (String str : array){ + linklist.add(str); + } + linklist.printList(); + linklist.remove(0,4); + linklist.printList(); + } } \ No newline at end of file diff --git a/group23/769232552/coding/src/test/java/code03/FileDownloaderTest.java b/group23/769232552/coding/src/test/java/code03/FileDownloaderTest.java new file mode 100644 index 0000000000..cdc58d24b3 --- /dev/null +++ b/group23/769232552/coding/src/test/java/code03/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package code03; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import code03.api.ConnectionManager; +import code03.api.DownloadListener; +import code03.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://litten.me/assets/blogImg/litten.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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} From 9ade33b49630678fd074a3736de3469c57cfbecb Mon Sep 17 00:00:00 2001 From: chk Date: Fri, 24 Mar 2017 18:10:47 +0800 Subject: [PATCH 143/155] week2 --- .../basic/week2/datastructure/ArrayTest.java | 50 ++++ .../basic/week2/datastructure/ArrayUtil.java | 228 ++++++++++++++++++ 2 files changed, 278 insertions(+) create mode 100644 group23/632678665/com/basic/week2/datastructure/ArrayTest.java create mode 100644 group23/632678665/com/basic/week2/datastructure/ArrayUtil.java diff --git a/group23/632678665/com/basic/week2/datastructure/ArrayTest.java b/group23/632678665/com/basic/week2/datastructure/ArrayTest.java new file mode 100644 index 0000000000..46c3f8b601 --- /dev/null +++ b/group23/632678665/com/basic/week2/datastructure/ArrayTest.java @@ -0,0 +1,50 @@ +package com.basic.week2.datastructure; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; + +public class ArrayTest { + private ArrayUtil t=new ArrayUtil(); + @Test + public void testReverseArray(){ + int [] data1={7, 9 , 30, 3}; + int [] data2={7, 9, 30, 3, 4}; + t.reverseArray(data2); + } + @Test + public void testRemoveZero(){ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println(Arrays.toString(t.removeZero(oldArr))); + } + @Test + public void testMerge(){ + int [] a1 = {3, 5, 7,8}; + int [] a2 = {4, 5, 6,7}; + System.out.println(Arrays.toString(t.merge(a1, a2))); + } + @Test + public void testGrow(){ + int [] oldArray = {2,3,6}; + System.out.println(Arrays.toString(t.grow(oldArray, 5))); + } + @Test + public void testFibonacci(){ + System.out.println(Arrays.toString(t.fibonacci(1))); + } + @Test + public void testGetPrimes(){ + System.out.println(Arrays.toString(t.getPrimes(23))); + } + @Test + public void testGetPerfectNumbers(){ + System.out.println(Arrays.toString(t.getPerfectNumbers(7))); + } + @Test + public void testJoin(){ + int [] array= {3,8,9}; + System.out.println(t.join(array, "-")); + } +} diff --git a/group23/632678665/com/basic/week2/datastructure/ArrayUtil.java b/group23/632678665/com/basic/week2/datastructure/ArrayUtil.java new file mode 100644 index 0000000000..10089c1b3a --- /dev/null +++ b/group23/632678665/com/basic/week2/datastructure/ArrayUtil.java @@ -0,0 +1,228 @@ +package com.basic.week2.datastructure; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + int index=(int) (origin.length/2); + for(int i=0;iarray=new ArrayList (); + int from; + int to; + for(int i=0;i list=new ArrayList(); + for(int i=0;i set=new HashSet(); + set.addAll(list); + Object[] array=set.toArray(); + int [] arrayInt=new int [array.length]; + for(int i=0;ilist=new ArrayList(); + list.add(front); + list.add(behind); + while(true){ + result=front+behind; + if(max list=new ArrayList(); + int num=3; + list.add(2); + boolean flag=true; + while(true){ + flag=true; + for(int i=2;i list=new ArrayList(); + Set temp=new HashSet(); + int num=1; + int result=0; + while(true){ + for(int i=1;imax){ + break; + } + if(num==result){ + list.add(num); + } + temp.clear(); + result=0; + num++; + } + int [] array=new int [list.size()]; + for(int i=0;i list=new ArrayList(); + for(int i=0;i Date: Fri, 24 Mar 2017 20:30:12 +0800 Subject: [PATCH 144/155] download test --- .../com/donaldy/download/DownloadThread.java | 45 +++++++++ .../com/donaldy/download/FileDownloader.java | 93 +++++++++++++++++++ .../donaldy/download/FileDownloaderTest.java | 57 ++++++++++++ .../com/donaldy/download/api/Connection.java | 23 +++++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 13 +++ .../download/api/DownloadListener.java | 5 + .../donaldy/download/impl/ConnectionImpl.java | 66 +++++++++++++ .../download/impl/ConnectionImplTest.java | 24 +++++ .../download/impl/ConnectionManagerImpl.java | 55 +++++++++++ .../impl/ConnectionManagerImplTest.java | 19 ++++ 11 files changed, 405 insertions(+) create mode 100644 group24/448641125/src/com/donaldy/download/DownloadThread.java create mode 100644 group24/448641125/src/com/donaldy/download/FileDownloader.java create mode 100644 group24/448641125/src/com/donaldy/download/FileDownloaderTest.java create mode 100644 group24/448641125/src/com/donaldy/download/api/Connection.java create mode 100644 group24/448641125/src/com/donaldy/download/api/ConnectionException.java create mode 100644 group24/448641125/src/com/donaldy/download/api/ConnectionManager.java create mode 100644 group24/448641125/src/com/donaldy/download/api/DownloadListener.java create mode 100644 group24/448641125/src/com/donaldy/download/impl/ConnectionImpl.java create mode 100644 group24/448641125/src/com/donaldy/download/impl/ConnectionImplTest.java create mode 100644 group24/448641125/src/com/donaldy/download/impl/ConnectionManagerImpl.java create mode 100644 group24/448641125/src/com/donaldy/download/impl/ConnectionManagerImplTest.java diff --git a/group24/448641125/src/com/donaldy/download/DownloadThread.java b/group24/448641125/src/com/donaldy/download/DownloadThread.java new file mode 100644 index 0000000000..2d018cec3d --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/DownloadThread.java @@ -0,0 +1,45 @@ +package com.donaldy.download; + +import com.donaldy.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + + public void run(){ + System.out.println("running --"); + try ( + + RandomAccessFile raf = new RandomAccessFile("test.jpg", "rw") + + ) + { + raf.seek(startPos); + + byte [] buffer = conn.read(startPos, endPos); + + int hasRead = conn.getContentLength(); + + System.out.println("hasRead : " + hasRead); + + if (hasRead > 0) + raf.write(buffer, 0, hasRead); + + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("write part file default!"); + } + } +} diff --git a/group24/448641125/src/com/donaldy/download/FileDownloader.java b/group24/448641125/src/com/donaldy/download/FileDownloader.java new file mode 100644 index 0000000000..811d462be4 --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/FileDownloader.java @@ -0,0 +1,93 @@ +package com.donaldy.download; + +import com.donaldy.download.api.Connection; +import com.donaldy.download.api.ConnectionException; +import com.donaldy.download.api.ConnectionManager; +import com.donaldy.download.api.DownloadListener; + +import java.io.IOException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute() throws IOException { + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵 浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + + ExecutorService executorService = Executors.newFixedThreadPool(5); + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + int partLength = length ; + + System.out.println("partLengh : " + partLength); + + int startPos = 0; + + /*for (int i = 0 ; i < 5; ++ i) { + System.out.println("Thread is ready..."); + executorService.execute(new DownloadThread(conn, startPos, startPos + partLength)); + startPos += partLength; + }*/ + + new DownloadThread(conn, startPos, startPos + partLength).start(); + + executorService.shutdown(); + + Thread.sleep(10000); + + listener.notifyFinished(); + + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally{ + + } + } + + 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/group24/448641125/src/com/donaldy/download/FileDownloaderTest.java b/group24/448641125/src/com/donaldy/download/FileDownloaderTest.java new file mode 100644 index 0000000000..7cd0608bb6 --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/FileDownloaderTest.java @@ -0,0 +1,57 @@ +package com.donaldy.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.donaldy.download.api.ConnectionManager; +import com.donaldy.download.api.DownloadListener; +import com.donaldy.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() throws Exception{ + + String url = "https://gss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/d1a20cf431adcbef25b551dfaaaf2edda2cc9f61.jpg"; + + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + } + +} diff --git a/group24/448641125/src/com/donaldy/download/api/Connection.java b/group24/448641125/src/com/donaldy/download/api/Connection.java new file mode 100644 index 0000000000..bd5a61cdbc --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.donaldy.download.api; + +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(); +} diff --git a/group24/448641125/src/com/donaldy/download/api/ConnectionException.java b/group24/448641125/src/com/donaldy/download/api/ConnectionException.java new file mode 100644 index 0000000000..f520af58cd --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.donaldy.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group24/448641125/src/com/donaldy/download/api/ConnectionManager.java b/group24/448641125/src/com/donaldy/download/api/ConnectionManager.java new file mode 100644 index 0000000000..318b031866 --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/api/ConnectionManager.java @@ -0,0 +1,13 @@ +package com.donaldy.download.api; + +import java.io.IOException; +import java.net.MalformedURLException; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException, IOException; +} diff --git a/group24/448641125/src/com/donaldy/download/api/DownloadListener.java b/group24/448641125/src/com/donaldy/download/api/DownloadListener.java new file mode 100644 index 0000000000..af8e1e7d04 --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.donaldy.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group24/448641125/src/com/donaldy/download/impl/ConnectionImpl.java b/group24/448641125/src/com/donaldy/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..589a010e68 --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/impl/ConnectionImpl.java @@ -0,0 +1,66 @@ +package com.donaldy.download.impl; + +import java.io.IOException; +import java.io.InputStream; + +import com.donaldy.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + int contentLength; + + InputStream inputStream; + + public void setContentLength(int contentLength) { + this.contentLength = contentLength; + } + + public void setInputStream (InputStream inputStream) { + this.inputStream = inputStream; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + if (inputStream == null) + return null; + + System.out.println("inputStream is not equal null"); + + inputStream.skip(startPos); + + int length = endPos - startPos + 1; + + System.out.println("瑕佽鐨勯暱搴 : " + length); + + byte [] buffer = new byte[length]; + + System.out.println("buffer - 1: " + buffer.length); + + contentLength = inputStream.read(buffer); + + System.out.println("buffer - 2: " + buffer.length); + + System.out.println("contentLength : " + contentLength); + + return buffer; + } + + @Override + public int getContentLength() { + return contentLength; + } + + @Override + public void close() { + + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/group24/448641125/src/com/donaldy/download/impl/ConnectionImplTest.java b/group24/448641125/src/com/donaldy/download/impl/ConnectionImplTest.java new file mode 100644 index 0000000000..869b343787 --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/impl/ConnectionImplTest.java @@ -0,0 +1,24 @@ +package com.donaldy.download.impl; + +import org.junit.Test; + +/** + * Created by donal on 2017/3/22. + */ +public class ConnectionImplTest { + + @Test + public void testRead() { + + } + + @Test + public void testGetContentLength() { + + } + + @Test + public void testClose() { + + } +} diff --git a/group24/448641125/src/com/donaldy/download/impl/ConnectionManagerImpl.java b/group24/448641125/src/com/donaldy/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..9c98b0d4fb --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,55 @@ +package com.donaldy.download.impl; + +import com.donaldy.download.api.Connection; +import com.donaldy.download.api.ConnectionException; +import com.donaldy.download.api.ConnectionManager; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + try { + URL urlName = new URL(url); + + HttpURLConnection connection = (HttpURLConnection) urlName.openConnection(); + + connection.setConnectTimeout(8000); + + connection.setRequestMethod("GET"); + + connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + + connection.setRequestProperty("Accept", + "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " + + "application/x-shockwave-flash, application/xaml+xml, " + + "application/vnd.ms-xpsdocument, application/x-ms-xbap, " + + "application/x-ms-application, application/vnd.ms-excel, " + + "application/vnd.ms-powerpoint, application/msword, */*"); + connection.setRequestProperty("Accept-Language", "zh-CN"); + + connection.setRequestProperty("Charset", "UTF-8"); + + ConnectionImpl conn = new ConnectionImpl(); + + conn.setContentLength(connection.getContentLength()); + + System.out.println("connection.getContentLength() : " + connection.getContentLength()); + + conn.setInputStream(connection.getInputStream()); + + return conn; + + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group24/448641125/src/com/donaldy/download/impl/ConnectionManagerImplTest.java b/group24/448641125/src/com/donaldy/download/impl/ConnectionManagerImplTest.java new file mode 100644 index 0000000000..2ae3f8d3f8 --- /dev/null +++ b/group24/448641125/src/com/donaldy/download/impl/ConnectionManagerImplTest.java @@ -0,0 +1,19 @@ +package com.donaldy.download.impl; + +import org.junit.Test; + +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * Created by donal on 2017/3/22. + */ +public class ConnectionManagerImplTest { + + @Test + public void testOpen() throws Exception{ + URL url = new URL(""); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + } +} From e8e5a7a92dff6f3702ca4881db8e8d8fe4bd8f21 Mon Sep 17 00:00:00 2001 From: GordenChow <513274874@qq.com> Date: Sun, 26 Mar 2017 11:36:11 +0800 Subject: [PATCH 145/155] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 绗笁鍛ㄤ綔涓氭彁浜 --- .../src/com/coding/basic/LinkedList.java | 77 ++++++++++++ .../coderising/download/DownloadThread.java | 18 ++- .../coderising/download/FileDownloader.java | 10 +- .../download/FileDownloaderTest.java | 8 +- .../coderising/download/api/Connection.java | 2 +- .../download/api/ConnectionException.java | 2 +- .../download/api/ConnectionManager.java | 2 +- .../download/api/DownloadListener.java | 2 +- .../coderising/download/demo/DownThread.java | 72 ++++++++++++ .../coderising/download/demo/MutilDown.java | 72 ++++++++++++ .../download/impl/ConnectionImpl.java | 111 +++++++++++++++--- .../download/impl/ConnectionManagerImpl.java | 26 +++- 12 files changed, 360 insertions(+), 42 deletions(-) create mode 100644 group27/513274874/homework/src/com/coding/coderising/download/demo/DownThread.java create mode 100644 group27/513274874/homework/src/com/coding/coderising/download/demo/MutilDown.java diff --git a/group27/513274874/homework/src/com/coding/basic/LinkedList.java b/group27/513274874/homework/src/com/coding/basic/LinkedList.java index d66be49758..1d574e8aa3 100644 --- a/group27/513274874/homework/src/com/coding/basic/LinkedList.java +++ b/group27/513274874/homework/src/com/coding/basic/LinkedList.java @@ -185,4 +185,81 @@ public void setNext(Node next) { } + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺listB鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪listB涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + + } \ No newline at end of file diff --git a/group27/513274874/homework/src/com/coding/coderising/download/DownloadThread.java b/group27/513274874/homework/src/com/coding/coderising/download/DownloadThread.java index 1456314140..eca4a105e6 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/DownloadThread.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/DownloadThread.java @@ -1,6 +1,9 @@ -package com.coderising.download; +package com.coding.coderising.download; -import com.coderising.download.api.Connection; +import com.coding.coderising.download.api.Connection; + +import java.io.IOException; +import java.io.RandomAccessFile; public class DownloadThread extends Thread{ @@ -8,13 +11,20 @@ public class DownloadThread extends Thread{ int startPos; int endPos; + // 灏嗕笅杞藉埌鐨勫瓧鑺傝緭鍑哄埌raf涓 + private RandomAccessFile raf; + public DownloadThread( Connection conn, int startPos, int endPos){ this.conn = conn; this.startPos = startPos; this.endPos = endPos; } - public void run(){ - + public void run(){ + try { + conn.read(startPos,endPos); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/group27/513274874/homework/src/com/coding/coderising/download/FileDownloader.java b/group27/513274874/homework/src/com/coding/coderising/download/FileDownloader.java index f5d7999eb4..dc99e91598 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/FileDownloader.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/FileDownloader.java @@ -1,9 +1,9 @@ -package com.coderising.download; +package com.coding.coderising.download; -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; +import com.coding.coderising.download.api.Connection; +import com.coding.coderising.download.api.ConnectionException; +import com.coding.coderising.download.api.ConnectionManager; +import com.coding.coderising.download.api.DownloadListener; public class FileDownloader { diff --git a/group27/513274874/homework/src/com/coding/coderising/download/FileDownloaderTest.java b/group27/513274874/homework/src/com/coding/coderising/download/FileDownloaderTest.java index 8171ee5763..8a9531530f 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/FileDownloaderTest.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/FileDownloaderTest.java @@ -1,12 +1,12 @@ -package com.coderising.download; +package com.coding.coderising.download; import org.junit.After; import org.junit.Before; import org.junit.Test; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; +import com.coding.coderising.download.api.ConnectionManager; +import com.coding.coderising.download.api.DownloadListener; +import com.coding.coderising.download.impl.ConnectionManagerImpl; public class FileDownloaderTest { boolean downloadFinished = false; diff --git a/group27/513274874/homework/src/com/coding/coderising/download/api/Connection.java b/group27/513274874/homework/src/com/coding/coderising/download/api/Connection.java index 9710e270e1..1b00c983d6 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/api/Connection.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/api/Connection.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.coding.coderising.download.api; import java.io.IOException; diff --git a/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionException.java b/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionException.java index 8dbfe95dda..e08c73bd98 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionException.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionException.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.coding.coderising.download.api; public class ConnectionException extends Exception { diff --git a/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionManager.java b/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionManager.java index fb44ede457..f95561e023 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionManager.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/api/ConnectionManager.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.coding.coderising.download.api; public interface ConnectionManager { /** diff --git a/group27/513274874/homework/src/com/coding/coderising/download/api/DownloadListener.java b/group27/513274874/homework/src/com/coding/coderising/download/api/DownloadListener.java index 4cd0b3eab1..32c399a204 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/api/DownloadListener.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/api/DownloadListener.java @@ -1,4 +1,4 @@ -package com.coderising.download.api; +package com.coding.coderising.download.api; public interface DownloadListener { public void notifyFinished(); diff --git a/group27/513274874/homework/src/com/coding/coderising/download/demo/DownThread.java b/group27/513274874/homework/src/com/coding/coderising/download/demo/DownThread.java new file mode 100644 index 0000000000..b675d65c9d --- /dev/null +++ b/group27/513274874/homework/src/com/coding/coderising/download/demo/DownThread.java @@ -0,0 +1,72 @@ +package com.coding.coderising.download.demo; + +import java.io.InputStream; +import java.io.RandomAccessFile; + +public class DownThread extends Thread { + + // 瀹氫箟瀛楄妭鏁扮粍锛堝彇姘寸殑绔圭瓛锛夌殑闀垮害 + private final int BUFF_LEN = 32; + + // 瀹氫箟涓嬭浇鐨勮捣濮嬬偣 + private long start; + + // 瀹氫箟涓嬭浇鐨勭粨鏉熺偣 + private long end; + + // 涓嬭浇璧勬簮瀵瑰簲鐨勮緭鍏ユ祦 + private InputStream is; + + // 灏嗕笅杞藉埌鐨勫瓧鑺傝緭鍑哄埌raf涓 + private RandomAccessFile raf; + + + // 鏋勯犲櫒锛屼紶鍏ヨ緭鍏ユ祦锛岃緭鍑烘祦鍜屼笅杞借捣濮嬬偣銆佺粨鏉熺偣 + public DownThread(long start, long end, InputStream is, RandomAccessFile raf) { + // 杈撳嚭璇ョ嚎绋嬭礋璐d笅杞界殑瀛楄妭浣嶇疆 + System.out.println(start + "---->" + end); + this.start = start; + this.end = end; + this.is = is; + this.raf = raf; + } + + @Override + public void run() { + try { + is.skip(start); + raf.seek(start); + // 瀹氫箟璇诲彇杈撳叆娴佸唴瀹圭殑鐨勭紦瀛樻暟缁勶紙绔圭瓛锛 + byte[] buff = new byte[BUFF_LEN]; + // 鏈嚎绋嬭礋璐d笅杞借祫婧愮殑澶у皬 + long contentLen = end - start; + // 瀹氫箟鏈澶氶渶瑕佽鍙栧嚑娆″氨鍙互瀹屾垚鏈嚎绋嬬殑涓嬭浇 + long times = contentLen / BUFF_LEN + 4; + // 瀹為檯璇诲彇鐨勫瓧鑺傛暟 + int hasRead = 0; + for (int i = 0; i < times; i++) { + hasRead = is.read(buff); + // 濡傛灉璇诲彇鐨勫瓧鑺傛暟灏忎簬0锛屽垯閫鍑哄惊鐜紒 + if (hasRead < 0) { + break; + } + raf.write(buff, 0, hasRead); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + // 浣跨敤finally鍧楁潵鍏抽棴褰撳墠绾跨▼鐨勮緭鍏ユ祦銆佽緭鍑烘祦 + finally { + try { + if (is != null) { + is.close(); + } + if (raf != null) { + raf.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/group27/513274874/homework/src/com/coding/coderising/download/demo/MutilDown.java b/group27/513274874/homework/src/com/coding/coderising/download/demo/MutilDown.java new file mode 100644 index 0000000000..3b6cfa020d --- /dev/null +++ b/group27/513274874/homework/src/com/coding/coderising/download/demo/MutilDown.java @@ -0,0 +1,72 @@ +package com.coding.coderising.download.demo; + +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.URL; +import java.net.URLConnection; + +public class MutilDown { + + public static void main(String[] args) { + //瀹氫箟鍑犱釜绾跨▼鍘讳笅杞 + final int DOWN_THREAD_NUM = 4; + final String OUT_FILE_NAME = "down.jpg"; + InputStream[] isArr = new InputStream[DOWN_THREAD_NUM]; + RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM]; + try { + // 鍒涘缓涓涓猆RL瀵硅薄 + URL url = new URL("http://hiphotos.baidu.com/240728057/pic/item/6a50e38242aad8f60cf4d2b3.jpg"); + // 浠ユURL瀵硅薄鎵撳紑绗竴涓緭鍏ユ祦 + isArr[0] = url.openStream(); + long fileLen = getFileLength(url); + System.out.println("缃戠粶璧勬簮鐨勫ぇ灏" + fileLen); + // 浠ヨ緭鍑烘枃浠跺悕鍒涘缓绗竴涓猂andomAccessFile杈撳嚭娴 + //鍒涘缓浠庝腑璇诲彇鍜屽悜鍏朵腑鍐欏叆锛堝彲閫夛級鐨勯殢鏈哄瓨鍙栨枃浠舵祦锛岀涓涓弬鏁帮細鏂囦欢鍚嶏紝绗簩涓弬鏁版槸锛氬弬鏁版寚瀹氱敤浠ユ墦寮鏂囦欢鐨勮闂ā寮 + //"rw"鍙兘鏄彲璇诲彲鍐欙紝 + outArr[0] = new RandomAccessFile(OUT_FILE_NAME, "rw"); + // 鍒涘缓涓涓笌涓嬭浇璧勬簮鐩稿悓澶у皬鐨勭┖鏂囦欢 + for (int i = 0; i < fileLen; i++) { + outArr[0].write(0); + } + // 姣忕嚎绋嬪簲璇ヤ笅杞界殑瀛楄妭鏁 + long numPerThred = fileLen / DOWN_THREAD_NUM; + // 鏁翠釜涓嬭浇璧勬簮鏁撮櫎鍚庡墿涓嬬殑浣欐暟鍙栨ā + long left = fileLen % DOWN_THREAD_NUM; + for (int i = 0; i < DOWN_THREAD_NUM; i++) { + // 涓烘瘡涓嚎绋嬫墦寮涓涓緭鍏ユ祦銆佷竴涓猂andomAccessFile瀵硅薄锛 + // 璁╂瘡涓嚎绋嬪垎鍒礋璐d笅杞借祫婧愮殑涓嶅悓閮ㄥ垎銆 + //isArr[0]鍜宱utArr[0]宸茬粡浣跨敤锛屼粠涓嶄负0寮濮 + if (i != 0) { + // 浠RL鎵撳紑澶氫釜杈撳叆娴 + isArr[i] = url.openStream(); + // 浠ユ寚瀹氳緭鍑烘枃浠跺垱寤哄涓猂andomAccessFile瀵硅薄 + outArr[i] = new RandomAccessFile(OUT_FILE_NAME, "rw"); + } + // 鍒嗗埆鍚姩澶氫釜绾跨▼鏉ヤ笅杞界綉缁滆祫婧 + if (i == DOWN_THREAD_NUM - 1) { + // 鏈鍚庝竴涓嚎绋嬩笅杞芥寚瀹歯umPerThred+left涓瓧鑺 + new DownThread(i * numPerThred, (i + 1) * numPerThred + + left, isArr[i], outArr[i]).start(); + } else { + // 姣忎釜绾跨▼璐熻矗涓嬭浇涓瀹氱殑numPerThred涓瓧鑺 + new DownThread(i * numPerThred, (i + 1) * numPerThred, + isArr[i], outArr[i]).start(); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + // 瀹氫箟鑾峰彇鎸囧畾缃戠粶璧勬簮鐨勯暱搴︾殑鏂规硶 + public static long getFileLength(URL url) throws Exception { + long length = 0; + // 鎵撳紑璇RL瀵瑰簲鐨刄RLConnection + URLConnection con = url.openConnection(); + // 鑾峰彇杩炴帴URL璧勬簮鐨勯暱搴 + long size = con.getContentLength(); + length = size; + return length; + } + +} \ No newline at end of file diff --git a/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionImpl.java b/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionImpl.java index 32f03efdc7..e1f649b88b 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionImpl.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionImpl.java @@ -1,27 +1,100 @@ -package com.coderising.download.impl; +package com.coding.coderising.download.impl; -import java.io.IOException; +import com.coding.coderising.download.api.Connection; -import com.coderising.download.api.Connection; +import java.io.*; +import java.net.URL; +import java.net.URLConnection; -public class ConnectionImpl implements Connection{ +public class ConnectionImpl implements Connection { + private URL url; - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } + // 瀹氫箟瀛楄妭鏁扮粍锛堝彇姘寸殑绔圭瓛锛夌殑闀垮害 + private final int BUFF_LEN = 32; - @Override - public int getContentLength() { - - return 0; - } + // 涓嬭浇璧勬簮瀵瑰簲鐨勮緭鍏ユ祦 + private InputStream is; - @Override - public void close() { - - - } + ByteArrayOutputStream bos; + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + this.is = url.openStream(); + + is.skip(startPos); + // 瀹氫箟璇诲彇杈撳叆娴佸唴瀹圭殑鐨勭紦瀛樻暟缁勶紙绔圭瓛锛 + byte[] buff = new byte[BUFF_LEN]; + // 鏈嚎绋嬭礋璐d笅杞借祫婧愮殑澶у皬 + long contentLen = endPos - startPos; + bos = new ByteArrayOutputStream((int) contentLen); + BufferedInputStream in = new BufferedInputStream(is); + int len = 0; + while (-1 != (len = in.read(buff, 0, BUFF_LEN))) { + bos.write(buff, 0, len); + } + return bos.toByteArray(); + } +// @Override +// public byte[] read(int startPos, int endPos) throws IOException { +// raf = new RandomAccessFile("newfile.jpg", "rw"); +// this.is = url.openStream(); +// +// is.skip(startPos); +// raf.seek(startPos); +// // 瀹氫箟璇诲彇杈撳叆娴佸唴瀹圭殑鐨勭紦瀛樻暟缁勶紙绔圭瓛锛 +// byte[] buff = new byte[BUFF_LEN]; +// // 鏈嚎绋嬭礋璐d笅杞借祫婧愮殑澶у皬 +// long contentLen = endPos - startPos; +// ByteArrayOutputStream bos = new ByteArrayOutputStream((int) contentLen); +// // 瀹氫箟鏈澶氶渶瑕佽鍙栧嚑娆″氨鍙互瀹屾垚鏈嚎绋嬬殑涓嬭浇 +// long times = contentLen / BUFF_LEN + 4; +// // 瀹為檯璇诲彇鐨勫瓧鑺傛暟 +// int hasRead = 0; +// for (int i = 0; i < times; i++) { +// hasRead = is.read(buff); +// // 濡傛灉璇诲彇鐨勫瓧鑺傛暟灏忎簬0锛屽垯閫鍑哄惊鐜紒 +// if (hasRead < 0) { +// break; +// } +// raf.write(buff, 0, hasRead); +// } +// +// return null; +// } + + @Override + public int getContentLength() { + int length = 0; + // 鎵撳紑璇RL瀵瑰簲鐨刄RLConnection + URLConnection con = null; + try { + con = url.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + // 鑾峰彇杩炴帴URL璧勬簮鐨勯暱搴 + length = con.getContentLength(); + return length; + } + + @Override + public void close() { + try { + if (is != null) { + is.close(); + } + if (bos != null) { + bos.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public ConnectionImpl(URL url) { + this.url = url; + } } diff --git a/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionManagerImpl.java b/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionManagerImpl.java index 046f7c49a4..2295167f6d 100644 --- a/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionManagerImpl.java +++ b/group27/513274874/homework/src/com/coding/coderising/download/impl/ConnectionManagerImpl.java @@ -1,15 +1,29 @@ -package com.coderising.download.impl; +package com.coding.coderising.download.impl; -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; +import com.coding.coderising.download.api.Connection; +import com.coding.coderising.download.api.ConnectionException; +import com.coding.coderising.download.api.ConnectionManager; + +import java.net.MalformedURLException; +import java.net.URL; public class ConnectionManagerImpl implements ConnectionManager { @Override public Connection open(String url) throws ConnectionException { - - return null; + + Connection connection = null; + try { + if(url == null || "".equals(url.trim())) return null; + + URL urlO = new URL(url); + connection = new ConnectionImpl(urlO); + + + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return connection; } } From b3bed134ac95545a630dd8697b2076cc27dfc942 Mon Sep 17 00:00:00 2001 From: Andy <14703250@qq.com> Date: Sun, 26 Mar 2017 16:58:24 +0800 Subject: [PATCH 146/155] Delete .gitignore.bak --- .gitignore.bak | 301 ------------------------------------------------- 1 file changed, 301 deletions(-) delete mode 100644 .gitignore.bak diff --git a/.gitignore.bak b/.gitignore.bak deleted file mode 100644 index bfc52534d9..0000000000 --- a/.gitignore.bak +++ /dev/null @@ -1,301 +0,0 @@ -<<<<<<< HEAD - -======= -################# -## Eclipse -################# - -*.pydevproject -.project -.metadata -bin/ -tmp/ -*.tmp -*.bak -*.swp -*~.nib -local.properties -.classpath -.settings/ -.loadpath - -# External tool builders -.externalToolBuilders/ - -# Locally stored "Eclipse launch configurations" -*.launch - -# CDT-specific -.cproject - -# PDT-specific -.buildpath - - -################# -## Visual Studio -################# - -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.sln.docstates - -# Build results - -[Dd]ebug/ -[Rr]elease/ -x64/ -build/ -[Bb]in/ -[Oo]bj/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -*_i.c -*_p.c -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.log -*.scc - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opensdf -*.sdf -*.cachefile - -# Visual Studio profiler -*.psess -*.vsp -*.vspx - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# NCrunch -*.ncrunch* -.*crunch*.local.xml - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.Publish.xml -*.pubxml -*.publishproj - -# NuGet Packages Directory -## TODO: If you have NuGet Package Restore enabled, uncomment the next line -#packages/ - -# Windows Azure Build Output -csx -*.build.csdef - -# Windows Store app package directory -AppPackages/ - -# Others -sql/ -*.Cache -ClientBin/ -[Ss]tyle[Cc]op.* -~$* -*~ -*.dbmdl -*.[Pp]ublish.xml -*.pfx -*.publishsettings - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file to a newer -# Visual Studio version. Backup files are not needed, because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm - -# SQL Server files -App_Data/*.mdf -App_Data/*.ldf - -############# -## Windows detritus -############# - -# Windows image file caches -Thumbs.db -ehthumbs.db - -# Folder config file -Desktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Mac crap -.DS_Store - - -############# -## Python -############# - -*.py[cod] - -# Packages -*.egg -*.egg-info -dist/ -build/ -eggs/ -parts/ -var/ -sdist/ -develop-eggs/ -.installed.cfg - -# Installer logs -pip-log.txt - -# Unit test / coverage reports -.coverage -.tox - -#Translations -*.mo - -#Mr Developer -.mr.developer.cfg -.gitignore -======= - - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -#ide config -.metadata -.recommenders - - -*.xml -*.iml -.idea -*.iml -rebel.xml -rebel-remote.xml - -.classpath -.project -.setting -.metadata - -target -*.class - -log -*.log -tmp -*.tmp - -.metadata -RemoteSystemsTempFiles -.gitignore - - -build/ -.idea/ -.gradle/ ->>>>>>> 9480ef8682c393444f53a282b34e23a087ef73e6 -*.class -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -#ide config -.metadata -.recommenders -.idea/ -*.iml -rebel.* -.rebel.* - -target -<<<<<<< HEAD - -#bin/ -#.setting/ -#*.classpath -#*.project -======= -*.DS_Store -liuxin/.DS_Store -liuxin/src/.DS_Store ->>>>>>> 9480ef8682c393444f53a282b34e23a087ef73e6 - From 092b309d246eada46c36684f932f9c7621321391 Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Sun, 26 Mar 2017 17:02:48 +0800 Subject: [PATCH 147/155] =?UTF-8?q?LRU=E7=AE=97=E6=B3=95=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding/basic/linklist/LRUPageFrame.java | 60 +++++++++++++++++++ .../basic/linklist/LRUPageFrameTest.java | 31 ++++++++++ 2 files changed, 91 insertions(+) create mode 100644 liuxin/src/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 liuxin/src/com/coding/basic/linklist/LRUPageFrameTest.java diff --git a/liuxin/src/com/coding/basic/linklist/LRUPageFrame.java b/liuxin/src/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..25595cbe4c --- /dev/null +++ b/liuxin/src/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,60 @@ +package com.coding.basic.linklist; + +/** + * 鐢ㄥ弻鍚戦摼琛ㄥ疄鐜癓RU绠楁硶 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + + private Node first;// 閾捐〃澶 + private Node last;// 閾捐〃灏 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 鑾峰彇缂撳瓨涓璞 + * + * @param key + * @return + */ + public void access(int pageNum) { + + + } + + + + 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/liuxin/src/com/coding/basic/linklist/LRUPageFrameTest.java b/liuxin/src/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..4070f1f2b3 --- /dev/null +++ b/liuxin/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,31 @@ +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()); + } + +} From c6a4ebd324550c03fdf07470b0a73dc224637c0b Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Sun, 26 Mar 2017 17:31:39 +0800 Subject: [PATCH 148/155] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liuxin/.classpath | 7 - liuxin/.gitignore | 1 - liuxin/.project | 17 -- .../org.eclipse.core.resources.prefs | 2 - .../coderising/download/DownloadThread.java | 40 +-- .../coderising/download/FileDownloader.java | 146 +++++------ .../download/FileDownloaderTest.java | 118 ++++----- .../coderising/download/api/Connection.java | 46 ++-- .../download/api/ConnectionException.java | 10 +- .../download/api/ConnectionManager.java | 20 +- .../download/api/DownloadListener.java | 10 +- .../download/impl/ConnectionImpl.java | 54 ++-- .../download/impl/ConnectionManagerImpl.java | 30 +-- .../coderising/litestruts/LoginAction.java | 78 +++--- .../src/com/coderising/litestruts/Struts.java | 68 ++--- .../com/coderising/litestruts/StrutsTest.java | 86 +++--- .../src/com/coderising/litestruts/View.java | 46 ++-- .../src/com/coding/basic/BinaryTreeNode.java | 64 ++--- .../src/com/coding/basic/Iterator.java | 14 +- .../src/com/coding/basic/List.java | 18 +- .../src/com/coding/basic/Queue.java | 38 +-- .../src/com/coding/basic/Stack.java | 46 ++-- .../com/coding/basic/array}/ArrayList.java | 67 ++--- .../com/coding/basic}/array/ArrayUtil.java | 192 +++++++------- .../coding/basic/linklist/LRUPageFrame.java | 120 ++++----- .../basic/linklist/LRUPageFrameTest.java | 62 ++--- .../coding/basic/linklist}/LinkedList.java | 247 +++++++++--------- liuxin/src/com/coderising/download/.classpath | 5 - liuxin/src/com/coderising/download/.project | 17 -- .../src/com/coderising/litestruts/struts.xml | 11 - 30 files changed, 814 insertions(+), 866 deletions(-) delete mode 100644 liuxin/.classpath delete mode 100644 liuxin/.gitignore delete mode 100644 liuxin/.project delete mode 100644 liuxin/.settings/org.eclipse.core.resources.prefs rename liuxin/{ => data-structure}/src/com/coderising/download/DownloadThread.java (94%) rename liuxin/{ => data-structure}/src/com/coderising/download/FileDownloader.java (96%) rename liuxin/{ => data-structure}/src/com/coderising/download/FileDownloaderTest.java (95%) rename liuxin/{ => data-structure}/src/com/coderising/download/api/Connection.java (95%) rename liuxin/{ => data-structure}/src/com/coderising/download/api/ConnectionException.java (94%) rename liuxin/{ => data-structure}/src/com/coderising/download/api/ConnectionManager.java (95%) rename liuxin/{ => data-structure}/src/com/coderising/download/api/DownloadListener.java (95%) rename liuxin/{ => data-structure}/src/com/coderising/download/impl/ConnectionImpl.java (93%) rename liuxin/{ => data-structure}/src/com/coderising/download/impl/ConnectionManagerImpl.java (96%) rename liuxin/{ => data-structure}/src/com/coderising/litestruts/LoginAction.java (95%) rename liuxin/{ => data-structure}/src/com/coderising/litestruts/Struts.java (96%) rename liuxin/{ => data-structure}/src/com/coderising/litestruts/StrutsTest.java (96%) rename liuxin/{ => data-structure}/src/com/coderising/litestruts/View.java (94%) rename liuxin/{ => data-structure}/src/com/coding/basic/BinaryTreeNode.java (94%) rename liuxin/{ => data-structure}/src/com/coding/basic/Iterator.java (93%) rename liuxin/{ => data-structure}/src/com/coding/basic/List.java (95%) rename liuxin/{ => data-structure}/src/com/coding/basic/Queue.java (92%) rename liuxin/{ => data-structure}/src/com/coding/basic/Stack.java (87%) rename liuxin/{src/com/coding/basic => data-structure/src/com/coding/basic/array}/ArrayList.java (80%) rename liuxin/{src/com/coderising => data-structure/src/com/coding/basic}/array/ArrayUtil.java (95%) rename liuxin/{ => data-structure}/src/com/coding/basic/linklist/LRUPageFrame.java (92%) rename liuxin/{ => data-structure}/src/com/coding/basic/linklist/LRUPageFrameTest.java (95%) rename liuxin/{src/com/coding/basic => data-structure/src/com/coding/basic/linklist}/LinkedList.java (94%) delete mode 100644 liuxin/src/com/coderising/download/.classpath delete mode 100644 liuxin/src/com/coderising/download/.project delete mode 100644 liuxin/src/com/coderising/litestruts/struts.xml diff --git a/liuxin/.classpath b/liuxin/.classpath deleted file mode 100644 index 2d7497573f..0000000000 --- a/liuxin/.classpath +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/liuxin/.gitignore b/liuxin/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/liuxin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/liuxin/.project b/liuxin/.project deleted file mode 100644 index b6d8ce6204..0000000000 --- a/liuxin/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - coding2017 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/liuxin/.settings/org.eclipse.core.resources.prefs b/liuxin/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 99f26c0203..0000000000 --- a/liuxin/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/=UTF-8 diff --git a/liuxin/src/com/coderising/download/DownloadThread.java b/liuxin/data-structure/src/com/coderising/download/DownloadThread.java similarity index 94% rename from liuxin/src/com/coderising/download/DownloadThread.java rename to liuxin/data-structure/src/com/coderising/download/DownloadThread.java index 1456314140..900a3ad358 100644 --- a/liuxin/src/com/coderising/download/DownloadThread.java +++ b/liuxin/data-structure/src/com/coderising/download/DownloadThread.java @@ -1,20 +1,20 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - - public DownloadThread( Connection conn, int startPos, int endPos){ - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - } - public void run(){ - - } -} +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/liuxin/src/com/coderising/download/FileDownloader.java b/liuxin/data-structure/src/com/coderising/download/FileDownloader.java similarity index 96% rename from liuxin/src/com/coderising/download/FileDownloader.java rename to liuxin/data-structure/src/com/coderising/download/FileDownloader.java index f5d7999eb4..c3c8a3f27d 100644 --- a/liuxin/src/com/coderising/download/FileDownloader.java +++ b/liuxin/data-structure/src/com/coderising/download/FileDownloader.java @@ -1,73 +1,73 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; - - -public class FileDownloader { - - String url; - - DownloadListener listener; - - ConnectionManager cm; - - - public FileDownloader(String _url) { - this.url = _url; - - } - - public void execute(){ - // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 - // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 - // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 - // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 - // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 - // 鍏蜂綋鐨勫疄鐜版濊矾锛 - // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 - // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 - // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 - // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 - // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 - - // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 - Connection conn = null; - try { - - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).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; - } - -} +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).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; + } + +} diff --git a/liuxin/src/com/coderising/download/FileDownloaderTest.java b/liuxin/data-structure/src/com/coderising/download/FileDownloaderTest.java similarity index 95% rename from liuxin/src/com/coderising/download/FileDownloaderTest.java rename to liuxin/data-structure/src/com/coderising/download/FileDownloaderTest.java index 8171ee5763..4ff7f46ae0 100644 --- a/liuxin/src/com/coderising/download/FileDownloaderTest.java +++ b/liuxin/data-structure/src/com/coderising/download/FileDownloaderTest.java @@ -1,59 +1,59 @@ -package com.coderising.download; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://localhost:8080/test.jpg"; - - 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); - //浼戠湢5绉 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("涓嬭浇瀹屾垚锛"); - - - - } - -} +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/liuxin/src/com/coderising/download/api/Connection.java b/liuxin/data-structure/src/com/coderising/download/api/Connection.java similarity index 95% rename from liuxin/src/com/coderising/download/api/Connection.java rename to liuxin/data-structure/src/com/coderising/download/api/Connection.java index 9710e270e1..0957eaf7f4 100644 --- a/liuxin/src/com/coderising/download/api/Connection.java +++ b/liuxin/data-structure/src/com/coderising/download/api/Connection.java @@ -1,23 +1,23 @@ -package com.coderising.download.api; - -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(); -} +package com.coderising.download.api; + +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(); +} diff --git a/liuxin/src/com/coderising/download/api/ConnectionException.java b/liuxin/data-structure/src/com/coderising/download/api/ConnectionException.java similarity index 94% rename from liuxin/src/com/coderising/download/api/ConnectionException.java rename to liuxin/data-structure/src/com/coderising/download/api/ConnectionException.java index 8dbfe95dda..1551a80b3d 100644 --- a/liuxin/src/com/coderising/download/api/ConnectionException.java +++ b/liuxin/data-structure/src/com/coderising/download/api/ConnectionException.java @@ -1,5 +1,5 @@ -package com.coderising.download.api; - -public class ConnectionException extends Exception { - -} +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/liuxin/src/com/coderising/download/api/ConnectionManager.java b/liuxin/data-structure/src/com/coderising/download/api/ConnectionManager.java similarity index 95% rename from liuxin/src/com/coderising/download/api/ConnectionManager.java rename to liuxin/data-structure/src/com/coderising/download/api/ConnectionManager.java index fb44ede457..ce045393b1 100644 --- a/liuxin/src/com/coderising/download/api/ConnectionManager.java +++ b/liuxin/data-structure/src/com/coderising/download/api/ConnectionManager.java @@ -1,10 +1,10 @@ -package com.coderising.download.api; - -public interface ConnectionManager { - /** - * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; -} +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/liuxin/src/com/coderising/download/api/DownloadListener.java b/liuxin/data-structure/src/com/coderising/download/api/DownloadListener.java similarity index 95% rename from liuxin/src/com/coderising/download/api/DownloadListener.java rename to liuxin/data-structure/src/com/coderising/download/api/DownloadListener.java index 4cd0b3eab1..bf9807b307 100644 --- a/liuxin/src/com/coderising/download/api/DownloadListener.java +++ b/liuxin/data-structure/src/com/coderising/download/api/DownloadListener.java @@ -1,5 +1,5 @@ -package com.coderising.download.api; - -public interface DownloadListener { - public void notifyFinished(); -} +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/liuxin/src/com/coderising/download/impl/ConnectionImpl.java b/liuxin/data-structure/src/com/coderising/download/impl/ConnectionImpl.java similarity index 93% rename from liuxin/src/com/coderising/download/impl/ConnectionImpl.java rename to liuxin/data-structure/src/com/coderising/download/impl/ConnectionImpl.java index 32f03efdc7..36a9d2ce15 100644 --- a/liuxin/src/com/coderising/download/impl/ConnectionImpl.java +++ b/liuxin/data-structure/src/com/coderising/download/impl/ConnectionImpl.java @@ -1,27 +1,27 @@ -package com.coderising.download.impl; - -import java.io.IOException; - -import com.coderising.download.api.Connection; - -public class ConnectionImpl implements Connection{ - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - - } - -} +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java b/liuxin/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java similarity index 96% rename from liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java rename to liuxin/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java index 046f7c49a4..172371dd55 100644 --- a/liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java +++ b/liuxin/data-structure/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -1,15 +1,15 @@ -package com.coderising.download.impl; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return null; - } - -} +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/liuxin/src/com/coderising/litestruts/LoginAction.java b/liuxin/data-structure/src/com/coderising/litestruts/LoginAction.java similarity index 95% rename from liuxin/src/com/coderising/litestruts/LoginAction.java rename to liuxin/data-structure/src/com/coderising/litestruts/LoginAction.java index 1005f35a29..dcdbe226ed 100644 --- a/liuxin/src/com/coderising/litestruts/LoginAction.java +++ b/liuxin/data-structure/src/com/coderising/litestruts/LoginAction.java @@ -1,39 +1,39 @@ -package com.coderising.litestruts; - -/** - * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - 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"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } -} +package com.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + 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"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/liuxin/src/com/coderising/litestruts/Struts.java b/liuxin/data-structure/src/com/coderising/litestruts/Struts.java similarity index 96% rename from liuxin/src/com/coderising/litestruts/Struts.java rename to liuxin/data-structure/src/com/coderising/litestruts/Struts.java index 44cc35bf01..85e2e22de3 100644 --- a/liuxin/src/com/coderising/litestruts/Struts.java +++ b/liuxin/data-structure/src/com/coderising/litestruts/Struts.java @@ -1,34 +1,34 @@ -package com.coderising.litestruts; - -import java.util.Map; - - - -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 璇诲彇閰嶇疆鏂囦欢struts.xml - - 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 - 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 - ("name"="test" , "password"="1234") , - 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 - - 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" - - 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, - 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , - 鏀惧埌View瀵硅薄鐨刾arameters - - 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 - 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 - - */ - - return null; - } - -} +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return null; + } + +} diff --git a/liuxin/src/com/coderising/litestruts/StrutsTest.java b/liuxin/data-structure/src/com/coderising/litestruts/StrutsTest.java similarity index 96% rename from liuxin/src/com/coderising/litestruts/StrutsTest.java rename to liuxin/data-structure/src/com/coderising/litestruts/StrutsTest.java index a44c1878ac..b8c81faf3c 100644 --- a/liuxin/src/com/coderising/litestruts/StrutsTest.java +++ b/liuxin/data-structure/src/com/coderising/litestruts/StrutsTest.java @@ -1,43 +1,43 @@ -package com.coderising.litestruts; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 - - 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")); - } -} +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + 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")); + } +} diff --git a/liuxin/src/com/coderising/litestruts/View.java b/liuxin/data-structure/src/com/coderising/litestruts/View.java similarity index 94% rename from liuxin/src/com/coderising/litestruts/View.java rename to liuxin/data-structure/src/com/coderising/litestruts/View.java index 0194c681f6..07df2a5dab 100644 --- a/liuxin/src/com/coderising/litestruts/View.java +++ b/liuxin/data-structure/src/com/coderising/litestruts/View.java @@ -1,23 +1,23 @@ -package com.coderising.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/data-structure/src/com/coding/basic/BinaryTreeNode.java similarity index 94% rename from liuxin/src/com/coding/basic/BinaryTreeNode.java rename to liuxin/data-structure/src/com/coding/basic/BinaryTreeNode.java index 266eff3d56..d7ac820192 100644 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ b/liuxin/data-structure/src/com/coding/basic/BinaryTreeNode.java @@ -1,32 +1,32 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/liuxin/src/com/coding/basic/Iterator.java b/liuxin/data-structure/src/com/coding/basic/Iterator.java similarity index 93% rename from liuxin/src/com/coding/basic/Iterator.java rename to liuxin/data-structure/src/com/coding/basic/Iterator.java index dbe8b9afb2..06ef6311b2 100644 --- a/liuxin/src/com/coding/basic/Iterator.java +++ b/liuxin/data-structure/src/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/liuxin/src/com/coding/basic/List.java b/liuxin/data-structure/src/com/coding/basic/List.java similarity index 95% rename from liuxin/src/com/coding/basic/List.java rename to liuxin/data-structure/src/com/coding/basic/List.java index 396b1f6416..10d13b5832 100644 --- a/liuxin/src/com/coding/basic/List.java +++ b/liuxin/data-structure/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -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(); -} +package com.coding.basic; + +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(); +} diff --git a/liuxin/src/com/coding/basic/Queue.java b/liuxin/data-structure/src/com/coding/basic/Queue.java similarity index 92% rename from liuxin/src/com/coding/basic/Queue.java rename to liuxin/data-structure/src/com/coding/basic/Queue.java index 08d2d86b14..36e516e266 100644 --- a/liuxin/src/com/coding/basic/Queue.java +++ b/liuxin/data-structure/src/com/coding/basic/Queue.java @@ -1,19 +1,19 @@ -package com.coding.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/liuxin/src/com/coding/basic/Stack.java b/liuxin/data-structure/src/com/coding/basic/Stack.java similarity index 87% rename from liuxin/src/com/coding/basic/Stack.java rename to liuxin/data-structure/src/com/coding/basic/Stack.java index 4bfe28057f..459ec560b4 100644 --- a/liuxin/src/com/coding/basic/Stack.java +++ b/liuxin/data-structure/src/com/coding/basic/Stack.java @@ -1,22 +1,24 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} +package com.coding.basic; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/liuxin/src/com/coding/basic/ArrayList.java b/liuxin/data-structure/src/com/coding/basic/array/ArrayList.java similarity index 80% rename from liuxin/src/com/coding/basic/ArrayList.java rename to liuxin/data-structure/src/com/coding/basic/array/ArrayList.java index 57412dcf7f..4576c016af 100644 --- a/liuxin/src/com/coding/basic/ArrayList.java +++ b/liuxin/data-structure/src/com/coding/basic/array/ArrayList.java @@ -1,32 +1,35 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - - } - public void add(int index, Object o){ - - } - - public Object get(int index){ - return null; - } - - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public Iterator iterator(){ - return null; - } - -} +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/liuxin/src/com/coderising/array/ArrayUtil.java b/liuxin/data-structure/src/com/coding/basic/array/ArrayUtil.java similarity index 95% rename from liuxin/src/com/coderising/array/ArrayUtil.java rename to liuxin/data-structure/src/com/coding/basic/array/ArrayUtil.java index 78845d06d0..45740e6d57 100644 --- a/liuxin/src/com/coderising/array/ArrayUtil.java +++ b/liuxin/data-structure/src/com/coding/basic/array/ArrayUtil.java @@ -1,96 +1,96 @@ -package com.coderising.array; - -public class ArrayUtil { - - /** - * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 - 渚嬪锛 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){ - - } - - /** - * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - return null; - } - - /** - * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 - * 渚嬪 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){ - return null; - } - /** - * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size - * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 - * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - return null; - } - - /** - * 鏂愭尝閭e鏁板垪涓猴細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){ - return null; - } - - /** - * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 - * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - return null; - } - - /** - * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 - * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - return null; - } - - /** - * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 - * 渚嬪array= [3,8,9], seperator = "-" - * 鍒欒繑鍥炲间负"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - return null; - } - - -} +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 瑕佹眰灏嗕互涓婃暟缁勪腑鍊间负0鐨勯」鍘绘帀锛屽皢涓嶄负0鐨勫煎瓨鍏ヤ竴涓柊鐨勬暟缁勶紝鐢熸垚鐨勬柊鏁扮粍涓猴細 + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + return null; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + return null; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/liuxin/src/com/coding/basic/linklist/LRUPageFrame.java b/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java similarity index 92% rename from liuxin/src/com/coding/basic/linklist/LRUPageFrame.java rename to liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java index 25595cbe4c..a4f2c14606 100644 --- a/liuxin/src/com/coding/basic/linklist/LRUPageFrame.java +++ b/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrame.java @@ -1,60 +1,60 @@ -package com.coding.basic.linklist; - -/** - * 鐢ㄥ弻鍚戦摼琛ㄥ疄鐜癓RU绠楁硶 - * @author liuxin - * - */ -public class LRUPageFrame { - - private static class Node { - - Node prev; - Node next; - int pageNum; - - Node() { - } - } - - private int capacity; - - - private Node first;// 閾捐〃澶 - private Node last;// 閾捐〃灏 - - - public LRUPageFrame(int capacity) { - - this.capacity = capacity; - - } - - /** - * 鑾峰彇缂撳瓨涓璞 - * - * @param key - * @return - */ - public void access(int pageNum) { - - - } - - - - 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(); - } - -} +package com.coding.basic.linklist; + +/** + * 鐢ㄥ弻鍚戦摼琛ㄥ疄鐜癓RU绠楁硶 + * @author liuxin + * + */ +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + + private Node first;// 閾捐〃澶 + private Node last;// 閾捐〃灏 + + + public LRUPageFrame(int capacity) { + + this.capacity = capacity; + + } + + /** + * 鑾峰彇缂撳瓨涓璞 + * + * @param key + * @return + */ + public void access(int pageNum) { + + + } + + + + 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/liuxin/src/com/coding/basic/linklist/LRUPageFrameTest.java b/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java similarity index 95% rename from liuxin/src/com/coding/basic/linklist/LRUPageFrameTest.java rename to liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java index 4070f1f2b3..67cf36067b 100644 --- a/liuxin/src/com/coding/basic/linklist/LRUPageFrameTest.java +++ b/liuxin/data-structure/src/com/coding/basic/linklist/LRUPageFrameTest.java @@ -1,31 +1,31 @@ -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()); - } - -} +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()); + } + +} diff --git a/liuxin/src/com/coding/basic/LinkedList.java b/liuxin/data-structure/src/com/coding/basic/linklist/LinkedList.java similarity index 94% rename from liuxin/src/com/coding/basic/LinkedList.java rename to liuxin/data-structure/src/com/coding/basic/linklist/LinkedList.java index d162a3687c..f4c7556a2e 100644 --- a/liuxin/src/com/coding/basic/LinkedList.java +++ b/liuxin/data-structure/src/com/coding/basic/linklist/LinkedList.java @@ -1,122 +1,125 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - /** - * 鎶婅閾捐〃閫嗙疆 - * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 - */ - public void reverse(){ - - } - - /** - * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 - * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 - * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 - * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺listB鎵鎸囧畾鐨勫厓绱 - * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - return null; - } - - /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪listB涓嚭鐜扮殑鍏冪礌 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 - */ - public void removeDuplicateValues(){ - - } - - /** - * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 - * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 - * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭istB鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺listB鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪listB涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/liuxin/src/com/coderising/download/.classpath b/liuxin/src/com/coderising/download/.classpath deleted file mode 100644 index ac37fb2e4b..0000000000 --- a/liuxin/src/com/coderising/download/.classpath +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/liuxin/src/com/coderising/download/.project b/liuxin/src/com/coderising/download/.project deleted file mode 100644 index 9360f320db..0000000000 --- a/liuxin/src/com/coderising/download/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - download - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/liuxin/src/com/coderising/litestruts/struts.xml b/liuxin/src/com/coderising/litestruts/struts.xml deleted file mode 100644 index 4c6eeabbd4..0000000000 --- a/liuxin/src/com/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file From 0ba945a8a2d0acc3f14e1ce13b307c9b6abd668b Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Sun, 26 Mar 2017 17:43:09 +0800 Subject: [PATCH 149/155] =?UTF-8?q?mini-jvm=E7=AC=AC=E4=B8=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jvm/test/ClassFileloaderTest.java | 92 +++++++++++++++++++ .../com/coderising/jvm/test/EmployeeV1.java | 28 ++++++ 2 files changed, 120 insertions(+) create mode 100644 liuxin/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java create mode 100644 liuxin/mini-jvm/src/com/coderising/jvm/test/EmployeeV1.java diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java b/liuxin/mini-jvm/src/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..a05534b210 --- /dev/null +++ b/liuxin/mini-jvm/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 = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\bin"; + static String path2 = "C:\temp"; + + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 娉ㄦ剰锛氳繖涓瓧鑺傛暟鍙兘鍜屼綘鐨凧VM鐗堟湰鏈夊叧绯伙紝 浣犲彲浠ョ湅鐪嬬紪璇戝ソ鐨勭被鍒板簳鏈夊澶 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + + private String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i Date: Sun, 26 Mar 2017 17:48:12 +0800 Subject: [PATCH 150/155] =?UTF-8?q?mini-jvm=E7=AC=AC=E4=B8=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jvm/loader/ClassFileLoader.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..9e6fc3be97 --- /dev/null +++ b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,51 @@ +package com.coderising.jvm.loader; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public byte[] readBinaryCode(String className) { + + return null; + + + } + + private byte[] loadClassFile(String clzFileName) { + + return null; + } + + + + public void addClassPath(String path) { + + } + + public String getClassPath_V1(){ + + return null; + } + + public String getClassPath(){ + return null; + } + + + + + +} From 7ba4eeb57fa25b0328e3c5752b824b81642b1605 Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Sun, 26 Mar 2017 17:49:03 +0800 Subject: [PATCH 151/155] =?UTF-8?q?mini-jvm=E7=AC=AC=E4=B8=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/jvm/loader/ClassFileLoader.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java index 9e6fc3be97..ec08e74097 100644 --- a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java +++ b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -1,16 +1,8 @@ package com.coderising.jvm.loader; -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; - public class ClassFileLoader { From 615e7d1c4b34160b8fd66fe24454be017aa0b613 Mon Sep 17 00:00:00 2001 From: zhanglei <383117348@qq.com> Date: Sun, 26 Mar 2017 18:52:51 +0800 Subject: [PATCH 152/155] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/download/DownloadThread.java | 61 ++++++ .../coderising/download/FileDownloader.java | 70 +++++++ .../download/FileDownloaderTest.java | 55 ++++++ .../coderising/download/api/Connection.java | 23 +++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 71 +++++++ .../download/impl/ConnectionManagerImpl.java | 15 ++ .../src/com/coding/basic/LinkedList.java | 177 +++++++++++++++++- 10 files changed, 489 insertions(+), 3 deletions(-) create mode 100644 group27/383117348/src/com/coderising/download/DownloadThread.java create mode 100644 group27/383117348/src/com/coderising/download/FileDownloader.java create mode 100644 group27/383117348/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group27/383117348/src/com/coderising/download/api/Connection.java create mode 100644 group27/383117348/src/com/coderising/download/api/ConnectionException.java create mode 100644 group27/383117348/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group27/383117348/src/com/coderising/download/api/DownloadListener.java create mode 100644 group27/383117348/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group27/383117348/src/com/coderising/download/impl/ConnectionManagerImpl.java diff --git a/group27/383117348/src/com/coderising/download/DownloadThread.java b/group27/383117348/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..739dc261a6 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,61 @@ +package com.coderising.download; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.DownloadListener; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + FileDownloader fileDown; + + public DownloadThread( Connection conn, int startPos, int endPos){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + FileOutputStream fos = null; + ByteArrayInputStream bis =null; + try { + byte[] bt = conn.read(startPos, endPos); + File file = new File("C:\\Users\\Adminstater\\Desktop\\test"+Math.ceil(Math.random()*100)+".jpg"); + if(!file.exists()){ + file.createNewFile(); + } + fos = new FileOutputStream(file); + bis = new ByteArrayInputStream(bt); + int i = 0; + byte[] copy = new byte[1024]; + while((i=bis.read(copy))!=-1){ + fos.write(copy, 0, i); + fos.flush(); + } + + DownloadListener listener = fileDown.getListener(); + listener.notifyFinished(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }finally{ + try { + fos.close(); + bis.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + } + public void setFileDown(FileDownloader fileDown) { + this.fileDown = fileDown; + } + +} diff --git a/group27/383117348/src/com/coderising/download/FileDownloader.java b/group27/383117348/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..b390613073 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,70 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm ; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + DownloadThread thread = new DownloadThread(conn,0,length-1); + thread.setFileDown(this); + thread.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; + } + +} diff --git a/group27/383117348/src/com/coderising/download/FileDownloaderTest.java b/group27/383117348/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..6b7da40aea --- /dev/null +++ b/group27/383117348/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,55 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + String url = "http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"; + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group27/383117348/src/com/coderising/download/api/Connection.java b/group27/383117348/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..9710e270e1 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +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(); +} diff --git a/group27/383117348/src/com/coderising/download/api/ConnectionException.java b/group27/383117348/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..8dbfe95dda --- /dev/null +++ b/group27/383117348/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group27/383117348/src/com/coderising/download/api/ConnectionManager.java b/group27/383117348/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..fb44ede457 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group27/383117348/src/com/coderising/download/api/DownloadListener.java b/group27/383117348/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..4cd0b3eab1 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group27/383117348/src/com/coderising/download/impl/ConnectionImpl.java b/group27/383117348/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..2f1cfa14a5 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,71 @@ +package com.coderising.download.impl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private URL url; + private HttpURLConnection connection; + + public ConnectionImpl(String url) { + try { + this.url = new URL(url); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + private HttpURLConnection initConnection(){ + HttpURLConnection con = null; + try { + con = (HttpURLConnection)url.openConnection(); + con.setConnectTimeout(2000); + con.setRequestMethod("GET"); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return con; + } + @Override + public byte[] read(int startPos, int endPos) throws IOException { + connection = initConnection(); + connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); //nullPointException + InputStream input = connection.getInputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int i=0; + byte[] bt = new byte[1024]; + while((i=input.read(bt))!=-1){ + bos.write(bt,0,i); + } + return bos.toByteArray(); + } + + @Override + public int getContentLength() { + HttpURLConnection con = initConnection(); + try { + if (con.getResponseCode() == 200){ + //鏈嶅姟鍣ㄨ繑鍥炲唴瀹圭殑闀垮害锛屾湰璐ㄥ氨鏄枃浠剁殑闀垮害 + return con.getContentLength(); + } + } catch (IOException e) { + e.printStackTrace(); + } + return 0; + } + + @Override + public void close() { + this.connection=null; + } + +} diff --git a/group27/383117348/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/383117348/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..f1db9d72a9 --- /dev/null +++ b/group27/383117348/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group27/383117348/src/com/coding/basic/LinkedList.java b/group27/383117348/src/com/coding/basic/LinkedList.java index faeafffca6..56cead0c67 100644 --- a/group27/383117348/src/com/coding/basic/LinkedList.java +++ b/group27/383117348/src/com/coding/basic/LinkedList.java @@ -1,5 +1,6 @@ package com.coding.basic; +import java.util.Arrays; import java.util.NoSuchElementException; import org.junit.Test; @@ -266,6 +267,172 @@ private Node() { } } + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + Node head = first; + Node reverse = null; + while (head != null) { + Node second = head.next; + head.next = reverse; + reverse = head; + head = second; + } + first = reverse; + } + //鏈夐棶棰 + @Test + public void testReverse(){ + LinkedList list = getList(); + Iterator ite = list.iterator(); + while(ite.hasNext()){ + System.out.print(ite.next()+" "); + } + list.reverse(); + Iterator it = list.iterator(); + while(it.hasNext()){ + System.out.println("----"); + System.out.print(it.next()+" "); + } + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + */ + public void removeFirstHalf(){ + int mid = (int) Math.ceil(size/2.0); + for(int x=0;x0){ + + Node prev = getNodeByIndex(i-1); + Node next = getNodeByIndex(i+length); + for(int x=i;x101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] array = new int[list.size]; + for (int i = 0; i < array.length; i++) { + int element = (int) list.get(i); + array[i] = ((Integer) get(element)); + } + + System.out.println(Arrays.toString(array)); + + return array; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + * @param list + */ + + public void subtract(LinkedList list){ + int length = list.size(); + for (int i = size - 1; i >= 0; i--) { + for (int j = 0; j < length; j++) { + if (get(i) == list.get(j)) { + remove(i); + break; + } + } + } + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + for (int i = size - 1; i > 0; i--) { + if (get(i) == get(i - 1)) { + remove(i); + } + } + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + for (int i = size - 1; i >= 0; i--) { + int element = ((int) get(i)); + if ((element > min) && element < max) { + remove(i); + } + } + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList newList = new LinkedList(); + int length = list.size(); + for (int i = 0; i < size; i++) { + for (int j = 0; j < length; j++) { + if (get(i) == list.get(j)) { + newList.add(get(i)); + break; + } + } + } + + Iterator it = newList.iterator(); + while (it.hasNext()) { + System.out.print(it.next() + " "); + } + System.out.println(); + return newList; + } /*------------------------------------------------------鍗曞厓娴嬭瘯----------------------------------------------------*/ @@ -375,8 +542,12 @@ public void TestIterator() { System.out.println(ite.next()); } } - - public static void main(String[] args) { - java.util.LinkedList list = null; + + private LinkedList getList(){ + LinkedList list = new LinkedList(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + return list; } } From bf73be0582648a489258ea769fd2570afcf83930 Mon Sep 17 00:00:00 2001 From: fzon0902 <815591664@qq.com> Date: Sun, 26 Mar 2017 22:11:19 +0800 Subject: [PATCH 153/155] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/download/DownloadThread.java | 47 +++ .../coderising/download/FileDownloader.java | 91 ++++ .../download/FileDownloaderTest.java | 59 +++ .../src/com/coderising/download/Test.java | 172 ++++++++ .../coderising/download/api/Connection.java | 25 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 44 ++ .../download/impl/ConnectionManagerImpl.java | 47 +++ .../src/com/coding/basic/LinkedList.java | 393 +++++++++++++----- 11 files changed, 804 insertions(+), 94 deletions(-) create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/DownloadThread.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/FileDownloader.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/Test.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java diff --git a/group27/815591664/2017Learning/src/com/coderising/download/DownloadThread.java b/group27/815591664/2017Learning/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..9deb365291 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,47 @@ +package com.coderising.download; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + String threadId; + int startPos; + int endPos; + + public DownloadThread( Connection conn, String threadId, int startPos, int endPos){ + + this.conn = conn; + this.threadId = threadId; + this.startPos = startPos; + this.endPos = endPos; + + } + public void run(){ + System.out.println("绾跨▼"+threadId+"寮濮嬩笅杞,璧风偣涓"+startPos+",缁堢偣涓"+endPos); + + RandomAccessFile raf; + try { + raf = new RandomAccessFile("f:/test.jpg", "rw"); + byte[] content = conn.read(startPos, endPos); + raf.seek(startPos);//鏂囦欢鍐欏叆鐨勫紑濮嬩綅缃. + raf.write(content); + System.out.println("绾跨▼"+threadId+"涓嬭浇瀹屾瘯锛"); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + + } +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/FileDownloader.java b/group27/815591664/2017Learning/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..ce984d9ced --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,91 @@ +package com.coderising.download; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + int threadCount = 3; + + + public FileDownloader(String _url) { + this.url = _url; + + + } + + public FileDownloader(String _url, int _threadCount) { + this.url = _url; + this.threadCount = _threadCount; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + Connection conn = null; + try { + + conn = cm.open(url); + + int length = conn.getContentLength(); + System.out.println(length); + for(int i = 0;i < threadCount; i++){ + int startPos = i * (length/threadCount); + int endPos = startPos + (length/threadCount); + if(i == (threadCount-1)){ + endPos = length - 1; + } + + new DownloadThread(cm.open(url), String.valueOf(i), startPos, endPos).start(); + } + + + } catch (Exception e) { + e.printStackTrace(); + } + + + + + } + + 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/group27/815591664/2017Learning/src/com/coderising/download/FileDownloaderTest.java b/group27/815591664/2017Learning/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..ebc6978554 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/ForDownload/test.jpg"; + + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }*/ + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/Test.java b/group27/815591664/2017Learning/src/com/coderising/download/Test.java new file mode 100644 index 0000000000..994a14b989 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/Test.java @@ -0,0 +1,172 @@ +package com.coderising.download; + +import java.io.File; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * 澶氱嚎绋嬩笅杞 鍜 鏂偣缁紶 + * @author 鏉ㄦ枌. + * + */ +public class Test { + +// private String path = "http://mpge.5nd.com/2016/2016-11-15/74847/1.mp3"; //涓嬭浇璺緞 + private String path = "http://localhost:8080/ForDownload/test.jpg"; + private String targetFilePath="/"; //涓嬭浇鏂囦欢瀛樻斁鐩綍 + private int threadCount = 3; //绾跨▼鏁伴噺 + + /** + * 鏋勯犳柟娉 + * @param path 瑕佷笅杞芥枃浠剁殑缃戠粶璺緞 + * @param targetFilePath 淇濆瓨涓嬭浇鏂囦欢鐨勭洰褰 + * @param threadCount 寮鍚殑绾跨▼鏁伴噺,榛樿涓 3 + */ + public Test(String path, String targetFilePath, int threadCount) { + this.path = path; + this.targetFilePath = targetFilePath; + this.threadCount = threadCount; + } + public Test() { + + } + + /** + * 涓嬭浇鏂囦欢 + */ + public void download() throws Exception{ + //杩炴帴璧勬簮 + URL url = new URL(path); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setConnectTimeout(10000); + + int code = connection.getResponseCode(); + if(code == 200){ + //鑾峰彇璧勬簮澶у皬 + int connectionLength = connection.getContentLength(); + System.out.println(connectionLength); + //鍦ㄦ湰鍦板垱寤轰竴涓笌璧勬簮鍚屾牱澶у皬鐨勬枃浠舵潵鍗犱綅 + /*RandomAccessFile randomAccessFile = new RandomAccessFile(new File(targetFilePath,getFileName(url)), "rw"); + randomAccessFile.setLength(connectionLength);*/ + /* + * 灏嗕笅杞戒换鍔″垎閰嶇粰姣忎釜绾跨▼ + */ + int blockSize = connectionLength/threadCount;//璁$畻姣忎釜绾跨▼鐞嗚涓婁笅杞界殑鏁伴噺. + for(int threadId = 0; threadId < threadCount; threadId++){//涓烘瘡涓嚎绋嬪垎閰嶄换鍔 + int startIndex = threadId * blockSize; //绾跨▼寮濮嬩笅杞界殑浣嶇疆 + int endIndex = (threadId+1) * blockSize -1; //绾跨▼缁撴潫涓嬭浇鐨勪綅缃 + if(threadId == (threadCount - 1)){ //濡傛灉鏄渶鍚庝竴涓嚎绋,灏嗗墿涓嬬殑鏂囦欢鍏ㄩ儴浜ょ粰杩欎釜绾跨▼瀹屾垚 + endIndex = connectionLength - 1; + } + + new DownloadThread(threadId, startIndex, endIndex).start();//寮鍚嚎绋嬩笅杞 + + } +// randomAccessFile.close(); + } + + } + + //涓嬭浇鐨勭嚎绋 + private class DownloadThread extends Thread{ + + private int threadId; + private int startIndex; + private int endIndex; + + public DownloadThread(int threadId, int startIndex, int endIndex) { + this.threadId = threadId; + this.startIndex = startIndex; + this.endIndex = endIndex; + } + + @Override + public void run() { + System.out.println("绾跨▼"+ threadId + "寮濮嬩笅杞"); + try { + //鍒嗘璇锋眰缃戠粶杩炴帴,鍒嗘灏嗘枃浠朵繚瀛樺埌鏈湴. + URL url = new URL(path); + + //鍔犺浇涓嬭浇浣嶇疆鐨勬枃浠 + File downThreadFile = new File(targetFilePath,"downThread_" + threadId+".dt"); + RandomAccessFile downThreadStream = null; + if(downThreadFile.exists()){//濡傛灉鏂囦欢瀛樺湪 + downThreadStream = new RandomAccessFile(downThreadFile,"rwd"); + String startIndex_str = downThreadStream.readLine(); + this.startIndex = Integer.parseInt(startIndex_str);//璁剧疆涓嬭浇璧风偣 + + }else{ + downThreadStream = new RandomAccessFile(downThreadFile,"rwd"); + } + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setConnectTimeout(10000); + + //璁剧疆鍒嗘涓嬭浇鐨勫ご淇℃伅銆 Range:鍋氬垎娈垫暟鎹姹傜敤鐨勩傛牸寮: Range bytes=0-1024 鎴栬 bytes:0-1024 + connection.setRequestProperty("Range", "bytes="+ startIndex + "-" + endIndex); + + System.out.println("绾跨▼_"+threadId + "鐨勪笅杞借捣鐐规槸 " + startIndex + " 涓嬭浇缁堢偣鏄: " + endIndex); + + if(connection.getResponseCode() == 206){//200锛氳姹傚叏閮ㄨ祫婧愭垚鍔燂紝 206浠h〃閮ㄥ垎璧勬簮璇锋眰鎴愬姛 + InputStream inputStream = connection.getInputStream();//鑾峰彇娴 + RandomAccessFile randomAccessFile = new RandomAccessFile( + new File(targetFilePath,getFileName(url)), "rw");//鑾峰彇鍓嶉潰宸插垱寤虹殑鏂囦欢. + randomAccessFile.seek(startIndex);//鏂囦欢鍐欏叆鐨勫紑濮嬩綅缃. + + + /* + * 灏嗙綉缁滄祦涓殑鏂囦欢鍐欏叆鏈湴 + */ + byte[] buffer = new byte[1024]; + int length = -1; + int total = 0;//璁板綍鏈涓嬭浇鏂囦欢鐨勫ぇ灏 + while((length = inputStream.read(buffer)) > 0){ + randomAccessFile.write(buffer, 0, length); + total += length; + /* + * 灏嗗綋鍓嶇幇鍦ㄥ埌鐨勪綅缃繚瀛樺埌鏂囦欢涓 + */ + downThreadStream.seek(0); + downThreadStream.write((startIndex + total + "").getBytes("UTF-8")); + } + + downThreadStream.close(); + inputStream.close(); + randomAccessFile.close(); + cleanTemp(downThreadFile);//鍒犻櫎涓存椂鏂囦欢 + System.out.println("绾跨▼"+ threadId + "涓嬭浇瀹屾瘯"); + }else{ + System.out.println("鍝嶅簲鐮佹槸" +connection.getResponseCode() + ". 鏈嶅姟鍣ㄤ笉鏀寔澶氱嚎绋嬩笅杞"); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + + //鍒犻櫎绾跨▼浜х敓鐨勪复鏃舵枃浠 + private synchronized void cleanTemp(File file){ + file.delete(); + } + + //鑾峰彇涓嬭浇鏂囦欢鐨勫悕绉 + private String getFileName(URL url){ + String filename = url.getFile(); + return filename.substring(filename.lastIndexOf("/")+1); + } + + public static void main(String[] args) { + try { + new Test().download(); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java b/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..7a8c9e7600 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/Connection.java @@ -0,0 +1,25 @@ +package com.coderising.download.api; + +import java.io.IOException; +import java.net.HttpURLConnection; + +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(); +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java b/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..2d1053ebae --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,44 @@ +package com.coderising.download.impl; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private HttpURLConnection conn = null; + + public ConnectionImpl(HttpURLConnection conn) { + super(); + this.conn = conn; + } + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + conn.setRequestProperty("Range", "bytes="+ startPos + "-" + endPos); + InputStream is = conn.getInputStream(); + byte[] buffer = new byte[endPos - startPos + 1]; + is.read(buffer, 0, endPos - startPos + 1); + + return buffer; + + } + + @Override + public int getContentLength(){ + return conn.getContentLength(); + } + + @Override + public void close() { + + if(conn != null){ + conn.disconnect(); + } + } + +} diff --git a/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..69817bd783 --- /dev/null +++ b/group27/815591664/2017Learning/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,47 @@ +package com.coderising.download.impl; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + try { + URL urlObj = new URL(url); + HttpURLConnection conn = (HttpURLConnection)urlObj.openConnection(); + //瓒呮椂 + conn.setConnectTimeout(3*1000); + //conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + conn.setRequestMethod("GET"); + return new ConnectionImpl(conn); + } catch (Exception e) { + throw new ConnectionException(); + } + + } + + public static void main(String[] args) throws ConnectionException, IOException { + ConnectionManager cm = new ConnectionManagerImpl(); + Connection conn = cm.open("http://localhost:8080/ForDownload/test.jpg"); + + System.out.println(conn.getContentLength()); + + byte[] content = conn.read(0, conn.getContentLength()-1); + OutputStream os = new FileOutputStream("d:test.jpg"); + os.write(content); + os.flush(); + + conn.close(); + os.close(); + } + +} diff --git a/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java b/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java index 6f2fbf1f5b..abb912f644 100644 --- a/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java +++ b/group27/815591664/2017Learning/src/com/coding/basic/LinkedList.java @@ -3,7 +3,6 @@ public class LinkedList implements List { private Node head; - private Node tail; private int size; @@ -11,100 +10,90 @@ public void add(Object o){ this.addLast(o); } - public void add(int index , Object o) throws Exception{ + public void add(int index , Object o){ + if(index<0 || index>size){ + throw new IndexOutOfBoundsException(); + } if(index==0){ this.addFirst(o); + size++; return; - }else if(index==size-1){ + }else if(index==size){ this.addLast(o); + size++; return; - }else{ - - - Node curNode = this.getNode(index); - Node pre = curNode.previous; -// Node next = curNode.next; - //加入链表 - Node newNode = new Node(o, pre, curNode); - curNode.previous = newNode; - pre.next = newNode; } + Node preNode = this.getNode(index-1); + Node curNode = this.getNode(index); + Node newNode = new Node(o, curNode); + preNode.next = newNode; + + size++; } - private Node getNode(int index) throws Exception{ - if(index>=size){ - throw new Exception("下标超限"); + + private Node getNode(int index){ + if(index <0 || index>=size){ + throw new IndexOutOfBoundsException(); } if(index ==0){ return head; - }else if(index==size-1){ - return tail; - - }else{ - Node temp = head; - for(int i =1;i<=index;i++){ - temp = temp.next; - } - return temp; } + Node curNode = head; + for(int i=1;i<=index;i++){ + curNode = curNode.next; + } + return curNode; } - public Object get(int index) throws Exception{ - if(index>=size){ - throw new Exception("下标超限"); + + public Object get(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); } - if(index ==0){ - return head.data; - }else if(index==size-1){ - return tail.data; - - }else{ - Node temp = head; - for(int i =1;i<=index;i++){ - temp = temp.next; - } - return temp.data; + + Node temp = head; + for(int i =1;i<=index;i++){ + temp = temp.next; } + return temp.data; } - public Object remove(int index) throws Exception{ - if(index>=size){ - throw new Exception("下标超限"); + public Object remove(int index){ + if(index<0 || index>=size){ + throw new IndexOutOfBoundsException(); } Object o = null; if(size == 1){ o = head.data; + size--; + return o; + } + if(index==0){ + o = head.data; + Node afterHead = head.next; + head = afterHead; + + }else if(index==size-1){ + Node preTail = getNode(index-1); + Node tail = preTail.next; + o = tail.data; + preTail.next = null; }else{ - if(index==0){ - - Node afterHead = head.next; - afterHead.previous = null; - head = afterHead; - o = head.data; - - }else if(index == size-1){ - Node beforeTail = tail.previous; - beforeTail.next = null; - tail = beforeTail; - o = tail.data; - }else{ - Node curNode = this.getNode(index); - Node pre = curNode.previous; - Node next = curNode.next; - //中间变量用于断开指针 - Node temp = new Node(next.data, pre, next.next); - pre.next = temp; - next = temp; - o = curNode.data; - - } + Node preCur = getNode(index-1); + Node cur = preCur.next; + Node nextCur = cur.next; + o = cur.data; + preCur.next = nextCur; + + } + size--; + return o; - } - size--; - return o; + } @@ -114,39 +103,39 @@ public int size(){ } public void addFirst(Object o){ - Node node = new Node(o, null, head); + Node node = new Node(o,null); if(head == null){ head = node; - tail = node; - }else{ - head.previous = node; - head = node; + size++; + return; } + head = new Node(o, head); size++; } public void addLast(Object o){ - //新节点的previous指针指向tail - Node curNode = new Node(o, tail, null); - if(tail==null){ - //当前链表为空时,将该节点加入链表,头尾均为该节点 - head = curNode; - tail = curNode; - }else{ - //如果该链表不为空时,将最后一个节点的next指针指向新加入的节点即可 - tail.next = curNode; - //将新加入的这个节点置为tail - tail = curNode; - + //新节点的next指针指向tail + Node add = new Node(o, null); + if(head==null){ + head = add; + size++; + return; + } + Node curNode = head; + while(curNode.next != null){ + curNode = curNode.next; } - size++; + curNode.next = add; + size++; } - public Object removeFirst() throws Exception{ + + + public Object removeFirst(){ return this.remove(0); } - public Object removeLast() throws Exception{ + public Object removeLast(){ return this.remove(size-1); } @@ -186,7 +175,9 @@ public String toString() { } } - sb.deleteCharAt(sb.lastIndexOf(",")); + if(sb.indexOf(",") != -1){ + sb.deleteCharAt(sb.lastIndexOf(",")); + } sb.append("]"); return sb.toString(); @@ -199,12 +190,10 @@ public String toString() { private static class Node{ private Object data; - private Node previous; private Node next; - public Node(Object data, Node previous, Node next) { + public Node(Object data, Node next) { super(); this.data = data; - this.previous = previous; this.next = next; } @@ -228,9 +217,225 @@ public static void main(String[] args) throws Exception { ll.add(4); System.out.println(ll); - Iterator itr = ll.iterator(); + + ll.reverse(); + System.out.println(ll); + /*System.out.println(ll.get(0)); + System.out.println(ll.get(1)); + System.out.println(ll.get(2));*/ + + LinkedList ll2 = new LinkedList(); + ll2.add(1); +// ll2.add(1); + ll2.add(2); +// ll2.add(3); + ll2.add(3); +// ll2.add(4); + ll2.add(4); + ll2.add(5); +// ll2.removeFirstHalf(); +// ll2.remove(2,3); +// ll2.removeDuplicateValues(); + + System.out.println(ll2); + +// ll2.removeRange(2, 6); +// ll2.remove(3); + System.out.println(ll2); + + LinkedList ll3 = new LinkedList(); + ll3.add(2); + ll3.add(4); + ll2.subtract(ll3); + System.out.println(ll2); + + + + + + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + LinkedList temp = new LinkedList(); + for(int i = size - 1;i >= 0; i--){ + temp.add(this.get(i)); + } + System.out.println("---"+temp.toString()+"---"); + //清空原链表 + this.clear(); + + System.out.println("---"+this.toString()+"---"); + for(int i = 0; i < temp.size();i++){ + Object o = temp.get(i); + this.add(o); + } + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(this.size() == 0){ + return; + } + int temp = this.size(); + for(int i = 1; i <= temp/2; i++){ + this.removeFirst(); + } + + + } + + public void clear(){ + + Iterator itr = this.iterator(); while(itr.hasNext()){ - System.out.println(itr.next()); + this.removeFirst(); + } + this.head = null; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + for(int j = 0;j < length; j++){ + this.remove(i); + } + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] result = new int[list.size()]; + for(int i = 0; i min && countForMin == 0){ + indexMin = i; + countForMin++; + } + if(eleBack < max && countForMax == 0){ + indexMax = this.size()-1-i; + countForMax++; + } + } + + if(indexMin != -1 && indexMax != -1){ + for(int i = indexMin; i <= indexMax; i++){ + this.remove(indexMin); + } + + } + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + LinkedList result = new LinkedList(); + for(int i = 0; i < this.size(); i++){ + Integer temp1 = (Integer)this.get(i); + for(int j = 0; j < list.size(); j++){ + Integer temp2 = (Integer)list.get(j); + if(temp1 == temp2){ + result.add(temp2); + } + + } } + return result; } } From 84029fa594e51b27e59bc19da51039304b374b03 Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Mon, 27 Mar 2017 09:28:23 +0800 Subject: [PATCH 154/155] =?UTF-8?q?=E9=87=8D=E6=9E=84=20ClassFilieLoader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/jvm/loader/ClassFileLoader.java | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java index ec08e74097..86d4619407 100644 --- a/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java +++ b/liuxin/mini-jvm/src/com/coderising/jvm/loader/ClassFileLoader.java @@ -16,21 +16,12 @@ public byte[] readBinaryCode(String className) { } - private byte[] loadClassFile(String clzFileName) { - - return null; - } - - public void addClassPath(String path) { } - public String getClassPath_V1(){ - - return null; - } + public String getClassPath(){ return null; From bbd2cd22d50a9d18048c76c14095529b893e6a24 Mon Sep 17 00:00:00 2001 From: guoqixuan <1016908591@qq.com> Date: Tue, 28 Mar 2017 21:13:11 +0800 Subject: [PATCH 155/155] 1016908591week03 --- .../coderising/download/DownloadThread.java | 49 +++ .../coderising/download/FileDownloader.java | 142 +++++++++ .../download/FileDownloaderTest.java | 59 ++++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 83 +++++ .../download/impl/ConnectionManagerImpl.java | 14 + .../download/test/ConnectionTest.java | 57 ++++ .../download/test/FileDownloaderTest.java | 66 ++++ .../src/com/coding/basic/LinkedList.java | 283 ++++++++++++++++++ 12 files changed, 796 insertions(+) create mode 100644 group27/1016908591/week03/src/com/coderising/download/DownloadThread.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/FileDownloader.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/api/Connection.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/api/ConnectionException.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/api/DownloadListener.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/test/ConnectionTest.java create mode 100644 group27/1016908591/week03/src/com/coderising/download/test/FileDownloaderTest.java create mode 100644 group27/1016908591/week03/src/com/coding/basic/LinkedList.java diff --git a/group27/1016908591/week03/src/com/coderising/download/DownloadThread.java b/group27/1016908591/week03/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..c357c245c3 --- /dev/null +++ b/group27/1016908591/week03/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,49 @@ +package com.coderising.download; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier; + String localFile; + +public DownloadThread( Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + public void run(){ + + + try { + System.out.println("Begin to read [" + startPos +"-"+endPos+"]"); + //璋冪敤read + byte[] data = conn.read(startPos, endPos); + + RandomAccessFile file = new RandomAccessFile(localFile,"rw"); + //绫讳技鎸囬拡鎸囧埌寮濮嬪 + file.seek(startPos); + //鎶婃暟鎹啓鍏 + file.write(data); + + file.close(); + + conn.close(); + + barrier.await(); //绛夊緟鍒殑绾跨▼瀹屾垚 + + } catch (Exception e) { + e.printStackTrace(); + + } + } +} diff --git a/group27/1016908591/week03/src/com/coderising/download/FileDownloader.java b/group27/1016908591/week03/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..632cf676fb --- /dev/null +++ b/group27/1016908591/week03/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,142 @@ +package com.coderising.download; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + private String localFile; + + DownloadListener listener; + + ConnectionManager cm; + private String loaclFile; + + + private static final int DOWNLOAD_TRHEAD_NUM = 3; + + public FileDownloader(String _url, String localFile) { + this.url = _url; + this.localFile = localFile; + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }); + Connection conn = null; + + try { + conn = cm.open(this.url); + int length = conn.getContentLength(); + + createPlaceHolderFile(this.loaclFile,length); + int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length); +for(int i=0; i< DOWNLOAD_TRHEAD_NUM; i++){ + + + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + + thread.start(); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + + + + + + + + + private int[][] allocateDownloadRange(int threadNum, int contentLen) { + int[][] ranges = new int[threadNum][2]; + //鏂囦欢闀垮害闄ゅ幓绾跨▼涓暟 + int eachThreadSize = contentLen / threadNum;// 姣忎釜绾跨▼闇瑕佷笅杞界殑鏂囦欢澶у皬 + //璁板綍浣欐暟 + int left = contentLen % threadNum;// 鍓╀笅鐨勫綊鏈鍚庝竴涓嚎绋嬫潵澶勭悊 + //瀵圭嚎绋嬫暟鐩仛涓涓惊鐜 + for(int i=0;i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + } + + return baos.toByteArray(); + + } + + @Override + public int getContentLength() { + + URLConnection con; + try { + con = url.openConnection(); + + return con.getContentLength(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return -1; + } + + @Override + public void close() { + + + } + +} diff --git a/group27/1016908591/week03/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group27/1016908591/week03/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..8b7e9cc665 --- /dev/null +++ b/group27/1016908591/week03/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,14 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/group27/1016908591/week03/src/com/coderising/download/test/ConnectionTest.java b/group27/1016908591/week03/src/com/coderising/download/test/ConnectionTest.java new file mode 100644 index 0000000000..6c127cb054 --- /dev/null +++ b/group27/1016908591/week03/src/com/coderising/download/test/ConnectionTest.java @@ -0,0 +1,57 @@ +package com.coderising.download.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class ConnectionTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + //娴嬭瘯杩炴帴url鐨勫姛鑳 + public void testContentLength() throws Exception{ + //new涓涓帴鍙o紝鐒跺悗瀹炵幇杩欎釜鎺ュ彛 + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + Assert.assertEquals(35470, conn.getContentLength()); + } + + @Test + //娴嬭瘯璇诲叆锛岀‘淇濊璁$殑鎺ュ彛ok + public void testRead() throws Exception{ + + ConnectionManager connMan = new ConnectionManagerImpl(); + Connection conn = connMan.open("http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"); + + byte[] data = conn.read(0, 35469); + + Assert.assertEquals(35470, data.length); + + data = conn.read(0, 1023); + + Assert.assertEquals(1024, data.length); + + data = conn.read(1024, 2023); + + Assert.assertEquals(1000, data.length); + + + // 娴嬭瘯涓嶅厖鍒嗭紝娌℃湁鏂█鍐呭鏄惁姝g‘ + } + + + + +} diff --git a/group27/1016908591/week03/src/com/coderising/download/test/FileDownloaderTest.java b/group27/1016908591/week03/src/com/coderising/download/test/FileDownloaderTest.java new file mode 100644 index 0000000000..40f01fd9ac --- /dev/null +++ b/group27/1016908591/week03/src/com/coderising/download/test/FileDownloaderTest.java @@ -0,0 +1,66 @@ +package com.coderising.download.test; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.FileDownloader; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + /*棰 涓鑸殑鏃跺欐槸缃戠粶杩炴帴鐨勯棶棰樸傝繕鏈変綘鍙互璁剧疆涓涓 鏈嶅姟鍣ㄧ殑瓒呮椂鏃堕棿銆傛湁鐨勫彲鑳芥槸涓夊崄绉掋備綘璇曡瘯鏇撮暱鐐圭殑銆 + * 杩樻湁涓绉嶅彲鑳芥ф槸銆備綘绋嬪簭閲屽垱寤轰簡寰堝connection 浣嗘槸娌℃湁鍏抽棴璋冦傜幇鍦ㄦ暟鎹簱澶勪簬鍗婃鐘舵侊紝鐒跺悗杩炴帴瓒呮椂銆 + * 浣爌ing鐨勬槸鏈嶅姟鍣ㄧ殑ip鍚楋紵 浣犲彲浠ョ敤plsql妫楠屼竴涓嬩綘鐨勭綉缁滄槸鍚﹂氥傝繕鏈夐厤缃湇鍔″櫒鏁版嵁婧愮殑鏃跺 濡傛灉鑳介厤缃垚鍔燂紝 + * 閭g綉缁滀篃娌¢棶棰樸 + */ + //String url = "http://www.hinews.cn/pic/0/13/91/26/13912621_821796.jpg"; + + String url = "http://images2015.cnblogs.com/blog/610238/201604/610238-20160421154632101-286208268.png"; + + FileDownloader downloader = new FileDownloader(url,"e:\\椤圭洰缁冩墜\\test.jpg"); + + + 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("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group27/1016908591/week03/src/com/coding/basic/LinkedList.java b/group27/1016908591/week03/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..8efd566a00 --- /dev/null +++ b/group27/1016908591/week03/src/com/coding/basic/LinkedList.java @@ -0,0 +1,283 @@ +package com.coding.basic; +import javax.xml.crypto.Data; + + + +public class LinkedList implements List { + + private Node head; + private int length; + //鏋勯犲嚱鏁 + public LinkedList(){ + clear(); + } + public final void clear(){ + head = null; + length = 0; + } + + public void add(Object o){ + Node newNode = new Node(o); + if(length == 0) + { + head = newNode; + } + else{ + Node lastNode = getNodeAt(length); + lastNode.next = newNode; + + } + length++; + + + } + public void add(int index , Object o){ + Node newNode = new Node(o); + Node nodeBefor = getNodeAt(index-1); + Node nodeAfter = nodeBefor.next; + newNode.next = nodeAfter; + nodeBefor.next = newNode; + length++; + + + } + public Object get(int index){ + if((1<=index)&&(index<=length)) + { + Node currentNode = head; + for(int i= 0;i7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + + Node lastNode = getNodeAt(length); + head = lastNode; + while(length>0){ + Node currentNode = getNodeAt(--length); + add(currentNode); + + } + + + + + + + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + int num = length/2; + while(num>0){ + remove(num); + num--; + } + + } + + /** + * 浠庣i涓厓绱犲紑濮嬶紝 鍒犻櫎length 涓厓绱 锛 娉ㄦ剰i浠0寮濮 + * @param i + * @param length + */ + public void remove(int i, int length){ + while (length>0){ + remove(i+length); + length--; + } + + } + /** + * 鍋囧畾褰撳墠閾捐〃鍜宭ist鍧囧寘鍚凡鍗囧簭鎺掑垪鐨勬暣鏁 + * 浠庡綋鍓嶉摼琛ㄤ腑鍙栧嚭閭d簺list鎵鎸囧畾鐨勫厓绱 + * 渚嬪褰撳墠閾捐〃 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int[] arr = new int[list.size()]; + + for(int i =0;idata) + remove(i); + } + + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection( LinkedList list){ + if(list==null){ + return null; + } + int i1 = 0; + int i2 = 0; + LinkedList result = new LinkedList(); + Node currentListNode = list.head; + Node currentThisNode = this.head; + for(i1 =0;i1