From 607da26d6d94e5e9732d7452d1c8b200c175c6b5 Mon Sep 17 00:00:00 2001 From: wdn <626451284@163.com> Date: Mon, 13 Mar 2017 21:34:11 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/626451284Learning/pom.xml | 12 - .../wdn/coding2017/basic/ArrayList.java | 88 ------- .../wdn/coding2017/basic/BinaryTreeNode.java | 35 --- .../github/wdn/coding2017/basic/Iterator.java | 10 - .../wdn/coding2017/basic/LinkedList.java | 233 ------------------ .../com/github/wdn/coding2017/basic/List.java | 9 - .../github/wdn/coding2017/basic/Queue.java | 24 -- .../github/wdn/coding2017/basic/Stack.java | 27 -- 8 files changed, 438 deletions(-) delete mode 100644 group24/626451284Learning/pom.xml delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java delete mode 100644 group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java diff --git a/group24/626451284Learning/pom.xml b/group24/626451284Learning/pom.xml deleted file mode 100644 index 534ff39232..0000000000 --- a/group24/626451284Learning/pom.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - 4.0.0 - - com.github.wdn - coding2017 - 1.0-SNAPSHOT - - - \ No newline at end of file diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java deleted file mode 100644 index 3b3ea048f3..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.github.wdn.coding2017.basic; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData; - public ArrayList(){ - elementData = new Object[10]; - } - public ArrayList(int size){ - elementData = new Object[size]; - } - public void add(Object o){ - if(size>=elementData.length){ - elementData = Arrays.copyOf(elementData,elementData.length*2); - } - elementData[size]=o; - size++; - } - public void add(int index, Object o){ - Object[] newElementData; - if(size()+1>=elementData.length){ - newElementData=new Object[elementData.length*2]; - - }else{ - newElementData=new Object[elementData.length]; - } - for (int i = 0; i < elementData.length; i++) { - if(index==1){ - newElementData[i]=o; - }else if(i>index) { - newElementData[i]=elementData[i-1]; - }else{ - newElementData[i]=elementData[i]; - } - } - elementData = newElementData; - size++; - } - - public Object get(int index){ - if(index>=size){ - throw new IndexOutOfBoundsException(); - } - return elementData[index]; - } - - public Object remove(int index) { - if(index>=size){ - throw new IndexOutOfBoundsException(); - } - Object returnO = elementData[index]; - for (int i = index; i < size; i++) { - if(i==size-1){ - elementData[i]=null; - }else { - elementData[i] = elementData[i + 1]; - } - } - size--; - return returnO; - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return null; - } - - public static void main(String[] args) { - ArrayList list = new ArrayList(2); - list.add("1"); - list.add("2"); - list.remove(1); - list.add("3"); - for (int i = 0; i < list.size(); i++) { - System.out.println(list.get(i)); - } - int[] i = {}; - System.out.println(i[0]); - } - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java deleted file mode 100644 index b8b613d6f0..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * TODO 代码在公司电脑里。。。后续提交 - */ -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/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java deleted file mode 100644 index 044ddf0993..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Iterator.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * TODO 主要考虑遍历中安全删除元素的实现 - */ -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java deleted file mode 100644 index 6040cb5a47..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java +++ /dev/null @@ -1,233 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * TODO 只是简单实现 缺少对边界的处理 - * 参考JDK源码改为更优雅的实现 - */ - -public class LinkedList implements List { - - private Node head; - private Node tail; - private int size; - - public LinkedList(){ - this.head=null; - this.tail=null; - } - public void add(Object o){ - Node newNode = new Node(o); - if (head == null) { - head = newNode; - tail = newNode; - }else{ - tail.setNext(newNode); - newNode.setPre(tail); - tail = newNode; - } - size++; - } - public void add(int index , Object o){ - checkIndex(index); - Node indexNode = getNode(index); - Node newNode = new Node(o); - Node pre = indexNode.getPre(); - if(pre!=null){ - pre.setNext(newNode); - }else{ - head = newNode; - } - newNode.setPre(pre); - newNode.setNext(indexNode); - indexNode.setPre(newNode); - } - private void checkIndex(int index){ - if(index >= size || index <0){ - throw new IndexOutOfBoundsException(); - } - } - private Node getNode(int index){ - checkIndex(index); - // TODO这里可以优化,先判断index在前半部还是后半部分 然后确定从头部或者尾部查找 - Node result=null; - if(index==0){ - return head; - } - if(index==size-1){ - return tail; - } - for (int i = 0; i < index; i++) { - result = head.getNext(); - } - return result; - } - public Object get(int index){ - return getNode(index).getData(); - } - public Object remove(int index){ - checkIndex(index); - Node indexNode = getNode(index); - Node pre = indexNode.getPre(); - Node next = indexNode.getNext(); - if(pre!=null){ - pre.setNext(next); - }else{ - head = next; - } - if(next!=null){ - next.setPre(pre); - }else{ - tail = pre; - } - return indexNode.getData(); - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node newNode = new Node(o); - head.setPre(newNode); - newNode.setNext(head); - head = newNode; - } - public void addLast(Object o){ - Node newNode = new Node(o); - tail.setNext(newNode); - newNode.setPre(tail); - tail = newNode; - } - public Object removeFirst(){ - Node next = head.getNext(); - Node oldHead = head; - head = next; - head.setPre(null); - return oldHead; - } - public Object removeLast(){ - Node oldTail = tail; - Node pre = tail.getPre(); - tail = pre; - tail.setNext(null); - return oldTail; - } - public Iterator iterator(){ - - return null; - } - - /** - * JDK 中使用构造方法的方式设置next和pre减少不必要的getset方法更优雅 - */ - private static class Node{ - - Object data; - Node next; - Node pre; - public Node(){ - - } - public Node(Object data){ - this.data = data; - } - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public Node getNext() { - return next; - } - - public void setNext(Node next) { - this.next = next; - } - - public Node getPre() { - return pre; - } - - public void setPre(Node pre) { - this.pre = pre; - } - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java deleted file mode 100644 index d6c9e95cb0..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.wdn.coding2017.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java deleted file mode 100644 index 59992f0cbd..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Queue.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * 队列使用链表实现比较简单理论上是无界队列 - * 如果使用数组需要处理很多问题比如长度限制,元素的复制 - */ -public class Queue { - private LinkedList queue = new LinkedList(); - public void enQueue(Object o){ - queue.add(o); - } - - public Object deQueue(){ - return queue.remove(0); - } - - public boolean isEmpty(){ - return queue.size()==0; - } - - public int size(){ - return queue.size(); - } -} diff --git a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java b/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java deleted file mode 100644 index f0a0fe56e9..0000000000 --- a/group24/626451284Learning/src/main/java/com/github/wdn/coding2017/basic/Stack.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.github.wdn.coding2017.basic; - -/** - * 使用list实现比较简单 - * 可以考虑使用其它结构 - */ -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(); - } -} From 50bbedb60c61a2c31b540e7c5f92ca81ff936c25 Mon Sep 17 00:00:00 2001 From: wdn <626451284@163.com> Date: Mon, 13 Mar 2017 21:37:21 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/626451284/pom.xml | 12 + .../wdn/coding2017/basic/ArrayList.java | 88 +++++++ .../wdn/coding2017/basic/BinaryTreeNode.java | 35 +++ .../github/wdn/coding2017/basic/Iterator.java | 10 + .../wdn/coding2017/basic/LinkedList.java | 233 ++++++++++++++++++ .../com/github/wdn/coding2017/basic/List.java | 9 + .../github/wdn/coding2017/basic/Queue.java | 24 ++ .../github/wdn/coding2017/basic/Stack.java | 27 ++ 8 files changed, 438 insertions(+) create mode 100644 group24/626451284/pom.xml create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java diff --git a/group24/626451284/pom.xml b/group24/626451284/pom.xml new file mode 100644 index 0000000000..534ff39232 --- /dev/null +++ b/group24/626451284/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.github.wdn + coding2017 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..3b3ea048f3 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/ArrayList.java @@ -0,0 +1,88 @@ +package com.github.wdn.coding2017.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + public ArrayList(){ + elementData = new Object[10]; + } + public ArrayList(int size){ + elementData = new Object[size]; + } + public void add(Object o){ + if(size>=elementData.length){ + elementData = Arrays.copyOf(elementData,elementData.length*2); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + Object[] newElementData; + if(size()+1>=elementData.length){ + newElementData=new Object[elementData.length*2]; + + }else{ + newElementData=new Object[elementData.length]; + } + for (int i = 0; i < elementData.length; i++) { + if(index==1){ + newElementData[i]=o; + }else if(i>index) { + newElementData[i]=elementData[i-1]; + }else{ + newElementData[i]=elementData[i]; + } + } + elementData = newElementData; + size++; + } + + public Object get(int index){ + if(index>=size){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index) { + if(index>=size){ + throw new IndexOutOfBoundsException(); + } + Object returnO = elementData[index]; + for (int i = index; i < size; i++) { + if(i==size-1){ + elementData[i]=null; + }else { + elementData[i] = elementData[i + 1]; + } + } + size--; + return returnO; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(2); + list.add("1"); + list.add("2"); + list.remove(1); + list.add("3"); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + int[] i = {}; + System.out.println(i[0]); + } + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b8b613d6f0 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 代码在公司电脑里。。。后续提交 + */ +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/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..044ddf0993 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Iterator.java @@ -0,0 +1,10 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 主要考虑遍历中安全删除元素的实现 + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..6040cb5a47 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/LinkedList.java @@ -0,0 +1,233 @@ +package com.github.wdn.coding2017.basic; + +/** + * TODO 只是简单实现 缺少对边界的处理 + * 参考JDK源码改为更优雅的实现 + */ + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size; + + public LinkedList(){ + this.head=null; + this.tail=null; + } + public void add(Object o){ + Node newNode = new Node(o); + if (head == null) { + head = newNode; + tail = newNode; + }else{ + tail.setNext(newNode); + newNode.setPre(tail); + tail = newNode; + } + size++; + } + public void add(int index , Object o){ + checkIndex(index); + Node indexNode = getNode(index); + Node newNode = new Node(o); + Node pre = indexNode.getPre(); + if(pre!=null){ + pre.setNext(newNode); + }else{ + head = newNode; + } + newNode.setPre(pre); + newNode.setNext(indexNode); + indexNode.setPre(newNode); + } + private void checkIndex(int index){ + if(index >= size || index <0){ + throw new IndexOutOfBoundsException(); + } + } + private Node getNode(int index){ + checkIndex(index); + // TODO这里可以优化,先判断index在前半部还是后半部分 然后确定从头部或者尾部查找 + Node result=null; + if(index==0){ + return head; + } + if(index==size-1){ + return tail; + } + for (int i = 0; i < index; i++) { + result = head.getNext(); + } + return result; + } + public Object get(int index){ + return getNode(index).getData(); + } + public Object remove(int index){ + checkIndex(index); + Node indexNode = getNode(index); + Node pre = indexNode.getPre(); + Node next = indexNode.getNext(); + if(pre!=null){ + pre.setNext(next); + }else{ + head = next; + } + if(next!=null){ + next.setPre(pre); + }else{ + tail = pre; + } + return indexNode.getData(); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newNode = new Node(o); + head.setPre(newNode); + newNode.setNext(head); + head = newNode; + } + public void addLast(Object o){ + Node newNode = new Node(o); + tail.setNext(newNode); + newNode.setPre(tail); + tail = newNode; + } + public Object removeFirst(){ + Node next = head.getNext(); + Node oldHead = head; + head = next; + head.setPre(null); + return oldHead; + } + public Object removeLast(){ + Node oldTail = tail; + Node pre = tail.getPre(); + tail = pre; + tail.setNext(null); + return oldTail; + } + public Iterator iterator(){ + + return null; + } + + /** + * JDK 中使用构造方法的方式设置next和pre减少不必要的getset方法更优雅 + */ + private static class Node{ + + Object data; + Node next; + Node pre; + public Node(){ + + } + public Node(Object data){ + this.data = data; + } + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public Node getPre() { + return pre; + } + + public void setPre(Node pre) { + this.pre = pre; + } + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java new file mode 100644 index 0000000000..d6c9e95cb0 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.wdn.coding2017.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java new file mode 100644 index 0000000000..59992f0cbd --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Queue.java @@ -0,0 +1,24 @@ +package com.github.wdn.coding2017.basic; + +/** + * 队列使用链表实现比较简单理论上是无界队列 + * 如果使用数组需要处理很多问题比如长度限制,元素的复制 + */ +public class Queue { + private LinkedList queue = new LinkedList(); + public void enQueue(Object o){ + queue.add(o); + } + + public Object deQueue(){ + return queue.remove(0); + } + + public boolean isEmpty(){ + return queue.size()==0; + } + + public int size(){ + return queue.size(); + } +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java new file mode 100644 index 0000000000..f0a0fe56e9 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/basic/Stack.java @@ -0,0 +1,27 @@ +package com.github.wdn.coding2017.basic; + +/** + * 使用list实现比较简单 + * 可以考虑使用其它结构 + */ +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(); + } +} From 09befe6d356c2f472893af51476d494c73381b94 Mon Sep 17 00:00:00 2001 From: wdn <626451284@163.com> Date: Wed, 15 Mar 2017 23:07:48 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=AE=8C=E6=88=90ArrayUtils=E5=8F=8A?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/626451284/pom.xml | 9 +- .../wdn/coding2017/array/ArrayUtil.java | 230 ++++++++++++++++++ .../wdn/coding2017/array/ArrayUtilTest.java | 55 +++++ 3 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtil.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtilTest.java diff --git a/group24/626451284/pom.xml b/group24/626451284/pom.xml index 534ff39232..6f5438ce3b 100644 --- a/group24/626451284/pom.xml +++ b/group24/626451284/pom.xml @@ -7,6 +7,11 @@ com.github.wdn coding2017 1.0-SNAPSHOT - - + + + junit + junit + 4.11 + + \ No newline at end of file diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtil.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..feac516229 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtil.java @@ -0,0 +1,230 @@ +package com.github.wdn.coding2017.array; + +import java.util.Arrays; + +public class ArrayUtil { + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int length = origin.length; + for (int i = 0; i < length/2; i++) { + origin[i]=origin[i]+origin[length-1-i]; + origin[length-1-i] = origin[i]-origin[length-1-i]; + origin[i] = origin[i]-origin[length-1-i]; + } + } + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] result = new int[oldArray.length]; + int resultSize=0; + for (int i = 0; i < oldArray.length; i++) { + int item = oldArray[i]; + if (item!=0){ + result[resultSize]=item; + resultSize++; + } + } + if(resultSize=array1.length && array2Index>=array2.length){ + break; + } + int array1Value = array1Index < array1.length ? array1[array1Index] : array2[array2Index]; + int array2Value = array2Index < array2.length ? array2[array2Index] : array1[array1Index]; + if (array1Value > array2Value) { + mergeArr[i] = array2Value; + array2Index++; + } else if (array1Value < array2Value) { + mergeArr[i] = array1Value; + array1Index++; + }else{ + mergeArr[i] = array1Value; + array1Index++; + array2Index++; + } + mergeArrIndex++; + } + return Arrays.copyOfRange(mergeArr,0,mergeArrIndex); + */ + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + if (size<0 || oldArray.length+size>Integer.MAX_VALUE){ + throw new IndexOutOfBoundsException(); + } + + // 方法一:使用jdk自带方法 + //return Arrays.copyOf(oldArray,oldArray.length+size); + + // 方法二:遍历 + int[] growArr = new int[oldArray.length+size]; + for (int i = 0; i < oldArray.length; i++) { + growArr[i] = oldArray[i]; + } + return growArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if(max<1 || max>Integer.MAX_VALUE){ + throw new IllegalArgumentException(); + } + int[] initArr = new int[]{1, 1}; + if(max<=2){ + return initArr; + } + int aIndex = 0; + int bIndex = 1; + int[] fibonacciArr = Arrays.copyOf(initArr,max); + int index = 2; + while(fibonacciArr[aIndex]+fibonacciArr[bIndex] Date: Fri, 24 Mar 2017 22:39:15 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=8F=90=E4=BA=A4arrayutil=E5=92=8C?= =?UTF-8?q?=E7=AE=80=E6=98=93struts=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/626451284/pom.xml | 5 + .../{ => coderising}/array/ArrayUtil.java | 2 +- .../{ => coderising}/array/ArrayUtilTest.java | 2 +- .../coderising/download/DownloadThread.java | 20 ++++ .../coderising/download/FileDownloader.java | 73 ++++++++++++++ .../download/FileDownloaderTest.java | 59 ++++++++++++ .../coderising/download/api/Connection.java | 23 +++++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 ++ .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 ++++++ .../download/impl/ConnectionManagerImpl.java | 15 +++ .../coderising/litestruts/LoginAction.java | 39 ++++++++ .../coderising/litestruts/Struts.java | 96 +++++++++++++++++++ .../coderising/litestruts/StrutsTest.java | 43 +++++++++ .../coderising/litestruts/View.java | 23 +++++ .../coderising/litestruts/struts.xml | 11 +++ 17 files changed, 456 insertions(+), 2 deletions(-) rename group24/626451284/src/main/java/com/github/wdn/coding2017/{ => coderising}/array/ArrayUtil.java (99%) rename group24/626451284/src/main/java/com/github/wdn/coding2017/{ => coderising}/array/ArrayUtilTest.java (97%) create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java create mode 100644 group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml diff --git a/group24/626451284/pom.xml b/group24/626451284/pom.xml index 6f5438ce3b..13a23ca6f4 100644 --- a/group24/626451284/pom.xml +++ b/group24/626451284/pom.xml @@ -13,5 +13,10 @@ junit 4.11 + + dom4j + dom4j + 1.6.1 + \ No newline at end of file diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtil.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java similarity index 99% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtil.java rename to group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java index feac516229..76a6cb326e 100644 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtil.java +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtil.java @@ -1,4 +1,4 @@ -package com.github.wdn.coding2017.array; +package com.github.wdn.coding2017.coderising.array; import java.util.Arrays; diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtilTest.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtilTest.java similarity index 97% rename from group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtilTest.java rename to group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtilTest.java index 8f3a8f56a4..b63b85a9a8 100644 --- a/group24/626451284/src/main/java/com/github/wdn/coding2017/array/ArrayUtilTest.java +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/array/ArrayUtilTest.java @@ -1,4 +1,4 @@ -package com.github.wdn.coding2017.array; +package com.github.wdn.coding2017.coderising.array; import org.junit.Assert; import org.junit.Test; diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..b342a51061 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.github.wdn.coding2017.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/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..619689a002 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/Struts.java @@ -0,0 +1,96 @@ +package com.github.wdn.coding2017.coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + + + +public class Struts { + public static void main(String[] args) { + Struts s = new Struts(); + Map> strutsMap = s.readStrutsXml(); + System.out.println(strutsMap); + } + public static View runAction(String actionName, Map parameters) { + Map> strutsMap = readStrutsXml(); + View view = new View(); + if(actionName.contains(actionName)){ + Map actionMap = strutsMap.get(actionName); + Map resultMap = (Map) actionMap.get("result"); + String actionClassName = actionMap.get("class").toString(); + System.out.println(actionClassName); + try { + Class c = Class.forName(actionClassName); + // 创建实例 + Object instance = c.newInstance(); + // 根据parameters调用setter方法 + for(Map.Entry entry:parameters.entrySet()){ + String key = entry.getKey(); + String value = entry.getValue(); + Method setter = c.getMethod("set"+key.substring(0, 1).toUpperCase() + key.substring(1),String.class); + if(setter!=null){ + setter.invoke(instance, value); + } + } + Method executeMethod = c.getMethod("execute"); + Object result = executeMethod.invoke(instance,null); + view.setJsp(resultMap.get(result)); + + Map paramters = new HashMap(); + Method[] methods = c.getMethods(); + for (Method m:methods) { + String methodName = m.getName(); + if(methodName.startsWith("get")){ + String key = methodName.replace("get","").toLowerCase(); + paramters.put(key,m.invoke(instance,null).toString()); + } + } + view.setParameters(paramters); + } catch (Exception e) { + e.printStackTrace(); + } + }else{ + try { + throw new ClassNotFoundException(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + return view; + } + public static Map> readStrutsXml(){ + SAXReader reader = new SAXReader(); + Map> strutsMap = new HashMap>(); + try { + String path = System.getProperty("user.dir"); + Document document = reader.read(new File(path+"\\src\\main\\java\\com\\github\\wdn\\coding2017\\coderising\\litestruts\\struts.xml")); + Element root = document.getRootElement(); + for (Iterator i = root.elementIterator(); i.hasNext(); ) { + Map actionMap = new HashMap(); + Element actionElement = (Element) i.next(); + if(actionElement.getName().equals("action")){ + actionMap.put("name",actionElement.attributeValue("name")); + actionMap.put("class",actionElement.attributeValue("class")); + } + Map resultMap = new HashMap(); + for (Iterator j=actionElement.elementIterator();j.hasNext();){ + Element resultElement = (Element)j.next(); + resultMap.put(resultElement.attributeValue("name"), resultElement.getText()); + actionMap.put("result", resultMap); + } + strutsMap.put(actionElement.attributeValue("name"),actionMap); + } + } catch (DocumentException e) { + e.printStackTrace(); + } + return strutsMap; + } +} diff --git a/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9fee464283 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.wdn.coding2017.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/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..31e1805dc7 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.wdn.coding2017.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/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..fb12bdc239 --- /dev/null +++ b/group24/626451284/src/main/java/com/github/wdn/coding2017/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + +