diff --git a/group11/1059156023/Array/.classpath b/group11/1059156023/Array/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group11/1059156023/Array/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group11/1059156023/Array/.gitignore b/group11/1059156023/Array/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/Array/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/Array/.project b/group11/1059156023/Array/.project new file mode 100644 index 0000000000..1b5c14fe3f --- /dev/null +++ b/group11/1059156023/Array/.project @@ -0,0 +1,17 @@ + + + Array + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java b/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java new file mode 100644 index 0000000000..2b47957623 --- /dev/null +++ b/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java @@ -0,0 +1,158 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.HashSet; + +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){ + 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; + } + System.out.println(Arrays.toString(origin));//输出数组 + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] arr = new int[oldArray.length]; + int count = 0; + for(int i=0;i set1 = new HashSet(Arrays.asList(array1));//以array1建立集合 + HashSet set2 = new HashSet(Arrays.asList(array2)); + set1.addAll(set2);//求并集 + Integer[] arr = set1.toArray(new Integer[set1.size()]);//获取并集后的数组 + Arrays.sort(arr);//数组排序 + for(int i=0;i + + + + + diff --git a/group11/1059156023/dataStructure/.gitignore b/group11/1059156023/dataStructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/dataStructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/dataStructure/.project b/group11/1059156023/dataStructure/.project new file mode 100644 index 0000000000..35bb23a0fc --- /dev/null +++ b/group11/1059156023/dataStructure/.project @@ -0,0 +1,17 @@ + + + dataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java b/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c853dcaea3 --- /dev/null +++ b/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.coding.basic; + +import java.util.List; + +public class ArrayList implements List{ + private int size; + + //设置一个默认容量,当调用默认构造函数实例化数组后,需要扩容时可用 + private static final int DEFAULT_CAPACITY=10; + + //Integer.MAX_VALUE:2147483647,MAX_ARRAY_SIZE:2147483639 + private static final int MAX_ARRAY_SIZE=Integer.MAX_VALUE-8; + + private Object[] elementData; + + //定义一个默认为空的数组,供默认构造函数使用 + private static final Object[] EMPTY_ELEMENTDATA={}; + + //定义默认构造函数,实例化为空数组 + public ArrayList(){ + this.elementData=EMPTY_ELEMENTDATA; + } + + //定义一个有参的构造函数 + public ArrayList(int initialCapacity){ + if(initialCapacity<0) + throw new IllegalArgumentException("Illegal Capacity:"+initialCapacity); + this.elementData = new Object[initialCapacity]; + } + + //定义add(Object o)方法,默认在数组末尾添加 + public boolean add(Object o){ + //要添加一个数,所以用ensureCapacityInternal()判断size+1个的数,数组是否放得下 + ensureCapacityInternal(size+1); + elementData[size++]=o; + return true; + } + + private void ensureCapacityInternal(int minCapacity) { + if(elementData == EMPTY_ELEMENTDATA) + minCapacity = DEFAULT_CAPACITY; + + //如果需要扩容,则调用grow() + if(minCapacity-elementData.length>0) + grow(minCapacity); + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity+(oldCapacity>>1); + + //原始长度是0时,即原来是空数组时 + if(newCapacity-minCapacity<0) + newCapacity = minCapacity; + + //如果新的容量超过了数组最大容量,就调用hugeCapacity()把能给的最大容量给它 + if(newCapacity-MAX_ARRAY_SIZE>0) + newCapacity = hugeCapacity(minCapacity); + + + } + + private static int hugeCapacity(int minCapacity) { + if (minCapacity<0) { + throw new OutOfMemoryError(); //抛出内存溢出异常 + } + //如果minCapacity比MAX_ARRAY_SIZE大,则返回int类型所能表示的最大值,否则返回MAX_ARRAY_SIZE + return (minCapacity>MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; + } + +} diff --git a/group11/1059156023/struts/.classpath b/group11/1059156023/struts/.classpath new file mode 100644 index 0000000000..400cc1471c --- /dev/null +++ b/group11/1059156023/struts/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group11/1059156023/struts/.gitignore b/group11/1059156023/struts/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/struts/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/struts/.project b/group11/1059156023/struts/.project new file mode 100644 index 0000000000..2c00c2049a --- /dev/null +++ b/group11/1059156023/struts/.project @@ -0,0 +1,17 @@ + + + struts + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/struts/src/com/coding/basic/LoginAction.java b/group11/1059156023/struts/src/com/coding/basic/LoginAction.java new file mode 100644 index 0000000000..23683995f2 --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group11/1059156023/struts/src/com/coding/basic/Struts.java b/group11/1059156023/struts/src/com/coding/basic/Struts.java new file mode 100644 index 0000000000..a0d26a207b --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/Struts.java @@ -0,0 +1,59 @@ +package com.coding.basic; + +import java.io.File; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.experimental.theories.Theories; + +import com.sun.corba.se.impl.orbutil.graph.Node; +import com.sun.org.apache.bcel.internal.classfile.Attribute; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException { + //创建SAXReader对象 + SAXReader reader = new SAXReader(); + //读取文件 转换成Document + Document document = reader.read(new File("src/com/coding/basic/struts.xml")); + //获取根节点元素对象 + Element root = document.getRootElement(); + //遍历根节点 + listNodes(root); + + /* + + 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; + } + + public static void listNodes(Element node) { + String name; + List list = node.attributes(); + + } + +} diff --git a/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java b/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java new file mode 100644 index 0000000000..5bac52805d --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class StrutsTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testLoginActionSuccess() throws DocumentException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws DocumentException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} diff --git a/group11/1059156023/struts/src/com/coding/basic/View.java b/group11/1059156023/struts/src/com/coding/basic/View.java new file mode 100644 index 0000000000..9e479018c8 --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/View.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +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/group11/1059156023/struts/src/com/coding/basic/struts.xml b/group11/1059156023/struts/src/com/coding/basic/struts.xml new file mode 100644 index 0000000000..b3b576c15f --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/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/group11/1178243325/DataStructure/build.gradle b/group11/1178243325/DataStructure/build.gradle index 5c3694d8ec..9c6bc859e6 100644 --- a/group11/1178243325/DataStructure/build.gradle +++ b/group11/1178243325/DataStructure/build.gradle @@ -1,10 +1,20 @@ + apply plugin: 'java' apply plugin: 'eclipse' jar { + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }} manifest { - attributes 'Main-Class' : 'com.coding.Main' + attributes 'Main-Class' : 'com.Main' } } +repositories { + mavenCentral() +} + +dependencies { + compile 'junit:junit:4.12' + compile 'dom4j:dom4j:1.6.1' +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java new file mode 100644 index 0000000000..f5e5a36ebd --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/Main.java @@ -0,0 +1,56 @@ +package com; + +import java.util.*; +import com.coderising.litestruts.*; +import com.coderising.array.*; +public class Main { + public static void main(String[] args) { + int[] array = {1, 2, 3, 4, 5}; + System.out.print("reverseArray测试:"); + ArrayUtil.reverseArray(array); + for (int i : array) + System.out.print(i + " "); + System.out.print("\nremoveZero测试:"); + + int[] oldArray = {1, 3, 4, 5, 0, 0, 8 , 0, 9}; + oldArray = ArrayUtil.removeZero(oldArray); + for (int i : oldArray) { + System.out.print(i + " "); + } + + System.out.print("\nmerge测试:"); + int[] a1 = {3, 5,8}; + int[] a2 = {4, 5, 6,7}; + int[] arrays = ArrayUtil.merge(a1, a2); + for (int i : arrays) + System.out.print(i + " "); + + System.out.print("\ngrow测试:"); + + int[] growArray = ArrayUtil.grow(a1, 5); + for (int i : growArray) + System.out.print(i + " "); + + System.out.print("\nfibonacci测试"); + int[] fArray = ArrayUtil.fibonacci(1); + System.out.print(fArray); + System.out.println(); + fArray = ArrayUtil.fibonacci(15); + for (int i : fArray) + System.out.print(i + " "); + System.out.print("\ngetPrimes测试:"); + int[] primesArray = ArrayUtil.getPrimes(23); + for (int i : primesArray) + System.out.print(i + " "); + System.out.print("\ngetPerfectNumbers测试:"); + int[] pArray = ArrayUtil.getPerfectNumbers(100); + for (int i : pArray) + System.out.print(i + " "); + System.out.print("\njoin测试:"); + int[] jArray = new int[]{2, 3, 8}; + System.out.print(ArrayUtil.join(jArray, "-")); + Map map = new HashMap<>(); + Struts.runAction("login", map); + + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f94d5d01c4 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.coderising.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){ + if (origin == null) { + return; + } + + int length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) + temp[i] = origin[i]; + for (int i = length - 1, j = 0; i >= 0 && j < length; i--, j++) + origin[j] = temp[i]; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if (oldArray == null) { + return null; + } + + int zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeroCount++; + } + int[] newArray = new int[oldArray.length-zeroCount]; + for (int i = 0, j = 0; i < oldArray.length && j < newArray.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){ + if (array1 == null && array2 == null) + return null; + int index1 = 0, index2 = 0; + int[] array3 = new int[array1.length + array2.length]; + int index = 0; + while (index1 != array1.length && index2 != array2.length) { + if (array1[index1] < array2[index2]) { + array3[index++] = array1[index1++]; + } else if (array1[index1] > array2[index2]) { + array3[index++] = array2[index2++]; + } else if (array1[index1] == array2[index2]){ + array3[index++] = array1[index1++]; + index2++; + } + } + + if (index1 == array1.length && index2 != array2.length) { + for (int i = index2; i < array2.length; i++) + array3[index++] = array2[i]; + } else if (index2 == array2.length && index1 != array1.length) { + for (int i = index1; i < array1.length; i++) { + array3[index++] = array1[i]; + } + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array3[i]; + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + if (size <= 0) + return null; + 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 null; + if (max == 1) + return null; + int[] array = new int[max]; + int i = 0; + int value = fibonaccis(i+1); + while ( value < max) { + array[i++] = value; + value = fibonaccis(i+1); + } + int[] newArray = new int[i]; + for (int j = 0; j < newArray.length; j++) { + newArray[j] = array[j]; + } + return newArray; + } + + private static int fibonaccis(int n) { + if (n <=0) + return 0; + if (n == 1 || n ==2 ) + return 1; + return fibonaccis(n-1)+fibonaccis(n-2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if (max <= 1) { + return null; + } + int[] array = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + if (i == 2 || i == 3 || i == 5 || i == 7) + array[index++] = i; + if (i%2 !=0 && i%3 != 0 && i%5 != 0 && i%7 != 0) + array[index++] = i; + } + int[] newArray = new int[index]; + 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){ + if (max <= 0) + return null; + int[] array = new int[max]; + int index = 0; + for (int i = 1; i < max; i++) { + if (isPerfectNumber(i)) + array[index++] = i; + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array[i]; + + return newArray; + } + + private static boolean isPerfectNumber(int n) { + int sum = 0; + int i = 1; + while (i < n) { + if (n%i == 0) + sum += i; + i++; + } + if (sum == n) + return true; + 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 == null) + return null; + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length-1) + str.append(array[i]); + else + str.append(array[i] + seperator); + } + return str.toString(); + } + + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp new file mode 100644 index 0000000000..1f45a5f25e Binary files /dev/null and b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp differ diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..b3bd421435 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,62 @@ +package com.coderising.litestruts; + +import java.util.Map; +import java.util.HashMap; +import java.lang.reflect.Method; +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字段中。 + + */ + try { + String targetClassName = XmlUtil.parseXML("struts.xml", actionName); + Class targetClass = Class.forName(targetClassName); + + Method setName = targetClass.getMethod("setName", String.class); + Method setPassword = targetClass.getMethod("setPassword", String.class); + Object object = targetClass.newInstance(); + + setName.invoke(object, parameters.get("name")); + setPassword.invoke(object, parameters.get("password")); + + Method execute = targetClass.getMethod("execute"); + String result = (String)execute.invoke(object); + + Method getMessage = targetClass.getMethod("getMessage"); + String message = (String)getMessage.invoke(object); + + Map params = new HashMap(); + params.put("message", message); + String jspUrl = XmlUtil.getJspUrl("struts.xml", actionName, result); + View view = new View(); + view.setJsp(jspUrl); + view.setParameters(params); + return view; + + } catch (Exception e) { + e.printStackTrace(); + } + + + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java new file mode 100644 index 0000000000..d200452cc8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts; + +import java.io.*; +import java.util.*; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; +public class XmlUtil { + + public static String parseXML(String filePath, String actionName) { + try { + File file = new File(filePath); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { + Element element = (Element)iter.next(); + Attribute nameAttr = element.attribute("name"); + if (nameAttr.getValue().equals(actionName)) { + Attribute classAttr = element.attribute("class"); + return classAttr.getValue(); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.out.println("parse error"); + } + return null; + } + + public static String getJspUrl(String filePath, String actionName, String resultName) { + try { + File file = new File(filePath); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { + Element element = (Element)iter.next(); + Attribute nameAttr = element.attribute("name"); + if (nameAttr.getValue().equals(actionName)) { + for (Iterator ite = element.elementIterator("result"); ite.hasNext();) { + Element ele = (Element)ite.next(); + Attribute resultAttr = ele.attribute("name"); + if (resultAttr.getValue().equals(resultName)) { + return ele.getText(); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java b/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java deleted file mode 100644 index 31d2f2f277..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.coding; - -import com.coding.basic.*; -public class Main { - public static void main(String[] args) { - ArrayList list = new ArrayList(); - - list.add(0, "2xxx"); - list.add(1, "we"); - list.add(2, "sss"); - list.add("xing"); - list.remove(2); - System.out.println(list.get(2)); - Iterator iterator = list.iterator(); - while(iterator.hasNext()) { - System.out.println(iterator.next()); - } - System.out.println(list.size()); - - LinkedList llist = new LinkedList(); - llist.add("hu"); - llist.add("zhao"); - llist.add(2,"xing"); - llist.addFirst("身骑白马"); - llist.addLast("德州小老虎"); - llist.add(5, "sf"); - llist.remove(5); - llist.removeFirst(); - llist.removeLast(); - for (int i = 2; i >=0; i--) - System.out.print(llist.get(i)); - System.out.println(llist.size()); - - Iterator literator = llist.iterator(); - while(literator.hasNext()) { - System.out.println(literator.next()); - } - - Stack stack = new Stack(); - stack.push(1); - stack.push(2); - stack.push(3); - stack.push(4); - System.out.println(stack.peek()); - while(!stack.isEmpty()) - System.out.println(stack.pop()); - - Queue queue = new Queue(); - queue.enQueue(1); - queue.enQueue(2); - queue.enQueue(3); - System.out.println(queue.size()); - while (!queue.isEmpty()) { - System.out.println(queue.deQueue()); - } - - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java index 266eff3d56..1cf38aee30 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -9,24 +9,25 @@ public class BinaryTreeNode { public Object getData() { return data; } + public void setData(Object data) { this.data = data; } + public BinaryTreeNode getLeft() { return left; } + public void setLeft(BinaryTreeNode left) { this.left = left; } + public BinaryTreeNode getRight() { return right; } + public void setRight(BinaryTreeNode right) { this.right = right; } - public BinaryTreeNode insert(Object o){ - return null; - } - } diff --git a/group11/1178243325/DataStructure/src/main/resources/struts.xml b/group11/1178243325/DataStructure/src/main/resources/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1178243325/DataStructure/struts.xml b/group11/1178243325/DataStructure/struts.xml new file mode 100644 index 0000000000..0582b7d4ea --- /dev/null +++ b/group11/1178243325/DataStructure/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/171535320/ArrayUtil.java b/group11/171535320/ArrayUtil.java new file mode 100644 index 0000000000..6b6cb818d0 --- /dev/null +++ b/group11/171535320/ArrayUtil.java @@ -0,0 +1,211 @@ +import sun.security.util.Length; + +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; + +/** + * Created by dengdechao on 2017/2/27. + */ +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){ + if(origin == null) { + return ; + } + int i = 0; + int j = origin.length - 1; + while(i != j) { + int temp = origin[i]; + origin[i] = origin[j]; + origin[j] = temp; + i++; + j--; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if(oldArray == null) { + return null; + } + int i = 0; + int j = oldArray.length - 1; + + while(i != j) { + if(oldArray[i] != 0) { + i++; + continue; + } + if(oldArray[j] == 0) { + j--; + continue; + } + int temp = oldArray[i]; + oldArray[i] = oldArray[j]; + oldArray[j] = temp; + } + int[] array = new int[i]; + for(int n = 0; n < i; ++n) { + array[n] = oldArray[n]; + } + return array; + } + + /** + * 给定两个已经排序好的整形数组, 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[] result = new int[array1.length + array2.length]; + int len1 = 0; + int len2 = 0; + int i = 0; + while(len1 != array1.length || len2 != array2.length) { + if(len1 < array1.length && len2 < array2.length) { + if(array1[len1] < array2[len2]) { + result[i++] = array1[len1++]; + } else if(array1[len1] > array2[len2]) { + result[i++] = array2[len2++]; + } else { + result[i++] = array1[len1]; + len1++; + len2++; + } + } else if(len1 < array1.length){ + result[i++] = array1[len1++]; + } else { + result[i++] = array2[len2++]; + } + + } + int[] last = new int[i]; + System.arraycopy(result,0,last,0,i); + return last; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int len = oldArray.length + size; + int[] result = new int[len]; + for(int i = 0; i < oldArray.length; ++i) { + result[i] = oldArray[i]; + } + return result; + } + + /** + * 斐波那契数列为: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){ + List list = new ArrayList(); + + for(int i = 2; i <= max; ++i) { + int temp = 2; + while(temp < i) { + if(i % temp == 0) { + break; + } + ++temp; + } + if(i == temp) { + list.add(i); + } + } + + int[] result = new int[list.size()]; + + for(int i = 0; i < list.size(); ++i) { + result[i] = list.get(i); + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + List list = new ArrayList(); + + for(int j = 0; j < max; ++j) { + int temp = 0; + for(int i = 0; i < j / 2 + 1; ++i) { + if(j % i == 0) { + temp += i; + } + } + if(temp == j) { + list.add(j); + } + } + + int[] result = new int[list.size()]; + + for(int i = 0; i < list.size(); ++i) { + result[i] = list.get(i); + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder str = new StringBuilder(); + for(int i = 0; i < array.length; ++i) { + str.append(array[i]); + if(i + 1 < array.length) { + str.append(seperator); + } + } + String s = str.toString(); + return s; + } +} diff --git a/group11/171535320/ArrayList.java b/group11/171535320/DataStruct/ArrayList.java similarity index 98% rename from group11/171535320/ArrayList.java rename to group11/171535320/DataStruct/ArrayList.java index e277814cc9..4340deecf4 100644 --- a/group11/171535320/ArrayList.java +++ b/group11/171535320/DataStruct/ArrayList.java @@ -1,3 +1,5 @@ +package DataStruct; + import java.util.Arrays; import java.util.Objects; diff --git a/group11/171535320/BinaryTreeNode.java b/group11/171535320/DataStruct/BinaryTreeNode.java similarity index 97% rename from group11/171535320/BinaryTreeNode.java rename to group11/171535320/DataStruct/BinaryTreeNode.java index 3c8d543355..e0cc417a43 100644 --- a/group11/171535320/BinaryTreeNode.java +++ b/group11/171535320/DataStruct/BinaryTreeNode.java @@ -1,3 +1,5 @@ +package DataStruct; + public class BinaryTreeNode { private Node root = null; diff --git a/group11/171535320/DataStruct/Iterator.java b/group11/171535320/DataStruct/Iterator.java new file mode 100644 index 0000000000..012d28806a --- /dev/null +++ b/group11/171535320/DataStruct/Iterator.java @@ -0,0 +1,12 @@ +package DataStruct; + +/** + * Created by dengdechao on 2017/2/27. + */ + + public interface Iterator { + public boolean hasNext(); + public Object next(); + + } + diff --git a/group11/171535320/LinkedList.java b/group11/171535320/DataStruct/LinkedList.java similarity index 98% rename from group11/171535320/LinkedList.java rename to group11/171535320/DataStruct/LinkedList.java index 43bb74e516..24a99466ca 100644 --- a/group11/171535320/LinkedList.java +++ b/group11/171535320/DataStruct/LinkedList.java @@ -1,4 +1,6 @@ -import java.util.Objects; +package DataStruct; + +import DataStruct.List; public class LinkedList implements List { diff --git a/group11/171535320/List.java b/group11/171535320/DataStruct/List.java similarity index 89% rename from group11/171535320/List.java rename to group11/171535320/DataStruct/List.java index 4f7bcc71a8..125407436c 100644 --- a/group11/171535320/List.java +++ b/group11/171535320/DataStruct/List.java @@ -1,3 +1,5 @@ +package DataStruct; + public interface List { public void add(Object o); public void add(int index, Object o); diff --git a/group11/171535320/Iterator.java b/group11/171535320/Iterator.java deleted file mode 100644 index 96a43dbe0a..0000000000 --- a/group11/171535320/Iterator.java +++ /dev/null @@ -1,5 +0,0 @@ -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group11/252308879/dataStructure/pom.xml b/group11/252308879/dataStructure/pom.xml index 4b756e4f0d..0bf21adba4 100644 --- a/group11/252308879/dataStructure/pom.xml +++ b/group11/252308879/dataStructure/pom.xml @@ -21,5 +21,15 @@ 4.12 test + + junit + junit + RELEASE + + + dom4j + dom4j + 1.6.1 + diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..82d7073a0f --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java @@ -0,0 +1,282 @@ +package org.pan.coding2017.array; + +import java.util.Arrays; + +/** + * Created by QiPan on 2017/2/27. + */ +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) { + + // 如果是null, 或者长度小于等于1, 直接返回 + if (origin == null || origin.length <= 1) { + return; + } + for (int i = 0; i < origin.length / 2; i++) { + int tmp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = tmp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + + if (oldArray == null) { + return oldArray; + } + int[] newArray = null; + int count = 0; //统计被移出的数组的个数 + for (int i = 0; i < oldArray.length; i++) { + int num = oldArray[i]; + if (num == 0) { + count++; + System.arraycopy(oldArray, i + 1, oldArray, i, oldArray.length - i - 1); + i--; + } + } + if (count == 0) { + newArray = oldArray; + } else { + newArray = new int[oldArray.length - count]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length - count); + } + return newArray; + } + + /** + * 不用JavaAPI来做 + * + * @param oldArray + * @return + */ + public static int[] removeZero_2(int[] oldArray) { + + if (oldArray == null) { + return oldArray; + } + int count = 0; + for (int num : oldArray) { + if (num == 0) { + count++; + } + } + int[] newArray = new int[oldArray.length - count]; + for (int i = 0, j = 0; i < oldArray.length; i++, j++) { + int num = oldArray[i]; + if (num == 0) { + j--; + } else { + newArray[j] = num; + } + } + 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) { + //先初始化一个array3,但不是最后返回的数组 + int[] array3 = new int[array1.length + array2.length]; + int i = 0, j = 0; + int array3Size = 0; // 统计实际上合并数组后的大小 + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) {// 如果array1中元素小,则插入到array3中 + array3[array3Size++] = array1[i]; + ++i; + } else if (array1[i] > array2[j]) {//如果array2中元素小,则插入到array3中 + array3[array3Size++] = array2[j]; + ++j; + } else {//否则随便插入一个,但是计数要同时加1 + array3[array3Size++] = array1[i]; + ++i; + ++j; + } + } + + if (i == array1.length) { //如果array1中全部循环完毕了,那么需要去处理array2中剩余的元素 + for (int n = j; n < array2.length; n++) { + array3[array3Size++] = array2[n]; + } + } else if (j == array2.length) {// 如果array2中全部循环完毕,那么需要去处理array1中剩余的元素 + for (int n = i; n < array1.length; n++) { + array3[array3Size++] = array1[n]; + } + } + int[] returnResultArray = new int[array3Size]; + System.arraycopy(array3, 0, returnResultArray, 0, + array3Size); + return returnResultArray; + } + + /** + * 把一个已经存满数据的数组 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) { + if (oldArray == null) { + oldArray = new int[size]; + } + oldArray = Arrays.copyOf(oldArray, oldArray.length + size); + return oldArray; + } + + /** + * 斐波那契数列为: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[0]; + } + int[] arrays = new int[max / 2]; + int firstNum = 1; //第一个数字 + int secondNum = 1; // 第二个数字 + int arraySize = 0; + arrays[arraySize++] = 1; // 初始化第一位 + while (secondNum < max) { + arrays[arraySize++] = secondNum; + int tmpNum = secondNum; // 保存第二个数,得会需要付给第一个数 + secondNum = firstNum + secondNum; // 为前两个数之和 + firstNum = tmpNum; // 第一个数,后移 + } + return Arrays.copyOf(arrays, arraySize); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] returnResultArray = new int[max + 1]; + int arraySize = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + returnResultArray[arraySize++] = i; + } + + } + if (arraySize == returnResultArray.length) { + return returnResultArray; + } + return Arrays.copyOf(returnResultArray, arraySize); + } + + private static boolean isPrime(final int number) { + if (number < 2) { + return false; + } + // 因为不可能将一个数除与所有小于它的数字,只要检查到N的平方根就好了。 + // 但直接开根号还有个精度的问题。这个可能会产生误差。 索性将判断条件写成 i*i<=number + for (int i = 2; i * i <= number; i++) { + if (number % i == 0) {//查看所有小于number平方根的数,能够被整除 + return false; + } + } + // 如果一个都没有,那么就是素数 + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] array = new int[max]; + int arraySize = 0; + for (int n = 0; n < max; n++) { + int fac,// 被除的因子 + sum,// 用来统计因子之和 + num;// 除数的因子,中间变量 + for (sum = 1, num = n, fac = 2; fac < num; fac++) { + + if (n % fac == 0) {// 如果余数为0,那么说明有因子 + sum += fac; // 统计因子和 + num = n / fac; // num=等于除数的最大因子 + if (num == fac) // 如果最大和最小相等跳出循环 + break; + sum += num; // 再统计因子 + } + } + + if (sum == n) { //因子和与整数相等,那么就是一个完美数 + if (n != 1) { + System.out.println(n + "是一个完全数,其因子为:"); + } + for (fac = 1; fac < n; fac++) { + if (n % fac == 0) {// 列出所有的因子 + System.out.print(fac + " "); + } + } + System.out.println(); + array[arraySize++] = n; // 放到数组中 + } + } + return Arrays.copyOf(array, arraySize); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (array == null) { + return null; + } + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + str.append(array[i]); + continue; + } + str.append(array[i]).append(seperator); + } + return str.toString(); + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java similarity index 99% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java index 3d2a685f35..73dd4b7a5f 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import java.util.Arrays; import java.util.NoSuchElementException; diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java similarity index 98% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java index 3725e5c71b..80f7f78751 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java similarity index 80% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java index 94dc84dfdc..3d1849f1a0 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java similarity index 98% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java index e83de27c11..015ac3d59d 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java similarity index 90% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java index 15c9d9d3be..daa8253313 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java similarity index 96% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java index d51695b148..af478b4288 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java similarity index 97% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java index 3233954cf8..918db8f70d 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java similarity index 95% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java index 2769c72485..2a056b8bbf 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by Pan on 2017/2/25. diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java new file mode 100644 index 0000000000..f38cbcb084 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java @@ -0,0 +1,39 @@ +package org.pan.coding2017.parsingXML; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java new file mode 100644 index 0000000000..90c5443c23 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java @@ -0,0 +1,110 @@ +package org.pan.coding2017.parsingXML; + +import org.pan.coding2017.utils.JaxpDomUtil; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +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字段中。 + + */ + try { + Document document = JaxpDomUtil.getDocument(); + NodeList actionNodeList = document.getElementsByTagName("action"); + for (int i = 0; i < actionNodeList.getLength(); i++) { + NamedNodeMap attributes = actionNodeList.item(i).getAttributes(); + String methodName = attributes.getNamedItem("name").getTextContent(); + if (!actionName.equals(methodName)) { + continue; + } + // 获取全类名对象,反射创建对象 + String className = attributes.getNamedItem("class").getTextContent(); + Class actionClass = Class.forName(className); + + // 获取反射的方法名称, 因为是public修饰所以用的是getMethod,获取私有方法需要用 getDeclaredMethod + Method setName = actionClass.getMethod("setName", String.class); + Method setPassword = actionClass.getMethod("setPassword", String.class); + // 创建对象 + Object actionObject = actionClass.newInstance(); + + // 调用反射的setter方法,给参数赋值 + setName.invoke(actionObject, parameters.get("name")); + setPassword.invoke(actionObject, parameters.get("password")); + + // 获取execute方法 + Method execute = actionClass.getMethod("execute"); + // 返回结果 + String result = (String) execute.invoke(actionObject); + + // 获取getMessage方法 + Method getMessage = actionClass.getMethod("getMessage"); + String message = (String) getMessage.invoke(actionObject); + // 创建一个Map 用来放置在 View中 + Map params = new HashMap(); + params.put("message", message); + + // 获取返回的JSP路径,这个需要比较result节点的name属性 + //获取action的子节点 + NodeList resultNodes = actionNodeList.item(i).getChildNodes(); + String viewUrl = ""; + for (int n = 0; n < resultNodes.getLength(); n++) { + Node item = resultNodes.item(n); + NamedNodeMap resultAttributes = item.getAttributes(); + if (resultAttributes == null) { + continue; + } + String name = resultAttributes.getNamedItem("name").getTextContent(); + if (result.equals(name)) { + viewUrl = item.getTextContent(); + break; + } + } + View view = new View(); + view.setJsp(viewUrl); + view.setParameters(params); + return view; + } + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return null; + + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java new file mode 100644 index 0000000000..ccdacc514c --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java @@ -0,0 +1,43 @@ +package org.pan.coding2017.parsingXML; + +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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java new file mode 100644 index 0000000000..3271ab4ed1 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java @@ -0,0 +1,23 @@ +package org.pan.coding2017.parsingXML; + +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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java new file mode 100644 index 0000000000..f1679f6bef --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java @@ -0,0 +1,41 @@ +package org.pan.coding2017.utils; + +import java.io.FileWriter; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; + +public class Dom4JUtil { + + public static Document getDocument(String xmlPath) { + + try { + //创建解析器 + SAXReader saxReader = new SAXReader(); + //得到Documment + Document document = saxReader.read(xmlPath); + return document; + } catch (DocumentException e) { + e.printStackTrace(); + } + + return null; + } + + public static void xmlWrite(Document document,String xmlPath){ + + try { + OutputFormat format = OutputFormat.createPrettyPrint(); + XMLWriter xmlWriter = new XMLWriter(new FileWriter(xmlPath),format); + xmlWriter.write(document); + xmlWriter.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java new file mode 100644 index 0000000000..d8fe537e35 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java @@ -0,0 +1,152 @@ +package org.pan.coding2017.utils; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class JaxpDomUtil { + public static final String XMLPATH = JaxpDomUtil.class.getClassLoader().getResource("struts.xml").getPath(); + + /** + * 通过 解析器 获取到 Document + * @return + */ + public static Document getDocument() { + try { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory + .newDocumentBuilder(); + Document document = documentBuilder.parse(XMLPATH); + return document; + } catch (Exception e) { + e.printStackTrace(); + + } + return null; + } + + /** + * 回写 XML 方法 + * @param document + */ + public static void tranFormMethod(Document document) { + try { + TransformerFactory transformerFactory = TransformerFactory + .newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.transform(new DOMSource(document), new StreamResult( + XMLPATH)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 递归调用 获取所有的元素 并打印元素名称 + * @param node + */ + private static void listElement(Node node) { + // 判断是元素类型时才打印 + if (node.getNodeType() == Node.ELEMENT_NODE) { + System.out.println(node.getNodeName()); + } + + // 获得一层子节点 + NodeList nodelist = node.getChildNodes(); + for (int i = 0; i < nodelist.getLength(); i++) { + // 得到每一个子节点 + Node nodeChild = nodelist.item(i); + + // 递归调用 + listElement(nodeChild); + } + + } + + /** + * 获取所有的 元素名称 + */ + public static void getListElement() { + + Document document = JaxpDomUtil.getDocument(); + listElement(document); + } + + /** + * 删除nan节点 + */ + public static void delSex() { + + Document document = JaxpDomUtil.getDocument(); + + // 获取元素 + Node nodeSex = document.getElementsByTagName("sex").item(0); + + // 得到父节点 + Node parent = nodeSex.getParentNode(); + + // 通过父节点删除 + parent.removeChild(nodeSex); + + // 回写XML + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 修改 sex 标签的 内容为nv + */ + public static void modifySex() { + Document document = JaxpDomUtil.getDocument(); + Node nodeSex = document.getElementsByTagName("sex").item(0); + nodeSex.setTextContent("nv"); + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 为第一个p1 增加 nv + */ + public static void addSex(){ + Document document = JaxpDomUtil.getDocument(); + Node p1Node = document.getElementsByTagName("p1").item(0); + + //通过 Document 创建 Element + Element sexElement = document.createElement("sex"); + sexElement.setTextContent("nv"); + p1Node.appendChild(sexElement); + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 查询xml中第一个name元素的值 + */ + public static void selectSin(){ + Document document = JaxpDomUtil.getDocument(); + Node nameNode = document.getElementsByTagName("name").item(0); + String name = nameNode.getTextContent(); + System.out.println(name); + } + + /** + * 查询所有name元素的值 + */ + public static void selectAll(){ + Document document = JaxpDomUtil.getDocument(); + NodeList nodeList = document.getElementsByTagName("name"); + for (int i = 0; i < nodeList.getLength(); i++) { + System.out.println(nodeList.item(i).getTextContent()); + } + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java new file mode 100644 index 0000000000..8756c8ab95 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java @@ -0,0 +1,89 @@ +package org.pan.coding2017.utils; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +public class JaxpSAXPUtil { + public static void main(String[] args) { + /* + * 1、创建解析器工厂 + * 2、创建解析器 + * 3、执行 parse 方法 + * + * 4、自己创建一个类、继承DefaultHandler + * 5、重写类里面的三个方法 + */ + + try { + SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); + SAXParser saxParser = saxParserFactory.newSAXParser(); + saxParser.parse("src/p1.xml",new MyDeafultHandler2() ); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} + +class MyDeafultHandler1 extends DefaultHandler{ + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + System.out.println("<"+qName+">"); + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + System.out.println(new String(ch,start,length)); + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + System.out.println("<"+qName+"/>"); + } + +} + +//实现获取所有的name元素的值 +class MyDeafultHandler2 extends DefaultHandler{ + + boolean flag = false; + int index = 1; + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + //判断qName 是否为 name 元素 + if("name".equals(qName) && index == 2){ + flag = true; + } + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + if(flag == true){ + System.out.println(new String(ch, start, length)); + } + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + if("name".equals(qName)){ + flag = false; + index++ ; + } + } + + +} diff --git a/group11/252308879/dataStructure/src/main/resources/struts.xml b/group11/252308879/dataStructure/src/main/resources/struts.xml new file mode 100644 index 0000000000..35830922ba --- /dev/null +++ b/group11/252308879/dataStructure/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java similarity index 94% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java index 1b38998253..281a5bf07b 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java @@ -1,4 +1,4 @@ -package org.apn.coding2017; +package org.pan.coding2017; import org.junit.Assert; import org.junit.Test; diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java new file mode 100644 index 0000000000..29ab6bf6dc --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java @@ -0,0 +1,89 @@ +package org.pan.coding2017.array; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by QiPan on 2017/2/27. + */ +public class ArrayUtilTest { + + @Test + public void removeZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println("removeZero 移除0之前: "+ Arrays.toString(oldArr)); + int[] newArrays = ArrayUtil.removeZero(oldArr); + System.out.println("removeZero 移除0之后: "+ Arrays.toString(newArrays)); + } + + @Test + public void removeZero_2() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println("removeZero_2 移除0之前: "+ Arrays.toString(oldArr)); + int[] newArrays = ArrayUtil.removeZero_2(oldArr); + System.out.println("removeZero_2 移除0之后: "+ Arrays.toString(newArrays)); + } + + @Test + public void reverseArray() throws Exception { + int[] array = new int[]{7, 9 , 30, 3}; + int[] array2 = new int[] {7, 9, 30, 3, 4}; + System.out.println("置换前: " + Arrays.toString(array)); + ArrayUtil.reverseArray(array); + System.out.println("置换后: "+ Arrays.toString(array)); + System.out.println("置换前: " + Arrays.toString(array2)); + ArrayUtil.reverseArray(array2); + System.out.println("置换后: "+ Arrays.toString(array2)); + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7,8}, a2 = {4, 5, 6,7}; + //则 a3 为[3,4,5,6,7,8] + int[] merge = ArrayUtil.merge(a1, a2); + System.out.println(Arrays.toString(merge)); + } + + @Test + public void grow() throws Exception { + int[] oldArray = {2,3,6} ; + int size = 3; + System.out.println("grow 之前:"+ Arrays.toString(oldArray)); + int[] newArrays = ArrayUtil.grow(oldArray, size); + System.out.println("grow 之后:"+ Arrays.toString(newArrays)); + + } + + @Test + public void fibonacci() throws Exception { + //max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + int[] fibonacci = ArrayUtil.fibonacci(988); + System.out.println(Arrays.toString(fibonacci)); + } + + @Test + public void getPrimes() throws Exception { + //例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + int[] primes = ArrayUtil.getPrimes(23); + System.out.println(Arrays.toString(primes)); + } + + @Test + public void getPerfectNumbers() throws Exception { + int[] primes = ArrayUtil.getPerfectNumbers(10000); + System.out.println(Arrays.toString(primes)); + } + + @Test + public void join() throws Exception { + int [] array= {3,8,9}; + String seperator = "-"; + String result = ArrayUtil.join(array, seperator); + System.out.println(result); + } + + + + +} \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/ArrayListTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java similarity index 95% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/ArrayListTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java index a52647b7df..f93876522a 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/ArrayListTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/BinaryTreeNodeTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java similarity index 91% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/BinaryTreeNodeTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java index 8b15597ed2..4c3c01cd73 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/BinaryTreeNodeTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/LinkedListTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java similarity index 96% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/LinkedListTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java index f932e49cf0..d10e2a1d48 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/LinkedListTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/QueueTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java similarity index 91% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/QueueTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java index 0d52d8585f..c720e7d95e 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/QueueTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/StackTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java similarity index 91% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/StackTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java index f1798f8329..df85b797d4 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/StackTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/283091182/src/com/coderising/array/ArrayUtil.java b/group11/283091182/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..93c43d9500 --- /dev/null +++ b/group11/283091182/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,303 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if(origin==null||origin.length==0){ + throw new RuntimeException("invalid array argument!"); + } + if(origin.length>1){ + int temp; + for(int i=0;iarray2[pos2]){ + array3[pos3]=array2[pos2]; + pos2++; + pos3++; + }else if(array1[pos1]3){ + result[pos]=2; + pos++; + } + if(max>4){ + result[pos]=3; + pos++; + } + for(int i=4;i0){ + sb.append(seperator); + } + sb.append(i); + } + return sb.toString(); + } + + private void printArray(String msg,int[] array){ + System.out.print(msg); + for(int i=0;i resultMap= new HashMap(); + public static final String CONST_ACTION = "action"; + public static final String CONST_NAME = "name"; + public static final String CONST_CLASS = "class"; + public static final String CONST_RESULT = "result"; + + private String actionName; + private String actionClass; + + public Action(){}; + + public Action(String actionName,String actionClass){ + this.actionName = actionName; + this.actionClass = actionClass; + } + + public void setActionResultJsp(String result,String dispatcherJsp){ + this.resultMap.put(result, dispatcherJsp); + } + + public String getActionResultJsp(String result){ + return this.resultMap.get(result); + } + + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public String getActionClass() { + return actionClass; + } + + public void setActionClass(String actionClass) { + this.actionClass = actionClass; + } + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append(CONST_ACTION).append(":").append(this.actionName).append(","); + sb.append(CONST_CLASS).append(":").append(this.actionClass).append(","); + sb.append("ResultMap").append(":").append(this.resultMap.toString()); + return sb.toString(); + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/LoginAction.java b/group11/283091182/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/Struts.java b/group11/283091182/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..dbe4691938 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,229 @@ +package com.coderising.litestruts; + +import java.beans.BeanInfo; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +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字段中。 + * + */ + + //0. Load Structs.xml and return a map + Map actionMap = loadActionMap(); + + //1.Find and initialize action instance to actionName provided + Action action = actionMap.get(actionName); + Object instance = initializeActionInstance(action.getActionClass(),parameters); + + //2.invoke the execute method and get the result + String result = executeAction(instance); + + //3.Extract info for view + View view = new View(); + view.setParameters(extractInfo(instance)); + + //4.set dispatcher jsp according to execution result + view.setJsp(action.getActionResultJsp(result)); + + return view; + } + + private static Map loadActionMap() { + try { + + InputStream is = Struts.class.getResourceAsStream("struts.xml"); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(is); + Element struct = document.getDocumentElement(); + + return getActions(struct); + + } catch (IOException e) { + throw new RuntimeException("Error Reading Configuration XML",e); + } catch (ParserConfigurationException e) { + throw new RuntimeException("Error Parsing Configuration XML",e); + } catch (SAXException e) { + throw new RuntimeException("Error Parsing Configuration XML",e); + } + } + + /** + * Parse the XML and construct ActionName:Action map + * + * @param Element struct, root of the struct xml + * @return Map + */ + private static Map getActions(Element struct) { + + Map map = new HashMap(); + + NodeList nl = struct.getElementsByTagName(Action.CONST_ACTION); + + for (int i = 0; i < nl.getLength(); i++) + { + Node actionNode = nl.item(i); + // Get action name and corresponding action class from property + String actionClass = getAttribute(actionNode,Action.CONST_CLASS); + String actionName = getAttribute(actionNode,Action.CONST_NAME); + + Action action = new Action(actionName, actionClass); + + // get results under action + NodeList childNodes = actionNode.getChildNodes(); + + for (int j = 0; j < childNodes.getLength(); j++) + { + Node result = childNodes.item(j); + //Only accept if Node Type is element and Node name is "result" + if ((result.getNodeType() == result.ELEMENT_NODE) + && (result.getNodeName() == Action.CONST_RESULT)) + { + String resultName = getAttribute(result,Action.CONST_NAME); + String dispatcherJsp = result.getTextContent(); + action.setActionResultJsp(resultName, dispatcherJsp); + } + } + map.put(action.getActionName(), action); + } + System.out.println(map); + return map; + } + /** + * Get property from given node + * @param node + * @param key + * @return attribute as String + */ + private static String getAttribute(Node node,String key){ + NamedNodeMap map = node.getAttributes(); + Node attriNode = map.getNamedItem(key); + if(attriNode!=null && attriNode.getNodeType()==Node.ATTRIBUTE_NODE){ + return attriNode.getNodeValue(); + } + return null; + } + + /** + * Initialize instance from given class name and parameters map + * @param actionClass + * @param parameters + * @return instance of specified class + */ + private static Object initializeActionInstance(String actionClass,Map parameters){ + try { + Class clazz= Class.forName(actionClass); + //Instantiate by calling constructor + Constructor constructor = clazz.getConstructor(); + + constructor.setAccessible(true); + Object instance = constructor.newInstance(new Object[]{}); + + //Check class propertes with instrospector + BeanInfo beanInfo = Introspector.getBeanInfo(clazz); + PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); + + for(PropertyDescriptor prop:props){ + String propName = prop.getName(); + Method propSetter = prop.getWriteMethod(); + //If there is a setter for the property and also there is a value in parameter map + //then invoke the setter method to set the values + if(propSetter!=null && parameters.containsKey(propName)) + { + propSetter.invoke(instance, parameters.get(propName)); + } + } + + return instance; + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Error initializing instance: ClassName="+actionClass,e); + } + } + + + /** + * Invoke the "execute" method from the action instance + * @param Action instance + * @return execute result as String + */ + private static String executeAction(Object instance){ + Class clazz = instance.getClass(); + try { + //exepct no argument for execute method + Method execute = clazz.getMethod("execute", new Class[0]); + return (String)execute.invoke(instance, new Object[0]); + } catch (Exception e) { + throw new RuntimeException("Error executing action,class name="+clazz.getCanonicalName()); + } + } + + + /** + * Extracting Bean info by calling the getting method in the Action instance + * @param instance + * @return map + */ + private static Map extractInfo(Object instance){ + Map map = new HashMap(); + Class clazz = instance.getClass(); + try{ + Method[] methods = clazz.getMethods(); + for(Method method:methods) + { + String methodName = method.getName(); + if(methodName.startsWith("get")&&method.getParameterTypes().length==0) + { + Object methodReturn = method.invoke(instance, new Object[0]); + //construct the properties name by getter method name,first character toLower case + String propName = methodName.replaceFirst("get", ""); + char[] propNameCharArr = propName.toCharArray(); + propNameCharArr[0]=Character.toLowerCase(propNameCharArr[0]); + + map.put(String.valueOf(propNameCharArr), methodReturn); + } + } + + }catch(Exception e){ + throw new RuntimeException("Error extracting info from Action Insance,class="+clazz.getCanonicalName(),e); + } + return map; + } + +} diff --git a/group11/283091182/src/com/coderising/litestruts/StrutsTest.java b/group11/283091182/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/View.java b/group11/283091182/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/struts.xml b/group11/283091182/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..4c6eeabbd4 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/283091182/src/com/coding/basic/ArrayListTest.java b/group11/283091182/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..7807fa831e --- /dev/null +++ b/group11/283091182/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,142 @@ +/** + * + */ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Administrator + * + */ +public class ArrayListTest { + + private ArrayList al; + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + System.out.println("SetUp"); + al= new ArrayList(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + System.out.println("TearDown"); + al = null; + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(java.lang.Object)}. + */ + @Test + public final void testAddObject() { + al.add("aaa"); + al.add("bbb"); + al.add("ccc"); + assertEquals("aaa",al.get(0)); + assertEquals("bbb",al.get(1)); + assertEquals("ccc",al.get(2)); + assertEquals(3,al.size()); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test + public final void testAddIntObject() { + al.add("aaa"); + al.add(0,"bbb"); + al.add(1,"ccc"); + assertEquals("bbb",al.get(0)); + assertEquals("ccc",al.get(1)); + assertEquals("aaa",al.get(2)); + assertEquals(3,al.size()); + } + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test(expected=IndexOutOfBoundsException.class) + public final void testAddIntObjectWithException1() { + al.add(-1, "aaa"); + } + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test(expected=IndexOutOfBoundsException.class) + public final void testAddIntObjectWithException2() { + al.add("aaa"); + al.add(1,"bbb"); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGet() { + fail("Not yet implemented"); // TODO + } + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGetWithException1() { + fail("Not yet implemented"); // TODO + } + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGetWithException2() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#remove(int)}. + */ + @Test + public final void testRemove() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#size()}. + */ + @Test + public final void testSize() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#iterator()}. + */ + @Test + public final void testIterator() { + fail("Not yet implemented"); // TODO + } + +} diff --git a/group11/395443277/data_structure/.gitignore b/group11/395443277/.gitignore similarity index 100% rename from group11/395443277/data_structure/.gitignore rename to group11/395443277/.gitignore diff --git a/group11/395443277/src/com/coderising/array/ArrayUtil.java b/group11/395443277/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..d73df85b05 --- /dev/null +++ b/group11/395443277/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,254 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int first = 0; + int last = origin.length - 1; + + while (first < last) { + int temp; + temp = origin[first]; + origin[first] = origin[last]; + origin[last] = temp; + + first++; + last--; + } + } + + /** + * 现在有如下的一个数组: 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){ + // count how many zeros + int zeroCount = 0; + int len= oldArray.length; + + if (len==0) { + return new int[]{}; + } + + for (int i=0; i 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字段中。 + * + */ + + // create a new DocumentBuilderFactory + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + try { + // use the factory to create a documentbuilder + DocumentBuilder builder = factory.newDocumentBuilder(); + InputStream istream = Struts.class.getResourceAsStream("struts.xml"); + Document doc = builder.parse(istream); + + // find jsp page based on the result from execute result + Element element = doc.getDocumentElement(); + NodeList actionNodeList = element.getElementsByTagName("action"); + + for (int i = 0; i < actionNodeList.getLength(); i++) { + NamedNodeMap actionNodeAttr = actionNodeList.item(i).getAttributes(); + + if (actionNodeAttr.getNamedItem("name").getTextContent().equals(actionName)) { + String className = actionNodeAttr.getNamedItem("class").getTextContent(); + + Class cls = Class.forName(className); + Object obj = cls.newInstance(); + + // set name and password + Method setName = cls.getDeclaredMethod("setName", String.class); + Method setPassword = cls.getDeclaredMethod("setPassword", String.class); + setName.invoke(obj, parameters.get("name")); + setPassword.invoke(obj, parameters.get("password")); + + // execute + Method execute = cls.getDeclaredMethod("execute"); + String executeResult = (String) execute.invoke(obj); + + // get message and jsp + Method getMessage = cls.getDeclaredMethod("getMessage"); + String msg = (String) getMessage.invoke(obj); + Map params = new HashMap(); + params.put("message",msg); + + // check result nodes + NodeList resultNodes = actionNodeList.item(i).getChildNodes(); + + String jsp = ""; + for (int j=0; j viewCls = Class.forName("com.coderising.litestruts.View"); + View viewObj = (View) viewCls.newInstance(); + Method setParameters = viewCls.getDeclaredMethod("setParameters", Map.class); + setParameters.invoke(viewObj, params); + Method setJsp = viewCls.getDeclaredMethod("setJsp", String.class); + setJsp.invoke(viewObj, jsp); + + return viewObj; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group11/395443277/src/com/coderising/litestruts/StrutsTest.java b/group11/395443277/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group11/395443277/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/395443277/src/com/coderising/litestruts/View.java b/group11/395443277/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group11/395443277/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/395443277/src/com/coderising/litestruts/struts.xml b/group11/395443277/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..7f8d558286 --- /dev/null +++ b/group11/395443277/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/395443277/data_structure/src/com/coding/basic/ArrayList.java b/group11/395443277/src/com/coding/basic/ArrayList.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/ArrayList.java rename to group11/395443277/src/com/coding/basic/ArrayList.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/ArrayListTest.java b/group11/395443277/src/com/coding/basic/ArrayListTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/ArrayListTest.java rename to group11/395443277/src/com/coding/basic/ArrayListTest.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNode.java b/group11/395443277/src/com/coding/basic/BinaryTreeNode.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNode.java rename to group11/395443277/src/com/coding/basic/BinaryTreeNode.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNodeTest.java b/group11/395443277/src/com/coding/basic/BinaryTreeNodeTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNodeTest.java rename to group11/395443277/src/com/coding/basic/BinaryTreeNodeTest.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/Iterator.java b/group11/395443277/src/com/coding/basic/Iterator.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/Iterator.java rename to group11/395443277/src/com/coding/basic/Iterator.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/LinkedList.java b/group11/395443277/src/com/coding/basic/LinkedList.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/LinkedList.java rename to group11/395443277/src/com/coding/basic/LinkedList.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/LinkedListTest.java b/group11/395443277/src/com/coding/basic/LinkedListTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/LinkedListTest.java rename to group11/395443277/src/com/coding/basic/LinkedListTest.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/List.java b/group11/395443277/src/com/coding/basic/List.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/List.java rename to group11/395443277/src/com/coding/basic/List.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/Queue.java b/group11/395443277/src/com/coding/basic/Queue.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/Queue.java rename to group11/395443277/src/com/coding/basic/Queue.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/QueueTest.java b/group11/395443277/src/com/coding/basic/QueueTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/QueueTest.java rename to group11/395443277/src/com/coding/basic/QueueTest.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/Stack.java b/group11/395443277/src/com/coding/basic/Stack.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/Stack.java rename to group11/395443277/src/com/coding/basic/Stack.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/StackTest.java b/group11/395443277/src/com/coding/basic/StackTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/StackTest.java rename to group11/395443277/src/com/coding/basic/StackTest.java diff --git a/group11/542194147/myDataStructure/.classpath b/group11/542194147/myDataStructure/.classpath new file mode 100644 index 0000000000..ddd63c6d08 --- /dev/null +++ b/group11/542194147/myDataStructure/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group11/542194147/myDataStructure/.gitignore b/group11/542194147/myDataStructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/542194147/myDataStructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/542194147/myDataStructure/.project b/group11/542194147/myDataStructure/.project new file mode 100644 index 0000000000..56bfc21f79 --- /dev/null +++ b/group11/542194147/myDataStructure/.project @@ -0,0 +1,17 @@ + + + myDataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java b/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7512dac816 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,208 @@ +package com.coderising.array; + +import java.util.Arrays; + +/** + * Array集合工具类 + * @author 小摩托 + * + */ +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 int[] reverseArray(int[] origin){ + if(origin.length==0){ + throw new RuntimeException("数组为空"); + } + int[]exchange=new int[origin.length]; + for(int i=origin.length-1;i>=0;i--){ + exchange[origin.length-1-i]=origin[i]; + } + return exchange; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int exchange[]={}; + for(int i=0;i 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(); + try { + SAXReader sr=new SAXReader(); + Document document= sr.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root=document.getRootElement(); + List actions=root.elements("action"); + for(Iterator it=actions.iterator();it.hasNext();){ + Element action =it.next(); + if(action.attribute("name").getText().equals(actionName)){ + LoginAction loginAction=(LoginAction)Class.forName( + action.attribute("class").getText()).newInstance(); + loginAction.setName(parameters.get("name")); + loginAction.setPassword(parameters.get("password")); + String loginMsg=loginAction.execute(); + if(loginMsg.equals("success")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("success")){ + createView(view, loginAction, result); + } + } + } + if(loginMsg.equals("fail")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("fail")){ + createView(view, loginAction, result); + } + } + } + if(loginMsg.equals("error")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("error")){ + createView(view, loginAction, result); + } + } + } + } + } + } catch (Exception e) { + System.out.println("have exception:"+e); + e.printStackTrace(); + } + return view; + } + + private static void createView(View view, LoginAction loginAction, Element result) { + Map msgMap=new HashMap(); + msgMap.put("message", loginAction.getMessage()); + view.setParameters(msgMap); + view.setJsp(result.getText()); + } + +} diff --git a/group11/542194147/myDataStructure/src/com/coderising/litestruts/StrutsTest.java b/group11/542194147/myDataStructure/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..7fa9e9a4e5 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/litestruts/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"); //密码和预设的不一致 + + 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/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java b/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..610ce0d092 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java @@ -0,0 +1,24 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/542194147/myDataStructure/src/com/coderising/litestruts/struts.xml b/group11/542194147/myDataStructure/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..fbede89fa1 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,133 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class MyArrayList implements MyList { + + /** + * 数组默认大小 + */ + private static final int DEFAULT_SIZE = 10; + /** + * 储存元素的数组 + */ + private Object[] elementData =null; + /** + * 数组大小指针; + */ + private int capacity; + /** + * 当前游标 + */ + private int current; + + public MyArrayList(int size){ + if(size<0){ + throw new RuntimeException("大小不能小于0"); + }else{ + this.elementData= new Object[size]; + this.current=0; + this.capacity=size; + } + } + + public MyArrayList(){ + this(DEFAULT_SIZE); + } + + @Override + public void add(Object o) { + isOverSize();//判断数组容量是否满足,不满足增加空间 + this.elementData[current]=o; + this.current++; + } + + @Override + public void add(int index, Object o) { + isOverSize();//判断数组容量是否满足,不满足增加空间 + isOutOfBoundIndex(index);//判断数组下标是否越界 + System.arraycopy(elementData, index, elementData, index+1, this.elementData.length-index); + this.current++; + } + + @Override + public Object get(int index) { + isOutOfBoundIndex(index);//判断数组下标是否越界 + return this.elementData[index]; + } + + @Override + public Object remove(int index) { + isOutOfBoundIndex(index);//判断数组下标是否越界 + Object o=this.elementData[index]; + if(this.elementData.length>index+1){ + System.arraycopy(elementData, index+1, elementData, index,this.elementData.length-index-1); + } + this.elementData[this.elementData.length-1]=null; + return o; + } + + public Iterator iterator(){ + return new MyArrayListIterator(); + } + + @Override + public int size() { + return this.elementData.length; + } + + /** + * 判断数组容量是否满足,不满足增加空间 + */ + private void isOverSize() { + if(this.current==this.capacity){ + this.capacity+=MyArrayList.DEFAULT_SIZE; + } + Object[]newElementData=new Object[this.capacity]; + for(int i=0;ithis.capacity||index<0){ + throw new RuntimeException("数组下标越界"); + } + } + + /** + * MyArrayList的迭代器 + * @author 小摩托 + * + */ + private class MyArrayListIterator implements Iterator{ + + private int current=0; + + @Override + public boolean hasNext() { + return currentsize||index<0){//检查下标是否越界 + throw new RuntimeException("下标越界"); + } + if(index==this.size){ + addLast(o);//插到队尾 + }else{ + Node l= node(index); + addBeforeNode(o,l);//插到指定下标节点之前 + } + } + + @Override + public Object get(int index) { + if (index >= size || index < 0) { + throw new RuntimeException("下标越界"); + } + return node(index).data; + } + + @Override + public Object remove(int index) { + if (index >= size || index < 0) { + throw new RuntimeException("下标越界"); + } + Node l=node(index); + Node prevNode=l.prev; + Node nextNode=l.next; + if(prevNode==null){ + head=nextNode; + }else{ + prevNode.next=nextNode; + + } + if(nextNode==null){ + last=prevNode; + }else{ + nextNode.prev=prevNode; + } + l.prev =null; + l.next=null; + l.data=null; + return l.data; + } + + @Override + public int size() { + return size; + } + + /** + * 获取对应节点的下标 + * @param element + * @return + */ + public int indexOf(Object element) { + Node current = head; + int count = 0; + while (current != null) { + if (element != null) { + if (element.equals(current.data)) { + return count; + } + }else{ + if (current.data == null) { + return count; + } + } + count ++; + current = current.next; + } + return -1; + } + + /** + * 添加到对应下标的节点之前 + * @param o + * @param theNode + */ + public void addBeforeNode(Object o,Node theNode){ + Node prevNode=theNode.prev; + Node newNode= new Node(o,theNode,prevNode); + theNode.prev=newNode; + if(null==prevNode){ + this.head=newNode; + }else{ + prevNode.next=newNode; + } + size++; + } + + /** + * 默认添加到队尾 + * @param o + */ + public void addLast(Object o){ + Node l=this.last; + Node node= new Node(o,null,l); + if(null!=l){ + l.next=node; + }else{ + this.head=node; + } + size++; + } + + /** + * 查找对应下标的节点并返回 + * @param index + * @return + */ + private Node node(int index){ + if(index<(this.size>>1)){ + Node current=head; + for(int i=0;iindex;i--){ + current=current.prev; + } + return current; + } + } + + public Iterator iterator(){ + return new MyLinkedListIterator(); + } + + private class MyLinkedListIterator implements Iterator{ + private Node current=head; + + @Override + public boolean hasNext() { + + return current!=last; + } + + @Override + public Object next() { + if(hasNext()==false){ + throw new RuntimeException("不存在对应元素"); + } + Object o=current.data; + current=current.next; + return o; + } + + @Override + public void remove() { + int index=MyLinkedList.this.indexOf(current); + MyLinkedList.this.remove(index); + } + + } + /** + * 双向链表 + * @author 小摩托 + * + */ + private static class Node{ + Object data; + Node next; + Node prev; + public Node(Object d,Node n,Node p){ + this.data=d; + this.next=n; + this.prev=p; + } + } + +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..73f331beec --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public interface MyList { + + /** + * 向集合中添加元素 + * @param o 任意类型元素 + */ + public void add(Object o); + /** + * 向集合中添加元素 + * @param index 指定位置下标 + * @param o 任意类型元素 + */ + public void add(int index, Object o); + /** + * 获取对应下标的元素 + * @param index 下标 + * @return 对应下标的元素 + */ + public Object get(int index); + /** + * 移除对应下标的元素 + * @param index 下标 + * @return + */ + public Object remove(int index); + /** + * 获取集合的大小 + * @return 大小数值 + */ + public int size(); + +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..c7fba907d4 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class MyQueue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..705b3ec7e0 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class MyStack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170302/ArrayUtil.java b/group11/729245768/DataStructure/src/main/coding_170302/ArrayUtil.java new file mode 100644 index 0000000000..db8d8bef49 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170302/ArrayUtil.java @@ -0,0 +1,211 @@ +package main.coding_170302; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for(int i=0,j=origin.length-1;i list = new ArrayList<>(); + for(int i=6;i<=max;i++){ + if(isPerfectNumbers(i)){ + list.add(i); + } + } + int[] array = new int[list.size()]; + for(int i=0;i list = new ArrayList<>(); + int end =(int) Math.sqrt(element);// + for(int i=2;i<=end;){ + if(element%i==0){ + list.add(i); + element=element/i; + }else{ + i++; + } + } + int sum =1; + for(Integer i:list){ + sum+=i; + } + if(element==sum){ + return true; + }else{ + return false; + } + } + + /** + * 用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 parameters) throws DocumentException { + /* + + 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(); + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read(new File("DataStructure/src/main/coding_170302/struts.xml")); + Element root = document.getRootElement();//获取根元素 + List listOfAction = root.elements("action");//获取action子元素 + int i=0; + for(;i> inputs = parameters.entrySet(); + //执行setName和setPassword + for(Map.Entry input:inputs){ + String key = input.getKey(); + String value = input.getValue(); + String methodName = "set"+Character.toUpperCase(key.charAt(0))+key.substring(1); + Method method = c.getDeclaredMethod(methodName,String.class); + method.invoke(o,value); + } + //执行execute方法,并返回message + Method execute = c.getDeclaredMethod("execute"); + String returnMessage = (String)execute.invoke(o); + + Method[] methods = c.getDeclaredMethods(); + //获取所有getter方法,并且将值放到view的parameters中 + Map paraMap = new HashMap<>(); + for(int j=0;j 配置,以及execute的返回值, 确定哪一个jsp, + // 放到View对象的jsp字段中。 + List resultOfElements = action.elements("result"); + for(int k=0;k + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java b/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java new file mode 100644 index 0000000000..4d5cc38728 --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java @@ -0,0 +1,78 @@ +package main.coding_170302; + +import junit.framework.TestCase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by peter on 2017/3/2. + */ +public class ArrayUtilTest extends TestCase { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testReverseArray() throws Exception { + int[] array1 = {1,2,3,4,5,6,7}; + int[] array2 = {7,6,5,4,3,2,1}; + ArrayUtil util = new ArrayUtil(); + util.reverseArray(array1); + Assert.assertArrayEquals(array1,array2); + } + + @Test + public void testRemoveZero() throws Exception { + int[] array1 = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] arry2 = {1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(new ArrayUtil().removeZero(array1),arry2); +} + + @Test + public void testMerge() throws Exception { + int[] array1 = {3,5,7,8}; + int[] array2 = {4,5,6,7}; + int[] array = {3,4,5,6,7,8}; + Assert.assertArrayEquals(array,new ArrayUtil().merge(array1,array2)); + } + + @Test + public void testGrow() throws Exception { + int[] array1 ={2,3,6}; + int[] array2 = {2,3,6,0,0,0}; + Assert.assertArrayEquals(array2,new ArrayUtil().grow(array1,3)); + } + + @Test + public void testFibonacci() throws Exception { + int[] array1 = {1,1,2,3,5,8,13}; + Assert.assertArrayEquals(array1,new ArrayUtil().fibonacci(15)); + } + + @Test + public void testGetPrimes() throws Exception { + int[] array1 = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(array1,new ArrayUtil().getPrimes(23)); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] array1 = {6}; + Assert.assertArrayEquals(array1,new ArrayUtil().getPerfectNumbers(10)); + } + + @Test + public void testJoin() throws Exception { + int[] array1 = {3,8,9}; + Assert.assertEquals("3-8-9",new ArrayUtil().join(array1,"-")); + } + +} \ No newline at end of file diff --git a/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java b/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java new file mode 100644 index 0000000000..309bdfc9ba --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java @@ -0,0 +1,42 @@ +package main.coding_170302; + +import junit.framework.TestCase; +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by peter on 2017/3/3. + */ +public class StrutsTest extends TestCase { + @Test + public void testLoginActionSuccess() throws DocumentException { + String actionName = "login"; + + Map params = new HashMap<>(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + @Test + public void testLoginActionFailed() throws DocumentException { + String actionName = "login"; + Map params = new HashMap<>(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} \ No newline at end of file diff --git a/group11/996108220/.classpath b/group11/996108220/.classpath index d171cd4c12..11cc9c31cb 100644 --- a/group11/996108220/.classpath +++ b/group11/996108220/.classpath @@ -2,5 +2,7 @@ + + diff --git a/group11/996108220/src/com/coderising/array/ArrayUtil.java b/group11/996108220/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..0637eb921c --- /dev/null +++ b/group11/996108220/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,194 @@ +package com.coderising.array; + +import com.coding.basic.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int mid=origin.length%2==1?origin.length/2:origin.length/2-1; + for (int i = 0; i <= mid; i++) { + int vlaue=origin[i]; + origin[i]=origin[origin.length-1-i]; + origin[origin.length-1-i]=vlaue; + } + } + + /** + * 现在有如下的一个数组: 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++) { + if (oldArray[i]!=0) { + list.add(oldArray[i]); + } + } + int[] array=new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + array[i]=(int) list.get(i); + } + + return array; + } + + /** + * 给定两个已经排序好的整形数组, 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){ + int[] array=new int[array1.length+array2.length]; + int ptr1=0;int ptr2=0;int index=0; + while (ptr1!=array1.length&&ptr2!=array2.length) { + array[index++]=array1[ptr1]>array2[ptr2]?array2[ptr2++]:array1[ptr1++]; + } + if (ptr1==array1.length) { + for (int i = ptr2; i < array2.length; i++)array[index++]=array2[i]; + } + else { + for (int i = ptr1; i < array1.length; i++)array[index++]=array1[i]; + } + return array; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int [] array=new int[oldArray.length+size]; + for (int i = 0; i < oldArray.length; i++) { + array[i]=oldArray[i]; + } + return array; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + + if (max==1) { + return null; + } + else if (max==2) { + return new int[]{1,1}; + } + else { + ArrayList list=new ArrayList(); + list.add(1); + list.add(1); + int next=2; + while (next messageToresult=null; + public String getActionName() { + return actionName; + } + public void setActionName(String actionName) { + this.actionName = actionName; + } + public String getClazzName() { + return clazzName; + } + public void setClazzName(String clazzName) { + this.clazzName = clazzName; + } + public HashMap getMessageToresult() { + return messageToresult; + } + public void setMessageToresult(HashMap messageToresult) { + this.messageToresult = messageToresult; + } + public ActionConfig(String actionName, String clazzName, + HashMap messageToresult) { + super(); + this.actionName = actionName; + this.clazzName = clazzName; + this.messageToresult = messageToresult; + } + + +} diff --git a/group11/996108220/src/com/coderising/litestruts/LoginAction.java b/group11/996108220/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..01b51fdd88 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction implements Action{ + 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/group11/996108220/src/com/coderising/litestruts/LogoutAction.java b/group11/996108220/src/com/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..8043fae56e --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/LogoutAction.java @@ -0,0 +1,35 @@ +package com.coderising.litestruts; + +public class LogoutAction implements Action{ + 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 = "logout successful"; + return "success"; + } + this.message = "logout failed,please try again"; + return "error"; + } + + 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/group11/996108220/src/com/coderising/litestruts/Struts.java b/group11/996108220/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..8f961f2175 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,167 @@ +package com.coderising.litestruts; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import com.coderising.litestruts.Action; + + + +/* + +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字段中。 + +*/ + +public class Struts { + //strut.xml文件所在的路径 + public static final String dir="/mycoding2017/group11/996108220/src/com/coderising/litestruts/struts.xml"; + + /** + * 用户提供action动作,以及用户名和密码,对应返回view视图 + * @param actionName 登入登出 + * @param parameters 用户名密码 + */ + public static View runAction(String actionName, Map parameters) throws Exception { + ActionConfig actionConfig=getActionConfig(actionName); + Action action=createAction(actionConfig.getClazzName(),parameters); + String message=getActionMessage(action); + View view=updaView(getALL(action), actionConfig, message); + return view; + } + + /** + * 步骤0:读取配置文件,将文件中的action生成ActionDao + * @param actionName传入action的名字 + */ + private static ActionConfig getActionConfig(String name) throws Exception { + // 生成一个Dom解析器 + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + // 解析XML文件 + Document document = builder.parse(dir); + NodeList actions = document.getElementsByTagName("action"); + int j = 0;String actionName="";String clazzName=""; + for (; j < actions.getLength(); j++) { + actionName= actions.item(j).getAttributes().getNamedItem("name").getNodeValue(); + if (actionName.equals(name)) { + clazzName=actions.item(j).getAttributes().getNamedItem("class").getNodeValue(); + break; + } + } + if (actions.item(0).getNodeType() == Node.ELEMENT_NODE) { + + Element action =(Element) actions.item(j); + NodeList results =action.getElementsByTagName("result"); + HashMap map=new HashMap(); + for (int i = 0; i < results.getLength(); i++) { + String nameString=results.item(i).getAttributes().getNamedItem("name").getNodeValue(); + String pageString=results.item(i).getTextContent(); + map.put(nameString, pageString); + } + return new ActionConfig(actionName, clazzName, map); + } + return null; + } + /** + * 步骤1:反射创建action的对象,将name和password赋值 + * @param clazzName + * @return + */ + private static Action createAction(String clazzName,Map parameters) throws Exception { + Class clazz=Class.forName(clazzName); + Object action = clazz.newInstance() ; + Method nameSetter = action.getClass().getMethod("setName", String.class); + nameSetter .invoke(action, parameters.get("name")); + Method passwordSetter = action.getClass().getMethod("setPassword", String.class); + passwordSetter.invoke(action, parameters.get("password")); + return (Action) action; + } + /** + * 步骤2:反射运行execute方法,获得message + * @param action + * @return message + */ + private static String getActionMessage (Action action) throws Exception { + + return (String) action.getClass().getMethod("execute").invoke(action); + + } + /** + * 步骤3:将action中get方法与get到的值的映射关系记录到view里的Parameters表中 + * @param action + * @return view + */ + private static View getALL(Action action) throws Exception { + HashMap map=new HashMap<>(); + Method nameGetter = action.getClass().getMethod("getName"); + map.put("name", (String) nameGetter.invoke(action)); + Method passwordGetter = action.getClass().getMethod("getPassword"); + map.put("password", (String) passwordGetter.invoke(action)); + Method MessageGetter = action.getClass().getMethod("getMessage"); + map.put("message", (String) MessageGetter.invoke(action)); + View view=new View(); + view.setParameters(map); + return view; + } + /** + * 步骤4:将execute获得的message查找Struts配置文件将对应的页面记录到view中 + * @param view + * @param actionConfig + * @param message + * @return + */ + private static View updaView(View view,ActionConfig actionConfig,String message) { + return view.setJsp(actionConfig.getMessageToresult().get(message)); + } +// public static void main(String[] args) { +// DocumentBuilder builder; +// try { +// builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); +// Document doc = builder.parse(dir); +// NodeList beans = doc.getElementsByTagName("action"); +// for (int j = 0; j < beans.getLength(); j++) { +// System.out.println(beans.item(j).getAttributes().getNamedItem("name").getNodeValue()); +// System.out.println(beans.item(j).getAttributes().getNamedItem("class").getNodeValue()); +// } +// if (beans.item(0).getNodeType() == Node.ELEMENT_NODE) { +// +// Element action =(Element) beans.item(0); +// NodeList results =action.getElementsByTagName("result"); +// HashMap map=new HashMap(); +// System.out.println(results.getLength()); +// for (int i = 0; i < results.getLength(); i++) { +// System.out.println(results.item(i).getAttributes().getNamedItem("name").getNodeValue()); +// System.out.println(results.item(i).getTextContent()); +// } +// } +// +//// NamedNodeMap name = beans.item(0).getAttributes(); +// +// } catch (Exception e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } + + +// } + +} diff --git a/group11/996108220/src/com/coderising/litestruts/StrutsTest.java b/group11/996108220/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..af5f82d275 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,63 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + @Test + public void testLogoutActionSuccess() throws Exception { + + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("logout successful", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionFailed() throws Exception { + String actionName = "logout"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("logout failed,please try again", view.getParameters().get("message")); + } +} diff --git a/group11/996108220/src/com/coderising/litestruts/View.java b/group11/996108220/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/996108220/src/com/coderising/litestruts/struts.xml b/group11/996108220/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/996108220/src/com/coding/basic/ArrayList.java b/group11/996108220/src/com/coding/basic/ArrayList.java index 6fe727645b..2445d52ae5 100644 --- a/group11/996108220/src/com/coding/basic/ArrayList.java +++ b/group11/996108220/src/com/coding/basic/ArrayList.java @@ -1,5 +1,6 @@ package com.coding.basic; + public class ArrayList implements List { private int size = 0; @@ -94,6 +95,13 @@ public Object next() { } } + public Object[] toArray() { + Object[] array= new Object[size]; + for (int i = 0; i < elementData.length; i++) { + array[i]=elementData[i]; + } + return array; + } public void grow(Object[] elementData2){ int[] elementData=new int[elementData2.length+elementData2.length/2]; System.arraycopy(elementData2,0,elementData,0,elementData2.length);