diff --git a/group16/1154151360/.classpath b/group16/1154151360/.classpath index fb5011632c..9746a6c933 100644 --- a/group16/1154151360/.classpath +++ b/group16/1154151360/.classpath @@ -2,5 +2,8 @@ + + + diff --git a/group16/1154151360/src/com/array/ArrayUtil.java b/group16/1154151360/src/com/array/ArrayUtil.java new file mode 100644 index 0000000000..39f587c847 --- /dev/null +++ b/group16/1154151360/src/com/array/ArrayUtil.java @@ -0,0 +1,266 @@ +package com.array; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.sun.org.apache.xalan.internal.xsltc.compiler.sym; + +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){ + + int [] temp = new int [origin.length]; + + for (int i = 0; i < temp.length; i++){ + temp [i] = origin [temp.length - 1 - i]; + } + + origin = temp; + + return 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 newLength = 0; + + for (int i = 0; i < oldArray.length ; i++){ + if (oldArray[i] == 0) + newLength ++; + } + + int [] newArray = new int [oldArray.length - newLength]; + int index = 0; + for (int j = 0; j < oldArray.length; j++){ + if (oldArray[j] != 0) + newArray[index++] = oldArray[j]; + } + + newArray = sort(newArray, 0, newArray.length - 1); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + + int newLength = array1.length + array2.length; + int [] newArray = new int [newLength]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + System.arraycopy(array2, 0, newArray, array1.length, array2.length); + Set arraySet = new HashSet(); + for (int i = 0; i < newArray.length; i++){ + arraySet.add(newArray[i]); + } + int [] tempArray = new int[arraySet.size()]; + Iterator iterator = arraySet.iterator(); + int index = 0; + while (iterator.hasNext()){ + tempArray[index++] = iterator.next(); + } + newArray = sort(tempArray, 0, tempArray.length - 1); + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int [] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, size); + 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 int[] fibonacci(int max){ + if (max > 1){ + List array = new ArrayList(); + array.add(1); + array.add(1); + for(int i = 2;;i++){ + array.add(array.get(i - 1) + array.get(i - 2)); + if (array.get(i) > max){ + array.remove(i); + break; + } + } + int [] result = returnArray(array); + return result; + }else{ + int [] b = new int[]{}; + return b; + } + + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList list1 = new ArrayList(); + ArrayList list2 = new ArrayList(); + for (int i = 2; i < max; i++){ + list1.add(i); + } + + for (int m = 0; m < list1.size(); m++){ + boolean flag = true; + for (int n = 2; n < list1.get(m); n++){ + if (list1.get(m) % n == 0){ + flag = false; + break; + } + + } + if (flag) + list2.add(list1.get(m)); + } + + int [] result = returnArray(list2); + + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + ArrayList array = new ArrayList(); + Map> arrayMap = new HashMap>(); + ArrayList array2 = new ArrayList(); + for (int i = 2; i < max; i++){ + array.add(i); + } + for(int m = 0; m < array.size(); m++){ + ArrayList tempArray = new ArrayList(); + for (int n = 2; n < array.get(m);n++){ + if (array.get(m) % n == 0) + tempArray.add(n); + } + arrayMap.put(array.get(m), tempArray); + } + for(Map.Entry> entry: arrayMap.entrySet()){ + Integer key = entry.getKey(); + ArrayList tempArray = entry.getValue(); + Integer tempInt = 0; + for (Integer i:tempArray){ + tempInt += i; + } + if (key == tempInt){ + array2.add(key); + } + } + int [] result = returnArray(array2); + + 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 builder = new StringBuilder(); + + for (int i = 0; i < array.length; i++){ + builder.append(array[i]); + builder.append(seperator); + } + builder.deleteCharAt(builder.length() - 1); + return builder.toString(); + } +//***************************工具类***************************************** + public int [] returnArray( List list){ + int [] result = new int[list.size()]; + int index = 0; + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + result[index++] = iterator.next(); + } + return result; + } + + //快速排序算法 + public int [] sort (int [] array, int low, int height ){ + + int start = low; + int end = height; + int key = array[low]; + + while (end > start){ + //从后往前遍历 + while (end > start && array[end] >= key) + end--; + + if (array[end] <= key){ + int temp = array[end]; + array[end] = array[start]; + array[start] = temp; + } + + //从前往后遍历 + while(end > start && array[start] <= key) + start++; + + if (array[start] >= key){ + int temp = array[start]; + array[start] = array[end]; + array[end] = temp; + } + } + + if(start > low)sort(array, low, start - 1); + if (end < height)sort(array, end + 1, height); + + return array; + } +//******************************************************************8 +} diff --git a/group16/1154151360/src/com/array/ArrayUtilTest.java b/group16/1154151360/src/com/array/ArrayUtilTest.java new file mode 100644 index 0000000000..159d497b6d --- /dev/null +++ b/group16/1154151360/src/com/array/ArrayUtilTest.java @@ -0,0 +1,102 @@ +package com.array; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +@SuppressWarnings("deprecation") +public class ArrayUtilTest { + + ArrayUtil util; + + @Before + public void init(){ + util = new ArrayUtil(); + } + + @Test + public void test_reverseArray() { + int [] a = {7, 9, 30, 3, 4}; + a = util.reverseArray(a); + Assert.assertEquals("[4,3,30,9,7]", toString(a)); + } + + @Test + public void test_removeZero(){ + int [] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + + oldArr = util.removeZero(oldArr); + + Assert.assertEquals("[1,3,4,5,6,6,5,4,7,6,7,5]", toString(oldArr)); + } + + + @Test + public void test_merge(){ + int [] a1 = {3, 5, 7,8}; + int [] a2 = {4, 5, 6,7}; + int [] a3 = util.merge(a1, a2); + + Assert.assertEquals("[3,4,5,6,7,8]", toString(a3)); + + } + + @Test + public void test_grow(){ + int [] oldArray = {2,3,6}; + int size = 3; + int [] newArray = util.grow(oldArray, size); + + Assert.assertEquals("[2,3,6,0,0,0]", toString(newArray)); + } + + + @Test + public void test_fibonacci(){ + + int [] array = util.fibonacci(15); + + Assert.assertEquals("[1,1,2,3,5,8,13]", toString(array)); + + } + + + @Test + public void test_getPrimes(){ + + int [] array = util.getPrimes(23); + Assert.assertEquals("[2,3,5,7,11,13,17,19]", toString(array)); + + } + + @Test + public void test_getPerfectNumbers(){ + int [] array = util.getPerfectNumbers(10); + + Assert.assertEquals("[6]", toString(array)); + + } + + @Test + public void test_join(){ + + int [] array = {3,8,9}; + String result = util.join(array, "-"); + Assert.assertEquals("3-8-9", result); + } + + + public String toString(int [] array){ + StringBuilder builder = new StringBuilder(); + builder.append("["); + for(int item: array){ + builder.append(item) + .append(","); + } + builder.replace(builder.length() - 1, builder.length(), ""); + builder.append("]"); + return builder.toString(); + + } +} diff --git a/group16/1154151360/src/com/litestruts/LoginAction.java b/group16/1154151360/src/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..24369135f9 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.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/group16/1154151360/src/com/litestruts/Struts.java b/group16/1154151360/src/com/litestruts/Struts.java new file mode 100644 index 0000000000..49254d6a5f --- /dev/null +++ b/group16/1154151360/src/com/litestruts/Struts.java @@ -0,0 +1,139 @@ +package com.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + + + //创建SAXreader对象 + SAXReader reader = new SAXReader(); + //将读取的文件封装成document对象 + Document document = reader.read(new File("src/com/litestruts/struts.xml")); + //获取文档的根节点 + Element root = document.getRootElement(); + + List elist = listNode(root); + + Map > actionMap = getAction(elist, actionName); + + for(Map.Entry> entity: actionMap.entrySet()){ + String className = entity.getKey(); + Class clazz = Class.forName(className); + Method setName = clazz.getMethod("setName", String.class); + Method setPassword = clazz.getMethod("setPassword", String.class); + Method getMessage = clazz.getMethod("getMessage"); + Method execute = clazz.getMethod("execute"); + Object object = clazz.newInstance(); + setName.invoke(object, parameters.get("name")); + setPassword.invoke(object, parameters.get("password")); + String status = (String) execute.invoke(object); + String message = (String) getMessage.invoke(object); + + String jsp = entity.getValue().get(status); + + Map parameter = new HashMap(); + + parameter.put("message", message); + + View view = new View(); + + view.setJsp(jsp); + view.setParameters(parameter); + + return view; + } + + return null; + } + + //获取节点的所有子节点 + private static List listNode(Element node){ + + Iterator iterator = node.elementIterator(); + List elist = new ArrayList(); + while (iterator.hasNext()){ + Element e = iterator.next(); + elist.add(e); + } + if (elist.isEmpty()) + return null; + else + return elist; + } + + //获取对应的Action信息 + private static Map > getAction(List elist,String actionName){ + + for (Element node:elist){ + List attributes = node.attributes(); + if (attributes.isEmpty()) + getAction(listNode(node),actionName); + else{ + Attribute attribute = node.attribute("name"); + if (attribute.getValue().equals(actionName)){ + String className = node.attribute("class").getValue(); + List childElements = listNode(node); + Map resMap =install(childElements); + Map > actionMap = new HashMap>(); + actionMap.put(className, resMap); + return actionMap; + } + } + } + return null; + } + + //组装action的result + private static Map install(List elements){ + Map resultMap = new HashMap(); + for (Element node: elements){ + Attribute attribute = node.attribute("name"); + String value; + if (node.getTextTrim().isEmpty()) + value = ""; + else + value = node.getTextTrim(); + + resultMap.put(attribute.getValue(), value); + + } + return resultMap; + } + +} diff --git a/group16/1154151360/src/com/litestruts/StrutsTest.java b/group16/1154151360/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9317cc5e26 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/StrutsTest.java @@ -0,0 +1,51 @@ +package com.litestruts; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import junit.framework.Assert; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} diff --git a/group16/1154151360/src/com/litestruts/View.java b/group16/1154151360/src/com/litestruts/View.java new file mode 100644 index 0000000000..af63dce301 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/View.java @@ -0,0 +1,23 @@ +package com.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/group16/1154151360/src/com/litestruts/struts.xml b/group16/1154151360/src/com/litestruts/struts.xml new file mode 100644 index 0000000000..ddbcbc38da --- /dev/null +++ b/group16/1154151360/src/com/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/group16/1287642108/0226/src/com/coding/basic/ArrayList.java b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java index e287419dc0..f82d064e2a 100644 --- a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java +++ b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java @@ -1,63 +1,63 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o) { - IncrementsCapacity(size + 1); - elementData[size++] = o; - - } - - public void add(int index, Object o) { - checkIndex(index); - IncrementsCapacity(size + 1); - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - size++; - } - - public Object get(int index) { - checkIndex(index); - return elementData[index]; - } - - public Object remove(int index) { - checkIndex(index); - Object o = elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - elementData[--size] = null; - return o; - } - - public int size() { - return size; - } - - public int length() { - return elementData.length; - } - - // - private void IncrementsCapacity(int num) { - if (num > elementData.length) { - int oldCapacity = elementData.length; // ǰ鳤 - int newCapacity = ((num + oldCapacity) * 3) >> 2; // ǰ鳤ȵ1.5 - if (newCapacity - num < 0) { - newCapacity = num; // Dz,ֱΪֵ - } - Object[] oldelements = elementData; - elementData = new Object[newCapacity]; - System.arraycopy(oldelements, 0, elementData, 0, size); - } - } - - // ±Խж - private void checkIndex(int index) { - if (index >= size || index < 0) - throw new IndexOutOfBoundsException("±Խ"); - } -} +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + IncrementsCapacity(size + 1); + elementData[size++] = o; + + } + + public void add(int index, Object o) { + checkIndex(index); + IncrementsCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public int length() { + return elementData.length; + } + + // + private void IncrementsCapacity(int num) { + if (num > elementData.length) { + int oldCapacity = elementData.length; // ǰ鳤 + int newCapacity = ((num + oldCapacity) * 3) >> 2; // ǰ鳤ȵ1.5 + if (newCapacity - num < 0) { + newCapacity = num; // Dz,ֱΪֵ + } + Object[] oldelements = elementData; + elementData = new Object[newCapacity]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + // ±Խж + private void checkIndex(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("±Խ"); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Iterator.java b/group16/1287642108/0226/src/com/coding/basic/Iterator.java index e7cbd474ec..ff93e30377 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Iterator.java +++ b/group16/1287642108/0226/src/com/coding/basic/Iterator.java @@ -1,6 +1,6 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java index 99c92fb9f1..fc206b4cec 100644 --- a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java +++ b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java @@ -1,101 +1,101 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private Node tail; - private int size = 0 ; - - public void add(Object o){ - addLast(o); - } - - public void add(int index, Object o) { - if (index == 0) { - addFirst(o); - } else if (index >= size) { - addLast(o); - } else { - Node node = new Node(); - node.data = o; - node.next = getNode(index); - getNode(index - 1).next = node; - size++; - } - } - - public Object get(int index) { - Node node = getNode(index); - return node.data; - } - - public Object remove(int index){ - Node currentNode = getNode(index); - Node prevNode = getNode(index - 1); - Node lastNode = getNode(index + 1); - prevNode.next = lastNode; - size--; - return currentNode.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node node=new Node(); - node.data = o; - node.next = head; - head = node; - size++; - } - public void addLast(Object o){ - Node node=new Node(); - node.data = o; - node.next = null; - Node lastNode = getNode(size-1); - lastNode.next = node; - size++; - } - public Object removeFirst(){ - Object obj = getNode(0).data; - Node node = getNode(1); - node.next = head; - size--; - return obj; - } - public Object removeLast(){ - Object obj = getNode(size - 1).data; - Node node = getNode(size - 2); - node.next = null; - size--; - return obj; - } - - //ȡڵ - public Node getNode(int index){ - checkIndex(index); - if(index == 0 ){ - return head; - }else if(index == size -1 ){ - return tail; - }else{ - Node node = head; - for(int i=0;i= size || index < 0) - throw new IndexOutOfBoundsException("±Խ"); - } - - private static class Node { - Object data; - Node next; - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size = 0 ; + + public void add(Object o){ + addLast(o); + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + } else if (index >= size) { + addLast(o); + } else { + Node node = new Node(); + node.data = o; + node.next = getNode(index); + getNode(index - 1).next = node; + size++; + } + } + + public Object get(int index) { + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + Node currentNode = getNode(index); + Node prevNode = getNode(index - 1); + Node lastNode = getNode(index + 1); + prevNode.next = lastNode; + size--; + return currentNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node=new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + Node node=new Node(); + node.data = o; + node.next = null; + Node lastNode = getNode(size-1); + lastNode.next = node; + size++; + } + public Object removeFirst(){ + Object obj = getNode(0).data; + Node node = getNode(1); + node.next = head; + size--; + return obj; + } + public Object removeLast(){ + Object obj = getNode(size - 1).data; + Node node = getNode(size - 2); + node.next = null; + size--; + return obj; + } + + //ȡڵ + public Node getNode(int index){ + checkIndex(index); + if(index == 0 ){ + return head; + }else if(index == size -1 ){ + return tail; + }else{ + Node node = head; + for(int i=0;i= size || index < 0) + throw new IndexOutOfBoundsException("±Խ"); + } + + private static class Node { + Object data; + Node next; + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/List.java b/group16/1287642108/0226/src/com/coding/basic/List.java index 10d13b5832..396b1f6416 100644 --- a/group16/1287642108/0226/src/com/coding/basic/List.java +++ b/group16/1287642108/0226/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Queue.java b/group16/1287642108/0226/src/com/coding/basic/Queue.java index 95dee3d81b..418e42826e 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Queue.java +++ b/group16/1287642108/0226/src/com/coding/basic/Queue.java @@ -1,28 +1,28 @@ -package com.coding.basic; - -public class Queue { - private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o){ - elementData.addLast(o); - } - - public Object deQueue(){ - if (isEmpty()) { - return null; - }else{ - return elementData.removeFirst(); - } - } - - public boolean isEmpty(){ - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + return null; + }else{ + return elementData.removeFirst(); + } + } + + public boolean isEmpty(){ + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Stack.java b/group16/1287642108/0226/src/com/coding/basic/Stack.java index c0b7da89f8..6138bc6973 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Stack.java +++ b/group16/1287642108/0226/src/com/coding/basic/Stack.java @@ -1,38 +1,38 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o) { - if (isEmpty()) { - elementData.add(elementData.length() - 1, o); - } - elementData.add(elementData.length() - elementData.size() - 1, o); - } - - public Object pop() { - if (isEmpty()) { - return null; - } - return elementData.remove(elementData.length() - elementData.size() - 1); - } - - public Object peek() { - if (isEmpty()) { - return null; - } - return elementData.get(elementData.length() - elementData.size() - 1); - } - - public boolean isEmpty() { - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size() { - return elementData.size(); - } - -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + if (isEmpty()) { + elementData.add(elementData.length() - 1, o); + } + elementData.add(elementData.length() - elementData.size() - 1, o); + } + + public Object pop() { + if (isEmpty()) { + return null; + } + return elementData.remove(elementData.length() - elementData.size() - 1); + } + + public Object peek() { + if (isEmpty()) { + return null; + } + return elementData.get(elementData.length() - elementData.size() - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..8e0d8af87b --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,205 @@ +package com.coderising.array; + +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = {7, 9 , 30, 3} , 置换后为 {3, 30, 9,7} 如果 a = + * {7, 9, 30, 3, 4} , 置换后为 {4,3, 30 , 9,7} + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] temp = new int[origin.length]; + int index = 0; + for (int i = origin.length - 1; i >= 0; i--) { + temp[index++] = origin[i]; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + int countZero = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] == 0){ + countZero++; + } + } + + int[] temp = new int[oldArray.length - countZero]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + temp[index++] = oldArray[i]; + } + } + return temp; + } + + /** + * 给定两个已经排序好的整形数组, 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 Integer[] merge(int[] array1, int[] array2) { + Integer[] temp = new Integer[array1.length + array2.length]; + int i = 0,j = 0; + int index = 0; + + while(i < array1.length && j < array2.length){ + if(array1[i] <= array2[j]){ + temp[index++] = array1[i++]; + }else{ + temp[index++] = array2[j++]; + } + } + + while(i < array1.length){ + temp[index++] = array1[i++]; + } + while(j < array2.length){ + temp[index++] = array2[j++]; + } + return removeRepetition(temp); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = {2,3,6} , size = 3,则返回的新数组为 + * {2,3,6,0,0,0} + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] temp = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, temp, 0, oldArray.length); + return temp; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max){ + int[] temp = new int[max]; + if(max == 1){ + return null; + } + int index = 0; + for(int i = 0;i temp = new ArrayList<>(); + for(int i=2; i < max; i++){ + isPrime = true; + for (int j = 2; j < i; j++){ + if ((i % j) == 0) { + isPrime = false; + break; + } + } + if(isPrime){ + temp.add(i); + } + } + int[] array = new int[temp.size()]; + int index = 0; + for(Integer te : temp){ + array[index++] = te; + } + return array; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static Integer[] getPerfectNumbers(int max) { + Integer[] temp = new Integer[max]; + int index = 0; + for (int i = 2; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + if (sum == i){ + temp[index++] = i; + } + } + } + return removeRepetition(temp); + } + + /** + * 用seperator 把数组 array给连接起来 例如array= {3,8,9}, seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String temp = ""; + for(int i = 0;i < array.length -1; i++){ + temp += array[i] + seperator; + } + temp += array[array.length-1]; + return temp; + } + + public static Integer[] removeRepetition(Integer[] oldArray){ + ArrayList temp = new ArrayList<>(); + for(int i=0;i parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + //创建SAXReader读取器,专门用于读取xml + SAXReader saxReader = new SAXReader(); + Document document = null; + try { + document = saxReader.read(new File("D:/DemoSpace/coding2017/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + //根据节点名称找节点 + Node node = root.selectSingleNode("action[@name='"+actionName+"']"); + String classPath = ((Element) node).attributeValue("class"); + //根据类名反射实例化 + Class onwClass = Class.forName(classPath); + Object o = onwClass.newInstance(); + Method setName = onwClass.getMethod("setName",String.class); + Method setPassword = onwClass.getMethod("setPassword",String.class); + Method execute = onwClass.getMethod("execute"); + Method getName = onwClass.getMethod("getName"); + Method getPassword = onwClass.getMethod("getPassword"); + Method getMessage = onwClass.getMethod("getMessage"); + setName.invoke(o,parameters.get("name")); + setPassword.invoke(o,parameters.get("password")); + String result = (String) execute.invoke(o); + //组装params参数 + HashMap map = new HashMap<>(); + String name = (String) getName.invoke(o); + String password = (String) getPassword.invoke(o); + String message = (String) getMessage.invoke(o); + map.put("name", name); + map.put("password", password); + map.put("message", message); + //组装view数据 + View view = new View(); + view.setParameters(map); + //根据execute的返回值,找对应的jsp页面路径 + String jspPath = node.valueOf("//result[@name='"+result+"']"); + view.setJsp(jspPath); + return view; + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..2a8ed76022 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/View.java b/group16/1287642108/0305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/1287642108/0305/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/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e22003fc12 --- /dev/null +++ b/group16/1287642108/0305/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/group16/1325756593/src/com/coderising/array/ArrayUtil.java b/group16/1325756593/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..0f67e6b54f --- /dev/null +++ b/group16/1325756593/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,273 @@ +package com.coderising.array; + +import static org.hamcrest.CoreMatchers.nullValue; + +import java.util.Arrays; + +import com.dong.week1.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){ + if(origin==null||origin.length==0){ + return; + } + int len = origin.length; + for(int i=0;iarray2[index2]){ + arrayList.add(array2[index2]); + index2++; + }else if(array1[index1]==array2[index2]){ + arrayList.add(array2[index2]); + index1++; + index2++; + } + else{ + arrayList.add(array1[index1]); + index1++; + } + } + if(index1==len1){ + for(int i=index2;i=max){ + break; + } + arrayList.add(third); + first = second; + second=third; + + + } + return ListToArray(arrayList); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList arrayList = new ArrayList(); + for(int i=2;i resultAndViewMap; + public String getActioName() { + return actioName; + } + public void setActioName(String actioName) { + this.actioName = actioName; + } + @Override + public String toString() { + return "Action [actioName=" + actioName + ", className=" + className + ", resultAndViewMap=" + resultAndViewMap + + "]"; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public Map getResultAndViewMap() { + return resultAndViewMap; + } + public void setResultAndViewMap(Map resultAndViewMap) { + this.resultAndViewMap = resultAndViewMap; + } + + + + +} diff --git a/group16/1325756593/src/com/coderising/litestruts/LoginAction.java b/group16/1325756593/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group16/1325756593/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/group16/1325756593/src/com/coderising/litestruts/Struts.java b/group16/1325756593/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..ae6289d5f0 --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,246 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +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 String strutsPath; + + + public static String getStrutsPath() { + return strutsPath; + } + + public static void setStrutsPath(String strutsPath) { + Struts.strutsPath = strutsPath; + } + + + + + public static View runAction(String actionName, Map parameters) throws ParserConfigurationException, SAXException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + if(strutsPath==null||strutsPath.equals("")){ + strutsPath = initStrutsPath(); + } + List actions = parseXml(strutsPath); + Action action = getAction(actions,actionName); + NotBeNull(action); + + + String actionClassName = action.getClassName(); + Class actionClass = Class.forName(actionClassName); + Object actionClassObject = actionClass.newInstance(); + + doSetterMethod(parameters, actionClass, actionClassObject); + + Method excuteMethod = actionClass.getMethod("execute", null); + String result = excuteMethod.invoke(actionClassObject, null).toString(); + String viewPath = action.getResultAndViewMap().get(result); + View view = new View(); + view.setJsp(viewPath); + Map parametersMap = doGetterMethod(actionClass, actionClassObject); + view.setParameters(parametersMap); + + return view; + } + + + /** + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + * @param actionClass + * @param actionClassObject + * @return + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static Map doGetterMethod(Class actionClass, Object actionClassObject) + throws IllegalAccessException, InvocationTargetException { + Map parametersMap = new HashMap<>(); + for(Method method :actionClass.getMethods()){ + if(method.getName().startsWith("get")){ + String getValue = getMethodValue(method.getName()); + parametersMap.put(getValue, method.invoke(actionClassObject, null)); + } + } + return parametersMap; + } + + /** + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + * @param parameters + * @param actionClass + * @param actionClassObject + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static void doSetterMethod(Map parameters, Class actionClass, Object actionClassObject) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for( Map.Entry entry: parameters.entrySet()){ + String param = entry.getKey(); + Method methodKeySet = actionClass.getMethod(getMethodSet(param), String.class); + methodKeySet.invoke(actionClassObject, entry.getValue()); + } + } + + /** + * 初始化strutsPath + * @return + */ + private static String initStrutsPath() { + String path = Struts.class.getClassLoader().getResource("").getPath(); + String strutsXmlPath = path+"/com/coderising/litestruts/struts.xml"; + return strutsXmlPath; + } + + + /** + * 通过name构造setName + * @param name + * @return + */ + public static String getMethodSet(String name){ + String firstChar = name.substring(0, 1).toUpperCase(); + return "set"+firstChar+name.substring(1); + } + + /** + * 获取getMessage对应的message + * @param name + * @return + */ + public static String getMethodValue(String name){ + if(name.length()>=3){ + name = name.substring(3); + } + String firstChar = name.substring(0, 1).toLowerCase(); + return firstChar+name.substring(1); + } + + + + /** + * 将xml解析成为Action的List结构 + * @param xmlPath + * @return + * @throws ParserConfigurationException + * @throws SAXException + * @throws IOException + */ + public static List parseXml(String xmlPath) throws ParserConfigurationException, SAXException, IOException{ + List retActionList = new ArrayList<>(); + File xmlFile = new File(xmlPath); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(xmlFile); + NodeList actionLists = doc.getElementsByTagName("action"); + for(int i=0;i resultAndViewMap=getResultAndViewNodeList(currentNode); + action.setResultAndViewMap(resultAndViewMap); + retActionList.add(action); + } + return retActionList; + } + + /** + * 获取该Node对应的result和对应的jsppath + * @param currentNode + * @param resultAndViewMap + */ + private static HashMap getResultAndViewNodeList(Node currentNode) { + Map resultAndViewMap = new HashMap<>(); + NodeList resultAndViewNodeList = currentNode.getChildNodes(); + + for(int j=0;j) resultAndViewMap; + } + + + private static void NotBeNull(Object result) { + if(result==null){ + throw new IllegalArgumentException(); + } + } + + + + public static String getAttribute(Node node ,String name){ + NamedNodeMap nameNodeMap = node.getAttributes(); + NotBeNull(nameNodeMap); + Node nameNode = nameNodeMap.getNamedItem(name); + NotBeNull(nameNode); + String actionName =nameNode.getNodeValue(); + return actionName; + } + + public static Action getAction(List actions ,String actionName){ + for(Action action:actions){ + if(action.getActioName().equals(actionName)){ + return action; + } + } + return null; + + } + + + +} diff --git a/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java b/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..487f05542c --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,48 @@ +package com.coderising.litestruts; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, ParserConfigurationException, SAXException, IOException { + + 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 InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, ParserConfigurationException, SAXException, IOException { + 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/group16/1325756593/src/com/coderising/litestruts/View.java b/group16/1325756593/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/1325756593/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/group16/1325756593/src/com/coderising/litestruts/struts.xml b/group16/1325756593/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group16/1325756593/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/group16/214074094/src/com/coding/basic/BinaryTreeNode.java b/group16/214074094/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index b40066ebe1..0000000000 --- a/group16/214074094/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,39 +0,0 @@ -package coding.basic; - -public class BinaryTreeNode { - - private Object data; - - private BinaryTreeNode left; - - private BinaryTreeNode right; - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public BinaryTreeNode getLeft() { - return left; - } - - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - - public BinaryTreeNode getRight() { - return right; - } - - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o) { - return null; - } - -} diff --git a/group16/214074094/src/com/coding/basic/List.java b/group16/214074094/src/com/coding/basic/List.java deleted file mode 100644 index 5da9b0d4c6..0000000000 --- a/group16/214074094/src/com/coding/basic/List.java +++ /dev/null @@ -1,15 +0,0 @@ -package coding.basic; - -public interface List { - - void add(Object o); - - void add(int index, Object o); - - Object get(int index); - - Object remove(int index); - - int size(); - -} diff --git a/group16/214074094/src/com/reading/blog_test.txt b/group16/214074094/src/com/reading/blog_test.txt deleted file mode 100644 index b7e5cbfe14..0000000000 --- a/group16/214074094/src/com/reading/blog_test.txt +++ /dev/null @@ -1 +0,0 @@ -just test new fork \ No newline at end of file diff --git a/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java b/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..c2b0c62a79 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java @@ -0,0 +1,213 @@ +package study.coderising.array; + + +import study.coding.basic.ArrayList; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +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) { + int tmp; + for (int i = 0; i < origin.length / 2; i++) { + tmp = origin[i]; + origin[i] = origin[origin.length - i - 1]; + origin[origin.length - i - 1] = 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) { + int[] newArray = new int[oldArray.length]; + int k = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + newArray[k++] = oldArray[i]; + } + } + return Arrays.copyOf(newArray, k); + } + + /** + * 给定两个已经排序好的整形数组,a1和a2,创建一个新的数组a3,使得a3包含a1和a2的所有元素,并且仍然是有序的 + * 例如 a1 = {3, 5, 7, 8},a2 = {4, 5, 6, 7},则 a3 为[3,4,5,6,7,8],注意:已经消除了重复 + * + * @param a1 + * @param a2 + * @return + */ + + public static int[] merge(int[] a1, int[] a2) { + Set set = new HashSet(); + + for (int i = 0; i < a1.length + a2.length; i++) { + if (i < a1.length) { + set.add(a1[i]); + } else { + set.add(a2[i - a1.length]); + } + } + + int[] a3 = new int[set.size()]; + + Iterator it = set.iterator(); + int i = 0; + while (it.hasNext()) { + a3[i++] = (Integer) it.next(); + } + return a3; + } + + /** + * 把一个已经存满数据的数组 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) { + return Arrays.copyOf(oldArray, oldArray.length + size); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max == 1) { + return new int[0]; + } + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + for (int i = 0; ; i++) { + int tmp = list.get(i) + list.get(i + 1); + if (tmp >= max) { + break; + } + list.add(i + 2, tmp); + } + + return convertIntegerArray2Int(list); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + ArrayList list = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + return convertIntegerArray2Int(list); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max < 2) { + return null; + } + ArrayList list = new ArrayList<>(); + + int count = 0; + //如果p是质数,且2^p-1也是质数,那么(2^p-1)X2^(p-1)便是一个完全数 + for (int i = 2; i <= max / 2; i++) { + count++; + if (isPrime(i) && isPrime((int) Math.pow(2, i) - 1)) { + System.out.println("count " + i + ":" + (int) (Math.pow(2, i) - 1) + " * " + (int) Math.pow(2, (i - 1))); + int perfectNum = (int) ((Math.pow(2, i) - 1) * Math.pow(2, (i - 1))); + if (perfectNum > max) { + break; + } + if (perfectNum < max) { + list.add(perfectNum); + } + } + } + System.out.println("total count : " + count); + return convertIntegerArray2Int(list); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (null == array || array.length < 1) { + return null; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]).append(seperator); + } + return sb.substring(0, sb.length() - 1); + } + + private static boolean isPrime(int n) { + if (n == 2) { + return true; + } + for (int j = 2; j <= Math.sqrt(n); j++) { + if (n % j == 0) { + return false; + } + } + return true; + } + + private static int[] convertIntegerArray2Int(ArrayList list) { + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i).intValue(); + } + return arr; + } + + +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java b/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..043df200d8 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package study.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author liuxin + */ +public class LoginAction { + + private String name; + private String password; + private String message; + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java b/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..e3a10a6726 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java @@ -0,0 +1,39 @@ +package study.coderising.litestruts; + +/** + * @Author shane + * @Time 2017/3/4 12:13 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class LogoutAction { + + private String name; + + private String message; + + public String execute() { + if ("test".equalsIgnoreCase(name)) { + message = name + " logout success"; + return "success"; + } + message = name + " logout fail"; + return "error"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java b/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..9a70e7a525 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java @@ -0,0 +1,147 @@ +package study.coderising.litestruts; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import study.coderising.litestruts.bean.Action; +import study.coderising.litestruts.bean.Result; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +public class Struts { + + private static final String EXECUTE = "execute"; + + public static View runAction(String actionName, Map parameters) + throws DocumentException, ClassNotFoundException, NoSuchMethodException, + InvocationTargetException, IllegalAccessException, InstantiationException { + + //0. 读取配置文件struts.xml + SAXReader reader = new SAXReader(); + Document doc = reader.read("./src/main/resources/struts.xml"); + Element root = doc.getRootElement(); + Action action = getAction(actionName, root); + + //1. 根据actionName找到相对应的class + Class clz = Class.forName(action.getClassPath()); + //获取一个实例,方便接下来对同一个对象赋值 + Object obj = clz.newInstance(); + + //1.1 据parameters中的数据,调用对象的setter方法 + for (Map.Entry entry : parameters.entrySet()) { + String setFunctionName = getFunctionName("set", entry.getKey()); + Method set = clz.getDeclaredMethod(setFunctionName, String.class); + set.invoke(obj, entry.getValue()); + } + + //2. 通过反射调用对象的exectue方法, 并获得返回值 + Method execute = clz.getDeclaredMethod(EXECUTE); + String response = execute.invoke(obj).toString(); + + //3. 通过反射找到对象的所有getter方法并调用, 把值和属性形成一个HashMap + Method[] methods = clz.getDeclaredMethods(); + Map map = new HashMap<>(); + for (Method m : methods) { + if (m.getName().startsWith("get")) { + String paramName = m.getName().replaceFirst("get", ""); + map.put(paramName.toLowerCase(), m.invoke(obj)); + } + } + + //3.1 放到View对象的parameters + View view = new View(); + view.setParameters(map); + + //4. 根据struts.xml中的配置,以及execute的返回值,确定jsp,放到View对象的jsp字段中。 + for (Result result : action.getResults()) { + if (response.equalsIgnoreCase(result.getResult())) { + view.setJsp(result.getJumpPath()); + break; + } + } + + return view; + } + + /** + * @Author: shane + * @Time: 2017/3/4 23:53 + * @Email: stevenchenguang@gmail.com + * @param: begin, key + * @Return: String + * @Throw: + * @Desc: 根据开始名称和key获取方法名 + * e.g.: begin: get, key: name, return getName + */ + private static String getFunctionName(String begin, String key) { + if (key == null || "".equals(key)) { + return null; + } + StringBuffer sb = new StringBuffer(begin); + if (key.length() < 2) { + sb.append(key.toUpperCase()); + } else { + String first = String.valueOf(key.charAt(0)); + sb.append(first.toUpperCase()); + sb.append(key.substring(1, key.length())); + } + return sb.toString(); + } + + /** + * @Author: shane + * @Time: 2017/3/4 23:55 + * @Email: stevenchenguang@gmail.com + * @param: actionName, node + * @Return: Action + * @Throw: + * @Desc: 根据actionName和xml节点获取Action + */ + private static Action getAction(String actionName, Element node) { + Action action = new Action(); + + Iterator iterator = node.elementIterator(); + boolean flag = false; + while (iterator.hasNext()) { + Element e = iterator.next(); + List list = e.attributes(); + + //遍历属性节点 + for (Attribute attr : list) { + if ("name".equalsIgnoreCase(attr.getName())) { + if (!actionName.equalsIgnoreCase(attr.getValue())) { + continue; + } else { + flag = true; + } + action.setName(attr.getValue()); + } + if ("class".equalsIgnoreCase(attr.getName())) { + action.setClassPath(attr.getValue()); + } + + List results = new ArrayList<>(); + + Iterator it = e.elementIterator(); + while (it.hasNext()) { + Result result = new Result(); + Element el = it.next(); + result.setResult(el.attribute(0).getValue()); + result.setJumpPath(el.getText()); + results.add(result); + } + action.setResults(results); + } + if (flag) { + break; + } + } + return action; + } + +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java b/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..db1b37de1b --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java @@ -0,0 +1,109 @@ +package study.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); //密码和预设的不一致 + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + @Test + public void testLogoutSuccess() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name", "test"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("test logout success", view.getParameters().get("message")); + } + + @Test + public void testLogoutFail() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name", "unknownUser"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("unknownUser logout fail", view.getParameters().get("message")); + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/View.java b/group16/214074094/src/main/java/study/coderising/litestruts/View.java new file mode 100644 index 0000000000..af3df5b9e9 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package study.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/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java new file mode 100644 index 0000000000..12aa461cfc --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java @@ -0,0 +1,43 @@ +package study.coderising.litestruts.bean; + + +import java.util.List; + +/** + * @Author shane + * @Time 2017/3/4 21:49 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class Action { + + private String name; + + private String classPath; + + private List results; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassPath() { + return classPath; + } + + public void setClassPath(String classPath) { + this.classPath = classPath; + } + + public List getResults() { + return results; + } + + public void setResults(List results) { + this.results = results; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java new file mode 100644 index 0000000000..649844ff31 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java @@ -0,0 +1,30 @@ +package study.coderising.litestruts.bean; + +/** + * @Author shane + * @Time 2017/3/4 21:50 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class Result { + + private String result; + + private String jumpPath; + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + + public String getJumpPath() { + return jumpPath; + } + + public void setJumpPath(String jumpPath) { + this.jumpPath = jumpPath; + } +} diff --git a/group16/214074094/src/com/coding/basic/ArrayList.java b/group16/214074094/src/main/java/study/coding/basic/ArrayList.java similarity index 84% rename from group16/214074094/src/com/coding/basic/ArrayList.java rename to group16/214074094/src/main/java/study/coding/basic/ArrayList.java index 158c866d45..ebe04dd177 100644 --- a/group16/214074094/src/com/coding/basic/ArrayList.java +++ b/group16/214074094/src/main/java/study/coding/basic/ArrayList.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; import java.util.Arrays; @@ -10,7 +10,7 @@ * @Email stevenchenguang@gmail.com * @Desc OwnArrayList */ -public class ArrayList implements List { +public class ArrayList implements List { private int size = 0; @@ -28,22 +28,22 @@ public ArrayList() { } @Override - public void add(Object o) { + public void add(E e) { if (elementData == EMPTY_ELEMENTDATA) { elementData = Arrays.copyOf(elementData, DEFAULT_CAPACITY); - elementData[0] = o; + elementData[0] = e; } else if (size < elementData.length) { - elementData[size] = o; + elementData[size] = e; } else { _grow(); - elementData[size] = o; + elementData[size] = e; } size++; _analyze(); } @Override - public void add(int index, Object o) { + public void add(int index, E e) { if (index < 0) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } @@ -52,40 +52,40 @@ public void add(int index, Object o) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } else { elementData = new Object[DEFAULT_CAPACITY]; - elementData[0] = o; + elementData[0] = e; } } else if (index > size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } else if (index == size) { _grow(); - elementData[size] = o; + elementData[size] = e; size++; } else { if (elementData.length == size) { _grow(); } System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; + elementData[index] = e; size++; } _analyze(); } @Override - public Object get(int index) { - if (index < 0 || index > size) { + public E get(int index) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } - return elementData[index]; + return (E) elementData[index]; } @Override - public Object remove(int index) { + public E remove(int index) { - if (index < 0 || index > size) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } - Object oldValue = elementData[index]; + E oldValue = (E) elementData[index]; //需要复制的长度 int needMoveLength = size - index - 1; //如果该长度小于0, 说明只有一个元素, 直接置空即可 @@ -102,6 +102,11 @@ public int size() { return size; } + @Override + public E[] toArray() { + return (E[]) elementData; + } + public Iterator iterator() { return new ArrayListItrator(); } diff --git a/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java b/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4eae81c97a --- /dev/null +++ b/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java @@ -0,0 +1,95 @@ +package study.coding.basic; + +/** + * @Author shane + * @Time 2017/2/26 19:30 + * @Email stevenchenguang@gmail.com + * @Desc Own BinaryTreeNode + */ +public class BinaryTreeNode { + + private Object data; + + private BinaryTreeNode left; + + private BinaryTreeNode right; + + public BinaryTreeNode insert(Object o) { + if (null == data) { + data = o; + return this; + } + if (bigger(data, o)) { + if (null == left) { + left = new BinaryTreeNode(); + left.data = o; + } else { + left.insert(o); + } + } else if (smaller(data, o)) { + if (null == right) { + right = new BinaryTreeNode(); + right.data = o; + } else { + right.insert(o); + } + } else { + throw new RuntimeException("The value has exists"); + } + return this; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + private boolean bigger(Object data1, Object data2) { + return data1.toString().compareTo(data2.toString()) > 0; + } + + private boolean smaller(Object data1, Object data2) { + return data1.toString().compareTo(data2.toString()) < 0; + } + + private ArrayList list = new ArrayList(); + + /** + * 对二叉树进行遍历 结果存储到list中 + */ + private void sort(BinaryTreeNode node) { + + list.add(node.data); + if(null != node.left){ + sort(node.left); + } + if(null != node.right){ + sort(node.right); + } + } + + @Override + public String toString() { + sort(this); + return list.toString(); + } +} diff --git a/group16/214074094/src/com/coding/basic/Iterator.java b/group16/214074094/src/main/java/study/coding/basic/Iterator.java similarity index 73% rename from group16/214074094/src/com/coding/basic/Iterator.java rename to group16/214074094/src/main/java/study/coding/basic/Iterator.java index 1acc5349a4..0befe6a209 100644 --- a/group16/214074094/src/com/coding/basic/Iterator.java +++ b/group16/214074094/src/main/java/study/coding/basic/Iterator.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; public interface Iterator { diff --git a/group16/214074094/src/com/coding/basic/LinkedList.java b/group16/214074094/src/main/java/study/coding/basic/LinkedList.java similarity index 97% rename from group16/214074094/src/com/coding/basic/LinkedList.java rename to group16/214074094/src/main/java/study/coding/basic/LinkedList.java index 9108c2b6fe..b6fede6824 100644 --- a/group16/214074094/src/com/coding/basic/LinkedList.java +++ b/group16/214074094/src/main/java/study/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane @@ -50,6 +50,7 @@ public Object remove(int index) { } else if (index == size - 1) { return removeLast(); } + Node curr = _node(index); Object data = curr.data; final Node prev = curr.prev; @@ -120,6 +121,11 @@ public int size() { return size; } + @Override + public Object[] toArray() { + return new Object[0]; + } + public Iterator iterator() { return null; } diff --git a/group16/214074094/src/main/java/study/coding/basic/List.java b/group16/214074094/src/main/java/study/coding/basic/List.java new file mode 100644 index 0000000000..e62dd86d52 --- /dev/null +++ b/group16/214074094/src/main/java/study/coding/basic/List.java @@ -0,0 +1,16 @@ +package study.coding.basic; + +public interface List { + + void add(E e); + + void add(int index, E e); + + E get(int index); + + E remove(int index); + + int size(); + + E[] toArray(); +} diff --git a/group16/214074094/src/com/coding/basic/Queue.java b/group16/214074094/src/main/java/study/coding/basic/Queue.java similarity index 95% rename from group16/214074094/src/com/coding/basic/Queue.java rename to group16/214074094/src/main/java/study/coding/basic/Queue.java index 869d0f7333..b15950ac36 100644 --- a/group16/214074094/src/com/coding/basic/Queue.java +++ b/group16/214074094/src/main/java/study/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane diff --git a/group16/214074094/src/com/coding/basic/Stack.java b/group16/214074094/src/main/java/study/coding/basic/Stack.java similarity index 95% rename from group16/214074094/src/com/coding/basic/Stack.java rename to group16/214074094/src/main/java/study/coding/basic/Stack.java index 7ef1c9ad06..188de74ee9 100644 --- a/group16/214074094/src/com/coding/basic/Stack.java +++ b/group16/214074094/src/main/java/study/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane diff --git a/group16/214074094/src/main/java/study/reading/blog.txt b/group16/214074094/src/main/java/study/reading/blog.txt new file mode 100644 index 0000000000..f112f8ea6a --- /dev/null +++ b/group16/214074094/src/main/java/study/reading/blog.txt @@ -0,0 +1 @@ +学习-CPU、内存、硬盘、指令及它们之间的关系: https://stevenshane.github.io/study/2017/02/27/coding2017-reading.html \ No newline at end of file diff --git a/group16/214074094/src/main/resources/struts.xml b/group16/214074094/src/main/resources/struts.xml new file mode 100644 index 0000000000..f93213e241 --- /dev/null +++ b/group16/214074094/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/group16/214074094/src/test/coding/basic/AbstractTest.java b/group16/214074094/src/test/java/study/AbstractTest.java similarity index 71% rename from group16/214074094/src/test/coding/basic/AbstractTest.java rename to group16/214074094/src/test/java/study/AbstractTest.java index 80eaaa6fe5..42737e270f 100644 --- a/group16/214074094/src/test/coding/basic/AbstractTest.java +++ b/group16/214074094/src/test/java/study/AbstractTest.java @@ -1,4 +1,6 @@ -package coding.basic; +package study; + +import com.alibaba.fastjson.JSON; /** * @Author shane @@ -16,4 +18,7 @@ protected void printHyphen() { System.out.println("--------------------------------------------"); } + protected void printJson(Object obj) { + System.out.println(JSON.toJSONString(obj)); + } } diff --git a/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java b/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java new file mode 100644 index 0000000000..24cdeadb64 --- /dev/null +++ b/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java @@ -0,0 +1,82 @@ +package study.coderising; + +import org.junit.Assert; +import org.junit.Test; +import study.AbstractTest; +import study.coderising.array.ArrayUtil; + +/** + * @Author shane + * @Time 2017/3/1 20:29 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class ArrayUtilTest extends AbstractTest { + + @Test + public void testReverseArray(){ + int[] a = {7, 9 , 30, 3}; + printJson(a); + ArrayUtil.reverseArray(a); + printJson(a); + } + + @Test + public void testremoveZero(){ + int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int newArr[] = ArrayUtil.removeZero(oldArr); + printJson(oldArr); + printJson(newArr); + } + + @Test + public void testMerge(){ + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + + int[] a3 = ArrayUtil.merge(a1, a2); + printJson(a1); + printJson(a2); + printJson(a3); + } + + @Test + public void testGrow(){ + int[] oldArray = {2,3,6}; + printJson(oldArray); + int[] newArray = ArrayUtil.grow(oldArray, 3); + printJson(newArray); + } + + @Test + public void testFibonacci(){ + int[] onlyOne = {}; + int[] before15 = {1, 1, 2, 3, 5, 8, 13}; + int[] before21 = {1, 1, 2, 3, 5, 8, 13}; + Assert.assertArrayEquals(onlyOne, ArrayUtil.fibonacci(1)); + Assert.assertArrayEquals(before15, ArrayUtil.fibonacci(15)); + Assert.assertArrayEquals(before21, ArrayUtil.fibonacci(21)); + } + + @Test + public void testGetPrimes(){ + int[] _19 = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(_19, ArrayUtil.getPrimes(23)); + int[] _32 = {2,3,5,7,11,13,17,19,23,29,31}; + Assert.assertArrayEquals(_32, ArrayUtil.getPrimes(32)); + } + + @Test + public void testGetPerfectNumbers(){ + int[] arr = {6,28,496,8128,33550336}; + Assert.assertArrayEquals(arr, ArrayUtil.getPerfectNumbers(33550337)); + } + + @Test + public void testJoin(){ + int[] arr = {3,8,9}; + String seperator = "-"; + Assert.assertEquals("3-8-9", ArrayUtil.join(arr, seperator)); + } + +} diff --git a/group16/214074094/src/test/coding/basic/ArrayListTest.java b/group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java similarity index 96% rename from group16/214074094/src/test/coding/basic/ArrayListTest.java rename to group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java index 2f03342d61..6ffe2e5715 100644 --- a/group16/214074094/src/test/coding/basic/ArrayListTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java b/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..370b339434 --- /dev/null +++ b/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,33 @@ +package study.coding.basic; + +import org.junit.Test; +import study.AbstractTest; + +/** + * @Author shane + * @Time 2017/2/26 19:57 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class BinaryTreeNodeTest extends AbstractTest { + + @Test + public void test(){ + BinaryTreeNode node = new BinaryTreeNode(); + node.insert(8); + node.insert(5); + node.insert(9); + node.insert(1); + node.insert(6); + node.insert(11); + node.insert(10); + node.insert(15); + node.insert(13); + node.insert(19); + + printStar(); + System.out.println(node.getData()); + System.out.println(node); + printStar(); + } +} diff --git a/group16/214074094/src/test/coding/basic/LinkedListTest.java b/group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java similarity index 96% rename from group16/214074094/src/test/coding/basic/LinkedListTest.java rename to group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java index bc78728a25..f22d1cc611 100644 --- a/group16/214074094/src/test/coding/basic/LinkedListTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/coding/basic/QueueTest.java b/group16/214074094/src/test/java/study/coding/basic/QueueTest.java similarity index 95% rename from group16/214074094/src/test/coding/basic/QueueTest.java rename to group16/214074094/src/test/java/study/coding/basic/QueueTest.java index 12302783b3..cdfe5c95ab 100644 --- a/group16/214074094/src/test/coding/basic/QueueTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/QueueTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/coding/basic/StackTest.java b/group16/214074094/src/test/java/study/coding/basic/StackTest.java similarity index 95% rename from group16/214074094/src/test/coding/basic/StackTest.java rename to group16/214074094/src/test/java/study/coding/basic/StackTest.java index f289744a67..8269967dff 100644 --- a/group16/214074094/src/test/coding/basic/StackTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/StackTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/2816977791/.gitignore b/group16/2816977791/.gitignore new file mode 100644 index 0000000000..4ede1404d2 --- /dev/null +++ b/group16/2816977791/.gitignore @@ -0,0 +1,181 @@ +# Created by https://www.gitignore.io/api/java,intellij,eclipse,emacs,svn,maven + +### Java ### +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/workspace.xml +.idea/tasks.xml +.idea/dictionaries +.idea/vcs.xml +.idea/jsLibraryMappings.xml +*.idea + +# Sensitive or high-churn files: +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml + +# Gradle: +.idea/gradle.xml +.idea/libraries + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +*.iml + + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +### SVN ### +.svn/ + + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +Servers/ diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java b/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7ac196b700 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java @@ -0,0 +1,94 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkForLength(index); + + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkForLength(index); + return elementData[index]; + } + + public Object remove(int index) { + checkForLength(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return oldValue; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private void checkForLength(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("out of memory"); + } + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1);//增大容量 + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } + elementData = copyOf(elementData, newCapacity); + } + + private Object[] copyOf(Object[] src, int newCapacity) { + Object[] target = new Object[newCapacity]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + String num = "num"; + for (int i = 0; i < 100; i++) { + list.add(num + String.valueOf(i)); + System.out.println(String.valueOf(i) + ":size:" + list.size()); + System.out.println(String.valueOf(i) + ":length:" + list.elementData.length); + } + System.out.println(list.size()); + + for (int i = 0; i < 100; i++) { + list.add(i, num + String.valueOf(i)); + System.out.println(String.valueOf(i) + ":size:" + list.size()); + System.out.println(String.valueOf(i) + ":length:" + list.elementData.length); + } + System.out.println(list.size()); + + for (int i = 0; i < 200; i++) { + System.out.println(list.remove(0)); + } + + System.out.println(list.size()); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java b/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e34a68b3c2 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(o, null, null); + if (root == null) { + root = binaryTreeNode; + } else { + add(o, root); + } + return binaryTreeNode; + } + + private BinaryTreeNode add(Object o, BinaryTreeNode target) { + if (target == null) { + target = new BinaryTreeNode(o, null, null); + } else { + if (compare(o, target.data) > 0) { + target.right = add(o, target.right); + } else if (compare(o, target.data) < 0) { + target.left = add(o, target.left); + } + } + return target; + } + + private int compare(Object src, Object target) { + return ((String) src).compareTo((String) target); + } + +} diff --git a/group16/542087872/src/com/coding/basic/Iterator.java b/group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java similarity index 100% rename from group16/542087872/src/com/coding/basic/Iterator.java rename to group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java b/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..be5b236700 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node last; + + private int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkPosition(index); + if (index == size) { + addLast(o); + } else { + addIndex(index, o); + } + } + + public Object get(int index) { + return node(index).data; + } + + public Object remove(int index) { + checkPosition(index); + Object old = get(index); + if (index == 0) { + removeFirst(); + } else if (index == size - 1) { + removeLast(); + } else { + node(index - 1).next = node(index + 1); + size--; + } + return old; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node h = head; + Node newNode = new Node(o, h); + head = newNode; + if (h == null) { + last = newNode; + } + size++; + } + + public void addLast(Object o) { + Node l = last; + Node newNode = new Node(o, null); + last = newNode; + if (l == null) { + head = newNode; + } else { + l.next = newNode; + } + size++; + } + + public Object removeFirst() { + Node old = head; + head = old.next; + size--; + return old.data; + } + + public Object removeLast() { + Node old = last; + Node prev = node(size - 2); + last = prev; + size--; + return old.data; + } + + public Iterator iterator() { + return null; + } + + private void checkPosition(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("out of memory"); + } + } + + private void addIndex(int index, Object o) { + checkPosition(index); + Node newNode = new Node(o, node(index)); + if (index != 0) { + node(index - 1).next = newNode; + } else { + head = newNode; + } + size++; + } + + private Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.addLast("last"); + System.out.println(list.size()); + list.addFirst("head"); + System.out.println(list.size()); + list.add(0, "0Object"); + System.out.println(list.size()); + list.add(2, "2Object"); + System.out.println(list.size()); + list.removeLast(); + System.out.println(list.size()); + list.removeFirst(); + System.out.println(list.size()); + list.removeLast(); + System.out.println(list.size()); + list.get(0); + System.out.println(list.size()); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/List.java b/group16/2816977791/firstExercise/src/com/coding/basic/List.java new file mode 100644 index 0000000000..01398944e6 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java b/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..f2475fcfd6 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java b/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..12a89eb613 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(0, o); + } + + public Object pop() { + return elementData.remove(0); + } + + public Object peek() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java b/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..3572229739 --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,258 @@ +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 i = 0; + while (i < origin.length - 1 - i) { + int temp = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = origin[i]; + origin[i] = 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 int[] removeZero(int[] oldArray) { + int point = 0; + for (int i : oldArray) { + if (i != 0) { + oldArray[point++] = i; + } + } + int[] newArray = new int[point]; + System.arraycopy(oldArray, 0, newArray, 0, point); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + //两个指针 + int pos1 = 0; + int pos2 = 0; + int point = 0; + int[] newArray = new int[array1.length + array2.length]; + while (pos1 < array1.length && pos2 < array2.length) { + if (array1[pos1] > array2[pos2]) { + newArray[point++] = array2[pos2++]; + } else if (array1[pos1] < array2[pos2]) { + newArray[point++] = array1[pos1++]; + } else { + newArray[point++] = array1[pos1++]; + pos2++; + } + } + while (pos1 < array1.length) { + newArray[point++] = array1[pos1++]; + } + while (pos1 < array2.length) { + newArray[point++] = array1[pos2++]; + } + int[] array = new int[point]; + System.arraycopy(newArray, 0, array, 0, point); + 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[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + 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 int[] fibonacci(int max) { + int a = 0; + int b = 1; + int[] array = new int[max]; + int i = 0; + if (max >= 2) { + array[i++] = 1; + } + while (a + b < max) { + int temp = b; + b = a + b; + a = temp; + array[i++] = b; + } + int[] newArray = new int[i]; + System.arraycopy(array, 0, newArray, 0, i); + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] array = new int[max / 2 + 1]; + int pos = 0; + for (int i = 1; i < max; i++) { + if (prime(i)) { + array[pos++] = i; + } + } + int[] newArray = new int[pos]; + System.arraycopy(array, 0, newArray, 0, pos); + return newArray; + } + + private boolean prime(int value) { + if (value < 2) { + return false; + } else if (value == 2) { + return true; + } else { + for (int i = 2; i < value / 2 + 1; i++) { + if (value % 2 == 0) { + return false; + } + } + return true; + } + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int pos = 0; + int[] array = new int[max]; + for (int i = 1; i < max; i++) { + if (perfectNumber(i)) { + array[pos++] = i; + } + } + int[] newArray = new int[pos]; + System.arraycopy(array, 0, newArray, 0, pos); + return newArray; + } + + private boolean perfectNumber(int value) { + if (value == 1 || prime(value)) { + return false; + } else { + int sum = 0; + for (int i = 1; i <= value / 2; i++) { + if (value % i == 0) { + sum += i; + } + } + if (sum == value) { + return true; + } + return false; + } + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + String out = ""; + for (int i = 0; i < array.length; i++) { + if (i == 0) { + out += String.valueOf(i); + } else { + out += seperator + String.valueOf(i); + } + } + return out; + } + + + public static void main(String[] args) { + ArrayUtil arrayUtil = new ArrayUtil(); + //reverse + int[] a = {7, 9, 30, 3}; + arrayUtil.reverseArray(a); + System.out.println(a); + + //remove zero + int[] zero = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int[] afterZero = arrayUtil.removeZero(zero); + System.out.println(afterZero); + + //merge + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] merge = arrayUtil.merge(a1, a2); + System.out.println(merge); + + //grow + int[] oldArray = {2, 3, 6}; + int[] grow = arrayUtil.grow(oldArray, 3); + System.out.println(grow); + + //fibonacci + int[] fibonacci = arrayUtil.fibonacci(2); + System.out.println(fibonacci); + + //primes + int[] primes = arrayUtil.getPrimes(15); + System.out.println(primes); + + //perfect + int[] perfect = arrayUtil.getPerfectNumbers(500); + System.out.println(perfect); + + //join + int[] joinArray = {2}; + String join = arrayUtil.join(joinArray, "-"); + System.out.println(join); + + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java new file mode 100644 index 0000000000..55988befbe --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.util.Map; + +/** + * @author nvarchar + * date 2017/3/1 + */ +public class ActionXml { + private String name; + private String className; + private Map map; + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group16/2816977791/secondExercise/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/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..f169cb1cbb --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,150 @@ +package com.coderising.litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + private static Map actionXmlMap; + + static { + //0. 读取配置文件struts.xml + actionXmlMap = parserXml("src/com/coderising/litestruts/struts.xml"); + System.out.println("parse end"); + } + + public static View runAction(String actionName, Map parameters) { + + try { + ActionXml actionXml = actionXmlMap.get(actionName); + if (actionName == null) { + return null; + } else { + String className = actionXml.getClassName(); + Class cls = Class.forName(className); + Object obj = cls.newInstance(); + /** + 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + */ + Method[] methods = cls.getMethods(); + for (String field : parameters.keySet()) { + for (Method method : methods) { + if (method.getName().startsWith("set") && method.getName().toLowerCase().endsWith(field.toLowerCase())) { + method.invoke(obj, parameters.get(field)); + break; + } + } + } + Method action = cls.getMethod("execute"); + //通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + String result = (String) action.invoke(obj); + /** + 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + */ + Field[] fields = cls.getDeclaredFields(); + Map params = new HashMap<>(); + for (Field field : fields) { + for (Method method : methods) { + if (method.getName().startsWith("get") && method.getName().toLowerCase().endsWith(field.getName().toLowerCase())) { + String viewResult = (String) method.invoke(obj); + params.put(field.getName(), viewResult); + break; + } + } + } + /** + 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + View view = new View(); + view.setParameters(params); + view.setJsp(actionXmlMap.get(actionName).getMap().get(result)); + return view; + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return null; + } catch (InstantiationException e) { + e.printStackTrace(); + return null; + } catch (IllegalAccessException e) { + e.printStackTrace(); + return null; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return null; + } catch (InvocationTargetException e) { + e.printStackTrace(); + return null; + } + } + + //解析xml文件 + private static Map parserXml(String fileName) { + try { + Map map = new HashMap<>(); + //create documentBuilder + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + //create document + Document document = db.parse(fileName); + //extract root element + Element root = document.getDocumentElement(); + System.out.println("Root element :" + document.getDocumentElement().getNodeName()); + NodeList nodeList = document.getElementsByTagName("action"); + System.out.println("----------------------------"); + for (int temp = 0; temp < nodeList.getLength(); temp++) { + Node nNode = nodeList.item(temp); + System.out.println("\nCurrent Element :" + + nNode.getNodeName()); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + ActionXml actionXml = new ActionXml(); + Element eElement = (Element) nNode; + System.out.println("action name : " + + eElement.getAttribute("name")); + System.out.println("class name : " + + eElement.getAttribute("class")); + actionXml.setName(eElement.getAttribute("name")); + actionXml.setClassName(eElement.getAttribute("class")); + NodeList result = eElement.getElementsByTagName("result"); + Map results = new HashMap<>(); + for (int i = 0; i < result.getLength(); i++) { + Node resultNode = result.item(i); + if (resultNode.getNodeType() == Node.ELEMENT_NODE) { + Element resultElement = (Element) resultNode; + System.out.println("result name:" + resultElement.getAttribute("name")); + System.out.println("result context:" + resultElement.getTextContent()); + results.put(resultElement.getAttribute("name"), resultElement.getTextContent()); + } + actionXml.setMap(results); + } + map.put(actionXml.getName(), actionXml); + } + } + return map; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static void main(String[] args) { +// parserXml("src/com/coderising/litestruts/struts.xml"); + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group16/2816977791/secondExercise/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/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/2816977791/secondExercise/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/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml b/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group16/2816977791/secondExercise/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/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java index eaaa690fa6..3bec144013 100644 Binary files a/group16/313001956/src/com/coding/basic/ArrayList.java and b/group16/313001956/src/com/coding/basic/ArrayList.java differ diff --git a/group16/313001956/src/com/coding/basic/LinkedList.java b/group16/313001956/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..de886c9084 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Խ磡"); + } + if (head == null) { + head = new Node(); + head.data = o; + } else { + + Node n = getNodebyIndex(index); + + Node newnode = new Node(); + newnode.data = o; + newnode.next = n.next; + n.next = newnode; + } + size++; + } + + public Object get(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Խ磡"); + } + if (head == null) + return null; + Node n = getNodebyIndex(index); + return n.data; + } + + public Object remove(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Խ磡"); + } + Node n = getNodebyIndex(index - 1); + Object o = n.next.data; + n.next = n.next.next; + size--; + return o; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + } else { + Node newnode = new Node(); + newnode.data = o; + newnode.next = head; + head = newnode; + } + size++; + } + + public void addLast(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + } else { + Node n = getNodebyIndex(size); + Node newnode = new Node(); + newnode.data = o; + n.next = newnode; + } + size++; + } + + public Object removeFirst() { + if (head == null) + return null; + + head = head.next; + size--; + return head.data; + } + + public Object removeLast() { + if (head == null) + return null; + Node n = getNodebyIndex(size - 1); + Object o = n.next.data; + n.next = null; + size--; + return o; + } + + public Iterator iterator() { + return null; + } + + private Node getNodebyIndex(int index) { + int i = 0; + Node n = head; + while (i < index) { + n = n.next; + i++; + } + return n; + } + + private static class Node { + Object data; + Node next; + + Object Getdata() { + return data; + } + + void Setdata(Object o) { + data = o; + } + + Node Getnext() { + return next; + } + + void Setnext(Node n) { + next = n; + } + } +} + diff --git a/group16/313001956/src/com/coding/basic/Queue.java b/group16/313001956/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4b9f311f99 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + Object o= elementData.get(0); + elementData.removeFirst(); + return o; + } + + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/313001956/src/com/coding/basic/Stack.java b/group16/313001956/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..6ec157201e --- /dev/null +++ b/group16/313001956/src/com/coding/basic/Stack.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public Stack() { + // TODO Auto-generated constructor stub + } + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + int size = elementData.size(); + Object o = elementData.get(size - 1); + elementData.remove(size - 1); + return o; + } + + public Object peek() { + int size = elementData.size(); + Object o = elementData.get(size - 1); + + return o; + } + + public boolean isEmpty() { + if (elementData.size() == 0) + return true; + return false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/420355244/Homework1/.classpath b/group16/420355244/Homework1/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group16/420355244/Homework1/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group16/420355244/Homework1/.gitignore b/group16/420355244/Homework1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/420355244/Homework1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/420355244/Homework1/.project b/group16/420355244/Homework1/.project new file mode 100644 index 0000000000..ec1134f33a --- /dev/null +++ b/group16/420355244/Homework1/.project @@ -0,0 +1,17 @@ + + + Homework1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs b/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java b/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..6534c3c029 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,91 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size < elementData.length){ + elementData[size] = o; + }else{ + Object[] newElementData = new Object[elementData.length + elementData.length/2]; + System.arraycopy(elementData, 0, newElementData, 0, size); + newElementData[size] = o; + this.elementData = newElementData; + } + size++; + + } + public void add(int index, Object o){ + if(index >= 0 && index <= size){ + //1.不扩容 + if(index == size - 1){ + //1.1 加在最后 + elementData[index] = o; + }else{ + //1.2 加在前面 + //index的位置的数值变为改对象,index以后位置的都往后挪一位 + Object[] newElementData = new Object[elementData.length]; + System.arraycopy(elementData, 0, newElementData, 0, index); + newElementData[index] = o ; + System.arraycopy(elementData, index, newElementData, index + 1, size - index); + this.elementData = newElementData; + } + size++; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public Object get(int index){ + if(index < size){ + return elementData[index]; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public Object remove(int index){ + if(index < size){ + Object obj = elementData[index]; + Object[] newElementData = new Object[elementData.length]; + if(size != 1){ + //1.若集合长度为1 + if(0 == index){ + //1.1.如果remove的是0索引的 + System.arraycopy(elementData, 1, newElementData, 0, size - 1); + }else if(index == size -1){ + //1.2.如果remove的是最后索引的 + System.arraycopy(elementData, 0, newElementData, 0, size - 1); + }else{ + //1.3.在中间 + System.arraycopy(elementData, 0, newElementData, 0, index); + System.arraycopy(elementData, index + 1, newElementData, index, size - index - 1); + } + } + this.elementData = newElementData; + size--; + return obj; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + @Override + public String toString() { + return "ArrayList [size=" + size + ", elementData=" + Arrays.toString(elementData) + "]"; + } + + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java b/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..420c412a7b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + private static ArrayList arrayList = new ArrayList(); + + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + for(int i = 0 ;i < 150; i++){ + arrayList.add("aaa"); + } + System.out.println(arrayList); + System.out.println(arrayList.size()); + } + + @Test + public void testAddIntObject() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + arrayList.add(1,"eee"); + System.out.println(arrayList); + } + + @Test + public void testGet() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + Object object = arrayList.get(0); + System.out.println(object); + } + + @Test + public void testRemove() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + arrayList.remove(0); + System.out.println(arrayList); + } + + @Test + public void testSize() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + System.out.println(arrayList.size()); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java b/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/Iterator.java b/group16/420355244/Homework1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java b/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..9af35a399d --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,224 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + + private Node first; + + private Node last; + + private int size = 0; + + public void add(Object o){ + if(null == first){ + //当链表元素为空时,新建一个Node + Node node = new Node(); + node.data = o; + node.next = null; + first = node; + last = node; + size ++; + }else{ + addLast(o); + } + } + public void add(int index , Object o){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果加在头上 + addFirst(o); + }else{ + //2.加在中间位置 + Node node = first.next; + int nodeIndex = 1; + if(nodeIndex == index){ + //如果是第二个位置的话 + Node nodeAdd = new Node(); + nodeAdd.data = o; + first.next = nodeAdd; + nodeAdd.next = node; + last = node; + size ++; + } + //第三个位置及以后、开始遍历所有的索引 + while(null != node.next){ + //保留遍历中node之前的结点 + Node nodeLast = node; + node = node.next; + nodeIndex++; + if(nodeIndex == index){ + Node nodeAdd = new Node(); + nodeAdd.data = o; + nodeLast.next = nodeAdd; + nodeAdd.next = node; + size ++; + break; + } + } + } + } + + } + public Object get(int index){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果加在头上 + return first.data; + } + Node node = first.next; + int nodeIndex = 1; + if(nodeIndex == index){ + //如果是第二个位置的话 + return node.data; + } + //第三个位置及以后、开始遍历所有的索引 + while(null != node.next){ + //保留遍历中node之前的结点 + node = node.next; + nodeIndex++; + if(nodeIndex == index){ + return node.data; + } + } + } + throw new IndexOutOfBoundsException(); + } + public Object remove(int index){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果移除头 + removeFirst(); + }else if(index == (size - 1)){ + //2.移除尾 + removeLast(); + }else{ + //3.移除中间位置 + Node node = first.next; + //从first的零号索引开始 + int nodeIndex = 1; + + //开始遍历所有的索引,记住要移除的索引位数据的前后结点 + Node lastNode = first; + if(index == nodeIndex){ + //第一次不匹配则后续的循环执行 + Object o = node.data; + lastNode.next = node.next; + size--; + return o; + }else{ + while(null != node.next){ + lastNode = node; + node = node.next; + nodeIndex++; + if(index == nodeIndex){ + Object o = node.data; + lastNode.next = node.next; + size--; + return o; + } + } + } + } + } + throw new IndexOutOfBoundsException(); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o ; + node.next = first; + first = node; + size++; + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o ; + node.next = null; + last.next = node; + last = node; + size++; + } + public Object removeFirst(){ + Object o = first.data; + Node node = first.next; + first = node; + size--; + return o; + } + public Object removeLast(){ + if(0 == size){ + throw new NoSuchElementException(); + + }else if(1 == size){ + //只有一个元素 + removeFirst(); + }else{ + //第二个元素 + Node node = first.next; + if(null == node.next){ + Object o = node.data; + last = first; + first.next = null; + return o; + }else{ + while(null != node.next){ + //若不止只有2个 ,记录最后一个结点的前一个。 + Node lastNode = node; + node = node.next; + if(null == node.next){ + Object o = node.data; + lastNode.next = null; + last = lastNode; + size--; + return o; + } + } + } + } + throw new NoSuchElementException(); + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if(null != first){ + sb.append(first.data.toString() + ","); + Node node = first.next; + sb.append(node.data.toString() + ","); + while(null != node.next){ + node = node.next; + sb.append(node.data.toString() + ","); + } + } + return sb.toString(); + } + + + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java b/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..2caea9679b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,112 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + private static LinkedList linkedList = new LinkedList(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + linkedList.add("aaa"); + linkedList.add("bbb"); + System.out.println(linkedList); + } + + @Test + public void testAddIntObject() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add(2,"ddd"); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + + @Test + public void testGet() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); +// System.out.println(linkedList.size()); + System.out.println(linkedList.get(3)); + } + + @Test + public void testRemove() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); + linkedList.remove(5); + linkedList.remove(1); + linkedList.remove(2); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + + @Test + public void testSize() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); + System.out.println(linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.addFirst("sss"); + System.out.println(linkedList); + } + + @Test + public void testAddLast() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + System.out.println(linkedList); + } + + @Test + public void testRemoveFirst() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.removeFirst(); + linkedList.addFirst("eee"); + linkedList.removeFirst(); + System.out.println(linkedList); + } + + @Test + public void testRemoveLast() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.removeLast(); + linkedList.add("eee"); + linkedList.addFirst("xxx"); + System.out.println(linkedList); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group16/542087872/src/com/coding/basic/List.java b/group16/420355244/Homework1/src/com/coding/basic/List.java similarity index 100% rename from group16/542087872/src/com/coding/basic/List.java rename to group16/420355244/Homework1/src/com/coding/basic/List.java diff --git a/group16/420355244/Homework1/src/com/coding/basic/Queue.java b/group16/420355244/Homework1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a2f9577b7b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Queue.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +public class Queue { + private Node first; + private Node last; + private int size = 0; + public void enQueue(Object o){ + if(null == first){ + Node node = new Node(); + node.data = o; + node.next = null; + first = node; + last = node; + }else{ + Node node = new Node(); + node.data = o; + node.next = null; + last.next = node; + last = node; + } + size++; + } + + public Object deQueue(){ + Node second = first.next; + Object o = first.data; + if(null != second){ + first = second; + return o; + }else{ + first = null; + size = 0; + return o; + } + } + + public boolean isEmpty(){ + if(size > 0){ + return false; + }else{ + return true; + } + } + + public int size(){ + return size; + } + static class Node{ + Node next; + Object data; + } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if(null != first){ + sb.append(first.data.toString() + ","); + Node node = first.next; + sb.append(node.data.toString() + ","); + while(null != node.next){ + node = node.next; + sb.append(node.data.toString() + ","); + } + } + return sb.toString(); + } +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java b/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..50d7fc0903 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + + public static Queue queue = new Queue(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testEnQueue() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("aaa"); + System.out.println(queue); + } + + @Test + public void testDeQueue() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("ddd"); + queue.enQueue("eee"); + queue.deQueue(); + System.out.println(queue); + } + + @Test + public void testIsEmpty() { + System.out.println(queue.isEmpty()); + + } + + @Test + public void testSize() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("ddd"); + queue.enQueue("eee"); + System.out.println(queue.size()); + } + + @Test + public void testToString() { + fail("Not yet implemented"); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/Stack.java b/group16/420355244/Homework1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..5c59bf34fc --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + public void push(Object o){ + //入栈在栈顶进入最后压入 + elementData.add(o); + size ++; + } + + public Object pop(){ + Object object = elementData.get(size -1); + elementData.remove(size -1); + size --; + return object; + } + + public Object peek(){ + Object object = elementData.get(size -1); + return object; + } + public boolean isEmpty(){ + if(size <= 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return size; + } + + @Override + public String toString() { + return elementData.toString(); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/StackTest.java b/group16/420355244/Homework1/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..b436785574 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/StackTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + private static Stack stack = new Stack(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testPush() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + System.out.println(stack); + } + + @Test + public void testPop() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + Object pop = stack.pop(); + System.out.println(pop); + System.out.println(stack); + } + + @Test + public void testPeek() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + Object peek = stack.peek(); + System.out.println(peek); + } + + @Test + public void testIsEmpty() { + System.out.println(stack.isEmpty()); + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + System.out.println(stack.isEmpty()); + stack.pop(); + stack.pop(); + stack.pop(); + System.out.println(stack.isEmpty()); + } + + @Test + public void testSize() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + stack.pop(); + stack.pop(); + System.out.println(stack.size()); + } + +} diff --git a/group16/420355244/Homework2/.classpath b/group16/420355244/Homework2/.classpath new file mode 100644 index 0000000000..f5d4a033ec --- /dev/null +++ b/group16/420355244/Homework2/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group16/420355244/Homework2/.gitignore b/group16/420355244/Homework2/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/420355244/Homework2/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/420355244/Homework2/.project b/group16/420355244/Homework2/.project new file mode 100644 index 0000000000..1a13fc592d --- /dev/null +++ b/group16/420355244/Homework2/.project @@ -0,0 +1,17 @@ + + + Homework2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java b/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5496d8084d --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5fd5f1efba --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,120 @@ +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){ + for(int i = 0;i < origin.length/2; i++){ + int x = origin[i]; + origin[i] = origin[origin.length - i -1]; + origin[origin.length - i -1] = x; + } + } + + /** + * 现在有如下的一个数组: 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){ + for(int i = 0;i < oldArray.length ;i++){ + if(oldArray[i] == 0){ + int[] a = {}; + System.arraycopy(oldArray, 0, a, 0, i); + System.arraycopy(oldArray, 0, a, i, oldArray.length); + oldArray = a; + removeZero(oldArray); + } + } + return oldArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + return null; + } + + public static void main(String[] args) { + /*int[] a = {7, 9 , 30, 3}; + reverseArray(a); + for (int i : a) { + System.out.print(i+","); + }*/ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ; + removeZero(oldArr); + for (int i : oldArr) { + System.out.print(i+","); + } + } +} diff --git a/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java b/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..40af955dfa --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,133 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据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. 读取配置文件struts.xml + SAXReader reader = new SAXReader(); + try { + //0.2 读取文件 + Document doc = reader.read(new File("./src/com/coderising/litestruts/struts.xml")); + //0.3 得到根标签 + Element rootElement = doc.getRootElement(); + //0.4 得到根标签下的所有action标签 + Iterator elementIterator = rootElement.elementIterator("action"); + while(elementIterator.hasNext()){ + Element element = elementIterator.next(); + String nameValue = element.attributeValue("name"); + try { + if(null != actionName && actionName.trim() != ""){ + if(actionName.equals(nameValue)){ + View view = new View(); + //进入该action标签内,结束后停止循环 + String classValue = element.attributeValue("class"); + //1.1 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)据parameters中的数据 + Class clazz =Class.forName(classValue); + Object instance = clazz.newInstance(); + Method[] methods = clazz.getMethods(); + for (Entry entry : parameters.entrySet()) { + String methodName = "set" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); + //1.2调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , "password"="1234") ,那就应该调用 setName和setPassword方法 + for (Method setterMethod : methods) { + if(methodName.equals(setterMethod.getName())){ + setterMethod.invoke(instance,entry.getValue()); + break; + } + } + } + //2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method method = clazz.getMethod("execute", null); + Object exectueResult = method.invoke(instance, null); + Iterator resultElement = element.elementIterator("result"); + while(resultElement.hasNext()){ + Element result = resultElement.next(); + if(exectueResult.equals(result.attributeValue("name"))){ + String jsp = result.getText(); + view.setJsp(jsp); + break; + } + } + /*3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters*/ + HashMap hashMap = new HashMap<>(); + for (Method getterMethod : methods) { + if(getterMethod.getName().contains("get")){ + Object resultValue = getterMethod.invoke(instance,null); + String resultKey = getterMethod.getName().replace("get", "").substring(0,1).toLowerCase() + + getterMethod.getName().replace("get", "").substring(1); + hashMap.put(resultKey, resultValue); + } + } + view.setParameters(hashMap); + return view; + } + } + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + public static void main(String[] args) { + runAction("login",null); + } + +} diff --git a/group16/420355244/Homework2/src/com/coderising/litestruts/StrutsTest.java b/group16/420355244/Homework2/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group16/420355244/Homework2/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/group16/420355244/Homework2/src/com/coderising/litestruts/View.java b/group16/420355244/Homework2/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/420355244/Homework2/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/group16/420355244/Homework2/src/com/coderising/litestruts/struts.xml b/group16/420355244/Homework2/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group16/420355244/Homework2/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/group16/502059278/.classpath b/group16/502059278/.classpath index fb5011632c..c0abaf014f 100644 --- a/group16/502059278/.classpath +++ b/group16/502059278/.classpath @@ -2,5 +2,7 @@ + + diff --git "a/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" "b/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" deleted file mode 100644 index 31dfe4c14b..0000000000 Binary files "a/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" and /dev/null differ diff --git a/group16/502059278/src/cn/mark/MyArrayList.java b/group16/502059278/src/cn/mark/work0219/MyArrayList.java similarity index 99% rename from group16/502059278/src/cn/mark/MyArrayList.java rename to group16/502059278/src/cn/mark/work0219/MyArrayList.java index 9e0e406274..3d7a064919 100644 --- a/group16/502059278/src/cn/mark/MyArrayList.java +++ b/group16/502059278/src/cn/mark/work0219/MyArrayList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; import java.util.ArrayList; import java.util.Arrays; diff --git a/group16/502059278/src/cn/mark/MyLinkedList.java b/group16/502059278/src/cn/mark/work0219/MyLinkedList.java similarity index 97% rename from group16/502059278/src/cn/mark/MyLinkedList.java rename to group16/502059278/src/cn/mark/work0219/MyLinkedList.java index 7f9c3856a2..896d7bcb79 100644 --- a/group16/502059278/src/cn/mark/MyLinkedList.java +++ b/group16/502059278/src/cn/mark/work0219/MyLinkedList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; /** * 自定义实现LinkedList数据结构 * @author hilih diff --git a/group16/502059278/src/cn/mark/MyList.java b/group16/502059278/src/cn/mark/work0219/MyList.java similarity index 96% rename from group16/502059278/src/cn/mark/MyList.java rename to group16/502059278/src/cn/mark/work0219/MyList.java index 19bc3f92fe..91f26ff8d5 100644 --- a/group16/502059278/src/cn/mark/MyList.java +++ b/group16/502059278/src/cn/mark/work0219/MyList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; public interface MyList { /** diff --git a/group16/502059278/src/cn/mark/work0226/ArrayUtil.java b/group16/502059278/src/cn/mark/work0226/ArrayUtil.java new file mode 100644 index 0000000000..94e253e8bb --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/ArrayUtil.java @@ -0,0 +1,166 @@ +package cn.mark.work0226; + +import java.util.Arrays; + +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[] target = new int[origin.length];//声明置换后数组 + int temp = target.length - 1;//记录置换后下标位置 + for( int i = 0; i < origin.length; i++ ){ + target[temp] = origin[i]; + temp--; + } + System.out.println("置换前:"+Arrays.toString(origin)); + System.out.println("置换后:"+Arrays.toString(target)); + + + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] target = new int[1]; + boolean flag = true; + for( int i = 0; i < oldArray.length; i++ ){ + if ( oldArray[i] == 0 ){ // 跳过值为0的元素 + continue; + } + + if ( flag ){ + //首位赋值无需扩容 + target[target.length-1] = oldArray[i]; + flag = false; + }else{ + //确定值不是0才能进入扩容步骤 + target = Arrays.copyOf(target, target.length+1); + target[target.length-1] = oldArray[i]; + } + + + + } + System.out.println("去0前:"+Arrays.toString(oldArray)); + System.out.println("去0后:"+Arrays.toString(target)); + return target; + } + + /** + * 给定两个已经排序好的整形数组, 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){ + //1.去重 + int[] array3 = Arrays.copyOf(array1, array1.length); + for( int i = 0; i < array1.length; i++ ){ + for( int j = 0; j < array2.length ; j++ ){ + if ( array1[i] == array2[j] ){ + + } + } + + + + + } + + System.out.println(Arrays.toString(array1)); + System.out.println(Arrays.toString(array2)); + //2.合并 + + //3.排序 + + + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = Arrays.copyOf(oldArray, oldArray.length+size); + System.out.println("扩容后:"+Arrays.toString(newArray)); + 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 int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for( int i = 0; i < array.length; i++ ){ + if ( i == 0 ){ + sb.append(array[i]); + }else{ + sb.append(seperator+array[i]); + } + } + return sb.toString(); + } + + + public static void main(String[] args) { + int[] a1 =new int[]{3,8}, a2 = new int[]{4, 5, 6,7}; + System.out.println(new ArrayUtil().join(a1, "*")); + + } +} diff --git a/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java b/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java new file mode 100644 index 0000000000..da1abd3b38 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java @@ -0,0 +1,35 @@ +package cn.mark.work0226; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestArrayUtil { + ArrayUtil arrayUtil = null; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void reverseArray() { + int[] origin = new int[]{1,2,8,31}; + arrayUtil.reverseArray(origin); + } + + @Test + public void removeZero() { + int[] origin = new int[]{0,1,2,0,3,0,4,7,0}; + Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 7}, arrayUtil.removeZero(origin)); + } + +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java b/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java new file mode 100644 index 0000000000..4966d7d170 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package cn.mark.work0226.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/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java b/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java new file mode 100644 index 0000000000..93a449adaf --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java @@ -0,0 +1,84 @@ +package cn.mark.work0226.litestruts; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据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字段中。 + + */ + String classPath = null; + String className = null; + + Document dom = XMLUtils.getDocument("bin"+File.separator+"struts.xml"); + Element ele = XMLUtils.getElement(dom, actionName); + Attribute classAttr = ele.attribute("class"); + classPath = classAttr.getValue(); + className = classPath.substring(classPath.lastIndexOf(".")+1); + System.out.println(className); + + + + try { + Class clz = Class.forName(classPath); + System.out.println(clz.getName()); + + + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + return null; + } + + + public static void main(String[] args) { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + Struts.runAction(actionName,params); + } +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java b/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java new file mode 100644 index 0000000000..1a60626460 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package cn.mark.work0226.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/group16/502059278/src/cn/mark/work0226/litestruts/View.java b/group16/502059278/src/cn/mark/work0226/litestruts/View.java new file mode 100644 index 0000000000..31e3df3427 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/View.java @@ -0,0 +1,23 @@ +package cn.mark.work0226.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/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java b/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java new file mode 100644 index 0000000000..2eeb6e624e --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java @@ -0,0 +1,49 @@ +package cn.mark.work0226.litestruts; + +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class XMLUtils { + /** + * 获取Document + * @param filePath 配置文件路径名 + * @return Document对象 + */ + public static Document getDocument(String filePath){ + //1.创建解析器 + SAXReader reader = new SAXReader(); + //2.解析XML文档,返回document对象 + Document dom = null; + try { + dom = reader.read(filePath); + } catch (DocumentException e) { + e.printStackTrace(); + } + return dom; + } + /** + * 获取指定action元素 + * @param doc Document + * @param actionName 要获取的元素属性名 + * @return 包含所要属性的元素 + */ + public static Element getElement(Document doc , String actionName){ + Element result = null; + Element root = doc.getRootElement(); + List elements = root.elements(); + for(Element e : elements){ + Attribute attr = e.attribute("name"); + if(attr.getValue().equals(actionName)){ + result = e; + return result; + } + } + return result; + } + +} diff --git a/group16/502059278/src/struts.xml b/group16/502059278/src/struts.xml new file mode 100644 index 0000000000..1aaa6ea5a0 --- /dev/null +++ b/group16/502059278/src/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/542087872/out/production/coding2017/com/coderising/litestruts/struts.xml b/group16/542087872/out/production/coding2017/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group16/542087872/out/production/coding2017/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/group16/542087872/out/production/coding2017/net/coding/coderising/litestruts/struts.xml b/group16/542087872/out/production/coding2017/net/coding/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..848f04cfba --- /dev/null +++ b/group16/542087872/out/production/coding2017/net/coding/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/group16/542087872/src/com/coding/basic/ArrayList.java b/group16/542087872/src/net/coding/basic/ArrayList.java similarity index 98% rename from group16/542087872/src/com/coding/basic/ArrayList.java rename to group16/542087872/src/net/coding/basic/ArrayList.java index 1b10b441cf..a0286827d6 100644 --- a/group16/542087872/src/com/coding/basic/ArrayList.java +++ b/group16/542087872/src/net/coding/basic/ArrayList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; import java.util.Arrays; diff --git a/group16/542087872/src/com/coding/basic/BinaryTreeNode.java b/group16/542087872/src/net/coding/basic/BinaryTreeNode.java similarity index 97% rename from group16/542087872/src/com/coding/basic/BinaryTreeNode.java rename to group16/542087872/src/net/coding/basic/BinaryTreeNode.java index df167343a0..4cac873d08 100644 --- a/group16/542087872/src/com/coding/basic/BinaryTreeNode.java +++ b/group16/542087872/src/net/coding/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class BinaryTreeNode { diff --git a/group16/542087872/src/net/coding/basic/Iterator.java b/group16/542087872/src/net/coding/basic/Iterator.java new file mode 100644 index 0000000000..ca3fd054ae --- /dev/null +++ b/group16/542087872/src/net/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package net.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/542087872/src/com/coding/basic/LinkedList.java b/group16/542087872/src/net/coding/basic/LinkedList.java similarity index 99% rename from group16/542087872/src/com/coding/basic/LinkedList.java rename to group16/542087872/src/net/coding/basic/LinkedList.java index 144af4ec8d..0f9d326545 100644 --- a/group16/542087872/src/com/coding/basic/LinkedList.java +++ b/group16/542087872/src/net/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class LinkedList implements List { diff --git a/group16/542087872/src/net/coding/basic/List.java b/group16/542087872/src/net/coding/basic/List.java new file mode 100644 index 0000000000..189fe091d8 --- /dev/null +++ b/group16/542087872/src/net/coding/basic/List.java @@ -0,0 +1,9 @@ +package net.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/542087872/src/com/coding/basic/Queue.java b/group16/542087872/src/net/coding/basic/Queue.java similarity index 92% rename from group16/542087872/src/com/coding/basic/Queue.java rename to group16/542087872/src/net/coding/basic/Queue.java index 8e4285464b..d233a02617 100644 --- a/group16/542087872/src/com/coding/basic/Queue.java +++ b/group16/542087872/src/net/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class Queue { diff --git a/group16/542087872/src/com/coding/basic/Stack.java b/group16/542087872/src/net/coding/basic/Stack.java similarity index 94% rename from group16/542087872/src/com/coding/basic/Stack.java rename to group16/542087872/src/net/coding/basic/Stack.java index bfe98dd8b7..4a8099bcee 100644 --- a/group16/542087872/src/com/coding/basic/Stack.java +++ b/group16/542087872/src/net/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group16/542087872/src/com/coding/basic/Test.java b/group16/542087872/src/net/coding/basic/Test.java similarity index 99% rename from group16/542087872/src/com/coding/basic/Test.java rename to group16/542087872/src/net/coding/basic/Test.java index 2db5b2f9ab..d973793899 100644 --- a/group16/542087872/src/com/coding/basic/Test.java +++ b/group16/542087872/src/net/coding/basic/Test.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; /** diff --git a/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java b/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7cf4e1fd3f --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java @@ -0,0 +1,203 @@ +package net.coding.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 l = 0, r = origin.length - 1; + while (l < r) { + int tmp = origin[l]; + origin[l] = origin[r]; + origin[r] = tmp; + l++; + r--; + } + } + + /** + * 现在有如下的一个数组: 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 cntZero = 0; + for (int i = 0; i < oldArray.length;i++) { + if (oldArray[i] == 0) { + cntZero ++; + } + } + if (cntZero == 0) { + return oldArray; + } + + int[] newArray = new int[oldArray.length - cntZero]; + int j = 0; + for (int i = 0; i < oldArray.length;i++) { + if (oldArray[i] != 0) { + newArray[j++] = oldArray[i]; + } + } + + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int[] result = new int[array1.length + array2.length]; + int l = 0; + int r = 0; + + int cnt = 0; + while (true) { + if (l >= array1.length && r >= array2.length) { + break; + } + if (l >= array1.length) { + result[cnt++] = array2[r]; + r++; + } else if (r >= array1.length) { + result[cnt++] = array1[l]; + l++; + } else { + if (array1[l] < array2[r]) { + result[cnt++] = array1[l]; + l++; + } else { + result[cnt++] = array2[r]; + r++; + } + } + } + + return result; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] result = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + 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){ + int[] result = {}; + int a = 0; + int b = 1; + + int cnt = 0; + while (b < max) { + result = grow(result, 1); + result[cnt++] = b; + int tmp = a + b; + a = b; + b = tmp; + } + + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[1,2,3,5,7,11,13,17,19] + * @param max + * @return + */ + private boolean isPrime(int n) { + for (int i = 2; i <= Math.sqrt(n + 1.0); i++) { + if (n % i == 0) { + return false; + } + } + return true; + } + public int[] getPrimes(int max){ + int[] result = {}; + int cnt = 0; + for (int i = 1; i < max; i++) { + if (isPrime(i)) { + result = grow(result, 1); + result[cnt++] = i; + } + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + private boolean isPerfect(int n) { + int total = 0; + for (int i = 1; i < n; i++) { + if (n % i == 0) { + total += i; + } + } + + return total == n; + } + public int[] getPerfectNumbers(int max){ + int[] result = {}; + int cnt = 0; + for (int i = 1; i < max; i++) { + if (isPerfect(i)) { + result = grow(result, 1); + result[cnt++] = 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 sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (sb.length() > 0) { + sb.append(seperator); + } + sb.append(array[i]); + } + return sb.toString(); + } + + +} diff --git a/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java b/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..0ae290bbfd --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package net.coding.coderising.array; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by xiaoyuan on 02/03/2017. + */ +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] nums = {1, 2, 3}; + new ArrayUtil().reverseArray(nums); + Assert.assertArrayEquals(nums, new int[]{3, 2, 1}); + } + // removeZero + @Test + public void testRemoveZero() { + int[] nums = {0, 1, 0, 2, 3}; + int[] ans = new ArrayUtil().removeZero(nums); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3}); + } + + // merge + @Test + public void testMerge() { + int[] nums1 = {1, 3, 9}; + int[] nums2 = {2, 4, 5}; + int[] ans = new ArrayUtil().merge(nums1, nums2); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3, 4, 5, 9}); + } + + // grow + @Test + public void testGrow() { + int[] nums = {1, 3, 9}; + int[] ans = new ArrayUtil().grow(nums, 2); + Assert.assertArrayEquals(ans, new int[]{1, 3, 9, 0, 0}); + } + + // fibonacci + @Test + public void testFibonacci() { + int[] ans = new ArrayUtil().fibonacci(10); + Assert.assertArrayEquals(ans, new int[]{1, 1, 2, 3, 5, 8}); + } + + + // getPrimes + @Test + public void testgetPrimes() { + int[] ans = new ArrayUtil().getPrimes(10); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3, 5, 7}); + } + + + // getPerfectNumbers + @Test + public void testGetPerfectNumbers() { + int[] ans = new ArrayUtil().getPerfectNumbers(10); + Assert.assertArrayEquals(ans, new int[]{6}); + } + + + // join + + @Test + public void testJoin() { + String ans = new ArrayUtil().join(new int[]{1, 3, 4}, "-"); + Assert.assertEquals(ans, "1-3-4"); + } + + + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java b/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..5e4f956c2f --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package net.coding.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/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java b/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..560d8f4fd8 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java @@ -0,0 +1,36 @@ +package net.coding.coderising.litestruts; + +/** + * Created by xiaoyuan on 02/03/2017. + */ +public class LogoutAction { + + String ifLogout; + String message; + + public String execute() { + if (ifLogout.equals("yes")) { + this.message = "success"; + return "success"; + } else { + this.message = "error"; + return "error"; + } + } + + public String getIfLogout() { + return ifLogout; + } + + public void setIfLogout(String ifLogout) { + this.ifLogout = ifLogout; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/Struts.java b/group16/542087872/src/net/coding/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..1d9337f4f2 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/Struts.java @@ -0,0 +1,126 @@ +package net.coding.coderising.litestruts; + + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +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字段中。 + + */ + + Map name2ClassMap = new HashMap(); + Map result2JSPMap = new HashMap(); + + + try { + File xmlFile = new File("group16/542087872/src/net/coding/coderising/litestruts/struts.xml"); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = factory.newDocumentBuilder(); + Document doc = documentBuilder.parse(xmlFile); + + doc.getDocumentElement().normalize(); + + NodeList actionList = doc.getElementsByTagName("action"); + for (int i = 0; i < actionList.getLength(); i++) { + Node node = actionList.item(i); + System.out.println(node.getNodeName()); + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element element = (Element)node; + String acName = element.getAttribute("name"); + String acClass = element.getAttribute("class"); + name2ClassMap.put(acName, acClass); + + NodeList resultList = element.getElementsByTagName("result"); + for (int j = 0; j < resultList.getLength(); j++) { + Element resultElemet = (Element)(resultList.item(j)); + String acResultName = resultElemet.getAttribute("name"); + String acResultJSP = resultElemet.getTextContent(); + + result2JSPMap.put(acName + "_" + acResultName, acResultJSP); + } + } + } + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("parse XML ERROR!"); + } + + String classStr = name2ClassMap.get(actionName); + if (classStr == null) { + throw new RuntimeException("ACTION FOUND ERROR!"); + } + + try { + Class actionClass = Class.forName(classStr); + Object actionObj = actionClass.newInstance(); + for (String key : parameters.keySet()) { + String value = parameters.get(key); + Method theMethod = actionClass.getMethod("set" + key.substring(0, 1).toUpperCase() + key.substring(1), String.class); + theMethod.invoke(actionObj, value); + } + + // execut + Method exeMethod = actionClass.getMethod("execute"); + String result = (String)exeMethod.invoke(actionObj); + + // find JSP + String JSPPath = result2JSPMap.get(actionName + "_" + result); + View view = new View(); + view.setJsp(JSPPath); + + // generate map + Map map = new HashMap(); + for(Method method: actionClass.getMethods()) { + if (method.getName().startsWith("get")) { + Object key = method.getName().substring(3).toLowerCase(); + Object value = method.invoke(actionObj); + map.put(key, value); + } + } + view.setParameters(map); + + return view; + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + + public static void main(String[] args) { + runAction(null, null); + } + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java b/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8c4de18ac4 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java @@ -0,0 +1,70 @@ +package net.coding.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + + @Test + public void testLogoutActionSuccess() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("ifLogout","yes"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("success", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionError() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("ifLogout","no"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("error", view.getParameters().get("message")); + } + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/View.java b/group16/542087872/src/net/coding/coderising/litestruts/View.java new file mode 100644 index 0000000000..63fb1d025e --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package net.coding.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/group16/542087872/src/net/coding/coderising/litestruts/struts.xml b/group16/542087872/src/net/coding/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..848f04cfba --- /dev/null +++ b/group16/542087872/src/net/coding/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/group16/63912401/.classpath b/group16/63912401/.classpath new file mode 100644 index 0000000000..74368f11c3 --- /dev/null +++ b/group16/63912401/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group16/63912401/.gitignore b/group16/63912401/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/63912401/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/63912401/.project b/group16/63912401/.project new file mode 100644 index 0000000000..ec6117b543 --- /dev/null +++ b/group16/63912401/.project @@ -0,0 +1,17 @@ + + + 63912401 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/63912401/src/com/coderising/action/LoginAction.java b/group16/63912401/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..64d4d355b6 --- /dev/null +++ b/group16/63912401/src/com/coderising/action/LoginAction.java @@ -0,0 +1,45 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/63912401/src/com/coderising/array/ArrayUtil.java b/group16/63912401/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..c8510b08e1 --- /dev/null +++ b/group16/63912401/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,270 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + + +/** + * ArrayUtil + * @author greenhills + * 2017年2月28日 下午10:49:41 + */ +public class ArrayUtil { + + public static void main(String[] args) { +// int[] array1={5,8,9,0,-4}; +// int[] array2={4,5,6,7,8,9}; +// int[] array3=ArrayUtil.merge(array1, array2); +// for(Integer t:array3){ +// System.out.print(t +"\t"); +// } + +// int[] array3={4,5,6,7,8,9}; +// array3 = ArrayUtil.grow(array3,5); +// for(int t:array3){ +// System.out.print(t +"\t"); +// } + +// int[] array4=ArrayUtil.fibonacci(100); +// for(Integer t:array4){ +// System.out.print(t +"\t"); +// } + +// int[] array5=ArrayUtil.getPrimes(2); +// for(Integer t:array5){ +// System.out.print(t +"\t"); +// } + + int[] array6=ArrayUtil.getPerfectNumbers(2000); + for(Integer t:array6){ + System.out.print(t +"\t"); + } + + } + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + for (int i = 0; i < origin.length>>1; i++) { + origin[i]^=origin[origin.length - 1 - i]^(origin[origin.length - 1 - i]=origin[i]); + } + + //方法2 可以使用Collections.reverse(list)方法,但是int 和 Integer数组之间转化消耗性能 + //Collections.reverse(list); + } + + /** + * 现在有如下的一个数组: Integer 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){ + List list=new ArrayList(); + for(Integer data:oldArray){ + if(data != 0){ + list.add(data); + } + } + int[] newArray=new int[list.size()]; + for(Integer i=0;i set=new HashSet(); + for(Integer t:array1){ + set.add(t); + } + for(Integer t:array2){ + set.add(t); + } + List list=new ArrayList(set); + java.util.Collections.sort(list); + int[] result =new int[list.size()]; + for(int i=0;i 1){ + List list=new ArrayList(); + list.add(1); + list.add(1); + int i=0; + int temp=list.get(i)+list.get(i+1); + while(temp < max){ + list.add(temp); + i++; + temp=list.get(i)+list.get(i+1); + } + + result=new int[list.size()]; + + i=0; + for(int d:list){ + result[i]=d; + i++; + } + } + + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + List list=new ArrayList(); + for(int i=2;i list=new ArrayList(); + for(int i=1;i resultMappings=new HashMap(); + + public ActionMapping() {} + + public ActionMapping(String name, String className, String method) { + this.name = name; + this.className = className; + this.method = StringUtils.isBlank(method) ? "execute" : method; //未配置时,默认查找execute方法执行 + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public String getMethod() { + return method; + } + public void setMethod(String method) { + this.method = method; + } + public Map getResultMappings() { + return resultMappings; + } + public void setResultMappings(Map resultMappings) { + this.resultMappings = resultMappings; + } + + //扩展的方法,用于保存 + public void setResultMappings(String key, ResultMapping value) { + this.resultMappings.put(key, value); + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java b/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java new file mode 100644 index 0000000000..55204d1b38 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java @@ -0,0 +1,83 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * 解析XML文件 + * ConfigurationManager + * @author greenhills + * 2017年2月27日 下午10:18:10 + */ +public class ConfigurationManager { + /** + * 读取xml文件的文档对象 + */ + private static Document document; + /** + * 配置文件路径 + */ + static String configureFileName="struts.xml"; + /** + * 当前类路径 + */ + static String currentPath; + static{ + currentPath = ConfigurationManager.class.getResource("").getPath().substring(1); + } + + static{ + try { + SAXReader sax=new SAXReader(); + document = sax.read(new File(currentPath+configureFileName)); + } catch (DocumentException e) { + e.printStackTrace(); + } + } + + /** + * 解析配置文件 + * ConfigurationManager.java + * @param @return + * @author greenhills + * 2017年2月27日 下午11:28:59 + */ + public static Map loadXml(){ + Map actionMappings=new HashMap(); + + Element root=document.getRootElement(); + List actionList= root.elements("action"); + + for(Element actionElemnt:actionList){ + ActionMapping actionMapping=new ActionMapping( + actionElemnt.attributeValue("name"), + actionElemnt.attributeValue("class"), + actionElemnt.attributeValue("method") + ); + //获取action下的result节点 + List resultList = actionElemnt.elements("result"); + for(Element resultElemnt:resultList){ + ResultMapping resultMapping=new ResultMapping( + resultElemnt.attributeValue("name"), + resultElemnt.attributeValue("type"), + resultElemnt.getTextTrim() + ); + + //保存ResultMapping(以result标签的name为key) + actionMapping.setResultMappings(resultMapping.getName(),resultMapping); + } + + //保存ActionMapping(以action标签的name为key) + actionMappings.put(actionMapping.getName(), actionMapping); + } + + return actionMappings; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/DefaultAction.java b/group16/63912401/src/com/coderising/litestruts/DefaultAction.java new file mode 100644 index 0000000000..c654a11a02 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/DefaultAction.java @@ -0,0 +1,121 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author greenhills + * @version 创建时间:2017年2月27日 下午11:48:56 + * + */ +public class DefaultAction { + private ActionMapping actionMapping; + private Object targetAction; //由DefaultAction反射调用的目标对象 + + //构造方法,实例化对象 + public DefaultAction(ActionMapping actionMapping) { + this.actionMapping = actionMapping; + //实例化对象 + try { + this.targetAction = Class.forName(this.actionMapping.getClassName()).newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 初始化参数 + * DefaultAction.java + * @param + * @author greenhills + * 2017年2月27日 下午11:58:05 + */ + public void initParam(Map parameters){ + Class clazz=this.targetAction.getClass(); + Set keys=parameters.keySet(); + try { + for(String key:keys){ + String _key = getFirstUpper(key); + //调用set方法赋值 + Method method=clazz.getDeclaredMethod("set"+_key,clazz.getDeclaredField(key).getType()); + method.invoke(this.targetAction, parameters.get(key)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 调用实例方法 + * DefaultAction.java + * @param @return + * @author greenhills + * 2017年2月27日 下午11:53:56 + */ + public String runMethod(){ + String methodName=this.actionMapping.getMethod(); + Class clazz=this.targetAction.getClass(); + String result="success"; + //调用set方法赋值 + try { + Method method=clazz.getDeclaredMethod(methodName); + result = (String) method.invoke(this.targetAction); + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + /** + * 获取action字段的值 + * DefaultAction.java + * @param @param fields + * @param @return + * @author greenhills + * 2017年2月28日 上午12:39:01 + */ + public Map getFieldValue(String[] fields){ + Map result=new HashMap(); + try { + Class clazz=this.targetAction.getClass(); + for(String field:fields){ + String _field = getFirstUpper(field); + //调用get方法获取值 + Method method=clazz.getDeclaredMethod("get"+_field); + result.put(field,method.invoke(this.targetAction)); + } + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + + + /** + * 将首字母改为大写 + * DefaultAction.java + * @param @param val + * @param @return + * @author greenhills + * 2017年2月28日 上午12:24:34 + */ + private String getFirstUpper(String val){ + return val.substring(0, 1).toUpperCase()+val.substring(1); + } + + + public ActionMapping getActionMapping() { + return actionMapping; + } + public void setActionMapping(ActionMapping actionMapping) { + this.actionMapping = actionMapping; + } + public Object getTargetAction() { + return targetAction; + } + public void setTargetAction(Object targetAction) { + this.targetAction = targetAction; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/ResultMapping.java b/group16/63912401/src/com/coderising/litestruts/ResultMapping.java new file mode 100644 index 0000000000..9a2650db09 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/ResultMapping.java @@ -0,0 +1,48 @@ +package com.coderising.litestruts; + +import org.apache.commons.lang3.StringUtils; + +/** + * @author greenhills + * @version 创建时间:2017年2月27日 下午10:39:20 + * + */ +public class ResultMapping { + /** + * 映射结构:/jsp/homepage.jsp + */ + private String name; + private String type; + private String urlPath; + + public ResultMapping() { + super(); + } + + public ResultMapping(String name, String type, String urlPath) { + super(); + this.name = StringUtils.isBlank(name) ? "success" : name; //默认成功 + this.type = StringUtils.isBlank(type) ? "dispatcher" : type; //默认转发 + this.urlPath = urlPath; + } + + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getUrlPath() { + return urlPath; + } + public void setUrlPath(String urlPath) { + this.urlPath = urlPath; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/Struts.java b/group16/63912401/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..06a23bb864 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { +// 0. 读取配置文件struts.xml + Map actionMappings = ConfigurationManager.loadXml(); + +// 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) +// 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 +// ("name"="test" , "password"="1234") , +// 那就应该调用 setName和setPassword方法 + ActionMapping actionMapping = actionMappings.get(actionName); + DefaultAction action=new DefaultAction(actionMapping); + action.initParam(parameters); + +// 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + String result= action.runMethod(); + +// 3. 通过反射找到对象的所有getter方法(例如 getMessage), +// 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , +// 放到View对象的parameters + + Map messageMaps=action.getFieldValue(new String[]{"message"}); + +// 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, +// 放到View对象的jsp字段中。 + View view =new View(); + view.setJsp(actionMapping.getResultMappings().get(result).getUrlPath()); + view.setParameters(messageMaps); + return view; + } + +} diff --git a/group16/63912401/src/com/coderising/litestruts/StrutsTest.java b/group16/63912401/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..4061019b2e --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +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/group16/63912401/src/com/coderising/litestruts/View.java b/group16/63912401/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/63912401/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/group16/63912401/src/com/coderising/litestruts/struts.xml b/group16/63912401/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..76cc617a8a --- /dev/null +++ b/group16/63912401/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/group16/63912401/src/com/coding/basic/ArrayList.java b/group16/63912401/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f86a2e7a8a --- /dev/null +++ b/group16/63912401/src/com/coding/basic/ArrayList.java @@ -0,0 +1,206 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * ArrayList + * @author greenhills + * @version 创建时间:2017年2月19日 下午10:54:02 + * @param + * + */ +public class ArrayList implements List { + /** + * 默认容量 + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * 数据存放区 + */ + private Object[] elementData; + + /** + * 真实的数据数量 + */ + private int size = 0; + + /** + * 无参构造函数 + */ + public ArrayList(){ + this.elementData=new Object[DEFAULT_CAPACITY]; + } + + /** + * 带初始大小的构造函数 + * @param beginSize + */ + public ArrayList(int beginSize){ + if(beginSize<0) + this.elementData=new Object[DEFAULT_CAPACITY]; + else + this.elementData=new Object[beginSize]; + } + + /** + * 在后面追加数据 + */ + @Override + public void add(Object o){ + autoGrow(size+1); + this.elementData[size++] = o; //在尾部追加数据 + } + + /** + * 把数据加入指定索引处 + */ + @Override + public void add(int index, Object o){ + rangeCheck(index); + autoGrow(size+1); + //把index处的所有数据往后移 + //System.arraycopy(elementData, index, elementData, index+1, size-index); + + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + + this.elementData[index] = o; //使数据连续加入 + size++; + } + + /** + * 获取指定索引处的数据 + */ + @Override + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + /** + * 获取末尾数据 + */ + public Object getLast(){ + return elementData[this.size-1]; + } + + /** + * 移除索引处数据 + */ + @Override + public Object remove(int index){ + rangeCheck(index); + + Object removed = elementData[index]; + int num=size - index - 1; //移动数量 + if(num>0) { + System.arraycopy(elementData, index+1, elementData, index,num); + } + elementData[--size] = null; //清除最后一个数据位 + return removed; + } + + /** + * 移除末尾数据 + */ + public Object removeLast(){ + return remove(this.size-1); + } + + /** + * 获取数据量 + */ + @Override + public int size(){ + return this.size; + } + + /** + * 获取存储数据的容量大小 + */ + @Override + public int capacity() { + return this.elementData.length; + } + + /** + * 判断是否为空 + */ + @Override + public boolean isEmpty() { + return this.size==0; + } + + /** + * 空间容量自增长 + * @param minCapacity 增长后最小容量 + */ + private void autoGrow(int minCapacity){ + int oldCapacity = elementData.length; + if (minCapacity >= oldCapacity) { + int newCapacity = oldCapacity<<1; //空间翻倍 + if (newCapacity < minCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + /** + * 判断是否为有效索引 + * @param @param index + * @param @return + */ + private void rangeCheck(int index) { + if (!isEffectiveIndex(index)) + throw new IndexOutOfBoundsException("Index: "+index+" Out Of Bounds, 有效数据索引范围:0~"+(this.size-1)); + } + + /** + * 判断是否为有效索引 + * @param @param index + * @param @return + */ + private boolean isEffectiveIndex(int index){ + return index >-1 && index < this.size; + } + + /** + * 返回遍历数据对象 + * @param @return + * @author greenhills + * 2017年2月25日 下午9:55:31 + */ + public Iterator iterator(){ + return new Its(); + } + + /** + * 实现Iterator的内部实现类 + * Its + * @author greenhills + * 2017年2月25日 下午9:54:54 + */ + private class Its implements Iterator { + private int index=0; + + public Its(){ + //this.len = size; //逆向遍历 + } + + @Override + public boolean hasNext() { +// return this.len > 0; //逆向遍历 + return this.index < size; //正向遍历 + } + + @Override + public Object next() { +// return get(--this.len); //逆向遍历 +// return elementData[--this.len];//逆向遍历 + return get(this.index++); //正向遍历 + } + } +} diff --git a/group16/63912401/src/com/coding/basic/BinaryTreeNode.java b/group16/63912401/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..3ab1e431c0 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,99 @@ +package com.coding.basic; + +/** + * 二叉树数据结构 + * BinaryTreeNode + * @author greenhills + * 2017年2月25日 下午9:51:05 + */ +public class BinaryTreeNode implements Comparable{ + + private int height=0; //当前树高度 + private Object data; //当前节点数据 + private BinaryTreeNode left; //小于当前节点数据data的节点 + private BinaryTreeNode right; //大于当前节点数据data的节点 + + public BinaryTreeNode() { + } + + public BinaryTreeNode(Object data) { + this.data = data; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode newNode=new BinaryTreeNode(o); + BinaryTreeNode that = findNode(o); + int result=that.compareTo(o); + + if(result<0){//节点数据小于插入数据,进右树 + that.setRight(newNode); + }else if(result>0){ //当前节点数据大于插入数据,进左树 + that.setLeft(newNode); + }else{ + return null; + } + newNode.height++; //树高度加1 + return newNode; + } + + private BinaryTreeNode findNode(Object data){ + int result=this.compareTo(data); + BinaryTreeNode that = new BinaryTreeNode(); //空节点 + if(result<0){ //当前节点数据小于插入数据,进右树 + if(this.right==null){ + that = this; + }else{ + that = findNode(this.getRight()); + } + }else if(result>0){ //当前节点数据大于插入数据,进左树 + if(this.left==null){ + that = this; + }else{ + that = findNode(this.getLeft()); + } + }else{ + System.out.println("无效数据"); + } + return that; + } + + public int getTreeMaxHeight(){ + int h=0; + //TODO + + return h; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + 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; + } + + @Override + public int compareTo(Object o) { + if(this.data==null || o==null) return 0; + return Double.valueOf(this.data.toString()).compareTo(Double.valueOf(o.toString())); + } +} diff --git a/group16/63912401/src/com/coding/basic/Iterator.java b/group16/63912401/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..fa815258c1 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/63912401/src/com/coding/basic/LinkedList.java b/group16/63912401/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..2120a5a4b8 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/LinkedList.java @@ -0,0 +1,320 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * 链表数据结构 + * LinkedList + * @author greenhills + * 2017年2月22日 下午11:52:41 + */ +public class LinkedList implements List { + /** + * 链表数据量 + */ + private int size=0; + /** + * 链表头节点 + */ + private Node head; + /** + * 链表尾节点 + */ + private Node tail; + + + /** + * 在数据链尾部添加数据 + */ + @Override + public void add(Object o) { + addLast(o); + } + + /** + * 在数据链指定位置添加数据 + */ + @Override + public void add(int index, Object data) { + checkIndex(index); + Node old=getNode(index); + link2Before(old,data); + } + + /** + * 获取指定索引处数据 + */ + @Override + public Object get(int index) { + checkIndex(index); + return getNode(index).data; + } + + /** + * 移除指定索引位置的节点 + */ + @Override + public Object remove(int index) { + checkIndex(index); + return unlink(getNode(index)); + } + + /** + * 获取数据链的数据量 + */ + @Override + public int size() { + return this.size; + } + + /** + * 获取数据链的数据量 + */ + @Override + @Deprecated + public int capacity() { + return size(); + } + + /** + * 判断是否为空 + */ + @Override + public boolean isEmpty() { + return this.size==0; + } + + /** + * 在数据链头部追加数据 + * @param data + */ + public void addFirst(Object data){ + final Node oldNode=this.head; + final Node newNode=new Node(null,data,oldNode);//形成新节点,并指向第一个节点 + this.head = newNode; //变更集合保存的首节点 + + if(oldNode == null){ //没有数据 + this.tail = newNode; //变更集合保存的尾节点 + }else{ + oldNode.prev = newNode; //原首节点指向新的首节点 + } + this.size++;//数据量加1 + } + + /** + * 在数据链尾部追加数据 + * @param data + */ + public void addLast(Object data){ + final Node oldNode=this.tail; + final Node newNode=new Node(oldNode,data,null);//形成新节点,并指向最后一个节点 + this.tail = newNode; //变更集合保存的尾节点 + + if(oldNode == null){ //没有数据 + this.head = newNode; //变更集合保存的首节点 + }else{ + oldNode.next = newNode;//原尾节点指向新的尾节点 + } + this.size++;//数据量加1 + } + + /** + * 把指定数据链接到指定节点前面 + */ + void link2Before(Node node,Object data){ + //传进来的node就是后节点 + final Node prev=node.prev; //指定节点的上一个节点(前节点) + //生成新节点,并指向前后的节点 + final Node newNode=new Node(prev,data,node);//生成新节点 + //后节点指向新节点 + node.prev = newNode; + //前节点指向新节点 + if(prev == null){//没有前节点了(当前节点已是首节点) + this.head = newNode;//把新的节点作为首节点 + }else{ + prev.next = newNode; + } + this.size++;//数据量加1 + } + + /** + * 把指定数据链接到指定节点后面 + */ + void link2Last(Node node,Object data){ + //传进来的node就是前节点 + final Node next=node.next; //指定节点的下一个节点(后节点) + //生成新节点,并指向前后的节点 + final Node newNode=new Node(node,data,next); + //前节点指向新节点 + node.next = newNode; + //后节点指向新节点 + if(next == null){//没有后节点了(当前节点已是尾节点) + this.tail = newNode;//把新的节点作为尾节点 + }else{ + next.prev = newNode; + } + this.size++;//数据量加1 + } + + /** + * 移除首节点 + * @return + */ + public Object removeFirst(){ + return unlink(getNode(0)); + } + + /** + * 移除尾节点 + * @return + */ + public Object removeLast(){ + return unlink(getNode(this.size-1)); + } + + /** + * 移除节点 + * @return + */ + Object unlink(Node node){ + final Object element = node.data; + final Node next = node.next; //下一个节点 + final Node prev = node.prev;//前一个节点 + + if (prev == null) {//待删除节点是首节点 + head = next; + } else { + prev.next = next; + node.prev = null;//解除待删除节点的引用关系 + } + + if (next == null) {//待删除节点是尾节点 + tail = prev; + } else { + next.prev = prev; + node.next = null;//解除待删除节点的引用关系 + } + + node.data = null;//清除节点数据 + size--; + return element;//返回清除节点数据 + } + + /** + * 在中间位置创建Iterator遍历 + * @return + */ + public Iterator iterator() { + return new Its(this.size>>1); + } + + /** + * 在指定位置创建Iterator遍历 + * @return + */ + public Iterator iterator(int index) { + checkIndex(index); + return new Its(index); + } + + /** + * 获取指定索引的节点对象 + * @param @param index + * @param @return + */ + private Node getNode(int index){ + if (index < (this.size >> 1)) { //在前半部分 + Node node = this.head; + for (int i = 0; i < index; i++){ + node = node.next; //向后查找 + } + return node; + } else { //在后半部分 + Node node = this.tail; + for (int i = this.size - 1; i > index; i--){ + node = node.prev; //向前查找 + } + return node; + } + } + + /** + * 判断是否为有效索引 + * @param @param index + * @param @return + */ + private boolean isEffectiveIndex(int index){ + return (index>=0 && index < size); + } + + /** + * 检测索引有效性,无效时抛出异常 + * @param index + */ + private void checkIndex(int index) { + if (!isEffectiveIndex(index)) + throw new IndexOutOfBoundsException("Index: "+index+" Out Of Bounds, 最大索引: "+(size-1)); + } + + + /** + * 实现Iterator的内部实现类 + */ + private class Its implements Iterator { + private Node lastReturned = null; + private Node node;//当前节点 + private int index;//当前节点索引 + + public Its(int index){ + node = isEffectiveIndex(index) ? getNode(index) : null; + this.index = index; + } + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = node; + node = node.next; + index++; + return lastReturned.data; + + } + + public boolean hasPrevious() { + return index >= 0; + } + + public Object previous() { + if (!hasPrevious()) + throw new NoSuchElementException(); + + lastReturned = node; + node = node.prev; + index--; + return lastReturned.data; + } + } + + /** + * 节点数据 + * Node + */ + private static class Node{ + Object data; + Node prev; + Node next; + + Node(Node prev,Object data,Node next){ + this.prev=prev; + this.data=data; + this.next=next; + } + } +} diff --git a/group16/63912401/src/com/coding/basic/List.java b/group16/63912401/src/com/coding/basic/List.java new file mode 100644 index 0000000000..7fe012ccab --- /dev/null +++ b/group16/63912401/src/com/coding/basic/List.java @@ -0,0 +1,16 @@ +package com.coding.basic; +/** + * 集合接口 + * @author greenhills + * @version 创建时间:2017年2月19日 下午10:49:40 + * + */ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + public int capacity(); + boolean isEmpty(); +} diff --git a/group16/63912401/src/com/coding/basic/Queue.java b/group16/63912401/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..c6007c9e99 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Queue.java @@ -0,0 +1,43 @@ +package com.coding.basic; + +/** + * 队列数据结构 + * Queue + * @author greenhills + * 2017年2月25日 下午9:50:04 + */ +public class Queue { + private LinkedList elementData = new LinkedList(); + + /** + * 入队 + * @param o + */ + public void enQueue(Object o){ + elementData.addLast(o); + } + + /** + * 出队 + * @return + */ + public Object deQueue(){ + return elementData.removeFirst(); + } + + /** + * 判断是否为空 + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 获取栈内数据量 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/63912401/src/com/coding/basic/Stack.java b/group16/63912401/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b343c6de1d --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Stack.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +/** + * 栈数据结构 + * Stack + * @author greenhills + * 2017年2月25日 下午9:49:41 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + + /** + * 出栈 + * @return + */ + public Object pop(){ + return elementData.removeLast(); + } + + /** + * 获取栈顶数据 + * @return + */ + public Object peek(){ + return elementData.getLast(); + } + + /** + * 判断是否为空 + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 获取栈内数据量 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/63912401/src/com/coding/basic/Stack2.java b/group16/63912401/src/com/coding/basic/Stack2.java new file mode 100644 index 0000000000..8c10ccdf06 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Stack2.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +/** + * 栈数据结构 + * Stack + * @author greenhills + * 2017年2月25日 下午9:49:41 + */ +public class Stack2 { + private LinkedList elementData = new LinkedList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o){ + elementData.addFirst(o); + } + + /** + * 出栈 + * @return + */ + public Object pop(){ + return elementData.removeFirst(); + } + + /** + * 获取栈顶数据 + * @return + */ + public Object peek(){ + return elementData.get(0); + } + + /** + * 判断是否为空 + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 获取栈内数据量 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/932886072/djj/ArrayList.java b/group16/932886072/djj/ArrayList.java new file mode 100644 index 0000000000..df3a11c386 --- /dev/null +++ b/group16/932886072/djj/ArrayList.java @@ -0,0 +1,64 @@ +package djj; + + +import java.util.Arrays; + +/** + * Created by jerry on 2017/2/26. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size>elementData.length*0.8){ + Arrays.copyOf(elementData,elementData.length*2); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + if (size>=index){ + Object[] temp=new Object[elementData.length]; + System.arraycopy(elementData,0,temp,0,index); + temp[index]=o; + System.arraycopy(elementData,index,temp,index+1,size-index); + elementData=temp; + }else if(sizesize){ + throw new RuntimeException("越界"); + }else{ + return elementData[index]; + } + } + + public Object remove(int index){ + Object tempObj=null; + if(index<=size){ + Object[] temp=new Object[elementData.length]; + System.arraycopy(elementData,0,temp,0,index); + tempObj=elementData[index]; + System.arraycopy(elementData,index+1,temp,index,size-index-1); + elementData=temp; + } + size--; + return tempObj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group16/932886072/djj/BinaryTreeNode.java b/group16/932886072/djj/BinaryTreeNode.java new file mode 100644 index 0000000000..d697be6ffc --- /dev/null +++ b/group16/932886072/djj/BinaryTreeNode.java @@ -0,0 +1,34 @@ +package djj; + +public class BinaryTreeNode { + + private BinaryTreeNode root; + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode insert(Object o){ + + return null; + } + + + 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; + } +} diff --git a/group16/932886072/djj/Iterator.java b/group16/932886072/djj/Iterator.java new file mode 100644 index 0000000000..4482e3a408 --- /dev/null +++ b/group16/932886072/djj/Iterator.java @@ -0,0 +1,9 @@ +package djj; + +/** + * Created by jerry on 2017/2/26. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/932886072/djj/LinkedList.java b/group16/932886072/djj/LinkedList.java new file mode 100644 index 0000000000..6e40d5cc69 --- /dev/null +++ b/group16/932886072/djj/LinkedList.java @@ -0,0 +1,96 @@ +package djj; + +public class LinkedList implements List { + //头节点 + private Node head; + //尾节点 +// private Node tail; + //当前游标节点 + private Node curNode; + public int size=0; + + + public void add(Object o){ + if(head==null){ + head=new Node(o); + head.next=null; + }else{ + curNode=head; + while(curNode.next!=null){ + curNode=curNode.next; + } + curNode.next=new Node(o); + } + size++; + } + public void add(int index , Object o){ + if(index>size||index<=0){ + throw new RuntimeException("越界"); + }else{ + curNode=head; + for(int i=0;isize||index<=0){ + throw new RuntimeException("越界"); + } + Node temp=head; + for(int i=0;isize||index<=0){ + throw new RuntimeException("越界"); + } + Node temp=head; + for(int i=0;i