From 6bf56f4658b8a6532fc98f328349e28bc8dd15e4 Mon Sep 17 00:00:00 2001 From: OnlyLYJ <382266293@qq.com> Date: Sun, 26 Feb 2017 16:44:05 +0800 Subject: [PATCH 1/4] Amend local --- .../src/TestCollection/StackTest.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/group12/382266293/src/TestCollection/StackTest.java b/group12/382266293/src/TestCollection/StackTest.java index 3784a9b972..8d2f85e1bf 100644 --- a/group12/382266293/src/TestCollection/StackTest.java +++ b/group12/382266293/src/TestCollection/StackTest.java @@ -104,13 +104,22 @@ public void testSize() { @Test public void testAdd() { - myStack.push(5); - myStack.push(10); - myStack.push(15); - println(myStack.get(0)); - println(myStack.get(1)); - println(myStack.get(2)); + + int size = getRandomNumber(); + int[] expected = new int[size]; + int actual; + for (int i = 0; i < size; i++) { + actual = getRandomNumber(); + expected[i] = actual; + myStack.add(actual); + } + int expectedInt; + for (int i = 0; i < size; i++) { + expectedInt = expected[size - i - 1]; + actual = myStack.pop(); + assertEquals(expectedInt, actual); + } } @Test From 0a2e978d7547d6c207ded2044fe4deda2617b204 Mon Sep 17 00:00:00 2001 From: OnlyLYJ <382266293@qq.com> Date: Mon, 27 Feb 2017 20:18:28 +0800 Subject: [PATCH 2/4] new Exs --- group12/382266293/.classpath | 16 +-- .../basic}/Collection/AbstractList.java | 0 .../basic}/Collection/Concrete/ArrayList.java | 0 .../Collection/Concrete/BinaryTreeNode.java | 0 .../Collection/Concrete/LinkedList.java | 0 .../basic}/Collection/Concrete/Queue.java | 0 .../basic}/Collection/Concrete/Stack.java | 0 .../basic}/Collection/Iterator.java | 0 .../basic}/Collection/List.java | 0 .../basic}/TestCollection/AllTests.java | 0 .../basic}/TestCollection/ArrayListTest.java | 0 .../TestCollection/BinaryTreeNodeTest.java | 0 .../basic}/TestCollection/LinkedListTest.java | 0 .../basic}/TestCollection/QueueTest.java | 0 .../basic}/TestCollection/StackTest.java | 0 .../{src => coding/basic}/util/Print.java | 0 .../{src => coding/basic}/util/TestUtil.java | 8 ++ group12/382266293/lib/.gitignore | 1 + group12/382266293/src/TestDom4J.java | 59 +++++++++ group12/382266293/src/array/ArrayUtil.java | 115 ++++++++++++++++++ .../382266293/src/array/ArrayUtilTest.java | 83 +++++++++++++ .../382266293/src/litestruts/LoginAction.java | 39 ++++++ group12/382266293/src/litestruts/Struts.java | 34 ++++++ .../382266293/src/litestruts/StrutsTest.java | 43 +++++++ group12/382266293/src/litestruts/View.java | 23 ++++ group12/382266293/src/litestruts/struts.xml | 11 ++ 26 files changed, 425 insertions(+), 7 deletions(-) rename group12/382266293/{src => coding/basic}/Collection/AbstractList.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/ArrayList.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/BinaryTreeNode.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/LinkedList.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/Queue.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Concrete/Stack.java (100%) rename group12/382266293/{src => coding/basic}/Collection/Iterator.java (100%) rename group12/382266293/{src => coding/basic}/Collection/List.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/AllTests.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/ArrayListTest.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/BinaryTreeNodeTest.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/LinkedListTest.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/QueueTest.java (100%) rename group12/382266293/{src => coding/basic}/TestCollection/StackTest.java (100%) rename group12/382266293/{src => coding/basic}/util/Print.java (100%) rename group12/382266293/{src => coding/basic}/util/TestUtil.java (90%) create mode 100644 group12/382266293/lib/.gitignore create mode 100644 group12/382266293/src/TestDom4J.java create mode 100644 group12/382266293/src/array/ArrayUtil.java create mode 100644 group12/382266293/src/array/ArrayUtilTest.java create mode 100644 group12/382266293/src/litestruts/LoginAction.java create mode 100644 group12/382266293/src/litestruts/Struts.java create mode 100644 group12/382266293/src/litestruts/StrutsTest.java create mode 100644 group12/382266293/src/litestruts/View.java create mode 100644 group12/382266293/src/litestruts/struts.xml diff --git a/group12/382266293/.classpath b/group12/382266293/.classpath index 3e0fb272a8..97b7b051d0 100644 --- a/group12/382266293/.classpath +++ b/group12/382266293/.classpath @@ -1,7 +1,9 @@ - - - - - - - + + + + + + + + + diff --git a/group12/382266293/src/Collection/AbstractList.java b/group12/382266293/coding/basic/Collection/AbstractList.java similarity index 100% rename from group12/382266293/src/Collection/AbstractList.java rename to group12/382266293/coding/basic/Collection/AbstractList.java diff --git a/group12/382266293/src/Collection/Concrete/ArrayList.java b/group12/382266293/coding/basic/Collection/Concrete/ArrayList.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/ArrayList.java rename to group12/382266293/coding/basic/Collection/Concrete/ArrayList.java diff --git a/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java b/group12/382266293/coding/basic/Collection/Concrete/BinaryTreeNode.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/BinaryTreeNode.java rename to group12/382266293/coding/basic/Collection/Concrete/BinaryTreeNode.java diff --git a/group12/382266293/src/Collection/Concrete/LinkedList.java b/group12/382266293/coding/basic/Collection/Concrete/LinkedList.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/LinkedList.java rename to group12/382266293/coding/basic/Collection/Concrete/LinkedList.java diff --git a/group12/382266293/src/Collection/Concrete/Queue.java b/group12/382266293/coding/basic/Collection/Concrete/Queue.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/Queue.java rename to group12/382266293/coding/basic/Collection/Concrete/Queue.java diff --git a/group12/382266293/src/Collection/Concrete/Stack.java b/group12/382266293/coding/basic/Collection/Concrete/Stack.java similarity index 100% rename from group12/382266293/src/Collection/Concrete/Stack.java rename to group12/382266293/coding/basic/Collection/Concrete/Stack.java diff --git a/group12/382266293/src/Collection/Iterator.java b/group12/382266293/coding/basic/Collection/Iterator.java similarity index 100% rename from group12/382266293/src/Collection/Iterator.java rename to group12/382266293/coding/basic/Collection/Iterator.java diff --git a/group12/382266293/src/Collection/List.java b/group12/382266293/coding/basic/Collection/List.java similarity index 100% rename from group12/382266293/src/Collection/List.java rename to group12/382266293/coding/basic/Collection/List.java diff --git a/group12/382266293/src/TestCollection/AllTests.java b/group12/382266293/coding/basic/TestCollection/AllTests.java similarity index 100% rename from group12/382266293/src/TestCollection/AllTests.java rename to group12/382266293/coding/basic/TestCollection/AllTests.java diff --git a/group12/382266293/src/TestCollection/ArrayListTest.java b/group12/382266293/coding/basic/TestCollection/ArrayListTest.java similarity index 100% rename from group12/382266293/src/TestCollection/ArrayListTest.java rename to group12/382266293/coding/basic/TestCollection/ArrayListTest.java diff --git a/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java b/group12/382266293/coding/basic/TestCollection/BinaryTreeNodeTest.java similarity index 100% rename from group12/382266293/src/TestCollection/BinaryTreeNodeTest.java rename to group12/382266293/coding/basic/TestCollection/BinaryTreeNodeTest.java diff --git a/group12/382266293/src/TestCollection/LinkedListTest.java b/group12/382266293/coding/basic/TestCollection/LinkedListTest.java similarity index 100% rename from group12/382266293/src/TestCollection/LinkedListTest.java rename to group12/382266293/coding/basic/TestCollection/LinkedListTest.java diff --git a/group12/382266293/src/TestCollection/QueueTest.java b/group12/382266293/coding/basic/TestCollection/QueueTest.java similarity index 100% rename from group12/382266293/src/TestCollection/QueueTest.java rename to group12/382266293/coding/basic/TestCollection/QueueTest.java diff --git a/group12/382266293/src/TestCollection/StackTest.java b/group12/382266293/coding/basic/TestCollection/StackTest.java similarity index 100% rename from group12/382266293/src/TestCollection/StackTest.java rename to group12/382266293/coding/basic/TestCollection/StackTest.java diff --git a/group12/382266293/src/util/Print.java b/group12/382266293/coding/basic/util/Print.java similarity index 100% rename from group12/382266293/src/util/Print.java rename to group12/382266293/coding/basic/util/Print.java diff --git a/group12/382266293/src/util/TestUtil.java b/group12/382266293/coding/basic/util/TestUtil.java similarity index 90% rename from group12/382266293/src/util/TestUtil.java rename to group12/382266293/coding/basic/util/TestUtil.java index 2dfeeade7f..b9f996bc56 100644 --- a/group12/382266293/src/util/TestUtil.java +++ b/group12/382266293/coding/basic/util/TestUtil.java @@ -16,6 +16,14 @@ public class TestUtil extends TestCase { private static final int RANDOM_SIZE = 500; + public static int[] getRandomIntArray(int number) { + int[] arr = new int[number]; + for (int i = 0; i < arr.length; i++) { + arr[i] = getRandomNumber(); + } + return arr; + } + public static int getRandomNumber() { return random.nextInt(RANDOM_SIZE); } diff --git a/group12/382266293/lib/.gitignore b/group12/382266293/lib/.gitignore new file mode 100644 index 0000000000..3d08860a86 --- /dev/null +++ b/group12/382266293/lib/.gitignore @@ -0,0 +1 @@ +/dom4j-1.6.1.zip diff --git a/group12/382266293/src/TestDom4J.java b/group12/382266293/src/TestDom4J.java new file mode 100644 index 0000000000..b5dabfcc0c --- /dev/null +++ b/group12/382266293/src/TestDom4J.java @@ -0,0 +1,59 @@ +import java.net.URL; +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.Node; +import org.dom4j.io.SAXReader; + +import static util.Print.*; + +public class TestDom4J { + + public Document parse(String add) throws DocumentException { + SAXReader reader = new SAXReader(); + Document document = reader.read(add); + return document; + } + + + + public void bar(Document document) { + List list = document.selectNodes( "/struts-config/action-mappings/action" ); + Node node = document.selectSingleNode( "/struts-config/action-mappings/action/forward" ); + + String name = node.valueOf( "@name" ); + println(name); + } + + public void listNodes(Element node){ + System.out.println("当前节点的名称:" + node.getName()); + //首先获取当前节点的所有属性节点 + List list = node.attributes(); + //遍历属性节点 + for(Attribute attribute : list){ + System.out.println("属性"+attribute.getName() +":" + attribute.getValue()); + } + //如果当前节点内容不为空,则输出 + if(!(node.getTextTrim().equals(""))){ + System.out.println( node.getName() + ":" + node.getText()); + } + //同时迭代当前节点下面的所有子节点 + //使用递归 + Iterator iterator = node.elementIterator(); + while(iterator.hasNext()){ + Element e = iterator.next(); + listNodes(e); + } + } + + public static void main(String args[]) throws DocumentException { + + + + } + +} diff --git a/group12/382266293/src/array/ArrayUtil.java b/group12/382266293/src/array/ArrayUtil.java new file mode 100644 index 0000000000..a9a4a66491 --- /dev/null +++ b/group12/382266293/src/array/ArrayUtil.java @@ -0,0 +1,115 @@ +package array; + +import static util.Print.*; + +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 len = origin.length; + int temp; + for (int i = 0; i < len/2; i++) { + temp = origin[i]; + origin[i] = origin[len-1-i]; + origin[len-1-i] = temp; + } + + } + + public static void main(String args[]) { + + ArrayUtil arrayUtil = new ArrayUtil(); + int[] my = new int[] {1,2,3,4,5}; + arrayUtil.reverseArray(my); + println(my); + } + + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和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){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} \ No newline at end of file diff --git a/group12/382266293/src/array/ArrayUtilTest.java b/group12/382266293/src/array/ArrayUtilTest.java new file mode 100644 index 0000000000..4e8df6912c --- /dev/null +++ b/group12/382266293/src/array/ArrayUtilTest.java @@ -0,0 +1,83 @@ +package array; + +import static org.junit.Assert.*; +import static util.TestUtil.*; + +import java.util.Arrays; + +import static util.Print.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class ArrayUtilTest { + + private int[] myArr; + ArrayUtil au = new ArrayUtil(); + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + myArr = null; + } + + + @Test + public void testReverseArray() { + + int size = getRandomNumber(); + int[] expected = getRandomIntArray(size); + int[] myArr = Arrays.copyOf(expected, size); + + au.reverseArray(myArr); + + for (int i = 0; i < size; i++) { + assertEquals(expected[i], myArr[size-1-i]); + } + + } + + + + + @Test + public void testRemoveZero() { + + } + + @Test + public void testMerge() { + + } + + @Test + public void testGrow() { + + } + + @Test + public void testFibonacci() { + + } + + @Test + public void testGetPrimes() { + + } + + @Test + public void testGetPerfectNumbers() { + + } + + @Test + public void testJoin() { + + } + +} diff --git a/group12/382266293/src/litestruts/LoginAction.java b/group12/382266293/src/litestruts/LoginAction.java new file mode 100644 index 0000000000..c37c250014 --- /dev/null +++ b/group12/382266293/src/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package 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; + } +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/Struts.java b/group12/382266293/src/litestruts/Struts.java new file mode 100644 index 0000000000..f26fa8d8b7 --- /dev/null +++ b/group12/382266293/src/litestruts/Struts.java @@ -0,0 +1,34 @@ +package 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; + } + +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/StrutsTest.java b/group12/382266293/src/litestruts/StrutsTest.java new file mode 100644 index 0000000000..e08fab55c9 --- /dev/null +++ b/group12/382266293/src/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/View.java b/group12/382266293/src/litestruts/View.java new file mode 100644 index 0000000000..5a7d948765 --- /dev/null +++ b/group12/382266293/src/litestruts/View.java @@ -0,0 +1,23 @@ +package litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} \ No newline at end of file diff --git a/group12/382266293/src/litestruts/struts.xml b/group12/382266293/src/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group12/382266293/src/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 cbf8ec80b4be4a2874627c503b94534c471c782b Mon Sep 17 00:00:00 2001 From: OnlyLYJ <382266293@qq.com> Date: Tue, 28 Feb 2017 07:21:02 +0800 Subject: [PATCH 3/4] Array Ex & Test --- .../382266293/coding/basic/util/Print.java | 6 ++ .../382266293/coding/basic/util/TestUtil.java | 9 +- group12/382266293/src/array/ArrayUtil.java | 101 +++++++++++++++--- .../382266293/src/array/ArrayUtilTest.java | 82 ++++++++++++-- 4 files changed, 173 insertions(+), 25 deletions(-) diff --git a/group12/382266293/coding/basic/util/Print.java b/group12/382266293/coding/basic/util/Print.java index b2ae48552b..81b5cf539e 100644 --- a/group12/382266293/coding/basic/util/Print.java +++ b/group12/382266293/coding/basic/util/Print.java @@ -1,5 +1,6 @@ package util; +import java.util.Arrays; public class Print { @@ -10,5 +11,10 @@ public static void print(Object o){ public static void println(Object o){ System.out.println(o); } + + public static void printArr(int[] arr) { + println(Arrays.toString(arr)); + } + } diff --git a/group12/382266293/coding/basic/util/TestUtil.java b/group12/382266293/coding/basic/util/TestUtil.java index b9f996bc56..0820e0e684 100644 --- a/group12/382266293/coding/basic/util/TestUtil.java +++ b/group12/382266293/coding/basic/util/TestUtil.java @@ -1,5 +1,6 @@ package util; +import java.util.Arrays; import java.util.Random; import Collection.List; @@ -16,10 +17,12 @@ public class TestUtil extends TestCase { private static final int RANDOM_SIZE = 500; + + public static int[] getRandomIntArray(int number) { int[] arr = new int[number]; for (int i = 0; i < arr.length; i++) { - arr[i] = getRandomNumber(); + arr[i] = getRandomNumber() + 1; } return arr; } @@ -28,6 +31,10 @@ public static int getRandomNumber() { return random.nextInt(RANDOM_SIZE); } + public static int getRandomNumber(int bound) { + return random.nextInt(bound); + } + public static void addIntWithNatureOrder(List myList, int numbers) { for (int acutal = 0; acutal < numbers ; acutal++) { diff --git a/group12/382266293/src/array/ArrayUtil.java b/group12/382266293/src/array/ArrayUtil.java index a9a4a66491..aafa3c410c 100644 --- a/group12/382266293/src/array/ArrayUtil.java +++ b/group12/382266293/src/array/ArrayUtil.java @@ -2,6 +2,10 @@ import static util.Print.*; +import java.util.Arrays; +import java.util.BitSet; + + public class ArrayUtil { /** @@ -13,25 +17,18 @@ public class ArrayUtil { */ public void reverseArray(int[] origin){ - int len = origin.length; int temp; - for (int i = 0; i < len/2; i++) { + int index = origin.length - 1; + int numbersToReverse = origin.length/2; + for (int i = 0; i < numbersToReverse ; i++) { temp = origin[i]; - origin[i] = origin[len-1-i]; - origin[len-1-i] = temp; + origin[i] = origin[index - i]; + origin[index - i] = temp; } } - public static void main(String args[]) { - - ArrayUtil arrayUtil = new ArrayUtil(); - int[] my = new int[] {1,2,3,4,5}; - arrayUtil.reverseArray(my); - println(my); - } - /** * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: @@ -39,10 +36,29 @@ public static void main(String args[]) { * @param oldArray * @return */ - + public int[] removeZero(int[] oldArray){ - return null; + + BitSet check = new BitSet(oldArray.length); + boolean isZero; + for (int i = 0; i < oldArray.length; i++) { + isZero = (oldArray[i] == 0) ? true : false; + check.set(i, isZero); + } + + int newSize = oldArray.length-check.cardinality(); + int[] newArr = new int[newSize]; + + int nextIndex = check.nextClearBit(0); + for(int i = 0 ; i < newSize ; i++) { + newArr[i] = oldArray[nextIndex]; + nextIndex = check.nextClearBit(nextIndex+1); + } + + return newArr; } + + /** * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 @@ -53,8 +69,34 @@ public int[] removeZero(int[] oldArray){ */ public int[] merge(int[] array1, int[] array2){ - return null; + + int len1 = array1.length; + int len2 = array2.length; + int len3 = array1[len1-1] < array2[len2-1] ? array2[len2-1] + 1: array1[len1-1] + 1; + int[] newArr = new int[len3]; + initialArray(newArr, -1); + for (int i = 0; i < len1; i++) { + newArr[array1[i]] = 0; + } + for (int i = 0; i < len2; i++) { + newArr[array2[i]] = 0; + } + int mergedLength = 0; + for (int i = 0; i < len3; i++) { + if (newArr[i] != -1) + newArr[mergedLength++] = i; + } + return Arrays.copyOf(newArr, mergedLength); } + + public static void initialArray(int[] arr, int j) { + for (int i = 0; i < arr.length; i++) { + arr[i] = j; + } + } + + + /** * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size * 注意,老数组的元素在新数组中需要保持 @@ -65,9 +107,19 @@ public int[] merge(int[] array1, int[] array2){ * @return */ public int[] grow(int [] oldArray, int size){ - return null; + + int[] newArr = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArr[i] = oldArray[i]; + } + + return newArr; } + + + + /** * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] @@ -79,6 +131,15 @@ public int[] fibonacci(int max){ return null; } + + public static void main(String args[]) { + + ArrayUtil aUtil = new ArrayUtil(); + + + + } + /** * 返回小于给定最大值max的所有素数数组 * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] @@ -108,7 +169,13 @@ public int[] getPerfectNumbers(int max){ * @return */ public String join(int[] array, String seperator){ - return null; + 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/group12/382266293/src/array/ArrayUtilTest.java b/group12/382266293/src/array/ArrayUtilTest.java index 4e8df6912c..b242e4b73b 100644 --- a/group12/382266293/src/array/ArrayUtilTest.java +++ b/group12/382266293/src/array/ArrayUtilTest.java @@ -2,18 +2,20 @@ import static org.junit.Assert.*; import static util.TestUtil.*; - import java.util.Arrays; - +import java.util.Iterator; +import java.util.TreeSet; import static util.Print.*; import org.junit.After; import org.junit.Before; import org.junit.Test; + + public class ArrayUtilTest { - private int[] myArr; + private int[] actual; ArrayUtil au = new ArrayUtil(); @Before @@ -23,7 +25,7 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { - myArr = null; + actual = null; } @@ -32,12 +34,12 @@ public void testReverseArray() { int size = getRandomNumber(); int[] expected = getRandomIntArray(size); - int[] myArr = Arrays.copyOf(expected, size); + actual = Arrays.copyOf(expected, size); - au.reverseArray(myArr); + au.reverseArray(actual); for (int i = 0; i < size; i++) { - assertEquals(expected[i], myArr[size-1-i]); + assertEquals(expected[i], actual[size-1-i]); } } @@ -47,16 +49,71 @@ public void testReverseArray() { @Test public void testRemoveZero() { + + int size = getRandomNumber(10000); + int[] expected = getRandomIntArray(size); + int zeros = getRandomNumber(size-1); + TreeSet t = new TreeSet(); + while (t.size() != zeros) { + t.add(getRandomNumber(size)); + } + + for (Integer i : t) { + expected[i] = 0; + } + + int expectedSize = size - zeros; + actual = au.removeZero(expected); + assertEquals(expectedSize, actual.length); + + for (int i = 0, j = 0; i < size; i++) { + if (expected[i] != 0) + assertEquals(expected[i], actual[j++]); + } + } @Test public void testMerge() { + int[] arr1 = getRandomIntArray(getRandomNumber()); + int[] arr2 = getRandomIntArray(getRandomNumber()); + Arrays.sort(arr1); + Arrays.sort(arr2); + TreeSet t = new TreeSet(); + for (int i = 0; i < arr1.length; i++) { + t.add(arr1[i]); + } + for (int i = 0; i < arr2.length; i++) { + t.add(arr2[i]); + } + int[] actual = new int[arr1.length + arr2.length]; + actual = au.merge(arr1, arr2); + + assertEquals(t.size(), actual.length); + + Iterator it = t.iterator(); + for(int i = 0; it.hasNext(); i++) { + assertEquals((int)it.next(), actual[i]); + } } @Test public void testGrow() { + int[] expected = getRandomIntArray(getRandomNumber()); + int growSize = getRandomNumber(); + int[] actual = au.grow(expected, growSize); + + assertEquals(expected.length + growSize, actual.length); + + for (int i = 0; i < actual.length; i++) { + if (i < expected.length) { + assertEquals(expected[i], actual[i]); + } else { + assertEquals(0, actual[i]); + } + } } @@ -78,6 +135,17 @@ public void testGetPerfectNumbers() { @Test public void testJoin() { + int[] expected = getRandomIntArray(getRandomNumber()); + String seperator = "-"; + String joinedString = au.join(expected, seperator); + + String[] actual = joinedString.split(seperator); + + assertEquals(expected.length, actual.length); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], Integer.parseInt(actual[i])); + } + } } From eb8351a74c2c2a2a4316b64f549ca1e94f6d7038 Mon Sep 17 00:00:00 2001 From: OnlyLYJ <382266293@qq.com> Date: Tue, 28 Feb 2017 20:15:18 +0800 Subject: [PATCH 4/4] Array Exs --- group12/382266293/src/array/ArrayUtil.java | 97 ++++++++++++++----- .../382266293/src/array/ArrayUtilTest.java | 20 ++-- 2 files changed, 89 insertions(+), 28 deletions(-) diff --git a/group12/382266293/src/array/ArrayUtil.java b/group12/382266293/src/array/ArrayUtil.java index aafa3c410c..f33f64150b 100644 --- a/group12/382266293/src/array/ArrayUtil.java +++ b/group12/382266293/src/array/ArrayUtil.java @@ -1,9 +1,11 @@ package array; import static util.Print.*; - import java.util.Arrays; import java.util.BitSet; +import Collection.Iterator; +import Collection.Concrete.ArrayList; + public class ArrayUtil { @@ -25,7 +27,6 @@ public void reverseArray(int[] origin){ origin[i] = origin[index - i]; origin[index - i] = temp; } - } @@ -59,7 +60,6 @@ public int[] removeZero(int[] oldArray){ } - /** * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 @@ -95,8 +95,6 @@ public static void initialArray(int[] arr, int j) { } } - - /** * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size * 注意,老数组的元素在新数组中需要保持 @@ -112,13 +110,9 @@ public int[] grow(int [] oldArray, int size){ for (int i = 0; i < oldArray.length; i++) { newArr[i] = oldArray[i]; } - return newArr; } - - - - + /** * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 @@ -128,16 +122,17 @@ public int[] grow(int [] oldArray, int size){ * @return */ public int[] fibonacci(int max){ - return null; - } - - - public static void main(String args[]) { - - ArrayUtil aUtil = new ArrayUtil(); - - - + if (max == 1) + return new int[0]; + int[] result = new int[max]; + result[0] = result[1] = 1; + int count = 0; + for (int i = 2, j = 0; j < max ; i++) { + result[i] = result[i-1] + result[i-2]; + j = result[i]; + count++; + } + return Arrays.copyOf(result, ++count); } /** @@ -147,9 +142,39 @@ public static void main(String args[]) { * @return */ public int[] getPrimes(int max){ - return null; + + String temp = ""; + for(int i = 0; i < max; i++) { + if(isPrime(i)) { + temp += i + " "; + } + } + String[] tempArr = temp.split(" "); + int[] result = new int[tempArr.length]; + for (int i = 0; i < result.length; i++) { + result[i] = Integer.parseInt(tempArr[i]); + } + + return result; } + public static boolean isPrime(int num) { + + if (num <= 1) + return false; + + if (num == 2) + return true; + + for(int i = 2; i <= Math.sqrt(num) + 1; i++) { + if (num % i == 0) + return false; + } + + return true; + } + + /** * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 @@ -157,8 +182,36 @@ public int[] getPrimes(int max){ * @return */ public int[] getPerfectNumbers(int max){ - return null; + + int count = 0; + ArrayList myList = new ArrayList(); + for(int i = 1; i < max; i++) { + if(isPerfectNum(i)) { + count++; + myList.add(i); + } + } + int[] result = new int[count]; + Iterator iterator = myList.iterator(); + for (int i = 0; i < count; i++) { + result[i] = iterator.next(); + } + return result; + } + + + public static boolean isPerfectNum(int num) { + + int sum = 0; + for (int i = 1; i <= num/2; i++) { + if (num % i == 0) + sum += i; + } + + return (num == sum) ? true : false; + } + /** * 用seperator 把数组 array给连接起来 diff --git a/group12/382266293/src/array/ArrayUtilTest.java b/group12/382266293/src/array/ArrayUtilTest.java index b242e4b73b..22a3250613 100644 --- a/group12/382266293/src/array/ArrayUtilTest.java +++ b/group12/382266293/src/array/ArrayUtilTest.java @@ -43,9 +43,6 @@ public void testReverseArray() { } } - - - @Test public void testRemoveZero() { @@ -119,17 +116,28 @@ public void testGrow() { @Test public void testFibonacci() { - + int[] expected = new int[] {1, 1, 2, 3, 5, 8, 13}; + int[] acutal = new int[expected.length]; + actual = au.fibonacci(15); + assertArrayEquals(expected,actual); } @Test public void testGetPrimes() { - + + int[] expected = new int[] {2,3,5,7,11,13,17,19}; + int[] acutal = new int[expected.length]; + actual = au.getPrimes(23); + assertArrayEquals(expected,actual); } @Test public void testGetPerfectNumbers() { - + + int[] expected = new int[] {6, 28, 496, 8128}; + int[] acutal = new int[expected.length]; + actual = au.getPerfectNumbers(10000); + assertArrayEquals(expected,actual); } @Test