From 0516d03c5a461829a7d0314cb21e0b7425d58048 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Thu, 16 Mar 2017 22:10:54 +0800 Subject: [PATCH 1/6] =?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 , 对该数组的值进行置换 + * 例如: 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和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 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; + } + + /** + * 斐波那契数列为: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; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如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 + * 给定一个最大值max, 返回一个数组, 数组中是小于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; + } + + /** + * 用seperator 把数组 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 2/6] =?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 3/6] =?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 f5fad16fcc57dba7348741cc03c95094b06d4d2c Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sat, 18 Mar 2017 20:22:02 +0800 Subject: [PATCH 4/6] =?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 5/6] =?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 54afebf63804325359c724489801860025981d5c Mon Sep 17 00:00:00 2001 From: johnChnia Date: Sun, 19 Mar 2017 19:33:08 +0800 Subject: [PATCH 6/6] =?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, 通过反射实例化(创建对象) + 据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字段中。 + + */ + + 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")); + } +}