diff --git a/.gitignore b/.gitignore index 8b1378917..6143e53f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,22 @@ +# Compiled class file +*.class +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* diff --git a/group21/1197171274/2017-03/.classpath b/group21/1197171274/2017-03/.classpath new file mode 100644 index 000000000..fb5011632 --- /dev/null +++ b/group21/1197171274/2017-03/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group21/1197171274/2017-03/.gitignore b/group21/1197171274/2017-03/.gitignore new file mode 100644 index 000000000..ae3c17260 --- /dev/null +++ b/group21/1197171274/2017-03/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group21/1197171274/2017-03/.project b/group21/1197171274/2017-03/.project new file mode 100644 index 000000000..44e7a0cdc --- /dev/null +++ b/group21/1197171274/2017-03/.project @@ -0,0 +1,17 @@ + + + 201702 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/1197171274/2017-03/src/com/coding/basic/ArrayList.java b/group21/1197171274/2017-03/src/com/coding/basic/ArrayList.java new file mode 100644 index 000000000..57412dcf7 --- /dev/null +++ b/group21/1197171274/2017-03/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group21/1197171274/2017-03/src/com/coding/basic/BinaryTreeNode.java b/group21/1197171274/2017-03/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 000000000..266eff3d5 --- /dev/null +++ b/group21/1197171274/2017-03/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/group21/1197171274/2017-03/src/com/coding/basic/Iterator.java b/group21/1197171274/2017-03/src/com/coding/basic/Iterator.java new file mode 100644 index 000000000..dbe8b9afb --- /dev/null +++ b/group21/1197171274/2017-03/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/group21/1197171274/2017-03/src/com/coding/basic/LinkedList.java b/group21/1197171274/2017-03/src/com/coding/basic/LinkedList.java new file mode 100644 index 000000000..1fd99bf26 --- /dev/null +++ b/group21/1197171274/2017-03/src/com/coding/basic/LinkedList.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group21/1197171274/2017-03/src/com/coding/basic/List.java b/group21/1197171274/2017-03/src/com/coding/basic/List.java new file mode 100644 index 000000000..396b1f641 --- /dev/null +++ b/group21/1197171274/2017-03/src/com/coding/basic/List.java @@ -0,0 +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(); +} diff --git a/group21/1197171274/2017-03/src/com/coding/basic/Queue.java b/group21/1197171274/2017-03/src/com/coding/basic/Queue.java new file mode 100644 index 000000000..08d2d86b1 --- /dev/null +++ b/group21/1197171274/2017-03/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group21/1197171274/2017-03/src/com/coding/basic/Stack.java b/group21/1197171274/2017-03/src/com/coding/basic/Stack.java new file mode 100644 index 000000000..4bfe28057 --- /dev/null +++ b/group21/1197171274/2017-03/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group21/1473147314/.classpath b/group21/1473147314/.classpath new file mode 100644 index 000000000..d171cd4c1 --- /dev/null +++ b/group21/1473147314/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group21/1473147314/.gitignore b/group21/1473147314/.gitignore new file mode 100644 index 000000000..ae3c17260 --- /dev/null +++ b/group21/1473147314/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group21/1473147314/.project b/group21/1473147314/.project new file mode 100644 index 000000000..ebbfae904 --- /dev/null +++ b/group21/1473147314/.project @@ -0,0 +1,17 @@ + + + Learning2017 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/1473147314/src/com/coding/basic/ArrayList.java b/group21/1473147314/src/com/coding/basic/ArrayList.java new file mode 100644 index 000000000..b5e61bb31 --- /dev/null +++ b/group21/1473147314/src/com/coding/basic/ArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +public class ArrayList implements List { + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(2,1); + list.add(0,6); + list.remove(0); + list.remove(4); + list.add(0,6); + list.add(0,6); + list.add(0,6); + list.add(0,6); + list.add(0,6); + list.add(0,6); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (size == elementData.length) { + grow(); + } + elementData[size++] = o; + } + + private void grow() { + Object[] target = new Object[elementData.length + size]; + for (int i = 0; i < elementData.length; i++) { + target[i] = elementData[i]; + } + elementData = target; + } + + public void add(int index, Object o) { + if (size == elementData.length) { + grow(); + } + for (int i = size++; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + } + + public Object get(int index) { + return elementData[index]; + } + + public Object remove(int index) { + Object data = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + size--; + return data; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + +} diff --git a/group21/1473147314/src/com/coding/basic/BinaryTreeNode.java b/group21/1473147314/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 000000000..266eff3d5 --- /dev/null +++ b/group21/1473147314/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/group21/1473147314/src/com/coding/basic/Iterator.java b/group21/1473147314/src/com/coding/basic/Iterator.java new file mode 100644 index 000000000..dbe8b9afb --- /dev/null +++ b/group21/1473147314/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/group21/1473147314/src/com/coding/basic/LinkedList.java b/group21/1473147314/src/com/coding/basic/LinkedList.java new file mode 100644 index 000000000..ac86bbf52 --- /dev/null +++ b/group21/1473147314/src/com/coding/basic/LinkedList.java @@ -0,0 +1,171 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + list.add(8); + list.add(10); + list.add(3,18888); + list.remove(8); + list.addFirst(111111111); + list.addFirst(2222); + list.addLast(5678); + list.addLast(1234); + System.out.println(list.removeFirst()); + System.out.println(list.removeLast()); + int len = list.size(); + System.out.print(list.size()+" : "); + for (int i = 0; i < len; i++) { + System.out.print(list.get(i)+" "); + } + + System.out.println("\n==========================="); + System.out.println(list.get(5)); + } + public void add(Object o) { + Node node = new Node(o); + if (head == null) { + head = node; + return; + } + Node current = head; + while (current.next != null) { + current = current.next; + } + current.next = node; + } + + public void add(int index, Object o) { + Node node = new Node(o); + if (index == 0) { + node.next = head; + head = node; + return; + } + Node pre = head; + Node current = head.next; + for (int i = 0; i < index; i++) { + if (i == index - 1) { + pre.next = node; + node.next = current; + return; + } + pre = pre.next; + current = current.next; + } + } + + public Object get(int index) { + int i = 0; + Node current = head; + while (current != null) { + if (i == index) { + return current.data; + } + current = current.next; + i++; + } + return null; + } + + public Object remove(int index) { + if (index == 0) { + head = head.next; + } + Node pre = head; + Node current = head.next; + for (int i = 0; i < index; i++) { + if (i == index - 1) { + pre.next = current.next; + current.next = null; + return current.data; + } + pre = pre.next; + current = current.next; + } + return null; + } + + public int size() { + int size = 0; + Node current = head; + while (current != null) { + current = current.next; + size++; + } + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + if (head == null) { + head = node; + return; + } + node.next = head; + head = node; + } + + public void addLast(Object o) { + Node node = new Node(o); + if (head == null) { + head = node; + return; + } + Node current = head; + while (current.next != null) { + current = current.next; + } + current.next = node; + } + + public Object removeFirst() { + if (head == null) { + return null; + } + Object data = head.data; + head = head.next; + return data; + } + + public Object removeLast() { + if (head == null) { + return null; + } + Node current = head; + if (current.next == null) { + return current.data; + } + Object data = null; + while (current.next != null) { + if (current.next.next != null) { + current = current.next; + continue; + } + data = current.next.data; + current.next = null; + } + return data; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + } +} diff --git a/group21/1473147314/src/com/coding/basic/List.java b/group21/1473147314/src/com/coding/basic/List.java new file mode 100644 index 000000000..396b1f641 --- /dev/null +++ b/group21/1473147314/src/com/coding/basic/List.java @@ -0,0 +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(); +} diff --git a/group21/1473147314/src/com/coding/basic/Queue.java b/group21/1473147314/src/com/coding/basic/Queue.java new file mode 100644 index 000000000..c9039dc1f --- /dev/null +++ b/group21/1473147314/src/com/coding/basic/Queue.java @@ -0,0 +1,55 @@ +package com.coding.basic; + +public class Queue { + + private Node head; + + public void enQueue(Object o){ + Node node = new Node(o); + if (head == null) { + head = node; + return; + } + Node current = head; + while (current.next != null) { + current = current.next; + } + current.next = node; + } + + public Object deQueue(){ + if (head == null) { + return null; + } + Object data = head.data; + if (head.next == null) { + head = null; + return data; + } + head = head.next; + return data; + } + + public boolean isEmpty(){ + return head == null; + } + + public int size(){ + int size = 0; + Node node = head; + while (node != null) { + node = node.next; + size++; + } + return size; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + } +} diff --git a/group21/1473147314/src/com/coding/basic/Stack.java b/group21/1473147314/src/com/coding/basic/Stack.java new file mode 100644 index 000000000..f67fbc05a --- /dev/null +++ b/group21/1473147314/src/com/coding/basic/Stack.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() < 1; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group21/231525696/.classpath b/group21/231525696/.classpath new file mode 100644 index 000000000..b2febef55 --- /dev/null +++ b/group21/231525696/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group21/231525696/.gitignore b/group21/231525696/.gitignore new file mode 100644 index 000000000..ae3c17260 --- /dev/null +++ b/group21/231525696/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group21/231525696/.project b/group21/231525696/.project new file mode 100644 index 000000000..623711e20 --- /dev/null +++ b/group21/231525696/.project @@ -0,0 +1,17 @@ + + + 231525696Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/231525696/dom4j-2.0.0-RC1.jar b/group21/231525696/dom4j-2.0.0-RC1.jar new file mode 100644 index 000000000..d3398d24e Binary files /dev/null and b/group21/231525696/dom4j-2.0.0-RC1.jar differ diff --git a/group21/231525696/src/common/BeanHelper.java b/group21/231525696/src/common/BeanHelper.java new file mode 100644 index 000000000..18263df3a --- /dev/null +++ b/group21/231525696/src/common/BeanHelper.java @@ -0,0 +1,156 @@ +package common; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * invokeUtil + * @author William on 2017-03-05 15:00:43 + */ +public class BeanHelper { + + + /** + * 调用实例中methodName的方法 + * @param bean 实例 + * @param methodName 方法名称 + * @param fieldName 字段名称 + * @return + */ + public static Object excuteBeanMethod(Object bean, String methodName, String fieldName) { + Class clazz = bean.getClass(); + String method = getMethodName(fieldName, methodName); + try { + Method getMethod = clazz.getMethod(method); + return getMethod.invoke(bean); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static T invokeClassFromMap(Class clazz, Map map) throws InstantiationException, IllegalAccessException { + T bean = clazz.newInstance(); + return invokeBeanFromMap(bean, map); + } + + public static T invokeBeanFromMap(T bean, Map map) throws InstantiationException, IllegalAccessException { + + Class clazz = bean.getClass(); + + Field[] fields = clazz.getDeclaredFields(); + for (Field field: fields) { + String fieldName = field.getName(); + + // 从m里找相应的信息 + String value = map.get(fieldName); + if (value == null) { + continue; + } + + String methodName = getMethodName(fieldName, "set"); + Object methodParam = null; + try { + // 定位set方法 + Method setMethod = clazz.getDeclaredMethod(methodName, field.getType()); + + if (field.getType().equals(String.class)) { + methodParam = value; + + } else if (field.getType().equals(Long.class)) { + methodParam = Long.parseLong(value); + + } else if (field.getType().equals(int.class)) { + methodParam = Integer.parseInt(value); + + } else if (field.getType().equals(float.class)) { + methodParam = Float.parseFloat(value); + + } else if (field.getType().equals(Float.class)) { + methodParam = Float.parseFloat(value); + + } else if (field.getType().equals(Date.class)) { + if (value == null || value.equals("")) { + continue; + } + value = value.replaceAll("/", "-"); + String formate=""; + if (value.length()>11) { + // 年-月-日 时:分:秒 + formate = "yyyy-MM-dd HH:mm:ss"; + } else { + // 年-月-日 + formate = "yyyy-MM-dd"; + } + SimpleDateFormat sdf = new SimpleDateFormat(formate); + methodParam = sdf.parse(value); + } + + if (value.equals("")) { + setMethod.invoke(bean, new Object[]{null}); + } else { + if (methodParam == null) { + System.err.println("未支持的反射类型:" + field.getType().getName() + ",已忽略反射。"); + continue; + } else { + setMethod.invoke(bean, methodParam); + } + } + + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + return bean; + } + + private static String getMethodName(String fieldName,String name) { + // 找方法名字 + if (fieldName == null || fieldName.equals("")){ + return name; + } + String methodName = name + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); + return methodName; + } + + //调用所有get方法返回属性map + public static Map getBeanFields (T bean){ + Class clazz = bean.getClass(); + Map result = new HashMap(); + Field[] fields = clazz.getDeclaredFields(); + + for (Field field: fields) { + String fieldName = field.getName(); + String methodName = getMethodName(fieldName, "get"); + // 定位set方法 + Method getMethod; + try { + getMethod = clazz.getMethod(methodName); + result.put(fieldName, getMethod.invoke(bean)); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + return result; + } +} diff --git a/group21/231525696/src/day20170219/ArrayList.java b/group21/231525696/src/day20170219/ArrayList.java new file mode 100644 index 000000000..27efa2488 --- /dev/null +++ b/group21/231525696/src/day20170219/ArrayList.java @@ -0,0 +1,94 @@ +package day20170219; + +import java.util.Arrays; + +public class ArrayList implements List{ + + //默认容量 + private static final int DEFAULT_CAPACITY = 10; + + //数据 + private Object[] elementData = new Object[DEFAULT_CAPACITY]; + + //数据当前索引(arraylist的大小) + private int currentIndex = 0; + + public ArrayList (){ + this.elementData = new Object[]{}; + } + + public ArrayList (int capacity){ + if (capacity > 0) { + this.elementData = new Object[capacity]; + } else if (capacity == 0) { + this.elementData = new Object[]{}; + } else { + throw new RuntimeException("Illegal Capacity: " + capacity); + } + + } + + //扩容 + private void grow (int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity * 2; + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + + //试探边界 + private void trialBound(int index){ + if (index > elementData.length || index < 0){ + throw new RuntimeException("OutOfBound:" + index); + } + } + + @Override + public void add(Object obj) { + if (currentIndex > elementData.length - 1) { + grow(elementData.length); + } + elementData[currentIndex] = obj; + currentIndex ++; + } + + @Override + public void add(Object obj, int index) { + trialBound(index); + //把原来的数据中需要加入元素以后的数据复制到原来索引+1的地方 + System.arraycopy(elementData, index, elementData, index + 1, currentIndex - index); + //用新元素替换旧元素 + elementData[index] = obj; + currentIndex ++; + } + + @Override + public Object get(int index) { + trialBound(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + trialBound(index); + Object oldElement = elementData[index]; + int moveIndex = currentIndex - index - 1; + if (moveIndex > 0) { + //把原来的数据中需要删除之后的moveIndex个元素复制到之前一格 + System.arraycopy(elementData, index + 1, elementData, index, moveIndex); + } + //删除最后一个元素 + elementData[--currentIndex] = null; + + return oldElement; + } + + @Override + public int size() { + return currentIndex; + } + + +} diff --git a/group21/231525696/src/day20170219/BinaryTreeNode.java b/group21/231525696/src/day20170219/BinaryTreeNode.java new file mode 100644 index 000000000..b4fbbaaea --- /dev/null +++ b/group21/231525696/src/day20170219/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package day20170219; + +/** + * 左边比父小 + * 右边比父大 + * @author RedKnife + * + */ +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/group21/231525696/src/day20170219/Iterator.java b/group21/231525696/src/day20170219/Iterator.java new file mode 100644 index 000000000..0fefbfc9e --- /dev/null +++ b/group21/231525696/src/day20170219/Iterator.java @@ -0,0 +1,10 @@ +package day20170219; + +public interface Iterator { + + public boolean hasNext(); + + public Object next(); + + public Object remove(); +} diff --git a/group21/231525696/src/day20170219/JavaTest.java b/group21/231525696/src/day20170219/JavaTest.java new file mode 100644 index 000000000..ca9ddc595 --- /dev/null +++ b/group21/231525696/src/day20170219/JavaTest.java @@ -0,0 +1,32 @@ +package day20170219; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Stack; +import java.util.Queue; + +public class JavaTest { + public static void main(String[] args) { + int[] a = new int[10]; + a[0] = 0; + a[1] = 1; + a[2] = 2; + a[9] = 9; + for(int i = 0; i < a.length; i++){ + System.out.println(a[i]); + } + } + + public static int[] grow(int[] src, int size){ + + //return Arrays.copyOf(src, src.length + size); + List list = new LinkedList(); + Stack stack = new Stack(); + LinkedList ls = new LinkedList(); + int[] target = new int[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + + return target; + } +} diff --git a/group21/231525696/src/day20170219/LinkedList.java b/group21/231525696/src/day20170219/LinkedList.java new file mode 100644 index 000000000..a43a445d2 --- /dev/null +++ b/group21/231525696/src/day20170219/LinkedList.java @@ -0,0 +1,123 @@ +package day20170219; + +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(Object o, int index){ + + } + + public Object get(int index){ + checkSize(); + return getNode(index).data; + } + + public Object remove(int index){ + checkSize(); + final Node c = getNode(index); + final Node p = c.prev; + final Node n = c.next; + p.next = n; + n.prev = p; + c.prev = null; + c.next = null; + return c.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + final Node h = head; + final Node newNode = new Node(null, o, h); + head = newNode; + if(h == null){ + head = newNode; + } else { + h.prev = newNode; + } + } + + public void addLast(Object o){ + final Node l = last; + final Node newNode = new Node(l, o, null); + last = newNode; + if (l == null){ + head = newNode; + } else { + l.next = newNode; + } + size ++; + } + + public Object removeFirst(){ + final Node f = head; + final Object element = head.data; + final Node next = head.next; + f.data = null; + f.next = null; + head = next; + if (next == null){ + last = null; + } else { + next.prev = null; + } + size --; + return element; + } + + public Object removeLast(){ + final Node l = last; + final Object element = last.data; + final Node prev = last.prev; + l.data = null; + l.prev = null; + last = prev; + if (prev == null){ + head = null; + } else { + prev.next = null; + } + return element; + } + + private Node getNode(int index) { + if (index < (size >> 1)) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = last; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + private void checkSize(){ + if(size < 0){ + throw new RuntimeException("out of size"); + } + } + private static class Node{ + + private Node prev; + private Object data; + private Node next; + + Node(Node prev, Object data, Node next){ + this.data = data; + this.next = next; + this.prev = prev; + } + + } +} diff --git a/group21/231525696/src/day20170219/List.java b/group21/231525696/src/day20170219/List.java new file mode 100644 index 000000000..41255c494 --- /dev/null +++ b/group21/231525696/src/day20170219/List.java @@ -0,0 +1,15 @@ +package day20170219; + +public interface List { + + public void add(Object o); + + public void add(Object o, int index); + + public Object get(int index); + + public Object remove(int index); + + public int size(); + +} diff --git a/group21/231525696/src/day20170219/Outer.java b/group21/231525696/src/day20170219/Outer.java new file mode 100644 index 000000000..bea92e3a1 --- /dev/null +++ b/group21/231525696/src/day20170219/Outer.java @@ -0,0 +1,33 @@ +package day20170219; + +public class Outer { + private int age; + private String name; + + private void saySomething(){ + System.out.println("ssk"); + } + + private class InnerClass{ + public InnerClass(){ + name = "chengsc"; + age = 23; + } + + private void display(){ + saySomething(); + System.out.println("name: " + name + ". age: " + age); + } + + private Outer getOuter(){ + return Outer.this; + } + } + public static void main(String[] args) { + + Outer outer = new Outer(); + Outer.InnerClass inner = outer.new InnerClass(); + inner.display(); + inner.getOuter().saySomething(); + } +} diff --git a/group21/231525696/src/day20170219/Queue.java b/group21/231525696/src/day20170219/Queue.java new file mode 100644 index 000000000..97e352e44 --- /dev/null +++ b/group21/231525696/src/day20170219/Queue.java @@ -0,0 +1,28 @@ +package day20170219; + +/** + * 队列,先进先出 + * @author RedKnife + * + */ +public class Queue { + + private LinkedList elementData; + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeLast(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } + +} diff --git a/group21/231525696/src/day20170219/Stack.java b/group21/231525696/src/day20170219/Stack.java new file mode 100644 index 000000000..bc5a8570d --- /dev/null +++ b/group21/231525696/src/day20170219/Stack.java @@ -0,0 +1,38 @@ +package day20170219; + +/** + * 先进后出,自顶向下 + * @author RedKnife + * + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + + private int currentIndex = 0; + + + public Object push(Object o){ + elementData.add(o); + currentIndex ++; + return o; + } + + public Object pop(){ + Object ob = peek(); + elementData.remove(currentIndex-1); + return ob; + } + + public Object peek(){ + return elementData.get(currentIndex-1); + } + + public boolean isEmpty(){ + return currentIndex == 0 ; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group21/231525696/src/day20170222/Recursion.java b/group21/231525696/src/day20170222/Recursion.java new file mode 100644 index 000000000..415dcab85 --- /dev/null +++ b/group21/231525696/src/day20170222/Recursion.java @@ -0,0 +1,29 @@ +package day20170222; + +public class Recursion { + + public static void main(String[] args) { + System.out.println(getResult(5,1)); + System.out.println(getResult2(5)); + } + + //尾递归,每次调用自己时没有表达式(需要等待下次调用的返回值来计算),而是一个确切的数值,运行时,jvm会一直使用一个栈桢 + public static int getResult(int n, int result){ + if(n == 1){ + return result; + } else { + return getResult(n-1, n*result); + } + } + + //普通递归,调用自己时传递的是个表达式,需要等待下次调用的返回值来进行计算,会造成一定要递归进最深层获得确切返回值, + //再返回上一层计算上一层,依次类推,到最上层计算出最后结果。 + //每调用一次带表达式的递归,内存中会有一个指向的栈桢,递归链太长,栈桢过大,内存溢出 + public static int getResult2 (int n){ + if (n == 1) { + return 1; + } else { + return n * getResult2 (n-1); + } + } +} diff --git a/group21/231525696/src/day20170226/homework/array/ArrayUtil.java b/group21/231525696/src/day20170226/homework/array/ArrayUtil.java new file mode 100644 index 000000000..6c46686d8 --- /dev/null +++ b/group21/231525696/src/day20170226/homework/array/ArrayUtil.java @@ -0,0 +1,227 @@ +package day20170226.homework.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +/** + * All method are static. + * + * Auth by RedKnife on on 2017-03-05 11:21:51 + */ +public class ArrayUtil { + public static void main(String[] args) { + } + + //sence thread not safety, :( + //inefficient when multithread + private static volatile List container = new ArrayList(); + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + if (origin == null || origin.length == 0){ + return ; + } + int dstIndex = origin.length - 1; + for (int i = 0; i < (origin.length / 2) ; i++, dstIndex--){ + int temp = origin[i]; + origin[i] = origin[dstIndex]; + origin[dstIndex] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if (oldArray == null || oldArray.length == 0){ + return oldArray; + } + container.clear(); + + for (int i = 0; i < oldArray.length; i++){ + if(oldArray[i] != 0) { + container.add(oldArray[i]); + } + } + + return listToArray(container); + } + + /** + * 给定两个已经排序好的整形数组, 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 + */ + //can sort and merge + public static int[] merge(int[] array1, int[] array2){ + LinkedList container = new LinkedList(); + int searchLength = array1.length; + + for (int i = 0; i < array1.length; i++) { + if(insertSort(container, array1[i], i)){ + searchLength--; + } + } + + for (int i = 0; i < array2.length; i++) { + if(insertSort(container, array2[i], searchLength + i)){ + searchLength--; + } + } + + if (container.size() == 0){ + return array1; + } + + return listToArray(container); + } + + public static boolean insertSort(List dst, int src, int dstIndex){ + if (dstIndex > 0 && src == dst.get(dstIndex - 1)) { + return true; + } + if (dstIndex > 0 && src <= dst.get(dstIndex - 1)) { + dstIndex--; + return insertSort(dst, src, dstIndex); + } else { + dst.add(dstIndex , src); + return false; + } + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + if (size < oldArray.length || size == 0){ + throw new RuntimeException("not enough length"); + } + 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 + */ + //definition: Fa = 0, Fb = 1, Fn = Fa(n-1)+Fb(n-2)(n >= 2, n ∈ N*). + public static int[] fibonacci(int max){ + if (max <= 1){ + return new int[]{}; + } + container.clear(); + container.add(1); + container.add(1); + int fa = 1, fb = 1, fn = 0; + for (int i = 1; i < max; i ++) { + fn = fa + fb; + fa = fb; + fb = fn; + if(fn > max){ + break; + } + container.add(fn); + } + return listToArray(container); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + + container.clear(); + + outer : for (int i = 2; i < max; i++) {//the number to judge + for (int j = 2; j <= Math.sqrt(i); j++) {//if is primes + if (i%j == 0) { + continue outer; + } + } + container.add(i); + } + return listToArray(container); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + int number = 0; + int factor = 0; + int adder = 0; + container.clear(); + for (number = 1; number < max; number++) { + adder = 0; + for (factor = 1; factor < number; factor++) { + if(number % factor == 0) { + adder += factor; + if(adder == number && number/factor == 2) { + container.add(number); + } + } + } + } + + return listToArray(container); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + if (array == null || array.length == 0){ + return ""; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if ( i != array.length - 1) { + sb.append(seperator); + } + } + return sb.toString(); + } + + public static int[] listToArray(List list){ + int[] result = new int[list.size()]; + for (int i = 0; i < result.length; i++){ + result[i] = list.get(i); + } + return result; + } + +} diff --git a/group21/231525696/src/day20170226/homework/array/ArrayUtilTest.java b/group21/231525696/src/day20170226/homework/array/ArrayUtilTest.java new file mode 100644 index 000000000..06729b241 --- /dev/null +++ b/group21/231525696/src/day20170226/homework/array/ArrayUtilTest.java @@ -0,0 +1,78 @@ +package day20170226.homework.array; + +import static org.junit.Assert.assertArrayEquals; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author RedKnife on on 2017-03-05 13:11:23 + */ +public class ArrayUtilTest { + + private int[] origin; + private int[] dst; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() { + origin = new int[]{1, 99, 30, 55, 40, 13}; + dst = new int []{13, 40, 55, 30, 99, 1}; + ArrayUtil.reverseArray(origin); + assertArrayEquals(dst, origin); + } + + @Test + public void testRemoveZero() { + origin = new int[] {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + dst = new int[] {1,3,4,5,6,6,5,4,7,6,7,5}; + assertArrayEquals(dst, ArrayUtil.removeZero(origin)); + } + + @Test + public void testMerge() { + int[] a1 = new int[] {7, 3, 4, 8}; + int[] a2 = new int[] {6, 4, 1, 5, 7}; + int[] result = new int[] {1,3,4,5,6,7,8}; + + assertArrayEquals(result, ArrayUtil.merge(a1, a2)); + } + + @Test + public void testGrow() { + int[] oldArray = new int[]{2,3,6}; + assertArrayEquals(new int[]{2,3,6,0,0,0}, ArrayUtil.grow(oldArray, 3)); + } + + @Test + public void testFibonacci() { + assertArrayEquals(new int[]{1,1,2,3,5,8,13}, ArrayUtil.fibonacci(15)); + } + + @Test + public void testGetPrimes() { + assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, ArrayUtil.getPrimes(23)); + } + + @Test + public void testGetPerfectNumbers() { + assertArrayEquals(new int[]{6,28,496}, ArrayUtil.getPerfectNumbers(497)); + } + + + @Test + public void testJoin() { + Assert.assertEquals("3-8-9", ArrayUtil.join(new int[]{3, 8, 9}, "-")); + } + + +} diff --git a/group21/231525696/src/day20170226/homework/struts/LoginAction.java b/group21/231525696/src/day20170226/homework/struts/LoginAction.java new file mode 100644 index 000000000..7f6b93c16 --- /dev/null +++ b/group21/231525696/src/day20170226/homework/struts/LoginAction.java @@ -0,0 +1,45 @@ +package day20170226.homework.struts; + +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/group21/231525696/src/day20170226/homework/struts/Struts.java b/group21/231525696/src/day20170226/homework/struts/Struts.java new file mode 100644 index 000000000..e29874eef --- /dev/null +++ b/group21/231525696/src/day20170226/homework/struts/Struts.java @@ -0,0 +1,94 @@ +package day20170226.homework.struts; + +import java.io.InputStream; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import common.BeanHelper; + +/** + * 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字段中。 + * + * @author RedKnife on 2017-03-05 15:22:23 +*/ +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + View view = new View(); + InputStream is = Struts.class.getResourceAsStream("/day20170226/homework/struts/struts.xml"); + SAXReader reader = new SAXReader(); + Document doc; + + try { + doc = reader.read(is); + Element root = doc.getRootElement(); + //action node + Element actionNode = getNode("action", "name", actionName, root); + if (actionNode == null) { + throw new RuntimeException("do not have this action: " + actionName); + } + //get Class + Class c = Class.forName(actionNode.attributeValue("class")); + //invoke by map's value + Object ob = BeanHelper.invokeClassFromMap(c, parameters); + //resultNode's name + String resultName = String.valueOf(BeanHelper.excuteBeanMethod(ob, "execute", null)); + + Element resultNode = getNode("result", "name", resultName, actionNode); + + //package view + view.setJsp(resultNode.getTextTrim()); + view.setParameters(BeanHelper.getBeanFields(ob)); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } + + return view; + } + + /** + * Find the 'eleName' node from the parent and attribute's value = value + * @param eleName node's name + * @param attrName node's attribute name + * @param value attribute's value + * @param parent parent node + * @return + */ + public static Element getNode(String eleName, String attrName, String value, Element parent){ + List actions = parent.elements(eleName); + for(Element activeNode : actions){ + if(activeNode.attributeValue(attrName).equals(value)){ + return activeNode; + } + } + return null; + } +} diff --git a/group21/231525696/src/day20170226/homework/struts/StrutsTest.java b/group21/231525696/src/day20170226/homework/struts/StrutsTest.java new file mode 100644 index 000000000..cfa80b625 --- /dev/null +++ b/group21/231525696/src/day20170226/homework/struts/StrutsTest.java @@ -0,0 +1,38 @@ +package day20170226.homework.struts; + +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/group21/231525696/src/day20170226/homework/struts/View.java b/group21/231525696/src/day20170226/homework/struts/View.java new file mode 100644 index 000000000..59c16c5a4 --- /dev/null +++ b/group21/231525696/src/day20170226/homework/struts/View.java @@ -0,0 +1,23 @@ +package day20170226.homework.struts; + +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/group21/231525696/src/day20170226/homework/struts/struts.xml b/group21/231525696/src/day20170226/homework/struts/struts.xml new file mode 100644 index 000000000..a3d2b860d --- /dev/null +++ b/group21/231525696/src/day20170226/homework/struts/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/group21/231525696/src/qinzechao.txt b/group21/231525696/src/qinzechao.txt new file mode 100644 index 000000000..e69de29bb diff --git a/group21/28394073/homework/src/com/coderising/array/ArrayUtil.java b/group21/28394073/homework/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 000000000..358968cfa --- /dev/null +++ b/group21/28394073/homework/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,266 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + /*Yang - 为了写Junit用例,将返回类型改成int[]*/ + public int[] reverseArray(int[] origin){ + int tail = origin.length; + for(int i=0;i<(int)origin.length/2;i++){ + int temp = origin[i]; + origin[i] = origin[--tail]; + origin[tail] = 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 count = 0; + int leng = oldArray.length; + for(int i=0;i primeList = new ArrayList(); + if(max==1){ + return null; + }else{ + for(int n=2;n<=max;n++){ + if((int)Math.sqrt(n)<2){ + primeList.add(n); + count++; + + } + else{ + int i=2; + for(;i<=(int)Math.sqrt(n);i++){ + if(n%i==0){ + break; + } + } + if(i>(int)Math.sqrt(n)){ + primeList.add(n); + count++; + } + }//else + }//for + } + + //将ArrayList里面的值取出来,放到数组里 + int[] result = new int[count]; + for(int e=0;e parameters) throws DocumentException, ClassNotFoundException, ReflectiveOperationException, InstantiationException { + + /* + + 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 classValue = null;//为什么一定要初始化null? + + //0.Open,read file and get corresponding class + File file = new File("src/com/coderising/litestruts/struts.xml"); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();){ + Element element = (Element) iter.next(); + Attribute nameAttr = element.attribute("name"); + String attrValue = nameAttr.getValue(); + if(attrValue.equals(actionName)){ + Attribute classAttr = element.attribute("class"); + classValue= classAttr.getValue(); + System.out.println(classValue); + }//end if + }//end for + + //1. + Class reflectClass = Class.forName(classValue); +// System.out.println(reflectClass.getName()); + Object obj = reflectClass.newInstance(); + Field nameField = reflectClass.getDeclaredField("name"); + Field passwordField = reflectClass.getDeclaredField("password"); + nameField.setAccessible(true); + passwordField.setAccessible(true); + nameField.set(obj, parameters.get("name")); + passwordField.set(obj, parameters.get("password")); +// System.out.println(nameField.get(obj)); +// System.out.println(passwordField.get(obj)); + + //2. + Method method = reflectClass.getMethod("execute"); + String result = method.invoke(obj).toString(); + + //3. + Field msgField = reflectClass.getDeclaredField("message"); + msgField.setAccessible(true); + String msgValue = msgField.get(obj).toString(); +// System.out.println(msgField.get(obj)); + Map viewParameters = new HashMap(); + viewParameters.put("message", msgValue); + + View view = new View(); + view.setParameters(viewParameters); + + //4. + String s1 = "//action[@name="+"'"+actionName+"'"+"]"+"/result[@name="+"'"+result+"'"+"]"; + System.out.println(s1); + Element node = (Element)doc.selectSingleNode(s1); + System.out.println(node.getText()); + view.setJsp(node.getText()); + + return view; + + } + + public static void main(String[] args) throws Exception{ + String actionName="login"; + Map parameters = new HashMap(); + parameters.put("name", "test"); + parameters.put("password", "1234"); + Struts.runAction(actionName, parameters); + } + +} \ No newline at end of file diff --git a/group21/28394073/homework/src/com/coderising/litestruts/StrutsTest.java b/group21/28394073/homework/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 000000000..941967c6b --- /dev/null +++ b/group21/28394073/homework/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +package com.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() throws ReflectiveOperationException, InstantiationException, DocumentException, ReflectiveOperationException { + + 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 ReflectiveOperationException, InstantiationException, DocumentException, ReflectiveOperationException { + 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/group21/28394073/homework/src/com/coderising/litestruts/View.java b/group21/28394073/homework/src/com/coderising/litestruts/View.java new file mode 100644 index 000000000..07df2a5da --- /dev/null +++ b/group21/28394073/homework/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/group21/28394073/homework/src/com/coderising/litestruts/struts.xml b/group21/28394073/homework/src/com/coderising/litestruts/struts.xml new file mode 100644 index 000000000..6f23f0a83 --- /dev/null +++ b/group21/28394073/homework/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/group21/28394073/homework/src/com/coding/basic/ArrayList.java b/group21/28394073/homework/src/com/coding/basic/ArrayList.java new file mode 100644 index 000000000..1f185736f --- /dev/null +++ b/group21/28394073/homework/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group21/28394073/homework/src/com/coding/basic/BinaryTreeNode.java b/group21/28394073/homework/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 000000000..d7ac82019 --- /dev/null +++ b/group21/28394073/homework/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/group21/28394073/homework/src/com/coding/basic/Iterator.java b/group21/28394073/homework/src/com/coding/basic/Iterator.java new file mode 100644 index 000000000..06ef6311b --- /dev/null +++ b/group21/28394073/homework/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/group21/28394073/homework/src/com/coding/basic/LinkedList.java b/group21/28394073/homework/src/com/coding/basic/LinkedList.java new file mode 100644 index 000000000..e2c4e5e79 --- /dev/null +++ b/group21/28394073/homework/src/com/coding/basic/LinkedList.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group21/28394073/homework/src/com/coding/basic/List.java b/group21/28394073/homework/src/com/coding/basic/List.java new file mode 100644 index 000000000..10d13b583 --- /dev/null +++ b/group21/28394073/homework/src/com/coding/basic/List.java @@ -0,0 +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(); +} diff --git a/group21/28394073/homework/src/com/coding/basic/Queue.java b/group21/28394073/homework/src/com/coding/basic/Queue.java new file mode 100644 index 000000000..36e516e26 --- /dev/null +++ b/group21/28394073/homework/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group21/28394073/homework/src/com/coding/basic/Stack.java b/group21/28394073/homework/src/com/coding/basic/Stack.java new file mode 100644 index 000000000..a5a04de76 --- /dev/null +++ b/group21/28394073/homework/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group21/3029423192/.classpath b/group21/3029423192/.classpath new file mode 100644 index 000000000..fb5011632 --- /dev/null +++ b/group21/3029423192/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group21/3029423192/.gitignore b/group21/3029423192/.gitignore new file mode 100644 index 000000000..ae3c17260 --- /dev/null +++ b/group21/3029423192/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group21/3029423192/.project b/group21/3029423192/.project new file mode 100644 index 000000000..d14d213e0 --- /dev/null +++ b/group21/3029423192/.project @@ -0,0 +1,17 @@ + + + 3029423192Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/3029423192/src/zhangpc2017_01/MyArrayList.java b/group21/3029423192/src/zhangpc2017_01/MyArrayList.java new file mode 100644 index 000000000..9c5c72062 --- /dev/null +++ b/group21/3029423192/src/zhangpc2017_01/MyArrayList.java @@ -0,0 +1,101 @@ +import java.util.Scanner; + +public class MyArrayList implements MyList { + + private int num = 0;//用来记录集合中的元素个数 + private int size = 0; + Scanner sc = new Scanner(System.in); + private int arrayLength = sc.nextInt(); + + private Object[] elementDate = new Object[arrayLength]; + + //直接在后面增加元素 + public void add(Object o){ + if(size <= elementDate.length){ + elementDate[size] = o; + }else{ + elementDate = new Object[arrayLength+5]; + elementDate[size] =o; + } + size ++; + } + + + //在指定位置添加元素,能自增加 + public void add(int index,Object o){ + try{ + //当插入位置为空时 + if(elementDate[index-1] == null){ + elementDate[index-1] = o; + } + + //插入位置不为空但最后一个位置为空 + if(elementDate[index] != null && elementDate[arrayLength-1] == null){ + + //index+1后面的元素后移 + for(int x = arrayLength-1; x>= index; x--){ + elementDate[x-1] = elementDate[x]; + } + elementDate[index-1] = o; + } + + //插入位置和最后一个位置均不为空 + if(elementDate[index] != null && elementDate[arrayLength-1] !=null){ + //溢出自增5 + elementDate = new Object[arrayLength+5]; + for(int x = elementDate.length-1; x>=index; x--){ + elementDate[x-1] = elementDate[x]; + } + elementDate[index-1] = o; + } + num++; + }catch(Exception e){ + System.out.println("捕获异常信息为"+e.getMessage()); + } + } + + //获取第index元素 + public Object get(int index){ + Object flag; + try{ + flag = elementDate[index]; + }catch(Exception e){ + flag = null; + System.out.println("捕获异常信息为"+e.getMessage()); + } + return flag; + } + + //删除第index元素,返回第index个元素 + public Object remove(int index){ + Object flag; + try{ + if(index == arrayLength-1){ + flag = elementDate[index]; + elementDate[index] = null; + } + else if(index >=0 && index < elementDate.length-1){ + flag = elementDate[index]; + for(int x = index; x < elementDate.length-1; x++){ + elementDate[index] = elementDate[index+1]; + } + }else{ + flag = null; + } + num--; + }catch(Exception e){ + System.out.println("捕获异常信息为"+e.getMessage()); + flag = null; + } + return flag; + } + + //返回列表中的元素个数 + public int size(){ + return num; + } + + public MyIterator myiterator(){ + return null; + } +} diff --git a/group21/3029423192/src/zhangpc2017_01/MyBinaryTreeNode.java b/group21/3029423192/src/zhangpc2017_01/MyBinaryTreeNode.java new file mode 100644 index 000000000..1471572aa --- /dev/null +++ b/group21/3029423192/src/zhangpc2017_01/MyBinaryTreeNode.java @@ -0,0 +1,58 @@ +package zhangpc2017_01; + +public class MyBinaryTreeNode { + private Object data; + + //递归定义,左二叉树和右二叉树 + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public MyBinaryTreeNode getLeft() { + return left; + } + public void setLeft(MyBinaryTreeNode left) { + this.left = left; + } + public MyBinaryTreeNode getRight() { + return right; + } + public void setRight(MyBinaryTreeNode right) { + this.right = right; + } + + public MyBinaryTreeNode insert(Object o){ + + //创建对象调用compareTo方法,btn为父节点 + MyBinaryTreeNode btn = new MyBinaryTreeNode(); + btn.insertNode(btn,o); + return btn; + } + + private void insertNode(MyBinaryTreeNode parentNode,Object o){ + if(compareTo(parentNode, o)<=0){ + if(parentNode.right == null){ + parentNode.right.data = o; + return;//跳出if + } + insertNode(parentNode.right,o); + }else{ + if(parentNode.left == null){ + parentNode.left.data = o; + return;//跳出if + } + insertNode(parentNode.left,o); + } + } + + public int compareTo(MyBinaryTreeNode parentNode,Object o) { + //多态,强制类型转换 Object--Integer + return (Integer)parentNode.data - (Integer)o; + } + +} diff --git a/group21/3029423192/src/zhangpc2017_01/MyIterator.java b/group21/3029423192/src/zhangpc2017_01/MyIterator.java new file mode 100644 index 000000000..a4efd6491 --- /dev/null +++ b/group21/3029423192/src/zhangpc2017_01/MyIterator.java @@ -0,0 +1,5 @@ +public interface MyIterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group21/3029423192/src/zhangpc2017_01/MyLinkedList.java b/group21/3029423192/src/zhangpc2017_01/MyLinkedList.java new file mode 100644 index 000000000..a4ce27bec --- /dev/null +++ b/group21/3029423192/src/zhangpc2017_01/MyLinkedList.java @@ -0,0 +1,174 @@ +public class MyLinkedList implements MyList { + + //定义了一个头结点,里面包含data和next(相当于指针) + Node head; + + private int size = 0;//为了统计元素个数 + + //构建链表对象 + private Node[] elementDate = new Node[10]; + + @Override + //在尾部增加一个节点 + public void add(Object o) { + // TODO Auto-generated method stub + Node n = new Node(o); + if(head == null){ + head = n; + head.next = null; + }else{ + if(size0 && sizeelementDate.length){ + elementDate = new Node[elementDate.length+5]; + elementDate[size].data = n.data; + n.next = null; + } + size++; + } + + //将链表的头元素删除 + public Object removeFirst(){ + Object flag; + if(head == null){ + flag = null; + }else{ + flag = head.data; + head = head.next; + } + size--; + return flag; + } + + //将链表的尾删除 + public Object removeLast(){ + Object flag; + if(head == null){ + flag = null; + }else{ + if(size=length) { + elementData = grow(elementData, length); + } + last++; + elementData[last] = obejct; + size++; + } + + public void add(int index, Object object) { + rangeCheckAdd(index); + if (last> length-1) { + elementData = grow(elementData, length); + } + System.arraycopy(elementData, index, elementData, index + 1, last + - index + 1); + elementData[index] = object; + last++; + size++; + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object object = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, last - index); + last--; + size--; + return object; + + } + + public int size() { + return size; + } + + public Object[] grow(Object[] src, int length) { + Object[] target = new Object[src.length + length]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + private void rangeCheck(int index) { + if (index > last) + throw new IndexOutOfBoundsException(); + } + private void rangeCheckAdd(int index) { + if(index > last+1) + throw new IndexOutOfBoundsException(); + } + public Iterator iterator() { + + return new ArrayIterator(); + } + private class ArrayIterator implements Iterator{ + int next=-1; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return next!=last; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + next++; + return get(next); + } + + } + +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/ArrayListTest.java b/group21/315752375/homework20170219/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 000000000..1626f386e --- /dev/null +++ b/group21/315752375/homework20170219/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void testArrayList() { + fail("Not yet implemented"); + ArrayList arrayList=new ArrayList(); + } + + @Test + public void testArrayListInt() { + fail("Not yet implemented"); + ArrayList arrayList=new ArrayList(10); + } + + @Test + public void testAddObject() { + fail("Not yet implemented"); + ArrayList arrayList=new ArrayList(); + arrayList.add("1"); + System.out.println(arrayList.get(0)); + } + + @Test + public void testAddIntObject() { + fail("Not yet implemented"); + ArrayList arrayList=new ArrayList(); + arrayList.add(0,"1"); + arrayList.add(1,"2"); + arrayList.add(2,"3"); + arrayList.add(3,"4"); + arrayList.add(4,"5"); + System.out.println(arrayList.get(4)); + arrayList.add(4,"6"); + System.out.println(arrayList.get(4)); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + ArrayList arrayList=new ArrayList(); + arrayList.add(0,"1"); + arrayList.add(1,"2"); + arrayList.add(2,"3"); + arrayList.add(3,"4"); + arrayList.add(4,"5"); + System.out.println(arrayList.get(4)); + arrayList.add(4,"6"); + System.out.println(arrayList.get(4)); + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + ArrayList arrayList=new ArrayList(); + arrayList.add(0,"1"); + arrayList.add(1,"2"); + arrayList.add(2,"3"); + arrayList.add(3,"4"); + arrayList.add(4,"5"); + System.out.println(arrayList.get(4)); + arrayList.remove(4); + //System.out.println(arrayList.get(4)); + + } + + @Test + public void testSize() { + fail("Not yet implemented"); + ArrayList arrayList=new ArrayList(); + System.out.println(arrayList.size()); + arrayList.add("sdfasdf"); + System.out.println(arrayList.size()); + } + + @Test + public void testGrow() { + fail("Not yet implemented"); + ArrayList arrayList=new ArrayList(1); + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("2"); + arrayList.add("2"); + arrayList.add("2"); + } + + @Test + public void testIterator() { + ArrayList arrayList=new ArrayList(); + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("3"); + arrayList.add("4"); + arrayList.add("5"); + Iterator iterator=arrayList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/BinaryTree.java b/group21/315752375/homework20170219/src/com/coding/basic/BinaryTree.java new file mode 100644 index 000000000..4d1503fea --- /dev/null +++ b/group21/315752375/homework20170219/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,110 @@ +package com.coding.basic; + +public class BinaryTree { + private BinaryTreeNode root; + private BinaryTreeNode compareNode=root; + + public BinaryTree() { + root = null; + System.out.println("success"); + } + + public void Insert(BinaryTreeNode newNode) { + if (compareNode== null) { + compareNode= newNode; + root=newNode; + System.out.println("ڵ"+newNode.data); + } else if (compareNode.data.compareTo(newNode.data) > 0) { + if (compareNode.left == null) { + compareNode.left = newNode; + System.out.println(compareNode.data+"ӽڵ"+newNode.data); + compareNode=root; + } else{ + compareNode=compareNode.left; + Insert(newNode); + } + } else { + if (compareNode.right == null) { + compareNode.right = newNode; + System.out.println(compareNode.data+"ӽڵ"+newNode.data); + compareNode=root; + } else{ + compareNode=compareNode.right; + Insert(compareNode.left); + } + } + } + + public BinaryTreeNode getRoot() { + return root; + } + + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + public void preOrder(BinaryTreeNode treeNode) {//ǰ + if(treeNode!=null) + { + System.out.print(treeNode.data+"-"); + preOrder(treeNode.left); + preOrder(treeNode.right); + } + + } + public void inOrder(BinaryTreeNode treeNode) { + if(treeNode!=null){ + inOrder(treeNode.left); + System.out.print(treeNode.data+"-"); + inOrder(treeNode.right); + } + } + public void postOrder(BinaryTreeNode treeNode){ + if(treeNode!=null){ + postOrder(treeNode.left); + postOrder(treeNode.right); + System.out.print(treeNode.data+"-"); + } + } + + public static class BinaryTreeNode { + private T data; + private BinaryTreeNode left = null; + private BinaryTreeNode right = null; + + public BinaryTreeNode() { + } + + public T getData() { + return data; + } + + public void setData(T 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(T data) { + this.data = data; + } + + public int compareTo(BinaryTreeNode compareNode) { + return this.data.compareTo(compareNode.data); + } + + } +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/BinaryTreeTest.java b/group21/315752375/homework20170219/src/com/coding/basic/BinaryTreeTest.java new file mode 100644 index 000000000..6996e0a01 --- /dev/null +++ b/group21/315752375/homework20170219/src/com/coding/basic/BinaryTreeTest.java @@ -0,0 +1,44 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.BinaryTree.BinaryTreeNode; + +public class BinaryTreeTest { + + @Test + public void testBinaryTree() { + BinaryTree binaryTree=new BinaryTree(); + } + + @Test + public void testInsert() { + BinaryTree binaryTree=new BinaryTree(); + BinaryTree.BinaryTreeNode newNode=new BinaryTreeNode(); + newNode.setData(80); + binaryTree.Insert(newNode); + BinaryTree.BinaryTreeNode newNode1=new BinaryTreeNode(); + newNode1.setData(100); + binaryTree.Insert(newNode1); + BinaryTree.BinaryTreeNode newNode2=new BinaryTreeNode(); + newNode2.setData(50); + binaryTree.Insert(newNode2); + BinaryTree.BinaryTreeNode newNode3=new BinaryTreeNode(); + newNode3.setData(12); + binaryTree.Insert(newNode3); + BinaryTree.BinaryTreeNode newNode4=new BinaryTreeNode(); + newNode4.setData(25); + binaryTree.Insert(newNode4); + System.out.print("˳"); + binaryTree.preOrder(binaryTree.getRoot()); + System.out.println(); + System.out.print("˳"); + binaryTree.inOrder(binaryTree.getRoot()); + System.out.println(); + System.out.print("˳"); + binaryTree.postOrder(binaryTree.getRoot()); + } + +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/Iterator.java b/group21/315752375/homework20170219/src/com/coding/basic/Iterator.java new file mode 100644 index 000000000..06ef6311b --- /dev/null +++ b/group21/315752375/homework20170219/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/group21/315752375/homework20170219/src/com/coding/basic/LinkedList.java b/group21/315752375/homework20170219/src/com/coding/basic/LinkedList.java new file mode 100644 index 000000000..34f50a48a --- /dev/null +++ b/group21/315752375/homework20170219/src/com/coding/basic/LinkedList.java @@ -0,0 +1,98 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head;//ͷ + private int size;//Ԫظ + public LinkedList() { + head = new Node(); + head.next=null; + size=0; + } + public void add(Object o){ + addLast(o); + + } + public void add(int index , Object o){ + rangeCheckAdd(index); + Node preNode=getNode(index-1); + Node node=new Node(); + node.data=o; + node.next=preNode.next; + preNode.next=node; + size++; + } + private Node getNode(int index) { + rangeCheck(index); + int count=-1; + Node node=head; + while(count!=index){ + node=node.next; + count++; + } + return node; + } + public Object get(int index){ + return getNode(index).data; + } + public Object remove(int index){ + rangeCheck(index); + Node preNode=getNode(index-1); + Node node=preNode.next; + preNode.next=preNode.next.next; + size--; + return node.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node=new Node(); + node.data=o; + node.next=head.next; + head.next=node; + size++; + } + public void addLast(Object o){ + add(size, o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new LinkedIterator(); + } + private void rangeCheck(int index){//ͷΪ-1get + if(index<-1||index>size-1)throw new IndexOutOfBoundsException(); + } + private void rangeCheckAdd(int index){//add + if(index<0||index>size)throw new IndexOutOfBoundsException(); + } + + private static class Node{ + Object data; + Node next; + } + private class LinkedIterator implements Iterator{ + Node nextNode=head.next; + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return nextNode!=null; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + Object object=nextNode.data; + nextNode=nextNode.next; + return object; + } + + } +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/LinkedListTest.java b/group21/315752375/homework20170219/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 000000000..e692aa761 --- /dev/null +++ b/group21/315752375/homework20170219/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,138 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + + + + +public class LinkedListTest { + + @Test + public void testLinkedList() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + } + + @Test + public void testAddObject() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + linkedList.add("1"); + } + + @Test + public void testAddIntObject() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + linkedList.add(0,"2"); + } + @Test + public void testGet() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + linkedList.add(0,"2"); + System.out.println(linkedList.get(0)); + } + @Test + public void testRemove() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + linkedList.add(0,"2"); + System.out.println(linkedList.size()); + linkedList.remove(0); + System.out.println(linkedList.size()); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + System.out.println(linkedList.size()); + linkedList.add(0,"2"); + linkedList.add(0,"2"); + linkedList.add(0,"2"); + linkedList.add(0,"2"); + linkedList.add(0,"2"); + linkedList.add(0,"2"); + linkedList.add(0,"2"); + linkedList.add(0,"2"); + System.out.println(linkedList.size()); + linkedList.remove(0); + System.out.println(linkedList.size()); + } + + @Test + public void testAddFirst() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + linkedList.add(0,"1"); + linkedList.add(1,"2"); + linkedList.addFirst("3"); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + System.out.println(linkedList.get(2)); + } + + @Test + public void testAddLast() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + linkedList.add(0,"1"); + linkedList.add(1,"2"); + linkedList.addLast("3"); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + System.out.println(linkedList.get(2)); + + } + + @Test + public void testRemoveFirst() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + linkedList.add(0,"1"); + linkedList.add(1,"2"); + linkedList.addLast("3"); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + System.out.println(linkedList.get(2)); + linkedList.removeFirst(); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + } + + @Test + public void testRemoveLast() { + fail("Not yet implemented"); + LinkedList linkedList=new LinkedList(); + linkedList.add(0,"1"); + linkedList.add(1,"2"); + linkedList.addLast("3"); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + System.out.println(linkedList.get(2)); + linkedList.removeLast(); + System.out.println(linkedList.get(0)); + System.out.println(linkedList.get(1)); + } + + @Test + public void testIterator() { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,"1"); + linkedList.add(1,"2"); + linkedList.add(2,"3"); + linkedList.add(3,"4"); + linkedList.add(4,"5"); + linkedList.add(5,"6"); + linkedList.add(6,"7"); + linkedList.add(7,"8"); + Iterator iterator=linkedList.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/List.java b/group21/315752375/homework20170219/src/com/coding/basic/List.java new file mode 100644 index 000000000..10d13b583 --- /dev/null +++ b/group21/315752375/homework20170219/src/com/coding/basic/List.java @@ -0,0 +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(); +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/Queue.java b/group21/315752375/homework20170219/src/com/coding/basic/Queue.java new file mode 100644 index 000000000..5eaf69ce4 --- /dev/null +++ b/group21/315752375/homework20170219/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + LinkedList linkedList=new LinkedList(); + public void enQueue(Object o){ + linkedList.addLast(o); + } + + public Object deQueue(){ + return linkedList.removeFirst(); + } + public boolean isEmpty(){ + return linkedList.size()==0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group21/315752375/homework20170219/src/com/coding/basic/Stack.java b/group21/315752375/homework20170219/src/com/coding/basic/Stack.java new file mode 100644 index 000000000..783dfe7f3 --- /dev/null +++ b/group21/315752375/homework20170219/src/com/coding/basic/Stack.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group21/315752375/homework20170226/.classpath b/group21/315752375/homework20170226/.classpath new file mode 100644 index 000000000..05cf0dba9 --- /dev/null +++ b/group21/315752375/homework20170226/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group21/315752375/homework20170226/.gitignore b/group21/315752375/homework20170226/.gitignore new file mode 100644 index 000000000..dbc5ccaae --- /dev/null +++ b/group21/315752375/homework20170226/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/.settings diff --git a/group21/315752375/homework20170226/.project b/group21/315752375/homework20170226/.project new file mode 100644 index 000000000..b571c262e --- /dev/null +++ b/group21/315752375/homework20170226/.project @@ -0,0 +1,17 @@ + + + homework20170226 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtil.java b/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 000000000..535582111 --- /dev/null +++ b/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,242 @@ +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) { + int length = origin.length; + int tmp = 0; + for (int i = 0; i < length / 2; i++) { + tmp = origin[i]; + origin[i] = origin[length - i - 1]; + 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 last = 0;// 记录最后一个元素的位置 + int length = oldArray.length; + int zero = 0;// 记录数组中第一个0的位置 + int nonzero = 1;// 记录zero后面第一个非零的位置 + while (zero < length - 1) { + if (oldArray[zero] == 0) { + // System.out.println("zero:" + zero); + // nonzero = zero + 1; + while (nonzero < length) { + if (oldArray[nonzero] != 0) + break; + nonzero++; + } + // System.out.println("nonzero:" + nonzero); + if (nonzero >= length) + break; + oldArray[zero] = oldArray[nonzero]; + oldArray[nonzero] = 0; + last = zero; + // System.out.println(last); + } else + zero++; + } + int[] target = new int[last + 1]; + // System.out.println(target.length); + System.arraycopy(oldArray, 0, target, 0, last + 1); + 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 static int[] merge(int[] array1, int[] array2) { + int index1 = 0;// 指向下一个元素 + int index2 = 0;// 指向下一个元素 + int len1 = array1.length; + int len2 = array2.length; + int[] target = new int[len1 + len2]; + int index = 0;// 指向下一个空位置 + int min = 0; + while (index1 < len1 && index2 < len2) { + if (array1[index1] < array2[index2]) { + target[index++] = array1[index1++]; + } else { + target[index++] = array2[index2++]; + } + } + if (index1 >= len1) + System.arraycopy(array2, index2, target, index, len2 - index2); + else + System.arraycopy(array1, index1, target, index, len1 - index1); + return target; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + if (size < 0) + throw new IllegalArgumentException(); + System.out.println("asdfwefaw"); + int len = oldArray.length; + int[] target = new int[len + size]; + System.arraycopy(oldArray, 0, target, 0, len); + return target; + } + + /** + * 斐波那契数列为: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 first = 0;// 记录第一个数 + int[] target = new int[max]; + int count = 0; + int i = 1;// 记录后一个数 + while (i < max) { + int tmp = i; + target[count++] = i; + i = i + first; + first = tmp; + } + int[] answer = new int[count]; + System.arraycopy(target, 0, answer, 0, count); + return answer; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) {// 素数筛选法 + boolean[] primes = new boolean[max]; + int[] target = new int[max / 2]; + int count = 0; + for (int i = 1; i < max; i += 2) {// 全部偶数默认false,奇数设为true + primes[i] = true; + } + primes[2] = true;// 2为偶数但是是素数 + for (int i = 3; i < (int) Math.sqrt(max); i += 2) {// 0,1非素数非合数,还有2是素数,所以从3开始 + if (primes[i]) + for (int j = i + i; j < max; j += i) {// 将i的整倍数不是素数,设为false + primes[j] = false; + } + } + for (int i = 2; i < max; i++) {// 最后所有下标从2开始的true为素数 + if (primes[i]) + target[count++] = i; + } + int[] answer = new int[count]; + System.arraycopy(target, 0, answer, 0, count); + return answer; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + long start = System.currentTimeMillis(); + int sum = 0; + int[] array = new int[50]; + int count = 0; + int count1 = 5; + for (int i = 1; i < max;) { + try { +// System.out.println("i:" + i + " sum:" + sum); + if (i % 10 != 6 && i % 10 != 8) { + continue; + } else if (i != 6) { +// System.out.println(i + " " + i % 3 + " " + i % 9); + if (i % 3 != 1 || i % 9 != 1) { + continue; + } + } + sum = 1;// 1已一定为因子 + // System.out.print("数"+i+"的因子:"); + // System.out.print(1+" "); + for (int j = 2; j < Math.sqrt(i); j++) { + if (i % j == 0) { + sum += j; + sum += i / j; + // System.out.print(j+" "+i/j+" "); + } + + } + if (Math.abs(Math.sqrt(i) - (int) (Math.sqrt(i))) == 0) { + sum += (int) Math.sqrt(i); + } + // System.out.println(); +// System.out.println("i:" + i + " sum:" + sum); + if (sum == i) + array[count++] = i; + } finally { + if (i < 28) + i++; + else { + i = i + (int) Math.pow(count1, 3); + count1 += 2; +// System.out.println(i); + } + } + } + + int[] answer = new int[count]; + System.arraycopy(array, 0, answer, 0, count); + long cost = System.currentTimeMillis()-start; + System.out.println("cost time: "+cost+"ms"); + + return answer; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + + int len = array.length; + String answer = ""; + for (int i = 0; i < len - 1; i++) { + answer += array[i]; + answer += seperator; + } + answer += array[len - 1]; + + return answer; + } + +} diff --git a/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtilTest.java b/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 000000000..9943556a3 --- /dev/null +++ b/group21/315752375/homework20170226/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,104 @@ +package com.coderising.array; + + +import java.util.Arrays; + +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + System.out + .println("-----------------------这是华丽的分割线------------------------"); + System.out.println("test reverseArray(int[] array)"); + int[] src = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + System.out.println("values of arrays:" + Arrays.toString(src)); + ArrayUtil.reverseArray(src); + System.out.println("after reversing" + Arrays.toString(src)); + } + + @Test + public void testRemoveZero() { + System.out + .println("-----------------------这是华丽的分割线------------------------"); + System.out.println("test removeZero(int[] array)"); + int[] src = { 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0 }; + System.out.println("values of arrays:" + Arrays.toString(src)); + int[] target = ArrayUtil.removeZero(src); + System.out.println("after removings:" + Arrays.toString(target)); + } + + @Test + public void testMerge() { + System.out + .println("-----------------------这是华丽的分割线------------------------"); + System.out.println("test merge(int[] array1,int[] array2)"); + int[] merge1 = { 1, 3, 4, 6, 8, }; + int[] merge2 = { 0, 2, 3, 4, 5, 7, 9 }; + System.out.println("sorted array1:" + Arrays.toString(merge1)); + System.out.println("sorted array2:" + Arrays.toString(merge2)); + int[] target = new int[merge1.length + merge2.length]; + target = ArrayUtil.merge(merge1, merge2); + System.out.println("after merging and sorting:" + Arrays.toString(target)); + } + + @Test + public void testGrow() { + System.out + .println("-----------------------这是华丽的分割线------------------------"); + System.out.println("test grow(int[] array,int size)"); + int[] oldArrays = { 1, 3, 4, 6, 8, }; + int size = 10; + System.out.println("size of oldArray:" + oldArrays.length); + System.out.println("size grows " + size); + oldArrays = ArrayUtil.grow(oldArrays, size); + System.out.println("after growing:" + oldArrays.length); + } + + @Test + public void testFibonacci() { + System.out + .println("-----------------------这是华丽的分割线------------------------"); + System.out.println("test fibonacci(int max)"); + int num=19; + System.out.print("小于 "+num+" 的斐波那契数列:"); + System.out.println(Arrays.toString(ArrayUtil.fibonacci(15))); + } + + @Test + public void testGetPrimes() { + System.out + .println("-----------------------这是华丽的分割线------------------------"); + System.out.println("test getPrimes(int max)"); + int num = 10000; + System.out.println("小于 " + num + " 的素数有:" + + Arrays.toString(ArrayUtil.getPrimes(num))); + } + + @Test + public void testGetPerfectNumbers() { + System.out + .println("-----------------------这是华丽的分割线------------------------"); + System.out.println("test getPrimes(int max)"); + int num=33550337; + System.out.println("小于 "+num+" 的完数有:"+Arrays.toString(ArrayUtil.getPerfectNumbers(num))); + } + + @Test + public void testJoin() { + System.out + .println("-----------------------这是华丽的分割线------------------------"); + System.out.println("test join(int[] array, String seperator)"); + int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; + String seperator="+"; + System.out.println("use "+"\""+seperator+"\""+" to join array"); + System.out.println("old array:"+Arrays.toString(array)); + System.out.println("after joining:"+ArrayUtil.join(array, "-")); + } + +} diff --git a/group21/315752375/homework20170226/src/com/coderising/litestruts/LoginAction.java b/group21/315752375/homework20170226/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 000000000..dcdbe226e --- /dev/null +++ b/group21/315752375/homework20170226/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/group21/315752375/homework20170226/src/com/coderising/litestruts/Struts.java b/group21/315752375/homework20170226/src/com/coderising/litestruts/Struts.java new file mode 100644 index 000000000..7f0c0dc22 --- /dev/null +++ b/group21/315752375/homework20170226/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,106 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + public static View runAction(String actionName, + Map parameters) { + View view = new View(); + try { + InputStream inputStream = null; + inputStream = new FileInputStream(new File( + "./src/com/coderising/litestruts/struts.xml")); + SAXReader saxReader = new SAXReader(); + Document document = null; + document = saxReader.read(inputStream); + Element root = document.getRootElement();//根节点struts + Iterator actions = root.elements("action").iterator();//变量用于存储所有action的节点 + Element element = null; + while (actions.hasNext()) {//获得name为参数actionName的action节点 + element = actions.next(); + if (actionName.equals(element.attributeValue("name"))) { + break; + } + + } + Map result = new HashMap<>();//变量用于存储action下result的name和value; + Iterator results = element.elements("result").iterator();//获得name为参数actionName的action节点的所有result节点的迭代器 + while (results.hasNext()) {//读取所有的action下result的name和value; + Element tmp = results.next(); + String resultname = tmp.attributeValue("name"); + String resultvalue = tmp.getText(); + result.put(resultname, resultvalue); + } + inputStream.close(); + String clazz = element.attributeValue("class");//获得actionName对应action的class属性对应的值 +// System.out.println(clazz); + Object object = null; + object = Class.forName(clazz).newInstance();// + int len = parameters.size(); + Method method =null; + Iterator> map = parameters.entrySet() + .iterator(); + while (map.hasNext()) {//取得所有parameters中的map,调用上面类实例中map中key值的setter,设为value; + Map.Entry tmp = map.next(); + String namemethod = "set" + + tmp.getKey().substring(0, 1).toUpperCase() + + tmp.getKey().substring(1); + method = object.getClass().getMethod(namemethod, + String.class); + String namevalue = tmp.getValue(); + method.invoke(object, namevalue); + } + Method execute = null; + String loginresult = null;//登陆结果"success",or"fail" + execute = object.getClass().getMethod("execute"); + + loginresult = (String) execute.invoke(object); + view.setJsp(result.get(loginresult)); + int count=object.getClass().getDeclaredMethods().length; + Map viewParameters=new HashMap<>(); + while(count--!=0){//获取所有的getter + method=object.getClass().getDeclaredMethods()[count]; + String mName=method.getName(); + if(mName.startsWith("get")){ + String value=(String)method.invoke(object); + String key=(String)mName.substring(3,4).toLowerCase()+mName.substring(4); + viewParameters.put(key, value); + } + } + view.setParameters(viewParameters); +// System.out.println(view.getJsp()); +// System.out.println(view.getParameters().get("message")); + } catch (Exception e) { + e.printStackTrace(); + } + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + + return view; + } +} diff --git a/group21/315752375/homework20170226/src/com/coderising/litestruts/StrutsTest.java b/group21/315752375/homework20170226/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 000000000..df7c0f105 --- /dev/null +++ b/group21/315752375/homework20170226/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,45 @@ +package com.coderising.litestruts; + + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + System.out.println("---------------这是一条华丽的分割线-------------"); + System.out.println("test Login Action Success"); + 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() { + System.out.println("---------------这是一条华丽的分割线-------------"); + System.out.println("test Login Action Failed"); + + 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/group21/315752375/homework20170226/src/com/coderising/litestruts/View.java b/group21/315752375/homework20170226/src/com/coderising/litestruts/View.java new file mode 100644 index 000000000..07df2a5da --- /dev/null +++ b/group21/315752375/homework20170226/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/group21/315752375/homework20170226/src/com/coderising/litestruts/struts.xml b/group21/315752375/homework20170226/src/com/coderising/litestruts/struts.xml new file mode 100644 index 000000000..e5d9aebba --- /dev/null +++ b/group21/315752375/homework20170226/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/group21/315752375/homework20170226/src/dom4j-1.6.1.jar b/group21/315752375/homework20170226/src/dom4j-1.6.1.jar new file mode 100644 index 000000000..c8c4dbb92 Binary files /dev/null and b/group21/315752375/homework20170226/src/dom4j-1.6.1.jar differ diff --git "a/group21/315752375/\344\273\213\347\273\215.txt" "b/group21/315752375/\344\273\213\347\273\215.txt" new file mode 100644 index 000000000..68a4528a6 --- /dev/null +++ "b/group21/315752375/\344\273\213\347\273\215.txt" @@ -0,0 +1 @@ +this is a test file \ No newline at end of file diff --git a/group21/361141326/README.md b/group21/361141326/README.md new file mode 100644 index 000000000..e3cf295e5 --- /dev/null +++ b/group21/361141326/README.md @@ -0,0 +1,2 @@ +## 2017编程提高 + diff --git a/group21/361141326/pom.xml b/group21/361141326/pom.xml new file mode 100644 index 000000000..3e00d3f84 --- /dev/null +++ b/group21/361141326/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + + com.coding + zzx + 1.0-SNAPSHOT + + 2017 编程提高 + + + junit + junit + 4.12 + test + + + + \ No newline at end of file diff --git a/group21/361141326/src/main/java/com/coding/basic/ArrayList.java b/group21/361141326/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 000000000..dea019c50 --- /dev/null +++ b/group21/361141326/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,134 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + /** + * 默认初始化大小 + */ + private final static int DEFAULT_SIZE = 10; + + /** + * 默认增加的固定长度,该值后期可以改成与size相关的值 + * 原ArrayList中采用length >> 1 + */ + private final static int DEFAULT_STEP = 10; + + private final static int MAX_SIZE = Integer.MAX_VALUE - 8; + + public ArrayList(int initCapacity) { + // 初始化 + elementData = new Object[initCapacity <= 0 ? DEFAULT_SIZE : initCapacity]; + } + + public ArrayList() { + this(DEFAULT_SIZE); + } + + public void add(T t) { + sizeCheck(size + 1); + elementData[size ++] = t; + } + + public void add(int index, T o) { + rangeCheck(index); + sizeCheck(size + 1); + + // 将index开始的元素向后移动一位,利用copy + // 拷贝长度为size - index + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + // 赋值 + elementData[index] = o; + size ++; + } + + public T get(int index) { + rangeCheck(index); + return elementData(index); + } + + public T remove(int index) { + rangeCheck(index); + + T origin = elementData(index); + + // 从index + 1到最后一个元素的长度 + int num = size - (index + 1); + System.arraycopy(elementData, index + 1, elementData, index, num); + + size--; + return origin; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator { + + /** + * 当前迭代器位置 + */ + private int currentPoint = 0; + + @Override + public boolean hasNext() { + return currentPoint < size; + } + + @Override + public T next() { + return get(currentPoint ++); + } + } + + private void sizeCheck(int expectSize) { + if (expectSize > elementData.length) grow(); + } + + @SuppressWarnings("unchecked") + private T elementData(int index) { + return (T) elementData[index]; + } + + /** + * 改变elementData容量大小 + */ + private void grow() { + int currentSize = elementData.length; + int newSize = MAX_SIZE - currentSize > DEFAULT_STEP ? currentSize+ DEFAULT_STEP : MAX_SIZE ; + elementData = Arrays.copyOf(elementData, newSize); + } + + /** + * 长度校验 + * @param index 指定index + */ + private void rangeCheck(int index) { + if (size < index) throw new ArrayIndexOutOfBoundsException(); + } + + /** + * 方便输出查看 + * @return 格式化后的内容 + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + for (int i = 0; i < size; i++){ + sb.append(elementData[i]); + if (i < size - 1) sb.append(","); + } + return sb.append("]").toString(); + } +} diff --git a/group21/361141326/src/main/java/com/coding/basic/BinaryTreeNode.java b/group21/361141326/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 000000000..d7ac82019 --- /dev/null +++ b/group21/361141326/src/main/java/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/group21/361141326/src/main/java/com/coding/basic/Iterator.java b/group21/361141326/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 000000000..13fa4422f --- /dev/null +++ b/group21/361141326/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + + boolean hasNext(); + + T next(); +} diff --git a/group21/361141326/src/main/java/com/coding/basic/LinkedList.java b/group21/361141326/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 000000000..807d93149 --- /dev/null +++ b/group21/361141326/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,200 @@ +package com.coding.basic; + +/** + * 单向链表 + */ +public class LinkedList implements List { + + /** + * 首节点 + */ + private Node head; + + /** + * 尾节点 + */ + private Node last; + + private int size; + + public void add(T t) { + addLast(t); + } + + public void add(int index, T t) { + rangeCheck(index); + + Node previous = node(index); + Node next = previous.next; + + Node node = new Node<>(previous, t, next); + previous.next = node; + next.previous = node; + + size ++; + } + + public T get(int index) { + rangeCheck(index); + + return node(index).data; + } + + public T remove(int index) { + rangeCheck(index); + + Node node = node(index); + node.previous.next = node.next; + + size --; + return node.data; + } + + public int size() { + return size; + } + + public void addFirst(T t) { + Node h = head; + Node node = new Node<>(null, t, h); + head = node; + if (h == null) { + // 初始化尾节点 + last = node; + } else { + // 原有第一个节点,变为第二个节点 + h.previous = node; + } + + size++; + } + + public void addLast(T t) { + Node l = last; + Node node = new Node<>(last, t, null); + last = node; + if (l == null) { + // 初始化首节点 + head = node; + } else { + // 原有的最后一个节点,变为倒数第二个节点 + l.next = node; + } + + size++; + } + + public T removeFirst() { + if (head == null) return null; + + Node next = head.next; + T data = head.data; + head = next; + if (next == null) { + last = null; + } else { + // 首节点前一个节点为空 + next.previous = null; + } + + size--; + return data; + } + + public T removeLast() { + if (last == null) return null; + + Node previous = last.previous; + T data = last.data; + last = previous; + + if (previous == null) { + head = previous; + } else { + previous.next = null; + } + + size--; + return data; + } + + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator { + + /** + * 迭代器当前指向的位置 + */ + private int currentPoint; + + @Override + public boolean hasNext() { + return currentPoint < size; + } + + @Override + public T next() { + return get(currentPoint ++); + } + } + + private void rangeCheck(int index) { + if (index > size - 1) throw new IndexOutOfBoundsException("index:" + index + ", size:" + size); + } + + /** + * 获取对应的元素,如果i > size / 2 则从尾节点开始遍历,反之则从首节点开会遍历, 避免index在最后,顺序循环的浪费性能 + * + * @param index 指定的位置 + * @return 元素 + */ + private Node node(int index) { + if (index > (size / 2)) { + Node node = last; + for (int i = size - 1; i > index; i --) { + node = node.previous; + } + return node; + } else { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + } + + private static class Node { + + // 上一个 + Node previous; + + T data; + + // 下一个 + Node next; + + Node(Node pre, T data, Node next) { + this.previous = pre; + this.data = data; + this.next = next; + } + + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + Node node = head; + while(node != null) { + sb.append(node.data).append(","); + node = node.next; + } + + sb.delete(sb.length() - 1, sb.length()).append("]"); + + return sb.toString(); + } +} diff --git a/group21/361141326/src/main/java/com/coding/basic/List.java b/group21/361141326/src/main/java/com/coding/basic/List.java new file mode 100644 index 000000000..97acafd19 --- /dev/null +++ b/group21/361141326/src/main/java/com/coding/basic/List.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +public interface List { + + void add(T t); + + void add(int index, T t); + + T get(int index); + + T remove(int index); + + int size(); + + Iterator iterator(); +} diff --git a/group21/361141326/src/main/java/com/coding/basic/Queue.java b/group21/361141326/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 000000000..4d5911e6d --- /dev/null +++ b/group21/361141326/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList queue; + + public Queue() { + queue = new LinkedList<>(); + } + + public void enQueue(T t) { + queue.add(t); + } + + public T deQueue() { + return queue.removeLast(); + } + + public boolean isEmpty() { + return queue.get(0) != null; + } + + public int size() { + return queue.size(); + } + + @Override + public String toString() { + return queue.toString(); + } +} diff --git a/group21/361141326/src/main/java/com/coding/basic/Stack.java b/group21/361141326/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 000000000..7e4325147 --- /dev/null +++ b/group21/361141326/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,33 @@ +package com.coding.basic; + +public class Stack { + + private int size; + + private List elementData; + + public Stack() { + elementData = new ArrayList<>(); + } + + public void push(T t){ + elementData.add(t); + size ++; + } + + public T pop(){ + return elementData.remove(-- size); + } + + public T peek(){ + return elementData.get(size - 1); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group21/361141326/src/test/java/com/coding/basic/ArrayListTest.java b/group21/361141326/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 000000000..d5f221626 --- /dev/null +++ b/group21/361141326/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,99 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by mortimer on 2017/2/25. + * + */ +public class ArrayListTest { + + private List newList() { + return new ArrayList<>(); + } + + @Test + public void testAdd() { + List list = newList(); + Integer expectedValue = 100; + list.add(expectedValue); + + System.out.println(list); + Assert.assertEquals(expectedValue, list.get(0)); + } + + @Test + public void testSize() { + List list = newList(); + + list.add(100); + list.add(90); + list.add(80); + + System.out.println(list); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testRemove() { + List list = newList(); + + for (int i = 0; i < 10; i++) { + list.add(i); + } + + Assert.assertEquals(10, list.size()); + + Integer remove = list.remove(4); + Assert.assertEquals(remove, Integer.valueOf(4)); + Assert.assertEquals(Integer.valueOf(5), list.get(4)); + Assert.assertEquals(9, list.size()); + + System.out.println(list); + } + + @Test + public void testAddAssignIndex() { + List list = newList(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + + Assert.assertEquals(Integer.valueOf(4), list.get(4)); + + list.add(4, 5); + + System.out.println(list); + Assert.assertEquals(Integer.valueOf(5), list.get(4)); + Assert.assertEquals(21, list.size()); + } + + @Test + public void testAddForAutoResize() { + List list = newList(); + + int expectSize = 100; + for (int i = 0; i < expectSize; i++) { + list.add(i); + } + + System.out.println(list); + Assert.assertEquals(expectSize, list.size()); + } + + @Test + public void testIterator() { + List list = newList(); + + for (int i = 0; i < 100; i++) { + list.add(i); + } + + Iterator iterator = list.iterator(); + while(iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} diff --git a/group21/361141326/src/test/java/com/coding/basic/LinkedListTest.java b/group21/361141326/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 000000000..fed183ba4 --- /dev/null +++ b/group21/361141326/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,135 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by mortimer on 2017/2/26. + * + */ +public class LinkedListTest { + + private LinkedList newList() { + return new LinkedList<>(); + } + + @Test + public void testAdd() { + List list = newList(); + list.add(10); + list.add(10); + list.add(10); + + System.out.println(list); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testGet() { + List list = newList(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + + System.out.println(list); + Assert.assertEquals(Integer.valueOf(4), list.get(4)); + } + + @Test + public void testAddByAssign() { + List list = newList(); + + for (int i = 0; i < 10; i++) { + list.add(i); + } + + list.add(4, 4); + + System.out.println(list); + + Assert.assertEquals(Integer.valueOf(4), list.get(4)); + Assert.assertEquals(Integer.valueOf(4), list.get(5)); + } + + @Test + public void testRemove() { + List list = newList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + + list.remove(5); + + System.out.println(list); + + Assert.assertEquals(99, list.size()); + Assert.assertEquals(Integer.valueOf(6), list.get(5)); + } + + @Test + public void testAddFirst() { + LinkedList list = newList(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + + list.addFirst(10); + System.out.println(list); + + Assert.assertEquals(11, list.size()); + Assert.assertEquals(Integer.valueOf(10), list.get(0)); + } + + @Test + public void testAddLast() { + LinkedList list = newList(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + + list.addLast(10); + + System.out.println(list); + Assert.assertEquals(21, list.size()); + Assert.assertEquals(Integer.valueOf(10), list.get(20)); + } + + @Test + public void testRemoveFirst() { + LinkedList list = newList(); + + for (int i = 0; i < 10; i++) { + list.add(i); + } + + list.removeFirst(); + System.out.println(list); + Assert.assertEquals(Integer.valueOf(1), list.get(0)); + } + + @Test + public void testRemoveLast() { + LinkedList list= newList(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + + list.removeLast(); + System.out.println(list); + Assert.assertEquals(9, list.size()); + Assert.assertEquals(Integer.valueOf(8), list.get(8)); + } + + @Test + public void testIterator() { + LinkedList list = newList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + + Iterator iterator = list.iterator(); + while(iterator.hasNext()) { + System.out.println(iterator.next()); + } + } +} diff --git a/group21/361141326/src/test/java/com/coding/basic/QueueTest.java b/group21/361141326/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 000000000..feaaf694b --- /dev/null +++ b/group21/361141326/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by mortimer on 2017/2/26. + * + */ +public class QueueTest { + + @Test + public void testEnQueue() { + Queue queue = new Queue<>(); + + for (int i = 0; i < 10; i++) { + queue.enQueue(i); + } + + System.out.println(queue); + Integer data = queue.deQueue(); + System.out.println(queue); + Assert.assertEquals(data, Integer.valueOf(9)); + } +} diff --git a/group21/361141326/src/test/java/com/coding/basic/StackTest.java b/group21/361141326/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 000000000..f3c8952c9 --- /dev/null +++ b/group21/361141326/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,47 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by mortimer on 2017/2/26. + * + */ +public class StackTest { + + @Test + public void testPush() { + Stack stack = new Stack<>(); + + stack.push(10); + stack.push(11); + stack.push(12); + stack.push(13); + + Assert.assertEquals(4, stack.size()); + } + + @Test + public void testPop() { + Stack stack = new Stack<>(); + + for (int i = 0; i < 10; i++) { + stack.push(i); + } + + Integer pop = stack.pop(); + Assert.assertEquals(Integer.valueOf(9), pop); + } + + @Test + public void testPeek() { + Stack stack = new Stack<>(); + + for (int i = 0; i < 100; i++) { + stack.push(i); + } + + Integer peek = stack.peek(); + Assert.assertEquals(Integer.valueOf(99), peek); + } +} diff --git a/group21/396632540/2017-02/.classpath b/group21/396632540/2017-02/.classpath new file mode 100644 index 000000000..e72ef7c0d --- /dev/null +++ b/group21/396632540/2017-02/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group21/396632540/2017-02/.project b/group21/396632540/2017-02/.project new file mode 100644 index 000000000..452e7b11b --- /dev/null +++ b/group21/396632540/2017-02/.project @@ -0,0 +1,17 @@ + + + MapDemo + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/396632540/2017-02/bin/com/coding/basic/ArrayListTest.class b/group21/396632540/2017-02/bin/com/coding/basic/ArrayListTest.class new file mode 100644 index 000000000..441bf1b86 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/ArrayListTest.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/BinaryTreeNodeOperationImplTest.class b/group21/396632540/2017-02/bin/com/coding/basic/BinaryTreeNodeOperationImplTest.class new file mode 100644 index 000000000..1a2cf13e4 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/BinaryTreeNodeOperationImplTest.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/LinkListTest.class b/group21/396632540/2017-02/bin/com/coding/basic/LinkListTest.class new file mode 100644 index 000000000..c67f758c1 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/LinkListTest.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeNode.class b/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeNode.class new file mode 100644 index 000000000..79738f2a5 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeNode.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeOperation.class b/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeOperation.class new file mode 100644 index 000000000..40def1200 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeOperation.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeOperationImpl.class b/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeOperationImpl.class new file mode 100644 index 000000000..9df68b088 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/binaryTree/BinaryTreeOperationImpl.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/ArrayList$ArrayListIterator.class b/group21/396632540/2017-02/bin/com/coding/basic/list/ArrayList$ArrayListIterator.class new file mode 100644 index 000000000..c20bff79b Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/ArrayList$ArrayListIterator.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/ArrayList.class b/group21/396632540/2017-02/bin/com/coding/basic/list/ArrayList.class new file mode 100644 index 000000000..4b8ea9b71 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/ArrayList.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/Iterator.class b/group21/396632540/2017-02/bin/com/coding/basic/list/Iterator.class new file mode 100644 index 000000000..610823b01 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/Iterator.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList$LinkedListIterator.class b/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList$LinkedListIterator.class new file mode 100644 index 000000000..c7815c1af Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList$LinkedListIterator.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList$Node.class b/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList$Node.class new file mode 100644 index 000000000..a86cbce44 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList$Node.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList.class b/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList.class new file mode 100644 index 000000000..63c22548e Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/LinkedList.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/List.class b/group21/396632540/2017-02/bin/com/coding/basic/list/List.class new file mode 100644 index 000000000..69170722a Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/List.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/Queue.class b/group21/396632540/2017-02/bin/com/coding/basic/list/Queue.class new file mode 100644 index 000000000..4e5f39c6f Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/Queue.class differ diff --git a/group21/396632540/2017-02/bin/com/coding/basic/list/Stack.class b/group21/396632540/2017-02/bin/com/coding/basic/list/Stack.class new file mode 100644 index 000000000..0e2b57031 Binary files /dev/null and b/group21/396632540/2017-02/bin/com/coding/basic/list/Stack.class differ diff --git a/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeNode.java b/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeNode.java new file mode 100644 index 000000000..2497bd7eb --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeNode.java @@ -0,0 +1,51 @@ +package com.coding.basic.binaryTree; + +public class BinaryTreeNode implements Comparable { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + super(); + this.data = data; + } + + 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(BinaryTreeNode o) { + String str = String.valueOf(o.getData()); + int newData = Integer.parseInt(str); + int data = Integer.parseInt(String.valueOf(getData())); + return Integer.compare(data, newData); + } + + @Override + public String toString() { + return "BinaryTreeNode [data=" + data + ", left=" + left + ", right=" + right + "]"; + } + +} diff --git a/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeOperation.java b/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeOperation.java new file mode 100644 index 000000000..1281d0e53 --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeOperation.java @@ -0,0 +1,5 @@ +package com.coding.basic.binaryTree; + +public interface BinaryTreeOperation { + public T insert(T object); +} diff --git a/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeOperationImpl.java b/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeOperationImpl.java new file mode 100644 index 000000000..d855c74e4 --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/binaryTree/BinaryTreeOperationImpl.java @@ -0,0 +1,40 @@ +package com.coding.basic.binaryTree; + +public class BinaryTreeOperationImpl implements BinaryTreeOperation { + + private BinaryTreeNode root; + + private BinaryTreeNode insertNextNode(BinaryTreeNode newNode, BinaryTreeNode compareNode) { + int result = compareNode.compareTo(newNode); + if (result == 0) return compareNode; + BinaryTreeNode nextNode = null; + if (result > 0) { + if (null == compareNode.getLeft()) { + compareNode.setLeft(newNode); + } else { + nextNode = compareNode.getLeft(); + } + } else { + if (null == compareNode.getRight()) { + compareNode.setRight(newNode); + } else { + nextNode = compareNode.getRight(); + } + } + if (null == nextNode) { + return compareNode; + } + return insertNextNode(nextNode, compareNode); + } + + @Override + public BinaryTreeNode insert(BinaryTreeNode node) { + if (null == root) { + root = node; + } else { + insertNextNode(node, root); + } + return node; + } + +} diff --git a/group21/396632540/2017-02/src/com/coding/basic/list/ArrayList.java b/group21/396632540/2017-02/src/com/coding/basic/list/ArrayList.java new file mode 100644 index 000000000..7e717f1d8 --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/list/ArrayList.java @@ -0,0 +1,110 @@ +package com.coding.basic.list; + +import java.util.Arrays; + +public class ArrayList implements List { + static final int length = 10; + private Object[] elementData = new Object[length]; + private int index = 0; + + private Object[] getElementData() { + return elementData; + } + + private void setElementData(Object[] elementData) { + this.elementData = elementData; + } + + private int getIndex() { + return index; + } + + private void setIndex(int index) { + this.index = index; + } + + private void increaseElementDataLength() { + int size = getElementData().length; + if (getIndex() >= size) { + size += length; + setElementData(Arrays.copyOf(getElementData(), size)); + } + } + + public void add(Object o) { + increaseElementDataLength(); + getElementData()[getIndex()] = o; + setIndex(getIndex()+1); + } + + public void add(int index, Object o){ + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("数组越界"); + } else { + if (null != get(size()-1)) { + increaseElementDataLength(); + } + } + Object[] tempElementData = Arrays.copyOfRange(elementData, index, size()); + setIndex(size()+1); + for (int i = 0 ; i < tempElementData.length ; i++) { + int pos = index+i+1; + elementData[pos] = tempElementData[i]; + } + elementData[index] = o; + } + + public Object get(int index) { + return getElementData()[index]; + } + + public Object remove(int index) { + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("数组越界"); + } + Object object = get(index); + // 如果是删除的最后一个元素,就不需要移动前面的元素 + if (index != size()-1) { + Object[] tempElementData = Arrays.copyOfRange(elementData, index+1, size()); + for (int i = 0 ; i < tempElementData.length ; i++) { + elementData[index+i] = tempElementData[i]; + } + } + // 移动元素完成后,收回最后一个元素内存空间 + elementData[index] = null; + setIndex(size()-1); + + // 收回多余的内存空间 + int tempLength = size()%length == 0 ? size() : (size()/length+1)*length; + if (getElementData().length > tempLength) { + setElementData(Arrays.copyOf(getElementData(), tempLength)); + } + return object; + } + + public int size() { + return getIndex(); + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + private int iteratorIndex = 0; + + @Override + public boolean hasNext() { + return iteratorIndex >= size(); + } + + @Override + public Object next() { + Object object = getElementData()[iteratorIndex]; + iteratorIndex++; + return object; + } + + } + +} diff --git a/group21/396632540/2017-02/src/com/coding/basic/list/Iterator.java b/group21/396632540/2017-02/src/com/coding/basic/list/Iterator.java new file mode 100644 index 000000000..88bf0112c --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/list/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic.list; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group21/396632540/2017-02/src/com/coding/basic/list/LinkedList.java b/group21/396632540/2017-02/src/com/coding/basic/list/LinkedList.java new file mode 100644 index 000000000..c7c4b94e2 --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/list/LinkedList.java @@ -0,0 +1,141 @@ +package com.coding.basic.list; + +public class LinkedList implements List { + + private Node head; + private Node last; + private int index; + + private Node getNode(int index) { + Node tempNode = head; + for (int i = 0; i < index; i++) { + tempNode = tempNode.next; + } + return tempNode; + } + + public void add(Object o) { + Node node = new Node(o, null); + if (null == head) { + head = node; + } else { + last.next = node; + } + last = node; + index++; + } + + public void add(int index, Object o) { + if (index > size()) { + throw new ArrayIndexOutOfBoundsException("越界"); + } + if (index == size()) { + addLast(o); + } else if (index == 0) { + addFirst(o); + } else { + Node tempLast = getNode(index-1); + Node newNode = new Node(o, null); + Node tempNext = tempLast.next; + tempLast.next = newNode; + newNode.next = tempNext; + this.index++; + } + } + + public Object get(int index) { + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("越界"); + } + return getNode(index).data; + } + + public Object remove(int index) { + if (index >= size()) { + throw new ArrayIndexOutOfBoundsException("越界"); + } + Object object; + if (index == 0) { + object = head.data; + head = head.next; + } else { + Node lastNode = getNode(index-1); + if (index == size()-1) { + object = last.data; + last = lastNode; + lastNode.next = null; + } else { + Node node = lastNode.next; + Node next = node.next; + lastNode.next = next; + object = node.data; + node = null; + } + } + this.index--; + return object; + } + + public int size() { + return index; + } + + public void addFirst(Object o) { + Node node = new Node(o, null); + if (null != head) node.next = head; + if (null == last) last = node; + head = node; + index++; + } + + public void addLast(Object o) { + Node node = new Node(o, null); + if (null == head) head = node; + last.next = node; + last = node; + index++; + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size()-1); + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { + private Object data; + private Node next; + + public Node(Object data, Node next) { + super(); + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + + private int index = 0; + private Node last = head; + @Override + public boolean hasNext() { + return index < size(); + } + + @Override + public Object next() { + Object object = last.data; + last = last.next; + index++; + return object; + } + + } + +} diff --git a/group21/396632540/2017-02/src/com/coding/basic/list/List.java b/group21/396632540/2017-02/src/com/coding/basic/list/List.java new file mode 100644 index 000000000..7cf51db0a --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/list/List.java @@ -0,0 +1,15 @@ +package com.coding.basic.list; + +public interface List { + public void add(Object o); + + public void add(int index, Object o) throws ArrayIndexOutOfBoundsException; + + public Object get(int index); + + public Object remove(int index); + + public int size(); + + public Iterator iterator(); +} diff --git a/group21/396632540/2017-02/src/com/coding/basic/list/Queue.java b/group21/396632540/2017-02/src/com/coding/basic/list/Queue.java new file mode 100644 index 000000000..a4861c65d --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/list/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic.list; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o){ + linkedList.addFirst(o); + } + + public Object deQueue(){ + return linkedList.removeLast(); + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group21/396632540/2017-02/src/com/coding/basic/list/Stack.java b/group21/396632540/2017-02/src/com/coding/basic/list/Stack.java new file mode 100644 index 000000000..660cab862 --- /dev/null +++ b/group21/396632540/2017-02/src/com/coding/basic/list/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic.list; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return null; + } + + public Object peek() { + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group21/396632540/2017-02/test/com/coding/basic/ArrayListTest.java b/group21/396632540/2017-02/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 000000000..aa8fd45dd --- /dev/null +++ b/group21/396632540/2017-02/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,74 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.coding.basic.list.ArrayList; +import com.coding.basic.list.Iterator; +import com.coding.basic.list.List; + +public class ArrayListTest { + + private List list = new ArrayList(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Test + public void testAddObject() { + for (int i = 0; i < 13; i++) { + list.add(Integer.toString(i)); + } + testIterator(); + } + + @Test + public void testAddIntObject() { + testAddObject(); + System.out.println("testAddIntObject"); + try { + list.add(13, "10000"); + } catch (Exception e) { + // TODO: handle exception + System.out.println("message == "+e.getMessage()); + } finally { + System.out.println("size ==="+list.size()); + } + + testIterator(); + + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + testAddObject(); + System.out.println("testRemove"); + for (int i = 0; i < 3; i++) { + list.remove(0); + } + testAddObject(); + testIterator(); + } + + @Test + public void testIterator() { + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} diff --git a/group21/396632540/2017-02/test/com/coding/basic/BinaryTreeNodeOperationImplTest.java b/group21/396632540/2017-02/test/com/coding/basic/BinaryTreeNodeOperationImplTest.java new file mode 100644 index 000000000..5a61a3e5b --- /dev/null +++ b/group21/396632540/2017-02/test/com/coding/basic/BinaryTreeNodeOperationImplTest.java @@ -0,0 +1,115 @@ +package com.coding.basic; + +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.coding.basic.binaryTree.BinaryTreeNode; +import com.coding.basic.binaryTree.BinaryTreeOperation; +import com.coding.basic.binaryTree.BinaryTreeOperationImpl; + +public class BinaryTreeNodeOperationImplTest { + + private BinaryTreeOperation op = new BinaryTreeOperationImpl(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + + } + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testBinaryTreeNode() { + fail("Not yet implemented"); + } + + @Test + public void testGetData() { + fail("Not yet implemented"); + } + + @Test + public void testSetData() { + fail("Not yet implemented"); + } + + @Test + public void testGetLeft() { + fail("Not yet implemented"); + } + + @Test + public void testSetLeft() { + fail("Not yet implemented"); + } + + @Test + public void testGetRight() { + fail("Not yet implemented"); + } + + @Test + public void testSetRight() { + fail("Not yet implemented"); + } + + @Test + public void testGetRoot() { + fail("Not yet implemented"); + } + + @Test + public void testSetRoot() { + fail("Not yet implemented"); + } + + @Test + public void testInsert() { + BinaryTreeNode node = new BinaryTreeNode("100"); + op.insert(node); + + BinaryTreeNode node1 = new BinaryTreeNode("90"); + op.insert(node1); + BinaryTreeNode node2 = new BinaryTreeNode("110"); + op.insert(node2); + BinaryTreeNode node3 = new BinaryTreeNode("120"); + op.insert(node3); + + BinaryTreeNode node4 = new BinaryTreeNode("80"); + op.insert(node4); + + BinaryTreeNode node5 = new BinaryTreeNode("99"); + op.insert(node5); + + + + System.out.println("getLeft === "+node.getLeft()); + System.out.println("getRight === "+node.getRight()); + } + + @Test + public void testCompareTo() { + BinaryTreeNode node = new BinaryTreeNode("1"); + BinaryTreeNode node1 = new BinaryTreeNode("0"); + System.out.println(node.compareTo(node1)); + } + +} diff --git a/group21/396632540/2017-02/test/com/coding/basic/LinkListTest.java b/group21/396632540/2017-02/test/com/coding/basic/LinkListTest.java new file mode 100644 index 000000000..cadbc75b3 --- /dev/null +++ b/group21/396632540/2017-02/test/com/coding/basic/LinkListTest.java @@ -0,0 +1,99 @@ +package com.coding.basic; + +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.coding.basic.list.Iterator; +import com.coding.basic.list.LinkedList; + +public class LinkListTest { + + private LinkedList list = new LinkedList(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + list.add("setUp"); + testAddFirst(); + list.add(1, "after setUp run testAddFirst()"); + testIterator(); + System.out.println("---------setUp End---------"); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + list.add("testAddObject"); + testIterator(); + testSize(); + testGet(); + } + + @Test + public void testIterator() { + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + + @Test + public void testAddIntObject() { + for (int u = 0; u < 4; u++) { + list.add(u, "testAddIntObject"+Integer.toString(u)); + } + testIterator(); + } + + @Test + public void testGet() { + System.out.println("testGet index==1,data=="+list.get(1)); + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + System.out.println("size ==="+list.size()); + } + + @Test + public void testAddFirst() { + list.addFirst("testAddFirst"); + } + + @Test + public void testAddLast() { + list.addLast("testAddLast"); + } + + @Test + public void testRemoveFirst() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveLast() { + fail("Not yet implemented"); + } + +} diff --git "a/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877797651516.jpg" "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877797651516.jpg" new file mode 100644 index 000000000..c8791524c Binary files /dev/null and "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877797651516.jpg" differ diff --git "a/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877798054456.jpg" "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877798054456.jpg" new file mode 100644 index 000000000..c8791524c Binary files /dev/null and "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877798054456.jpg" differ diff --git "a/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877798824642.jpg" "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877798824642.jpg" new file mode 100644 index 000000000..718bee6ed Binary files /dev/null and "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877798824642.jpg" differ diff --git "a/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877800570460.jpg" "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877800570460.jpg" new file mode 100644 index 000000000..12c5c98f1 Binary files /dev/null and "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14877800570460.jpg" differ diff --git "a/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14878305553788.jpg" "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14878305553788.jpg" new file mode 100644 index 000000000..28d42b38b Binary files /dev/null and "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14878305553788.jpg" differ diff --git "a/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14878305834397.jpg" "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14878305834397.jpg" new file mode 100644 index 000000000..28d42b38b Binary files /dev/null and "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/media/14877768801189/14878305834397.jpg" differ diff --git "a/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/\345\205\263\344\272\216\351\200\222\345\275\222.html" "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/\345\205\263\344\272\216\351\200\222\345\275\222.html" new file mode 100644 index 000000000..85eb5c71c --- /dev/null +++ "b/group21/396632540/\346\226\207\347\253\240/\345\205\263\344\272\216\351\200\222\345\275\222/\345\205\263\344\272\216\351\200\222\345\275\222.html" @@ -0,0 +1,363 @@ + + + + +# 关于递归 + + +
+

关于递归

+ +

什么是递归

+ +

递归(英语:Recursion),又译为递回,在数学与计算机科学中,是指在函数的定义中使用函数自身的方法。递归一词还较常用于描述以自相似方法重复事物的过程。例如,当两面镜子相互之间近似平行时,镜中嵌套的图像是以无限递归的形式出现的。也可以理解为自我复制的过程。下面这张图是递归的一种表现形式:
+

+ +

解释以及图片来自维基百科
+一个好玩的现象,当我们用Google搜索递归时,你会发现
+
+😅这是程序员特有的幽默么!

+ +

普通递归

+ +

关于递归的应用,我以二叉树来举例。

+ +
private BinaryTreeNode insertNextNode(BinaryTreeNode newNode, BinaryTreeNode compareNode) {
+    int result = compareNode.compareTo(newNode);
+    if (result == 0) return compareNode;
+    BinaryTreeNode nextNode = null;
+    if (result > 0) { 
+        if (null == compareNode.getLeft()) {
+            compareNode.setLeft(newNode);
+        } else {
+            nextNode = compareNode.getLeft();
+        }
+    } else { 
+        if (null == compareNode.getRight()) {
+            compareNode.setRight(newNode);
+        } else {
+            nextNode = compareNode.getRight();
+        }
+    }
+    return null == nextNode? compareNode : insertNextNode(nextNode, compareNode);
+}
+
+ +

上面的代码实现了对二叉树插入一个节点的操作。根据递归的定义,实现这种效果采用了递归。递归在内存中逻辑大概是这样样子:
+
+ 图片来自码农翻身
+也就是说,函数的每一次调用都会在内存中形成栈帧。如果计算机发现这个栈帧还没有执行完,或者会依赖下层栈帧,此栈帧就会一直存在。如果栈过大,则会抛出stack over flow异常。
+那上面这段代码执行的过程中,就会遇到上面所述的问题,产生这个问题的代码就在于:

+ +
return null == nextNode? compareNode : insertNextNode(nextNode, compareNode);
+
+ +

上层栈帧的返回函数(三目运算符)依赖了下层栈帧的运算,当没有执行到递归结束条件时,这样的依赖会一直存在。
+ 递归一定要有退出条件,否则就是死循环!
+那么要解决这个问题,就需要用到尾递归。

+ +

尾递归

+ +

我们对上面的代码做出修改,修改后如下:

+ +
private BinaryTreeNode insertNextNode(BinaryTreeNode newNode, BinaryTreeNode compareNode) {
+    // 前面的代码不变,这是修改了返回代码
+    if (null == nextNode) {
+        return compareNode;
+    }
+    return insertNextNode(nextNode, compareNode);
+}
+
+ +

虽然我们只是修改了返回代码,但是对于计算机来说,这就意味着上层栈帧不会依赖下层栈帧。

+ +

总结

+ +

关于递归和尾递归用愚公移山来做比喻:
+普通递归:愚公开始移山了...
+1. 他挖了一点点,但是他不搬走。他让他儿子继续挖,让他儿子告诉他还剩下多少。
+2. 他儿子挖了一点点,也不搬走,让他儿子的儿子继续挖,并且告诉他还剩下多少。
+3. 他儿子的儿子挖了一点点,也不搬走,让他儿子的儿子的儿子继续挖,并且告诉他还剩下多少。
+4. ...
+5. 当最后一个子孙挖了最后一点山,这个时候,这个子孙就会告诉他的父亲,山没啦,他的父亲告诉他的父亲...直到告诉愚公,这个时候,愚公一声令下,搬走,愚公移山就完成了。
+尾递归:愚公开始移山了...
+1. 他挖了一点点,但是他搬走了。他让他儿子继续挖,并且留下了工具。
+2. 他儿子挖了一点点,他也搬走了。他儿子让他儿子继续挖,并且也留下了工具。
+3. 他儿子的儿子挖了一点点,也搬走了,让他儿子的儿子的儿子继续挖,并且也留下了工具。
+3. ...
+4. 当最后一个子孙挖了最后一点山,山没了,愚公移山也就完成了。

+ +
+ + diff --git a/group21/519623727/README.md b/group21/519623727/README.md new file mode 100644 index 000000000..a1b517fd9 --- /dev/null +++ b/group21/519623727/README.md @@ -0,0 +1 @@ +### 大家好,这是fan的文件夹 \ No newline at end of file diff --git a/group21/544168502/test@wanghuan b/group21/544168502/test@wanghuan new file mode 100644 index 000000000..4a5413521 --- /dev/null +++ b/group21/544168502/test@wanghuan @@ -0,0 +1 @@ +test@wanghuan \ No newline at end of file diff --git a/group21/648084221/CainaTest/.gitignore b/group21/648084221/CainaTest/.gitignore new file mode 100644 index 000000000..6fc499b0d --- /dev/null +++ b/group21/648084221/CainaTest/.gitignore @@ -0,0 +1,24 @@ + +*/.DS_Store +.DS_Store + + +*.xml +*.iml +.idea +*.iml +rebel.xml +rebel-remote.xml + +.classpath +.project +.settings +.metadata + +target +*.class + +log +*.log +tmp +*.tmp diff --git a/group21/648084221/CainaTest/src/cc/gzhlaker/blog/ArrayList.java b/group21/648084221/CainaTest/src/cc/gzhlaker/blog/ArrayList.java new file mode 100644 index 000000000..7e146bf4c --- /dev/null +++ b/group21/648084221/CainaTest/src/cc/gzhlaker/blog/ArrayList.java @@ -0,0 +1,111 @@ +package cc.gzhlaker.blog; + +public class ArrayList implements List{ + //------------------------------ + //-Ա + //------------------------------ + public int size; + MyData[] now; + //------------------------------ + //-캯 + //------------------------------ + public ArrayList(){ + size = 0; + now = new MyData[100]; + } + public ArrayList(int length){ + size = 0; + now = new MyData[length]; + } + //------------------------------ + //-߽ + //------------------------------ + public boolean isFill(int size,int length){ + if(size>length){ + return true; + }else if(size<=length){ + return false; + } + return false; + } + //------------------------------ + //-߽չ + //------------------------------ + public MyData[] increaseLength(MyData[] now){ + MyData[] newarray = new MyData[now.length+10]; + System.arraycopy(now, 0, newarray, 0, now.length); + return newarray; + } + + @Override + public void add(int data) { + if(isFill(size+1,now.length)){ + now = increaseLength(now); + } + now[size].number = data; + size ++; + } + + @Override + public void add(int data, int index) { + if(isFill(size+1,now.length)){ + now = increaseLength(now); + } + for(int i=size+1;i>index;i--){ + now[i].number = now[i-1].number; + } + now[index].number = data; + size++; + + + } + + @Override + public void addFirst(int data) { + if(isFill(size+1,now.length)){ + now = increaseLength(now); + } + for(int i=size+1;i>0;i--){ + now[i].number = now[i-1].number; + } + now[0].number = data; + size++; + } + + @Override + public void remove(int index) { + now[size].number = 0; + size--; + } + + @Override + public void removeFirst() { + now[0].number = 0; + for(int i=0;i<=size;i++){ + now[i].number = now[i+1].number; + } + size--; + } + + @Override + public void setData(int data,int index) { + if(index>now.length){ + return; + } + now[index].number = data; + + } + + @Override + public int getData(int index) { + if(index>now.length){ + return -1; + } + int i = now[index].number; + return i; + } + + + + +} diff --git a/group21/648084221/CainaTest/src/cc/gzhlaker/blog/Linkedlist.java b/group21/648084221/CainaTest/src/cc/gzhlaker/blog/Linkedlist.java new file mode 100644 index 000000000..34389bef7 --- /dev/null +++ b/group21/648084221/CainaTest/src/cc/gzhlaker/blog/Linkedlist.java @@ -0,0 +1,110 @@ +package cc.gzhlaker.blog; + +public class Linkedlist implements List { + //------------------------------ + //-ͷΪ0 + //------------------------------ + //-ڲ + //------------------------------ + public static class Node{ + public int data; + public Node next; + public Node(){ + data = 0; + next = null; + } + public Node(int data){ + this.data = data; + next = null; + } + } + //------------------------------ + //-Ա + //------------------------------ + private int size; + Node first; + //------------------------------ + //-캯 + //------------------------------ + public Linkedlist(){ + first = new Node(); + size = 0; + } + //------------------------------ + //-ѯ + //------------------------------ + public int getSize(){ + size = first.data; + return size; + } + + @Override + public void add(int data) { + Node now = first; + while(now.next!=null){ + now = now.next; + } + now.next = new Node(data); + first.data++; + } + + @Override + public void add(int data, int index) { + Node now = first; + int i = 0; + while(ilength){ + return true; + }else if(size<=length){ + return false; + } + return false; + } + //------------------------------ + //- + //------------------------------ + public MyData[] increaseLength(MyData[] now){ + MyData[] newarray = new MyData[now.length+10]; + System.arraycopy(now, 0, newarray, 0, now.length); + return newarray; + } + + @Override + public void add(int data) { + if(isFill(size+1,now.length)){ + now = increaseLength(now); + } + now[size].number = data; + size ++; + } + + @Override + public void add(int data, int index) { + if(isFill(size+1,now.length)){ + now = increaseLength(now); + } + for(int i=size+1;i>index;i--){ + now[i].number = now[i-1].number; + } + now[index].number = data; + size++; + + + } + + @Override + public void addFirst(int data) { + if(isFill(size+1,now.length)){ + now = increaseLength(now); + } + for(int i=size+1;i>0;i--){ + now[i].number = now[i-1].number; + } + now[0].number = data; + size++; + } + + @Override + public void remove(int index) { + now[size].number = 0; + size--; + } + + @Override + public void removeFirst() { + now[0].number = 0; + for(int i=0;i<=size;i++){ + now[i].number = now[i+1].number; + } + size--; + } + + @Override + public void removeEnd(){ + + } + + @Override + public void setData(int data,int index) { + if(index>now.length){ + return; + } + now[index].number = data; + + } + + @Override + public int getData(int index) { + if(index>now.length){ + return -1; + } + int i = now[index].number; + return i; + } + + + + +} diff --git a/group21/648084221/JavaLearnProject/DataStruct/src/cc/gzhlaker/LinkList.java b/group21/648084221/JavaLearnProject/DataStruct/src/cc/gzhlaker/LinkList.java new file mode 100644 index 000000000..9955295f9 --- /dev/null +++ b/group21/648084221/JavaLearnProject/DataStruct/src/cc/gzhlaker/LinkList.java @@ -0,0 +1,124 @@ +package cc.gzhlaker; + +public class LinkList implements List { + //------------------------------ + //- + //------------------------------ + //- + //------------------------------ + public static class Node{ + public int data; + public Node next; + public Node(){ + data = 0; + next = null; + } + public Node(int data){ + this.data = data; + next = null; + } + } + //------------------------------ + //- + //------------------------------ + private int size; + Node first; + //------------------------------ + //- + //------------------------------ + public LinkList(){ + first = new Node(); + size = 0; + } + //------------------------------ + //- + //------------------------------ + public int getSize(){ + size = first.data; + return size; + } + + @Override + public void add(int data) { + Node now = first; + while(now.next!=null){ + now = now.next; + } + now.next = new Node(data); + first.data++; + } + + @Override + public void add(int data, int index) { + Node now = first; + int i = 0; + while(i + + + + + + diff --git a/group21/669808226/.project b/group21/669808226/.project new file mode 100644 index 000000000..fab8d7f04 --- /dev/null +++ b/group21/669808226/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/669808226/.settings/org.eclipse.jdt.core.prefs b/group21/669808226/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..3a2153707 --- /dev/null +++ b/group21/669808226/.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/group21/669808226/bin/struts.xml b/group21/669808226/bin/struts.xml new file mode 100644 index 000000000..171848ecd --- /dev/null +++ b/group21/669808226/bin/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group21/669808226/src/com/coderising/array/ArrayUtil.java b/group21/669808226/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 000000000..91235bb28 --- /dev/null +++ b/group21/669808226/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,265 @@ +package com.coderising.array; + +import java.util.ArrayList; + +import java.security.InvalidParameterException; + +public final class ArrayUtil { + + private 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) throws NullPointerException + { + check(origin); + int len = origin.length; + if(len == 0) + return; + int[] temp = new int[len]; + for(int i = 0; i < len; ++i) + { + temp[i] = origin[len-i-1]; + } + System.arraycopy(temp, 0, origin, 0, len); + } + + /** + * 现在有如下的一个数组: 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) throws NullPointerException + { + check(oldArray); + int len = oldArray.length; + int[] temp= new int[len]; + int size = 0; + if(len == 0) + return temp; + for(int i=0; i= 0 ? index2 + 1 : index2; + size = difference == 0 ? size - 1 : size; + } + int[] result = new int[size]; + System.arraycopy(temp, 0, result, 0, size); + return result; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size) throws NullPointerException + { + check(oldArray); + int len = oldArray.length; + int[] result = new int[len + size]; + System.arraycopy(oldArray, 0, result, 0, len); + 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 static int[] fibonacci(int max) throws InvalidParameterException + { + if(max < 1) + throw new InvalidParameterException("参数不能小于1"); + if(max == 1) + return new int[]{}; + int size = 0; + int[] temp = new int[max]; + for(int i=0; i list = new ArrayList(); + for(int i=6; i parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + final URL uri = Struts.class.getProtectionDomain().getCodeSource().getLocation(); + XMLParser xmlParser = new XMLParser(); + try { + xmlParser.Parse(uri.getPath() + "/struts.xml"); + //查找拥有属性name=actionName的node,并获取其class属性的value + String className = xmlParser.GetNodesByName("action") + .FindNodeByAttribute("name", actionName) + .getAttributeValue("class"); + //通过反射获取class对象并创建实例 + Class actionClass = Class.forName(className); + Object actionInstance = actionClass.newInstance(); + //设置参数 + for(Map.Entry entry : parameters.entrySet()) + { + String setter = "set" + + entry.getKey().substring(0, 1).toUpperCase() + + entry.getKey().substring(1); + Method m = actionClass.getDeclaredMethod(setter, entry.getValue().getClass()); + m.invoke(actionInstance, entry.getValue()); + } + //execute + Method executeMethod = actionClass.getDeclaredMethod("execute"); + String executeResult = executeMethod.invoke(actionInstance).toString(); + //存储执行后的结果 + HashMap hashMap = new HashMap(); + //获取所有getter方法,将其值放入HashMap,key为字段名 + Method[] methods = actionClass.getDeclaredMethods(); + for(Method m : methods) + { + if(m.getName().startsWith("get")) + { + String fieldName = m.getName().substring(3).toLowerCase(); + Object fieldValue = m.invoke(actionInstance); + hashMap.put(fieldName, fieldValue); + } + } + //查找拥有属性name=actionName的node,并获取其子节点 + //然后查找拥有name=executeResult的node,并获取其文本内容 + String jsp = xmlParser.GetNodesByName("action") + .FindNodeByAttribute("name", actionName) + .getChildNodes() + .FindNodeByAttribute("name", executeResult) + .getNode() + .getTextContent(); + View view = new View(); + view.setParameters(hashMap); + view.setJsp(jsp); + return view; + } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + static public void main(String[] args) throws Exception + { + System.out.println("main"); + //Struts.runAction("", parameters) + } + + + +} diff --git a/group21/669808226/src/com/coderising/litestruts/StrutsTest.java b/group21/669808226/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 000000000..b8c81faf3 --- /dev/null +++ b/group21/669808226/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/group21/669808226/src/com/coderising/litestruts/View.java b/group21/669808226/src/com/coderising/litestruts/View.java new file mode 100644 index 000000000..07df2a5da --- /dev/null +++ b/group21/669808226/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/group21/669808226/src/com/coderising/litestruts/XMLParser.java b/group21/669808226/src/com/coderising/litestruts/XMLParser.java new file mode 100644 index 000000000..ddc13667b --- /dev/null +++ b/group21/669808226/src/com/coderising/litestruts/XMLParser.java @@ -0,0 +1,115 @@ +package com.coderising.litestruts; + +import java.io.IOException; +import java.net.URL; + +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 XMLParser { + + private Document doc; + private NodeList nodes; + private Node currentNode; + + public Node getNode() + { + return currentNode; + } + + public NodeList getNodeList() + { + return nodes; + } + + public Document getDocument() + { + return doc; + } + + public XMLParser(){} + + public void Parse(String uri) throws Exception + { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db; + try { + db = dbf.newDocumentBuilder(); + this.doc = db.parse(uri); + nodes = this.doc.getChildNodes(); + if(nodes.getLength() < 1) + { + throw new Exception("Target is empty!"); + } + this.currentNode = nodes.item(0); + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + throw(e); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + throw(e); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + throw(e); + } + } + + public XMLParser GetNodesByName(String name) + { + this.nodes = this.doc.getElementsByTagName(name); + return this; + } + + public XMLParser FindNodeByAttribute(String attrName, String attrValue) throws Exception + { + for(int i = 0; i < this.nodes.getLength(); ++i) + { + Node n = this.nodes.item(i); + if(getAttributeByName(n, attrName).equals(attrValue)) + { + this.currentNode = n; + return this; + } + } + throw new Exception("Target node not found!"); + } + + public String getAttributeValue(String name) + { + return getAttributeByName(this.currentNode, name); + } + + public XMLParser getChildNodes() + { + this.nodes = this.currentNode.getChildNodes(); + return this; + } + + private String getAttributeByName(Node node, String name) + { + String result = ""; + NamedNodeMap attributes = node.getAttributes(); + if(attributes != null) + { + for (int j = 0; j < attributes.getLength(); j++) + { + Node attrNode = attributes.item(j); + if(attrNode.getNodeName().equals(name)) + { + result = attrNode.getNodeValue(); + } + } + } + return result; + } +} diff --git a/group21/669808226/src/com/coderising/litestruts/struts.xml b/group21/669808226/src/com/coderising/litestruts/struts.xml new file mode 100644 index 000000000..dd598a366 --- /dev/null +++ b/group21/669808226/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/group21/669808226/src/com/coding/basic/ArrayList.java b/group21/669808226/src/com/coding/basic/ArrayList.java new file mode 100644 index 000000000..b943a3019 --- /dev/null +++ b/group21/669808226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,125 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private final int EXPAND_LENGTH = 20; + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(o==null) + return; + growSize(); + elementData[size - 1] = o; + } + public void add(int index, Object o){ + if(index < 0 || o == null) + return; + + growSize(); + + if(index >= size - 1) + { + elementData[size - 1] = o; + } + else + { + Object objToMoveAfterward = elementData[index]; + for(int i = index; i < size - 1; ++i) + { + elementData[i] = o; + Object temp = elementData[i+1]; + elementData[i+1]= objToMoveAfterward; + objToMoveAfterward = temp; + } + } + } + + public Object get(int index){ + if(index >=0 && index < elementData.length) + { + return elementData[index]; + } + return null; + } + + public Object remove(int index){ + if(index >=0 && index < size && elementData[index] != null) + { + Object rtn = elementData[index]; + elementData[index] = null; + + for(int i = index; i < size; ++i) + { + elementData[i] = elementData[i + 1]; + } + + --size; + + return rtn; + } + return null; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(this); + } + + private void growSize() + { + ++size; + { + //expand array length by EXPAND_LENGTH + elementData = Arrays.copyOf(elementData, elementData.length + EXPAND_LENGTH); + } + } + + private class ArrayListIterator implements Iterator + { + + private ArrayList data; + + private int index; + + public ArrayListIterator(ArrayList list) + { + if(list != null) + { + this.data = list; + this.index = -1; + } + } + + @Override + public boolean hasNext() { + if(this.data != null) + { + //++index; + if(this.data.size() > index + 1) + { + return true; + } + } + return false; + } + + @Override + public Object next() { + if(this.hasNext()) + { + return this.data.get(++index); + } + return null; + } + + } + +} diff --git a/group21/669808226/src/com/coding/basic/BinaryTreeNode.java b/group21/669808226/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 000000000..00a681a69 --- /dev/null +++ b/group21/669808226/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,60 @@ +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) + { + if(o == null) + return null; + + if(o instanceof Comparable) + { + return insert((Comparable)o); + } + + return null; + } + + private BinaryTreeNode insert(Comparable object) + { + if(object == null) + return null; + + //be the root + if(this.left == null && this.right == null) + { + + } + + return null; + } + +} diff --git a/group21/669808226/src/com/coding/basic/Iterator.java b/group21/669808226/src/com/coding/basic/Iterator.java new file mode 100644 index 000000000..c85412021 --- /dev/null +++ b/group21/669808226/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/group21/669808226/src/com/coding/basic/LinkedList.java b/group21/669808226/src/com/coding/basic/LinkedList.java new file mode 100644 index 000000000..28ba012cc --- /dev/null +++ b/group21/669808226/src/com/coding/basic/LinkedList.java @@ -0,0 +1,221 @@ +package com.coding.basic; + +public class LinkedList implements List { + //head 和 tail 有可能指向同一个对象 + private Node head; + private Node tail; + + private int size = 0; + + + public void add(Object o){ + if(o == null) + return; + + Node nodeToAdd = new LinkedList.Node(); + nodeToAdd.data = o; + + if(this.size == 0) + { + Init(nodeToAdd); + return; + } + + AddTailNode(nodeToAdd); + } + + public void add(int index , Object o){ + //check user input + if(o == null || index < 0) + return; + + Node nodeToAdd = new LinkedList.Node(); + nodeToAdd.data = o; + + if(this.size == 0) + { + Init(nodeToAdd); + return; + } + + //be the tail + if(index >= this.size) + { + AddTailNode(nodeToAdd); + return; + } + + //be the head + if(index == 0) + { + AddHeadNode(nodeToAdd); + return; + } + + Insert(index, nodeToAdd); + } + + + public Object get(int index){ + //check user input + if(index < 0 || index >= this.size) + return null; + + Node rtn = getNode(index); + return rtn.data; + } + + /* + * User needs to validate input + */ + private Node getNode(int index) + { + Node rtn = this.head; + for(int i = 0; i < index; ++i) + { + rtn = rtn.next; + } + return rtn; + } + + public Object remove(int index){ + //check user input + if(index < 0 || index >= this.size) + return null; + + Node nodeToRemove = getNode(index); + + //如果是循环链表,则要通过判等的方式 + if(nodeToRemove.prev == null)//is the head + { + this.head = nodeToRemove.next;//could be null + } + + if(nodeToRemove.next == null)//is the tail + { + this.tail = this.tail.prev;//could be null + } + + --size; + + return nodeToRemove.data; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + this.add(0, o);; + } + + public void addLast(Object o){ + this.add(this.size, o); + } + + public Object removeFirst(){ + return this.remove(0); + } + + public Object removeLast(){ + return this.remove(this.size - 1); + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + /* + * User need to validate input + */ + private void Init(Node node) + { + head = node; + tail = node; + ++size; + } + + /* + * User need to validate input + */ + private void AddHeadNode(Node node) + { + this.head.prev = node; + node.next = this.head; + this.head = node; + ++size; + } + + + /* + * User need to validate input + */ + private void AddTailNode(Node node) { + this.tail.next = node; + node.prev = this.tail; + this.tail = node; + ++size; + } + + /* + * Head and Tail will not be changed + * So 0 < index < this.size && this.size > 1 + * User validate input + */ + private void Insert(int index, Node node) + { + Node prevNode = getNode(index - 1); + Node temp = prevNode.next;//could not possibly be null + prevNode.next = node; + node.prev = prevNode; + node.next = temp; + } + + //static class is not allowed to access it's + //outerclass's instance members without an outer class instance + private static class Node{ + Object data; + Node next; + Node prev; + } + + private class LinkedListIterator implements Iterator + { + private LinkedList data; + + private int index; + + public LinkedListIterator(LinkedList linkedList) + { + if(linkedList != null) + { + this.data = linkedList; + this.index = -1; + } + } + + @Override + public boolean hasNext() { + if(this.data != null) + { + if(this.data.size() > index + 1) + { + return true; + } + } + return false; + } + + @Override + public Object next() { + if(this.hasNext()) + { + return this.data.get(++index); + } + return null; + } + + } + + +} diff --git a/group21/669808226/src/com/coding/basic/List.java b/group21/669808226/src/com/coding/basic/List.java new file mode 100644 index 000000000..10d13b583 --- /dev/null +++ b/group21/669808226/src/com/coding/basic/List.java @@ -0,0 +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(); +} diff --git a/group21/669808226/src/com/coding/basic/Queue.java b/group21/669808226/src/com/coding/basic/Queue.java new file mode 100644 index 000000000..25b08d4c1 --- /dev/null +++ b/group21/669808226/src/com/coding/basic/Queue.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +/* + * 先入先出 + * 链表实现 + * 链表头是队列头 + * 链表尾是队列尾 + */ +public class Queue { + + private LinkedList linkedList; + + public Queue() + { + this.linkedList = new LinkedList(); + } + + //往队列头添加新对象 + public void enQueue(Object o){ + if(o == null) + return; + + this.linkedList.addFirst(o); + } + + //从队列尾移除对象 + public Object deQueue(){ + return this.linkedList.removeLast(); + } + + public boolean isEmpty(){ + return this.size() == 0; + } + + public int size(){ + return this.linkedList.size(); + } +} diff --git a/group21/669808226/src/com/coding/basic/Stack.java b/group21/669808226/src/com/coding/basic/Stack.java new file mode 100644 index 000000000..14b9248cb --- /dev/null +++ b/group21/669808226/src/com/coding/basic/Stack.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +/* + * 先入后出 + * 链表实现 + * 链表头是栈顶,链表尾是栈底 + */ +public class Stack { + + private LinkedList linkedList; + + public Stack() + { + this.linkedList = new LinkedList(); + } + + public void push(Object o){ + this.linkedList.addFirst(o); + } + + public Object pop(){ + return this.linkedList.removeFirst(); + } + + public Object peek(){ + return this.linkedList.get(0); + } + public boolean isEmpty(){ + return this.size() == 0; + } + public int size(){ + return this.linkedList.size(); + } +} diff --git a/group21/669808226/src/com/coding/basic/tests/ArrayListTest.java b/group21/669808226/src/com/coding/basic/tests/ArrayListTest.java new file mode 100644 index 000000000..ac864a554 --- /dev/null +++ b/group21/669808226/src/com/coding/basic/tests/ArrayListTest.java @@ -0,0 +1,240 @@ +/** + * + */ +package com.coding.basic.tests; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + +/** + * @author Jack + * + */ +public class ArrayListTest { + + private ArrayList arrayList; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + arrayList = new ArrayList(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + arrayList = null; + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(java.lang.Object)}. + */ + @Test + public void testAddObject() { + //fail("Not yet implemented"); + arrayList.add(null); + assertEquals(arrayList.size(), 0); + + arrayList.add(new Integer(1024)); + assertEquals(arrayList.size(), 1); + + for(int i=0; i<200; ++i) + { + arrayList.add(new Integer(2048)); + } + assertEquals(arrayList.size(), 201); + + AssertIntegerElement(arrayList.size() - 1, 2048); + + AssertIntegerElement(0, 1024); + } + + private void AssertIntegerElement(int index, int val) { + Object element = arrayList.get(index); + assertNotNull(element); + Integer iFirst = (Integer)element; + assertNotNull(iFirst); + assertEquals(iFirst.intValue(), val); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test + public void testAddIntObject() { + //fail("Not yet implemented"); + arrayList.add(0, null); + assertEquals(arrayList.size(), 0); + + arrayList.add(-1, new Object()); + assertEquals(arrayList.size(), 0); + + arrayList.add(0, new Integer(1024)); + assertEquals(arrayList.size(), 1); + + for(int i=0; i<200; ++i) + { + arrayList.add(i, new Integer(i)); + } + assertEquals(arrayList.size(), 201); + //[0,1,2,3,....198,199,1024] + + AssertIntegerElement(0, 0); + + AssertIntegerElement(arrayList.size() - 2, 199); + + AssertIntegerElement(arrayList.size() - 1, 1024); + + arrayList.add(201, new Integer(201)); + //[0,1,2,3,....198,199,1024,201] + arrayList.add(210, new Integer(210)); + //[0,1,2,3,....198,199,1024,201,210] + arrayList.add(300, new Integer(300)); + //[0,1,2,3,....198,199,1024,201,210,300] + + assertEquals(arrayList.size(), 204);//actual size + + AssertIntegerElement(201,201); + + Object number210Element = arrayList.get(210); + assertNull(number210Element); + + AssertIntegerElement(202,210); + + Object number300Element = arrayList.get(300); + assertNull(number300Element); + + AssertIntegerElement(203,300); + + for(int j=0; j<204; ++j) + { + assertNotNull(arrayList.get(j)); + } + + assertNull(arrayList.get(204)); + assertNull(arrayList.get(209)); + assertNull(arrayList.get(300)); + } + + + /** + * Test method for {@link com.coding.basic.ArrayList#remove(int)}. + */ + @Test + public void testRemove() { + arrayList.add(new Integer(1024)); + //assertEquals(arrayList.size(), 1); + + Object obj1024 = arrayList.remove(0); + assertEquals(arrayList.size(), 0); + + assertNotNull(obj1024); + + Integer int1024 = (Integer)obj1024; + assertNotNull(int1024); + assertEquals(int1024.intValue(), 1024); + + assertNull(arrayList.get(0)); + + for(int i=0; i<200; ++i) + { + arrayList.add(i, new Integer(i)); + } + assertEquals(arrayList.size(), 200); + + arrayList.remove(0); + assertEquals(arrayList.size(), 199); + + AssertIntegerElement(0, 1); + + AssertIntegerElement(arrayList.size() - 1, 199); + + assertNull(arrayList.get(arrayList.size())); + + //verify this:[1,2,3,....,199] + for(int i=0; i<199; ++i) + { + assertEquals(((Integer)arrayList.get(i)).intValue(), i + 1); + } + + Object number198Element = arrayList.remove(198); + assertEquals(arrayList.size(), 198); + assertNull(arrayList.get(198)); + assertEquals(((Integer)number198Element).intValue(), 199); + + Object number5Element = arrayList.remove(5); + assertEquals(arrayList.size(), 197); + assertNull(arrayList.get(197)); + assertEquals(((Integer)number5Element).intValue(), 6); + Object newNumber5Element = arrayList.get(5); + assertEquals(((Integer)newNumber5Element).intValue(), 7); + //Խ��index + Object outofboundElement = arrayList.remove(1024); + assertNull(outofboundElement); + assertEquals(arrayList.size(), 197); + + Object negativeIndexElement= arrayList.remove(-1); + assertNull(negativeIndexElement); + assertEquals(arrayList.size(), 197); + + //fail("Not yet implemented"); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#iterator()}. + */ + @Test + public void testIterator() { + //fail("Not yet implemented"); + + Iterator iter=arrayList.iterator(); + + assertFalse(iter.hasNext()); + assertNull(iter.next()); + + for(int i=0; i<10; ++i) + { + arrayList.add(i, new Integer(i)); + } + + iter=arrayList.iterator(); + + for(int i=0; i<10; ++i) + { + assertTrue(iter.hasNext()); + Object obj = iter.next(); + assertNotNull(obj); + assertEquals(((Integer)obj).intValue(), i); + } + + assertFalse(iter.hasNext()); + assertNull(iter.next()); + } + +} diff --git a/group21/669808226/src/com/coding/basic/tests/LinkedListTest.java b/group21/669808226/src/com/coding/basic/tests/LinkedListTest.java new file mode 100644 index 000000000..9ce89d294 --- /dev/null +++ b/group21/669808226/src/com/coding/basic/tests/LinkedListTest.java @@ -0,0 +1,86 @@ +package com.coding.basic.tests; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + LinkedList linkedList; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @Test + public void testAddObject() { + linkedList.add(new Integer(0)); + assertEquals(linkedList.size(), 1); + } + + @Test + public void testAddIntObject() { + //fail("Not yet implemented"); + } + + @Test + public void testGet() { + //fail("Not yet implemented"); + } + + @Test + public void testRemove() { + //fail("Not yet implemented"); + } + + @Test + public void testSize() { + //fail("Not yet implemented"); + } + + @Test + public void testAddFirst() { + //fail("Not yet implemented"); + } + + @Test + public void testAddLast() { + //fail("Not yet implemented"); + } + + @Test + public void testRemoveFirst() { + //fail("Not yet implemented"); + } + + @Test + public void testRemoveLast() { + //fail("Not yet implemented"); + } + + @Test + public void testIterator() { + //fail("Not yet implemented"); + } + +} diff --git a/group21/669808226/src/com/coding/basic/tests/package-info.java b/group21/669808226/src/com/coding/basic/tests/package-info.java new file mode 100644 index 000000000..d32375f7e --- /dev/null +++ b/group21/669808226/src/com/coding/basic/tests/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author Jack + * + */ +package com.coding.basic.tests; \ No newline at end of file diff --git a/group21/670706603/.classpath b/group21/670706603/.classpath new file mode 100644 index 000000000..fb5011632 --- /dev/null +++ b/group21/670706603/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group21/670706603/.gitignore b/group21/670706603/.gitignore new file mode 100644 index 000000000..ae3c17260 --- /dev/null +++ b/group21/670706603/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group21/670706603/.project b/group21/670706603/.project new file mode 100644 index 000000000..53d950079 --- /dev/null +++ b/group21/670706603/.project @@ -0,0 +1,17 @@ + + + 670706603 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/670706603/liyuhang.txt b/group21/670706603/liyuhang.txt new file mode 100644 index 000000000..eafb06764 --- /dev/null +++ b/group21/670706603/liyuhang.txt @@ -0,0 +1,2 @@ +670706603 +123456 \ No newline at end of file diff --git a/group21/752463008/.classpath b/group21/752463008/.classpath new file mode 100644 index 000000000..d171cd4c1 --- /dev/null +++ b/group21/752463008/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group21/752463008/.gitignore b/group21/752463008/.gitignore new file mode 100644 index 000000000..ae3c17260 --- /dev/null +++ b/group21/752463008/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group21/752463008/.project b/group21/752463008/.project new file mode 100644 index 000000000..2bb939f26 --- /dev/null +++ b/group21/752463008/.project @@ -0,0 +1,17 @@ + + + 752463008Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/898104523/.classpath b/group21/898104523/.classpath new file mode 100644 index 000000000..fb5011632 --- /dev/null +++ b/group21/898104523/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group21/898104523/.gitignore b/group21/898104523/.gitignore new file mode 100644 index 000000000..e660fd93d --- /dev/null +++ b/group21/898104523/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/group21/898104523/.project b/group21/898104523/.project new file mode 100644 index 000000000..476255393 --- /dev/null +++ b/group21/898104523/.project @@ -0,0 +1,17 @@ + + + Learning2017 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group21/898104523/src/com/funnywu/basic/ArrayList.java b/group21/898104523/src/com/funnywu/basic/ArrayList.java new file mode 100644 index 000000000..a2b54bf2a --- /dev/null +++ b/group21/898104523/src/com/funnywu/basic/ArrayList.java @@ -0,0 +1,5 @@ +package com.funnywu.basic; + +public class ArrayList { + +} diff --git a/group21/898104523/src/com/funnywu/basic/LinkedList.java b/group21/898104523/src/com/funnywu/basic/LinkedList.java new file mode 100644 index 000000000..c5a382dff --- /dev/null +++ b/group21/898104523/src/com/funnywu/basic/LinkedList.java @@ -0,0 +1,5 @@ +package com.funnywu.basic; + +public class LinkedList { + +} diff --git a/group21/898104523/src/com/funnywu/basic/Queue.java b/group21/898104523/src/com/funnywu/basic/Queue.java new file mode 100644 index 000000000..6cfd99e92 --- /dev/null +++ b/group21/898104523/src/com/funnywu/basic/Queue.java @@ -0,0 +1,5 @@ +package com.funnywu.basic; + +public class Queue { + +} diff --git a/group21/898104523/src/com/funnywu/basic/Stack.java b/group21/898104523/src/com/funnywu/basic/Stack.java new file mode 100644 index 000000000..6e070660a --- /dev/null +++ b/group21/898104523/src/com/funnywu/basic/Stack.java @@ -0,0 +1,5 @@ +package com.funnywu.basic; + +public class Stack { + +}