From f80f2b54ee51d8aedddf40603a615231f50f6e5f Mon Sep 17 00:00:00 2001 From: 592146505 <592146505@qq.com> Date: Tue, 28 Feb 2017 23:05:03 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E4=B8=BAArrayList=E5=AE=9E=E7=8E=B0toArr?= =?UTF-8?q?ay()=E6=96=B9=E6=B3=95=EF=BC=8C=E6=B7=BB=E5=8A=A0DocumentUtil?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/org/wsc/array/ArrayUtil.java | 54 ++++++++++++--- .../src/org/wsc/array/ArrayUtilTest.java | 59 ++++++++-------- .../src/org/wsc/litestruts/LoginAction.java | 39 +++++++++++ .../src/org/wsc/litestruts/Struts.java | 34 +++++++++ .../src/org/wsc/litestruts/StrutsTest.java | 43 ++++++++++++ .../src/org/wsc/litestruts/View.java | 26 +++++++ .../src/org/wsc/litestruts/struts.xml | 11 +++ .../org/wsc/litestruts/util/DocumentUtil.java | 69 +++++++++++++++++++ .../src/org/wsc/list/ArrayList.java | 23 ++++--- .../src/org/wsc/list/LinkedList.java | 12 ++++ .../src/org/wsc/list/List.java | 29 ++++---- 11 files changed, 335 insertions(+), 64 deletions(-) create mode 100644 group20/592146505/array_util/src/org/wsc/litestruts/LoginAction.java create mode 100644 group20/592146505/array_util/src/org/wsc/litestruts/Struts.java create mode 100644 group20/592146505/array_util/src/org/wsc/litestruts/StrutsTest.java create mode 100644 group20/592146505/array_util/src/org/wsc/litestruts/View.java create mode 100644 group20/592146505/array_util/src/org/wsc/litestruts/struts.xml create mode 100644 group20/592146505/array_util/src/org/wsc/litestruts/util/DocumentUtil.java diff --git a/group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java b/group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java index 1cf53a0e6d..fad0a797cd 100644 --- a/group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java +++ b/group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java @@ -1,6 +1,5 @@ package org.wsc.array; -import java.util.Arrays; public class ArrayUtil { /** @@ -10,7 +9,7 @@ public class ArrayUtil { * @param origin * @return */ - public void reverseArray(int[] origin){ + public static void reverseArray(int[] origin){ //折半 for (int i = 0; i < (origin.length >> 1); i++) { int num = origin[i]; @@ -26,7 +25,7 @@ public void reverseArray(int[] origin){ * @param oldArray * @return */ - public int[] removeZero(int[] oldArray){ + public static int[] removeZero(int[] oldArray){ int count = 0;//计数器 /* * 利用冒泡,将0元素向后排 @@ -53,6 +52,30 @@ public int[] removeZero(int[] oldArray){ return newArray; } + /** + * 现在有如下的一个数组: 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[] removeZero2(int[] oldArray){ + int count = 0;//计数器 + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] == 0) + count++;//计数器+1 + } + //创建新数组 + int[] newArray = new int[oldArray.length-count]; + for (int i = 0,j=0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + newArray[j] = oldArray[i]; + j++; + } + } + 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] , 注意: 已经消除了重复 @@ -61,7 +84,7 @@ public int[] removeZero(int[] oldArray){ * @return */ - public int[] merge(int[] array1, int[] array2){ + public static int[] merge(int[] array1, int[] array2){ return null; } /** @@ -73,8 +96,12 @@ public int[] merge(int[] array1, int[] array2){ * @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]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; } /** @@ -84,7 +111,7 @@ public int[] grow(int [] oldArray, int size){ * @param max * @return */ - public int[] fibonacci(int max){ + public static int[] fibonacci(int max){ return null; } @@ -94,7 +121,7 @@ public int[] fibonacci(int max){ * @param max * @return */ - public int[] getPrimes(int max){ + public static int[] getPrimes(int max){ return null; } @@ -104,7 +131,7 @@ public int[] getPrimes(int max){ * @param max * @return */ - public int[] getPerfectNumbers(int max){ + public static int[] getPerfectNumbers(int max){ return null; } @@ -116,7 +143,12 @@ public int[] getPerfectNumbers(int max){ * @param s * @return */ - public String join(int[] array, String seperator){ - return null; + public static String join(int[] array, String seperator){ + String str = ""; + for (int i = 0; i < array.length; i++) { + str += i != array.length -1 ? + array[i] + seperator : array[i]; + } + return str; } } diff --git a/group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java b/group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java index 0c9204eeb5..88220ea43b 100644 --- a/group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java +++ b/group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java @@ -2,40 +2,43 @@ import static org.junit.Assert.*; -import org.junit.After; -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 { - arrayUtil = null; - } - @Test - public void testReverseArray() { - int[] nums = new int[]{7, 9 , 30, 3, 5}; - arrayUtil.reverseArray(nums); - for (int i = 0; i < nums.length; i++) { - System.out.println(nums[i]); - } - fail("Not yet implemented"); - } +// @Test +// public void testReverseArray() { +// int[] nums = new int[]{7, 9 , 30, 3, 5}; +// ArrayUtil.reverseArray(nums); +// for (int i = 0; i < nums.length; i++) { +// System.out.println(nums[i]); +// } +// fail("Not yet implemented"); +// } +// +// @Test +// public void testRemoveZero() { +// int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; +// nums = ArrayUtil.removeZero2(nums); +// for (int i = 0; i < nums.length; i++) { +// System.out.println(nums[i]); +// } +// fail("Not yet implemented"); +// } +// +// @Test +// public void testJoin() { +// int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; +// String str = ArrayUtil.join(nums,"-"); +// System.out.println(str); +// fail("Not yet implemented"); +// } @Test - public void testRemoveZero() { - int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; - nums = arrayUtil.removeZero(nums); - for (int i = 0; i < nums.length; i++) { - System.out.println(nums[i]); - } - fail("Not yet implemented"); + public void testGrow() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5}; + nums = ArrayUtil.grow(nums,3); + assertTrue(nums.length==12); } } diff --git a/group20/592146505/array_util/src/org/wsc/litestruts/LoginAction.java b/group20/592146505/array_util/src/org/wsc/litestruts/LoginAction.java new file mode 100644 index 0000000000..bb23f5eebb --- /dev/null +++ b/group20/592146505/array_util/src/org/wsc/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package org.wsc.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/group20/592146505/array_util/src/org/wsc/litestruts/Struts.java b/group20/592146505/array_util/src/org/wsc/litestruts/Struts.java new file mode 100644 index 0000000000..a856046d1b --- /dev/null +++ b/group20/592146505/array_util/src/org/wsc/litestruts/Struts.java @@ -0,0 +1,34 @@ +package org.wsc.litestruts; + +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + + /* + + 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字段中。 + + */ + + return null; + } + +} diff --git a/group20/592146505/array_util/src/org/wsc/litestruts/StrutsTest.java b/group20/592146505/array_util/src/org/wsc/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8101e95fe3 --- /dev/null +++ b/group20/592146505/array_util/src/org/wsc/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package org.wsc.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/group20/592146505/array_util/src/org/wsc/litestruts/View.java b/group20/592146505/array_util/src/org/wsc/litestruts/View.java new file mode 100644 index 0000000000..f21906d1a5 --- /dev/null +++ b/group20/592146505/array_util/src/org/wsc/litestruts/View.java @@ -0,0 +1,26 @@ +package org.wsc.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/group20/592146505/array_util/src/org/wsc/litestruts/struts.xml b/group20/592146505/array_util/src/org/wsc/litestruts/struts.xml new file mode 100644 index 0000000000..f6aecf208a --- /dev/null +++ b/group20/592146505/array_util/src/org/wsc/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/group20/592146505/array_util/src/org/wsc/litestruts/util/DocumentUtil.java b/group20/592146505/array_util/src/org/wsc/litestruts/util/DocumentUtil.java new file mode 100644 index 0000000000..9125b244cd --- /dev/null +++ b/group20/592146505/array_util/src/org/wsc/litestruts/util/DocumentUtil.java @@ -0,0 +1,69 @@ +package org.wsc.litestruts.util; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +/** + * DOM解析工具类 + * 懒汉单例模式 + * @author Administrator + * @date 2017年2月28日下午9:45:39 + * @version v1.0 + * + */ +public class DocumentUtil { + private static DocumentUtil documentUtil; + + private DocumentUtil() { + super(); + } + + /** + * 获取实例 + * @return + */ + public static DocumentUtil newInstance(){ + if(documentUtil == null) + synchronized (DocumentUtil.class) { + if(documentUtil == null) + documentUtil = new DocumentUtil(); + } + return documentUtil; + } + + + /** + * 解析XML文件获取DOM树 + * @param fileUrl + * XML文件路径 + * @return + * @throws IOException + * @throws SAXException + * @throws ParserConfigurationException + */ + public Document getDocument(String fileUrl) throws ParserConfigurationException, SAXException, IOException{ + return getDocument(new File(fileUrl)); + } + /** + * 解析XML文件获取DOM树 + * @param file + * XML文件实例 + * @return + * @throws ParserConfigurationException + * @throws IOException + * @throws SAXException + */ + public Document getDocument(File xmlFile) throws ParserConfigurationException, SAXException, IOException{ + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//解析器工厂 + DocumentBuilder db = dbf.newDocumentBuilder();//解析器 + return db.parse(xmlFile);//DOM树 + } + +} diff --git a/group20/592146505/data _structure/src/org/wsc/list/ArrayList.java b/group20/592146505/data _structure/src/org/wsc/list/ArrayList.java index d4d2abdc48..28b8db4132 100644 --- a/group20/592146505/data _structure/src/org/wsc/list/ArrayList.java +++ b/group20/592146505/data _structure/src/org/wsc/list/ArrayList.java @@ -4,6 +4,8 @@ import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; +import javafx.stage.StageStyle; + /** * ArrayList类 * @@ -93,17 +95,16 @@ public void remove() { } - // @Override - // public Object[] toArray() { - // // TODO Auto-generated method stub - // return null; - // } - // - // @Override - // public T[] toArray(T[] a) { - // // TODO Auto-generated method stub - // return null; - // } + @SuppressWarnings("unchecked") + @Override + public E[] toArray() { + return (E[]) elementData; + } + + @Override + public T[] toArray(T[] a) { + return null; + } @Override public boolean add(E element) { diff --git a/group20/592146505/data _structure/src/org/wsc/list/LinkedList.java b/group20/592146505/data _structure/src/org/wsc/list/LinkedList.java index 09ad35f200..b909cfeabc 100644 --- a/group20/592146505/data _structure/src/org/wsc/list/LinkedList.java +++ b/group20/592146505/data _structure/src/org/wsc/list/LinkedList.java @@ -301,4 +301,16 @@ private String outOfBoundsMsg(int index) { return "Index: " + index + ", Size: " + this.size; } + @Override + public E[] toArray() { + // TODO Auto-generated method stub + return null; + } + + @Override + public T[] toArray(T[] a) { + // TODO Auto-generated method stub + return null; + } + } diff --git a/group20/592146505/data _structure/src/org/wsc/list/List.java b/group20/592146505/data _structure/src/org/wsc/list/List.java index d7ef5ee96f..2fd90c6395 100644 --- a/group20/592146505/data _structure/src/org/wsc/list/List.java +++ b/group20/592146505/data _structure/src/org/wsc/list/List.java @@ -38,20 +38,21 @@ public interface List { * @return */ Iterator iterator(); - // - // /** - // * 返回集合数组对象 - // * - // * @return - // */ - // Object[] toArray(); - // - // /** - // * 将集合元素复制到新数组中 - // * @param a - // * @return - // */ - // T[] toArray(T[] a); + + /** + * 返回集合数组对象 + * + * @return + */ + Object[] toArray(); + + /** + * 将集合元素复制到新数组中 + * + * @param a + * @return + */ + T[] toArray(T[] a); /** * 在集合末尾追加元素 From c75f041f2af3889557542be45c774053b611cddf Mon Sep 17 00:00:00 2001 From: RalfNick Date: Wed, 1 Mar 2017 14:38:25 +0800 Subject: [PATCH 02/12] Ralf --- .../MyTreeNode.java" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyTreeNode.java" "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyTreeNode.java" index 6ca5cecb15..5386f34431 100644 --- "a/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyTreeNode.java" +++ "b/group20/925290009/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MyTreeNode.java" @@ -1,6 +1,6 @@ package BasicData; -//insert ���������� +//insert 方法有问题 public class MyTreeNode> { private T data; From 02dad56c90781f23d7fa77f0b3ec19423e16e306 Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Sat, 4 Mar 2017 13:04:50 +0800 Subject: [PATCH 03/12] udpate struts.xml --- .../src/com/coderising/litestruts/struts.xml | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/liuxin/src/com/coderising/litestruts/struts.xml b/liuxin/src/com/coderising/litestruts/struts.xml index dd598a3664..4c6eeabbd4 100644 --- a/liuxin/src/com/coderising/litestruts/struts.xml +++ b/liuxin/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 ac6d6e5c18731833598f8575f245724b6a978b25 Mon Sep 17 00:00:00 2001 From: yc <755659358@qq.com> Date: Sat, 4 Mar 2017 22:11:00 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/liuxincourse/ArrayList.java | 0 .../src/liuxincourse/LinkedList.java | 0 .../src/liuxincourse/List.java | 0 .../src/liuxincourse/Queue.java | 0 .../src/liuxincourse/Stack.java | 0 .../test/liuxincourse/ArrayListTest.java | 0 .../test/liuxincourse/LinkedListTest.java | 0 .../test/liuxincourse/QueueTest.java | 0 .../test/liuxincourse/StackTest.java | 0 .../test/liuxincourse/SuiteTest.java | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename group20/755659358/{liuxincourse_datastructure => week01}/src/liuxincourse/ArrayList.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/src/liuxincourse/LinkedList.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/src/liuxincourse/List.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/src/liuxincourse/Queue.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/src/liuxincourse/Stack.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/test/liuxincourse/ArrayListTest.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/test/liuxincourse/LinkedListTest.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/test/liuxincourse/QueueTest.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/test/liuxincourse/StackTest.java (100%) rename group20/755659358/{liuxincourse_datastructure => week01}/test/liuxincourse/SuiteTest.java (100%) diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java b/group20/755659358/week01/src/liuxincourse/ArrayList.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java rename to group20/755659358/week01/src/liuxincourse/ArrayList.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java b/group20/755659358/week01/src/liuxincourse/LinkedList.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java rename to group20/755659358/week01/src/liuxincourse/LinkedList.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java b/group20/755659358/week01/src/liuxincourse/List.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java rename to group20/755659358/week01/src/liuxincourse/List.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java b/group20/755659358/week01/src/liuxincourse/Queue.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java rename to group20/755659358/week01/src/liuxincourse/Queue.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java b/group20/755659358/week01/src/liuxincourse/Stack.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java rename to group20/755659358/week01/src/liuxincourse/Stack.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java b/group20/755659358/week01/test/liuxincourse/ArrayListTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java rename to group20/755659358/week01/test/liuxincourse/ArrayListTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java b/group20/755659358/week01/test/liuxincourse/LinkedListTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java rename to group20/755659358/week01/test/liuxincourse/LinkedListTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java b/group20/755659358/week01/test/liuxincourse/QueueTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java rename to group20/755659358/week01/test/liuxincourse/QueueTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java b/group20/755659358/week01/test/liuxincourse/StackTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java rename to group20/755659358/week01/test/liuxincourse/StackTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java b/group20/755659358/week01/test/liuxincourse/SuiteTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java rename to group20/755659358/week01/test/liuxincourse/SuiteTest.java From 0096fca9d0453acc5a826abe712aeee8b802b1f3 Mon Sep 17 00:00:00 2001 From: yc <755659358@qq.com> Date: Sat, 4 Mar 2017 22:12:27 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/practice/array/ArrayUtil.java | 222 ++++++++++++++++++ .../practice/array/ArrayUtisTest.java | 73 ++++++ .../practice/litestruts/LoginAction.java | 39 +++ .../practice/litestruts/Struts.java | 140 +++++++++++ .../practice/litestruts/StrutsHandler.java | 57 +++++ .../practice/litestruts/StrutsTest.java | 53 +++++ .../coderising/practice/litestruts/View.java | 23 ++ .../coderising/practice/litestruts/struts.xml | 11 + 8 files changed, 618 insertions(+) create mode 100644 group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java create mode 100644 group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java create mode 100644 group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java create mode 100644 group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java create mode 100644 group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java create mode 100644 group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java create mode 100644 group20/755659358/week02/src/com/coderising/practice/litestruts/View.java create mode 100644 group20/755659358/week02/src/com/coderising/practice/litestruts/struts.xml diff --git a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java new file mode 100644 index 0000000000..0a6fe3da60 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java @@ -0,0 +1,222 @@ +package com.coderising.practice.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +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 j = origin.length - 1; + for (int i = 0; i <= j; i++, j--) { + int temp = origin[i]; + origin[i] = origin[j]; + origin[j] = 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) { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < oldArray.length; i++) { + list.add(oldArray[i]); + } + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + Integer integer = (Integer) iterator.next(); + if (integer.equals(0)) { + iterator.remove(); + } + } + + return integerListToArray(list); + } + + /** + * 给定两个已经排序好的整形数组, 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 list = new ArrayList<>(); + for (int i = 0; i < array1.length; i++) { + if (list.contains(Integer.valueOf(array1[i]))) { + continue; + } + list.add(Integer.valueOf(array1[i])); + } + for (int i = 0; i < array2.length; i++) { + if (list.contains(Integer.valueOf(array2[i]))) { + continue; + } + list.add(Integer.valueOf(array2[i])); + } + Collections.sort(list); + return integerListToArray(list); + } + + /** + * 把一个已经存满数据的数组 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 Arrays.copyOf(oldArray, oldArray.length + size); + } + + /** + * 斐波那契数列为: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 < 2) { + return new int[0]; + } + ArrayList list = new ArrayList<>(); + list.add(1); + list.add(1); + int nextFibo = list.get(list.size() - 2) + list.get(list.size() - 1); + while (nextFibo < max) { + list.add(nextFibo); + nextFibo = list.get(list.size() - 2) + list.get(list.size() - 1); + } + + return integerListToArray(list); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max<2) { + return new int[]{}; + } + ArrayList list = new ArrayList<>(); + + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + return integerListToArray(list); + } + + public int[] integerListToArray(List list){ + int len = list.size(); + int[] result = new int[len]; + for (int i = 0; i < len; i++) { + result[i] = list.get(i); + } + return result; + } + + public boolean isPrime(int a) { + boolean flag = true; + for (int i = 2; i <= Math.sqrt(a); i++) { + if (a % i == 0) { + flag = false; + break; + } + } + return flag; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList list=new ArrayList<>(); + for (int i = 2; i < max; i++) { + int [] splits=primeSplit(i); + if (sumArray(splits)==i) { + list.add(i); + } + } + + return integerListToArray(list); + } + + public int sumArray(int[] arr){ + int sum=0; + for (int i = 0; i < arr.length; i++) { + sum+=arr[i]; + } + return sum; + } + + public int[] primeSplit(int x){ + if (x<=1) { + return new int[]{}; + } + if (x==2) { + return new int[]{1,2}; + } + int k=2; + ArrayList list=new ArrayList<>(); + list.add(1); + while (k<=x) { + if (x%k==0) { + list.add(k); + x=x/k; + }else { + k++; + } + } + + + return integerListToArray(list); + } + + + /** + * 用seperator 把数组 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(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if (i!=array.length-1) { + sb.append(seperator); + } + } + return sb.toString(); + } + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java new file mode 100644 index 0000000000..e3e4d6e873 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java @@ -0,0 +1,73 @@ +package com.coderising.practice.array; + +import static org.junit.Assert.*; + +import java.util.ArrayList; + +import org.junit.Before; +import org.junit.Test; + +import com.coderising.practice.array.ArrayUtil; + +public class ArrayUtisTest { + + private ArrayUtil util; + + @Before + public void init(){ + util=new ArrayUtil(); + } + + + @Test + public void testReverse(){ + int [] origin={1,2,3,4,6}; + ArrayList list=new ArrayList<>(); + list.add(1); + list.add(2); + util.reverseArray(origin); + assertArrayEquals(new int[]{6,4,3,2,1}, origin); + } + + @Test + public void testRomoveZero(){ + int [] origin={1,2,3,0,4,0,6}; + assertArrayEquals(new int[]{1,2,3,4,6}, util.removeZero(origin)); + } + + @Test + public void testMerge(){ + int [] a1={3,0,4,6}; + int [] a2={3,6,8,10}; + assertArrayEquals(new int[]{0,3,4,6,8,10}, util.merge(a1,a2)); + } + + @Test + public void testGrow(){ + int [] a1={3,0,4,6}; + assertArrayEquals(new int[]{3,0,4,6,0,0}, util.grow(a1,2)); + } + + @Test + public void testFibo(){ + assertArrayEquals(new int[]{1,1,2,3}, util.fibonacci(4)); + } + + @Test + public void testGetPrime(){ + assertArrayEquals(new int[]{2,3,5,7,11}, util.getPrimes(13)); + } + + @Test + public void testGetPerfectNum(){ + + assertArrayEquals(new int[]{6}, util.getPerfectNumbers(13)); + } + + @Test + public void testJoin(){ + + assertEquals("3-5-8", util.join(new int[]{3,5,8}, "-")); + } + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java new file mode 100644 index 0000000000..71f5c939f2 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.practice.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/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java new file mode 100644 index 0000000000..069faad8bb --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java @@ -0,0 +1,140 @@ +package com.coderising.practice.litestruts; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.SAXException; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 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字段中。 + + */ + + + + View view=new View(); + Map viewMap=new HashMap(); + Map xmlMap=parseXML(actionName); + + try { + + Class clazz= Class.forName(xmlMap.get("className")); + + Object object=clazz.newInstance(); + if (object instanceof LoginAction) { + LoginAction action =(LoginAction) object; + action.setName(parameters.get("name")); + action.setPassword(parameters.get("password")); + } + Method execute = clazz.getMethod("execute"); + String executResult=(String) execute.invoke(object); + Field[] fields=clazz.getDeclaredFields(); + Method[] methods=clazz.getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().contains("get")) { + String resultString=(String) method.invoke(object); + for (Field field : fields) { + field.setAccessible(true); + if (field.get(object).equals(resultString)) { + viewMap.put(field.getName(), resultString); + } + } + } + } + + view.setJsp(xmlMap.get(executResult)); + + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException 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 (NoSuchMethodException 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(); + } + + + view.setParameters(viewMap); + + + return view; + } + + public static Map parseXML(String actionName){ + SAXParserFactory factory=SAXParserFactory.newInstance(); + StrutsHandler hander=new StrutsHandler(actionName); + SAXParser parser; + try { + parser = factory.newSAXParser(); + parser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/coderising/practice/litestruts/struts.xml"),hander); + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return hander.getResult(); + } + + public static void setPara(Map map){ + + + } + + public static String executAction(String className){ + + return null; + } + + + + + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java new file mode 100644 index 0000000000..2b3d3ab88b --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java @@ -0,0 +1,57 @@ +package com.coderising.practice.litestruts; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +public class StrutsHandler extends DefaultHandler { + + private String actionName; + private String key; + private String value; + + private String currentActionName; + + private Map result = new HashMap(); + + public StrutsHandler(String actionName) { + this.actionName = actionName; + } + + public Map getResult() { + return result; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + if ("action".equals(qName)) { + currentActionName = attributes.getValue(attributes.getIndex("name")); + if (currentActionName.equals(actionName)) { + value = attributes.getValue(attributes.getIndex("class")); + result.put("className", value); + } + + } + if ("result".equals(qName) && actionName.equals(currentActionName)) { + key = attributes.getValue("name"); + } + + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if ("result".equals(qName) && actionName.equals(currentActionName)) { + result.put(key, value); + } + } + + public void characters(char[] ch, int start, int length) throws SAXException { + if (actionName.equals(currentActionName)) { + value = new String(ch, start, length); + } + }; +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java new file mode 100644 index 0000000000..cc57e4c6a8 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java @@ -0,0 +1,53 @@ +package com.coderising.practice.litestruts; + +import java.io.File; +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")); + } + + @Test + public void testParse() { + String actionName = "login"; + + System.out.println(Struts.parseXML(actionName)); + } + + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java new file mode 100644 index 0000000000..66643290d4 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.practice.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/group20/755659358/week02/src/com/coderising/practice/litestruts/struts.xml b/group20/755659358/week02/src/com/coderising/practice/litestruts/struts.xml new file mode 100644 index 0000000000..7b2a62ef58 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/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 534e9f57d79ddcff016d5ab14600e21e7d75f1b6 Mon Sep 17 00:00:00 2001 From: yc <755659358@qq.com> Date: Sun, 5 Mar 2017 12:53:52 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/practice/array/ArrayUtil.java | 13 +++++-------- .../coderising/practice/array/ArrayUtisTest.java | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java index 0a6fe3da60..e4985900cb 100644 --- a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java +++ b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java @@ -160,7 +160,7 @@ public boolean isPrime(int a) { public int[] getPerfectNumbers(int max) { ArrayList list=new ArrayList<>(); for (int i = 2; i < max; i++) { - int [] splits=primeSplit(i); + int [] splits=numSplit(i); if (sumArray(splits)==i) { list.add(i); } @@ -177,23 +177,20 @@ public int sumArray(int[] arr){ return sum; } - public int[] primeSplit(int x){ + public int[] numSplit(int x){ if (x<=1) { return new int[]{}; } if (x==2) { return new int[]{1,2}; } - int k=2; + int k=1; ArrayList list=new ArrayList<>(); - list.add(1); - while (k<=x) { + while (k<=x&&(x/k>=2)) { if (x%k==0) { list.add(k); - x=x/k; - }else { - k++; } + k++; } diff --git a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java index e3e4d6e873..2984b2719c 100644 --- a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java +++ b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java @@ -61,7 +61,7 @@ public void testGetPrime(){ @Test public void testGetPerfectNum(){ - assertArrayEquals(new int[]{6}, util.getPerfectNumbers(13)); + assertArrayEquals(new int[]{6,28,496}, util.getPerfectNumbers(1000)); } @Test From a909aad2abd0c6e47b449b618370567491396e7f Mon Sep 17 00:00:00 2001 From: RalfNick Date: Sun, 5 Mar 2017 15:09:04 +0800 Subject: [PATCH 07/12] Ralf --- .../PracticeOfData/ArrayUtil.java" | 213 ++++++++++++++++++ .../PracticeOfData/ArrayUtilTest.java" | 100 ++++++++ .../Struts/LoginAction.java" | 34 +++ .../Struts/ReadXml.java" | 71 ++++++ .../Struts/Struts.java" | 93 ++++++++ .../Struts/StrutsTest.java" | 40 ++++ .../Struts/View.java" | 28 +++ .../\346\226\207\347\253\240.txt" | 1 + 8 files changed, 580 insertions(+) create mode 100644 "group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" create mode 100644 "group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" create mode 100644 "group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" create mode 100644 "group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" create mode 100644 "group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" create mode 100644 "group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" create mode 100644 "group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" create mode 100644 "group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" new file mode 100644 index 0000000000..42bda4b210 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" @@ -0,0 +1,213 @@ +package org.Ralf.ArrayUtil; + +import java.util.ArrayList; +import java.util.Arrays; + +import javax.naming.spi.DirStateFactory.Result; + +public class ArrayUtil { + + public static void reverseArray(int[] origin) { + /** + * ����һ����������a , �Ը������ֵ�����û� ���磺 a = [7, 9 , 30, 3] , �û���Ϊ [3, 30, 9,7] ��� + * a = [7, 9, 30, 3, 4] , �û���Ϊ [4,3, 30 , 9,7] + * + * @param origin + * @return + * + */ + + for (int i = 0; i < origin.length / 2; i++) { + int temp = origin[i]; + origin[i] = origin[origin.length - i - 1]; + origin[origin.length - 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 int[] + */ + public static int[] removeZero(int[] oldArr) { + + if (oldArr == null) { + return null; + } + int[] newArr = new int[oldArr.length]; + int size = 0; + + for (int i = 0; i < oldArr.length; i++) { + if (oldArr[i] != 0) { + newArr[size] = oldArr[i]; + size++; + } + } + return Arrays.copyOf(newArr, size); + + } + + /** + * ���������Ѿ�����õ��������飬 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 static int[] merge(int[] array1, int[] array2) { + + // method + ArrayList arrayList = new ArrayList<>(); + for (int i = 0; i < array1.length; i++) { + if (!arrayList.contains(array1[i])) { + arrayList.add(array1[i]); + } + } + for (int i = 0; i < array2.length; i++) { + if (!arrayList.contains(array2[i])) {// ����list��index�ҵ����������������ж��Ƿ������Ԫ�� + arrayList.add(array2[i]); + } + } + int[] newArr = new int[arrayList.size()]; + // arrayList.toArray(newArr); + for (int i = 0; i < arrayList.size(); i++) { + newArr[i] = arrayList.get(i); + } + Arrays.sort(newArr);// ����ð�����򣬲������򣬿������򷨵�ʵ�� + return newArr; + } + + /** + * ��һ���Ѿ��������ݵ����� oldArray������������չ�� ��չ��������ݴ�СΪoldArray.length + size + * ע�⣬�������Ԫ��������������Ҫ���� ���� oldArray = [2,3,6] , size = 3,�򷵻ص�������Ϊ + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 쳲���������Ϊ��1��1��2��3��5��8��13��21...... ������һ�����ֵ�� ����С�ڸ�ֵ������ ���磬 max = 15 , + * �򷵻ص�����Ӧ��Ϊ [1��1��2��3��5��8��13] max = 1, �򷵻ؿ����� [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + int[] newArray = {}; + if (max == 1) { + return newArray; + } + newArray = new int[2 * max]; + int size = 0; + int a = 1; + int b = 1; + newArray[size++] = a; + newArray[size++] = b; + while (b <= max) { + int temp = b; + b = a + b; + newArray[size++] = b; + a = temp; + } + + return Arrays.copyOf(newArray, size - 1); + } + + /** + * ����С�ڸ������ֵmax�������������� ����max = 23, ���ص�����Ϊ[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + int[] aar = new int[max]; + int size = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + aar[size++] = i; + } + } + return Arrays.copyOf(aar, size); + } + + private static boolean isPrime(int aar) { + boolean flag = true; + for (int i = 2; i <= Math.sqrt(aar); i++) { + if (aar % i == 0) { + flag = false; + break; + } + } + return flag; + } + + /** + * ��ν���������� ��ָ�����ǡ�õ�����������֮�ͣ�����6=1+2+3 ����һ�����ֵmax�� ����һ�����飬 ��������С��max ���������� + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max < 1) { + return null; + } + int[] arr = new int[max]; + int size = 0; + for (int i = 1; i <= max; i++) { + if (isPerfectNumber(i)) { + arr[size++] = i; + } + } + return Arrays.copyOf(arr, size); + } + + private static boolean isPerfectNumber(int num) { + int sum = 0; + for (int i = 1; i < num; i++) { + if (num % i == 0) { + sum += i; + } + + } + if (sum == num) { + return true; + } else + return false; + } + + /** + * ��seperator ������ array���������� ����array= [3,8,9], seperator = "-" �򷵻�ֵΪ"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + if (array.length < 1) { + return null; + } + StringBuilder string = new StringBuilder(); + for (int i = 0; i < array.length - 1; i++) { + string.append(array[i]).append(seperator); + } + string.append(array[array.length - 1]); + return string.toString(); + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" new file mode 100644 index 0000000000..48f12a3337 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" @@ -0,0 +1,100 @@ +package org.Ralf.ArrayUtilTest; + +import static org.junit.Assert.*; + +import org.Ralf.ArrayUtil.ArrayUtil; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ArrayUtilTest { + + + @Before + public void setUp() throws Exception { + } + + @Test + public void reverseArray() { + int[] origin = {9,8,7,6,5,4,3,2,1}; + int[] originCopy = origin; + int[] reverse = {1,2,3,4,5,6,7,8,9}; + ArrayUtil.reverseArray(origin); + Assert.assertArrayEquals(origin, reverse); + ArrayUtil.reverseArray(origin); + Assert.assertArrayEquals(origin, originCopy); + } + + @Test + public void removeZero(){ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newarr = ArrayUtil.removeZero(oldArr); + int[] realArr = {1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(newarr, realArr); + } + + @Test + public void merge(){ + + int[] a1 ={3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] newarr = ArrayUtil.merge(a1, a2); + int[] realArr = {3,4,5,6,7,8}; + Assert.assertArrayEquals(newarr, realArr); + } + + @Test + public void grow(){ + int[] oldArray = {2,3,6}; + int[] realArr = {2,3,6,0,0,0}; + int[] newArray = ArrayUtil.grow(oldArray, 3); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void fibonacci(){ + + int[] realArr = {1,1,2,3,5,8,13}; + int[] newArray = ArrayUtil.fibonacci(15); + Assert.assertArrayEquals(newArray, realArr); + } + @Test + public void getPrimes(){ + int[] realArr = {2,3,5,7,11,13,17,19}; + int[] newArray = ArrayUtil.getPrimes(23); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void getPerfectNumbers(){ + int[] realArr = {6, 28, 496, 8128}; + int[] newArray = ArrayUtil.getPerfectNumbers(10000); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void join(){ + int[] realArr = {6, 28, 496, 8128}; + int[] newArray = ArrayUtil.getPerfectNumbers(10000); + Assert.assertArrayEquals(newArray, realArr); + } + + + + + + + + + + + + + + + + + + +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" new file mode 100644 index 0000000000..40b5de161a --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +public class LoginAction { + + private String name; + private String passWord; + private String message; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getPassWord() { + return passWord; + } + public void setPassWord(String passWord) { + this.passWord = passWord; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + 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"; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" new file mode 100644 index 0000000000..e971e779b6 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" @@ -0,0 +1,71 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class ReadXml { + + private Document document = null; + private HashMap hashMap; + + public ReadXml(String filename) { + try { + document = new SAXReader().read((filename)); + hashMap = new HashMap(); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public String parseXml(String actionName) { + + // List actions = document.selectNodes("//struts/action"); + String className = null; + Element root = document.getRootElement(); + List actions = root.elements("action"); + if (actions.isEmpty()) { + return null; + } + for (Iterator iter = actions.iterator(); iter.hasNext();) { + Element element = (Element) iter.next(); + Attribute attr1 = element.attribute("name"); + if (attr1.getValue().equals(actionName)) { + Attribute attr2 = element.attribute("class"); + className = attr2.getValue(); + //��ȡ��Ԫ�صĵ�����ֵ + for (Iterator iterator = element.elementIterator(); iterator + .hasNext();) { + Element childElement = (Element) iterator.next(); + Attribute childAttribute = childElement.attribute("name"); + hashMap.put(childAttribute.getValue(), + childElement.getText()); + } + } + + } + return className; + } + + public String getJsp(String result) { + if (result == null) { + return null; + } + String string_jsp = null; + if (!hashMap.isEmpty()) { + for (String string : hashMap.keySet()) { + if (result.equals(string)) { + string_jsp = hashMap.get(string); + } + } + } + return string_jsp; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" new file mode 100644 index 0000000000..2892617845 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" @@ -0,0 +1,93 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Struts { + + private static final String NAME = "name"; + private static final String PASSWORD = "password"; + private static String excuteString; + private static Object object = null;// ���ط����ʵ�� + private static Class actionClass = null;// �����ȡ���� + + @SuppressWarnings("unchecked") + public static View runAction(String actionName, + Map parameters) { + // ��ȡ�����ļ�struts.xml + View view = new View(); + ReadXml readXml = new ReadXml("E:\\struts.xml"); + String classNameString = readXml.parseXml(actionName);//��ȡxml + object = initAction(classNameString);//ͨ�������ʼ���� + + excuteMethod(parameters);//ִ��setter��excute���� + + view.setParameterMap(setMapParameter());//��ȡ���е�getter������ִ�к󽫷������ͽ�����浽view�� + String jspResult = readXml.getJsp(excuteString);//��ȡjsp���� + view.setJsp(jspResult); + + return view; + } + + public static Object initAction(String classNameString) { + System.out.println(classNameString); + try { + actionClass = Class.forName(classNameString); + } catch (ClassNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + Object newObject = null; + try { + newObject = actionClass.newInstance(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return newObject; + } + + public static void excuteMethod(Map parameters) { + + try { + Method methodOfName = actionClass + .getMethod("setName", String.class); + methodOfName.invoke(object, parameters.get(NAME)); + // + Method methodOfPassword = actionClass.getMethod("setPassWord", + String.class); + methodOfPassword.invoke(object, parameters.get(PASSWORD)); + + Method excuteMethod = actionClass.getMethod("execute"); + excuteString = (String) excuteMethod.invoke(object); + + } catch (Exception e) { + // TODO: handle exception + } + } + + public static Map setMapParameter() { + + Method[] getterMethods = actionClass.getMethods(); + HashMap hashMap = new HashMap<>(); + + for (int i = 0; i < getterMethods.length; i++) { + String getterName = getterMethods[i].getName(); + if (getterName.startsWith("get")) { + try { + String value = (String) getterMethods[i].invoke(object); + hashMap.put(getterName.substring(3).toLowerCase(), value); + //System.out.println("----" + getterName.substring(2)); + } catch (Exception e) { + // TODO: handle exception + } + + } + } + return hashMap; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" new file mode 100644 index 0000000000..b7f0884f41 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" @@ -0,0 +1,40 @@ +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"); //�����Ԥ��IJ�һ�� + + 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/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" new file mode 100644 index 0000000000..bda8419e5f --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" @@ -0,0 +1,28 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameter; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameter; + } + + public View setParameterMap(Map parameter) { + this.parameter = parameter; + return this; + } + +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" new file mode 100644 index 0000000000..d4f3154c38 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" @@ -0,0 +1 @@ +http://blog.csdn.net/u011371324/article/details/60329949 \ No newline at end of file From 51449ddf4d6b4f7592fb69c3cc1c89bafffc24c2 Mon Sep 17 00:00:00 2001 From: yc <755659358@qq.com> Date: Sun, 5 Mar 2017 15:56:13 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=9A=E5=AE=A2?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "group20/755659358/blogs/blog\345\234\260\345\235\200.txt" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "group20/755659358/blogs/blog\345\234\260\345\235\200.txt" diff --git "a/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" "b/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..82dd8add37 --- /dev/null +++ "b/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +week02:http://www.jianshu.com/p/22d2cbefdaa1 From 8a501a5c4955e1982cfd421b105e754f179c1a19 Mon Sep 17 00:00:00 2001 From: 592146505 <592146505@qq.com> Date: Sun, 5 Mar 2017 19:19:27 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E7=9A=84struts=E5=92=8C=E6=95=B0=E7=BB=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/org/wsc/array/ArrayUtil.java | 154 ------------ .../src/org/wsc/array/ArrayUtilTest.java | 44 ---- .../src/org/wsc/litestruts/Struts.java | 34 --- .../src/org/wsc/array/ArrayUtil.java | 232 ++++++++++++++++++ .../src/org/wsc/array/ArrayUtilTest.java | 98 ++++++++ .../src/org/wsc/litestruts/Action.java | 21 ++ .../src/org/wsc/litestruts/LoginAction.java | 0 .../src/org/wsc/litestruts/Struts.java | 124 ++++++++++ .../src/org/wsc/litestruts/StrutsTest.java | 0 .../src/org/wsc/litestruts/View.java | 0 .../org/wsc/litestruts/util/DocumentUtil.java | 0 .../litestruts => coderising/src}/struts.xml | 4 +- 12 files changed, 477 insertions(+), 234 deletions(-) delete mode 100644 group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java delete mode 100644 group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java delete mode 100644 group20/592146505/array_util/src/org/wsc/litestruts/Struts.java create mode 100644 group20/592146505/coderising/src/org/wsc/array/ArrayUtil.java create mode 100644 group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java create mode 100644 group20/592146505/coderising/src/org/wsc/litestruts/Action.java rename group20/592146505/{array_util => coderising}/src/org/wsc/litestruts/LoginAction.java (100%) create mode 100644 group20/592146505/coderising/src/org/wsc/litestruts/Struts.java rename group20/592146505/{array_util => coderising}/src/org/wsc/litestruts/StrutsTest.java (100%) rename group20/592146505/{array_util => coderising}/src/org/wsc/litestruts/View.java (100%) rename group20/592146505/{array_util => coderising}/src/org/wsc/litestruts/util/DocumentUtil.java (100%) rename group20/592146505/{array_util/src/org/wsc/litestruts => coderising/src}/struts.xml (68%) diff --git a/group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java b/group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java deleted file mode 100644 index fad0a797cd..0000000000 --- a/group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java +++ /dev/null @@ -1,154 +0,0 @@ -package org.wsc.array; - - -public class ArrayUtil { - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public static void reverseArray(int[] origin){ - //折半 - for (int i = 0; i < (origin.length >> 1); i++) { - int num = origin[i]; - origin[i] = origin[origin.length-1-i]; - origin[origin.length-1-i] = num; - } - } - - /** - * 现在有如下的一个数组: 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 count = 0;//计数器 - /* - * 利用冒泡,将0元素向后排 - * {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * {1,3,4,5,0,6,6,0,5,4,7,6,7,0,5,0} - * {1,3,4,5,6,6,0,5,4,7,6,7,0,5,0,0} - * .... - * */ - for (int i = 0; i < oldArray.length - count; i++) { - //索引为i的元素为0,则依次将索引i的元素与i+1的元素对换 - if(oldArray[i] == 0){ - for (int j = i; j < oldArray.length - 1 - count; j++) { - int num = oldArray[j]; - oldArray[j] = oldArray[j+1]; - oldArray[j+1] = num; - } - count++;//计数器+1 - i--;//防止原索引i+1位置的元素为0, - } - } - //创建新数组 - int[] newArray = new int[oldArray.length-count]; - System.arraycopy(oldArray, 0, newArray, 0, newArray.length); - return newArray; - } - - /** - * 现在有如下的一个数组: 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[] removeZero2(int[] oldArray){ - int count = 0;//计数器 - for (int i = 0; i < oldArray.length; i++) { - if(oldArray[i] == 0) - count++;//计数器+1 - } - //创建新数组 - int[] newArray = new int[oldArray.length-count]; - for (int i = 0,j=0; i < oldArray.length; i++) { - if(oldArray[i] != 0){ - newArray[j] = oldArray[i]; - j++; - } - } - 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 static 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 static int[] grow(int [] oldArray, int size){ - int[] newArray = new int[oldArray.length+size]; - for (int i = 0; i < oldArray.length; i++) { - newArray[i] = oldArray[i]; - } - return newArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public static int[] fibonacci(int max){ - return null; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public static int[] getPrimes(int max){ - return null; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public static int[] getPerfectNumbers(int max){ - return null; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public static String join(int[] array, String seperator){ - String str = ""; - for (int i = 0; i < array.length; i++) { - str += i != array.length -1 ? - array[i] + seperator : array[i]; - } - return str; - } -} diff --git a/group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java b/group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java deleted file mode 100644 index 88220ea43b..0000000000 --- a/group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.wsc.array; - -import static org.junit.Assert.*; - -import org.junit.Test; - -public class ArrayUtilTest { - -// @Test -// public void testReverseArray() { -// int[] nums = new int[]{7, 9 , 30, 3, 5}; -// ArrayUtil.reverseArray(nums); -// for (int i = 0; i < nums.length; i++) { -// System.out.println(nums[i]); -// } -// fail("Not yet implemented"); -// } -// -// @Test -// public void testRemoveZero() { -// int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; -// nums = ArrayUtil.removeZero2(nums); -// for (int i = 0; i < nums.length; i++) { -// System.out.println(nums[i]); -// } -// fail("Not yet implemented"); -// } -// -// @Test -// public void testJoin() { -// int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; -// String str = ArrayUtil.join(nums,"-"); -// System.out.println(str); -// fail("Not yet implemented"); -// } - - @Test - public void testGrow() { - int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5}; - nums = ArrayUtil.grow(nums,3); - assertTrue(nums.length==12); - } - -} diff --git a/group20/592146505/array_util/src/org/wsc/litestruts/Struts.java b/group20/592146505/array_util/src/org/wsc/litestruts/Struts.java deleted file mode 100644 index a856046d1b..0000000000 --- a/group20/592146505/array_util/src/org/wsc/litestruts/Struts.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.wsc.litestruts; - -import java.util.Map; - - -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - - /* - - 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字段中。 - - */ - - return null; - } - -} diff --git a/group20/592146505/coderising/src/org/wsc/array/ArrayUtil.java b/group20/592146505/coderising/src/org/wsc/array/ArrayUtil.java new file mode 100644 index 0000000000..7bba1bb77b --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/array/ArrayUtil.java @@ -0,0 +1,232 @@ +package org.wsc.array; + +public class ArrayUtil { + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + // 折半 + for (int i = 0; i < (origin.length >> 1); i++) { + int num = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = num; + } + } + + /** + * 现在有如下的一个数组: 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 count = 0;// 计数器 + /* + * 利用冒泡,将0元素向后排 {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * {1,3,4,5,0,6,6,0,5,4,7,6,7,0,5,0} {1,3,4,5,6,6,0,5,4,7,6,7,0,5,0,0} + * .... + */ + for (int i = 0; i < oldArray.length - count; i++) { + // 索引为i的元素为0,则依次将索引i的元素与i+1的元素对换 + if (oldArray[i] == 0) { + for (int j = i; j < oldArray.length - 1 - count; j++) { + int num = oldArray[j]; + oldArray[j] = oldArray[j + 1]; + oldArray[j + 1] = num; + } + count++;// 计数器+1 + i--;// 防止原索引i+1位置的元素为0, + } + } + // 创建新数组 + int[] newArray = new int[oldArray.length - count]; + System.arraycopy(oldArray, 0, newArray, 0, newArray.length); + return newArray; + } + + /** + * 现在有如下的一个数组: 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[] removeZero2(int[] oldArray) { + int count = 0;// 计数器 + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + count++;// 计数器+1 + } + // 创建新数组 + int[] newArray = new int[oldArray.length - count]; + for (int i = 0, j = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + 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 static int[] merge(int[] array1, int[] array2) { + int[] newArray = new int[array1.length + array2.length]; + int i = 0, j = 0, k = 0; + while (i < array1.length && j < array2.length) { + // <= 都取 array1 + if (array1[i] <= array2[j]) { + // 等于时,将array2下标++ + if (array1[i] == array2[j]) + j++; + newArray[k++] = array1[i++]; + } else + newArray[k++] = array2[j++]; + + } + // 将没有循环完毕的元素插入 + while (i < array1.length) + newArray[k++] = array1[i++]; + while (j < array2.length) + newArray[k++] = array2[j++]; + int[] result = newArray; + // 长度缩短则新建数组 + if (k < newArray.length) { + result = new int[k]; + for (int l = 0; l < result.length; l++) + result[l] = newArray[l]; + } + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) + return new int[] {}; + int[] nums = new int[max]; + nums[0] = nums[1] = 1; + int flag; + for (flag = 0; (flag < max - 2 && nums[flag] + nums[flag + 1] < max); flag++) { + nums[flag + 2] = nums[flag] + nums[flag + 1]; + } + // 创建新数组 + int[] newArray = nums; + if (newArray.length != flag + 2) { + newArray = new int[flag + 2]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = nums[i]; + } + } + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] array = new int[max>>1]; + int flag = 0; + for (int i = 2; i < max; i++) { + int j; + for (j = 2; j <= (i >> 1); j++) { + if (i % j == 0) + break; + + } + //如果大于,则证明j++有运行,已经完整对比 + if(j > i>>1) + array[flag++] = i; + + } + int[] newArray = array; + if(flag < array.length){ + newArray = new int[flag]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + } + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] array = new int[max]; + int flag = 0; + for (int i = 1; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) + sum+=j; + } + //如果大于,则证明j++有运行,已经完整对比 + if(sum == i) + array[flag++] = i; + + } + int[] newArray = array; + if(flag < array.length){ + newArray = new int[flag]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + } + return newArray; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + String str = ""; + for (int i = 0; i < array.length; i++) + str += i != array.length - 1 ? array[i] + seperator : array[i]; + return str; + } +} diff --git a/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java b/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java new file mode 100644 index 0000000000..009dec2938 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java @@ -0,0 +1,98 @@ +package org.wsc.array; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] nums = new int[]{7, 9 , 30, 3, 5}; + ArrayUtil.reverseArray(nums); + assertArrayEquals(nums, new int[]{5, 3 ,30, 9, 7}); + } + + /** + * 删除0 + */ + @Test + public void testRemoveZero() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + nums = ArrayUtil.removeZero(nums); + assertArrayEquals(nums, new int[]{7, 9 ,30, 3, 5}); + } + + /** + * 删除0 + */ + @Test + public void testRemoveZero2() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + nums = ArrayUtil.removeZero2(nums); + assertArrayEquals(nums, new int[]{7, 9 ,30, 3, 5}); + } + + /** + * 拼接 + */ + @Test + public void testJoin() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + String str = ArrayUtil.join(nums,"-"); + assertEquals(str, "0-7-9-0-0-30-0-3-5-0"); + } + + /** + * 扩容 + */ + @Test + public void testGrow() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5}; + nums = ArrayUtil.grow(nums,3); + assertTrue(nums.length==12); + } + + /** + * 合并 + */ + @Test + public void testMerge() { + int[] nums = new int[]{3, 5, 7,8}; + int[] nums2 = new int[]{4, 5, 6,7,8}; + nums = ArrayUtil.merge(nums,nums2); + assertTrue(nums.length==6); + assertArrayEquals(nums, new int[]{3, 4 ,5, 6, 7,8}); + } + + /** + *斐波那契数列 + */ + @Test + public void testFibonacci() { + int[] nums = ArrayUtil.fibonacci(15); + assertTrue(nums.length==7); + assertArrayEquals(nums, new int[]{1,1,2,3,5,8,13}); + } + + /** + * 素数 + */ + @Test + public void testGetPrimes() { + int[] nums = ArrayUtil.getPrimes(23); + assertTrue(nums.length==8); + assertArrayEquals(nums, new int[]{2,3,5,7,11,13,17,19}); + } + + /** + * 完数 + */ + @Test + public void testGetPerfectNumbers() { + int[] nums = ArrayUtil.getPerfectNumbers(10000); + assertTrue(nums.length==4); + assertArrayEquals(nums, new int[]{6,28,496,8128}); + } + +} diff --git a/group20/592146505/coderising/src/org/wsc/litestruts/Action.java b/group20/592146505/coderising/src/org/wsc/litestruts/Action.java new file mode 100644 index 0000000000..083519bac6 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/Action.java @@ -0,0 +1,21 @@ +package org.wsc.litestruts; + +import java.util.Set; + +public class Action { + + /** 标签名 */ + private String tag; + /** 属性 */ + private String name; + + /** 类名 */ + private String className; + + /** 子标签 */ + private Set statusElement; + + /** 文本内容 */ + private String textContent; + +} diff --git a/group20/592146505/array_util/src/org/wsc/litestruts/LoginAction.java b/group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java similarity index 100% rename from group20/592146505/array_util/src/org/wsc/litestruts/LoginAction.java rename to group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java diff --git a/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java b/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java new file mode 100644 index 0000000000..30a418979f --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java @@ -0,0 +1,124 @@ +package org.wsc.litestruts; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.wsc.litestruts.util.DocumentUtil; +import org.xml.sax.SAXException; + +public class Struts { + private static final DocumentUtil DOCUMENT_UTIL; + private static Document document; + static{ + /* 0. 读取配置文件struts.xml */ + DOCUMENT_UTIL = DocumentUtil.newInstance(); + try { + document = DOCUMENT_UTIL.getDocument("src/struts.xml"); + } catch (ParserConfigurationException | SAXException | IOException e) { + e.printStackTrace(); + } + } + + /** + * + * @param actionName + * @param parameters + * @return + */ + public static View runAction(String actionName, Map parameters) { + String className = null; + String jsp = null; + Map results = new HashMap(); + Node struts = document.getDocumentElement();// 获取根节点 + NodeList actions = struts.getChildNodes();// 获取子节点 + for (int i = 0; i < actions.getLength(); i++) { + // 过滤空节点 + if (actions.item(i).getNodeType() != Node.ELEMENT_NODE) + continue; + Element action = (Element) actions.item(i); + if (!action.getAttribute("name").equals(actionName)) + continue; + className = action.getAttribute("class"); + /* + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + */ + Class clazz = null; + Object instance = null; + try { + clazz = Class.forName(className); + instance = clazz.getConstructor().newInstance(); + Set keySet = parameters.keySet(); + for (String key : keySet) { + Object parameter = parameters.get(key); + Method method = instance.getClass().getMethod( + "set" + (key.substring(0, 1).toUpperCase() + key.substring(1)), parameter.getClass()); + method.invoke(instance, parameter); + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + + /* 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" */ + String rt = null; + try { + Method method = clazz.getMethod("execute"); + rt = (String) method.invoke(instance); + } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /* + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap + * , 例如 {"message": "登录成功"} , 放到View对象的parameters + */ + Field[] fields = instance.getClass().getDeclaredFields(); + Method[] methods = instance.getClass().getMethods(); + for (Field field : fields) { + String fieldName = field.getName(); + for (Method method : methods) { + if (method.getName() + .equals(("get" + (fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1))))) { + try { + results.put(fieldName, (String) method.invoke(instance)); + break; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + NodeList resultNodes = action.getChildNodes(); + for (int j = 0; j < resultNodes.getLength(); j++) { + if (resultNodes.item(j).getNodeType() != Node.ELEMENT_NODE) + continue; + Element result = (Element) resultNodes.item(j); + if (!result.getAttribute("name").equals(rt)) + continue; + jsp = result.getTextContent(); + } + + } + /* + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + View view = new View(); + view.setJsp(jsp); + view.setParameters(results); + return view; + } + +} diff --git a/group20/592146505/array_util/src/org/wsc/litestruts/StrutsTest.java b/group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java similarity index 100% rename from group20/592146505/array_util/src/org/wsc/litestruts/StrutsTest.java rename to group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java diff --git a/group20/592146505/array_util/src/org/wsc/litestruts/View.java b/group20/592146505/coderising/src/org/wsc/litestruts/View.java similarity index 100% rename from group20/592146505/array_util/src/org/wsc/litestruts/View.java rename to group20/592146505/coderising/src/org/wsc/litestruts/View.java diff --git a/group20/592146505/array_util/src/org/wsc/litestruts/util/DocumentUtil.java b/group20/592146505/coderising/src/org/wsc/litestruts/util/DocumentUtil.java similarity index 100% rename from group20/592146505/array_util/src/org/wsc/litestruts/util/DocumentUtil.java rename to group20/592146505/coderising/src/org/wsc/litestruts/util/DocumentUtil.java diff --git a/group20/592146505/array_util/src/org/wsc/litestruts/struts.xml b/group20/592146505/coderising/src/struts.xml similarity index 68% rename from group20/592146505/array_util/src/org/wsc/litestruts/struts.xml rename to group20/592146505/coderising/src/struts.xml index f6aecf208a..b709ec6636 100644 --- a/group20/592146505/array_util/src/org/wsc/litestruts/struts.xml +++ b/group20/592146505/coderising/src/struts.xml @@ -1,10 +1,10 @@ - + /jsp/homepage.jsp /jsp/showLogin.jsp - + /jsp/welcome.jsp /jsp/error.jsp From 753893ce71ffc91462df40000d7347ad7b743072 Mon Sep 17 00:00:00 2001 From: dingxiaoling Date: Sun, 5 Mar 2017 19:54:39 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 3月5日之前的代码,copy的组长的 --- group05/351592484/.gitignore | 9 + .../coding2017/basic/ArrayList.java | 132 ++++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 37 ++++ .../coding2017/basic/Iterator.java | 7 + .../coding2017/basic/LinkedList.java | 168 ++++++++++++++++++ .../zhanglifeng/coding2017/basic/List.java | 9 + .../zhanglifeng/coding2017/basic/Queue.java | 25 +++ .../zhanglifeng/coding2017/basic/Stack.java | 31 ++++ 8 files changed, 418 insertions(+) create mode 100644 group05/351592484/.gitignore create mode 100644 group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java create mode 100644 group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java create mode 100644 group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java create mode 100644 group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java create mode 100644 group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java create mode 100644 group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java create mode 100644 group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java diff --git a/group05/351592484/.gitignore b/group05/351592484/.gitignore new file mode 100644 index 0000000000..0aca6d5fe8 --- /dev/null +++ b/group05/351592484/.gitignore @@ -0,0 +1,9 @@ +/bin/ +*.class +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* \ No newline at end of file diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..e0fefc0ab9 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java @@ -0,0 +1,132 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.*; + +/** + * 功能:实现ArrayList. + * @author zhanglifeng. + */ +public class ArrayList implements List { + private int size = 0; //当前数组大小 + + private Object[] elementData = new Object[5]; //初始数组 + + /** + * 将对象o添加到ArrayList中. + * @param o:需要添加的对象. + */ + public void add(Object o) { + ensureCapacity(size + 1); //确保数组的容量可以装的下size + 1个元素,如果不够则扩容 + + elementData[size] = o; //将o添加到数组中 + size++; //数组大小增加1 + } + + /** + * 将对象o添加到ArrayList的指定位置. + * @param index: 指定位置. + * @param o: 需要添加的对象. + */ + public void add(int index, Object o) { + rangeCheck(index); //判断指定的位置index是否合法 + + ensureCapacity(size + 1); //确保数组的容量可以装的下size + 1个元素,如果不够则扩容 + + System.arraycopy(elementData, index, elementData, index + 1, size - index); //将index位置到结束位置所有的数组往后移动一个位置 + elementData[index] = o; //将对象o添加到index位置 + size++;//数组大小增加1 + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + + if (index != elementData.length - 1) { + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + } + + size--; + return elementData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private void ensureCapacity(int number) { + if (number > elementData.length) { + elementData = grow(elementData, 1); + } + } + + public Object[] grow(Object[] src, int step) { + Object[] target = new Object[src.length + step]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public void rangeCheck(int index){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + private class ArrayListIterator implements Iterator { + ArrayList arrayList = null; + int current = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + current++; + return current > arrayList.size() ? false : true; + } + + @Override + public Object next() { + return elementData[current]; + } + + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + arrayList.add("s1"); + arrayList.add("s2"); + arrayList.add("s3"); + arrayList.add("s4"); + arrayList.add(3, "s33"); + arrayList.add("s5"); + + System.out.println(arrayList.size()); + + System.out.println(arrayList.get(2)); + + arrayList.remove(3); + + System.out.println(arrayList.size()); + + arrayList.add("s1"); + System.out.println(arrayList.size()); + arrayList.remove(5); + System.out.println(arrayList.size()); + + Iterator it = arrayList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + } + +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..772f30c092 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.github.zhanglifeng.coding2017.basic; + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + this.data = data; + left = null; + right = null; + } + + public Object getData() { + return this.data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return this.left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return this.right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..288204f51f --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.zhanglifeng.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..c93a677bfd --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java @@ -0,0 +1,168 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * 功能:实现LinkedList. + * @author zhanglifeng. + */ +public class LinkedList implements List { + private Node head, tail; + private int size; + + private Node getNodeByIndex(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + Node current = head; + for (int i = 0; i < size && current != null; i++, current = current.next) { + if (i == index) { + return current; + } + } + return null; + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + + if (0 == index) { + addFirst(o); + }else{ + Node node = getNodeByIndex(index - 1); + node.next = new Node(o, node.next); + size ++; + } + } + + public Object get(int index) { + return getNodeByIndex(index).data; + } + + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + + if(0 == index){ + return removeFirst(); + }else if(size - 1 == index){ + return removeLast(); + }else{ + Node node = getNodeByIndex(index); + Node preNode = getNodeByIndex(index - 1); + preNode.next = node.next; + size --; + return node.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node currentHead = head; + Node newNode = new Node(o, currentHead); + head = newNode; + if(currentHead == null){ + tail = newNode; + } + + size++; + } + + public void addLast(Object o) { + Node currentTail = tail; + Node newNode = new Node(o, null); + tail = newNode; + if(currentTail == null){ + head = newNode; + }else { + currentTail.next = newNode; + } + size++; + } + + public Object removeFirst() { + if(head == null){ + throw new NoSuchElementException(); + } + Node node = new Node(head.data, null); + head = head.next; + size --; + return node.data; + } + + public Object removeLast() { + if(tail == null){ + throw new NoSuchElementException(); + } + Node node = getNodeByIndex(size - 1); + node.next = null; + size --; + return node.data; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + LinkedList linkedList = null; + private int current = 0; + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + return linkedList.get(current ++); + } + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add("s1"); + linkedList.add("s2"); + linkedList.add("s3"); + linkedList.addFirst("s0"); + linkedList.addLast("s4"); + + Iterator it = linkedList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + System.out.println("第3个元素:" + linkedList.get(3)); + + System.out.println(linkedList.removeFirst()); + System.out.println(linkedList.size()); + System.out.println("Last element:" + linkedList.removeLast()); + System.out.println(linkedList.size()); + System.out.println("第2个元素:" + linkedList.remove(2)); + System.out.println(linkedList.size()); + } +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java new file mode 100644 index 0000000000..e6f5743399 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.zhanglifeng.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/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java new file mode 100644 index 0000000000..42ef512321 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java @@ -0,0 +1,25 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.EmptyStackException; + +public class Queue { + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java new file mode 100644 index 0000000000..0761bdd9e7 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0); + } + + public Object pop(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} From 73dd728b62234e28f928a43652c1e61ca45dc449 Mon Sep 17 00:00:00 2001 From: Korben_CHY Date: Mon, 6 Mar 2017 00:31:23 +0800 Subject: [PATCH 11/12] submit Korben's blog of week2wq --- group20/1107837739/korben.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/group20/1107837739/korben.md b/group20/1107837739/korben.md index 983f6f47b6..91ee50808b 100644 --- a/group20/1107837739/korben.md +++ b/group20/1107837739/korben.md @@ -5,3 +5,5 @@ | Blog Title | Date| | ---------- | -----------| | [初窥计算机程序的运行](http://korben-chy.github.io/2017/02/26/%E5%88%9D%E7%AA%A5%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%90%E8%A1%8C/) | 2017/02/26 | +| +[程序在计算机中的运行过程简析](http://korben-chy.github.io/2017/03/06/%E7%A8%8B%E5%BA%8F%E5%9C%A8%E8%AE%A1%E7%AE%97%E6%9C%BA%E4%B8%AD%E7%9A%84%E8%BF%90%E8%A1%8C%E8%BF%87%E7%A8%8B%E7%AE%80%E6%9E%90) | 2017/03/05 | From 110e8dfd3bbfd35506d828b2477ed626b2f9f530 Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Mon, 6 Mar 2017 10:42:56 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E5=8F=91=E5=B8=83=E7=AC=AC3=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liuxin/.classpath | 14 +- .../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 ++ liuxin/src/com/coding/basic/LinkedList.java | 170 +++++++++++++----- 11 files changed, 368 insertions(+), 53 deletions(-) create mode 100644 liuxin/src/com/coderising/download/DownloadThread.java create mode 100644 liuxin/src/com/coderising/download/FileDownloader.java create mode 100644 liuxin/src/com/coderising/download/FileDownloaderTest.java create mode 100644 liuxin/src/com/coderising/download/api/Connection.java create mode 100644 liuxin/src/com/coderising/download/api/ConnectionException.java create mode 100644 liuxin/src/com/coderising/download/api/ConnectionManager.java create mode 100644 liuxin/src/com/coderising/download/api/DownloadListener.java create mode 100644 liuxin/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java diff --git a/liuxin/.classpath b/liuxin/.classpath index 3e0fb272a8..2d7497573f 100644 --- a/liuxin/.classpath +++ b/liuxin/.classpath @@ -1,7 +1,7 @@ - - - - - - - + + + + + + + diff --git a/liuxin/src/com/coderising/download/DownloadThread.java b/liuxin/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..1456314140 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/FileDownloader.java b/liuxin/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..f5d7999eb4 --- /dev/null +++ b/liuxin/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(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int 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/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..8171ee5763 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/api/Connection.java b/liuxin/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..9710e270e1 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/api/ConnectionException.java b/liuxin/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..8dbfe95dda --- /dev/null +++ b/liuxin/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/liuxin/src/com/coderising/download/api/ConnectionManager.java b/liuxin/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..fb44ede457 --- /dev/null +++ b/liuxin/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/liuxin/src/com/coderising/download/api/DownloadListener.java b/liuxin/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..4cd0b3eab1 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/impl/ConnectionImpl.java b/liuxin/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..32f03efdc7 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java b/liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..046f7c49a4 --- /dev/null +++ b/liuxin/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/liuxin/src/com/coding/basic/LinkedList.java b/liuxin/src/com/coding/basic/LinkedList.java index e2c4e5e795..d6f6ffebec 100644 --- a/liuxin/src/com/coding/basic/LinkedList.java +++ b/liuxin/src/com/coding/basic/LinkedList.java @@ -1,46 +1,124 @@ -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; - - } -} +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; + } +}