diff --git a/.gitignore b/.gitignore index dbf44ab662..d749cfbd62 100644 --- a/.gitignore +++ b/.gitignore @@ -1,26 +1,21 @@ -*.class -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear -target - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -#ide config -.metadata -.recommenders - - -#macOS -.DS_Store - -.idea/ -*.iml -rebel.* -.rebel.* - +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ +*.iml +rebel.* +.rebel.* + +target diff --git a/.project b/.project new file mode 100644 index 0000000000..6b4d50ed9a --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + coding2017 + + + + + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..4824b80263 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/README.md b/README.md new file mode 100644 index 0000000000..a7d23c04cf --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## 2017编程提高社群 + +2017编程提高社群代码仓库所在地 \ No newline at end of file diff --git a/group01/1298552064/src/week01/basic/Iterator.java b/group01/1298552064/src/week01/basic/Iterator.java new file mode 100644 index 0000000000..e209875b54 --- /dev/null +++ b/group01/1298552064/src/week01/basic/Iterator.java @@ -0,0 +1,8 @@ +package week01.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group01/1298552064/src/week01/basic/List.java b/group01/1298552064/src/week01/basic/List.java new file mode 100644 index 0000000000..608c1b532b --- /dev/null +++ b/group01/1298552064/src/week01/basic/List.java @@ -0,0 +1,13 @@ +package week01.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/group01/1298552064/src/week01/basic/MyArrayList.java b/group01/1298552064/src/week01/basic/MyArrayList.java new file mode 100644 index 0000000000..c4f6572f1c --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyArrayList.java @@ -0,0 +1,131 @@ +package week01.basic; + +import java.util.Arrays; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + ensureCapacity(size + 1); + + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkPositionIndex(index); + ensureCapacity(size + 1); + + if (index >= size) { + elementData[size++] = o; + } else { + System.arraycopy(elementData, index, elementData, index + 1, size + - index); + + elementData[index] = o; + + size++; + } + } + + public Object get(int index) { + checkElementIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkElementIndex(index); + Object removeElement = elementData[index]; + if (index == (size - 1)) { + elementData[index] = null; + size--; + } else { + System.arraycopy(elementData, index + 1, elementData, index, size + - index - 1); + elementData[size - 1] = null; + size--; + } + return removeElement; + } + + public int size() { + return size; + } + + /** + * 保证数组空间充足 + * + * @param minCapacity + */ + private void ensureCapacity(int minCapacity) { + int capacity = elementData.length; + if (minCapacity > capacity) { + capacity += capacity / 2; + grow(capacity); + } + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private void grow(int newCapacity) { + elementData = Arrays.copyOf(elementData, newCapacity); + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private MyArrayList list; + private int position = 0; + + private ArrayListIterator(MyArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size) { + return false; + } + return true; + } + + @Override + public Object next() { + return list.get(position++); + } + } + + @Override + public String toString() { + String elementStr = ""; + for (int i = 0; i < size; i++) { + elementStr += elementData[i] + ","; + } + return "MyArrayList: { size=" + size + ", elementData=" + "[" + + elementStr.substring(0, elementStr.length() - 1) + "]" + " }"; + } +} diff --git a/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java b/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java new file mode 100644 index 0000000000..30e6c810a5 --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyBinaryTreeNode.java @@ -0,0 +1,70 @@ +package week01.basic; + +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) { + if(this.getData() == null && this.getLeft() == null && this.getRight() == null){ + this.setData(o); + this.setLeft(null); + this.setRight(null); + return this; + } + + MyBinaryTreeNode node = new MyBinaryTreeNode(); + MyBinaryTreeNode currentNode = this; + while(true){ + if((Integer) o < (Integer) getData()){ + if(currentNode.getLeft() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + currentNode.setLeft(node); + return this; + }else{ + currentNode = currentNode.getLeft(); + } + + }else{ + if(currentNode.getRight() == null){ + node.setData(o); + node.setLeft(null); + node.setRight(null); + + currentNode.setRight(node); + return this; + }else{ + currentNode = currentNode.getRight(); + } + } + } + } +} diff --git a/group01/1298552064/src/week01/basic/MyLinkedList.java b/group01/1298552064/src/week01/basic/MyLinkedList.java new file mode 100644 index 0000000000..4894c5ff6c --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyLinkedList.java @@ -0,0 +1,215 @@ +package week01.basic; + +public class MyLinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + // 空链表 + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + Node p = head; + while (p.next != null) { + p = p.next; + } + + Node target = new Node(); + target.data = o; + target.next = null; + p.next = target; + } + size++; + } + + public void add(int index, Object o) { + // index 是否合法 + checkPositionIndex(index); + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + if (index == 0) { + addFirst(o); + } else if (index == size) { + addLast(o); + } else { + Node p = new Node(); + Node p1 = head; + for (int i = 0; i < index - 1; i++) { + p1 = p1.next; + } + p.data = o; + p.next = p1.next; + p1.next = p; + + size++; + } + } + } + + public Object get(int index) { + checkElementIndex(index); + Node p = head; + for (int i = 0; i < index; i++) { + p = p.next; + } + return p.data; + } + + public Object remove(int index) { + checkElementRemove(); + checkElementIndex(index); + + Object removeObject = null; + if (index == 0) { + removeObject = removeFirst(); + } else if (index == (size - 1)) { + removeObject = removeLast(); + } else { + Node p = head; + for (int i = 1; i < index; i++) { + p = p.next; + } + removeObject = p.next.data; + p.next = p.next.next; + size--; + } + return removeObject; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + } else { + Node p = new Node(); + p.data = o; + p.next = head; + head = p; + } + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + checkElementRemove(); + Object removeObject = head.data; + head = head.next; + size--; + return removeObject; + } + + public Object removeLast() { + checkElementRemove(); + + Object removeObject = null; + + if (size == 1) { + removeObject = head.data; + head = head.next; + } else { + Node p = head; + for (int i = 0; i < size; i++) { + if (p.next.next == null) { + removeObject = p.next; + p.next = null; + break; + } else { + p = p.next; + } + } + } + size--; + return removeObject; + } + + private boolean isEmpty() { + return size == 0; + } + + private void checkElementRemove() { + if (isEmpty()) { + throw new NullPointerException("The list is empty."); + } + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + + size); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + public Iterator iterator() { + return new MyLinkedListIterator(this); + } + + private class MyLinkedListIterator implements Iterator { + private MyLinkedList list = null; + private int position = 0; + + private MyLinkedListIterator(MyLinkedList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + if ((position + 1) > size()) { + return false; + } + return true; + } + + @Override + public Object next() { + return list.get(position++); + } + } + + private static class Node { + Object data; + Node next; + } + + @Override + public String toString() { + String elementStr = ""; + Node p = head; + while (p != null) { + elementStr += p.data + ","; + p = p.next; + } + + return "MyLinkedList: { size=" + size + ", elementData=" + "[" + + elementStr.substring(0, elementStr.length() - 1) + "]" + " }"; + } + +} diff --git a/group01/1298552064/src/week01/basic/MyQueue.java b/group01/1298552064/src/week01/basic/MyQueue.java new file mode 100644 index 0000000000..54008652ff --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyQueue.java @@ -0,0 +1,22 @@ +package week01.basic; + +public class MyQueue { + + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/1298552064/src/week01/basic/MyStack.java b/group01/1298552064/src/week01/basic/MyStack.java new file mode 100644 index 0000000000..aea4d94e24 --- /dev/null +++ b/group01/1298552064/src/week01/basic/MyStack.java @@ -0,0 +1,25 @@ +package week01.basic; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return elementData.remove(size() - 1); + } + + public Object peek() { + return elementData.get(size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/1298552064/src/week01/test/MyArrayListTest.java b/group01/1298552064/src/week01/test/MyArrayListTest.java new file mode 100644 index 0000000000..219035b46f --- /dev/null +++ b/group01/1298552064/src/week01/test/MyArrayListTest.java @@ -0,0 +1,53 @@ +package week01.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyArrayList; + + + +public class MyArrayListTest { + + private MyArrayList list = null; + + @Before + public void setUp() throws Exception { + list = new MyArrayList(); + + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void testAdd(){ + list.add(4, 10); + Assert.assertEquals("MyArrayList: { size=6, elementData=[1,2,3,4,10,5] }", list.toString()); + } + + @Test + public void testGet(){ + Assert.assertEquals((Object)new Integer(3), list.get(2)); + } + + @Test + public void testRemove(){ + list.remove(2); + Assert.assertEquals("MyArrayList: { size=4, elementData=[1,2,4,5] }", list.toString()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(5), list.size()); + } +} diff --git a/group01/1298552064/src/week01/test/MyLinkedListTest.java b/group01/1298552064/src/week01/test/MyLinkedListTest.java new file mode 100644 index 0000000000..b5d6c048d6 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyLinkedListTest.java @@ -0,0 +1,78 @@ +package week01.test; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyLinkedList; + +public class MyLinkedListTest { + + private MyLinkedList list = null; + + @Before + public void setUp() throws Exception { + list = new MyLinkedList(); + + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + + @Test + public void testAdd(){ + list.add(3,10); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[1,2,3,10,4,5] }",list.toString()); + } + + @Test + public void testAddFirst(){ + list.addFirst(100); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[100,1,2,3,4,5] }",list.toString()); + } + + @Test + public void testAddLast(){ + list.addLast(100); + Assert.assertEquals("MyLinkedList: { size=6, elementData=[1,2,3,4,5,100] }",list.toString()); + } + + @Test + public void testGet(){ + Assert.assertEquals((Object)new Integer(5), list.get(4)); + } + + @Test + public void testRemove(){ + list.remove(3); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[1,2,3,5] }",list.toString()); + } + + @Test + public void testRemoveFirst(){ + list.removeFirst(); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[2,3,4,5] }",list.toString()); + } + + @Test + public void testRemoveLast(){ + list.removeLast(); + Assert.assertEquals("MyLinkedList: { size=4, elementData=[1,2,3,4] }",list.toString()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(5), list.size()); + } + +} diff --git a/group01/1298552064/src/week01/test/MyQueueTest.java b/group01/1298552064/src/week01/test/MyQueueTest.java new file mode 100644 index 0000000000..f9b7cb63f2 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyQueueTest.java @@ -0,0 +1,48 @@ +package week01.test; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyQueue; + +public class MyQueueTest { + + private MyQueue queue = null; + + @Before + public void setUp() throws Exception { + queue = new MyQueue(); + + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void testEnQueue(){ + queue.enQueue(4); + Assert.assertEquals((Object)new Integer(4), queue.size()); + } + + @Test + public void testDeQueue(){ + Assert.assertEquals((Object) new Integer(1), queue.deQueue()); + } + + @Test + public void testIsEmpty(){ + Assert.assertFalse(queue.isEmpty()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object)new Integer(3), queue.size()); + } +} diff --git a/group01/1298552064/src/week01/test/MyStackTest.java b/group01/1298552064/src/week01/test/MyStackTest.java new file mode 100644 index 0000000000..4efbc2b204 --- /dev/null +++ b/group01/1298552064/src/week01/test/MyStackTest.java @@ -0,0 +1,53 @@ +package week01.test; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.basic.MyStack; + +public class MyStackTest { + private MyStack stack = null; + + @Before + public void setUp() throws Exception { + stack = new MyStack(); + + stack.push(1); + stack.push(2); + stack.push(3); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void tearPush() throws Exception { + stack.push(10); + Assert.assertEquals((Object) new Integer(4), stack.size()); + Assert.assertEquals((Object) new Integer(10), stack.peek()); + } + + @Test + public void testPop(){ + Assert.assertEquals((Object) new Integer(3), stack.pop()); + Assert.assertEquals((Object) new Integer(2), stack.size()); + } + + @Test + public void testPeek(){ + Assert.assertEquals((Object) new Integer(3), stack.peek()); + } + + @Test + public void testIsEmpty(){ + Assert.assertFalse(stack.isEmpty()); + } + + @Test + public void testSize(){ + Assert.assertEquals((Object) new Integer(3), stack.size()); + } +} diff --git a/group01/1328404806/RemoteSystemsTempFiles/.project b/group01/1328404806/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..7675629320 --- /dev/null +++ b/group01/1328404806/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group01/1328404806/dataStructure/.classpath b/group01/1328404806/dataStructure/.classpath new file mode 100644 index 0000000000..7faf63dc05 --- /dev/null +++ b/group01/1328404806/dataStructure/.classpath @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group17/82427129/JavaUtil/.gitignore b/group01/1328404806/dataStructure/.gitignore similarity index 90% rename from group17/82427129/JavaUtil/.gitignore rename to group01/1328404806/dataStructure/.gitignore index 24d64373c4..b83d22266a 100644 --- a/group17/82427129/JavaUtil/.gitignore +++ b/group01/1328404806/dataStructure/.gitignore @@ -1 +1 @@ -/target/ +/target/ diff --git a/group01/1328404806/dataStructure/.project b/group01/1328404806/dataStructure/.project new file mode 100644 index 0000000000..86bf42de91 --- /dev/null +++ b/group01/1328404806/dataStructure/.project @@ -0,0 +1,23 @@ + + + dataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/group01/1328404806/dataStructure/.settings/org.eclipse.core.resources.prefs b/group01/1328404806/dataStructure/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..4c28b1a898 --- /dev/null +++ b/group01/1328404806/dataStructure/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/group01/1328404806/dataStructure/.settings/org.eclipse.jdt.core.prefs b/group01/1328404806/dataStructure/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..8626026241 --- /dev/null +++ b/group01/1328404806/dataStructure/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/group01/1328404806/dataStructure/.settings/org.eclipse.m2e.core.prefs b/group01/1328404806/dataStructure/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..14b697b7bb --- /dev/null +++ b/group01/1328404806/dataStructure/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group01/1328404806/dataStructure/pom.xml b/group01/1328404806/dataStructure/pom.xml new file mode 100644 index 0000000000..0b5e3a1ca3 --- /dev/null +++ b/group01/1328404806/dataStructure/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + increaseLearning + dataStructure + 0.0.1-SNAPSHOT + jar + + dataStructure + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + diff --git a/group01/1328404806/dataStructure/src/main/java/ListService/KILinkedList.java b/group01/1328404806/dataStructure/src/main/java/ListService/KILinkedList.java new file mode 100644 index 0000000000..8f0307f340 --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/ListService/KILinkedList.java @@ -0,0 +1,10 @@ +package ListService; + +//集合接口 +public interface KILinkedList { + + public void add(T t, int pos); + + public T remove(int pos); + +} diff --git a/group01/1328404806/dataStructure/src/main/java/ListService/KIList.java b/group01/1328404806/dataStructure/src/main/java/ListService/KIList.java new file mode 100644 index 0000000000..b0851d30b0 --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/ListService/KIList.java @@ -0,0 +1,28 @@ +package ListService; + +//集合接口 +public interface KIList { + + public void add(T item); + + public void add(int index, T item); + + public void set(int index, T item); + + public void remove(int index); + + public void remove(T item); + + public void clear(); + + public boolean contains(T item); + + public boolean isEmpty(); + + public T get(int index); + + public int indexOf(T item); + + public int size(); + +} diff --git a/group01/1328404806/dataStructure/src/main/java/ListService/KIQueueList.java b/group01/1328404806/dataStructure/src/main/java/ListService/KIQueueList.java new file mode 100644 index 0000000000..b02c0e86a5 --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/ListService/KIQueueList.java @@ -0,0 +1,11 @@ +package ListService; + +//集合接口 +public interface KIQueueList { + public T add(T ele); + + public T remove(); + + public Object[] getData(); + +} diff --git a/group01/1328404806/dataStructure/src/main/java/ListService/KIStackList.java b/group01/1328404806/dataStructure/src/main/java/ListService/KIStackList.java new file mode 100644 index 0000000000..8a1f031976 --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/ListService/KIStackList.java @@ -0,0 +1,9 @@ +package ListService; + +//集合接口 +public interface KIStackList { + public void push(T ele); + + public void pop(); + +} diff --git a/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KArrayList.java b/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KArrayList.java new file mode 100644 index 0000000000..d85a8957cf --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KArrayList.java @@ -0,0 +1,192 @@ +package ListServiceImpl; + +import ListService.KIList; + +public class KArrayList implements KIList { + + /** 初始化的容量的大小 */ + private final static int INIT_CAPACITY = 12; + private Object[] mList = null; + + /** 当前的容量 */ + private int mCurrentCapacity = 0; + /** 容器中元素的个数 */ + private int mSize = 0; + + public KArrayList() { + mList = new Object[INIT_CAPACITY]; + mCurrentCapacity = INIT_CAPACITY; + } + + /** + * 插入一个元素到链表尾部 + * + * @param item + */ + public void add(T item) { + if (mSize == mCurrentCapacity) { + expansion(); + } + mList[mSize] = item; + mSize++; + + } + + /** + * 插入一个元素到指定位置,从插入位置及其后面的元素往后移动一个位置 + * + * @param index + * 要插入的位置 + * @param item + */ + public void add(int index, T item) { + if (index < 0 || index >= mSize) { // 不允许index小于0,或者index >= 数组当前大小 + throw new IndexOutOfBoundsException(); + } + if (mSize == mCurrentCapacity) { + expansion(); + } + Object[] newList = new Object[mCurrentCapacity]; + System.arraycopy(mList, 0, newList, 0, index); + System.arraycopy(mList, index, newList, index + 1, mSize - index); + newList[index] = item; + mList = newList; + mSize++; + + } + + /** + * 更新指定位置的元素 + * + * @param index + * @param item + */ + public void set(int index, T item) { + if (index < 0 || index >= mSize) { + throw new IndexOutOfBoundsException(); + } + mList[index] = item; + + } + + /** + * 移除指定位置的元素,后面的元素向前移动一位 + * + * @param index + */ + public void remove(int index) { + if (index < 0 || index >= mSize) { + throw new IndexOutOfBoundsException(); + } + Object[] newList = new Object[mCurrentCapacity]; + System.arraycopy(mList, 0, newList, 0, index); + System.arraycopy(mList, index + 1, newList, index, mSize - index); + mList = newList; + mSize--; + + } + + /** + * 移除链表中特定的元素。(如果item在链表中有多个,只移除第一个) + * + * @param item + */ + public void remove(T item) { + for (int i = 0; i < mSize; i++) { + if (mList[i].equals(item)) { + remove(i); + break; + } + } + + } + + /** + * 将链表清空,capacity不变 + */ + public void clear() { + mList = new Object[mCurrentCapacity]; + mSize = 0; + + } + + /** + * 判断是否包含某个元素 + * + * @param item + * @return true表示有这个元素,false表示没有这个元素 + */ + public boolean contains(T item) { + for (int i = 0; i < mSize; i++) { + if (mList[i].equals(item)) { + return true; + } + } + return false; + } + + /** + * 判断链表是否为空 + * + * @return boolean + */ + public boolean isEmpty() { + return (mSize == 0) ? true : false; + } + + /** + * 获取指定位置的元素 + * + * @param index + * @return + */ + @SuppressWarnings("unchecked") + public T get(int index) { + if (index < 0 || index >= mSize) { + throw new IndexOutOfBoundsException(); + } + return (T) mList[index]; + } + + /** + * 获取特定元素所在的位置。 如果该链表中存在多个相同的元素,只返回第一个的位置,如果找不到,则返回-1。 + * + * @param item + * @return int 如果没找到,返回-1 + */ + public int indexOf(T item) { + for (int i = 0; i < mSize; i++) { + if (mList[i].equals(item)) { + return i; + } + } + return -1; + } + + /** + * 获取当前链表的长度 + * + * @return int + */ + public int size() { + return mSize; + } + + /** + * 扩容,当 mSize == mCurrentCapacity 时调用 + */ + private void expansion() { + Object[] oldList = mList; + Object[] newList = new Object[getNewCapacity()]; + System.arraycopy(oldList, 0, newList, 0, oldList.length); + mList = newList; + } + + /** + * 获取新的容量大小 当满的时候每次增加当前容量的50% + */ + private int getNewCapacity() { + return mCurrentCapacity = mCurrentCapacity + (mCurrentCapacity >> 1); + } + +} diff --git a/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KLinkedList.java b/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KLinkedList.java new file mode 100644 index 0000000000..6afb12befc --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KLinkedList.java @@ -0,0 +1,201 @@ +package ListServiceImpl; + +import java.util.Iterator; + +import ListService.KILinkedList; + +public class KLinkedList implements Iterable, KILinkedList { + + // 记录链表的长度 + private int mSize = 0; + // private int mActionCount = 0; + // 开始和结束节点 + private Node mBeginNode, mLastNode; + + public KLinkedList() { + // TODO Auto-generated constructor stub + init(); + } + + /** + * 初始化一个只有开始节点和结束节点的空链表 + */ + private void init() { + // 将首位节点链接起来 + mBeginNode = new Node(null, null, null); + mLastNode = new Node(null, mBeginNode, null); + mBeginNode.nextNode = mLastNode; + mSize = 0; + // mActionCount++; + } + + public int size() { + return mSize; + } + + public boolean isEmpty() { + return mSize == 0 ? true : false; + } + + /** + * 在链表的pos位置之前放置t_node这个节点 + * + * @param t_node + * 需要放置的节点 + * @param pos + * 放置节点在pos之前 + */ + private void add(Node newNode, int pos) { + // 抛出不合法的位置 + if (pos < 0 || pos > mSize) { + throw new IndexOutOfBoundsException(); + } + + // 链接新节点 + newNode.nextNode = getNode(pos); + getNode(pos - 1).nextNode = newNode; + getNode(pos).preNode = newNode; + // mActionCount++; + mSize++; + + } + + /** + * t 供外部调用,直接在链表的末尾添加,即在mLastNode节点之前 + * + * @param + */ + public void add(T t) { + add(new Node(t, null, null), mSize); + } + + /** + * 往链表pos位置之前添加数据t + * + * @param t + * 添加的数据 + * @param pos + * 添加在pos位置之前 + */ + public void add(T t, int pos) { + add(new Node(t, null, null), pos); + } + + /** + * + * @param pos + * 链表中的某个位置 + * @return 翻去pos位置的节点 (此处的pos的范围是[-1,mSize],此方法是私有方法,外部访问不了,只共此类中呢个访问) + */ + private Node getNode(int pos) { + Node node; + int currentPos; + if (pos == -1) { + // -1的位置是开始节点 + return mBeginNode; + } else if (pos == mSize) { + // mSize的位置是结束的节点 + return mLastNode; + } + // 因为这是双向节点,所以判断一下能提高搜索效率 + if (pos < mSize / 2) { + currentPos = 0; + node = mBeginNode.nextNode; + while (currentPos < pos) { + node = node.nextNode; + currentPos++; + } + } else { + node = mLastNode.preNode; + currentPos = mSize - 1; + while (currentPos > pos) { + node = node.preNode; + currentPos--; + } + } + return node; + } + + public T get(int pos) { + return getNode(pos).t; + } + + public void set(T t, int pos) { + if (pos < 0 || pos >= mSize) { + throw new IndexOutOfBoundsException(); + } + getNode(pos).t = t; + } + + /** + * 删除特定位置的节点 + * + * @param t_node + * 需要删除节点的位置 + * @return + */ + private T remove(Node t_node) { + + t_node.preNode.nextNode = t_node.nextNode; + t_node.nextNode.preNode = t_node.preNode; + // 最好在此处给其设置为空,不要其链接到其他节点,因为已经被销毁,不再持有其他的节点的引用 + t_node.nextNode = null; + t_node.preNode = null; + mSize--; + // mActionCount++; + return t_node.t; + } + + public T remove(int pos) { + if (pos < 0 || pos >= mSize) { + throw new IndexOutOfBoundsException(); + } + Node tempNode = getNode(pos); + remove(tempNode); + return tempNode.t; + } + + public Iterator iterator() { + + return new MyLinkedListIterator(); + } + + private class MyLinkedListIterator implements Iterator { + + private int currentPos = 0; + + public boolean hasNext() { + // TODO Auto-generated method stub + if (currentPos < mSize) { + return true; + } + return false; + } + + public T next() { + // TODO Auto-generated method stub + return (T) getNode(currentPos++).t; + } + + public void remove() { + // TODO Auto-generated method stub + KLinkedList.this.remove(getNode(--currentPos)); + ; + } + + } + + // 静态内部类,定义的节点,双向链表,需要一个指向前面一项的引用域和一个指向后面一项的引用域,方便查找 + private static class Node { + public T t; + public Node preNode; + public Node nextNode; + + public Node(T t, Node preNode, Node nextNode) { + this.preNode = preNode; + this.nextNode = nextNode; + this.t = t; + } + } + +} diff --git a/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KQueueList.java b/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KQueueList.java new file mode 100644 index 0000000000..7ea8643c43 --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KQueueList.java @@ -0,0 +1,97 @@ +package ListServiceImpl; + +import java.util.Arrays; +import java.util.Collection; + +import ListService.KIQueueList; + +public class KQueueList implements KIQueueList{ + + /** 初始容量 */ + public static final int DEFAULT_SIZE = 10; + /** 容量不足时翻倍数 */ + public static final float DEFAULT_INCREMENT = 1.5f; + /** 数据 */ + private Object[] elementData; + /** 元素个数 */ + private int elementCount; + /** 数组的头部,即 下次删除数据的 index */ + private int head; + /** 数组的尾部,即 下次插入数据的 index */ + private int tail; + + public KQueueList() { + this(DEFAULT_SIZE); + } + + public KQueueList(int size) { + this.elementData = new Object[size]; + this.elementCount = 0; + this.head = 0; + this.tail = 0; + } + + public KQueueList(Object[] data) { + this.elementData = data; + this.elementCount = data.length; + this.head = 0; + this.tail = 0; + } + + public KQueueList(Collection c) { + this(c.toArray()); + } + + /** + * 添加数据 到尾部 + * + * @param ele + * @return + */ + public T add(T ele) { + if (tail >= elementData.length) { + adjustData(); + } + elementData[tail] = ele; + elementCount++; + tail++; + return ele; + }; + + /** + * 删除数据 从头部 + * + * @return + */ + @SuppressWarnings("unchecked") + public T remove() { + T e = (T) elementData[head]; + elementData[head] = null; + elementCount--; + head++; + return e; + }; + + /** + * 获得当前的数据 + * + * @return + */ + public Object[] getData() { + return Arrays.copyOfRange(this.elementData, this.head, this.tail); + } + + public void adjustData() { + if (tail >= elementData.length) { // tail 处空间不足时调用 + // head 的空位去掉 + int newSize = (elementData.length == elementCount) ? (int) Math.ceil(elementCount * DEFAULT_INCREMENT) + : elementData.length; + elementData = Arrays.copyOfRange(elementData, head, elementData.length); + // 调整空间 + elementData = Arrays.copyOf(elementData, newSize); + tail = elementCount; + head = 0; + } + } + +} diff --git a/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KStackList.java b/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KStackList.java new file mode 100644 index 0000000000..e2b9ee1186 --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/ListServiceImpl/KStackList.java @@ -0,0 +1,48 @@ +package ListServiceImpl; + +import java.util.Arrays; + +import ListService.KIStackList; + +public class KStackList implements KIStackList{ + Object[] data; + private int capacity; + private int size; + + public KStackList() + { + capacity=16; + size=0; + data=new Object[capacity]; + } + + public void ensureCapacity() { + capacity = capacity * 2; + data = Arrays.copyOf(data, capacity); + } + + //压入栈底 + public void push(T ele) { + if (size < capacity) { + data[size++] = ele; + } else { + ensureCapacity(); + data[size++] = ele; + } + } + + //弹出 + public void pop() { + if (size > 0) { + System.out.println(data[size - 1]); + data[--size] = null; + } else { + System.out.println("Empty stack!"); + } + } + + boolean isEmpty() { + return size == 0; + } + +} diff --git a/group01/1328404806/dataStructure/src/main/java/increaseLearning/dataStructure/App.java b/group01/1328404806/dataStructure/src/main/java/increaseLearning/dataStructure/App.java new file mode 100644 index 0000000000..018ba70bdb --- /dev/null +++ b/group01/1328404806/dataStructure/src/main/java/increaseLearning/dataStructure/App.java @@ -0,0 +1,13 @@ +package increaseLearning.dataStructure; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/AppTest.java b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/AppTest.java new file mode 100644 index 0000000000..4fffcb78ed --- /dev/null +++ b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/AppTest.java @@ -0,0 +1,38 @@ +package increaseLearning.dataStructure; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/arrayListTest.java b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/arrayListTest.java new file mode 100644 index 0000000000..3815775989 --- /dev/null +++ b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/arrayListTest.java @@ -0,0 +1,36 @@ +package increaseLearning.dataStructure; + + +import org.junit.Test; + +import ListServiceImpl.KArrayList; +import junit.framework.TestCase; + +public class arrayListTest extends TestCase { + @Test + public void testArrayList() { + + KArrayList arr = new KArrayList(); + + for (int i = 1; i <= 50; i++) { + arr.add(i); + } + + arr.add(10, 99); + arr.add(0, 99); + + System.out.println(arr.get(51)); + System.out.println(arr.get(11)); + + // arr.clear(); + + // System.out.println(arr.contains(99)); + // System.out.println(arr.indexOf(59)); + + arr.remove(11); + + arr = null; + + } + +} diff --git a/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/linkedListTest.java b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/linkedListTest.java new file mode 100644 index 0000000000..6b42a261fa --- /dev/null +++ b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/linkedListTest.java @@ -0,0 +1,21 @@ +package increaseLearning.dataStructure; + +import org.junit.Test; + +import ListServiceImpl.KLinkedList; +import junit.framework.TestCase; + +public class linkedListTest extends TestCase{ + @Test + public void testLinkedList(){ + KLinkedList linkList=new KLinkedList(); + + + linkList.add(3, 0); + + System.out.println(linkList.get(0)); +// System.out.println("成功!!!"); + + } + +} diff --git a/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/queueListTest.java b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/queueListTest.java new file mode 100644 index 0000000000..af4f952f6c --- /dev/null +++ b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/queueListTest.java @@ -0,0 +1,42 @@ +package increaseLearning.dataStructure; + +import org.junit.Assert; +import org.junit.Test; + +import ListServiceImpl.KQueueList; +import junit.framework.TestCase; + +public class queueListTest extends TestCase { + @Test + public void testQueueList() { + KQueueList queueOne = new KQueueList(); + // 第1次 加入个数 + int addCountOne = 30; + // 第1次 删除个数 + int removeCountOne = 20; + // 第2次 加入个数 + int addCountTwo = 10; + + for (int i = 0; i < addCountOne; i++) { + queueOne.add(i); + } + Object[] data = queueOne.getData(); + for (int i = 0; i < data.length; i++) { + Assert.assertTrue((Integer) data[i] == i); + } + + for (int i = 0; i < removeCountOne; i++) { + Assert.assertTrue(queueOne.remove() == i); + } + + for (int i = 0; i < addCountTwo; i++) { + queueOne.add(i * 10); + } + Object[] data2 = queueOne.getData(); + int baseCount = addCountOne - removeCountOne; + for (int i = 0; i < addCountTwo; i++) { + Assert.assertTrue((Integer) data2[baseCount + i] == i * 10); + } + } + +} diff --git a/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/stackListTest.java b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/stackListTest.java new file mode 100644 index 0000000000..614f18cb8c --- /dev/null +++ b/group01/1328404806/dataStructure/src/test/java/increaseLearning/dataStructure/stackListTest.java @@ -0,0 +1,26 @@ +package increaseLearning.dataStructure; + +import org.junit.Test; + +import ListServiceImpl.KStackList; +import junit.framework.TestCase; + +public class stackListTest extends TestCase{ + @Test + public void testStackList(){ + KStackList fcStack=new KStackList(); + for(int i=0;i<10;i++) + { + fcStack.push(i); + System.out.println(fcStack); + } + + for(int i=0;i<10;i++) + { + fcStack.pop(); + System.out.println(fcStack); + } + fcStack.pop(); + } + +} diff --git a/group17/102228177/.classpath b/group01/1664823950/.classpath similarity index 100% rename from group17/102228177/.classpath rename to group01/1664823950/.classpath diff --git a/group18/935542673/Coding/.gitignore b/group01/1664823950/.gitignore similarity index 100% rename from group18/935542673/Coding/.gitignore rename to group01/1664823950/.gitignore diff --git a/group01/1664823950/.project b/group01/1664823950/.project new file mode 100644 index 0000000000..6cca5cf64b --- /dev/null +++ b/group01/1664823950/.project @@ -0,0 +1,21 @@ + + +<<<<<<< HEAD:group01/1664823950/.project + 1664823950 +======= + 1264835468 +>>>>>>> master:group17/1264835468/.project + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/1664823950/src/com/coding/basic/ArrayList.java b/group01/1664823950/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..87928e7483 --- /dev/null +++ b/group01/1664823950/src/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +public class ArrayList implements List +{ + + private int size = 0; + + + private Object[] elementData = new Object[100]; + + public void add(Object o) + { + elementData[size] = o; + size ++; + } + + public void add(int index, Object o) + { + if(index > size || index < 0) + { + return; + } + else + { + for (int i = size-1; i > index; i--) + { + elementData[i+1] = elementData[size]; + } + elementData[index] = o; + size++; + } + + } + + public Object get(int index) + { + if(index < size || index >= 0) + { + return elementData[index]; + } + return null; + } + + public Object remove(int index) + { + Object removedObj; + if(index >= size || index < 0) + { + removedObj = null; + } + else + { + removedObj = elementData[index]; + for (int j = index; j < elementData.length; j++) + { + elementData[j] = elementData[j+1]; + } + size--; + } + return removedObj; + } + + public int size() + { + return size; + } + + public Iterator iterator() + { + return null; + } + +} diff --git a/group01/1664823950/src/com/coding/basic/BinaryTreeNode.java b/group01/1664823950/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..24710872cb --- /dev/null +++ b/group01/1664823950/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,45 @@ +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/group01/1664823950/src/com/coding/basic/Iterator.java b/group01/1664823950/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e5ccd24b42 --- /dev/null +++ b/group01/1664823950/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/group01/1664823950/src/com/coding/basic/LinkedList.java b/group01/1664823950/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..2a217b2df7 --- /dev/null +++ b/group01/1664823950/src/com/coding/basic/LinkedList.java @@ -0,0 +1,108 @@ +package com.coding.basic; + +public class LinkedList implements List +{ + + private Node head; + private Node tail; + + public void add(Object o) + { + Node n = new Node(o); + tail.next = n; + tail = n; + } + + public void add(int index , Object o) + { + Node n = new Node(o); + getNode(index-1).next = n; + n.next = getNode(index); + } + + private Node getNode(int index) + { + Node n = head; + int counter = 0; + while (counter + + + + + + diff --git a/group01/1814014897/zhouhui/.gitignore b/group01/1814014897/zhouhui/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/1814014897/zhouhui/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/1814014897/zhouhui/.project b/group01/1814014897/zhouhui/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group01/1814014897/zhouhui/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/1814014897/zhouhui/.settings/org.eclipse.jdt.core.prefs b/group01/1814014897/zhouhui/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group01/1814014897/zhouhui/.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/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java new file mode 100644 index 0000000000..23ed3f6bc2 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/ArrayList.java @@ -0,0 +1,75 @@ +package week01.BasicDataStructure; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + ensureCapacity(size + 1); //size increase,in order to have enough capacity. + elementData[size++] = o; //similar to: elementData[size]=o; size++; + } + + private void ensureCapacity(int minCapacity){ + if(minCapacity > elementData.length){ + grow(minCapacity); + } + } + + private void grow(int minCapacity){ + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + ( oldCapacity >> 1 ); + if(newCapacity < minCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + + } + + public void add(int index, Object o){ + if(index < 0 || index > size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + ensureCapacity(size+1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + return elementData[index]; + } + + public Object remove(int index){ + if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + Object data_index = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[size - 1] = null; + size--; + return data_index; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int pos = 0; + + public boolean hasNext() { + return pos < size; + } + + public Object next() { + return elementData[pos++]; + } + } + +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java new file mode 100644 index 0000000000..a4fb2cf8b9 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/BinaryTreeNode.java @@ -0,0 +1,56 @@ +package week01.BasicDataStructure; + +public class BinaryTreeNode{ + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data){ + this.data = data; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if((Integer)o < (Integer)this.data) + { + if(this.left == null){ + BinaryTreeNode node = new BinaryTreeNode(o); + this.setLeft(node); + return node; + }else{ + return this.left.insert(o); + } + }else{ + if(this.right == null){ + BinaryTreeNode node = new BinaryTreeNode(o); + this.setRight(node); + return node; + }else{ + return this.right.insert(o); + } + } + } + } + + diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java new file mode 100644 index 0000000000..0ad3fff8f3 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Iterator.java @@ -0,0 +1,7 @@ +package week01.BasicDataStructure; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java new file mode 100644 index 0000000000..35b1158cd1 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/LinkedList.java @@ -0,0 +1,113 @@ +package week01.BasicDataStructure; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if(head == null){ + head = new Node(o); + }else{ + Node pos = head; + while(pos.next != null){ + pos = pos.next; + } + pos.next = new Node(o); + } + size++; + } + + public void add(int index , Object o){ + if(index < 0 || index >size ) throw new IndexOutOfBoundsException("Index:"+index+",Size"+size); + if(index == 0) { + Node node = new Node(o); + node.next = head; + head = node; + } + else{ + Node pos = head; + for(int i = 0;i < index-1;i++){ + pos = pos.next; + } + Node node = new Node(o); + node.next = pos.next; + pos.next = node; + } + size++; + } + + public Object get(int index){ + if(index < 0 || index >=size ) throw new IndexOutOfBoundsException("Index:"+index+",Size"+size); + Node pos = head; + for(int i = 0;i < index;i++){ + pos = pos.next; + } + return pos.data; + } + + public Object remove(int index){ + if(index < 0 || index >=size ) throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size); + Node element = head; + if(index == 0){ + head = head.next; + }else{ + Node pos = head; + for(int i = 0;i < index - 1;i++){ + pos = pos.next; + } + element = pos.next; + pos.next = pos.next.next; + } + size--; + return element.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + 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 LinkedListIterator(); + } + + class LinkedListIterator implements Iterator{ + + private Node node = head; + private int pos = 0; + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public Object next() { + pos++; + if(pos != 1){ + node = node.next; + } + return node.data; + } + } + + private static class Node{ + Object data; + Node next; + public Node(Object data){ + this.data = data; + next = null; + } + } +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java new file mode 100644 index 0000000000..7806b75ed3 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/List.java @@ -0,0 +1,9 @@ +package week01.BasicDataStructure; + +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/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java new file mode 100644 index 0000000000..e0ab6bbb9c --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Queue.java @@ -0,0 +1,25 @@ +package week01.BasicDataStructure; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + private int size = 0; + + public void enQueue(Object o){ + linkedList.add(o); + size++; + } + + public Object deQueue(){ + size--; + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return size; + } +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java new file mode 100644 index 0000000000..53f99b37c7 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructure/Stack.java @@ -0,0 +1,25 @@ +package week01.BasicDataStructure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + return elementData.remove(--size); + } + + public Object peek(){ + return elementData.get(size - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java new file mode 100644 index 0000000000..5d5f07d815 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/AllTest.java @@ -0,0 +1,18 @@ +package week01.BasicDataStructureTest; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ + ArrayListTest.class, + BinaryTreeNodeTest.class, + LinkedListTest.class, + QueueTest.class, + StackTest.class +}) + +public class AllTest { + +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java new file mode 100644 index 0000000000..c5513acfda --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/ArrayListTest.java @@ -0,0 +1,72 @@ +package week01.BasicDataStructureTest; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.ArrayList; +import week01.BasicDataStructure.Iterator; + +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void setUp() throws Exception { + for(int i = 0;i < 100 ; i++){ + arrayList.add(i); + } + } + + @Test + public void testAddObject() { + for(int i = 0;i < 100;i++){ + Assert.assertEquals(arrayList.get(i), i); + } + } + + @Test + public void testAddIntObject() { + arrayList.add(0,10); + arrayList.add(22, 44); + arrayList.add(40, 5); + arrayList.add(100,88); + Assert.assertEquals(arrayList.get(0), 10); + Assert.assertEquals(arrayList.get(22),44); + Assert.assertEquals(arrayList.get(40), 5); + Assert.assertEquals(arrayList.get(100), 88); + } + + @Test + public void testGet() { + Assert.assertEquals(arrayList.get(0), 0); + Assert.assertEquals(arrayList.get(33), 33); + Assert.assertEquals(arrayList.get(77), 77); + Assert.assertEquals(arrayList.get(99), 99); + } + + @Test + public void testRemove() { + Assert.assertEquals(arrayList.remove(0), 0); + Assert.assertEquals(arrayList.remove(0), 1); + Assert.assertEquals(arrayList.remove(97), 99); + Assert.assertEquals(arrayList.size(), 97); + } + + @Test + public void testSize() { + Assert.assertEquals(arrayList.size(), 100); + arrayList.add(5,5); + Assert.assertEquals(arrayList.size(),101); + arrayList.remove(5); + Assert.assertEquals(arrayList.size(), 100); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + for(int i=0;iterator.hasNext();i++){ + Assert.assertEquals(iterator.next(),i); + } + } +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..724e6c0e03 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/BinaryTreeNodeTest.java @@ -0,0 +1,80 @@ +package week01.BasicDataStructureTest; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.BinaryTreeNode; + + +public class BinaryTreeNodeTest { + + private BinaryTreeNode root = new BinaryTreeNode(5); + + @Before + public void setUp() throws Exception { + root.insert(2); + root.insert(7); + root.insert(1); + root.insert(6); + } + + @Test + public void testGetData() { + Assert.assertEquals(root.getData(), 5); + Assert.assertEquals(root.getLeft().getData(), 2); + Assert.assertEquals(root.getRight().getData(), 7); + Assert.assertEquals(root.getLeft().getLeft().getData(), 1); + Assert.assertEquals(root.getRight().getLeft().getData(), 6); + } + + @Test + public void testSetData() { + root.setData(8); + Assert.assertEquals(root.getData(),8); + root.getLeft().setData(88); + Assert.assertEquals(root.getLeft().getData(),88); + root.getRight().setData(888); + Assert.assertEquals(root.getRight().getData(),888); + } + + @Test + public void testGetLeft() { + BinaryTreeNode node_left = root.getLeft(); + Assert.assertEquals(node_left.getData(), 2); + BinaryTreeNode node_left_left = root.getLeft().getLeft(); + Assert.assertEquals(node_left_left.getData(), 1); + } + + @Test + public void testSetLeft() { + BinaryTreeNode node = new BinaryTreeNode(100); + root.setLeft(node); + Assert.assertEquals(root.getLeft().getData(), 100); + } + + @Test + public void testGetRight() { + BinaryTreeNode node_right = root.getRight(); + Assert.assertEquals(node_right.getData(), 7); + root.insert(8); + BinaryTreeNode node_right_right = root.getRight().getRight(); + Assert.assertEquals(node_right_right.getData(), 8); + } + + @Test + public void testSetRight() { + BinaryTreeNode node = new BinaryTreeNode(100); + root.setRight(node); + Assert.assertEquals(root.getRight().getData(), 100); + } + + @Test + public void testInsert() { + root.insert(4); + root.insert(8); + Assert.assertEquals(root.getLeft().getRight().getData(), 4); + Assert.assertEquals(root.getRight().getRight().getData(), 8); + } + +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java new file mode 100644 index 0000000000..2fb20d12f4 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/LinkedListTest.java @@ -0,0 +1,106 @@ +package week01.BasicDataStructureTest; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.Iterator; +import week01.BasicDataStructure.LinkedList; + +public class LinkedListTest { + + private LinkedList linkedList = new LinkedList(); + + @Before + public void setUp() throws Exception { + for(int i=0;i<100;i++){ + linkedList.add(i); + } + } + + @Test + public void testAddObject() { + for(int i=0;i<200;i++){ + linkedList.add(i); + } + for(int i=0;i<100;i++){ + Assert.assertEquals(linkedList.get(i), i); + } + for(int i=100;i<300;i++){ + Assert.assertEquals(linkedList.get(i), i-100); + } + } + + @Test + public void testAddIntObject() { + linkedList.add(0, 10); + Assert.assertEquals(linkedList.get(0), 10); + linkedList.add(5,60); + Assert.assertEquals(linkedList.get(5), 60); + Assert.assertEquals(linkedList.get(101), 99); + } + + @Test + public void testGet() { + for(int i =0;i<100;i++){ + Assert.assertEquals(linkedList.get(i), i); + } + } + + @Test + public void testRemove() { + Assert.assertEquals(linkedList.remove(0), 0); + Assert.assertEquals(linkedList.remove(0), 1); + Assert.assertEquals(linkedList.size(), 98); + linkedList.remove(97); + Assert.assertEquals(linkedList.get(96), 98); + } + + @Test + public void testSize() { + linkedList.add(0); + Assert.assertEquals(linkedList.size(), 101); + linkedList.add(0, 10); + Assert.assertEquals(linkedList.size(), 102); + linkedList.remove(0); + Assert.assertEquals(linkedList.size(), 101); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(22); + Assert.assertEquals(linkedList.get(0), 22); + linkedList.addFirst(44); + Assert.assertEquals(linkedList.get(0), 44); + Assert.assertEquals(linkedList.size(), 102); + } + + @Test + public void testAddLast() { + linkedList.addLast(22); + Assert.assertEquals(linkedList.get(100), 22); + linkedList.addLast(44); + Assert.assertEquals(linkedList.get(101), 44); + } + + @Test + public void testRemoveFirst() { + Assert.assertEquals(linkedList.removeFirst(), 0); + Assert.assertEquals(linkedList.removeFirst(), 1); + } + + @Test + public void testRemoveLast() { + Assert.assertEquals(linkedList.removeLast(),99 ); + Assert.assertEquals(linkedList.removeLast(), 98); + } + + @Test + public void testIterator() { + Iterator iterator = linkedList.iterator(); + for(int i = 0;iterator.hasNext();i++){ + Assert.assertEquals(iterator.next(), i); + } + } + +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java new file mode 100644 index 0000000000..7302b5ec38 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/QueueTest.java @@ -0,0 +1,56 @@ +package week01.BasicDataStructureTest; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.Queue; + + +public class QueueTest { + + Queue queue = new Queue(); + + @Before + public void setUp() throws Exception { + for(int i=0;i<100;i++){ + queue.enQueue(i); + } + } + + @Test + public void testEnQueue() { + Assert.assertEquals(queue.size(), 100); + for(int i =0;i<100;i++){ + queue.enQueue(i); + } + Assert.assertEquals(queue.size(), 200); + } + + @Test + public void testDeQueue() { + for(int i =0;i<100;i++){ + Assert.assertEquals(queue.deQueue(), i); + } + + } + + @Test + public void testIsEmpty() { + Assert.assertEquals(queue.isEmpty(), false); + for(int i=0;i<100;i++){ + queue.deQueue(); + } + Assert.assertEquals(queue.isEmpty(), true); + } + + @Test + public void testSize() { + Assert.assertEquals(queue.size(), 100); + queue.enQueue(100); + Assert.assertEquals(queue.size(), 101); + queue.deQueue(); + Assert.assertEquals(queue.size(), 100); + } + +} diff --git a/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java new file mode 100644 index 0000000000..ae6d3a39d4 --- /dev/null +++ b/group01/1814014897/zhouhui/src/week01/BasicDataStructureTest/StackTest.java @@ -0,0 +1,71 @@ +package week01.BasicDataStructureTest; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import week01.BasicDataStructure.Stack; + + +public class StackTest { + + Stack stack = new Stack(); + + @Before + public void setUp() throws Exception { + for(int i =0 ;i <100;i++){ + stack.push(i); + } + } + + @Test + public void testPush() { + Assert.assertEquals(stack.peek(), 99); + for(int i =0;i <200;i++){ + stack.push(i); + } + Assert.assertEquals(stack.peek(), 199); + Assert.assertEquals(stack.size(), 300); + } + + @Test + public void testPop() { + Assert.assertEquals(stack.pop(), 99); + Assert.assertEquals(stack.pop(), 98); + for(int i=0;i<98;i++){ + stack.pop(); + } + Assert.assertEquals(stack.size(), 0); + } + + @Test + public void testPeek() { + for(int i=0;i<100;i++){ + Assert.assertEquals(stack.peek(), 99); + Assert.assertEquals(stack.size(), 100); + } + stack.pop(); + Assert.assertEquals(stack.peek(), 98); + Assert.assertEquals(stack.peek(), 98); + } + + @Test + public void testIsEmpty() { + Assert.assertEquals(stack.isEmpty(), false); + for(int i =0 ;i <100;i++){ + stack.pop(); + } + Assert.assertEquals(stack.isEmpty(), true); + } + + @Test + public void testSize() { + stack.push(100); + Assert.assertEquals(stack.size(), 101); + stack.pop(); + Assert.assertEquals(stack.size(), 100); + stack.peek(); + Assert.assertEquals(stack.size(), 100); + } + +} diff --git a/group01/1925347167/1925347167.md b/group01/1925347167/1925347167.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group01/1925347167/1925347167.md @@ -0,0 +1 @@ + diff --git a/group01/1925347167/Week1 Basic Data Structure/ArrayList.java b/group01/1925347167/Week1 Basic Data Structure/ArrayList.java new file mode 100644 index 0000000000..2797e0b129 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/ArrayList.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (fullCheck()) + elementData = Arrays.copyOf(elementData, size*2); + elementData[size++] = o; + } + public void add(int index, Object o){ + + if (fullCheck()) + elementData = Arrays.copyOf(elementData, size*2); + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + for (int i = size; i > index; --i) + elementData[i] = elementData[i-1]; + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (rangeCheck(index)) + return elementData[index]; + throw new IndexOutOfBoundsException(); + } + + public Object remove(int index){ + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + Object rmo = elementData[index]; + for (int i = index; i < size-1; ++i) + elementData[i] = elementData[i-1]; + size--; + return rmo; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + private boolean rangeCheck(int index) { + if (index < 0 || index >= size) + return false; + return true; + } + + private boolean fullCheck() { + if (size >= elementData.length) + return true; + return false; + } + +} diff --git a/group01/1925347167/Week1 Basic Data Structure/BinaryTreeNode.java b/group01/1925347167/Week1 Basic Data Structure/BinaryTreeNode.java new file mode 100644 index 0000000000..45827be3a5 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/BinaryTreeNode.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object o) { + data = o; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group16/542087872/src/com/coding/basic/Iterator.java b/group01/1925347167/Week1 Basic Data Structure/Iterator.java similarity index 100% rename from group16/542087872/src/com/coding/basic/Iterator.java rename to group01/1925347167/Week1 Basic Data Structure/Iterator.java diff --git a/group01/1925347167/Week1 Basic Data Structure/LinkedList.java b/group01/1925347167/Week1 Basic Data Structure/LinkedList.java new file mode 100644 index 0000000000..3097f69edc --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/LinkedList.java @@ -0,0 +1,127 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public void add(Object o){ + Node tmp = head; + while (tmp.next != null) + tmp = tmp.next; + + Node n = new Node(o); + tmp.next = n; + size++; + } + public void add(int index , Object o){ + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + + if (index == 0) { + Node newhead = new Node(o); + newhead.next = head; + head = newhead; + } else { + Node tmp = head; + for (int i = 0; i < index - 1; ++i) + tmp = tmp.next; + Node node = new Node(o); + node.next = tmp.next; + tmp.next = node; + } + + size++; + } + public Object get(int index){ + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + Node tmp = head; + for (int i = 0; i < index - 1; ++i) + tmp = tmp.next; + return tmp.data; + + } + + public Object remove(int index){ + if (!rangeCheck(index)) + throw new IndexOutOfBoundsException(); + + if (index == 0) { + Node oldHead= head; + head = head.next; + size--; + return oldHead.data; + }else { + Node tmp = head; + for (int i = 0; i < index - 1; i++) { + tmp = tmp.next; + } + Node node = tmp.next; + tmp.next = node.next; + size--; + return node.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newHead = new Node(o); + newHead.next = head; + head = newHead; + size++; + } + public void addLast(Object o){ + Node tmp = head; + while (tmp.next != null) { + tmp = tmp.next; + } + Node node = new Node(o); + tmp.next = node; + size++; + } + public Object removeFirst(){ + if (head == null) + throw new IndexOutOfBoundsException(); + Node oldHead = head; + head = head.next; + size--; + return oldHead.data; + } + public Object removeLast(){ + if (head == null) + throw new IndexOutOfBoundsException(); + Node tmp = head; + while (tmp.next.next != null) { + tmp = tmp.next; + } + Node node = tmp.next; + tmp.next = null; + size--; + return node.data; + } + public Iterator iterator(){ + return null; + } + + private boolean rangeCheck(int index) { + if (index < 0 || index >= size) + return false; + return true; + } + + private static class Node{ + Object data; + Node next; + + Node(Object data) { + this.data = data; + next = null; + } + + } +} diff --git a/group16/542087872/src/com/coding/basic/List.java b/group01/1925347167/Week1 Basic Data Structure/List.java similarity index 100% rename from group16/542087872/src/com/coding/basic/List.java rename to group01/1925347167/Week1 Basic Data Structure/List.java diff --git a/group01/1925347167/Week1 Basic Data Structure/Queue.java b/group01/1925347167/Week1 Basic Data Structure/Queue.java new file mode 100644 index 0000000000..b8c394b833 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList llist = new LinkedList(); + + public void enQueue(Object o){ + llist.add(o); + } + + public Object deQueue(){ + if (isEmpty()) + return null; + return llist.removeFirst(); + } + + public boolean isEmpty(){ + return (llist.size()==0); + } + + public int size(){ + return -llist.size(); + } +} diff --git a/group01/1925347167/Week1 Basic Data Structure/Stack.java b/group01/1925347167/Week1 Basic Data Structure/Stack.java new file mode 100644 index 0000000000..4458cb61d7 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) + return null; + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (elementData.size() == 0) + return null; + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return (elementData.size() == 0); + } + public int size(){ + return elementData.size(); + } +} diff --git a/group01/1925347167/Week1 Basic Data Structure/readme.md b/group01/1925347167/Week1 Basic Data Structure/readme.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group01/1925347167/Week1 Basic Data Structure/readme.md @@ -0,0 +1 @@ + diff --git a/group01/2137642225/work01/.classpath b/group01/2137642225/work01/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group01/2137642225/work01/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group01/2137642225/work01/.gitignore b/group01/2137642225/work01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/2137642225/work01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/2137642225/work01/.project b/group01/2137642225/work01/.project new file mode 100644 index 0000000000..f8dde642e5 --- /dev/null +++ b/group01/2137642225/work01/.project @@ -0,0 +1,17 @@ + + + work01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/2137642225/work01/src/com/coding/mybasic/ArrayList.java b/group01/2137642225/work01/src/com/coding/mybasic/ArrayList.java new file mode 100644 index 0000000000..1826ee7c50 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/ArrayList.java @@ -0,0 +1,139 @@ +package com.coding.mybasic; + +public class ArrayList implements List{ + + private static final int DEF_CAPACITY = 10; + private int size; + private Object[] elementData; + + public ArrayList(){ + elementData = new Object[DEF_CAPACITY]; + } + + public ArrayList(int initCapacity) { + if(initCapacity <= 0){ + throw new RuntimeException("初始化长度必须大于0"); + } + elementData = new Object[initCapacity]; + } + + @Override + public void add(Object element) { + checkArrayOutOfRange(); + elementData[size++] = element; + } + + + @Override + public void add(int index, Object element) { + // 末尾插入 + if(index == size){ + add(element); + return; + } + // index 在 0到size 之间,index之后元素要后移 + checkIndex(index); + checkArrayOutOfRange(); + moveBackwardElement(index); + elementData[index] = element; + size++; + } + + + @Override + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + checkIndex(index); + Object temp = elementData[index]; + moveForwardElement(index); + elementData[size--] = null; + return temp; + } + + + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int i = 0; + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + checkIndex(i); + return elementData[i++]; + } + + } + + /** + * 数组增长 + * @param newCapacity 新数组容量 + */ + private void grow(int newCapacity) { + Object[] dest = new Object[newCapacity]; + System.arraycopy(elementData, 0, dest , 0, elementData.length); + elementData = dest; + } + + /** + * 检查index index >=0 且 < size + * @param index + * @throws Exception + */ + private void checkIndex(int index) { + if(index < 0){ + throw new RuntimeException("index 必须大于0"); + } + // 越界 + if(index >= size){ + throw new RuntimeException("index 必须小于size:" + size); + } + } + + /** + * 检查数组容量是否已满,已满则扩容 + */ + private void checkArrayOutOfRange() { + if(size >= elementData.length){ + // 扩容 默认新容量是原来容量的2倍 + grow(elementData.length * 2); + } + } + + /** + * 后移元素,从index开始 + * @param index + */ + private void moveBackwardElement(int index) { + for (int i = size; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + } + /** + * 前移元素,从index开始 + * @param index + */ + private void moveForwardElement(int index) { + for (int i = index; i < size; i++) { + elementData[i] = elementData[i + 1]; + } + } + +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/BinaryTreeNode.java b/group01/2137642225/work01/src/com/coding/mybasic/BinaryTreeNode.java new file mode 100644 index 0000000000..21bd8f696f --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/BinaryTreeNode.java @@ -0,0 +1,73 @@ +package com.coding.mybasic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Integer 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(Integer o){ + if(o == null){ + throw new RuntimeException("不能插入空值"); + } + BinaryTreeNode searchNode = search(this,o); + if(isExistData(searchNode,o)){ + throw new RuntimeException("该值已存在 无法插入"); + } + if(searchNode != null){ + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode.setData(o); + if(searchNode.data.intValue() > o.intValue()){ + searchNode.setLeft(binaryTreeNode); + }else{ + searchNode.setRight(binaryTreeNode); + } + } else { + throw new RuntimeException("根节点未赋值,无法插入"); + } + return this; + } + + private boolean isExistData(BinaryTreeNode searchNode,Integer data) { + return searchNode != null && searchNode.data.intValue() == data.intValue(); + + } + + private BinaryTreeNode search(BinaryTreeNode binaryTreeNode, Integer data) { + if(binaryTreeNode == null || binaryTreeNode.data == null){ + return null; + } + Integer curNodeData = binaryTreeNode.data; + if(curNodeData.intValue() > data.intValue()){// 左 curNodeData > data + if(binaryTreeNode.left != null){ + return search(binaryTreeNode.left,data); + } + }else if(curNodeData.intValue() < data.intValue()){ + if(binaryTreeNode.right != null){ + return search(binaryTreeNode.right,data); + } + + } + return binaryTreeNode; + } + +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/Iterator.java b/group01/2137642225/work01/src/com/coding/mybasic/Iterator.java new file mode 100644 index 0000000000..622cc5b902 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.mybasic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java b/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java new file mode 100644 index 0000000000..ab37360e78 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/LinkedList.java @@ -0,0 +1,226 @@ +package com.coding.mybasic; + +public class LinkedList implements List { + + private Node head; + private Node last; + private int size; + + public LinkedList() { + } + + @Override + public void add(Object element) { + if(head == null){ + addHead(element); + }else{ + addLast(element); + } + } + + @Override + public void add(int index, Object element) { + if(index == size){ + add(element); + return; + } + + if(index == 0){ + addFirst(element); + return; + } + checkIndex(index); + insertElement(index - 1,element); + } + + + @Override + public Object get(int index) { + checkIndex(index); + Node node = getNodeByIndex(index); + return node != null ? node.data : null; + } + + @Override + public Object remove(int index) { + + checkIndex(index); + Object element = null; + if(index == 0){ + element = removeFirst(); + } + else if(index == (size - 1)){ + element = removeLast(); + } + else { + element = removeMiddle(index); + } + return element; + } + + + @Override + public int size() { + return size; + } + + + @Override + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + private Node node = head; + int i = 0; + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + checkIndex(i); + Object element = node.data; + node = node.next; + i++; + return element; + } + + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = head.next; + head = node; + size++; + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o; + node.next = null; + last.next = node; + last = node; + size++; + } + public Object removeFirst(){ + return removeFirstNode(); + } + public Object removeLast(){ + return removeLastNode(); + } + private Object removeMiddle(int index) { + Node temp = getNodeByIndex(index - 1); + Node removeNode = temp.next; + Object element = removeNode.data; + temp.next = removeNode.next; + removeNode = null; + size--; + return element; + } + + /** + * 检查index index >=0 且 < size + * @param index + * @throws Exception + */ + private void checkIndex(int index) { + if(index < 0){ + throw new RuntimeException("index 必须大于0"); + } + // 越界 + if(index >= size){ + throw new RuntimeException("index 必须小于size:" + size); + } + } + + /** + * 添加head + * @param element + */ + private void addHead(Object element) { + head = new Node(); + head.data = element; + head.next = null; + last = head; + size++; + } + /** + * 插入序号在0-size之间的元素,不包含0和size位置 + * @param index + * @param element + */ + private void insertElement(int index, Object element) { + + Node temp = getNodeByIndex(index); + if(temp != null){ + Node node = new Node(); + node.data = element; + node.next = temp.next; + temp.next = node; + } + size++; + } + /** + * 获取下标为index的元素 + * @param index + * @return + */ + private Node getNodeByIndex(int index) { + Node temp = head; + int i = 0; + + while(i < size){ + if(i == index){ + return temp; + } + temp = temp.next; + i++; + } + + return null; + } + /** + * 移除最后一个元素 + * @return + */ + private Object removeLastNode() { + Node node = getNodeByIndex(size - 2); + Node lastNode = node.next; + Object element = lastNode.data; + lastNode = null; + last = node; + size--; + return element; + } + /** + * 移除第一个元素 + * @return + */ + private Object removeFirstNode() { + Node node = head.next; + Object element = head.data; + head = null; + head = node; + size--; + return element; + } + + + + private static class Node{ + Object data; + Node next; + public Node() { + } + @SuppressWarnings("unused") + public Node(Object data, Node next) { + super(); + this.data = data; + this.next = next; + } + + + } +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/List.java b/group01/2137642225/work01/src/com/coding/mybasic/List.java new file mode 100644 index 0000000000..87a58a6c4c --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/List.java @@ -0,0 +1,10 @@ +package com.coding.mybasic; + +public interface List { + public void add(Object element); + public void add(int index, Object element); + public Object get(int index); + public Object remove(int index); + public int size(); + public Iterator iterator(); +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/Queue.java b/group01/2137642225/work01/src/com/coding/mybasic/Queue.java new file mode 100644 index 0000000000..36d10fd668 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.mybasic; + +public class Queue { + private LinkedList linkedList = new LinkedList(); + public void enQueue(Object o){ + linkedList.add(o); + } + + public Object deQueue(){ + checkEmptyQueue(); + return linkedList.remove(0); + } + + public boolean isEmpty(){ + return size() <= 0; + } + + public int size(){ + return linkedList.size(); + } + + /** + * 检查队列是否为空 + */ + private void checkEmptyQueue() { + if(isEmpty()){ + throw new RuntimeException("size:" + size() + " 空队列"); + } + } +} diff --git a/group01/2137642225/work01/src/com/coding/mybasic/Stack.java b/group01/2137642225/work01/src/com/coding/mybasic/Stack.java new file mode 100644 index 0000000000..f50e686317 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/mybasic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.mybasic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + checkEmptyStack(); + return elementData.remove(size() - 1); + } + + public Object peek(){ + checkEmptyStack(); + Object element = elementData.get(size() - 1); + return element; + } + + public boolean isEmpty(){ + return size() <= 0; + } + public int size(){ + return elementData.size(); + } + /** + * 检查栈是否为空 + */ + private void checkEmptyStack() { + if(isEmpty()){ + throw new RuntimeException("size:" + size() + " 空栈"); + } + } +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestArrayList.java b/group01/2137642225/work01/src/com/coding/test/TestArrayList.java new file mode 100644 index 0000000000..8bd8952195 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestArrayList.java @@ -0,0 +1,81 @@ +package com.coding.test; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.ArrayList; +import com.coding.mybasic.Iterator; +import com.coding.mybasic.List; + +public class TestArrayList { + + private List list; + @Before + public void before() { + list = new ArrayList(); + } + + @Test + public void testAddObject() { + list.add("ele"); + Assert.assertEquals("ele", list.get(0)); + } + + @Test + public void testAddIntObject() { + + for (int i = 0; i < 5; i++) { + list.add(i,i); + Assert.assertEquals(i, list.get(i)); + } + + } + + @Test + public void testGet() { + list.add("ss"); + Assert.assertEquals("ss", list.get(0)); + } + + @Test + public void testRemove() { + list.add("we"); + list.add(1, "gga"); + list.add(0, "start"); + list.add(3, "end"); + + Assert.assertEquals("end", list.remove(3)); + + } + + @Test + public void testSize() { + + for (int i = 0; i < 10; i++) { + list.add(i); + } + + Assert.assertEquals(10, list.size()); + } + + @Test + public void testIterator() { + + for (int i = 0; i < 10; i++) { + list.add(i); + } + Iterator iterator = list.iterator(); + int i = 0; + while(iterator.hasNext()){ + Assert.assertEquals(i++, iterator.next()); + } + } + + @After + public void after(){ + + } +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestBinaryTreeNode.java b/group01/2137642225/work01/src/com/coding/test/TestBinaryTreeNode.java new file mode 100644 index 0000000000..662bb55570 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestBinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.BinaryTreeNode; + +public class TestBinaryTreeNode { + + private BinaryTreeNode node; + @Before + public void before(){ + node = new BinaryTreeNode(); + } + + @Test + public void testInsert() { + node.insert(1); + node.insert(0); + node.insert(3); + node.insert(-2); + node.insert(-1); + assertEquals(1, node.getData()); + assertEquals(0, node.getLeft().getData()); + assertEquals(3, node.getRight().getData()); + assertEquals(-2, node.getLeft().getLeft().getData()); + assertEquals(-1, node.getLeft().getLeft().getRight().getData()); + } + +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestLinkedList.java b/group01/2137642225/work01/src/com/coding/test/TestLinkedList.java new file mode 100644 index 0000000000..57a8b13bb8 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestLinkedList.java @@ -0,0 +1,73 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.Iterator; +import com.coding.mybasic.LinkedList; +import com.coding.mybasic.List; + +public class TestLinkedList { + + private List list; + + @Before + public void before(){ + list = new LinkedList(); + } + + @Test + public void testAddObject() { + list.add(1); + + System.out.println(list.get(0)); + assertEquals(1, list.get(0)); + assertEquals(1, list.size()); + } + + @Test + public void testAddIntObject() { + list.add(0,1); + System.out.println(list.get(0)); + assertEquals(1, list.get(0)); + assertEquals(1, list.size()); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + list.add(0,1); + System.out.println(list.remove(0)); + assertEquals(0, list.size()); + } + + @Test + public void testSize() { + + for(int i = 0; i < 10; i++){ + list.add(i, i); + } + + assertEquals(10, list.size()); + } + + @Test + public void testIterator() { + + for(int i = 0; i < 10; i++){ + list.add(i, i); + } + Iterator iterator = list.iterator(); + int i = 0; + while(iterator.hasNext()){ + assertEquals(i++, iterator.next()); + } + //iterator.next(); + } +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestQueue.java b/group01/2137642225/work01/src/com/coding/test/TestQueue.java new file mode 100644 index 0000000000..367a44d151 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestQueue.java @@ -0,0 +1,36 @@ +package com.coding.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.Queue; + +public class TestQueue { + + private Queue queue; + @Before + public void before(){ + queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + } + @Test + public void testEnQueue() { + queue.enQueue(3); + assertEquals(3, queue.size()); + } + + @Test + public void testDeQueue() { + assertEquals(2, queue.deQueue()); + assertEquals(1, queue.deQueue()); + } + + @Test + public void testSize() { + assertEquals(2, queue.size()); + } + +} diff --git a/group01/2137642225/work01/src/com/coding/test/TestStack.java b/group01/2137642225/work01/src/com/coding/test/TestStack.java new file mode 100644 index 0000000000..0e278f2992 --- /dev/null +++ b/group01/2137642225/work01/src/com/coding/test/TestStack.java @@ -0,0 +1,49 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.mybasic.Stack; + +public class TestStack { + + private Stack stack; + @Before + public void before() { + stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + } + + @Test + public void testPush() { + assertEquals(3, stack.peek()); + } + + @Test + public void testPop() { + assertEquals(3, stack.pop()); + assertEquals(2, stack.pop()); + assertEquals(1, stack.pop()); + //stack.pop(); + //System.out.println(stack.size()); + } + + @Test + public void testPeek() { + assertEquals(3, stack.peek()); + assertEquals(3, stack.pop()); + assertEquals(2, stack.pop()); + //assertEquals(1, stack.pop()); + assertEquals(1, stack.peek()); + } + + @Test + public void testSize() { + assertEquals(3, stack.size()); + } + +} diff --git a/group01/275150374/275150374Learning/.idea/compiler.xml b/group01/275150374/275150374Learning/.idea/compiler.xml new file mode 100644 index 0000000000..217af471a9 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/group01/275150374/275150374Learning/.idea/description.html b/group01/275150374/275150374Learning/.idea/description.html new file mode 100644 index 0000000000..db5f129556 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/group01/275150374/275150374Learning/.idea/encodings.xml b/group01/275150374/275150374Learning/.idea/encodings.xml new file mode 100644 index 0000000000..e206d70d85 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/group01/275150374/275150374Learning/.idea/misc.xml b/group01/275150374/275150374Learning/.idea/misc.xml new file mode 100644 index 0000000000..de8f7c75a3 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group01/275150374/275150374Learning/.idea/modules.xml b/group01/275150374/275150374Learning/.idea/modules.xml new file mode 100644 index 0000000000..5534fceb30 --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group01/275150374/275150374Learning/.idea/vcs.xml b/group01/275150374/275150374Learning/.idea/vcs.xml new file mode 100644 index 0000000000..c2365ab11f --- /dev/null +++ b/group01/275150374/275150374Learning/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group01/275150374/275150374Learning/275150374Learning.iml b/group01/275150374/275150374Learning/275150374Learning.iml new file mode 100644 index 0000000000..d5c0743275 --- /dev/null +++ b/group01/275150374/275150374Learning/275150374Learning.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group01/275150374/275150374Learning/src/task01/ArrayList.java b/group01/275150374/275150374Learning/src/task01/ArrayList.java new file mode 100644 index 0000000000..8d604f109e --- /dev/null +++ b/group01/275150374/275150374Learning/src/task01/ArrayList.java @@ -0,0 +1,86 @@ +package task01; + +import java.util.Arrays; + +/**第一周作业 + * 自己实现一个 ArrayList + * Created by eurry on 2017/2/26. + */ +public class ArrayList { + /** + * ArrayList的长度 + */ + private int size = 0; + + private Object[] elementData = {}; + + public void add(Object o){ + elementData = Arrays.copyOf(elementData, size+1); + elementData[size] = o; + size++; + } + + public void add(int index, Object o){ + if(index < size){ + elementData = Arrays.copyOf(elementData, size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + }else{ + elementData = Arrays.copyOf(elementData, index+1); + elementData[index] = o; + size = index+1; + } + } + + public Object get(int index){ + if(index < size){ + return elementData[index]; + }else{ + throw new IndexOutOfBoundsException("导致对数组范围以外的数据的访问"); + } + } + + public Object remove(int index){ + if(index < size){ + Object re = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData = Arrays.copyOf(elementData, size-1); + size--; + return re; + }else{ + throw new IndexOutOfBoundsException("导致对数组范围以外的数据的访问"); + } + } + + public int size(){ + return size; + } + + public String toString(){ + String str = null; + if(elementData.length > 0){ + str = ""; + for (Object anElementData : elementData) { + str += anElementData.toString() + ","; + } + str = str.substring(0, str.length()-1); + } + return str; + } + + /** + * 测试 + */ + public static void main(String[] str){ + ArrayList list = new ArrayList(); + list.add("A"); + list.add("B"); + list.add(1, "C"); + list.add("D"); + Object d = list.get(3); + Object dd = list.remove(3); + System.out.println(list.size()); + System.out.println(list.toString()); + } +} diff --git a/group01/275150374/275150374Learning/src/task01/LinkedList.java b/group01/275150374/275150374Learning/src/task01/LinkedList.java new file mode 100644 index 0000000000..23eb1adaae --- /dev/null +++ b/group01/275150374/275150374Learning/src/task01/LinkedList.java @@ -0,0 +1,162 @@ +package task01; + +import java.util.Arrays; + +/**第一周作业 + * 自己实现一个 LinkedList + * Created by eurry on 2017/2/26. + */ +public class LinkedList { + + private int size = 0; + private Node head=null; + + public void add(Object o){ + if(size == 0){ + head = new Node(o); + }else{ + Node next = head; + while(next.next != null){ + next = next.next; + } + next.next = new Node(o); + } + size++; + } + + public void add(int index, Object o){ + if(index <= size){ + if(size == 0){ + add(o); + }else{ + if(index==0){ + addFirst(o); + }else if(index==size){ + add(o); + }else{ + Node next = head; + for(int i=0; i 0){ + Node ele = null; + Node next = head; + for(int i=0; i + + + root + com.coding2017 + 1.0-SNAPSHOT + + 4.0.0 + + basic + + + + + junit + junit + + + junit + junit-dep + + + + + \ No newline at end of file diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/ArrayList.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..a4199bdbdb --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding2017.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[4]; + + public void add(Object o) { + if (noSpace()) { + extendSpace(); + } + + elementData[size++] = o; + } + + private void extendSpace() { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + + private boolean noSpace() { + return size == elementData.length; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (noSpace()) { + extendSpace(); + } + + if (index == size) { + add(o); + return; + } + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + if (index == size - 1) { + return elementData[--size]; + } + + Object removed = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return removed; + } + + public int size() { + return size; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("["); + if (size > 0) { + builder.append(get(0)); + } + for (int i = 1; i < size; i++) { + builder.append(", ").append(get(i)); + } + builder.append("]"); + return builder.toString(); + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + private int pos; + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public Object next() { + return ArrayList.this.get(pos++); + } + } +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..acf4798b9e --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,57 @@ +package com.coding2017.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode insert(Integer o) { + if (o <= data) { + if (left == null) { + left = new BinaryTreeNode(o); + return left; + } + return left.insert(o); + } else { + if (right == null) { + right = new BinaryTreeNode(o); + return right; + } + return right.insert(o); + } + } + + public BinaryTreeNode(Integer data) { + this.data = data; + } + + public Integer getData() { + return data; + } + + public void setData(Integer 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 String toString() { + return data + " " + left + " " + right; + } +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/Iterator.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..19e214cfbb --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/LinkedList.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..5aeeb7c7f8 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/LinkedList.java @@ -0,0 +1,179 @@ +package com.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (index == size) { + addLast(o); + } else if (index == 0) { + addFirst(o); + } else { + Node node = new Node(o); + Node prevNode = getNode(index - 1); + Node nextNode = prevNode.next; + prevNode.next = node; + node.prev = prevNode; + nextNode.prev = node; + node.next = nextNode; + size++; + } + } + + private Node getNode(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Node node = head; + for (int j = 0; j < index; j++) { + node = node.next; + } + return node; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + return getNode(index).data; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + if (index == 0) { + return removeFirst(); + } else if (index == size - 1) { + return removeLast(); + } else { + Node node = getNode(index); + node.prev.next = node.next; + node.next.prev = node.prev; + size--; + return node.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + if (size == 0) { + head = node; + tail = node; + } else { + head.prev = node; + node.next = head; + head = node; + } + size++; + } + + public void addLast(Object o) { + if (size == 0) { + addFirst(o); + } else { + Node node = new Node(o); + tail.next = node; + node.prev = tail; + tail = node; + size++; + } + } + + public Object removeFirst() { + if (size == 0) { + throw new IndexOutOfBoundsException(); + } + Node node = head; + if (size == 1) { + head = null; + tail = null; + size--; + } else { + head.next.prev = null; + head = head.next; + size--; + } + return node.data; + } + + public Object removeLast() { + if (size == 0) { + throw new IndexOutOfBoundsException(); + } + if (size == 1) { + return removeFirst(); + } + Node node = tail; + tail.prev.next = null; + tail = tail.prev; + size--; + return node.data; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("["); + if (size > 0) { + builder.append(get(0)); + } + for(Node node = head.next; node != null; node = node.next) { + builder.append(", ").append(node.data); + } + builder.append("]"); + return builder.toString(); + } + + private static class Node { + private Object data; + private Node next; + private Node prev; + + public Node() {} + + private Node(Object data) { + this.data = data; + } + } + + private class LinkedListIterator implements Iterator { + private Node node; + + public LinkedListIterator() { + this.node = LinkedList.this.head; + } + + @Override + public boolean hasNext() { + return node != null; + } + + @Override + public Object next() { + Node temp = node; + node = node.next; + return temp.data; + } + } +} diff --git a/group16/214074094/src/com/coding/basic/List.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/List.java similarity index 84% rename from group16/214074094/src/com/coding/basic/List.java rename to group01/280646174/basic/src/main/java/com/coding2017/basic/List.java index 5da9b0d4c6..0fee4d7a42 100644 --- a/group16/214074094/src/com/coding/basic/List.java +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/List.java @@ -1,7 +1,6 @@ -package coding.basic; +package com.coding2017.basic; public interface List { - void add(Object o); void add(int index, Object o); @@ -11,5 +10,4 @@ public interface List { Object remove(int index); int size(); - } diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/Queue.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/Queue.java new file mode 100644 index 0000000000..f611b874e0 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding2017.basic; + +public class Queue { + + LinkedList list = new LinkedList(); + + public void enQueue(Object o) { + list.addLast(o); + } + + public Object deQueue() { + return list.removeFirst(); + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/basic/Stack.java b/group01/280646174/basic/src/main/java/com/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3ec7b5788b --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding2017.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/group01/280646174/basic/src/test/java/com/coding2017/basic/ArrayListTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..21e9d59694 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/ArrayListTest.java @@ -0,0 +1,79 @@ +package com.coding2017.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by kaitao.li on 17/2/21. + */ +public class ArrayListTest { + @Test + public void testAdd() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + Assert.assertTrue(arrayList.get(0).equals("0")); + } + + @Test + public void testAddWithIndex() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + arrayList.add(1, "2"); + Assert.assertTrue(arrayList.get(1).equals("2")); + Assert.assertTrue(arrayList.get(2).equals("1")); + Assert.assertTrue(arrayList.size() == 3); + } + + @Test + public void get() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + Assert.assertTrue(arrayList.get(1).equals("1")); + } + + @Test + public void remove() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + arrayList.add("2"); + Object remove = arrayList.remove(1); + Assert.assertTrue(remove.equals("1")); + Assert.assertTrue(arrayList.size() == 2); + } + + @Test + public void size() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + Assert.assertEquals(arrayList.size(), 2); + } + + @Test + public void testExtend() { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("3"); + arrayList.add("4"); + Assert.assertTrue(arrayList.get(4).equals("4")); + } + + @Test + public void iterator() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + Iterator iterator = arrayList.iterator(); + Assert.assertTrue(iterator.hasNext()); + Assert.assertTrue(iterator.next().equals("0")); + Assert.assertTrue(iterator.hasNext()); + Assert.assertTrue(iterator.next().equals("1")); + Assert.assertTrue(!iterator.hasNext()); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/BinaryTreeNodeTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..3a9877c596 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/BinaryTreeNodeTest.java @@ -0,0 +1,24 @@ +package com.coding2017.basic; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 17/2/24. + */ +public class BinaryTreeNodeTest { + + @Test + public void insert() throws Exception { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(5); + binaryTreeNode.insert(4); + binaryTreeNode.insert(6); + binaryTreeNode.insert(5); + assertTrue(binaryTreeNode.getLeft().getData() == 4); + assertTrue(binaryTreeNode.getRight().getData() == 6); + assertTrue(binaryTreeNode.getLeft().getRight().getData() == 5); + System.out.println(binaryTreeNode); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/LinkedListTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..f6855d3583 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/LinkedListTest.java @@ -0,0 +1,83 @@ +package com.coding2017.basic; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 17/2/21. + */ +public class LinkedListTest { + @Test + public void testAdd() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + Assert.assertTrue(list.get(0).equals("0")); + } + + @Test + public void testAddWithIndex() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + list.add(1, "2"); + Assert.assertTrue(list.get(1).equals("2")); + Assert.assertTrue(list.get(2).equals("1")); + Assert.assertTrue(list.size() == 3); + } + + @Test + public void get() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + Assert.assertTrue(list.get(1).equals("1")); + } + + @Test + public void remove() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + list.add("2"); + Object remove = list.remove(1); + Assert.assertTrue(remove.equals("1")); + Assert.assertTrue(list.size() == 2); + } + + @Test + public void size() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + Assert.assertEquals(list.size(), 2); + } + + @Test + public void testAddFirst() { + LinkedList list = new LinkedList(); + list.addFirst("0"); + Assert.assertTrue(list.get(0).equals("0")); + list.addFirst("1"); + Assert.assertTrue(list.get(0).equals("1")); + list.removeFirst(); + Assert.assertTrue(list.get(0).equals("0")); + list.removeLast(); + Assert.assertTrue(list.size() == 0); + } + + @Test + public void iterator() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add("0"); + arrayList.add("1"); + Iterator iterator = arrayList.iterator(); + Assert.assertTrue(iterator.hasNext()); + Assert.assertTrue(iterator.next().equals("0")); + Assert.assertTrue(iterator.hasNext()); + Assert.assertTrue(iterator.next().equals("1")); + Assert.assertTrue(!iterator.hasNext()); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/QueueTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..5fde883433 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/QueueTest.java @@ -0,0 +1,23 @@ +package com.coding2017.basic; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 17/2/21. + */ +public class QueueTest { + @Test + public void enQueue() throws Exception { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + Assert.assertTrue(queue.size() == 2); + Assert.assertTrue(queue.deQueue().equals(1)); + Assert.assertTrue(queue.deQueue().equals(2)); + Assert.assertTrue(queue.isEmpty()); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/basic/StackTest.java b/group01/280646174/basic/src/test/java/com/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..145d22d371 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/basic/StackTest.java @@ -0,0 +1,24 @@ +package com.coding2017.basic; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by kaitao.li on 17/2/21. + */ +public class StackTest { + @Test + public void push() throws Exception { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + Assert.assertTrue(stack.size() == 2); + Assert.assertTrue(stack.peek().equals(2)); + Assert.assertTrue(stack.pop().equals(2)); + Assert.assertTrue(stack.pop().equals(1)); + Assert.assertTrue(stack.isEmpty()); + } + +} \ No newline at end of file diff --git a/group01/280646174/pom.xml b/group01/280646174/pom.xml new file mode 100644 index 0000000000..c99644072b --- /dev/null +++ b/group01/280646174/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.coding2017 + root + pom + 1.0-SNAPSHOT + + basic + + + + 4.12 + 4.11 + + + + + + + junit + junit + ${junit.junit.version} + test + + + junit + junit-dep + ${junit.junit-dep.version} + test + + + + + \ No newline at end of file diff --git a/group01/349209948/.gitignore b/group01/349209948/.gitignore new file mode 100644 index 0000000000..b3d8633e2b --- /dev/null +++ b/group01/349209948/.gitignore @@ -0,0 +1,3 @@ +/bin/ +*.project +*.classpath \ No newline at end of file diff --git a/group01/349209948/src/week1_0226/ArrayList.java b/group01/349209948/src/week1_0226/ArrayList.java new file mode 100644 index 0000000000..57d25187f8 --- /dev/null +++ b/group01/349209948/src/week1_0226/ArrayList.java @@ -0,0 +1,68 @@ +package week1_0226; +import java.util.Arrays; +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + ensureCapacity(size + 1); + elementData[size++] = o; + } + private void ensureCapacity(int size){ + if (size > elementData.length){ + grow(); + } + } + private void grow(){ + elementData = Arrays.copyOf(elementData, size * 2); + } + public void add(int index, Object o){ + rangeCheckForAdd(index); + ensureCapacity(size + 1); + System.arraycopy(elementData,index, elementData, index + 1, size - index); + elementData[index] = o; + size ++; + } + private void rangeCheckForAdd(int index){ + //index = size时不需要报错? + if (index > size || index < 0){ + throw new IndexOutOfBoundsException(); + } + } + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + private void rangeCheck(int index){ + if (index >= size || index < 0){ + throw new IndexOutOfBoundsException(); + } + } + + public Object remove(int index){ + rangeCheck(index); + Object dest = elementData[index]; + System.arraycopy(elementData, index +1, elementData, index, size-index-1); + size --; + return dest; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator(){ + private int index = 0; + public Object next(){ + return elementData[index++]; + } + public boolean hasNext(){ + return index >= size; + } + }; + } + +} diff --git a/group01/349209948/src/week1_0226/BinaryTreeNode.java b/group01/349209948/src/week1_0226/BinaryTreeNode.java new file mode 100644 index 0000000000..e667bdf15a --- /dev/null +++ b/group01/349209948/src/week1_0226/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package week1_0226; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object o){ + this.data = o; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = new BinaryTreeNode(o); + this.setRight(node); + return node; + } + +} diff --git a/group01/349209948/src/week1_0226/Iterator.java b/group01/349209948/src/week1_0226/Iterator.java new file mode 100644 index 0000000000..3eddc2b726 --- /dev/null +++ b/group01/349209948/src/week1_0226/Iterator.java @@ -0,0 +1,6 @@ +package week1_0226; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group01/349209948/src/week1_0226/LinkedList.java b/group01/349209948/src/week1_0226/LinkedList.java new file mode 100644 index 0000000000..51c9ab8844 --- /dev/null +++ b/group01/349209948/src/week1_0226/LinkedList.java @@ -0,0 +1,129 @@ +package week1_0226; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if (head == null) { + head = new Node(o); + } else { + //遍历到链表的尾部 + Node tail = head; + while (tail.next != null) { + tail = tail.next; + } + Node node = new Node(o); + tail.next = node; + } + size ++; + } + public void add(int index , Object o){ + rangeCheckForAdd(index); + if (index ==0) { + Node node = new Node(o); + node.next = head; + head = node; + } else { + Node preHead = head; + for (int i = 0; i < index -1; i ++){ + preHead = head.next; + } + Node node = new Node(o); + node.next = preHead.next; + preHead.next = node; + } + } + public Object get(int index){ + rangeCheck(index); + Node dest = head; + for (int i = 0; i< index; i++){ + dest = dest.next; + } + return dest.data; + } + private void rangeCheck(int index){ + if (index >= size || index <0){ + throw new IndexOutOfBoundsException(); + } + } + public Object remove(int index){ + rangeCheck(index); + Node preDest = head; + for (int i = 0; i < index; i++){ + preDest = preDest.next; + } + Node dest = preDest.next; + preDest.next = dest.next; + size --; + return dest; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(o); + node.next = head; + head = node; + size ++; + } + public void addLast(Object o){ + Node lastNode = head; + while (lastNode.next != null){ + lastNode = lastNode.next; + } + Node node = new Node(o); + lastNode.next = node; + size++; + } + public Object removeFirst(){ + if (head == null) { + throw new NoSuchElementException(); + } + Node headTmp = head; + head = head.next; + size --; + return headTmp; + } + public Object removeLast(){ + if (head == null) { + throw new NoSuchElementException(); + } + if (head.next == null) { + Node dest = head; + head = null; + size --; + return dest; + } + Node preLastNode = head; + while(preLastNode.next.next != null) { + preLastNode = preLastNode.next; + } + Node dest = preLastNode.next; + preLastNode.next = null; + size --; + return dest; + } + public Iterator iterator(){ + return null; + } + private void rangeCheckForAdd(int index){ + if (index > size || index <0){ + throw new IndexOutOfBoundsException(); + } + } + + private static class Node{ + Object data; + Node next; + Node (Object data) { + this.data = data; + next = null; + } + } +} diff --git a/group01/349209948/src/week1_0226/List.java b/group01/349209948/src/week1_0226/List.java new file mode 100644 index 0000000000..ba1577cfaa --- /dev/null +++ b/group01/349209948/src/week1_0226/List.java @@ -0,0 +1,9 @@ +package week1_0226; + +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/group01/349209948/src/week1_0226/Queue.java b/group01/349209948/src/week1_0226/Queue.java new file mode 100644 index 0000000000..a20d6a303e --- /dev/null +++ b/group01/349209948/src/week1_0226/Queue.java @@ -0,0 +1,21 @@ +package week1_0226; + +public class Queue { + + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + return list.removeFirst(); + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group01/349209948/src/week1_0226/Stack.java b/group01/349209948/src/week1_0226/Stack.java new file mode 100644 index 0000000000..f5d6add66d --- /dev/null +++ b/group01/349209948/src/week1_0226/Stack.java @@ -0,0 +1,29 @@ +package week1_0226; +import java.util.EmptyStackException; +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + checkStack(); + return elementData.remove(elementData.size() - 1); + } + private void checkStack(){ + if (elementData.size() == 0){ + throw new EmptyStackException(); + } + } + public Object peek(){ + checkStack(); + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group01/360176196/.classpath b/group01/360176196/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group01/360176196/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group01/360176196/.gitignore b/group01/360176196/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group01/360176196/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group01/360176196/.project b/group01/360176196/.project new file mode 100644 index 0000000000..a895f05a76 --- /dev/null +++ b/group01/360176196/.project @@ -0,0 +1,17 @@ + + + xqfGit + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/360176196/src/xqfGit/dataStructure/ArrayList.java b/group01/360176196/src/xqfGit/dataStructure/ArrayList.java new file mode 100644 index 0000000000..5996182fbe --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/ArrayList.java @@ -0,0 +1,73 @@ +package xqfGit.dataStructure; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + + public void add(Object o){ + if(this.size >= elementData.length){ + elementData = Arrays.copyOf(elementData, size+1); + elementData[size] = o; + size++; + } + else{ + elementData[size-1] = o; + size++; + } + + } + + public void add(int index, Object o){ + if(index<0 || index>elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBounds"); + } + else{ + System.arraycopy(elementData, index, elementData, index+1, elementData.length+1); + elementData[index] = o; + size++; + } + } + + + public Object get(int index){ + if(index<0 || index>elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBounds"); + } + else{ + return elementData[index]; + } + } + + + public Object remove(int index){ + if(index<0 || index>elementData.length){ + throw new ArrayIndexOutOfBoundsException("OutOfBounds"); + } + else{ + Object reObject = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, elementData.length-1); + size--; + return reObject; + } + } + + public int size(){ + if(this.size < elementData.length){ + return size; + }else{ + elementData = Arrays.copyOf(elementData, size); + return size; + } + } + + + public Iterator iterator(){ + return null; + } + +} diff --git a/group01/360176196/src/xqfGit/dataStructure/BinaryTreeNode.java b/group01/360176196/src/xqfGit/dataStructure/BinaryTreeNode.java new file mode 100644 index 0000000000..7662484884 --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/BinaryTreeNode.java @@ -0,0 +1,49 @@ +package xqfGit.dataStructure; + +import com.sun.swing.internal.plaf.basic.resources.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + + + + public Integer getData() { + return data; + } + public void setData(Integer 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(Integer i){ + + return null; + } + + public BinaryTreeNode (){ + + } + + + + public BinaryTreeNode(BinaryTreeNode b1,BinaryTreeNode b2){ + this.left = b1; + this.right = b2; + } + +} diff --git a/group01/360176196/src/xqfGit/dataStructure/Iterator.java b/group01/360176196/src/xqfGit/dataStructure/Iterator.java new file mode 100644 index 0000000000..ee4842739f --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/Iterator.java @@ -0,0 +1,7 @@ +package xqfGit.dataStructure; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/360176196/src/xqfGit/dataStructure/LinkedList.java b/group01/360176196/src/xqfGit/dataStructure/LinkedList.java new file mode 100644 index 0000000000..a663f85b15 --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/LinkedList.java @@ -0,0 +1,116 @@ +package xqfGit.dataStructure; + +public class LinkedList implements List { + + private Node first; + private Node last; + private int size; + + public void add(Object o){ + Node l = new Node(o); + l = last.next; + size++; + } + + public void add(int index , Object o){ + Node l = new Node(o); + Node n = first; + if(size == index){ + l = last.next; + l = last; + size++; + }else{ + Node m = first; + for(int i =0;i + + + + + diff --git a/group01/378213871/.gitignore b/group01/378213871/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/378213871/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/378213871/.project b/group01/378213871/.project new file mode 100644 index 0000000000..a6666f301e --- /dev/null +++ b/group01/378213871/.project @@ -0,0 +1,17 @@ + + + coding + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/378213871/src/com/coding/basic/week01/ArrayList.java b/group01/378213871/src/com/coding/basic/week01/ArrayList.java new file mode 100644 index 0000000000..5745209a08 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/ArrayList.java @@ -0,0 +1,74 @@ +package com.coding.basic.week01; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void ensureCapacity(int size) { + if (size > elementData.length) { + grow(); + } + } + + public void grow() { + elementData = Arrays.copyOf(elementData, size * 2); + } + + public void add(int index, Object o){ + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(); + } + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(); + } + Object removedItem = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return removedItem; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + private int current = 0; + + public boolean hasNext() { + return current < size(); + } + + public Object next() { + if(!hasNext()) { + throw new java.util.NoSuchElementException(); + } + return elementData[current++]; + } + }; + } + +} diff --git a/group01/378213871/src/com/coding/basic/week01/BinaryTreeNode.java b/group01/378213871/src/com/coding/basic/week01/BinaryTreeNode.java new file mode 100644 index 0000000000..36e20a95c5 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic.week01; + +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/group01/378213871/src/com/coding/basic/week01/Iterator.java b/group01/378213871/src/com/coding/basic/week01/Iterator.java new file mode 100644 index 0000000000..365e98e8b4 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic.week01; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/378213871/src/com/coding/basic/week01/LinkedList.java b/group01/378213871/src/com/coding/basic/week01/LinkedList.java new file mode 100644 index 0000000000..dd6f3fb2e6 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/LinkedList.java @@ -0,0 +1,129 @@ +package com.coding.basic.week01; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + private int size; + + public void add(Object o){ + if (head == null) { + head = new Node(o); + size++; + } else{ + addLast(o); + } + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + if (index == 0) { + addFirst(o); + } else { + //定义标记节点sentinelNode,标记节点的下一个节点即为要新加的元素 + Node sentinelNode = head; + for (int i = 0; i < index - 1; i++) { + sentinelNode = sentinelNode.next; + } + Node node = new Node(o); + node.next = sentinelNode.next; + sentinelNode.next = node; + size++; + } + } + public Object get(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } else { + Node indexNode = head; + for (int i = 0; i < index; i++) { + indexNode = indexNode.next; + } + return indexNode.data; + } + } + public Object remove(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } else { + /** + * sentinelNode是所删除节点的上一个节点; + * indexNode是需要被删除的节点 + */ + Node sentinelNode = head; + Node indexNode = head; + for (int i = 0; i < index - 1; i++) { + sentinelNode = sentinelNode.next; + } + for (int i = 0; i < index; i++) { + indexNode = indexNode.next; + } + Node nextIndexNode = indexNode.next; + sentinelNode.next = nextIndexNode; + indexNode.next = null; + size--; + return indexNode.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + //定义尾节点并通过while循环找到当前链表的尾节点 + Node tailNode = head; + while (tailNode.next != null) { + tailNode = tailNode.next; + } + Node node = new Node(o); + tailNode.next = node; + size++; + } + public Object removeFirst(){ + if (head == null) { + throw new NoSuchElementException(); + } + Node newNode = head; + head = head.next; + size--; + return newNode.data; + } + public Object removeLast(){ + if (head == null) { + throw new NoSuchElementException(); + } + Node newNode = head; + while (newNode.next.next != null) { + newNode = newNode.next; + } + Node lastNode = newNode.next; + newNode.next = null; + size--; + return lastNode.data; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; // 下一个节点 + + private Node(Object data) { + this.data = data; + next = null; + } + } +} diff --git a/group01/378213871/src/com/coding/basic/week01/List.java b/group01/378213871/src/com/coding/basic/week01/List.java new file mode 100644 index 0000000000..966ca016e8 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/List.java @@ -0,0 +1,9 @@ +package com.coding.basic.week01; + +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/group01/378213871/src/com/coding/basic/week01/Queue.java b/group01/378213871/src/com/coding/basic/week01/Queue.java new file mode 100644 index 0000000000..1b25880f67 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic.week01; + +public class Queue { + private LinkedList list = new LinkedList(); + //入队列 + public void enQueue(Object o){ + list.add(o); + } + //出队列 + public Object deQueue(){ + if(list.size() == 0) { + return null; + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group01/378213871/src/com/coding/basic/week01/Stack.java b/group01/378213871/src/com/coding/basic/week01/Stack.java new file mode 100644 index 0000000000..64cfa30da6 --- /dev/null +++ b/group01/378213871/src/com/coding/basic/week01/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic.week01; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + //入栈 + public void push(Object o){ + elementData.add(o); + + } + + //出栈 + public Object pop(){ + if(elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + //读取栈顶元素 + public Object peek(){ + if(elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group01/496740686/src/Impl/MyArraryList.java b/group01/496740686/src/Impl/MyArraryList.java new file mode 100644 index 0000000000..20fb5dcfdf --- /dev/null +++ b/group01/496740686/src/Impl/MyArraryList.java @@ -0,0 +1,141 @@ +package Impl; + +import Interface.ArrayList; +import Interface.Iterator; +import ex.MyArrest; + +/** + * Created by Administrator on 2017/2/25. + */ +public class MyArraryList extends ArrayList { + private Object[] objArr; + private int size; + private int postion; + + public MyArraryList() { + this.objArr = new Object[10]; + this.size = 10; + this.postion = 0; + } + + + public MyArraryList(int size) { + this.objArr = new Object[size]; + this.size = size; + this.postion = 0; + } + + public MyArraryList(Object[] objArr) { + this.objArr = objArr; + this.size = objArr.length; + this.postion = objArr.length - 1; + } + + @Override + public void add(Object o) { + int limit = this.size + (this.size / 2); + Object[] newObjArr = new Object[limit]; + //public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)从指定源数组中复制一个数组, + // 复制从指定的位置开始,到目标数组的指定位置结束。从src引用的源数组到dest引用的目标数组, + // 数组组件的一个子序列被复制下来。被复制的组件的编号等于length参数。 + // 源数组中位置在srcPos到srcPos+length-1之间的组件被分别复制到目标数组中的destPos到destPos+length-1位置。 + System.arraycopy(this.objArr, 0, newObjArr, 0, objArr.length); + this.postion = this.size - 1; + newObjArr[this.postion] = o; + this.size = limit; + objArr = null; + this.objArr = newObjArr; + } + + @Override + public void add(int index, Object o) { + arrIndexVildate(index); + objArr[index - 1] = o; + size++; + } + + @Override + public Object get(int index) { + arrIndexVildate(index); + return objArr[index - 1]; + } + + @Override + public Object remove(int index) { + arrIndexVildate(index); + Object remoteObj = objArr[index - 1]; + objArr[index - 1] = null; + size--; + //TODO need GC ccontrol + return remoteObj; + } + + @Override + public int size() { + return this.size; + } + + @Override + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + private MyArraryList arraryList; + private int index; + + public ArrayListIterator(MyArraryList arraryList) { + this.arraryList = arraryList; + this.index = arraryList.size - 1; + } + + @Override + public boolean hasNext() { + if (index > arraryList.size) { + return true; + } else { + return false; + } + } + + @Override + public Object next() { + Object obj = arraryList.get(index); + index++; + return obj; + } + } + + private void arrIndexVildate(int index) { + if (index > size - 1 || index < 0) { + new Exception(String.format("cant than that array index %s,but got %", size - 1, index)); + } + } + + //test method + public static void main(String[] args) { + MyArraryList myArrary = new MyArraryList(); + MyArrest.arrestEq(10, myArrary.size()); + myArrary.add(1, 10); + MyArrest.arrestEq(10, myArrary.get(1)); + myArrary.add(100); + System.out.println(myArrary.get(11)); + myArrary.remove(1); + MyArrest.arrestIsNull(myArrary.get(1)); + if (myArrary.iterator().hasNext()) { + myArrary.iterator().next(); + } + System.out.println("test myArrary2"); + MyArraryList myArrary2 = new MyArraryList(20); + MyArrest.arrestEq(20, myArrary2.size()); + myArrary2.add(1, 10); + MyArrest.arrestEq(10, myArrary2.get(1)); + myArrary2.add(100); + MyArrest.arrestIsNull(myArrary2.get(20)); + myArrary2.remove(1); + MyArrest.arrestIsNull(myArrary2.get(1)); + if (myArrary.iterator().hasNext()) { + myArrary2.iterator().next(); + } + } +} diff --git a/group01/496740686/src/Impl/MyLinkedList.java b/group01/496740686/src/Impl/MyLinkedList.java new file mode 100644 index 0000000000..017bac5baf --- /dev/null +++ b/group01/496740686/src/Impl/MyLinkedList.java @@ -0,0 +1,177 @@ +package Impl; + +import Interface.Iterator; +import Interface.LinkedList; +import ex.MyArrest; + +/** + * Created by Administrator on 2017/2/26. + */ +public class MyLinkedList extends LinkedList { + private MyLinkedList.Node head; + private int size = 1; + + public MyLinkedList() { + + } + + private static class Node { + Object data; + MyLinkedList.Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public void add(Object o) { + if (this.size == 1) { + head.data = o; + return; + } + MyLinkedList.Node newHead = new MyLinkedList.Node(o, this.head); + this.size += 1; + this.head = newHead; + } + + + public void add(int index, Object o) { + IndexVildate(index); + int pos = 0; + if (index == 1) { + this.head = new Node(o, null); + return; + } + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + pos += 1; + if (pos == index - 1) { + node.data = o; + this.size += 1; + } + } + } + + + public Object get(int index) { + int pos = 0; + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + if (pos == index - 1) { + return node.data; + } + pos += 1; + } + return null; + } + + + public Object remove(int index) { + IndexVildate(index); + int pos = 0; + MyLinkedList.Node preNode; + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + pos += 1; + if (pos == index - 2) { + //record previous node + preNode = node; + if (pos == index - 1) { + MyLinkedList.Node willDelNode = node; + preNode.next = node.next; + node = null; + this.size -= 1; + return willDelNode; + } + } + } + return null; + } + + + public int size() { + return this.size; + } + + + public void addFirst(Object o) { + MyLinkedList.Node newHead = this.head; + newHead.data = o; + newHead.next = this.head; + this.size += 1; + this.head = newHead; + } + + + public void addLast(Object o) { + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + if (node.next == null) { + MyLinkedList.Node lastNode = new MyLinkedList.Node(o, null); + node.next = lastNode; + this.size += 1; + } + } + } + + + public Object removeFirst() { + MyLinkedList.Node oldHead = this.head; + this.head = oldHead.next; + this.size -= 1; + return oldHead; + } + + + public Object removeLast() { + for (MyLinkedList.Node node = this.head; node != null; node = node.next) { + if (node.next == null) { + MyLinkedList.Node willDelNode = node.next; + node.next = null; + this.size -= 1; + return willDelNode; + } + } + return null; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements Iterator { + private MyLinkedList linkedList; + private int index; + + public LinkedListIterator(MyLinkedList linkedList) { + this.linkedList = linkedList; + this.index = linkedList.size; + } + + @Override + public boolean hasNext() { + if (index > linkedList.size) { + return true; + } else { + return false; + } + } + + @Override + public Object next() { + Object obj = linkedList.get(index); + index++; + return obj; + } + } + + private void IndexVildate(int index) { + if (index > this.size || index < 0) { + System.out.println("happend error"); + } + } + + public static void main(String[] args) { + MyLinkedList linkedList = new MyLinkedList(); + linkedList.add(1, 23); + MyArrest.arrestEqByBasicType(1, linkedList.size()); + + } +} diff --git a/group01/496740686/src/Impl/MyQueue.java b/group01/496740686/src/Impl/MyQueue.java new file mode 100644 index 0000000000..1a029738d2 --- /dev/null +++ b/group01/496740686/src/Impl/MyQueue.java @@ -0,0 +1,68 @@ +package Impl; + +import Interface.Queue; + +/** + * Created by Administrator on 2017/2/26. + */ +public class MyQueue extends Queue { + + private Node first; // beginning of queue + private Node last; // end of queue + private int size; // number of elements on queue + + private static class Node { + private Object value; + private Node next; + + public Node(Object value, Node next) { + this.value = value; + this.next = next; + } + } + + public MyQueue() { + first = null; + last = null; + int n = 0; + } + + @Override + public void enQueue(Object o) { + Node oldlast = this.last; + this.last = new Node(o, null); + size += 1; + //第一个进队列 + if (isEmpty()) { + first = last; + } else { + oldlast.next = this.last; + } + + } + + @Override + public Object deQueue() { + if (isEmpty()) { + return null; + } else { + Node oldFirst = this.first; + Node newFirst = this.first.next; + this.first = null; + this.first = newFirst; + this.size -= 1; + return oldFirst; + + } + } + + @Override + public boolean isEmpty() { + return first == null; + } + + @Override + public int size() { + return size; + } +} diff --git a/group01/496740686/src/Impl/MyStack.java b/group01/496740686/src/Impl/MyStack.java new file mode 100644 index 0000000000..3a7c119e99 --- /dev/null +++ b/group01/496740686/src/Impl/MyStack.java @@ -0,0 +1,70 @@ +package Impl; + +import Interface.ArrayList; +import Interface.Stack; + + +/** + * Created by Administrator on 2017/2/26. + */ +public class MyStack extends Stack { + + private MyStack.Node first; // beginning of queue + private MyStack.Node last; // end of queue + private int size; // number of elements on queue + + private static class Node { + private Object value; + private MyStack.Node next; + + public Node(Object value, MyStack.Node next) { + this.value = value; + this.next = next; + } + } + + public MyStack() { + first = null; + last = null; + int n = 0; + } + + @Override + public void push(Object o) { + if (isEmpty()) { + MyStack.Node oldFirst = this.first; + this.first = new MyStack.Node(o, null); + size += 1; + //第一个进栈 + if (isEmpty()) { + first = last; + } else { + oldFirst.next = this.last; + } + } + } + + @Override + public Object pop() { + if (isEmpty()) { + return null; + } else { + MyStack.Node oldFirst = this.first; + this.first = oldFirst.next; + this.size -= 1; + return oldFirst; + + } + + } + + @Override + public Object peek() { + return this.first; + } + + @Override + public boolean isEmpty() { + return first == null; + } +} diff --git a/group01/496740686/src/Interface/ArrayList.java b/group01/496740686/src/Interface/ArrayList.java new file mode 100644 index 0000000000..567c1996b1 --- /dev/null +++ b/group01/496740686/src/Interface/ArrayList.java @@ -0,0 +1,34 @@ +package Interface; + + + +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/group01/496740686/src/Interface/BinaryTreeNode.java b/group01/496740686/src/Interface/BinaryTreeNode.java new file mode 100644 index 0000000000..c5480a614f --- /dev/null +++ b/group01/496740686/src/Interface/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package Interface; + +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/group01/496740686/src/Interface/Iterator.java b/group01/496740686/src/Interface/Iterator.java new file mode 100644 index 0000000000..77e3c0b216 --- /dev/null +++ b/group01/496740686/src/Interface/Iterator.java @@ -0,0 +1,7 @@ +package Interface; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/496740686/src/Interface/LinkedList.java b/group01/496740686/src/Interface/LinkedList.java new file mode 100644 index 0000000000..b7166a1731 --- /dev/null +++ b/group01/496740686/src/Interface/LinkedList.java @@ -0,0 +1,47 @@ +package Interface; + +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/group01/496740686/src/Interface/List.java b/group01/496740686/src/Interface/List.java new file mode 100644 index 0000000000..98d6a4da0a --- /dev/null +++ b/group01/496740686/src/Interface/List.java @@ -0,0 +1,9 @@ +package Interface; + +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/group01/496740686/src/Interface/Queue.java b/group01/496740686/src/Interface/Queue.java new file mode 100644 index 0000000000..8e3a5d5f43 --- /dev/null +++ b/group01/496740686/src/Interface/Queue.java @@ -0,0 +1,19 @@ +package Interface; + +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/group20/592146505/data _structure/src/cn/wsc/utils/Stack.java b/group01/496740686/src/Interface/Stack.java similarity index 92% rename from group20/592146505/data _structure/src/cn/wsc/utils/Stack.java rename to group01/496740686/src/Interface/Stack.java index 3f156461e8..7e536e250a 100644 --- a/group20/592146505/data _structure/src/cn/wsc/utils/Stack.java +++ b/group01/496740686/src/Interface/Stack.java @@ -1,4 +1,4 @@ -package cn.wsc.utils; +package Interface; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group01/496740686/src/ex/MyArrest.java b/group01/496740686/src/ex/MyArrest.java new file mode 100644 index 0000000000..9987b83535 --- /dev/null +++ b/group01/496740686/src/ex/MyArrest.java @@ -0,0 +1,75 @@ +package ex; + +/** + * Created by Administrator on 2017/2/26. + */ +public class MyArrest { + + + public static void arrestEq(T expect, T value) { + if (expect == null || value == null) { + if (expect == value) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + if (expect.equals(value)) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + + public static void arrestEq(T expect, T value, String errorInfo) { + if (expect == null || value == null) { + if (expect == value) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + if (expect.equals(value)) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + + public static void arrestEqByBasicType(T expect, T value) { + if (expect == null || value == null) { + if (expect == value) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + if (expect == value) { + System.out.println("it's ok \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } + + public static void arrestIsNull(Object obj) { + if (obj == null) { + System.out.println("it's null , you're right \n" ); + return; + } else { + System.out.println("happend error \n" ); + return; + } + } +} diff --git a/group01/751425278/.classpath b/group01/751425278/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group01/751425278/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group01/751425278/.gitignore b/group01/751425278/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/751425278/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/751425278/.project b/group01/751425278/.project new file mode 100644 index 0000000000..85d6b2c816 --- /dev/null +++ b/group01/751425278/.project @@ -0,0 +1,17 @@ + + + basicDataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/751425278/src/com/sanmubird/basicDataStructure/ArrayList.java b/group01/751425278/src/com/sanmubird/basicDataStructure/ArrayList.java new file mode 100644 index 0000000000..3e6c874d90 --- /dev/null +++ b/group01/751425278/src/com/sanmubird/basicDataStructure/ArrayList.java @@ -0,0 +1,156 @@ +package com.sanmubird.basicDataStructure; + +import java.util.Arrays; + +public class ArrayList implements List { + + /** ArrayList 是一个类,一个类就会有对象,属性,构造方法,方法 + * ArrayList 是基于数组来实现List接口; 那么它的元素就会有 存储在数组中的元素, 和ArrayList的长度 + * 这个地方需要区分size= ArrayList.size() 和 length = Array.length ; + * size 是 已经占用的长度; + * length 是数组的长度; length 》= size 当,size > length 时,数组要动态扩容; + * */ + +// 数组默认的长度 + private static final int DEFAULT_SIZE = 10; + +// ArrayList的大小 + private int size ; + +// 数组中存储的元素 + private Object[] elementData = null; + + private int count ; + +// ArrayList 的构造方法 通过构造方法 可以得到这个类的对象 +// 有参构造方法 + public ArrayList(int i){ + if(i <= 0 ){ + throw new RuntimeException("数组的长度不能小于等于0"); + }else{ + this.elementData = new Object[i]; + this.size = 0 ; // 集合ArrayList的大小; + } + } + // 无参构造方法 + public ArrayList(){ + this(DEFAULT_SIZE); // this 会调用本类中 相同参数(相同的参数个数和参数类型)的构造方法; + } + + /** ArrayList 其他方法分析 + * 目标方法: + * size(); Array的length就是ArrayList的大小 + * get(int index); Array的【index-1】就是 ArrayList的第index位元素 + * add(Object o) ; 这个方法是在数组的末尾顺序添加一个元素; 找到数组的长度size,将array【size-1】= Object + * add(int index , Object o); 这个方法是在数组的指定位置添加一个元素;找到index位,将index位起往后挪一位,并将array【index】=Object + * remove(int index); 这个方法是 删除指定位上的元素,直接将这个位至最后的元素往前挪移一位。 + * + * 工具方法: + * argumentCheck(int index); 判断输入的参数是否合法;比如传入的参数不能比数组长度大,或者不能为负数等 + * ensureCapacity(); 判断当前数组的长度是否足够大,能再容纳下一个元素。 + * + * */ + + +// 对传入的参数进行验证是否合法 如果输入的参数不合法 就抛出异常 + public void argumentCheck(int index){ + if(index >= size || index < 0 ){ // 此处我觉得需要 ‘=’ 因为 index 我觉得是下标 + throw new IndexOutOfBoundsException("插入的下标是:"+index +",但ArrayLsit的长度是:"+size); + } + } + + // 判断是否数组是否溢出的方法 如果数组现在的长度小于所需的最小长度,就需要对数组进行扩容 + public void ensureCapacity(int minCapacity){ + int length = elementData.length; // 得出当前数组的长度 + if(minCapacity > length){ + int newCapacity = length * 3 / 2 + 1 ; //你是否对此有疑问?得出的结果会不会是小数? 答案是不会的,java中算术运算符“/”;两个int类型相除,结果一定是int类型 + if(minCapacity > newCapacity ){ + newCapacity = minCapacity ; + } + count++; + System.out.println("扩容"+count+"次"); + elementData = Arrays.copyOf(elementData, newCapacity);//此处为什么用Arrays.copyOf()而不用System.arraycopy()? + // Arrays.copyOf(): 不仅仅copy数组中的元素,还会创建一个新的数组来存放copy的对象 + // System.arraycopy():仅仅是copy数组中的元素,不会新建一个数组对象,也不会改变原有的数组长度。 + // 在原有数组长度不够的情况下,只能选择新建一个数组,并将原有的数组复制到新数组的办法来解决。 + } + } + + // 得到ArrayList 大小的方法 ; 此处的size 不是Array的length,而是ArrayList中元素的个数 + public int size(){ + return size; + } + +// 传入下标得到元素 + public Object get(int index){ + argumentCheck(index); //需要判断传入的参数是否合法; + return elementData[index]; + } + +// 按顺序在数字尾部添加元素 + public void add(Object o){ + ensureCapacity(size+1); // 判断是否会溢出 + elementData[size++] = o ; //此处使用 size++ 的好处:elementData[size+1];size++; + } + + public void add(int index, Object o){ //这个地方需要搞清楚index的含义:index在此处是下标的意思 + argumentCheck(index); //判断输入的下标是否合法 ---> + // 刚开始的时候 ; 我觉得这个地方不需要加这个判断,因为ArrayList是动态增长的; + // 我还需要想明白这个问题; + ensureCapacity(size+1); // 判断是否会溢出 + int moveLength = size - (index + 1) + 1; // 此处index是下标;下标是从0开始计算的;所以第n位的下标就是(n-1);所以,n = index + 1 + // 此处的 +1 刚开始没想明白,后来组长给举了个例子,1-3 有三个数,但不是通过3-1=2 算出来的 + System.arraycopy(elementData, index, elementData, index+1, moveLength ); + elementData[index] = o ; + size++; + } + + public Object remove(int index){ + argumentCheck(index); //判断输入的下标是否合法 + Object o = elementData[index]; + System.arraycopy(elementData, index, elementData, index-1, size-index); + elementData[size] = null ; + size--; + return o; + } + + public Iterator iterator(){ + return new Iterator(){ + private int index = 0 ; + + @Override + public Object next(){ + return elementData[index++]; + } + @Override + public boolean hasNext() { + return index < size ; + } + }; + } + + public static void main(String [] args){ + ArrayList al = new ArrayList(); + al.add(1); + al.add(2); + al.add(4); + al.add(5); + al.add(6); + al.add(7); + al.add(2,3); + al.add(8); + al.add(9); + al.add(10); + al.add(11); + al.add(13); + al.add(9,12); + al.add(14); + al.add(15); + al.remove(9); + for(int i = 0 ; i < al.size() ; i++ ){ + System.out.println(al.get(i)); + } + System.out.println("al的size是"+al.size()); + System.out.println(al.get(15)); + } +} \ No newline at end of file diff --git a/group01/751425278/src/com/sanmubird/basicDataStructure/BinaryTreeNode.java b/group01/751425278/src/com/sanmubird/basicDataStructure/BinaryTreeNode.java new file mode 100644 index 0000000000..4096c79e36 --- /dev/null +++ b/group01/751425278/src/com/sanmubird/basicDataStructure/BinaryTreeNode.java @@ -0,0 +1,58 @@ +package com.sanmubird.basicDataStructure; + +public class BinaryTreeNode { + /** 二叉树同时具有数组和链表各自的特点:它可以像数组一样迅速查找;也可以像链表一样快速添加; + * 但 删除操作复杂; + * 二叉树是每个节点最多有两个子树的有序树; + * 一个节点的左子点的关键值必须小于此节点,右节点的关键值必须大于或者等于此节点, + * */ + + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Integer i){ + this.data = i ; + } + + + public Object getData() { + return data; + } + public void setData(Integer i) { + this.data = i; + } + 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(Integer i){ + BinaryTreeNode node = new BinaryTreeNode(i); + if(i > this.data){ + if(this.getRight() == null ){ + this.setRight(node); + return node; + }else{ + return this.getRight().insert(i); + } + }else{ + if(this.getLeft() == null ){ + this.setLeft(node); + return node ; + }else{ + return this.getLeft().insert(i); + } + } + } + +} \ No newline at end of file diff --git a/group01/751425278/src/com/sanmubird/basicDataStructure/Iterator.java b/group01/751425278/src/com/sanmubird/basicDataStructure/Iterator.java new file mode 100644 index 0000000000..df4a1e3a6d --- /dev/null +++ b/group01/751425278/src/com/sanmubird/basicDataStructure/Iterator.java @@ -0,0 +1,6 @@ +package com.sanmubird.basicDataStructure; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} \ No newline at end of file diff --git a/group01/751425278/src/com/sanmubird/basicDataStructure/LinkedList.java b/group01/751425278/src/com/sanmubird/basicDataStructure/LinkedList.java new file mode 100644 index 0000000000..31703dd5f1 --- /dev/null +++ b/group01/751425278/src/com/sanmubird/basicDataStructure/LinkedList.java @@ -0,0 +1,159 @@ +package com.sanmubird.basicDataStructure; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + /** 链表:是由节点(Node)组成的, + * 而向外暴露的只有一个头节点;我们对链表的所有操作,都是直接或简洁地通过头节点来实现的。 + * 节点(Node)是由 一个 存储的对象和一个对下个节点的引用组成的。 + * Node中最重要的就是引用了,如果说对ArrayList的增删是ArrayCopy的话,那对LinkedList的增删就是改变引用的指向。 + * 因为节点的添加顺序是自右向左的,且最左边的节点是头节点; + * 所以,新添加的节点会在左边,而是新添加的节点会成为新的t头节点。 + * 头节点可以通过对下一个节点的引用,来找到所有的节点; + * 所以:链表里面的属性就有两个,一个是头节点,一个是节点的数量。 + ***/ + private Node head; //定义一个头节点 + private int size ; //节点的数量 + + public LinkedList(){ + this.head = null ; + this.size = 0 ; + } + +// 检查输入的参数是否合法 + public void argumentCheckForAdd(int index){ + if(index > size || index < 0 ){ //这个地反需要验证 index = 0 的情况 + throw new IndexOutOfBoundsException("输入的参数超出了边界!"); + } + } + +// 检查输入的参数是否合法 + public void argumentCheckForOther(int index){ + if(index >= size || index < 0 ){ + throw new IndexOutOfBoundsException("输入的参数超出了边界!"); + } + } + + + public Node getHead(){ + return head ; + } + + public Object get(int index){ + argumentCheckForOther(index); + Node node = head ; + for(int i = 0 ; i < index ; i++){ + node = head.next ; + } + return node.data; + } + public int size(){ + return size; // return this.size 跟 return size ;有区别没有? + } + +// + public void add(Object o){ + Node newNode = new Node(o); //实例化一个要添加的节点 + if(head == null ){ + head = newNode ; + }else{ + Node temp = head ; + while (temp.next != null ){ //这个地方为什么是 temp.next != null ? + // 这个地方的作用就是:取出下一个节点;那么当然是在存在下个节点的情况下,才能取出下个节点 + temp = temp.next ; + // 这样就找到了最后一个节点: temp + } + temp.next = newNode ; + } + size++; + } + +// 这个通过画图,然后看图说话就很容易弄出来。 + public void add(int index , Object o){ + argumentCheckForAdd(index); + if(size == index ) + add(o); + else{ + Node preNode = head ; + for (int i = 0 ; i < index ; i++){ + preNode = preNode.next; + } + Node nextNode = preNode.next ; + Node node = new Node(o); + preNode.next = node; + node.next = nextNode; + size++; + } + } + + public Object remove(int index){ + argumentCheckForOther(index); + Node temp = head ; + for(int i = 0 ; i < index ; i++){ + temp = temp.next ; + } + Node removedNode = temp.next ; + Node nextNode = removedNode.next ; + temp.next = nextNode ; + size--; + return removedNode.data; + } + + + public void addFirst(Object o){ + Node node = new Node(o); + node.next = head ; + head = node ; + size++; + } + + public void addLast(Object o){ + Node lastNode = head ; + while(lastNode.next != null ){ + lastNode = lastNode.next ; + } + Node node = new Node(o); + lastNode.next = node ; + size++; + } + + public void noSuchElement(){ + if(head.next == null){ + throw new NoSuchElementException("没有这个元素"); + } + } + + + public Object removeFirst(){ + noSuchElement(); + Node node = head.next ; + head.next = node.next ; + size--; + return node.data; + } + + public Object removeLast(){ + noSuchElement(); + Node temp = head ; + for(int i = 0 ; i 0){ + this.queArray = new Object[initialSize]; + this.maxSize = initialSize; + this.front = this.rear = 0 ; + }else{ + throw new RuntimeException("初始化的大小不能小于0;"+ initialSize); + } +} + + public void enQueue(Object o){ + if(rear == maxSize){ + throw new RuntimeException("队列已满,无法插入新的元素!"); + }else{ + queArray[rear++] = o ; + } + } + + public Object deQueue(){ + if(isEmpty()){ + throw new RuntimeException("空队列异常!"); + }else{ + Object obj = (Object) queArray[front]; + queArray[front++] = null; + return obj; + } + } + + //判空 + public boolean isEmpty(){ + return rear == front?true:false; + } + + public int size(){ + return rear-front; + } + */ + + + /** 采用链表实现对队列;队列的特点是:先进先出,而且不能插队;所以只能从尾进,从头出。 + * + * */ + private LinkedList ll ; + + public Queue(){ + this.ll = new LinkedList(); + } + + public void enQueue(Object o){ + ll.addLast(o); + } + + public Object deQueue(){ + return ll.removeLast(); + } + + public boolean isEmpty(){ + return ll.getHead().next == null ? true: false; + } + + public int size(){ + return ll.size(); + } +} \ No newline at end of file diff --git a/group01/751425278/src/com/sanmubird/basicDataStructure/Stack.java b/group01/751425278/src/com/sanmubird/basicDataStructure/Stack.java new file mode 100644 index 0000000000..7c6b03b3df --- /dev/null +++ b/group01/751425278/src/com/sanmubird/basicDataStructure/Stack.java @@ -0,0 +1,50 @@ +package com.sanmubird.basicDataStructure; + +public class Stack { + + private Object[] arrayStack ;// + private int maxSize ; //栈容量 + private int top ; //栈指针; + + public Stack(int initialSize){ + if(initialSize >= 0 ){ + this.arrayStack = new Object[initialSize]; + this.maxSize = initialSize ; + this.top = -1 ; + }else{ + throw new RuntimeException("初始化的大小不能小于0"+initialSize); + } + } + + // 进栈,进栈的第一个元素的top = 0 ; + public void push(Object o){ + if(top == maxSize -1 ){ + throw new RuntimeException("栈已满,无法将元素插入栈"); + }else{ + arrayStack[++top] = o ; + } + } + + public Object pop(){ + if(top == -1){ + throw new RuntimeException("栈为空!"); + }else{ + return arrayStack[top--]; + } + } + +// 查看栈顶元素,但是不移除 + public Object peek(){ + if(top == -1){ + throw new RuntimeException("栈为空!"); + }else{ + return arrayStack[top]; + } + } + public boolean isEmpty(){ + return top == -1 ? true : false; + } + public int size(){ + return arrayStack.length; + } +} \ No newline at end of file diff --git a/group01/765324639/src/zavier/week01/basic/ArrayList.java b/group01/765324639/src/zavier/week01/basic/ArrayList.java new file mode 100644 index 0000000000..38e5739fb8 --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/ArrayList.java @@ -0,0 +1,85 @@ +package zavier.week01.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + @Override + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size++] = o; + } + + private void ensureCapacity(int size) { + if (size > elementData.length) { + grow(); + } + } + + private void grow() { + elementData = Arrays.copyOf(elementData, size * 2); + } + + @Override + public void add(int index, Object o) { + rangeCheckForAdd(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + private void rangeCheckForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + private void rangeCheck(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public Object remove(int index) { + rangeCheck(index); + Object dest = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return dest; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator() { + return new Iterator() { + + private int index = 0; + + @Override + public Object next() { + return elementData[index++]; + } + + @Override + public boolean hasNext() { + return index < size; + } + }; + } + +} diff --git a/group01/765324639/src/zavier/week01/basic/BinaryTreeNode.java b/group01/765324639/src/zavier/week01/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6ef26e8f9a --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/BinaryTreeNode.java @@ -0,0 +1,63 @@ +package zavier.week01.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Integer data) { + this.data = data; + } + + public Integer getData() { + return data; + } + + public void setData(Integer 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(Integer o) { + + if (o > this.data) { + + if (this.getRight() == null) { + BinaryTreeNode node = new BinaryTreeNode(o); + this.setRight(node); + return node; + } else { + return this.getRight().insert(o); + } + + } else { + + if (this.getLeft() == null) { + BinaryTreeNode node = new BinaryTreeNode(o); + this.setLeft(node); + return node; + } else { + return this.getLeft().insert(o); + } + + } + + } + +} diff --git a/group01/765324639/src/zavier/week01/basic/Iterator.java b/group01/765324639/src/zavier/week01/basic/Iterator.java new file mode 100644 index 0000000000..664983e0fd --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/Iterator.java @@ -0,0 +1,8 @@ +package zavier.week01.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} diff --git a/group01/765324639/src/zavier/week01/basic/LinkedList.java b/group01/765324639/src/zavier/week01/basic/LinkedList.java new file mode 100644 index 0000000000..c876b48f1b --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/LinkedList.java @@ -0,0 +1,148 @@ +package zavier.week01.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + @Override + public void add(Object o) { + if (head == null) { + head = new Node(o); + } else { + Node tail = head; + while (tail.next != null) { + tail = tail.next; + } + Node node = new Node(o); + + tail.next = node; + } + size++; + } + + @Override + public void add(int index, Object o) { + rangeCheckForAdd(index); + if (index == 0) { + Node node = new Node(o); + node.next = head; + head = node; + } else { + Node preDest = head; + for (int i = 0; i < index - 1; i++) { + preDest = preDest.next; + } + Node node = new Node(o); + node.next = preDest.next; + preDest.next = node; + } + + size++; + } + + private void rangeCheckForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public Object get(int index) { + rangeCheck(index); + + Node dest = head; + for (int i = 0; i < index; i++) { + dest = dest.next; + } + return dest.data; + } + + private void rangeCheck(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public Object remove(int index) { + rangeCheck(index); + + Node preDest = head; + for (int i = 0; i < index - 1; i++) { + preDest = preDest.next; + } + Node dest = preDest.next; + preDest.next = dest.next; + + size--; + return dest.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + + public void addLast(Object o) { + Node lastNode = head; + while (lastNode.next != null) { + lastNode = lastNode.next; + } + + Node node = new Node(o); + lastNode.next = node; + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + Node target = head; + head = head.next; + size--; + return target.data; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + + Node preDest = head; + while (preDest.next.next != null) { + preDest = preDest.next; + } + Node dest = preDest.next; + preDest.next = null; + + size--; + return dest.data; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + + Node(Object data) { + this.data = data; + next = null; + } + } +} diff --git a/group01/765324639/src/zavier/week01/basic/List.java b/group01/765324639/src/zavier/week01/basic/List.java new file mode 100644 index 0000000000..4f2d49bd73 --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/List.java @@ -0,0 +1,13 @@ +package zavier.week01.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/group01/765324639/src/zavier/week01/basic/Queue.java b/group01/765324639/src/zavier/week01/basic/Queue.java new file mode 100644 index 0000000000..5a212d46c1 --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/Queue.java @@ -0,0 +1,25 @@ +package zavier.week01.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) { + list.add(o); + } + + public Object deQueue() { + if (list.size() == 0) { + return null; + } + return list.removeFirst(); + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group01/765324639/src/zavier/week01/basic/Stack.java b/group01/765324639/src/zavier/week01/basic/Stack.java new file mode 100644 index 0000000000..ebe4afb19f --- /dev/null +++ b/group01/765324639/src/zavier/week01/basic/Stack.java @@ -0,0 +1,33 @@ +package zavier.week01.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek() { + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/765324639/src/zavier/week01/test/AllTests.java b/group01/765324639/src/zavier/week01/test/AllTests.java new file mode 100644 index 0000000000..c1755f6803 --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/AllTests.java @@ -0,0 +1,12 @@ +package zavier.week01.test; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ArrayListTest.class, LinkedListTest.class, QueueTest.class, StackTest.class, + BinaryTreeNodeTest.class}) +public class AllTests { + +} diff --git a/group01/765324639/src/zavier/week01/test/ArrayListTest.java b/group01/765324639/src/zavier/week01/test/ArrayListTest.java new file mode 100644 index 0000000000..6a475500df --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/ArrayListTest.java @@ -0,0 +1,91 @@ +package zavier.week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week01.basic.ArrayList; +import zavier.week01.basic.Iterator; + +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void setUp() { + for (int i = 0; i < 500; i++) { + arrayList.add(i); + } + } + + @Test + public void testAddObject() { + for (int i = 0; i < 500; i++) { + arrayList.add(i); + } + } + + @Test + public void testAddIntObject() { + arrayList.add(100, -100); + Assert.assertEquals(-100, arrayList.get(100)); + Assert.assertEquals(100, arrayList.get(101)); + Assert.assertEquals(501, arrayList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddIllegalIntObject() { + arrayList.add(1000, 5); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddNegativeIntObject() { + arrayList.add(-1, 5); + } + + @Test + public void testGet() { + for (int i = 0; i < 500; i++) { + Assert.assertEquals(i, ((Integer) arrayList.get(i)).intValue()); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testIllegalGet() { + arrayList.get(500); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testNegativeGet() { + arrayList.get(-10); + } + + @Test + public void testRemove() { + Assert.assertEquals(100, arrayList.remove(100)); + Assert.assertEquals(101, arrayList.get(100)); + Assert.assertEquals(499, arrayList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testIllegalRemove() { + arrayList.remove(500); + } + + @Test + public void testSize() { + Assert.assertEquals(500, arrayList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + int i = 0; + while (iterator.hasNext()) { + Assert.assertEquals(i, iterator.next()); + i++; + } + Assert.assertEquals(500, i); + } + +} diff --git a/group01/765324639/src/zavier/week01/test/BinaryTreeNodeTest.java b/group01/765324639/src/zavier/week01/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..30a096a350 --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/BinaryTreeNodeTest.java @@ -0,0 +1,33 @@ +package zavier.week01.test; + +import org.junit.Assert; +import org.junit.Test; + +import zavier.week01.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode root = new BinaryTreeNode(5); + + @Test + public void testInsert() { + root.insert(2); + root.insert(7); + root.insert(1); + root.insert(6); + + Assert.assertEquals((Integer) 5, root.getData()); + Assert.assertEquals((Integer) 2, root.getLeft().getData()); + Assert.assertEquals((Integer) 1, root.getLeft().getLeft().getData()); + Assert.assertEquals(null, root.getLeft().getRight()); + Assert.assertEquals((Integer) 7, root.getRight().getData()); + Assert.assertEquals((Integer) 6, root.getRight().getLeft().getData()); + Assert.assertEquals(null, root.getRight().getRight()); + + root.insert(4); + root.insert(8); + Assert.assertEquals((Integer) 4, root.getLeft().getRight().getData()); + Assert.assertEquals((Integer) 8, root.getRight().getRight().getData()); + } + +} diff --git a/group01/765324639/src/zavier/week01/test/LinkedListTest.java b/group01/765324639/src/zavier/week01/test/LinkedListTest.java new file mode 100644 index 0000000000..de7436a350 --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/LinkedListTest.java @@ -0,0 +1,109 @@ +package zavier.week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week01.basic.LinkedList; + + +public class LinkedListTest { + + private LinkedList linkedList = new LinkedList(); + + @Before + public void setUp() { + for (int i = 0; i < 500; i++) { + linkedList.add(i); + } + } + + @Test + public void testAddObject() { + for (int i = 0; i < 100; i++) { + linkedList.add(i); + } + Assert.assertEquals(600, linkedList.size()); + } + + @Test + public void testAddIntObject() { + linkedList.add(100, -100); + Assert.assertEquals(-100, linkedList.get(100)); + Assert.assertEquals(100, linkedList.get(101)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddIllegalIntObject() { + linkedList.add(1000, 10); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddNegativeIntObject() { + linkedList.add(-10, 10); + } + + @Test + public void testGet() { + for (int i = 0; i < 500; i++) { + Assert.assertEquals(i, ((Integer) linkedList.get(i)).intValue()); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testIllegalGet() { + linkedList.get(500); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testNegativeGet() { + linkedList.get(-10); + } + + @Test + public void testRemove() { + Assert.assertEquals(100, linkedList.remove(100)); + Assert.assertEquals(101, linkedList.get(100)); + Assert.assertEquals(499, linkedList.size()); + } + + @Test + public void testSize() { + Assert.assertEquals(500, linkedList.size()); + linkedList.add(10); + Assert.assertEquals(501, linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(-10); + Assert.assertEquals(-10, linkedList.get(0)); + linkedList.addFirst(-100); + Assert.assertEquals(-100, linkedList.get(0)); + Assert.assertEquals(-10, linkedList.get(1)); + } + + @Test + public void testAddLast() { + linkedList.addLast(-9); + Assert.assertEquals(-9, linkedList.get(linkedList.size() - 1)); + linkedList.addLast(-8); + Assert.assertEquals(-8, linkedList.get(linkedList.size() - 1)); + Assert.assertEquals(-9, linkedList.get(linkedList.size() - 2)); + } + + @Test + public void testRemoveFirst() { + Assert.assertEquals(0, linkedList.removeFirst()); + Assert.assertEquals(1, linkedList.removeFirst()); + Assert.assertEquals(498, linkedList.size()); + } + + @Test + public void testRemoveLast() { + Assert.assertEquals(499, linkedList.removeLast()); + Assert.assertEquals(498, linkedList.removeLast()); + Assert.assertEquals(498, linkedList.size()); + } + +} diff --git a/group01/765324639/src/zavier/week01/test/QueueTest.java b/group01/765324639/src/zavier/week01/test/QueueTest.java new file mode 100644 index 0000000000..99d6466c8a --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/QueueTest.java @@ -0,0 +1,49 @@ +package zavier.week01.test; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week01.basic.Queue; + +public class QueueTest { + + private Queue queue = new Queue(); + + @Before + public void setUp() { + for (int i = 0; i < 500; i++) { + queue.enQueue(i); + } + } + + @Test + public void testEnQueue() { + for (int i = 0; i < 100; i++) { + queue.enQueue(i); + } + Assert.assertEquals(600, queue.size()); + } + + @Test + public void testDeQueue() { + for (int i = 0; i < 500; i++) { + Assert.assertEquals(i, queue.deQueue()); + } + Assert.assertNull(queue.deQueue()); + Assert.assertNull(queue.deQueue()); + } + + @Test + public void testIsEmpty() { + Assert.assertFalse(queue.isEmpty()); + Queue q = new Queue(); + Assert.assertTrue(q.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(500, queue.size()); + } + +} diff --git a/group01/765324639/src/zavier/week01/test/StackTest.java b/group01/765324639/src/zavier/week01/test/StackTest.java new file mode 100644 index 0000000000..8138f97d3d --- /dev/null +++ b/group01/765324639/src/zavier/week01/test/StackTest.java @@ -0,0 +1,60 @@ +package zavier.week01.test; + +import java.util.EmptyStackException; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import zavier.week01.basic.Stack; + + +public class StackTest { + + private Stack stack = new Stack(); + + @Before + public void setUp() { + for (int i = 0; i < 500; i++) { + stack.push(i); + } + } + + @Test + public void testPush() { + for (int i = 0; i < 100; i++) { + stack.push(i); + } + Assert.assertEquals(600, stack.size()); + } + + @Test(expected = EmptyStackException.class) + public void testPop() { + for (int i = 0; i < 500; i++) { + Assert.assertEquals(499 - i, stack.pop()); + } + stack.pop(); + } + + @Test + public void testPeek() { + Assert.assertEquals(499, stack.peek()); + Assert.assertEquals(499, stack.peek()); + stack.pop(); + Assert.assertEquals(498, stack.peek()); + } + + @Test + public void testIsEmpty() { + Assert.assertFalse(stack.isEmpty()); + Assert.assertTrue(new Stack().isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(500, stack.size()); + stack.pop(); + Assert.assertEquals(499, stack.size()); + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/BinaryTree.java b/group01/819048836/lvxg2017/src/basic/BinaryTree.java new file mode 100644 index 0000000000..f966389ea5 --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/BinaryTree.java @@ -0,0 +1,41 @@ +package basic; + +public class BinaryTree { + private BinaryTreeNode root;//根节点 + + //插入操作 + public void insert(int value){ + BinaryTreeNode newNode = new BinaryTreeNode(value); + if(root==null){ + root = newNode; + root.setLeft(null); + root.setRight(null); + }else{ + BinaryTreeNode currentNode = root; + BinaryTreeNode parentNode; + while(true) + { + parentNode = currentNode; + //往右放 + if(newNode.getData()>currentNode.getData()){ + currentNode = currentNode.getRight(); + if(currentNode ==null){ + parentNode.setRight(newNode); + return; + } + }else if(newNode.getData()<=currentNode.getData()){ + currentNode = currentNode.getLeft(); + if(currentNode ==null){ + parentNode.setLeft(newNode); + return; + } + } + + } + + } + + + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/BinaryTreeNode.java b/group01/819048836/lvxg2017/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..7f984b6131 --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/BinaryTreeNode.java @@ -0,0 +1,34 @@ +package basic; + +//������ +public class BinaryTreeNode { + private int data;//节点值 + private BinaryTreeNode left; //左子节点 + private BinaryTreeNode right;//右子节点 + public BinaryTreeNode(int data){ + this.data =data; + this.left = null; + this.right = null; + } + public void setDate(int data){ + this.data =data; + } + public int getData(){ + return data; + } + public BinaryTreeNode getLeft(){ + return left; + } + public BinaryTreeNode getRight(){ + return right; + } + public void setLeft(BinaryTreeNode left){ + this.left = left; + } + public void setRight(BinaryTreeNode right){ + this.right =right; + } + + + +} diff --git a/group01/819048836/lvxg2017/src/basic/MyArrayList.java b/group01/819048836/lvxg2017/src/basic/MyArrayList.java new file mode 100644 index 0000000000..064acf941e --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/MyArrayList.java @@ -0,0 +1,88 @@ +package basic; +/** + * + * + * @author lvxg + * + */ +public class MyArrayList { + private Object[] element; + private int size; + private static final Object[] EMPTY = new Object[10]; + + public MyArrayList() { + this.element = EMPTY; + } + + public boolean add(Object o) { + if (size < element.length) { + element[size] = o; + size++; + } else { + //数组扩容 + grow(); + element[size] = o; + size++; + } + return true; + } + + //根据索引添加 + public boolean add(int index, Object o) { + rangeCheckForAdd(index); + if (size < element.length + 1) { + Object[] e = new Object[element.length+1]; + System.arraycopy(element, 0, e, 0, index); + e[index] = o; + System.arraycopy(element, index, e, index + 1, element.length-index); + element = e; + size++; + } + return true; + } + + public Object get(int index) { + rangeCheck(index); + return element[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object oldValue = element[index]; + int numMoved = size - index-1; + if(numMoved>0){ + System.arraycopy(element, index+1, element, index, numMoved); + } + element[--size] =null; + return oldValue; + } + public int size() { + return size; + } + private void rangeCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(); + } + private void rangeCheckForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + //数组扩容方法 + private void grow() { + Object[] e = new Object[size * 2]; + System.arraycopy(element, 0, e, 0, element.length); + element = e; + + } + + public static void main(String[] args) { + MyArrayList m = new MyArrayList(); + for (int i = 0; i < 10; i++) { + m.add(i); + } + m.add(2, "123"); + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/MyLinkedList.java b/group01/819048836/lvxg2017/src/basic/MyLinkedList.java new file mode 100644 index 0000000000..c79c0e1b81 --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/MyLinkedList.java @@ -0,0 +1,168 @@ +package basic; + + + +/** + * 链表 + * + * @author lvxg + * + */ +public class MyLinkedList { + // 头节点 + private Node first; + // 尾节点 + private Node last; + // 链表长度 + private int size = 0; + + public boolean add(Object o) { + linklast(o); + return true; + } + /** + * 根据索引添加节点 + * @param index + * @param o + */ + public void add(int index, Object o) { + checkPositionIndex(index); + if (index == size) { + linklast(o); + } else { + final Node succ = node(index); + final Node pred = succ.prev; + Node newNode = new Node(o, pred, succ); + if (pred == null) { + first = newNode; + } else { + pred.next = newNode; + } + size++; + } + } + //根据索引删除节点 + private Object remove(int index){ + Object x= unllink(node(index)); + return x; + } + private Object remove(){ + Object x = unllink(node(size)); + return x; + } + @SuppressWarnings("unused") + private Object get(int index) + { + Node f = first; + for (int i = 0; i < index; i++) { + f = f.next; + } + return f.data; + } + @SuppressWarnings("unused") + private int size(){ + return size; + } + @SuppressWarnings("unused") + private void addFirst(Object o){ + Node f= first; + Node newNode = new Node(o, f, null); + f.prev = newNode; + first = newNode; + } + @SuppressWarnings("unused") + private void addLast(Object o){ + Node l = last; + Node newNode = new Node(o, null, l); + l.next = newNode; + last = newNode; + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + + private void linklast(Object e) { + final Node l = last; + final Node newNode = new Node(e, null, l); + last = newNode; + if (l == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + + } + + private Object unllink(Node x) { + final Object element = x.data; + final Node next = x.next; + final Node prev = x.prev; + if (prev == null) { + first = next; + } else { + prev.next = next; + x.next = null; + x.prev = null; + } + if (next == null) { + last = prev; + x.next = null; + } + size--; + x.data = null; + return element; + } + + private Node node(int index) { + if (index < (size >> 1)) { + Node x = first; + 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 static class Node { + Object data; // 节点值 + Node next;// 后继节点 + Node prev;// 前驱节点 + + public Node(Object o, Node n, Node p) { + this.data = o; + this.prev = p; + this.next = n; + } + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) { + throw new IndexOutOfBoundsException(); + } + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + public static void main(String[] args) { + MyLinkedList l = new MyLinkedList(); + l.add("1"); + l.add("2"); + l.add("3"); + l.add(2, "4"); + l.remove(); + l.remove(1); + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/Queue.java b/group01/819048836/lvxg2017/src/basic/Queue.java new file mode 100644 index 0000000000..e29bf6fa4f --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/Queue.java @@ -0,0 +1,43 @@ +package basic; + +public class Queue { + private Node first; + private Node last; + private int size = 0; + + //入列 + @SuppressWarnings("unused") + private void enQueue(Object o) { + final Node f = first; + final Node newNode = new Node(o, f, null); + f.prev = newNode; + } + public int size(){ + return size; + } +public boolean isEmpty(){ + return size>=0; +} + // 出列 + @SuppressWarnings("unused") + private Object deQueue() { + final Node l = last; + final Node p = l.prev; + last = p; + p.next = null; + return l; + } + + private static class Node { + Object data; + Node next; + Node prev; + + public Node(Object o, Node n, Node p) { + this.data = o; + this.prev = p; + this.next = n; + } + } + +} diff --git a/group01/819048836/lvxg2017/src/basic/Stack.java b/group01/819048836/lvxg2017/src/basic/Stack.java new file mode 100644 index 0000000000..916eac298a --- /dev/null +++ b/group01/819048836/lvxg2017/src/basic/Stack.java @@ -0,0 +1,58 @@ +package basic; + +//ջ +public class Stack { + private Node first; + private Node last; + private int size = 0; + + // 出栈 + private void pop() { + removeLast(); + } + + // 入栈 + private void push(Object o) { + addLast(o); + } + private boolean isEmpty(){ + if(size==0){ + return true; + }else{ + return false; + } + } + private int size(){ + return size; + } + private void removeLast(){ + final Node f = last.prev; + last = f; + f.next =null; + } + + + private void addLast(Object o){ + final Node f= first; + final Node l = last; + final Node newNode = new Node(o, last, null); + if(f==null){ + first =newNode; + }else{ + l.next = newNode; + } + size++; + } + + private static class Node { + Object data; + Node next; + Node prev; + + public Node(Object o, Node n, Node p) { + this.data = o; + this.prev = p; + this.next = n; + } + } +} diff --git a/group01/895457260/code/src/datastructure/basic/ArrayList.java b/group01/895457260/code/src/datastructure/basic/ArrayList.java new file mode 100644 index 0000000000..28eb439c1b --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/ArrayList.java @@ -0,0 +1,136 @@ +package datastructure.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + public ArrayList() { + elementData = new Object[100]; + } + + public ArrayList(int initCapacity) { + elementData = new Object[initCapacity]; + } + + public void add(Object o) { + autoGrow(); + elementData[size()] = o; + size++; + } + + public void add(int index, Object o) { + autoGrow(); + System.arraycopy(elementData, index, elementData, index + 1, size() - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object removed = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); + size--; + return removed; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Iterator() { + int index = -1; + @Override + public boolean hasNext() { + return index + 1 < size(); + } + + @Override + public Object next() { + index++; + return elementData[index]; + } + }; + } + + private void autoGrow() { + if (size >= elementData.length) { + Object[] newArray = new Object[nextCapacity()]; + System.arraycopy(elementData, 0, newArray, 0, elementData.length); + elementData = newArray; + } + } + + private int nextCapacity() { + return elementData.length * 2; + } + + private void checkIndex(int index) { + if (index >= size() || index < 0) { + throw new IndexOutOfBoundsException(indexOutOfBoundMessage(index)); + } + } + + private String indexOutOfBoundMessage(int index) { + return "index: " + index + ", size: " + size(); + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + for (int i = 0; i < 10; ++i) { + list.add(i); + list.add(10 - i); + } + System.out.println("------------------size"); + System.out.println("size: " + list.size()); + + System.out.println("------------------for(int i)"); + for (int i = 0; i < list.size(); ++i) { + System.out.print(list.get(i) + " "); + } + + System.out.println("\n-----------------iterator"); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + + System.out.println("\n-----------------add at index 0 100~104"); + for (int i = 100; i < 105; ++i) { + list.add(0, i); + } + list.print(); + System.out.println("-----------------add at last 200~204"); + for (int i = 200; i < 205; ++i) { + list.add(list.size(), i); + } + list.print(); + + System.out.println("-----------------removeFirst x4"); + for (int i = 0; i < 4; ++i) { + list.remove(0); + } + list.print(); + + System.out.println("\n-----------------removeLast x4"); + for (int i = 0; i < 4; ++i) { + list.remove(list.size() - 1); + } + list.print(); + } + + public void print() { + Iterator iterator = iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + System.out.println("\nsize: " + size()); + } +} diff --git a/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java b/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java new file mode 100644 index 0000000000..b251ff02ee --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/BinarySortedTree.java @@ -0,0 +1,61 @@ +package datastructure.basic; + +/** + * Created by Haochen on 2017/2/24. + * TODO: + */ +public class BinarySortedTree { + + private BinaryTreeNode root = null; + + public void traverse(Visitor visitor) { + traverse(root, visitor); + } + + private void traverse(BinaryTreeNode node, Visitor visitor) { + if (node == null) { + return; + } + traverse(node.getLeft(), visitor); + visitor.visit(node); + traverse(node.getRight(), visitor); + } + + public interface Visitor { + void visit(BinaryTreeNode node); + } + + //不递归的写法 + public void add(T o) { + //根节点空,直接加入 + if (root == null) { + root = new BinaryTreeNode(); + root.setData(o); + } else { + BinaryTreeNode target = root; + //从根结点不断向下比较target和o,o小则往左,o大则往右,相等不加入 + while (true) { + int compare = o.compareTo(target.getData()); + if (compare == 0) {//相等不加入 + return; + } else if (compare < 0) {//o小往左 + if (target.getLeft() == null) {//左空则加入 + target.setLeft(new BinaryTreeNode()); + target.getLeft().setData(o); + return; + } else {//不空继续比较 + target = target.getLeft(); + } + } else {//o大往右 + if (target.getRight() == null) { + target.setRight(new BinaryTreeNode()); + target.getRight().setData(o); + return; + } else { + target = target.getRight(); + } + } + } + } + } +} diff --git a/group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java b/group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d3a9ff377b --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package datastructure.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/group01/895457260/code/src/datastructure/basic/Iterator.java b/group01/895457260/code/src/datastructure/basic/Iterator.java new file mode 100644 index 0000000000..c1fb7ae8a5 --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/Iterator.java @@ -0,0 +1,6 @@ +package datastructure.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group01/895457260/code/src/datastructure/basic/LinkedList.java b/group01/895457260/code/src/datastructure/basic/LinkedList.java new file mode 100644 index 0000000000..174044c546 --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/LinkedList.java @@ -0,0 +1,132 @@ +package datastructure.basic; + +import datastructure.exception.EmptyListException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + head = new Node(); + } + + @Override + public void add(Object o) { + addLast(o); + } + + @Override + public void add(int index , Object o) { + Node pre = findNode(index - 1); + Node node = new Node(); + node.data = o; + addNode(node, pre); + } + + @Override + public Object get(int index) { + checkIndex(index); + return findNode(index).data; + } + + @Override + public Object remove(int index) { + checkIndex(index); + Node pre = findNode(index - 1); + Node removed = pre.next; + removeNode(removed, pre); + return removed.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(); + node.data = o; + addNode(node, head); + } + + public void addLast(Object o) { + Node node = new Node(); + node.data = o; + Node pre = findNode(size() - 1); + addNode(node, pre); + } + + public Object removeFirst() { + if (size() == 0) { + throw new EmptyListException(); + } + Node removed = head.next; + removeNode(head.next, head); + return removed.data; + } + + public Object removeLast() { + if (size() == 0) { + throw new EmptyListException(); + } + return remove(size() - 1); + } + + @Override + public Iterator iterator() { + return new Iterator() { + Node node = head; + @Override + public boolean hasNext() { + return node.next != null; + } + + @Override + public Object next() { + node = node.next; + return node.data; + } + }; + } + + private static class Node{ + Object data; + Node next; + } + + private Node findNode(int index) { + if (index == -1) { + return head; + } else { + checkIndex(index); + } + Node node = head.next; + for (int i = 0; i < index; ++i) { + node = node.next; + } + return node; + } + + private void checkIndex(int index) { + if (index >= size() || index < 0) { + throw new IndexOutOfBoundsException(indexOutOfBoundMessage(index)); + } + } + + private String indexOutOfBoundMessage(int index) { + return "index: " + index + ", size: " + size(); + } + + private void addNode(Node node, Node pre) { + node.next = pre.next; + pre.next = node; + size++; + } + + private void removeNode(Node node, Node pre) { + pre.next = node.next; + node.next = null; + size--; + } +} diff --git a/group01/895457260/code/src/datastructure/basic/List.java b/group01/895457260/code/src/datastructure/basic/List.java new file mode 100644 index 0000000000..2f085701c5 --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/List.java @@ -0,0 +1,10 @@ +package datastructure.basic; + +public interface List { + void add(Object o); + void add(int index, Object o); + Object get(int index); + Object remove(int index); + int size(); + Iterator iterator(); +} diff --git a/group01/895457260/code/src/datastructure/basic/Queue.java b/group01/895457260/code/src/datastructure/basic/Queue.java new file mode 100644 index 0000000000..edd0a6a29e --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/Queue.java @@ -0,0 +1,68 @@ +package datastructure.basic; + +import datastructure.exception.EmptyQueueException; + +public class Queue { + //数组实现自增长的循环队列 + private Object[] array; + private int head = 0; + private int rear = 0; + + public Queue() { + this.array = new Object[10]; + } + + public Queue(int initCapacity) { + this.array = new Object[initCapacity]; + } + + public void enQueue(Object o) { + int target = mapIndex(rear); + autoGrow(); + array[target] = o; + rear++; + } + + public Object deQueue() { + if (isEmpty()) { + throw new EmptyQueueException(); + } + Object obj = array[mapIndex(head)]; + head++; + return obj; + } + + public boolean isEmpty() { + return head == rear; + } + + public int size() { + return rear - head; + } + + private int capacity() { + return array.length; + } + + private void autoGrow() { + if (size() >= capacity()) { + Object[] newArray = new Object[nextCapacity()]; + System.arraycopy(array, 0, newArray, 0, capacity()); + + int increase = nextCapacity() - capacity(); + int moveCount = size() - mapIndex(rear); + System.arraycopy(newArray, mapIndex(head), newArray, mapIndex(head) + increase, moveCount); + array = newArray; + head += increase; + rear += increase; + } + } + + private int nextCapacity() { + return capacity() * 2; + } + + private int mapIndex(int index) { + return index >= capacity() ? index % capacity() : index; + } +} diff --git a/group01/895457260/code/src/datastructure/basic/Stack.java b/group01/895457260/code/src/datastructure/basic/Stack.java new file mode 100644 index 0000000000..ab4fc874ae --- /dev/null +++ b/group01/895457260/code/src/datastructure/basic/Stack.java @@ -0,0 +1,32 @@ +package datastructure.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (isEmpty()) { + throw new EmptyStackException(); + } + Object peek = peek(); + elementData.remove(elementData.size() - 1); + return peek; + } + + public Object peek() { + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/895457260/code/src/datastructure/exception/EmptyListException.java b/group01/895457260/code/src/datastructure/exception/EmptyListException.java new file mode 100644 index 0000000000..6f38ed6c43 --- /dev/null +++ b/group01/895457260/code/src/datastructure/exception/EmptyListException.java @@ -0,0 +1,8 @@ +package datastructure.exception; + +/** + * Created by Haochen on 2017/2/24. + * TODO: + */ +public class EmptyListException extends RuntimeException { +} diff --git a/group01/895457260/code/src/datastructure/exception/EmptyQueueException.java b/group01/895457260/code/src/datastructure/exception/EmptyQueueException.java new file mode 100644 index 0000000000..071a366ed8 --- /dev/null +++ b/group01/895457260/code/src/datastructure/exception/EmptyQueueException.java @@ -0,0 +1,7 @@ +package datastructure.exception; + +/** + * Created by Haochen on 2017/2/24. + * TODO: + */ +public class EmptyQueueException extends RuntimeException {} diff --git a/group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java b/group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java new file mode 100644 index 0000000000..42b9144d38 --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java @@ -0,0 +1,152 @@ +package test.datastructure.basic; + +import datastructure.basic.ArrayList; +import datastructure.basic.Iterator; +import datastructure.basic.List; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * ArrayList Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class ArrayListTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + protected final List getList() { + List list = createList(); + init(list); + return list; + } + + List createList() { + return new ArrayList(5); + } + + private void init(List list) { + for (int i = 1; i <= 5; ++i) { + list.add(i); + } + } + + protected final Object[] toArray(List list) { + Object[] array = new Object[list.size()]; + Iterator iterator = list.iterator(); + int pos = 0; + while (iterator.hasNext()) { + array[pos++] = iterator.next(); + } + return array; + } + + /** + * Method: add(Object o) + */ + @Test + public void testAddO() throws Exception { +//TODO: Test goes here... + List list = getList(); + for (int i = 6; i <= 10; ++i) { + list.add(i); + } + Assert.assertArrayEquals(toArray(list), new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + Assert.assertEquals(list.size(), 10); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndexO() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {nowSize + 1, -1, nowSize, nowSize, 0, 1}; + Object[] values = {0, 0, 300, 400, 100, 200}; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + list.add(indexes[i], values[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(toArray(list), new Object[]{100, 200, 1, 2, 3, 4, 5, 400, 300}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize + 4); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {-1, nowSize, 0, 1, nowSize - 1, nowSize - 2}; + Object[] values = new Object[indexes.length]; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + values[i] = list.get(indexes[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{null, null, 1, 2, 5, 4}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { +//TODO: Test goes here... + List list = getList(); + int nowSize = list.size(); + int[] indexes = {-1, nowSize, nowSize - 2, nowSize - 2, 1, 0}; + Object[] values = new Object[indexes.length]; + boolean[] exceptions = new boolean[indexes.length]; + for (int i = 0; i < indexes.length; ++i) { + try { + values[i] = list.remove(indexes[i]); + } catch (IndexOutOfBoundsException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{null, null, 4, 5, 2, 1}); + Assert.assertArrayEquals(exceptions, new boolean[]{true, true, false, false, false, false}); + Assert.assertEquals(list.size(), nowSize - 4); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { +//TODO: Test goes here... + List list = getList(); + Iterator iterator = list.iterator(); + Object[] values = new Object[list.size()]; + int pos = 0; + while (iterator.hasNext()) { + values[pos++] = iterator.next(); + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5}); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java b/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java new file mode 100644 index 0000000000..ce15d5622c --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java @@ -0,0 +1,60 @@ +package test.datastructure.basic; + +import datastructure.basic.BinarySortedTree; +import datastructure.basic.BinaryTreeNode; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * BinarySortedTree Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class BinarySortedTreeTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + private BinarySortedTree getTree() { + return new BinarySortedTree<>(); + } + + /** + * Method: add(T o) + */ + @Test + public void testAdd() throws Exception { +//TODO: Test goes here... + BinarySortedTree tree = getTree(); + int[] addValues = {5, 3, 1, 7, 6, 4, 8}; + for (int i : addValues) { + tree.add(i); + } + + final Object[] left = new Object[addValues.length]; + final Object[] value = new Object[addValues.length]; + final Object[] right = new Object[addValues.length]; + tree.traverse(new BinarySortedTree.Visitor() { + int pos = 0; + @Override + public void visit(BinaryTreeNode node) { + left[pos] = node.getLeft() == null ? null : (int) node.getLeft().getData(); + value[pos] = node.getData(); + right[pos] = node.getRight() == null ? null : (int) node.getRight().getData(); + pos++; + } + }); + Assert.assertArrayEquals(left, new Object[]{null, 1, null, 3, null, 6, null}); + Assert.assertArrayEquals(value, new Object[]{1, 3, 4, 5, 6, 7, 8}); + Assert.assertArrayEquals(right, new Object[]{null, 4, null, 7, null, 8, null}); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java b/group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java new file mode 100644 index 0000000000..0ab03eb589 --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java @@ -0,0 +1,88 @@ +package test.datastructure.basic; + +import datastructure.exception.EmptyListException; +import datastructure.basic.LinkedList; +import datastructure.basic.List; +import org.junit.Assert; +import org.junit.Test; + +/** + * LinkedList Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class LinkedListTest extends ArrayListTest { + + @Override + List createList() { + return new LinkedList(); + } + + /** + * Method: addFirst(Object o) + */ + @Test + public void testAddFirst() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.addFirst(100); + Assert.assertArrayEquals(toArray(list), new Object[]{100, 1, 2, 3, 4, 5}); + } + + /** + * Method: addLast(Object o) + */ + @Test + public void testAddLast() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + list.addLast(100); + Assert.assertArrayEquals(toArray(list), new Object[]{1, 2, 3, 4, 5, 100}); + } + + /** + * Method: removeFirst() + */ + @Test + public void testRemoveFirst() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + int count = list.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = list.removeFirst(); + } catch (EmptyListException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + Assert.assertArrayEquals(toArray(list), new Object[0]); + } + + /** + * Method: removeLast() + */ + @Test + public void testRemoveLast() throws Exception { +//TODO: Test goes here... + LinkedList list = (LinkedList) getList(); + int count = list.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = list.removeLast(); + } catch (EmptyListException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{5, 4, 3, 2, 1, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + Assert.assertArrayEquals(toArray(list), new Object[0]); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/QueueTest.java b/group01/895457260/code/src/test/datastructure/basic/QueueTest.java new file mode 100644 index 0000000000..df7587bd3c --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/QueueTest.java @@ -0,0 +1,108 @@ +package test.datastructure.basic; + +import datastructure.exception.EmptyQueueException; +import datastructure.basic.Queue; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Queue Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class QueueTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + private Queue getQueue() { + Queue queue = new Queue(5); + for (int i = 1; i <= 5; ++i) { + queue.enQueue(i); + } + return queue; + } + + private void assertQueue(Queue queue, Object[] actual) { + Class clazz = Queue.class; + Object[] array = null; + int head = 0; + int rear = 0; + Method mapIndex = null; + try { + Field arrayField = clazz.getDeclaredField("array"); + Field headField = clazz.getDeclaredField("head"); + Field rearField = clazz.getDeclaredField("rear"); + mapIndex = clazz.getDeclaredMethod("mapIndex", int.class); + arrayField.setAccessible(true); + headField.setAccessible(true); + rearField.setAccessible(true); + mapIndex.setAccessible(true); + array = (Object[]) arrayField.get(queue); + head = (int) headField.get(queue); + rear = (int) rearField.get(queue); + } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) { + e.printStackTrace(); + } + int size = queue.size(); + Object[] excepted = new Object[size]; + int pos = 0; + try { + while (head < rear) { + excepted[pos++] = array[(int) mapIndex.invoke(queue, head)]; + head++; + } + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + Assert.assertArrayEquals(excepted, actual); + } + + /** + * Method: enQueue(Object o) + */ + @Test + public void testEnQueue() throws Exception { +//TODO: Test goes here... + Queue queue = getQueue(); + for (int i = 6; i <= 10; ++i) { + queue.enQueue(i); + } + assertQueue(queue, new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + } + + /** + * Method: deQueue() + */ + @Test + public void testDeQueue() throws Exception { +//TODO: Test goes here... + Queue queue = getQueue(); + int count = queue.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = queue.deQueue(); + } catch (EmptyQueueException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{1, 2, 3, 4, 5, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + assertQueue(queue, new Object[0]); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/StackTest.java b/group01/895457260/code/src/test/datastructure/basic/StackTest.java new file mode 100644 index 0000000000..ac2bd31a22 --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/StackTest.java @@ -0,0 +1,93 @@ +package test.datastructure.basic; + +import datastructure.basic.*; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.lang.reflect.Field; +import java.util.EmptyStackException; + +/** + * Stack Tester. + * + * @author + * @version 1.0 + * @since
二月 24, 2017
+ */ +public class StackTest { + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + private Stack getStack() { + Stack stack = new Stack(); + for (int i = 1; i <= 5; ++i) { + stack.push(i); + } + return stack; + } + + private void assertStack(Stack stack, Object[] actual) { + Class clazz = Stack.class; + ArrayList elementData = null; + try { + Field field = clazz.getDeclaredField("elementData"); + field.setAccessible(true); + elementData = (ArrayList) field.get(stack); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + + Object[] excepted = null; + if (elementData != null) { + int size = stack.size(); + excepted = new Object[size]; + for (int i = 0; i < size; ++i) { + excepted[i] = elementData.get(i); + } + } + Assert.assertArrayEquals(excepted, actual); + } + + /** + * Method: push(Object o) + */ + @Test + public void testPush() throws Exception { +//TODO: Test goes here... + Stack stack = getStack(); + for (int i = 6; i <= 10; ++i) { + stack.push(i); + } + assertStack(stack, new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + } + + /** + * Method: pop() + */ + @Test + public void testPop() throws Exception { +//TODO: Test goes here... + Stack stack = getStack(); + int count = stack.size() + 2; + Object[] values = new Object[count]; + boolean[] exceptions = new boolean[count]; + for (int i = 0; i < count; ++i) { + try { + values[i] = stack.pop(); + } catch (EmptyStackException e) { + exceptions[i] = true; + } + } + Assert.assertArrayEquals(values, new Object[]{5, 4, 3, 2, 1, null, null}); + Assert.assertArrayEquals(exceptions, new boolean[]{false, false, false, false, false, true, true}); + assertStack(stack, new Object[0]); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/TestSuite.java b/group01/895457260/code/src/test/datastructure/basic/TestSuite.java new file mode 100644 index 0000000000..f7c5868383 --- /dev/null +++ b/group01/895457260/code/src/test/datastructure/basic/TestSuite.java @@ -0,0 +1,13 @@ +package test.datastructure.basic; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Created by Haochen on 2017/2/24. + * TODO: + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ArrayListTest.class, LinkedListTest.class, + BinarySortedTreeTest.class, QueueTest.class, StackTest.class}) +public class TestSuite {} diff --git a/group01/932573198/20170220/.classpath b/group01/932573198/20170220/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group01/932573198/20170220/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group01/932573198/20170220/.gitignore b/group01/932573198/20170220/.gitignore new file mode 100644 index 0000000000..65776c32fc --- /dev/null +++ b/group01/932573198/20170220/.gitignore @@ -0,0 +1 @@ +/bin/ \ No newline at end of file diff --git a/group01/932573198/20170220/.project b/group01/932573198/20170220/.project new file mode 100644 index 0000000000..82b0a5ccfd --- /dev/null +++ b/group01/932573198/20170220/.project @@ -0,0 +1,17 @@ + + + 20170220 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/liuxin/.settings/org.eclipse.jdt.core.prefs b/group01/932573198/20170220/.settings/org.eclipse.jdt.core.prefs similarity index 100% rename from liuxin/.settings/org.eclipse.jdt.core.prefs rename to group01/932573198/20170220/.settings/org.eclipse.jdt.core.prefs diff --git a/group01/932573198/20170220/src/com/coding/basic/ArrayList.java b/group01/932573198/20170220/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..039e83f095 --- /dev/null +++ b/group01/932573198/20170220/src/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + /** + * 扩容 + */ + private void expansion() { + if (elementData.length <= size) + elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); + } + + /** + * 越界 + */ + private void outOfBoundsForAdd(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + private void outOfBoundsForOther(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + public void add(Object o) { + expansion(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + outOfBoundsForAdd(index); + expansion(); + for (int i = size - 1; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + outOfBoundsForOther(index); + return elementData[index]; + } + + public Object remove(int index) { + outOfBoundsForOther(index); + Object re = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size - 1] = null; + size--; + return re; + } + + public int size() { + return size; + } + + @Override + public String toString() { + return Arrays.toString(elementData); + } + + public Iterator iterator() { + return new ArrayIterator(); + } + + private class ArrayIterator implements Iterator { + + int pos = -1; + + @Override + public boolean hasNext() { + return size > ++pos ? true : false; + } + + @Override + public Object next() { + return elementData[pos]; + } + + } + +} diff --git a/group01/932573198/20170220/src/com/coding/basic/BinaryTree.java b/group01/932573198/20170220/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..730d7e7b3a --- /dev/null +++ b/group01/932573198/20170220/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,77 @@ +package com.coding.basic; + +public class BinaryTree { + + private BinaryTreeNode tNode; + + @Override + public String toString() { + return tNode + ""; + } + + public void insert(Object o) { + tNode = insert(o, tNode); + } + + public BinaryTreeNode insert(Object o, BinaryTreeNode node) { + if (node == null) { + node = new BinaryTreeNode(o); + } else { + int result = o.toString().compareTo(node.getData().toString()); + if (result < 0) + node.setLeft(insert(o, node.getLeft())); + if (result > 0) + node.setRight(insert(o, node.getRight())); + } + return node; + } + + private static class BinaryTreeNode { + + private BinaryTreeNode left; + + private Object data; + + private BinaryTreeNode right; + + public BinaryTreeNode() { + } + + public BinaryTreeNode(Object data) { + this.left = null; + this.data = data; + this.right = null; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + @Override + public String toString() { + return "[" + left + ", " + data + ", " + right + "]"; + } + + } + +} diff --git a/group01/932573198/20170220/src/com/coding/basic/Iterator.java b/group01/932573198/20170220/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ff93e30377 --- /dev/null +++ b/group01/932573198/20170220/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group01/932573198/20170220/src/com/coding/basic/LinkedList.java b/group01/932573198/20170220/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..db61384e4c --- /dev/null +++ b/group01/932573198/20170220/src/com/coding/basic/LinkedList.java @@ -0,0 +1,144 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + + public Node getHead() { + return head; + } + + public LinkedList() { + this.head = new Node(); + } + + @Override + public String toString() { + return "[" + head + "]"; + } + + private void outOfBoundsForAdd(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + private void outOfBoundsForOther(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + public void add(Object o) { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.next = new Node(o); + size++; + } + + public void add(int index, Object o) { + outOfBoundsForAdd(index); + if(size == index) + add(o); + else{ + Node prevNode = head; + for (int i = 0; i < index; i++) { + prevNode = prevNode.next; + } + Node nextNode = prevNode.next; + Node node = new Node(o); + prevNode.next = node; + node.next = nextNode; + size++; + } + } + + public Object get(int index) { + outOfBoundsForOther(index); + Node node = head; + for (int i = 0; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + outOfBoundsForOther(index); + Node prevNode = head; + for (int i = 0; i < index; i++) { + prevNode = prevNode.next; + } + Node node = prevNode.next; + prevNode.next = node.next; + size--; + return node.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + Node node = head.next; + head.next = newNode; + newNode.next = node; + size++; + } + + public void addLast(Object o) { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.next = new Node(o); + size++; + } + + private void noSuchEle() { + if (head.next == null) + throw new NoSuchElementException("没有这个元素"); + } + + public Object removeFirst() { + noSuchEle(); + Node node = head.next; + head.next = node.next; + size--; + return node.data; + } + + public Object removeLast() { + noSuchEle(); + Node node = head; + for(int i=0;i + + 4.0.0 + + com.aaront.execrise + coding2017 + 1.0.0-SNAPSHOT + jar + + + + UTF-8 + 1.8 + + + + + junit + junit + 4.12 + + + \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/ArrayList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/ArrayList.java new file mode 100644 index 0000000000..ae462ea905 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/ArrayList.java @@ -0,0 +1,90 @@ +package com.aaront.exercise.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private static final double factor = 0.75; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + _ensureCapacityEnough(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) throw new IndexOutOfBoundsException("index超出边界"); + _ensureCapacityEnough(); + int i = size; + for (; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[i] = o; + size++; + } + + private void _ensureCapacityEnough() { + if (size >= elementData.length) { + dilatancy(); + } + } + + private void dilatancy() { + int newLength = elementData.length + (int) (elementData.length * factor); + elementData = Arrays.copyOf(elementData, newLength); + } + + public Object get(int index) { + if(index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); + return elementData[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); + Object element = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + size--; + return element; + + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + public Object[] toArray() { + Object[] objects = new Object[size]; + System.arraycopy(elementData, 0, objects, 0, size); + return objects; + } + + private static class ArrayListIterator implements Iterator { + + private ArrayList arrayList; + private int pos = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + public boolean hasNext() { + return pos < arrayList.size(); + } + + public Object next() { + return arrayList.elementData[pos++]; + } + + public void remove() { + arrayList.remove(pos - 1); + pos--; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/BinaryTree.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/BinaryTree.java new file mode 100644 index 0000000000..2c0d156561 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/BinaryTree.java @@ -0,0 +1,235 @@ +package com.aaront.exercise.basic; + +public class BinaryTree { + + private BinaryTreeNode head = new BinaryTreeNode(null); + private BinaryTreeNode root; + private int size; + private int index = 0; + + public static final int PREORDER = 0; + public static final int INORDER = 1; + public static final int POSTORDER = 2; + public static final int HIERARCHICAL = 3; + + public static final int RECURSION = 10; + public static final int ITERATION = 11; + + public void add(Integer o) { + BinaryTreeNode node = new BinaryTreeNode(o); + if (root == null) { + root = node; + head.setLeft(root); + } else { + insert(root, node); + } + size++; + } + + private void insert(BinaryTreeNode node, BinaryTreeNode newNode) { + // 要插入的节点插入当前节点的左子树 + if (node.getData() > newNode.getData()) { + if (node.getLeft() == null) { + node.setLeft(newNode); + } else { + insert(node.left, newNode); + } + } else { // 要插入的节点插入当前节点的右子树 + if (node.getRight() == null) { + node.setRight(newNode); + } else { + insert(node.right, newNode); + } + } + } + + public BinaryTreeNode search(int data) { + return search(data, ITERATION); + } + + public BinaryTreeNode search(int data, int method) { + switch (method) { + case RECURSION: + return findNodeRecursion(root, data); + case ITERATION: + return findNodeIteration(data); + default: + throw new IllegalArgumentException("不支持的查找方法"); + } + } + + private BinaryTreeNode findNodeRecursion(BinaryTreeNode node, int data) { + if (node == null) return null; + if (node.getData() == data) return node; + if (node.getData() > data) return findNodeRecursion(node.getLeft(), data); + return findNodeRecursion(node.getRight(), data); + } + + private BinaryTreeNode findNodeIteration(int data) { + BinaryTreeNode currentNode = root; + while (currentNode != null) { + if (currentNode.getData() == data) { + return currentNode; + } + if (currentNode.getData() > data) { + currentNode = currentNode.getLeft(); + } else { + currentNode = currentNode.getRight(); + } + } + return null; + } + + public BinaryTreeNode min() { + return findMin(root); + } + + private BinaryTreeNode findMin(BinaryTreeNode node) { + if (node == null) return null; + if (node.getLeft() == null) return node; + return findMin(node.getLeft()); + } + + public BinaryTreeNode max() { + return findMax(root); + } + + private BinaryTreeNode findMax(BinaryTreeNode node) { + if (node == null) return null; + if (node.getRight() == null) return node; + return findMax(node.getRight()); + } + + public void delete(Integer data) { + BinaryTreeNode node = search(data); + if (node == null) return; + BinaryTreeNode parentNode = searchParentNode(node); + if (parentNode == null) return; + // 删除叶子节点 + if (node.getLeft() == null && node.getRight() == null) { + if (parentNode.getLeft() == node) parentNode.setLeft(null); + else parentNode.setRight(null); + } else if (node.getLeft() != null && node.getRight() == null) { // 删除只有左子树的节点 + if (parentNode.getLeft() == node) parentNode.setLeft(node.getLeft()); + else parentNode.setRight(node.getLeft()); + } else if (node.getRight() != null && node.getLeft() == null) { // 删除只有右子树的节点 + if (parentNode.getLeft() == node) parentNode.setLeft(node.getRight()); + else parentNode.setRight(node.getRight()); + } else { // 删除有两个子树的节点 + BinaryTreeNode replace = findMin(node.getRight()); + BinaryTreeNode replaceParentNode = searchParentNode(replace); + replaceParentNode.setLeft(replace.getRight()); + node.setData(replace.getData()); + replace.setLeft(null); + replace.setRight(null); + } + size--; + } + + private BinaryTreeNode searchParentNode(BinaryTreeNode node) { + if (node == null) return null; + if (node == root) return head; + BinaryTreeNode current = root; + while (current != null) { + if (current.getLeft() == node || current.getRight() == node) return current; + if (current.getData().compareTo(node.getData()) > 0) current = current.getLeft(); + else current = current.getRight(); + } + return null; + } + + public int[] traversal() { + return traversal(PREORDER); + } + + public int[] traversal(int order) { + int[] datas = new int[size]; + if (order == PREORDER) { + preorderTraversal(root, datas); + } else if (order == INORDER) { + inorderTraversal(root, datas); + } else if (order == POSTORDER) { + postorderTraversal(root, datas); + } else { + hierarchicalTraversal(root, datas); + } + index = 0; + return datas; + } + + private void preorderTraversal(BinaryTreeNode node, int[] datas) { + if (node == null) { + return; + } + + datas[index++] = node.getData(); + preorderTraversal(node.getLeft(), datas); + preorderTraversal(node.getRight(), datas); + } + + private void inorderTraversal(BinaryTreeNode node, int[] datas) { + if (node == null) { + return; + } + + inorderTraversal(node.getLeft(), datas); + datas[index++] = node.getData(); + inorderTraversal(node.getRight(), datas); + } + + private void postorderTraversal(BinaryTreeNode node, int[] datas) { + if (node == null) { + return; + } + + postorderTraversal(node.getLeft(), datas); + postorderTraversal(node.getRight(), datas); + datas[index++] = node.getData(); + } + + private void hierarchicalTraversal(BinaryTreeNode node, int[] datas) { + if (node == null) return; + Queue queue = new Queue(); + queue.enQueue(node); + while (!queue.isEmpty()) { + BinaryTreeNode tmp = (BinaryTreeNode) queue.deQueue(); + datas[index++] = tmp.getData(); + if (tmp.getLeft() != null) queue.enQueue(tmp.getLeft()); + if (tmp.getRight() != null) queue.enQueue(tmp.getRight()); + } + } + + public class BinaryTreeNode { + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Integer data) { + this.data = data; + } + + public Integer getData() { + return data; + } + + public void setData(Integer data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Iterator.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Iterator.java new file mode 100644 index 0000000000..e446dd8f65 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.aaront.exercise.basic; + +public interface Iterator { + boolean hasNext(); + + Object next(); + + void remove(); +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LinkedList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LinkedList.java new file mode 100644 index 0000000000..504e73580b --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/LinkedList.java @@ -0,0 +1,129 @@ +package com.aaront.exercise.basic; + +public class LinkedList implements List { + + private Node head = new Node(null); + private int size = 0; + + public void add(Object o) { + Node newNode = new Node(o); + Node first = head.next; + Node second = head; + while (first != null) { + second = first; + first = first.next; + } + second.next = newNode; + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + Node node = new Node(o); + node.next = first.next; + first.next = node; + size++; + } + + public Object get(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head.next; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + return first.data; + } + + public Object remove(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + Node element = first.next; + first.next = first.next.next; + size--; + return element.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + add(0, o); + } + + 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 LinkedListIterator(this); + } + + public Object[] toArray() { + Object[] objects = new Object[size]; + Node first = head.next; + int pos = 0; + while (first!= null) { + objects[pos++] = first.data; + first = first.next; + } + return objects; + } + + private static class LinkedListIterator implements Iterator { + + private int pos = 0; + private LinkedList linkedList; + + private LinkedListIterator(LinkedList linkList) { + this.linkedList = linkList; + } + + @Override + public boolean hasNext() { + return pos < linkedList.size(); + } + + @Override + public Object next() { + return linkedList.get(pos++); + } + + @Override + public void remove() { + linkedList.remove(pos - 1); + pos--; + } + } + + + private static class Node { + private Object data; + private Node next; + + private Node(Object data) { + this.data = data; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/List.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/List.java new file mode 100644 index 0000000000..0988b60f4a --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/List.java @@ -0,0 +1,9 @@ +package com.aaront.exercise.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/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Queue.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Queue.java new file mode 100644 index 0000000000..7c310bca9e --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Queue.java @@ -0,0 +1,26 @@ +package com.aaront.exercise.basic; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o) { + linkedList.add(o); + } + + public Object deQueue() { + return linkedList.removeFirst(); + } + + public boolean isEmpty() { + return linkedList.size() == 0; + } + + public int size() { + return linkedList.size(); + } + + public Object[] toArray() { + return linkedList.toArray(); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Stack.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Stack.java new file mode 100644 index 0000000000..450d21ee89 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Stack.java @@ -0,0 +1,29 @@ +package com.aaront.exercise.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(); + } + + public Object[] toArray() { + return elementData.toArray(); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericArrayList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericArrayList.java new file mode 100644 index 0000000000..a099746b55 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericArrayList.java @@ -0,0 +1,98 @@ +package com.aaront.exercise.generic; + +import java.util.Arrays; + +public class GenericArrayList implements GenericList { + + private int size = 0; + + private static final double factor = 0.75; + + private Object[] elementData = new Object[100]; + + public void add(T o) { + _ensureCapacityEnough(); + elementData[size++] = o; + } + + public void add(int index, T o) { + if (index < 0 || index > size) throw new IndexOutOfBoundsException("index超出边界"); + _ensureCapacityEnough(); + int i = size; + for (; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[i] = o; + size++; + } + + private void _ensureCapacityEnough() { + if (size >= elementData.length) { + dilatancy(); + } + } + + private void dilatancy() { + int newLength = elementData.length + (int) (elementData.length * factor); + elementData = Arrays.copyOf(elementData, newLength); + } + + public T get(int index) { + if(index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); + return (T) elementData[index]; + } + + public T remove(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); + Object element = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + size--; + return (T) element; + + } + + public int size() { + return size; + } + + public GenericIterator iterator() { + return new ArrayListGenericIterator(this); + } + + public Object[] toArray() { + Object[] objects = new Object[size]; + System.arraycopy(elementData, 0, objects, 0, size); + return objects; + } + + public T[] toArray(T[] a) { + if (a.length < size) + // Make a new array of a's runtime type, but my contents: + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + System.arraycopy(elementData, 0, a, 0, size); + return a; + } + + private static class ArrayListGenericIterator implements GenericIterator { + + private GenericArrayList genericArrayList; + private int pos = 0; + + private ArrayListGenericIterator(GenericArrayList genericArrayList) { + this.genericArrayList = genericArrayList; + } + + public boolean hasNext() { + return pos < genericArrayList.size(); + } + + public T next() { + return (T) genericArrayList.elementData[pos++]; + } + + public void remove() { + genericArrayList.remove(pos - 1); + pos--; + } + } +} \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericBinaryTree.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericBinaryTree.java new file mode 100644 index 0000000000..e5cf3b439a --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericBinaryTree.java @@ -0,0 +1,255 @@ +package com.aaront.exercise.generic; + +import java.util.Arrays; + +public class GenericBinaryTree> { + + private BinaryTreeNode head = new BinaryTreeNode<>(null); + private BinaryTreeNode root; + private int size; + private int index = 0; + public static final int PREORDER = 0; + public static final int INORDER = 1; + public static final int POSTORDER = 2; + public static final int HIERARCHICAL = 3; + + public static final int RECURSION = 10; + public static final int ITERATION = 11; + + public void add(T o) { + BinaryTreeNode node = new BinaryTreeNode<>(o); + if (root == null) { + root = node; + head.setLeft(root); + } else { + insert(root, node); + } + size++; + } + + private void insert(BinaryTreeNode node, BinaryTreeNode newNode) { + // 要插入的节点插入当前节点的左子树 + if (node.getData().compareTo(newNode.getData()) > 0) { + if (node.getLeft() == null) { + node.setLeft(newNode); + } else { + insert(node.left, newNode); + } + } else { // 要插入的节点插入当前节点的右子树 + if (node.getRight() == null) { + node.setRight(newNode); + } else { + insert(node.right, newNode); + } + } + } + + public BinaryTreeNode search(T data) { + return search(data, ITERATION); + } + + public BinaryTreeNode search(T data, int method) { + switch (method) { + case RECURSION: + return findNodeRecursion(root, data); + case ITERATION: + return findNodeIteration(data); + default: + throw new IllegalArgumentException("不支持的查找方法"); + } + } + + private BinaryTreeNode findNodeRecursion(BinaryTreeNode node, T data) { + if (node == null) return null; + if (node.getData().compareTo(data) == 0) return node; + if (node.getData().compareTo(data) > 0) return findNodeRecursion(node.getLeft(), data); + return findNodeRecursion(node.getRight(), data); + } + + private BinaryTreeNode findNodeIteration(T data) { + BinaryTreeNode currentNode = root; + while (currentNode != null) { + if (currentNode.getData().compareTo(data) == 0) { + return currentNode; + } + if (currentNode.getData().compareTo(data) > 0) { + currentNode = currentNode.getLeft(); + } else { + currentNode = currentNode.getRight(); + } + } + return null; + } + + public BinaryTreeNode min() { + return findMin(root); + } + + private BinaryTreeNode findMin(BinaryTreeNode node) { + if (node == null) return null; + if (node.getLeft() == null) return node; + return findMin(node.getLeft()); + } + + public BinaryTreeNode max() { + return findMax(root); + } + + private BinaryTreeNode findMax(BinaryTreeNode node) { + if (node == null) return null; + if (node.getRight() == null) return node; + return findMax(node.getRight()); + } + + public void delete(T data) { + BinaryTreeNode node = search(data); + if (node == null) return; + BinaryTreeNode parentNode = searchParentNode(node); + if (parentNode == null) return; + // 删除叶子节点 + if (node.getLeft() == null && node.getRight() == null) { + if (parentNode.getLeft() == node) parentNode.setLeft(null); + else parentNode.setRight(null); + } else if (node.getLeft() != null && node.getRight() == null) { // 删除只有左子树的节点 + if (parentNode.getLeft() == node) parentNode.setLeft(node.getLeft()); + else parentNode.setRight(node.getLeft()); + } else if (node.getRight() != null && node.getLeft() == null) { // 删除只有右子树的节点 + if (parentNode.getLeft() == node) parentNode.setLeft(node.getRight()); + else parentNode.setRight(node.getRight()); + } else { // 删除有两个子树的节点 + BinaryTreeNode replace = findMin(node.getRight()); + BinaryTreeNode replaceParentNode = searchParentNode(replace); + replaceParentNode.setLeft(replace.getRight()); + node.setData(replace.getData()); + replace.setLeft(null); + replace.setRight(null); + } + size--; + } + + private BinaryTreeNode searchParentNode(BinaryTreeNode node) { + if (node == null) return null; + if (node == root) return head; + BinaryTreeNode current = root; + while (current != null) { + if (current.getLeft() == node || current.getRight() == node) return current; + if (current.getData().compareTo(node.getData()) > 0) current = current.getLeft(); + else current = current.getRight(); + } + return null; + } + + public Object[] traversal() { + return traversal(PREORDER); + } + + public T[] traversal(T[] a) { + Object[] elementData = traversal(PREORDER); + return toArray(elementData, a); + } + + public T[] traversal(int order, T[] a) { + Object[] elementData = traversal(order); + return toArray(elementData, a); + } + + private T[] toArray(Object[] elementData, T[] a) { + if (a.length < size) + // Make a new array of a's runtime type, but my contents: + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + System.arraycopy(elementData, 0, a, 0, size); + return a; + } + + public Object[] traversal(int order) { + Object[] datas = new Object[size]; + if (order == PREORDER) { + preorderTraversal(root, datas); + } else if (order == INORDER) { + inorderTraversal(root, datas); + } else if (order == POSTORDER) { + postorderTraversal(root, datas); + } else { + hierarchicalTraversal(root, datas); + } + index = 0; + return datas; + } + + private void preorderTraversal(BinaryTreeNode node, Object[] datas) { + if (node == null) { + return; + } + + datas[index++] = node.getData(); + preorderTraversal(node.getLeft(), datas); + preorderTraversal(node.getRight(), datas); + } + + private void inorderTraversal(BinaryTreeNode node, Object[] datas) { + if (node == null) { + return; + } + + inorderTraversal(node.getLeft(), datas); + datas[index++] = node.getData(); + inorderTraversal(node.getRight(), datas); + } + + private void postorderTraversal(BinaryTreeNode node, Object[] datas) { + if (node == null) { + return; + } + + postorderTraversal(node.getLeft(), datas); + postorderTraversal(node.getRight(), datas); + datas[index++] = node.getData(); + } + + private void hierarchicalTraversal(BinaryTreeNode node, Object[] datas) { + if (node == null) return; + GenericQueue> queue = new GenericQueue<>(); + queue.enQueue(node); + while (!queue.isEmpty()) { + BinaryTreeNode tmp = queue.deQueue(); + datas[index++] = tmp.getData(); + if (tmp.getLeft() != null) queue.enQueue(tmp.getLeft()); + if (tmp.getRight() != null) queue.enQueue(tmp.getRight()); + } + } + + + class BinaryTreeNode> { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T data) { + this.data = data; + } + + 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; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericIterator.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericIterator.java new file mode 100644 index 0000000000..565114dce7 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericIterator.java @@ -0,0 +1,9 @@ +package com.aaront.exercise.generic; + +public interface GenericIterator { + boolean hasNext(); + + T next(); + + void remove(); +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericLinkedList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericLinkedList.java new file mode 100644 index 0000000000..7caf32eae1 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericLinkedList.java @@ -0,0 +1,140 @@ +package com.aaront.exercise.generic; + +import java.util.Arrays; + +public class GenericLinkedList implements GenericList { + + private Node head = new Node<>(null); + private int size = 0; + + public void add(T o) { + Node newNode = new Node<>(o); + Node first = head.next; + Node second = head; + while (first != null) { + second = first; + first = first.next; + } + second.next = newNode; + size++; + } + + public void add(int index, T o) { + if (index < 0 || index > size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + Node node = new Node<>(o); + node.next = first.next; + first.next = node; + size++; + } + + public T get(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head.next; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + return first.data; + } + + public T remove(int index) { + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("索引超出范围"); + Node first = head; + int i = 0; + while (i < index) { + first = first.next; + i++; + } + Node element = first.next; + first.next = first.next.next; + size--; + return element.data; + } + + public int size() { + return size; + } + + public void addFirst(T o) { + add(0, o); + } + + public void addLast(T o) { + add(size, o); + } + + public T removeFirst() { + return remove(0); + } + + public T removeLast() { + return remove(size - 1); + } + + public GenericIterator iterator() { + return new LinkedListGenericIterator<>(this); + } + + public Object[] toArray() { + Object[] objects = new Object[size]; + Node first = head.next; + int pos = 0; + while (first != null) { + objects[pos++] = first.data; + first = first.next; + } + return objects; + } + + public T[] toArray(T[] a) { + Object[] elementData = toArray(); + if (a.length < size) + // Make a new array of a's runtime type, but my contents: + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + System.arraycopy(elementData, 0, a, 0, size); + return a; + } + + private static class LinkedListGenericIterator implements GenericIterator { + + private int pos = 0; + private GenericLinkedList genericLinkedList; + + private LinkedListGenericIterator(GenericLinkedList linkList) { + this.genericLinkedList = linkList; + } + + @Override + public boolean hasNext() { + return pos < genericLinkedList.size(); + } + + @Override + public T next() { + return genericLinkedList.get(pos++); + } + + @Override + public void remove() { + genericLinkedList.remove(pos - 1); + pos--; + } + } + + + private static class Node { + private T data; + private Node next; + + private Node(T data) { + this.data = data; + } + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericList.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericList.java new file mode 100644 index 0000000000..94dc9f8a98 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericList.java @@ -0,0 +1,9 @@ +package com.aaront.exercise.generic; + +public interface GenericList { + public void add(T o); + public void add(int index, T o); + public T get(int index); + public T remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericQueue.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericQueue.java new file mode 100644 index 0000000000..d5cf5681e5 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericQueue.java @@ -0,0 +1,30 @@ +package com.aaront.exercise.generic; + +public class GenericQueue { + + private GenericLinkedList linkedList = new GenericLinkedList<>(); + + public void enQueue(T o) { + linkedList.add(o); + } + + public T deQueue() { + return linkedList.removeFirst(); + } + + public boolean isEmpty() { + return linkedList.size() == 0; + } + + public int size() { + return linkedList.size(); + } + + public Object[] toArray() { + return linkedList.toArray(); + } + + public T[] toArray(T[] a) { + return linkedList.toArray(a); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericStack.java b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericStack.java new file mode 100644 index 0000000000..9efb2f2220 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/generic/GenericStack.java @@ -0,0 +1,33 @@ +package com.aaront.exercise.generic; + +public class GenericStack { + private GenericArrayList elementData = new GenericArrayList<>(); + + public void push(T o) { + elementData.add(o); + } + + public T pop() { + return elementData.remove(elementData.size() - 1); + } + + public T peek() { + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + + public Object[] toArray() { + return elementData.toArray(); + } + + public T[] toArray(T[] a) { + return elementData.toArray(a); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/AllTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/AllTest.java new file mode 100644 index 0000000000..ebff633b23 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/AllTest.java @@ -0,0 +1,19 @@ +package com.aaront.execrise.basic; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * @author tonyhui + * @since 17/2/21 + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + ArrayListTest.class, + BinaryTreeTest.class, + LinkListTest.class, + QueueTest.class, + StackTest.class +}) +public class AllTest { +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java new file mode 100644 index 0000000000..dcc45d792e --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java @@ -0,0 +1,69 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.ArrayList; +import com.aaront.exercise.basic.Iterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/20 + */ +public class ArrayListTest { + + private ArrayList arrayList = new ArrayList(); + + @Before + public void init() { + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + } + + @Test + public void testAdd() { + Assert.assertEquals(arrayList.get(0), 1); + Assert.assertEquals(arrayList.get(1), 2); + Assert.assertEquals(arrayList.get(2), 3); + Assert.assertEquals(arrayList.size(), 3); + } + + @Test + public void testAddIndex() { + arrayList.add(1, 4); + arrayList.add(2, 5); + Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 4, 5, 2, 3}); + } + + @Test + public void testToArray() { + Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 2, 3}); + } + + @Test + public void testGet() { + Assert.assertEquals(arrayList.get(2), 3); + Assert.assertEquals(arrayList.get(0), 1); + Assert.assertEquals(arrayList.get(1), 2); + } + + @Test + public void testRemove() { + testAddIndex(); + arrayList.remove(2); + arrayList.add(4, 10); + arrayList.add(3, 9); + Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 4, 2, 9, 3, 10}); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + iterator.next(); + iterator.remove(); + } + Assert.assertArrayEquals(arrayList.toArray(), new Object[]{}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java new file mode 100644 index 0000000000..11fb7ad66b --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java @@ -0,0 +1,94 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.BinaryTree; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/20 + */ +public class BinaryTreeTest { + + private BinaryTree binaryTree = null; + + @Before + public void init() { + int[] datas = new int[]{9, 4, 5, 7, 1, 2, 3, 10, 17, 9}; + binaryTree = new BinaryTree(); + for (int data : datas) { + binaryTree.add(data); + } + } + + + @Test + public void testAdd() { + int[] preorderDatas = binaryTree.traversal(BinaryTree.PREORDER); + Assert.assertArrayEquals(preorderDatas, new int[]{9, 4, 1, 2, 3, 5, 7, 10, 9, 17}); + int[] inorderDatas = binaryTree.traversal(BinaryTree.INORDER); + Assert.assertArrayEquals(inorderDatas, new int[]{1, 2, 3, 4, 5, 7, 9, 9, 10, 17}); + int[] postorderDatas = binaryTree.traversal(BinaryTree.POSTORDER); + Assert.assertArrayEquals(postorderDatas, new int[]{3, 2, 1, 7, 5, 4, 9, 17, 10, 9}); + int[] hierarchicalDatas = binaryTree.traversal(BinaryTree.HIERARCHICAL); + Assert.assertArrayEquals(hierarchicalDatas, new int[]{9, 4, 10, 1, 5, 9, 17, 2, 7, 3}); + } + + @Test + public void testSearch() { + BinaryTree.BinaryTreeNode node1 = binaryTree.search(5, BinaryTree.RECURSION); + Assert.assertTrue(node1.getData() == 5); + BinaryTree.BinaryTreeNode node2 = binaryTree.search(17, BinaryTree.RECURSION); + Assert.assertTrue(node2.getData() == 17); + BinaryTree.BinaryTreeNode node3 = binaryTree.search(100, BinaryTree.RECURSION); + Assert.assertTrue(node3 == null); + } + + @Test + public void testMin() { + BinaryTree.BinaryTreeNode min = binaryTree.min(); + Assert.assertTrue(min.getData() == 1); + } + + @Test + public void testMax() { + BinaryTree.BinaryTreeNode max = binaryTree.max(); + Assert.assertTrue(max.getData() == 17); + } + + @Test + public void testDelete() { + buildTree(new int[]{50, 25, 12, 11, 40, 14, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + // 删除叶子节点 + binaryTree.delete(11); + int[] preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + binaryTree.delete(88); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82}); + + // 删除一个子节点的节点 + binaryTree.delete(70); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 80, 85, 82}); + binaryTree.delete(80); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + + // 删除两个子节点的节点 + binaryTree.delete(40); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 42, 35, 45, 44, 43, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + binaryTree.delete(50); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new int[]{55, 25, 12, 14, 42, 35, 45, 44, 43, 75, 60, 65, 63, 61, 90, 85, 82}); + } + + private void buildTree(int[] datas) { + binaryTree = new BinaryTree(); + for (int data : datas) { + binaryTree.add(data); + } + } +} \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java new file mode 100644 index 0000000000..b690c69c94 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java @@ -0,0 +1,81 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.Iterator; +import com.aaront.exercise.basic.LinkedList; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class LinkListTest { + + private LinkedList linkedList = new LinkedList(); + + @Before + public void init() { + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + } + + @Test + public void testAdd() { + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1, 2, 3}); + } + + @Test + public void testAddIndex() { + linkedList.add(1, 10); + linkedList.add(0, 8); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{8, 1, 10, 2, 3}); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(-1); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{-1, 1, 2, 3}); + } + + @Test + public void testAddLast() { + linkedList.addLast(99); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1, 2, 3, 99}); + } + + @Test + public void testRemove() { + testAddIndex(); + linkedList.remove(1); + linkedList.remove(2); + linkedList.add(3, 3); + linkedList.add(1, 2); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{8, 2, 10, 3, 3}); + } + + @Test + public void testRemoveFirst() { + linkedList.removeFirst(); + linkedList.removeFirst(); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{3}); + } + + @Test + public void testRemoveLast() { + linkedList.removeLast(); + linkedList.removeLast(); + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1}); + } + + @Test + public void testIterator() { + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()) { + iterator.next(); + iterator.remove(); + } + Assert.assertArrayEquals(linkedList.toArray(), new Object[]{}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java new file mode 100644 index 0000000000..0035a353ec --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java @@ -0,0 +1,33 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.Queue; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class QueueTest { + private Queue queue = new Queue(); + + @Before + public void init() { + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + } + + @Test + public void testEnqueue() { + Assert.assertArrayEquals(queue.toArray(), new Object[]{1, 2, 3}); + } + + @Test + public void testDequeue() { + queue.deQueue(); + queue.deQueue(); + Assert.assertArrayEquals(queue.toArray(), new Object[]{3}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java new file mode 100644 index 0000000000..3add6bcfdf --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java @@ -0,0 +1,46 @@ +package com.aaront.execrise.basic; + +import com.aaront.exercise.basic.Stack; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class StackTest { + + private Stack stack = new Stack(); + + @Before + public void init() { + stack.push(1); + stack.push(2); + stack.push(3); + } + + @Test + public void testPush() { + Assert.assertArrayEquals(stack.toArray(), new Object[]{1, 2, 3}); + } + + @Test + public void testPop() { + Object element1 = stack.pop(); + Assert.assertEquals(element1, 3); + Object element2 = stack.pop(); + Assert.assertEquals(element2, 2); + Assert.assertArrayEquals(stack.toArray(), new Object[]{1}); + } + + @Test + public void testPeek() { + Object element1 = stack.peek(); + Assert.assertEquals(element1, 3); + Object element2 = stack.peek(); + Assert.assertEquals(element2, 3); + Assert.assertArrayEquals(stack.toArray(), new Object[]{1, 2, 3}); + } + +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericAllTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericAllTest.java new file mode 100644 index 0000000000..66c071cf5c --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericAllTest.java @@ -0,0 +1,19 @@ +package com.aaront.execrise.generic; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * @author tonyhui + * @since 17/2/22 + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + GenericArrayListTest.class, + GenericLinkedListTest.class, + GenericQueueTest.class, + GenericStackTest.class, + GenericBinaryTreeTest.class +}) +public class GenericAllTest { +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java new file mode 100644 index 0000000000..8f97cbd3ea --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java @@ -0,0 +1,76 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericArrayList; +import com.aaront.exercise.generic.GenericIterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/22 + */ +public class GenericArrayListTest { + + private GenericArrayList arrayList = new GenericArrayList<>(); + + @Before + public void init() { + arrayList.add("1"); + arrayList.add("2"); + arrayList.add("3"); + } + + + @Test + public void testAdd() { + Assert.assertEquals(arrayList.get(0), "1"); + Assert.assertEquals(arrayList.get(1), "2"); + Assert.assertEquals(arrayList.get(2), "3"); + Assert.assertEquals(arrayList.size(), 3); + } + + @Test + public void testAddIndex() { + arrayList.add(1, "4"); + arrayList.add(2, "5"); + Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "4", "5", "2", "3"}); + } + + @Test + public void testToArray() { + Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testToGenericArray() { + Assert.assertArrayEquals(arrayList.toArray(new String[0]), new String[]{"1", "2", "3"}); + } + + @Test + public void testGet() { + Assert.assertEquals(arrayList.get(2), "3"); + Assert.assertEquals(arrayList.get(0), "1"); + Assert.assertEquals(arrayList.get(1), "2"); + } + + @Test + public void testRemove() { + testAddIndex(); + arrayList.remove(2); + arrayList.add(4, "10"); + arrayList.add(3, "9"); + Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "4", "2", "9", "3", "10"}); + } + + @Test + public void testIterator() { + GenericIterator genericIterator = arrayList.iterator(); + while (genericIterator.hasNext()) { + genericIterator.next(); + genericIterator.remove(); + } + Assert.assertArrayEquals(arrayList.toArray(), new String[]{}); + } + +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java new file mode 100644 index 0000000000..41adbf6706 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java @@ -0,0 +1,75 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericBinaryTree; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/20 + */ +public class GenericBinaryTreeTest { + + @Before + public void init() { + String[] datas = new String[]{"9", "4", "5", "7", "1", "2", "3", "10", "17", "9"}; + GenericBinaryTree binaryTree = new GenericBinaryTree<>(); + for (String data : datas) { + binaryTree.add(data); + } + } + + @Test + public void testAdd() { + String[] datas = new String[]{"9", "4", "5", "7", "1", "2", "3", "10", "17", "9"}; + GenericBinaryTree binaryTree = new GenericBinaryTree<>(); + for (String data : datas) { + binaryTree.add(data); + } + String[] preorderDatas = binaryTree.traversal(GenericBinaryTree.PREORDER, new String[0]); + Assert.assertArrayEquals(preorderDatas, new String[]{"9", "4", "1", "2", "10", "17", "3", "5", "7", "9" }); + String[] inorderDatas = binaryTree.traversal(GenericBinaryTree.INORDER, new String[0]); + Assert.assertArrayEquals(inorderDatas, new String[]{"1", "10", "17", "2", "3", "4", "5", "7", "9", "9" }); + String[] postorderDatas = binaryTree.traversal(GenericBinaryTree.POSTORDER, new String[0]); + Assert.assertArrayEquals(postorderDatas, new String[]{"17", "10", "3", "2", "1", "7", "5", "4", "9", "9" }); + String[] hierarchicalDatas = binaryTree.traversal(GenericBinaryTree.HIERARCHICAL, new String[0]); + Assert.assertArrayEquals(hierarchicalDatas, new String[]{"9", "4", "9", "1", "5", "2", "7", "10", "3", "17" }); + } + + @Test + public void testDelete() { + GenericBinaryTree binaryTree = buildTree(new int[]{50, 25, 12, 11, 40, 14, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + // 删除叶子节点 + binaryTree.delete(11); + Object[] preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + binaryTree.delete(88); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82}); + + // 删除一个子节点的节点 + binaryTree.delete(70); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 80, 85, 82}); + binaryTree.delete(80); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + + // 删除两个子节点的节点 + binaryTree.delete(40); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 42, 35, 45, 44, 43, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + binaryTree.delete(50); + preOrderDatas = binaryTree.traversal(); + Assert.assertArrayEquals(preOrderDatas, new Object[]{55, 25, 12, 14, 42, 35, 45, 44, 43, 75, 60, 65, 63, 61, 90, 85, 82}); + } + + private GenericBinaryTree buildTree(int[] datas) { + GenericBinaryTree binaryTree = new GenericBinaryTree<>(); + for (int data : datas) { + binaryTree.add(data); + } + return binaryTree; + } +} \ No newline at end of file diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java new file mode 100644 index 0000000000..513119fa6e --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java @@ -0,0 +1,90 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericLinkedList; +import com.aaront.exercise.generic.GenericIterator; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/22 + */ +public class GenericLinkedListTest { + private GenericLinkedList linkedList = new GenericLinkedList<>(); + + @Before + public void init() { + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + } + + @Test + public void testAdd() { + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testAddIndex() { + linkedList.add(1, "10"); + linkedList.add(0, "8"); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"8", "1", "10", "2", "3"}); + } + + @Test + public void testAddFirst() { + linkedList.addFirst("-1"); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"-1", "1", "2", "3"}); + } + + @Test + public void testAddLast() { + linkedList.addLast("99"); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3", "99"}); + } + + @Test + public void testRemove() { + testAddIndex(); + linkedList.remove(1); + linkedList.remove(2); + linkedList.add(3, "3"); + linkedList.add(1, "2"); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"8", "2", "10", "3", "3"}); + } + + @Test + public void testRemoveFirst() { + linkedList.removeFirst(); + linkedList.removeFirst(); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"3"}); + } + + @Test + public void testRemoveLast() { + linkedList.removeLast(); + linkedList.removeLast(); + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1"}); + } + + @Test + public void testToArray() { + Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testToGenericArray() { + Assert.assertArrayEquals(linkedList.toArray(new String[0]), new String[]{"1", "2", "3"}); + } + + @Test + public void testIterator() { + GenericIterator genericIterator = linkedList.iterator(); + while (genericIterator.hasNext()) { + genericIterator.next(); + genericIterator.remove(); + } + Assert.assertArrayEquals(linkedList.toArray(), new String[]{}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java new file mode 100644 index 0000000000..6b33a4b3e0 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java @@ -0,0 +1,33 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericQueue; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class GenericQueueTest { + private GenericQueue queue = new GenericQueue<>(); + + @Before + public void init() { + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + } + + @Test + public void testEnqueue() { + Assert.assertArrayEquals(queue.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testDequeue() { + queue.deQueue(); + queue.deQueue(); + Assert.assertArrayEquals(queue.toArray(), new String[]{"3"}); + } +} diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java new file mode 100644 index 0000000000..0b4b587704 --- /dev/null +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java @@ -0,0 +1,46 @@ +package com.aaront.execrise.generic; + +import com.aaront.exercise.generic.GenericStack; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author tonyhui + * @since 17/2/21 + */ +public class GenericStackTest { + + private GenericStack stack = new GenericStack<>(); + + @Before + public void init() { + stack.push("1"); + stack.push("2"); + stack.push("3"); + } + + @Test + public void testPush() { + Assert.assertArrayEquals(stack.toArray(), new String[]{"1", "2", "3"}); + } + + @Test + public void testPop() { + String element1 = stack.pop(); + Assert.assertEquals(element1, "3"); + String element2 = stack.pop(); + Assert.assertEquals(element2, "2"); + Assert.assertArrayEquals(stack.toArray(), new String[]{"1"}); + } + + @Test + public void testPeek() { + String element1 = stack.peek(); + Assert.assertEquals(element1, "3"); + String element2 = stack.peek(); + Assert.assertEquals(element2, "3"); + Assert.assertArrayEquals(stack.toArray(), new String[]{"1", "2", "3"}); + } + +} diff --git a/group01/group01.md b/group01/group01.md index d3f5a12faa..8b13789179 100644 --- a/group01/group01.md +++ b/group01/group01.md @@ -1 +1 @@ - + diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/ArrayUtil.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..38c0889f32 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/ArrayUtil.java @@ -0,0 +1,301 @@ +package com.github.Ven13.coding2017.array; + +public class ArrayUtil { + + /** + * һa , Ըֵû + 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] + a = [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + int originalLen = origin.length; + + int len = originalLen; + + int temp; + + for(int i = 0; i < (originalLen/2); i++){ + + temp = origin[len - i - 1]; + + origin[len - i - 1] = origin[i]; + + origin[i] = 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 int[] removeZero(int[] oldArray){ + + // 鳤ȱ + int newLength = 0; + // 鳤ֵ + for (int i = 0; i < oldArray.length; i++) { + + if(oldArray[i] != 0) { + + newLength++; + + } + + } + + // + int[] newArray = new int[newLength]; + // ± + int n = 0; + // ת + for (int i = 0; i < oldArray.length; i++) { + + if(oldArray[i] != 0) { + + newArray[n] = oldArray[i];// ת + n++;// ±ƫ + + } + + } + + //ɵ + return newArray; + + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + + int[] newArray = new int[array1.length + array2.length]; + + int k = 0; + + String inNum = ""; + + for(int i = 0; i < array1.length; i++) { + + for(int j = 0; j < array2.length; j++) { + + if (array1[i] < array2[j]) { + if (inNum.indexOf(array1[i] + "|") < 0) { + newArray[k++] = array1[i]; + inNum += array1[i] + "|"; + } + + } else if (array1[i] == array2[j]) { + if (inNum.indexOf(array1[i] + "|") < 0) { + newArray[k++] = array1[i]; + inNum += array1[i] + "|"; + } + } else { + if (i == array1.length - 1) { + if (inNum.indexOf(array1[i] + "|") < 0) { + newArray[k++] = array1[i]; + inNum += array1[i] + "|"; + } + } else { + if (inNum.indexOf(array2[j] + "|") < 0) { + newArray[k++] = array2[j]; + inNum += array2[j] + "|"; + } + } + + } + + } + + } + + return newArray; + } + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + return newArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * Сڸֵmax + * max = 23, صΪ[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + int[] newArray = new int[max]; + + int k = 0; + + boolean isN = true; + + if (max > 2) { + + for (int i = 2; i < max; i++) { + + isN = true; + + for (int j = 2; j < max; j++) { + + if (i % j == 0 && i != j) { + + isN = false; + + } + } + + if (isN) { + + newArray[k++] = i; + + } + + } + + } else if (max == 2) { + + newArray[0] = 2; + k++; + } else { + + return null; + + } + + int[] newArray2 = new int[k]; + + for(int i = 0; i < k; i ++) { + + newArray2[i] = newArray[i]; + + } + + return newArray2; + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 + * һֵmax һ飬 Сmax + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + + int i, j, k; + + int sum; + + k = 0; + + for(i = 1; i <= max; i++) { + + + sum = 0; + + for(j = 1; j < i; j++) { + + if(i % j == 0) { + + sum += j; + + } + + } + + if(i == sum) + k++; + } + + int[] newArray = new int[k]; + + k = 0; + + for(i = 1; i <= max; i++) { + + + sum = 0; + + for(j = 1; j < i; j++) { + + if(i % j == 0) { + + sum += j; + + } + + } + + if(i == sum) { + + newArray[k] = i; + + k++; + + } + + } + + + return newArray; + } + + /** + * seperator array + * array= [3,8,9], seperator = "-" + * 򷵻ֵΪ"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + String str = ""; + + for(int i = 0; i < array.length; i++) { + + str += array[i] + seperator; + + } + + str = str.substring(0, str.length() - 1); + + return str; + } + + + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/test/ArrayUtilTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/test/ArrayUtilTest.java new file mode 100644 index 0000000000..c9684fa326 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/test/ArrayUtilTest.java @@ -0,0 +1,46 @@ +package com.github.Ven13.coding2017.array.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.github.Ven13.coding2017.array.ArrayUtil; + +public class ArrayUtilTest { + + @Test + public final void testMerge() { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] a3 = {}; + a3 = arrayUtil.merge(a1, a2); + assertEquals(3, a3[0]); + assertEquals(4, a3[1]); + assertEquals(5, a3[2]); + assertEquals(6, a3[3]); + assertEquals(7, a3[4]); + assertEquals(8, a3[5]); + + } + + @Test + public final void testgetPrimes() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 23; + int[] a1 = {}; + a1 = arrayUtil.getPrimes(max); + assertEquals(3, a1.length); + assertEquals(1, a1[0]); + assertEquals(2, a1[1]); + assertEquals(3, a1[2]); + } + + @Test + public final void testgetPerfectNumbers() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 6; + int[] a1 = {}; + a1 = arrayUtil.getPerfectNumbers(max); + } +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..59ca0d34ee --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.github.Ven13.coding2017.basic; + +public class ArrayList implements List { + + //ؼϴС + private int size = 0; + + //ȸһΪ10 + Object[] elementData = new Object[100]; + + @Override + //̬Ԫ + public void add(Object o) { + //жǷ + if(size == elementData.length) { + Object[] newObjects = new Object[elementData.length * 2]; + System.arraycopy(elementData, 0, newObjects, 0, elementData.length); + elementData = newObjects; + } + + //ΪӵԪָ± + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + //жǷ + if(size == elementData.length) { + Object[] newObjects = elementData; + this.elementData = new Object[elementData.length * 2]; + for(int j = 0; j < newObjects.length; j++) { + this.elementData[j] = newObjects[j]; + } + } + + for(int i = size - 1; i >= index; i--) { + elementData[i+1] = elementData[i]; + } + + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + return elementData[index]; + } + + @Override + public Object remove(int index) { + if (index > size) { + return null; + }; + + int moveSize = size - index - 1; + + if (moveSize > 0) { + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + } + elementData[--size] = null; + + //for(int i = index; i < elementData.length; i++) { + // elementData[i] = elementData[i+1]; + //} + + return elementData; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int currentIndex = 0; + + @Override + public boolean hasNext() { + if(currentIndex >= size) return false; + else return true; + } + + @Override + public Object next() { + Object o = elementData[currentIndex]; + currentIndex ++; + return o; + } + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..354b2ba7a0 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.Ven13.coding2017.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/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..3cf6540e5e --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.Ven13.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..adf30d89c2 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/LinkedList.java @@ -0,0 +1,173 @@ +package com.github.Ven13.coding2017.basic; + +public class LinkedList implements List { + + //ʾij + private int size; + + //ͷԪ + private Node head; + //βԪ + private Node tail; + + //ʹڲʵÿһڵ㣬ÿڵһָһԪصnextԼdata + private static class Node { + public Object data; + public Node next; + + public Node(Object data) { + this.data = data; + } + } + + //Ĺ췽 + public LinkedList() { + } + + @Override + public void add(Object o) { + add(size, o); + } + + @Override + public void add(int index, Object o) { + if(index == 0) { + addFirst(o); + } else { + if(index >= size) { + addLast(o); + } else { + Node node = head; + for (int i = 1; i < index; i++) { + head = head.next; + } + Node nextNode = node.next; + Node temp = new Node(o); + node.next = temp; + temp.next = nextNode; + size++; + } + } + } + + //ǰ + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + if(tail == null) { + tail = head; + } + } + + //Ӻ + public void addLast(Object o) { + if(tail == null) { + tail = head = new Node(o); + } else { + Node newNode = new Node(o); + tail.next = newNode; + tail = tail.next; + } + size++; + } + + + @Override + public Object get(int index) { + Node node = head; + for(int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + @Override + public Object remove(int index) { + if(size == 0) { + throw new java.util.NoSuchElementException(); + } + if(index == 0) { + Node node = head; + Node temp = node.next; + head = temp; + size--; + return node.data; + } else { + if(index >= size) { + throw new java.util.NoSuchElementException(); + } else { + Node node = head; + for(int i = 1; i < index; i++) { + node = node.next; + } + Node temp = node.next; + node.next = temp.next; + size--; + return node.data; + } + } + + } + + @Override + public int size() { + return size; + } + + public Object removeFirst() { + //ͨͷָ봴ͷڵ + Node hNode = head; + if (hNode == null) { + throw new java.util.NoSuchElementException(); + } + Node nNode = hNode.next; + Object element = hNode.data; + + //Ƴ + hNode.data = null; + hNode.next = null; + head = nNode; + //жǷΪβڵ + if (nNode == null) { + tail = null; + }else { + nNode = null; + } + size --; + return element; + } + + public Object removeLast() { + return remove(size - 1); + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + + private Node node = head.next; + + @Override + public boolean hasNext() { + return node != tail; + } + + @Override + public Object next() { + + if(!hasNext()) { + throw new java.util.NoSuchElementException(); + } + Object nextData = node.data; + node = node.next; + return nextData; + } + + } + + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java new file mode 100644 index 0000000000..02e4297a33 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/List.java @@ -0,0 +1,11 @@ +package com.github.Ven13.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(); + + public Iterator iterator(); +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java new file mode 100644 index 0000000000..112299b5e9 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Queue.java @@ -0,0 +1,30 @@ +package com.github.Ven13.coding2017.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + private int size = 0; + + public void enQueue(Object o){ + size++; + list.addLast(o); + } + + public Object deQueue(){ + size--; + return list.removeFirst(); + } + + public boolean isEmpty(){ + if(size == 0) { + return true; + } else { + return false; + } + } + + public int size(){ + return size; + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java new file mode 100644 index 0000000000..c0a2658bb3 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/Stack.java @@ -0,0 +1,30 @@ +package com.github.Ven13.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + + elementData.add(o); + } + + public Object pop(){ + Object o = null; + if(elementData.size() > 0) { + o = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + } + return o; + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ArrayListTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..7f3f179f3b --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,11 @@ +package com.github.Ven13.coding2017.basic.test; + +import org.junit.Before; + +import com.github.Ven13.coding2017.basic.*; + +public class ArrayListTest extends ListTest{ + + + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/LinkedListTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..c6bc65698c --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,100 @@ +package com.github.Ven13.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.Ven13.coding2017.basic.ArrayList; +import com.github.Ven13.coding2017.basic.LinkedList; +import com.github.Ven13.coding2017.basic.List; + +public class LinkedListTest extends ListTest { + +private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + List aList = new ArrayList(); + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + //assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i = 1; i < 4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ListTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..6959da0421 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/ListTest.java @@ -0,0 +1,129 @@ +package com.github.Ven13.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.*; + +import com.github.Ven13.coding2017.basic.ArrayList; +import com.github.Ven13.coding2017.basic.Iterator; +import com.github.Ven13.coding2017.basic.List; + +public class ListTest { + + //protected static List aList; + + @Test + public void testFunctional() { + + List aList = new ArrayList(); + + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + + List aList = new ArrayList(); + + for (int i = 0; i < 100; i++) { + aList.add(i); + } + + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + + List aList = new ArrayList(); + + aList.add(1); + aList.add(2); + aList.add(3); + aList.remove(3); + assertEquals(2, aList.size()); + + } + + @Test + public void testSize() { + + List aList = new ArrayList(); + + for (int i = 0; i < 10; i++) { + aList.add(i * 2); + } + + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + + List aList = new ArrayList(); + + expectedEx.expect(Exception.class); + + aList.remove(1); + aList.add(3); + aList.add(2, 5); + } + + @Test + public void testIterator() { + + List aList = new ArrayList(); + + Iterator it = aList.iterator(); + + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/QueueTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..965610cdce --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/QueueTest.java @@ -0,0 +1,36 @@ +package com.github.Ven13.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.Ven13.coding2017.basic.Queue; + +public class QueueTest { + +private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/StackTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..9bbf41914f --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/basic/test/StackTest.java @@ -0,0 +1,40 @@ +package com.github.Ven13.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.Ven13.coding2017.basic.Stack; + +public class StackTest { + +private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/LoginAction.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..d43bc2c452 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.github.Ven13.coding2017.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 void setMessage(String message) { + this.message = message; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/Struts.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..a9040e257d --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/Struts.java @@ -0,0 +1,166 @@ +package com.github.Ven13.coding2017.litestruts; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.io.File; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + View view = new View(); + + Map map = testParseXmlData(actionName, "/struts.xml"); + + String className = (String)map.get(actionName); + + String resultName = ""; + System.out.println(className); + + try { + Class c = Class.forName(className); + Object obj = c.newInstance(); + Method method = null; + + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + PropertyDescriptor pd = new PropertyDescriptor(key, c); + Method setMethod = pd.getWriteMethod(); + setMethod.invoke(obj, value); + } + + Method exec = c.getDeclaredMethod("execute"); + Object res = exec.invoke(obj); + if(res != null) { + resultName = (String)map.get(actionName + '|' + res); + view.setJsp(resultName); + } + Field[] fields = c.getDeclaredFields(); + for(Field f : fields){ + PropertyDescriptor descriptor = new PropertyDescriptor(f.getName(), c); + Method getMethod = descriptor.getReadMethod(); + Object value = getMethod.invoke(obj); + parameters.put(f.getName(), (String)value); + } + view.setParameters(parameters); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IntrospectionException e) { + System.out.println(e.toString()); + e.printStackTrace(); + } + + + return view; + } + + public static Document parse2Document(String xmlFilePath){ + SAXReader reader = new SAXReader(); + Document doc = null; + try { + InputStream inputStream = Class.class.getResourceAsStream(xmlFilePath); + doc = reader.read(inputStream); + } catch (DocumentException e) { + e.printStackTrace(); + } + return doc; + } + + public static Map testParseXmlData(String actionName, String xmlFilePath){ + //获取xml解析器对象 + //SAXReader reader = new SAXReader(); + //将xml解析为Document对象 + Document doc = parse2Document(xmlFilePath); + //获取文档的根元素 + Element root = doc.getRootElement(); + + //定义保存属性、值的map + Map map = new HashMap(); + + + //遍历当前元素(在此是根元素)的子元素 + + for (Iterator i_pe = root.elementIterator(); i_pe.hasNext();) { + + Element e_pe = (Element) i_pe.next(); + //获取当前元素的名字 + String act = e_pe.getName(); + //获取当前元素的name和class属性的值并分别赋给attName,attClass变量 + System.out.println(act); + String attName = e_pe.attributeValue("name"); + String attClass = e_pe.attributeValue("class"); + if (attName.equals(actionName)) { + map.put(attName, attClass); + + //Element e_res = e_pe.element("result"); + //System.out.println(e_res.getName()); + for (Iterator i_res = e_pe.elementIterator(); i_res.hasNext();) { + + Element e_re = (Element) i_res.next(); + //获取当前元素的名字 + String person_n = e_re.getName(); + //获取当前元素的name和class属性的值并分别赋给attName,attClass变量 + System.out.println(person_n); + String resName = e_re.attributeValue("name"); + String resClass = e_re.getStringValue(); + map.put(attName + '|' + resName, resClass); + + } + } + } + + return map; + + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/StrutsTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a5a777be1c --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.Ven13.coding2017.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/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/View.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/View.java new file mode 100644 index 0000000000..2b0997095c --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.Ven13.coding2017.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/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/struts.xml b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..59ec89bb69 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/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/group02/106614649/106614649Learnin/src/struts.xml b/group02/106614649/106614649Learnin/src/struts.xml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..133db97491 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/ArrayList.java @@ -0,0 +1,105 @@ +package com.github.FelixCJF.coding2017.basic; + + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //容量增加 + ensureCapacity(size + 1); + //添加 + elementData[size ++] = o; + } + + public void add(int index, Object o){ + + //检查是否越界 + rangeCheck(index); + // 进行扩容检查 + ensureCapacity(size + 1); + // 对数组进行复制处理,目的就是空出index的位置插入element,并将index后的元素位移一个位置 + System. arraycopy(elementData, index, elementData, index + 1, + size - index); + // 将指定的index位置赋值为Object o + elementData[index] = o; + // 自增一位长度 + size++; + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + // 数组越界检查 + rangeCheck(index); + // 取出要删除位置的元素,供返回使用 + Object oldValue = elementData[index]; + // 计算数组要复制的数量 + int numMoved = size - index - 1; + // 数组复制,就是将index之后的元素往前移动一个位置 + if (numMoved > 0) + System. arraycopy(elementData, index+1, elementData, index, + numMoved); + // 将数组最后一个元素置空(因为删除了一个元素,然后index后面的元素都向前移动了,所以最后一个就没用了),好让gc尽快回收 + // 不要忘了size减一 + elementData[--size] = null; + return oldValue; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + //内部类,实现Iterator + private class ArrayListIterator implements Iterator{ + + private int currentIndex = 0; //当前索引 + + public boolean hasNext() { + if (currentIndex >= size) { + return false; + } + return true; + } + + public Object next() { + Object object = elementData[currentIndex]; + currentIndex ++ ; + return object; + } + } + //扩容 + public void ensureCapacity( int minCapacity) { + // 当前数组的长度 + int oldCapacity = elementData .length; + // 最小需要的容量大于当前数组的长度则进行扩容 + if (minCapacity > oldCapacity) { + // 扩容 + int newCapacity = oldCapacity + (oldCapacity >> 1); + // 如果新扩容的数组长度还是比最小需要的容量小,则以最小需要的容量为长度进行扩容 + if (newCapacity < minCapacity) + newCapacity = minCapacity; + //数组复制 + Object[] elementData2 = new Object[newCapacity]; + for (int i = 0; i < oldCapacity; i++) { + elementData2[i] = elementData[i]; + } + elementData = elementData2; + } + } + //检查是否越界 + private void rangeCheck(int index){ + if (index < 0 || index >= this.size) { + throw new IndexOutOfBoundsException("index :" + index + "size :" + size); + } + } +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/BinaryTreeNode.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6dccc25dcb --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.FelixCJF.coding2017.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; + } + +} \ No newline at end of file diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Iterator.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..3a1b9abf8c --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.github.FelixCJF.coding2017.basic; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/LinkedList.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..d86e970b8a --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/LinkedList.java @@ -0,0 +1,214 @@ +package com.github.FelixCJF.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head;//头指针 + private Node last;//尾指针 + private int size = 0; + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + //检查是否越界 + checkIndex(index); + + Node indexNode = node(index); + + if (index == size) { + addLast(o); + } else { + final Node pred = indexNode.prv; + + final Node newNode = new Node(); + newNode.data = o; + newNode.next = indexNode; + newNode.prv = pred; + + indexNode.prv = newNode; + + if (pred == null) { + head = newNode; + } else { + pred.next = newNode; + } + } + size ++; + } + public Object get(int index){ + //检查是否越界 + checkIndex(index); + + Node indexNode = node(index); + + return indexNode.data; + } + public Object remove(int index){ + //检查是否越界 + checkIndex(index); + + Node indexNode = node(index); + Object element = indexNode.data; + Node pre = indexNode.prv; + Node next = indexNode.next; + + if (pre == null) { + head = next; + } else { + pre.next = next; + indexNode.prv = null; + } + + if (next == null) { + last = pre; + } else { + next.prv = pre; + indexNode.next = null; + } + + indexNode.data = null; + + size --; + + return element; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + //节点变量存放原来的头指针 + final Node oldHead = head; + //创建新的节点对象 + final Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + newNode.prv = null; + //判断oldhead是否为null + if (oldHead == null) { + last = newNode; + }else { + //头指针指向新创建的节点对象 + oldHead.prv = newNode; + } + //将newNode变为头指针 + head = newNode; + size ++; + } + public void addLast(Object o){ + //节点新变量放原先的尾指针 + final Node oldLast = last; + //创建新节点,加入要添加的对象 + final Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + newNode.prv = oldLast; + if (oldLast == null) { + head = newNode; + } else { + //尾指针指向新创建的节点 + oldLast.next = newNode; + } + //newNode变为尾指针 + last = newNode; + size++; + } + public Object removeFirst(){ + //通过头指针创建头节点 + final Node hNode = head; + if (hNode == null) { + throw new NoSuchElementException(); + } + final Node next = hNode.next; + final Object element = hNode.data; + + //移除 + hNode.data = null; + hNode.next = null; + head = next; + //判断是否为尾节点 + if (next == null) { + last = null; + }else { + next.prv = null; + } + size --; + return element; + } + public Object removeLast(){ + //通过尾指针创建节点 + final Node lastNode = last; + if (lastNode == null) { + throw new NoSuchElementException(); + } + final Object element = lastNode.data; + final Node prve = lastNode.prv; + + //移除 + lastNode.data = null; + lastNode.prv = null; + last = prve; + + if (prve == null) { + head = null; + } else { + prve.next = null; + } + size --; + return element; + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + + private Node currentNode = head;//当前节点 + + public boolean hasNext() { + if (currentNode == null) { + return false; + } + return true; + } + + public Object next() { + Object element = currentNode.data; + currentNode = currentNode.next; + return element; + } + + } + + //查找index节点,并返回该节点 + Node node(int index) { + // assert isElementIndex(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.prv; + return x; + } + } + //检查索引 + private void checkIndex(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } + private static class Node{ + Object data; + Node next; + Node prv; + } +} \ No newline at end of file diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/List.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/List.java new file mode 100644 index 0000000000..ecbb657597 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/List.java @@ -0,0 +1,11 @@ +package com.github.FelixCJF.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(); + public Iterator iterator(); +} \ No newline at end of file diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Queue.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Queue.java new file mode 100644 index 0000000000..c2661ce35f --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Queue.java @@ -0,0 +1,53 @@ +package com.github.FelixCJF.coding2017.basic; + + +public class Queue { + + private Node head;//头节点 + private Node last;//尾节点 + private int size;//记录节点 + + public void enQueue(Object o){ + //设置一个节点变量存放原先的尾节点 + final Node oldLast = last; + //创建一个新的节点 + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + //添加到队列 + if (isEmpty()) { + head = newNode; + } else { + oldLast.next = newNode; + } + //新节点变为尾节点 + last = newNode; + size ++; + } + + public Object deQueue(){ + + Object object = head.data; + + head = head.next; + + if (isEmpty()) { + last = null; + } + size --; + return object; + } + + public boolean isEmpty(){ + return head == null; + } + + public int size(){ + return size; + } + + private static class Node{ + Object data; + Node next; + } +} \ No newline at end of file diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java new file mode 100644 index 0000000000..16684f7d92 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/Stack.java @@ -0,0 +1,34 @@ +package com.github.FelixCJF.coding2017.basic; + +import java.util.EmptyStackException; + +public class Stack { + + //存放栈内元素的容器 + private ArrayList elementData = new ArrayList(); + //记录栈内元素个数 + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..9484ce1527 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,14 @@ +package com.github.FelixCJF.coding2017.basic.test; + +import org.junit.Before; + +import com.github.FelixCJF.coding2017.basic.ArrayList; + +public class ArrayListTest extends ListTest { + +/* @Before + public void setUpArrayList() { + aList = new ArrayList(); + }*/ + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..ce0c0d1c0d --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,100 @@ +package com.github.FelixCJF.coding2017.basic.test; + + + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.FelixCJF.coding2017.basic.ArrayList; +import com.github.FelixCJF.coding2017.basic.LinkedList; +import com.github.FelixCJF.coding2017.basic.List; + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + List aList = new ArrayList(); + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..b970372bbe --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/ListTest.java @@ -0,0 +1,124 @@ +package com.github.FelixCJF.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.FelixCJF.coding2017.basic.ArrayList; +import com.github.FelixCJF.coding2017.basic.Iterator; +import com.github.FelixCJF.coding2017.basic.List; + +public class ListTest { + + + //protected static List aList = new ArrayList(); + + @Test + public void testFunctional() { + List aList = new ArrayList(); + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + List aList = new ArrayList(); + for (int i=0; i<100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + List aList = new ArrayList(); + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + List aList = new ArrayList(); + for (int i=0; i<10; i++) + aList.add(i*2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + List aList = new ArrayList(); + expectedEx.expect(Exception.class); + + aList.remove(1); + aList.add(3); + aList.add(2, 5); + expectedEx.expect(Exception.class); + } + + @Test + public void testIterator() { + List aList = new ArrayList(); + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/QueueTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..49506c2b35 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/QueueTest.java @@ -0,0 +1,34 @@ +package com.github.FelixCJF.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +import com.github.FelixCJF.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/StackTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..6bb53571a5 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/basic/test/StackTest.java @@ -0,0 +1,41 @@ +package com.github.FelixCJF.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.FelixCJF.coding2017.basic.Stack; + + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f60be05977 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,228 @@ +package com.github.FelixCJF.coding2017.coderising.array; + +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int array[] = new int[origin.length]; + for (int i = 0; i newArr[i+1]) { + int temp = newArr[i]; + newArr[i] = newArr[i+1]; + newArr[i + 1] = temp; + } + } + return newArr; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int newArr[] = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] newArr; + int f1 = 0; + int f2 = 1; + int f = 0; + if (max < 2) { + return newArr = new int[0]; + } + ArrayList list = new ArrayList(); + for (int i = 2; f < max; i++) { + list.add(f2); + f = f1 + f2; + f1 = f2; + f2 = f; + } + newArr = new int[list.size()]; + for (int i = 0; i < newArr.length; i++) { + newArr[i] = (int) list.get(i); + } + return newArr; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + ArrayList list = new ArrayList(); + + for (int i = 1; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + int[] newArr = new int[list.size()]; + for (int i = 0; i < newArr.length; i++) { + newArr[i] = (int) list.get(i); + } + return newArr; + } + //判断是否为素数 + private boolean isPrime(int a) { + + boolean flag = true; + + if (a < 2) {// 素数不小于2 + return false; + } else { + + for (int i = 2; i <= Math.sqrt(a); i++) { + + if (a % i == 0) {// 若能被整除,则说明不是素数,返回false + + flag = false; + break;// 跳出循环 + } + } + } + return flag; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] newArr; + if (max == 0) { + return newArr = new int[0]; + } + ArrayList list = new ArrayList(); + for (int i = 1; i < max; i++) { + if (isWanshu(i)) { + list.add(i); + } + } + newArr = new int[list.size()]; + for (int i = 0; i < newArr.length; i++) { + newArr[i] = (int) list.get(i); + } + return newArr; + } + //判断一个数是不是完数 + private boolean isWanshu(int n) + { + boolean flag=false; + int i,sum=0; + for(i=1;i<=n/2;i++) + { + if(n%i==0) + { + sum+=i; + } + } + if(sum==n) + { + flag=true; + } + return flag; + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String string = ""; + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + string += array[i]; + } else { + string += array[i] + seperator; + } + } + return string; + } + + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java new file mode 100644 index 0000000000..378e052164 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java @@ -0,0 +1,148 @@ +package com.github.FelixCJF.coding2017.coderising.array.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.FelixCJF.coding2017.coderising.array.ArrayUtil; + + +public class ArrayUtilTest +{ + private ArrayUtil myArray; + @Before + public void setUp() throws Exception + { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() + { + int[] a = {1, 2, 1, 3, 5, 6}; + int[] b = {6, 5, 3, 1, 2, 1}; + int[] c = new int[0]; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + //对空数组进行反转 + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + } + + @Test + public void testRemoveZero() + { + int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5}; + int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5}; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = new int[0]; + assertArrayEquals(d, new int[0]); + } + + @Test + public void testMerge() + { + int a1[] = {1, 2, 3, 4, 5}; + int b1[] = {3, 4, 5, 6, 7, 8}; + int c1[] = {1, 2, 3, 4, 5, 6, 7, 8}; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = {0, 2, 3, 6, 7, 8}; + int c2[] = {0, 2, 3, 6, 7, 8}; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = {0, 2, 3, 6, 7, 8}; + int b3[] = new int[0]; + int c3[] = {0, 2, 3, 6, 7, 8}; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = new int[0]; + int[] b4 = new int[0]; + int[] newArray4 = myArray.merge(a4, b4); + assertArrayEquals(new int[0], newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + @Test + public void testGrow() + { + int[] a = {3, 5, 7, 8, 9}; + int[] b = {3, 5, 7, 8, 9, 0, 0, 0}; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + + // size < 0 抛出异常 + expectedEx.expect(Exception.class); + int[] newArray1 = myArray.grow(a, -3); + assertArrayEquals(b, newArray1); + + } + + @Test + public void testFibonacci() + { + //max == 1时返回空数组 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + + int[] array2= myArray.fibonacci(35); + int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() + { + int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + //max <= 2的时候没有素数,数组为空数组 new int[0] + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() + { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = {6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() + { + int[] Array0 = {3, 5, 7, 8, 9}; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = {3}; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + //传递空数组时,返回空数组 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/LoginAction.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/View.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/struts.xml b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..03f6710788 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyArrayList.java @@ -0,0 +1,83 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyArrayList { + + private Object[] initialArray = {}; + private Object[] dataArray; + private int initSize = 10; + private int arraySize; + public MyArrayList() { + dataArray = initialArray; + } + + public MyArrayList(int init) { + dataArray = new Object[init]; + } + + public void ensureCapacity(int newCapacity) { + if (newCapacity < arraySize) + return; + + Object[] old = dataArray; + dataArray = new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + dataArray[i] = old[i]; + } + } + + public void add(T element) { + add(size(), element); + } + + public void add(int index, T element) { + if (size() == dataArray.length) { + ensureCapacity(size()*2 + 1); + } + for(int i=arraySize;i>index;i--) { + dataArray[i] = dataArray[i - 1]; + } + dataArray[index] = element; + arraySize++; + } + + public T delete(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T removeElement = (T)dataArray[index]; + for (int i = index; i < size() -1; i++) { + dataArray[i] = dataArray[i + 1]; + } + arraySize--; + return removeElement; + } + + public T get(int index) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + return (T)dataArray[index]; + } + + public T set(int index, T newElement) { + if (index < 0 || index > arraySize) { + throw new ArrayIndexOutOfBoundsException(); + } + T oldElement = (T) dataArray[index]; + dataArray[index] = newElement; + + return oldElement; + } + + public int size() { + return arraySize; + } + + public boolean isEmpty() { + return size() == 0; + } + +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java new file mode 100644 index 0000000000..9e7eab3401 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyLinkedList.java @@ -0,0 +1,119 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/18. + */ +public class MyLinkedList { + private class Node { + public Node pre; + public Node next; + public T data; + + public Node(Node pre,Node next,T data) { + this.pre = pre; + this.next = next; + this.data = data; + } + } + + private int dataSize; + + private Node head; + private Node tail; + + public MyLinkedList() { + head = new Node(null,null,null); + tail = new Node(head, null, null); + head.next = tail; + dataSize = 0; + } + + public void add(T t) { +// add(size(), t); + Node newNode = new Node<>(null, tail, t); + newNode.pre = tail.pre; + tail.pre.next = newNode; + tail.pre = newNode; + dataSize++; + + } + + /** + * 根据索引添加没有实现 + * @param index + * @param element + */ + public void add(int index,T element) { + //TODO 根据索引添加元素 +// addBefore(getNode(index,0,size()-1),element); +// if (index == dataSize) { +// add(element); +// } else { + // +// } + } + + public T get(int index) { + return getNode(index).data; + } + + public T set(int index, T newValue) { + Node node = getNode(index); + T oldData = node.data; + node.data = newValue; + return oldData; + } + + public T remove(int index) { + Node node = getNode(index); + node.next.pre = node.pre; + node.pre.next = node.next; + dataSize--; + + return node.data; + + } + + private void addBefore(Node node, T element) { +// newNode.pre.next = newNode; +// node.pre = newNode; + Node pre = node.pre; + Node newNode = new Node<>(node.pre, node, element); + node.pre = newNode; + pre.next = newNode; + + dataSize++; + } + + private Node getNode(int index) { + return getNode(index, 0, size()); + } + + private Node getNode(int index, int lower, int upper) { + Node p; + if (index < lower || index > upper) { + throw new IndexOutOfBoundsException(); + } + + if (index < size() / 2) { + p = head.next; + for (int i = 0; i < index; i++) { + p = p.next; + } + } else { + p = tail.pre; + for (int i = size()-1; i > index; i--) { + p = p.pre; + } + } + return p; + } + + public int size() { + return dataSize; + } + + public boolean isEmpty() { + return size() == 0; + } +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java new file mode 100644 index 0000000000..6d0c970b65 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyQueue.java @@ -0,0 +1,29 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyQueue { + private MyLinkedList link = new MyLinkedList<>(); + + public void enQueue(T t) { + link.add(t); + } + + public T deQueue() { + if (size() <= 0) { + return null; + } + T t = link.get(0); + link.remove(0); + return t; + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return link.size(); + } +} diff --git a/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java new file mode 100644 index 0000000000..3fd9d2f5c2 --- /dev/null +++ b/group02/435994736/src/main/java/com/github/lhpmatlab/coding2017/basic/MyStack.java @@ -0,0 +1,30 @@ +package com.github.lhpmatlab.coding2017.basic; + +/** + * Created by andy on 2017/2/22. + */ +public class MyStack { + private MyArrayList list = new MyArrayList<>(); + + public void push(T t) { + list.add(t); + } + + public T pop() { + if (size() <= 0) { + throw new IndexOutOfBoundsException(); + } + return list.delete(size() - 1); + } + + public T peek() { + return list.get(size() - 1); + } + + public boolean isEmpty() { + return list.size() == 0; + } + public int size() { + return list.size(); + } +} diff --git a/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java new file mode 100644 index 0000000000..e29d41feac --- /dev/null +++ b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyArrayListTest.java @@ -0,0 +1,113 @@ +package com.github.lhpmatlab.coding2017.basic; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class MyArrayListTest { + private MyArrayList list; + + @Before + public void init(){ + list = new MyArrayList<>(); + } + + @Test + public void testEnsureCapacity() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.ensureCapacity(10); + assertEquals("ensureCapacity size is 10 ", list.size(),1); + } + + /** + * 在列表的末尾添加元素 + */ + @Test + public void testAddT() { + assertEquals("init list size is 0 ", list.size(), 0); + list.add("1"); + list.add("2"); + assertEquals("add list size ", list.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyLinkedListTest { + private MyLinkedList linkedList; + + @Before + public void before() throws Exception { + linkedList = new MyLinkedList<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: add(T t) + * + */ + @Test + public void testAddT() throws Exception { + assertEquals("init list size is 0 ", linkedList.size(), 0); + linkedList.add("1"); + linkedList.add("2"); + assertEquals("add list size ", linkedList.size(), 2); + for (int i=0; i +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyQueueTest { + private MyQueue queue; + + @Before + public void init() throws Exception { + queue = new MyQueue<>(); + } + + /** + * + * Method: enQueue(T t) + * + */ + @Test + public void testEnQueue() throws Exception { + queue.enQueue("1"); + assertEquals("size ", queue.size(), 1); + } + + /** + * + * Method: deQueue() + * + */ + @Test + public void testDeQueue() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); +// queue.deQueue(); + assertEquals("dequeue element ",queue.deQueue(),"1"); + assertEquals("size ", queue.size(), 1); + + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("isEmpty method",queue.isEmpty(),true); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + queue.enQueue("1"); + queue.enQueue("2"); + assertEquals("size method", queue.size(),2); + } + + +} diff --git a/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java new file mode 100644 index 0000000000..a90af5d720 --- /dev/null +++ b/group02/435994736/src/test/java/com/github/lhpmatlab/coding2017/basic/MyStackTest.java @@ -0,0 +1,104 @@ +package com.github.lhpmatlab.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.*; + +/** +* MyStack Tester. +* +* @author +* @since
���� 26, 2017
+* @version 1.0 +*/ +public class MyStackTest { + + MyStack stack; + + + @Before + public void init() throws Exception { + stack = new MyStack<>(); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: push(T t) + * + */ + @Test + public void testPush() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + assertEquals("pust stack ", stack.size(),1); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + stack.pop(); + assertEquals("after pop ",stack.size(),1); + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("peek ", stack.peek(),"2"); + } + + /** + *测试判空方法 + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + assertEquals("stack is empty ", stack.isEmpty(), true); + } + + /** + *测试判空方法,不为空的情况 + * Method: isEmpty() + * + */ + @Test + public void testIsNotEmpty() throws Exception { + stack.push("1"); + assertEquals("stack is empty ", stack.isEmpty(), false); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + assertEquals("init stack ", stack.size(), 0); + stack.push("1"); + stack.push("2"); + assertEquals("size is 2", stack.size(), 2); + } + + +} diff --git a/group02/527705641/.gitignore b/group02/527705641/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group02/527705641/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java b/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java new file mode 100644 index 0000000000..d4e62075d9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java @@ -0,0 +1,218 @@ +package com.github.fei9009.coderising0226.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import com.sun.org.apache.bcel.internal.generic.ISTORE; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int i = 0, j = origin.length-1; + while(i < j){ + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + i++; + j--; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + if (oldArray == null || oldArray.length == 0){ + return oldArray; + } + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + count++; + } + } + int[] newArray = new int[count]; + int j = 0; + for (int i = 0; i <= oldArray.length - 1; i++) { + if (oldArray[i] == 0) { + continue; + } + newArray[j++] = oldArray[i]; + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + if (array1 == null && array2 == null) { + return null; + } + if (array1 == null) { + return array2; + } + if (array2 == null) { + return array1; + } + int i=0, j=0, k=0; + int len1 = array1.length; + int len2 = array2.length; + int[] mergeArr = new int[len1+len2]; + while(true){ + if(i == len1 || j == len2) + break; + if(array1[i]array2[j]){ + mergeArr[k++] = array2[j++]; + }else{ + mergeArr[k++] = array1[i++]; + j++; + } + } + + for(;i list = new ArrayList<>(); + int f1 = 1, f2 = 1, f3; + list.add(f1); + list.add(f2); + while (f1 +f2 < max) { + f3 = f1 + f2; + list.add(f3); + f1 = f2; + f2 = f3; + } + int[] result = new int[list.size()]; + int j = 0; + for(Integer i : list) { + result[j++] = i; + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] a = new int[max]; + int k=0; + for (int z = 1; ztype){ + try { + Method met = obj.getClass().getMethod("set" + initStr(att), type); + met.invoke(obj, value); + }catch (Exception e){ + e.printStackTrace(); + } + } + + private static String getter(Object obj, String att){ + try { + Method met = obj.getClass().getMethod("get" + initStr(att)); + return (String)met.invoke(obj); + }catch (Exception e){ + e.printStackTrace(); + } + return null; + } + private static String initStr(String name) { + name = name.substring(0, 1).toUpperCase() + name.substring(1); + return name; + } + + private static String toLowerString(String name) { + name = name.substring(0, 1).toLowerCase() + name.substring(1); + return name; + } + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + SAXReader saxReader = new SAXReader(); + try { + Document document = saxReader.read(new File("struts.xml")); + Element root = document.getRootElement(); + + + boolean flag = false; + String className = ""; + Element action = null; + for (Iterator iter = root.elementIterator(); iter.hasNext();) + { + Element e = (Element) iter.next(); + if(e.attributeValue("name").equals(actionName)){ + flag = true; + className = e.attributeValue("class"); + action = e; + break; + } + } + if(!flag) + throw new Exception(actionName+"未定义"); + + Class clz = Class.forName(className); + Constructor c = clz.getConstructor(); + Object obj = c.newInstance(); + + for (String in : parameters.keySet()) { + setter(obj,in,parameters.get(in), String.class); + } + Method exc = clz.getDeclaredMethod("execute"); + String res = (String)exc.invoke(obj); + + //获取所有getter方法 + //Get the methods + Method[] methods = clz.getDeclaredMethods(); + //Loop through the methods and print out their names + Map params = new HashMap(); + + for (Method method : methods) { + String name = method.getName(); + if(name.substring(0,3).equals("get")){ + params.put(toLowerString(name.substring(3)),getter(obj,toLowerString(name.substring(3)))); + } + } + View view = new View(); + view.setParameters(params); + //step 4 + flag = false; + Element result = null; + List actionChildList = action.elements("result"); + for (Iterator iter = action.elementIterator(); iter.hasNext();){ + Element e = (Element) iter.next(); + if(e.attributeValue("name").equals(res)){ + flag = true; + result = e; + break; + } + } + if(!flag) + throw new Exception(res+"undefined"); + view.setJsp(result.getText()); + return view; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java new file mode 100644 index 0000000000..c5ee5f10a9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.fei9009.coderising0226.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/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java new file mode 100644 index 0000000000..9332493c42 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.fei9009.coderising0226.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/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/struts.xml b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/struts.xml new file mode 100644 index 0000000000..68098b57e3 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/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/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..855e25b257 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.github.fei9009.coding2017.basic; + +public class ArrayList implements List { + + private int size = 0; + private int capacity; + + private Object[] elementData = new Object[100]; + + public ArrayList() { + this.capacity = 20; + } + + private void extend() { + Object[] updatedElementData = new Object[this.elementData.length + this.capacity]; + System.arraycopy(this.elementData, 0, updatedElementData, 0, this.elementData.length); + this.elementData = updatedElementData; + } + + public void add(Object o){ + if (this.size == elementData.length) { + extend(); + } + elementData[size] = o; + this.size++; + } + + public void add(int index, Object o){ + if (this.size == elementData.length) { + extend(); + } + int i; + for (i = this.size - 1; i >= index; i--) { + this.elementData[i + 1] = this.elementData[i]; + } + this.elementData[i + 1] = o; + this.size++; + } + + public Object get(int index){ + if (index >= 0 && index < this.size) { + return this.elementData[index]; + }else { + return null; + } + } + + public Object remove(int index){ + if (index >= 0 && index < this.size) { + int i = 0; + Object deletedElement = this.elementData[index]; + for (i = index + 1; i < this.size; i++) { + this.elementData[i - 1] = this.elementData[i]; + } + this.elementData[i] = null; + this.size--; + return deletedElement; + }else { + return null; + } + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..d623dd05e0 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/ArrayListTest.java @@ -0,0 +1,55 @@ +package com.github.fei9009.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + private static ArrayList testArray = new ArrayList(); + @Before + public void setUp() throws Exception { + //testArray.clear(); + } + + @Test + public void testArrayList() { + //fail("Not yet implemented"); + } + + @Test + public void testAddObject() { + testArray.add(10); + assertEquals(10, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testAddIntObject() { + testArray.add(10); + testArray.add(0, 3); + testArray.add(0, 2); + assertEquals(3, testArray.get(1)); + assertEquals(2, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testGet() { + testArray.add(10); + assertEquals(10, testArray.get(0)); + //fail("Not yet implemented"); + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6dbc7ee012 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,57 @@ +package com.github.fei9009.coding2017.basic; + +public class BinaryTreeNode > { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object o) { + data = o; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = new BinaryTreeNode(o); + boolean left = true; + BinaryTreeNode cur = this; + BinaryTreeNode pre = null; + while (cur != null) { + pre = cur; + if (node.getData().compareTo(cur.getData()) <= 0) { + cur = cur.left; + left = true; + } else { + cur = cur.right; + left = false; + } + } + if(left) { + pre.left = node; + } else { + pre.right = node; + } + return node; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..e2f03b8247 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.fei9009.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..1ed5923ee9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coding2017/basic/LinkedList.java @@ -0,0 +1,91 @@ +package com.github.fei9009.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + add(size, o); + } + + public void add(int index , Object o){ + if (index > this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node dummy = node(index); + Node newNode = new Node(o); + newNode.next = dummy; + if (index == 0) { + head = newNode; + } + else { + Node before = node(index-1); + before.next = newNode; + } + this.size++; + + } + + public Object get(int index){ + if (index >= this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node node = node(index); + return node.data; + + } + public Object remove(int index){ + if (index >= this.size || index < 0) { + throw new IndexOutOfBoundsException("index " + index + "beyond the size " + size ); + } + Node delNode = node(index); + if (index == 0) + head = delNode.next; + else { + Node before = node(index-1); + before.next = delNode.next; + } + size--; + return delNode.data; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + add(0, o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(this.size -1); + } + public Iterator iterator(){ + return null; + } + + Node node(int index) { + Node x = head; + for (int i=0; i + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group02/562768642/src/com/github/orajavac/coding2017/array/ArrayUtil.java b/group02/562768642/src/com/github/orajavac/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..62de048a68 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/array/ArrayUtil.java @@ -0,0 +1,234 @@ +package com.github.orajavac.coding2017.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int temp; + int c=origin.length; + for (int i=0,j=origin.length-1;itarget[j+1]){ + t=target[j]; + target[j]=target[j+1]; + target[j+1]=t; + } + } + return removeZero(target); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] target = new int[size]; + for (int i=0;ielementData.length) + grow(elementData,size); + Object temp = null; + int len=elementData.length; + for (int i=0;iindex){ + temp=elementData[i]; + elementData[i]=old; //[4]=c + old=temp; + } + } + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + if (index==elementData.length-1){ //删除最后一个索引元素里的值 + elementData[index]=null; + }else{ + elementData[index]=null; + int len=elementData.length; + for (int i=0;iindex){ + if(i+1!=len){ + elementData[i]=elementData[i+1]; + }else{ //我们假设数组索引 0-3,那么数组长度是4,3+1==4,elementData[i+1]会报错 + elementData[i]=null; + } + } + } + } + return null; + } + + public int size(){ + size=0; + for (int i=0;i "); + c=c.next; + } + System.out.println(); + } + public Iterator iterator(){ + LinkedList l = new LinkedList(); + l.head=this.head; + return l; + } + + public boolean hasNext(){ + current = head; + if (current!=null){ + head = current.next; + return true; + } + return false; + } + + public Object next(){ + return current.data; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/List.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/List.java new file mode 100644 index 0000000000..129333b4da --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.orajavac.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/group02/562768642/src/com/github/orajavac/coding2017/basic/Queue.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/Queue.java new file mode 100644 index 0000000000..772806e391 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/Queue.java @@ -0,0 +1,34 @@ +package com.github.orajavac.coding2017.basic; + +public class Queue { + +private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + Object obj = elementData.get(0); + elementData.remove(0); + return obj; + } + + public ArrayList getElementData() { + return elementData; + } + + public void setElementData(ArrayList elementData) { + this.elementData = elementData; + } + + public boolean isEmpty(){ + if (elementData.size()>0) + return true; + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/Stack.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/Stack.java new file mode 100644 index 0000000000..62d9f2e278 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/Stack.java @@ -0,0 +1,49 @@ +package com.github.orajavac.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int s=elementData.size(); + Object obj = null; + for (int i=s-1;i>=0;i--){ + if(elementData.get(i)!=null){ + obj = elementData.get(i); + elementData.remove(i); + break; + } + } + return obj; + } + + public Object peek(){ + int s=elementData.size(); + Object obj = null; + for (int i=s-1;i>=0;i--){ + if(elementData.get(i)!=null){ + obj = elementData.get(i); + break; + } + } + return obj; + } + public boolean isEmpty(){ + if (elementData.size()>0) + return true; + return false; + } + public int size(){ + return elementData.size(); + } + public ArrayList getElementData() { + return elementData; + } + + public void setElementData(ArrayList elementData) { + this.elementData = elementData; + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/litestructs/LoginAction.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/LoginAction.java new file mode 100644 index 0000000000..7c16390ef1 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/LoginAction.java @@ -0,0 +1,43 @@ +package com.github.orajavac.coding2017.litestructs; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public void setMessage(String message) { + this.message = 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/group02/562768642/src/com/github/orajavac/coding2017/litestructs/Struts.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/Struts.java new file mode 100644 index 0000000000..a5ea0cd702 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/Struts.java @@ -0,0 +1,99 @@ +package com.github.orajavac.coding2017.litestructs; + +import java.beans.PropertyDescriptor; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.List; +import java.util.Iterator; + +import org.dom4j.Attribute; +import org.dom4j.Element; +import org.dom4j.Document; +import org.dom4j.io.SAXReader; + + +public class Struts { + + private static Map xmlMap = new HashMap(); + + public static View runAction(String actionName, Map parameters) { + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + try{ + if(xmlMap.size()==0) + Struts.DOM4jParserXml(); + + StrutsXml sx = xmlMap.get(actionName); + Class clz = Class.forName(sx.getClassz()); + Object o = clz.newInstance(); + String key = null; + Method[] mets = clz.getDeclaredMethods(); + for (Method m : mets){ + if(m.getName().startsWith("set")){ + key = m.getName().substring(3,m.getName().length()).toLowerCase(); + m.invoke(o,parameters.get(key)); + } + } + Method m1 = clz.getDeclaredMethod("execute"); + String result = (String)m1.invoke(o); + Map param = new HashMap(); + Field[] fields = clz.getDeclaredFields(); + for(int i=0;i iterator = root.elementIterator(); + while(iterator.hasNext()){ + Element e = iterator.next(); + StrutsXml sx = new StrutsXml(); + sx.setName(e.attribute("name").getValue()); + sx.setClassz(e.attribute("class").getValue()); + xmlMap.put(e.attribute("name").getValue(), sx); + Iterator r = e.elementIterator("result"); + while(r.hasNext()){ + Element child = r.next(); + sx.getResult().put(child.attribute("name").getValue(), child.getTextTrim()); + } + } + }catch(Exception e){ + e.printStackTrace(); + } + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsTest.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsTest.java new file mode 100644 index 0000000000..447dc3a24b --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.orajavac.coding2017.litestructs; + +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/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsXml.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsXml.java new file mode 100644 index 0000000000..b22eb3ba05 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsXml.java @@ -0,0 +1,28 @@ +package com.github.orajavac.coding2017.litestructs; + +import java.util.HashMap; +import java.util.Map; + +public class StrutsXml { + private String name; + private String classz; + private Map result = new HashMap(); + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClassz() { + return classz; + } + public void setClassz(String classz) { + this.classz = classz; + } + public Map getResult() { + return result; + } + public void setResult(Map result) { + this.result = result; + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/litestructs/View.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/View.java new file mode 100644 index 0000000000..49c5feeba3 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/View.java @@ -0,0 +1,23 @@ +package com.github.orajavac.coding2017.litestructs; + +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/group02/562768642/src/com/github/orajavac/coding2017/litestructs/struts.xml b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/struts.xml new file mode 100644 index 0000000000..6e5022fb64 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/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/group02/609990377/DataStructure/.gitignore b/group02/609990377/DataStructure/.gitignore new file mode 100644 index 0000000000..fa968c2f2b --- /dev/null +++ b/group02/609990377/DataStructure/.gitignore @@ -0,0 +1,3 @@ +/bin/ +.classpath +.project diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayUtil.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayUtil.java new file mode 100644 index 0000000000..2e9c09effc --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayUtil.java @@ -0,0 +1,294 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.Arrays; +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + + int[] copy = origin.clone(); + int j, last = copy.length - 1; + + for (int i = last; i >= 0; i--) { + j = last - i; + origin[i] = copy[j]; + } + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + /* + * Method 1, traverse oldArray and count non-zero value, create new + * int[count], traverse again oldArray and insert to new one + */ + + /* + * Method 2, traverse olArray and insert non-zero value to an List, + * then List.toArray(). + */ + + // Method 3 + if(oldArray == null){ + return null; + } + int[] tmp = new int[oldArray.length]; + int count = 0; + + for (int v : oldArray) { + if (v != 0) { + tmp[count] = v; + count++; + } + } + + return Arrays.copyOf(tmp, count); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + if (array1 == null) { + if (array2 == null) { + return null; + } + return array2; + } else if (array2 == null) { + return array1; + } + + int l1 = array1.length, l2 = array2.length; + int[] whole = new int[l1 + l2]; + + int dupValue = 0; + int dupCount = 0; + boolean isDup = false; + + int j = 0; + // Traverse array1 + for (int i = 0; i < l1; i++) { + + // Get rid of duplicate value in array1 + if (isDup) { + if (array1[i] == dupValue) { + dupCount++; + continue; + } else { + isDup = false; + } + } + + while (j < l2) { + if (array1[i] > array2[j]) { + // If int from array1 is larger, add int from array2 first + whole[i + j - dupCount] = array2[j]; + j++; + continue; + } else if (array1[i] == array2[j]) { + // If equals, skip int from array2, and set duplicate value + isDup = true; + dupValue = array1[i]; + dupCount++; + j++; + continue; + } else if (array1[i] < array2[j]) { + // If smaller, break and add from in array1 + break; + } + } + whole[i + j - dupCount] = array1[i]; + } + + // Deal with left over in array2 + while (j < l2) { + whole[l1 + j - dupCount] = array2[j]; + j++; + } + + return Arrays.copyOf(whole, l1 + l2 - dupCount); + } + + /** + * 把一个已经存满数据的数组 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) { + checkGrowSize(size); + if (oldArray == null) { + return null; + } + int newlength = oldArray.length + size; + int[] res = new int[newlength]; + res = Arrays.copyOf(oldArray, newlength); + + return res; + } + + private void checkGrowSize(int size) { + if (size < 0) { + throw new IndexOutOfBoundsException( + "Negative size is not allowed in grow()"); + } + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + + // Fib(47) == 2971215073 > INT_MAX; + int[] tmp = new int[46]; + tmp[0] = 1; + tmp[1] = 1; + + int next = 1 + 1; + int i = 1; + while (next < max) { + i++; + tmp[i] = next; + next = tmp[i] + tmp[i - 1]; + } + + return Arrays.copyOf(tmp, i + 1); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max <= 2) { + return new int[0]; + } + + ArrayList primeList = new ArrayList(); + primeList.add(2); + + for (int candidate = 3; candidate < max; candidate += 2) { + // For every number smaller than max + int ceiling = (int) Math.floor(Math.sqrt(candidate)); + for (Integer prime : primeList) { + // Divided by every prime number smaller than sqrt(candidate) + if (candidate % prime == 0) { + // When reminder equals 0, candidate is not a prime + break; + } + if (prime > ceiling) { + // When every prime number <= sqrt(candidate), add candidate to primelist + primeList.add(candidate); + break; + } + } + } + + // Transfer primelist to int array + int[] res = arrayFromList(primeList); + + return res; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max <= 2) { + return new int[0]; + } + + ArrayList perfectList = new ArrayList(); + + for (int candidate = 2; candidate < max; candidate++) { + int sum = 1; + int ceiling = (int) Math.floor(Math.sqrt(candidate)); + if (Math.sqrt(candidate) == ceiling) { + sum += ceiling; + } + // For each number smaller than max + for (int divisor = 2; divisor <= ceiling; divisor++) { + if (candidate % divisor == 0) { + sum += divisor; + sum += candidate / divisor; + } + if (sum > candidate) { + break; + } + } + if (sum == candidate) { + perfectList.add(candidate); + } + } + + // Transfer primelist to int array + int[] res = arrayFromList(perfectList); + + return res; + } + + private int[] arrayFromList(ArrayList list) { + int[] r = new int[list.size()]; + int j = 0; + for (Integer v : list) { + r[j++] = v; + } + return r; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String res = ""; + if (array == null || array.length == 0) { + return res; + } + res += array[0]; + for (int i = 1; i < array.length; i++) { + res += seperator + array[i]; + } + return res; + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WArrayList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WArrayList.java new file mode 100644 index 0000000000..4aa718017a --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WArrayList.java @@ -0,0 +1,113 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.NoSuchElementException; + +public class WArrayList implements WList { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + + //check size limit + if(size + 1 > elementData.length){ + int newlength = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newlength); + } + + elementData[size++] = o; + } + + public void add(int index, Object o){ + //index Check + checkIndex(index); + + //check size limit + if(size + 1 > elementData.length){ + int newlength = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newlength); + } + + for(int i = ++size; i >= index; i-- ){ + elementData[i] = elementData[i-1]; + } + + elementData[index] = o; + + + } + + public Object get(int index){ + //index Check + checkIndex(index); + + return elementData[index]; + } + + public Object remove(int index){ + //index Check + checkIndex(index); + + Object old = elementData[index]; + for(int i = index; i < size-1 ; i++ ){ + elementData[i] = elementData[i+1]; + } + elementData[--size] = null; + return old; + } + + public int size(){ + return size; + } + + public WIterator wIterator(){ + return new Itr(); + } + + public void clear(){ + elementData = new Object[10]; + size = 0; + } + + private void checkIndex(int index){ + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + } + + + private class Itr implements WIterator{ + //index for next element to visit + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + if(cursor >= size){ + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + + @Override + public void remove() { + //Check bound + if(cursor == 0){ + throw new NoSuchElementException(); + } + + WArrayList.this.remove(--cursor); + + } + } + + + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WBinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WBinaryTreeNode.java new file mode 100644 index 0000000000..6601a5cd0a --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WBinaryTreeNode.java @@ -0,0 +1,78 @@ +package com.github.congcongcong250.coding2017.basic; + +public class WBinaryTreeNode >{ + + private Object data; + private WBinaryTreeNode left; + private WBinaryTreeNode right; + + public WBinaryTreeNode(){ + data = null; + left = null; + right = null; + } + + public WBinaryTreeNode(Object obj){ + data = obj; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public WBinaryTreeNode getLeft() { + return left; + } + public void setLeft(WBinaryTreeNode left) { + this.left = left; + } + public WBinaryTreeNode getRight() { + return right; + } + public void setRight(WBinaryTreeNode right) { + this.right = right; + } + + public void destroy(){ + this.data = null; + this.left = null; + this.right = null; + } + + public WBinaryTreeNode insert(Object o){ + //If is empty root + if(data == null){ + data = o; + return this; + } + + //If it is a normal root + WBinaryTreeNode in; + + if(o.compareTo(data) <= 0){ + if(left == null){ + in = new WBinaryTreeNode(o); + left = in; + }else{ + in = left.insert(o); + } + }else{ + if(right == null){ + in = new WBinaryTreeNode(o); + right = in; + }else{ + in = right.insert(o); + } + } + + assert (in == null):"Insert error"; + return in; + } + + + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WIterator.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WIterator.java new file mode 100644 index 0000000000..747045754c --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WIterator.java @@ -0,0 +1,8 @@ +package com.github.congcongcong250.coding2017.basic; + +public interface WIterator { + public boolean hasNext(); + public Object next(); + public void remove(); + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WLinkedList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WLinkedList.java new file mode 100644 index 0000000000..1c5728a63b --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WLinkedList.java @@ -0,0 +1,182 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.NoSuchElementException; + +public class WLinkedList implements WList { + + private Node head; + private int size; + + public WLinkedList(){ + head = new Node(); + size = 0; + } + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + //Check bound + checkIndex(index); + + Node nx = this.find(index); + Node pr = nx.previous; + Node in = new Node(o,pr,nx); + nx.previous = in; + pr.next = in; + size++; + } + + public Object get(int index){ + //Check bound + checkIndex(index); + + return this.find(index).data; + } + + public Object remove(int index){ + //Check bound + checkIndex(index); + Node rem = this.find(index); + + Node pr = rem.previous; + Node nx = rem.next; + pr.next = nx; + nx.previous = pr; + + Object ret = rem.data; + rem.previous = null; + rem.next = null; + rem.data = null; + size--; + return ret; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node nx = head.next; + Node in = new Node(o,head, nx); + head.next = in; + nx.previous = in; + size++; + } + + public void addLast(Object o){ + Node last = head.previous; + Node in = new Node(o,last,head); + last.next = in; + head.previous = in; + + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public WIterator wIterator(){ + return new ListItr(); + } + public void clear(){ + for (Node x = head; x != null; ) { + Node next = x.next; + x.data = null; + x.next = null; + x.previous = null; + x = next; + } + } + + private void checkIndex(int index){ + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + } + + private Node find(int index){ + Node tra = head; + + //If index < size/2 + if( index < (size >> 1)){ + for(int i = 0; i <= index; i++){ + tra = tra.next; + } + }else{ + for(int i = size; i > index; i--){ + tra = tra.previous; + } + } + return tra; + } + + private static class Node{ + Object data; + Node next; + Node previous; + + public Node(){ + data = null; + next = this; + previous = this; + } + + public Node(Object obj,Node pre, Node nx){ + data = obj; + next = nx; + previous = pre; + } + + + } + + private class ListItr implements WIterator{ + //Point to next node + Node cursor; + int nextIndex; + + public ListItr(){ + cursor = head.next; + nextIndex = 0; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + checkBound(); + Node re = cursor; + cursor = cursor.next; + nextIndex++; + return re.data; + } + + public Object previous() { + Node re = cursor.previous.previous; + cursor = cursor.previous; + nextIndex--; + return re.data; + } + + @Override + public void remove() { + //Check bound + checkBound(); + WLinkedList.this.remove(--nextIndex); + + } + + private void checkBound(){ + if(nextIndex >= size){ + throw new NoSuchElementException("Iterates to the end"); + } + } + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WList.java new file mode 100644 index 0000000000..a79f0f9ff1 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WList.java @@ -0,0 +1,9 @@ +package com.github.congcongcong250.coding2017.basic; + +public interface WList { + 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/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WQueue.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WQueue.java new file mode 100644 index 0000000000..66a269ac2a --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WQueue.java @@ -0,0 +1,34 @@ +package com.github.congcongcong250.coding2017.basic; + +public class WQueue { + private WLinkedList elementData; + + public WQueue(){ + elementData = new WLinkedList(); + } + + public void enQueue(Object o){ + elementData.addFirst(o); + } + + public Object deQueue(){ + Object ret = elementData.removeLast(); + return ret; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return (elementData.size() == 0); + } + + public int size(){ + return elementData.size(); + } + + public void clear(){ + elementData.clear(); + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WStack.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WStack.java new file mode 100644 index 0000000000..de14bf5200 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WStack.java @@ -0,0 +1,32 @@ +package com.github.congcongcong250.coding2017.basic; + +public class WStack { + private WLinkedList elementData = new WLinkedList(); + + public WStack(){ + elementData = new WLinkedList(); + } + + public void push(Object o){ + elementData.addLast(o); + } + + public Object pop(){ + Object ret = elementData.removeLast(); + return ret; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return (elementData.size() == 0); + } + public int size(){ + return elementData.size(); + } + + public void clear(){ + elementData.clear(); + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayUtilTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayUtilTest.java new file mode 100644 index 0000000000..d8b583095a --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayUtilTest.java @@ -0,0 +1,140 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.congcongcong250.coding2017.basic.ArrayUtil; + +public class ArrayUtilTest { + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] origin = { 1, 2, 1, 3, 5, 6 }; + int[] reverse = { 6, 5, 3, 1, 2, 1 }; + + myArray.reverseArray(origin); + assertArrayEquals(origin, reverse); + + int[] empty = new int[0]; + myArray.reverseArray(empty); + assertArrayEquals(empty, new int[0]); + } + + @Test + public void testRemoveZero() { + int[] oldArray = { 1, 5, 0, 0, 6, 6, 0, 5, 4, 0, 7, 6, 7, 1, 2, 0 }; + int[] newArray = { 1, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2 }; + int[] res = myArray.removeZero(oldArray); + assertArrayEquals(newArray, res); + + int[] nl = null; + int[] nll = myArray.removeZero(nl); + assertNull(nll); + } + + @Test + public void testMerge() { + int a1[] = { 1, 2, 3, 4, 5 }; + int b1[] = { 3, 4, 5, 6, 7, 8 }; + int c1[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = { 0, 2, 3, 6, 7, 8 }; + int c2[] = { 0, 2, 3, 6, 7, 8 }; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = { 0, 2, 3, 6, 7, 8 }; + int b3[] = new int[0]; + int c3[] = { 0, 2, 3, 6, 7, 8 }; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() { + int[] a = { 3, 5, 7, 8, 9 }; + int[] b = { 3, 5, 7, 8, 9, 0, 0, 0 }; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 + expectedEx.expect(Exception.class); + myArray.grow(a, -3); + } + + @Test + public void testFibonacci() { + + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + int[] array2 = myArray.fibonacci(35); + int[] c = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() { + int[] a = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = { 6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() { + int[] a = { 3, 5, 7, 8, 9 }; + String s0 = myArray.join(a, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] a1 = { 3 }; + String s2 = myArray.join(a1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + int[] a0 = new int[0]; + String s4 = myArray.join(a0, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java new file mode 100644 index 0000000000..31470679fd --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java @@ -0,0 +1,41 @@ +/* + * An automatic runner for Junit test for DataStructure assignment + * */ +package com.github.congcongcong250.coding2017.basicTest; + + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class TestRunner { + + public static void main(String[] args){ + WArrayListTest ALT = new WArrayListTest(); + test(ALT); + + WLinkedListTest LLT = new WLinkedListTest(); + test(LLT); + + WStackTest STT = new WStackTest(); + test(STT); + + WQueueTest QT = new WQueueTest(); + test(QT); + + WBinaryTreeNodeTest BTNT = new WBinaryTreeNodeTest(); + test(BTNT); + + } + + private static Result test(testCase tc){ + + Result result = JUnitCore.runClasses(tc.getClass()); + for (Failure failure : result.getFailures()) { + System.out.println(failure.toString()); + } + System.out.println(tc.getClass().toString()+ "\n>>> Test status: "+result.wasSuccessful()); + + return result; + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WArrayListTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WArrayListTest.java new file mode 100644 index 0000000000..ee437f5494 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WArrayListTest.java @@ -0,0 +1,115 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.WArrayList; +import com.github.congcongcong250.coding2017.basic.WIterator; + +public class WArrayListTest implements testCase { + + WArrayList testlist = new WArrayList(); + + @Override + @Before + public void setUp() { + + for(int i = 0; i < 30; i++){ + testlist.add(i); + } + } + + @Override + @After + public void tearDown() { + testlist.clear(); + } + + @Override + @Test + public void testAdd() { + + assertEquals(0,testlist.get(0)); + assertEquals(11,testlist.get(11)); + assertEquals(20,testlist.get(20)); + assertEquals(29,testlist.get(29)); + assertEquals(30,testlist.size()); + + testlist.add(20, 100); + assertEquals(100,testlist.get(20)); + assertEquals(20,testlist.get(21)); + assertEquals(29,testlist.get(30)); + assertEquals(31,testlist.size()); + + } + + @Override + @Test + public void testRemove() { + + + assertEquals(6,testlist.get(6)); + assertEquals(30,testlist.size()); + testlist.remove(6); + assertEquals(7,testlist.get(6)); + assertEquals(29,testlist.size()); + assertEquals(21,testlist.get(20)); + assertEquals(5,testlist.get(5)); + } + + + @Test(expected=IndexOutOfBoundsException.class) + public void testgetneg(){ + + WArrayList emptyList = new WArrayList(); + Object o = emptyList.get(-1); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testgetout(){ + + Object o = testlist.get(31); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testremoveExp(){ + Object o = testlist.remove(31); + } + + @Override + @Test + public void testFunctional() { + WIterator itr = testlist.wIterator(); + assertTrue(itr.hasNext()); + for(int i = 0; i < 20; i++){ + assertEquals(i, itr.next()); + } + itr.remove(); + + assertTrue(itr.hasNext()); + assertEquals(20, itr.next()); + assertEquals(29, testlist.size()); + + for(int i = 21; i < 30; i++){ + assertEquals(i, itr.next()); + } + assertFalse(itr.hasNext()); + + boolean hasExp = false; + try{ + itr.next(); + }catch (NoSuchElementException e){ + hasExp = true; + } + assertTrue(hasExp); + } + + + + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WBinaryTreeNodeTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WBinaryTreeNodeTest.java new file mode 100644 index 0000000000..4bcc7b0c8e --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WBinaryTreeNodeTest.java @@ -0,0 +1,69 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.WBinaryTreeNode; + +public class WBinaryTreeNodeTest implements testCase { + + WBinaryTreeNode node = new WBinaryTreeNode(); + + @Override + @Before + public void setUp() { + node.insert(10); + + } + + @Override + @After + public void tearDown() { + node.destroy(); + } + + @Override + @Test + public void testAdd() { + assertEquals(10,node.getData()); + node.insert(5); + assertEquals(5,node.getLeft().getData()); + node.insert(1); + node.insert(2); + node.insert(6); + node.insert(19); + node.insert(18); + /* + * 10 + * 5 19 + * 1 6 18 + * 2 + * + * */ + assertEquals(1,node.getLeft().getLeft().getData()); + assertEquals(2,node.getLeft().getLeft().getRight().getData()); + assertEquals(6,node.getLeft().getRight().getData()); + assertEquals(19,node.getRight().getData()); + assertEquals(18,node.getRight().getLeft().getData()); + } + + @Override + @Test + public void testRemove() { + // TODO Auto-generated method stub + + } + + @Override + @Test + public void testFunctional() { + // TODO Auto-generated method stub + + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WLinkedListTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WLinkedListTest.java new file mode 100644 index 0000000000..f45daa6f35 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WLinkedListTest.java @@ -0,0 +1,143 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.WArrayList; +import com.github.congcongcong250.coding2017.basic.WLinkedList; +import com.github.congcongcong250.coding2017.basic.WIterator; + +public class WLinkedListTest implements testCase { + + WLinkedList testlist = new WLinkedList(); + + @Override + @Before + public void setUp() { + for(int i = 0; i < 20;i++){ + testlist.add(i); + } + } + + @Override + @After + public void tearDown() { + testlist.clear(); + } + + @Override + @Test + public void testAdd() { + assertEquals(20,testlist.size()); + assertEquals(0,testlist.get(0)); + assertEquals(19,testlist.get(19)); + + testlist.add(11, 100); + assertEquals(21,testlist.size()); + assertEquals(5,testlist.get(5)); + assertEquals(100,testlist.get(11)); + assertEquals(18,testlist.get(19)); + + testlist.addFirst(200); + assertEquals(22,testlist.size()); + assertEquals(200,testlist.get(0)); + assertEquals(4,testlist.get(5)); + assertEquals(100,testlist.get(12)); + assertEquals(17,testlist.get(19)); + + testlist.addLast(300); + assertEquals(23,testlist.size()); + assertEquals(200,testlist.get(0)); + assertEquals(4,testlist.get(5)); + assertEquals(100,testlist.get(12)); + assertEquals(17,testlist.get(19)); + assertEquals(300,testlist.get(22)); + + } + + @Override + @Test + public void testRemove() { + assertEquals(20,testlist.size()); + assertEquals(0,testlist.get(0)); + assertEquals(19,testlist.get(19)); + + testlist.remove(10); + assertEquals(19,testlist.size()); + assertEquals(4,testlist.get(4)); + assertEquals(9,testlist.get(9)); + assertEquals(11,testlist.get(10)); + assertEquals(19,testlist.get(18)); + + testlist.removeFirst(); + assertEquals(18,testlist.size()); + assertEquals(1,testlist.get(0)); + assertEquals(12,testlist.get(10)); + assertEquals(19,testlist.get(17)); + + testlist.removeLast(); + assertEquals(17,testlist.size()); + assertEquals(1,testlist.get(0)); + assertEquals(12,testlist.get(10)); + assertEquals(18,testlist.get(16)); + + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testgetneg(){ + + WLinkedList emptyList = new WLinkedList(); + Object o = emptyList.get(-2); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testgetout(){ + + Object o = testlist.get(31); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testremoveExp(){ + + Object o = testlist.remove(31); + } + + @Override + @Test + public void testFunctional() { + WIterator itr = testlist.wIterator(); + + assertTrue(itr.hasNext()); + for(int i = 0; i < 12; i++){ + assertEquals(i, itr.next()); + } + + //previous() function not yet defined in interface + + itr.remove(); + + assertTrue(itr.hasNext()); + assertEquals(12, itr.next()); + assertEquals(19, testlist.size()); + + for(int i = 13; i < 20; i++){ + assertEquals(i, itr.next()); + } + assertFalse(itr.hasNext()); + + boolean hasExp = false; + try{ + itr.next(); + }catch (NoSuchElementException e){ + hasExp = true; + } + assertTrue(hasExp); + + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WQueueTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WQueueTest.java new file mode 100644 index 0000000000..48e84eb287 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WQueueTest.java @@ -0,0 +1,76 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.WQueue; + +public class WQueueTest implements testCase { + + WQueue testqueue = new WQueue(); + + @Override + @Before + public void setUp() { + + for(int i = 0; i < 20; i++){ + testqueue.enQueue(i); + } + } + + @Override + @After + public void tearDown() { + testqueue.clear(); + } + + + @Override + @Test + public void testAdd() { + assertEquals(20,testqueue.size()); + assertEquals(0,testqueue.peek()); + assertEquals(20,testqueue.size()); + assertFalse(testqueue.isEmpty()); + } + + @Override + @Test + public void testRemove() { + assertEquals(20,testqueue.size()); + assertEquals(0,testqueue.deQueue()); + assertEquals(19,testqueue.size()); + assertEquals(1,testqueue.peek()); + assertFalse(testqueue.isEmpty()); + } + + @Override + @Test + public void testFunctional() { + for(int i = 0; i < 20; i++){ + testqueue.deQueue(); + } + assertTrue(testqueue.isEmpty()); + testqueue.enQueue(100); + testqueue.enQueue(200); + assertEquals(100,testqueue.deQueue()); + testqueue.enQueue(400); + assertEquals(200,testqueue.deQueue()); + assertFalse(testqueue.isEmpty()); + assertEquals(400,testqueue.deQueue()); + + boolean hasExp = false; + try{ + testqueue.deQueue(); + }catch (IndexOutOfBoundsException e){ + hasExp = true; + } + assertTrue(hasExp); + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WStackTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WStackTest.java new file mode 100644 index 0000000000..4dad0ec720 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WStackTest.java @@ -0,0 +1,75 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.github.congcongcong250.coding2017.basic.WStack; + +public class WStackTest implements testCase { + + WStack teststack = new WStack(); + + @Override + @Before + public void setUp() { + + for(int i = 0; i < 20; i++){ + teststack.push(i); + } + } + + @Override + @After + public void tearDown() { + teststack.clear(); + } + + + @Override + @Test + public void testAdd() { + assertEquals(20,teststack.size()); + assertEquals(19,teststack.peek()); + assertEquals(20,teststack.size()); + assertFalse(teststack.isEmpty()); + } + + @Override + @Test + public void testRemove() { + assertEquals(20,teststack.size()); + assertEquals(19,teststack.pop()); + assertEquals(19,teststack.size()); + assertEquals(18,teststack.peek()); + assertFalse(teststack.isEmpty()); + } + + @Override + @Test + public void testFunctional() { + for(int i = 0; i < 20; i++){ + teststack.pop(); + } + assertTrue(teststack.isEmpty()); + teststack.push(100); + teststack.push(200); + assertEquals(200,teststack.pop()); + teststack.push(400); + assertEquals(400,teststack.pop()); + assertFalse(teststack.isEmpty()); + assertEquals(100,teststack.pop()); + + + boolean hasExp = false; + try{ + teststack.pop(); + }catch (IndexOutOfBoundsException e){ + hasExp = true; + } + assertTrue(hasExp); + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/testCase.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/testCase.java new file mode 100644 index 0000000000..17df6ee30e --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/testCase.java @@ -0,0 +1,21 @@ +package com.github.congcongcong250.coding2017.basicTest; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public interface testCase { + @Before + public void setUp(); + + @After + public void tearDown(); + + @Test + public void testAdd(); + + @Test + public void testRemove(); + + @Test + public void testFunctional(); +} diff --git a/group02/609990377/LiteStruts/.gitignore b/group02/609990377/LiteStruts/.gitignore new file mode 100644 index 0000000000..fa968c2f2b --- /dev/null +++ b/group02/609990377/LiteStruts/.gitignore @@ -0,0 +1,3 @@ +/bin/ +.classpath +.project diff --git a/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/LoginAction.java b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..a146c5046b --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.github.congcongcong250.coding2017.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 void setMessage(String message){ + this.message = message; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/Struts.java b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..8b6090e4a8 --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/Struts.java @@ -0,0 +1,120 @@ +package com.github.congcongcong250.coding2017.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.w3c.dom.*; +import org.xml.sax.SAXException; + +import javax.xml.parsers.*; + +import java.beans.PropertyDescriptor; +import java.io.*; +import java.lang.reflect.*; + +public class Struts { + + public static View runAction(String actionName, + Map parameters) { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + + /* + * File f = new File("."); System.out.println(f.getAbsolutePath()); + */ + String filepath = "./src/com/github/congcongcong250/coding2017/litestruts/struts.xml"; + File inputFile = new File(filepath); + View view = new View(); + + try { + // Use DOM parser + Element e = getActionByName(actionName, inputFile); + + // Get class and entity by reflection + String clsName = e.getAttribute("class"); + Class clazz = Class.forName(clsName); + Object obj = clazz.newInstance(); + + // Set entity attributes from the parameters passed in + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + PropertyDescriptor pd = new PropertyDescriptor(key,clazz); + Method setMethod = pd.getWriteMethod(); + setMethod.invoke(obj, value); + } + + // Execute Login class, get result message + Method exec = clazz.getDeclaredMethod("execute"); + Object res = exec.invoke(obj); + + // Get result jsp address according to struts config + NodeList list = e.getElementsByTagName("result"); + for(int i = 0; i params = new HashMap(); + Field[] fields = clazz.getDeclaredFields(); + for(Field f : fields){ + PropertyDescriptor descriptor = new PropertyDescriptor(f.getName(),clazz); + Method getMethod = descriptor.getReadMethod(); + Object value = getMethod.invoke(obj); + params.put(f.getName(), value); + + } + view.setParameters(params); + + + + } catch (Exception e) { + e.printStackTrace(); + } + + // Return view + return view; + } + + private static Element getActionByName(String actionName, File inputFile) + throws ParserConfigurationException, SAXException, IOException { + + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputFile); + doc.getDocumentElement().normalize(); + + NodeList nList = doc.getElementsByTagName("action"); + for (int i = 0; i < nList.getLength(); i++) { + Node nNode = nList.item(i); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + if (eElement.getAttribute("name").equalsIgnoreCase( + actionName.toLowerCase())) { + return eElement; + } + } + } + return null; + } + +} diff --git a/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/StrutsTest.java b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a06d8e57c3 --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.congcongcong250.coding2017.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/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/View.java b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/View.java new file mode 100644 index 0000000000..69b5c87885 --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.congcongcong250.coding2017.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/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/struts.xml b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..d288f75839 --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group02/609990377/LiteStruts/src/struts.xml b/group02/609990377/LiteStruts/src/struts.xml new file mode 100644 index 0000000000..d288f75839 --- /dev/null +++ b/group02/609990377/LiteStruts/src/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group02/727171008/.classpath b/group02/727171008/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group02/727171008/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group02/727171008/.gitignore b/group02/727171008/.gitignore new file mode 100644 index 0000000000..0cfebaa908 --- /dev/null +++ b/group02/727171008/.gitignore @@ -0,0 +1,19 @@ +/bin/ +.idea/workspace.xml +.idea/dictionaries/myj.xml +.idea/ +.DS_Store +*.classpath +*.project +.settings +.project +.target +.classpath +**/.settings +**/.classpath +**/.eclipse +**/target/ +target/ +bin/ +.svn +*.iml \ No newline at end of file diff --git a/group02/727171008/.project b/group02/727171008/.project new file mode 100644 index 0000000000..0d10c0ccb2 --- /dev/null +++ b/group02/727171008/.project @@ -0,0 +1,17 @@ + + + 727171008Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java b/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..a29fcdd193 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java @@ -0,0 +1,345 @@ +package com.github.HarryHook.coding2017.array; + +public class ArrayUtil +{ + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin) + { + for(int i=0, j = origin.length-1; i array2[j]) + { + newArray[count++] = array2[j++]; + } + else if(array1[i] == array2[j]) + { + newArray[count++] = array2[j++]; + i++; + } + } + while(i==array1.length && j 1) + { + s = s + seperator; + for(int i=1; i size || index < 0) + throw new IndexOutOfBoundsException( + "Index: " + index + ", Size: " + size); + ensureCapacity(size + 1); + //判断当前位置是否有元素,没有元素添加到当前位置;若有,当前元素及之后元素向右移 + if(elementData[index] == null) + { + elementData[index] = o; + } + else + { + for(int i=elementData.length-1; i>index; i--) + { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + size++; + + /* + //判断索引位置是否正确 + if (index > size || index < 0) + throw new IndexOutOfBoundsException( + "Index: " + index + ", Size: " + size); + //扩容检测 + ensureCapacity(size+1); + /* + * 对源数组进行复制处理(位移),从index + 1到size-index。 + * 主要目的就是空出index位置供数据插入, + * 即向右移动当前位于该位置的元素以及所有后续元素。 + + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + //在指定位置赋值 + elementData[index] = 0; + size++; + + */ + } + + public Object get(int index) + { + //若index超出size应该抛出异常 + if(index >= size) + throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); + return elementData[index]; + + } + + public Object remove(int index) + { //涉及到元素移位 + Object oldValue = elementData[index]; + for(int i=index; i oldCapacity) + { + //Object oldData[] = elementData; //防止copyof()执行的过程中新内存或者其他进程分配内存时占用旧内存 + int newCapacity = (oldCapacity * 3)/2 + 1; //增加50%+1 + if (newCapacity < minCapacity) + newCapacity = minCapacity; + // minCapacity is usually close to size, so this is a win: + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + //返回数组的大小 + public int size() + { + return size; + } + + public Iterator iterator() + { + return new MyArrayListIterator(); + } + + private class MyArrayListIterator implements Iterator + { + private int cursor = 0; //记录索引位置 + public boolean hasNext() + { + return cursor != size; + } + public Object next() + { + try { + Object next = get(cursor); + cursor++; + return next; + + } catch (IndexOutOfBoundsException e) + { + throw new NoSuchElementException(); + } + + } + } + + public static void main(String[] args) + { + MyArrayList myArrays = new MyArrayList(); + myArrays.add(3); + myArrays.add(0, 11); + myArrays.add(1, 2); + myArrays.add(3, 5); + myArrays.add(2, 1); + myArrays.add(7); + Print(myArrays); + + for(int i = 0; i < 19; i++) + myArrays.add(i, 55); + + System.out.println("获取指定位置元素: " + myArrays.get(2)); + System.out.println("删除指定位置元素: " + myArrays.remove(1)); + System.out.println("当前元素个数:" + myArrays.size()); + + Print(myArrays); + + } + public static void Print(MyArrayList myArrays) + { + Iterator it = myArrays.iterator(); + System.out.println("对链表中的元素进行打印:"); + while(it.hasNext()) + System.out.print(it.next() + " "); + System.out.println(""); + System.out.println("当前元素个数: " + myArrays.size()); + + } + +} + diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java new file mode 100644 index 0000000000..5aeab57496 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyLinkedList.java @@ -0,0 +1,219 @@ +/* + * created by Harry 2017-2-21 14:43:41 + * 实现简单的LinkedList + */ + +package com.github.HarryHook.coding2017.basic; + +public class MyLinkedList implements List +{ + private Node head = null; //头指针 + private int size = 0; + private static class Node + { + Object data; + Node next; + } + public void add(Object o) + { + addLast(o); + } + //在指定位置添加元素 + public void add(int index , Object o) + { + + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + //存在插入头结点的情况 + if(index == 0) + addFirst(o); + else + { //即 index != 0 的情况 + // p保存待插入节点的前一节点,x指向要插入的节点 + Node x = head; + Node p = null; + int i = 0; + while(i < index) + { + p = x; + x = x.next; + i++; + } + Node n = new Node(); + p.next = n; + n.next = x; + n.data = o; + size++; + } + + } + //返回指定位置元素 + public Object get(int index) + { + Node x = head; + int i = 0; + while(i < index && x != null) + { + x = x.next; + i++; + } + return x.data; + } + + //移除指定位置节点 + public Object remove(int index) + { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + //先判断是否是头节点 + if( index == 0) + { + return removeFirst(); + } + else + { + Node x = head; + Node pre = null; + int i = 0; + while(i < index) + { + pre = x; + x = x.next; + i++; + } + Object Data = pre.next.data; + pre.next = x.next; + x = null; + size--; + return Data; + } + + } + //头部添加节点 + public void addFirst(Object o) + { + Node n = new Node(); + n.next = head; + head = n; + n.data = o; + size++; + } + //尾部添加节点 + public void addLast(Object o) + { + if (head == null) + { + head = new Node(); + head.data = o; + } + else + { + Node x = head; + while(x.next != null) + { + x = x.next; + } + Node n = new Node(); + x.next = n; + n.next = null; + n.data = o; + } + size++; + } + //移除第一个节点 + public Object removeFirst() + { + Node n = head; + Object Data = n.data; + head = head.next; + n = null; + size--; + return Data; + } + + //移除最后一个节点 + //removeLast()方法存在bug + public Object removeLast() + { + Node x = head; + Node p = null; + if(x.next == null) + { + return removeFirst(); + } + else + { + while(x.next != null) + { + p = x; + x = x.next; + } + Object Data = x.data; + p.next = null; + x = null; //删除最后一个节点 + size--; + return Data; + } + } + public int size(){ + return size; + } + public Iterator iterator() + { + return new MyLinkedListIterator(); + } + private class MyLinkedListIterator implements Iterator + { + private int cursor = 0; //记录索引位置 + public boolean hasNext() + { + return cursor != size; + } + public Object next() + { + Object next = get(cursor); + cursor ++; + return next; + } + } + public static void main(String[] args) + { + MyLinkedList myList = new MyLinkedList(); + myList.add(3); + myList.add(5); + myList.add(0, 4); + myList.add(2, 7); + myList.addFirst(1); + myList.addLast(6); + + Print(myList); + + System.out.println("当前指定位置元素: " + myList.get(1)); + System.out.println("移除指定位置元素: " + myList.remove(4)); + Print(myList); + + System.out.println("移除第一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + System.out.println("移除最后一个节点元素: " + myList.removeLast()); + Print(myList); + + } + public static void Print(MyLinkedList myList) + { + Iterator it = myList.iterator(); + System.out.println("对链表中的元素进行打印:"); + while(it.hasNext()) + System.out.print(it.next() + " "); + System.out.println(""); + System.out.println("当前元素个数: " + myList.size()); + System.out.println(""); + } + + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java new file mode 100644 index 0000000000..9b713eaea9 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyQueue.java @@ -0,0 +1,54 @@ +/* + * created by Harry 2017-2-22 13:06:43 + * 实现简单的队列 + */ +package com.github.HarryHook.coding2017.basic; +import java.util.*; +public class MyQueue +{ + private MyArrayList elementData = new MyArrayList(); + private int size = 0; + //入队 + public void enQueue(Object o) + { + elementData.add(o); + size++; + } + //出队 + public Object deQueue() + { + if(isEmpty()) + throw new NoSuchElementException(); + Object Data = elementData.remove(0); + size--; + return Data; + } + //判断队列是否为空 + public boolean isEmpty() + { + return size() == 0; + } + //队列中元素个数 + public int size() + { + return size; + } + public static void main(String[] args) + { + MyQueue mq = new MyQueue(); + mq.enQueue(1); + mq.enQueue(2); + mq.enQueue(3); + mq.enQueue(4); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + System.out.println("队列中元素个数: " + mq.size()); + //System.out.println("队列出栈,出栈元素为: " + mq.deQueue()); + //System.out.println("队列中元素个数: " + mq.size()); + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java new file mode 100644 index 0000000000..c7f87c04e6 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/MyStack.java @@ -0,0 +1,59 @@ +/* + * created by Harry 2017-2-22 10:48:34 + * 实现简单的Stack + */ +package com.github.HarryHook.coding2017.basic; + +import java.util.*; + +public class MyStack +{ + private MyArrayList elementData = new MyArrayList(); + private int size = 0; + + //入栈操作 + public void push(Object o) + { + elementData.add(o); + size++; + } + //出栈操作 + public Object pop() + { + Object obj = peek(); + elementData.remove(size() - 1); + size--; + return obj; + } + //获取当前栈顶元素,不用出栈 + public Object peek() + { + if(isEmpty()) + throw new EmptyStackException(); + return elementData.get(size() - 1); + } + //判断栈是否为空 + public boolean isEmpty() + { + return size() == 0; + } + //返回栈内元素个数 + public int size(){ + return size; + } + + public static void main(String[] args) + { + MyStack ms = new MyStack(); + + ms.push(1); + ms.push(2); + ms.push(13); + System.out.println("当前栈顶元素是: " + ms.peek()); + System.out.println("出栈元素是: " + ms.pop()); + System.out.println("出栈元素是: " + ms.pop()); + ms.push(12); + System.out.println("出栈元素是: " + ms.pop()); + System.out.println("当前栈顶元素是: " + ms.peek()); + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..340f79d240 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.HarryHook.coding2017.basic.MyQueue; + +public class QueueTest { + private MyQueue queue; + + @Before + public void setUpQueue() { + queue = new MyQueue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..26160faef6 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/basic/StackTest.java @@ -0,0 +1,40 @@ +package com.github.HarryHook.coding2017.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.HarryHook.coding2017.basic.MyStack; + +public class StackTest { + + private MyStack stack; + + @Before + public void setUpStack() { + stack = new MyStack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/LoginAction.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..4b42b92093 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/LoginAction.java @@ -0,0 +1,45 @@ +package com.github.HarryHook.coding2017.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/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Struts.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..79f662699b --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/Struts.java @@ -0,0 +1,109 @@ +package com.github.HarryHook.coding2017.litestruts; + +import java.io.File; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; + +import org.w3c.dom.Attr; +import org.w3c.dom.Comment; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + + +public class Struts +{ + + public static View runAction(String actionName, Map parameters) + { + return null; + } + public static void main(String[] args) throws Exception + { + /* + Class clz = Class.forName("com.github.HarryHook.coding2017.litestruts.LoginAction"); + //创建类的实例 + Object obj = clz.newInstance(); + //得到方法的引用 + Method method = clz.getDeclaredMethod("setName", java.lang.String.class); + //调用该方法 + method.invoke(obj, "test"); + LoginAction b = (LoginAction) obj; + System.out.println(b.getName()); + + method = clz.getDeclaredMethod("setPassword", java.lang.String.class); + method.invoke(obj, "1234"); + b = (LoginAction) obj; + System.out.println(b.getPassword()); + */ + + //获得dom解析器工厂,用于创建具体的解析器 + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + //获得具体的dom解析器 + DocumentBuilder db = dbf.newDocumentBuilder(); + //解析一个xml文档,获得Document对象(根节点) + Document doc = db.parse(new File("src/com/github/HarryHook/coding2017/litestruts/struts.xml")); + //对文档进行解析 + Element root = doc.getDocumentElement(); + parseElement(root); + System.out.println(""); + + } + private static void parseElement(Element element) + { + String tagName = element.getNodeName(); + NodeList children = element.getChildNodes(); + System.out.print("<" + tagName); + + //element元素的所有属性所构成的NamedNodeMap队形,要对其进行判断 + NamedNodeMap nnm = element.getAttributes(); + + //如果元素存在属性 + if(nnm != null) + { + for(int i=0; i"); + + for(int i=0; i"); + } + } + System.out.print(""); + + } + + + +} \ No newline at end of file diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..458f2b190c --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,57 @@ +package com.github.HarryHook.coding2017.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; + + 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"); //密码和预设的不一致 + try + { + View 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")); + + }catch(Exception e) + { + e.printStackTrace(); + } + + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java new file mode 100644 index 0000000000..5d1977d887 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java @@ -0,0 +1,31 @@ +package com.github.HarryHook.coding2017.litestruts; + +import java.util.Map; + +public class View +{ + private String jsp; + private Map parameters; + + //对应action获取Jsp + public String getJsp() + { + return jsp; + } + public View setJsp(String jsp) + { + this.jsp = jsp; + return this; + } + + //execute()获取对应参数 + public Map getParameters() + { + return parameters; + } + public View setParameters(Map parameters) + { + this.parameters = parameters; + return this; + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/struts.xml b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..7d2a8ed4ab --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/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/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..34d062b327 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java new file mode 100644 index 0000000000..7a13f14c83 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.ZhoufeifeiJAVA.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/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java new file mode 100644 index 0000000000..0666530694 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyArrayList.java @@ -0,0 +1,78 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + if(size == elementData.length){ + elementData = arrayGrow(elementData,size/2); + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o)throws RuntimeException{ + if(index > size) + throw new RuntimeException("index is bigger than the size of MyArrayList"); + if(size == elementData.length){ + elementData = arrayGrow(elementData,size/2); + } + if(index == size) + add(o); + else{ + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + size ++; + } + private Object[] arrayGrow(Object[] src,int size){ + Object[] target = new Object[src.length+size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + public Object get(int index){ + if(index >= size) + return null; + else + return elementData[index]; + } + + public Object remove(int index)throws IndexOutOfBoundsException{ + if(index>=size || index<0) + throw new IndexOutOfBoundsException("index is bigger than the size of MyArrayList"); + Object removeObject = elementData[index]; + for(int i=index;isize || index<0) + throw new IndexOutOfBoundsException("the index is bigger than the size of MyLinkedList"); + Node newNode = new Node(); + newNode.data = o; + if(index == 0){ + newNode.next = head; + head = newNode; + } + else{ + int i = 0; + Node pointer = head; + while(isize-1) + return null; + Node pointer = head; + while(index>0){ + pointer = pointer.next; + index --; + } + return pointer.data; + + } + public Object remove(int index)throws IndexOutOfBoundsException{ + if(index<0 || index>size-1) + throw new IndexOutOfBoundsException("the index is not legal"); + Node pointer = head; + if(index == 0){ + head = head.next; + size --; + return pointer.data; + } + else{ + while(index>1){ + pointer = pointer.next; + index --; + } + Node temp = pointer.next; + pointer.next = pointer.next.next; + size --; + return temp.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(size,o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + + private static class Node{ + Object data; + Node next; + } + private class MyLinkedListIterator implements Iterator{ + private Node pointer; + MyLinkedListIterator(){ + pointer = head; + } + public boolean hasNext() { + if(pointer.next != null) + return true; + else + return false; + } + public Object next() { + Node temp = pointer; + pointer = pointer.next; + return temp.data; + } + } + public Iterator iterator(){ + return new MyLinkedListIterator(); + } + //public void showData(int index){ + // System.out.println("the data of "+index+" is "+get(index).data); + //} +} + + + + + + + diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java new file mode 100644 index 0000000000..6d5ca14e70 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/MyLinkedListTest.java @@ -0,0 +1,34 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class MyLinkedListTest{ + public static void main(String[] args){ + MyLinkedList al = new MyLinkedList(); + al.add("string0"); + al.add("string1"); + al.add("string2"); + al.add("string3"); + al.add("string4"); + al.add("string5"); + al.add("string6"); + al.add("string7"); + al.add("string8"); + al.add("string9"); + al.add("string10"); + al.add("string11"); + al.add("string12"); + al.add("string13"); + al.add("string14"); + al.add(11,"string10.5"); + sop(al.remove(4)); + sop(al.get(10)); + sop(al.get(11)); + sop(al.get(12)); + sop("the size of al is "+al.size()); + sop("the iterator method used,so the result is as follows:"); + for(Iterator it = al.iterator();it.hasNext();){ + sop(it.next()); + } + } + public static void sop(Object o){ + System.out.println(o); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java new file mode 100644 index 0000000000..0ce735f150 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Queue.java @@ -0,0 +1,26 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class Queue { + private MyLinkedList llist = null; + Queue(){ + llist = new MyLinkedList(); + } + public void enQueue(Object o){ + llist.add(o); + } + + public Object deQueue(){ + return llist.removeFirst(); + } + + public boolean isEmpty(){ + if(llist.size() != 0) + return true; + else + return false; + } + + public int size(){ + return llist.size(); + } +} diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..95cc5117ff --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/QueueTest.java @@ -0,0 +1,17 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class QueueTest{ + public static void sop(Object o){ + System.out.println(o); + } + public static void main(String[] args){ + Queue q = new Queue(); + q.enQueue("String0"); + q.enQueue("String1"); + q.enQueue("String2"); + sop("the queue is not empty "+q.isEmpty()); + sop("the size of queue is "+q.size()); + sop("out queue "+q.deQueue()); + sop("out queue "+q.deQueue()); + sop("the size of queue is "+q.size()); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java new file mode 100644 index 0000000000..3992f89f86 --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/Stack.java @@ -0,0 +1,28 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; + +public class Stack { + private MyLinkedList llist = null; + Stack(){ + llist = new MyLinkedList(); + } + public void push(Object o){ + llist.add(o); + } + + public Object pop(){ + return llist.removeLast(); + } + + public Object peek(){ + return llist.get(llist.size()-1); + } + public boolean isEmpty(){ + if(llist.size() != 0) + return true; + else + return false; + } + public int size(){ + return llist.size(); + } +} \ No newline at end of file diff --git a/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..4bcb60e35d --- /dev/null +++ b/group02/793337535/com/github/ZhoufeifeiJAVA/coding2017/basic/StackTest.java @@ -0,0 +1,17 @@ +package com.github.ZhoufeifeiJAVA.coding2017.basic; +public class StackTest{ + public static void sop(Object o){ + System.out.println(o); + } + public static void main(String[] args){ + Stack s = new Stack(); + s.push("String0"); + s.push("String1"); + s.push("String2"); + sop("the queue is not empty "+s.isEmpty()); + sop("the size of queue is "+s.size()); + sop("out queue "+s.pop()); + sop("just watch queue,not delete "+s.peek()); + sop("the size of queue is "+s.size()); + } +} \ No newline at end of file diff --git a/group02/812350401/.gitignore b/group02/812350401/.gitignore new file mode 100644 index 0000000000..c24866af6d --- /dev/null +++ b/group02/812350401/.gitignore @@ -0,0 +1,3 @@ +/bin/ +/lib/ +/src/java_training diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..1c1b9a6df3 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java @@ -0,0 +1,78 @@ +package com.github.miniyk2012.coding2017.basic; + +import java.util.Arrays; + + +public class ArrayList implements List { + + private int size = 0; + private int DEFAULT_LENGTH = 10; + + private Object[] elementData = new Object[DEFAULT_LENGTH]; + + public void add(Object o){ + ensureCapcacity(); + elementData[size++] = o; + } + public void add(int index, Object o){ + ensurnPosition(index); + ensureCapcacity(); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + ensureIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + ensureIndex(index); + Object temp = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, + size - index - 1); + elementData[size-1] = null; + size--; + return temp; + } + + public void clear() { + elementData = new Object[DEFAULT_LENGTH]; + size = 0; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + Iterator iterator = new IteratorImp(this); + return iterator; + } + + private void ensureCapcacity() { + int oldLength = elementData.length; + if (size+1 > oldLength) { + elementData = Arrays.copyOf(elementData, 2*oldLength); + } + } + + private void ensureIndex(int index) { + if (index >= size || index < 0) + throw new ArrayIndexOutOfBoundsException(String.format("index %d out of bounds [0-%d)", index, size)); + } + + private void ensurnPosition(int index) { + if (index <0 || index>size) + throw new ArrayIndexOutOfBoundsException(String.format("position %d out of position [0-%d)", index, size)); + } + + @Override + public String toString() { + Object[] tempArray = Arrays.copyOf(elementData, size); + return Arrays.toString(tempArray); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..5b1f17342b --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,55 @@ +package com.github.miniyk2012.coding2017.basic; + +public class BinaryTreeNode > { + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(E x) { + data = x; + } + public E getData() { + return data; + } + public void setData(E 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(E o){ + BinaryTreeNode node = new BinaryTreeNode(o); + boolean left = true; + BinaryTreeNode currentNode = this; + BinaryTreeNode previousNode = null; + + while (currentNode != null) { + previousNode = currentNode; + int compareTo = node.getData().compareTo(currentNode.getData()); + if (compareTo <= 0) { // 小于,往左插入 + currentNode = currentNode.left; + left = true; + } else { + currentNode = currentNode.right; + left = false; + } + } + if (left) { + previousNode.left = node; + } else { + previousNode.right = node; + } + return node; + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..229ba41b05 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.github.miniyk2012.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java new file mode 100644 index 0000000000..cfca8eaac7 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/IteratorImp.java @@ -0,0 +1,24 @@ +package com.github.miniyk2012.coding2017.basic; + +public class IteratorImp implements Iterator { + + private List aList; + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != aList.size(); + } + + @Override + public Object next() { + int i = cursor; + Object next = aList.get(i); + cursor = i+1; + return next; + } + + public IteratorImp(List aList) { + this.aList = aList; + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..e34d76001b --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/LinkedList.java @@ -0,0 +1,103 @@ +package com.github.miniyk2012.coding2017.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + /** + * node接收的index一定是范围内的值,不可能越界 + * @param index + * @return a Node + */ + Node node(int index) { + Node x = head; + for (int i=0; i= size) + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + + private void checkPositionIndex(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java new file mode 100644 index 0000000000..ff6718f462 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/List.java @@ -0,0 +1,10 @@ +package com.github.miniyk2012.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(); + public Iterator iterator(); +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java new file mode 100644 index 0000000000..bcb9d0f9e4 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Queue.java @@ -0,0 +1,23 @@ +package com.github.miniyk2012.coding2017.basic; + +public class Queue { + + // 队列入口 -》1,2,3,4 -》队列出口 + private LinkedList aList = new LinkedList(); + + public void enQueue(Object o){ + aList.addFirst(o); + } + + public Object deQueue(){ + return aList.removeLast(); + } + + public boolean isEmpty(){ + return aList.size() == 0; + } + + public int size(){ + return aList.size(); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java new file mode 100644 index 0000000000..ddfe76f774 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/Stack.java @@ -0,0 +1,25 @@ +package com.github.miniyk2012.coding2017.basic; + +public class Stack { + + // 栈顶 《-》 1,2,3,4 栈底 + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0, o); + } + + public Object pop(){ + return elementData.remove(0); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..b308c6bfef --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,14 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import org.junit.Before; +import com.github.miniyk2012.coding2017.basic.ArrayList; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new ArrayList(); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..698506e2b5 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java @@ -0,0 +1,70 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.miniyk2012.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + /** + // 4 + // 1 5 + // 2 3 + */ + @Before + public void setUpBinaryTreeNode() { + binaryTreeNode = new BinaryTreeNode(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + } + + @Test + public void testBinaryTreeNodeFunctional1() { + assertEquals(new Integer(4), binaryTreeNode.getData()); + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); + } + + @Test + public void testBinaryTreeFunctional2() { + BinaryTreeNode node1 = binaryTreeNode.getLeft(); + assertEquals(new Integer(1), node1.getData()); + assertEquals(new Integer(3), node1.getRight().getData()); + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testBinaryTreeFunctional3() + { + BinaryTreeNode treeNode = new BinaryTreeNode(100); + treeNode.insert(10); + binaryTreeNode.setRight(treeNode); + // 4 + // 1 100 + // 2 3 10 + assertEquals(new Integer(4), binaryTreeNode.getData()); + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); + assertEquals(new Integer(100), binaryTreeNode.getRight().getData()); + assertEquals(new Integer(10), binaryTreeNode.getRight().getLeft().getData()); + + expectedEx.expect(Exception.class); + binaryTreeNode.getRight().getRight().getRight(); // null exception + binaryTreeNode.getRight().getRight().getLeft(); // null exception + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..487fd2ee00 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,93 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.LinkedList; + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..587bf17f4d --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/ListTest.java @@ -0,0 +1,117 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.miniyk2012.coding2017.basic.Iterator; +import com.github.miniyk2012.coding2017.basic.List;; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i=0; i<100; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(99, aList.get(99)); + assertEquals(44, aList.get(44)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i=0; i<10; i++) + aList.add(i*2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + + aList.remove(1); + aList.add(3); + aList.add(2, 5); + } + + @Test + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..67035c1dcf --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.miniyk2012.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..a9b4e14afd --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/StackTest.java @@ -0,0 +1,40 @@ +package com.github.miniyk2012.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.miniyk2012.coding2017.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5d7ad8f4d3 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,279 @@ +package com.github.miniyk2012.coding2017.coderising.array; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.IntStream; +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 size = origin.length; + if (size == 0) return; + int start = 0, end = size-1; + while (start < end) { + int temp = origin[start]; + origin[start++] = origin[end]; + origin[end--] = 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 int[] removeZero(int[] oldArray){ + if (oldArray==null) return null; + List list=new ArrayList<>(); + for (int e : oldArray) { + if (e != 0) { + list.add(e); + } + } + return list2Array(list); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + if (array1==null || array2==null) return null; + if (array1.length == 0) return Arrays.copyOf(array2, array2.length); + List list = array2List(array1); + int currentIndex = 0; + for (int e : array2) { + for (int index = currentIndex; index < list.size(); index++ ) { + currentIndex = index + 1; + if (list.get(index) == e) break; + if (list.get(index) > e) { + list.add(index, e); + break; + } + } + if (e > list.get(list.size()-1)) list.add(e); + } + return list2Array(list); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + * @throws Exception + */ + public int[] grow(int [] oldArray, int size) throws Exception{ + if (oldArray==null) return null; + if (size < 0) throw new Exception(); + int oldSize = oldArray.length; + int[] newArray = new int[size+oldSize]; + System.arraycopy(oldArray, 0, newArray, 0, oldSize); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if (max <= 1) return new int[0]; + int i = 1, j = 1; + List list = new ArrayList<>(); + list.add(i); + list.add(j); + int next = i+j; + while (max > next) { + list.add(next); + i = j; + j = next; + next = i+j; + } + return list2Array(list); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + // TODO 使用筛法,写的不好,有待改善 + if (max <= 2) return new int[0]; + List list = new ArrayList<>(); + int i; + for (i=2; i= 0) + list.remove(index); + k += currentNum; + } + currentNum = list.get(++i); + } + return list2Array(list); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + List list = new ArrayList<>(); + int[] factors; + for (int i=1; i list = new ArrayList<>(); + for (int i=1; i < num; i++) { + if(num % i == 0) list.add(i); + } + return list2Array(list); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + if (array.length == 0) return ""; + if (array.length == 1) return "" + array[0]; + StringBuilder s = new StringBuilder(); + for (int i=0; i转换为相同顺序和长度的int[] + * @param list + * @return + */ + private int[] list2Array(List list) { + int size = list.size(); + int[] newArray = new int[size]; + for (int i=0; i + * @param array + * @return + */ + private List array2List(int[] array) { + List list = new ArrayList<>(); + for (int e : array) { + list.add(e); + } + return list; + } + + public static void main(String []args) throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + + // merge + int[] a1 = {1,2,3}, a2 = {-4,-2,2,3,4}; +// int[] a1 = {}, a2 = {}; +// int[] a1 = {1,2,3}, a2 = {}; +// int[] a1 = {}, a2 = {1,2,3}; +// int[] a1 = {4,5,6}, a2 = {1,2,3}; + int[] a3 = arrayUtil.merge(a1, a2); + System.out.println(Arrays.toString(a3)); + + // reverse +// a1 = new int[] {}; +// a1 = new int[] {4,}; +// a1 = new int[] {4,3,5,6,7,7,8}; + a1 = new int[] {4,3,5,6,7,7,8, 9}; + arrayUtil.reverseArray(a1); + System.out.println(Arrays.toString(a1)); + + // remove zero +// a1 = new int[] {}; +// a1 = new int[] {0,0}; + a1 = new int[] {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + a2 = arrayUtil.removeZero(a1); + System.out.println(Arrays.toString(a2)); + + // grow + a1 = new int[] {1,2,3}; + a2 = arrayUtil.grow(a1, 4); +// a2 = arrayUtil.grow(a1, 2); +// a2 = arrayUtil.grow(a1, 0); + System.out.println(Arrays.toString(a2)); + + // fibonacci + a1 = arrayUtil.fibonacci(15); +// a1 = arrayUtil.fibonacci(1); +// a1 = arrayUtil.fibonacci(2); +// a1 = arrayUtil.fibonacci(-2); + System.out.println(Arrays.toString(a1)); + + // prime + a1 = arrayUtil.getPrimes(2); +// a1 = arrayUtil.getPrimes(3); +// a1 = arrayUtil.getPrimes(8); +// a1 = arrayUtil.getPrimes(12); +// a1 = arrayUtil.getPrimes(23); +// a1 = arrayUtil.getPrimes(24); +// a1 = arrayUtil.getPrimes(50); + a1 = arrayUtil.getPrimes(100); + System.out.println(Arrays.toString(a1)); + + // perfectNumbers + a1 = arrayUtil.getPerfectNumbers(1000); + System.out.println(Arrays.toString(a1)); + + // join +// a1 = new int[] {}; +// a1 = new int[] {1}; + a1 = new int[] {1,2,3}; + String str = arrayUtil.join(a1, "-"); + System.out.println(str); + + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java new file mode 100644 index 0000000000..58de9f3efb --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java @@ -0,0 +1,156 @@ +package com.github.miniyk2012.coding2017.coderising.array.test; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.miniyk2012.coding2017.coderising.array.ArrayUtil; + +public class ArrayUtilTest +{ + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception + { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() + { + int[] a = {1, 2, 1, 3, 5, 6}; + int[] b = {6, 5, 3, 1, 2, 1}; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + int[] c = new int[0]; + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + + } + + @Test + public void testRemoveZero() + { + int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5}; + int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5}; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = null; + int[] e = myArray.removeZero(d); + assertNull(e); + + } + + @Test + public void testMerge() + { + int a1[] = {1, 2, 3, 4, 5}; + int b1[] = {3, 4, 5, 6, 7, 8}; + int c1[] = {1, 2, 3, 4, 5, 6, 7, 8}; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = {0, 2, 3, 6, 7, 8}; + int c2[] = {0, 2, 3, 6, 7, 8}; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = {0, 2, 3, 6, 7, 8}; + int b3[] = new int[0]; + int c3[] = {0, 2, 3, 6, 7, 8}; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() throws Exception + { + int[] a = {3, 5, 7, 8, 9}; + int [] oldA = Arrays.copyOf(a, a.length); + int[] b = {3, 5, 7, 8, 9, 0, 0, 0}; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + assertArrayEquals(a, oldA); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 抛出异常 + expectedEx.expect(Exception.class); + myArray.grow(a, -3); + } + + @Test + public void testFibonacci() + { + //max == 1时返回空数组 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + + int[] array2= myArray.fibonacci(35); + int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() + { + int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + //max <= 2的时候没有素数,数组为空数组 + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() + { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = {6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() + { + int[] Array0 = {3, 5, 7, 8, 9}; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = {3}; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + //传递空数组时,返回空字符串 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..b89a242df6 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java @@ -0,0 +1,48 @@ +package com.github.miniyk2012.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 void setMessage(String message) { + this.message = 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 void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } + + @Override + public String toString() { + return "LoginAction [name=" + name + ", password=" + password + ", message=" + message + "]"; + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..f283373b48 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java @@ -0,0 +1,48 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LogoutAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "LogoutAction [name=" + name + ", password=" + password + ", message=" + message + "]"; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "logout successful"; + return "success"; + } + this.message = "logout failed,please check your user/pwd"; + return "error"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..54e8468077 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java @@ -0,0 +1,173 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.logging.Logger; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; +import org.dom4j.Element; + + +public class Struts { + + /** dom4j object model representation of a xml document. Note: We use the interface(!) not its implementation */ + private static Document doc; + private static Element aElement; + private static Object object; + private static View view; + private static final Logger logger = Logger.getLogger(Struts.class.getName()); + + public static View runAction(String actionName, Map parameters) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + /* + + 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字段中。 + + */ + + readXml(); + String retValue = processAction(actionName, parameters); + view = generateView(retValue); + return view; + } + + private static View generateView(String retValue) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + view = new View(); + Map map = getFields(); + String jsp = getJsp(retValue); + view.setParameters(map); + view.setJsp(jsp); + return view; + } + + private static String getJsp(String retValue) { + for (Iterator i = aElement.elementIterator( "result" ); i.hasNext();) { + Element result = (Element) i.next(); + if (result.attributeValue("name").equals(retValue)) { + return result.getText(); + } + } + return ""; + } + + /** + * @return + * @throws IntrospectionException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static Map getFields() + throws IntrospectionException, IllegalAccessException, InvocationTargetException { + Map map = new HashMap<>(); + Class clazz = object.getClass(); + Field[] fields = object.getClass().getDeclaredFields();//获得属性 + for (Field field : fields) { + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), + clazz); + Method getMethod = pd.getReadMethod();//获得get方法 + String value = (String) getMethod.invoke(object);//执行get方法返回一个Object + map.put(field.getName(), value); + } + return map; + } + + private static void readXml() throws DocumentException { + String fileName = Thread.currentThread().getContextClassLoader().getResource("").getPath() + + "com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml"; + File aFile = new File(fileName); + SAXReader xmlReader = new SAXReader(); + doc = xmlReader.read(aFile); + } + + private static String processAction(String actionName, Map parameters) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + generateObject(actionName); + setFields(parameters); + return doExecute(); + } + + /** + * @return + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static String doExecute() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Class c = object.getClass(); + Method method = c.getMethod("execute"); + String ret = (String) method.invoke(object); + return ret; + } + + /** + * @param parameters + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static void setFields(Map parameters) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for (Map.Entry entry: parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + key = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Class c = object.getClass(); + Method method = c.getMethod(key, String.class); + method.invoke(object, value); + } + } + + /** + * @param actionName + * @throws InstantiationException + * @throws IllegalAccessException + * @throws ClassNotFoundException + */ + private static void generateObject(String actionName) + throws InstantiationException, IllegalAccessException, ClassNotFoundException { + Element root = doc.getRootElement(); + String className = null; + for ( Iterator i = root.elementIterator(); i.hasNext(); ) { + Element actionElement = (Element) i.next(); + if (actionElement.attributeValue("name").equals(actionName)) { + aElement = actionElement; + className = actionElement.attributeValue("class"); + break; + } + } + if (className == null) throw new InstantiationException("no such className"); + object = Class.forName(className).newInstance(); + } + + + public static void main(String args[]) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException + { + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = runAction("login", params); + logger.info(view.toString()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9443a32d97 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java @@ -0,0 +1,75 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +import java.beans.IntrospectionException; +import java.lang.reflect.InvocationTargetException; +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 DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionSuccess() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("logout successful", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + String actionName = "logout"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("logout failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..46759e7d01 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package com.github.miniyk2012.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; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0d3fd6495f --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/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/group02/851113375/.gitignore b/group02/851113375/.gitignore new file mode 100644 index 0000000000..fa968c2f2b --- /dev/null +++ b/group02/851113375/.gitignore @@ -0,0 +1,3 @@ +/bin/ +.classpath +.project diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/ArrayUtil.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..05e26ebe51 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/ArrayUtil.java @@ -0,0 +1,228 @@ +package com.github.eloiseSJTU.coding2017.array; + +import java.security.InvalidParameterException; + +import com.github.eloiseSJTU.coding2017.basic.ArrayList; +import com.github.eloiseSJTU.coding2017.basic.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (origin == null) { + return; + } + int left = 0; + int right = origin.length - 1; + while (left < right) { + int tmp = origin[left]; + origin[left] = origin[right]; + origin[right] = tmp; + left++; + right--; + } + } + + /** + * 现在有如下的一个数组: 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) { + if (oldArray == null) { + return null; + } + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + count++; + } + } + int[] newArray = new int[count]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[index++] = oldArray[i]; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + if (array1 == null) { + return array2; + } + if (array2 == null) { + return array1; + } + int len1 = array1.length; + int len2 = array2.length; + ArrayList arrayList = new ArrayList(); + int i = 0; + int j = 0; + while (i < len1 && j < len2) { + if (array1[i] < array2[j]) { + arrayList.add(array1[i]); + i++; + } else if (array1[i] > array2[j]) { + arrayList.add(array2[j]); + j++; + } else { + arrayList.add(array1[i]); + i++; + j++; + } + } + while (i < len1) { + arrayList.add(array1[i++]); + } + while (j < len2) { + arrayList.add(array2[j++]); + } + return toArray(arrayList); + } + + /** + * 把一个已经存满数据的数组 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 (oldArray == null) { + return null; + } + if (size < 0) { + throw new InvalidParameterException("size can't be negative"); + } + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + ArrayList arrayList = new ArrayList(); + int a = 1; + arrayList.add(a); + int b = 1; + arrayList.add(b); + int c = a + b; + while (c < max) { + arrayList.add(c); + a = b; + b = c; + c = a + b; + } + + return toArray(arrayList); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + ArrayList arrayList = new ArrayList(); + for (int i = 2; i < max; i++) { + boolean pn = true; + for (int j = 0; j < arrayList.size(); j++) { + if (i % (int) arrayList.get(j) == 0) { + pn = false; + break; + } + } + if (pn) { + arrayList.add(i); + } + } + + return toArray(arrayList); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList arrayList = new ArrayList(); + for (int i = 2; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + arrayList.add(i); + } + } + return toArray(arrayList); + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + if (array == null || array.length == 0) { + return ""; + } + StringBuffer stringBuffer = new StringBuffer(); + int i = 0; + for (; i < array.length - 1; i++) { + stringBuffer.append(array[i] + seperator); + } + stringBuffer.append(array[i]); + return stringBuffer.toString(); + } + + private int[] toArray(List list) { + int size = list.size(); + int[] result = new int[size]; + for (int i = 0; i < size; i++) { + result[i] = (int) list.get(i); + } + return result; + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/test/ArrayUtilTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/test/ArrayUtilTest.java new file mode 100644 index 0000000000..88a5108d07 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/test/ArrayUtilTest.java @@ -0,0 +1,142 @@ +package com.github.eloiseSJTU.coding2017.array.test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.eloiseSJTU.coding2017.array.ArrayUtil; + +public class ArrayUtilTest { + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] a = { 1, 2, 1, 3, 5, 6 }; + int[] b = { 6, 5, 3, 1, 2, 1 }; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + int[] c = new int[0]; + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + } + + @Test + public void testRemoveZero() { + int[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5 }; + int b[] = { 1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5 }; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = null; + int[] e = myArray.removeZero(d); + assertNull(e); + } + + @Test + public void testMerge() { + int a1[] = { 1, 2, 3, 4, 5 }; + int b1[] = { 3, 4, 5, 6, 7, 8 }; + int c1[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = { 0, 2, 3, 6, 7, 8 }; + int c2[] = { 0, 2, 3, 6, 7, 8 }; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = { 0, 2, 3, 6, 7, 8 }; + int b3[] = new int[0]; + int c3[] = { 0, 2, 3, 6, 7, 8 }; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() { + int[] a = { 3, 5, 7, 8, 9 }; + int[] b = { 3, 5, 7, 8, 9, 0, 0, 0 }; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 抛出异常 + expectedEx.expect(Exception.class); + myArray.grow(a, -3); + } + + @Test + public void testFibonacci() { + // max == 1时返回空数组 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + int[] array2 = myArray.fibonacci(35); + int[] c = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() { + int[] a = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + // max <= 2的时候没有素数,数组为空数组 + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = { 6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() { + int[] Array0 = { 3, 5, 7, 8, 9 }; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = { 3 }; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + // 传递空数组时,返回空字符串 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..1aa396ce37 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/ArrayList.java @@ -0,0 +1,91 @@ + +package com.github.eloiseSJTU.coding2017.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + ensureCapacity(size + 1); + + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkBoundsForAdd(index); + + ensureCapacity(size + 1); + + if (index < size) { + System.arraycopy(elementData, index, elementData, index + 1, size - index); + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkBounds(index); + + return elementData[index]; + } + + public Object remove(int index) { + checkBounds(index); + + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + + private int cur; + + @Override + public boolean hasNext() { + return cur != size; + } + + @Override + public Object next() { + if (cur >= size) { + throw new NoSuchElementException(); + } + return elementData[cur++]; + } + + } + + private void ensureCapacity(int capacity) { + if (capacity > elementData.length) { + capacity = elementData.length << 1; + elementData = Arrays.copyOf(elementData, capacity); + } + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkBoundsForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..9aa88f2155 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode() {} + + public BinaryTreeNode(Integer data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + public Integer getData() { + return data; + } + + public void setData(Integer 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(Integer o) { + if (data == null) { + data = o; + } else { + if (o < data) { + if (left == null) { + left = new BinaryTreeNode(o, null, null); + } else { + left = left.insert(o); + } + } else { + if (right == null) { + right = new BinaryTreeNode(o, null, null); + } else { + right = right.insert(o); + } + } + } + return this; + } + + public void print() { + if (left != null) { + left.print(); + } + System.out.println(data); + if (right != null) { + right.print(); + } + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..c37495db66 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..19833b1853 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.github.eloiseSJTU.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkBoundsForAdd(index); + + if (index == 0) { + addFirst(o); + } else if (index == size) { + addLast(o); + } else { + Node cur = head; + while (--index > 0) { + cur = cur.next; + } + Node newNode = new Node(o, cur.next); + cur.next = newNode; + size++; + } + } + + public Object get(int index) { + checkBounds(index); + + Node cur = head; + while (index-- > 0) { + cur = cur.next; + } + return cur.data; + } + + public Object remove(int index) { + checkBounds(index); + + if (index == 0) { + return removeFirst(); + } else if (index == size - 1) { + return removeLast(); + } else { + Node cur = head; + int i = 0; + while (++i < index) { + cur = cur.next; + } + Node node = cur.next; + Object o = node.data; + cur.next = node.next; + node.data = null; + node.next = null; + size--; + return o; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, head); + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o, null); + if (head == null) { + head = newNode; + } else { + Node cur = head; + while (cur.next != null) { + cur = cur.next; + } + cur.next = newNode; + } + size++; + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + + Object o = head.data; + Node node = head.next; + head.data = null; + head.next = null; + head = node; + size--; + return o; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + + if (head.next == null) { + return removeFirst(); + } + + Node cur = head; + int index = 0; + while (++index < size - 1) { + cur = cur.next; + } + Object o = cur.next.data; + cur.next.data = null; + cur.next = null; + size--; + return o; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + + private Node cur = head; + + @Override + public boolean hasNext() { + return cur != null; + } + + @Override + public Object next() { + if (cur == null) { + throw new NoSuchElementException(); + } + Object o = cur.data; + cur = cur.next; + return o; + } + + } + + private static class Node { + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkBoundsForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java new file mode 100644 index 0000000000..2f6bdcc950 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/List.java @@ -0,0 +1,15 @@ +package com.github.eloiseSJTU.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(); + + public Iterator iterator(); +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java new file mode 100644 index 0000000000..96e6547548 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Queue.java @@ -0,0 +1,28 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class Queue { + + private int size = 0; + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.addLast(o); + size++; + } + + public Object deQueue() { + Object o = elementData.get(0); + elementData.removeFirst(); + size--; + return o; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java new file mode 100644 index 0000000000..22b5117842 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.github.eloiseSJTU.coding2017.basic; + +public class Stack { + + private int size = 0; + + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object o = elementData.get(--size); + elementData.remove(size); + return o; + } + + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..a2bd0cad69 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ArrayListTest.java @@ -0,0 +1,15 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import org.junit.Before; + +import com.github.eloiseSJTU.coding2017.basic.ArrayList; + + +public class ArrayListTest extends ListTest { + + @Before + public void setUpArrayList() { + aList = new ArrayList(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..3da3cf3fa0 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/BinaryTreeNodeTest.java @@ -0,0 +1,34 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +import com.github.eloiseSJTU.coding2017.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUpBinaryTreeNode() { + binaryTreeNode = new BinaryTreeNode(); + } + + @Test + public void testBinaryTreeNodeFunctional() { + binaryTreeNode.insert(4); + binaryTreeNode.insert(1); + binaryTreeNode.insert(3); + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + assertEquals(true, 4 == binaryTreeNode.getData()); + assertEquals(true, 1 == binaryTreeNode.getLeft().getData()); + assertEquals(true, 5 == binaryTreeNode.getRight().getData()); + assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData()); + assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData()); + binaryTreeNode.print(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..0ad905694c --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/LinkedListTest.java @@ -0,0 +1,93 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.eloiseSJTU.coding2017.basic.LinkedList; + +public class LinkedListTest extends ListTest{ + + private LinkedList aLinkedList; + + @Before + public void setUpLinkedList() { + aList = new LinkedList(); + aLinkedList = new LinkedList(); + } + + @Test + public void testAddFirst() { + aLinkedList.addFirst(5); + assertEquals(5, aLinkedList.get(0)); + + aLinkedList.addFirst(6); + assertEquals(6, aLinkedList.get(0)); + assertEquals(5, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testAddLast() { + aLinkedList.addLast("hello"); + assertEquals("hello", aLinkedList.get(0)); + + aLinkedList.addLast("world"); + assertEquals("hello", aLinkedList.get(0)); + assertEquals("world", aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + + @Test + public void testRemoveFirst() { + aLinkedList.addLast("hello"); + aLinkedList.addLast("world"); + + aLinkedList.removeFirst(); + assertEquals("world", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeFirst(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testRemoveLast() { + aLinkedList.addFirst("world"); + aLinkedList.addFirst("hello"); + + aLinkedList.removeLast(); + assertEquals("hello", aLinkedList.get(0)); + assertEquals(1, aLinkedList.size()); + + aLinkedList.removeLast(); + assertEquals(0, aLinkedList.size()); + } + + @Test + public void testLinkedListFunctional() { + for (int i=1; i<4; i++) { + aLinkedList.add(i); // [1,2,3] + } + aLinkedList.remove(1); // [1,3] + + aLinkedList.add(1, 0); // [1,0,3] + for (int i=4; i<6; i++) { + aLinkedList.addFirst(i); // [5, 4, 1, 0, 3] + } + assertEquals(5, aLinkedList.size()); + assertEquals(5, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(2)); + assertEquals(0, aLinkedList.get(3)); + + aLinkedList.remove(3); // [5, 4, 1, 3] + assertEquals(3, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeLast(); // [5, 4, 1] + assertEquals(1, aLinkedList.get(aLinkedList.size()-1)); + aLinkedList.removeFirst(); // [4,1] + + assertEquals(4, aLinkedList.get(0)); + assertEquals(1, aLinkedList.get(1)); + assertEquals(2, aLinkedList.size()); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java new file mode 100644 index 0000000000..956ecb9167 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/ListTest.java @@ -0,0 +1,120 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.eloiseSJTU.coding2017.basic.Iterator; +import com.github.eloiseSJTU.coding2017.basic.List;; + +public class ListTest { + + protected static List aList; + + @Test + public void testFunctional() { + aList.add(1); + aList.add(2); + assertEquals(1, aList.get(0)); + assertEquals(2, aList.get(1)); + + aList.add(3); + aList.add(0, 5); + aList.add(2, 11); + assertEquals(5, aList.get(0)); + assertEquals(11, aList.get(2)); + + aList.add("hi"); + assertEquals("hi", aList.get(5)); + assertEquals(6, aList.size()); + + aList.remove(1); + assertEquals(11, aList.get(1)); + assertEquals(2, aList.get(2)); + + assertEquals(5, aList.size()); + } + + @Test + public void testAdd() { + for (int i = 0; i < 1000; i++) + aList.add(i); + assertEquals(0, aList.get(0)); + assertEquals(100, aList.get(100)); + assertEquals(999, aList.get(999)); + } + + @Test + public void testRemove() { + aList.add(1); + aList.add(2); + aList.add(3); + int u = (Integer)aList.remove(2); + assertEquals(3, u); + assertEquals(2, aList.size()); + + aList.add(1, 5); + u = (Integer)aList.remove(0); + assertEquals(1, u); + assertEquals(5, aList.get(0)); + assertEquals(2, aList.get(1)); + assertEquals(2, aList.size()); + + aList.remove(0); + aList.remove(0); + assertEquals(0, aList.size()); + + + } + + @Test + public void testSize() { + for (int i = 0; i < 10; i++) + aList.add(i * 2); + assertEquals(10, aList.size()); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testException() { + expectedEx.expect(Exception.class); + aList.remove(1); + + aList.add(3); + + expectedEx.expect(Exception.class); + aList.add(2, 5); + } + + @Test + public void testIterator() { + Iterator it = aList.iterator(); + assertEquals(false, it.hasNext()); + + aList.add(1); + aList.add(2); + aList.add(3); + + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(2, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + aList.remove(1); + it = aList.iterator(); + assertEquals(true, it.hasNext()); + assertEquals(1, it.next()); + assertEquals(3, it.next()); + assertEquals(false, it.hasNext()); + + expectedEx.expect(Exception.class); + it.next(); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java new file mode 100644 index 0000000000..39023e8aaa --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/QueueTest.java @@ -0,0 +1,33 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; +import com.github.eloiseSJTU.coding2017.basic.Queue; + +public class QueueTest { + private Queue queue; + + @Before + public void setUpQueue() { + queue = new Queue(); + } + + @Test + public void testQueueFunctional() { + assertEquals(true, queue.isEmpty()); + queue.enQueue(4); + queue.enQueue(2); + assertEquals(2, queue.size()); + assertEquals(false, queue.isEmpty()); + + int i = (Integer)queue.deQueue(); + assertEquals(4, i); + i = (Integer)queue.deQueue(); + assertEquals(2, i); + + assertEquals(0, queue.size()); + assertEquals(true, queue.isEmpty()); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java new file mode 100644 index 0000000000..98d2bd9de9 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/basic/test/StackTest.java @@ -0,0 +1,40 @@ +package com.github.eloiseSJTU.coding2017.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.github.eloiseSJTU.coding2017.basic.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUpStack() { + stack = new Stack(); + } + + @Test + public void testStackFunctional() { + assertEquals(true, stack.isEmpty()); + stack.push(4); + stack.push(2); + assertEquals(2, stack.size()); + assertEquals(false, stack.isEmpty()); + + int i = (Integer)stack.pop(); + assertEquals(2, i); + + i = (Integer)stack.peek(); + assertEquals(4, i); + + i = (Integer)stack.pop(); + assertEquals(4, i); + + assertEquals(0, stack.size()); + assertEquals(true, stack.isEmpty()); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..f95e054d95 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java @@ -0,0 +1,46 @@ +package com.github.eloiseSJTU.coding2017.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; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..11df02b877 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java @@ -0,0 +1,81 @@ +package com.github.eloiseSJTU.coding2017.litestruts; + +import java.beans.PropertyDescriptor; +import java.io.File; +import java.lang.reflect.Field; +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(); + SAXReader saxReader = new SAXReader(); + try { + // 0. 读取配置文件struts.xml + Document document = saxReader.read(new File(getPackagePath() + "struts.xml")); + Element struts = document.getRootElement(); + for (Iterator i = struts.elementIterator("action"); i.hasNext();) { + Element action = (Element) i.next(); + // 1. 根据actionName找到相对应的class,例如LoginAction,通过反射实例化(创建对象) + if (actionName.equals(action.attributeValue("name"))) { + String className = action.attributeValue("class"); + Class clazz = Class.forName(className); + Object object = clazz.newInstance(); + // 根据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + // ("name"="test", "password"="1234"),那就应该调用 + // setName和setPassword方法 + for (Map.Entry entry : parameters.entrySet()) { + PropertyDescriptor descriptor = new PropertyDescriptor(entry.getKey(), clazz); + Method method = descriptor.getWriteMethod(); + method.invoke(object, entry.getValue()); + } + // 2. 通过反射调用对象的execute方法,并获得返回值 + Method excute = clazz.getMethod("execute"); + String result = (String) excute.invoke(object); + // 3. 通过反射找到对象的所有getter方法(例如 getMessage),通过反射来调用, + // 把值和属性形成一个HashMap,例如{"message": "登录成功"}, + // 放到View对象的parameters + Map map = new HashMap<>(); + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), clazz); + Method method = descriptor.getReadMethod(); + Object value = method.invoke(object); + map.put(field.getName(), value); + } + view.setParameters(map); + // 4. 根据struts.xml中的 配置,以及execute的返回值, + // 确定哪一个jsp放到View对象的jsp字段中。 + for (Iterator j = action.elementIterator("result"); j.hasNext();) { + Element element = (Element) j.next(); + if (result.equals(element.attributeValue("name"))) { + view.setJsp(element.getText()); + break; + } + } + break; + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + return view; + } + + private static String getPackagePath() { + String path = Struts.class.getResource("").toString().replace("/", File.separator); + if (path.startsWith("file")) { + path = path.substring(5); + } + return path; + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/View.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/View.java new file mode 100644 index 0000000000..093026d7c3 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/View.java @@ -0,0 +1,26 @@ +package com.github.eloiseSJTU.coding2017.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/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/struts.xml b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..ba5aa33139 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/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/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java new file mode 100644 index 0000000000..f52b71975f --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java @@ -0,0 +1,41 @@ +package com.github.eloiseSJTU.coding2017.litestruts.test; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.github.eloiseSJTU.coding2017.litestruts.Struts; +import com.github.eloiseSJTU.coding2017.litestruts.View; + +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/group02/953840070/src/com/github/lqingchenl/coding2017/array/ArrayUtil.java b/group02/953840070/src/com/github/lqingchenl/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..a221c781b5 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/array/ArrayUtil.java @@ -0,0 +1,296 @@ +package com.github.lqingchenl.coding2017.array; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +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; + + int[] b = new int[length]; + for (int i = 0; i < length; i++) { + b[i] = origin[i]; + } + for (int i = 0; i < length; i++) { + origin[length - i - 1] = b[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) { + if (oldArray == null) { + return null; + } + int length = oldArray.length; + for (int i : oldArray) { + if (i == 0) { + length--; + } + } + + int[] newArray = new int[length]; + int j = 0; + for (int i : oldArray) { + if (i != 0) { + newArray[j] = i; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + if (array1 == null || array1.length == 0) { + return array2; + } + if (array2 == null || array2.length == 0) { + return array1; + } + + Map map = new HashMap<>(); + + + int sameNum = 0; + for (int i : array1) { + for (int j : array2) { + if (i == j) { + sameNum++; + map.put(i, 1); + } + } + } + + int length = array1.length + array2.length - sameNum; + int[] newArray = new int[length]; + int index = 0; + + for (int i : array1) { + newArray[index] = i; + index++; + } + for (int j : array2) { + if (map.get(j) == null) { + newArray[index] = j; + index++; + } + } + + for (int i = 0; i < newArray.length - 1; i++) { + for (int j = i + 1; j < newArray.length; j++) { + if (newArray[i] > newArray[j]) { + int temp = newArray[j]; + newArray[j] = newArray[i]; + newArray[i] = temp; + } + } + } + + return newArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) throws Exception { + + if (oldArray == null) { + return null; + } + if (size < 0) { + throw new Exception("12"); + } + + int newLength = oldArray.length + size; + int[] newArray = new int[newLength]; + for (int i = 0; i < newLength; i++) { + if (i < oldArray.length) { + newArray[i] = oldArray[i]; + } else { + newArray[i] = 0; + } + + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max == 1) { + return new int[0]; + } + + int index = 0; + for (int i = 0; i < Integer.MAX_VALUE; i++) { + if (getFibonacci(i) > max) { + index = i; + break; + } + } + int[] array = new int[index]; + for (int i = 0; i < index; i++) { + array[i] = getFibonacci(i); + } + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max == 1) { + return new int[0]; + } + + int length = 0; + for (int i = 1; i <= max; i++) { + if (isPrime(i)) { + length++; + } + } + int[] array = new int[length]; + int j = 0; + for (int i = 1; i <= max; i++) { + if (isPrime(i)) { + array[j] = i; + j++; + } + } + + return array; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 1; i <= max; i++) { + int temp = 0; + for (int n = 1; n < i / 2 + 1; n++) { + if (i % n == 0) { + temp += n; + } + } + if (temp == i) { + stringBuffer.append(i).append("-"); + } + } + String[] strArray = stringBuffer.toString().split("-"); + int length = stringBuffer.toString().split("-").length; + int[] intAarray = new int[length]; + for (int i=0; i= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + + /** + * 往固定位置添加一个元素 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + if (get(index - 1) == null) { //原来为空,添加到指定位置 + add(o); + return; + } + size++; + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + } + + /** + * 获取元素 + * + * @param index + * @return + */ + public Object get(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引越界"); + } + return elementData[index]; + } + + /** + * 移除元素 + * + * @param index + * @return + */ + public Object remove(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引越界"); + } + Object deleteData = elementData[index]; + if (index == size - 1) { + elementData[index] = null; + } else { + int movedCount = size - index; + System.arraycopy(elementData, index + 1, elementData, index, movedCount); + } + size--; + return deleteData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..ebf29cf406 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/ArrayListTest.java @@ -0,0 +1,69 @@ +package com.github.lqingchenl.coding2017.basic; + +import com.github.lqingchenl.coding2017.basic.ArrayList; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * ArrayList Tester. + */ +public class ArrayListTest { + + private static ArrayList testArray = new ArrayList(); + + /** + * Method: add(Object o) + */ + @Test + public void testAddO() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(1, testArray.get(0)); + assertEquals(2, testArray.get(1)); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndexO() throws Exception { + testArray.add(1, 1); + testArray.add(2, 2); + assertEquals(1, testArray.get(0)); + assertEquals(2, testArray.get(1)); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + testArray.add(1); + assertEquals(1, testArray.get(0)); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(1, testArray.remove(0)); + assertEquals(2, testArray.remove(0)); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testArray.add(1); + testArray.add(2); + assertEquals(2, testArray.size()); + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Iterator.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..086e1cd342 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.lqingchenl.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedList.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..316cad2ee5 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedList.java @@ -0,0 +1,118 @@ +package com.github.lqingchenl.coding2017.basic; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + public void add(Object o) { + if (head == null) { + head = new Node(o); + } else { + Node nextNode = head; + while (nextNode.next != null) { + nextNode = nextNode.next; + } + nextNode.next = new Node(o); + } + size++; + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + return; + } + Node oldNode = getNode(index - 1); + Node newNode = new Node(o); + newNode.next = oldNode.next; + oldNode.next = newNode; + size++; + } + + public Object get(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + public Node getNode(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + public Object remove(int index) { + if (index == 1) { + return removeFirst(); + } + Node fatherNode = getNode(index - 2); + Node oldNode = getNode(index - 1); + fatherNode.next = oldNode.next; + size--; + + return oldNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + } + + public void addLast(Object o) { + if (head == null) { + addFirst(o); + return; + } + Node newNode = new Node(o); + Node lastNode = getNode(size - 1); + lastNode.next = newNode; + size++; + } + + public Object removeFirst() { + Node oldHead = head; + Node secondNode = head.next; + head = secondNode; + size--; + return oldHead.data; + } + + public Object removeLast() { + if (size == 1) { + return removeFirst(); + } + Object data = get(size - 1); + Node oldNode = getNode(size - 2); + oldNode.next = null; + size--; + 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/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..0ef6290d28 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/LinkedListTest.java @@ -0,0 +1,125 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class LinkedListTest { + + private static LinkedList testLinkedList = new LinkedList(); + + /** + * Method: add(Object o) + */ + @Test + public void testAdd() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + assertEquals(2, testLinkedList.get(1)); + } + + /** + * Method: add(int index, Object o) + */ + @Test + public void testAddForIndex() throws Exception { + testLinkedList.add(0, 0); + testLinkedList.add(1, 1); + testLinkedList.add(2, 2); + assertEquals(0, testLinkedList.get(0)); + assertEquals(1, testLinkedList.get(1)); + assertEquals(2, testLinkedList.get(2)); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + assertEquals(2, testLinkedList.get(1)); + } + + /** + * Method: getNode(int index) + */ + @Test + public void testGetNode() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertNotNull(testLinkedList.get(0)); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testLinkedList.add(1); + testLinkedList.add(2); + assertEquals(2, testLinkedList.size()); + } + + /** + * Method: addFirst(Object o) + */ + @Test + public void testAddFirst() throws Exception { + testLinkedList.addFirst(1); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: addLast(Object o) + */ + @Test + public void testAddLast() throws Exception { + testLinkedList.addLast(1); + assertEquals(1, testLinkedList.get(0)); + } + + /** + * Method: removeFirst() + */ + @Test + public void testRemoveFirst() throws Exception { + testLinkedList.addFirst(1); + testLinkedList.addFirst(2); + assertEquals(2, testLinkedList.removeFirst()); + assertEquals(1, testLinkedList.removeFirst()); + } + + /** + * Method: removeLast() + */ + @Test + public void testRemoveLast() throws Exception { + testLinkedList.addFirst(1); + assertEquals(1, testLinkedList.removeLast()); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + } + + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/List.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/List.java new file mode 100644 index 0000000000..d993812b9a --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.lqingchenl.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/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Queue.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Queue.java new file mode 100644 index 0000000000..eb7f7e3cb0 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Queue.java @@ -0,0 +1,28 @@ +package com.github.lqingchenl.coding2017.basic; + +public class Queue { + private LinkedList queue = new LinkedList(); + private int size; + + public void enQueue(Object o) { + queue.addLast(o); + size++; + } + + public Object deQueue() { + Object o = queue.removeFirst(); + size--; + return o; + } + + public boolean isEmpty() { + if (queue.size() == 0) + return true; + return false; + } + + public int size() { + return size; + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/QueueTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..9b26b3cdcf --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/QueueTest.java @@ -0,0 +1,57 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * Queue Tester. + */ +public class QueueTest { + + private static Queue testQueue = new Queue(); + + /** + * Method: enQueue(Object o) + */ + @Test + public void testEnQueue() throws Exception { + testQueue.enQueue(1); + assertEquals(1, testQueue.deQueue()); + } + + /** + * Method: deQueue() + */ + @Test + public void testDeQueue() throws Exception { + testQueue.enQueue(1); + testQueue.enQueue(2); + assertEquals(1, testQueue.deQueue()); + assertEquals(2, testQueue.deQueue()); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + testQueue.enQueue(1); + assertEquals(1, testQueue.deQueue()); + assertEquals(true, testQueue.isEmpty()); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testQueue.enQueue(1); + testQueue.enQueue(2); + assertEquals(2, testQueue.size()); + } + + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Stack.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Stack.java new file mode 100644 index 0000000000..8b25283b40 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/Stack.java @@ -0,0 +1,33 @@ +package com.github.lqingchenl.coding2017.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object o = elementData.get(size - 1); + elementData.remove(size - 1); + size--; + return o; + } + + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) + return true; + return false; + } + + public int size() { + return size; + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/basic/StackTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..b2d4935c4e --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/basic/StackTest.java @@ -0,0 +1,64 @@ +package com.github.lqingchenl.coding2017.basic; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * Stack Tester. + */ +public class StackTest { + + private static Stack testStack = new Stack(); + + /** + * Method: push(Object o) + */ + @Test + public void testPush() throws Exception { + testStack.push(1); + assertEquals(1, testStack.peek()); + } + + /** + * Method: pop() + */ + @Test + public void testPop() throws Exception { + testStack.push(1); + assertEquals(1, testStack.pop()); + } + + /** + * Method: peek() + */ + @Test + public void testPeek() throws Exception { + testStack.push(1); + assertEquals(1, testStack.peek()); + testStack.push(2); + assertEquals(2, testStack.peek()); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + testStack.push(1); + assertEquals(false, testStack.isEmpty()); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + testStack.push(1); + testStack.push(2); + assertEquals(2, testStack.size()); + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/LoginAction.java b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..efcb58f724 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/LoginAction.java @@ -0,0 +1,44 @@ +package com.github.lqingchenl.coding2017.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + */ +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; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/Struts.java b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..e8c6d54b6b --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/Struts.java @@ -0,0 +1,74 @@ +package com.github.lqingchenl.coding2017.litestruts; + +import org.jsoup.Jsoup; +import org.jsoup.helper.StringUtil; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; + +import java.beans.PropertyDescriptor; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + try { + Document d = Jsoup.parse(new File("src/com/github/lqingchenl/coding2017/litestruts/struts.xml"), "UTF-8"); //读取配置文件 + String classStr = null; + + Map resultMap = new HashMap<>(); + for (Element element : d.select("action")) { + if (element.attr("name").equals(actionName)) { + classStr = element.attr("class"); + for (Element element1 : element.select("result")) { + resultMap.put(element1.attr("name"), element1.text()); + } + } + } + + if (StringUtil.isBlank(classStr)) { + return null; + } + Class c = Class.forName(classStr); + Object object = c.newInstance(); //创建对象 + //写数据 setName和setPassword方法 + for (Map.Entry entry : parameters.entrySet()) { + PropertyDescriptor pd = new PropertyDescriptor(entry.getKey(), c);//使用java.beans.PropertyDescriptor获取Method进行方法调用 + Method method = pd.getWriteMethod();//获得写方法 + method.invoke(object, entry.getValue()); + } + + //通过反射,执行execute方法 + Method method = c.getDeclaredMethod("execute", null); + String result = (String) method.invoke(object); + +// 通过反射找到对象的所有getter方法(例如 getMessage),通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , +// 放到View对象的parameters + Map map = new HashMap<>(); + Field[] fields = c.getDeclaredFields(); + for (Field field : fields) { + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c); // message没有set方法,报错 + Method getMethod = pd.getReadMethod(); + String str = (String) getMethod.invoke(object); + map.put(field.getName(), str); + + } +// 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp,放到View对象的jsp字段中。 + View view = new View(); + view.setParameters(map); + String jsp = resultMap.get(result); + view.setJsp(jsp); + return view; + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/StrutsTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..e17b6154a2 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.github.lqingchenl.coding2017.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/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/View.java b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/View.java new file mode 100644 index 0000000000..487232ce7b --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/View.java @@ -0,0 +1,26 @@ +package com.github.lqingchenl.coding2017.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/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/struts.xml b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..73218d4675 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/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/group02/group02.md b/group02/group02.md index d3f5a12faa..8b13789179 100644 --- a/group02/group02.md +++ b/group02/group02.md @@ -1 +1 @@ - + diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayList.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayList.java" new file mode 100644 index 0000000000..4a68cd276b --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayList.java" @@ -0,0 +1,102 @@ +package com.byhieg.coding2017; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + + public void add(Object o) { + isCapacityEnough(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkForAdd(index); + isCapacityEnough(size + 1); + System.arraycopy(elementData,index,elementData,index + 1,size - index); + elementData[index] = o; + size++; + } + + private void checkForAdd(int index){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index不在指定范围内"); + } + + } + private void isCapacityEnough(int size) { + if (size > 100) { + explicitCapacity(size); + } + if (size < 0) { + throw new OutOfMemoryError(); + } + } + + private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8; + + public void explicitCapacity(int size) { + int newLength = elementData.length * 2; + if (newLength > (MAX_ARRAY_LENGTH)){ + newLength = (size > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH); + } + elementData = Arrays.copyOf(elementData, newLength); + + } + + + public Object get(int index) { + checkRange(index); + return elementData[index]; + } + + private void checkRange(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index不在范围内"); + } + } + + public Object remove(int index) { + Object o = get(index); + //要保证后面的 index + 1是有效的 + int moveSize = size - index - 1; + if (moveSize > 0) { + System.arraycopy(elementData,index + 1,elementData,index, size - index); + } + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + if (cursor >= size) { + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + } + + +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTree.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTree.java" new file mode 100644 index 0000000000..26407d749a --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTree.java" @@ -0,0 +1,66 @@ +package com.byhieg.coding2017; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ + +public class BinaryTree { + + private BinaryTreeNode root = new BinaryTreeNode(); + + public BinaryTree(Object rootData){ + root = root.insert(rootData); + } + + + //左边的值小于等于父节点的值,右边的值大于父节点的值 + private void insertNode(BinaryTreeNode root, BinaryTreeNode node) { + int value = (int)node.getData(); + int rootValue = (int)root.getData(); + if (value <= rootValue){ + insertLeft(root,node); + }else { + insertRight(root,node); + } + } + + + public void insert(Object o) { + BinaryTreeNode node = new BinaryTreeNode(); + node = node.insert(o); + insertNode(root,node); + } + + private void insertLeft(BinaryTreeNode father, BinaryTreeNode node) { + if (father.getLeft() == null) { + father.setLeft(node); + }else{ + insertNode(father.getLeft(),node); + } + } + + private void insertRight(BinaryTreeNode father, BinaryTreeNode node) { + if (father.getRight() == null) { + father.setRight(node); + } else { + insertNode(father.getRight(),node); + } + } + + //前序遍历输出书 + private void preOrder(BinaryTreeNode node) { + if (node != null) { + System.out.println(node.getData()); + preOrder(node.getLeft()); + preOrder(node.getRight()); + } + } + + + //打印树 + public void printTree(){ + preOrder(root); + } + +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTreeNode.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTreeNode.java" new file mode 100644 index 0000000000..67f70bb696 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/BinaryTreeNode.java" @@ -0,0 +1,42 @@ +package com.byhieg.coding2017; + +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) { + BinaryTreeNode node = new BinaryTreeNode(); + int value = (int)o; + node.setData(value); + node.setRight(null); + node.setLeft(null); + return node; + } + +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Iterator.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Iterator.java" new file mode 100644 index 0000000000..beef5b5554 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Iterator.java" @@ -0,0 +1,7 @@ +package com.byhieg.coding2017; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/LinkedList.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/LinkedList.java" new file mode 100644 index 0000000000..04b3f9f027 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/LinkedList.java" @@ -0,0 +1,152 @@ +package com.byhieg.coding2017; + +import javax.swing.text.html.HTMLDocument; + +public class LinkedList implements List { + + private Node head; + int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkRangeForAdd(index); + if (index == size) { + addLast(o); + } + Node nextNode = node(index); + Node newNode = new Node(o, nextNode); + + Node prevNode; + if (index == 0) { + prevNode = null; + } else { + prevNode = node(index); + } + + + if (prevNode == null) { + head = newNode; + }else{ + prevNode.next = newNode; + } + + size++; + } + + + private Node node(int index) { + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor; + } + + private void checkRangeForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("指定的index超过界限"); + } + } + + public Object get(int index) { + checkRange(index); + return node(index).data; + } + + private void checkRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("指定index超过界限"); + } + } + + public Object remove(int index) { + checkRange(index); + Node targetNode = node(index); + Object o = targetNode.data; + Node prevNode ; + Node nextNode = targetNode.next; + + if (index == 0) { + prevNode = null; + }else{ + prevNode = node(index - 1); + } + if (prevNode == null) { + head = nextNode; + targetNode.next = null; + }else { + prevNode.next = nextNode; + targetNode.next = null; + } + + targetNode.data = null; + size --; + return o; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node nextNode = head; + Node newNode = new Node(o, nextNode); + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o, null); + if (size == 0) { + head = newNode; + }else{ + Node lastNode = node(size - 1); + lastNode.next = newNode; + } + size++; + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size() - 1); + } + + public Iterator iterator() { + + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + public Node cursor = head; + @Override + public boolean hasNext() { + return cursor != null; + } + + @Override + public Object next() { + Object o = cursor.data; + cursor = cursor.next; + return o; + } + } + + + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/List.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/List.java" new file mode 100644 index 0000000000..15c27c8cf2 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/List.java" @@ -0,0 +1,13 @@ +package com.byhieg.coding2017; + +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/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Queue.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Queue.java" new file mode 100644 index 0000000000..f83ad337e7 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Queue.java" @@ -0,0 +1,23 @@ +package com.byhieg.coding2017; + +public class Queue { + + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.addLast(o); + } + + public Object deQueue() { + Object value = list.get(0); + list.removeFirst(); + return value; + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Stack.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Stack.java" new file mode 100644 index 0000000000..45b8530a8f --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/Stack.java" @@ -0,0 +1,36 @@ +package com.byhieg.coding2017; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (size() == 0) { + throw new EmptyStackException(); + } + Object value = elementData.get(size() - 1); + elementData.remove(size() - 1); + return value; + } + + public Object peek(){ + if (size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayListTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayListTest.java" new file mode 100644 index 0000000000..b0b3b6704d --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayListTest.java" @@ -0,0 +1,81 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.ArrayList; +import com.byhieg.coding2017.Iterator; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class ArrayListTest extends TestCase { + ArrayList arrayList = new ArrayList(); + + public void testAdd() throws Exception { + arrayList.add(1); + arrayList.add(null); + arrayList.add(-1); + arrayList.add("1"); + arrayList.add(true); + arrayList.add(Integer.MAX_VALUE); + arrayList.add(Integer.MIN_VALUE); + + + } + + public void testAdd1() throws Exception { +// arrayList.add(-1,0); +// arrayList.add(100,0); + arrayList.add(0,2); + arrayList.add(1,10); + arrayList.add(2,111); + } + + public void testGet() throws Exception { + for (int i = 0; i < 10 ; i++) { + arrayList.add(i); + } + + for (int i = 0 ; i < 10 ; i++) { + System.out.println(arrayList.get(i)); + } + } + + public void testRemove() throws Exception { + for (int i = 0; i < 10 ; i++) { + arrayList.add(i); + } + + for (int i = 0 ; i < 10 ; i++) { + System.out.println(arrayList.get(i)); + } + + for (int i = 0 ; i < 10 ; i++) { + arrayList.remove(9 - i); + } + + for (int i = 0 ; i < arrayList.size() ; i++) { + System.out.println(arrayList.get(i)); + } + } + + public void testSize() throws Exception { + for (int i = 0; i < 10 ; i++) { + arrayList.add(i); + } + System.out.println(arrayList.size()); + } + + public void testIterator() throws Exception { + for (int i = 0; i < 10 ; i++) { + arrayList.add(i); + } + + System.out.println("开始测试Iterator"); + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} \ No newline at end of file diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/BinaryTreeTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/BinaryTreeTest.java" new file mode 100644 index 0000000000..1945f2c695 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/BinaryTreeTest.java" @@ -0,0 +1,24 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.BinaryTree; +import com.byhieg.coding2017.BinaryTreeNode; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class BinaryTreeTest extends TestCase { + + public void testPrintTree() throws Exception { + BinaryTree tree = new BinaryTree(5); + tree.insert(2); + tree.insert(7); + tree.insert(1); + tree.insert(6); + tree.insert(4); + tree.insert(8); + tree.printTree(); + } + +} \ No newline at end of file diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/LinkedListTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/LinkedListTest.java" new file mode 100644 index 0000000000..61a78e150a --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/LinkedListTest.java" @@ -0,0 +1,122 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.Iterator; +import com.byhieg.coding2017.LinkedList; +import com.sun.org.apache.bcel.internal.generic.INEG; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class LinkedListTest extends TestCase { + private LinkedList list = new LinkedList(); + public void testAdd() throws Exception { + list.add(null); + list.add(-1); + list.add(-2); + list.add(0x5); + list.add(true); + list.add("123"); + list.add(Integer.MAX_VALUE + 100000); + + } + + public void testAdd1() throws Exception { +// list.add(-1,100); +// list.add(20,111); + list.add(0,11); + list.add(1,"sad"); + list.add(2,"fas"); + + } + + public void testGet() throws Exception { + for (int i = 0 ; i < 10 ; i++) { + list.add(i,i + ""); + } + + for (int i = 0 ;i < list.size();i++) { + System.out.println(list.get(i)); + } + } + + public void testRemove() throws Exception { + for (int i = 0 ; i < 10 ; i++) { + list.add(i,i + ""); + } + + for (int i = 0 ; i < list.size() ; i++) { + list.remove(i); + } + + for (int i = 0 ;i < list.size();i++) { + System.out.println(list.get(i)); + } + } + + + public void testAddFirst() throws Exception { + list.addFirst("byhieg"); + list.addFirst("123412"); + list.addFirst("byhaieg"); + list.addFirst("byhfadas12ieg"); + list.addFirst("fas"); + for (int i = 0 ; i < list.size();i++) { + System.out.println(list.get(i)); + } + } + + public void testAddLast() throws Exception { + list.addLast("asga"); + list.addLast("124"); + list.addLast("fasd"); + list.addLast("fas"); + list.addLast("gasd2"); + + for (int i = 0 ; i < list.size();i++) { + System.out.println(list.get(i)); + } + + } + + public void testRemoveFirst() throws Exception { + list.addFirst("byhieg"); + list.addFirst("123412"); + list.addFirst("byhaieg"); + list.addFirst("byhfadas12ieg"); + list.addFirst("fas"); + for (int i = 0 ; i < list.size();i++) { + list.removeLast(); + } + + System.out.println(list.size()); + } + + public void testRemoveLast() throws Exception { + list.addLast("asga"); + list.addLast("124"); + list.addLast("fasd"); + list.addLast("fas"); + list.addLast("gasd2"); + for (int i = 0 ; i < list.size();i++) { + list.removeFirst(); + } + + System.out.println(list.size()); + } + + public void testIterator() throws Exception { + list.addLast("asga"); + list.addLast("124"); + list.addLast("fasd"); + list.addLast("fas"); + list.addLast("gasd2"); + + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} \ No newline at end of file diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/QueueTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/QueueTest.java" new file mode 100644 index 0000000000..82d0fe2349 --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/QueueTest.java" @@ -0,0 +1,34 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.Queue; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class QueueTest extends TestCase { + Queue queue = new Queue(); + + public void testEnQueue() throws Exception { + queue.enQueue(1); + queue.enQueue("true"); + queue.enQueue(true); + queue.enQueue(null); + queue.enQueue(-12341); + queue.enQueue(Integer.MIN_VALUE - 10000); + + } + + public void testDeQueue() throws Exception { + for (int i = 0 ; i < 10 ; i++) { + queue.enQueue(i); + } + + while (!queue.isEmpty()) { + System.out.println(queue.deQueue()); + } + } + + +} \ No newline at end of file diff --git "a/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/StackTest.java" "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/StackTest.java" new file mode 100644 index 0000000000..a81484251c --- /dev/null +++ "b/group03/1196051822/2\346\234\21026\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/StackTest.java" @@ -0,0 +1,52 @@ +package com.byhieg.coding2017test; + +import com.byhieg.coding2017.Stack; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/2/22. + * Mail to byhieg@gmail.com + */ +public class StackTest extends TestCase { + Stack stack = new Stack(); + + public void testPush() throws Exception { + stack.push(1); + stack.push("31231"); + stack.push(null); + stack.push(Integer.MAX_VALUE + 1000); + stack.push(Integer.MIN_VALUE - 1000000); + stack.push(true); + stack.push('a'); + } + + public void testPop() throws Exception { + int a = 1; + for (int i = 0; i < 10; i++) { + stack.push(a + i); + } + int size = stack.size(); + while (!stack.isEmpty()){ + System.out.println(stack.pop()); + } + } + + public void testPeek() throws Exception { + char a = 'a'; + for (int i = 0; i < 10; i++) { + stack.push(a + i); + } + + System.out.println("size的大小是" + stack.size()); + System.out.println(stack.peek()); + } + + public void testIsEmpty() throws Exception { + System.out.println(stack.isEmpty()); + stack.push(1); + System.out.println(stack.isEmpty()); + + } + + +} \ No newline at end of file diff --git a/group03/1196051822/README b/group03/1196051822/README new file mode 100644 index 0000000000..c7b21c2ef0 --- /dev/null +++ b/group03/1196051822/README @@ -0,0 +1,2 @@ +# 作业文件夹说明 +src文件夹存放是的作业源码的文件,test文件夹存放的是源码相应的测试文件 diff --git a/group03/1360464792/.gitignore b/group03/1360464792/.gitignore new file mode 100644 index 0000000000..b3c9df97d1 --- /dev/null +++ b/group03/1360464792/.gitignore @@ -0,0 +1,34 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# 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* + + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### maven +target \ No newline at end of file diff --git a/group03/1360464792/pom.xml b/group03/1360464792/pom.xml new file mode 100644 index 0000000000..5fed7b15ab --- /dev/null +++ b/group03/1360464792/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + rui.study + coding2017 + 0.0.1-SNAPSHOT + + 学习数据结构 + + + 1.6 + UTF-8 + + + + + junit + junit + 4.12 + + + + + + coding2017 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + ${java.version} + ${java.version} + ${encoding} + + + + + \ No newline at end of file diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java b/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java new file mode 100644 index 0000000000..4040049ed8 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java @@ -0,0 +1,142 @@ +package rui.study.coding2017; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size; + + private Object[] elementData; + + private static Object[] emptyObjects={}; + + private static int defaultCapacity=10; + + public void add(Object o){ + ensureCapacity(this.size+1); + elementData[size++]=o; + } + + public void add(int index, Object o){ + rangeCheckForAdd(index); + if(elementData[index]!=null){ + ensureCapacity(this.size+1); + //执行数组拷贝 + System.arraycopy(elementData,index,elementData,index+1,size-index); + size++; + } + elementData[index]=o; + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object object=elementData[index]; + + int numMoved=size-index-1; + //如果是最后一位remove ,无需进行数组拷贝 + if(numMoved>0){ + System.arraycopy(elementData, index+1, elementData, index,numMoved); + } + elementData[--size]=null; + return object; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public ArrayList(){ + this.size=0; + this.elementData=emptyObjects; + } + + public ArrayList(int size){ + this.size=size; + if(size>0){ + this.elementData=new Object[size]; + }else if(size==0){ + this.elementData=emptyObjects; + }else{ + throw new IllegalArgumentException("非法容器大小 "+size); + } + + } + + /** + * 判断索引是否合法 + * @param index 索引 + */ + private void rangeCheckForAdd(int index) { + if(index>size||index<0)throw new IndexOutOfBoundsException("索引为"+index+",但是当前数组长度为:"+size); + } + + /** + * 判断索引是否合法 , + * @param index 索引 + */ + private void rangeCheck(int index) { + if(index>=size||index<0)throw new IndexOutOfBoundsException("索引为"+index+",但是当前数组长度为:"+size); + } + + + /** + * 确保当前数组能够长度能够容纳新的对象,如果不够,就自行增长 + * @param needLength 需要的数组长度 + */ + private void ensureCapacity(int needLength) { + if(elementData==emptyObjects){ + needLength = Math.max(defaultCapacity, needLength); + } + + if(needLength-elementData.length>0){ + this.grow(needLength); + } + } + + /** + * 数组扩容 + * @param needLength 需要的长度 + */ + private void grow(int needLength) { + int elementLength=elementData.length; + //扩容1.5倍 + int newLength=elementLength+(elementLength>>1); + + if(needLength-newLength>0){ + newLength=needLength; + } + this.elementData= Arrays.copyOf(this.elementData,newLength); + } + + private class ArrayListIterator implements Iterator{ + //游标,当前迭代器执行到何处了 + private int cursor=0; + + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + if (cursor >= size)throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + return elementData[cursor++]; + } + } + + + + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java new file mode 100644 index 0000000000..7d63153ffb --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java @@ -0,0 +1,93 @@ +package rui.study.coding2017; + +/** + * 二叉树 + * Created by 赵睿 on 2017/2/25. + */ +public class BinaryTree { + private BinaryTreeNode root; + + private int size; + + public void insert(Comparable comparable){ + BinaryTreeNode binaryTreeNode=new BinaryTreeNode(comparable); + + if(this.root==null){ + this.root=binaryTreeNode; + }else { + boolean flag=false; + BinaryTreeNode cursorNode=root; + while(!flag){ + if(comparable.compareTo(cursorNode.getData())<0){ + if(cursorNode.getLeft()==null){ + cursorNode.setLeft(binaryTreeNode); + flag=true; + }else{ + cursorNode=cursorNode.getLeft(); + } + }else { + if(cursorNode.getRight()==null){ + cursorNode.setRight(binaryTreeNode); + flag=true; + }else{ + cursorNode=cursorNode.getRight(); + } + } + + } + } + size++; + } + + public LinkedList inorder(){ + LinkedList linkedList=new LinkedList(); + sortLeft(linkedList,root); + sortRight(linkedList,root); + return linkedList; + } + + private void sortRight(LinkedList linkedList,BinaryTreeNode binaryTreeNode){ + Queue queue=getRightList(binaryTreeNode); + while(!queue.isEmpty()){ + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList,queueNode); + } + + } + + private void sortLeft(LinkedList linkedList,BinaryTreeNode binaryTreeNode){ + Stack stack=getLeftList(binaryTreeNode); + while(!stack.isEmpty()) { + BinaryTreeNode stackNode = (BinaryTreeNode) stack.pop(); + linkedList.add(stackNode.getData()); + Queue queue = getRightList(stackNode); + while (!queue.isEmpty()) { + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList,queueNode); + } + } + linkedList.add(binaryTreeNode.getData()); + } + + + private Stack getLeftList(BinaryTreeNode binaryTreeNode){ + Stack stack=new Stack(); + while(binaryTreeNode.getLeft()!=null){ + binaryTreeNode=binaryTreeNode.getLeft(); + stack.push(binaryTreeNode); + } + return stack; + } + + private Queue getRightList(BinaryTreeNode binaryTreeNode){ + Queue queue=new Queue(); + while(binaryTreeNode.getRight()!=null){ + binaryTreeNode=binaryTreeNode.getRight(); + queue.enQueue(binaryTreeNode); + } + return queue; + } + + + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java new file mode 100644 index 0000000000..3895133f17 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java @@ -0,0 +1,40 @@ +package rui.study.coding2017; + +public class BinaryTreeNode { + + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Comparable getData() { + return data; + } + public void setData(Comparable 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() { + } + + public BinaryTreeNode(Comparable data) { + this.data = data; + } + + public BinaryTreeNode(Comparable data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java b/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java new file mode 100644 index 0000000000..dddde983c6 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java @@ -0,0 +1,8 @@ +package rui.study.coding2017; + + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java b/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java new file mode 100644 index 0000000000..f0a04cd32f --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java @@ -0,0 +1,156 @@ +package rui.study.coding2017; + +/** + * 单向链表 + */ +public class LinkedList { + private Node head; + + private Node current; + + private int size; + + public LinkedList(){ + } + + public int size(){ + return size; + } + + public void add(Object o){ + Node newNode=new Node(o,null); + if(size==0){ + head=current=newNode; + } + current.next=newNode; + current=newNode; + size++; + } + + public void add(int index , Object o){ + checkIndexForAdd(index); + if(index==size){ + add(o); + }else{ + Node newNode=new Node(o,null); + if(index==0){ + newNode.next=head; + head=newNode; + }else{ + Node after=getIndexNode(index); + Node before=getIndexNode(index-1); + before.next=newNode; + newNode.next=after; + } + size++; + } + } + + + public Object get(int index){ + return getIndexNode(index).data; + } + + public void addFirst(Object obj){ + add(0,obj); + } + public void addLast(Object obj){ + if(size==0){ + add(obj); + }else { + add(size,obj); + } + } + + public Object remove(int index){ + checkIndex(index); + Node needRemove; + if(index==0){ + needRemove=head; + if(size==1){ + head=null; + }else{ + head=head.next; + } + }else{ + needRemove=getIndexNode(index); + Node before=getIndexNode(index-1); + before.next=needRemove.next; + if(index==size-1){ + current=before; + } + } + size--; + return needRemove.data; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + + public class LinkedListIterator implements Iterator{ + + private int cursor=0; + + private Node cursorNode=head; + + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public Object next() { + Object object=cursorNode.data; + cursorNode=cursorNode.next; + cursor++; + return object; + } + } + + private void checkIndexForAdd(int index){ + if(!(index>=0&&index<=size)){ + throw new IndexOutOfBoundsException("索引"+index+"越界!"); + } + } + private void checkIndex(int index){ + if(!(index>=0&&index>>>>>>>>>>>>>>>>>>>>>>>>>"); + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + + @Test + public void addFirst() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.addFirst(-1); + + System.out.println(linkedList.size()); + + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + @Test + public void addLast() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.addLast(2); + System.out.println(linkedList.size()); + + Iterator iterator=linkedList.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + + + } + + @Test + public void remove() throws Exception { + LinkedList linkedList=new LinkedList(); + linkedList.add(0,0); + linkedList.add(1,1); + System.out.println(linkedList.size()); + try { + linkedList.remove(2); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + linkedList.remove(1); + System.out.println(linkedList.size()); + linkedList.remove(0); + System.out.println(linkedList.size()); + try { + linkedList.remove(0); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void removeFirst() throws Exception { + LinkedList linkedList=new LinkedList(); + try { + linkedList.removeFirst(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.removeFirst(); + System.out.println(linkedList.size()); + + } + + @Test + public void removeLast() throws Exception { + LinkedList linkedList=new LinkedList(); + try { + linkedList.removeLast(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + linkedList.add(0,0); + linkedList.add(1,1); + linkedList.removeLast(); + System.out.println(linkedList.size()); + + } +} \ No newline at end of file diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java new file mode 100644 index 0000000000..baff49411c --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java @@ -0,0 +1,39 @@ +package rui.study.coding2017; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * 测试队列 + * Created by 赵睿 on 2017/2/25. + */ +public class QueueTest { + @Test + public void enQueue() throws Exception { + Queue queue=new Queue(); + queue.enQueue(1); + queue.enQueue(2); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + } + + + @Test + public void isEmpty() throws Exception { + Queue queue=new Queue(); + System.out.println(queue.isEmpty()); + queue.enQueue(1); + System.out.println(queue.isEmpty()); + } + + @Test + public void size() throws Exception { + Queue queue=new Queue(); + System.out.println(queue.size()); + queue.enQueue(1); + System.out.println(queue.size()); + } + +} \ No newline at end of file diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java new file mode 100644 index 0000000000..115a16f1fc --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java @@ -0,0 +1,54 @@ +package rui.study.coding2017; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * 测试栈 + * Created by 赵睿 on 2017/2/25. + */ +public class StackTest { + @Test + public void push() throws Exception { + Stack stack=new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + + } + + @Test + public void peek() throws Exception { + Stack stack=new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println(stack.peek()); + System.out.println(stack.peek()); + + + } + + @Test + public void isEmpty() throws Exception { + Stack stack=new Stack(); + System.out.println(stack.isEmpty()); + stack.push(1); + System.out.println(stack.isEmpty()); + + } + + @Test + public void size() throws Exception { + Stack stack=new Stack(); + System.out.println(stack.size()); + stack.push(1); + System.out.println(stack.size()); + } + +} \ No newline at end of file diff --git a/group03/172487938/Iterator.java b/group03/172487938/Iterator.java new file mode 100644 index 0000000000..e6187ad2fd --- /dev/null +++ b/group03/172487938/Iterator.java @@ -0,0 +1,7 @@ +package 基本数据结构; + +public interface Iterator { + public boolean hasNext(); + public E next(); + public void remove(); +} diff --git a/group03/172487938/MyArrayList.java b/group03/172487938/MyArrayList.java new file mode 100644 index 0000000000..0c00a277ff --- /dev/null +++ b/group03/172487938/MyArrayList.java @@ -0,0 +1,186 @@ +package 基本数据结构; + + +/** + * Created by LIANG on 2017/2/24. + */ +public class MyArrayList implements MyList +{ + public static final int INITIAL_CAPACITTY = 16; + private E[] data = (E[]) new Object[INITIAL_CAPACITTY]; + private static int size = 0; + + public MyArrayList(E[] objects) + { + + for (int i = 0; i < objects.length; i++) + { + add(objects[i]); + size++; + } + } + + public MyArrayList() {} + + ; + + public void ensureCapicity() + { + if (size >= data.length) + { + E[] newData = (E[]) new Object[size * 2]; + System.arraycopy(data, 0, newData, 0, data.length); + data = newData; + } + } + +// public boolean add(E e) +// { +// ensureCapicity(); +// if(e != null) +// data[size++] = e; +// return true; +// } + + public void add(E e) + { + add(size, e); + } + + @Override + public void add(int index, E e) + { + ensureCapicity(); + for (int i = size - 1; i >= index; i--) + { + data[i + 1] = data[i]; + } + data[index] = e; + size++; + } + + @Override + public E get(int index) + { + return data[index]; + } + + @Override + public E remove(int index) + { + checkIndex(index); + E e = data[index]; + for (int i = index; i < data.length - 1; i++) + data[i] = data[i + 1]; + + data[size - 1] = null; + size--; + return e; + + } + + private void checkIndex(int index) + { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("输入下标有误"); + } + + @Override + public int size() + { + return size; + } + + @Override + public void clear() + { + data = (E[]) new Object[INITIAL_CAPACITTY]; + size = 0; + } + + @Override + public int indexOf(E e) + { + for (int i = 0; i < size; i++) + { + if (e.equals(data[i])) + return i; + } + return -1; + } + + + @Override + public boolean isEmpty() + { + return size == 0; + } + + @Override + public E set(int index, E e) + { + checkIndex(index); + E temp = data[index]; + data[index] = e; + return temp; + } + + @Override + public boolean contains(E e) + { + for (int i = 0; i < size; i++) + { + if(data[i].equals(e)) + return true; + } + return false; + } + + public void trimToSize() + { + if (size != data.length) + { + E[] newData = (E[]) new Object[size]; + System.arraycopy(data, 0, newData, 0, size); + data = newData; + } + } + + @Override + public String toString() + { + StringBuilder result = new StringBuilder("["); + + for (int i = 0; i < size; i++) + { + result.append(data[i]); + if (i < size - 1) + result.append(","); + } + return result.toString() + "]"; + } + + private class ArrayListIterator implements Iterator + { + private int current = 0; + + @Override + public boolean hasNext() + { + return (current < size); + } + + @Override + public E next() + { + return data[current++]; + } + + @Override + public void remove() + { + MyArrayList.this.remove(current); + } + } + +} diff --git a/group03/172487938/MyLinkedList.java b/group03/172487938/MyLinkedList.java new file mode 100644 index 0000000000..609ab8c81d --- /dev/null +++ b/group03/172487938/MyLinkedList.java @@ -0,0 +1,274 @@ +package 基本数据结构; + +public class MyLinkedList implements MyList +{ + private Node head, tail; + private int size; + + public MyLinkedList() {} + + public MyLinkedList(E[] objects) + { + for (int i = 0; i < objects.length; i++) + { + add(objects[i]); +// size++; + } + } + + public E getFirst() + { + if (size == 0) + return null; + else + return head.element; + } + + public E getLast() + { + if (size == 0) + return null; + else + return tail.element; + } + + public void addFirst(E e) + { + Node newNode = new Node<>(e); + newNode.next = head; + head = newNode; + size++; + + if (tail == null) + tail = head; + } + + public void addLast(E e) + { + Node newNode = new Node<>(e); + + if (tail == null) + head = tail = newNode; + else + { + tail.next = newNode; + tail = tail.next; + } + size++; + } + + public void add(E e) + { + add(size, e); + } + + @Override + public void add(int index, E e) + { + if (index == 0) + { + addFirst(e); + } else if (index >= size) + { + addLast(e); + } else + { + Node current = head; + for (int i = 1; i < index; i++) + { + current = current.next; + } + Node temp = current.next; + current.next = new Node(e); + (current.next).next = temp; + size++; + } + } + + @Override + public E get(int index) + { + if (index < 0 || index > size - 1) + return null; + + Node current = head; + for (int i = 0; i < index; i++) + current = current.next; + + return current.element; + } + + @Override + public E remove(int index) + { + if (index < 0 || index >= size) + return null; + else if (index == 0) + { + E e = removeFirst(); + return e; + } else if (index == size - 1) + return removeLast(); + else + { + Node previous = head; + + for (int i = 1; i < index; i++) + { + previous = previous.next; + } + + Node current = previous.next; + previous.next = current.next; + size--; + return current.element; + } + } + + public E removeFirst() + { + if (size == 0) + return null; + else + { + Node temp = head; + head = head.next; + size--; + if (head == null) + tail = null; + return temp.element; + } + } + + public E removeLast() + { + if (size == 0) + return null; + else if (size == 1) + { + Node temp = head; + head = tail = null; + size = 0; + return temp.element; + } else + { + Node current = head; + + for (int i = 0; i < size - 2; i++) + current = current.next; + + Node temp = tail; + tail = current; + tail.next = null; + size--; + return temp.element; + } + } + + @Override + public int size() + { + return size; + } + + @Override + public void clear() + { + size = 0; + head = tail = null; + } + + @Override + public int indexOf(E e) + { + Node current = head; + for (int i = 0; i < size; i++) + { + if (current.element.equals(e)) + return i; + current = current.next; + } + return -1; + } + + @Override + public boolean isEmpty() + { + if (size != 0) + return false; + else + return true; + } + + @Override + public E set(int index, E e) + { + if (index < 0 || index > size - 1) + return null; + + Node current = head; + for (int i = 0; i < index; i++) + current = current.next; + + E temp = current.element; + current.element = e; + + return temp; + } + + @Override + public boolean contains(E e) + { + Node current = head; + for (int i = 0; i < size; i++) + { + if (current.element.equals(e)) + return true; + current = current.next; + } + return false; + } + + @Override + public String toString() + { + StringBuilder result = new StringBuilder("["); + + if (size == 0) + return null; + else + { + Node current = head; + for (int i = 0; i < size; i++) + { + try + { + result.append(current.element); + } catch (Exception e) + { + e.printStackTrace(); + } + current = current.next; + if (current != null) + { + result.append(", "); + } else + { + result.append("]"); + } + } + return result.toString(); + } + } + + public static class Node + { + E element; + Node next; + + public Node(E e) + { + this.element = e; + } + } +} \ No newline at end of file diff --git a/group03/172487938/MyList.java b/group03/172487938/MyList.java new file mode 100644 index 0000000000..5d16147855 --- /dev/null +++ b/group03/172487938/MyList.java @@ -0,0 +1,27 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/24. + */ +public interface MyList +{ + + public void add(int index, E e); + + public E get(int index); + + public E remove(int index); + + public int size(); + + public void clear(); + + + public int indexOf(E e); + + public boolean isEmpty(); + + public E set(int index, E e); + + public boolean contains(E e); +} diff --git a/group03/172487938/Queue.java b/group03/172487938/Queue.java new file mode 100644 index 0000000000..af335f5274 --- /dev/null +++ b/group03/172487938/Queue.java @@ -0,0 +1,32 @@ +package 基本数据结构; + +import java.util.LinkedList; + +public class Queue +{ + + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) + { + list.addLast(o); + } + + public Object deQueue() + { + return list.removeFirst(); + } + + public boolean isEmpty() + { + if(list.size() != 0) + return true; + else + return false; + } + + public int size() + { + return list.size(); + } +} diff --git a/group03/172487938/Stack.java b/group03/172487938/Stack.java new file mode 100644 index 0000000000..f1b67822b1 --- /dev/null +++ b/group03/172487938/Stack.java @@ -0,0 +1,37 @@ +package 基本数据结构; + +import java.util.ArrayList; + +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + public void push(Object o) + { + elementData.add(o); + } + + public Object pop() + { + Object popElement = elementData.remove(elementData.size() - 1); + return popElement; + } + + public Object peek() + { + Object peekElement = elementData.get(size() - 1); + return peekElement; + } + + public boolean isEmpty() + { + if(elementData.size() != 0) + return false; + return true; + } + + public int size() + { + return elementData.size(); + } +} diff --git a/group03/172487938/TestMyArrayList.java b/group03/172487938/TestMyArrayList.java new file mode 100644 index 0000000000..dbf8b610e1 --- /dev/null +++ b/group03/172487938/TestMyArrayList.java @@ -0,0 +1,36 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/24. + */ +public class TestMyArrayList +{ + public static void main(String[] args) + { + MyArrayList list = new MyArrayList<>(); + list.add("America"); + System.out.println(list); + + list.add("Canada"); + System.out.println(list); + + list.add("Tokoy"); + System.out.println(list); + + list.add("Shanghai"); + + + int size = list.size(); + System.out.println(size); + list.add(0,"China"); + System.out.println(list); + String[] str = {"Japan","England","France","HonKong","BeiJing"}; + for(String s:str) + list.add(s); + System.out.println(list); + + list.remove(3); + System.out.println(list); + + } +} diff --git a/group03/172487938/TestMyLinkedList.java b/group03/172487938/TestMyLinkedList.java new file mode 100644 index 0000000000..cd412ef646 --- /dev/null +++ b/group03/172487938/TestMyLinkedList.java @@ -0,0 +1,19 @@ +package 基本数据结构; + +/** + * Created by LIANG on 2017/2/25. + */ +public class TestMyLinkedList +{ + public static void main(String[] args) + { + String[] name = {"Tom", "George", "Peter", "Jean", "George", "Jane"}; + MyList list = new MyLinkedList(name); + + System.out.println(list.contains("George")); + System.out.println(list.get(3)); + System.out.println(list.indexOf("George")); + list.set(4, "Michael"); + System.out.println(list); + } +} diff --git a/group03/1753176091/bin/.gitignore b/group03/1753176091/bin/.gitignore new file mode 100644 index 0000000000..c2d9872a16 --- /dev/null +++ b/group03/1753176091/bin/.gitignore @@ -0,0 +1 @@ +/com/ diff --git a/group03/1753176091/src/com/coding/basic/ArrayList.java b/group03/1753176091/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..88e9ec8ce0 --- /dev/null +++ b/group03/1753176091/src/com/coding/basic/ArrayList.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size++] = o; + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity > 100) { + grow(elementData); + } + } + + private void grow(Object[] elementData) { + int oldLength = elementData.length; + int newLength = oldLength * 2; + Arrays.copyOf(elementData, newLength); + } + + public void add(int index, Object o) { + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + } + + public Object get(int index) { + if (index < 0 || index > elementData.length) { + throw new IndexOutOfBoundsException(); + } + return (Object) elementData[index]; + } + + public Object remove(int index) { + int lowLength = size - index - 1; + System.arraycopy(elementData, index + 1, elementData, index, lowLength); + elementData[--size] = null; + return (Object) elementData[index]; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + +} diff --git a/group20/423184723/src/com/coding/basic/BinaryTreeNode.java b/group03/1753176091/src/com/coding/basic/BinaryTreeNode.java similarity index 100% rename from group20/423184723/src/com/coding/basic/BinaryTreeNode.java rename to group03/1753176091/src/com/coding/basic/BinaryTreeNode.java diff --git a/group03/1753176091/src/com/coding/basic/FileUtil.java b/group03/1753176091/src/com/coding/basic/FileUtil.java new file mode 100644 index 0000000000..856abf7249 --- /dev/null +++ b/group03/1753176091/src/com/coding/basic/FileUtil.java @@ -0,0 +1,68 @@ +package com.coding.basic; + + + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +public class FileUtil { + + public static String byteToHexString(byte[] codes ){ + + StringBuffer buffer = new StringBuffer(); + + for(int i=0;i= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + Node newHead = new Node(o, null); + Node f = head; + for (int i = 0; i < index - 1; i++) { + f = f.next; + } + newHead.next = f.next; + f.next = newHead; + size++; + } + + public Object get(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + Node f = head; + for (int i = 0; i < index; i++) { + f = f.next; + } + return f.data; + } + + public Object remove(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); + } + Node f = head; + for (int i = 0; i < index - 1; i++) { + f = f.next; + } + f.next = f.next.next; + final Node d = f.next; + final Object element = d.data; + d.data = null; + d.next = null; + size--; + return element; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newHead = new Node(o, head); + head = newHead; + size++; + } + + public void addLast(Object o) { + Node newHead = new Node(o, null); + Node f = head; + for (int i = 0; i < size - 1; i++) { + f = f.next; + } + f.next = newHead; + size++; + } + + public Object removeFirst() { + final Node f = head; + if (f == null) + throw new NoSuchElementException(); + final Object element = f.data; + head = f.next; + f.data = null; + f.next = null; + size--; + return element; + } + + public Object removeLast() { + Node f = head; + for (int i = 0; i < size - 2; i++) { + f = f.next; + } + Object element = f.next; + f.next = null; + size--; + return element; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + private Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group20/423184723/src/com/coding/basic/List.java b/group03/1753176091/src/com/coding/basic/List.java similarity index 100% rename from group20/423184723/src/com/coding/basic/List.java rename to group03/1753176091/src/com/coding/basic/List.java diff --git a/group03/1753176091/src/com/coding/basic/Queue.java b/group03/1753176091/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b5440dbade --- /dev/null +++ b/group03/1753176091/src/com/coding/basic/Queue.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Queue { + + private int size; + LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.addLast(o); + size++; + } + + public Object deQueue() { + size--; + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/1753176091/src/com/coding/basic/Stack.java b/group03/1753176091/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..0ca338f666 --- /dev/null +++ b/group03/1753176091/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (size > 0) + return elementData.remove(size); + return null; + } + + public Object peek() { + return elementData.get(size); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/2864885311/DS/.classpath b/group03/2864885311/DS/.classpath new file mode 100644 index 0000000000..6177796265 --- /dev/null +++ b/group03/2864885311/DS/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group03/2864885311/DS/.gitignore b/group03/2864885311/DS/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/2864885311/DS/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/2864885311/DS/.project b/group03/2864885311/DS/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group03/2864885311/DS/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/2864885311/DS/.settings/org.eclipse.jdt.core.prefs b/group03/2864885311/DS/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group03/2864885311/DS/.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/group03/2864885311/DS/src/com/coding/basic/ArrayList.java b/group03/2864885311/DS/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..31bc2649db --- /dev/null +++ b/group03/2864885311/DS/src/com/coding/basic/ArrayList.java @@ -0,0 +1,68 @@ +package com.coding.basic; +//import java.util.Iterator; + +//import java.util.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //int int_inarry = 10; + grow(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; + } + + private void grow(Object nbradd){ + + if (this.size>this.elementData.length){ + Object[] arrayRefVar = new Object[this.elementData.length+1]; + growcopy(nbradd,arrayRefVar); + }else{ + Object[] arrayRefVar = new Object[this.elementData.length]; + growcopy(nbradd,arrayRefVar); + } + } + private void growcopy(Object nbraddcopy,Object[] arrayRefVarcopy){ + System.arraycopy(this.elementData, 0, arrayRefVarcopy, 0, this.elementData.length); + this.elementData[0]=nbraddcopy; + System.arraycopy(arrayRefVarcopy, 0, this.elementData, 1, this.size+1); + this.size++; + } + private void instrgrow(int nbrindex,Object[] arraynbo,Object ino){ + + //Object[] arrayRefVar2 = new Object[nbrindex]; + Object[] arrayRefVar3 = new Object[this.size-nbrindex]; + + + System.arraycopy(this.elementData, nbrindex, arrayRefVar3, nbrindex, this.size); + this.elementData[nbrindex]=ino; + System.arraycopy(arrayRefVar3, 0, this.elementData, nbrindex+1, this.size); + + + } +} diff --git a/group03/2864885311/DS/src/com/coding/basic/BinaryTreeNode.java b/group03/2864885311/DS/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group03/2864885311/DS/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/group03/2864885311/DS/src/com/coding/basic/Iterator.java b/group03/2864885311/DS/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/2864885311/DS/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/group20/423184723/src/com/coding/basic/LinkedList.java b/group03/2864885311/DS/src/com/coding/basic/LinkedList.java similarity index 100% rename from group20/423184723/src/com/coding/basic/LinkedList.java rename to group03/2864885311/DS/src/com/coding/basic/LinkedList.java diff --git a/group03/2864885311/DS/src/com/coding/basic/List.java b/group03/2864885311/DS/src/com/coding/basic/List.java new file mode 100644 index 0000000000..d47df585d9 --- /dev/null +++ b/group03/2864885311/DS/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +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/group20/423184723/src/com/coding/basic/Queue.java b/group03/2864885311/DS/src/com/coding/basic/Queue.java similarity index 100% rename from group20/423184723/src/com/coding/basic/Queue.java rename to group03/2864885311/DS/src/com/coding/basic/Queue.java diff --git a/group20/423184723/src/com/coding/basic/Stack.java b/group03/2864885311/DS/src/com/coding/basic/Stack.java similarity index 100% rename from group20/423184723/src/com/coding/basic/Stack.java rename to group03/2864885311/DS/src/com/coding/basic/Stack.java diff --git a/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java b/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..023a1686ae --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,91 @@ +package com.coding.basic; + +//import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; // 记录数组当前长度 + + private Object[] elementData = new Object[10]; // 初始长度 + + /* + * (non-Javadoc) + * + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o) { + if (size > elementData.length) { // size大于数组初始长度,需要对原数组进行扩容 + grow(elementData, 10); + } + this.elementData[size++] = o; + } + + /* + * 在指定下标位置插入元素 (non-Javadoc) + * + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + if (size > elementData.length) { // size大于数组初始长度,需要对原数组进行扩容 + grow(elementData, 10); + } + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + // 1、先要判断index所在处有无值,没有则返回null + Object o = elementData[index]; + if (null == o) + throw new IndexOutOfBoundsException(); + return o; + } + + public Object remove(int index) { + Object oldVal = elementData[index]; // 保留要删除的元素 + int numMoved = size - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, numMoved);// 讲移除位置之后的元素向前 挪动 + } + elementData[--size] = null; // 将数组末尾元素置为空 + return oldVal; + } + + /** + * 获取数组元素个数 + */ + public int size() { + return this.size; + } + + public Object[] grow(Object[] src, int size) { + // Arrays.copyOf(src, src.length+size); + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public Iterator iterator() { + + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int cursor=0; + @Override + public boolean hasNext() { + return size != cursor; + } + + @Override + public Object next() { + return elementData[cursor++]; + } + + } + +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..2bc37a07e6 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,132 @@ +package com.coding.basic; + +/** + * 二叉树 + * + * @author cm + */ +public class BinaryTree { + + private BinaryTreeNode root; + + public BinaryTreeNode insert(Object o) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(o); + if (null == root) { + root = new BinaryTreeNode(o); + } else { + boolean flag = false; + BinaryTreeNode cursorNode = root; + while (!flag) { + if (binaryTreeNode.compareTo(cursorNode) < 0) { + if (cursorNode.getLeft() == null) { + cursorNode.setLeft(binaryTreeNode); + flag = true; + } else { + cursorNode = cursorNode.getLeft(); + } + } else { + if (cursorNode.getRight() == null) { + cursorNode.setRight(binaryTreeNode); + flag = true; + } else { + cursorNode = cursorNode.getRight(); + } + } + } + } + return binaryTreeNode; + } + + public LinkedList inOrder() { + LinkedList linkedList = new LinkedList(); + sortLeft(linkedList, root); + sortRight(linkedList, root); + return linkedList; + } + + private void sortRight(LinkedList linkedList, BinaryTreeNode binaryTreeNode) { + Queue queue = getRightList(binaryTreeNode); + while (!queue.isEmpty()) { + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList, queueNode); + } + } + + private void sortLeft(LinkedList linkedList, BinaryTreeNode binaryTreeNode) { + Stack stack = getLeftList(binaryTreeNode); + while (!stack.isEmpty()) { + BinaryTreeNode stackNode = (BinaryTreeNode) stack.pop(); + linkedList.add(stackNode.getData()); + Queue queue = getRightList(stackNode); + while (!queue.isEmpty()) { + BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue(); + sortLeft(linkedList, queueNode); + } + } + linkedList.add(binaryTreeNode.getData()); + } + + private Stack getLeftList(BinaryTreeNode binaryTreeNode) { + Stack stack = new Stack(); + while (binaryTreeNode.getLeft() != null) { + binaryTreeNode = binaryTreeNode.getLeft(); + stack.push(binaryTreeNode); + } + return stack; + } + + private Queue getRightList(BinaryTreeNode binaryTreeNode) { + Queue queue = new Queue(); + while (binaryTreeNode.getRight() != null) { + binaryTreeNode = binaryTreeNode.getRight(); + queue.enQueue(binaryTreeNode); + } + return queue; + } + + private class BinaryTreeNode implements Comparable { + 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(Object o) { + setData(o); + } + + @Override + public int compareTo(BinaryTreeNode binaryTreeNode) { + Integer currVal = (Integer) root.getData(); + Integer compVal = (Integer) binaryTreeNode.getData(); + if (currVal < compVal) + return -1; + else if (currVal == compVal) + return 0; + else + return 1; + } + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/Iterator.java b/group03/345943980/2017Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group03/345943980/2017Learning/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/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java b/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..b31c724a2f --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,129 @@ +package com.coding.basic; + +/** + * 单向链表 + * + * @author Administrator + * + */ +public class LinkedList implements List { + + private Node head = new Node(null, null); + private int size = 0; + + public LinkedList() { + head.next = head; + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + // 1、检查是否在合理范围内 + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + Node currNode = findNodeByIndex(index); + Node newNode = new Node(o, currNode); + if (index == 0) { // 直接插入到第一个位置 + head = newNode; + } else { + Node preNode = findNodeByIndex(index - 1); + preNode.next = newNode; + } + size++; + } + + public Object get(int index) { + return findNodeByIndex(index).data; + } + + public Object remove(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + Node targetNode = this.findNodeByIndex(index); + Object obj = targetNode.data; + if (index == 0) { + targetNode.data = null; + head = targetNode.next; + } else { + Node preNode = findNodeByIndex(index - 1); + preNode.next = targetNode.next; + } + // targetNode.data = null; + size--; + return obj; + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node nextNode = head; + Node newNode = new Node(o, nextNode); + head = newNode; + size++; + } + + public void addLast(Object o) { + Node subNode = new Node(o, null); + if (size == 0) { + head = subNode; + } else { + Node lastNode = findNodeByIndex(size - 1); + lastNode.next = subNode; + } + size++; + } + + public Object removeFirst() { + return this.remove(0); + } + + public Object removeLast() { + return this.remove(size - 1); + } + + private Node findNodeByIndex(int index) { + Node lastNode = head; + for (int i = 0; i < index; i++) { + lastNode = lastNode.next; + } + return lastNode; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + + private int cursor = 0; + private Node cursorNode = head; + + @Override + public boolean hasNext() { + return size != cursor; + } + + @Override + public Object next() { + Object obj = cursorNode.data; + cursorNode = cursorNode.next; + cursor++; + return obj; + } + + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/List.java b/group03/345943980/2017Learning/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group03/345943980/2017Learning/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/group03/345943980/2017Learning/src/com/coding/basic/Queue.java b/group03/345943980/2017Learning/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..6abba1993b --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/Queue.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +/** + * 队列(堆):特点,先进先出 + * + * @author Administrator + * + */ +public class Queue { + + private LinkedList linkedList = new LinkedList(); + private int size = 0; + + /** + * 入队列 + * + * @param o + */ + public void enQueue(Object obj) { + linkedList.add(obj); + size++; + } + + /** + * 出队列 + * + * @return + */ + public Object deQueue() { + Object obj = linkedList.remove(0); + size--; + return obj; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/basic/Stack.java b/group03/345943980/2017Learning/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..1dd8b15f59 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/basic/Stack.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +/** + * 栈(堆栈),特点先进后出的特点 + * + * @author Administrator + * + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + /** + * 在堆栈顶部中添加一个元素 + * + * @param o + */ + public void push(Object o) { + elementData.add(o); + size++; + } + + /** + * 在堆栈顶部移去一个元素 + * + * @return + */ + public Object pop() { + Object o = elementData.remove(size - 1); + size--; + return o; + + } + + /** + * 总是返回栈顶的元素 + * + * @return + */ + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java b/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..8d812b9f71 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,48 @@ +package com.coding.test; + +import org.junit.Test; + +import com.coding.basic.ArrayList; + +public class ArrayListTest { + + @Test + public void test01(){ + ArrayList arrayList = new ArrayList(); + arrayList.add(1); + arrayList.add(100); + arrayList.add(0, 1000); + System.out.println(arrayList.size()); + for(int i =0;i array = new java.util.ArrayList(); + array.add(1); + array.add(1, 20); + System.out.println(array.size()); + for(Object o:array){ + System.out.println(o.toString()); + } + //System.out.println(array.get(100)); + } + + +} diff --git a/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java b/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java new file mode 100644 index 0000000000..68962223c7 --- /dev/null +++ b/group03/345943980/2017Learning/src/com/coding/test/BinaryTreeTest.java @@ -0,0 +1,25 @@ +package com.coding.test; + +import org.junit.Test; + +import com.coding.basic.BinaryTree; +import com.coding.basic.LinkedList; + +public class BinaryTreeTest { + + @Test + public void test01(){ + BinaryTree binaryTree = new BinaryTree(); + binaryTree.insert(5); + binaryTree.insert(2); + binaryTree.insert(7); + binaryTree.insert(1); + binaryTree.insert(4); + binaryTree.insert(6); + binaryTree.insert(8); + LinkedList linkedList=binaryTree.inOrder(); + for(int i=0;i 0) { + grow(capacity); + } + } + + /** + * 扩容,扩容规则为:oldCapacity + oldCapacity/2 + * + * @param capacity + */ + private void grow(int capacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + oldCapacity / 2; + elementData = Arrays.copyOf(elementData, newCapacity); + } + + public Object get(int index) throws Exception { + checkSize(index); + return elementData[index]; + } + + public Object remove(int index) throws Exception { + modCount++; + checkSize(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + //回收多出来的内存。 + elementData[size--] = null; + return oldValue; + } + + /** + * 验证给定的数组下标是否小于数组的长度。 + * + * @param index + * @return + */ + private void checkSize(int index) throws Exception { + if (index > size) { + // 数组下标越界异常。 + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor;//记录下一个元素的索引 + int lastReturn = -1;//记录最后一个元素的索引 + int expectCount = modCount; + + @Override + public boolean hasNext() { + return (cursor != size); + } + + @Override + public Object next() { + checkForComodification(); + int i = cursor; + Object[] elementData = ArrayList.this.elementData; + cursor = i+ 1; + return elementData[lastReturn = i]; + } + + /** + * 核心方法, 这里remove可以避免fail-fast快速失败原则。 + * @throws Exception + */ + public void remove() throws Exception { + checkForComodification(); + ArrayList.this.remove(lastReturn); + cursor = lastReturn; + lastReturn = -1; + expectCount = modCount; + } + + /** + * 验证fail-fast规则。 + */ + final void checkForComodification() { + if (modCount != expectCount) + throw new ConcurrentModificationException(); + } + } +} diff --git a/group03/510782645/src/com/coding/basic/BinaryTreeNode.java b/group03/510782645/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..1e279dd56a --- /dev/null +++ b/group03/510782645/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,83 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + static class Node { + Integer data; + Node parent; + Node left; + Node right; + + public Node(Integer data, Node parent, Node left, Node right) { + this.data = data; + this.parent = parent; + this.left = left; + this.right = right; + } + + public String toString(){ + return "[data=" + data + "]"; + } + + public boolean equals(Object obj){ + if(this == obj){ + return true; + } + + if(obj.getClass() == Node.class){ + Node target = (Node) obj; + return data.equals(target.data) && left == target.left + && right == target.right && parent == target.parent; + } + + return false; + } + } + private Node root; + + BinaryTreeNode() { + root = null; + } + + BinaryTreeNode(Integer data) { + root = new Node(data, null, null, null); + } + + /** + * 暂且使用Intenger作为节点数据。 + * @param o + */ + public void insert(Integer o) { + if (root == null) { + root = new Node(o, null, null, null); + } else { + Node current = root; + Node parent = null; + int cmp; + + //搜索合适的叶子节点,以该叶子节点为父节点添加新节点 + do { + parent = current; + cmp = o.compareTo(current.data); + + //如果新节点的值大于当前节点的值 + if (cmp > 0) { + //以当前节点的右子节点作为当前节点 + current = current.right; + } else { + current = current.left; + } + } while (current != null); + + //创建新节点 + Node newNode = new Node(o, parent, null, null); + + //如果新节点的值大于父节点的值 + if (cmp > 0) { + parent.right = newNode; + } else { + parent.left = newNode; + } + } + } +} diff --git a/group03/510782645/src/com/coding/basic/Iterator.java b/group03/510782645/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/510782645/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/group03/510782645/src/com/coding/basic/LinkedList.java b/group03/510782645/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..2019f5c703 --- /dev/null +++ b/group03/510782645/src/com/coding/basic/LinkedList.java @@ -0,0 +1,202 @@ +package com.coding.basic; + +public class LinkedList implements List { + //链表的长度 + int size = 0; + private Node first; + private Node last; + + public void add(Object o){ + linkLast(o); + size++; + } + + /** + * 按照索引添加 + * @param index + * @param o + */ + public void add(int index , Object o){ + if (index == size) + linkLast(o); + else + linkBefore(o, node(index)); + } + + /** + * 向链表的最后添加元素 + * @param o + */ + private void linkLast(Object o) { + final Node l = last; + final Node newNode = new Node(o, l, null); + last = newNode; + if (l == null) + //如果只有一个元素, 那么设置链表的first为newNode + first = newNode; + else + l.next = newNode; + size++; + } + + /** + * 向链表指定位置添加元素 + * @param o + * @param node + */ + private void linkBefore(Object o, Node node) { + final Node pred = node.prev; + final Node newNode = new Node(o, pred, node); + node.prev = newNode; + if (pred == null) + first = newNode; + else + pred.next = newNode; + size++; + } + + /** + * 将元素添加到起始位置。 + * @param o + */ + private void linkFirst(Object o) { + final Node f = first; + final Node newNode = new Node(o, null, f); + first = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + /** + * 这里查找index节点时, 通过index与size/2的距离来判断是从前往后找还是从后往前找。 + * @param index + * @return + */ + Node node(int index) { + if (index < (size >> 1)) { + Node x = first; + 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; + } + } + + /** + * 直接调用node方法即可。 + * @param index + * @return + */ + public Object get(int index){ + return node(index); + } + + /** + * 根据下标删除 + * @param index + * @return + */ + public Object remove(int index){ + Node node = node(index); + return remove(node); + } + + /** + * 根据节点的data值来remove + * @param o + * @return + */ + public Object remove(Object o) { + if (o == null) { + for (Node x = first; x != null; x = x.next) { + if (x.data == null) { + return remove(x); + } + } + } else { + for (Node x = first; x != null; x = x.next) { + if (o.equals(x.data)) { + return remove(x); + } + } + } + return null; + } + + private Object remove(Node node){ + final Object obj = node.data; + final Node next = node.next; + final Node prev = node.prev; + //判断临界的地方,index为第一个元素, index为第二个元素 + if (node == first) { + first = next; + } else if (node == last) { + last = prev; + } else { + prev.next = next; + next.prev = prev; + + node.next = null; + node.prev = null; + } + + node.data = null; + size--; + return obj; + } + + public int size(){ + return -size; + } + + public void addFirst(Object o){ + linkFirst(o); + } + public void addLast(Object o){ + linkLast(o); + } + public Object removeFirst(){ + return remove(first); + } + + /** + * 获取但不删除栈顶元素,失败则抛出异常 + * @return + */ + public Object peekFirst() { + final Node f = first; + return (f == null) ? null : f.data; + } + + public Object removeLast(){ + return remove(last); + } + public Iterator iterator(){ + return null; + } + + /** + * Node内部实现类 + */ + private static class Node{ + Object data; + Node prev; + Node next; + + /** + * 使用内部类来实现链表的每一个节点,每个节点有一个指向下一个元素的next,指向上一个元素的prev,以及自身的data + */ + public Node(Object data, Node prev, Node next) { + this.data = data; + this.next = next; + this.prev = prev; + } + } +} diff --git a/group03/510782645/src/com/coding/basic/List.java b/group03/510782645/src/com/coding/basic/List.java new file mode 100644 index 0000000000..6aa2551499 --- /dev/null +++ b/group03/510782645/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) throws Exception; + public Object remove(int index) throws Exception; + public int size(); +} diff --git a/group03/510782645/src/com/coding/basic/Queue.java b/group03/510782645/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4ea6bf2fb3 --- /dev/null +++ b/group03/510782645/src/com/coding/basic/Queue.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Queue的常用方法: + * add(Object o):向队尾插入元素,失败则抛出异常 + * offer(Object o):向队尾插入元素,失败则返回false + * remove():获取并删除队首元素,失败则抛出异常 + * poll():获取并删除队首元素,失败则返回null + * element():获取但不删除队首元素,失败则抛出异常 + * peek():获取但不删除队首元素,失败则返回null + */ +public class Queue { + /** + * Queue中存储的元素 + */ + private Object[] data; + /** + * head指向首端第一个有效元素 + */ + private int head; + /** + * tail指向尾端第一个可以插入元素的空位。 + */ + private int tail; + + /** + * 进队列 + */ + public void enQueue(Object o) { + addLast(o); + } + + /** + * 向队列的尾部添加元素 + */ + public void addLast(Object o) { + if (o == null) + throw new NullPointerException(); + data[tail] = o; + //这里可以避免数组是否越界。 + if ((tail = (tail + 1) & (data.length - 1)) == head) + doubleCapacity(); + } + + /** + * 检查是否要扩容。 + */ + private void doubleCapacity() { + assert head == tail; + int p = head; + int n = data.length; + int r = n - p; // head右边元素的个数 + int newCapacity = n << 1;//原空间的2倍 + if (newCapacity < 0) + throw new IllegalStateException("Sorry, deque too big"); + Object[] a = new Object[newCapacity]; + System.arraycopy(data, p, a, 0, r);//复制右半部分 + System.arraycopy(data, 0, a, r, p);//复制左半部分 + data = (Object[]) a; + head = 0; + tail = n; + } + + /** + * 出队列 + */ + public Object deQueue() { + return removeFirst(); + } + + /** + * 移除第一个元素 + */ + public Object removeFirst() { + Object x = pollFirst(); + if (x == null) + throw new NoSuchElementException(); + return x; + } + + public Object pollFirst() { + int h = head; + Object result = data[h]; // Element is null if deque empty + if (result == null) + return null; + data[h] = null; // Must null out slot + head = (h + 1) & (data.length - 1); + return result; + } + + public boolean isEmpty() { + return head == tail; + } + + public int size() { + return (tail - head) & (data.length - 1); + } +} diff --git a/group03/510782645/src/com/coding/basic/Stack.java b/group03/510782645/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..6c9384ab67 --- /dev/null +++ b/group03/510782645/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +/** + * 堆栈是先进后出的结构。 + */ +public class Stack { + protected int elementCount; + private LinkedList elementData = new LinkedList(); + + /** + * 向栈顶插入元素,失败则抛出异常。同LikedList中的addFirst(); + * @param o + */ + public void push(Object o){ + elementData.addFirst(o); + } + + /** + * 获取并删除栈顶元素,失败则抛出异常。同LikedList中的removeFirst(); + * @return + */ + public Object pop(){ + return elementData.removeFirst(); + } + + /** + * 获取但不删除栈顶元素,失败则抛出异常. 同LinkedList中的peekFirst(); + * @return + */ + public Object peek(){ + return elementData.peekFirst(); + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group03/569045298/pom.xml b/group03/569045298/pom.xml new file mode 100644 index 0000000000..338f3870aa --- /dev/null +++ b/group03/569045298/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + com.ztc + JavaLevelUp + war + 1.0-SNAPSHOT + JavaLevelUp Maven Webapp + http://maven.apache.org + + + + junit + junit + 3.8.1 + test + + + org.junit.jupiter + junit-jupiter-api + RELEASE + + + junit + junit + 4.12 + + + + + JavaLevelUp + + + diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java b/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java new file mode 100644 index 0000000000..dee00342d2 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java @@ -0,0 +1,108 @@ +package com.coding.basic.datastructure; + + +/** + * Created by zt on 2017/2/19. + */ +public class ArrayList implements List { + + private static final int DEFAULT_CAPACITY = 10; + private int size = 0; + private Object[] elementData = null; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) { + throw new RuntimeException("initialCapacity is smaller than zero"); + } + elementData = new Object[initialCapacity]; + } + + @Override + public void add(Object o) { + checkCapacity(size + 1); + elementData[size] = o; + size++; + } + + @Override + public void add(int index, Object o) { + checkCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + checkRange(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + Object removedObject = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, elementData.length - index - 1); + elementData[--size] = null; + return removedObject; + } + + @Override + public int size() { + return size; + } + + private void checkRange(int index) { + if (index < 0 || index > elementData.length) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkCapacity(int size) { + if (size > elementData.length) { + int newLength = elementData.length * 2; + Object[] newObject = new Object[newLength]; + System.arraycopy(elementData, 0, newObject, 0, elementData.length); + elementData = newObject; + } + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + + ArrayList arrayList = null; + int pos = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + // TODO + pos++; + if (pos > size) { + return false; + } + return true; + } + + @Override + public Object next() { + // TODO + return elementData[pos]; + } + + @Override + public Object remove() { + // TODO + return null; + } + } +} diff --git a/group16/214074094/src/com/coding/basic/BinaryTreeNode.java b/group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java similarity index 81% rename from group16/214074094/src/com/coding/basic/BinaryTreeNode.java rename to group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java index b40066ebe1..b69f8932ed 100644 --- a/group16/214074094/src/com/coding/basic/BinaryTreeNode.java +++ b/group03/569045298/src/main/com/coding/basic/datastructure/BinaryTreeNode.java @@ -1,5 +1,8 @@ -package coding.basic; +package com.coding.basic.datastructure; +/** + * Created by zt on 2017/2/19. + */ public class BinaryTreeNode { private Object data; @@ -32,8 +35,7 @@ public void setRight(BinaryTreeNode right) { this.right = right; } - public BinaryTreeNode insert(Object o) { + public BinaryTreeNode insert(Object object) { return null; } - } diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java b/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java new file mode 100644 index 0000000000..8bf2ae7ed5 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Iterator.java @@ -0,0 +1,13 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public interface Iterator { + + boolean hasNext(); + + Object next(); + + Object remove(); +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java b/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java new file mode 100644 index 0000000000..8814ea82e8 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java @@ -0,0 +1,167 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size = 0; + + public LinkedList() { + + } + + @Override + public void add(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + tail = head; + size++; + } else { + // 尾插法 + Node newNode = new Node(object); + tail.next = newNode; + tail = newNode; + tail.next = null; + size++; + } + } + + @Override + public void add(int index, Object object) { + checkRange(index); + if (null == head) { + add(object); + return; + } + if (index == 0) { + addFirst(object); + return; + } + Node pre = node(index - 1); + Node newNode = new Node(object); + newNode.next = pre.next; + pre.next = newNode; + size++; + } + + @Override + public Object get(int index) { + checkRange(index); + checkNodeNotNull(); + Node node = node(index); + return node.data; + } + + @Override + public Object remove(int index) { + checkRange(index); + checkNodeNotNull(); + if (index == 0) { + removeFirst(); + return head; + } + Node pre = node(index - 1); + pre.next = pre.next.next; + size--; + return head; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + size++; + } else { + Node firstNode = new Node(object); + firstNode.next = head; + head = firstNode; + size++; + } + } + + public Object removeFirst() { + checkNodeNotNull(); + head = head.next; + size--; + return head; + } + + public Object removeLast() { + checkNodeNotNull(); + if (size == 1) { + head = null; + } + Node pre = node(size() - 2); + pre.next = null; + size--; + return head; + } + + private void checkRange(int index) { + if (index > size - 1 || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + private void checkNodeNotNull() { + if (null == head) { + throw new NullPointerException(); + } + } + + private Node node(int index) { + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + private static class Node { + Node next; + private Object data; + + public Node() { + + } + + public Node(Object data) { + this.data = data; + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + public Node(Object data, Node next, Node prev) { + this.data = data; + this.next = next; + } + } + + /*@Override + public void add(Object object) { + if (null == head) { + head = new Node(object); + head.next = null; + } else { + // 头插法 + Node nextNode = new Node(object); + nextNode.next = head.next; + head.next = nextNode; + } + }*/ + +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/List.java b/group03/569045298/src/main/com/coding/basic/datastructure/List.java new file mode 100644 index 0000000000..4d9292f156 --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/List.java @@ -0,0 +1,17 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public interface List { + + void add(Object o); + + void add(int index, Object o); + + Object get(int index); + + Object remove(int index); + + int size(); +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java b/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java new file mode 100644 index 0000000000..d3d4ebfbab --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Queue.java @@ -0,0 +1,43 @@ +package com.coding.basic.datastructure; + + +/** + * Created by zt on 2017/2/19. + */ +public class Queue { + + private ArrayList elementData; + + private int size; + + public Queue() { + elementData = new ArrayList(); + } + + public void enQueue(Object object) { + elementData.add(object); + size++; + } + + public Object deQueue() { + checkIsEmpty(); + Object object = elementData.get(0); + elementData.remove(0); + size--; + return object; + } + + private void checkIsEmpty() { + if (elementData.size() == 0) { + throw new RuntimeException("queue is empty"); + } + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java b/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java new file mode 100644 index 0000000000..c8dbc6b3af --- /dev/null +++ b/group03/569045298/src/main/com/coding/basic/datastructure/Stack.java @@ -0,0 +1,47 @@ +package com.coding.basic.datastructure; + +/** + * Created by zt on 2017/2/19. + */ +public class Stack { + + private ArrayList elementData = null; + + private int size = 0; + + public Stack() { + elementData = new ArrayList(); + } + + public void push(Object object) { + elementData.add(object); + size++; + } + + public Object pop() { + checkIsEmpty(); + Object peekObject = peek(); + elementData.remove(size - 1); + size--; + return peekObject; + } + + public Object peek() { + checkIsEmpty(); + return elementData.get(size - 1); + } + + private void checkIsEmpty() { + if (isEmpty()) { + throw new RuntimeException("stack is empty"); + } + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return size; + } +} diff --git a/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java b/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java new file mode 100644 index 0000000000..9f90242595 --- /dev/null +++ b/group03/569045298/src/test/com/coding/basic/datastructure/TestDataStructure.java @@ -0,0 +1,66 @@ +package com.coding.basic.datastructure; + +import org.junit.Test; + +/** + * Created by zt on 2017/2/19. + */ +public class TestDataStructure { + + @Test + public void testLinedList() { + LinkedList list = new LinkedList(); + for (int i = 0; i < 5; i++) { + list.add(i); + } + list.add(0, -1); + list.remove(1); + list.removeLast(); + list.addFirst(999); + list.removeFirst(); + System.out.println("list size : " + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + java.util.LinkedList list1 = new java.util.LinkedList(); + list1.add(0, 2); + System.out.print(list1.get(0)); + } + + @Test + public void testStack() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.pop(); + System.out.println(stack.size()); + Object obj = stack.peek(); + } + + @Test + public void testQueue() { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + Object object = queue.deQueue(); + System.out.println("dqueue object : " + object); + System.out.println(queue.isEmpty()); + System.out.println(queue.size()); + } + + @Test + public void testArrayList() { + List arrayList = new ArrayList(); + for (int i = 0; i < 30; i++) { + arrayList.add(i); + } + arrayList.add(0, -2); + arrayList.add(1, -1); + System.out.println(arrayList.remove(1)); + System.out.println("ArrayList size : " + arrayList.size()); + for (int i = 0; i < arrayList.size(); i++) { + System.out.println(arrayList.get(i)); + } + } +} diff --git a/group03/617187912/Learning02/.classpath b/group03/617187912/Learning02/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group03/617187912/Learning02/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/617187912/Learning02/.gitignore b/group03/617187912/Learning02/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/617187912/Learning02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/617187912/Learning02/.project b/group03/617187912/Learning02/.project new file mode 100644 index 0000000000..12dfa1118c --- /dev/null +++ b/group03/617187912/Learning02/.project @@ -0,0 +1,17 @@ + + + 617187912Learning02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs b/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group03/617187912/Learning02/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs b/group03/617187912/Learning02/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group03/617187912/Learning02/.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/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java b/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9e22bd0c0b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/ArrayList.java @@ -0,0 +1,117 @@ +package com.coding.basic; + + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + public ArrayList(){ + this(64); + } + + public ArrayList(int intSize) { + elementData = new Object[intSize]; + } + + public void add(Object o) { + checkMaxSize(); + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + checkMaxSize(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + private void checkIndexRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("index超出数组界限?"); + } + } + + private void checkMaxSize() { + if (size >= elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + } + + public Object get(int index) { + checkIndexRange(index); + return elementData[index]; + } + + public Object remove(int index) { + Object o = get(index); + if (index == size - 1) { + elementData[index] = null; + } else { + System.arraycopy(elementData, index + 1, elementData, index, size - index); + } + size--; + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + private int current = 0; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + if (current >= size) { + throw new NoSuchElementException(); + } + return elementData[current++]; + } + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(5); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + arrayList.add(6); + System.out.println(arrayList.get(1)); + arrayList.add(1, 100); + System.out.println(arrayList.get(1)); + System.out.println(arrayList.size); + System.out.println(arrayList.remove(2)); + System.out.println(arrayList.get(2)); + + + ArrayList mixArraylist = new ArrayList(5); + mixArraylist.add("String"); + mixArraylist.add(100); + mixArraylist.add('f'); + mixArraylist.add(3.1f); + mixArraylist.add(4L); + System.out.println(mixArraylist.get(1)); + mixArraylist.add(1, 101); + System.out.println(mixArraylist.get(1)); + System.out.println(mixArraylist.size); + System.out.println(mixArraylist.remove(2)); + System.out.println(mixArraylist.get(2)); + } +} \ No newline at end of file diff --git a/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java b/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..7b6479d535 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,33 @@ +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/group03/617187912/Learning02/src/com/coding/basic/Iterator.java b/group03/617187912/Learning02/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..017cbc4240 --- /dev/null +++ b/group03/617187912/Learning02/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/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java b/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..88a4c6c31b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/LinkedList.java @@ -0,0 +1,137 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkIndexRange(index); + if (index == 0) { + head = new Node(o, head); + size++; + } else { + Node nd = getNode(index-1); + nd.next = new Node(o, nd.next); + size++; + } + } + + public Object get(int index) { + return getNode(index).data; + } + + private Node getNode(int index) { + Node nd = head; + for (int i = 0; i < index; i++) { + nd = nd.next; + } + return nd; + } + + public Object remove(int index) { + if (size == 0) { + throw new NoSuchElementException(); + } + checkIndexRange(index); + if (index == 0) { + Object o = head.data; + head = head.next; + size--; + return o; + } else { + Node nd = getNode(index - 1); + Object o = nd.next.data; + nd.next = nd.next.next; + size--; + return o; + } + } + + private void checkIndexRange(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("index超出数组界限?"); + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + add(0, o); + } + + public void addLast(Object o) { + if (size == 0) { + addFirst(o); + } else { + add(size, o); + } + } + + public Object removeFirst() { + return remove(0); + } + + public Object removeLast() { + return remove(size - 1); + } + + public Iterator iterator() { + return new MyIterator(); + } + + private class MyIterator implements Iterator { + + public Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Object next() { + Object o = current.data; + current = current.next; + return o; + } + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + for (int i = 0; i < list.size; i++) { + System.out.println(list.get(i)); + } + System.out.println(list.get(2)); + list.add(2, 100); + System.out.println(list.get(2)); + list.addFirst(10); + System.out.println(list.get(2)); + list.addLast(100); + System.out.println(list.remove(1)); + System.out.println(list.removeFirst()); + System.out.println(list.removeLast()); + } +} diff --git a/group03/617187912/Learning02/src/com/coding/basic/List.java b/group03/617187912/Learning02/src/com/coding/basic/List.java new file mode 100644 index 0000000000..cd5130be3b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +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/group03/617187912/Learning02/src/com/coding/basic/Queue.java b/group03/617187912/Learning02/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..39e1a8b60b --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/Queue.java @@ -0,0 +1,33 @@ +package com.coding.basic; + + +public class Queue { + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o) { + linkedList.add(o); + } + + public Object deQueue() { + return linkedList.removeFirst(); + } + + public boolean isEmpty() { + return linkedList.size() ==0; + } + + public int size() { + return linkedList.size(); + } + public static void main(String[] args) { + Queue que = new Queue(); + que.enQueue(10); + que.enQueue(11); + que.enQueue(12); + System.out.println(que.deQueue()); + System.out.println(que.isEmpty()); + que.deQueue(); + que.deQueue(); + System.out.println(que.isEmpty()); + } +} diff --git a/group03/617187912/Learning02/src/com/coding/basic/Stack.java b/group03/617187912/Learning02/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..1ee047ba4a --- /dev/null +++ b/group03/617187912/Learning02/src/com/coding/basic/Stack.java @@ -0,0 +1,48 @@ +package com.coding.basic; + + +import java.util.EmptyStackException; + +import javax.lang.model.element.QualifiedNameable; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + checkIsEmpty(); + return elementData.remove(size()-1); + } + + private void checkIsEmpty() { + if (isEmpty()){ + throw new EmptyStackException(); + } + } + + public Object peek(){ + checkIsEmpty(); + return elementData.get(size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } + public static void main(String[] args) { + Stack que = new Stack(); + que.push(10); + que.push(11); + que.push(12); + System.out.println(que.peek()); + System.out.println(que.isEmpty()); + System.out.println(que.pop()); + System.out.println(que.pop()); + que.pop(); + System.out.println(que.isEmpty()); + } +} diff --git a/group03/617187912/Learning201702/.classpath b/group03/617187912/Learning201702/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group03/617187912/Learning201702/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/617187912/Learning201702/.gitignore b/group03/617187912/Learning201702/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/617187912/Learning201702/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/617187912/Learning201702/.project b/group03/617187912/Learning201702/.project new file mode 100644 index 0000000000..b3dfe82232 --- /dev/null +++ b/group03/617187912/Learning201702/.project @@ -0,0 +1,17 @@ + + + 617187912Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs b/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group03/617187912/Learning201702/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java b/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..517f614f71 --- /dev/null +++ b/group03/617187912/Learning201702/src/com/coding/basic/ArrayList.java @@ -0,0 +1,47 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (size>=elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + elementData[size] = o; + size+=1; + } + public void add(int index, Object o){ + if (size>=elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + System.arraycopy(elementData, index, + elementData, index+1, size-index); + elementData[index]=o; + size+=1; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + System.arraycopy(elementData, index+1, + elementData, index, size-index); + size-=1; + return elementData; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} \ No newline at end of file diff --git a/group03/617187912/Learning201702/src/com/coding/basic/BinaryTreeNode.java b/group03/617187912/Learning201702/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/Iterator.java b/group03/617187912/Learning201702/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/LinkedList.java b/group03/617187912/Learning201702/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/List.java b/group03/617187912/Learning201702/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/Queue.java b/group03/617187912/Learning201702/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/617187912/Learning201702/src/com/coding/basic/Stack.java b/group03/617187912/Learning201702/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group03/617187912/Learning201702/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/group03/619224754/.classpath b/group03/619224754/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group03/619224754/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group03/619224754/.gitignore b/group03/619224754/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/619224754/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/619224754/.project b/group03/619224754/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group03/619224754/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/619224754/.settings/org.eclipse.jdt.core.prefs b/group03/619224754/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group03/619224754/.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/group03/619224754/src/Main/Main.java b/group03/619224754/src/Main/Main.java new file mode 100644 index 0000000000..2419e8fa45 --- /dev/null +++ b/group03/619224754/src/Main/Main.java @@ -0,0 +1,5 @@ +package Main; + +public class Main { + +} diff --git a/group03/619224754/src/com/coding/basic/ArrayList.java b/group03/619224754/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..05fc412f93 --- /dev/null +++ b/group03/619224754/src/com/coding/basic/ArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if(elementData.length == size) { + Object[] arrTaget = new Object[size * 2]; + System.arraycopy(elementData, 0, arrTaget, 0, size); + this.elementData = arrTaget; + } + + elementData[size++] = o; + } + + public void add(int index, Object o){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + Object[] arrTarget = new Object[size - index]; + System.arraycopy(elementData, index, arrTarget, 0, size - index); + elementData[index] = o; + System.arraycopy(arrTarget, 0, elementData, index + 1, size - index); + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + Object retObj = elementData[index]; + + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + else if(index == size) { + elementData[index] = null; + } + else { + System.arraycopy(elementData, index + 1, elementData, index, size - index); + } + + size--; + return retObj; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int cursor = 0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return this.cursor != size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return elementData[this.cursor++]; + } + } + +} diff --git a/group03/619224754/src/com/coding/basic/BinaryTreeNode.java b/group03/619224754/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..58005fb1b4 --- /dev/null +++ b/group03/619224754/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,80 @@ +package com.coding.basic; + +import java.util.Comparator; + +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){ + BinaryTreeNode treeNode = new BinaryTreeNode(); + treeNode.data = o; + int intO = Integer.parseInt(o.toString()); + int intData = Integer.parseInt(this.data.toString()); + if(intO > intData){ + if(this.right == null){ + this.right = treeNode; + } + else { + this.right.insert(o); + } + } + else { + if(this.left == null) { + this.left = treeNode; + } + else { + this.left.insert(o); + } + } + return treeNode; + } + + private class MyComparator implements Comparator { + + @Override + public int compare(BinaryTreeNode arg0, BinaryTreeNode arg1) { + // TODO Auto-generated method stub + int int0 = Integer.parseInt(arg0.data.toString()); + int int1 = Integer.parseInt(arg1.data.toString()); + if(int0 > int1) { + return 1; + } + else if(int0 < int1){ + return -1; + } + + return 0; + + } + + + } + +} diff --git a/group03/619224754/src/com/coding/basic/Iterator.java b/group03/619224754/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group03/619224754/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/group03/619224754/src/com/coding/basic/LinkedList.java b/group03/619224754/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..8ca8159baf --- /dev/null +++ b/group03/619224754/src/com/coding/basic/LinkedList.java @@ -0,0 +1,182 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o) { + if(head == null) { + head = new Node(); + head.data = o; + } + else { + Node newNode = new Node(); + newNode.data = o; + Node lastNode = head; + while(head.next != null) { + lastNode = head.next; + } + lastNode.next = newNode; + } + } + + public void add(int index , Object o) { + if(index >= this.size()) + throw new IndexOutOfBoundsException("Index out of bound"); + + Node newNode = new Node(); + newNode.data = o; + if(index == 0) { + newNode.next = this.head; + this.head = newNode; + } + else if(index == this.size()) { + Node curNode = this.head; + while(curNode.next != null){ + curNode = curNode.next; + } + curNode.next = newNode; + } + else { + Node beforeNode = this.head; + Node afterNode = null; + for(int i = 1; i < index; i++) { + beforeNode = head.next; + } + afterNode = beforeNode.next; + newNode.next = afterNode; + beforeNode.next = newNode; + } + } + + public Object get(int index){ + Node retNode = this.head; + + if(index < 0){ + throw new IndexOutOfBoundsException("Index out of bound"); + } + + if(index != 0) { + for(int i = 0; i < index; i++) { + retNode = retNode.next; + } + } + + return retNode.data; + } + + public Object remove(int index){ + Node beforeNode = null; + Node afterNode = null; + Node removedNode = null; + if(index == 0) { + removedNode = this.head; + this.head = this.head.next; + } + else { + for(int i = 1; i < index; i++) { + beforeNode = head.next; + } + removedNode = beforeNode.next; + afterNode = removedNode.next; + beforeNode.next = afterNode; + } + + + return removedNode.data; + } + + public int size(){ + int i = 0; + if(this.head == null) + return 0; + + Node curNode = this.head; + while(curNode != null){ + curNode = curNode.next; + i++; + } + return i; + } + + public void addFirst(Object o){ + Node firstNode = new Node(); + firstNode.data = o; + firstNode.next = this.head; + this.head = firstNode; + } + + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + if(this.size() == 0){ + this.head = newNode; + } + + Node curNode = this.head; + while(curNode.next != null) { + curNode = curNode.next; + } + curNode.next = newNode; + } + + public Object removeFirst() { + Node retNode = this.head; + this.head = this.head.next; + + return retNode; + } + + public Object removeLast() { + Node curNode = null; + if(this.size() == 0) { + curNode = null; + } + else if(this.size() == 1) { + curNode = this.head; + this.head = null; + return curNode; + } + else { + Node beforeNode = this.head; + for (int i = 1; i < this.size() - 1; i++) { + beforeNode = beforeNode.next; + } + curNode = beforeNode.next; + beforeNode.next = null; + } + + return curNode; + } + + public Iterator iterator(){ + return null; + } + + private class LinkedListIterator implements Iterator { + + private Node curNode = head; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return this.curNode.next != null; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + return this.curNode.next; + } + + + + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group03/619224754/src/com/coding/basic/List.java b/group03/619224754/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group03/619224754/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/group03/619224754/src/com/coding/basic/Queue.java b/group03/619224754/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..de53482312 --- /dev/null +++ b/group03/619224754/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o); + } + + public Object deQueue(){ + Object ret = list.removeFirst(); + return ret; + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group03/619224754/src/com/coding/basic/Stack.java b/group03/619224754/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..cf10ecc1b3 --- /dev/null +++ b/group03/619224754/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + this.elementData.add(o); + } + + public Object pop(){ + Object ret = this.elementData.remove(this.elementData.size() - 1); + return ret; + } + + public Object peek(){ + Object ret = this.elementData.get(this.elementData.size() - 1); + return null; + } + public boolean isEmpty(){ + return this.elementData.size() == 0; + } + public int size(){ + return this.elementData.size(); + } +} diff --git a/group03/619224754/src/test/ArrayListTest.java b/group03/619224754/src/test/ArrayListTest.java new file mode 100644 index 0000000000..9d9e014379 --- /dev/null +++ b/group03/619224754/src/test/ArrayListTest.java @@ -0,0 +1,62 @@ +package test; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + + + +public class ArrayListTest { + + @Test + public void testAdd() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + Assert.assertEquals("Shoule be the same", 105, array.size()); + } + + @Test + public void testAddIndex() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + array.add(100, 100); + Assert.assertEquals("Shoule be the same", 100, array.get(100)); + Assert.assertEquals("Shoule be the same", 100, array.get(101)); + } + + @Test + public void testRemove() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + + Assert.assertEquals("Shoule be the same", 100, array.remove(100)); + Assert.assertEquals("Shoule be the same", 104, array.size()); + } + + @Test + public void testIterator() { + ArrayList array = new ArrayList(); + for (int i = 0; i < 105; i++) { + array.add(i); + } + Iterator iterator = array.iterator(); + int j = 0; + while(iterator.hasNext()){ + Assert.assertEquals("Shoule be the same", iterator.next(), array.get(j)); + j++; + } + } + +} diff --git a/group03/664269713/DataStructure/.gitignore b/group03/664269713/DataStructure/.gitignore new file mode 100644 index 0000000000..fb921cf6e9 --- /dev/null +++ b/group03/664269713/DataStructure/.gitignore @@ -0,0 +1,3 @@ +/bin/ +/.classpath +/.project \ No newline at end of file diff --git a/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java b/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java new file mode 100644 index 0000000000..a529faef7b --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java @@ -0,0 +1,84 @@ +package com.ace.coding; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + checkArrayLength(); + elementData[size++] = o; + } + + private void checkArrayLength(){ + if(elementData.length < size() + 1){ + // expand the origin length of the array + int newLength = size * 2 + 1; + elementData = Arrays.copyOf(elementData, newLength); + } + } + + private void checkIndex(int index){ + if(index < 0 || index >= size()){ + throw new IndexOutOfBoundsException("Index " + index + " is invalid."); + } + } + + public void add(int index, Object o){ + checkIndex(index); + checkArrayLength(); + System.arraycopy(elementData, index, elementData, index+1, size() - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + checkIndex(index); + Object obj = elementData[index]; + if(index == size() - 1){ + elementData[index] = null; + } else { + System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); + } + size--; + return obj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; +// return new ListIterator(); + } + + /*private class ListIterator implements Iterator{ + private int index = 0; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return index != size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + if(index >= size){ + throw new IndexOutOfBoundsException("There's no next element."); + } + return elementData[index++]; + } + + }*/ + +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/BinaryTreeNode.java b/group03/664269713/DataStructure/src/com/ace/coding/BinaryTreeNode.java new file mode 100644 index 0000000000..e6253b221c --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/BinaryTreeNode.java @@ -0,0 +1,61 @@ +package com.ace.coding; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode rootNode; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode newNode = new BinaryTreeNode(); + newNode.setData(o); + + if(rootNode == null){ + rootNode = newNode; + rootNode.data = data; + left = null; + right = null; + } else { + BinaryTreeNode currentNode = rootNode; + while(true){ + BinaryTreeNode pNode = currentNode; + if((int)newNode.getData() > (int)currentNode.getData()){ + currentNode = currentNode.right; + if(currentNode.right == null){ + pNode.right = newNode; + return newNode; + } + } else { + currentNode = currentNode.left; + if(currentNode.left == null){ + pNode.left = newNode; + return newNode; + } + } + } + + } + return newNode; + } + +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/Iterator.java b/group03/664269713/DataStructure/src/com/ace/coding/Iterator.java new file mode 100644 index 0000000000..09c5442076 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/Iterator.java @@ -0,0 +1,7 @@ +package com.ace.coding; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/LinkedList.java b/group03/664269713/DataStructure/src/com/ace/coding/LinkedList.java new file mode 100644 index 0000000000..351a6a8277 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/LinkedList.java @@ -0,0 +1,123 @@ +package com.ace.coding; + +public class LinkedList implements List { + private Node head = null; + private int size = 0; + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + if(head == null){ + head = newNode; + } else { + Node pNode = head; + while(pNode.next != null){ + pNode = pNode.next; + } + pNode.next = newNode; + } + size++; + } + + public void add(int index , Object o){ + checkLinkedListIndex(index); + + Node newNode = new Node(); + newNode.data = o; + Node pNode = getNode(index); + newNode.next = pNode.next; + pNode.next = newNode; + + size++; + } + + private Node getNode(int index){ + Node pNode = head; + for(int i = 0; i < index; i++){ + pNode = pNode.next; + } + return pNode; + } + + public Object get(int index){ + Node pNode = getNode(index); + return pNode.data; + } + public Object remove(int index){ + checkLinkedListIndex(index); + + Node pNode = head; + for(int i = 0; i < index - 1; i++){ + pNode = pNode.next; + } + Node tempNode = getNode(index); + pNode.next = tempNode.next; + size--; + return tempNode.data; + } + + + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + head = newNode; + size++; + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + Node pNode = getNode(size() - 1); + pNode.next = newNode; + size++; + } + public Object removeFirst(){ + Node pNode = head; + head = pNode.next; + size--; + return pNode.data; + } + public Object removeLast(){ + Object obj = remove(size() - 1); + return obj; + } + + public int size(){ + return size; + } + + private void checkLinkedListIndex(int index){ + if(index < 0 || index >= size()){ + throw new IndexOutOfBoundsException("The index " + index + " is invalid."); + } + } + + public Iterator iterator(){ + return null; +// return new ListIterator(); + } + + /*private class ListIterator implements Iterator{ + private Node pNode = head; + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return pNode.next != null; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + Object obj = pNode.data; + pNode = pNode.next; + return obj; + } + + }*/ + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/List.java b/group03/664269713/DataStructure/src/com/ace/coding/List.java new file mode 100644 index 0000000000..33bddf1899 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/List.java @@ -0,0 +1,9 @@ +package com.ace.coding; + +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/group03/664269713/DataStructure/src/com/ace/coding/Queue.java b/group03/664269713/DataStructure/src/com/ace/coding/Queue.java new file mode 100644 index 0000000000..15bcf5d497 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/Queue.java @@ -0,0 +1,27 @@ +package com.ace.coding; + +public class Queue { + private ArrayList arrayList = new ArrayList(); + private int size = 0; + + public void enQueue(Object data){ + arrayList.add(size(), data); + size++; + } + + public Object deQueue(){ + if(isEmpty()){ + throw new IndexOutOfBoundsException("The Queue is Empty."); + } + size--; + return arrayList.remove(0); + } + + public boolean isEmpty(){ + return size()>0; + } + + public int size(){ + return size; + } +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/Stack.java b/group03/664269713/DataStructure/src/com/ace/coding/Stack.java new file mode 100644 index 0000000000..4ef1f68084 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/coding/Stack.java @@ -0,0 +1,36 @@ +package com.ace.coding; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + checkStack(); + Object obj = elementData.remove(size()-1); + size--; + return obj; + } + + public Object peek(){ + checkStack(); + return elementData.get(size()-1); + } + + private void checkStack(){ + if(isEmpty()){ + throw new IndexOutOfBoundsException("This stack is empty"); + } + } + + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } +} diff --git a/group03/763878069/.classpath b/group03/763878069/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group03/763878069/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/763878069/.gitignore b/group03/763878069/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group03/763878069/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group03/763878069/.project b/group03/763878069/.project new file mode 100644 index 0000000000..14ea6baea5 --- /dev/null +++ b/group03/763878069/.project @@ -0,0 +1,17 @@ + + + SimpleDataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/763878069/src/cmj/datastructure/list/ArrayList.java b/group03/763878069/src/cmj/datastructure/list/ArrayList.java new file mode 100644 index 0000000000..21286512d0 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/ArrayList.java @@ -0,0 +1,154 @@ +package cmj.datastructure.list; + +import java.util.Arrays; +import java.util.Collection; + +public class ArrayList implements List { + private transient Object[] elementData; + private int size; + + /** + * ArrayList初始化无参数构造函数 + */ + public ArrayList() { + this(10); + } + + /** + * ArrayList带容量的构造函数 + * + * @param initialCapacity初始化容量 + */ + public ArrayList(int initialCapacity) { + super(); + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + // 新建一个数组 + this.elementData = new Object[initialCapacity]; + } + + /** + * 检查数组的容量 + * + * @param neededMinCapacity所需最小的容量 + */ + public void ensureCapacity(int neededMinCapacity) { + int currCapacity = elementData.length;// 获取当前数据的全部容量 + // 需要扩容的情况 + if (neededMinCapacity > currCapacity) { + int newCapacity = (currCapacity * 3) / 2 + 1;// 计算新的容量 + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + /** + * 添加数据 + * + * @param o要添加的元素 + * @return 是否添加成功 + */ + public void add(Object o) { + // 确定ArrayList的容量大小 + ensureCapacity(size + 1); // Increments modCount!! + // 添加o到ArrayList中 + elementData[size++] = o; + } + + /** + * 就是检查一下是不是超出数组界限了,超出了就抛出IndexOutBoundsException异常。 + * + * @param index要用于检查的索引 + */ + private void RangeCheck(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + " 超出访问范围"); + } + + /** + * 向指定的位置添加元素 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + RangeCheck(index); + ensureCapacity(size + 1);// 检查容量 + + /* 将原数组从第index个位置复制到原数组第index+1个位置上,一共移动size-index(也就是后面剩下的)个元素 */ + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public boolean addAll(Collection c) { + Object[] a = c.toArray(); + int growthNum = a.length; + ensureCapacity(size + growthNum); // Increments modCount + System.arraycopy(a, 0, elementData, size, growthNum); + size += growthNum; + return growthNum != 0; + } + + public Object get(int index) { + RangeCheck(index); + return elementData[index]; + + } + + public Object remove(int index) { + RangeCheck(index); + int numMoved = size - index - 1;// 删除后需要移动的对象 + Object RemovedValue = elementData[index]; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null; + return RemovedValue; + } + + public int size() { + return size; + } + + @Override + public String toString() { + String arraylist = "["; + for (int i = 0; i < size; i++) { + if (i == size - 1) { + arraylist += elementData[i].toString() + "]"; + } else { + arraylist += elementData[i].toString() + " ,"; + } + } + return arraylist; + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(5); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.add(4); + arrayList.add(5); + arrayList.add(6); + System.out.println(arrayList); + arrayList.add(1, 1234); + System.out.println(arrayList); + arrayList.remove(1); + System.out.println(arrayList); + System.out.println(arrayList.get(5)); + + ArrayList stringArraylist = new ArrayList(3); + stringArraylist.add("Hello "); + stringArraylist.add("string "); + stringArraylist.add("arraylist"); + System.out.println(stringArraylist); + + ArrayList mixArraylist = new ArrayList(5); + mixArraylist.add("String"); + mixArraylist.add(1); + mixArraylist.add('f'); + mixArraylist.add(3.1f); + mixArraylist.add(4L); + System.out.println(mixArraylist); + } +} diff --git a/group03/763878069/src/cmj/datastructure/list/LinkedList.java b/group03/763878069/src/cmj/datastructure/list/LinkedList.java new file mode 100644 index 0000000000..c36197b360 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/LinkedList.java @@ -0,0 +1,208 @@ +package cmj.datastructure.list; + +public class LinkedList implements List { + + private Node head;// 头结点 + private Node current;// 尾结点 + private int size; + + public LinkedList() { + // 头指针和尾指针都指向头结点 + head = new Node(null, null); + current = head; + } + + /** + * 添加元素 + * + * @param o——用于添加的元素 + */ + public void add(Object o) { + Node node = new Node(o, null);// 新建一个结点 + current.next = node;// 尾指针指向它 + current = current.next;// 尾指针指向最后一个元素 + size++; + } + + /** + * 在第index个位置插入元素 + * + * @param index——要插入的位置 + * @param o——用于插入的对象 + */ + public void add(int index, Object o) { + Node node = new Node(o, null);// 新建一个结点 + if (index == 0) { + addFirst(o); + } else { + Node curr = (Node) this.get(index - 1);// 获得前一个结点 + Node behind = (Node) this.get(index);// 获得后一个结点 + // 在这两个结点之间插入新的元素,修改引用指向 + curr.next = node; + node.next = behind; + size++; + } + + } + + /** + * 随机访问index位置上的元素 + * + * @param index——元素的位置 + * @return——对应的元素 + */ + public Object get(int index) { + RangeCheck(index);// 检查索引是否越界 + Node curr = head;// 得到头结点的引用 + // 从头结点开始遍历到第index个元素 + for (int i = 0; i <= index; i++) + curr = curr.next; + return curr; + } + + /** + * 删除第index个位置上的元素 + * + * @param index + * @return + */ + public Object remove(int index) { + RangeCheck(index);// 检查索引是否越界 + if (0 == index) { + return removeFirst(); + } else { + Node toRemove = (Node) this.get(index);// 获得要删除的结点 + Node preRemove = (Node) this.get(index - 1);// 获得前一个结点 + preRemove.next = toRemove.next;// 将前一个结点指向要删除的结点的下一个结点 + size--; + return toRemove; + } + + } + + /** + * 获取元素的大小 + * + * @return + */ + public int size() { + return size; + } + + /** + * 在链表头部增加元素 + * + * @param o——要增加的元素 + */ + public void addFirst(Object o) { + Node node = new Node(o, null);// 新建一个结点 + node.next = head.next;// 结点指向第一个元素 + head.next = node;// 将头结点指向它 + size++; + } + + /** + * 在链表末尾添加元素 + * + * @param o——要添加的元素 + */ + public void addLast(Object o) { + Node node = new Node(o, null);// 新建一个结点 + current.next.next = node;// 尾结点的next指向新建的结点 + current.next = node;// 尾结点引用指向向新结点 + size++; + } + + /** + * 移除第一个元素 + * + * @return——移除元素 + */ + public Object removeFirst() { + Node curr = head.next;// 新建一个引用记录第一个结点 + head.next = curr.next;// 头指针移动到原第二个元素上 + size--; + return curr; + } + + /** + * 移除最后一个元素 + * + * @return——移除元素 + */ + public Object removeLast() { + Node remove = current.next; + Node pre = (Node) this.get(size - 2);// 获得倒数第二个结点 + current.next = pre; + pre.next = null; + size--; + return remove; + } + + /** + * 就是检查一下是不是超出数组界限了,超出了就抛出IndexOutBoundsException异常。 + * + * @param index要用于检查的索引 + */ + private void RangeCheck(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("Index: " + index + " 超出访问范围"); + } + + /** + * 重写toString()方法 + */ + @Override + public String toString() { + String linkedlist = "["; + Node visit = head; + while (visit.next != null) { + visit = visit.next; + if (visit.next == null) { + linkedlist += visit.data.toString() + "]"; + } else { + linkedlist += visit.data.toString() + "--->"; + } + } + return linkedlist; + } + + /** + * 结点内部类,主要要声明为static的 + * + * @author think + * + */ + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + System.out.println(list); + System.out.println(((Node) list.get(3)).data); + list.add(4, "4"); + System.out.println(list); + list.add(0, "0"); + System.out.println(list); + list.addLast("last"); + System.out.println(list); + System.out.println("Removed:" + ((Node) list.remove(1)).data); + System.out.println(list); + System.out.println("Removed:" + ((Node) list.removeFirst()).data); + System.out.println(list); + System.out.println("Removed:" + ((Node) list.removeLast()).data); + System.out.println(list); + } +} diff --git a/group03/763878069/src/cmj/datastructure/list/List.java b/group03/763878069/src/cmj/datastructure/list/List.java new file mode 100644 index 0000000000..8e58ed10f4 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/List.java @@ -0,0 +1,13 @@ +package cmj.datastructure.list; + +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(); +} \ No newline at end of file diff --git a/group03/763878069/src/cmj/datastructure/list/Queue.java b/group03/763878069/src/cmj/datastructure/list/Queue.java new file mode 100644 index 0000000000..88d8a32e38 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/Queue.java @@ -0,0 +1,128 @@ +package cmj.datastructure.list; + +import java.util.Arrays; + +public class Queue { + + private transient Object[] elementData; + private int size; + + /** 数组的头部,即 下次删除数据的 index */ + private int head; + /** 数组的尾部,即 下次插入数据的 index */ + private int tail; + + /** + * Queue 初始化无参数构造函数 + */ + public Queue() { + this(10); + } + + /** + * Queue带容量的构造函数 + * + * @param initialCapacity初始化容量 + */ + public Queue(int initialCapacity) { + super(); + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + // 新建一个数组 + this.elementData = new Object[initialCapacity]; + this.head = 0; + this.tail = 0; + this.size = 0; + + } + + /** + * 检查数组的容量 + * + * @param neededMinCapacity所需最小的容量 + */ + public void ensureCapacity(int neededMinCapacity) { + int currCapacity = elementData.length;// 获取当前数据的全部容量 + // 需要扩容的情况 + if (neededMinCapacity > currCapacity) { + int newCapacity = (currCapacity * 3) / 2 + 1;// 计算新的容量 + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + /** + * 添加数据到尾部 + * + * @param o——要添加的数据 + */ + public void enQueue(Object o) { + // 确定ArrayList的容量大小 + ensureCapacity(size + 1); // Increments modCount!! + // 添加o到ArrayList中 + elementData[tail] = o; + size++; + tail++; + } + + /** + * 删除数据 从头部 + * + * @return——被删除的数据 + */ + public Object deQueue() { + if (isEmpty()) { + throw new RuntimeException("队列为空"); + } + Object deleted = (Object) elementData[head]; + elementData[head] = null; + size--; + head++; + return deleted; + } + + public boolean isEmpty() { + return size <= 0 ? true : false; + } + + public int size() { + return size; + } + + @Override + public String toString() { + if (isEmpty()) { + return "[空队列]"; + } + String arraylist = "["; + for (int i = head; i < tail; i++) { + if (i == tail - 1) { + arraylist += elementData[i].toString() + "]"; + } else { + arraylist += elementData[i].toString() + "--->"; + } + } + return arraylist; + } + + public static void main(String[] args) { + Queue queue = new Queue(4); + queue.enQueue("one"); + queue.enQueue("two"); + queue.enQueue("three"); + queue.enQueue("four"); + queue.enQueue("five"); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + queue.deQueue(); + System.out.println(queue); + + } + +} \ No newline at end of file diff --git a/group03/763878069/src/cmj/datastructure/list/Stack.java b/group03/763878069/src/cmj/datastructure/list/Stack.java new file mode 100644 index 0000000000..bc16af0203 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/list/Stack.java @@ -0,0 +1,58 @@ +package cmj.datastructure.list; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + private int size; + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + Object pop = elementData.get(elementData.size()); + elementData.remove(size - 1); + size--; + return pop; + } + + public Object peek() { + if (size == 0) { + throw new RuntimeException("栈为空"); + } + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size <= 0 ? true : false; + } + + public int size() { + return size; + } + + @Override + public String toString() { + return "Stack [elementData=" + elementData + ", size=" + size + "]"; + } + + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push("a"); + stack.push("b"); + stack.push("c"); + stack.push("d"); + stack.push("e"); + System.out.println(stack); + + stack.pop(); + System.out.println(stack); + stack.pop(); + System.out.println(stack); + stack.pop(); + System.out.println(stack); + + } + +} \ No newline at end of file diff --git a/group03/763878069/src/cmj/datastructure/tree/BSTree.java b/group03/763878069/src/cmj/datastructure/tree/BSTree.java new file mode 100644 index 0000000000..25a822a1a1 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/tree/BSTree.java @@ -0,0 +1,191 @@ +package cmj.datastructure.tree; + +/** + * 二叉搜索树 + * + * @author think + * + */ +public class BSTree { + private Node root;// 根结点 + + public BSTree() { + root = null; + } + + public void insert(Node node, int data) { + if (null == root) { + root = new Node(data); + } else { + if (data < node.data) { + if (null == node.left) { + + node.left = new Node(data); + } else { + insert(node.left, data); + } + } else { + if (node.right == null) { + node.right = new Node(data); + } else { + insert(node.right, data); + } + } + } + } + + /** + * 前序遍历 + * + * @param node + */ + public void preOrder(Node node) { + if (node != null) { + System.out.println(node.data); + preOrder(node.left); + preOrder(node.right); + } + } + + /** + * 中序遍历 + * + * @param node + */ + public void inOrder(Node node) { + if (node != null) { + inOrder(node.left); + System.out.println(node.data); + inOrder(node.right); + } + } + + /** + * 后序遍历 + * + * @param node + */ + public void postOrder(Node node) { + if (node != null) { + postOrder(node.left); + postOrder(node.right); + System.out.println(node.data); + } + } + + // 删除节点分三种方式删除节点 + // 1、删除没有子节点的节点,直接让该节点的父节点的左节点或右节点指向空 + // 2、删除有一个子节点的节点,直接让该节点的父节点指向被删除节点的剩余节点 + // 3、删除有三个节点的子节点,找到要删除节点的后继节点, 用该节点替代删除的节点 + public boolean delete(int data) { + // 首先查找节点,并记录该节点的父节点引用 + Node current = root; + Node parent = root; + boolean isLeftNode = true; + while (current.data != data) { + parent = current; + if (data < current.data) { + isLeftNode = true; + current = current.left; + } else { + isLeftNode = false; + current = current.right; + } + } + if (current == null) { + System.out.println("没有找到要删除的节点!"); + return false; + } + // 下面分三种情况删除节点 + if (current.left == null && current.right == null) { // 要删除的节点没有子节点 + if (current == root) { // 根节点就删除整棵树 + root = null; + } else if (isLeftNode) { // 如果是左节点,做节点指向空 + parent.left = null; + } else { // 如果是右节点,右节点指向空 + parent.right = null; + } + } else if (current.left == null) { // 要删除的节点只有右节点 + if (current == root) { + root = current.right; + } else if (isLeftNode) { + parent.left = current.right; + } else { + parent.right = current.right; + } + } else if (current.right == null) { // 要删除的节点只有左节点 + if (current == root) { + root = current.left; + } else if (isLeftNode) { + parent.left = current.left; + } else { + parent.right = current.left; + } + } else { // 要删除的节点有两个节点 + Node successor = findSuccessor(current); + if (current == root) { + root = successor; + } else if (isLeftNode) { + parent.left = successor; + } else { + parent.right = successor; + } + successor.left = current.left; + } + return true; + } + + /** + * 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。 + * + * @param delNode——要删除的结点 + * @return + */ + private Node findSuccessor(Node delNode) { + Node parent = delNode; + Node successor = delNode; + Node current = delNode.right; + /* 找到要删除结点的右子树的最左叶子结点,就是比要删除的数据大的最小结点 */ + while (current != null) { + parent = successor; + successor = current; + current = current.left; + } + + if (successor != delNode.right) { + parent.left = successor.right; + successor.right = delNode.right; + } + return successor; + } + + /** + * 内部结点类 + * + * @author think + * + */ + private class Node { + private Node left; + private Node right; + private int data; + + public Node(int data) { + this.left = null; + this.right = null; + this.data = data; + } + } + + public static void main(String[] args) { + int[] a = { 2, 4, 12, 45, 21, 6, 111, 1, 23, 45 }; + BSTree bTree = new BSTree(); + for (int i = 0; i < a.length; i++) { + bTree.insert(bTree.root, a[i]); + } + bTree.preOrder(bTree.root); + bTree.inOrder(bTree.root); + bTree.postOrder(bTree.root); + } + +} diff --git a/group03/763878069/src/cmj/datastructure/tree/BinaryTree.java b/group03/763878069/src/cmj/datastructure/tree/BinaryTree.java new file mode 100644 index 0000000000..421d342f36 --- /dev/null +++ b/group03/763878069/src/cmj/datastructure/tree/BinaryTree.java @@ -0,0 +1,185 @@ +package cmj.datastructure.tree; + +public class BinaryTree { + private Node root;// 根结点 + + public BinaryTree() { + root = null; + } + + public void insert(Node node, int data) { + if (null == root) { + root = new Node(data); + } else { + if (data < node.data) { + if (null == node.left) { + + node.left = new Node(data); + } else { + insert(node.left, data); + } + } else { + if (node.right == null) { + node.right = new Node(data); + } else { + insert(node.right, data); + } + } + } + } + + /** + * 前序遍历 + * + * @param node + */ + public void preOrder(Node node) { + if (node != null) { + System.out.println(node.data); + preOrder(node.left); + preOrder(node.right); + } + } + + /** + * 中序遍历 + * + * @param node + */ + public void inOrder(Node node) { + if (node != null) { + inOrder(node.left); + System.out.println(node.data); + inOrder(node.right); + } + } + + /** + * 后序遍历 + * + * @param node + */ + public void postOrder(Node node) { + if (node != null) { + postOrder(node.left); + postOrder(node.right); + System.out.println(node.data); + } + } + + // 删除节点分三种方式删除节点 + // 1、删除没有子节点的节点,直接让该节点的父节点的左节点或右节点指向空 + // 2、删除有一个子节点的节点,直接让该节点的父节点指向被删除节点的剩余节点 + // 3、删除有三个节点的子节点,找到要删除节点的后继节点, 用该节点替代删除的节点 + public boolean delete(int data) { + // 首先查找节点,并记录该节点的父节点引用 + Node current = root; + Node parent = root; + boolean isLeftNode = true; + while (current.data != data) { + parent = current; + if (data < current.data) { + isLeftNode = true; + current = current.left; + } else { + isLeftNode = false; + current = current.right; + } + } + if (current == null) { + System.out.println("没有找到要删除的节点!"); + return false; + } + // 下面分三种情况删除节点 + if (current.left == null && current.right == null) { // 要删除的节点没有子节点 + if (current == root) { // 根节点就删除整棵树 + root = null; + } else if (isLeftNode) { // 如果是左节点,做节点指向空 + parent.left = null; + } else { // 如果是右节点,右节点指向空 + parent.right = null; + } + } else if (current.left == null) { // 要删除的节点只有右节点 + if (current == root) { + root = current.right; + } else if (isLeftNode) { + parent.left = current.right; + } else { + parent.right = current.right; + } + } else if (current.right == null) { // 要删除的节点只有左节点 + if (current == root) { + root = current.left; + } else if (isLeftNode) { + parent.left = current.left; + } else { + parent.right = current.left; + } + } else { // 要删除的节点有两个节点 + Node successor = findSuccessor(current); + if (current == root) { + root = successor; + } else if (isLeftNode) { + parent.left = successor; + } else { + parent.right = successor; + } + successor.left = current.left; + } + return true; + } + + /** + * 找结点(x)的后继结点。即,查找"二叉树中数据值大于该结点"的"最小结点"。 + * + * @param delNode——要删除的结点 + * @return + */ + private Node findSuccessor(Node delNode) { + Node parent = delNode; + Node successor = delNode; + Node current = delNode.right; + /* 找到要删除结点的右子树的最左叶子结点,就是比要删除的数据大的最小结点 */ + while (current != null) { + parent = successor; + successor = current; + current = current.left; + } + + if (successor != delNode.right) { + parent.left = successor.right; + successor.right = delNode.right; + } + return successor; + } + + /** + * 内部结点类 + * + * @author think + * + */ + private class Node { + private Node left; + private Node right; + private int data; + + public Node(int data) { + this.left = null; + this.right = null; + this.data = data; + } + } + + public static void main(String[] args) { + int[] a = { 2, 4, 12, 45, 21, 6, 111 }; + BinaryTree bTree = new BinaryTree(); + for (int i = 0; i < a.length; i++) { + bTree.insert(bTree.root, a[i]); + } + bTree.preOrder(bTree.root); + bTree.inOrder(bTree.root); + bTree.postOrder(bTree.root); + } + +} diff --git a/group03/894844916/coding2017-01/.classpath b/group03/894844916/coding2017-01/.classpath new file mode 100644 index 0000000000..63b7e892d1 --- /dev/null +++ b/group03/894844916/coding2017-01/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group03/894844916/coding2017-01/.project b/group03/894844916/coding2017-01/.project new file mode 100644 index 0000000000..34cd5ea671 --- /dev/null +++ b/group03/894844916/coding2017-01/.project @@ -0,0 +1,17 @@ + + + coding2017-01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group03/894844916/coding2017-01/.settings/org.eclipse.jdt.core.prefs b/group03/894844916/coding2017-01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group03/894844916/coding2017-01/.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/group03/894844916/coding2017-01/src/com/coding/basic/ArrayList.java b/group03/894844916/coding2017-01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f245739d4c --- /dev/null +++ b/group03/894844916/coding2017-01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,149 @@ +/** + * + */ +package com.coding.basic; + +/** + * @author patchouli + * + */ +public class ArrayList implements List { + + /** + * 数组列表初始默认容量100. + */ + private static final int DEFAULT_CAPACITY=100; + + /** + * 数组列表中元素的数量。 + */ + private int size = 0; + + /** + * 数组列表当前容量 + */ + private int capacity; + + /** + * 存放元素的数组。 + */ + private Object[] elementData; + + /** + * 默认构造函数,构造一个初始容量为100的数组列表 + */ + public ArrayList() { + capacity=DEFAULT_CAPACITY; + elementData=new Object[DEFAULT_CAPACITY]; + } + + /* + * (non-Javadoc) + * + * @see nusub.coding2017.basic.List#add(java.lang.Object) + */ + @Override + public void add(Object o) { + expand(); + elementData[size]=o; + size=size++; + } + + /* + * (non-Javadoc) + * + * @see nusub.coding2017.basic.List#add(int, java.lang.Object) + */ + @Override + public void add(int index, Object o) throws ListIndexException { + if (index==size) { + add(o); + return; + } + if (0<=index&&indexindex; i--) { + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + return; + } + throw new ListIndexException("index不在[0,size]之间"); + } + + /** + * index在[0,size)之间返回通过数组下标访问的方式获取位置index处的元素,index超出这个范围抛出ListIndexException。 + * @param index 元素的位置 + * @return 获取的对象 + * @throws ListIndexException + */ + @Override + public Object get(int index) throws ListIndexException { + if (0<=index&&index + * + */ +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(){} + + public BinaryTreeNode(Object data){ + this.data=data; + left=null; + right=null; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + if (o.toString().compareTo(data.toString())<0) { + if (left==null) { + left=new BinaryTreeNode(o); + return left; + } + insert(o); + } + else { + if (right==null) { + right=new BinaryTreeNode(o); + return right; + } + insert(o); + } + return null; + } + +} diff --git a/group03/894844916/coding2017-01/src/com/coding/basic/Iterator.java b/group03/894844916/coding2017-01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..75a1ffaf3e --- /dev/null +++ b/group03/894844916/coding2017-01/src/com/coding/basic/Iterator.java @@ -0,0 +1,22 @@ +/** + * + */ +package com.coding.basic; + +/** + * @author patchouli + * + */ +public interface Iterator { + /** + * 集合中存在下一个元素返回true,不存在下一个元素返回false。 + * @return + */ + public boolean hasNext(); + + /** + * 返回集合中下一个元素 + * @return + */ + public Object next(); +} diff --git a/group03/894844916/coding2017-01/src/com/coding/basic/LinkedList.java b/group03/894844916/coding2017-01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e811536093 --- /dev/null +++ b/group03/894844916/coding2017-01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,168 @@ +/** + * + */ +package com.coding.basic; + +/** + * @author patchouli + * + */ +public class LinkedList implements List, Iterator { + + private Node head; + private Node current=head; + private int size=0; + + /* (non-Javadoc) + * @see nusub.coding2017.basic.Iterator#hasNext() + */ + @Override + public boolean hasNext() { + if (current.next==null) { + return false; + } + return true; + } + + /* (non-Javadoc) + * @see nusub.coding2017.basic.Iterator#next() + */ + @Override + public Object next() { + current=current.next; + return current; + } + + /** + * 从头结点遍历,找到最后一个节点,最后一个节点引用下一个元素。最后把current指向头结点。 + * @param o 要添加的对象 + */ + @Override + public void add(Object o) { + Node node=new LinkedList.Node(); + node.data=o; + if (head==null) { + head=node; + current=head; + size++; + return; + } + while (hasNext()) { + next(); + } + current.next=node; + current=head; + size++; + } + + /** + * 在指定的位置插入一个元素,并把当前节点指向head。 + * @throws ListIndexException + */ + @Override + public void add(int index, Object o) throws ListIndexException { + if (index<0||index>size) { + throw new ListIndexException("index必须在[0,size]之间"); + } + Node node=new LinkedList.Node(); + node.data=o; + if (index==0) { + node.next=current; + head=node; + current=node; + } + else{ + int i=0; + while (i=size) { + throw new ListIndexException("index必须在[0,size)之间"); + } + if (index==0) { + return current; + } + int i=0; + while (i=size) { + throw new ListIndexException("index必须在[0,size)之间"); + } + if (size==0) { + return null; + } + Node node=null; + if (index==0) { + node=current; + current=current.next; + } + else if (index + + + + + + diff --git a/group04/1020483199/1020483199Learning/.gitignore b/group04/1020483199/1020483199Learning/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/1020483199/1020483199Learning/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/1020483199/1020483199Learning/.project b/group04/1020483199/1020483199Learning/.project new file mode 100644 index 0000000000..1ee8060dd3 --- /dev/null +++ b/group04/1020483199/1020483199Learning/.project @@ -0,0 +1,17 @@ + + + 1020483199Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/1020483199/1020483199Learning/.settings/org.eclipse.core.resources.prefs b/group04/1020483199/1020483199Learning/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..f7e195b54e --- /dev/null +++ b/group04/1020483199/1020483199Learning/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding//src/com/coding/basic=UTF-8 +encoding/=UTF-8 diff --git a/group04/1020483199/1020483199Learning/.settings/org.eclipse.jdt.core.prefs b/group04/1020483199/1020483199Learning/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group04/1020483199/1020483199Learning/.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/group04/1020483199/1020483199Learning/src/com/coding/basic/ArrayList.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..a61325db63 --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,128 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + + private int size = 0; + + private transient Object[] elementData = new Object[100]; + /** + * 向数组中添加某个元素 + */ + public void add(Object o){ + /** + * 数组扩容判断 + */ + ensureSize(size+1); + elementData[size++] = o; + } + /** + * 向指定位置数组中添加某个元素 + */ + public void add(int index, Object o){ + if(index<0||index>size){ + throw new IndexOutOfBoundsException("数组越界"); + } + ensureSize(size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + } + /** + * 获取数组中某个位置元素 + */ + public Object get(int index){ + if(index<0||index>elementData.length){ + return null; + }else{ + return elementData[index]; + } + + } + /** + * 移除数组中指定位置元素 + */ + public Object remove(int index){ + if(index<0||index>elementData.length){ + return null; + }else{ + int newLength = size-index-1; + if (newLength>0) + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData[--size] = null; + return elementData; + } + } + /** + * 获取当前数组的大小 + */ + public int size(){ + if(size>0){ + return this.size; + }else{ + return 0; + } + } + /** + * 利用arraylist实现迭代器 + * @return + */ + public Iterator iterator(){ + + return new ArrayListIterator(); + } + private class ArrayListIterator implements Iterator{ + int cursor; + int lastReset = -1; + @Override + public boolean hasNext() { + return size!=cursor; + } + + @Override + public Object next() { + //标记索引当前位置 + int i = cursor; + if(i>size){ + throw new NoSuchElementException(); + } + Object[] newData = elementData; + if(i>newData.length){ + throw new ConcurrentModificationException(); + } + cursor = i + 1; + return newData[lastReset = i]; + } + + } + + + /** + * @author sulei + * @param minCapcity + */ + public void ensureSize(int minCapcity){ + if(minCapcity>elementData.length){ + grow(minCapcity); + } + } + + /** + * @author sulei + * @param autoCapcity + */ + public void grow(int autoCapcity){ + int oldLength = elementData.length; + if(autoCapcity>oldLength){ + Object[] oldData = elementData; + int newLength = (oldLength * 3)/2 + 1; + if(autoCapcity>newLength){ + newLength=autoCapcity; + } + elementData = Arrays.copyOf(elementData, newLength); + } + } +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/basic/BinaryTreeNode.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..48026be26c --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,82 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + //存放当前树 + private BinaryTreeNode primary; + + public BinaryTreeNode getPrimary() { + return primary; + } + + public void setPrimary(BinaryTreeNode primary) { + this.primary = primary; + } + + + 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; + } + /** + * 在二叉树中插入 + * @param o + * @return + */ + public BinaryTreeNode insert(Object o){ + if(o==null){ + throw new IllegalArgumentException("二叉树的元素不能为空!"); + } + //新建要插入的节点 + BinaryTreeNode bt = new BinaryTreeNode(); + bt.setData(o); + int value = (int)o; + //当原始二叉树为空时 + if(primary==null){ + primary = bt; + }else{ + BinaryTreeNode bi = primary; + while(true){ + if(value<(int)bi.data){ + if(bi.left==null){ + bi.setLeft(bt); + break; + }else{ + bi=bi.left; + } + }else if(value>(int)bi.data){ + if(bi.right==null){ + bi.setRight(bt); + break; + }else{ + bi=bi.right; + } + + }else{ + System.out.println("当前元素在二叉树已存在"); + break; + } + } + } + + return bt; + } + +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/basic/Iterator.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/1020483199/1020483199Learning/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/group04/1020483199/1020483199Learning/src/com/coding/basic/LinkedList.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a85c311f72 --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,175 @@ +package com.coding.basic; + +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + /** + * 当前节点的大小 + */ + private int size; + /** + * 头节点 + */ + private Node header; + /** + * 尾节点 + */ + private Node end; + + + + public void add(Object o){ + if(header==null){ + header = new Node(); + header.data = o; + end = header; + }else{ + Node n = new Node(); + n.data = o; + end.next = n; + n.previous=end; + end = n; + } + size++; + } + public void add(int index,Object o){ + judge(index); + Node n = new Node(); + n.data = o; + Node temp = header; + for(int i = 0;isize){ + throw new NoSuchElementException(); + } + Node n = header; + for(int j = 0;j < i;j ++){ + n = n.next; + } + cursor = i + 1; + lastReset = i; + return n.data; + } + + } + + + /** + * 判断数组是否越界方法 + */ + public void judge(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException("数组越界"); + } + } + /** + * @author sulei + */ + private static class Node{ + Object data;//当前节点数据 + Node next;//下一个节点 + Node previous;//上一个节点 + } +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/basic/List.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group04/1020483199/1020483199Learning/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/group04/1020483199/1020483199Learning/src/com/coding/basic/Queue.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b9a26bb40e --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/Queue.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class Queue { + /** + * 队列长度 + */ + private int size; + + private LinkedList elementData = new LinkedList(); + /** + * 入队操作[向栈尾插入] + * @param o + */ + public void enQueue(Object o){ + elementData.add(o); + size++; + } + + public Object deQueue(){ + Object obj = elementData.removeFirst(); + size--; + return obj; + } + + public boolean isEmpty(){ + if(size==0){ + return true; + }else{ + return false; + } + + } + + public int size(){ + return size; + } + +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/basic/Stack.java b/group04/1020483199/1020483199Learning/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..535922bb35 --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/basic/Stack.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +public class Stack { + /** + * 当前栈的大小 + */ + private int size; + + private ArrayList elementData = new ArrayList(); + /** + * 压栈 + * @param o + */ + public void push(Object o){ + elementData.add(o); + size++; + } + /** + * 出栈 + * @return + */ + public Object pop(){ + return elementData.remove(--size); + } + /** + * 返回栈顶元素 + * @return + */ + public Object peek(){ + return elementData.get(size-1); + } + public boolean isEmpty(){ + return size==0; + } + public int size(){ + return size; + } +} diff --git a/group04/1020483199/1020483199Learning/src/com/coding/test/JavaTest.java b/group04/1020483199/1020483199Learning/src/com/coding/test/JavaTest.java new file mode 100644 index 0000000000..045e3d7a48 --- /dev/null +++ b/group04/1020483199/1020483199Learning/src/com/coding/test/JavaTest.java @@ -0,0 +1,223 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import java.math.BigDecimal; + +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import com.coding.basic.List; +import com.coding.basic.Queue; +import com.coding.basic.Stack; + + + +public class JavaTest { + /** + * List集合单元测试内容 + */ + @Test + public void test() { + List list = new ArrayList(); + list.add(0, "aa"); + } + + @Test + public void test1() { + List list = new ArrayList(); + list.add("aa"); + System.out.println(list.get(0)); + } + + @Test + public void test2() { + List list = new ArrayList(); + list.add("aa"); + System.out.println(list.get(0)); + list.remove(0); + System.out.println(list.get(0)); + } + + @Test + public void test3() { + List list = new ArrayList(); + list.add("aa"); + System.out.println(list.size()); + list.add("aa"); + System.out.println(list.size()); + } + + @Test + public void test4() { + ArrayList list = new ArrayList(); + Integer [] str={11,22}; + list.add(str); + Iterator it = list.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + + } + LinkedList ll = new LinkedList(); + } + + /** + * LinkedList测试 + */ + @Test + public void test5() { + List list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + System.out.println(list.get(1)); + } + + @Test + public void test6() { + List list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.add(2, "ff"); + System.out.println(list.get(3)); + } + + @Test + public void test7() { + List list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.remove(2); + System.out.println(list.get(0)+"----"+list.get(1)); + } + + @Test + public void test8() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + System.out.println(list.get(0)); + list.addFirst("haha"); + System.out.println(list.get(0)); + } + + @Test + public void test9() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.addLast("haha"); + System.out.println(list.get(3)); + } + + @Test + public void test10() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.removeFirst(); + System.out.println(list.get(0)); + } + + @Test + public void test11() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + list.removeLast(); + System.out.println(list.get(2)); + } + + @Test + public void test12() { + LinkedList list = new LinkedList(); + list.add("aa"); + list.add("bb"); + list.add("cc"); + Iterator it = list.iterator(); + while(it!=null&&it.hasNext()){ + System.out.println(it.next()); + } + } + + /** + * Queue集合测试 + */ + @Test + public void test13() { + Queue q = new Queue(); + System.out.println(q.size()+"------"+q.isEmpty()); + q.enQueue("aaa"); + q.enQueue("bbb"); + q.enQueue("ccc"); + System.out.println(q.size()+"------"+q.isEmpty()); + q.deQueue(); + System.out.println(q.size()+"------"+q.isEmpty()); + } + + /** + * Stack集合测试 + */ + @Test + public void test14() { + Stack q = new Stack(); + System.out.println(q.isEmpty()); + System.out.println(q.size()); + q.push("aa"); + q.push("bb"); + q.push("cc"); + q.push("dd"); + System.out.println(q.size()); + q.pop(); + System.out.println(q.size()); + System.out.println(q.peek()); + q.pop(); + System.out.println(q.size()); + System.out.println(q.peek()); + System.out.println(q.isEmpty()); + } + + /** + * BinaryTree测试 + */ + @Test + public void test15(){ + BinaryTreeNode bt = new BinaryTreeNode(); + Integer[] data={3,2,5,4,6,8}; + for(Integer i=0;i=1;i--){ + if(y%i==0&&x%i==0){ + maxValue = i; + break; + } + } + System.out.println(x/maxValue+"比"+y/maxValue); + + } + + + +} diff --git a/group04/1020483199/RemoteSystemsTempFiles/.project b/group04/1020483199/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group04/1020483199/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group04/120549547/base/buil.bat b/group04/120549547/base/buil.bat new file mode 100644 index 0000000000..7164a42ac0 --- /dev/null +++ b/group04/120549547/base/buil.bat @@ -0,0 +1,5 @@ +javac -sourcepath src -d classes -encoding utf-8 src\com\coding\basic\*.java + +java -cp classes com.coding.basic.Main + +pause \ No newline at end of file diff --git a/group04/120549547/base/src/com/coding/basic/ArrayList.java b/group04/120549547/base/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..56e7aa4c26 --- /dev/null +++ b/group04/120549547/base/src/com/coding/basic/ArrayList.java @@ -0,0 +1,93 @@ +package com.coding.basic; +import java.util.Arrays; + +public class ArrayList implements List { + /*默认数组容量*/ + public static final int DEFAULT_CAPCATITY = 10; + + /*数组元素个数*/ + private int size; + + private Object[] elementData; + + public ArrayList(){ + this(DEFAULT_CAPCATITY); + } + + public ArrayList(int capacity){ + if (capacity<0 || capacity>Integer.MAX_VALUE) + throw new IllegalArgumentException("非法容量: " + capacity); + elementData = new Object[capacity]; + } + + + public void add(Object o){ + /*判断是否需要扩容*/ + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o){ + checkIndex(index); + /*判断是否需要扩容*/ + ensureCapacity(size + 1); + /*将index->size的元素依次向右移一个位置*/ + System.arraycopy(elementData, index, elementData, index+1, size-index); + /*插入o的值*/ + elementData[index] = o; + size++; + } + + + public Object get(int index){ + + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + + checkIndex(index); + Object value = elementData[index]; + /*将index+1 -> size的元素依次向左移一个位置*/ + System.arraycopy(elementData,index+1, elementData, index, size-index-1); + elementData[--size]=null; //释放最后一个元素,同时size减一; + return value; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return null; + } + /* 确认容量是否足够,也可以直接调用手动扩容*/ + public void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + /*容量不足需要扩容*/ + if(oldCapacity < minCapacity){ + int newCapacity = oldCapacity * 2; //扩大2倍 + if (newCapacity < minCapacity){ + newCapacity = minCapacity; //扩容后仍不足,取最大值 + } + System.out.println("数据扩容至: " + newCapacity); + elementData = Arrays.copyOf(elementData, newCapacity); + } + + } + + private void checkIndex(int index){ + if (index<0 || index>= size) + throw new IndexOutOfBoundsException ("index非法: " + index); + } + @Override + public String toString(){ + + StringBuffer sb = new StringBuffer(""); + for(int i=0; i "); + } + return sb.toString(); + } +} diff --git a/group04/120549547/base/src/com/coding/basic/BinaryTreeNode.java b/group04/120549547/base/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group04/120549547/base/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/group04/120549547/base/src/com/coding/basic/Iterator.java b/group04/120549547/base/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/120549547/base/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/group04/120549547/base/src/com/coding/basic/LinkedList.java b/group04/120549547/base/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..edfbcc1007 --- /dev/null +++ b/group04/120549547/base/src/com/coding/basic/LinkedList.java @@ -0,0 +1,127 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + /*创建一个带头节点的单链表*/ + public LinkedList(){ + head = new Node(null); + } + + /*添加一个元素(尾插法)*/ + public void add(Object o){ + + Node node = new Node(o); + Node pos = head; + //找到最后一个元素位置 + while(pos.next != null){ + pos = pos.next; + } + pos.next = node; + size++; + + } + + /*在index位置插入*/ + public void add(int index , Object o){ + //判断索引是否合法 + checkIndex(index); + Node node = new Node(o); + Node pos = head; + //找到插入位置 + while(index-- != 0){ + pos = pos.next; + } + node.next = pos.next; + pos.next = node; + size++; + + } + public Object get(int index){ + checkIndex(index); + if(this.isEmpty()){ + throw new IllegalArgumentException ("链表为空"); + } + Node pos = head.next; //pos指向要获取的节点 + while(index-- !=0){ + pos = pos.next; + } + + return pos.data; + } + public Object remove(int index){ + checkIndex(index); + if(this.isEmpty()){ + throw new IllegalArgumentException ("链表为空"); + } + + Node pos = head; //pos指向要删除的前一个结点 + while(index-- != 0){ + pos = pos.next; + } + Node value = pos.next; //要删除的节点 + pos.next = value.next; + size--; + return value.data; + + + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + this.add(o); + } + public Object removeFirst(){ + if(this.isEmpty()){ + throw new IllegalArgumentException ("链表为空"); + } + return remove(0); + } + public Object removeLast(){ + if(this.isEmpty()){ + throw new IllegalArgumentException ("链表为空"); + } + return remove(size-1); + } + + public boolean isEmpty(){ + return size == 0; + } + public Iterator iterator(){ + return null; + } + + private void checkIndex(int index){ + if (index<0 || index>=size ){ + throw new IllegalArgumentException ("index不合法: " + index ); + } + } + + @Override + public String toString(){ + + StringBuffer sb = new StringBuffer(""); + for(int i=0; i "); + } + return sb.toString(); + } + + private static class Node{ + public Object data; + public Node next; + + public Node(Object data){ + this.data = data; + this.next = null; + } + + } +} diff --git a/group04/120549547/base/src/com/coding/basic/List.java b/group04/120549547/base/src/com/coding/basic/List.java new file mode 100644 index 0000000000..d47df585d9 --- /dev/null +++ b/group04/120549547/base/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +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/group04/120549547/base/src/com/coding/basic/Main.java b/group04/120549547/base/src/com/coding/basic/Main.java new file mode 100644 index 0000000000..de8a0b0990 --- /dev/null +++ b/group04/120549547/base/src/com/coding/basic/Main.java @@ -0,0 +1,85 @@ +package com.coding.basic; +import com.coding.basic.*; + +class Main{ + + public static void main(String[] args){ + System.out.println("数组测试开始"); + ArrayListTest(); + System.out.println("----------------分割线----------------"); + System.out.println("链表测试开始"); + LinkedListTest(); + System.out.println("----------------分割线----------------"); + System.out.println("栈测试开始"); + StatckTest(); + System.out.println("----------------分割线----------------"); + System.out.println("队测试开始"); + QueueTest(); + } + + public static void ArrayListTest(){ + ArrayList list = new ArrayList(2); + list.add("HelloBobi0"); + list.add("HelloBobi1"); + list.add("HelloBobi2"); + list.add("HelloBobi3"); + list.add("HelloBobi4"); + list.add("HelloBobi5"); + System.out.println((String)list.get(0)); + System.out.println((String)list.get(4)); + list.add(3, "Hei Man"); + list.remove(5); + System.out.println(list); + System.out.println("size:=" + list.size()); + } + + public static void LinkedListTest(){ + LinkedList ll = new LinkedList(); + + + ll.add("SingleDog0"); + ll.add("SingleDog1"); + ll.add("SingleDog2"); + ll.add("SingleDog3"); + ll.add("SingleDog4"); + ll.add("SingleDog5"); + + System.out.println((String)(ll.get(1))); + System.out.println(ll); + System.out.println("size:=" + ll.size()); + ll.remove(0); + ll.removeFirst(); + ll.removeLast(); + System.out.println(ll); + } + + public static void StatckTest(){ + Stack stack = new Stack(); + stack.push("虾师傅0"); + stack.push("虾师傅1"); + stack.push("虾师傅2"); + stack.push("虾师傅3"); + stack.push("虾师傅4"); + + stack.pop(); + System.out.println(stack.peek()); + System.out.println(stack); + + } + public static void QueueTest(){ + Queue queue = new Queue(); + queue.enQueue("龙师傅0"); + queue.enQueue("龙师傅1"); + queue.enQueue("龙师傅2"); + queue.enQueue("龙师傅3"); + queue.enQueue("龙师傅4"); + + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.deQueue()); + System.out.println(queue.size()); + + + } +} \ No newline at end of file diff --git a/group04/120549547/base/src/com/coding/basic/Queue.java b/group04/120549547/base/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..8f86f7492c --- /dev/null +++ b/group04/120549547/base/src/com/coding/basic/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; +import com.coding.basic.*; +public class Queue { + private LinkedList list; + + public Queue(){ + list = new LinkedList(); + } + /*入队*/ + public void enQueue(Object o){ + list.addLast(o); + } + + /*出队*/ + public Object deQueue(){ + if (isEmpty()){ + System.out.println("队空"); + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + return list.isEmpty(); + } + + public int size(){ + return list.size(); + } +} diff --git a/group04/120549547/base/src/com/coding/basic/Stack.java b/group04/120549547/base/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..f7d3bba3db --- /dev/null +++ b/group04/120549547/base/src/com/coding/basic/Stack.java @@ -0,0 +1,37 @@ +package com.coding.basic; +import com.coding.basic.LinkedList; + +public class Stack { + private LinkedList list; + + public Stack(){ + list = new LinkedList(); + } + + public void push(Object o){ + list.addLast(o); + } + + public Object pop(){ + if(isEmpty()){ + System.out.println("栈空"); + } + return list.removeLast(); + + } + + public Object peek(){ + return list.get(list.size()-1); + } + public boolean isEmpty(){ + return list.isEmpty(); + } + public int size(){ + return list.size(); + } + + @Override + public String toString(){ + return list.toString(); + } +} diff --git a/group04/120549547/my.txt b/group04/120549547/my.txt new file mode 100644 index 0000000000..3da1ec26e9 --- /dev/null +++ b/group04/120549547/my.txt @@ -0,0 +1 @@ +HelloWorld diff --git a/group04/120549547/shell.sh b/group04/120549547/shell.sh new file mode 100644 index 0000000000..ab61214ddd --- /dev/null +++ b/group04/120549547/shell.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +git remote -v +read anykey +echo "抓取上游代码" +git fetch upstream +echo "代码合并" +git merge upstream/master +read anykey +echo "添加代码" +git add -A . +read anykey +git commit -m "bobi" + +echo "推送到githup" +read anykey +git push origin master +read anykey diff --git a/group04/1299310140/src/com/coding/basic/ArrayList.java b/group04/1299310140/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..afc073293a --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/ArrayList.java @@ -0,0 +1,125 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + private Object[] elementData = new Object[10]; + + public void add(Object o){ + if(size < this.elementData.length){//加至末尾,size+1 + this.elementData[size] = o; + size++; + }else{//扩张数组,然后加至末尾,size+1 + this.elementData = grow(this.elementData,10); + this.elementData[size] = o; + size++; + } + } + + public void add(int index, Object o){//-1 this.size){//index小于0or大于size,参数错误 + return; + } + if(size >= this.elementData.length){//当前数组容量已满,需扩张 + this.elementData = grow(this.elementData, 10); + } + + //此时只需考虑将o加至index处(0= index;i--){ + this.elementData[i] = this.elementData[i-1]; + } + this.elementData[index] = o; + this.size++; + }else{//直接插入o,size+1 + this.elementData[index] = o; + this.size++; + } + + } + + public Object get(int index){//-1= this.size){//index小于0or大于等于size,参数错误 + return null; + } + return this.elementData[index]; + } + + public Object remove(int index){//-1= this.size){//index小于0or大于等于size,参数错误 + return null; + } + + Object o = this.elementData[index];//o保存被移除的值 + //此时只需考虑将index处的o移除 + for(int i = index;i < this.size-1;i++){ + this.elementData[i] = this.elementData[i+1]; + } + this.size--; + return o; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(this); + } + + public static class ArrayListIterator implements Iterator{ + private ArrayList list; + private int pres; + + public ArrayListIterator(ArrayList list) { + super(); + this.list = list; + this.pres = 0; + } + + @Override + public boolean hasNext() { + if(this.pres < this.list.size()){ + return true; + }else{ + return false; + } + } + + @Override + public Object next() { + Object o = this.list.get(this.pres); + this.pres++; + return o; + } + + } + + /* + * 说明:扩张数组 + * 参数:被扩张的原数组,扩张的增加数(扩张后数组的大小为原数组的长度+增加数) + * 返回值:扩张后的数组 + */ + private static Object[] grow(Object[] src,int size){ +// return Arrays.copyOf(src, src.length + size); + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public String toString(){ + String result = "["; + if(this.size == 0){ + result = result + "]"; + return result; + }else{ + for(int i = 0;i < size;i++){ + result = result + this.elementData[i] + ","; + } + result = result.substring(0,result.length()-1); + result = result + "]"; + return result; + } + } + +} diff --git a/group04/1299310140/src/com/coding/basic/BinaryTreeNode.java b/group04/1299310140/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..dd539df626 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,73 @@ +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(this.data == null){//根节点为空 + this.data = o; + }else{//根节点非空 + BinaryTreeNode pres = this; + BinaryTreeNode insertNode = new BinaryTreeNode(); + insertNode.setData(o); + while(pres != null){ + if((int)o < (int)pres.data){//插入值<当前值,pres向左移动,或者将插入节点挂在当前节点左边 + if(pres.left == null){ + pres.left = insertNode; + break; + } + pres = pres.left; + }else{//插入值>=当前值,pres向右移动,或者将插入节点挂在当前节点右边 + if(pres.right == null){ + pres.right = insertNode; + break; + } + pres = pres.right; + } + } + } + return null; + } + + public void print(){ + if(this.data == null){ + return; + }else{ + if(this.left !=null){ + this.left.print(); + } + System.out.print(this.data); + System.out.print(" "); + if(this.right != null){ + this.right.print(); + } + } + } +} diff --git a/group04/1299310140/src/com/coding/basic/LinkedList.java b/group04/1299310140/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..4636bbd279 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/LinkedList.java @@ -0,0 +1,211 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if(this.size == 0){//size为0,给head赋值 + this.head = new Node(o); + size++; + }else{//将o加至链表末尾 + Node Last = new Node(o); + Node pres = this.head; + while(pres.next != null){ + pres = pres.next; + } + pres.next = Last; + size++; + } + } + + public void add(int index , Object o){//index:0~size + if(index < 0 || index > this.size){//index小于0or大于size,参数错误 + return; + } + if(index == 0){//将o加至链表头部 + //size为0时,index必为0,执行该分支 + Node first = new Node(o); + first.next = this.head; + this.head = first; + size++; + }else if(index == size){//将o加至链表末尾 + //index == size != 0时,执行该分支 + Node Last = new Node(o); + Node pres = this.head; + while(pres.next != null){ + pres = pres.next; + } + pres.next = Last; + size++; + }else{ + //0= this.size){//index小于0or大于等于size,参数错误 + return null; + } + + Node pres = this.head;//pres指向0 + for(int i = 0;i < index;i++){ + pres = pres.next; + } + //此时pres指向index + return pres.data; + } + + public Object remove(int index){//index:0~size-1 + if(index < 0 || index >= this.size){//index小于0or大于等于size,参数错误 + return null; + } + + Object o = null; + if(index == 0){//删除头节点 + o = this.head.data; + this.head = this.head.next; + size--; + }else{//删除头节点以外的其他节点 + Node pres = this.head;//pres指向0 + for(int i = 0;i < index-1;i++){ + pres = pres.next; + } + + //此时pres指向index-1 + o = pres.next.data; + pres.next = pres.next.next; + size--; + } + return o; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){//同add(int 0 , Object o) + Node first = new Node(o); + first.next = this.head; + this.head = first; + size++; + } + + public void addLast(Object o){//同add(int size , Object o) + if(this.size == 0){ + this.head = new Node(o); + size++; + }else{//size>=1 + Node Last = new Node(o); + Node pres = this.head; + while(pres.next != null){ + pres = pres.next; + } + pres.next = Last; + size++; + } + } + + public Object removeFirst(){//同remove(int 0) + if(this.size == 0){ + return null; + } + + Object o = this.head.data; + this.head = this.head.next; + size--; + return o; + } + + public Object removeLast(){ + if(this.size == 0){ + return null; + } + + Object o = null; + if(this.size == 1){//size==1 + o = this.head.data; + this.head = null; + this.size--; + }else{//size>=2 + Node pres = this.head; + while(pres.next.next != null){ + pres = pres.next; + } + o = pres.next.data; + pres.next = null; + size--; + } + return o; + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private static class LinkedListIterator implements Iterator{ +// private LinkedList list; + private Node pres; + + public LinkedListIterator(LinkedList list) { + super(); +// this.list = list; + this.pres = list.head; + } + + @Override + public boolean hasNext() { + if(this.pres != null){ + return true; + }else{ + return false; + } + } + + @Override + public Object next() { + Object o = this.pres.data; + pres = pres.next; + return o; + } + + } + + private static class Node{ + Object data; + Node next; + + public Node(Object data) { + super(); + this.data = data; + } + } + + public String toString(){ + String result = "["; + if(this.size == 0){ + result = result + "]"; + return result; + }else{ + Node pres = this.head; + while(pres != null){ + result = result + pres.data + ","; + pres = pres.next; + } + result = result.substring(0,result.length()-1); + result = result + "]"; + return result; + } + } +} diff --git a/group04/1299310140/src/com/coding/basic/Queue.java b/group04/1299310140/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..181f0cfcb0 --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + this.elementData.addLast(o); + } + + public Object deQueue(){ + return this.elementData.removeFirst(); + } + + public boolean isEmpty(){ + if(this.elementData.size() == 0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return this.elementData.size(); + } + + public String toString(){ + return this.elementData.toString(); + } +} diff --git a/group04/1299310140/src/com/coding/basic/Stack.java b/group04/1299310140/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..c0a3adac8e --- /dev/null +++ b/group04/1299310140/src/com/coding/basic/Stack.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + this.elementData.add(o); + } + + public Object pop(){ + return this.elementData.remove(this.elementData.size()-1); + } + + public Object peek(){ + return this.elementData.get(this.elementData.size()-1); + } + + public boolean isEmpty(){ + if(this.elementData.size() == 0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return this.elementData.size(); + } + + public String toString(){ + return this.elementData.toString(); + } + +} diff --git a/group04/1751801281/.classpath b/group04/1751801281/.classpath new file mode 100644 index 0000000000..e72ef7c0d4 --- /dev/null +++ b/group04/1751801281/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group04/1751801281/.gitignore b/group04/1751801281/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/1751801281/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/1751801281/.project b/group04/1751801281/.project new file mode 100644 index 0000000000..767083b86b --- /dev/null +++ b/group04/1751801281/.project @@ -0,0 +1,17 @@ + + + 1751801281Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/1751801281/src/com/coding/basic/ArrayList.java b/group04/1751801281/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..68bc111bc4 --- /dev/null +++ b/group04/1751801281/src/com/coding/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o) { + // 进行扩容检查 + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException("数组越界异常"); + ensureCapacity(size + 1); + // 对数组进行复制处理,目的就是空出index的位置插入o,并将index后的元素位移一个位置 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + if (index < 0 || index > elementData.length) { + return null; + } else if (index > size && index < elementData.length) { + return null; + } else { + return elementData[index]; + } + } + + public Object remove(int index) { + if (index < 0 || index > elementData.length) { + return null; + } else { + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return elementData[index]; + } + } + + public int size() { + return size; + } + + public void ensureCapacity(int minCapacity) { + if (minCapacity > elementData.length) { + grow(minCapacity); + } + } + + public void grow(int minCapacity) { + // 当前数组的长度 + int oldCapacity = elementData.length; + // 最小需要的容量大于当前数组的长度则进行扩容 + if (minCapacity > oldCapacity) { + Object oldData[] = elementData; + // 新扩容的数组长度为旧容量的1.5倍+1 + int newCapacity = (oldCapacity * 3) / 2 + 1; + // 如果新扩容的数组长度还是比最小需要的容量小,则以最小需要的容量为长度进行扩容 + if (newCapacity < minCapacity) + newCapacity = minCapacity; + // 进行数据拷贝,Arrays.copyOf底层实现是System.arrayCopy() + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + int cursor; // index of next element to return + int lastRet = -1; // index of last element returned; -1 if no such + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return elementData[lastRet = i]; + } + } +} diff --git a/group04/1751801281/src/com/coding/basic/Iterator.java b/group04/1751801281/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group04/1751801281/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group04/1751801281/src/com/coding/basic/LinkedList.java b/group04/1751801281/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..3fca710056 --- /dev/null +++ b/group04/1751801281/src/com/coding/basic/LinkedList.java @@ -0,0 +1,133 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size; + + private static class Node { + Object data; + Node next; + Node previous; + } + + public void add(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + tail = head; + } else { + Node oldtail = tail; + tail = new Node(); + tail.data = o; + tail.next = null; + tail.previous = oldtail; + oldtail.next = tail; + } + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("数组越界异常"); + } else if (index == size) { + Node oldtail = tail; + tail = new Node(); + tail.data = o; + tail.previous = oldtail; + oldtail.next = tail; + size++; + } else { + + } + size++; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("数组越界异常"); + } + for (int i = 0; i < index; i++) { + head = head.next; + } + return head.data; + } + + public Object remove(int index) { + + return null; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node oldhead = head; + head = new Node(); + head.data = o; + head.next = oldhead; + head.previous = null; + oldhead.previous = head; + size++; + } + + public void addLast(Object o) { + Node oldtail = tail; + tail = new Node(); + tail.data = o; + tail.previous = oldtail; + tail.next = null; + oldtail.next = tail; + size++; + } + + public Object removeFirst() { + Object data = head.data; + head = head.next; + head.previous = null; + size--; + return data; + } + + public Object removeLast() { + Object data = tail.data; + tail = tail.previous; + tail.next = null; + size--; + return data; + } + + public Iterator iterator() { + + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int cursor; + int lastReset = -1; + + @Override + public boolean hasNext() { + return size != cursor; + } + + @Override + public Object next() { + int i = cursor; + if (i > size) { + throw new NoSuchElementException(); + } + Node n = head; + for (int j = 0; j < i; j++) { + n = n.next; + } + cursor = i + 1; + lastReset = i; + return n.data; + } + } +} \ No newline at end of file diff --git a/group04/1751801281/src/com/coding/basic/List.java b/group04/1751801281/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group04/1751801281/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/group04/1751801281/src/com/coding/basic/Queue.java b/group04/1751801281/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..2c513f5b6b --- /dev/null +++ b/group04/1751801281/src/com/coding/basic/Queue.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class Queue { + + private int size; + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + size++; + } + + public Object deQueue() { + size--; + return elementData.removeFirst(); + + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group04/1751801281/src/com/coding/basic/Stack.java b/group04/1751801281/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..28852147e8 --- /dev/null +++ b/group04/1751801281/src/com/coding/basic/Stack.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + private int size; + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + return elementData.remove(--size); + } + + public Object peek() { + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group04/1751801281/test/com/coding/basic/ArrayListTest.java b/group04/1751801281/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..ed7c6549ed --- /dev/null +++ b/group04/1751801281/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void testArrayList(){ + ArrayList list = new ArrayList(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + list.add("5"); + list.add("6"); + list.add("7"); + list.add("8"); + list.add(2,"9"); + list.add("10"); + list.add(3,"11"); + System.out.println(list.get(3)); + System.out.println("======"); + Iterator it = list.iterator(); + while(it.hasNext()){ + System.out.print(it.next()+" "); + } + + } +} diff --git a/group04/1751801281/test/com/coding/basic/LinkedListTeat.java b/group04/1751801281/test/com/coding/basic/LinkedListTeat.java new file mode 100644 index 0000000000..ac1b0c118e --- /dev/null +++ b/group04/1751801281/test/com/coding/basic/LinkedListTeat.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +import org.junit.Test; + +public class LinkedListTeat { + + @Test + public void testLinkedList(){ + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add(1, "3"); + list.addFirst("4"); + list.addLast("5"); + list.removeFirst(); + list.removeLast(); + System.out.println(list.get(0)); + System.out.println(list.get(1)); + System.out.println(list.get(2)); + System.out.println(list.get(1)); + System.out.println("========"); + + Iterator it = list.iterator(); + while(it.hasNext()){ + System.out.print(it.next()+" "); + } + } +} diff --git a/group04/1751801281/test/com/coding/basic/QueueTest.java b/group04/1751801281/test/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..ba2be4262b --- /dev/null +++ b/group04/1751801281/test/com/coding/basic/QueueTest.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +import org.junit.Test; + +public class QueueTest { + + @Test + public void testQueue(){ + Queue q = new Queue(); + q.enQueue("a"); + q.enQueue("b"); + q.enQueue("c"); + q.deQueue(); + System.out.println(q.size()); + } +} diff --git a/group04/1751801281/test/com/coding/basic/StackTest.java b/group04/1751801281/test/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..a133f0ec1f --- /dev/null +++ b/group04/1751801281/test/com/coding/basic/StackTest.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +import org.junit.Test; + +public class StackTest { + + @Test + public void testStack() { + Stack s = new Stack(); + s.push("0"); + s.push("1"); + s.push("2"); + s.push("3"); + s.push("4"); + System.out.println(s.pop()); + System.out.println(s.peek()); + + } +} diff --git a/group04/1796244932/.gitignore b/group04/1796244932/.gitignore new file mode 100644 index 0000000000..b241c62e19 --- /dev/null +++ b/group04/1796244932/.gitignore @@ -0,0 +1,138 @@ +.metadata/ +RemoteSystemsTempFiles/ +.recommenders/ + +*.iml + +# Created by https://www.gitignore.io/api/eclipse,intellij,java + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +*/.idea/ +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +### Java ### +# 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* + +# End of https://www.gitignore.io/api/eclipse,intellij,java diff --git a/group04/1796244932/learn01/.gitignore b/group04/1796244932/learn01/.gitignore new file mode 100644 index 0000000000..83ef0e8099 --- /dev/null +++ b/group04/1796244932/learn01/.gitignore @@ -0,0 +1,6 @@ +.settings/ +/target/ +.classpath +.project +/bin/ + diff --git a/group04/1796244932/learn01/pom.xml b/group04/1796244932/learn01/pom.xml new file mode 100644 index 0000000000..5a00c4d139 --- /dev/null +++ b/group04/1796244932/learn01/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + + com.dudy + learn01 + 0.0.1-SNAPSHOT + jar + + learn01 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.11 + test + + + + + + + + jdk-1.8 + + true + 1.8 + + + 1.8 + 1.8 + 1.8 + + + + diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java new file mode 100644 index 0000000000..5a8e82aaaf --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyArrayList.java @@ -0,0 +1,124 @@ +package com.dudy.learn01.base; + +import java.util.Arrays; + +public class MyArrayList implements MyList { + + private int size = 0; + + private Object[] elementData = new Object[16]; + + /** + * 增加元素: ①数组没满之前,直接添加到最后,满了扩容添加 + */ + public void add(Object o) { + // 检查是否需要扩容 + this.checkGrow(size + 1); + elementData[size++] = o; + } + + /** + * 检查是否需要扩容 + * + * @param newSize + */ + private void checkGrow(int newSize) { + if (newSize > elementData.length) { + this.grow(elementData); + } + } + + /** + * 扩容 + * + * @param oldElementData + */ + private void grow(Object[] oldElementData) { + int lenth = (int) (oldElementData.length * 1.5); + elementData = Arrays.copyOf(oldElementData, lenth); + } + + /** + * 根据索引添加:①同 add ② 可能会出现 index 超出当前位置的情况 ③往 中间插入时需要移位 + */ + public void add(int index, Object o) { + // 检查是否需要扩容 + if (index > size || index < 0) { + throw new RuntimeException("Index: " + index + ", Size: " + size); + } + this.checkGrow(size + 1); + // 循环移位 + int tmp = size; + for (int i = 0; i < size - index; i++) { + elementData[tmp] = elementData[tmp - 1]; + tmp--; + } + // 索引位置赋值 + elementData[index] = o; + size++; + } + + /** + * 直接返回相应数组下标就好 + */ + public Object get(int index) { + return elementData[index]; + } + + /** + * 删除元素:①注意移位 + */ + public Object remove(int index) { + // 检查是否需要扩容 + if (index > size || index < 0) { + throw new RuntimeException("Index: " + index + ", Size: " + size); + } + Object desc = elementData[index]; + + for (int i = 0; i < size - index; i++) { + elementData[index] = elementData[index+1]; + index++; + } + size--; + return desc; + } + + public int size() { + return this.size; + } + + public MyIterator iterator() { + + + return new MyItr(); + } + + public class MyItr implements MyIterator{ + + int cursor; + + + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + + return elementData[cursor++]; + } + } + + + + @Override + public String toString() { + StringBuffer str = new StringBuffer(); + str.append("["); + for (int i = 0; i < size; i++) { + str.append(elementData[i]+","); + } + str.append("]"); + return str.toString(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java new file mode 100644 index 0000000000..cfc06bc0d2 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyBinaryTree.java @@ -0,0 +1,71 @@ +package com.dudy.learn01.base; + +public class MyBinaryTree { + + private Node root; + + public Node getRoot() { + return root; + } + + static class Node { + private int data; + private Node left; + private Node right; + + public Node(int data) { + this.data = data; + } + + } + + public Node insert(int o) { + Node newNode = new Node(o); + + if (root == null) { + root = newNode; + return newNode; + } + Node currentNode = root; + + while (true) { + // System.out.println("currentNode: " + currentNode.data ); + if (o <= currentNode.data) { // left + + if (currentNode.left != null) { + currentNode = currentNode.left; + } else { + currentNode.left = newNode; + // System.out.println("left return ..."); + // System.out.println(""); + return newNode; + } + } else { // right + if (currentNode.right != null) { + currentNode = currentNode.right; + } else { + currentNode.right = newNode; + // System.out.println("right return ..."); + // System.out.println(""); + return newNode; + } + } + + } + + } + + public void display(Node root) { + + System.out.print(root.data + " "); + if (root.left != null) { + display(root.left); + } + if (root.right != null) { + display(root.right); + } + } + + + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java new file mode 100644 index 0000000000..e53306fa03 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyIterator.java @@ -0,0 +1,6 @@ +package com.dudy.learn01.base; +public interface MyIterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java new file mode 100644 index 0000000000..60254997aa --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyLinkedList.java @@ -0,0 +1,132 @@ +package com.dudy.learn01.base; + + +public class MyLinkedList implements MyList { + + private int size = 0; + + private Node head; + + public void add(Object o) { + Node newNode = new Node(o); + newNode.next = head;// next --> head + head = newNode; // head --> newNode + size++; + } + + public void add(int index, Object o) { + checkRange(index); + Node current = getCurrentNode(index); + Node newNode = new Node(o); + newNode.next = current.next;//new next --> next + current.next = newNode; // next -->new + size++; + } + + private Node getCurrentNode(int index) {// 获取当前节点 + Node current = head; + for(int i = 0; i< size-index -1; i++){ + current = current.next; + } + return current; + } + + private void checkRange(int index) { + if(index > size || index < 0){ + throw new RuntimeException("indexOutOfException:" + "Index: " + index + ", Size: " + size); + } + } + + public Object get(int index) { + checkRange(index); + Node node = getCurrentNode(index); + return node.data; + } + + public Object remove(int index) { + if(size < 1){// ①没有元素的情况 + throw new RuntimeException("NoSuchElementException: size= " + size); + } + if(index == 0) return removeFirst(); + if(index == size - 1) return removeLast(); + + Node node = getCurrentNode(index+1); + node.next = node.next.next; + size--; + return node.data; + } + + public int size() { + return size; + } + + public void addLast(Object o) { + add(o); + } + + public void addFirst(Object o) { + Node current = head; + while(current.next != null){// 找到最后一个节点 + current = current.next; + } + Node newNode = new Node(o); + current.next = newNode; + size++; + } + + public Object removeFirst() { + Node currentNode = getCurrentNode(1); + Node tmp = currentNode.next; + currentNode.next = null; + size--; + return tmp ==null? currentNode.data:tmp.data;// 可能只有一个数据 + } + + public Object removeLast() { + Node tmp = head; + head = head.next; + size--; + return tmp.data; + } + + public MyIterator iterator() { + return new MyLinkedListItr(); + } + + + public class MyLinkedListItr implements MyIterator { + + int cursor; + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + Node currentNode = getCurrentNode(cursor++); + return currentNode.data; + } + } + + private static class Node { + Object data; + Node next; + public Node() { + } + public Node(Object data) { + this.data = data; + } + } + + + private void displayLink() {// 自己调试使用 + Node current = head; + while(current != null){ + System.out.print(current.data); + current = current.next; + } + System.out.println(""); + } + + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java new file mode 100644 index 0000000000..455727381d --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyList.java @@ -0,0 +1,8 @@ +package com.dudy.learn01.base; +public interface MyList { + 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(); +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java new file mode 100644 index 0000000000..25b9da93f2 --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyQueue.java @@ -0,0 +1,22 @@ +package com.dudy.learn01.base; +public class MyQueue { + + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0 ; + } + + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java new file mode 100644 index 0000000000..e59c366e0a --- /dev/null +++ b/group04/1796244932/learn01/src/main/java/com/dudy/learn01/base/MyStack.java @@ -0,0 +1,26 @@ +package com.dudy.learn01.base; +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + + Object o = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()); + return o; + } + + public Object peek(){ + + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java new file mode 100644 index 0000000000..5c6edd8fec --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyArrayListTest.java @@ -0,0 +1,59 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Iterator; + +public class MyArrayListTest { + + + + @Test + public void iteraterTest(){ + MyArrayList list = new MyArrayList(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + + + for(MyIterator it = list.iterator(); it.hasNext();){ + System.out.print(it.next() + " "); + } + + + } + + @Test + public void myArrayListTest() { + MyArrayList list = new MyArrayList(); + for (int i = 0; i < 20; i++) { + list.add(i); + } + + list.add(1, "s"); + list.add(21, 21); + System.out.println("--" + list.size()); + System.out.println(list); + + Object remove = list.remove(3); + System.out.println("remove:" + remove); + System.out.println("--" + list.size()); + System.out.println(list); + } + + @Test + public void arrayListTest(){ + + ArrayList list = new ArrayList(); + list.add("1"); + list.add("2"); + + for (Iterator it = list.iterator(); it.hasNext();){ + System.out.println(it.next()); + } + + } + + +} diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java new file mode 100644 index 0000000000..65bee44580 --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyBinaryTreeTest.java @@ -0,0 +1,42 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/21. + */ +public class MyBinaryTreeTest { + + + /** + * 5 + * 2 7 + * 1 4 6 8 + * + */ + @Test + public void insert() throws Exception { + + + MyBinaryTree tree = new MyBinaryTree(); + tree.insert(5); + tree.insert(2); + tree.insert(1); + tree.insert(7); + tree.insert(8); + tree.insert(4); + tree.insert(6); + + + tree.display(tree.getRoot()); + + } + + @Test + public void display() throws Exception { + + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java new file mode 100644 index 0000000000..c6ebb096ec --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyLinkedListTest.java @@ -0,0 +1,88 @@ +package com.dudy.learn01.base; + +import java.util.LinkedList; + +import org.junit.Test; + +public class MyLinkedListTest { + + + + @Test + public void iteraterTest(){ + MyLinkedList list = new MyLinkedList(); + for (int i = 0; i < 10; i++) { + list.add(i); + } + + + for(MyIterator it = list.iterator(); it.hasNext();){ + System.out.print(it.next() + " "); + } + + + } + + @Test + public void removeTest(){ + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + list.add(3); + //list.remove(0); + //list.remove(1); + list.remove(2); + System.out.println("--" + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + + + @Test + public void removeLastTest() { + MyLinkedList list = new MyLinkedList(); + list.add(1); + //list.add(2); + list.removeLast(); + System.out.println("--" + list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + @Test + public void removeFirstTest() { + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + Object first = list.removeFirst(); + System.out.println("--" + list.size() + ",first = " + first); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + @Test + public void baseTest() { + + MyLinkedList list = new MyLinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.addFirst(0); + list.addLast("last"); + list.add(3, "s");// 0 1 2 s 3 last + System.out.println(list.size()); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + Integer first = list.removeFirst(); + } + +} diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java new file mode 100644 index 0000000000..de8d683abe --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyQueueTest.java @@ -0,0 +1,27 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/20. + */ +public class MyQueueTest { + @Test + public void enQueue() throws Exception { + + + MyQueue queue = new MyQueue(); + queue.enQueue("1"); +// queue.enQueue("2"); +// +// queue.enQueue("3"); + + while (queue.size() > 0){ + System.out.println(queue.deQueue()); + } + + } + +} \ No newline at end of file diff --git a/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java new file mode 100644 index 0000000000..51175953b0 --- /dev/null +++ b/group04/1796244932/learn01/src/test/java/com/dudy/learn01/base/MyStackTest.java @@ -0,0 +1,58 @@ +package com.dudy.learn01.base; + +import org.junit.Test; + +import java.util.Stack; + +import static org.junit.Assert.*; + +/** + * Created by dudy on 2017/2/20. + */ +public class MyStackTest { + @Test + public void push() throws Exception { + + MyStack stack = new MyStack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + + Object pop = stack.pop(); + System.out.println( "size :" + stack.size() + "pop:" + pop); + Object peek = stack.peek(); + System.out.println( "size :" + stack.size() + "peek: " + peek); + } + + @Test + public void pop() throws Exception { + + } + + @Test + public void peek() throws Exception { + + } + + @Test + public void isEmpty() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void stackTest(){ + Stack stack = new Stack(); + stack.push("1"); + stack.push("2"); + stack.peek(); + stack.pop(); + System.out.println(stack.size()); + + } + +} \ No newline at end of file diff --git a/group04/1906242834/.classpath b/group04/1906242834/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group04/1906242834/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group04/1906242834/.gitignore b/group04/1906242834/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/1906242834/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/1906242834/.project b/group04/1906242834/.project new file mode 100644 index 0000000000..a30533712f --- /dev/null +++ b/group04/1906242834/.project @@ -0,0 +1,17 @@ + + + 1906242834Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/1906242834/src/com/coding/basic/ArrayList.java b/group04/1906242834/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..a4baff2fae --- /dev/null +++ b/group04/1906242834/src/com/coding/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //若插入元素后会溢出,则对数组进行扩容,在这里只把空间加1 + if ((size()+1)>elementData.length) { + System.arraycopy(elementData, 0, elementData, 0, elementData.length+1); + elementData[size] = o; + }else { + //若插入元素后不出现溢出,则直接添加在末尾 + elementData[size] = o; + } + } + //index在length范围之内正常插入,若index大于length则抛出异常 + public void add(int index, Object o){ + Object temp; + if (index index; i--) { + elementData[i-1] = elementData[i-2]; + } + elementData[index] = o; + }else if (index==elementData.length) { + add(o); + }else{ + System.out.println("ArrayIndexOutOfBoundsException"); + } + } + + public Object get(int index){ + if (index<=elementData.length-1) { + return elementData[index]; + }else { + System.out.println("ArrayIndexOutOfBoundsException"); + } + return elementData; + } + + public Object remove(int index){ + if (index + + + + + diff --git a/group04/1972376180/.gitignore b/group04/1972376180/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/1972376180/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/1972376180/.project b/group04/1972376180/.project new file mode 100644 index 0000000000..53a0e6ab9c --- /dev/null +++ b/group04/1972376180/.project @@ -0,0 +1,17 @@ + + + Coding2017 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/1972376180/src/tong/java/one/MyArrayList.java b/group04/1972376180/src/tong/java/one/MyArrayList.java new file mode 100644 index 0000000000..abb00f61b9 --- /dev/null +++ b/group04/1972376180/src/tong/java/one/MyArrayList.java @@ -0,0 +1,76 @@ +package tong.java.one; + +import java.util.Arrays; + +/** + * 自定义ArrayList + * + * @author tong + * + */ +public class MyArrayList { + private Object[] datas = new Object[10]; + private int size; + + // 默认在集合末尾添加元素 + public void add(Object o) { + if (datas[0] == null) { + datas[0] = o; + } else { + if (size < datas.length) { + datas[size] = o; + } else { + datas = grow(5); + datas[size] = o; + } + } + size++; + } + + // 指定索引处添加元素 + public void add(int index, Object o) { + if (index > size - 1) { + throw new ArrayIndexOutOfBoundsException(); + } else { + if (size + 1 > datas.length) { + datas = grow(5); + } + datas[index] = o; + for (int i = index + 1; i < size - 1; i++) { + datas[i] = datas[i + 1]; + } + size++; + } + } + + // 获取指定索引处的的元素 + public Object get(int index) { + if (index > size - 1) { + throw new ArrayIndexOutOfBoundsException(); + } else { + return datas[index]; + } + } + + // 删除指定索引处的元素 + public Object remove(int index) { + if (index > size - 1) { + throw new ArrayIndexOutOfBoundsException(); + } else { + Object removeData = datas[index]; + for (int i = index; i < size - 1; i++) { + datas[index] = datas[index + 1]; + } + size--; + return removeData; + } + } + + public int size() { + return size; + } + + private Object[] grow(int length) { + return Arrays.copyOf(datas, datas.length + length); + } +} diff --git a/group04/1972376180/src/tong/java/one/MyLinkedList.java b/group04/1972376180/src/tong/java/one/MyLinkedList.java new file mode 100644 index 0000000000..e238642bba --- /dev/null +++ b/group04/1972376180/src/tong/java/one/MyLinkedList.java @@ -0,0 +1,127 @@ +package tong.java.one; + +/** + * 自定义LinkedList + * + * @author 仝闯 + * + */ +public class MyLinkedList { + private Node head; + private int size; + + // 默认在链表的结尾添加元素 + public void add(Object o) { + Node newNode = new Node(o); + if (size == 0) { + head = newNode; + } else if (size == 1) { + Node oldHead = head; + head = newNode; + head.next = oldHead; + } else { + getNode(size - 1).next = newNode; + } + size++; + } + + // 在指定索引出添加元素 + public void add(int index, Object o) { + Node newNode = new Node(o); + if (size == 0) { + head = newNode; + } else if (size == 1) { + Node oldHead = head; + head = newNode; + head.next = oldHead; + } else { + if (index == 0) { + Node oldHead = head; + head = newNode; + head.next = oldHead; + } else if (index == size - 1) { + getNode(size - 1).next = newNode; + } else { + for (int i = 1; i < index; i++) { + getNode(index - 1).next = newNode; + newNode.next = getNode(index); + } + } + } + size++; + } + + // 添加元素到首位 + public void addFirst(Object o) { + Node oldHead = head; + head = new Node(o); + head.next = oldHead; + size++; + } + + // 获取指定索引处的元素 + public Object get(int index) { + return getNode(index).data; + } + + private Node getNode(int index) { + Node x = head; + if (index == 0) { + return head; + } else { + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } + + } + + // 删除指定索引处的元素 + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new RuntimeException(); + } else { + if (0 < index && index < size - 1) { + getNode(index - 1).next = getNode(index + 1); + size--; + return getNode(index); + } else if (index == 0) { + Node removeNode = head; + removeNode.next = null; + head = getNode(1); + size--; + return removeNode; + } else { + getNode(index - 1).next = null; + size--; + return getNode(index); + } + } + + } + + // 删除首位元素 + public Object removeFirst() { + return remove(0); + } + + // 删除末位元素 + public Object removeLast() { + return remove(size - 1); + } + + public int size() { + return size; + } + + class Node { + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + } + +} diff --git a/group04/1972376180/src/tong/java/one/MyQueue.java b/group04/1972376180/src/tong/java/one/MyQueue.java new file mode 100644 index 0000000000..3ad96a0854 --- /dev/null +++ b/group04/1972376180/src/tong/java/one/MyQueue.java @@ -0,0 +1,34 @@ +package tong.java.one; +/** + * 自定义队列 + * @author tong + * + */ +public class MyQueue { + private MyLinkedList datas = new MyLinkedList(); + private int size; + + + public void enqueue(Object o) { + datas.add(o); + size++; + } + + public Object dequeue() { + Object firstData = datas.removeFirst(); + size--; + return firstData; + } + + public boolean isEmpty() { + if (size == 0) { + return true; + } else { + return false; + } + } + + public int size() { + return size; + } +} diff --git a/group04/1972376180/src/tong/java/one/MyStack.java b/group04/1972376180/src/tong/java/one/MyStack.java new file mode 100644 index 0000000000..e01e92f581 --- /dev/null +++ b/group04/1972376180/src/tong/java/one/MyStack.java @@ -0,0 +1,38 @@ +package tong.java.one; +/** + * 自定义栈 + * @author tong + * + */ +public class MyStack { + private MyArrayList datas = new MyArrayList(); + private int size; + //入栈 + public void push(Object o){ + datas.add(o); + size++; + } + //出栈 + public Object pop(){ + Object topData = datas.remove(size-1); + size--; + return topData; + } + + public Object peek(){ + return datas.get(size-1); + } + + public boolean isEmpty(){ + if(size==0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return size; + } + +} diff --git a/group04/2082945465/.gitignore b/group04/2082945465/.gitignore new file mode 100644 index 0000000000..3f016d5a8c --- /dev/null +++ b/group04/2082945465/.gitignore @@ -0,0 +1,138 @@ +.metadata/ +RemoteSystemsTempFiles/ +.recommenders/ + +*.iml + +# Created by https://www.gitignore.io/api/eclipse,intellij,java + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +*/.idea/ +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +*.iml +modules.xml +.idea/misc.xml +# *.ipr + +### Java ### +# 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* + +# End of https://www.gitignore.io/api/eclipse,intellij,java diff --git a/group04/2082945465/week01/src/Hello.java b/group04/2082945465/week01/src/Hello.java new file mode 100644 index 0000000000..ac6e67463e --- /dev/null +++ b/group04/2082945465/week01/src/Hello.java @@ -0,0 +1,7 @@ +package src; + +public class Hello { + public static void main(String[] args) { + System.out.println("Hello! World!"); + } +} diff --git a/group04/2082945465/week01/src/MyArrayList.java b/group04/2082945465/week01/src/MyArrayList.java new file mode 100644 index 0000000000..798475771c --- /dev/null +++ b/group04/2082945465/week01/src/MyArrayList.java @@ -0,0 +1,79 @@ +package src; + +import java.util.Arrays; + +/** + * Created by Yang on 2/25/2017. + */ +public class MyArrayList implements MyList { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + @Override + public void add(Object obj) { + this.checkCapacity(size+1); + elementData[size++] = obj; + } + + @Override + public void add(int index, Object obj) { + this.validIndex(index); + this.checkCapacity(size+1); + if(index < size){ + for(int i = size; i > index; i--){ + this.elementData[i] = this.elementData[i-1]; + } + }else{ + this.elementData[index] = obj; + } + this.size++; + } + + @Override + public Object get(int index) { + this.validIndex(index); + return this.elementData[index]; + } + + @Override + public Object remove(int index) { + this.validIndex(index); + Object o = this.elementData[index]; + for(int i = index; i < this.size-1; i++){ + this.elementData[i] = this.elementData[i+1]; + } + this.size--; + return o; + } + + @Override + public int size() { + return this.size; + } + + private void checkCapacity(int newSize) { + if(newSize > elementData.length){ + this.extend(elementData); + } + } + + private void extend(Object[] oldElementData) { + int newLength = (int) (oldElementData.length * 1.5); + elementData = Arrays.copyOf(oldElementData, newLength); + } + + private void validIndex(int inputIndex) { + if(inputIndex > size || inputIndex < 0){ + throw new RuntimeException("Index: " + inputIndex + " out of bounds( " + size +" )"); + } + } + + public boolean isEmpty() { + if (size() == 0){ + return false; + } + return true; + } +} diff --git a/group04/2082945465/week01/src/MyIterator.java b/group04/2082945465/week01/src/MyIterator.java new file mode 100644 index 0000000000..245fa82be8 --- /dev/null +++ b/group04/2082945465/week01/src/MyIterator.java @@ -0,0 +1,9 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public interface MyIterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group04/2082945465/week01/src/MyLinkedList.java b/group04/2082945465/week01/src/MyLinkedList.java new file mode 100644 index 0000000000..69f518ba0f --- /dev/null +++ b/group04/2082945465/week01/src/MyLinkedList.java @@ -0,0 +1,111 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public class MyLinkedList implements MyList { + + private int size = 0; + + private Node header; + + @Override + public void add(Object obj) { + Node newNode = new Node(obj); + newNode.next = header; + header = newNode; + size++; + } + + @Override + public void add(int index, Object obj) { + this.validIndex(index); + Node current = this.getCurrentNode(index); + Node newNode = new Node(obj); + newNode.next = current.next; + current.next = newNode; + size++; + } + + @Override + public Object get(int index) { + this.validIndex(index); + Node node = getCurrentNode(index); + return node.data; + } + + @Override + public Object remove(int index) { + this.validIndex(index); + + if(index == 0) return removeFirst(); + if(index == size -1) return removeLast(); + + Node node = getCurrentNode(index+1); + node.next = node.next.next; + size--; + return node.data; + } + + @Override + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + public Node(){ + } + public Node(Object data) { + this.data = data; + } + } + + private void validIndex(int inputIndex) { + if(inputIndex > size || inputIndex < 0){ + throw new RuntimeException("Index: " + inputIndex + " out of bounds( " + size +" )"); + } + } + + private Node getCurrentNode(int index) { + Node current = header; + for(int i = 0; i < size-index -1; i++){ + current = current.next; + } + return current; + } + + public Object removeFirst() { + Node temp = new Node(); + temp.next = header; + temp.next = header.next; + return temp.next; + } + + private Object removeLast() { + Node preNode = new Node(); + while (preNode.next.next != null) { + preNode = preNode.next; + } + Node lastNode = preNode.next.next; + preNode.next = null; + return lastNode; + } + + public void addLast(Object o){ + Node preNode = new Node(); + while (preNode.next != null){ + preNode = preNode.next; + } + Node lastNode = new Node(o); + preNode.next = lastNode; + } + + public boolean isEmpty() { + if (size() == 0){ + return false; + } + return true; + } +} diff --git a/group04/2082945465/week01/src/MyList.java b/group04/2082945465/week01/src/MyList.java new file mode 100644 index 0000000000..578c86b803 --- /dev/null +++ b/group04/2082945465/week01/src/MyList.java @@ -0,0 +1,12 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public interface MyList { + public void add(Object obj); + public void add(int index, Object obj); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group04/2082945465/week01/src/MyQueue.java b/group04/2082945465/week01/src/MyQueue.java new file mode 100644 index 0000000000..87dc900c79 --- /dev/null +++ b/group04/2082945465/week01/src/MyQueue.java @@ -0,0 +1,20 @@ +package src; + +/** + * Created by Yang on 2/25/2017. + */ +public class MyQueue { + private MyLinkedList queue = new MyLinkedList(); + + public void enQuee(Object obj){ + queue.addLast(obj); + } + + public Object deQuee(Object obj){ + return queue.removeFirst(); + } + + public boolean isEmpty() { + return queue.isEmpty(); + } +} diff --git a/group04/2082945465/week01/src/MyStack.java b/group04/2082945465/week01/src/MyStack.java new file mode 100644 index 0000000000..3427d86e93 --- /dev/null +++ b/group04/2082945465/week01/src/MyStack.java @@ -0,0 +1,26 @@ +package src; + +import java.util.EmptyStackException; + +/** + * Created by Yang on 2/25/2017. + */ +public class MyStack { + + private MyArrayList stack = new MyArrayList(); + + public void push(Object obj){ + stack.add(obj); + } + + public Object pop(){ + if (stack.size()==0){ + throw new EmptyStackException(); + } + return stack.remove(stack.size()-1); + } + + public boolean isEmpty(){ + return stack.isEmpty(); + } +} diff --git a/group04/24658892/.classpath b/group04/24658892/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group04/24658892/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group04/24658892/.project b/group04/24658892/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group04/24658892/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/24658892/.settings/org.eclipse.jdt.core.prefs b/group04/24658892/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group04/24658892/.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/group04/24658892/learnjava/build.gradle b/group04/24658892/learnjava/build.gradle new file mode 100644 index 0000000000..ffde26b681 --- /dev/null +++ b/group04/24658892/learnjava/build.gradle @@ -0,0 +1,14 @@ +group 'com.learnjava' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.5 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/group04/24658892/learnjava/gradle/wrapper/gradle-wrapper.properties b/group04/24658892/learnjava/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..7d38f8e3be --- /dev/null +++ b/group04/24658892/learnjava/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sat Feb 25 00:50:00 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip diff --git a/group04/24658892/learnjava/gradlew b/group04/24658892/learnjava/gradlew new file mode 100755 index 0000000000..9aa616c273 --- /dev/null +++ b/group04/24658892/learnjava/gradlew @@ -0,0 +1,169 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/group04/24658892/learnjava/gradlew.bat b/group04/24658892/learnjava/gradlew.bat new file mode 100644 index 0000000000..f9553162f1 --- /dev/null +++ b/group04/24658892/learnjava/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/group04/24658892/learnjava/settings.gradle b/group04/24658892/learnjava/settings.gradle new file mode 100644 index 0000000000..5c0663e14b --- /dev/null +++ b/group04/24658892/learnjava/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'learnjava' + diff --git a/group04/24658892/learnjava/src/main/java/com/coding/basic/ArrayList.java b/group04/24658892/learnjava/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..afd9f2a3c8 --- /dev/null +++ b/group04/24658892/learnjava/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,77 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + int len = size + 1; + if (len == elementData.length) { + expandArray(); + } + elementData[size++] = o; + } + + public void add(int index, Object o) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + int remain = elementData.length - size; + if (remain == 0 || index == elementData.length) { + expandArray(); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + private void expandArray() { + int len = elementData.length; + Object[] temp = new Object[len * 2]; + System.arraycopy(elementData, 0, temp, 0, len); + elementData = temp; + } + + public Object get(int index) { + if (index < 0) { + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Object removed = elementData[index]; + int len = size - index - 1; + if (len > 0) { + System.arraycopy(elementData, index + 1, elementData, index, len); + } + elementData[--size] = null; + return removed; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int cursor; + + public boolean hasNext() { + return cursor + 1 < size; + } + + public Object next() { + return elementData[++cursor]; + } + } +} diff --git a/group04/24658892/learnjava/src/main/java/com/coding/basic/BinaryTreeNode.java b/group04/24658892/learnjava/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group04/24658892/learnjava/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/group04/24658892/learnjava/src/main/java/com/coding/basic/Iterator.java b/group04/24658892/learnjava/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/24658892/learnjava/src/main/java/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/group04/24658892/learnjava/src/main/java/com/coding/basic/LinkedList.java b/group04/24658892/learnjava/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group04/24658892/learnjava/src/main/java/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/group04/24658892/learnjava/src/main/java/com/coding/basic/List.java b/group04/24658892/learnjava/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group04/24658892/learnjava/src/main/java/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/group04/24658892/learnjava/src/main/java/com/coding/basic/Queue.java b/group04/24658892/learnjava/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4c87d9cb16 --- /dev/null +++ b/group04/24658892/learnjava/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void enQueue(Object o) { + elementData.add(o); + size++; + } + + public Object deQueue() { + return elementData.remove(0); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group04/24658892/learnjava/src/main/java/com/coding/basic/Stack.java b/group04/24658892/learnjava/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4c6a362da2 --- /dev/null +++ b/group04/24658892/learnjava/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(o); + size++; + } + + public Object pop() { + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.remove(--size); + } + + public Object peek() { + if (this.isEmpty()) { + throw new EmptyStackException(); + } + return elementData.get(size - 1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group04/24658892/learnjava/src/test/java/com/coding/basic/ArrayListTest.java b/group04/24658892/learnjava/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..73fce0f7f8 --- /dev/null +++ b/group04/24658892/learnjava/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,114 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void add() throws Exception { + ArrayList data; + int size; + //size < 100 + data = new ArrayList(); + size = 33; + for (int i = 0; i < size; i++) { + data.add(i); + } + for (int i = 0; i < size; i++) { + Assert.assertEquals(i, data.get(i)); + } + //size > 100 + data = new ArrayList(); + size = 333; + for (int i = 0; i < size; i++) { + data.add(i); + } + for (int i = 0; i < size; i++) { + Assert.assertEquals(i, data.get(i)); + } + //size = 100 + data = new ArrayList(); + size = 100; + for (int i = 0; i < size; i++) { + data.add(i); + } + for (int i = 0; i < size; i++) { + Assert.assertEquals(i, data.get(i)); + } + } + + @Test + public void add1() throws Exception { + ArrayList data; + int size; + int index; + boolean b; + // size < 100; + data = new ArrayList(); + size = 5; + index = 2; + b = false; + for (int i = 0; i < size; i++) { + data.add(i); + } + data.add(index, index + 10000); + for (int i = 0; i < data.size(); i++) { + if (i == index) { + b = true; + Assert.assertEquals(index + 10000, data.get(i)); + } + else { + if (b) { + Assert.assertEquals(i - 1, data.get(i)); + } + else { + Assert.assertEquals(i, data.get(i)); + } + } + } + } + + @Test + public void get() throws Exception { + ArrayList data = new ArrayList(); + data.add(1); + data.add(2); + data.add(3); + Assert.assertEquals(2, data.get(1)); + } + + @Test + public void remove() throws Exception { + ArrayList data = new ArrayList(); + data.add(1); + data.add(2); + data.add(3); + data.remove(1); + Assert.assertEquals(2, data.size()); + Assert.assertEquals(1, data.get(0)); + Assert.assertEquals(3, data.get(1)); + } + + @Test + public void size() throws Exception { + ArrayList data = new ArrayList(); + data.add(1); + data.add(1); + data.add(1); + Assert.assertEquals(3, data.size()); + } + + @Test + public void iterator() throws Exception { + ArrayList data = new ArrayList(); + data.add(0); + data.add(1); + data.add(2); + Iterator iterator = data.iterator(); + int i = 0; + while (iterator.hasNext()) { + Assert.assertEquals(++i, iterator.next()); + } + } +} \ No newline at end of file diff --git a/group04/24658892/learnjava/src/test/java/com/coding/basic/LinkedListTest.java b/group04/24658892/learnjava/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..6acbb70d37 --- /dev/null +++ b/group04/24658892/learnjava/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,57 @@ +package com.coding.basic; + +import org.junit.Test; + +public class LinkedListTest { + + @Test + public void add() throws Exception { + + } + + @Test + public void add1() throws Exception { + + } + + @Test + public void get() throws Exception { + + } + + @Test + public void remove() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void addFirst() throws Exception { + + } + + @Test + public void addLast() throws Exception { + + } + + @Test + public void removeFirst() throws Exception { + + } + + @Test + public void removeLast() throws Exception { + + } + + @Test + public void iterator() throws Exception { + + } + +} \ No newline at end of file diff --git a/group04/274407594/226/.classpath b/group04/274407594/226/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group04/274407594/226/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group04/274407594/226/.gitignore b/group04/274407594/226/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group04/274407594/226/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group04/274407594/226/.project b/group04/274407594/226/.project new file mode 100644 index 0000000000..1f70b883ee --- /dev/null +++ b/group04/274407594/226/.project @@ -0,0 +1,17 @@ + + + 226 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs b/group04/274407594/226/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group04/274407594/226/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git "a/group04/312816708/blog/blog01/CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group04/312816708/blog/blog01/CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000..68542a9e7b --- /dev/null +++ "b/group04/312816708/blog/blog01/CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\343\200\201\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,29 @@ +# CPU、内存、硬盘、指令之间的关系 + +## 1、简介 + +### 1.1 CPU +> CPU,中央处理器,是一个超大规模的集成电路,是一台计算机的运算核心和控制核心。它的主要功能是解释计算机指令以及处理计算机软件中的数据。 + +### 1.2 内存 +> 内存是计算机中重要的部件之一,它是与CPU沟通的桥梁。计算机中所有的程序都是在内存中运行的。其作用是用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换数据。 + +### 1.3 硬盘 +> 硬盘是计算机主要的存储媒介之一。硬盘分为固态硬盘(SSD 盘,新式硬盘)、机械硬盘(HDD 硬盘)、混合硬盘(HHD 一块基于传统机械硬盘诞生出来的新硬盘)。 + +### 1.4 指令 +> 指挥计算机工作的指示命令。程序是一系列按一定顺序排列的,执行程序的过程就是计算机的工作过程。 + +## 2、关系 +计算机在运行时,**CPU** 从**内存** 中获取第一条 **指令** ,然后内存再从硬盘中读取所需的数据。然后CPU再取出第二条指令,一直到指令结束。 + +## 3、参考资料 +[中央处理器 —— 百度百科](http://baike.baidu.com/item/%E4%B8%AD%E5%A4%AE%E5%A4%84%E7%90%86%E5%99%A8/284033?sefr=cr&fromtitle=CPU&fromid=120556) + +[内存 —— 百度百科](http://baike.baidu.com/item/%E5%86%85%E5%AD%98?sefr=enterbtn) + +[硬盘 —— 百度百科](http://baike.baidu.com/item/%E7%A1%AC%E7%9B%98?sefr=enterbtn) + +[计算机指令 —— 百度百科](http://baike.baidu.com/item/%E8%AE%A1%E7%AE%97%E6%9C%BA%E6%8C%87%E4%BB%A4) + + diff --git a/group04/312816708/coding/coding01/.gitignore b/group04/312816708/coding/coding01/.gitignore new file mode 100644 index 0000000000..22cb2e1fb7 --- /dev/null +++ b/group04/312816708/coding/coding01/.gitignore @@ -0,0 +1,7 @@ +# IntelliJ IDEA + +target/ +*.iml +*.ipr +*.iws +.idea/ \ No newline at end of file diff --git a/group04/312816708/coding/coding01/pom.xml b/group04/312816708/coding/coding01/pom.xml new file mode 100644 index 0000000000..9f29c935ae --- /dev/null +++ b/group04/312816708/coding/coding01/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.kevin + coding01 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyArrayList.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyArrayList.java new file mode 100644 index 0000000000..5e4c1bdd2e --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyArrayList.java @@ -0,0 +1,117 @@ +package com.kevin.coding01.basic; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Created by YinWenBing on 2017/2/25. + */ +public class MyArrayList implements MyList { + private int size = 0; + private Object[] elementData = new Object[10]; + + /** + * 添加 + * 判断数组空间是否足够,不够则扩容,将元素放在数组末尾 + * + * @param e + */ + public void add(E e) { + isGrow(size + 1);//判断是否需要扩容 + elementData[size++] = e; + } + + /** + * 是否需要扩容 + * + * @param size + */ + private void isGrow(int size) { + if (size > elementData.length) { + grow(size); + } + } + + /** + * 扩容 + * + * @param minCapacity + */ + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1);//>>将oldCapacity向右移一位,右移一位代表除2,右移n位代表除以2的n次方。左移则是乘 + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } else if (newCapacity - (Integer.MAX_VALUE - 8) > 0) { + newCapacity = hugeCapacity(minCapacity); + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + + private int hugeCapacity(int minCapacity) { + if (minCapacity < 0) { + throw new OutOfMemoryError(); + } + return (minCapacity > (Integer.MAX_VALUE)) ? Integer.MAX_VALUE : Integer.MAX_VALUE - 8; + } + + /** + * 添加指定索引的元素 + * 判断索引是否小于0或大于size + * + * @param index + * @param e + */ + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + isGrow(index); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = e; + size++; + } + + /** + * 根据索引获取数组中的元素 + * + * @param index + * @return + */ + public E get(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + return (E) elementData[index]; + } + + /** + * 根据索引移除数组中的元素,如果移除中间的元素,后面的元素要往前移 + * + * @param index + * @return + */ + public E remove(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + E oldValue = (E) elementData[index]; + + int numMoved = size - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null; + size--; + } + return oldValue; + } + + /** + * 获取数组中存放值得数量 + * + * @return + */ + public int size() { + return size; + } +} diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyLinkedList.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyLinkedList.java new file mode 100644 index 0000000000..4aa55cb9b9 --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyLinkedList.java @@ -0,0 +1,135 @@ +package com.kevin.coding01.basic; + +/** + * Created by YinWenBing on 2017/2/25. + */ +public class MyLinkedList implements MyList { + private Node first;//头节点 + private int size = 0;//默认大小为0 + + public void add(E e) { + if (size == 0) { + first = new Node(); + first.element = e; + size++; + } else { + Node head = first; + for (int i = 0; i < size - 1; i++) { + head = head.next; + } + + Node add = new Node(); + add.element = e; + head.next = add; + size++; + } + } + + + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); + } + Node prev = getNode(index - 1);//当前索引指向的节点的上一节点 + Node next = getNode(index);//当前索引指向的节点成为添加节点的next + Node add = new Node(); + add.element = e; + prev.next = add; + add.next = next; + size++; + } + + private Node getNode(int index) { + Node node = first; + + for (int i = 0; i < index; i++) { + node = first.next; + } + + return node; + } + + public E get(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); + } + + if (size == 0) { + return null; + } + + return getNode(index).element; + } + + public E remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("index:" + index + ";size:" + size); + } + + Node prev = getNode(index - 1); + Node next = getNode(index + 1); + + prev.next = next; + return getNode(index).element; + } + + public int size() { + return size; + } + + public void addFirst(E e) { + if (size == 0) { + first = new Node(); + first.element = e; + size++; + } else { + Node add = new Node(); + add.element = e; + add.next = first; + first = add; + size++; + } + } + + public void addLast(E e) { + Node oldLast = getNode(size - 1); + Node add = new Node(); + add.element = e; + oldLast.next = add; + size++; + } + + public E removeFirst() { + Node oldFirst = first; + if (first.next != null) { + first = first.next; + size--; + return (E) oldFirst.element; + } else {//只有一个节点或者一个节点也没有 + first = null; + return null; + } + } + + public E removeLast() { + Node last = getNode(size - 1); + if (last != null) { + E element = (E) last.element; + Node newLast = getNode(size - 2); + newLast.next = null; + size--; + return element; + } else { //一个节点都不存在 + return null; + } + } + + /** + * 静态内部类 + */ + private static class Node { + E element;//节点数据 + Node next;//下一节点 + } + +} diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyList.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyList.java new file mode 100644 index 0000000000..15758bd837 --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyList.java @@ -0,0 +1,17 @@ +package com.kevin.coding01.basic; + +/** + * Created by YinWenBing on 2017/2/25. + */ +public interface MyList { + + void add(E e); + + void add(int index, E e); + + E get(int index); + + E remove(int index); + + int size(); +} diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyQueue.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyQueue.java new file mode 100644 index 0000000000..feff484d76 --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyQueue.java @@ -0,0 +1,35 @@ +package com.kevin.coding01.basic; + +/** + * 队列:先进先出 + * Created by YinWenBing on 2017/2/25. + */ +public class MyQueue { + private MyLinkedList elementDate = new MyLinkedList(); + + //入队列 + public void enQueue(E e) { + elementDate.addLast(e); + } + + //出队列 + public E deQueue() { + return elementDate.removeFirst(); + } + + public boolean isEmpty() { + return elementDate.size() == 0 ? true : false; + } + + public int size() { + return elementDate.size(); + } + + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + queue.enQueue(1); + queue.enQueue(2); + + System.out.println(queue.deQueue());//1 + } +} diff --git a/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyStack.java b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyStack.java new file mode 100644 index 0000000000..10d115d052 --- /dev/null +++ b/group04/312816708/coding/coding01/src/main/java/com/kevin/coding01/basic/MyStack.java @@ -0,0 +1,67 @@ +package com.kevin.coding01.basic; + +/** + * 栈 先进后出 + * Created by YinWenBing on 2017/2/25. + */ +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + /** + * 往栈中添加元素 + * + * @param e + */ + public void push(E e) { + elementData.add(e); + } + + /** + * 删除栈顶元素 + * + * @return + */ + public E pop() { + return elementData.remove(elementData.size() - 1); + } + + /** + * 返回栈顶元素 + * + * @return + */ + public E peek() { + return elementData.get(elementData.size() - 1); + } + + /** + * 是否为空 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + /** + * 返回栈中元素的数量 + * + * @return + */ + public int size() { + return elementData.size(); + } + + + public static void main(String[] args) { + MyStack myStack = new MyStack(); + myStack.push("A"); + myStack.push("B"); + myStack.push("C"); + + System.out.println(myStack.peek()); + System.out.println(myStack.pop()); + System.out.println(myStack.peek()); + System.out.println(myStack.size()); + } +} diff --git "a/group04/349184132/post/CPU\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.docx" "b/group04/349184132/post/CPU\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.docx" new file mode 100644 index 0000000000..72f97352ac Binary files /dev/null and "b/group04/349184132/post/CPU\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.docx" differ diff --git a/group04/349184132/src/Collection/Onehomework/ArrayList.java b/group04/349184132/src/Collection/Onehomework/ArrayList.java new file mode 100644 index 0000000000..cf1aa4f58e --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/ArrayList.java @@ -0,0 +1,101 @@ +package Collection.Onehomework; + +import java.util.Date; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData ; + + public ArrayList(){ this(10); } + public ArrayList(int capacity){ + if(capacity<0) + throw new IllegalArgumentException(); + elementData = new Object[capacity]; + } + public void add(Object o){ + if(size==elementData.length) + ResizeCapacity(); + elementData[size++] = o; + } + private void ResizeCapacity(){ + Object[] newDatas = new Object[size*2+1]; + System.arraycopy(elementData,0,newDatas,0,size); + elementData = newDatas; + } + private void rangeCheck(int index){ + if(index<0 || index > size-1) + throw new IllegalArgumentException(); + } + public void add(int index, Object o){ + rangeCheck(index); + System.arraycopy(elementData,index,elementData,index+1,size-index); + elementData[index] = o; + size++; + } + + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object oldElement = elementData[index]; + System.arraycopy(elementData,index+1,elementData,index,size-index-1); + size--; + return oldElement; + } + + public int size(){ + return size; + } + public boolean isEmpty(){ return size==0; } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int pos = 0; + @Override + public boolean hasNext() { + return possize) + throw new IllegalArgumentException(); + return elementData[pos++]; + } + + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(1); + list.add("str"); + list.add(new Date()); + + + + Iterator iter = list.iterator(); + while(iter.hasNext()){ + System.out.println(iter.next()); + } + list.add(2,123); + list.add(0,15); + Iterator iter2 = list.iterator(); + while(iter2.hasNext()){ + System.out.println(iter2.next()); + } + + + + } + + +} diff --git a/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java b/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java new file mode 100644 index 0000000000..d57fee396f --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/BinaryTreeNode.java @@ -0,0 +1,79 @@ +package Collection.Onehomework; + + + + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + private BinaryTreeNode root; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode newNode =null; + if(o==null) + throw new NullPointerException("数据不能为空"); + + if(root==null){ + root = new BinaryTreeNode(); + root.setData(o); + }else { + newNode = new BinaryTreeNode(); + BinaryTreeNode nowNode = root; + int val = (int)root.getData(); + nowNode.setData(o); + while(true) { + + if ((int)newNode.getData()< val) { + if(nowNode.left==null){ + nowNode.setLeft(newNode); + break; + } else { + nowNode = nowNode.left; + } + } else if((int)newNode.getData()> val){ + if (nowNode.right==null ) { + nowNode.setRight(newNode); + break; + } else{ + nowNode = newNode.right; + + } + }else { + + throw new IllegalArgumentException("已存在元素结点"); + } + } + } + return newNode; + } + + public static void main(String[] args) { + + } + + + +} diff --git a/group04/349184132/src/Collection/Onehomework/Iterator.java b/group04/349184132/src/Collection/Onehomework/Iterator.java new file mode 100644 index 0000000000..e7c8a4f6d2 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/Iterator.java @@ -0,0 +1,7 @@ +package Collection.Onehomework; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group04/349184132/src/Collection/Onehomework/LinkedList.java b/group04/349184132/src/Collection/Onehomework/LinkedList.java new file mode 100644 index 0000000000..e2cb4ca4b1 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/LinkedList.java @@ -0,0 +1,170 @@ +package Collection.Onehomework; + +public class LinkedList implements List { + + private Node head; + private Node now ; + private int size = 0; + public void add(Object o){ + if(head == null) { + head = new Node(o, null); + now = head; + } + else{ + + Node node = new Node(o,null); + now.next = node; + now = node; + } + size++; + + } + private void rangeCheck(int index) { + if (index < 0 || index > size - 1) + throw new IllegalArgumentException(); + } + public void add(int index , Object o){ + rangeCheck(index); + + if(index==0){ + addFirst(o); + }else { + Node node = new Node(o,null); + Node now = head; + Node next = head; + for (int i = 1; i <= index; i++) { + next = next.next; + if (i == index) { + node.next = next; + now.next = node; + } + now = now.next; + } + size++; + } + } + + public Object get(int index){ + Node indexNode = head; + if(index==0) + return indexNode.data; + else { + + for (int i = 1; i <= index; i++) { + indexNode = indexNode.next; + if (i == index) + return indexNode.data; + } + } + return null; + } + public Object remove(int index){ + rangeCheck(index); + + if(index == 0){ + return removeFirst(); + }else { + Node pre = head; + Node now = head; + for (int i = 1; i <= index; i++) { + now = now.next; + if (i == index) { + pre.next = now.next; + } + pre = pre.next; + } + size--; + return now.data; + } + } + + public int size(){ + return size ; + } + + public boolean isEmpty(){ return size==0; } + + public void addFirst(Object o){ + Node oldhead = head; + Node newhead = new Node(o,oldhead); + head = newhead; + size++; + } + public void addLast(Object o){ + Node node = head; + while(node !=null){ + node = node.next; + if(node==null){ + Node lastnode = new Node(o,null); + node.next = lastnode; + } + } + size++; + } + public Object removeFirst(){ + Node oldhead = head; + Node newhead = head.next; + oldhead.next = null; + head = newhead; + size--; + return oldhead.data; + } + public Object removeLast(){ + Node node = head; + Node prev = head; + while(node !=null){ + node = node.next; + if(node==null){ + prev.next = null; + } + prev = prev.next; + } + size--; + return node.data; + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator{ + int pos = 0; + @Override + public boolean hasNext() { + return possize) + throw new IllegalArgumentException(); + return get(pos++); + } + } + private static class Node{ + Object data; + Node next; + private Node(Object data,Node next){ + this.data = data; + this.next = next; + + } + + + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.add(0,15); + list.add(1,14); + list.removeLast(); + list.addFirst(1); + list.removeFirst(); + Iterator iter = list.iterator(); + while(iter.hasNext()){ + System.out.println(iter.next()); + } + } +} diff --git a/group04/349184132/src/Collection/Onehomework/List.java b/group04/349184132/src/Collection/Onehomework/List.java new file mode 100644 index 0000000000..015dd06102 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/List.java @@ -0,0 +1,9 @@ +package Collection.Onehomework; + +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/group04/349184132/src/Collection/Onehomework/Queue.java b/group04/349184132/src/Collection/Onehomework/Queue.java new file mode 100644 index 0000000000..341adeeec6 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/Queue.java @@ -0,0 +1,29 @@ +package Collection.Onehomework; + +public class Queue { + private LinkedList elementData = new LinkedList(); + private int size = 0; + public void enQueue(Object o){ + elementData.addLast(o); + size++; + } + + public Object deQueue(){ + if(isEmpty()) + try { + throw new IllegalAccessException(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + size--; + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return size==0; + } + + public int size(){ + return size; + } +} diff --git a/group04/349184132/src/Collection/Onehomework/Stack.java b/group04/349184132/src/Collection/Onehomework/Stack.java new file mode 100644 index 0000000000..a6a488cb05 --- /dev/null +++ b/group04/349184132/src/Collection/Onehomework/Stack.java @@ -0,0 +1,34 @@ +package Collection.Onehomework; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + public void push(Object o){ + elementData.add(o); + size++; + } + + + public Object pop(){ + if(isEmpty()){ + try { + throw new IllegalAccessException(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return elementData.get(--size); + } + + public Object peek(){ + return elementData.get(size-1); + } + + public boolean isEmpty(){ + return size==0; + } + + public int size(){ + return size; + } +} diff --git a/group04/351121278/351121278/src/com/coding/basic/ArrayList.java b/group04/351121278/351121278/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..fd80c89662 --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //容量检查 + checkCapacity(size + 1); + elementData[size++] = o; + + } + + //对elementData数组进行容量检查 + private void checkCapacity(int minSize) { + //取得当前数组的长度 + int elementDataLength = elementData.length; + //如果最小长度大于当前数组的长度,则进行扩容 + if (minSize > elementDataLength) { + //ArrayList类扩容的长度是原来数组长度的1.5倍+1,此处参考ArrayList的长度进行扩容 + int newSize = (elementDataLength * 3) / 2 + 1; + //如果扩张后的长度还是比最小需要的长度小,则取需要的长度 + if (newSize < minSize) { + newSize = minSize; + } + //进行数据的拷贝 + elementData = Arrays.copyOf(elementData, newSize); + } + } + + public void add(int index, Object o){ + //对容量进行检查 + checkCapacity(size + 1); + //对数组进行复制,将指定索引位置空出 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + if (index > size) { + throw new IllegalArgumentException("参数不对"); + } + //size - index + Object o = elementData[index]; + int moverNum = size - index - 1; + if (moverNum > 0) { + System.arraycopy(elementData, index+1, elementData, index, moverNum); + } + //将数组最后一个元素置为null + elementData[--size] = null; + return o; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + +} + diff --git a/group04/351121278/351121278/src/com/coding/basic/Iterator.java b/group04/351121278/351121278/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..095e38e85b --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + + +public interface Iterator { + boolean hasNext(); + + Object next(); + +} \ No newline at end of file diff --git a/group04/351121278/351121278/src/com/coding/basic/LinkedList.java b/group04/351121278/351121278/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..5428802926 --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/LinkedList.java @@ -0,0 +1,79 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + + for (int i = 0; i <= index; i++) { + head = head.next; + } + Node head = this.head; + Node node = new Node(); + this.head.next = node; + node.data = o; + node.next = head.next; + size++; + } + public Object get(int index){ + for (int i = 0; i <= index; i++) { + head = head.next; + } + return head.data; + } + public Object remove(int index){ + for (int i = 0; i < index; i++) { + head = head.next; + } + Node head = this.head; + Object data = head.next.data; + Node next = this.head.next.next; + head.next = next; + return data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.next = head; + node.data = o; + size++; + } + public void addLast(Object o){ + Node node = new Node(); + head.next = node; + node.data = o; + node.next = null; + size++; + } + public Object removeFirst(){ + Object data = head.data; + head.next = null; + return data; + } + public Object removeLast(){ + for (int i = 0; i < size; i++) { + head = head.next; + } + Object data = head.next.data; + head.next = null; + return data; + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + Object data; + Node next; + } +} \ No newline at end of file diff --git a/group04/351121278/351121278/src/com/coding/basic/List.java b/group04/351121278/351121278/src/com/coding/basic/List.java new file mode 100644 index 0000000000..ee8ab69781 --- /dev/null +++ b/group04/351121278/351121278/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(); +} \ No newline at end of file diff --git a/group04/351121278/351121278/src/com/coding/basic/Queue.java b/group04/351121278/351121278/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..25737e5206 --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + Object remove = elementData.remove(0); + return remove; + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/351121278/351121278/src/com/coding/basic/Stack.java b/group04/351121278/351121278/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..fd3548a241 --- /dev/null +++ b/group04/351121278/351121278/src/com/coding/basic/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object remove = elementData.remove(elementData.size() - 1); + return remove; + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} \ No newline at end of file diff --git a/group04/474772605/.classpath b/group04/474772605/.classpath new file mode 100644 index 0000000000..28e2b79383 --- /dev/null +++ b/group04/474772605/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group04/474772605/.gitignore b/group04/474772605/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/474772605/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/474772605/.project b/group04/474772605/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group04/474772605/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/474772605/.settings/org.eclipse.jdt.core.prefs b/group04/474772605/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..416f4fb696 --- /dev/null +++ b/group04/474772605/.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.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +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.5 diff --git a/group04/474772605/src/com/coding/basic/ArrayList.java b/group04/474772605/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..de945501c2 --- /dev/null +++ b/group04/474772605/src/com/coding/basic/ArrayList.java @@ -0,0 +1,85 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + private int length = 1; + + private Object[] elementData = new Object[length]; + + public void add(Object o){ + System.out.println("进行add方法"); + capacity(size+1); + System.out.println(elementData.length); + elementData[size++] = o; + //size++; + } + public void add(int index, Object o){ + capacity(size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o ; + } + + public Object get(int index){ + System.out.println("进行get方法"); + return elementData[index]; + } + + public Object remove(int index){ + System.out.println("进行remove方法"); + if(index>size){ + return elementData; + } + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData[--size]=null; + System.out.println(size); + return elementData; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new Iterarorimp(this.elementData); + } + + public class Iterarorimp implements Iterator{ + + int index; + Object[] data = null; + + public Iterarorimp(Object[] data){ + this.data = data; + } + + + public boolean hasNext() { + if(index>=data.length){ + return false; + } + return true; + } + + + public Object next() { + + return this.data[index++]; + } + + + } + + + + public void capacity(int newlength) { + System.out.println("进行扩容方法"); + if(newlength>length){ + Object[] newelementData = new Object[length*2]; + System.arraycopy(elementData, 0, newelementData, 0, size); + this.elementData = newelementData; + length = length*2; + } + } + +} diff --git a/group04/474772605/src/com/coding/basic/BinaryTreeNode.java b/group04/474772605/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group04/474772605/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/group04/474772605/src/com/coding/basic/Dinkedlist.java b/group04/474772605/src/com/coding/basic/Dinkedlist.java new file mode 100644 index 0000000000..f3b9de03f2 --- /dev/null +++ b/group04/474772605/src/com/coding/basic/Dinkedlist.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Dinkedlist { +public static void main(String[] args) { + LinkedList linkedlist = new LinkedList(); + String o = "1234"; + String a = "aaaaaaaa"; + String v = "vvvvvvvv"; + linkedlist.add(o); + linkedlist.add(1,a); + linkedlist.add(2,v); + linkedlist.removeLast(); + for(int i =0 ; iindex){ + Node newnode = new Node(); + newnode.data = o; + Node nodefirst = new Node(); + Node nodelast = new Node(); + int amount =1; + Node first = head; + while(first.next !=null){ + if(amount+1 == index){ + nodefirst.next = newnode; + nodelast = first.next; + newnode.next = nodelast; + size++; + } + first = first.next; + amount++; + } + } + if(size==index){ + + Node node = new Node(); + node.data = o; + last.next =node; + last = node; + size++; + + } + } + public Object get(int index){ + int amount = 0; + Node newnode = head; + while(newnode.next !=null){ + if(amount==index){ + return newnode.data; + } + amount++; + newnode = newnode.next; + } + return newnode.data; + } + public Object remove(int index){ + + if(index ==0){ + removeFirst(); + } + int amount =0; + Node first = head; + Node remove = new Node(); + while(first.next!=null){ + if(amount+1==index){ + remove = first.next; + first.next = remove.next; + size--; + return remove.data; + } + first = first.next; + amount++; + } + return null; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node newhead =new Node(); + newhead.data=o; + newhead.next = head; + head = newhead; + size++; + + } + public void addLast(Object o){ + add(o); + + } + public Object removeFirst(){ + Object removedata = new Object(); + removedata = head.data; + head = head.next; + size--; + return removedata; + + } + public Object removeLast(){ + Node last = head; + Node cache =new Node(); + while(last.next !=null){ + cache = last; + last = last.next; + } + cache.next =null; + size--; + return last.data; + + } + public Iterator iterator(){ + return new Iteratorimp(this.head); + } + public class Iteratorimp implements Iterator{ + Node newnode = new Node(); + public Iteratorimp(Node node){ + this.newnode = node; + } + + public boolean hasNext() { + if(newnode.next ==null){ + return false; + } + return true; + } + + public Object next() { + newnode = newnode.next; + return newnode.data; + } + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group04/474772605/src/com/coding/basic/List.java b/group04/474772605/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group04/474772605/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/group04/474772605/src/com/coding/basic/Queue.java b/group04/474772605/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..29ea3f95ff --- /dev/null +++ b/group04/474772605/src/com/coding/basic/Queue.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class Queue { + Object[] elementData; + int maxsize; + int size =0; + int head=0; + int last=-1; + public Queue(int i){ + maxsize = i; + elementData = new Object[maxsize]; + } + + public void enQueue(Object o){ + + if(size == maxsize){ + throw new IllegalStateException("Queue full"); + } + elementData[size++]=o; + last=size; + + } + + public Object deQueue(){ + + return null; + } + + public boolean isEmpty(){ + if(this.size==0){ + return true; + } + + return false; + } + + public int size(){ + return this.size; + } +} diff --git a/group04/474772605/src/com/coding/basic/Stack.java b/group04/474772605/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7a5a56bdf0 --- /dev/null +++ b/group04/474772605/src/com/coding/basic/Stack.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + int size =0; + + public void push(Object o){ //入栈 + elementData.add(o); + size++; + } + + public Object pop(){ //移走栈顶对象,将该对象作为函数值返回 + Object top = elementData.get(size); + elementData.remove(size); + size--; + return top; + } + + public Object peek(){//查找栈顶对象,而不从栈中移走 + return elementData.get(size); + } + public boolean isEmpty(){ + if(this.size ==0){ + return true; + } + return false; + } + public int size(){ + return this.size; + } +} diff --git a/group04/474772605/src/com/coding/basic/testarraylist.java b/group04/474772605/src/com/coding/basic/testarraylist.java new file mode 100644 index 0000000000..df5e814ccf --- /dev/null +++ b/group04/474772605/src/com/coding/basic/testarraylist.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class testarraylist { + public static void main(String[] args) { + ArrayList abc = new ArrayList(); + String i = "aaa"; + String y = "bbb"; + Object o = new Object(); + + + abc.add(i); + System.out.println(abc.get(0)); + // System.out.println(abc.get(1)); + abc.add(y); + System.out.println(abc.get(0)); + System.out.println(abc.get(1)); + abc.remove(1); + System.out.println(abc.get(0)); + System.out.println(abc.get(1)); + } + +} diff --git a/group04/498654356/one/.classpath b/group04/498654356/one/.classpath new file mode 100644 index 0000000000..ebfd4f69f3 --- /dev/null +++ b/group04/498654356/one/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group04/498654356/one/.gitignore b/group04/498654356/one/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/498654356/one/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/498654356/one/.project b/group04/498654356/one/.project new file mode 100644 index 0000000000..3cad565ebc --- /dev/null +++ b/group04/498654356/one/.project @@ -0,0 +1,17 @@ + + + 498654356Learning_one + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/498654356/one/src/org/coding/one/ArrayList.java b/group04/498654356/one/src/org/coding/one/ArrayList.java new file mode 100644 index 0000000000..a68026101b --- /dev/null +++ b/group04/498654356/one/src/org/coding/one/ArrayList.java @@ -0,0 +1,95 @@ +package org.coding.one; + +import java.util.Arrays; + +public class ArrayList implements List { + + /** + * 用于存放数据的数组 + */ + private Object[] elementData; + + /** + * 记录 ArrayList 中存放元素的个数 + */ + private int size; + + /** + * ArrayList 的默认的初始容量值 + */ + private static final int DEFAULT_CAPACITY = 10; + + public ArrayList() { + this.elementData = new Object[DEFAULT_CAPACITY]; + } + + @Override + public void add(Object o) { + ensureGrow(); + this.elementData[size++] = o; + } + + private void grow(int capacity) { + this.elementData = Arrays.copyOf(this.elementData, capacity); + } + + @Override + public void add(int index, Object o) { + checkRangeIndex(index); + ensureGrow(); + System.arraycopy(this.elementData, index, this.elementData, index + 1, this.size - index); + this.elementData[index] = o; + this.size ++; + } + + private void ensureGrow() { + if(this.size == this.elementData.length) { //扩容 + grow(this.size + 1); + } + } + + @Override + public Object get(int index) { + checkGetValIndex(index); + return this.elementData[index]; + } + + private void checkGetValIndex(int index) { + if(index < 0 || index >= this.size) { //越界 + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+ this.size); + } + + } + + private void checkRangeIndex(int index) { + if(index < 0 || index > this.size) { //越界 + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+ this.size); + } + } + + @Override + public Object remove(int index) { + checkGetValIndex(index); + Object oldVal = this.elementData[index]; + this.elementData[index] = null; //释放引用 + System.arraycopy(this.elementData, index + 1, this.elementData, index, this.size - index -1); + this.size--; + return oldVal; + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return this.size == 0; + } + + @Override + public String toString() { + return "ArrayList [elementData=" + Arrays.toString(elementData) + ", size=" + size + "]"; + } + +} diff --git a/group04/498654356/one/src/org/coding/one/BinaryTreeNode.java b/group04/498654356/one/src/org/coding/one/BinaryTreeNode.java new file mode 100644 index 0000000000..3e2f9c149b --- /dev/null +++ b/group04/498654356/one/src/org/coding/one/BinaryTreeNode.java @@ -0,0 +1,62 @@ +package org.coding.one; + +@SuppressWarnings( {"unchecked", "rawtypes"}) +public class BinaryTreeNode { + + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + public BinaryTreeNode(Comparable data, BinaryTreeNode left, BinaryTreeNode right) { + super(); + this.data = data; + this.left = left; + this.right = right; + } + public Object getData() { + return data; + } + public void setData(Comparable 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(Comparable val){ + return doInsert(this, val); + } + + private BinaryTreeNode doInsert(BinaryTreeNode node, Comparable val) { + if(node == null) { + return null; + } + if(val.compareTo(node.data) == 0) { + return node; + } + if(val.compareTo(node.data) > 0) { + if(node.right == null) { + BinaryTreeNode rightNode = new BinaryTreeNode(val, null, null); + node.right = rightNode; + return rightNode; + } + return doInsert(node.right, val); + } else { + if(node.left == null) { + BinaryTreeNode leftNode = new BinaryTreeNode(val, null, null); + node.left = leftNode; + return leftNode; + } + return doInsert(node.left, val); + } + } + +} diff --git a/group04/498654356/one/src/org/coding/one/LinkedList.java b/group04/498654356/one/src/org/coding/one/LinkedList.java new file mode 100644 index 0000000000..183f634418 --- /dev/null +++ b/group04/498654356/one/src/org/coding/one/LinkedList.java @@ -0,0 +1,138 @@ +package org.coding.one; + +public class LinkedList implements List { + + private Node first; + + private Node last; + + private int size = 0; + + @Override + public void add(Object o) { + Node newNode = new Node(o, null); + if(this.last != null) { + this.last.next = newNode; + this.last = newNode; + } else { + this.first = newNode; + this.last = newNode; + } + this.size++; + } + + @Override + public void add(int index, Object o) { + checkIndex(index); + if(index == 0) { //first + Node newNode = new Node(o, this.first); + this.first = newNode; + if(this.last == null) { //通过该方法放入第一个元素时 + this.last = newNode; + } + this.size++; + } else if (index == this.size) { //last + add(o); + } else { + Node preNode = findNode(index - 1); + Node nextNode = preNode.next; + Node newNode = new Node(o, nextNode); + preNode.next = newNode; + this.size++; + } + } + + /** + * 通过角标获取对应的 Node 对象 + * @param index (0 <= index < size) + * @return + */ + private Node findNode(int index) { + Node node = this.first; + for(int i = 1; i <= index ; i++) { + node = node.next; + } + + return node; + } + + /** + * 0 <= index <= size + * @param index + */ + private void checkIndex(int index) { + if(index < 0 || index > this.size) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+ this.size); + } + } + + @Override + public Object get(int index) { + checkRangeIndex(index); + return findNode(index).data; + } + + /** + * 0 <= index < size + * @param index + */ + private void checkRangeIndex(int index) { + if(index < 0 || index > this.size - 1) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+ this.size); + } + } + + @Override + public Object remove(int index) { + checkRangeIndex(index); + Node oldNode = null; + if(index == 0) { + oldNode = this.first; + this.first = oldNode.next; + } else { + Node preNode = findNode(index - 1); + oldNode = preNode.next; + preNode.next = oldNode.next; + } + oldNode.next = null; + this.size--; + return oldNode.data; + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return this.size == 0; + } + + public void addFirst(Object o){ + add(0, o); + } + + public void addLast(Object o){ + add(this.size, o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size - 1); + + } + + private static class Node { + private Object data; + private Node next; + public Node(Object data, Node next) { + super(); + this.data = data; + this.next = next; + } + } +} diff --git a/group04/498654356/one/src/org/coding/one/List.java b/group04/498654356/one/src/org/coding/one/List.java new file mode 100644 index 0000000000..6c558a2f64 --- /dev/null +++ b/group04/498654356/one/src/org/coding/one/List.java @@ -0,0 +1,16 @@ +package org.coding.one; + +public interface List { + + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); + + public boolean isEmpty(); +} diff --git a/group04/498654356/one/src/org/coding/one/Queue.java b/group04/498654356/one/src/org/coding/one/Queue.java new file mode 100644 index 0000000000..82b9848d67 --- /dev/null +++ b/group04/498654356/one/src/org/coding/one/Queue.java @@ -0,0 +1,22 @@ +package org.coding.one; + +public class Queue { + + private LinkedList dataElement = new LinkedList(); + + public void enQueue(Object o){ + dataElement.addLast(o); + } + + public Object deQueue(){ + return dataElement.removeFirst(); + } + + public boolean isEmpty(){ + return dataElement.isEmpty(); + } + + public int size(){ + return dataElement.size(); + } +} diff --git a/group04/498654356/one/src/org/coding/one/Stack.java b/group04/498654356/one/src/org/coding/one/Stack.java new file mode 100644 index 0000000000..e66d6fe7be --- /dev/null +++ b/group04/498654356/one/src/org/coding/one/Stack.java @@ -0,0 +1,26 @@ +package org.coding.one; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(0, o); + } + + public Object pop(){ + return elementData.remove(0); + } + + public Object peek(){ + return elementData.get(0); + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group04/498654356/one/test/org/coding/one/AllTests.java b/group04/498654356/one/test/org/coding/one/AllTests.java new file mode 100644 index 0000000000..316e8a4143 --- /dev/null +++ b/group04/498654356/one/test/org/coding/one/AllTests.java @@ -0,0 +1,11 @@ +package org.coding.one; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ ArrayListTest.class, BinaryTreeNodeTest.class, LinkedListTest.class, QueueTest.class, StackTest.class }) +public class AllTests { + +} diff --git a/group04/498654356/one/test/org/coding/one/ArrayListTest.java b/group04/498654356/one/test/org/coding/one/ArrayListTest.java new file mode 100644 index 0000000000..1a33b95fa3 --- /dev/null +++ b/group04/498654356/one/test/org/coding/one/ArrayListTest.java @@ -0,0 +1,95 @@ +package org.coding.one; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + private ArrayList target; + private int size = 15; + + @Before + public void setUp() throws Exception { + this.target = new ArrayList(); + } + + @After + public void tearDown() throws Exception { + this.target = null; + } + + @Test + public void testAddObject() { + addElement(size); + Assert.assertFalse(target.isEmpty()); + Assert.assertEquals(size, target.size()); + for(int i = 0; i < size; i++) { + Assert.assertEquals(i, ((Integer)target.get(i)).intValue()); + } +// System.out.println(target); + } + + private void addElement(int size) { + for(int i = 0; i < size; i++) { + target.add(i); + } + } + + @Test + public void testAddIntObject() { + addElement(size); + int destIndex = 1; + int destVal = 11; + target.add(destIndex, destVal); + Assert.assertEquals(destVal, target.get(destIndex)); + Assert.assertEquals(size + 1, target.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddIntObjectException() { + target.add(1, 5); + } + + @Test + public void testGet() { + addElement(size); + Assert.assertEquals(1, ((Integer)target.get(1)).intValue()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetException() { + addElement(size); + target.get(size); + } + + @Test + public void testRemove() { + addElement(size); + int val = (int) target.remove(0); + Assert.assertEquals(0, val); + Assert.assertEquals(size -1, target.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveException() { + addElement(size); + target.remove(size); + } + + @Test + public void testSize() { + Assert.assertEquals(0, target.size()); + addElement(size); + Assert.assertEquals(size, target.size()); + } + + @Test + public void testIsEmpty() { + Assert.assertTrue(target.isEmpty()); + addElement(size); + Assert.assertFalse(target.isEmpty()); + } + +} diff --git a/group04/498654356/one/test/org/coding/one/BinaryTreeNodeTest.java b/group04/498654356/one/test/org/coding/one/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..a1f9670329 --- /dev/null +++ b/group04/498654356/one/test/org/coding/one/BinaryTreeNodeTest.java @@ -0,0 +1,45 @@ +package org.coding.one; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class BinaryTreeNodeTest { + + private BinaryTreeNode target; + + @Before + public void setUp() throws Exception { + target = new BinaryTreeNode(100, null, null); + } + + @After + public void tearDown() throws Exception { + target = null; + } + + @Test + public void testInsert() { + target.insert(70); + target.insert(60); + target.insert(80); + + target.insert(120); + target.insert(110); + target.insert(130); + + BinaryTreeNode left = target.getLeft(); + Assert.assertEquals(70, left.getData()); + Assert.assertEquals(60, left.getLeft().getData()); + Assert.assertEquals(80, left.getRight().getData()); + + BinaryTreeNode right = target.getRight(); + Assert.assertEquals(120, right.getData()); + Assert.assertEquals(110, right.getLeft().getData()); + Assert.assertEquals(130, right.getRight().getData()); + + } + +} diff --git a/group04/498654356/one/test/org/coding/one/LinkedListTest.java b/group04/498654356/one/test/org/coding/one/LinkedListTest.java new file mode 100644 index 0000000000..2ba9f1c6e5 --- /dev/null +++ b/group04/498654356/one/test/org/coding/one/LinkedListTest.java @@ -0,0 +1,194 @@ +package org.coding.one; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + private LinkedList target; + private int size = 5; + + @Before + public void setUp() throws Exception { + this.target = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + this.target = null; + } + + @Test + public void testAddObject() { + target.add(1); + Assert.assertEquals(1, ((Integer)target.get(0)).intValue()); + Assert.assertEquals(1, target.size()); + } + + @Test + public void testAddIntObjectFirst() { + target.add(0, 1); + Assert.assertEquals(1, ((Integer)target.get(0)).intValue()); + Assert.assertEquals(1, target.size()); + } + + @Test + public void testAddIntObject() { + addElement(); + target.add(2, 22); + Assert.assertEquals(22, ((Integer)target.get(2)).intValue()); + Assert.assertEquals(size + 1, target.size()); + } + + private void addElement() { + for(int i = 0; i < size; i++) { + target.add(i); + } + } + + @Test + public void testAddIntObjectLast() { + addElement(); + target.add(size, 100); + Assert.assertEquals(100, ((Integer)target.get(size)).intValue()); + Assert.assertEquals(size + 1, target.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddIntObjectException() { + target.add(-1, 3); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddIntObjectException2() { + addElement(); + target.add(size + 1, 100); + } + + + @Test + public void testGet() { + addElement(); + Assert.assertEquals(4, target.get(4)); + } + + @Test + public void testGetFirst() { + addElement(); + Assert.assertEquals(0, target.get(0)); + } + + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetException() { + addElement(); + target.get(size); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetException2() { + addElement(); + target.get(-1); + } + + @Test + public void testRemove() { + addElement(); + int dest = (int) target.remove(2); + Assert.assertEquals(2, dest); + Assert.assertEquals(4, target.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testEmptyObjRemove() { + target.remove(0); + } + + @Test + public void testRemoveFirst2() { + addElement(); + int dest = (int) target.remove(0); + Assert.assertEquals(0, dest); + Assert.assertEquals(4, target.size()); + } + + @Test + public void testRemoveLast2() { + addElement(); + int dest = (int) target.remove(size - 1); + Assert.assertEquals(4, dest); + Assert.assertEquals(4, target.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveException() { + addElement(); + target.remove( - 1); + } + + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveException2() { + addElement(); + target.remove(size); + } + + @Test + public void testSize() { + Assert.assertEquals(0, target.size()); + addElement(); + Assert.assertEquals(size, target.size()); + } + + @Test + public void testIsEmpty() { + Assert.assertTrue(target.isEmpty()); + addElement(); + Assert.assertFalse(target.isEmpty()); + } + + @Test + public void testAddFirst() { + addElement(); + target.addFirst(100); + Assert.assertEquals(100, target.get(0)); + } + + @Test + public void testAddLast() { + addElement(); + target.addLast(100); + Assert.assertEquals(100, target.get(size)); + } + + @Test + public void testRemoveFirst() { + addElement(); + int dest = (int) target.removeFirst(); + Assert.assertEquals(0, dest); + Assert.assertEquals(size - 1 , target.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveFirstException() { + target.removeFirst(); + } + + @Test + public void testRemoveLast() { + addElement(); + int dest = (int) target.removeLast(); + Assert.assertEquals(4, dest); + Assert.assertEquals(size - 1, target.size()); + } + + @Test + public void testRemoveLast_one() { + target.add(4); + int dest = (int) target.removeLast(); + Assert.assertEquals(4, dest); + Assert.assertEquals(0, target.size()); + } +} diff --git a/group04/498654356/one/test/org/coding/one/QueueTest.java b/group04/498654356/one/test/org/coding/one/QueueTest.java new file mode 100644 index 0000000000..9549bab245 --- /dev/null +++ b/group04/498654356/one/test/org/coding/one/QueueTest.java @@ -0,0 +1,60 @@ +package org.coding.one; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class QueueTest { + + private Queue target; + + @Before + public void setUp() throws Exception { + target = new Queue(); + } + + @After + public void tearDown() throws Exception { + target = null; + } + + @Test + public void testEnQueue() { + Assert.assertEquals(0, target.size()); + target.enQueue(1); + target.enQueue(2); + Assert.assertEquals(2, target.size()); + } + + @Test + public void testDeQueue() { + Assert.assertEquals(0, target.size()); + target.enQueue(1); + target.enQueue(2); + Assert.assertEquals(2, target.size()); + Assert.assertEquals(1, target.deQueue()); + Assert.assertEquals(2, target.deQueue()); + Assert.assertEquals(0, target.size()); + } + + @Test + public void testIsEmpty() { + Assert.assertTrue(target.isEmpty()); + target.enQueue(1); + Assert.assertFalse(target.isEmpty()); + target.deQueue(); + Assert.assertTrue(target.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0, target.size()); + target.enQueue(1); + Assert.assertEquals(1, target.size()); + target.deQueue(); + Assert.assertEquals(0, target.size()); + } + +} diff --git a/group04/498654356/one/test/org/coding/one/StackTest.java b/group04/498654356/one/test/org/coding/one/StackTest.java new file mode 100644 index 0000000000..ec8886ace0 --- /dev/null +++ b/group04/498654356/one/test/org/coding/one/StackTest.java @@ -0,0 +1,70 @@ +package org.coding.one; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class StackTest { + + private Stack target; + + @Before + public void setUp() throws Exception { + target = new Stack(); + } + + @After + public void tearDown() throws Exception { + target = null; + } + + @Test + public void testPush() { + Assert.assertEquals(0, target.size()); + target.push(1); + Assert.assertEquals(1, target.peek()); + Assert.assertEquals(1, target.size()); + } + + @Test + public void testPop() { + target.push(1); + target.push(2); + Assert.assertEquals(2, target.pop()); + Assert.assertEquals(1, target.size()); + Assert.assertEquals(1, target.pop()); + Assert.assertEquals(0, target.size()); + } + + @Test + public void testPeek() { + target.push(1); + target.push(2); + Assert.assertEquals(2, target.peek()); + Assert.assertEquals(2, target.size()); + Assert.assertEquals(2, target.peek()); + Assert.assertEquals(2, target.size()); + } + + @Test + public void testIsEmpty() { + Assert.assertTrue(target.isEmpty()); + target.push(1); + Assert.assertFalse(target.isEmpty()); + target.pop(); + Assert.assertTrue(target.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0, target.size()); + target.push(1); + Assert.assertEquals(1, target.size()); + target.pop(); + Assert.assertEquals(0, target.size()); + } + +} diff --git a/group04/564451732/.classpath b/group04/564451732/.classpath new file mode 100644 index 0000000000..2bdfdc1bf0 --- /dev/null +++ b/group04/564451732/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group04/564451732/.gitignore b/group04/564451732/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/564451732/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/564451732/.project b/group04/564451732/.project new file mode 100644 index 0000000000..23af4c7791 --- /dev/null +++ b/group04/564451732/.project @@ -0,0 +1,17 @@ + + + 564451732 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/564451732/src/DataStructureTest.java b/group04/564451732/src/DataStructureTest.java new file mode 100644 index 0000000000..2f5cc27a94 --- /dev/null +++ b/group04/564451732/src/DataStructureTest.java @@ -0,0 +1,114 @@ +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import hw1.ArrayListImpl; +import hw1.BinaryTreeNode; +import hw1.LinkedList; +import hw1.QueueImpl; +import hw1.StackImpl; + +public class DataStructureTest { + + @Test + public void ArrayListTest() { + ArrayListImpl al = new ArrayListImpl(); + Object o1 = new Object(); + Object o2 = new Object(); + Object o3 = new Object(); + al.add(new Object()); + al.add(new Object()); + assertTrue(al.size() == 2); + al.add(5); + assertFalse(al.size() == 2); + assertTrue(al.size() == 3); + al.add(2, 3); + //System.out.println((int)al.get(2)); + assertTrue((int)al.get(2) == 3); + assertTrue((int)al.get(3) == 5); + } + + @Test + public void LinkedListTest() { + LinkedList ll = new LinkedList(); + Object o1 = new Object(); + Object o2 = new Object(); + Object o3 = new Object(); + Object o4 = new Object(); + Object o5 = new Object(); + Object o6 = new Object(); + ll.add(o1); + ll.add(o2); + //System.out.println(ll.size()); + assertTrue(ll.size() == 2); + ll.add(o3); + ll.add(1,1); + assertTrue((int)ll.get(1) == 1); + assertTrue(ll.size() == 4); + ll.addFirst(2); + ll.addLast(4); + assertTrue((int)ll.get(0) == 2); + assertTrue((int)ll.get(ll.size() - 1) == 4); + assertTrue((int)ll.get(2) == 1); + ll.remove(2); + assertTrue(ll.get(2) == o2); + //System.out.print((int)ll.remove(2)); + //System.out.println((int)ll.get(2)); + //assertFalse((int)ll.get(2) == 1); + assertTrue(ll.size() == 5); + assertTrue((int)ll.removeFirst() == 2); + assertTrue(ll.size() == 4); + assertTrue((int)ll.removeLast() == 4); + assertTrue(ll.size() == 3); + } + + @Test + public void QueueTest(){ + QueueImpl qi = new QueueImpl(); + assertTrue(qi.isEmpty()); + qi.enQueue(1); + qi.enQueue(2); + qi.enQueue(3); + assertTrue(qi.size() == 3); + assertTrue((int)qi.deQueue() == 1); + assertTrue(qi.size() == 2); + assertFalse(qi.isEmpty()); + } + + @Test + public void StackTest() { + StackImpl stack = new StackImpl(); + assertTrue(stack.isEmpty()); + stack.push(1); + stack.push(2); + stack.push(3); + assertTrue(stack.size() == 3); + assertTrue((int)stack.pop() == 3); + assertTrue(stack.size() == 2); + assertTrue((int)stack.peek() == 2); + assertFalse(stack.isEmpty()); + } + + @Test + public void BinaryTreeTest() { + BinaryTreeNode bt = new BinaryTreeNode(); + Integer i1 = 1; + Integer i2 = 3; + Integer i3 = 4; + Integer i4 = 6; + Integer i5 = -1; + bt.insert(i3); + bt.insert(i1); + bt.insert(i4); + bt.insert(i2); + bt.insert(i5); + assertTrue((int)bt.getRoot().getData() == 4); + assertTrue((int)bt.getRoot().getLeft().getData() == 1); + assertTrue((int)bt.getRoot().getRight().getData() == 6); + assertTrue((int)bt.getRoot().getLeft().getLeft().getData() == -1); + assertTrue((int)bt.getRoot().getLeft().getRight().getData() == 3); + + } + +} diff --git a/group04/564451732/src/Test.java b/group04/564451732/src/Test.java new file mode 100644 index 0000000000..3335242a80 --- /dev/null +++ b/group04/564451732/src/Test.java @@ -0,0 +1,8 @@ + +public class Test { + + public static void main(String[] args) { + + } + +} diff --git a/group04/564451732/src/hw1/ArrayListImpl.java b/group04/564451732/src/hw1/ArrayListImpl.java new file mode 100644 index 0000000000..fe42bfc4f7 --- /dev/null +++ b/group04/564451732/src/hw1/ArrayListImpl.java @@ -0,0 +1,88 @@ +package hw1; + +//import java.util.ArrayList; +import java.util.Iterator; +//import java.util.List; + +import com.sun.xml.internal.ws.api.pipe.ThrowableContainerPropertySet; + +public class ArrayListImpl implements List { + +private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //size++; + try{ + if (size < elementData.length) { + elementData[size] = o; + } else { + elementData = grow(elementData, elementData.length); + elementData[size] = o; + } + size++; + } catch (IndexOutOfBoundsException e) { + System.out.println("The added index is out of bound"); + } + } + public void add(int index, Object o){ + if (index < 0) { + throw new IndexOutOfBoundsException("Index cannot be less than 0"); + } + while (index >= elementData.length) { + elementData = grow(elementData, elementData.length); + } + if (index >= size) { + elementData[index] = o; + size++; + } else { + if (size + 1 >= elementData.length) { + elementData = grow(elementData, elementData.length); + } + for (int i = size-1; i >= index;i--) { + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + + } + } + + public Object get(int index){ + if (index >= size ||index < 0) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } else { + return elementData[index]; + } + + } + + public Object remove(int index){ + if (index >= size ||index < 0) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } else { + Object result = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i+1]; + } + elementData[size - 1] = 0; + size--; + return result; + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + private Object[] grow (Object[] src, int increase) { + Object[] target = new Object[src.length + increase]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } +} diff --git a/group04/564451732/src/hw1/BinaryTreeNode.java b/group04/564451732/src/hw1/BinaryTreeNode.java new file mode 100644 index 0000000000..4b0367f13c --- /dev/null +++ b/group04/564451732/src/hw1/BinaryTreeNode.java @@ -0,0 +1,71 @@ +package hw1; + +public class BinaryTreeNode { + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode (Comparable data) { + this.data = data; + } + public BinaryTreeNode () { + + } + public Object getData() { + return data; + } + public void setData(Comparable 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; + return; + } + + + + public BinaryTreeNode getRoot() { + return root; + } + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + public BinaryTreeNode insert(Comparable o){ + BinaryTreeNode result = new BinaryTreeNode(o); + if (root == null) { + root = result; + } else { + BinaryTreeNode dummy = root; + insertHelper(o,dummy); + + } + return result; + } + + private void insertHelper (Comparable o, BinaryTreeNode root) { + if (o.compareTo(root.data) < 0) { + if (root.left == null) { + root.left = new BinaryTreeNode(o); + } else { + insertHelper (o, root.left); + } + } else { + if (root.right == null) { + root.right = new BinaryTreeNode(o); + + } else { + insertHelper (o, root.right); + } + } + } +} diff --git a/group04/564451732/src/hw1/Iterator.java b/group04/564451732/src/hw1/Iterator.java new file mode 100644 index 0000000000..93e22e9377 --- /dev/null +++ b/group04/564451732/src/hw1/Iterator.java @@ -0,0 +1,7 @@ +package hw1; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group04/564451732/src/hw1/LinkedList.java b/group04/564451732/src/hw1/LinkedList.java new file mode 100644 index 0000000000..793f7c66d6 --- /dev/null +++ b/group04/564451732/src/hw1/LinkedList.java @@ -0,0 +1,117 @@ +package hw1; + +import java.util.Iterator; + +public class LinkedList implements List { +private Node head; +private Node tail; + + public void add(Object o){ + if (head != null) { + Node dummy = new Node(new Object()); + dummy.next = head; + Node nextNode = head; + + while (nextNode != null) { + dummy = dummy.next; + nextNode = nextNode.next; + } + dummy.next = new Node(o); + tail = dummy.next; + } else { + head = new Node(o); + tail = head; + } + + } + public void add(int index , Object o){ + if (index < 0 || index > size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 1; i < index; i++) { + dummy = dummy.next; + } + Node temp = dummy.next; + dummy.next = new Node(o); + dummy.next.next = temp; + if (index == size()) { + tail = dummy.next; + } + } + public Object get(int index){ + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 0; i < index;i++) { + dummy = dummy.next; + } + return dummy.data; + } + public Object remove(int index){ + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 1; i < index;i++) { + dummy = dummy.next; + } + Node result = dummy.next; + dummy.next = dummy.next.next; + if (dummy.next == null) { + tail = dummy; + } + return result.data; + } + + public int size(){ + Node dummy = head; + int size = 0; + while (dummy != null) { + dummy = dummy.next; + size++; + } + + return size; + } + + public void addFirst(Object o){ + Node first = new Node(o); + first.next = head; + head = first; + } + public void addLast(Object o){ + tail.next = new Node(o); + tail = tail.next; + } + public Object removeFirst(){ + Node temp = head; + head = head.next; + return temp.data; + } + public Object removeLast(){ + Node dummy = head; + for (int i = 1; i < size()-1; i++) { + dummy = dummy.next; + } + Node result = dummy.next; + tail = dummy; + dummy.next = null; + return result.data; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + + } +} diff --git a/group04/564451732/src/hw1/List.java b/group04/564451732/src/hw1/List.java new file mode 100644 index 0000000000..a976c82204 --- /dev/null +++ b/group04/564451732/src/hw1/List.java @@ -0,0 +1,9 @@ +package hw1; + +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/group04/564451732/src/hw1/QueueImpl.java b/group04/564451732/src/hw1/QueueImpl.java new file mode 100644 index 0000000000..30c73585ff --- /dev/null +++ b/group04/564451732/src/hw1/QueueImpl.java @@ -0,0 +1,21 @@ +package hw1; + +public class QueueImpl { + LinkedList queueList = new LinkedList(); + + public void enQueue(Object o){ + queueList.add(o); + } + + public Object deQueue(){ + return queueList.removeFirst(); + } + + public boolean isEmpty(){ + return queueList.size() == 0; + } + + public int size(){ + return queueList.size(); + } +} diff --git a/group04/564451732/src/hw1/StackImpl.java b/group04/564451732/src/hw1/StackImpl.java new file mode 100644 index 0000000000..3ae23593fa --- /dev/null +++ b/group04/564451732/src/hw1/StackImpl.java @@ -0,0 +1,23 @@ +package hw1; + +public class StackImpl { +private ArrayListImpl elementData = new ArrayListImpl(); + + 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/group04/821655640/learning_projects/project_basic_001/.gitignore b/group04/821655640/learning_projects/project_basic_001/.gitignore new file mode 100644 index 0000000000..39d13906de --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/.gitignore @@ -0,0 +1,44 @@ + +# Created by https://www.gitignore.io/api/eclipse,intellij,java + +### Eclipse ### +target/ +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + diff --git a/group04/821655640/learning_projects/project_basic_001/pom.xml b/group04/821655640/learning_projects/project_basic_001/pom.xml new file mode 100644 index 0000000000..6675a5d76d --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + + com.sunline + project_basic_001 + 0.0.1-SNAPSHOT + jar + + project_basic_001 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.10 + test + + + diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/ArrayList.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c168b5efa8 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,119 @@ +package com.coding.basic; + +/** + * @ClassName: ArrayList + * @Description: 自增长数组 + * @author: tangxp + * @date: 2017年2月23日 下午10:43:03 + */ +public class ArrayList implements List { + + private final int step = 10; + private Object elementData[] = new Object[100]; + private int size = 0 ; + + + /** + * @Title: add + * @Description: TODO + * @param o , elements of this ArrayList + * @see com.coding.basic.List#add(java.lang.Object) + */ + public void add(Object o) { + add(size,o); + } + + + /** + * @Title: add + * @Description: TODO + * @param index + * @param o + * @see com.coding.basic.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + if(index < 0 || index> size) { + throw new IllegalArgumentException("下标越界"); + } + + if(null == o) { + throw new IllegalArgumentException("元素不能为空"); + } + + if(this.checkOutOfBounds()) { + this.autoGrow(this.step); + } + + int i = size; + while(i>index) { + elementData[i] = elementData[--i]; + } + addDriect(i, o); + } + + + public Object get(int index) { + if(index < 0 || index>= size) { + throw new IllegalArgumentException("下标越界"); + } + + return elementData[index]; + } + + public Object remove(int index) { + if(index < 0 || index>= size) { + throw new IllegalArgumentException("下标越界"); + } + + Object o = elementData[index]; + while (indexthis.size) { + return; + } + Object newElementData[] = new Object[elementData.length+growSize]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; + return; + } + + private void addDriect(int index, Object o) { + elementData[index] = o; + this.size++; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + for(int i =0;ithis.size) { + return false; + }else { + return true; + } + } +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/BinaryTreeNode.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..6587275b48 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,84 @@ +package com.coding.basic; + + +public class BinaryTreeNode > { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + 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 insert(T o){ + return insert(this,o); + } + + + public BinaryTreeNode insert(BinaryTreeNode bt, T o) { + BinaryTreeNode insertedNode = null; + if(null == this.data) { + this.data = o; + return this; + } + + if(-1 == o.compareTo(bt.data)) { + if(null == bt.left) { + insertedNode = new BinaryTreeNode(); + insertedNode.data = o; + bt.left = insertedNode; + return insertedNode; + } + return insert(bt.left,o); + }else { + if(null == bt.right) { + insertedNode = new BinaryTreeNode(); + insertedNode.data = o; + bt.right = insertedNode; + return insertedNode; + } + + return insert(bt.right,o); + } + } + + @Override + public String toString() { + StringBuilder space = new StringBuilder(""); + return getString(this,space,0).toString(); + } + + private StringBuilder getString(BinaryTreeNode head,StringBuilder space,int deepth) { + StringBuilder spaceTemp = new StringBuilder(space.toString()); + if (null == head) { + return new StringBuilder(" null , "); + } else { + StringBuilder tempStr = new StringBuilder(" {").append(spaceTemp.append(head.data.toString()) + .append(new StringBuilder(space)).append("} ")); + deepth++; + return getString(head.left, space, deepth).append(deepth) + .append(tempStr).append(deepth).append("\n") + .append(getString(head.right, space, deepth)); + } + + } + + + +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/Iterator.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/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/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/LinkedList.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a824ad9372 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,134 @@ +package com.coding.basic; + + +/** + * @ClassName: LinkedList + * @Description: 带头结点的单向列表. + * @author: tangxp + * @date: 2017年2月23日 下午9:14:28 + */ +public class LinkedList implements List { + + private Node head = new Node(); + private int size ; + + public void add(Object o){ + add(size,o); + } + + public void add(int index , Object o){ + if(index<0 || index>size) { + throw new IllegalArgumentException("目前链表长度不够!"); + } + Node tempHead = head; + int i = 0; + while(i++ < index) { + tempHead = tempHead.next; + } + addDriect(getNode(o),tempHead); + } + + + /** + * @Title: get + * @Description: TODO + * @param index + * @return + * @see com.coding.basic.List#get(int) + */ + public Object get(int index){ + if(index<0|| index>=size) { + throw new IllegalArgumentException("下标超出链表范围!"); + } + Node tempHead = head; + int i = 0; + while(i++ < index) { + tempHead = tempHead.next; + } + return tempHead.next.data; + } + + public Object remove(int index){ + if(index<0 || index>=size) { + throw new IllegalArgumentException("下标超出链表范围!"); + } + Node tempHead = head; + int i = 0; + while(i++ < index) { + tempHead = tempHead.next; + } + + Node removingNode = tempHead.next; + tempHead.next = removingNode.next; + removingNode.next = null; + size--; + return removingNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return null; + } + + private void addDriect(Node n,Node before) { + Node temp = before.next; + n.next = temp; + before.next = n; + size++; + } + + private Node getNode(Object o) { + + if(null == o) { + throw new IllegalArgumentException("节点值不能为空"); + } + + Node n = new Node(); + n.data = o; + return n; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("size: "+size+" {null | ---}--->"); + if(null == head.next) { + return sb.toString(); + } + + Node tempHead = head; + while(null != tempHead.next ) { + sb.append(tempHead.next.toString()); + tempHead = tempHead.next; + } + sb.append("null"); + return sb.toString(); + } + + private static class Node{ + Object data; + Node next; + + @Override + public String toString() { + return "{" + this.data +" |---}--->"; + } + + } +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/List.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/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/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/Queue.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..3ae10dc5c7 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elements = new LinkedList(); + + public void enQueue(Object o){ + elements.addFirst(o); + } + + public Object deQueue(){ + return elements.removeLast(); + } + + public boolean isEmpty(){ + return 0==elements.size(); + } + + public int size(){ + return elements.size(); + } + + @Override + public String toString() { + return elements.toString(); + } +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/Stack.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4638daf558 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +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 0 == elementData.size(); + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/main/java/com/sunline/project_basic_001/App.java b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/sunline/project_basic_001/App.java new file mode 100644 index 0000000000..48b5e72dc2 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/main/java/com/sunline/project_basic_001/App.java @@ -0,0 +1,15 @@ +package com.sunline.project_basic_001; + +import javax.swing.tree.TreeNode; + + +/** + * Hello world! + * + */ +public class App { + public static void main(String[] args) { + System.out.println("Hello World!"); + TreeNode a; + } +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/test/java/com/coding/basic/TestBasic.java b/group04/821655640/learning_projects/project_basic_001/src/test/java/com/coding/basic/TestBasic.java new file mode 100644 index 0000000000..de5eaf51ed --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/test/java/com/coding/basic/TestBasic.java @@ -0,0 +1,92 @@ +package com.coding.basic; + + +import org.junit.Test; + +public class TestBasic { + + @Test + public void testBinaryTree() { + BinaryTreeNode bt = new BinaryTreeNode(); + bt.insert(10); + for (int i = 0; i < 5; i++) { + bt.insert(i); + } + System.out.println(bt); + + } + + @Test + public void testStack() { + Stack stack = new Stack(); + System.out.println(stack.isEmpty()); + for (int i = 0; i < 5; i++) { + stack.push(i); + } + + System.out.println(stack); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack); + System.out.println(stack.peek()); + System.out.println(stack); + System.out.println(stack.isEmpty()); + System.out.println(stack.size()); + + } + + @Test + public void testQueue() { + Queue queue = new Queue(); + System.out.println(queue.isEmpty()); + for (int i = 0; i < 5; i++) { + queue.enQueue(i); + } + System.out.println(queue.isEmpty()); + System.out.println(queue); + queue.deQueue(); + queue.deQueue(); + System.out.println(queue); + System.out.println(queue.size()); + + } + + + + @Test + public void testLinkedList() { + LinkedList link = new LinkedList(); + for(int i=0;i<3;i++) { + link.add(i); + } + System.out.println(link); + link.add(123); + link.addFirst("first"); + link.addLast("last"); + System.out.println(link); + link.removeFirst(); + link.removeLast(); + System.out.println(link); + //link.remove(12); + System.out.println(link.get(2)); + + } + + @Test + public void testArrayList(){ + ArrayList a = new ArrayList(); + for (int i = 0; i <130; i++) { + a.add(i); + } + System.out.println(a); + a.add(3,"abc"); + System.out.println(a); + a.remove(0); + System.out.println(a); + System.out.println(a.get(2)); + a.add("tttttttt"); + System.out.println(a); + + } + +} diff --git a/group04/821655640/learning_projects/project_basic_001/src/test/java/com/sunline/project_basic_001/AppTest.java b/group04/821655640/learning_projects/project_basic_001/src/test/java/com/sunline/project_basic_001/AppTest.java new file mode 100644 index 0000000000..57fc7ea596 --- /dev/null +++ b/group04/821655640/learning_projects/project_basic_001/src/test/java/com/sunline/project_basic_001/AppTest.java @@ -0,0 +1,38 @@ +package com.sunline.project_basic_001; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/group04/844028312/.gitignore b/group04/844028312/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/844028312/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/844028312/one/.classpath b/group04/844028312/one/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group04/844028312/one/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group04/844028312/one/.gitignore b/group04/844028312/one/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/844028312/one/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/844028312/one/.project b/group04/844028312/one/.project new file mode 100644 index 0000000000..d674d0dad5 --- /dev/null +++ b/group04/844028312/one/.project @@ -0,0 +1,17 @@ + + + one + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/844028312/one/src/com/coding/basic/ArrayList.java b/group04/844028312/one/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..947b34ed7c --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + private static final int DEFAULT_CAPACITY = 10; + private Object[] elementData = new Object[100]; + /** + * Ԫ + */ + public void add(Object o){ + + if(size=0 && index<=size){ //ж indexǷsizeΧ 1 2 3 4 + if(size+1=insertData && left==null){ //жϲСһڵ,left==null + left=new BinaryTreeNode(); + left.data=o; + left.left=null; + left.right=null; + } + else if(insertData>nowData && right==null){ //жϲһڵ,==null + right=new BinaryTreeNode(); + right.data=o; + right.left=null; + right.right=null; + } + else{ + BinaryTreeNode treeNode=null; //¼ȽϽڵ + if(nowData>=insertData ){ //ǰڵݴ + treeNode=left; //ȽϽڵΪڵ + } + else{ + treeNode=right; //Ϊҽڵ + } + BinaryTreeNode tempNode=null; //ʱڵ㣬ڼ¼ȽϽڵڵҽڵΪʱ¼ȽϽڵ + while(treeNode!=null){ + nowData=(int) treeNode.data; //ĵǰֵ + if(insertData<=nowData){ //ǰڵݴ + tempNode=treeNode.left; //ʱڵΪڵ + } + else{ + tempNode=treeNode.right; //Ϊҽڵ + } + if(tempNode==null){ + tempNode=treeNode; //¼ȽϽڵ + if(insertData<=nowData){ //ǰڵݴ + treeNode=treeNode.left; //ȽϽڵΪڵ + } + else{ + treeNode=treeNode.right; //Ϊҽڵ + } + } + else{ + treeNode=tempNode; //ʱڵ㲻ΪʱȽϽڵ㸳ֵΪʱڵ + } + } + if(treeNode==null){ //ȽϽڵΪʱ + treeNode=new BinaryTreeNode(); //½ڵ + treeNode.data=o; + treeNode.left=null; + treeNode.right=null; + int upData=(int) tempNode.data; + if(insertData<=upData){ //һڵݴڲڵʱ + tempNode.left=treeNode; //һڵڵ㸳ڵ + } + else{ + tempNode.right=treeNode; + } + } + } + } + else{ //left!=null&&right!=null + BinaryTreeNode treeNode=null; //жһ + if(nowData>=insertData ){ + treeNode=left; + } + else{ + treeNode=right; + } + BinaryTreeNode tempNode=null; + while(treeNode!=null){ + nowData=(int) treeNode.data; + if(insertData<=nowData){ + tempNode=treeNode.left; + } + else{ + tempNode=treeNode.right; + } + if(tempNode==null){ + tempNode=treeNode; + if(insertData<=nowData){ + treeNode=treeNode.left; + } + else{ + treeNode=treeNode.right; + } + } + else{ + treeNode=tempNode; + } + } + if(treeNode==null){ + treeNode=new BinaryTreeNode(); + treeNode.data=o; + treeNode.left=null; + treeNode.right=null; + int upData=(int) tempNode.data; + if(insertData<=upData){ + tempNode.left=treeNode; + } + else{ + tempNode.right=treeNode; + } + } + } + } + return this; + } + + + +} diff --git a/group04/844028312/one/src/com/coding/basic/Iterator.java b/group04/844028312/one/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/844028312/one/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/group04/844028312/one/src/com/coding/basic/JavaTest.java b/group04/844028312/one/src/com/coding/basic/JavaTest.java new file mode 100644 index 0000000000..2d424110a1 --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/JavaTest.java @@ -0,0 +1,89 @@ +package com.coding.basic; + +//import java.util.ArrayList; +//import java.util.Iterator; +public class JavaTest { + public static void main(String[] args){ +// ArrayList a=new ArrayList(); +// for(int i=0;i<5;i++){ +// a.add(i); +// } +// Iterator i=a.iterator(); +// while(i.hasNext()){ +// int c=(int) i.next(); +// System.out.println(c); +// } + + +// a.add(0, "aa"); +// a.add(100, "bb"); +// a.add(200, "cc"); +// long start=System.currentTimeMillis(); +// System.out.println("1start:"+start); +// System.out.println(a.remove(40)); +// long end=System.currentTimeMillis(); +// System.out.println("1end:"+end); +// System.out.println("1:"+(end-start)); +// LinkedList l=new LinkedList(); +// for(int i=0;i<5;i++){ +// l.add(i); +// } +// Iterator i=l.iterator(); +// while(i.hasNext()){ +// Object c=i.next(); +// System.out.println(c); +// } + +// long start2=System.currentTimeMillis(); +// System.out.println("start2:"+start2); +//// System.out.println(l.remove(40)); +//// long end2=System.currentTimeMillis(); +// System.out.println("end2:"+end2); +// System.out.println("2:"+(end2-start2)); +// a.add(1000, "a"); +// Stack s=new Stack(); +// for(int i=0;i<100;i++){ +// s.push(i); +// } +// Object o=s.pop(); +// Object o2=s.peek(); +// System.out.println(o2); +// LinkedList l=new LinkedList(); +// for(int i=0;i<5;i++){ +// l.add(i); +// } +// System.out.println(l.remove(90)); +// l.add(90, "a"); +// System.out.println(l.get(90)); +// System.out.println(l.get(89)); +// System.out.println(l.get(91)); +// System.out.println("a"+l.size()); +// l.addFirst("bb"); +// System.out.println("b"+l.size()); +// //System.out.println(l.get(0)); +// l.addLast("cc"); +// +// System.out.println(l.get(l.size()-1)); +// System.out.println(l.size()); +// l.removeFirst(); +// l.removeLast(); +// System.out.println(l.size()); +// Queue q=new Queue(); +// for(int i=0;i<5;i++){ +// q.enQueue(i); +// } +// q.size(); +// q.isEmpty(); +// q.deQueue(); +// System.out.println(q); +// ArrayList a=new ArrayList(); + BinaryTreeNode btn=new BinaryTreeNode(); + + btn.insert(2); + btn.insert(3); + btn.insert(1); + btn.insert(6); + btn.insert(8); + System.out.println(btn); + } +} diff --git a/group04/844028312/one/src/com/coding/basic/LinkedList.java b/group04/844028312/one/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e8eb9fc4ca --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/LinkedList.java @@ -0,0 +1,195 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + if(head==null){ //headΪ + head=new Node(); //½һڵ + head.data=o; // + head.next=null; //һڵΪ + } + else{ + Node node=head.next; + Node temp = null; + while(node!=null){ + temp=node.next; + if(temp==null){ + temp=node; + node=temp.next; + } + else{ + node=temp; + } + } + if(node==null&&temp==null){ + node=new Node(); + node.data=o; + node.next=null; + head.next=node; + } + else{ + node=new Node(); + node.data=o; + node.next=null; + temp.next=node; + } + } + } + public void add(int index , Object o){ + int size=size(); + if(index0&&size>0){ + Node node=(Node) getNode(index-1); + Node newNode=new Node(); + newNode.data=o; + newNode.next=(Node) getNode(index); + node.next=newNode; + } + else if(index==size&&size>0){ + Node node=(Node) getNode(size-1); + Node newNode=new Node(); + newNode.data=o; + newNode.next=null; + node.next=newNode; + } + else if(index==0&&size!=0){ + Node temp=new Node(); + temp.next=head.next; + temp.data=head.data; + head.data=o; + //head.next=temp; + head.next=temp; + } + else if(index==0&&size==0){ + head=new Node(); + head.data=o; + head.next=null; + } + else{ + System.out.println("±곬Χ"); + } + } + public Object get(int index){ + if(index==0){ + return head.data; + } + else{ + int i=1; + Node node=head.next;//1 2 3 4 5 + while(node!=null){ + if(i==index){ + return node.data; + } + node=node.next; + i++; + } + } + return null; + } + public Object getNode(int index){ + if(index==0){ + return head; + } + else{ + int i=1; + Node node=head.next;//1 2 3 4 5 + while(node!=null){ + if(i==index){ + return node; + } + node=node.next; + i++; + } + } + return null; + } + public Object remove(int index){ + if(index==0){ + Node node=head.next; + Node temp=head; + head=node; + return temp.data; + } + else{ + Node temp=(Node) getNode(index); + Node node=(Node) getNode(index-1); + node.next=(Node) getNode(index+1); + return temp.data; + } + + } + + public int size(){ + if(head==null){ + return 0; + } + else{ + Node node=head.next; + if(node==null){ + return 1; + } + else{ + int i=1; + while(node!=null){ + node=node.next; + i++; + } + return i; + } + } + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(size(),o); + } + public Object removeFirst(){ + Object temp = null; + if(size()>0){ + temp=remove(0); + } + + return temp; + } + public Object removeLast(){ + Object temp = null; + if(size()>0){ + temp=remove(size()-1); + } + return temp; + } + public Iterator iterator(){ + return new Ir(); + } + + + private static class Node{ + Object data; + Node next; + + } + private class Ir implements Iterator{ + private int cursor; + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return cursor!=size(); + } + + @Override + public Object next() { + // TODO Auto-generated method stub + int i=cursor; + if(cursor0){ + return false; + } + return true; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group04/844028312/one/src/com/coding/basic/Stack.java b/group04/844028312/one/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..79733597ca --- /dev/null +++ b/group04/844028312/one/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); //ջѹԪ + } + + public Object pop(){ + Object o=elementData.get(elementData.size()-1); //ջԪأΪջȽ + elementData.remove(elementData.size()-1); //ƳջԪ + return o; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); //ȡջԪ + } + public boolean isEmpty(){ + if(elementData.size()==0){ //elementData.sizeжǷΪ + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group04/844028312/one/src/com/coding/test/ArrayListTest.java b/group04/844028312/one/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..6edc5205cd --- /dev/null +++ b/group04/844028312/one/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,60 @@ +package com.coding.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.BeforeClass; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + +public class ArrayListTest { + private ArrayList arrayList; + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + arrayList=new ArrayList(); + for(int i=0;i<10;i++){ + arrayList.add(i); + } + } + + + @org.junit.Test + public void addAndGet(){ + for(int i=0;i<10;i++){ + Assert.assertEquals(i, (int)arrayList.get(i)); + } + } + @org.junit.Test + public void addWithIndex(){ + arrayList.add(4, "a"); + Assert.assertEquals("a", arrayList.get(4)); + } + + @org.junit.Test + public void remove(){ + Object before=arrayList.get(4); + Object reMove=arrayList.remove(4); + Assert.assertEquals(before, reMove); + + } + @org.junit.Test + public void size(){ + Assert.assertEquals(10, arrayList.size()); + } + @org.junit.Test + public void iterator(){ + Iterator it=arrayList.iterator(); + int i=0; + while(it.hasNext()){ + Assert.assertEquals(it.next(),arrayList.get(i)); + i++; + } + } + +} diff --git a/group04/844028312/one/src/com/coding/test/BinaryTreeNodeTest.java b/group04/844028312/one/src/com/coding/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..2fb70cf976 --- /dev/null +++ b/group04/844028312/one/src/com/coding/test/BinaryTreeNodeTest.java @@ -0,0 +1,28 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + private BinaryTreeNode btn; + @Before + public void setUp() throws Exception { + btn=new BinaryTreeNode(); + + } + + @Test + public void test() { + btn.insert(3); + btn.insert(5); + btn.insert(2); + btn.insert(10); + btn.insert(4); + + } + +} diff --git a/group04/844028312/one/src/com/coding/test/LinkedListTest.java b/group04/844028312/one/src/com/coding/test/LinkedListTest.java new file mode 100644 index 0000000000..c7e0479736 --- /dev/null +++ b/group04/844028312/one/src/com/coding/test/LinkedListTest.java @@ -0,0 +1,66 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import com.coding.basic.Stack; + +public class LinkedListTest { + private LinkedList linkedList; + @Before + public void setUp() throws Exception { + linkedList =new LinkedList(); + for(int i=0;i<10;i++){ + linkedList.add(i); + } + } + @Test + public void addWithIndex() { + linkedList.add(4, "a"); + Object o=linkedList.get(4); + Assert.assertEquals("a",o); + } + @Test + public void get() { + Assert.assertEquals(1,linkedList.get(1)); + } + @Test + public void size() { + Assert.assertEquals(10,linkedList.size()); + } + @Test + public void addFirst() { + linkedList.addFirst("one"); + Object o=linkedList.get(0); + Assert.assertEquals("one",o); + } + @Test + public void addLast() { + linkedList.addLast("last"); + Object o=linkedList.get(10); + Assert.assertEquals("last",o); + } + @Test + public void removeFirst() { + Assert.assertEquals(0,linkedList.removeFirst()); + } + @Test + public void removeLast() { + Assert.assertEquals(9,linkedList.removeLast()); + } + @Test + public void iterator() { + Iterator it=linkedList.iterator(); + int i=0; + while(it.hasNext()){ + Assert.assertEquals(it.next(),linkedList.get(i)); + i++; + } + } + +} diff --git a/group04/844028312/one/src/com/coding/test/QueueTest.java b/group04/844028312/one/src/com/coding/test/QueueTest.java new file mode 100644 index 0000000000..0859b73de6 --- /dev/null +++ b/group04/844028312/one/src/com/coding/test/QueueTest.java @@ -0,0 +1,35 @@ +package com.coding.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Queue; + +public class QueueTest { + private Queue queue; + @Before + public void setUp() throws Exception { + queue=new Queue(); + for(int i=0;i<10;i++){ + queue.enQueue(i); + } + } + + @Test + public void deQueue() { + Assert.assertEquals(0,queue.deQueue() ); + } + @Test + public void isEmpty() { + Assert.assertEquals(false,queue.isEmpty() ); + } + @Test + public void size() { + Assert.assertEquals(10,queue.size()); + } + +} diff --git a/group04/844028312/one/src/com/coding/test/StackTest.java b/group04/844028312/one/src/com/coding/test/StackTest.java new file mode 100644 index 0000000000..efebbdd80d --- /dev/null +++ b/group04/844028312/one/src/com/coding/test/StackTest.java @@ -0,0 +1,40 @@ +package com.coding.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.Stack; + +public class StackTest { + private Stack stack; + @Before + public void setUp() throws Exception { + stack=new Stack(); + for(int i=0;i<10;i++){ + stack.push(i); + } + } + + @Test + public void pushAndpop() { + Assert.assertEquals(9, stack.pop()); + } + @Test + public void peek() { + Assert.assertEquals(9, stack.peek()); + Assert.assertEquals(9, stack.peek()); + } + @Test + public void isEmpty() { + Assert.assertEquals(false,stack.isEmpty()); + } + @Test + public void size(){ + Assert.assertEquals(10,stack.size()); + } + +} diff --git a/group04/916758663/learn01/.gitignore b/group04/916758663/learn01/.gitignore new file mode 100644 index 0000000000..45026a9caf --- /dev/null +++ b/group04/916758663/learn01/.gitignore @@ -0,0 +1,21 @@ +# Eclipse project files +.classpath +.project +.settings/ + + +# Intellij project files +*.iml +.idea/ +*.iws + +# maven +target/ +logs/ +.DS_Store + +# Mac +.DS_Store + +# +*.log diff --git a/group04/916758663/learn01/learn01.iml b/group04/916758663/learn01/learn01.iml new file mode 100644 index 0000000000..6e0c17c683 --- /dev/null +++ b/group04/916758663/learn01/learn01.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group04/916758663/learn01/pom.xml b/group04/916758663/learn01/pom.xml new file mode 100644 index 0000000000..e40157b3bf --- /dev/null +++ b/group04/916758663/learn01/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coding + basic + 1.0-SNAPSHOT + jar + + basic + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + + org.assertj + assertj-core + 3.6.2 + test + + + diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java b/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..0c9e702951 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[3]; + + public void add(Object o){ + add(size,o); + } + public void add(int index, Object o){ + if (index > size){ + throw new IndexOutOfBoundsException(); + } + // 扩容 + if (size == elementData.length || index + 1 > elementData.length) { + int newLength = index + 1 > size * 2 ? index + 1 :size * 2; + elementData = Arrays.copyOf(elementData, newLength); + } + // 移动元素 + System.arraycopy(elementData,index,elementData,index + 1 ,size-index); + elementData[index] = o; + size ++ ; + } + + public Object get(int index){ + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index){ + checkIndex(index); + Object removed = elementData[index]; + System.arraycopy(elementData,index + 1,elementData,index,size-1 - index); + size --; + return removed; + } + + private void checkIndex(int index) { + if (index > size-1){ + throw new IndexOutOfBoundsException(); + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int currentIndex = 0; + + @Override + public boolean hasNext() { + return currentIndex < size(); + } + + @Override + public Object next() { + Object o = get(currentIndex); + currentIndex ++ ; + return o; + } + } + +} diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/BinaryTreeNode.java b/group04/916758663/learn01/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group04/916758663/learn01/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/group04/916758663/learn01/src/main/java/com/coding/basic/Iterator.java b/group04/916758663/learn01/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java b/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..cba0879ac4 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,117 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + Node node = new Node(o, null); + if (head == null){ + head = node; + }else { + Node current = head; + while (current.getNext() != null) { + current = current.getNext(); + } + current.setNext(node); + } + } + + public void add(int index , Object o){ + Node current = head; + for (int i = 0; i < index-1; i++) { + current = current.getNext(); + if (current == null){ + throw new IndexOutOfBoundsException(); + } + } + Node right = current.getNext(); + current.setNext(new Node(o,right)); + } + + public Object get(int index){ + if (head == null){ + return null; + } + Node current = head; + for (int i = 0; i < index; i++) { + current = current.getNext(); + if (current == null){ + throw new IndexOutOfBoundsException(); + } + } + return current.getData(); + } + + public Object remove(int index){ + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.getNext(); + if (current == null){ + throw new IndexOutOfBoundsException(); + } + } + if (current.getNext() == null){ + throw new IndexOutOfBoundsException(); + } + Object removed = current.getNext().getData(); + current.setNext(current.getNext().getNext()); + return removed; + } + + public int size(){ + if (head == null){ + return 0; + } + int n = 1; + Node current = head; + while (current.getNext() != null) { + n ++; + current = current.getNext(); + } + return n; + } + + 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; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + 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; + } + } +} diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/List.java b/group04/916758663/learn01/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java b/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4e23a01d17 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.remove(0); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java b/group04/916758663/learn01/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..83896e5dc5 --- /dev/null +++ b/group04/916758663/learn01/src/main/java/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/group04/916758663/learn01/src/test/java/com/coding/basic/ArrayListTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..8793adf11f --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,68 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/24. + */ +public class ArrayListTest { + + @Test + public void add() throws Exception { + ArrayList l = new ArrayList(); + l.add(1); + l.add(2); + l.add(3); + l.add(4); + assertThat(l.size()).isEqualTo(4); + } + + @Test + public void insert() throws Exception { + ArrayList l = new ArrayList(); + l.add(1); + l.add(2); + l.add(3); + l.add(1,4); + assertThat(l.size()).isEqualTo(4); + assertThat(l.get(1)).isEqualTo(4); + } + + @Test + public void remove() throws Exception { + ArrayList l = new ArrayList(); + l.add(1); + l.add(2); + l.add(3); + Object removed = l.remove(1); + assertThat(l.size()).isEqualTo(2); + assertThat(removed).isEqualTo(2); + } + + @Test + public void get() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void iterator() throws Exception { + ArrayList l = new ArrayList(); + l.add(1); + l.add(2); + l.add(3); + Iterator iterator = l.iterator(); + while (iterator.hasNext()) { + Object next = iterator.next(); + System.out.println(next); + } + + } + +} \ No newline at end of file diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/JavaArrayListTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/JavaArrayListTest.java new file mode 100644 index 0000000000..348f7e9a66 --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/JavaArrayListTest.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/25. + */ +public class JavaArrayListTest { + + @Test + public void add() throws Exception { + java.util.ArrayList l = new java.util.ArrayList(); + l.add(1); + l.add(2); + l.add(3,3); + assertThat(l.size()).isEqualTo(3); + } + +} diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/LinkedListTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..29a5a3d90e --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/25. + */ +public class LinkedListTest { + + @Test + public void add() throws Exception { + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + assertThat(l.size()).isEqualTo(3); + assertThat(l.get(1)).isEqualTo(2); + } + + @Test + public void insert() throws Exception { + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + l.add(1,4); + assertThat(l.size()).isEqualTo(4); + assertThat(l.get(1)).isEqualTo(4); + } + + @Test + public void remove() throws Exception { + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + Object removed = l.remove(1); + assertThat(l.size()).isEqualTo(2); + assertThat(removed).isEqualTo(2); + } + + @Test + public void addFirst() throws Exception { + + } + + @Test + public void addLast() throws Exception { + + } + + @Test + public void removeFirst() throws Exception { + + } + + @Test + public void removeLast() throws Exception { + + } + +} \ No newline at end of file diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/QueueTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..1e451c045c --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/25. + */ +public class QueueTest { + + @Test + public void enQueue() throws Exception { + Queue q = new Queue(); + q.enQueue("a"); + assertThat(q.size()).isEqualTo(1); + } + + @Test + public void deQueue() throws Exception { + Queue q = new Queue(); + q.enQueue("a"); + q.enQueue("b"); + Object o = q.deQueue(); + assertThat(q.size()).isEqualTo(1); + assertThat(o).isEqualTo("a"); + } + +} \ No newline at end of file diff --git a/group04/916758663/learn01/src/test/java/com/coding/basic/StackTest.java b/group04/916758663/learn01/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..1c25b17007 --- /dev/null +++ b/group04/916758663/learn01/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Test; + +/** + * Created by qilei on 17/2/25. + */ +public class StackTest { + + @Test + public void push() throws Exception { + Stack s = new Stack(); + s.push("a"); + assertThat(s.size()).isEqualTo(1); + assertThat(s.peek()).isEqualTo("a"); + } + + @Test + public void pop() throws Exception { + Stack s = new Stack(); + s.push("a"); + s.push("b"); + Object pop = s.pop(); + assertThat(pop).isEqualTo("b"); + } + +} \ No newline at end of file diff --git a/group04/group04.md b/group04/group04.md index d3f5a12faa..8b13789179 100644 --- a/group04/group04.md +++ b/group04/group04.md @@ -1 +1 @@ - + diff --git a/group05/1026626960/.classpath b/group05/1026626960/.classpath new file mode 100644 index 0000000000..bb62e29268 --- /dev/null +++ b/group05/1026626960/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group05/1026626960/.gitignore b/group05/1026626960/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/1026626960/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/1026626960/.project b/group05/1026626960/.project new file mode 100644 index 0000000000..a8522c9339 --- /dev/null +++ b/group05/1026626960/.project @@ -0,0 +1,17 @@ + + + 1026626960Coding + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/1026626960/src/cn/study1/myIterator.java b/group05/1026626960/src/cn/study1/myIterator.java new file mode 100644 index 0000000000..a5e5528559 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myIterator.java @@ -0,0 +1,5 @@ +package cn.study1; + +public class myIterator { + // +} diff --git a/group05/1026626960/src/cn/study1/myQueue.java b/group05/1026626960/src/cn/study1/myQueue.java new file mode 100644 index 0000000000..cfb5e061d7 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myQueue.java @@ -0,0 +1,38 @@ +package cn.study1; + +public class myQueue { + private class Node{ + T t; + Node next; + } + private Node first; + private Node last; + private int N; + public boolean isEmpty(){ + return N==0; + } + public int size(){ + return N; + } + public void enqueue(T t){ + Node oldlast = last; + last = new Node(); + last.t = t; + last.next = null; + if(isEmpty()){ + first = last; + }else{ + oldlast.next = last; + } + N++; + } + public T dequeue(){ + T t = first.t; + first = first.next; + if(isEmpty()){ + last = null; + } + N--; + return t; + } +} diff --git a/group05/1026626960/src/cn/study1/myStack.java b/group05/1026626960/src/cn/study1/myStack.java new file mode 100644 index 0000000000..8364401c45 --- /dev/null +++ b/group05/1026626960/src/cn/study1/myStack.java @@ -0,0 +1,29 @@ +package cn.study1; + +public class myStack { + private class Node{ + T t; + Node next; + } + private Node first; + private int N; + public boolean isEmpty(){ + return N==0; + } + public int size(){ + return N; + } + public void push(T t){ + Node oldfirst = first; + first = new Node(); + first.t = t; + first.next = oldfirst; + N++; + } + public T pop(){ + T t = first.t; + first = first.next; + N--; + return t; + } +} diff --git a/group05/1026626960/src/cn/study2/array/ArrayUtil.java b/group05/1026626960/src/cn/study2/array/ArrayUtil.java new file mode 100644 index 0000000000..a57881918b --- /dev/null +++ b/group05/1026626960/src/cn/study2/array/ArrayUtil.java @@ -0,0 +1,213 @@ +package cn.study2.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] + */ + public void exchangeArray(int[] a){ + int index = a.length; + int a1 = 0; + for(int i = 1; i < index/2; i++){ + a[i] = a1; + a[i] = a[index - i]; + a[index - i] = a[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} + */ + public int[] removeZero(){ + int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int size = 0; + for(int i = 0; i < oldArr.length; i++){ + if(oldArr[i] != 0){ + size++; + } + } + int newArr[] = new int[size]; + for(int i = 0; i < oldArr.length; i++){ + int j = 0; + if(oldArr[i] != 0){ + newArr[j] = oldArr[i]; + j++; + } + } + return newArr; + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + */ + public int[] reshapeArr(int a1[], int a2[]){ + int size = a1.length+a2.length; + int a3[] = new int[size]; + //δ + return a3; + } + + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + */ + public int[] addSize(int oldArr[],int size){ + int newSize = oldArr.length+size; + int newArr[] = new int[newSize]; + for(int i = 0; i < newArr.length; i++){ + if(i < oldArr.length){ + newArr[i] = oldArr[i]; + } + newArr[i] = 0; + } + return newArr; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + */ + public int[] method1(int max){ + int Arr[] = new int[max]; + if(max <= 1){ + return null; + }else if(max <= 2){ + int a[] = {1,1,2}; + return a; + }else{ + Arr[0] = 1; + Arr[1] = 1; + Arr[2] = 2; + int n = 3; + while(n < max){ + Arr[n] = Arr[n-2] + Arr[n-1]; + } + return Arr; + } + } + + /** + * 쳲 + */ + //δ + + + + /** + * Сڸֵmax + * max = 23, صΪ[2,3,5,7,11,13,17,19] + */ + public int[] getPrimes(int max) { + int size = 0; + for (int i = 0; i < max; i++) { + if (isPrimes(i)) { + size++; + } + } + int Arr[] = new int[size]; + int j = 0; + for (int i = 0; i < max; i++) { + if (isPrimes(i)) { + Arr[j++] = i; + } + } + return Arr; + } + + /** + * жǷΪ + */ + public boolean isPrimes(int num){ + if(num<=1){ + return false; + }else{ + for(int i=2;i + /index.jsp + /login.jsp + + */ +public class ActionMapping { + // · + private String name; + // acitonȫ + private String className; + // + private String method; + // ͼ + private Map results; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public String getMethod() { + return method; + } + public void setMethod(String method) { + this.method = method; + } + public Map getResults() { + return results; + } + public void setResults(Map results) { + this.results = results; + } +} diff --git a/group05/1026626960/src/cn/study2/myStruts/Result.java b/group05/1026626960/src/cn/study2/myStruts/Result.java new file mode 100644 index 0000000000..0ed80bd80f --- /dev/null +++ b/group05/1026626960/src/cn/study2/myStruts/Result.java @@ -0,0 +1,28 @@ +package cn.study2.myStruts; + +public class Result { + // תĽ + private String name; + // תͣĬΪת "redirect"Ϊض + private String type; + // תҳ + private String page; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getPage() { + return page; + } + public void setPage(String page) { + this.page = page; + } +} diff --git a/group05/1026626960/src/cn/study2/myStruts/Test.java b/group05/1026626960/src/cn/study2/myStruts/Test.java new file mode 100644 index 0000000000..3f571354f9 --- /dev/null +++ b/group05/1026626960/src/cn/study2/myStruts/Test.java @@ -0,0 +1,7 @@ +package cn.study2.myStruts; + +public class Test { + public static void main(String[] args) { + new readStrutsXml(); + } +} diff --git a/group05/1026626960/src/cn/study2/myStruts/readStrutsXml.java b/group05/1026626960/src/cn/study2/myStruts/readStrutsXml.java new file mode 100644 index 0000000000..c90fdfe5af --- /dev/null +++ b/group05/1026626960/src/cn/study2/myStruts/readStrutsXml.java @@ -0,0 +1,98 @@ +package cn.study2.myStruts; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; + +/** + * ģȡһstruts.xmlļ + * + + + + + /index.jsp + /login.jsp + + + + /login + + + + + + * @author zhengliang + * + */ +public class readStrutsXml { + // actionļ + private Map allActions = new HashMap(); + public readStrutsXml() { + this.init(); + } + + // ʼallActions + private void init() { + /********DOM4Jȡļ***********/ + System.out.println("ڶȡmystruts.xmlļ"); + try { + // õ + SAXReader reader = new SAXReader(); + // õsrc/mystruts.xml ļ + InputStream inStream = this.getClass().getResourceAsStream("/mystruts.xml"); + // ļ + Document doc = reader.read(inStream); + // ȡ + Element root = doc.getRootElement(); + // õpackageڵ + Element ele_package = root.element("package"); + // õpackageڵ£ еactionӽڵ + List listAction = ele_package.elements("action"); + // װ + for (Element ele_action : listAction) { + // װһActionMapping + ActionMapping actionMapping = new ActionMapping(); + actionMapping.setName(ele_action.attributeValue("name")); + actionMapping.setClassName(ele_action.attributeValue("class")); + actionMapping.setMethod(ele_action.attributeValue("method")); + + // װǰacitonڵеĽͼ + Map results = new HashMap(); + + // õǰactionڵеresultӽڵ + Iterator it = ele_action.elementIterator("result"); + while (it.hasNext()) { + // ǰÿһԪض + Element ele_result = it.next(); + // װ + Result res = new Result(); + res.setName(ele_result.attributeValue("name")); + res.setType(ele_result.attributeValue("type")); + res.setPage(ele_result.getTextTrim()); + // ӵ + results.put(res.getName(), res); + } + // õactionMapping + actionMapping.setResults(results); + + // 6.x actionMappingӵmap + allActions.put(actionMapping.getName(), actionMapping); + } + + System.out.println("struts.xmlļȡ"); + + } catch (Exception e) { + throw new RuntimeException("ʱʼ",e); + } + } + + +} diff --git a/group05/1026626960/src/mystruts.xml b/group05/1026626960/src/mystruts.xml new file mode 100644 index 0000000000..2a3b40e879 --- /dev/null +++ b/group05/1026626960/src/mystruts.xml @@ -0,0 +1,15 @@ + + + + + /index.jsp + /login.jsp + + + + /login + + + + + \ No newline at end of file diff --git a/group05/1094051862/test01/.classpath b/group05/1094051862/test01/.classpath new file mode 100644 index 0000000000..9794cd8084 --- /dev/null +++ b/group05/1094051862/test01/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group05/1094051862/test01/.gitignore b/group05/1094051862/test01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/1094051862/test01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/1094051862/test01/.project b/group05/1094051862/test01/.project new file mode 100644 index 0000000000..1dfd9165c6 --- /dev/null +++ b/group05/1094051862/test01/.project @@ -0,0 +1,17 @@ + + + test01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs b/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..d17b6724d1 --- /dev/null +++ b/group05/1094051862/test01/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java b/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..80c2bf7c32 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,235 @@ +package com.coderising.array; + +import org.junit.experimental.max.MaxCore; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + if (origin.length < 2) + return; + int temp; + for (int i = 0; i < origin.length >> 1; i++) { + temp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = 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 null; + int zeros = 0;// 数组中0元素的个数 + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeros++; + } + int[] newArr = new int[oldArray.length - zeros]; + for (int i = 0, j = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArr[j] = oldArray[i]; + j++; + } + } + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, 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[] mergedArr = new int[array1.length + array2.length]; + int temp; + int i = 0, j = 0, index = 0, size = 0; + while (i < array1.length && j < array2.length) { + //两个数组都没遍历到最后一个元素 + if (i != array1.length - 1 && j != array2.length - 1) { + if (array1[i] < array2[j]) { + temp = array1[i++]; + } else if (array1[i] > array2[j]) { + temp = array2[j++]; + } else { + //遇到相等元素,存放任意一个就实现去重了 + temp = array1[i++]; + j++; + } + mergedArr[index++] = temp; + size++; + //array1遍历到最后一个元素 + } else if (i == array1.length - 1 && j != array2.length - 1) { + if (array1[i] < array2[j]) { + temp = array1[i]; + mergedArr[index++] = temp; + size++; + //将array2的剩余元素复制到mergedArr中 + System.arraycopy(array2, j, mergedArr, index, array2.length - j); + size += array2.length - j; + break; + } else if (array1[i] > array2[j]) { + temp = array2[j++]; + size++; + } else { + System.arraycopy(array2, j, mergedArr, index, array2.length - j); + size += array2.length - j; + break; + } + //array2遍历到最后一个元素 + } else if (i != array1.length - 1 && j == array2.length - 1) { + if (array1[i] > array2[j]) { + temp = array2[j]; + mergedArr[index++] = temp; + size++; + //将array1的剩余元素复制到mergedArr中 + System.arraycopy(array1, i, mergedArr, index, array1.length - i); + size += array1.length - i; + break; + } else if (array1[i] < array2[j]) { + temp = array2[i++]; + size++; + } else { + System.arraycopy(array1, i, mergedArr, index, array1.length - i); + size += array1.length - i; + break; + } + } + } + //构造新数组,去除mergedArr中尾部若干0元素 + int[] result = new int[size]; + System.arraycopy(mergedArr, 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) { + int[] arr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, arr, 0, oldArray.length); + return arr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max < 2) return null; + int[] a = new int[max]; + a[0] = 1; + a[1] = 1; + int size = 2; + for (int i = 2;; i++) { + a[i] = a[i-1] + a[i-2]; + if (a[i] > max) break; + size ++; + } + int[] fibonacci = new int[size]; + System.arraycopy(a, 0, fibonacci, 0, size); + return fibonacci; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + int[] a = new int[max]; + int size = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + a[size++] = i; + } + } + int[] primes = new int[size]; + System.arraycopy(a, 0, primes, 0, size); + return primes; + } + private static boolean isPrime(int i) { + for (int j = 2; j*j <= i; j++) { + if (i % j == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] a = new int[max]; + int size = 0; + for (int i = 6; i < max; i++) { + if (isPerfectNumber(i)) { + a[size++] = i; + } + } + int[] perfectNumbers = new int[size]; + System.arraycopy(a, 0, perfectNumbers, 0, size); + return perfectNumbers; + } + private static boolean isPerfectNumber(int i) { + int sum = 0; + for (int j = 1; j <= i >> 1; j++) { + if (i % j == 0) { + sum += j; + } + } + if (i == sum) return true; + else return false; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i); + sb.append(seperator); + } + return sb.substring(0, sb.lastIndexOf(seperator)); + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java b/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..ae75b41379 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,91 @@ +package com.coderising.array; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import sun.misc.Perf.GetPerfAction; + +public class ArrayUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() { + int[] a = {7,9,30,3,5}; + int[] b = a.clone(); + ArrayUtil.reverseArray(a); + for (int i = 0; i < b.length; i ++) { + Assert.assertEquals(b[i], a[b.length - 1 - i]); + } + + } + + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] result = {1,3,4,5,6,6,5,4,7,6,7,5}; + int[] removeZero = ArrayUtil.removeZero(oldArr); + assertArrayEquals(result, removeZero); + } + + @Test + public void testMerge() { + int[] a1 = {3,5,7,8}; + int[] a2 = {4,5,6,7}; + int[] a3 = {3,4,5,6,7,8}; + int[] merge = ArrayUtil.merge(a1, a2); + assertArrayEquals(a3, merge); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int size = 3; + int[] growedArr = {2,3,6,0,0,0}; + assertArrayEquals(growedArr, ArrayUtil.grow(oldArray, size)); + } + + @Test + public void testFibonacci() { + int[] fibonacci = ArrayUtil.fibonacci(15); + for (int i : fibonacci) { + System.out.println(i); + } + } + + @Test + public void testGetPrimes() { + int[] primes = ArrayUtil.getPrimes(3); + for (int i : primes) { + System.out.println(i); + } + } + + @Test + public void testGetPerfectNumbers() { + int[] perfectNumbers = ArrayUtil.getPerfectNumbers(10000); + int[] expecteds = {6,28,496,8128}; + assertArrayEquals(expecteds, perfectNumbers); + for (int i : perfectNumbers) { + System.out.println(i); + } + } + + @Test + public void testJoin() { + int[] arr = {3,4,5}; + String join = ArrayUtil.join(arr, "-"); + assertEquals("3-4-5", join); + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/litestruts/LoginAction.java b/group05/1094051862/test01/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group05/1094051862/test01/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/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java b/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..c9e9f50631 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,181 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.parsers.ParserConfigurationException; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; +import org.omg.PortableInterceptor.ObjectIdHelper; +import org.xml.sax.SAXException; + +import com.coding.basic.ArrayList; +import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; + +public class Struts { + @Test + public static View runAction(String actionName, + Map parameters) { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + + // DOM4J方式解析xml + // 读取文件 转换成Document + View view = new View(); + if (actionName == null || actionName.isEmpty()) { + return view; + } + try { + //获取action标签元素 + Element actionEle = getActionElement(actionName); + + String qualifiedName = actionEle.attributeValue("class"); + Class clazz = Class.forName(qualifiedName); + Object instanceOfAction = getParameteredInstance(parameters, clazz); + Method executeMethod = clazz.getMethod("execute"); + + //调用execute方法,改变action实例,一定要在执行getResultMap方法之前执行 + String executeResult = (String) executeMethod.invoke(instanceOfAction); + Method[] methods = clazz.getMethods(); + Map resultMap = getResultMap(instanceOfAction, methods); + view.setParameters(resultMap); + + List resultEles = actionEle.elements("result"); + String jspPath = getJspPath(executeResult, resultEles); + view.setJsp(jspPath); + } catch (ClassNotFoundException e1) { + e1.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } + + private static String getJspPath(String result, List resultEles) { + for (int i = 0; i < resultEles.size(); i++) { + String text = resultEles.get(i).attributeValue("name"); + if (text.equals(result)) { + String jspString = resultEles.get(i).getText(); + return jspString; + } + } + return null; + } + + private static Map getResultMap(Object obj, Method[] methodsOfObj) + throws IllegalAccessException, InvocationTargetException { + Map resultMap = new HashMap(); + Method m; + for (int i = 0; i < methodsOfObj.length; i++) { + if (methodsOfObj[i].getName().startsWith("get")) { + m = methodsOfObj[i]; + String methodName = getMethodNameInLowerCase(m); + Object result = m.invoke(obj); + resultMap.put(methodName.substring(3), result); + } + } + return resultMap; + } + + private static String getMethodNameInLowerCase(Method m) { + String methodName = m.getName(); + StringBuilder sbr = new StringBuilder(methodName); + sbr.setCharAt(3, Character.toLowerCase(sbr.charAt(3))); + methodName = sbr.toString(); + return methodName; + } + + private static Object getParameteredInstance(Map parameters, + Class clazz) { + Object obj = null; + try { + obj = clazz.newInstance(); + Map methodsAndParams = new HashMap(); + Set keySet = parameters.keySet(); + for (String k : keySet) { + String methodName = getMethodNameByProperty(k); + methodsAndParams.put(methodName, parameters.get(k)); + Method setter = clazz.getMethod(methodName, parameters.get(k) + .getClass()); + setter.invoke(obj, parameters.get(k)); + } + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return obj; + } + + private static String getMethodNameByProperty(String k) { + StringBuilder sb = new StringBuilder(k); + sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); + sb = sb.insert(0, "set"); + String method = sb.toString();// 获得方法名 + return method; + } + + @SuppressWarnings("unchecked") + private static Element getActionElement(String actionName) throws DocumentException { + SAXReader reader = new SAXReader(); + Document document = reader.read(new File( + "src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + List actNodes = root.elements("action"); + Element e = null; + String className; + for (int i = 0; i < actNodes.size(); i++) { + className = actNodes.get(i).attributeValue("name"); + if (className != null && className.equals(actionName)) { + e = actNodes.get(i); + break; + } + } + return e; + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/litestruts/StrutsTest.java b/group05/1094051862/test01/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group05/1094051862/test01/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/group05/1094051862/test01/src/com/coderising/litestruts/View.java b/group05/1094051862/test01/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group05/1094051862/test01/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/group05/1094051862/test01/src/com/coderising/litestruts/struts.xml b/group05/1094051862/test01/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..c017732676 --- /dev/null +++ b/group05/1094051862/test01/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/group05/1094051862/test01/src/com/coding/basic/ArrayList.java b/group05/1094051862/test01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..60fe8b9150 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,79 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + private int increaseSize = 10; + private void increaseArray() { + Object[] newData = Arrays.copyOf(elementData, elementData.length + increaseSize); + elementData = newData; + } + public void add(Object o){ + if (size == elementData.length) { + increaseArray(); + elementData[size++] = o; + } else { + elementData[size++] = o; + } + } + public void add(int index, Object o){ + if (index < 0 || index > size) { + System.out.println("错误提示:index > size || index < 0"); + return; + } + Object temp; + for (int i = index; i < size; i++) { + temp = elementData[i]; + elementData[i] = o; + o = temp; + } + elementData[size ++] = o; + } + + public Object get(int index){ + if (index < 0 || index > size ){ + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index > size ){ + return null; + } + Object result = elementData[index]; + for (int i = index; i < size-1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size-1] = null; + size --; + return result; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + private int cusor = 0; + @Override + public Object next() { + if (!hasNext()) { + System.out.println("next: !hasNext"); + return null; + } + return elementData[cusor ++]; + } + @Override + public boolean hasNext() { + return cusor < size; + } + }; + } +} diff --git a/group05/1094051862/test01/src/com/coding/basic/ArrayListTest.java b/group05/1094051862/test01/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..8a5875427e --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void test() { + List list = new ArrayList(); + for(int i = 0; i < 10; i++) { + list.add(i); + } + Assert.assertEquals(10, list.size()); + list.add(11); + list.add(3,99); + Assert.assertEquals(99, list.get(3)); + Assert.assertEquals(12, list.size()); + Assert.assertEquals(99, list.remove(3)); + Assert.assertEquals(11, list.size()); + Iterator iterator = list.iterator(); + for (int i = 0; i< list.size(); i++) { + System.out.println(list.get(i)); + } + System.out.println("======"); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} diff --git a/group05/1094051862/test01/src/com/coding/basic/BinaryTreeNode.java b/group05/1094051862/test01/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/1094051862/test01/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/group05/1094051862/test01/src/com/coding/basic/Iterator.java b/group05/1094051862/test01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/1094051862/test01/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/group05/1094051862/test01/src/com/coding/basic/LinkedList.java b/group05/1094051862/test01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..4ef910fc16 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,149 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node last; + private int size = 0; + + public void add(Object o){ + if (head == null) { + head = new Node(o, null); + size ++; + return; + } + Node n = new Node(o, null); + if (last == null) { + last = n; + head.next = last; + } + last.next = n; + last = n; + size ++; + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + System.out.println("linkedList.add: index < 0 || index > size"); + return; + } + if (index == size) { + add(o); + return; + } + if (index == 0) { + addFirst(o); + return; + } + Node pre = head; + for (int i = 1; i < index; i++) { + pre = pre.next; + } + Node post = pre.next; + Node n = new Node(o,post); + pre.next = n; + size ++; + } + public Object get(int index){ + if (index == 0) { + return head.data; + } + Node n = head; + for (int i = 1; i <= index; i++) { + n = n.next; + } + return n.data; + } + public Object remove(int index){ + if (index < 0 || index >= size) { + System.out.println("remove :index < 0 || index >= size"); + return null; + } + if (index == 0) { + return removeFirst(); + } + if (index == size - 1) { + return removeLast(); + } + Node pre = head; + for (int i = 1; i < index; i++) { + pre = pre.next; + } + Node n = pre.next; + Node post = n.next; + n.next = null; + pre.next = post; + size --; + return n.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node n = new Node(o,head); + head = n; + size ++; + return; + } + public void addLast(Object o){ + Node n = new Node(o,null); + last.next = n; + last = n; + size ++; + return; + } + public Object removeFirst(){ + Object o = head.data; + Node n = head.next; + head.next = null; + head = n; + size --; + return o; + } + public Object removeLast(){ + Node preLast = head; + for (int i = 1; i < size; i++) { + preLast = preLast.next; + } + preLast.next = null; + Object o = last.data; + last = preLast; + size --; + return o; + } + public Iterator iterator(){ + return new Iterator() { + int cusor = 0; + Node current = head; + @Override + public Object next() { + if (!hasNext()) { + System.out.println("next : !hasNext"); + return null; + } + Object o = current.data; + current = current.next; + cusor ++; + return o; + } + + @Override + public boolean hasNext() { + return cusor < size; + } + }; + } + + + private static class Node { + + Object data; + Node next; + + public Node (Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group05/1094051862/test01/src/com/coding/basic/LinkedListTest.java b/group05/1094051862/test01/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..ca269f7d9f --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +import junit.framework.Assert; + +import org.junit.Test; + +public class LinkedListTest extends LinkedList { + + @Test + public void test() { + List list = new LinkedList(); + list.add(0); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(3, 33); + list.add(0, 100); + list.add(8,800); + Assert.assertEquals(9, list.size()); + Assert.assertEquals(100, list.get(0)); + Assert.assertEquals(0, list.get(1)); + Assert.assertEquals(1, list.get(2)); + Assert.assertEquals(2, list.get(3)); + Assert.assertEquals(33, list.get(4)); + Assert.assertEquals(3, list.get(5)); + Assert.assertEquals(4, list.get(6)); + Assert.assertEquals(800, list.get(8)); + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} diff --git a/group05/1094051862/test01/src/com/coding/basic/List.java b/group05/1094051862/test01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..ef939ae2cc --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/List.java @@ -0,0 +1,10 @@ +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(); + public Iterator iterator(); +} diff --git a/group05/1094051862/test01/src/com/coding/basic/Queue.java b/group05/1094051862/test01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b65d01ab8e --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/Queue.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Queue { + private List list = new ArrayList(); + private int size = 0; + public void enQueue(Object o){ + list.add(o); + size ++; + } + + public Object deQueue(){ + if (size == 0) + return null; + size --; + return list.remove(0); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group05/1094051862/test01/src/com/coding/basic/QueueTest.java b/group05/1094051862/test01/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..f75beb397f --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/QueueTest.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Test; + +import sun.org.mozilla.javascript.internal.ast.NewExpression; + +public class QueueTest { + + @Test + public void test() { + Queue queue = new Queue(); + for (int i = 0; i < 100; i++) { + queue.enQueue(i); + } + Assert.assertEquals(100, queue.size()); + for (int i = 0; i < 100; i++) { + Assert.assertEquals(i, queue.deQueue()); + } + Assert.assertEquals(0, queue.size()); + + } + +} diff --git a/group05/1094051862/test01/src/com/coding/basic/Stack.java b/group05/1094051862/test01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7fabb3494f --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private List elementData = new ArrayList(); + private int size = 0; + public void push(Object o){ + elementData.add(o); + size ++; + } + + public Object pop(){ + if (size == 0) + return null; + return elementData.remove(--size); + } + + public Object peek(){ + if (size == 0) + return null; + return elementData.get(size - 1); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } +} diff --git a/group05/1094051862/test01/src/com/coding/basic/StackTest.java b/group05/1094051862/test01/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..adcb2c6522 --- /dev/null +++ b/group05/1094051862/test01/src/com/coding/basic/StackTest.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class StackTest { + + @Test + public void test() { + Stack stack = new Stack(); + for (int i = 0; i < 100; i++) { + stack.push(i); + } + Assert.assertEquals(100, stack.size()); + Assert.assertEquals(99, stack.pop()); + for (int i = 98; i >= 0; i--) { + Assert.assertEquals(i, stack.peek()); + Assert.assertEquals(i, stack.pop()); + } + Assert.assertEquals(0, stack.size()); + } + +} diff --git a/group05/183127807/HomeWork0226/.idea/misc.xml b/group05/183127807/HomeWork0226/.idea/misc.xml new file mode 100644 index 0000000000..2e47c90c3c --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/misc.xml @@ -0,0 +1,22 @@ + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/modules.xml b/group05/183127807/HomeWork0226/.idea/modules.xml new file mode 100644 index 0000000000..e8f27e7b12 --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/vcs.xml b/group05/183127807/HomeWork0226/.idea/vcs.xml new file mode 100644 index 0000000000..c2365ab11f --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/.idea/workspace.xml b/group05/183127807/HomeWork0226/.idea/workspace.xml new file mode 100644 index 0000000000..927ae842b9 --- /dev/null +++ b/group05/183127807/HomeWork0226/.idea/workspace.xml @@ -0,0 +1,1155 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1487584408499 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/HomeWork0226.iml b/group05/183127807/HomeWork0226/HomeWork0226.iml new file mode 100644 index 0000000000..c90834f2d6 --- /dev/null +++ b/group05/183127807/HomeWork0226/HomeWork0226.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java b/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..d8609f5ca3 --- /dev/null +++ b/group05/183127807/HomeWork0226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,85 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private static final int DEFAULT_CAPACITY = 100; + + private int size = 0; + + private Object[] elementData = new Object[DEFAULT_CAPACITY]; + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + ensureCapacity(size+1); + for (int i = size;i>index;i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + + + public Object remove(int index){ + Object[] removeData = (Object[]) elementData[index]; + for (int i =index;i elementData.length) { + + elementData = Arrays.copyOf(elementData, minCapacity + DEFAULT_CAPACITY); + } + } + +} diff --git a/group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java b/group05/183127807/HomeWork0226/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/183127807/HomeWork0226/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/group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java b/group05/183127807/HomeWork0226/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/183127807/HomeWork0226/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/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java b/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a1bbcad9db --- /dev/null +++ b/group05/183127807/HomeWork0226/src/com/coding/basic/LinkedList.java @@ -0,0 +1,142 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size ; + + private Node current = head; + public LinkedList() { + head = null; + size = 0; + } + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + if (current.next == null) + { + current.next = newNode; + } + + while (current.next != null){ + current = current.next; + } + current.next = newNode; + size++; + + } + public void add(int index , Object o){ + + Node newNode = new Node(); + newNode.data = o; + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + for (int i = 0; i < index - 2; i++) { + + current = current.next; + } + + newNode.next = current.next; + current.next = newNode; + size++; + } + public Object get(int index){ + + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } else if (index == 0) { + return head; + } + for (int i = 0;i + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group05/183127807/HomeWork0305/src/com/coderising/array/ArrayUtil.java b/group05/183127807/HomeWork0305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..510efb2565 --- /dev/null +++ b/group05/183127807/HomeWork0305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,201 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int length = origin.length; + for (int i = 0, j = length / 2; i < j; i++) { + int temp = origin[i]; + origin[i] = origin[length - i - 1]; + origin[length - i - 1] = 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 int[] removeZero(int[] oldArray){ + int newLength = 0; + for (int t : oldArray){ + newLength += t != 0 ? 1 : 0; + } + int[] newArr = new int[newLength]; + int i = 0; + for (int t : oldArray) { + if (t != 0) { + newArr[i] = t; + i++; + } + } + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int array1Length = array1.length; + int array2Length = array2.length; + + int[] newArray=new int[array1Length+array2Length]; + + int i = 0, j = 0, index = 0; + + while (i < array1.length && j < array2.length) { + if (array1[i] <= array2[j]) { + if (array1[i] == array2[j]) { + j++; + } else { + newArray[index] = array1[i]; + i++; + index++; + } + } else { + newArray[index] = array2[j]; + j++; + index++; + } + } + //当array2为null + while (i < array1.length) { + newArray[index] = array1[i]; + index++; + i++; + } + //当array1为null + while (j < array2.length) { + newArray[index] = array2[j]; + j++; + index++; + } + return newArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int oldArrLength = oldArray.length; + int newArrLength = oldArrLength + size; + int[] newArray = new int[newArrLength]; + System.arraycopy(oldArray,0,newArray,0,newArrLength); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] fibonacci = new int[max]; + if (max <=1) { + return null; + } else if (max == 2) { + fibonacci[0] = 1; + fibonacci[1] = 1; + } else { + for (int i =2;i parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + View view = new View(); + SAXReader reader = new SAXReader(); + String classValue = ""; + + + try { + Document document= reader.read(new File("src/com/coderising/litestruts/struts.xml")); + //获取根节点 + Element rootElement = document.getRootElement(); + //根节点属性 + List elements = rootElement.elements(); + Element actionElement = null; + for (Element element : elements) { + Attribute actionNameAttr = element.attribute("name"); + if (actionNameAttr.getValue().equals(actionName)) { + Attribute classAttr = element.attribute("class"); + classValue = classAttr.getValue(); + actionElement = element; + } + } + + + + Class newClass = Class.forName(classValue); + Object function = newClass.newInstance(); + Field[] fields = newClass.getDeclaredFields(); + String jsp = ""; + + Set> entrySet = parameters.entrySet(); + for (Map.Entry entry:entrySet) { + String name = (String) entry.getKey(); + String password = (String) entry.getValue(); + + Method setName = newClass.getDeclaredMethod("setName", String.class); + setName.invoke(function, name); + + Method setPassword = newClass.getDeclaredMethod("setPassword", String.class); + setPassword.invoke(function, password); + + Method executeMethod = newClass.getDeclaredMethod("execute"); + executeMethod.invoke(function); + + List resultElements = actionElement.elements(); + for(Element e:resultElements) { + Attribute resultNameAttr = e.attribute("name"); + if ( resultNameAttr.getValue().equals(executeMethod.invoke(function))){ + jsp = e.getText(); + break; + } + } + } + + HashMap hashMap = new HashMap<>(); + for (Field field : fields) { + Method getMessage = newClass.getDeclaredMethod("getMessage"); + Object message = getMessage.invoke(function); + hashMap.put(field.getName(), message); + } + + view.setParameters(hashMap); + view.setJsp(jsp); + + + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } + +} diff --git a/group05/183127807/HomeWork0305/src/com/coderising/litestruts/StrutsTest.java b/group05/183127807/HomeWork0305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group05/183127807/HomeWork0305/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/group05/183127807/HomeWork0305/src/com/coderising/litestruts/View.java b/group05/183127807/HomeWork0305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group05/183127807/HomeWork0305/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/group05/183127807/HomeWork0305/src/com/coderising/litestruts/struts.xml b/group05/183127807/HomeWork0305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0325783136 --- /dev/null +++ b/group05/183127807/HomeWork0305/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/group05/284422826/.gitignore b/group05/284422826/.gitignore new file mode 100644 index 0000000000..a906dbdd56 --- /dev/null +++ b/group05/284422826/.gitignore @@ -0,0 +1,11 @@ +/bin/ +/lib/ +/out/ +*.class +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* diff --git a/group05/284422826/src/com/coderising/array/ArrayUtil.java b/group05/284422826/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..440a930be0 --- /dev/null +++ b/group05/284422826/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,268 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayUtil { + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public int[] reverseArray(int[] origin) { + if (origin == null || 0 == origin.length) { + throw new NullPointerException(); + } + + int N = origin.length; + for (int i = 0; i < N / 2; i++) { + int temp = origin[i]; + origin[i] = origin[N - 1 - i]; + origin[N - 1 - i] = 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) { + if (oldArray == null || 0 == oldArray.length) { + throw new NullPointerException(); + } + + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + count++; + } + } + + int[] newArray = new int[count]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + if (array1.length == 0 && array2.length == 0) { + throw new NullPointerException(); + } else if (array1.length == 0) { + return array2; + } else if (array2.length == 0) { + return array1; + } + int count = array1.length + array2.length; + for (int i = 0; i < array1.length; i++) { + for (int j = 0; j < array2.length; j++) { + if (array1[i] == array2[j]) { + count--; + } + } + } + int[] newArray = new int[count]; + int m = 0, n = 0; + for (int i = 0; i < count; i++) { + if (m == array1.length) { + System.arraycopy(array2, n, newArray, i, array2.length - n); + break; + } + + if (n == array2.length) { + System.arraycopy(array1, m, newArray, i, array1.length - m); + break; + } + + if (array1[m] < array2[n]) { + newArray[i] = array1[m]; + m++; + } else if (array1[m] == array2[n]) { + newArray[i] = array1[m]; + m++; + n++; + } else { + newArray[i] = array2[n]; + n++; + } + } + return newArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + if (oldArray == null || 0 == oldArray.length) { + int[] array = new int[size]; + return array; + } + int[] target = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, target, 0, oldArray.length); + 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 int[] fibonacci(int max) { + if (0 == max) { + throw new IllegalArgumentException(); + } else if (1 == max) { + int[] array = {}; + return array; + } + + int length = 0; + List list = new ArrayList<>(); + for (int i = 1; i < max; i++) { + if (fibo(i) < max) { + length++; + list.add(fibo(i)); + } else { + break; + } + } + + int[] array = new int[length]; + for (int i = 0; i < length; i++) { + array[i] = list.get(i); + } + return array; + } + + + public int fibo(int N) { + if (N <= 2) { + return 1; + } else { + return fibo(N - 1) + fibo(N - 2); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + List list = new ArrayList<>(); + + int count = 0; + for (int i = 0; i < max; i++) { + if (isPrime(i)) { + count++; + list.add(i); + } + } + + int[] array = new int[count]; + for (int i = 0; i < count; i++) { + array[i] = list.get(i); + } + + return array; + } + + public boolean isPrime(int N) { + if (N < 2) { + return false; + } + + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int count = 0; + List list = new ArrayList<>(); + for (int i = 1; i < max; i++) { + if (isPerfectNumber(i)) { + count++; + list.add(i); + } + } + + int[] array = new int[count]; + for (int i = 0; i < count; i++) { + array[i] = list.get(i); + } + return array; + } + + public boolean isPerfectNumber(int N) { + int sum = 0; + for (int i = 1; i < N; i++) { + if (N % i == 0) { + sum += i; + } + } + return sum == N; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length - 1; i++) { + sb.append(array[i]).append(seperator); + } + sb.append(array[array.length - 1]); + return sb.toString(); + } +} diff --git a/group05/284422826/src/com/coderising/array/ArrayUtilTest.java b/group05/284422826/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..0b37ac11fb --- /dev/null +++ b/group05/284422826/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,82 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ArrayUtilTest { + private ArrayUtil arrayUtil = null; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void reverseArray() throws Exception { + int[] a = {7, 9, 30, 3}; + int[] b = {3, 30, 9, 7}; + assertArrayEquals(arrayUtil.reverseArray(a), b); + } + + @Test + public void removeZero() throws Exception { + int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int[] newArray = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + assertArrayEquals(arrayUtil.removeZero(oldArr), newArray); + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] newArray = {3, 4, 5, 6, 7, 8}; + assertArrayEquals(arrayUtil.merge(a1, a2), newArray); + } + + @Test + public void grow() throws Exception { + int[] oldArray = {2, 3, 6}; + int size = 3; + int[] newArray = {2, 3, 6, 0, 0, 0}; + assertArrayEquals(arrayUtil.grow(oldArray, size), newArray); + } + + @Test + public void fibonacci() throws Exception { + int max = 15; + int[] array = {1, 1, 2, 3, 5, 8, 13}; + assertArrayEquals(arrayUtil.fibonacci(max), array); + } + + @Test + public void getPrimes() throws Exception { + int[] array = {2, 3, 5, 7, 11, 13, 17, 19}; + int max = 23; + assertArrayEquals(arrayUtil.getPrimes(max), array); + } + + @Test + public void getPerfectNumbers() throws Exception { + int max = 100; + int[] array = {6, 28}; + assertArrayEquals(arrayUtil.getPerfectNumbers(max), array); + } + + @Test + public void join() throws Exception { + int[] array = {3, 8, 9}; + String seperator = "-"; + String str = "3-8-9"; + Assert.assertEquals(arrayUtil.join(array, seperator), str); + } + +} \ No newline at end of file diff --git a/group05/284422826/src/com/coderising/litestruts/LoginAction.java b/group05/284422826/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group05/284422826/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/group05/284422826/src/com/coderising/litestruts/Struts.java b/group05/284422826/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..4d9004db93 --- /dev/null +++ b/group05/284422826/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,143 @@ +package com.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.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + + +public class Struts { + private static String className = null; + + /* 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + public static View runAction(String actionName, Map parameters) { + View view = new View(); + Element element = null; + + element = parseXml(actionName); + Class clazz = null; + Object obj = null; + try { + clazz = Class.forName(className); + obj = clazz.newInstance(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + + if (setValue(parameters, clazz, obj)) return null; + + String result = null; + + try { + Method method = clazz.getDeclaredMethod("execute"); + result = (String) method.invoke(obj); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + Map map = new HashMap(); + Field[] field = clazz.getDeclaredFields(); + try { + for (Field f : field) { + f.setAccessible(true); + String fieldName = f.toString().substring(f.toString().lastIndexOf(".")+1); + map.put(fieldName, f.get(obj)); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + view.setParameters(map); + view.setJsp(getResultString(result, element)); + + return view; + } + + private static boolean setValue(Map parameters, Class clazz, Object obj) { + for (Map.Entry entry : parameters.entrySet()) { + String name = entry.getKey(); + String value = entry.getValue(); + + if (name == null || "".equals(name)) { + System.out.println("属性名称不能为空!"); + return true; + } + System.out.println("Key = " + name + ", Value = " + value); + try { + Field field = clazz.getDeclaredField(name); + field.setAccessible(true); + field.set(obj, value); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return false; + } + + private static Element parseXml(String name) { + Element el = null; + SAXReader reader = new SAXReader(); + try { + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + Iterator it = root.elementIterator(); + while (it.hasNext()) { + Element element = ((Element) it.next()); + if (name.equals(element.attributeValue("name"))) { + className = element.attributeValue("class"); + el = element; + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } + + return el; + } + + private static String getResultString(String name, Element element) { + String result = null; + Iterator iterator = element.elementIterator(); + while (iterator.hasNext()) { + Element e = ((Element) iterator.next()); + if (name.equals(e.attributeValue("name"))) { + result = e.getText(); + } + } + return result; + } + +} diff --git a/group05/284422826/src/com/coderising/litestruts/StrutsTest.java b/group05/284422826/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group05/284422826/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/group05/284422826/src/com/coderising/litestruts/View.java b/group05/284422826/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group05/284422826/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/group05/284422826/src/com/coderising/litestruts/struts.xml b/group05/284422826/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a7cb57e188 --- /dev/null +++ b/group05/284422826/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/group05/284422826/src/com/coding2017/basic/ArrayList.java b/group05/284422826/src/com/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..3d2182ae5a --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/ArrayList.java @@ -0,0 +1,130 @@ +package com.coding2017.basic; + +/** + * 功能:实现ArrayList. + * @author zhanglifeng. + */ +public class ArrayList implements List { + private int size = 0; //当前数组大小 + + private Object[] elementData = new Object[5]; //初始数组 + + /** + * 将对象o添加到ArrayList中. + * @param o:需要添加的对象. + */ + public void add(Object o) { + ensureCapacity(size + 1); //确保数组的容量可以装的下size + 1个元素,如果不够则扩容 + + elementData[size] = o; //将o添加到数组中 + size++; //数组大小增加1 + } + + /** + * 将对象o添加到ArrayList的指定位置. + * @param index: 指定位置. + * @param o: 需要添加的对象. + */ + public void add(int index, Object o) { + rangeCheck(index); //判断指定的位置index是否合法 + + ensureCapacity(size + 1); //确保数组的容量可以装的下size + 1个元素,如果不够则扩容 + + System.arraycopy(elementData, index, elementData, index + 1, size - index); //将index位置到结束位置所有的数组往后移动一个位置 + elementData[index] = o; //将对象o添加到index位置 + size++;//数组大小增加1 + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + + if (index != elementData.length - 1) { + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + } + + size--; + return elementData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private void ensureCapacity(int number) { + if (number > elementData.length) { + elementData = grow(elementData, 1); + } + } + + public Object[] grow(Object[] src, int step) { + Object[] target = new Object[src.length + step]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public void rangeCheck(int index){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + private class ArrayListIterator implements Iterator { + ArrayList arrayList = null; + int current = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + current++; + return current > arrayList.size() ? false : true; + } + + @Override + public Object next() { + return elementData[current]; + } + + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + arrayList.add("s1"); + arrayList.add("s2"); + arrayList.add("s3"); + arrayList.add("s4"); + arrayList.add(3, "s33"); + arrayList.add("s5"); + + System.out.println(arrayList.size()); + + System.out.println(arrayList.get(2)); + + arrayList.remove(3); + + System.out.println(arrayList.size()); + + arrayList.add("s1"); + System.out.println(arrayList.size()); + arrayList.remove(5); + System.out.println(arrayList.size()); + + Iterator it = arrayList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + } + +} diff --git a/group05/284422826/src/com/coding2017/basic/BinaryTreeNode.java b/group05/284422826/src/com/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..f4ce9f561c --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.coding2017.basic; + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + this.data = data; + left = null; + right = null; + } + + public Object getData() { + return this.data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return this.left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return this.right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/284422826/src/com/coding2017/basic/Iterator.java b/group05/284422826/src/com/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..19e214cfbb --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/284422826/src/com/coding2017/basic/LinkedList.java b/group05/284422826/src/com/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..f0ab5d7969 --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/LinkedList.java @@ -0,0 +1,168 @@ +package com.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * 功能:实现LinkedList. + * @author zhanglifeng. + */ +public class LinkedList implements List { + private Node head, tail; + private int size; + + private Node getNodeByIndex(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + Node current = head; + for (int i = 0; i < size && current != null; i++, current = current.next) { + if (i == index) { + return current; + } + } + return null; + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + + if (0 == index) { + addFirst(o); + }else{ + Node node = getNodeByIndex(index - 1); + node.next = new Node(o, node.next); + size ++; + } + } + + public Object get(int index) { + return getNodeByIndex(index).data; + } + + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + + if(0 == index){ + return removeFirst(); + }else if(size - 1 == index){ + return removeLast(); + }else{ + Node node = getNodeByIndex(index); + Node preNode = getNodeByIndex(index - 1); + preNode.next = node.next; + size --; + return node.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node currentHead = head; + Node newNode = new Node(o, currentHead); + head = newNode; + if(currentHead == null){ + tail = newNode; + } + + size++; + } + + public void addLast(Object o) { + Node currentTail = tail; + Node newNode = new Node(o, null); + tail = newNode; + if(currentTail == null){ + head = newNode; + }else { + currentTail.next = newNode; + } + size++; + } + + public Object removeFirst() { + if(head == null){ + throw new NoSuchElementException(); + } + Node node = new Node(head.data, null); + head = head.next; + size --; + return node.data; + } + + public Object removeLast() { + if(tail == null){ + throw new NoSuchElementException(); + } + Node node = getNodeByIndex(size - 1); + node.next = null; + size --; + return node.data; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + LinkedList linkedList = null; + private int current = 0; + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + return linkedList.get(current ++); + } + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add("s1"); + linkedList.add("s2"); + linkedList.add("s3"); + linkedList.addFirst("s0"); + linkedList.addLast("s4"); + + Iterator it = linkedList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + System.out.println("第3个元素:" + linkedList.get(3)); + + System.out.println(linkedList.removeFirst()); + System.out.println(linkedList.size()); + System.out.println("Last element:" + linkedList.removeLast()); + System.out.println(linkedList.size()); + System.out.println("第2个元素:" + linkedList.remove(2)); + System.out.println(linkedList.size()); + } +} diff --git a/group05/284422826/src/com/coding2017/basic/List.java b/group05/284422826/src/com/coding2017/basic/List.java new file mode 100644 index 0000000000..25c197cc18 --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.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/group05/284422826/src/com/coding2017/basic/Queue.java b/group05/284422826/src/com/coding2017/basic/Queue.java new file mode 100644 index 0000000000..57d63f43bf --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/Queue.java @@ -0,0 +1,25 @@ +package com.coding2017.basic; + +import java.util.EmptyStackException; + +public class Queue { + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group05/284422826/src/com/coding2017/basic/Stack.java b/group05/284422826/src/com/coding2017/basic/Stack.java new file mode 100644 index 0000000000..16e72a3942 --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.coding2017.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0); + } + + public Object pop(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group05/289326186/.classpath b/group05/289326186/.classpath new file mode 100644 index 0000000000..fe22904a9e --- /dev/null +++ b/group05/289326186/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group05/289326186/.gitignore b/group05/289326186/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/289326186/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/289326186/.project b/group05/289326186/.project new file mode 100644 index 0000000000..5364d34f0e --- /dev/null +++ b/group05/289326186/.project @@ -0,0 +1,23 @@ + + + work + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/group05/289326186/.settings/org.eclipse.jdt.core.prefs b/group05/289326186/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..0c68a61dca --- /dev/null +++ b/group05/289326186/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +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.compliance=1.8 +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/group05/289326186/.settings/org.eclipse.wst.common.project.facet.core.xml b/group05/289326186/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000000..f4ef8aa0a5 --- /dev/null +++ b/group05/289326186/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/group05/289326186/src/com/coderising/array/ArrayUtil.java b/group05/289326186/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..628bb4564c --- /dev/null +++ b/group05/289326186/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,270 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public int[] reverseArray(int[] origin) { + int[] array = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + array[i] = origin[origin.length - 1 - i]; + } + return array; + } + + /** + * 现在有如下的一个数组: 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 length = 0; + for (int arr : oldArray) { + if (arr != 0) { + length++; + } + } + int[] newArray = new int[length]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + public int[] merge(int[] array1, int[] array2) { + Set set = new TreeSet(); + + List list1 = new ArrayList(); + List list2 = new ArrayList(); + for (int i = 0; i < array1.length; i++) { + list1.add(array1[i]); + } + for (int i = 0; i < array2.length; i++) { + list1.add(array2[i]); + } + set.addAll(list1); + set.addAll(list2); + int length = set.size(); + int[] array = new int[length]; + int i = 0; + for (int a : set) { + array[i] = a; + i++; + } + return array; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int length = oldArray.length + size; + int[] newArray = new int[length]; + for (int i = 0; i < newArray.length; i++) { + if (i > oldArray.length - 1) { + newArray[i] = 0; + } else { + newArray[i] = oldArray[i]; + } + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max < 2) { + int[] array = {}; + return array; + } + List list = new ArrayList(); + for (int i = 1; i < max; i++) { + System.out.println("iiiii:" + getFibonacci(i)); + if (max < getFibonacci(i)) { + break; + } else { + list.add(getFibonacci(i)); + } + } + int[] array = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + array[i] = list.get(i); + } + return array; + } + + /** + * 获取斐波那契数列 + * + * @param n + * @return + */ + private int getFibonacci(int n) { + if (n <= 2) { + return 1; + } else { + return getFibonacci(n - 1) + getFibonacci(n - 2); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + List list = new ArrayList(); + for (int i = 1; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + int[] array = new int[list.size()]; + for (int i = 0; i < array.length; i++) { + array[i] = list.get(i); + } + return array; + } + + /** + * 判断一个数是否为素数 + * + * @param a + * @return + */ + private boolean isPrime(int a) { + if (a < 2) + return false; + for (int i = 2; i < a; i++) { + if (a % i == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + List list = new ArrayList(); + int sum = 0; + for(int i=1; i parameters) throws Exception { + // 0. 读取配置文件struts.xml + // 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + // 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + // ("name"="test" , "password"="1234") , + // 那就应该调用 setName和setPassword方法 + // + // 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + // + // 3. 通过反射找到对象的所有getter方法(例如 getMessage), + // 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + // 放到View对象的parameters + // + // 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + // 放到View对象的jsp字段中。 + View view = new View(); + // 0. 读取配置文件struts.xml + SAXReader reader = new SAXReader(); + // 读取文件 转换成Document + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + // 获取根节点元素对象 + Element root = document.getRootElement(); + //根节点的元素 + Iterator it = root.elementIterator(); + while(it.hasNext()){ + Element e = (Element) it.next(); + if(actionName.equals(e.attributeValue("name"))){ + // 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + String className = e.attributeValue("class"); + Class clazz = Class.forName(className); + Object obj = clazz.newInstance(); + //获取类的所有属性 + BeanInfo beanInfo = Introspector.getBeanInfo(clazz); + PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); + for(PropertyDescriptor pd : pds){ + for (Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + if(pd.getName().equals(key)){ + Method setMethod = pd.getWriteMethod();//获得set方法 + setMethod .invoke(obj, entry.getValue());//调用 + break; + } + } + } + //2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method method = clazz.getMethod("execute", null); + Object result = method.invoke(obj); + Map map = new HashMap(); + if("success".equals(result)){ + map.put("message", "login successful"); + }else{ + map.put("message", "login failed,please check your user/pwd"); + } + // 3. 通过反射找到对象的所有getter方法(例如 getMessage), + // 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + // 放到View对象的parameters + for(PropertyDescriptor pd : pds){ + for (Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + if(pd.getName().equals(key)){ + Method getMethod = pd.getReadMethod();//获得get方法 + Object getresult = getMethod .invoke(obj);//调用 + map.put(pd.getName(), getresult.toString()); + break; + } + } + } + view.setParameters(map); + //遍历action的子元素 + Iterator it2 = e.elementIterator(); + while(it2.hasNext()){ + Element resultEle = (Element) it2.next(); + if(resultEle.attributeValue("name").equals(result)){ + view.setJsp(resultEle.getText()); + break; + } + } + } + } + return view; + } + + public static void main(String[] args) throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = runAction(actionName, params); + System.out.println("view jsp:"+view.getJsp()); + System.out.println("view param:"+view.getParameters()); + } + +} diff --git a/group05/289326186/src/com/coderising/litestruts/StrutsTest.java b/group05/289326186/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..c4e1f588d0 --- /dev/null +++ b/group05/289326186/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group05/289326186/src/com/coderising/litestruts/View.java b/group05/289326186/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group05/289326186/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/group05/289326186/src/com/coderising/litestruts/struts.xml b/group05/289326186/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group05/289326186/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/group05/351592484/.gitignore b/group05/351592484/.gitignore new file mode 100644 index 0000000000..0aca6d5fe8 --- /dev/null +++ b/group05/351592484/.gitignore @@ -0,0 +1,9 @@ +/bin/ +*.class +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* \ No newline at end of file diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..e0fefc0ab9 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java @@ -0,0 +1,132 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.*; + +/** + * 功能:实现ArrayList. + * @author zhanglifeng. + */ +public class ArrayList implements List { + private int size = 0; //当前数组大小 + + private Object[] elementData = new Object[5]; //初始数组 + + /** + * 将对象o添加到ArrayList中. + * @param o:需要添加的对象. + */ + public void add(Object o) { + ensureCapacity(size + 1); //确保数组的容量可以装的下size + 1个元素,如果不够则扩容 + + elementData[size] = o; //将o添加到数组中 + size++; //数组大小增加1 + } + + /** + * 将对象o添加到ArrayList的指定位置. + * @param index: 指定位置. + * @param o: 需要添加的对象. + */ + public void add(int index, Object o) { + rangeCheck(index); //判断指定的位置index是否合法 + + ensureCapacity(size + 1); //确保数组的容量可以装的下size + 1个元素,如果不够则扩容 + + System.arraycopy(elementData, index, elementData, index + 1, size - index); //将index位置到结束位置所有的数组往后移动一个位置 + elementData[index] = o; //将对象o添加到index位置 + size++;//数组大小增加1 + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + + if (index != elementData.length - 1) { + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + } + + size--; + return elementData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private void ensureCapacity(int number) { + if (number > elementData.length) { + elementData = grow(elementData, 1); + } + } + + public Object[] grow(Object[] src, int step) { + Object[] target = new Object[src.length + step]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public void rangeCheck(int index){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + private class ArrayListIterator implements Iterator { + ArrayList arrayList = null; + int current = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + current++; + return current > arrayList.size() ? false : true; + } + + @Override + public Object next() { + return elementData[current]; + } + + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + arrayList.add("s1"); + arrayList.add("s2"); + arrayList.add("s3"); + arrayList.add("s4"); + arrayList.add(3, "s33"); + arrayList.add("s5"); + + System.out.println(arrayList.size()); + + System.out.println(arrayList.get(2)); + + arrayList.remove(3); + + System.out.println(arrayList.size()); + + arrayList.add("s1"); + System.out.println(arrayList.size()); + arrayList.remove(5); + System.out.println(arrayList.size()); + + Iterator it = arrayList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + } + +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..772f30c092 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.github.zhanglifeng.coding2017.basic; + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + this.data = data; + left = null; + right = null; + } + + public Object getData() { + return this.data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return this.left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return this.right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..288204f51f --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.github.zhanglifeng.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..c93a677bfd --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java @@ -0,0 +1,168 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * 功能:实现LinkedList. + * @author zhanglifeng. + */ +public class LinkedList implements List { + private Node head, tail; + private int size; + + private Node getNodeByIndex(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + Node current = head; + for (int i = 0; i < size && current != null; i++, current = current.next) { + if (i == index) { + return current; + } + } + return null; + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + + if (0 == index) { + addFirst(o); + }else{ + Node node = getNodeByIndex(index - 1); + node.next = new Node(o, node.next); + size ++; + } + } + + public Object get(int index) { + return getNodeByIndex(index).data; + } + + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("线性表索引越界"); + } + + if(0 == index){ + return removeFirst(); + }else if(size - 1 == index){ + return removeLast(); + }else{ + Node node = getNodeByIndex(index); + Node preNode = getNodeByIndex(index - 1); + preNode.next = node.next; + size --; + return node.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node currentHead = head; + Node newNode = new Node(o, currentHead); + head = newNode; + if(currentHead == null){ + tail = newNode; + } + + size++; + } + + public void addLast(Object o) { + Node currentTail = tail; + Node newNode = new Node(o, null); + tail = newNode; + if(currentTail == null){ + head = newNode; + }else { + currentTail.next = newNode; + } + size++; + } + + public Object removeFirst() { + if(head == null){ + throw new NoSuchElementException(); + } + Node node = new Node(head.data, null); + head = head.next; + size --; + return node.data; + } + + public Object removeLast() { + if(tail == null){ + throw new NoSuchElementException(); + } + Node node = getNodeByIndex(size - 1); + node.next = null; + size --; + return node.data; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + LinkedList linkedList = null; + private int current = 0; + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + return linkedList.get(current ++); + } + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add("s1"); + linkedList.add("s2"); + linkedList.add("s3"); + linkedList.addFirst("s0"); + linkedList.addLast("s4"); + + Iterator it = linkedList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + System.out.println("第3个元素:" + linkedList.get(3)); + + System.out.println(linkedList.removeFirst()); + System.out.println(linkedList.size()); + System.out.println("Last element:" + linkedList.removeLast()); + System.out.println(linkedList.size()); + System.out.println("第2个元素:" + linkedList.remove(2)); + System.out.println(linkedList.size()); + } +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java new file mode 100644 index 0000000000..e6f5743399 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.zhanglifeng.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/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java new file mode 100644 index 0000000000..42ef512321 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java @@ -0,0 +1,25 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.EmptyStackException; + +public class Queue { + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java new file mode 100644 index 0000000000..0761bdd9e7 --- /dev/null +++ b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.github.zhanglifeng.coding2017.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0); + } + + public Object pop(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group05/371492887/task_01/.classpath b/group05/371492887/task_01/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group05/371492887/task_01/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group05/371492887/task_01/.gitignore b/group05/371492887/task_01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/371492887/task_01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/371492887/task_01/.project b/group05/371492887/task_01/.project new file mode 100644 index 0000000000..eca593c703 --- /dev/null +++ b/group05/371492887/task_01/.project @@ -0,0 +1,17 @@ + + + task_01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java b/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java new file mode 100644 index 0000000000..848b4fafe9 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/ArrayListTest.java @@ -0,0 +1,95 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.ArrayList; +import com.nitasty.util.Iterator; + +public class ArrayListTest { + + private ArrayList list; + + @Before + public void init(){ + list=new ArrayList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + } + + @Test + public void testAddObject() { + list.add(100); + Assert.assertEquals(101, list.size()); + } + + @Test + public void testAddIntObject() { + list.add(3,"test"); + Assert.assertEquals("test", list.get(3)); + } + + @Test + public void testRemoveInt() { + list.add(3,"test"); + list.remove(3); + Assert.assertEquals(3, list.get(3)); + } + + @Test + public void testRemoveObject() { + list.add(0,"test"); + list.remove("test"); + Assert.assertEquals(0, list.get(0)); + } + + + @Test + public void testIsEmpty() { + list.clear(); + Assert.assertEquals(true, list.isEmpty()); + } + + @Test + public void testContains() { + Assert.assertEquals(false, list.contains("test")); + list.add("test"); + Assert.assertEquals(true, list.contains("test")); + } + + + + @Test + public void testSet() { + Assert.assertEquals(true, list.contains(3)); + list.set(3, "test"); + Assert.assertEquals(true, list.contains("test")); + Assert.assertEquals(false, list.contains(3)); + } + + @Test + public void testIndexOf() { + list.set(3, "test"); + Assert.assertEquals(3, list.indexOf("test")); + } + + @Test + public void testLastIndexOf() { + list.set(3, "test"); + list.set(33, "test"); + Assert.assertEquals(33, list.lastIndexOf("test")); + } + + @Test + public void testHasNext(){ + int i=0; + for(Iterator it=list.iterator();it.hasNext();i++){ + Assert.assertEquals(i, it.next()); +// System.out.println(it.next()); + } + } +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java b/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java new file mode 100644 index 0000000000..abb27b5691 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/BinaryTreeTest.java @@ -0,0 +1,66 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.BinaryTree; + +public class BinaryTreeTest { + + BinaryTree tree; + + @Before + public void init(){ + tree=new BinaryTree(); + tree.insert(5); + tree.insert(3); + tree.insert(8); + tree.insert(2); + tree.insert(7); + tree.insert(9); + tree.insert(1); + tree.insert(4); + tree.insert(10); + tree.insert(6); + } + + @Test + public void testMakeEmpty() { + tree.makeEmpty(); + Assert.assertEquals(true, tree.isEmpty()); + } + + @Test + public void testGetHeight() { + Assert.assertEquals(3, tree.getHeight()); + } + + @Test + public void testContains() { + for (int i = 1; i < 11; i++) { + Assert.assertEquals(true, tree.contains(i)); + } + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin()); + } + + @Test + public void testFindMax() { + Assert.assertEquals(10, tree.findMax()); + } + + + @Test + public void testRemove() { + tree.remove(3); + Assert.assertEquals(false, tree.contains(3)); + } + + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java b/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java new file mode 100644 index 0000000000..dcbe6353c3 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/LinkedListTest.java @@ -0,0 +1,142 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.ArrayList; +import com.nitasty.util.Iterator; +import com.nitasty.util.LinkedList; + +public class LinkedListTest { + + private LinkedList list; + + @Before + public void init(){ + list=new LinkedList(); + for (int i = 0; i < 100; i++) { + list.add(i); + } + } + + @Test + public void testGet() { + IndexOutOfBoundsException tx=null; + for (int i = 0; i < 100; i++) { + Assert.assertEquals(i, list.get(i)); + } + + try { + list.get(100); + } catch (IndexOutOfBoundsException e) { + tx=e; + } + Assert.assertEquals(IndexOutOfBoundsException.class,tx.getClass()); + } + + @Test + public void testRemoveInt() { + for (int i = 99; i >= 0; i--) { + Assert.assertEquals(i, list.remove(i)); + } + } + + @Test + public void testSize() { + Assert.assertEquals(100, list.size()); + } + + @Test + public void testAddFirst() { + list.addFirst(-1); + for (int i = 0; i < 101; i++) { + Assert.assertEquals(i-1, list.get(i)); + } + } + + @Test + public void testAddLast() { + + for (int i = 100; i < 1000; i++) { + list.addLast(i); + } + + for (int i = 0; i < 1000; i++) { + Assert.assertEquals(i, list.get(i)); + } + } + + @Test + public void testAddBefore() { + list.addBefore(66,list.node(3)); + Assert.assertEquals(66, list.get(3)); + } + + @Test + public void testAddAfter() { + list.addAfter(66,list.node(3)); + Assert.assertEquals(66, list.get(4)); + } + + @Test + public void testIsEmpty() { + list.clear(); + Assert.assertEquals(true, list.isEmpty()); + } + + @Test + public void testContains() { + for (int i = 0; i < 100; i++) { + Assert.assertEquals(true, list.contains(i)); + Assert.assertEquals(false, list.contains(i+100)); + } + } + + + @Test + public void testAddIntObject() { + list.add(20,"test"); + Assert.assertEquals("test", list.get(20)); + } + + @Test + public void testRemoveObject() { + list.remove(30); + Assert.assertEquals(31, list.get(30)); + } + + @Test + public void testSet() { + for (int i = 0; i < 100; i++) { + list.set(i, i+100); + Assert.assertEquals(i+100, list.get(i)); + } + } + + @Test + public void testIndexOf() { + list.set(3, "test"); + Assert.assertEquals(3, list.indexOf("test")); + } + + @Test + public void testLastIndexOf() { + list.set(3, "test"); + list.set(33, "test"); + Assert.assertEquals(33, list.lastIndexOf("test")); + } + + @Test + public void testHasNext(){ + int i=0; + + for(Iterator it=list.iterator();it.hasNext();i++){ + Assert.assertEquals(i, it.next()); +// System.out.println(it.next()); + } + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java b/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java new file mode 100644 index 0000000000..fa17263108 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/QueueTest.java @@ -0,0 +1,49 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.LinkedList; +import com.nitasty.util.Queue; + +public class QueueTest { + + Queue queue; + + @Before + public void init(){ + queue=new Queue(); + for (int i = 0; i < 100; i++) { + queue.enQueue(i); + } + } + + @Test + public void testDeQueue() { + for(int i=0; i<100;i++){ + Assert.assertEquals(i, queue.deQueue()); + } + } + + @Test + public void testIsEmpty() { + for(int i=0; i<100;i++){ + queue.deQueue(); + if(i<99) + Assert.assertEquals(false, queue.isEmpty()); + } + Assert.assertEquals(true, queue.isEmpty()); + } + + @Test + public void testSize() { + for(int i=99; i>0;i--){ + queue.deQueue(); + Assert.assertEquals(i, queue.size()); + } + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/test/StackTest.java b/group05/371492887/task_01/src/com/nitasty/test/StackTest.java new file mode 100644 index 0000000000..3ecd59219a --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/test/StackTest.java @@ -0,0 +1,51 @@ +package com.nitasty.test; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.nitasty.util.LinkedList; +import com.nitasty.util.Stack; + +public class StackTest { + + Stack stack; + + @Before + public void init(){ + stack=new Stack(); + for (int i = 0; i < 100; i++) { + stack.push(i); + } + } + + @Test + public void testPop() { + for (int i = 99; i >=0; i--) { + Assert.assertEquals(i, stack.pop()); + } + } + + @Test + public void testPeek() { + for (int i = 99; i >=0; i--) { + Assert.assertEquals(99, stack.peek()); + } + } + + @Test + public void testIsEmpty() { + for (int i = 99; i >=0; i--) { + stack.pop(); + } + Assert.assertEquals(true,stack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(100,stack.size()); + } + +} diff --git a/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java b/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java new file mode 100644 index 0000000000..88ef682cf9 --- /dev/null +++ b/group05/371492887/task_01/src/com/nitasty/util/ArrayList.java @@ -0,0 +1,307 @@ +package com.nitasty.util; + +import java.util.Arrays; +import java.util.Objects; + +public class ArrayList implements List { + + // ڲ + private static final int DEFAULT_CAPACITY = 10; + + private static final Object[] EMPTY_ELEMENTDATA = {}; + + private int size; + + private Object[] elementData; + + /** + * + * ޲γʼΪ飬ʡռ + */ + public ArrayList() { + this.elementData = EMPTY_ELEMENTDATA; + } + + /** + * + * @param initCapacity + */ + public ArrayList(int initCapacity) { + if (initCapacity > 0) { + elementData = new Object[initCapacity]; + } else if (initCapacity == 0) { + elementData = EMPTY_ELEMENTDATA; + } else { + throw new IllegalArgumentException("Ƿʼ" + initCapacity); + } + + } + + // TODO + public ArrayList(List list) { + list.toArray(); + + } + + /** + * У + * + * @param minCapacity + */ + private void ensureCapacity(int minCapacity) { + + if (elementData.length < minCapacity) { + grow(minCapacity); + } + + } + + /** + * + * + * @param minCapacity + * @return + */ + private void grow(int minCapacity) { + // + int oldCapacity = this.elementData.length; + // Ϊ1.5 + int newCapacity = oldCapacity + oldCapacity >> 1; + // СȽ + if (newCapacity < minCapacity) { + newCapacity = minCapacity; + } + // ܴintֵ + if (newCapacity > Integer.MAX_VALUE) { + newCapacity = Integer.MAX_VALUE; + } + elementData = Arrays.copyOf(elementData, newCapacity); + + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(Object o) { + return indexOf(o) >= 0; //д=ֵbug + } + + @Override + public boolean add(Object o) { + + ensureCapacity(size + 1); + + elementData[size++] = o;// sizeindex1 + + return true; + } + + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + @Override + public boolean add(int index, Object o) { + + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size + - index);// sizeը + elementData[index] = o; + return true; + } + + @Override + public boolean addAll(Object[] o) { + int numNew = o.length; + ensureCapacity(size + numNew); + System.arraycopy(o, 0, elementData, size, numNew); + size += numNew;// size + return numNew != 0; + } + + @Override + public boolean addAll(int index, Object[] o) { + rangeCheck(index); + int numNew = o.length; + ensureCapacity(size + numNew); + + int numMoved = size - index;// rangeCheckindex϶Сsize + if (numMoved > 0) + System.arraycopy(elementData, index, elementData, size + numNew, + numNew);// ԭԪƵ + System.arraycopy(o, 0, elementData, index, numNew);// ƽҪӵԪ + + size += numNew;// Ҫ˰ + return numNew != 0; + } + + @Override + public Object remove(int index) { + rangeCheck(index); + Object oldValue = elementData[index]; + + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index+1, elementData, index, + numMoved); + elementData[--size] = null;// Clear to let gc do its work + + return oldValue; + } + + @Override + public boolean remove(Object o) { + int index=this.indexOf(o); + if(index==-1){ + return false; + }else{ + this.remove(index); + return true; + } + } + + @Override + /** + * ɾlist + */ + public boolean removeAll(List list) { + Objects.requireNonNull(list); + return batchRemove(list,false); + } + + /** + * ʵremoveALlretainAll + * @param list + * @param complement + * @return + */ + private boolean batchRemove(List list, boolean complement) { + final Object[] elementData=this.elementData; + int r=0,w=0; + boolean modified=false; + try{ + for(;r= 0; i--) { + if (elementData[i] == null) { + return i; + } + } + } else { + for (int i = size-1; i >= 0; i--) { + if (o.equals(elementData[i])) { + return i; + } + } + } + return -1;// ûҵ + } + + @Override + public Iterator iterator() { + return new Itr(); + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size);//ҪֱӷelementData + } + + @Override + public void clear() { + for(int i=0; i ʵComparable + */ +public class BinaryTree> { + + private BinaryNode root; + + public BinaryTree(){ + this.root=null; + } + + public BinaryTree(BinaryNode root) { + this.root = root; + } + + public void makeEmpty(){ + root=null; + } + + public boolean isEmpty(){ + return root==null; + } + + public int getHeight(){ + return height(root); + } + + public boolean contains(E x){ + return contains(x,root); + } + + + public E findMin(){ + if(isEmpty()) + throw new NullPointerException();//׸ʲôأTODO + return (E) findMin(root).data; //ΪʲôҪתͣ + } + + + + public E findMax(){ + if(isEmpty()) + throw new NullPointerException();//׸ʲôأTODO + return (E) findMax(root).data;//ΪʲôҪתͣ + } + + public void insert(E x){ + root=insert(x,root);//ΪɶҪrootӷֵΪⷽǵݹ + } + + public void remove(E x){ + root=remove(x,root);//ΪɶҪrootӷֵΪⷽǵݹ + } + + //ӡdata + public void printTree(){ + if(isEmpty()) + System.out.println("Empty tree"); + else + printTree(root); + } + + public void printTreeStructure(){ + if(isEmpty()) + System.out.println("Empty tree"); + else + printTreeStructure(root,0); + } + + private void printTreeStructure(BinaryNode t,int i) { + StringBuffer buff=new StringBuffer(); + if(t!=null){ + for(int j=0;j= 0 && index < size; + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; // postionӵ + } + + @Override + public boolean remove(Object o) { + + return false; + } + + @Override + public boolean removeAll(List list) { + // TODO Auto-generated method stub + return false; + } + + @Override + public Object set(int index, Object o) { + checkElementIndex(index); + Node node=node(index); + Object oldValue=node.data; + node.data=o; + return oldValue; + } + + @Override + public int indexOf(Object o) { + int index = 0; + if (o == null) { + for (Node x = first; x != null; x = x.next) { // µѭʽ + if (x.data == null) + return index; + index++; + } + } else { + for (Node x = first; x != null; x = x.next) { // µѭʽ + if (o.equals(x.data)) + return index; + index++; + } + } + return -1; + } + + @Override + public int lastIndexOf(Object o) { + int index = size-1; + if (o == null) { + for (Node x = last; x != null; x = x.prev) { // µѭʽ + if (x.data == null) + return index; + index--; + } + } else { + for (Node x = last; x != null; x = x.prev) { // µѭʽ + if (o.equals(x.data)) + return index; + index--; + } + } + return -1; + } + + @Override + public Object[] toArray() { + Object[] elementData = new Object[size]; + int i = 0; + for (Node x = first; x != null; x = x.next) { + elementData[i++] = x.data; + } + return elementData; + } + + @Override + public void clear() { + // bugը + // for(Node x=first;x!=null;x=x.next){ + // x=null; + // } + + for (Node x = first; x != null;) { + Node next = x.next; + x.prev = null; + x.data = null; + x.next = null; + x = next; + } + size = 0; + first = last = null; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + private Node lastRetured; + private Node next; + int cursor; + + @Override + public boolean hasNext() { + return cursor + + + + + + + + + + + + + + + + + + + + diff --git a/group05/371492887/task_02/.gitignore b/group05/371492887/task_02/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/371492887/task_02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/371492887/task_02/.project b/group05/371492887/task_02/.project new file mode 100644 index 0000000000..1cadc86932 --- /dev/null +++ b/group05/371492887/task_02/.project @@ -0,0 +1,23 @@ + + + task_02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/group05/371492887/task_02/pom.xml b/group05/371492887/task_02/pom.xml new file mode 100644 index 0000000000..319e86e55c --- /dev/null +++ b/group05/371492887/task_02/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + java_207 + task_02 + 0.0.1-SNAPSHOT + + + + dom4j + dom4j + 1.6.1 + + + + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 3.1 + + + + + + + + \ No newline at end of file diff --git a/group05/371492887/task_02/src/com/coderising/action/LoginAction.java b/group05/371492887/task_02/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..72e00894bb --- /dev/null +++ b/group05/371492887/task_02/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group05/371492887/task_02/src/com/nitasty/array/ArrayUtil.java b/group05/371492887/task_02/src/com/nitasty/array/ArrayUtil.java new file mode 100644 index 0000000000..1138368352 --- /dev/null +++ b/group05/371492887/task_02/src/com/nitasty/array/ArrayUtil.java @@ -0,0 +1,269 @@ +package com.nitasty.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class ArrayUtil { + + + public static void main(String[] args) { + ArrayUtil util=new ArrayUtil(); + + int[] origin={7, 9 ,10, 30, 3}; + util.reverseArray(origin); + int[] oldArray={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] a1={3, 5,7,9,10}; + int[] a2={4,6,100,111,132}; + + System.out.println(Arrays.toString(origin)); + System.out.println(Arrays.toString(util.removeZero(oldArray))); + System.out.println(Arrays.toString(util.merge(a1,a2))); + System.out.println(Arrays.toString(util.grow(a1,3))); + System.out.println(Arrays.toString(util.fibonacci(100))); + System.out.println(Arrays.toString(util.getPrimes(100))); + System.out.println(Arrays.toString(util.getPerfectNumbers(1000))); + System.out.println(util.join(oldArray,"--")); + } + /** + * 给定一个整形数组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 dataBus; + int len=origin.length; + for(int i=0;i>1;i++){ + dataBus=origin[i]; + origin[i]=origin[len-i-1]; + origin[len-i-1]=dataBus; + } + } + + /** + * 现在有如下的一个数组: 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[] arrayBus=new int[oldArray.length]; + int count = 0; + //利用中间数值来剔除0 + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0) + arrayBus[count++]=oldArray[i]; + } + //返回新的数值 + int[] newArray=new int[count]; + System.arraycopy(arrayBus, 0, newArray, 0, count); + + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + //插入排序挺快的 + public int[] merge(int[] array1, int[] array2){ + int len1=array1.length; + int len2=array2.length; + List longList=new ArrayList(); + List shortList=new ArrayList(); + //数组转list,有没有简单的方法啊我擦 + if(len1>len2){ + for (int i = 0; i < len1; i++) { + longList.add(array1[i]); + } + for (int i = 0; i < len2; i++) { + shortList.add(array2[i]); + } + }else{ + for (int i = 0; i < len1; i++) { + shortList.add(array1[i]); + } + for (int i = 0; i < len2; i++) { + longList.add(array2[i]); + } + } + + //将短list中的值插入长list中 + int j=0; + for (int i = 0; i < longList.size(); i++) { + if(j==shortList.size()) + continue; + if((Integer)shortList.get(j)<(Integer)longList.get(i)){ + longList.add(i, shortList.get(j)); + j++; + }else if(((Integer)shortList.get(j)).equals((Integer)longList.get(i))){ + continue; + }else{ + if(i==(longList.size()-1)){ + longList.add(shortList.get(j)); + j++; + } + } + } + + //list再转数组···阿西吧 + int[] intArray=new int[longList.size()]; + + for (int i = 0; i < longList.size(); i++) { + intArray[i]=longList.get(i); + } + + return intArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int len=oldArray.length; + int[] newArray=new int[len+size]; + + System.arraycopy(oldArray, 0, newArray, 0, len); + + for(int i=len;i list=new ArrayList(); + for (int i = 1; i < max; i++) { + if(isPrimeNumber(i)) + list.add(i); + } + + int[] intArr=new int[list.size()]; + for (int i = 0; i < intArr.length; i++) { + intArr[i]=list.get(i); + } + return intArr; + } + + private boolean isPrimeNumber(int n) + { + if (n==2) + { + return true; + } + + if (n%2==0) + { + return false; + } + + int sqrtn=(int)Math.sqrt((double)n); + boolean flag=true; + + for (int i=3;i<=sqrtn;i+=2) + { + if (n%i==0) + { + flag=false; + } + } + return flag; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + List list=new ArrayList(); + + for (int i = 1; i < max; i++) { + int sum=0; + for (int j = 1; j < i; j++) { + if(i%j==0) + sum+=j; + } + if(sum==i) + list.add(i); + } + + int[] intArr=new int[list.size()]; + for (int i = 0; i < intArr.length; i++) { + intArr[i]=list.get(i); + } + return intArr; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer buff=new StringBuffer(); + for (int i = 0; i < array.length; i++) { + buff.append(array[i]); + if(i parameters) { + + View view=new View(); + + /* + * + * 0. 读取配置文件struts.xml + * + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + // 0.读取配置文件struts.xml + try { + // 将struts.xml转换成输入流 + InputStream in = new FileInputStream(new File( + "src/com/nitasty/litestruts/struts.xml")); + // 穿件SAXReader读取器,用于读取xml + SAXReader saxReader = new SAXReader(); + // + Document document = saxReader.read(in); + // 获取根节点对象 + Element rootElement = document.getRootElement(); + //获取action节点列表 + List elementList=rootElement.elements(); + //加载第一个action的class类 + Element login=elementList.get(0); + Class clazz=Class.forName(login.attribute("class").getStringValue()); + //new一个该class实例 + Object obj=clazz.newInstance(); + //获取name和password + String name=parameters.get("name"); + String password=parameters.get("password"); + //获取setName方法 + Method setName=clazz.getMethod("setName",String.class); + //获取setPassword方法 + Method setPassword=clazz.getMethod("setPassword",String.class); + //获取execute方法 + Method execute=clazz.getMethod("execute"); + + //执行获取的方法 + setName.invoke(obj,name); + setPassword.invoke(obj,password); + String result=(String) execute.invoke(obj); + List results=login.elements(); + for (int i = 0; i < results.size(); i++) { + if(result.equalsIgnoreCase(results.get(i).attribute(0).getStringValue())){ + view.setJsp(results.get(i).getTextTrim()); + } + } + + //获取message属性 + Field fld=clazz.getDeclaredField("message"); + //允许访问私有属性 + fld.setAccessible(true); + //获取该属性值 + String message=(String) fld.get(obj); + + //将结果返回 + Map map=new HashMap(); + map.put("message", message); + view.setParameters(map); + + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + return view; + } + + + /** + * 测试用 + * @param args + */ + public static void main(String[] args) { + // 0.读取配置文件struts.xml + try { + // 将struts.xml转换成输入流 + InputStream in = new FileInputStream(new File( + "src/com/nitasty/litestruts/struts.xml")); + // 创建SAXReader读取器,用于读取xml + SAXReader saxReader = new SAXReader(); + // + Document document = saxReader.read(in); + // 获取根节点对象 + Element rootElement = document.getRootElement(); + List elementList=rootElement.elements(); + + System.out.println(elementList.get(0).attribute("name").getStringValue()); + System.out.println(elementList.get(0).attribute("class").getStringValue()); + + + Class clazz=Class.forName(elementList.get(0).attribute("class").getStringValue()); + + Object obj=clazz.newInstance(); + + Method setName=clazz.getMethod("setName",String.class); + Method setPassword=clazz.getMethod("setPassword",String.class); + setName.invoke(obj,"test"); + setPassword.invoke(obj,"1234"); + Method execute=clazz.getMethod("execute"); + String str=(String) execute.invoke(obj); + Field fld=clazz.getDeclaredField("message"); + fld.setAccessible(true); + String message=(String) fld.get(obj); + + System.out.println(str); + System.out.println(message); + elementList.get(0).attribute("name").getStringValue(); + elementList.get(0).attribute("class").getStringValue(); + + Map map = new HashMap(); +// map = getAttributes(rootElement, map); + + // Element login=element.element("result"); + // System.out.println(login.getText()); + + // for (Iterator it=rootElement.elementIterator(); it.hasNext();) { + // System.out.println(it.next()); + // } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchFieldException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/group05/371492887/task_02/src/com/nitasty/litestruts/StrutsTest.java b/group05/371492887/task_02/src/com/nitasty/litestruts/StrutsTest.java new file mode 100644 index 0000000000..f9dc6be21f --- /dev/null +++ b/group05/371492887/task_02/src/com/nitasty/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.nitasty.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/group05/371492887/task_02/src/com/nitasty/litestruts/View.java b/group05/371492887/task_02/src/com/nitasty/litestruts/View.java new file mode 100644 index 0000000000..28f3d586f2 --- /dev/null +++ b/group05/371492887/task_02/src/com/nitasty/litestruts/View.java @@ -0,0 +1,23 @@ +package com.nitasty.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/group05/371492887/task_02/src/com/nitasty/litestruts/struts.xml b/group05/371492887/task_02/src/com/nitasty/litestruts/struts.xml new file mode 100644 index 0000000000..cafe98fdea --- /dev/null +++ b/group05/371492887/task_02/src/com/nitasty/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/group05/399258474/.classpath b/group05/399258474/.classpath new file mode 100644 index 0000000000..5dc752a7bd --- /dev/null +++ b/group05/399258474/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group05/399258474/.gitignore b/group05/399258474/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group05/399258474/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/399258474/.project b/group05/399258474/.project new file mode 100644 index 0000000000..2858b5b710 --- /dev/null +++ b/group05/399258474/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/399258474/.settings/org.eclipse.jdt.core.prefs b/group05/399258474/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group05/399258474/.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/group05/399258474/src/com/coderising/array/ArrayUtil.java b/group05/399258474/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..57efb6de72 --- /dev/null +++ b/group05/399258474/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,247 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int[] arr = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + arr[i] = origin[origin.length-i-1]; + } + for (int i = 0; i < arr.length; i++) { + origin[i] = arr[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 size = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + size ++; + } + } + int[] newArray = new int[size]; + for (int i = 0,j = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int[] totalArr = new int[array1.length + array2.length]; + for (int i = 0; i < array1.length; i++) { + totalArr[i] = array1[i]; + } + for (int j = array1.length,i = 0; i < array2.length; i++) { + totalArr[j] = array2[i]; + j++; + } + //排序 + for (int i = 0; i < totalArr.length; i++) { + for (int j = 0; j < totalArr.length-i-1; j++) { + if(totalArr[j] > totalArr[j+1]){ + int temp = totalArr[j+1]; + + totalArr[j+1] = totalArr[j]; + totalArr[j] = temp; + } + } + } + //去重 + if(totalArr.length < 2){ + return totalArr; + } + int size = 1; + for (int i = 0; i < totalArr.length-1; i++) { + if(totalArr[i] != totalArr[i+1]){ + size ++ ; + } + } + int[] newArr = new int[size]; + for (int i = 0,j = 0; i < totalArr.length-1; i++) { + if(totalArr[i] != totalArr[i+1]){ + newArr[j] = totalArr[i]; + j++; + } + } + newArr[size-1] = totalArr[totalArr.length-1]; + + return newArr; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int size = 0; + while(f(size) < max){ + size++; + } + int[] arr = new int[size]; + for (int i = 0; i < arr.length; i++) { + arr[i] = f(i); + } + return arr; + } + + public int f(int n){ + if(n == 0 || n ==1){ + return 1; + }else{ + return f(n-1)+f(n-2); + } + + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max <= 2){ + return null; + } + int size = 1; + for (int i = 3; i < max; i++) { + int n = 2; + while(n < i){ + if(i%n == 0){ + break; + } + n++; + } + if(n == i){ + size ++; + } + } + int[] arr = new int[size]; + arr[0] = 2; + for (int i = 3,j = 1; i < max; i++) { + int n = 2; + while(n < i){ + if(i%n == 0){ + break; + } + n++; + } + if(n == i){ + arr[j] = i; + j ++; + } + } + return arr; + } + + + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 5){ + return null; + } + int size = 0; + for (int i = 6; i < max; i++) { + int sum = 0; + int n = 1; + while(n < i){ + if(i % n == 0){ + sum += n; + } + n++; + } + if(sum == i){ + size ++; + } + } + int[] arr = new int[size]; + for (int i = 6,j = 0; i < max; i++) { + int sum = 0; + int n = 1; + while(n < i){ + if(i % n == 0){ + sum += n; + } + n++; + } + if(sum == i){ + arr[j] = i; + j ++; + } + } + return arr; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String str = ""; + for (int i = 0; i < array.length; i++) { + if(i != array.length-1){ + str += (array[i] + seperator); + }else{ + str += array[i]; + } + } + return str; + } + + +} diff --git a/group05/399258474/src/com/coderising/array/ArrayUtilTest.java b/group05/399258474/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..952b6cd26d --- /dev/null +++ b/group05/399258474/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,92 @@ +package com.coderising.array; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.ArrayList; + +public class ArrayUtilTest { + ArrayUtil util = new ArrayUtil(); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + System.out.println("-----------------------------"); + } + + @Test + public void reverseArraytest() { + int[] origin = new int[]{7,9,30,3}; + System.out.println(Arrays.toString(origin)); + util.reverseArray(origin); + System.out.println("数组置换:"+Arrays.toString(origin)); + } + + @Test + public void removeZeroTest(){ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArr = util.removeZero(oldArr); + System.out.println(Arrays.toString(oldArr) +"数组去0后为:"+ Arrays.toString(newArr)); + } + + @Test + public void mergeTest(){ + int[] a1 = {3,5,7,8}; + int[] a2 = {4,5,6,7}; + int[] a3 = util.merge(a1, a2); + System.out.println(Arrays.toString(a1)+Arrays.toString(a2)+"合并、排序、去重后为:"+Arrays.toString(a3)); + } + + @Test + public void growTest(){ + int[] oldArray = {2,3,6}; + int i = 3; + int[] newArray = util.grow(oldArray, i); + System.out.println(Arrays.toString(oldArray) +" 扩容 "+ i +"后为:"+Arrays.toString(newArray)); + } + + @Test + public void fibonacciTest(){ + int i = 15; + int[] arr = util.fibonacci(i); + System.out.println("斐波那契数列最大数小于"+ i +"的数组是:" + Arrays.toString(arr)); + } + + @Test + public void getPrimesTest(){ + int max = 23; + int[] arr = util.getPrimes(max); + System.out.println("最大数小于"+ max +"的素数数组是:" + Arrays.toString(arr)); + } + + @Test + public void getPerfectNumbersTest(){ + int max = 1000; + int[] arr = util.getPerfectNumbers(max); + System.out.println("最大数小于"+ max +"的完数数组是:" + Arrays.toString(arr)); + } + + @Test + public void joinTest(){ + int[] arr = {3,8,9}; + String seperator = "-"; + String str = util.join(arr, seperator); + System.out.println(Arrays.toString(arr)+"数组用\""+seperator+"\"连接后为:"+str); + } + +} + + + + + + + + + diff --git a/group05/399258474/src/com/coderising/litestruts/LoginAction.java b/group05/399258474/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coderising/litestruts/Struts.java b/group05/399258474/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..79c82a78f4 --- /dev/null +++ b/group05/399258474/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,162 @@ +package com.coderising.litestruts; + +import java.beans.PropertyDescriptor; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + SAXReader saxReader = new SAXReader(); + View view = new View(); + try { + Document document = saxReader.read(new File("src/com/coderising/litestruts/struts.xml")); + //获取根元素 + Element root = document.getRootElement(); + //根据参数获取子元素 + List listElement=root.elements();//所有一级子节点的list + Element actionElement = null; + String classStr = ""; + for(Element e:listElement){ + Attribute actionNameAttr = e.attribute("name"); + if(actionNameAttr.getValue().equals(actionName)){ + //获取属性值 + Attribute classAttr = e.attribute("class"); + classStr = classAttr.getValue(); + actionElement = e; + break; + } + } + + //通过反射创建对象 + Class clazz = Class.forName(classStr); + Object c = clazz.newInstance(); + Field[] fields = clazz.getDeclaredFields(); + //通过set方法赋值 + Set> set = parameters.entrySet(); + for(Entry entry : set){ + String key = entry.getKey(); + String val = entry.getValue(); + for(Field f :fields){ + PropertyDescriptor pd = getPropertyDescriptor(clazz,key,true); + Method setMethod = pd.getWriteMethod(); + setMethod.invoke(c, new Object[]{val}); + } + } + + //通过返回值确定jsp + String jspStr = ""; + Method executeMethod = clazz.getDeclaredMethod("execute"); + executeMethod.setAccessible(true); + String str = (String) executeMethod.invoke(c); + List resultElements = actionElement.elements(); + for(Element e:resultElements){ + Attribute resultNameAttr = e.attribute("name"); + if(resultNameAttr.getValue().equals(str)){ + jspStr = e.getText(); + break; + } + + } + + //获取所有的get方法,存入map + HashMap map = new HashMap(); + for(Field f :fields){ + PropertyDescriptor pd = getPropertyDescriptor(clazz,f.getName(),false); + Method getMethod = pd.getReadMethod(); + Object value = getMethod.invoke(c, new Object[]{}); + map.put(f.getName(), value); + } + + //确定返回的View + + view.setJsp(jspStr); + view.setParameters(map); + + + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return view; + } + + public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName,boolean hasSetMethod) { + StringBuffer sb = new StringBuffer(); + Method setMethod = null; + Method getMethod = null; + PropertyDescriptor pd = null; + try { + Field f = clazz.getDeclaredField(propertyName);//根据字段名来获取字段 + if (f!= null) { + //构建方法的后缀 + String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); + if(hasSetMethod){ + sb.append("set" + methodEnd); + setMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ f.getType() }); + } + sb.delete(0, sb.length());//清空 + sb.append("get" + methodEnd);//构建get方法 + //构建get 方法 + getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ }); + //构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中 + pd = new PropertyDescriptor(propertyName, getMethod, setMethod); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + return pd; + } + +} diff --git a/group05/399258474/src/com/coderising/litestruts/StrutsTest.java b/group05/399258474/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..56bb5b23d0 --- /dev/null +++ b/group05/399258474/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,48 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException { + 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/group05/399258474/src/com/coderising/litestruts/View.java b/group05/399258474/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coderising/litestruts/struts.xml b/group05/399258474/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coding/basic/ArrayList.java b/group05/399258474/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..78706c993f --- /dev/null +++ b/group05/399258474/src/com/coding/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + int len = elementData.length; + if(size >= len){ + Object[] new_elmentData = new Object[len*2]; + System.arraycopy(elementData, 0, new_elmentData, 0, size); + elementData = new_elmentData; + } + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + Object[] new_elementData = new Object[size+1]; + System.arraycopy(elementData, 0, new_elementData, 0, index); + System.arraycopy(elementData, index, new_elementData, index+1, size-index); + new_elementData[index] = o; + elementData = new_elementData; + size++; + } + + public Object get(int index){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >= size){ + throw new RuntimeException("下标越界"); + } + Object oldElement = elementData[index]; + if((index+1) != size){ + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + } + elementData[size-1] = null; + size --; + return oldElement; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + @Override + public String toString() { + String s = "{"; + for (int i = 0; i < size; i++) { + if(i == (size -1)){ + s += elementData[i] + "}"; + }else{ + s += elementData[i]+","; + } + } + return s; + } +} diff --git a/group05/399258474/src/com/coding/basic/BinaryTreeNode.java b/group05/399258474/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coding/basic/Iterator.java b/group05/399258474/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coding/basic/LinkedList.java b/group05/399258474/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ff69d8d341 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/LinkedList.java @@ -0,0 +1,104 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + head = new Node(new Object(),null,null); + } + + public void add(Object o){ + Node last = head; + for (int i = 0; i < size; i++) { + last = last.next; + } + Node newNode = new Node(o,null,last); + last.next = newNode; + size++; + } + public void add(int index , Object o){ + Node oldNode = getNode(index); + Node newNode = new Node(o, oldNode, oldNode.prev); + oldNode.prev.next = newNode; + oldNode.prev = newNode; + size ++; + } + public Object get(int index){ + Node node = getNode(index); + return node.data; + } + + private Node getNode(int index){ + Node n = head.next; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + public Object remove(int index){ + Node node = getNode(index); + Object o =node.data; + Node prevNode = node.prev; + Node nextNode = node.next; + prevNode.next = nextNode; + nextNode.prev = prevNode; + node.next = node.prev =null; + node.data = null; + size --; + return o; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + Object o = remove(0); + return o; + } + public Object removeLast(){ + Object o = remove(size); + return o; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + Node prev; + + public Node(Object o,Node next,Node prev){ + this.data = o; + this.next = next; + this.prev = prev; + } + } + + @Override + public String toString() { + String s = "{"; + Node n = head; + for (int i = 0; i < size; i++) { + n = n.next; + if(i == (size -1)){ + s += n.data; + }else{ + s += n.data + ","; + } + } + s += "}"; + return s; + } +} diff --git a/group05/399258474/src/com/coding/basic/List.java b/group05/399258474/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coding/basic/Queue.java b/group05/399258474/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a0bb03e6f2 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/Queue.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + if(isEmpty()){ + throw new RuntimeException("已为空"); + } + Object o = list.get(0); + list.remove(0); + return o; + } + + public boolean isEmpty(){ + if(size() == 0){ + return true; + }else{ + return false; + } + } + + public int size(){ + return list.size(); + } + + @Override + public String toString() { + return list.toString(); + } +} diff --git a/group05/399258474/src/com/coding/basic/Stack.java b/group05/399258474/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4d137996b6 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(isEmpty()){ + throw new RuntimeException("已为空"); + } + Object o = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return o; + } + + public Object peek(){ + Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + int size = elementData.size(); + if(size == 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group05/399258474/src/com/coding/basic/test/BasicTest.java b/group05/399258474/src/com/coding/basic/test/BasicTest.java new file mode 100644 index 0000000000..8febc26102 --- /dev/null +++ b/group05/399258474/src/com/coding/basic/test/BasicTest.java @@ -0,0 +1,78 @@ +package com.coding.basic.test; + +import org.junit.Test; + +import com.coding.basic.ArrayList; +import com.coding.basic.LinkedList; +import com.coding.basic.Queue; +import com.coding.basic.Stack; + +public class BasicTest { + + @Test + public void ArrayListTest() { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + System.out.println(list); + list.add(1, 99); + System.out.println(list); + list.remove(1); + System.out.println(list); + } + + @Test + public void LinkedListTest(){ + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + System.out.println(l); + l.add(1, 99); + System.out.println(l); + l.remove(1); + System.out.println(l); + System.out.println(l.size()); + } + + @Test + public void StackTest(){ + Stack s = new Stack(); + s.push(1); + s.push(2); + System.out.println(s); + if(s.isEmpty()){ + System.out.println("空"); + }else{ + System.out.println("非空"); + + } + System.out.println(s.peek()); + s.pop(); + System.out.println(s); + s.pop(); + System.out.println(s); + if(s.isEmpty()){ + System.out.println("空"); + }else{ + System.out.println("非空"); + + } + s.pop(); + } + + @Test + public void QueueTest(){ + Queue q = new Queue(); + q.enQueue(1); + q.enQueue(2); + System.out.println(q); + q.deQueue(); + System.out.println(q); + q.enQueue(3); + System.out.println(q); + System.out.println(q.size()); + } + +} diff --git a/group05/441517454/work1/.classpath b/group05/441517454/work1/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group05/441517454/work1/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/441517454/work1/.gitignore b/group05/441517454/work1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/441517454/work1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/441517454/work1/.project b/group05/441517454/work1/.project new file mode 100644 index 0000000000..49a7fabffd --- /dev/null +++ b/group05/441517454/work1/.project @@ -0,0 +1,17 @@ + + + work1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs b/group05/441517454/work1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group05/441517454/work1/.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/group05/441517454/work1/src/com/coding/basic/ArrayList.java b/group05/441517454/work1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..01671f55d8 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + if(sizeindex;i--){ + elementData[i]=elementData[i-1]; + } + elementData[index] = o; + this.size++; + } + else if(index<(this.size)&&this.size==elementData.length) + { + elementData = grow(elementData,1); + elementData[index] = o; + this.size++; + } + else{ + System.out.println("index/>sizeʧ"); + + } + + + } + + public Object get(int index){ + + if(this.size>0&&index<(this.size)) + return elementData[index]; + else{ + return null; + } + } + + public Object remove(int index){ + + if(this.size>0&&index<(this.size)) + { Object o= elementData[index]; + for(int i=index;iarrayList.size){ + return false; + }else + return true; + } + + @Override + public Object next() { + + return arrayList.get(pos-1); + }} +} diff --git a/group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java b/group05/441517454/work1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/441517454/work1/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/group05/441517454/work1/src/com/coding/basic/Iterator.java b/group05/441517454/work1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/441517454/work1/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/group05/441517454/work1/src/com/coding/basic/LinkedList.java b/group05/441517454/work1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..5eefddce8b --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,156 @@ +package com.coding.basic; + + + +public class LinkedList implements List { + + private Node head; + private Node body; + //private Node body1; + private int size = 0; + + + public void add(Object o){ + if(null == head){ + head = new Node(); + head.data = o; + head.next = null; + size++; + } +// else if(head.next == null){ +// +// head.next =new Node(); +// head.next.data = o; +// head.next.next = null; +// body=head.next; +// size++; +// } + else { + body=head; + while(!(body.next ==null)) + {body =body.next;} + body.next =new Node(); + body.next.data =o; + body.next.next =null; + size++; + } + + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + if (index1) + {body=head; + for (int i=0;ilinkedList.size){ + return false; + }else + return true; + } + + @Override + public Object next() { + + return linkedList.get(pos-1); + }} +} diff --git a/group05/441517454/work1/src/com/coding/basic/List.java b/group05/441517454/work1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group05/441517454/work1/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/group05/441517454/work1/src/com/coding/basic/Queue.java b/group05/441517454/work1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..cae520dbce --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + + if(elementData.size()>0) + + { + elementData.remove(0); + return elementData.remove(0); + } + else return null; + } + + public boolean isEmpty(){ + if(elementData.size()>0) + return false; + else return true; + + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group05/441517454/work1/src/com/coding/basic/Stack.java b/group05/441517454/work1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..40e8518b24 --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Stack.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size()>0) + + { int size =elementData.size(); + elementData.remove(size-1); + return elementData.remove(size-1);} + else return null; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + if(elementData.size()>0) + return false; + else return true; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group05/441517454/work1/src/com/coding/basic/Test1.java b/group05/441517454/work1/src/com/coding/basic/Test1.java new file mode 100644 index 0000000000..b7f644c95d --- /dev/null +++ b/group05/441517454/work1/src/com/coding/basic/Test1.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +public class Test1 { + public static void main (String[] args){ +// ArrayList arr = new ArrayList(); +// arr.add(1); +// arr.add(2); +// arr.add(3); +// arr.add(4); +// arr.remove(3); +// arr.add(6); +// arr.add(7); +// System.out.println(arr.size()+"_"); +// Iterator it =arr.iterator(); +// while(it.hasNext()){ +// System.out.println(it.next()); +// } +// LinkedList link = new LinkedList(); +// link.add(0); +// link.add(1); +// link.add(2); +// link.add(3); +// link.add(4); +// link.add(5); +// link.remove(2); +// link.addFirst(100); +// link.addLast(200); +// //link.removeFirst(); +// //link.removeLast(); +//// System.out.println(link.size()+"_"); +//// System.out.println(link.get(0)); +//// System.out.println(link.get(link.size()-1)); +//// +// +// Iterator it =link.iterator(); +// while(it.hasNext()){ +// System.out.println(it.next()); +// } + Stack st = new Stack(); + st.push(0); + st.push(1); + st.push(2); + st.push(4); + st.push(5); + st.pop(); + System.out.println(st.peek()); + } +} \ No newline at end of file diff --git a/group05/515505513/RemoteSystemsTempFiles/.project b/group05/515505513/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group05/515505513/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group05/515505513/Task01/.classpath b/group05/515505513/Task01/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group05/515505513/Task01/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/515505513/Task01/.gitignore b/group05/515505513/Task01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/515505513/Task01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/515505513/Task01/.project b/group05/515505513/Task01/.project new file mode 100644 index 0000000000..cf494f7a91 --- /dev/null +++ b/group05/515505513/Task01/.project @@ -0,0 +1,17 @@ + + + Task01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs b/group05/515505513/Task01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group05/515505513/Task01/.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/group05/515505513/Task01/src/com/coding/basic/ArrayList.java b/group05/515505513/Task01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..29fc9b98cb --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,78 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + + private int size = 0; + private static final int DEFAULT_SIZE = 100; + private Object[] elementData = new Object[DEFAULT_SIZE]; + + //添加元素 + public void add(Object o){ + add(size(),o); + } + + + public void add(int index, Object o){ + if(elementData.length==size()){ + ensureCapacity(size()*2 + 1); + } + for (int i = size; i > index; i--) + elementData[i]=elementData[i-1]; + elementData[index] = o; + size++; + } + //扩容 + public void ensureCapacity(int newCapacity){ + if(newCapacity < size){ + return; + } + Object[] oldElements = elementData; + elementData = new Object[newCapacity]; + for (int i = 0; i < size; i++) { + elementData[i] = oldElements[i]; + } + } + //返回固定下标的元素 + public Object get(int index){ + if(index<0 || index >=size){ + throw new ArrayIndexOutOfBoundsException("指定的index超过界限"); + } + return elementData[index]; + } + //删除指定位置的元素 + public Object remove(int index){ + Object removeElement = elementData[index]; + for (int i = index; i < size; i++) { + elementData[i] = elementData[i+1]; + } + size--; + return removeElement; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int current = 0; + @Override + public boolean hasNext() { + return current < size; + } + @Override + public Object next() { + if(!hasNext()){ + throw new NoSuchElementException(); + } + return elementData[current+1]; + } + } + +} diff --git a/group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java b/group05/515505513/Task01/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group05/515505513/Task01/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/group05/515505513/Task01/src/com/coding/basic/Iterator.java b/group05/515505513/Task01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group05/515505513/Task01/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/group05/515505513/Task01/src/com/coding/basic/LinkedList.java b/group05/515505513/Task01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ea3d6a07f3 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head;//指针 + private Object element;//元素 + //添加一个元素 + public void add(Object o){ + + } + //在index处添加一个元素 + 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/group05/515505513/Task01/src/com/coding/basic/List.java b/group05/515505513/Task01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group05/515505513/Task01/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/group05/515505513/Task01/src/com/coding/basic/Queue.java b/group05/515505513/Task01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..fd316ac7bf --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/Queue.java @@ -0,0 +1,49 @@ +package com.coding.basic; + +public class Queue { + private int maxSize;//队列容量 + private Object[] queue;//队列 + private int head;//队列头,可以删除 + private int tail;//队列尾,可以插入 + + + public Queue() { + this(10); + } + + public Queue(int maxSize) { + if(maxSize >=0){ + this.maxSize = maxSize; + this.queue = new Object[maxSize]; + head = tail = 0; + }else { + throw new RuntimeException("初始化大小不能小于0"); + } + } + + public void enQueue(Object o){ + if(tail == maxSize){ + throw new RuntimeException("队列已满,无法插入新的元素!"); + }else { + queue[tail++] = o; + } + } + + public Object deQueue(){ + if(isEmpty()){ + throw new RuntimeException("空队列异常!"); + }else { + Object value = queue[head]; + queue[head++] = null; + return value; + } + } + //队列是否为空 + public boolean isEmpty(){ + return head==tail?true:false; + } + + public int size(){ + return tail-head; + } +} diff --git a/group05/515505513/Task01/src/com/coding/basic/Stack.java b/group05/515505513/Task01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a85cfed9d2 --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/Stack.java @@ -0,0 +1,49 @@ +package com.coding.basic; + +public class Stack { + private Object[] data; + private int capacity; + private int size; + public Stack(){ + capacity = 16; + size = 0; + data = new Object[capacity]; + } + public void push(Object o){ + if(size < capacity){ + data[size++] = o; + }else { + ensureCapacity(); + data[size++] = o; + } + } + + private void ensureCapacity() { + capacity = capacity*2; + } + public Object pop(){ + if(size > 0){ + System.out.println(data[size-1]); + //data[size--] = null; + }else { + System.out.println("Empty stack"); + } + size--; + return data[size]; + } + + public Object peek(){ + if(size>0){ + return data[size-1]; + }else { + return null; + } + } + public boolean isEmpty(){ + + return size==0; + } + public int size(){ + return size; + } +} diff --git a/group05/515505513/Task01/src/com/coding/basic/TestExample.java b/group05/515505513/Task01/src/com/coding/basic/TestExample.java new file mode 100644 index 0000000000..59bfa47a3c --- /dev/null +++ b/group05/515505513/Task01/src/com/coding/basic/TestExample.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class TestExample { + + public static void main(String[] args) { + /*Stack mystack = new Stack(); + for (int i = 0; i < 10; i++) { + mystack.push(i); + } + for (int i = 0; i < 10; i++) { + mystack.pop(); + System.out.println("==="+mystack.peek()); + }*/ + + /*Test Queue + Queue myQqueue = new Queue(10); + for (int i = 0; i < 10; i++) { + myQqueue.enQueue(i); + } + for (int i = 0; i < 10; i++) { + System.out.println(myQqueue.deQueue()); + }*/ + + + } + +} diff --git a/group05/515505513/Task02/.classpath b/group05/515505513/Task02/.classpath new file mode 100644 index 0000000000..916d837ac0 --- /dev/null +++ b/group05/515505513/Task02/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group05/515505513/Task02/.gitignore b/group05/515505513/Task02/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/515505513/Task02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/515505513/Task02/.project b/group05/515505513/Task02/.project new file mode 100644 index 0000000000..e4d9a52aa9 --- /dev/null +++ b/group05/515505513/Task02/.project @@ -0,0 +1,17 @@ + + + Task02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/515505513/Task02/.settings/org.eclipse.jdt.core.prefs b/group05/515505513/Task02/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group05/515505513/Task02/.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/group05/515505513/Task02/lib/dom4j-2.0.0.jar b/group05/515505513/Task02/lib/dom4j-2.0.0.jar new file mode 100644 index 0000000000..fd9e2ccd44 Binary files /dev/null and b/group05/515505513/Task02/lib/dom4j-2.0.0.jar differ diff --git a/group05/515505513/Task02/src/com/coderising/action/LoginAction.java b/group05/515505513/Task02/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5cdf1b78e4 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/LoginAction.java @@ -0,0 +1,45 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = 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; + } + +} diff --git a/group05/515505513/Task02/src/com/coderising/action/Struts.java b/group05/515505513/Task02/src/com/coderising/action/Struts.java new file mode 100644 index 0000000000..7a342e28bb --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/Struts.java @@ -0,0 +1,165 @@ +package com.coderising.action; +import java.beans.PropertyDescriptor; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + //0. 读取配置文件struts.xml + + SAXReader saxReader = new SAXReader(); + View view = new View(); + Element actionElement = null; + String classVal=""; + try { + Document read = saxReader.read(new File("src/com/coderising/action/struts.xml")); + //获得根节点 + Element rootElement = read.getRootElement(); + //获得根节点下的一级节点 + List elements = rootElement.elements(); + for (Element element : elements) { + Attribute attribute = element.attribute("name"); + //如果等于传递进来的actionName + if(attribute.getValue().equals(actionName)){ + actionElement = element; + //获得属性值 + Attribute classAttr = element.attribute("class"); + classVal = classAttr.getValue(); + } + } + +// 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) +// 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 +// ("name"="test" , "password"="1234") , +// 那就应该调用 setName和setPassword方法 + //com.coderising.action.LoginAction + + Class clazz = Class.forName(classVal); + Object c = clazz.newInstance(); + Field[] fields = clazz.getDeclaredFields(); + Set> set = parameters.entrySet(); + for (Entry entry : set) { + String key = entry.getKey(); + String value = entry.getValue(); + for (Field f : fields) { + setProperty(c, key, value); + } + } + + //2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method executeMethod = clazz.getDeclaredMethod("execute"); + executeMethod.setAccessible(true); + String jspStr = ""; + String str = (String) executeMethod.invoke(c); + List resultNameJSp = actionElement.elements(); + for (Element element : resultNameJSp) { + Attribute attribute = element.attribute("name"); + if(attribute.getValue().equals(str)){ + jspStr = element.getText(); + break; + } + } + +// 3. 通过反射找到对象的所有getter方法(例如 getMessage), +// 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , +// 放到View对象的parameters + HashMap hashMap = new HashMap<>(); + for (Field f : fields) { + Object value = getProperty(c, f.getName()); + hashMap.put(f.getName(),value); + } + +// 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, +// 放到View对象的jsp字段中。 + view.setJsp(jspStr); + //System.out.println("====="+view.getJsp()); + view.setParameters(hashMap); + //System.out.println(view.getParameters().get("message")); + + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return view; + } + + public static PropertyDescriptor getPropertyDescriper(Class clazz,String propertyName){ + //构建一个可变字符串用来构建方法名称 + StringBuffer sBuffer = new StringBuffer(); + Method setMethod = null; + Method getMethod = null; + PropertyDescriptor pd = null; + try { + //根据字段名来获取字段 + Field f = clazz.getDeclaredField(propertyName); + if(f!=null){ + //构建方法的后缀 + String methodEnd = propertyName.substring(0,1).toUpperCase()+propertyName.substring(1); + sBuffer.append("set"+methodEnd);//构建set方法 + setMethod = clazz.getDeclaredMethod(sBuffer.toString(),new Class[]{f.getType()}); + sBuffer.delete(0, sBuffer.length());//清空整个可变字符串 + sBuffer.append("get"+methodEnd);//构建get方法 + getMethod = clazz.getDeclaredMethod(sBuffer.toString(),new Class[]{}); + //构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中 + pd = new PropertyDescriptor(propertyName, getMethod, setMethod); + } + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return pd; + } + + /** + * @param obj + * @param propertyName + * @param value + */ + public static void setProperty(Object obj,String propertyName,Object value){ + //获取对象的类型 + Class clazz = obj.getClass(); + PropertyDescriptor pd = getPropertyDescriper(clazz,propertyName); + //从属性描述器中获取set方法 + Method setMethod = pd.getWriteMethod(); + try { + setMethod.invoke(obj, new Object[]{value}); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + public static Object getProperty(Object obj,String propertyName){ + Class clazz = obj.getClass(); + PropertyDescriptor pd = getPropertyDescriper(clazz,propertyName); + Method getMethod = pd.getReadMethod(); + Object value= null; + try { + value = getMethod.invoke(obj,new Object[]{}); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return value; + } + + +} diff --git a/group05/515505513/Task02/src/com/coderising/action/StrutsTest.java b/group05/515505513/Task02/src/com/coderising/action/StrutsTest.java new file mode 100644 index 0000000000..f162786054 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/StrutsTest.java @@ -0,0 +1,42 @@ +package com.coderising.action; + +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/group05/515505513/Task02/src/com/coderising/action/View.java b/group05/515505513/Task02/src/com/coderising/action/View.java new file mode 100644 index 0000000000..11cb1872e5 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/View.java @@ -0,0 +1,23 @@ +package com.coderising.action; + +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/group05/515505513/Task02/src/com/coderising/action/struts.xml b/group05/515505513/Task02/src/com/coderising/action/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/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/group05/515505513/Task02/src/com/coderising/array/ArrayUtil.java b/group05/515505513/Task02/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..eca61be119 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,221 @@ +package com.coderising.array; + +import java.util.Iterator; +import java.util.TreeSet; + +import org.junit.Test; + +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 leon1900 + */ + public void reverseArray(int[] origin){ + for (int i = 0; i < origin.length/2; i++) { + int temp = origin[origin.length-1-i]; + origin[origin.length-1-i] = origin[i]; + origin[i] = 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 leon1900 + */ + public int[] removeZero(int[] oldArray){ + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] !=0 ){ + size++; + } + } + int [] newArray = new int [size]; + for (int i = 0,j = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + newArray[j++] = oldArray[i]; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return leon1900 + */ + + public int[] merge(int[] array1, int[] array2){ + TreeSet ts = new TreeSet<>(); + for (int i = 0; i < array1.length; i++) { + ts.add(array1[i]); + } + for (int i = 0; i < array2.length; i++) { + ts.add(array2[i]); + } + int size = ts.size(); + int[] newArr = new int[size]; + int i = 0; + for (Integer integer : ts) { + newArr[i++] = integer; + } + return newArr; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return leon1900 + */ + public int[] grow(int [] oldArray, int size){ + int newsize = oldArray.length+size; + int [] newArr = new int[newsize]; + for (int i = 0; i < oldArray.length; i++) { + newArr[i] = oldArray[i]; + } + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return leon1900 + */ + public int[] fibonacci(int max){ + int size = 1; + while(fun(size) + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/578505552/pom.xml b/group05/578505552/pom.xml new file mode 100644 index 0000000000..b22dfcedd8 --- /dev/null +++ b/group05/578505552/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + + com.coding2017.yang + basic + 1.0-SNAPSHOT + jar + + basic + http://maven.apache.org + + + UTF-8 + + + + + dom4j + dom4j + 1.6.1 + + + junit + junit + 4.12 + + + diff --git a/group05/578505552/src/main/java/com/coderising/Q&A.md b/group05/578505552/src/main/java/com/coderising/Q&A.md new file mode 100644 index 0000000000..29fcbb1560 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/Q&A.md @@ -0,0 +1,5 @@ +# Struts中去获得getter或者setter方法的时候怎样比较靠谱?目前有两种思路 +## 获取Struts中所有以“get”或“set”开头的方法 +### 问题1:可能有不是getter或者setter的方法也是以“get”或者“set”开头 +## 先获取Struts中所有的成员变量的名字,再与字符串“get”或“set”拼接出getter或者setter方法名,再通过方法名用反射获取方法 +### 问题1:因为拼接方法名肯定以驼峰命名法来拼接,这就要求getter和setter方法也是以这个规则命名,虽然ide自动生成的方法是如此,但不能保存总是如此。 \ No newline at end of file diff --git a/group05/578505552/src/main/java/com/coderising/array/ArrayUtil.java b/group05/578505552/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..2c248db331 --- /dev/null +++ b/group05/578505552/src/main/java/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 void reverseArray(int[] origin){ + int length = origin.length; + int i = 0; + int j = length - 1; + while (i < j){ + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + i++; + j--; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int length = oldArray.length; + int[] newArray = new int[length]; + int j = 0; + for (int i = 0; i < length; i++) { + if (oldArray[i] != 0){ + newArray[j++] = oldArray[i]; + } + } + int[] res = new int[j]; + System.arraycopy(newArray, 0, res, 0, j); + return res; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int length1 = array1.length; + int length2 = array2.length; + int i = 0; + int j = 0; + + int[] res = new int[length1 + length2]; + int k = 0; + while (i < length1 || j < length2){ + int next = Integer.MIN_VALUE; + if (i < length1 && j < length2){ + if (array1[i] == array2[j]){ + next = array1[i]; + i++; + j++; + } else if (array1[i] < array2[j]){ + next = array1[i++]; + } else { + next = array2[j++]; + } + } else if (i < length1){ + next = array1[i++]; + } else { + next = array2[j++]; + } + + if (k == 0){ + res[k++] = next; + } else if (next > res[k-1]){ + res[k++] = next; + } + } + + int[] merged = new int[k]; + System.arraycopy(res, 0, merged, 0, k); + return merged; + } + /** + * 把一个已经存满数据的数组 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){ + throw new IllegalArgumentException("illegal size"); + } + int newLength = oldArray.length + size; + int[] newArray = new int[newLength]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + + if (max <= 1){ + return new int[0]; + } + + int[] res = new int[max]; + int i = 1; + int j = 1; + int k = 0; + res[k++] = 1; + res[k++] = 1; + + int tmp = i + j; + while (tmp < max){ + res[k++] = tmp; + i = j; + j = tmp; + tmp = i + j; + } + + int[] result = new int[k]; + System.arraycopy(res, 0, result, 0, k); + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if (max < 0){ + return new int[0]; + } + int[] res = new int[max]; + int k = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)){ + res[k++] = i; + } + } + int[] result = new int[k]; + System.arraycopy(res, 0, result, 0, k); + return result; + } + + private boolean isPrime(int num){ + + if (num < 1){ + return false; + } + for (int i = 2; i <= num / 2; i++) { + if (num % i == 0){ + return false; + } + + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + if (max < 0){ + return new int[0]; + } + int[] res = new int[max]; + int k = 0; + for (int i = 0; i < max; i++) { + if (isPerfectNumbers(i)){ + res[k++] = i; + } + } + int[] result = new int[k]; + System.arraycopy(res, 0, result, 0, k); + return result; + } + + private boolean isPerfectNumbers(int num){ + + return num == getFactorSum(num); + } + + private int getFactorSum(int num){ + if (num == 0 || num == 1){ + return -1; + } + int sum = 0; + for (int i = 1; i <= num / 2; i++) { + if (num % i == 0){ + sum += i; + } + } + return sum; + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param separator + * @return + */ + public String join(int[] array, String separator){ + + if (array.length <= 0){ + return ""; + } + + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < array.length - 1; i++) { + stringBuffer.append(String.valueOf(array[i])).append(separator); + } + stringBuffer.append(String.valueOf(array[array.length-1])); + return stringBuffer.toString(); + } + +} diff --git a/group05/578505552/src/main/java/com/coderising/litestruts/Action.java b/group05/578505552/src/main/java/com/coderising/litestruts/Action.java new file mode 100644 index 0000000000..743f452030 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/Action.java @@ -0,0 +1,38 @@ +package com.coderising.litestruts; + + +import java.util.Map; + +/** + * Created by songbao.yang on 2017/3/1. + * + */ +public class Action { + private String name; + private String className; + private Map results; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } +} diff --git a/group05/578505552/src/main/java/com/coderising/litestruts/LoginAction.java b/group05/578505552/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..b74dfa425d --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,38 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + */ +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/group05/578505552/src/main/java/com/coderising/litestruts/Struts.java b/group05/578505552/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..95b4d37a52 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,131 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + +// 0. 读取配置文件struts.xml + Action action = matchAction(parseXml("/struts.xml"), actionName); + try { + +// 1. 根据actionName找到相对应的class, 通过反射实例化(创建对象), +// 根据parameters中的数据,调用对象的setter方法 + Class clazz = Class.forName(action.getClassName()); + Object instance = clazz.newInstance(); + for (String key : parameters.keySet()){ + try { + PropertyDescriptor propertyDescriptor = new PropertyDescriptor(key, clazz); + Method setMethod = propertyDescriptor.getWriteMethod(); + setMethod.invoke(instance, parameters.get(key)); + } catch (IntrospectionException e) { + e.printStackTrace(); + } + } + +// 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method exectueMethod = null; + String result = null; + try { + exectueMethod = clazz.getMethod("execute"); + result = (String)exectueMethod.invoke(instance); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + +// 3. 通过反射找到对象的所有getter方法(例如 getMessage), +// 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , +// 放到View对象的parameters + Map hashMap = new HashMap(); + Field[] declaredFields = clazz.getDeclaredFields(); + for (Field field : declaredFields){ + String name = field.getName(); + try { + PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, clazz); + Method getMethod = propertyDescriptor.getReadMethod(); + Object res = getMethod.invoke(instance); + hashMap.put(name, res); + } catch (IntrospectionException e) { + e.printStackTrace(); + } + } + +// 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, +// 放到View对象的jsp字段中。 + View view = new View(); + view.setJsp((String)action.getResults().get(result)); + view.setParameters(hashMap); + return view; + + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + return null; + } + + private static Element parseXml(String resourcePath){ + + InputStream resourceAsStream = Struts.class.getResourceAsStream(resourcePath); + SAXReader saxReader = new SAXReader(); + try { + Document document = saxReader.read(resourceAsStream); + Element rootElement = document.getRootElement(); + return rootElement; + } catch (DocumentException e) { + e.printStackTrace(); + } + + throw new RuntimeException("fail to parse xml"); + } + + private static Action matchAction(Element rootElement, String actionName){ + + List actions = rootElement.elements("action"); + Iterator iterator = actions.iterator(); + Action action = new Action(); + while (iterator.hasNext()){ + Element actionElement = (Element) iterator.next(); + String nameAttributeValue = actionElement.attributeValue("name"); + if (actionName.equals(nameAttributeValue)){ + action.setName(nameAttributeValue); + action.setClassName(actionElement.attributeValue("class")); + List results = actionElement.elements("result"); + Map resultMap = new HashMap(); + Iterator it = results.iterator(); + while (it.hasNext()){ + Element resultElement = (Element)it.next(); + resultMap.put(resultElement.attributeValue("name"), (String)resultElement.getData()); + } + action.setResults(resultMap); + } + } + + return action; + } + + + + +} diff --git a/group05/578505552/src/main/java/com/coderising/litestruts/View.java b/group05/578505552/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group05/578505552/src/main/java/com/coderising/litestruts/XmlUtil.java b/group05/578505552/src/main/java/com/coderising/litestruts/XmlUtil.java new file mode 100644 index 0000000000..6e3c261882 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/XmlUtil.java @@ -0,0 +1,8 @@ +package com.coderising.litestruts; + +/** + * + * Created by songbao.yang on 2017/2/28. + */ +public class XmlUtil { +} diff --git a/group05/578505552/src/main/java/com/coding/basic/ArrayList.java b/group05/578505552/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..54d8f05f02 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,103 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public class ArrayList implements List { + + private int size = 0; + private Object[] elementData; + private static final int MIN_CAPACITY = 10; + + public ArrayList(int size) { + if (size < 0){ + throw new IllegalArgumentException("illega size: " + size); + } + this.elementData = new Object[size]; + } + + public ArrayList() { + this.elementData = new Object[0]; + } + + public void add(Object o){ + ensureCapacity(size + 1); + elementData[size++] = o; + } + + private void ensureCapacity(int minCapacity){ + if (minCapacity < 0 ){ + throw new OutOfMemoryError(); + } + + int newCapcity = size; + if(minCapacity < MIN_CAPACITY){ + newCapcity = MIN_CAPACITY; + } else if(minCapacity > elementData.length){ + int tmp = elementData.length << 1; + newCapcity = tmp > elementData.length ? tmp : Integer.MAX_VALUE; + } + + newCapcity = minCapacity; + Object[] newData = new Object[newCapcity]; + System.arraycopy(elementData, 0, newData, 0, size); + elementData = newData; + } + + public void add(int index, Object o){ + indexCheck(index); + ensureCapacity(size+1); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + indexCheck(index); + return elementData[index]; + } + + private void indexCheck(int index){ + if(index < 0){ + throw new IllegalArgumentException("illegal index: " + index); + } + if(index >= size){ + throw new IndexOutOfBoundsException(); + } + } + + public Object remove(int index){ + indexCheck(index); + Object rm = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + size--; + return rm; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Itr(); + } + + //静态内部类的访问权限不同有何区别?? + private class Itr implements Iterator{ + private int cursor = 0; + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + if (hasNext()){ + return elementData[cursor++]; + } + throw new NoSuchElementException(); + } + } +} diff --git a/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java b/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..3a36db4031 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,60 @@ +package com.coding.basic; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Integer 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(Integer o){ + if (o == null){ + throw new IllegalArgumentException("can not insert null"); + } + + BinaryTreeNode newNode = new BinaryTreeNode(); + newNode.data = o; + newNode.left = null; + newNode.right = null; + + BinaryTreeNode cursor = this; + BinaryTreeNode pre = cursor; + while (cursor != null){ + pre = cursor; + if (o.compareTo(cursor.data) < 0){ + cursor = cursor.left; + } else { + cursor = cursor.right; + } + } + + if (o.compareTo(pre.data) < 0){ + pre.left = newNode; + } else { + pre.right = newNode; + } + return this; + } +} diff --git a/group05/578505552/src/main/java/com/coding/basic/Iterator.java b/group05/578505552/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..6765eae519 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,11 @@ +package com.coding.basic; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/578505552/src/main/java/com/coding/basic/LinkedList.java b/group05/578505552/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d1fa42bf4c --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,156 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public class LinkedList implements List { + + private Node head; + private int elementCount; + + //head作为一个节点,其next的值指向List中真正的第一个节点 + public LinkedList() { + head = new Node(); + head.next = null; + head.data = null; + elementCount = 0; + } + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + Node cursor = head; + while (cursor.next != null){ + cursor = cursor.next; + } + cursor.next = newNode; + elementCount++; + } + + + public void add(int index , Object o){ + indexRangeCheck(index); + Node newNode = new Node(); + newNode.data = o; + + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; //将cursor移动到index-1节点处; + } + + newNode.next = cursor.next; //将新节点指向原index处的节点 + cursor.next = newNode;//将原index-1处的节点指向新节点 + elementCount++; + } + + private void indexRangeCheck(int index){ + if (index < 0 || index >= size()){ + throw new IndexOutOfBoundsException(); + } + } + + public Object get(int index){ + indexRangeCheck(index); + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor.next.data; + } + + public Object remove(int index){ + indexRangeCheck(index); + Node cursor = head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + Node indexNode = cursor.next; + cursor.next = indexNode.next; + elementCount--; + return indexNode; + } + + public int size(){ + return elementCount; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = head.next; + head.next = node; + elementCount++; + } + + public void addLast(Object o){ + + Node cursor = head; + while (cursor.next != null){ + cursor = cursor.next; + } + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + cursor.next = newNode; + elementCount++; + } + + public Object removeFirst(){ + + if (size() == 0){ + throw new RuntimeException("no element in list"); + } + Node firstNode = head.next; + head.next = head.next.next; + elementCount--; + return firstNode; + } + + public Object removeLast(){ + if (size() == 0){ + throw new RuntimeException("no element in list"); + } + + Node cursor = head; + for (int i = 0; i < size() - 1; i++) { + cursor = cursor.next; + } + + Node lastNode = cursor.next; + cursor.next = null; + elementCount--; + + return lastNode; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator { + + private Node itrCursor = head; + + public boolean hasNext() { + + return itrCursor.next != null; + } + + public Object next() { + if (hasNext()){ + return itrCursor.next; + } + throw new NoSuchElementException(); + } + } + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group05/578505552/src/main/java/com/coding/basic/List.java b/group05/578505552/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..e26fbe7c98 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/List.java @@ -0,0 +1,14 @@ +package com.coding.basic; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + public Iterator iterator(); +} diff --git a/group05/578505552/src/main/java/com/coding/basic/Queue.java b/group05/578505552/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..ec31573ee7 --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,90 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by songbao.yang on 2017/2/22. + * + */ +public class Queue { + + private Object[] elementData; + private int head; //对头的位置 + private int tail; //队尾的位置 + private int size; //队列中元素的个数 + private static final int MIN_INITIAL_CAPACITY = 10; + + public Queue() { + this.elementData = new Object[MIN_INITIAL_CAPACITY]; + this.head = 0; + this.tail = 0; + this.size = 0; + } + + public Queue(int initCapcacity) { + if (initCapcacity < MIN_INITIAL_CAPACITY){ + initCapcacity = MIN_INITIAL_CAPACITY; + } + this.elementData = new Object[initCapcacity]; + this.head = 0; + this.tail = 0; + this.size = 0; + } + + public void enQueue(Object o){ + ensureCapacity(size+1); + if(size != 0){ + tail++; + } + if(tail == elementData.length){ + tail = 0; + } + elementData[tail] = o; + size++; + } + + private void ensureCapacity(int minCapcacity){ + if(minCapcacity <= elementData.length){ + return; + } + + int newCapcacity = elementData.length << 1; + if (newCapcacity < elementData.length){ + newCapcacity = Integer.MAX_VALUE; + } + + Object[] newData = new Object[newCapcacity]; + if(size != 0){ + if(tail >= head){ + System.arraycopy(elementData, head, newData, 0, size); + } else { + System.arraycopy(elementData, head, newData, 0, elementData.length - head); + System.arraycopy(elementData, 0, newData, elementData.length - head, tail + 1); + } + elementData = newData; + head = 0; + tail = this.size - 1; + } + } + + public Object deQueue(){ + if (isEmpty()){ + throw new NoSuchElementException("empty queue"); + } + Object ele = elementData[head]; + size--; + head++; + if(head == elementData.length){ + head = 0; + } + return ele; + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group05/578505552/src/main/java/com/coding/basic/Stack.java b/group05/578505552/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..ffc4915bef --- /dev/null +++ b/group05/578505552/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,63 @@ +package com.coding.basic; + + +import java.util.EmptyStackException; + +/** + * Created by songbao.yang on 2017/2/22. + * + */ +public class Stack { + + private Object[] elementData; + private static final int MIN_INITIAL_CAPACITY = 10; + private int cursor; + + public Stack() { + elementData = new Object[MIN_INITIAL_CAPACITY]; + cursor = -1; + } + + public void push(Object o){ + ensureCapacity(size() + 1); + cursor++; + elementData[cursor] = o; + } + + private void ensureCapacity(int minCapacity){ + if(minCapacity <= elementData.length){ + return; + } + + int newSize = elementData.length << 1; + if (newSize < elementData.length){ + newSize = Integer.MAX_VALUE; + } + + Object[] newDataArray = new Object[newSize]; + System.arraycopy(elementData, 0, newDataArray, 0, size()); + elementData = newDataArray; + } + + + public Object pop(){ + Object ele = peek(); + cursor--; + return ele; + } + + public Object peek(){ + if (isEmpty()){ + throw new EmptyStackException(); + } + return elementData[cursor]; + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return cursor + 1; + } +} diff --git a/group05/578505552/src/main/resources/struts.xml b/group05/578505552/src/main/resources/struts.xml new file mode 100644 index 0000000000..0a44e0927e --- /dev/null +++ b/group05/578505552/src/main/resources/struts.xml @@ -0,0 +1,12 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group05/578505552/src/test/java/JavaTest.java b/group05/578505552/src/test/java/JavaTest.java new file mode 100644 index 0000000000..26d2197ffe --- /dev/null +++ b/group05/578505552/src/test/java/JavaTest.java @@ -0,0 +1,25 @@ +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Queue; +import java.util.Stack; + +/** + * Created by songbao.yang on 2017/2/21. + */ +public class JavaTest { + + public static void main(String[] args) { + + ArrayList ss = new ArrayList(); + ss.add("a"); + ss.add("b"); + ss.add("c"); + ss.add("d"); + + System.out.println(ss.size()); + ss.remove(0); + System.out.println(ss.size()); + + + } +} diff --git a/group05/578505552/src/test/java/com/coderising/array/ArrayUtilTest.java b/group05/578505552/src/test/java/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..607f4fed5b --- /dev/null +++ b/group05/578505552/src/test/java/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,206 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Created by songbao.yang on 2017/3/2. + * + */ +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void reverseArray() throws Exception { + + int[][] actualAndExpected = { + {}, {}, {0}, {0}, + {1,2,3,4,5,6}, {6,5,4,3,2,1}, + {7,9,30,3,4}, {4,3,30,9,7} + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int[] acutal = actualAndExpected[i]; + int[] expected = actualAndExpected[i+1]; + arrayUtil.reverseArray(acutal); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, acutal)); + } + } + + @Test + public void removeZero() throws Exception { + int[][] actualAndExpected = { + {}, {}, {0}, {}, + {1,0,3,0,5,0}, {1,3,5}, + {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}, {1,3,4,5,6,6,5,4,7,6,7,5} + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int[] acutal = actualAndExpected[i]; + int[] expected = actualAndExpected[i+1]; + int[] ints = arrayUtil.removeZero(acutal); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, ints)); + } + } + + @Test + public void merge() throws Exception { + int[][] actualAndExpected = { + {}, {}, {}, + {}, {0}, {0}, + {3,5,7,8}, {4,5,6,7},{3,4,5,6,7,8}, + {1,2,3,4,5,}, {6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10} + }; + + for (int i = 0; i < actualAndExpected.length; i += 3) { + int[] array1 = actualAndExpected[i]; + int[] array2 = actualAndExpected[i+1]; + int[] expected = actualAndExpected[i+2]; + int[] result = arrayUtil.merge(array1, array2); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, result)); + } + + } + + @Test + public void grow() throws Exception { + int[][] actualAndExpected = { + {}, {}, + {1}, {}, + {5}, {0,0,0,0,0}, + {0},{2,3,6}, + {3}, {2,3,6,0,0,0} + }; + + for (int i = 0; i < actualAndExpected.length; i += 3) { + int[] oldArray = actualAndExpected[i]; + int size = actualAndExpected[i+1][0]; + int[] expected = actualAndExpected[i+2]; + int[] newArray = arrayUtil.grow(oldArray, size); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, newArray)); + } + } + + @Test + public void fibonacci() throws Exception { + int[][] actualAndExpected = { + {0}, {}, + {1}, {}, + {2}, {1,1}, + {3}, {1,1,2}, + {4}, {1,1,2,3}, + {15}, {1,1,2,3,5,8,13}, + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int max = actualAndExpected[i][0]; + int[] expected = actualAndExpected[i+1]; + int[] actual = arrayUtil.fibonacci(max); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, actual)); + } + } + + @Test + public void getPrimes() throws Exception { + int[][] actualAndExpected = { + {-1}, {}, + {0}, {}, + {1}, {}, + {2}, {}, + {3}, {2}, + {4}, {2,3}, + {23}, {2,3,5,7,11,13,17,19}, + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int max = actualAndExpected[i][0]; + int[] expected = actualAndExpected[i+1]; + int[] actual = arrayUtil.getPrimes(max); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, actual)); + } + } + + @Test + public void getPerfectNumbers() throws Exception { + int[][] actualAndExpected = { + {-1}, {}, + {0}, {}, + {1}, {}, + {2}, {}, + {7}, {6}, + {30}, {6,28}, + {500}, {6,28,496}, + {10000}, {6,28,496,8128} + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int max = actualAndExpected[i][0]; + int[] expected = actualAndExpected[i+1]; + int[] actual = arrayUtil.getPerfectNumbers(max); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, actual)); + } + } + + @Test + public void join() throws Exception { + int[][] arrays = { + {}, + {3,8,9}, + {1}, + {0,0,0,0,0}, + {1,2,3,4,5} + }; + String[] separators = {"", "-", "+", "*", "00"}; + String[] expecteds = { + "", + "3-8-9", + "1", + "0*0*0*0*0", + "1002003004005" + }; + for (int i = 0; i < arrays.length; i++) { + int[] array = arrays[i]; + String separator = separators[i]; + String expected = expecteds[i]; + String actual = arrayUtil.join(array, separator); + Assert.assertTrue("wrong index: " + String.valueOf(i), expected.equals(actual)); + } + } + + private boolean isArrayEqual(int[] expected, int[] actual){ + if (expected.length != actual.length){ + System.out.println("expected.length != actual.length"); + System.out.println("expected: " + Arrays.toString(expected)); + System.out.println("actual: " + Arrays.toString(actual)); + return false; + } + + for (int i = 0; i < expected.length; i++) { + if (expected[i] != actual[i]){ + System.out.println("expected[i] != actual[i]"); + System.out.println("expected: " + Arrays.toString(expected)); + System.out.println("actual: " + Arrays.toString(actual)); + return false; + } + } + + return true; + } + +} \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coderising/litestruts/StrutsTest.java b/group05/578505552/src/test/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..6df05450b6 --- /dev/null +++ b/group05/578505552/src/test/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group05/578505552/src/test/java/com/coding/basic/BinaryTreeNodeTest.java b/group05/578505552/src/test/java/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..d1e408554e --- /dev/null +++ b/group05/578505552/src/test/java/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by songbao.yang on 2017/2/28. + * + */ +public class BinaryTreeNodeTest { + + private BinaryTreeNode node; + + @Before + public void setUp() throws Exception { + node = new BinaryTreeNode(); + node.setData(100); + node.setLeft(null); + node.setRight(null); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void insert() throws Exception { + + for (int i = 0; i < 100; i++) { + int ele = (int)Math.floor(Math.random() * 200); + node.insert(ele); + } + } +} \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coding/basic/ListTest.java b/group05/578505552/src/test/java/com/coding/basic/ListTest.java new file mode 100644 index 0000000000..0c786de96c --- /dev/null +++ b/group05/578505552/src/test/java/com/coding/basic/ListTest.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by songbao.yang on 2017/2/21. + * + */ +public class ListTest { + + private List list; + + public static final int SIZE = 10000; + + @Before + public void setUp() throws Exception { + +// list = new ArrayList(); + list = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void add() throws Exception { + + for (int i = 0; i < SIZE; i++) { + list.add(i); + Assert.assertEquals(i+1, list.size()); + } + } + + @Test + public void add1() throws Exception { + + add(); + for (int i = 0; i < 1000; i++) { + int oldSize = list.size(); + int randomIndex = (int)Math.floor(Math.random() * oldSize); + + list.add(randomIndex, randomIndex); + int newSize = list.size(); + + Assert.assertEquals(newSize, oldSize+1); + Assert.assertEquals(list.get(randomIndex), randomIndex); + } + } + + @Test + public void get() throws Exception { + + add(); + for (int i = 0; i < SIZE * 100; i++) { + int randomIndex = (int)Math.floor(Math.random() * list.size()); + if(randomIndex < 0 || randomIndex >= SIZE){ + System.out.println("illegal index: " + randomIndex); + throw new RuntimeException(); + } + int o = (Integer) list.get(randomIndex); + Assert.assertEquals(randomIndex, o); + } + } + + @Test + public void remove() throws Exception { + + add(); + for (int i = 0; i < SIZE; i++) { + int oldSize = list.size(); + int randomIndex = (int)Math.floor(Math.random() * oldSize); + list.remove(randomIndex); + int newSize = list.size(); + Assert.assertEquals(newSize, oldSize-1); + } + } + + @Test + public void size() throws Exception { + + } + + @Test + public void iterator() throws Exception { + add(); + Iterator iterator1 = list.iterator(); + int i = 0; + while (iterator1.hasNext()){ + Object next = iterator1.next(); + Assert.assertEquals(i, next); + i++; + } + } + +} \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coding/basic/QueueTest.java b/group05/578505552/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..905054670d --- /dev/null +++ b/group05/578505552/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by songbao.yang on 2017/2/24. + */ +public class QueueTest { + + private static final int SIZE = 2000; + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void enQueue() throws Exception { + for (int i = 0; i < SIZE; i++) { + queue.enQueue(i); + Assert.assertEquals(i+1, queue.size()); + } + } + + @Test + public void deQueue1() throws Exception { + enQueue(); + + int i = 0; + int startSize = queue.size(); + while (!queue.isEmpty()) { + Assert.assertEquals(startSize - i, queue.size()); + Object o = queue.deQueue(); + Assert.assertEquals(SIZE - i - 1, queue.size()); + Assert.assertEquals(i, o); + i++; + } + } + + @Test + public void deQueue2() throws Exception { + enQueue(); + int startSize = queue.size(); + + for (int i = 0; i < startSize; i++) { + queue.deQueue(); + Assert.assertEquals(startSize - 1, queue.size()); + queue.enQueue(i+1000); + Assert.assertEquals(startSize, queue.size()); + } + + } + + @Test + public void isEmpty() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + +} \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coding/basic/StackTest.java b/group05/578505552/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..b65d446c97 --- /dev/null +++ b/group05/578505552/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,65 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by songbao.yang on 2017/2/24. + * + */ +public class StackTest { + + private Stack stack; + + public static final int SIZE = 100; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void push() throws Exception { + for (int i = 0; i < SIZE; i++) { + stack.push(i); + Assert.assertEquals(i+1, stack.size()); + } + System.out.println(); + } + + @Test + public void pop() throws Exception { + push(); + int beginSize = stack.size(); + for (int i = 0; i < beginSize; i++) { + Object ele = stack.pop(); + Assert.assertEquals(beginSize-i-1, stack.size()); + Assert.assertEquals(beginSize-i-1, ele); + } + } + + @Test + public void peek() throws Exception { + + } + + @Test + public void isEmpty() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + +} \ No newline at end of file diff --git a/group05/591010847/.gitignore b/group05/591010847/.gitignore new file mode 100644 index 0000000000..7a02c10a7e --- /dev/null +++ b/group05/591010847/.gitignore @@ -0,0 +1,10 @@ +/vendor +/node_modules +Homestead.yaml +Homestead.json +.env +.idea +.xml +/out +.class +.DS_Store diff --git a/group05/591010847/job_2017_02/.gitignore b/group05/591010847/job_2017_02/.gitignore new file mode 100644 index 0000000000..a93e887bab --- /dev/null +++ b/group05/591010847/job_2017_02/.gitignore @@ -0,0 +1 @@ +libs diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml new file mode 100644 index 0000000000..3a8ffcf1f5 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/job_2017_02_19_data_structure.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java new file mode 100644 index 0000000000..27ee61ce66 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/ArrayListImpl.java @@ -0,0 +1,127 @@ +package com.java.xiaoqin.impl; + +import com.java.xiaoqin.interfaces.IIterator; +import com.java.xiaoqin.interfaces.IList; + +/** + * Created by xiaoqin on 17-2-25. + */ +public class ArrayListImpl implements IList { + + private static final int DEFAULT_INIT_SIZE = 20; + + private T[] data; + + private int size = 0; + + public ArrayListImpl() { + this(DEFAULT_INIT_SIZE); + } + + public ArrayListImpl(int capacity) { + if (capacity < 0) { + throw new IllegalArgumentException("无效的capacity:" + capacity); + } + if (0 == capacity) { + capacity = DEFAULT_INIT_SIZE; + } + data = (T[]) new Object[capacity]; + } + + @Override + public void add(T t) { + group(size); + data[size++] = t; + } + + @Override + public void add(int index, T t) { + if (index < 0) { + throw new IllegalArgumentException("index < 0,index:" + index); + } + if (index > size) { + throw new IndexOutOfBoundsException("index >= size。index:" + index + "\tsize:" + size); + } + group(size); + T[] temp = (T[]) new Object[size - index]; + System.arraycopy(data, index, temp, 0, temp.length); + data[index] = t; + size++; + System.arraycopy(temp, 0, data, index + 1, temp.length); + } + + @Override + public T get(int index) { + if (index < data.length) { + return data[index]; + } else { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + @Override + public T remove(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException("index invalid!!!index:" + index); + } + T[] temp = (T[]) new Object[size - index - 1]; + System.arraycopy(data, index + 1, temp, 0, temp.length); + T result = data[index]; + System.arraycopy(temp, 0, data, index, temp.length); + data[--size] = null; + return result; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public IIterator iterator() { + return new ArrayIteratorImpl<>(); + } + + @Override + public String toString() { + StringBuilder sbToString = new StringBuilder(); + for (T t : + data) { + sbToString.append(t).append("\t"); + } + return sbToString.toString(); + } + + private void group(int minSize) { + if (minSize >= data.length) { + T[] temp = (T[]) new Object[size]; + System.arraycopy(data, 0, temp, 0, size); + int groupSize = (size >> 1) + size; + data = (T[]) new Object[Math.max(groupSize, minSize)]; + System.arraycopy(temp, 0, data, 0, size); + } + } + + private class ArrayIteratorImpl implements IIterator { + + private int index = 0; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public T next() { + if (index >= size) { + throw new ArrayIndexOutOfBoundsException("index out"); + } + return (T) data[index++]; + } + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/BinaryTreeNode.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/BinaryTreeNode.java new file mode 100644 index 0000000000..664468d901 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/BinaryTreeNode.java @@ -0,0 +1,82 @@ +package com.java.xiaoqin.impl; + +/** + * Created by xiaoqin on 17-2-26. + */ +public class BinaryTreeNode { + private T data; + private BinaryTreeNode leftChild; + private BinaryTreeNode rightChild; + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeftChild() { + return leftChild; + } + + public void setLeftChild(BinaryTreeNode leftChild) { + this.leftChild = leftChild; + } + + public BinaryTreeNode getRightChild() { + return rightChild; + } + + public void setRightChild(BinaryTreeNode rightChild) { + this.rightChild = rightChild; + } + + public BinaryTreeNode insert(T o) { + if (data == null) { + data = o; + return this; + } else { + return insert(this, o); + } + } + + private BinaryTreeNode insert(BinaryTreeNode node, T o) { + if (o instanceof Integer) { + if ((Integer) node.data > (Integer) o) { + if (null == node.leftChild) { + node.leftChild = new BinaryTreeNode(); + node.leftChild.data = o; + return node.leftChild; + } else { + return insert(node.leftChild, o); + } + } else if ((Integer) node.data < (Integer) o) { + if (null == node.rightChild) { + node.rightChild = new BinaryTreeNode(); + node.rightChild.data = o; + return node.rightChild; + } else { + return insert(node.rightChild, o); + } + } else { + return node; + } + } else { + return null; + } + } + + @Override + public String toString() { + StringBuilder sbToString = new StringBuilder(); + sbToString.append("data:").append(data); + if (null != leftChild) { + sbToString.append("\t").append(data).append("left:").append(leftChild.toString()); + } + if (null != rightChild) { + sbToString.append("\t").append(data).append("right:").append(rightChild.toString()); + } + return sbToString.toString(); + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java new file mode 100644 index 0000000000..7d8ad60419 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/LinkedListImpl.java @@ -0,0 +1,202 @@ +package com.java.xiaoqin.impl; + +import com.java.xiaoqin.interfaces.IIterator; +import com.java.xiaoqin.interfaces.IList; +import com.java.xiaoqin.interfaces.IQueue; + +/** + * Created by xiaoqin on 17-2-26. + */ +public class LinkedListImpl implements IList, IQueue { + + private Node head; + private Node last; + private int size = 0; + + public LinkedListImpl() { + } + + @Override + public void add(T t) { + if (size == 0) { + head = new Node<>(); + head.data = t; + head.index = size++; + last = head; + } else { + last.right = new Node<>(); + last.right.left = last; + last = last.right; + last.data = t; + last.index = size++; + } + } + + @Override + public void add(int index, T t) { + Node node = findNodeByIndex(index); + if (node.index == 0) { + node.left = new Node<>(); + node.left.right = node; + node.left.index = 0; + node.left.data = t; + head = node.left; + } else { + node.left.right = new Node<>(); + node.left.right.index = node.index; + node.left.right.right = node; + node.left.right.data = t; + node.left = node.left.right; + } + while (node != null) { + node.index++; + node = node.right; + } + size++; + } + + private Node findNodeByIndex(int index) { + if (index >= size) { + throw new ArrayIndexOutOfBoundsException("index:" + index); + } + Node resultNode = null; + Node origin = null; + int half = size >> 1; + if (index > half) { + origin = last; + while (origin.index != index) { + origin = origin.left; + } + } else { + origin = head; + while (origin.index != index) { + origin = origin.right; + } + } + return origin; + } + + @Override + public T get(int index) { + return findNodeByIndex(index).data; + } + + @Override + public T remove(int index) { + Node node = findNodeByIndex(index); + if (null != node.left) { + node.left.right = node.right; + } else { + node.right.left = null; + head = node.right; + } + if (null != node.right) { + node.right.left = node.left; + while (node.right == null) { + node.right.index--; + } + } else { + node.left.right = null; + last = node.left; + } + size--; + return node.data; + } + + @Override + public int size() { + return size; + } + + @Override + public void enQueue(T t) { + add(t); + } + + @Override + public T deQueue() { + if (0 >= size) { + throw new NullPointerException("remove is null"); + } + T t = null; + Node node = findNodeByIndex(0); + if (null != node) { + t = node.data; + if (null != node.right) { + node.right.left = null; + Node right = node.right; + head = right; + while (right != null) { + right.index--; + right = right.right; + } + size--; + } else { + head = last = null; + size = 0; + } + } + return t; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public IIterator iterator() { + return new LinkedIteratorImpl<>(); + } + + @Override + public String toString() { + StringBuilder sbToString = new StringBuilder(); + Node temp = head; + while (null != temp){ + sbToString.append(temp.toString()).append("\n"); + temp = temp.right; + } + return sbToString.toString(); + } + + private static class Node { + private T data = null; + private Node left = null; + private Node right = null; + private int index = 0; + + @Override + public String toString() { + return "Node{" + + "data=" + data + + ", index=" + index + + '}'; + } + } + + private class LinkedIteratorImpl implements IIterator { + + private int index = 0; + private Node next; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public T next() { + if (0 == index) { + next = (Node) head; + } + if (null == next) { + throw new ArrayIndexOutOfBoundsException("next is null"); + } + Node result = next; + next = next.right; + index++; + return result.data; + } + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/StackImpl.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/StackImpl.java new file mode 100644 index 0000000000..0a585ab904 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/impl/StackImpl.java @@ -0,0 +1,60 @@ +package com.java.xiaoqin.impl; + +/** + * Created by xiaoqin on 17-2-26. + */ +public class StackImpl extends ArrayListImpl { + + /** + * 把数据压住栈 + * + * @param t + */ + public void push(T t) { + add(t); + } + + /** + * 返回栈顶数据,并移除出栈 + * + * @return + */ + public T pop() { + if (0 >= size()) { + throw new NullPointerException("size is 0"); + } + return remove(size() - 1); + } + + /** + * 返回栈顶数据 + * + * @return + */ + public T peek() { + if (0 >= size()) { + throw new NullPointerException("size is 0"); + } + return get(size() - 1); + } + + /** + * 是否为null + * + * @return + */ + @Override + public boolean isEmpty() { + return super.isEmpty(); + } + + /** + * 大小 + * + * @return + */ + @Override + public int size() { + return super.size(); + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java new file mode 100644 index 0000000000..bd83985af4 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IIterator.java @@ -0,0 +1,19 @@ +package com.java.xiaoqin.interfaces; + +/** + * Created by xiaoqin on 17-2-25. + */ +public interface IIterator { + + /** + * 是否有下一个 + * @return + */ + boolean hasNext(); + + /** + * 取出下一个的元素 + * @return + */ + T next(); +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java new file mode 100644 index 0000000000..7c08a46293 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IList.java @@ -0,0 +1,53 @@ +package com.java.xiaoqin.interfaces; + +/** + * Created by xiaoqin on 17-2-19. + */ +public interface IList { + + /** + * 添加元素 + * @param t + */ + void add(T t); + + /** + * 添加元素在第几个 + * @param index + * @param t + */ + void add(int index,T t); + + /** + * 获取第index个的元素 + * @param index + * @return + */ + T get(int index); + + /** + * 移除第index个的元素 + * @param index + * @return + */ + T remove(int index); + + /** + * 返回List的大小 + * @return + */ + int size(); + + /** + * 是否为empty + * @return + */ + boolean isEmpty(); + + /** + * 返回迭代器 + * @return + */ + IIterator iterator(); + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IQueue.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IQueue.java new file mode 100644 index 0000000000..4ae4b7d785 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/com/java/xiaoqin/interfaces/IQueue.java @@ -0,0 +1,15 @@ +package com.java.xiaoqin.interfaces; + +/** + * Created by xiaoqin on 17-2-26. + */ +public interface IQueue { + + void enQueue(T t); + + T deQueue(); + + boolean isEmpty(); + + int size(); +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/ArrayListImplTest.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/ArrayListImplTest.java new file mode 100644 index 0000000000..ce6d903ff2 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/ArrayListImplTest.java @@ -0,0 +1,107 @@ +package test.com.java.xiaoqin.impl; + +import com.java.xiaoqin.impl.ArrayListImpl; +import com.java.xiaoqin.interfaces.IIterator; +import com.java.xiaoqin.interfaces.IList; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * ArrayListImpl Tester. + * + * @author + * @version 1.0 + * @since
二月 26, 2017
+ */ +public class ArrayListImplTest { + + private IList mList; + + @Before + public void before() throws Exception { + mList = new ArrayListImpl<>(); + } + + @After + public void after() throws Exception { + mList = null; + } + + private void addTen() { + for (int i = 0; i < 10; i++) { + mList.add(i); + } + } + + /** + * Method: add(T t) + */ + @Test + public void testAddT() throws Exception { + addTen(); + System.out.println(mList.toString()); + } + + /** + * Method: add(int index, T t) + */ + @Test + public void testAddForIndexT() throws Exception { + addTen(); + mList.add(4, 50); + mList.add(8, 150); + System.out.println(mList.toString()); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + addTen(); + Assert.assertEquals(mList.get(5), (Integer) 5); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { + addTen(); + Assert.assertEquals(mList.remove(4), (Integer) 4); + System.out.println(mList); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + addTen(); + Assert.assertEquals(mList.size(), 10); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + System.out.println(mList.isEmpty()); + addTen(); + System.out.println(mList.isEmpty()); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + addTen(); + IIterator iterator = mList.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/BinaryTreeNodeTest.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..fa28cdf0d9 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/BinaryTreeNodeTest.java @@ -0,0 +1,46 @@ +package test.com.java.xiaoqin.impl; + +import com.java.xiaoqin.impl.BinaryTreeNode; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * BinaryTreeNode Tester. + * + * @author + * @version 1.0 + * @since
二月 27, 2017
+ */ +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + + @Before + public void before() throws Exception { + binaryTreeNode = new BinaryTreeNode<>(); + } + + @After + public void after() throws Exception { + binaryTreeNode = null; + } + + /** + * Method: insert(T o) + */ + @Test + public void testInsert() throws Exception { + binaryTreeNode.insert(5); + binaryTreeNode.insert(2); + binaryTreeNode.insert(7); + binaryTreeNode.insert(1); + binaryTreeNode.insert(6); + System.out.println(binaryTreeNode.toString()); + binaryTreeNode.insert(4); + binaryTreeNode.insert(8); + System.out.println(binaryTreeNode.toString()); + } + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/IQueueImplTest.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/IQueueImplTest.java new file mode 100644 index 0000000000..14bbcac149 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/IQueueImplTest.java @@ -0,0 +1,77 @@ +package test.com.java.xiaoqin.impl; + +import com.java.xiaoqin.impl.LinkedListImpl; +import com.java.xiaoqin.interfaces.IQueue; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * IQueueImplTest Tester. + * + * @author + * @version 1.0 + * @since
二月 26, 2017
+ */ +public class IQueueImplTest { + + private IQueue mQueue; + + @Before + public void before() throws Exception { + mQueue = new LinkedListImpl<>(); + } + + @After + public void after() throws Exception { + mQueue = null; + } + + /** + * Method: enQueue(T t) + */ + @Test + public void enQueueT() throws Exception { + for (int i = 0; i < 10; i++) { + mQueue.enQueue(i); + } + System.out.println(mQueue.toString()); + } + + /** + * Method: deQueue + */ + @Test + public void deQueue() throws Exception { + for (int i = 0; i < 10; i++) { + mQueue.enQueue(i); + } + Assert.assertEquals(mQueue.deQueue(), (Integer) 0); + Assert.assertEquals(mQueue.deQueue(), (Integer) 1); + } + + /** + * Method: isEmpty + */ + @Test + public void isEmpty() throws Exception { + Assert.assertEquals(true, mQueue.isEmpty()); + for (int i = 0; i < 10; i++) { + mQueue.enQueue(i); + } + Assert.assertEquals(false, mQueue.isEmpty()); + } + + /** + * Method: size(); + */ + @Test + public void size() throws Exception { + for (int i = 0; i < 10; i++) { + mQueue.enQueue(i); + } + Assert.assertEquals(10, mQueue.size()); + } + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/IStackImplTest.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/IStackImplTest.java new file mode 100644 index 0000000000..a55667aadb --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/IStackImplTest.java @@ -0,0 +1,90 @@ +package test.com.java.xiaoqin.impl; + +import com.java.xiaoqin.impl.StackImpl; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * IQueueImplTest Tester. + * + * @author + * @version 1.0 + * @since
二月 26, 2017
+ */ +public class IStackImplTest { + + private StackImpl mStack; + + @Before + public void before() throws Exception { + mStack = new StackImpl<>(); + } + + @After + public void after() throws Exception { + mStack = null; + } + + /** + * Method: push(T t) + */ + @Test + public void pushT() throws Exception { + for (int i = 0; i < 10; i++) { + mStack.push(i); + } + System.out.println(mStack.toString()); + } + + /** + * Method: pop + */ + @Test + public void pop() throws Exception { + for (int i = 0; i < 10; i++) { + mStack.push(i); + } + Assert.assertEquals(mStack.pop(), (Integer) 9); + Assert.assertEquals(mStack.pop(), (Integer) 8); + } + + /** + * Method: peek + */ + @Test + public void peek() throws Exception { + Assert.assertEquals(true, mStack.isEmpty()); + for (int i = 0; i < 10; i++) { + mStack.push(i); + } + Assert.assertEquals(mStack.peek(), (Integer) 9); + Assert.assertEquals(mStack.peek(), (Integer) 9); + Assert.assertEquals(mStack.peek(), (Integer) 9); + } + + /** + * Method: isEmpty + */ + @Test + public void isEmpty() throws Exception { + Assert.assertEquals(true, mStack.isEmpty()); + for (int i = 0; i < 10; i++) { + mStack.push(i); + } + Assert.assertEquals(false, mStack.isEmpty()); + } + + /** + * Method: size(); + */ + @Test + public void size() throws Exception { + for (int i = 0; i < 10; i++) { + mStack.push(i); + } + Assert.assertEquals(10, mStack.size()); + } + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/LinkedListImplTest.java b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/LinkedListImplTest.java new file mode 100644 index 0000000000..669f88d9b1 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_19_data_structure/src/test/com/java/xiaoqin/impl/LinkedListImplTest.java @@ -0,0 +1,108 @@ +package test.com.java.xiaoqin.impl; + +import com.java.xiaoqin.impl.LinkedListImpl; +import com.java.xiaoqin.interfaces.IIterator; +import com.java.xiaoqin.interfaces.IList; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * LinkedListImpl Tester. + * + * @author + * @version 1.0 + * @since
二月 26, 2017
+ */ +public class LinkedListImplTest { + + private IList mList; + + @Before + public void before() throws Exception { + mList = new LinkedListImpl<>(); + } + + @After + public void after() throws Exception { + mList = null; + } + + private void addTen() { + for (int i = 0; i < 10; i++) { + mList.add(i); + } + } + + /** + * Method: add(T t) + */ + @Test + public void testAddT() throws Exception { + addTen(); + System.out.println(mList.toString()); + } + + /** + * Method: add(int index, T t) + */ + @Test + public void testAddForIndexT() throws Exception { + addTen(); + mList.add(4, 50); + mList.add(8, 150); + System.out.println(mList.toString()); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + addTen(); + Assert.assertEquals(mList.get(5), (Integer) 5); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemove() throws Exception { + addTen(); + Assert.assertEquals(mList.remove(4), (Integer) 4); + System.out.println(mList); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + addTen(); + Assert.assertEquals(mList.size(), 10); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + System.out.println(mList.isEmpty()); + addTen(); + System.out.println(mList.isEmpty()); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + addTen(); + IIterator iterator = mList.iterator(); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/job_2017_02_26_Struts.iml b/group05/591010847/job_2017_02/job_2017_02_26_Struts/job_2017_02_26_Struts.iml new file mode 100644 index 0000000000..a141f7b1fe --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/job_2017_02_26_Struts.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/res/struts.xml b/group05/591010847/job_2017_02/job_2017_02_26_Struts/res/struts.xml new file mode 100644 index 0000000000..92c2e5800b --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/res/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/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/array/ArrayUtil.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/array/ArrayUtil.java new file mode 100644 index 0000000000..75ae466816 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.java.xiaoqin.array; + +import java.util.Objects; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (null != origin) { + int[] temp = new int[origin.length]; + System.arraycopy(origin, 0, temp, 0, origin.length); + for (int index = origin.length; index > 0; index--) { + origin[origin.length - index] = temp[index - 1]; + } + } + } + + /** + * 现在有如下的一个数组: 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 size = 0; + int[] newArray = new int[oldArray.length]; + for (int indexO = 0; indexO < oldArray.length; indexO++) { + if (0 == oldArray[indexO]) { + continue; + } + newArray[size++] = oldArray[indexO]; + } + int[] returnArray = new int[size]; + System.arraycopy(newArray, 0, returnArray, 0, returnArray.length); + return returnArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int[] mergeArray = new int[array1.length + array2.length]; + int size = 0; + for (int index = 0, max = Math.max(array1.length, array2.length); index < max; index++) { + Object arrObj1 = null; + Object arrObj2 = null; + if (index < array1.length) { + arrObj1 = array1[index]; + } + if (index < array2.length) { + arrObj2 = array2[index]; + } + if (null != arrObj1 && Objects.equals(arrObj1, arrObj2)) { + arrObj2 = null; + } + for (int indexMerge = 0; indexMerge < size; indexMerge++) { + if (Objects.equals(arrObj1, mergeArray[indexMerge])) { + arrObj1 = null; + } + if (Objects.equals(arrObj2, mergeArray[indexMerge])) { + arrObj2 = null; + break; + } + } + if (null != arrObj1 && null != arrObj2 && (int) arrObj1 > (int) arrObj2) { + arrObj1 = (int) arrObj1 + (int) arrObj2; + arrObj2 = (int) arrObj1 - (int) arrObj2; + arrObj1 = (int) arrObj1 - (int) arrObj2; + } + if (null != arrObj1) { + mergeArray[size++] = (int) arrObj1; + } + if (null != arrObj2) { + mergeArray[size++] = (int) arrObj2; + } + } + int[] resultArr = new int[size]; + System.arraycopy(mergeArray, 0, resultArr, 0, size); + return resultArr; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for (int indexOld = 0; indexOld < oldArray.length; indexOld++) { + newArray[indexOld] = oldArray[indexOld]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int[] newArray = new int[0]; + if (max > 1) { + int size = 0; + newArray = new int[max]; + if (max >= 1) { + for (int n = 1; n < Integer.MAX_VALUE; n++) { + int fibonacci = mFibonacci(n); + if (fibonacci < max) { + newArray[size++] = fibonacci; + } else { + break; + } + } + } + int[] resultArr = new int[size]; + System.arraycopy(newArray, 0, resultArr, 0, size); + newArray = resultArr; + } + return newArray; + } + + private int mFibonacci(int n) { + if (n == 0) return 0; + else if (n == 1) return 1; + else return mFibonacci(n - 1) + mFibonacci(n - 2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] newArray = new int[max]; + int size = 0; + for (int index = 2; index < max; index++) { + if (isPrimes(index)) { + newArray[size++] = index; + } + } + int[] resultArr = new int[size]; + System.arraycopy(newArray, 0, resultArr, 0, size); + return resultArr; + } + + private boolean isPrimes(int number) { + int i = 1; + while (++i < number) { + if (number % i == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] newArray = new int[max]; + int size = 0; + for (int i = 6; i < max; i++) { + int total = 0; + for (int index = 1, length = i / 2; index <= length; index++) { + if (i % index == 0) { + total += index; + } + } + if (i == total) { + newArray[size++] = i; + } + } + int[] resultArr = new int[size]; + System.arraycopy(newArray, 0, resultArr, 0, size); + return resultArr; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder arrBuilder = new StringBuilder(); + for (int arr : array) { + if (arrBuilder.length() > 0) { + arrBuilder.append(seperator); + } + arrBuilder.append(arr); + } + return arrBuilder.toString(); + } + + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/Struts.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/Struts.java new file mode 100644 index 0000000000..0726c17779 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/Struts.java @@ -0,0 +1,64 @@ +package com.java.xiaoqin.litestruts; + +import com.java.xiaoqin.litestruts.bean.View; +import com.java.xiaoqin.litestruts.manager.StrutsParseManager; +import com.java.xiaoqin.litestruts.util.ReflectUtils; + +import java.net.URL; +import java.util.Map; + + +public class Struts { + + private static final String METHOD_EXECUTE = "execute"; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + URL resourceURL = Struts.class.getResource("/struts.xml"); + StrutsParseManager.getInstance().init(resourceURL.getPath()); + + String className = StrutsParseManager.getInstance().findClassNameByActionName(actionName); + + Object instance = ReflectUtils.newInstance(className); + if (null != parameters) { + for (Map.Entry paramSet : parameters.entrySet()) { + ReflectUtils.setMethod(instance, paramSet.getKey(), paramSet.getValue()); + } + } + + Object resultExecute = ReflectUtils.executeMethod(instance, METHOD_EXECUTE); + + Map getMap = ReflectUtils.executeGets(instance); + + View view = new View(); + view.setParameters(getMap); + + if (resultExecute instanceof String) { + String result = StrutsParseManager.getInstance().getResult(actionName, (String) resultExecute); + view.setJsp(result); + } + + return view; + } + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LoginAction.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LoginAction.java new file mode 100644 index 0000000000..de6a7aefca --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.java.xiaoqin.litestruts.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LogoutAction.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LogoutAction.java new file mode 100644 index 0000000000..b26cce5630 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LogoutAction.java @@ -0,0 +1,7 @@ +package com.java.xiaoqin.litestruts.action; + +/** + * Created by xiaoqin on 17-3-5. + */ +public class LogoutAction { +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/ActionBean.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/ActionBean.java new file mode 100644 index 0000000000..91f9f8f213 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/ActionBean.java @@ -0,0 +1,41 @@ +package com.java.xiaoqin.litestruts.bean; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by xiaoqin on 17-3-5. + */ +public class ActionBean { + private String className; + private Map result; + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public Map getResult() { + return result; + } + + public void setResult(Map result) { + this.result = result; + } + + public void addResult(String name, String text) { + if (null == result) result = new HashMap<>(); + result.put(name, text); + } + + @Override + public String toString() { + return "ActionBean{" + + "className='" + className + '\'' + + ", result=" + result + + '}'; + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/View.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/View.java new file mode 100644 index 0000000000..d3cb44085c --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/View.java @@ -0,0 +1,23 @@ +package com.java.xiaoqin.litestruts.bean; + +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/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/manager/StrutsParseManager.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/manager/StrutsParseManager.java new file mode 100644 index 0000000000..fc7ac10f89 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/manager/StrutsParseManager.java @@ -0,0 +1,95 @@ +package com.java.xiaoqin.litestruts.manager; + +import com.java.xiaoqin.litestruts.bean.ActionBean; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; + +/** + * Created by xiaoqin on 17-3-5. + */ +public class StrutsParseManager { + + private final static String ACTION = "action"; + private final static String ACTION_NAME = "name"; + private final static String ACTION_CLASS = "class"; + private static final String RESULT_NAME = "name"; + + private final static StrutsParseManager INSTANCE = new StrutsParseManager(); + + + private Map mActionBeanMap = new HashMap<>(); + + private StrutsParseManager() { + } + + public static StrutsParseManager getInstance() { + return INSTANCE; + } + + public void init(String path) { + if (!Objects.isNull(path) && !"".equals(path)) { + parseXML(path); + } else { + throw new NullPointerException("path is null"); + } + } + + private void parseXML(String path) { + try { + SAXReader reader = new SAXReader(); + Document document = reader.read(new File(path)); + Element rootElement = document.getRootElement(); + Iterator actionIterator = rootElement.elementIterator(ACTION); + while (actionIterator.hasNext()) { + Object actionObj = actionIterator.next(); + if (actionObj instanceof Element) { + String name = ((Element) actionObj).attributeValue(ACTION_NAME); + String className = ((Element) actionObj).attributeValue(ACTION_CLASS); + ActionBean actionBean = new ActionBean(); + actionBean.setClassName(className); + mActionBeanMap.put(name, actionBean); + Iterator iteratorResult = ((Element) actionObj).elementIterator(); + while (iteratorResult.hasNext()) { + Object resultObj = iteratorResult.next(); + if (resultObj instanceof Element) { + String resultName = ((Element) resultObj).attributeValue(RESULT_NAME); + String resultText = ((Element) resultObj).getText(); + actionBean.addResult(resultName, resultText); + } + } + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } + } + + + public String findClassNameByActionName(String actionName) { + String className = ""; + if (mActionBeanMap.containsKey(actionName)) { + className = mActionBeanMap.get(actionName).getClassName(); + } + return className; + } + + public String getResult(String actionName, String result) { + String resultResult = ""; + if (mActionBeanMap.containsKey(actionName)) { + Map actionResult = mActionBeanMap.get(actionName).getResult(); + if (null != actionResult && actionResult.containsKey(result)) { + resultResult = actionResult.get(result); + } + + } + return resultResult; + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/util/ReflectUtils.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/util/ReflectUtils.java new file mode 100644 index 0000000000..1fc3ff1e1c --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/util/ReflectUtils.java @@ -0,0 +1,75 @@ +package com.java.xiaoqin.litestruts.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by xiaoqin on 17-3-5. + */ +public class ReflectUtils { + + public static Object newInstance(String className) { + Object obj = null; + try { + Class aClass = Class.forName(className); + obj = aClass.newInstance(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + return obj; + } + + public static void setMethod(Object obj, String methodName, String paramter) { + try { + StringBuilder methodNameBuilder = new StringBuilder(); + methodNameBuilder.append("set").append(methodName.substring(0, 1).toUpperCase()).append(methodName.substring(1)); + Method method = obj.getClass().getMethod(methodNameBuilder.toString(), String.class); + method.invoke(obj, paramter); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + public static Object executeMethod(Object obj, String methodName) { + Object resultObj = null; + try { + Method method = obj.getClass().getMethod(methodName); + resultObj = method.invoke(obj); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return resultObj; + } + + public static Map executeGets(Object obj) { + Map resultMap = new HashMap<>(); + try { + Method[] methods = obj.getClass().getMethods(); + for (Method method : methods) { + String methodName = method.getName(); + if (methodName.startsWith("get")) { + resultMap.put(methodName.substring(3, 4).toLowerCase() + methodName.substring(4), method.invoke(obj)); + } + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return resultMap; + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/array/ArrayUtilTest.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/array/ArrayUtilTest.java new file mode 100644 index 0000000000..ea196a50ca --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/array/ArrayUtilTest.java @@ -0,0 +1,110 @@ +package test.com.java.xiaoqin.array; + +import com.java.xiaoqin.array.ArrayUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * ArrayUtil Tester. + * + * @author + * @version 1.0 + * @since
三月 4, 2017
+ */ +public class ArrayUtilTest { + + private ArrayUtil mArrayUtil; + + @Before + public void before() throws Exception { + mArrayUtil = new ArrayUtil(); + } + + @After + public void after() throws Exception { + mArrayUtil = null; + } + + /** + * Method: reverseArray(int[] origin) + */ + @Test + public void testReverseArray() throws Exception { + int[] origin = {7, 9, 30, 3}; + mArrayUtil.reverseArray(origin); + System.out.println(Arrays.toString(origin)); + } + + /** + * Method: removeZero(int[] oldArray) + */ + @Test + public void testRemoveZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + System.out.println(Arrays.toString(mArrayUtil.removeZero(oldArr))); + } + + /** + * Method: merge(int[] array1, int[] array2) + */ + @Test + public void testMerge() throws Exception { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] mergeArr = mArrayUtil.merge(a1, a2); + System.out.println(Arrays.toString(mergeArr)); + } + + /** + * Method: grow(int[] oldArray, int size) + */ + @Test + public void testGrow() throws Exception { + int[] oldArray = {2, 3, 6}; + int[] growArray = mArrayUtil.grow(oldArray, 3); + System.out.println(Arrays.toString(growArray)); + } + + /** + * Method: fibonacci(int max) + */ + @Test + public void testFibonacci() throws Exception { + int[] fibonacciArray = mArrayUtil.fibonacci(1); + System.out.println(Arrays.toString(fibonacciArray)); + fibonacciArray = mArrayUtil.fibonacci(15); + System.out.println(Arrays.toString(fibonacciArray)); + } + + /** + * Method: getPrimes(int max) + */ + @Test + public void testGetPrimes() throws Exception { + int[] primes = mArrayUtil.getPrimes(23); + System.out.println(Arrays.toString(primes)); + } + + /** + * Method: getPerfectNumbers(int max) + */ + @Test + public void testGetPerfectNumbers() throws Exception { + int[] perfectNumbers = mArrayUtil.getPerfectNumbers(10000); + System.out.println(Arrays.toString(perfectNumbers)); + } + + /** + * Method: join(int[] array, String seperator) + */ + @Test + public void testJoin() throws Exception { + String join = mArrayUtil.join(new int[]{3, 8, 9}, "-"); + System.out.println(join); + } + + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/litestruts/StrutsTest.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a6b8645648 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package test.com.java.xiaoqin.litestruts; + +import com.java.xiaoqin.litestruts.Struts; +import com.java.xiaoqin.litestruts.bean.View; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group05/810574652/.classpath b/group05/810574652/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group05/810574652/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group05/810574652/.gitignore b/group05/810574652/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/810574652/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/810574652/.project b/group05/810574652/.project new file mode 100644 index 0000000000..292266f147 --- /dev/null +++ b/group05/810574652/.project @@ -0,0 +1,17 @@ + + + 810574652HomeWork + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework1/ArrayList.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/ArrayList.java new file mode 100644 index 0000000000..2fb0968eb5 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/ArrayList.java @@ -0,0 +1,81 @@ +package com.github.PingPi357.coding2017.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 100; // 定义静态final类型, + // final在一个对象类唯一, + // static + // final在多个对象中都唯一,如果作为常量用的话,只是final的话就得在别的类引用的时候要创建对象,会占用不必要的空间,而加上static的话在编译的时候占用一个空间,其他时候就不会再占用空间了。所以常量一般用static修饰,其他时候看你自己的需要 + // 理解:比如我是一个维修工人....public相当于我接任何活,static相当于10分钟之内赶到,final相当于就我一家. + // reference: http://bbs.itheima.com/thread-162971-1-1.html + + int size = 0; // 如果是中文分号,报错:Syntax error on token "invalid Character", ; + // excepted; + + private Object[] elementData = new Object[DEFAULT_CAPACITY]; // new + // Object后面接的是中括号 + + @Override + public void add(Object o) { + // TODO Auto-generated method stub + ensureCapacity(++size); // size自加操作 + elementData[size - 1] = o; + } + + private void ensureCapacity(int minCapacity) { + // TODO Auto-generated method stub + if (minCapacity <= elementData.length) { // 当满足小于等于的情况 ??? + return; + } else { + elementData = Arrays.copyOf(elementData, minCapacity); // elementData写错为elementDta,导致报错 elementDta + // cannot be + // resolved + // as a + // variable + // 这里为什么不直接是minCapacity 而要加上原始elementData.length???? + } + } + + @Override + public void add(int index, Object o) { + // TODO Auto-generated method stub + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + ensureCapacity(++size); // size自加操作 + for (int i = size - 2; i > index - 1; i--) { // for中用';'号隔开; + // ???i应该从size-2开始赋值 + // ???终止条件为i>index-1 + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; // 插入元素 + } + + @Override + public Object remove(int index) { + if (this.size > 0 && index < (this.size)) { + Object o = elementData[index]; + for (int i = index; i < this.size; i++) { + elementData[i] = elementData[i + 1]; + } + this.size--; + return o; + } else { + return null; + } + } + + @Override + public Object get(int index) { + // TODO Auto-generated method stub + return elementData[index]; // 必须进行角标越界检查吗??? + } + + @Override + public int size() { + // TODO Auto-generated method stub + return this.size; + } + +} diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework1/LinkedList.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/LinkedList.java new file mode 100644 index 0000000000..4e1ebda327 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/LinkedList.java @@ -0,0 +1,131 @@ +package com.github.PingPi357.coding2017.basic; + + +//实现单向单端列表 +public class LinkedList implements List { + private Node head = new Node(); // 定义链表的头节点 + private Node current; + private int size; + + public LinkedList() { + head = null; + size = 0; + } + + @Override + public void add(Object o) { + if (null == head) { // 头结点为空,直接放在头结点上 + head.data = o; + head.next = null; + size++; + } else { + current = head; + while (!(current.next == null)) { // 找到链表的末尾节点,在其后增加 + current = current.next; + } + current.next = new Node(); + current.next.data = o; + current.next.next = null; + size++; + } + } + + public void addFirst(Object o) { + if (null == head) { + head.data = o; + head.next = null; + size++; + } else { + current = new Node(); + current.data = o; + current.next = head; + head = current; + size++; + } + } + + public void addLast(Object o) { + if (!(head == null)) { + current = head; + while (!(current.next == null)) { + current = current.next; + } + current.next = new Node(); + current.next.data = o; + current.next.next = current.next; + size++; + } + } + + @Override + public void add(int index, Object o) { + // TODO Auto-generated method stub + + } + + @Override + public Object remove(int index) { + // TODO Auto-generated method stub + if (index < size && index > 1) { // 为什么index>1??? + current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; // 遍历到指定到index的上一个节点 + } + current.next = current.next.next; + size--; + } + return null; + } + + public Object removeFirst() { + if (!(head == null)) { + Node removeHead = head; + head.next = head.next.next; + size--; + return removeHead; + } else + return null; + } + + public Object removeLast() { + if (!(head == null)) { + Node theNext = current.next; + Node oldLast; + while (theNext.next != null) { + current = theNext; + theNext = theNext.next; + } + oldLast = current.next; + current.next = theNext.next; + + size--; + return oldLast; + } else + return null; + + } + + @Override + public Object get(int index) { + // TODO Auto-generated method stub + if (index < size) { + current = head; + for (int i = 0; i < index; i++) { + current = current.next; + } + return current.data; + } else + return null; + } + + @Override + public int size() { + return this.size; + } + + private static class Node { // 创建Node类,定义Node类的两个属性 + Object data; + Node next; + } + +} diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework1/List.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/List.java new file mode 100644 index 0000000000..d9cbdec7b4 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/List.java @@ -0,0 +1,17 @@ +package com.github.PingPi357.coding2017.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); // Object 首字母大写,不然会出现"object" can not + // be resolved a type错误 + + public Object remove(int index); + + public Object get(int index); + + public int size(); // 获取List的实际大小,而length定义的是总的容量 + /* + * public void change(int index, Object o); 实现改变特定index处的值 + */ +} \ No newline at end of file diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework1/Queue.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/Queue.java new file mode 100644 index 0000000000..709d0f4059 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/Queue.java @@ -0,0 +1,18 @@ +package com.github.PingPi357.coding2017.basic; + +public class Queue { + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o) { + linkedList.addLast(o); + } + + public Object deQueue() { + Object removeQueue = linkedList.removeFirst(); + return removeQueue; + } + + public int size() { + return linkedList.size(); + } +} diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework1/Stack.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/Stack.java new file mode 100644 index 0000000000..2267cd05d4 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/Stack.java @@ -0,0 +1,33 @@ +package com.github.PingPi357.coding2017.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + int size = 0; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(this.isEmpty()==true) + { + throw new EmptyStackException(); + } + ArrayList popData = (ArrayList) elementData.remove(elementData.size()-1); + size--; + return popData; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return this.size; + } +} diff --git "a/group05/810574652/src/com/github/PingPi357/coding2017/homework1/\350\257\264\346\230\216" "b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/\350\257\264\346\230\216" new file mode 100644 index 0000000000..8fd2d3fbb1 --- /dev/null +++ "b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/\350\257\264\346\230\216" @@ -0,0 +1,2 @@ +代码实现主要参考了waking2017(441517454)和C-Bobo(183127807)两位的代码完成, +自己仿照敲了一遍,加了些注释,同时在此基础上改了些 diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework2/ArrayUtil.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/ArrayUtil.java new file mode 100644 index 0000000000..b12c28c589 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/ArrayUtil.java @@ -0,0 +1,203 @@ +package com.github.PingPi357.coding2017.homework2; + +import java.lang.reflect.Array; +import java.util.ArrayList; +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 int[] reverseArray(int[] origin) { + int len = origin.length; + int[] temp = new int[len]; + for (int i = 0; i < len; i++) { + temp[i] = origin[--len]; + } + return 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 int[] removeZero(int[] oldArray) { + int[] newArray = new int[oldArray.length]; + int i = 0; + for (int element : oldArray) { + if (element != 0) { + newArray[i++] = element; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + ArrayList list = new ArrayList(); + int k1=0; + int k2 = 0; + for (int i = 0; i < array1.length; i++) { + for (int j = k2; j < array2.length; j++) { + if (array1[i] < array2[j]) { + list.add(array1[i]); + k1++; + break; + } else if(array1[i] > array2[j]) { + list.add(array2[j]); + k2++; + }else{ + list.add(array1[i]); + k1++; + k2++; + break; + } + } + if(k2==array2.length-1){ + for (; k1 <= array1.length-1 ; k1++) { + list.add(array1[k1]); + } + break; + } + if(k1== array1.length-1){ + for (; k2 <= array2.length-1 ; k2++) { + list.add(array2[k2]); + } + break; + } + } + + + int[] mergeArray = arrayListToArray(list); + return mergeArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] growArray = Arrays.copyOf(oldArray, oldArray.length + size); + return growArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int a = 1; + int b = 1; + int fibMaxNum = 1; + ArrayList list=new ArrayList(); + list.add(1); + if (max <= 1) { + return new int[0]; + } + while (max > fibMaxNum) { + list.add(fibMaxNum); + fibMaxNum = a + b; + b = fibMaxNum; + a = b; + } + return arrayListToArray(list); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + ArrayList list =new ArrayList(); + for(int i=2; i <=max-1;i++){ + boolean flag=true; + for(int j=2; j < Math.floor(i/2); j++){ + if(i%j==0){ + flag=false; + break; + } + } + if(!flag){ + list.add(i); + } + } + return arrayListToArray(list); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList list =new ArrayList(); + int sum=0; + if(max<=1){ + return null; + } + for(int i=1;i <=(max-1);i++){ + for(int j=1; j<=i/2;j++){ + if(i%j==0){ + sum+=j; + } + } + if(sum==i){ + list.add(i); + } + } + return arrayListToArray(list); + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb=new StringBuilder(); + sb.append(array[0]); + for(int i=1;i list) { + int[] Array = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + Array[i] = (int) list.get(i); + } + return Array; + } +} diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/LoginAction.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/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/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/Struts.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/StrutsTest.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/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/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/View.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/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/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/struts.xml b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/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/group05/810574652/src/com/github/PingPi357/coding2017/homework2/\350\257\264\346\230\216" "b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/\350\257\264\346\230\216" new file mode 100644 index 0000000000..0ef3e2e2f8 --- /dev/null +++ "b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/\350\257\264\346\230\216" @@ -0,0 +1 @@ +"litestruts"作业暂时超出能力范围内,仍在查找资料解决中...... \ No newline at end of file diff --git a/group05/group05.md b/group05/group05.md index d3f5a12faa..8b13789179 100644 --- a/group05/group05.md +++ b/group05/group05.md @@ -1 +1 @@ - + diff --git a/group06/1049564215/src/com/coding/basic/MyArrayList.java b/group06/1049564215/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..13a0bd64a0 --- /dev/null +++ b/group06/1049564215/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,49 @@ +import java.util.*; + +/** + * @author CCD + * + */ +public class MyArrayList { + + private Object[] elementData = new Object[100]; + private int size = 100 ; + + public void add(Object o){ + elementData[size++] = o; + } + public void add(int index, Object o){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+ + "index is less than 0"); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+ + "index is less than 0"); + return elementData[index]; + } + + public Object remove(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException("index "+ index +"is biger than size" + size+ + "index is less than 0"); + Object E = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, + size - index - 1); + elementData[--size] = null; + return E; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } +} diff --git a/group06/1049564215/src/com/coding/basic/MyLinkedList.java b/group06/1049564215/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..275bf14a4f --- /dev/null +++ b/group06/1049564215/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,124 @@ +import java.util.*; + +public class MyLinkedList implements List { + + private Node first; + private Node last; + private int size = 0 ; + public void Myadd(Object o){ + final Node l = last; + final Node newNode = new Node(l,o,null); + last = newNode; + if(l == null) + first = newNode; + else + l.next = newNode; + size++; + } + public void Myadd(int index , Object o){ + checkPosition(index); + if(index == size){ + Myadd(o); + } + else{ + final Node PreNode =GetNodeByIndex(index).prev; + final Node newNode = new Node(PreNode,o,GetNodeByIndex(index)); + PreNode.next =newNode; + if(PreNode == null) + first = newNode; //ΪʲôҪָ룿 + else + PreNode.next = newNode; + size++; + } + } + public Object get(int index){ + checkPosition(index); + return GetNodeByIndex(index); + } + public void remove(int index){ + Node node = GetNodeByIndex(index); + node.prev.next = node.next; + node.next.prev = node.prev; + node = null; + size--; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + final Node FirstNode= first; + final Node newNode = new Node(null,o,first); + first = newNode; + if(FirstNode == null) + last = newNode; + else + first.prev = newNode; + size++; + + } + public void addLast(Object o){ + final Node LastNode = last; + final Node newNode = new Node(last,o,null); + last = newNode; + if(last == null) + first = newNode; + else + last.next = newNode; + size++; + } + public void removeFirst(){ + final Node f = first; + if(f == null) + throw new NoSuchElementException(); + first = f.next; + first = null; + size--; + } + public void removeLast(){ + final Node f = last; + if(f == null) + throw new NoSuchElementException(); + last = last.prev; + last = null; + size--; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object item; + Node next; + Node prev; + Node (Node prev,Object element ,Node next){ + this.item = element; + this.next = next; + this.prev = prev; + } + } + + private Node GetNodeByIndex(int index){ + if(index > size/2) + { + Node Temp = first; + for(int i = 0; i< index;i++) + Temp = Temp.next; // + return Temp; + } + else + { + Node Temp = last; + for(int i = size-1; i> index; i--) + Temp = Temp.prev; + return Temp; + } + } + + private void checkPosition(int index){ + if(index < 0 || index > size) + throw new IndexOutOfBoundsException("index:"+ index+"is llegal"); + } +} \ No newline at end of file diff --git a/group06/1049564215/src/com/coding/basic/MyQueue.java b/group06/1049564215/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..c36703c145 --- /dev/null +++ b/group06/1049564215/src/com/coding/basic/MyQueue.java @@ -0,0 +1,52 @@ + +/** + * @author CCD + * + */ + +import java.util.*; + +public class MyQueue { + + private static final int DEFAULT_SIZE = 10; + + private Object[] elementData; + private int head; + private int tail; + public MyQueue(){ + this(DEFAULT_SIZE); + } + public MyQueue(int size){ + this.elementData = new Object[size]; + this.head = 0; + this.tail = 0; + } + + public void enQueue(Object o){ + if((tail+1)%elementData.length == head){ + } + else{ + elementData[tail] = o; + tail = (tail+1)%elementData.length; + } + } + + public Object deQueue(){ + if(head == tail){ + return null; + } + else{ + Object o = elementData[head]; + head = (head+1)% elementData.length; + return o ; + } + } + + public boolean isEmpty(){ + return head == tail ; + } + + public int size(){ + return (tail-head)&(elementData.length -1); + } +} diff --git a/group06/1049564215/src/com/coding/basic/MyStack.java b/group06/1049564215/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..f7968adb00 --- /dev/null +++ b/group06/1049564215/src/com/coding/basic/MyStack.java @@ -0,0 +1,45 @@ +import java.util.*; + +/** + * + */ + +/** + * @author CCD + * + */ +public class MyStack { + + private ArrayList elementData = new ArrayList(); + private Object[] Myelement = elementData.toArray(); + private int Length = elementData.size(); + + public void push(Object E){ + Myelement[++Length] = E ; + } + + public Object pop(){ + int NowLength = size()-1; + Object obj = peek(); + Length--; + Myelement[Length] = null ; + return obj; + } + + public Object peek(){ + int NowLength = size(); + if(NowLength == 0) + throw new EmptyStackException(); + NowLength -= 1 ; + if(NowLength >= Length ) + throw new ArrayIndexOutOfBoundsException(NowLength + " >= " + Length); + return Myelement[NowLength]; + + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return Length; + } +} diff --git a/group06/1259131938/JavaLearning/.classpath b/group06/1259131938/JavaLearning/.classpath new file mode 100644 index 0000000000..18d70f02cb --- /dev/null +++ b/group06/1259131938/JavaLearning/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/1259131938/JavaLearning/.gitignore b/group06/1259131938/JavaLearning/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group06/1259131938/JavaLearning/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1259131938/JavaLearning/.project b/group06/1259131938/JavaLearning/.project new file mode 100644 index 0000000000..63b66d0b73 --- /dev/null +++ b/group06/1259131938/JavaLearning/.project @@ -0,0 +1,17 @@ + + + JavaLearning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/1259131938/JavaLearning/.settings/org.eclipse.jdt.core.prefs b/group06/1259131938/JavaLearning/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..8000cd6ca6 --- /dev/null +++ b/group06/1259131938/JavaLearning/.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.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +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.6 diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/BinaryTreeNode.java b/group06/1259131938/JavaLearning/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group06/1259131938/JavaLearning/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/group06/1259131938/JavaLearning/src/com/coding/basic/Iterator.java b/group06/1259131938/JavaLearning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group06/1259131938/JavaLearning/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/group06/1259131938/JavaLearning/src/com/coding/basic/List.java b/group06/1259131938/JavaLearning/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group06/1259131938/JavaLearning/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/group06/1259131938/JavaLearning/src/com/coding/basic/MyArrayList.java b/group06/1259131938/JavaLearning/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..255b748204 --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,77 @@ +package com.coding.basic; + +/** + * @deprecated 用数组实现list + * @author wang + * + */ +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + // 确保数组大小 + ensureCapacity(size + 1); + // 数组赋值并使得size+1; + elementData[size ++] = o; + size ++; + } + + /** + * 确保数组不越界,否则就扩容; + * @param minCapacity list的大小; + */ + public void ensureCapacity(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = (oldCapacity / 3) * 2 + 1; + if (minCapacity > newCapacity) { + // 对数组扩容 + Object[] newDate = new Object[newCapacity]; + System.arraycopy(elementData, 0, newDate, 0, oldCapacity); + elementData = newDate; + } + } + + public void add(int index, Object o){ + // 对index进行校验: + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size ++; + } + + /** + * 边界检查: + * @param index + */ + private void rangeCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("size" + size + ", index:" + + index); + } + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index, elementData, index - 1, size - index); + return oldValue; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/MyLinkedList.java b/group06/1259131938/JavaLearning/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..89016c4b35 --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + + +public class MyLinkedList implements List { + + private Node headNode; + + private Node endNode; + + private int size; + + public void add(Object o){ + add(size,o); + } + public void add(int index , Object o){ + addBefore(getNode(index),o); + } + + // 执行添加元素: + private void addBefore(Node node,Object o) { + Node newNode = new Node(o, node.prev, node.next); + newNode.prev.next = newNode; + newNode.next.prev = newNode; + size ++; + } + + // 获得元素; + private Node getNode(int index) { + Node rtnNode; + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + if (index < size / 2) { + rtnNode = headNode.next; + for (int i = 0; i < index; i++) { + rtnNode = rtnNode.next; + } + } else { + rtnNode = endNode; + for (int i = size; i > index; i--) { + rtnNode = rtnNode.prev; + } + } + return rtnNode; + } + + public Object get(int index){ + return getNode(index).data; + } + public Object remove(int index){ + return remove(getNode(index)); + } + + private Object remove(Node node) { + node.prev.next = node.next; + node.next.prev = node.prev; + size --; + return node.data; + } + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(headNode.prev); + } + public void addLast(Object o){ + add(endNode.next); + } + public Object removeFirst(){ + remove(headNode); + return headNode.data; + } + public Object removeLast(){ + remove(endNode); + return endNode.data; + } + public Iterator iterator(){ + return null; + } + + public boolean isEmpty(){ + return size == 0; + } + + private static class Node{ + //当前元素,下一个及前一个; + Object data; + Node next; + Node prev; + public Node(Object data,Node prev, Node next) { + this.data = data; + this.next = next; + this.prev = prev; + } + } +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/MyStack.java b/group06/1259131938/JavaLearning/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..e576a1826d --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/MyStack.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + + /** + * 出栈 + * @return + */ + public Object pop(){ + Object oldValue = elementData.get(elementData.size()); + elementData.remove(elementData.size()); + return oldValue; + } + /** + * 查看栈顶元素; + * @return + */ + public Object peek(){ + return elementData.get(elementData.size()); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/1259131938/JavaLearning/src/com/coding/basic/Queue.java b/group06/1259131938/JavaLearning/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..9e84a6c6b8 --- /dev/null +++ b/group06/1259131938/JavaLearning/src/com/coding/basic/Queue.java @@ -0,0 +1,20 @@ +package com.coding.basic; + +public class Queue { + MyLinkedList elementData = new MyLinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.remove(elementData.size()); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group06/1378560653/.classpath b/group06/1378560653/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group06/1378560653/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/1378560653/.gitignore b/group06/1378560653/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/1378560653/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1378560653/.project b/group06/1378560653/.project new file mode 100644 index 0000000000..0feed82399 --- /dev/null +++ b/group06/1378560653/.project @@ -0,0 +1,17 @@ + + + 1378560653Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/1378560653/src/com/coderising/array/ArrayUtil.java b/group06/1378560653/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..11fefff510 --- /dev/null +++ b/group06/1378560653/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,208 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int N = origin.length; + for(int i = 0; i < N/2; i++){ + int temp = origin[i]; + origin[i] = origin[N-i-1]; + origin[N-i-1] = 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 int[] removeZero(int[] oldArray){ + int N = oldArray.length; + int[] newArray = new int[N]; + int j = 0; + for(int i = 0; i < N; i++){ + if(oldArray[i] != 0){ + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int N = array1.length + array2.length; + int[] array3 = new int[N]; + System.arraycopy(array1, 0, array3, 0, array1.length); + for(int i = 0; i < N; i++){ + for(int j = 0; j < array2.length; j++){ + if(array3[i] > array2[j]){ + System.arraycopy(array3, i , array3, i+1, N-i-1); + array3[i] = array2[j]; + } + } + } + return array3; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] zero = new int[0]; + int[] array = new int[100]; + array[0] = 1; + array[1] = 1; + int i = 1; + + if(max == 1){ + return zero; + }else{ + while(array[i] <= max){ + i++; + if(i > array.length){ + grow(array, i*2); + } + array[i+1] = array[i] + array[i-1]; + } + return array; + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + boolean[] prime = new boolean[max + 1]; + int[] zero = new int[0]; + int[] array = new int[max]; + int q = 1; + if(max == 1 || max ==2){ + return zero; + }else{ + for(int i = 3; i < max; i++) + if(i % 2 == 0){ + prime[i] = false; + }else{ + prime[i] = true; + } + + for(int i = 3; i < Math.sqrt(max); i++){//因子;若n是合数,则其所有因子都不超过sqrt(n) + if(prime[i]) + for(int j = 2 * i; j<= max; j += i){//其倍数 + prime[j] = false; + } + } + array[0] = 2; + for(int p = 0; p < max; p++){ + if(prime[p] == true){ + array[q] = p; + q++; + } + } + return array; + } + } + /*int[] zero = new int[0]; + int[] array = new int[100]; + int k = 0; + + if(max == 1 || max == 2){ + return zero; + }else{ + for(int n = 2; n <= max; n++){ + int isPrime = 1; + for(int i = 2; i < n; i++){ + if(n % i == 0){ + isPrime = 0; + break; + } + if(isPrime == 1){ + array[k] = n; + k++; + } + } + } + return array; + }*/ + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] perfectNumbers = new int[max]; + int n = 0; + for(int i = 1; i < max; i++){//i:要判断的数 + int sum = 0; + for(int j = 1; j < i; j++){//j:可能因子 + if(i % j == 0){ + sum += j; + } + } + if(sum == i){ + perfectNumbers[n] = i; + n++; + } + } + return perfectNumbers; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String s = ""; + for(int i = 0; i < array.length; i++){ + s = s + array[i] + seperator; + } + return s; + } + + +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/litestruts/LoginAction.java b/group06/1378560653/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..76547ac3b3 --- /dev/null +++ b/group06/1378560653/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; + } +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/litestruts/StructsTest.java b/group06/1378560653/src/com/coderising/litestruts/StructsTest.java new file mode 100644 index 0000000000..4e761b0b2d --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/StructsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + +public class StructsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/litestruts/Struts.java b/group06/1378560653/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..0e73017f99 --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,56 @@ +package com.coderising.litestruts; + +import java.io.IOException; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction,通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder db = dbf.newDocumentBuilder(); + Document document = db.parse("structs.xml"); + NodeList actionList = document.getElementsByTagName("action"); + + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/litestruts/View.java b/group06/1378560653/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..f68a8c438b --- /dev/null +++ b/group06/1378560653/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; + } +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/litestruts/structs.xml b/group06/1378560653/src/com/coderising/litestruts/structs.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/structs.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/group06/1378560653/src/com/coding/basic/ArrayList.java b/group06/1378560653/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8d9d0c30fb --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/ArrayList.java @@ -0,0 +1,95 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if( size <= elementData.length){ + elementData[size + 1] = o; + size++; + }else{ + elementData = grow(elementData, 1); + elementData[size+1] = o; + size++; + } + } + public void add(int index, Object o){ + Object[] temp = new Object[elementData.length]; + for(int i = 0; i= size){ + return false; + }else{ + return true; + } + } + + @Override + public Object next() { + return elementData[pos]; + } + + } + public static Object[] grow(Object[]src, int size){ + Object[] target = new Object[src.length + size]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } +} diff --git a/group06/1378560653/src/com/coding/basic/BinaryTree.java b/group06/1378560653/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..042d3d8488 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +public class BinaryTree { + private BinaryTreeNode root; + + public BinaryTreeNode getRoot(){ + return root; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode node = new BinaryTreeNode(o); + if(root == null){ + root = node; + root.setLeft(null); + root.setRight(null); + return root; + }else{ + BinaryTreeNode currentNode = root; + BinaryTreeNode parentNode; + while(true){ + parentNode = currentNode; + + if(((Integer)node.getData()) > ((Integer)currentNode.getData())){ + currentNode = currentNode.getRight(); + if(currentNode == null){ + parentNode.setRight(node); + return node; + } + }else{ + currentNode = currentNode.getLeft(); + if(currentNode == null){ + parentNode.setLeft(node); + return node; + } + } + } + } + } + +} diff --git a/group06/1378560653/src/com/coding/basic/BinaryTreeNode.java b/group06/1378560653/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a8745a43c3 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data){ + this.left = null; + this.right = null; + this.data = data; + } + + public Object getData() { + return data; + } + public void setData(int data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } +} + diff --git a/group06/1378560653/src/com/coding/basic/Iterator.java b/group06/1378560653/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group06/1378560653/src/com/coding/basic/LinkedList.java b/group06/1378560653/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..3c82bb9da2 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/LinkedList.java @@ -0,0 +1,164 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + if(null == head){ + head = new Node(o); + head.next = null; + }else{ + Node pt = head; + while(head.next != null){ + pt = pt.next; + } + pt.next = new Node(o); + pt.next.next =null; + } + size++; + } + + public void add(int index , Object o){ + if(index < size){ + Node pt = head; + for(int i = 0; i < index-1; i++){ + pt = pt.next; + } + Node pt1 = pt.next.next; + pt.next = new Node(o); + pt.next.next = pt1; + size ++; + } + } + public Object get(int index){ + if(index < size){ + Node pt = head; + for(int i = 0; i < index; i++){ + pt = pt.next; + } + return pt.data; + }else{ + return null; + } + } + public Object remove(int index){ + if(index < size){ + Node pt = head; + for(int i = 0; i< index -1;i++){ + pt = pt.next; + } + Node pt1 = pt.next; + pt.next = pt1.next; + return pt1.data; + }else{ + return null; + } + } + + public int size(){ + if(null == head){ + size = 0; + }else{ + Node pt = head; + while(pt.next != null){ + size++; + } + } + return size; + } + + public void addFirst(Object o){ + if(null == head){ + head = new Node(o); + head.next = null; + }else{ + Node pt = new Node(o); + pt.next = head; + head = pt; + } + size++; + } + public void addLast(Object o){ + if(null == head){ + head = new Node(o); + head.next = null; + }else{ + Node pt = head; + while(pt.next != null){ + pt = pt.next; + } + pt.next = new Node(o); + Node pt1 = pt.next; + pt1.next = null; + } + size++; + } + public Object removeFirst(){ + if(null != head){ + Node pt = head; + head = pt.next; + size--; + return head.data; + }else{ + return null; + } + } + public Object removeLast(){ + if(null != head){ + Node pt = head; + while(pt.next.next != null){ + pt = pt.next; + } + Node pt1 = pt.next; + pt.next = null; + size--; + return pt1.data; + }else{ + return null; + } + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements Iterator { + LinkedList linkedlist = null; + int pos = 0; + + private LinkedListIterator(LinkedList linkedlist) { + this.linkedlist = linkedlist; + } + @Override + public boolean hasNext() { + pos++; + if(pos >= size){ + return false; + }else{ + return true; + } + } + + @Override + public Object next() { + Node pt = head; + for(int i = 0; i < pos; i++ ){ + while(pt.next != null){ + pt = pt.next; + } + } + return pt.data; + } + + } + + private static class Node{ + public Node(Object o) { + this.data = o; + } + Object data; + Node next; + } +} diff --git a/group06/1378560653/src/com/coding/basic/List.java b/group06/1378560653/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group06/1378560653/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/group06/1378560653/src/com/coding/basic/Queue.java b/group06/1378560653/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a574f4b859 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Queue { + private LinkedList linkedlist = new LinkedList(); + + public void enQueue(Object o){ + linkedlist.add(o); + } + + public Object deQueue(){ + if(!isEmpty()){ + return linkedlist.get(0); + }else{ + return null; + } + } + + public boolean isEmpty(){ + if(size() > 0){ + return false; + }else{ + return true; + } + } + + public int size(){ + return linkedlist.size(); + } +} diff --git a/group06/1378560653/src/com/coding/basic/Stack.java b/group06/1378560653/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..2d0b260880 --- /dev/null +++ b/group06/1378560653/src/com/coding/basic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(!isEmpty()){ + return elementData.remove(size()-1); + }else{ + return null; + } + } + + public Object peek(){ + if(!isEmpty()){ + return elementData.get(size()-1); + }else{ + return null; + } + } + public boolean isEmpty(){ + if(size() > 0){ + return false; + }else{ + return true; + } + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/1454385822/.classpath b/group06/1454385822/.classpath new file mode 100644 index 0000000000..f68e6279c1 --- /dev/null +++ b/group06/1454385822/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group06/1454385822/.gitignore b/group06/1454385822/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/1454385822/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1454385822/.project b/group06/1454385822/.project new file mode 100644 index 0000000000..35750341bb --- /dev/null +++ b/group06/1454385822/.project @@ -0,0 +1,17 @@ + + + 1454385822Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/1454385822/src/com/coding/basic/homework_01/ArrayList.java b/group06/1454385822/src/com/coding/basic/homework_01/ArrayList.java new file mode 100644 index 0000000000..8524b89318 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_01/ArrayList.java @@ -0,0 +1,124 @@ +package com.coding.basic.homework_01; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private int pos = 0; // 当前数组的末尾元素的下一位置 + + private Object[] elementData = new Object[3]; + + public void add(Object o){ + + if(pos >= elementData.length - 1){ + //数组扩容1/3 + elementData = Arrays.copyOf(elementData, elementData.length + elementData.length/3); + } + elementData[pos++] = o; + + } + public void add(int index, Object o){ + + if(pos >= elementData.length - 1){ + //数组扩容1/3 + elementData = Arrays.copyOf(elementData, elementData.length + elementData.length/3); + } + /* + * 情况1.index < pos && pos < elementData.length - 1 + * index 只能在pos前面 + */ + if(index <= pos && pos <= elementData.length - 1){ + Object[] tem = new Object[pos - index]; + System.arraycopy(elementData, index, tem, 0, tem.length); + elementData[index] = o; + System.arraycopy(tem, 0, elementData, index + 1, tem.length); + pos++; + }else{ + throw new IndexOutOfBoundsException(); + } + + + + } + + public Object get(int index){ + if(index < pos){ + return elementData[index]; + } + throw new IndexOutOfBoundsException(); + } + + public Object remove(int index){ + Object result; + //将数字删除并将该数字后面的元素向前移动一位 + if(index < pos ){ //只有index在pos之前才进行删除操作 + result = elementData[index]; + if(pos - index > 1){ //删除的不是最后一个元素 + Object[] tem = new Object[pos - index - 1]; + System.arraycopy(elementData, index + 1, tem, 0, tem.length); + for(int i=0; i i2 ? 1 : (i1 == i2 ? 0 : -1); +// return result; +// } +// +//} diff --git a/group06/1454385822/src/com/coding/basic/homework_01/Iterator.java b/group06/1454385822/src/com/coding/basic/homework_01/Iterator.java new file mode 100644 index 0000000000..b683268cd9 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_01/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic.homework_01; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group06/1454385822/src/com/coding/basic/homework_01/LinkedList.java b/group06/1454385822/src/com/coding/basic/homework_01/LinkedList.java new file mode 100644 index 0000000000..c05bca48ec --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_01/LinkedList.java @@ -0,0 +1,241 @@ +package com.coding.basic.homework_01; + + +public class LinkedList implements List{ + + //private Node head; + private Node pre; //指向当前结点的前一个元素 + private Node pHead; //头节点指向第一个元素 + private Node cur; //指向链表的最后一个元素 + private int num = 0; //链表中的元素个数 + + public void add(Object o){ + Node node = new Node(); + node.data = o; + if(pHead == null){ //链表为空,从第一个元素添加 + pHead = cur = node; + pHead.pre = null; + }else{ + node.pre = cur; //前一结点向后移动一位 + cur.next = node; // 添加元素 + cur = cur.next; //当前结点向后移动一位 + } + num++; //链表数目增1 + } + + /** + * 根据索引找到对应的结点 + * @param index + * @return + */ + public Node findNode(int index){ + Node node = pHead; + int tem = 0; + while(tem++ != index){ + node = node.next; + } + return node; + } + + public void add(int index , Object o){ + if(num == 0 || index == num){ + add(o); + return; + } + if(index <= num-1 && index > 0){ + Node node = new Node(); + node.data = o; + Node tem = findNode(index); + Node preNode = tem.pre; + Node posNode = tem.next; + preNode.next = node; + node.next = posNode; + posNode.pre = node; + num++; + return; + } + if(index == 0){ + Node node = new Node(); + node.data = o; + pHead.pre = node; + node.next = pHead; + pHead = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + } + public Object get(int index){ + if(index <= num - 1 && index >= 0){ + return findNode(index).data; + } + throw new IndexOutOfBoundsException(); + } + + public Object remove(int index){ + Object result; + if(index >0 && index < num - 1){ //删除链表中间的元素 + Node node = findNode(index); + result = node.data; + Node preNode = node.pre; + Node posNode = node.next; + preNode.next = posNode; + posNode.pre = preNode; + num--; + return result; + } + if(index == 0 && num > 0){ //删除第一个元素 + Node node = pHead.next; + result = pHead.data; + node.pre = null; + pHead = node; + num--; + return result; + } + if(index == num - 1 && num > 0){ //删除最后一个元素 + result = cur.data; + cur = cur.pre; + cur.next = null; + num--; + return result; + } + throw new IndexOutOfBoundsException(); + } + + public int size(){ + return num; + } + + public void addFirst(Object o){ + if(num == 0){ + add(o); + return; + } + if(num > 0){ + Node node = new Node(); + node.data = o; + node.pre = null; + node.next = pHead; + pHead = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + + } + public void addLast(Object o){ + if(num == 0){ + add(o); + return; + } + if(num > 0){ + Node node = new Node(); + node.data = o; + node.pre = cur; + cur.next = node; + node.next = null; + cur = node; + num++; + return; + } + throw new IndexOutOfBoundsException(); + } + public Object removeFirst(){ + Object result; + if(num > 0){ + result = pHead.data; + if(num == 1){ + pHead = null; + num = 0; + } + if(num > 1){ + pHead = pHead.next; + pHead.pre = null; + num--; + } + return result; + } + throw new IndexOutOfBoundsException(); + } + + public Object removeLast(){ + Object result; + if(num == 1){ + result = pHead.data; + pHead = null; + num = 0; + return result; + } + if(num > 1){ + + result = cur.data; + cur = cur.pre; + cur.next = null; + num--; + return result; + } + throw new IndexOutOfBoundsException(); + } + public Iterator iterator(){ + return new Iterator(){ + int cur = 0; + Node node = pHead; + @Override + public boolean hasNext() { + if(cur++ < num){ + return true; + } + return false; + } + + @Override + public Object next() { + Object result = node.data; + node = node.next; + return result; + } + + }; + } + + + private static class Node{ + Object data; + Node pre; + Node next; + + } + + public static void main(String[]args){ + LinkedList list = new LinkedList(); + list.add(1); +// list.add(2); +// list.add(3); +// list.add(4); +// list.add(0, 0); +// list.addFirst(0); +// list.addLast(5); +// list.removeFirst(); + System.out.println(list.removeLast()); + Iterator it = list.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + } + +} + + + + + + + + + + + + + + + diff --git a/group06/1454385822/src/com/coding/basic/homework_01/List.java b/group06/1454385822/src/com/coding/basic/homework_01/List.java new file mode 100644 index 0000000000..df248690b7 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_01/List.java @@ -0,0 +1,11 @@ +package com.coding.basic.homework_01; + +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/group06/1454385822/src/com/coding/basic/homework_01/Queue.java b/group06/1454385822/src/com/coding/basic/homework_01/Queue.java new file mode 100644 index 0000000000..3909ea420a --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_01/Queue.java @@ -0,0 +1,42 @@ +package com.coding.basic.homework_01; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + private int num = 0; + + public void enQueue(Object o){ + elementData.add(o); + num++; + } + + public Object deQueue(){ + num--; + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return num <= 0; + } + + public int size(){ + return num; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + queue.enQueue(4); + System.out.println("当前队列的长度为:"+queue.size()); + while(!queue.isEmpty()){ + System.out.println(queue.deQueue()); + } + + } + +} + + + diff --git a/group06/1454385822/src/com/coding/basic/homework_01/Stack.java b/group06/1454385822/src/com/coding/basic/homework_01/Stack.java new file mode 100644 index 0000000000..9b1d2c1439 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_01/Stack.java @@ -0,0 +1,42 @@ +package com.coding.basic.homework_01; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int num = 0; + + public void push(Object o){ + elementData.add(o); + num++; + } + + public Object pop(){ + + return elementData.remove(--num) ; + } + + public Object peek(){ + return elementData.get(num - 1); + } + public boolean isEmpty(){ + return num <= 0 ; + } + public int size(){ + return num; + } + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + System.out.println(stack.peek()); + System.out.println(stack.size()); +// while(!stack.isEmpty()){ +// System.out.println(stack.pop()); +// } + } +} + + + + diff --git a/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtil.java b/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtil.java new file mode 100644 index 0000000000..36c733e7b9 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtil.java @@ -0,0 +1,218 @@ +package com.coding.basic.homework_02.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 + */ + public static void reverseArray(int [] origin){ + if(origin.length > 1){ // 数组的长度大于1置换才有意义 + int tem; + for(int i = 0; i < origin.length / 2; i++){ + tem = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = tem; + } + } + } + + /** + * 现在有如下的一个数组: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 len = 0; + for(int i : oldArray){ + if(i == 0) + continue; + len++; + } + int[] result = new int[len]; + len = 0; + for(int i = 0; i < oldArray.length; i++){ + if(oldArray[i] == 0) + continue; + result[len++] = oldArray[i]; + } + return result; + } + + /** + * 给定两个已经排序好的整形数组 a1,a2,创建一个新的数组a3,使得a3包含a1和a2的所有元素并集 + * 例如a1 = [3,5,7,8] , a2 = [4,5,6,7] 则a3为[3,4,5,6,7,8] + * @param array1 + * @param array2 + * @return + */ + public static int [] merge(int[] array1, int [] array2){ + if(array1.length == 0) + return array2; + if(array2.length == 0) + return array1; + if(array1.length == 0 && array2.length == 0) + return null; + int arrLen1 = 0; + int arrLen2 = 0; + int arrLen3 = 0; + int []result = new int[array1.length + array2.length]; + while(arrLen1 < array1.length || arrLen2 < array2.length){ + if(arrLen1 >= array1.length){ //数组1已经没了 + result[arrLen3++] = array2[arrLen2++]; + continue; + } + if(arrLen2 >= array2.length){ //数组2已经没了 + result[arrLen3++] = array1[arrLen1++]; + continue; + } + if(array1[arrLen1] > array2[arrLen2]){ + result[arrLen3++] = array2[arrLen2++]; + }else if(array1[arrLen1] < array2[arrLen2]){ + result[arrLen3++] = array1[arrLen1++]; + }else{ + result[arrLen3++] = array1[arrLen1++]; + arrLen2++; + } + } + result = Arrays.copyOf(result, arrLen3); + return result; + } + + /** + * 把一个已经存满数据的数组oldArray的容量进行扩容,扩容后的新数据大小为oldArray.length * 2 + * 注意,老数组的元素在新数组中需要保持 + * 例如oldArray = {2,3,6}, size = 3,则返回的新数组为{2,3,6,0,0,0} + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size){ + int[] result = new int [size * 2]; + for(int i = 0; i < oldArray.length; i++){ + result[i] = oldArray[i]; + } + return result; + } + + /** + * 斐波那契数列为:1, 1, 2, 3, 5, 8, 13, 21...... ,给定一个最大值,返回小于该值的数列 + * 例如:max = 15, 则返回的数组应该为{1, 1, 2, 3, 5, 8, 13} + * max = 1, 则返回空数组{} + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max == 1) + return new int[]{}; + int[] result = new int[10]; + int reLen = 2; + result[0] = result[1] = 1; + while(result[reLen - 1] + result[reLen -2] < max){ + if(reLen >= result.length) + result = grow(result, result.length); + result[reLen] = result[reLen - 1] + result[reLen -2]; + reLen++; + } + result = Arrays.copyOf(result, reLen); + + return result; + } + + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23,返回的数组为{2,3,5,7,11,13,17,19} + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if(max <= 2){ + return null; + } + int[] result = new int[10]; + int len = 0; + boolean flag = true; + result[0] = 2; + if(max == 3) + return result; + len++; + for(int i = 3; i < max; i++){ + flag = true; + if(len >= result.length) + result = grow(result, result.length); + for(int j = 2; j < i; j++){ + if(i % j == 0){ + flag = false; + } + } + if(flag == true){ + result[len++] = i; + } + + } + result = Arrays.copyOf(result, len); + return result; + } + + /** + * 所谓"完数",是指这个数恰好等于他的因子之和,例如6 = 1+2+3 + * 给定一个最大值max,返回一个数组,数组中是小于max的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + int sum = 1; + int [] result = new int[10]; + int len = 0; + for(int i = 2;i < max; i++){ + sum = 1; + if(len >= result.length) + result = grow(result, result.length); + for(int j = 2; j < i; j++){ + if(i % j == 0) + sum += j; + } + if(sum == i) + result[len++] = sum; + } + result = Arrays.copyOf(result, len); + return result; + } + + + /** + * 用seperator 把数组array给连接起来 + * 例如array = [3,8,9] ,seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public static String join(int[]array, String seperator){ + String result = new String(); + int len = 0; + result = new Integer(array[0]).toString(); + for(int i = 1; i < array.length; i++){ + result += seperator; + result += array[i]; + } + return result; + } + +} + + + + + + + + + diff --git a/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtilTest.java b/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtilTest.java new file mode 100644 index 0000000000..5c2c13083f --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtilTest.java @@ -0,0 +1,93 @@ +package com.coding.basic.homework_02.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class ArrayUtilTest { + int[] array; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() { + array = new int[]{7, 9, 30, 3}; +// array = new int[]{7, 9, 30, 3, 4}; + ArrayUtil.reverseArray(array); + Assert.assertArrayEquals(new int[]{3, 30, 9, 7}, array); +// Assert.assertArrayEquals(new int[]{4, 3, 30, 9, 7}, array); + } + + @Test + public void testRemoveZero(){ + array = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + Assert.assertArrayEquals(new int[]{1,3,4,5,6,6,5,4,7,6,7,5}, ArrayUtil.removeZero(array)); + } + + @Test + public void testMerge(){ + array = new int[]{3,5,7,8}; + int [] array2 = new int[]{4,5,6,7}; + Assert.assertArrayEquals(new int[]{3,4,5,6,7,8}, ArrayUtil.merge(array, array2)); + } + + @Test + public void testGrow(){ + array = new int[]{2, 3, 6}; + Assert.assertArrayEquals(new int[]{2, 3, 6, 0, 0, 0} ,ArrayUtil.grow(array, 3)); + } + + @Test + public void testFibonacci(){ + Assert.assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, ArrayUtil.fibonacci(15)); +// array = ArrayUtil.fibonacci(15); +// for(int i: array) +// System.out.println(i); + } + + + @Test + public void testPrimes(){ + Assert.assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, ArrayUtil.getPrimes(23)); +// array = ArrayUtil.getPrimes(23); +// for(int i : array){ +// System.out.print(i + " "); +// } + + } + + @Test + public void testPerfectNumbers(){ +// Assert.assertArrayEquals(new int[]{6}, ArrayUtil.getPerfectNumbers(7)); + array = ArrayUtil.getPerfectNumbers(100); + for(int i : array){ + System.out.print(i + " "); + } + } + + @Test + public void testJoin(){ + Assert.assertEquals("3-8-9", ArrayUtil.join(new int[]{3,8,9}, "-")); + } + +} + + + + + + + + + + + + + diff --git a/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LoginAction.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LoginAction.java new file mode 100644 index 0000000000..8156c45e43 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.basic.homework_02.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/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LogoutAction.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LogoutAction.java new file mode 100644 index 0000000000..e563673a89 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LogoutAction.java @@ -0,0 +1,5 @@ +package com.coding.basic.homework_02.litestruts; + +public class LogoutAction { + +} diff --git a/group06/1454385822/src/com/coding/basic/homework_02/litestruts/Struts.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/Struts.java new file mode 100644 index 0000000000..5c6c57cf73 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/Struts.java @@ -0,0 +1,286 @@ +package com.coding.basic.homework_02.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + + private static String jspUrl = null; + private static String resultStatic = null; + + public static View runAction(String actionName, Map params) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, DocumentException{ + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 根据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + View view = new View(); + Element root = getRoot(); + Class clazz = getClazz(actionName, root); + Object obj = clazz.newInstance(); + + methodInvoke(clazz, obj, params); + String result = getExecuteInfo(clazz, obj); + setParams(clazz, obj, view); + getJsp(result, root, actionName); + view.setJsp(jspUrl); + + return view; + } + + /** + * 读取xml文件获得根节点 + * @return + * @throws DocumentException + */ + private static Element getRoot() throws DocumentException{ + //step1:创建SAXReader对象 + SAXReader reader = new SAXReader(); + //step2:读取文件 转换成Document + Document document = reader.read("src/com/coding/basic/homework_02/litestruts/struts.xml"); + return document.getRootElement(); + } + + + /** + * 根据给定的actionName找到对应的Class + * @return + * @throws ClassNotFoundException + */ + @SuppressWarnings("rawtypes") + private static Class getClazz(String actionName, Element node) throws ClassNotFoundException{ + + findClassNameByAttr(node, actionName); + if(resultStatic != null) + return Class.forName(resultStatic); + + throw new ClassNotFoundException(); + } + + /** + * 根据resultName找到对应的jsp路径 + * @param resultName + * @param node + * @return + */ + @SuppressWarnings("unchecked") + private static void getJsp(String resultName, Element node, String actionName){ + + if(node.attributes() != null) + forEachAttr(node.attributes(), actionName, node, resultName); + + List listElement = node.elements(); + for(Element e : listElement) + getJsp(resultName, e, actionName); + + if(jspUrl != null) + return; + } + + /** + * 遍历当前结点的属性 + * @param list + * @param actionName + * @param node + * @param resultName + */ + private static void forEachAttr(List list, String actionName, Element node, String resultName){ + List attrs = node.attributes(); + for(Attribute attr : attrs){ + if(resultName.equals(attr.getValue()) && !"".equals(node.getTextTrim())) + findJspByParentNode(actionName, node); + } + } + + /** + * 根据跟定的action找到对应的jspUrl + * @param actionName + * @param node + */ + private static void findJspByParentNode(String actionName, Element node){ + Element parent = node.getParent(); + if(parent.attributes() != null){ + for(Attribute pattr : (List)parent.attributes()){ + if(actionName.equals(pattr.getValue())) + jspUrl = node.getTextTrim(); + } + } + } + + /** + * 获取execute()方法运行后的信息 + * @param clazz + * @param obj + * @return + * @throws NoSuchMethodException + * @throws SecurityException + * @throws IllegalAccessException + * @throws IllegalArgumentException + * @throws InvocationTargetException + */ + private static String getExecuteInfo(Class clazz, Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Method executeMethod = clazz.getMethod("execute", null); + + return (String)executeMethod.invoke(obj, null); + + } + + /** + * 将类中的getter信息放入View + * @return + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + public static View setParams(Class clazz, Object obj, View view) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Map viewMap = getterAttr(clazz, obj); + view.setParameters(viewMap); + return view; + } + + /** + * 找出当前对象的所有getter方法,将信息放入map中 + * @param clazz + * @return + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + private static Map getterAttr(Class clazz, Object obj) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Map viewMap = new HashMap(); + //获取所有的getter方法 + Method[] methods = clazz.getDeclaredMethods(); + for(Method me : methods){ + if("get".equals(me.getName().substring(0, 3))) + viewMap.put(method2Attr(me), (String) me.invoke(obj, null)); + } + return viewMap; + } + + /** + * 将方法名转换为属性名 + * @param method + * @return + */ + private static String method2Attr(Method method){ + StringBuilder builder = new StringBuilder(); + return builder.append(new Character(method.getName().charAt(3)).toString().toLowerCase()) + .append(method.getName().substring(4)).toString(); + } + + /** + * 调用setXXX给属性设置值 + * @param params + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + private static void methodInvoke(Class clazz, Object obj,Map params) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + //将参数名转换为方法名 + Map methodMap = methodMap(params); + + //将数据set到属性中 + Iterator> it = methodMap.entrySet().iterator(); + while(it.hasNext()){ + Map.Entry entry = it.next(); + Method method = clazz.getMethod(entry.getKey(), String.class); + method.invoke(obj, entry.getValue()); + } + } + + /** + * 将属性名转换为方法名之后放入到map中 + * @param params + * @return + */ + @SuppressWarnings("rawtypes") + private static Map methodMap(Map params){ + Map methodMap = new HashMap(); + Iterator> it = params.entrySet().iterator(); + while(it.hasNext()){ + Map.Entry entry = it.next(); + String attrName = entry.getKey(); + StringBuilder builder = new StringBuilder(); + builder.append("set").append(new Character(attrName.charAt(0)).toString().toUpperCase()).append(attrName.substring(1)); + methodMap.put(builder.toString(), entry.getValue()); + } + return methodMap; + } + + + /** + * 遍历结点的属性找到类名 + * @param attrList + * @return List attrList + * @throws ClassNotFoundException + */ + @SuppressWarnings("unchecked") + private static void findClassNameByAttr(Element node,String actionName) throws ClassNotFoundException{ + + if(!node.attributes().isEmpty()) + For2attr(actionName, node); + + List listElement = node.elements(); + for(Element e : listElement) + findClassNameByAttr(e, actionName); + + if(resultStatic != null) + return; + } + + /** + * 遍历属性找到类名 + * @param actionName + * @param findAction + * @param node + */ + private static void For2attr(String actionName, Element node){ + boolean findAction = false; + for(Attribute attribute : (List)node.attributes()){ + + if(actionName.equals(attribute.getValue())){ + findAction = true; + break; + } + } + for(Attribute attribute : (List)node.attributes()){ + if(findAction == true && "class".equals(attribute.getName())){ + resultStatic = attribute.getValue(); + return; + } + } + } + + +} diff --git a/group06/1454385822/src/com/coding/basic/homework_02/litestruts/StrutsTest.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..fc5f202c3b --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/StrutsTest.java @@ -0,0 +1,46 @@ +package com.coding.basic.homework_02.litestruts; + +import java.lang.reflect.InvocationTargetException; +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 ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, DocumentException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, DocumentException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group06/1454385822/src/com/coding/basic/homework_02/litestruts/View.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/View.java new file mode 100644 index 0000000000..8dfd610cb1 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.basic.homework_02.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/group06/1454385822/src/com/coding/basic/homework_02/litestruts/struts.xml b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/struts.xml new file mode 100644 index 0000000000..53f1245121 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/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/group06/1730798243/.classpath b/group06/1730798243/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group06/1730798243/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/1730798243/.gitignore b/group06/1730798243/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/1730798243/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/1730798243/.project b/group06/1730798243/.project new file mode 100644 index 0000000000..b4bd3f32d4 --- /dev/null +++ b/group06/1730798243/.project @@ -0,0 +1,17 @@ + + + homework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/1730798243/src/com/coding/basic/first/Iterator.java b/group06/1730798243/src/com/coding/basic/first/Iterator.java new file mode 100644 index 0000000000..9ade302dda --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/Iterator.java @@ -0,0 +1,15 @@ +package com.coding.basic.first; + +public interface Iterator { + /** + * 查看是否有下一个元素 + * @return 有的话返回true,否则返回false + */ + public boolean hasNext(); + /** + * 获得下一个元素,也就是当前元素 + * @return 获取当前位置的元素 + */ + public Object next(); + +} diff --git a/group06/1730798243/src/com/coding/basic/first/List.java b/group06/1730798243/src/com/coding/basic/first/List.java new file mode 100644 index 0000000000..f999f13052 --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/List.java @@ -0,0 +1,37 @@ +package com.coding.basic.first; +/** + * 数组的接口 + * @author zap + * + */ + +public interface List { + /** + * 向数组中添加一个元素 + * @param o 添加的元素 + */ + public void add(Object o); + /** + * 向数组中某一个位置添加一个元素 + * @param index 添加元素的位置 + * @param o 添加的元素 + */ + public void add(int index,Object o); + /** + * 从数组中根据位置获取一个元素 + * @param index 想要获取的元素的位置 + * @return 想获取的元素 + */ + public Object get(int index); + /** + * 根据位置移除数组中的一个元素 + * @param index 被移除元素的位置 + * @return 被移除的元素 + */ + public Object remove(int index); + /** + * 获取数组的长度 + * @return 返回数组的长度 + */ + public int size(); +} diff --git a/group06/1730798243/src/com/coding/basic/first/impl/ArrayList.java b/group06/1730798243/src/com/coding/basic/first/impl/ArrayList.java new file mode 100644 index 0000000000..d42381300b --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/impl/ArrayList.java @@ -0,0 +1,87 @@ +package com.coding.basic.first.impl; + +import com.coding.basic.first.List; + +/** + * 实现一个ArrayList + * @author zap + * + */ + +public class ArrayList implements List{ + + private int size; + private Object[] elementData = null; + private static final int DEFAULT_SIZE=100; + + public ArrayList() { + this(DEFAULT_SIZE); + } + + public ArrayList(int size) { + if(size < 0) { + System.out.println("size 必须大于0"); + } else { + this.elementData = new Object[size]; + } + } + + @Override + public int size() { + return size; + } + + @Override + public void add(Object o) { + judgeSize(size+1); + elementData[size++] = o; + } + + @Override + public void add(int index, Object o) { + judgeIndexRangge(index); + System.arraycopy(elementData, index, elementData, index + 1, + size - index);//整体往后挪移--当前往后挪 + elementData[index] = o; + size++; + + } + + @Override + public Object get(int index) { + judgeIndexRangge(index); + return elementData[index]; + } + + @Override + public Object remove(int index) { + judgeIndexRangge(index); + Object e = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, + size - index - 1);//整体往前挪移--后一位往前挪 + size--; + return e; + } + + private void judgeIndexRangge(int index){ + + if (index > size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + + + private void judgeSize(int size) { + if(size > elementData.length){ + Object[] newarr = new Object[elementData.length + DEFAULT_SIZE]; + System.arraycopy(elementData, 0, newarr, 0, elementData.length); + this.elementData = newarr; + } + } + + + +} diff --git a/group06/1730798243/src/com/coding/basic/first/impl/LinkedList.java b/group06/1730798243/src/com/coding/basic/first/impl/LinkedList.java new file mode 100644 index 0000000000..a06b635e04 --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/impl/LinkedList.java @@ -0,0 +1,143 @@ +package com.coding.basic.first.impl; + +import com.coding.basic.first.List; + +/** + * 链表 + * @author zap + * LinkedList + */ +public class LinkedList implements List { + + + + private Node head = new Node(); + private int size ;// + + private static class Node{ + Object data; + Node next; + + public Node() { + } + + public Node(Object data) { + this.data = data; + } + + } + + + @Override + public void add(Object o) { + + if(size==0){ + Node node = new Node(o); + head = node; +// head = tail = node; + }else{ + Node current = getCurrentNode(size); + Node node = new Node(o); + current.next = node; +// tail = node; + } + size ++; + + + } + + @Override + public void add(int index, Object o) { + judgeIndexRangge(index); + if(size == 0 ){ + Node node = new Node(o); + head = node; +// head = tail = node; + }else if(index == 0){ + Node node = new Node(o); + node.next = head; + head = node; + }else{ + Node prev = getCurrentNode(index); + Node temp = prev.next; + Node node = new Node(o); + node.next = temp; + prev.next = node; + } + size ++; + + } + + @Override + public Object get(int index) { + judgeGetIndexRangge(index); + Node current = getCurrentNode(index + 1); + return current.data; + } + + @Override + public Object remove(int index) { + judgeIndexRangge(index);//下标 + Object obj = null; + if(index == 0){ + Node node = head.next; + obj = head.data; + head = node; + }else{ + Node prev = getCurrentNode(index); + Node temp = prev.next; + Node after =temp.next ; + prev.next = after; + obj = temp.data; + } + size -- ; + return obj; + } + + @Override + public int size() { + return size; + } + + + + private void judgeIndexRangge(int index){ + + if (index > size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private void judgeGetIndexRangge(int index){ + + if (index >= size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + + private Node getCurrentNode(int index){ + + Node temp = head; + Node prev = head; + for(int i = 0 ;i < index;i++){//找到该个元素 + prev = temp;// + temp = temp.next;// + } + return prev; + + } + + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + Object o = remove(0); + return o; + } + + +} diff --git a/group06/1730798243/src/com/coding/basic/first/impl/Queue.java b/group06/1730798243/src/com/coding/basic/first/impl/Queue.java new file mode 100644 index 0000000000..a38f33c759 --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/impl/Queue.java @@ -0,0 +1,46 @@ +package com.coding.basic.first.impl; + +/** + * 基本数据结构-队列 + * @author zap + * + */ + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + /** + * 入队操作 + * @param o 入队的元素 + */ + public void enQueue(Object o){ + linkedList.addLast(o); + } + + /** + * 出队操作 + * @return 返回出队的元素 + */ + public Object deQueue(){ + if(!isEmpty()) + return linkedList.removeFirst(); + return null; + } + + /** + * 判断队列是否为空 + * @return 为空返回true,否则返回false + */ + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + /** + * 返回队列的长度 + * @return + */ + public int size(){ + return linkedList.size(); + } +} diff --git a/group06/1730798243/src/com/coding/basic/first/impl/Stack.java b/group06/1730798243/src/com/coding/basic/first/impl/Stack.java new file mode 100644 index 0000000000..9e19850fb7 --- /dev/null +++ b/group06/1730798243/src/com/coding/basic/first/impl/Stack.java @@ -0,0 +1,53 @@ +package com.coding.basic.first.impl; + +/** + * 基本数据结构-栈 + * @author Pxshuo + * + */ + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + /** + * 使一个元素入栈 + * @param o 将要入栈的元素 + */ + public void push(Object o){ + elementData.add(elementData.size(), o); + } + + /** + * 使一个元素出栈 + * @return 返回出栈的元素 + */ + public Object pop(){ + return size() == 0 ? null : elementData.remove(elementData.size() - 1); + } + + /** + * 获得栈顶元素 + * @return 返回栈顶元素 + */ + public Object peek(){ + return elementData.size() == 0 ? null : elementData.get(elementData.size() - 1); + } + + /** + * 查看栈是否为空 + * @return 空的话返回true + */ + public boolean isEmpty(){ + return size() == 0 ? true : false; + } + + /** + * 查看栈中元素的个数 + * @return 返回栈中的个数 + */ + public int size(){ + return elementData.size(); + } + +} diff --git a/group06/1730798243/src/com/coding/test/ArrayListTest.java b/group06/1730798243/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..a92bbef7a3 --- /dev/null +++ b/group06/1730798243/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,58 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.first.List; +import com.coding.basic.first.impl.ArrayList; + +public class ArrayListTest { + + @Before + public void setUp() throws Exception { + } + + + @Test + public void testSize() { + List list = new ArrayList (10); + list.add(2); + list.add(1); + int str = (int)list.remove(1); + assertEquals(1, str); + assertEquals(1, list.size()); + } + + @Test + public void testAddObject() { + List list = new ArrayList (10); + list.add(1); + list.add(3); + list.add(2,2); + + assertEquals(2, list.get(2)); + } + + + @Test + public void testGet() { + List list = new ArrayList (10); + list.add(1); + list.add(3); + list.add(0,2); + assertEquals(2, list.get(0)); + } + + @Test + public void testRemove() { + List list = new ArrayList (10); + list.add(1); + list.add(2); + list.remove(1); + assertEquals(1, list.get(0)); + assertEquals(1, list.size()); + } + +} diff --git a/group06/1730798243/src/com/coding/test/LinkedListTest.java b/group06/1730798243/src/com/coding/test/LinkedListTest.java new file mode 100644 index 0000000000..7d50c34c42 --- /dev/null +++ b/group06/1730798243/src/com/coding/test/LinkedListTest.java @@ -0,0 +1,112 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.first.List; +import com.coding.basic.first.impl.LinkedList; + +public class LinkedListTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + assertEquals(1, list.get(2)); + } + + @Test + public void testAddIntObject() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + assertEquals(8, list.get(3)); + } + + @Test + public void testGet() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + assertEquals(3, list.get(6)); + } + + @Test + public void testRemove() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + list.remove(3); + assertEquals(2, list.get(3)); + } + + @Test + public void testSize() { + List list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + assertEquals(7, list.size()); + } + + @Test + public void testAddLast() { + LinkedList list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + list.addLast(7); + assertEquals(7, list.get(7)); + } + + @Test + public void testRemoveFirst() { + LinkedList list = new LinkedList(); + list.add(0,4); + list.add(1); + list.add(2); + list.add(3); + list.add(0,5); + list.add(3,8); + list.add(5,6);//5418263 + list.removeFirst(); + assertEquals(4, list.get(0)); + assertEquals(6, list.size()); + } + +} diff --git a/group06/1730798243/src/com/coding/test/QueueTest.java b/group06/1730798243/src/com/coding/test/QueueTest.java new file mode 100644 index 0000000000..b6fafbe3f9 --- /dev/null +++ b/group06/1730798243/src/com/coding/test/QueueTest.java @@ -0,0 +1,78 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.first.impl.Queue; + +public class QueueTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testEnQueue() { + Queue queue = new Queue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + assertEquals(5, queue.size()); + } + + @Test + public void testDeQueue() { + Queue queue = new Queue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + for(int i=queue.size();i>0;i=queue.size()){ + if(!queue.isEmpty()){ + System.out.print("i:"); + System.out.println(queue.deQueue()); + } + } + assertEquals(0, queue.size()); + } + + @Test + public void testIsEmpty() { + Queue queue = new Queue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + for(int i=queue.size();i>0;i=queue.size()){ + if(!queue.isEmpty()){ + System.out.print("i:"); + System.out.println(queue.deQueue()); + } + } + assertEquals(0, queue.size()); + } + + @Test + public void testSize() { + Queue queue = new Queue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + for(int i=queue.size();i>0;i=queue.size()){ + if(!queue.isEmpty()){ + System.out.print("i:"); + System.out.println(queue.deQueue()); + } + } + assertEquals(0, queue.size()); + } + +} diff --git a/group06/1730798243/src/com/coding/test/StackTest.java b/group06/1730798243/src/com/coding/test/StackTest.java new file mode 100644 index 0000000000..973b0cca35 --- /dev/null +++ b/group06/1730798243/src/com/coding/test/StackTest.java @@ -0,0 +1,105 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.first.impl.Stack; + +public class StackTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testPush() { + Stack stack = new Stack(); + stack.push("1"); + stack.push("2"); + stack.push("8"); + stack.push("3"); + stack.push("4"); + stack.push("5"); + + for(int i=stack.size();i>0;i=stack.size()){ + if(!stack.isEmpty()){ + System.out.print("i:"); + System.out.println(stack.pop()); + } + } + assertEquals(0,stack.size()); + } + + @Test + public void testPop() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(8); + stack.push(3); + stack.push(4); + stack.push(5); + + for(int i=stack.size();i>0;i=stack.size()){ + if(!stack.isEmpty()){ + System.out.print("i:"); + System.out.println(stack.pop()); + } + } + assertEquals(0,stack.size()); + } + + @Test + public void testPeek() { + + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(8); + stack.push(3); + stack.push(4); + stack.push(5); + assertEquals(5,stack.peek()); + } + + @Test + public void testIsEmpty() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(8); + stack.push(3); + stack.push(4); + stack.push(5); + + for(int i=stack.size();i>0;i=stack.size()){ + if(!stack.isEmpty()){ + System.out.print("i:"); + System.out.println(stack.pop()); + } + } + assertEquals(0,stack.size()); + } + + @Test + public void testSize() { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(8); + stack.push(3); + stack.push(4); + stack.push(5); + + for(int i=stack.size();i>0;i=stack.size()){ + if(!stack.isEmpty()){ + System.out.print("i:"); + System.out.println(stack.pop()); + } + } + assertEquals(0,stack.size()); + } + +} diff --git a/group06/236995728/.classpath b/group06/236995728/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group06/236995728/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/236995728/.gitignore b/group06/236995728/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group06/236995728/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/236995728/.project b/group06/236995728/.project new file mode 100644 index 0000000000..2858b5b710 --- /dev/null +++ b/group06/236995728/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/236995728/.settings/org.eclipse.jdt.core.prefs b/group06/236995728/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group06/236995728/.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/group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000..fa7a796a74 Binary files /dev/null and "b/group06/236995728/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" differ diff --git a/group06/236995728/src/com/coding/basic/ArrayList.java b/group06/236995728/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..54f7ee73d5 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * 2017/2/24 + * @author 236995728 + * + */ +public class ArrayList implements List { + + private static int size = 0; + + private static Object[] elementData = new Object[100]; + + /** + * 添加元素 + */ + @Override + public void add(Object o){ + if(size >= elementData.length){ + grow(size); + } + elementData[size++] = o; + } + + /** + * 按索引添加元素 + */ + @Override + public void add(int index, Object o){ + if(index < 0){ + throw new IllegalArgumentException("param invalid"); + } + if(index >= elementData.length){ + grow(index); + } + for(int i=index; i<=size; i++){ + elementData[i] = elementData[i+1]; + } + elementData[index] = o; + size ++; + } + + /** + * 根据索引获取元素 + */ + @Override + public Object get(int index){ + if(index<0 || index >size){ + throw new IllegalArgumentException("param invalid"); + } + return elementData[index]; + } + + /** + * 根据索引删除元素 + */ + @Override + public Object remove(int index){ + if(index<0 || index >size){ + throw new IllegalArgumentException("param invalid"); + } + Object o = elementData[index]; + for(int i=index;i>1); + elementData = Arrays.copyOf(elementData, newCapacity); + } +} diff --git a/group06/236995728/src/com/coding/basic/ArrayListTest.java b/group06/236995728/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..1159bb1829 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,82 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +/** + * 2017/2/24 + * @author 236995728 + * + */ +public class ArrayListTest { + private static ArrayList list = new ArrayList(); + + @Before + public void setUp() throws Exception { + for(int i=0;i<10;i++){ + list.add(i); + System.out.println(list.get(i)); + } + } + + @Test + public void testAddObject() { + list.add("www"); + assertEquals("www", list.get(10)); + } + + @Test + public void testAddIntObject() { + list.add(101, 101); + assertEquals(101, list.get(101)); + } + + @Test(expected = IllegalArgumentException.class) + public void testAddIntObjectException1(){ + list.add(-1, -1); + } + + @Test + public void testGet() { + assertEquals(1, list.get(1)); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetException1(){ + list.get(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetException2(){ + list.get(11); + } + + @Test + public void testRemove() { + list.remove(3); + assertEquals(4, list.get(3)); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveException1(){ + list.remove(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveException2(){ + list.remove(1000000000); + } + + @Test + public void testSize() { + assertEquals(10, list.size()); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group06/236995728/src/com/coding/basic/BinaryTreeNode.java b/group06/236995728/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group06/236995728/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/group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000..fa7a796a74 Binary files /dev/null and "b/group06/236995728/src/com/coding/basic/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" differ diff --git a/group06/236995728/src/com/coding/basic/Iterator.java b/group06/236995728/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ff93e30377 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group06/236995728/src/com/coding/basic/LinkedList.java b/group06/236995728/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..57ee5a6dd9 --- /dev/null +++ b/group06/236995728/src/com/coding/basic/LinkedList.java @@ -0,0 +1,149 @@ +package com.coding.basic; + +/** + * 2017/2/24 + * @author 236995728 + * 单链表 + */ +public class LinkedList implements List { + + private Node head = new Node(null,null); + private Node current = new Node(null, null); + + private int size = 0; + + /** + * 在尾节点添加节点 + */ + @Override + public void add(Object o){ + Node newNode = new Node(null,o); + if(head.next == null){ + head.next = newNode; + }else{ + current.next = newNode; + } + current = newNode; + size ++; + } + + /** + * 按照索引添加节点 + */ + @Override + public void add(int index , Object o){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + Node newNode = new Node(null,o); + for(int i=0; i size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + for(int i=0; i size){ + throw new IndexOutOfBoundsException("param invalid"); + } + Node node = head; + Node nextNode = null; + Object o = null; + for(int i=0; i { + private int data; + + public TreeData(int data) { + this.data = data; + } + + public int getData() { + return data; + } + + @Override + public String toString() { + return data + ""; + } + + @Override + public int compareTo(Object o) { + return data - ((TreeData)o).data; + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/ArrayList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/ArrayList.java new file mode 100644 index 0000000000..ae64aca982 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/ArrayList.java @@ -0,0 +1,138 @@ +package com.pxshuo.se01.basic.impl; + +import com.pxshuo.se01.basic.Iterator; +import com.pxshuo.se01.basic.List; + +/** + * 实现一个ArrayList + * @author Pxshuo + * + */ + +public class ArrayList implements List{ + + private int size = -1;//数组的长度的下标 + private Object[] elements = new Object[10];//数组内容 + private int addSize = 10;//每次增加的长度 + + @Override + public void add(Object o) { + elements = grow(); + size++; + elements[size] = o;//size与index同一个概念 + } + + @Override + public void add(int index, Object o) { + if (index > size + 1) { + return; + } + elements = grow(); + int moveNum = size - index + 1;//本次操作需要移动的元素的个数; + size++; + if (index >= elements.length - 1) {//按照位置来看 + elements = grow(elements, index - (elements.length - 1)); + size = index;//size与index同一个概念 + } + + /** + * 整体向后移一位 + */ + if(moveNum > 0){ + System.arraycopy(elements, index, elements, index + 1, moveNum); + } +// for(int i = size - 1; i >= index; i--) +// { +// elements[i] = elements[i-1]; +// } + + elements[index] = o; + } + + @Override + public Object get(int index) { + return elements[index]; + } + + @Override + public Object remove(int index) { + if (index > size) { + return null; + } + Object removeEle = elements[index]; + int moveNum = size - index;//本次操作需要移动的元素的个数; + if (moveNum > 0) { + System.arraycopy(elements, index + 1, elements, index, size - index + 1); + } + elements[size] = null; + size--; + return removeEle; + } + + @Override + public int size() { + return size + 1; + } + + /** + * 设置迭代器 + * @return + */ + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator{ + + ArrayList arrayList = null; + int position = -1; + + public ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + position ++; + if (position >= arrayList.size()) { + return false; + } + return true; + } + + @Override + public Object next() { + return arrayList.elements[position]; + } + + } + + /** + * 自动控制是否增加数组长度 + * @return 如果增加一条数据会造成数组溢出,则增加数组的长度,否则不进行改变。 + */ + private Object[] grow(){ + if (size() >= elements.length) { + return grow(elements, addSize); + } + else { + return elements; + } + + } + + /** + * 动态增加数组长度 + * @param src + * @param addSize + * @return + */ + private Object[] grow(Object[] src,int addSize){ + Object[] target = new Object[src.length + addSize]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + + //return Arrays.copyOf(src, src.length + addSize);同理 + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTree.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTree.java new file mode 100644 index 0000000000..69bee72af0 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTree.java @@ -0,0 +1,65 @@ +package com.pxshuo.se01.basic.impl; + +import com.pxshuo.se01.basic.Iterator; + +/** + * 排序二叉树 + * @author Pxshuo + * + */ + +public class BinaryTree { + BinaryTreeNode root = null; + + /** + * 添加一个二叉树的节点 + * @param o + */ + public void add(Comparable o){ + if (root == null) { + root = new BinaryTreeNode(); + root.setData(o); + } + else { + root.insert(o); + } + } + + public Object get(int index){ + Stack findChild = childPath(index); + BinaryTreeNode child = null; + int childNum = 0; + for(;!findChild.isEmpty();){ + childNum = (int)findChild.pop(); + if (childNum != -1) { + child = child.getChild(childNum); + } + else { + child = root; + } + } + return child == null ? null : child.getData(); + } + + public void display(){ + root.display(1); + } + + private Stack childPath(int index) { + Stack findChild = new Stack(); + + while(true){ + if (index == 1 || index <= 0) { + findChild.push(-1); + return findChild; + } + if (index%2 == 1) { + findChild.push(1); + } + else { + findChild.push(0); + } + index = index/2; + } + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTreeNode.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTreeNode.java new file mode 100644 index 0000000000..57cf2da9e1 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTreeNode.java @@ -0,0 +1,109 @@ +package com.pxshuo.se01.basic.impl; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int index = 0; + + public BinaryTreeNode() { + data = null; + left = null; + right = null; + } + + /** + * 差入一个二叉树节点 + * @param o + * @return + */ + public BinaryTreeNode insert(Comparable o){ + if(data == null){ + data = o; + return this; + } + //本节点已经存过数据 + BinaryTreeNode child = new BinaryTreeNode(); + child.setData(o); + if (o.compareTo(data) > 0) { + if (right == null) { + right = child; + } + else { + right.insert(o); + } + } + else {//小于等于的数据放在左子树中 + if (left == null) { + left = child; + } + else { + left.insert(o); + } + } + return child; + } + + /** + * 根据二叉树的位置获取孩子节点 + * @param index 0代表左孩子,1代表右孩子 + * @return + */ + public BinaryTreeNode getChild(int index){ + if(index == 0){ + return getLeft(); + } + else if(index == 1){ + return getRight(); + } + else { + return null; + } + } + + /** + * 用于打印二叉树 + * @param myIndex 在二叉树中的位置--采用完全二叉树 + */ + public void display(int myIndex){ + + System.out.println(myIndex + ":" + data.toString()); + if (left != null) { + left.display(2 * myIndex); + } + if (right != null) { + right.display(2 * myIndex + 1); + } + } + + /////////////////get和set函数/////////////////////////////////////// + + 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 void setIndex(int index) { + this.index = index; + } + + public int getIndex() { + return index; + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/LinkedList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/LinkedList.java new file mode 100644 index 0000000000..eeded51514 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/LinkedList.java @@ -0,0 +1,181 @@ +package com.pxshuo.se01.basic.impl; + +import java.time.Period; + +import com.pxshuo.se01.basic.Iterator; +import com.pxshuo.se01.basic.List; + +public class LinkedList implements List { + + private Node head = new Node(); + private Node tail = null; + private int size = -1;//与index的位置等同 + + //封装空表时候的增加与最后一次的删除-- + + public void add(Object o){ + Node node = new Node(o); + node.next = null; + + if (head.next == null) {//初始化 + head.next = node; + tail = node; + } + else { + tail.next = node; + tail = node; + } + size ++; + } + public void add(int index , Object o){ + if (index > size + 1) { + add(o); + } + else{ + Node prev = head; + Node current = new Node(o); + for(int i=0; i < index; i++) + { + prev = prev.next; + } + current.next = prev.next; + prev.next = current; + size ++; + } + } + public Object get(int index){ + if (index <= size) { + Node node = head; + for(int i = 0; i <= index;i++){ + node = node.next; + } + return node.data; + } + return null; + } + public Object remove(int index){ + Node remove = null; + if (index <= size) { + Node prev = head; + for(int i=0; i < index; i++) + { + prev = prev.next; + } + remove = prev.next; + prev.next = remove.next; + remove.next = null; + + if (index == size) {//设置尾部 + tail = prev; + if (size == 0) { + tail = null; + } + } + size --; + } + return remove != null ? remove.data : null; + } + + public int size(){ + return size + 1; + } + + public void addFirst(Object o){ + Node first = new Node(o); + first.next = head.next; + head.next = first; + if(tail == null) + { + tail = first; + } + size ++; + } + public void addLast(Object o){ + if(tail == null){ + add(o); + } + else { + Node last = new Node(o); + last.next = null; + tail.next = last; + tail = last; + size ++; + } + + } + public Object removeFirst(){ + Node first = head.next; + if(first != null) + { + head.next = first.next; + first.next = null; + } + else { + head.next = null; + } + if (head.next == null) {//如果链表为空 + tail = null; + } + size --; + return first!=null ? first.data : null; + } + public Object removeLast(){ + Node last = head; + for(;last.next != tail; last = last.next ){ + + } + tail = last; + last = last.next; + if(tail == head){//最后一个元素 + head.next=null; + tail = null; + }else { + tail.next = null; + } + + size --; + return last != null? last.data : null; + } + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private static class Node{ + Object data; + Node next; + + public Node() { + } + + public Node(Object data) { + this.data = data; + } + + } + + private class LinkedListIterator implements Iterator{ + LinkedList linkedList = null; + Node position = new Node(); + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + this.position = head; + } + + @Override + public boolean hasNext() { + position = position.next; + if (position == null) { + return false; + } + return true; + } + + @Override + public Object next() { + return position.data; + } + + } + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Queue.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Queue.java new file mode 100644 index 0000000000..5d9f4eee31 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Queue.java @@ -0,0 +1,44 @@ +package com.pxshuo.se01.basic.impl; + +/** + * 基本数据结构-队列 + * @author Pxshuo + * + */ + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + /** + * 入队操作 + * @param o 入队的元素 + */ + public void enQueue(Object o){ + linkedList.addLast(o); + } + + /** + * 出队操作 + * @return 返回出队的元素 + */ + public Object deQueue(){ + return linkedList.removeFirst(); + } + + /** + * 判断队列是否为空 + * @return 为空返回true,否则返回false + */ + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + /** + * 返回队列的长度 + * @return + */ + public int size(){ + return linkedList.size(); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Stack.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Stack.java new file mode 100644 index 0000000000..e6237fa7e8 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Stack.java @@ -0,0 +1,51 @@ +package com.pxshuo.se01.basic.impl; + +/** + * 基本数据结构-栈 + * @author Pxshuo + * + */ + +public class Stack { + private ArrayList elementData = new ArrayList(); + + /** + * 使一个元素入栈 + * @param o 将要入栈的元素 + */ + public void push(Object o){ + elementData.add(elementData.size(), o); + } + + /** + * 使一个元素出栈 + * @return 返回出栈的元素 + */ + public Object pop(){ + return elementData.size() == 0 ? null : elementData.remove(elementData.size() - 1); + } + + /** + * 获得栈顶元素 + * @return 返回栈顶元素 + */ + public Object peek(){ + return elementData.size() == 0 ? null : elementData.get(elementData.size() - 1); + } + + /** + * 查看栈是否为空 + * @return 空的话返回true + */ + public boolean isEmpty(){ + return elementData.size() == 0 ? true : false; + } + + /** + * 查看栈中元素的个数 + * @return 返回栈中的个数 + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/package-info.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/package-info.java new file mode 100644 index 0000000000..5d2c270af8 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/package-info.java @@ -0,0 +1,14 @@ +/** + * 用于实现基本数据类型,包括但不仅限于: + * ArrayList + * Stack + * LinkedList + * Queue + * Tree + * Iterator + */ +/** + * @author Pxshuo + * + */ +package com.pxshuo.se01.basic.impl; \ No newline at end of file diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se02/array/ArrayUtil.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/array/ArrayUtil.java new file mode 100644 index 0000000000..e2d5a07eed --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/array/ArrayUtil.java @@ -0,0 +1,226 @@ +package com.pxshuo.se02.array; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int[] result = new int[origin.length]; + for(int i = 0;i resultList = new ArrayList<>(); + for(int i = 0,j = 0;i array2[j]){ + resultList.add(array2[j++]); + } + else if (array1[i] < array2[j]) { + resultList.add(array1[i++]); + } else { + j++; + } + } + + int[] result = new int[resultList.size()]; + for(int i = 0;i < resultList.size();i++){ + result[i] = resultList.get(i); + } + return result; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] result = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + List resultList = new ArrayList<>(); + resultList.add(1); + resultList.add(1); + int fibon = 1,fibonPrev1 = 1,fibonPrev2 = 1; + while(fibon < max){ + fibonPrev2 = fibon; + fibon = fibonPrev1 + fibonPrev2; + fibonPrev1 = fibonPrev2; + resultList.add(fibon); + } + resultList.remove(resultList.size()-1); + + int[] result = new int[resultList.size()]; + for(int i = 0;i < resultList.size();i++){ + result[i] = resultList.get(i); + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if (max < 2) { + return null; + }else if (max < 4) { + return new int[]{2}; + } + if (max < 6) { + return new int[]{2,3}; + } + List primeList = new ArrayList<>(); + primeList.add(2); + primeList.add(3); + boolean isPrime = true; + /** + * 非2偶数肯定不是素数 + */ + for(int i = 5;i < max; i = i + 2){ + isPrime =true; + for(int j = 1;j= max) { + primeList.remove(primeList.size() - 1); + } + + int[] result = new int[primeList.size()]; + for(int i = 0;i < primeList.size();i++){ + result[i] = primeList.get(i); + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if (max < 7) { + return null; + } + List perfectList = new ArrayList<>(); + int sum = 1; + + for (int i = 2; i < max; i++) { + sum = 1; + for(int j = 2;j < i; j++){ + if (i%j == 0) { + sum += j; + } + } + if (sum == i) { + perfectList.add(sum); + } + } + + if (perfectList.get(perfectList.size() - 1) >= max) { + perfectList.remove(perfectList.size() - 1); + } + + int[] result = new int[perfectList.size()]; + for(int i = 0;i < perfectList.size();i++){ + result[i] = perfectList.get(i); + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String result = ""; + for (int i = 0; i < array.length; i++) { + result = result + array[i] + seperator; + } + if (result.endsWith(seperator)) { + result = result.substring(0, result.length()-1); + } + return result; + } + + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/LoginAction.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/LoginAction.java new file mode 100644 index 0000000000..067a2875b1 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.pxshuo.se02.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/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Struts.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Struts.java new file mode 100644 index 0000000000..6801a838d7 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Struts.java @@ -0,0 +1,151 @@ +package com.pxshuo.se02.litestruts; + +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + Element struts = strutsFactory("struts.xml"); + List actions = struts.elements(); + List resultRefs = new ArrayList<>();//action下面的结果跳转 + String actionClass = "";//获取action的类型 + for (Element element : actions) { + if (actionName.equals(element.attributeValue("name"))) { + actionClass = element.attributeValue("class"); + resultRefs = element.elements();//获取结果跳转 + } + } + + Iterator> iterator = parameters.entrySet().iterator();//规范化传递进来的数据 + + try { + //反射实例化一个对象 + Object action = Class.forName(actionClass).newInstance(); + while (iterator.hasNext()) {//执行set方法 + Map.Entry entry = (Map.Entry) iterator.next(); + Method setMethod = action.getClass().getDeclaredMethod("set" + Tools.upperFirst(entry.getKey()) + ,String.class);//这里的类型值不知道怎么取 + setMethod.invoke(action, entry.getValue()); + } + + //调用exectue + Method exectue = action.getClass().getDeclaredMethod("execute"); + String result = (String)exectue.invoke(action); + + //生成返回列表 + Map actionParam = new HashMap<>(); + Method[] methods = action.getClass().getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().startsWith("get")) { + String methodName = Tools.lowerFirst(method.getName().substring(3)); + String methodValue = (String)method.invoke(action); + actionParam.put(methodName, methodValue); + } + } + + //返回的视图 + View view = new View(); + view.setParameters(actionParam); + for(Element element:resultRefs){ + if (result.equals(element.attributeValue("name"))) { + view.setJsp((String)element.getData()); + } + } + + return view; + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + return null; + } + + public static Element strutsFactory(String filename) { + //读配置文件 + InputStream inputStream = Struts.class.getResourceAsStream(filename); + SAXReader reader = new SAXReader(); + Document document = null; + + try { + document = reader.read(inputStream); + + Element struts = document.getRootElement(); + return struts; + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) { + //strutsFactory("struts.xml"); + //runAction(null, null); + String actionName = "login"; + Element struts = strutsFactory("struts.xml"); + List actions = struts.elements(); + for (Element element : actions) { + if (actionName.equals(element.attributeValue("name"))) { + System.out.println(element.attributeValue("class")); + + for(Element element1:(List)element.elements()){ + System.out.println(element1.getData()); + } + } + } + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/StrutsTest.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..7ed32c89f6 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.pxshuo.se02.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/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Tools.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Tools.java new file mode 100644 index 0000000000..66acb26157 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Tools.java @@ -0,0 +1,33 @@ +package com.pxshuo.se02.litestruts; + +public class Tools { + /** + * 将第一个字母转为大写 + * @param string + * @return + */ + public static String upperFirst(String string) { + if (string == null || ("").equals(string)) { + return ""; + } + string = string.substring(0,1).toUpperCase() + string.substring(1); + return string; + } + + /** + * 将第一个字母转为大写 + * @param string + * @return + */ + public static String lowerFirst(String string) { + if (string == null || ("").equals(string)) { + return ""; + } + string = string.substring(0,1).toLowerCase() + string.substring(1); + return string; + } + + public static void main(String[] args) { + upperFirst("1ame"); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/View.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/View.java new file mode 100644 index 0000000000..5f017e9a8a --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/View.java @@ -0,0 +1,23 @@ +package com.pxshuo.se02.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/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/struts.xml b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/struts.xml new file mode 100644 index 0000000000..227b855f45 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/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/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java new file mode 100644 index 0000000000..9737d269e8 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java @@ -0,0 +1,59 @@ +package com.pxshuo.test; + + +import com.pxshuo.se01.basic.Iterator; +import com.pxshuo.se01.basic.TreeData; +import com.pxshuo.se01.basic.impl.ArrayList; +import com.pxshuo.se01.basic.impl.BinaryTree; +import com.pxshuo.se01.basic.impl.LinkedList; +import com.pxshuo.se01.basic.impl.Queue; +import com.pxshuo.se01.basic.impl.Stack; + +public class Test { + public static void main(String[] args) { +// LinkedList arrayList = new LinkedList(); +// arrayList.add("hello1"); +// arrayList.add("hello2"); +// arrayList.add(9,"hello3"); +// //arrayList.add(10,"hello4"); +// arrayList.addLast("hi"); +// arrayList.addLast("hihi"); +// arrayList.addFirst("hi1"); +// arrayList.removeFirst(); +// arrayList.removeLast(); +// arrayList.add(1,"hi1"); +// arrayList.remove(1); +// //arrayList.add(0, "hi"); +// //arrayList.remove(8); +// //arrayList.remove(0); +// for (Iterator iterator = arrayList.iterator(); iterator.hasNext();) { +// System.out.println("hi"+iterator.next()); +// } + //Queue queue = new Queue(); +// Stack stack = new Stack(); +// +// for (int i = 0; i < 10; i++) { +// //queue.enQueue("test-" + i); +// stack.push("test-" + i); +// } +// for(int i =0; i< 11; i++) +// { +// System.out.println(stack.pop()); +// } +// stack.push("test-" + 233); +// System.out.println(stack.pop()); + + BinaryTree binaryTree = new BinaryTree(); + binaryTree.add(new TreeData(5)); + binaryTree.add(new TreeData(2)); + binaryTree.add(new TreeData(7)); + binaryTree.add(new TreeData(1)); + binaryTree.add(new TreeData(6)); + binaryTree.add(new TreeData(4)); + binaryTree.add(new TreeData(8)); + + System.out.println(binaryTree.get(5).getClass()); + + //binaryTree.display(); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/ArrayListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/ArrayListTest.java new file mode 100644 index 0000000000..26b7b977e6 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/ArrayListTest.java @@ -0,0 +1,42 @@ +package test.com.pxshuo.se01.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.se01.basic.impl.ArrayList; + +public class ArrayListTest { + ArrayList object = new ArrayList(); + + @Test + public void addTest() { + object.add("String"); + Assert.assertEquals("String", object.get(0)); + } + + @Test + public void addIndexTest(){ + object.add(3,"Hello"); + Assert.assertEquals("Hello", object.get(3) ); + } + + @Test + public void removeTest() { + object.add("Hello"); + object.add("Hello"); + object.add("Hello"); + object.add(3,"Hello"); + Assert.assertNotNull(object.get(3)); + object.remove(3); + Assert.assertNull(object.get(3)); + } + + @Test + public void sizeTest(){ + object.add("new"); + object.add("hi"); + object.add(1,"new"); + object.remove(2); + Assert.assertEquals(2, object.size()); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/BinaryTreeTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/BinaryTreeTest.java new file mode 100644 index 0000000000..dc53c5988d --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/BinaryTreeTest.java @@ -0,0 +1,26 @@ +package test.com.pxshuo.se01.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.se01.basic.TreeData; +import com.pxshuo.se01.basic.impl.BinaryTree; + +public class BinaryTreeTest { + BinaryTree object = new BinaryTree(); + + @Test + public void binaryTest() { + BinaryTree binaryTree = new BinaryTree(); + binaryTree.add(new TreeData(5)); + binaryTree.add(new TreeData(2)); + binaryTree.add(new TreeData(7)); + binaryTree.add(new TreeData(1)); + binaryTree.add(new TreeData(6)); + binaryTree.add(new TreeData(4)); + binaryTree.add(new TreeData(8)); + + Assert.assertEquals("4", binaryTree.get(5).toString()); + Assert.assertEquals("8", binaryTree.get(7).toString()); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/LinkedListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/LinkedListTest.java new file mode 100644 index 0000000000..21fea9680f --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/LinkedListTest.java @@ -0,0 +1,43 @@ +package test.com.pxshuo.se01.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.se01.basic.impl.ArrayList; +import com.pxshuo.se01.basic.impl.LinkedList; + +public class LinkedListTest { + LinkedList object = new LinkedList(); + + @Test + public void addTest() { + object.add("String"); + Assert.assertEquals("String", object.get(0)); + } + + @Test + public void addIndexTest(){ + object.add(3,"Hello"); + Assert.assertEquals("Hello", object.get(0)); + } + + @Test + public void removeTest() { + object.add("Hello"); + object.add("Hello"); + object.add("Hello"); + object.add(3,"Hello"); + Assert.assertNotNull(object.get(3)); + object.remove(3); + Assert.assertNull(object.get(3)); + } + + @Test + public void sizeTest(){ + object.add("new"); + object.add("hi"); + object.add(1,"new"); + object.remove(2); + Assert.assertEquals(2, object.size()); + } +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/QueueTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/QueueTest.java new file mode 100644 index 0000000000..edd3bfd73a --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/QueueTest.java @@ -0,0 +1,23 @@ +package test.com.pxshuo.se01.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.se01.basic.impl.Queue; + +public class QueueTest { + public Queue object = new Queue(); + + @Test + public void enQueueTest() { + Assert.assertEquals(true, object.isEmpty()); + object.enQueue("hello"); + object.enQueue("world"); + Assert.assertEquals(false, object.isEmpty()); + Assert.assertEquals(2, object.size()); + Assert.assertEquals("hello", object.deQueue()); + Assert.assertEquals("world", object.deQueue()); + Assert.assertEquals(0, object.size()); + } + +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/StackTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/StackTest.java new file mode 100644 index 0000000000..fc3f450e01 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/StackTest.java @@ -0,0 +1,25 @@ +package test.com.pxshuo.se01.basic.impl; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.se01.basic.impl.Queue; +import com.pxshuo.se01.basic.impl.Stack; + +public class StackTest { + public Stack object = new Stack(); + + @Test + public void enQueueTest() { + Assert.assertEquals(true, object.isEmpty()); + object.push("hello"); + object.push("world"); + Assert.assertEquals(false, object.isEmpty()); + Assert.assertEquals(2, object.size()); + Assert.assertEquals("world", object.peek()); + Assert.assertEquals("world", object.pop()); + Assert.assertEquals("hello", object.pop()); + Assert.assertEquals(0, object.size()); + } + +} diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/se02/array/ArrayUntilTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se02/array/ArrayUntilTest.java new file mode 100644 index 0000000000..6a6e3d4229 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se02/array/ArrayUntilTest.java @@ -0,0 +1,72 @@ +package test.com.pxshuo.se02.array; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.se02.array.*; + +public class ArrayUntilTest { + private ArrayUtil obj = new ArrayUtil(); + + @Test + public void reverseArrayTest() { + int[] origin = {3, 30, 9,7}; + obj.reverseArray(origin); + int[] expect = {7,9,30,3}; + Assert.assertArrayEquals(expect, origin); + } + + @Test + public void removeZeroTest() { + int[] origin = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] actual = obj.removeZero(origin); + int[] expect = {1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void mergeTest() { + int[] array1 = {3, 5, 7,8}; + int[] array2 = {4, 5, 6,7}; + int[] actual = obj.merge(array2,array1); + int[] expect = {3,4,5,6,7,8}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void growTest() { + int[] origin = {2,3,6}; + int[] actual = obj.grow(origin,3); + int[] expect = {2,3,6,0,0,0}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void fibonacciTest() { + int[] actual = obj.fibonacci(15); + int[] expect = {1,1,2,3,5,8,13}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void getPrimesTest() { + int[] actual = obj.getPrimes(23); + int[] expect = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void getPerfectNumbersTest() { + int[] actual = obj.getPerfectNumbers(1000); + int[] expect = {6,28,496}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void joinTest() { + int[] origin = {3,8,9}; + String actual = obj.join(origin, "-"); + String expect = "3-8-9"; + Assert.assertEquals(expect, actual); + } +} diff --git "a/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000..ee1bdbd433 --- /dev/null +++ "b/group06/2415980327/\346\226\207\347\253\240/SE01_\350\256\241\347\256\227\346\234\272CPU\343\200\201\347\241\254\347\233\230\343\200\201\345\206\205\345\255\230\344\273\245\345\217\212\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,13 @@ +# 计算机CPU、硬盘、内存以及指令之间的关系 + +by 2415980327 + +​ 但凡常见的事物,总会让人习以为常。伴随着计算机在在现代社会中的普及,人们可以日常生活中随处见到计算机在辅助我们进行各种各样的工作。在未经深究的情况下,我们就自然而然的认为,计算机本身就是这个样子。计算机实际上是什么样,我也不清楚。从小学知道这种事物开始,也是慢慢地在了解这种机器。 + +​ 就我所知,我们目前使用的常见的个人计算机在概念上是属于图灵机的。图灵机这个概念是研究将人们数学的运算过程使用机器来进行代替。它是由一个纸带作为输入与输出,同时使用一个处理器来处理这个纸带,达到运算的目的。类似是程序中函数的概念。或许来说程序中函数的概念要比图灵机的概念晚得多,但是作为一个程序员来说,就没有必要非要把自己置于一无所知的情形下再去学习这个概念。既然我已经知道了函数这个概念,那我就用函数来类比图灵机的概念吧。类似于函数的输入-处理-输出这种结构,想必大家也是一目了然的。 + +​ 图灵机只是作为一个理论上模拟出来的机器,只是为了证明这样是可行的。而实际上实现的是冯诺依曼体系结构的计算机。我们日常所见的计算机也是基于这种体系结构建立起来的。冯诺依曼体系结构是分为五部分的,包括存储器,运算器、控制器、输入设备和输出设备。同样也可以类比为函数,输入设备输出设备同图灵机一样只是作为输入与输出,剩下的存储器、控制器与运算器是做为处理器存在的,类似于函数中的变量,逻辑结构与逻辑运算的存在。存储器就是存储一些信息,运算器是做一些实际上的运算,控制器是进行程序指令的跳转,来实现各种不同的结构。 + +​ 而本文提及的CPU就是运算器与控制器的集合,内存就相当于函数中的变量,也就是相当于存储器。那硬盘是用来做什么的呢?硬盘是用作数据的持久化,一方面由于成本较低,所以是当做大量的数据存储单元。另一方面由于内存断电会清空,所以使用硬盘来存储信息更为方便。所以我认为硬盘应该是游离于这个体系之外的辅助设备。类似于程序的数据库或者程序的文本存档。 + +​ 指令与数据平时存储于硬盘之中,在需要运行程序时,将其读入到内存中来,通过CPU来处理内存中的数据。那么CPU为什么不直接处理硬盘中的数据呢?因为硬盘相比于内存的访问速度太慢了,相比于CPU的处理速度更是慢到离谱,所以才需要内存的当做存储器为CPU的处理工作提供支持。 \ No newline at end of file diff --git a/group06/263050006/article.txt b/group06/263050006/article.txt new file mode 100644 index 0000000000..2eaa5b792f --- /dev/null +++ b/group06/263050006/article.txt @@ -0,0 +1,2 @@ +CPU、内存、硬盘、指令以及他们之间的关系 +https://zhuanlan.zhihu.com/p/25442061 \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java new file mode 100644 index 0000000000..b807a9b321 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java @@ -0,0 +1,197 @@ +package com.github.chaoswang.learning.java.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.TreeSet; + + +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 int[] reverseArray(int[] origin){ + int length = origin.length; + for(int i=0;i list = new ArrayList(); + for(int value : oldArray){ + if(value != 0){ + list.add(value); + } + } + return returnByIntArray(list); + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * 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){ + TreeSet ts1 = new TreeSet(Arrays.asList(convertToIntegerArray(array1))); + TreeSet ts2 = new TreeSet(Arrays.asList(convertToIntegerArray(array2))); + ts2.addAll(ts1); + return returnByIntArray(ts2); + } + + private static Integer[] convertToIntegerArray(int[] array){ + Integer[] returnArray = new Integer[array.length]; + for(int i=0;i collection){ + int[] returnArray = new int[collection.size()]; + int i = 0; + for(Iterator it = collection.iterator(); it.hasNext();){ + returnArray[i] = it.next(); + i++; + } + return returnArray; + } + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] returnArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, returnArray, 0, oldArray.length); + return returnArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max <= 1){ + return new int[0]; + } + Integer[] init = {1,1}; + LinkedList result = new LinkedList(Arrays.asList(init)); + for(int tmp = -1; tmp <= max;){ + tmp = generateFibonacci(result); + } + result.removeLast(); + return returnByIntArray(result); + } + + private static int generateFibonacci(LinkedList result){ + int a = result.getLast(); + int b = result.get(result.size()-2); + result.add(a + b); + return result.getLast(); + } + + /** + * Сڸֵmax + * max = 23, صΪ[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + List list = new LinkedList(); + for(int i=2;i list = new LinkedList(); + for(int i=2;i<=max;i++){ + if(isPerfectNumber(i)){ + list.add(i); + } + } + return returnByIntArray(list); + } + + private static boolean isPerfectNumber(int number) { + int sum = 0; + for(int i=1;i { + private int size = 0; + private int initialSize; + private Object[] elements = null; + + public MyArrayList(int initialSize){ + this.initialSize = initialSize; + elements = new Object[initialSize]; + } + + public void add(E element){ + //ﵽޣinitialSize50% + if(++size == elements.length){ + elements = Arrays.copyOf(elements, size + (int)Math.round(initialSize * 0.5)); + } + elements[size - 1] = element; + } + + // + public void add(int index, E element){ + //index=sizeʱ൱ڵadd + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException(); + } + for (int i=size; i > index; i--){ + elements[i] = elements[i - 1]; + } + elements[index] = element; + } + + // + public E remove(int index){ + E removed = (E)elements[index]; + for(int i=index; i 0){ + //ڸtreenodeҲɹ򷵻true + if(parent.getLeftChild() == null){ + TreeNode node = new TreeNode(obj); + node.setParent(parent); + parent.setLeftChild(node); + return true; + } + return insert(parent.getLeftChild(), obj); + } + if(parent.getRightChild() == null){ + TreeNode node = new TreeNode(obj); + node.setParent(parent); + parent.setRightChild(node); + return true; + } + return insert(parent.getRightChild(), obj); + } + + @Override + public String toString() { + if(root == null){ + return ""; + } + return root.toString(); + } + + private static class TreeNode { + private TreeNode parent; + private TreeNode leftChild; + private TreeNode rightChild; + private Comparable userObject;//ڵ㶼洢ݵ + + public TreeNode(Comparable userObject){ + this.userObject = userObject; + } + + public Comparable getUserObject() { + return userObject; + } + + public TreeNode getParent() { + return parent; + } + + public void setParent(TreeNode parent) { + this.parent = parent; + } + + public TreeNode getLeftChild() { + return leftChild; + } + + public void setLeftChild(TreeNode leftChild) { + this.leftChild = leftChild; + } + + public TreeNode getRightChild() { + return rightChild; + } + + public void setRightChild(TreeNode rightChild) { + this.rightChild = rightChild; + } + + @Override + public String toString() { + return "TreeNode [parent=" + (parent==null?null:parent.getUserObject()) + ", leftChild=" + (leftChild==null?null:leftChild) + + ", rightChild=" + (rightChild==null?null:rightChild) + ", userObject=" + + userObject + "]"; + } + + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyIterator.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyIterator.java new file mode 100644 index 0000000000..7c35be59aa --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyIterator.java @@ -0,0 +1,8 @@ +package com.github.chaoswang.learning.java.collection.myown; + +public interface MyIterator { + /*arraylistʵһ*/ + boolean hasNext(); + Object next(); + void remove(); +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java new file mode 100644 index 0000000000..007a6e48c3 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedList.java @@ -0,0 +1,120 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.NoSuchElementException; + +public class MyLinkedList { + private int size = 0; + private Node head = null; + private Node tail = null; + + // + public void add(E element){ + Node tmp = new Node(element, null); + if(tail == null){ + head = tmp; + }else{ + tail.next = tmp;; + } + tail = tmp; + size++; + } + + public void add(int index, E element){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException(); + } + Node tmpBefore = getElement(index -1); + Node tmpAfter = getElement(index); + Node tmp = new Node(element, tmpAfter); + tmpBefore.next = tmp; + + } + + public void addFirst(E element){ + Node tmp = new Node(element, null); + if(head != null){ + tmp.next = head; + }else{ + tail = tmp; + } + head = tmp; + } + + public E removeFirst(){ + if(size <= 0){ + throw new NoSuchElementException(); + } + Node tmp = head; + head = head.next; + return (E)tmp.element; + } + + // + public E remove(int index) { + if(index < 0 || index >= size()){ + throw new IndexOutOfBoundsException(); + } + Node tmpBeore = this.getElement(index-1); + Node tmp = this.getElement(index); + Node tmpNext = this.getElement(index+1); + tmpBeore.next = tmpNext; + size--; + return (E)tmp.element; + } + + // + public E get(int index){ + return (E)this.getElement(index).element; + } + + public int size() { + return size; + } + + @Override + public String toString() { + if(head == null){ + return "[]"; + } + StringBuffer sb = new StringBuffer("["); + Node tmp = head; + while(tmp != null){ + sb.append(tmp.element.toString()); + sb.append(","); + tmp = tmp.next; + } + String returnStr = sb.toString(); + returnStr = returnStr.substring(0, returnStr.length()-1); + return returnStr + "]"; + } + + private Node getElement(int index) { + Node tmp = head; + for(int i=0;i { + private int size = 0; + private int capacitySize; + private OneElement first = null; + private OneElement last = null;//ԸΪԼLinkedListʵ + + public MyQueue(int capacitySize){ + this.capacitySize = capacitySize; + } + + //β ʱ쳣 + public void add(E element){ + if(size+1 > capacitySize){ + throw new IllegalStateException("over capacity."); + } + addLast(element); + } + + //β 쳣᷵Ƿɹ + public boolean offer(E element){ + if(size+1 > capacitySize){ + return false; + } + addLast(element); + return true; + } + + private void addLast(E element){ + OneElement tmp = new OneElement(element, null); + if(last == null){ + first = tmp; + }else{ + last.next = tmp;; + } + last = tmp; + size++; + } + + //ƳͷԪأΪʱ쳣 + public E remove(){ + if(size == 0){ + throw new NoSuchElementException(); + } + return removeFirst(); + } + + //ƳͷԪأΪʱnullqueueһ㲻ònullԪ + public E poll(){ + if(size == 0){ + return null; + } + return removeFirst(); + } + + private E removeFirst(){ + OneElement tmp = first; + first = first.next; + size--; + return tmp.element; + } + + //ضͷԪأDzƳΪʱ쳣 + public E element(){ + if(size == 0){ + throw new NoSuchElementException(); + } + return first.element; + } + + //ضͷԪأDzƳΪʱnull + public E peek(){ + if(size == 0){ + return null; + } + return first.element; + } + + private class OneElement{ + private E element = null; + private OneElement next = null; + + public OneElement(E element, OneElement next) { + this.element = element; + this.next = next; + } + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyStack.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyStack.java new file mode 100644 index 0000000000..f425c8de63 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyStack.java @@ -0,0 +1,63 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.Arrays; +import java.util.EmptyStackException; + +public class MyStack { + private int size = 0; + private int initialSize; + private Object[] elements = null;//ԸΪԼArrayListʵ + + public MyStack(int initialSize){ + this.initialSize = initialSize; + elements = new Object[initialSize]; + } + + //ѹջ + public void push(E element){ + //ﵽޣinitialSize100% + if(++size == elements.length){ + elements = Arrays.copyOf(elements, size + initialSize); + } + elements[size - 1] = element; + } + + //жջǷΪ + public boolean empty(){ + return size <= 0? true : false; + } + + public int size(){ + return size; + } + + //鿴ջԪ + public E peek(){ + if(size == 0){ + throw new EmptyStackException(); + } + return (E)elements[size - 1]; + } + + //ջԪ + public E pop(){ + if(size == 0){ + throw new EmptyStackException(); + } + E removed = (E)elements[size -1]; + elements[size -1] = null; + size--; + return removed; + } + + public int search(E element) { + int index = 0; + for (int i = 0; i < size - 1; i++) { + if (element.equals(elements[i])) { + index = (size -1) - i; + break; + } + } + return index; + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyTree.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyTree.java new file mode 100644 index 0000000000..dcb4a59a8b --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/collection/myown/MyTree.java @@ -0,0 +1,59 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.List; + +public class MyTree { + + public MyTree(MyTreeNode treeNode){ + + } + + + private class MyTreeNode { + private MyTreeNode parent; + private List children;//ʱΪleftright + private boolean allowsChildren; + private Object userObject;//ڵ㶼洢ݵ + + public MyTreeNode(Object userObject){ + this.userObject = userObject; + } + + public List children(){ + return children; + } + + public MyTreeNode getChildAt(int childIndex){ + return children.get(childIndex); + } + + public int getIndex(MyTreeNode node){ + return children.indexOf(node); + } + + public MyTreeNode getParent(){ + return parent; + } + + public boolean isLeaf(){ + return children.size() > 0 ? false : true; + } + + public boolean getAllowsChildren(){ + return allowsChildren; + } + + public void insert(MyTreeNode node, int index){ + children.remove(node); + children.add(index, node); + } + + public void remove(int index){ + children.remove(index); + } + + public void remove(MyTreeNode node){ + children.remove(node); + } + } +} diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/LoginAction.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/LoginAction.java new file mode 100644 index 0000000000..7df73b09d9 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.github.chaoswang.learning.java.litestruts; + +/** + * һչʾ¼ҵ࣬ еû붼Ӳġ + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/Struts.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/Struts.java new file mode 100644 index 0000000000..26c39ef661 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/Struts.java @@ -0,0 +1,134 @@ +package com.github.chaoswang.learning.java.litestruts; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.net.URLDecoder; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; + +public class Struts { + private static Map action_class = new HashMap(); + private static Map> action_result = new HashMap>(); + + + public static View runAction(String actionName, + Map parameters) { + + /* + * 0. ȡļstruts.xml + */ + parseConfig(); + + /* + * + * 1. actionNameҵӦclass LoginAction, ͨʵ + * parametersеݣösetter parametersе ("name"="test" , + * "password"="1234") , ǾӦõ setNamesetPassword + */ + Class actionClass = null; + Object actionInstance = null; + try { + actionClass = Class.forName(action_class.get(actionName)); + actionInstance = actionClass.newInstance(); + for (Entry entry : parameters.entrySet()) { + String key = (String) entry.getKey(); + String value = (String) entry.getValue(); + actionClass.getMethod( + "set" + key.substring(0, 1).toUpperCase() + + key.substring(1), String.class).invoke( + actionInstance, value); + } + } catch (Exception e) { + e.printStackTrace(); + } + /* + * 2. ͨöexectue ÷ֵ"success" + */ + + String returnStr = null; + try { + returnStr = (String)actionClass.getMethod("execute", new Class[0]).invoke(actionInstance, new Object[0]); + } catch (Exception e) { + e.printStackTrace(); + } + + /* + * 3. ͨҵgetter getMessage, ͨã ֵγһHashMap , + * {"message": "¼ɹ"} , ŵViewparameters + */ + Map params = new HashMap(); + Method[] methods = actionClass.getDeclaredMethods(); + try { + for(Method method : methods){ + if(method.getModifiers() == Modifier.PUBLIC && method.getName().startsWith("get")){ + String key = method.getName().substring(3,4).toLowerCase() + method.getName().substring(4); + String value = (String)method.invoke(actionInstance, new Object[0]); + params.put(key, value); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + /* + * 4. struts.xmlе ,Լexecuteķֵ ȷһjsp + * ŵViewjspֶС + */ + View view = new View(); + view.setParameters(params); + Map results = action_result.get(actionName); + view.setJsp(results.get(returnStr)); + return view; + } + + private static void parseConfig() { + String xmlpath = Struts.class.getResource("struts.xml").getFile(); + try { + xmlpath = URLDecoder.decode(xmlpath, "utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + SAXBuilder builder = new SAXBuilder(false); + try { + Document doc = builder.build(xmlpath); + Element struts = doc.getRootElement(); + List actionlist = struts.getChildren("action"); + for (Iterator iter = actionlist.iterator(); iter.hasNext();) { + Element action = (Element) iter.next(); + String actionName = action.getAttributeValue("name"); + String actionClass = action.getAttributeValue("class"); + action_class.put(actionName, actionClass); + List resultlist = action.getChildren("result"); + Map result_value = new HashMap(); + for (Iterator iter1 = resultlist.iterator(); iter1.hasNext();) { + Element result = (Element) iter1.next(); + String resultName = result.getAttributeValue("name"); + String resultView = result.getTextTrim(); + result_value.put(resultName, resultView); + } + action_result.put(actionName, result_value); + } + } catch (JDOMException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println(action_class); + System.out.println(action_result); + } + + public static void main(String[] args) { + Struts.parseConfig(); + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/View.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/View.java new file mode 100644 index 0000000000..ae57b41d32 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.chaoswang.learning.java.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/struts.xml b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/struts.xml new file mode 100644 index 0000000000..10671eac25 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/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/group06/263050006/src/test/java/com/github/chaoswang/learning/java/array/ArrayUtilTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/array/ArrayUtilTest.java new file mode 100644 index 0000000000..4ebff76195 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/array/ArrayUtilTest.java @@ -0,0 +1,134 @@ +package com.github.chaoswang.learning.java.array; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayUtilTest { + + /** + * һa , Ըֵû + 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] + a = [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * @param origin + * @return + */ + @Test + public void testReverseArray() { + int a[] = {7, 9, 30, 3}; + int reversedA[] = {3, 30, 9, 7}; + int b[] = {7, 9, 30, 3, 4}; + int reversedB[] = {4, 3, 30, 9, 7}; + Assert.assertArrayEquals(reversedA, ArrayUtil.reverseArray(a)); + Assert.assertArrayEquals(reversedB, ArrayUtil.reverseArray(b)); + } + + /** + * µһ飺 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 + */ + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(newArr, ArrayUtil.removeZero(oldArr)); + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * @param array1 + * @param array2 + * @return + */ + @Test + public void testMerge() { + int a1[] = {3, 5, 7,8}; + int a2[] = {4, 5, 6,7}; + int a3[] = {3,4,5,6,7,8}; + Assert.assertArrayEquals(a3, ArrayUtil.merge(a1, a2)); + } + + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + @Test + public void testGrow() { + int oldArray[] = {2,3,6}; + int newArray[] = {2,3,6,0,0,0}; + Assert.assertArrayEquals(newArray, ArrayUtil.grow(oldArray, 3)); + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + @Test + public void testFibonacci() { + int returnA[] = {1,1,2,3,5,8,13}; + int returnB[] = {}; + int returnC[] = {1,1,2,3,5,8,13,21,34}; + int returnD[] = {1,1,2}; + Assert.assertArrayEquals(returnA, ArrayUtil.fibonacci(15)); + Assert.assertArrayEquals(returnB, ArrayUtil.fibonacci(1)); + Assert.assertArrayEquals(returnC, ArrayUtil.fibonacci(34)); + Assert.assertArrayEquals(returnD, ArrayUtil.fibonacci(2)); + } + + /** + * Сڸֵmax + * max = 23, صΪ[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + @Test + public void testGetPrimes() { + int returnC[] = {2,3,5,7,11,13,17,19}; + int returnD[] = {2,3,5,7,11}; + Assert.assertArrayEquals(returnC, ArrayUtil.getPrimes(23)); + Assert.assertArrayEquals(returnD, ArrayUtil.getPrimes(12)); + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 + * һֵmax һ飬 Сmax + * @param max + * @return + */ + @Test + public void testGetPerfectNumbers() { + int returnC[] = {6,28,496}; + int returnD[] = {6,28,496,8128}; + int returnE[] = {6,28,496,8128,33550336}; + Assert.assertArrayEquals(returnC, ArrayUtil.getPerfectNumbers(496)); + Assert.assertArrayEquals(returnD, ArrayUtil.getPerfectNumbers(8129)); + Assert.assertArrayEquals(returnE, ArrayUtil.getPerfectNumbers(33550337)); + } + + /** + * seperator array + * array= [3,8,9], seperator = "-" + * 򷵻ֵΪ"3-8-9" + * @param array + * @param s + * @return + */ + @Test + public void testJoin() { + int array[] = {3,8,9}; + Assert.assertEquals("3-8-9", ArrayUtil.join(array,"-")); + Assert.assertEquals("3@$8@$9", ArrayUtil.join(array,"@$")); + } + +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyArrayListTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyArrayListTest.java new file mode 100644 index 0000000000..cb83d6e506 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyArrayListTest.java @@ -0,0 +1,53 @@ +package com.github.chaoswang.learning.java.collection.myown; + + +import org.junit.Assert; +import org.junit.Test; + +import com.github.chaoswang.learning.java.collection.myown.MyArrayList; + +public class MyArrayListTest { + + @Test + public void testAdd(){ + MyArrayList myList = new MyArrayList(3); + myList.add("1"); + myList.add("2"); + myList.add("3"); + Assert.assertEquals(3, myList.size()); + myList.add("4"); + Assert.assertEquals(4, myList.size()); + String str = myList.get(2); + Assert.assertEquals("3", str); + + } + + @Test + public void testInsert(){ + MyArrayList myList = new MyArrayList(3); + myList.add("1"); + myList.add("2"); + myList.add("4"); + String str = myList.get(2); + Assert.assertEquals("4", str); + myList.add(2,"3"); + str = myList.get(2); + Assert.assertEquals("3", str); + } + + @Test + public void testRemove(){ + MyArrayList myList = new MyArrayList(3); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + String str = myList.remove(2); + Assert.assertEquals("3", str); + str = myList.get(2); + Assert.assertEquals("4", str); + Assert.assertEquals(3, myList.size()); + } + + +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyBinarySearchTreeTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyBinarySearchTreeTest.java new file mode 100644 index 0000000000..9c76a8056c --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyBinarySearchTreeTest.java @@ -0,0 +1,17 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import org.junit.Test; + +public class MyBinarySearchTreeTest { + @Test + public void testInsert(){ + MyBinarySearchTree tree = new MyBinarySearchTree(12); + tree.insert(5); + tree.insert(18); + tree.insert(2); + tree.insert(9); + tree.insert(15); + tree.insert(19); + System.out.println(tree); + } +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java new file mode 100644 index 0000000000..cdbbcf2812 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyLinkedListTest.java @@ -0,0 +1,76 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import org.junit.Assert; +import org.junit.Test; + +public class MyLinkedListTest { + + @Test + public void testAdd(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + Assert.assertEquals(3, myList.size()); + myList.add("4"); + Assert.assertEquals(4, myList.size()); + System.out.println(myList); + String str = myList.get(2); + Assert.assertEquals("3", str); + + } + + @Test + public void testInsert(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("4"); + String str = myList.get(2); + Assert.assertEquals("4", str); + myList.add(2,"3"); + str = myList.get(2); + Assert.assertEquals("3", str); + } + + @Test + public void testAddFirst(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("2"); + myList.add("3"); + myList.add("4"); + System.out.println(myList); + Assert.assertEquals("2", myList.get(0)); + myList.addFirst("1"); + Assert.assertEquals("1", myList.get(0)); + System.out.println(myList); + } + + @Test + public void testRemoveFirst(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + String str = myList.removeFirst(); + System.out.println(myList); + Assert.assertEquals("1", str); + Assert.assertEquals("2", myList.get(0)); + } + + @Test + public void testRemove(){ + MyLinkedList myList = new MyLinkedList(); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + String str = myList.remove(2); + Assert.assertEquals("3", str); + str = myList.get(2); + Assert.assertEquals("4", str); + Assert.assertEquals(3, myList.size()); + } + +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyQueueTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyQueueTest.java new file mode 100644 index 0000000000..79579c6719 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyQueueTest.java @@ -0,0 +1,55 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.NoSuchElementException; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MyQueueTest { + + @Rule + public ExpectedException thrown= ExpectedException.none(); + + @Test + public void testAdd(){ + MyQueue myQueue = new MyQueue(3); + //3Ԫ + myQueue.add("1"); + myQueue.add("2"); + myQueue.offer("3"); + //ӷfalse + Assert.assertFalse(myQueue.offer("4")); + //ȡ + Assert.assertEquals("1", myQueue.element()); + Assert.assertEquals("1", myQueue.peek()); + //ʼƳ + Assert.assertEquals("1", myQueue.remove()); + Assert.assertEquals("2", myQueue.remove()); + Assert.assertEquals("3", myQueue.poll()); + //Ƴfalse + Assert.assertNull(myQueue.poll()); + } + + @Test + public void testAddWhenQueueIsFull(){ + thrown.expect(IllegalStateException.class); + MyQueue myQueue = new MyQueue(3); + myQueue.add("1"); + myQueue.add("2"); + myQueue.add("3"); + //쳣 + myQueue.add("4"); + } + + @Test + public void testRemoveWhenQueueIsEmpty(){ + thrown.expect(NoSuchElementException.class); + MyQueue myQueue = new MyQueue(3); + myQueue.add("1"); + myQueue.remove(); + //Ƴ쳣 + myQueue.remove(); + } +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java new file mode 100644 index 0000000000..4ae0c84989 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/collection/myown/MyStackTest.java @@ -0,0 +1,47 @@ +package com.github.chaoswang.learning.java.collection.myown; + +import java.util.EmptyStackException; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MyStackTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testPushAndPop(){ + MyStack myStack = new MyStack(3); + myStack.push("1"); + myStack.push("2"); + myStack.push("3"); + Assert.assertEquals("3", myStack.pop()); + Assert.assertEquals("2", myStack.peek()); + Assert.assertEquals("2", myStack.pop()); + Assert.assertEquals("1", myStack.peek()); + } + + @Test + public void testPopWhenQueueIsEmpty(){ + thrown.expect(EmptyStackException.class); + MyStack myStack = new MyStack(3); + myStack.push("1"); + myStack.pop(); + //Ƴ쳣 + myStack.pop(); + } + + @Test + public void testSearch(){ + MyStack myStack = new MyStack(3); + myStack.push("1"); + myStack.push("2"); + myStack.push("3"); + Assert.assertEquals(2, myStack.search("1")); + Assert.assertEquals(1, myStack.search("2")); + Assert.assertEquals(0, myStack.search("3")); + } +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/litestruts/StrutsTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/litestruts/StrutsTest.java new file mode 100644 index 0000000000..834a4dd390 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.github.chaoswang.learning.java.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"); //ԤIJһ + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} \ No newline at end of file diff --git a/group06/284999210/.classpath b/group06/284999210/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/284999210/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/284999210/.gitignore b/group06/284999210/.gitignore new file mode 100644 index 0000000000..f5ebe83019 --- /dev/null +++ b/group06/284999210/.gitignore @@ -0,0 +1,3 @@ +*.class +/.metadata +/bin \ No newline at end of file diff --git a/group06/284999210/.project b/group06/284999210/.project new file mode 100644 index 0000000000..c3e8c69e46 --- /dev/null +++ b/group06/284999210/.project @@ -0,0 +1,17 @@ + + + 284999210 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group06/284999210/2017_week_8_CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.doc" "b/group06/284999210/2017_week_8_CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.doc" new file mode 100644 index 0000000000..d7812844b3 Binary files /dev/null and "b/group06/284999210/2017_week_8_CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.doc" differ diff --git a/group06/284999210/src/com/coding/basic/container/ArrayList.java b/group06/284999210/src/com/coding/basic/container/ArrayList.java new file mode 100644 index 0000000000..3d091153c9 --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/ArrayList.java @@ -0,0 +1,156 @@ +package com.coding.basic.container; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private Object[] elementData; + private int size; + private int capacity; + private static final int DEFAULT_CAPACITY = 8; + + public ArrayList() { + elementData = new Object[8]; + size = 0; + capacity = DEFAULT_CAPACITY; + } + + public boolean add(Object element) { + if (size == capacity) { + Object[] tempArray = new Object[capacity]; + for (int i = 0; i < size; i++) { + tempArray[i] = elementData[i]; + } + elementData = new Object[capacity * 2]; + for (int i = 0; i < size; i++) { + elementData[i] = tempArray[i]; + } + elementData[capacity] = element; + capacity = capacity * 2; + } + elementData[size++] = element; + return true; + } + + @Override + public void add(int index, Object element) { + checkIndex(index); + Object[] tempArray = new Object[capacity]; + for (int i = 0; i < size; i++) { + tempArray[i] = elementData[i]; + } + elementData = new Object[capacity * 2]; + for (int i = 0; i < size; i++) { + if (i < index) { + elementData[i] = tempArray[i]; + } else { + elementData[i + 1] = tempArray[i]; + } + } + elementData[index] = element; + capacity = capacity * 2; + size++; + } + + @SuppressWarnings("unchecked") + public Object remove(int index) { + checkIndex(index); + + Object o = elementData[index]; + for (int i = index; i < size; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size] = null; + size = size - 1; + return o; + } + + private void checkIndex(int index) { + if (index >= capacity) { + throw new IndexOutOfBoundsException(); + } + } + + public Object set(int index, Object element) { + checkIndex(index); + + Object o = elementData[index]; + elementData[index] = element; + return o; + } + + @SuppressWarnings("unchecked") + public Object get(int index) { + checkIndex(index); + + return (Object) elementData[index]; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + if (i != size - 1) { + sb.append(elementData[i] + ", "); + } else { + sb.append(elementData[i]); + } + } + sb.append("]"); + return sb.toString(); + } + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public Iterator iterator() { + return new IteratorArrayList(); + } + + @Override + public boolean remove(Object element) { + if (element == null) + return false; + int findIndex = -1; + for (int i = 0; i < size; i++) { + if (elementData[i].equals(element)) { + findIndex = i; + break; + } + } + + for (int i = findIndex; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size - 1] = null; + size--; + return false; + } + + private class IteratorArrayList implements Iterator { + + private int cursor; + + @Override + public boolean hasNext() { + return cursor < size; + } + + @Override + public Object next() { + if (cursor < size) + return elementData[cursor++]; + else + throw new NoSuchElementException(); + } + + } +} diff --git a/group06/284999210/src/com/coding/basic/container/BinaryTreeNode.java b/group06/284999210/src/com/coding/basic/container/BinaryTreeNode.java new file mode 100644 index 0000000000..09b2a9db9f --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/BinaryTreeNode.java @@ -0,0 +1,68 @@ +package com.coding.basic.container; + +public class BinaryTreeNode { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode(T data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + 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 insert(T o) { + if (null == root) { + root = new BinaryTreeNode(o, null, null); + return root; + } + + return insert(o, root); + } + public BinaryTreeNode insert(T o, BinaryTreeNode node) { + BinaryTreeNode nodeNew = new BinaryTreeNode(o, null, null); + BinaryTreeNode nodeCurrent = node; + if (o.compareTo(nodeCurrent.data) < 0) { + if (nodeCurrent.left == null) { + nodeCurrent.left = nodeNew; + return nodeNew; + } else { + return insert(o, nodeCurrent.left); + } + } else if (o.compareTo(nodeCurrent.data) > 0) { + if (nodeCurrent.right == null) { + nodeCurrent.right = nodeNew; + return nodeNew; + } else { + return insert(o, nodeCurrent.right); + } + } else { + return nodeCurrent; + } + } +} diff --git a/group06/284999210/src/com/coding/basic/container/Iterator.java b/group06/284999210/src/com/coding/basic/container/Iterator.java new file mode 100644 index 0000000000..5ec89a4983 --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic.container; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); +} diff --git a/group06/284999210/src/com/coding/basic/container/LinkedList.java b/group06/284999210/src/com/coding/basic/container/LinkedList.java new file mode 100644 index 0000000000..239c4189ec --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/LinkedList.java @@ -0,0 +1,186 @@ +/** + * + */ +package com.coding.basic.container; + +/** + * @author Administrator + * + */ +public class LinkedList implements List { + + private int size; + private Node head; + + public LinkedList() { + head = new Node(null, null, null); + size = 0; + } + + @Override + public boolean add(Object o) { + Node nodeAdd; + Node nodeCurrent = head; + while (nodeCurrent.next != null) { + nodeCurrent = nodeCurrent.next; + } + nodeAdd = new Node(o, nodeCurrent, null); + nodeCurrent.next = nodeAdd; + size++; + return true; + } + + @Override + public boolean remove(Object o) { + if (head.next == null) { + return false; + } + Node nodeCurrent = head; + + while (nodeCurrent.next != null) { + nodeCurrent = nodeCurrent.next; + if (nodeCurrent.data.equals(o)) { + nodeCurrent.previous.next = nodeCurrent.next; + nodeCurrent.next.previous = nodeCurrent.previous; + size--; + return true; + } + } + return false; + } + + @Override + public Object get(int index) { + checkIndex(index); + int i = 0; + Node nodeCurrent = head; + do { + nodeCurrent = nodeCurrent.next; + } while (i < index); + return nodeCurrent.data; + } + + @Override + public Object set(int index, Object element) { + checkIndex(index); + int i = 0; + Node nodeCurrent = head; + do { + nodeCurrent = nodeCurrent.next; + } while (i < index); + Object o = nodeCurrent.data; + nodeCurrent.data = element; + return o; + } + + @Override + public void add(int index, Object element) { + checkIndex(index); + int i = 0; + Node nodeCurrent = head; + do { + nodeCurrent = nodeCurrent.next; + } while (i < index); + Node nodeNew = new Node(element, nodeCurrent.previous, nodeCurrent); + nodeCurrent.previous = nodeNew; + size++; + } + + @Override + public Object remove(int index) { + checkIndex(index); + Node nodeCurrent = head; + + int i = 0; + do { + nodeCurrent = nodeCurrent.next; + } while (i < index); + + Object o = nodeCurrent.data; + if (index == size - 1) { + nodeCurrent.previous.next = null; + } else { + nodeCurrent.previous.next = nodeCurrent.next; + nodeCurrent.next.previous = nodeCurrent.previous; + } + + size--; + return o; + } + + private void checkIndex(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return 0 == size; + } + + private static class Node { + public Node(Object data, Node pre, Node next) { + this.data = data; + this.previous = pre; + this.next = next; + } + + Object data; + Node previous; + Node next; + } + + public void addFirst(Object o) { + add(0, o); + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + Object o = remove(0); + return o; + } + + public Object removeLast() { + Object o = remove(size); + return o; + } + + @Override + public Iterator iterator() { + return new IteratorlinkedList(); + } + + private class IteratorlinkedList implements Iterator { + + private int cursor; + + @Override + public boolean hasNext() { + return cursor < size - 1; + } + + @Override + public Object next() { + if (hasNext()) { + int i = 0; + Node nodeCurrent = head; + do { + nodeCurrent = nodeCurrent.next; + } while (i < cursor + 1); + cursor++; + return nodeCurrent.data; + } else { + return null; + } + } + } +} diff --git a/group06/284999210/src/com/coding/basic/container/List.java b/group06/284999210/src/com/coding/basic/container/List.java new file mode 100644 index 0000000000..112d3c2f1e --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/List.java @@ -0,0 +1,28 @@ +/** + * + */ +package com.coding.basic.container; + +/** + * @author Administrator + * + */ +public interface List { + public boolean add(Object element); + + public void add(int index, Object element); + + public Object remove(int index); + + public boolean remove(Object element); + + public Object set(int index, Object element); + + public Object get(int index); + + public int size(); + + public boolean isEmpty(); + + public Iterator iterator(); +} diff --git a/group06/284999210/src/com/coding/basic/container/Queue.java b/group06/284999210/src/com/coding/basic/container/Queue.java new file mode 100644 index 0000000000..1157e8a473 --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/Queue.java @@ -0,0 +1,34 @@ +/** + * + */ +package com.coding.basic.container; + +/** + * @author Administrator + * + */ +public class Queue { + + private ArrayList list = new ArrayList(); + + public void enQueue(Object o) { + list.add(o); + } + + public Object deQueue() { + final int size = list.size(); + if (0 == size) + return null; + Object o = list.remove(size); + return o; + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public int size() { + return list.size(); + } + +} diff --git a/group06/284999210/src/com/coding/basic/container/Stack.java b/group06/284999210/src/com/coding/basic/container/Stack.java new file mode 100644 index 0000000000..2fd59b0c9c --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/Stack.java @@ -0,0 +1,39 @@ +/** + * + */ +package com.coding.basic.container; + +/** + * @author Administrator + * + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + final int size = elementData.size(); + if (0 == size) + return null; + return elementData.get(size); + } + + public Object peek() { + final int size = elementData.size(); + if (0 == size) + return null; + Object o = elementData.remove(size - 1); + return o; + } + + public boolean isEmpty() { + return elementData.isEmpty(); + } + + public int size() { + return elementData.size(); + } +} diff --git a/group06/284999210/src/com/coding/basic/container/test/TestContainer.java b/group06/284999210/src/com/coding/basic/container/test/TestContainer.java new file mode 100644 index 0000000000..d68104f703 --- /dev/null +++ b/group06/284999210/src/com/coding/basic/container/test/TestContainer.java @@ -0,0 +1,71 @@ +/** + * + */ +package com.coding.basic.container.test; + +import java.util.List; + +/** + * @author devin.yin + * + */ +public class TestContainer { + + /** + * @param args + */ + public static void main(String[] args) { + List list1 = new java.util.ArrayList(); + System.out.println(list1); + + // 4 basic operation for java.util.ArrayList--add remove change query + list1.add("0"); + list1.add("1"); + list1.add("2"); + list1.add("3"); + list1.add("4"); + list1.add("5"); + list1.add("6"); + list1.add("7"); + list1.add("8"); + list1.add("9"); + System.out.println(list1); + + list1.remove(0); + System.out.println(list1); + + list1.set(0, "set"); + System.out.println(list1); + + System.out.println(list1.get(0)); + + list1.add(9, "10"); + System.out.println(list1); + + System.out.println("------------------------------------------------"); + + // 4 basic operation for com.coding.basic.container.ArrayList--add remove change query + com.coding.basic.container.ArrayList list2 = new com.coding.basic.container.ArrayList(); + System.out.println(list2); + list2.add("0"); + list2.add("1"); + list2.add("2"); + list2.add("3"); + list2.add("4"); + list2.add("5"); + list2.add("6"); + list2.add("7"); + list2.add("8"); + list2.add("9"); + System.out.println(list2); + + list2.remove(0); + System.out.println(list2); + + list2.set(0, "set"); + System.out.println(list2); + + System.out.println(list2.get(0)); + } + +} diff --git a/group06/290149544/blog/README.md b/group06/290149544/blog/README.md new file mode 100644 index 0000000000..17ed336d0c --- /dev/null +++ b/group06/290149544/blog/README.md @@ -0,0 +1 @@ +[CSDN:](http://blog.csdn.net/dzxxbj) Ųҵ \ No newline at end of file diff --git a/group06/290149544/hw1/.classpath b/group06/290149544/hw1/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/290149544/hw1/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/290149544/hw1/.gitattributes b/group06/290149544/hw1/.gitattributes new file mode 100644 index 0000000000..bdb0cabc87 --- /dev/null +++ b/group06/290149544/hw1/.gitattributes @@ -0,0 +1,17 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/group06/290149544/hw1/.gitignore b/group06/290149544/hw1/.gitignore new file mode 100644 index 0000000000..b1f3107451 --- /dev/null +++ b/group06/290149544/hw1/.gitignore @@ -0,0 +1,63 @@ +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# ========================= +# Operating System Files +# ========================= + +# OSX +# ========================= + +.DS_Store +.AppleDouble +.LSOverride + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows +# ========================= + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk diff --git a/group06/290149544/hw1/.project b/group06/290149544/hw1/.project new file mode 100644 index 0000000000..f2571d221d --- /dev/null +++ b/group06/290149544/hw1/.project @@ -0,0 +1,17 @@ + + + hw1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/290149544/hw1/README.md b/group06/290149544/hw1/README.md new file mode 100644 index 0000000000..724da1115d --- /dev/null +++ b/group06/290149544/hw1/README.md @@ -0,0 +1,3 @@ +1. 添加泛型 +2. 添加单元测试 +3. Object是什么类型,如果要处理各种不同的数据类型怎么办这些数据结构呢? \ No newline at end of file diff --git a/group06/290149544/hw1/src/hw1/ArrayList.java b/group06/290149544/hw1/src/hw1/ArrayList.java new file mode 100644 index 0000000000..20a48d10ef --- /dev/null +++ b/group06/290149544/hw1/src/hw1/ArrayList.java @@ -0,0 +1,58 @@ +package hw1; + +import java.util.Arrays; + +// 先不考虑线程安全,增删改查 +// 业务导向就是复制 +public class ArrayList implements List { + // 又想到阁成员函数 + private int size = 0; // 属于elementData的属性 + // 暂时不要泛型,以后优化 + private Object[] elementData = new Object[10]; + public void add(Object o) { + elementData = grow(elementData, 10); + elementData[size()] = o; + } + public void add(int index, Object o) { + elementData = grow(elementData, 10); + for (int i = elementData.length-1; i >= index ; i--) { + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + } + public Object get(int index) { + // 入境检查 + if (index >= elementData.length) { + System.out.println("如何抛出数组越界异常?"); + } + return elementData[index]; + } + public Object remove(int index) { + // 入境检查 + if (index >= elementData.length || index < 0) { + System.out.println("如何抛出数组越界异常?"); + } + for (int i = index; i < elementData.length; i++) { + elementData[i-1] = elementData[i]; + } + return null; + } + public int size() { // 元素的个数 +// return -1; + return size; + } + public void print() { + for (int i = 0; i < size(); i++) { + System.out.println(elementData[i]); + } + } + // 注意有返回值 + private Object[] grow(Object[] src, int size) { + if (size() < src.length) { + return src; // 说明至少还能再放一个 + } else { + // 放不下了,则增加10个,数据结构是层层服务的 + return Arrays.copyOf(src, src.length+size); + } + } +} \ No newline at end of file diff --git a/group06/290149544/hw1/src/hw1/JavaTest.java b/group06/290149544/hw1/src/hw1/JavaTest.java new file mode 100644 index 0000000000..a59b8e2e10 --- /dev/null +++ b/group06/290149544/hw1/src/hw1/JavaTest.java @@ -0,0 +1,24 @@ +package hw1; + +import java.util.Arrays; + +public class JavaTest { + public static void main(String[] args) { + int[] a = new int[10]; // 创建了一个数组对象 + a[0] = 0; + a[1] = 1; + a[2] = 2; + a[3] = 3; +// a[10] = 3; + for (int i = 0; i < a.length; i++) { + System.out.println(a[i]); + } + } + // 然后就开始扩张了,ArrayList能自增长 + public static int[] grow(int[]src, int size) { + return Arrays.copyOf(src, src.length+size); +// int[] target = new int[src.length+size]; +// System.arraycopy(src, 0, target, 0, src.length); +// return target; + } +} diff --git a/group06/290149544/hw1/src/hw1/LinkedList.java b/group06/290149544/hw1/src/hw1/LinkedList.java new file mode 100644 index 0000000000..7966173b79 --- /dev/null +++ b/group06/290149544/hw1/src/hw1/LinkedList.java @@ -0,0 +1,115 @@ +package hw1; + +public class LinkedList implements List { +// public static void main(String[] args) { +// +// } + private Node head = null; + private int size; // 链表的节点个数,从1开始计数的哦 + + public void add(Object o) { + // 没有头节点,则创建头节点 + if (null == head) { + head = new Node(); + head.next = null; + } +// Node temp = new Node(); +// temp = head.next; + Node newNode = new Node(); + newNode.data = o; + newNode.next = head.next; + head.next = newNode; + + size++; + } + public void add(int index, Object o) { + + size++; + } + // 头节点的索引为0 + public Object get(int index) { + Node ptr = new Node(); + ptr = head; + // 如果index越界,则抛出异常啊?要抛出异常吗? + if (index >= size() || index < 0) { + System.out.println("要抛出异常吗?"); + return null; + } else { + // 假设头节点有数据吧,节约点资源 + for (int i = 0; i < index; i++) { + ptr = ptr.next; + } + } + return ptr; + } + // 删除特定索引位置的对象 + public Object remove(int index) { + Node ptr = new Node(); + Node temp = new Node(); + ptr = head; + // 如果index越界,则抛出异常啊?要抛出异常吗? + if (index >= size() || index < 0) { + System.out.println("要抛出异常吗?"); + return null; + } else { + // 假设头节点有数据吧,节约点资源 + for (int i = 0; i < index; i++) { + ptr = ptr.next; + } + // 此时ptr指向的就是index这个节点,但是我们要的是前一个节点 + temp = ptr.next; + ptr.next = ptr.next.next; + } + size--; + return temp; + } + public int size() { + // 隐藏的成本就是多写一个函数,没别的用途就是隐藏安全用的 + return size; + } + // 因为头节点head里面也是有数据的,所以这里的插入指的是插到头前面 + public void addFirst(Object o) { + // 不能发呆了 + // 首先创建节点 + Node ptr = new Node(); + ptr.data = o; + ptr.next = head; + head = ptr; + size++; + } + // 最后一个节点指向 null嘛 + public void addLast(Object o) { + Node ptr = new Node(); + ptr.data = o; + ptr.next = null; + // 取尾节点,size正好比索引大一个 + Node lastNode = head; + for (int i = 0; i < size-1; i++) { + lastNode = lastNode.next; + } + lastNode.next = ptr; + size++; + } + public Object removeLast() { + // 取尾节点前一个,size正好比索引大一个 + Node lastNode = head; + for (int i = 0; i < size-2; i++) { + lastNode = lastNode.next; + } + lastNode.next = null; + size--; + return null; + } + public Object removeFirst() { + Node firstNode = new Node(); + firstNode.next = head.next; + head = firstNode; + size--; + return null; + } + // 节点静态内部类 + private static class Node { + Object data; // Object 是一个通用的类型 + Node next; + } +} diff --git a/group06/290149544/hw1/src/hw1/List.java b/group06/290149544/hw1/src/hw1/List.java new file mode 100644 index 0000000000..cf68bf919e --- /dev/null +++ b/group06/290149544/hw1/src/hw1/List.java @@ -0,0 +1,9 @@ +package hw1; + +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/group06/290149544/hw1/src/hw1/Queue.java b/group06/290149544/hw1/src/hw1/Queue.java new file mode 100644 index 0000000000..a8340c6a8d --- /dev/null +++ b/group06/290149544/hw1/src/hw1/Queue.java @@ -0,0 +1,20 @@ +package hw1; + +// 本质是排队,先进先出,公平嘛 +// 往队列头删除,往队列尾部插入,因为栈是严格在顶部操作,而队列就复杂的多 +public class Queue { + public void enQueue() { + + } + + public Object deQueue() { + return null; + } + public boolean isEmpty() { + return false; + } + + public int size() { + return -1; + } +} diff --git a/group06/290149544/hw1/src/hw1/Stack.java b/group06/290149544/hw1/src/hw1/Stack.java new file mode 100644 index 0000000000..590aa3a93b --- /dev/null +++ b/group06/290149544/hw1/src/hw1/Stack.java @@ -0,0 +1,39 @@ +package hw1; + +// mini jvm 用来做函数调用,比如表达式求值啦,都会用到栈 +public class Stack { + // 动态需要的成员属性 + private int size = 0; + + // ArrayList 使用我们自己定义的数据结构,自己吃自己的狗粮 + private ArrayList elementData = new ArrayList(); // 估计先调用自己吧 + + // push对内部而言就是向数组插入一个元素,吃自己的狗粮了,狗粮是之前做的独立的 + public void push(Object o) { + elementData.add(o); + size++; + } + // 弹出一个元素,内部实现就是删除一个元素 + public Object pop() { + // + Object temp; + temp = elementData.remove(size); + size--; // 单独写,保证代码可读性 + return temp; + } + public boolean isEmpty() { + if (size == 0) { + return true; + } else { + return false; + } + } + + public Object peek() { + return elementData.get(size-1); + } + + public int size() { + return size; + } +} diff --git a/group06/309953838/.classpath b/group06/309953838/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group06/309953838/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group06/309953838/.gitignore b/group06/309953838/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/309953838/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/309953838/.project b/group06/309953838/.project new file mode 100644 index 0000000000..7d9becf359 --- /dev/null +++ b/group06/309953838/.project @@ -0,0 +1,17 @@ + + + 309953838Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/309953838/src/com/team6/week1/ArrayList.java b/group06/309953838/src/com/team6/week1/ArrayList.java new file mode 100644 index 0000000000..6aba74bd93 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/ArrayList.java @@ -0,0 +1,57 @@ +package com.team6.week1; + +public class ArrayList implements List { + + int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + + if (size >= elementData.length) { + Object[] tem = new Object[elementData.length * 2]; + for (int i = 0; i < size; i++) { + tem[i] = elementData[i]; + } + elementData = tem; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + if (size >= elementData.length) { + Object[] tem = new Object[elementData.length * 2]; + for (int i = 0; i < size; i++) { + tem[i] = elementData[i]; + } + elementData = tem; + } + for (int i = index; i < size; i++) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + return elementData[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + for (int i = index; i < size; i++) { + elementData[i - 1] = elementData[i]; + } + size--; + return elementData[index]; + } + + public int size() { + return size; + + } + +} diff --git a/group06/309953838/src/com/team6/week1/LinkedList.java b/group06/309953838/src/com/team6/week1/LinkedList.java new file mode 100644 index 0000000000..d3863b0b05 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/LinkedList.java @@ -0,0 +1,121 @@ +package com.team6.week1; + +public class LinkedList implements List { + + private Node root; + int index; + + public void addNode(String name) { + if (root == null) { + root = new Node(name); + } else { + root.add(name); + } + } + + class Node { + Object data; + Node next; + Node(Object data) { + this.data = data; + } + + + public void add(Object data) { + if (this.next == null) { + this.next = new Node(data); + } else { + this.next.add(data); + } + } + + + public Object del(int i) { + if (this.next != null) { + index++; + if (i == index) { + this.next = this.next.next; + return this.next.data; + } else { + this.next.del(i); + } + } + return null; + } + + public void traversal() { + if (this.next != null) { + index++; + this.next.traversal(); + } + } + + public void add(int i, Object o) { + if (this.next != null) { + if (i == index) { + Node node = new Node(data); + node.next = this.next.next; + this.next = node; + return; + } else { + this.next.add(i, o); + } + index++; + } + } + + public Object get(int i) { + if (this.next != null) { + + if (i == index) { + return this.data; + } else { + this.next.get(i); + } + index++; + } + return null; + } + + } + + @Override + public void add(Object data) { + if (root == null) { + root = new Node(data); + } else { + root.add(data); + } + } + + @Override + public void add(int index, Object o) { + if (root != null) { + root.add(index, o); + } + } + + @Override + public Object get(int index) { + if (root.next != null) { + return root.get(index); + } + return null; + } + + @Override + public Object remove(int index) { + if (root != null) { + return root.del(index); + } + return null; + } + + @Override + public int size() { + if (root != null) { + root.traversal(); + } + return index; + } +} diff --git a/group06/309953838/src/com/team6/week1/List.java b/group06/309953838/src/com/team6/week1/List.java new file mode 100644 index 0000000000..ee63e5d6f3 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/List.java @@ -0,0 +1,14 @@ +package com.team6.week1; + +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/group06/309953838/src/com/team6/week1/Queue.java b/group06/309953838/src/com/team6/week1/Queue.java new file mode 100644 index 0000000000..6bdd2604a8 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/Queue.java @@ -0,0 +1,62 @@ +package com.team6.week1; + +public class Queue { + private Node first; + private int index; + + class Node{ + Object data; + Node next; + + + Node(Object data){ + this.data = data; + } + + public void add(Object data){ + if(this.next == null){ + this.next = new Node(data); + }else{ + this.next.add(data); + } + } + + + public void traversal(){ + if(this.next != null){ + index++; + this.next.traversal(); + } + } + } + + public void enQueue(Object o){ + if(first != null){ + first.add(o); + } + } + + public Object deQueue(){ + if(first != null){ + Object obj = first.data; + first = first.next; + return obj; + } + return null; + } + + public boolean isEmpty(){ + if(first == null){ + return true; + }else{ + return false; + } + } + + public int size(){ + if(first != null){ + first.traversal(); + } + return index; + } +} diff --git a/group06/309953838/src/com/team6/week1/Stack.java b/group06/309953838/src/com/team6/week1/Stack.java new file mode 100644 index 0000000000..6568b5dea9 --- /dev/null +++ b/group06/309953838/src/com/team6/week1/Stack.java @@ -0,0 +1,32 @@ +package com.team6.week1; + +public class Stack { + +private List elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int size = elementData.size(); + Object obj = elementData.remove(size--); + return obj; + } + + public Object peek(){ + int size = elementData.size(); + return elementData.get(size - 1); + } + public boolean isEmpty(){ + int size = elementData.size(); + if(size == 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/547958234/src/com/coding/basic/ArrayList.java b/group06/547958234/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..30be14a906 --- /dev/null +++ b/group06/547958234/src/com/coding/basic/ArrayList.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +import java.util.*; + +public class ArrayList implements List { + private int size = 0; + private static final int INCREMENT = 10; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + growLength(); + elementData[size++] = o; + } + public void add(int index, Object o){ + growLength(); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index) { + //如果之前分配了很多内存,多次remove后是否需要将elementData手动缩小容量 + if (index > size) throw new IndexOutOfBoundsException("index: "+index+", size: "+size); + Object retVal = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + elementData[--size] = null; + return retVal; + } + + public int size(){ + return this.size; + } + public Iterator iterator(){ + return null; + } + + private void growLength() { + if (this.size == elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length+INCREMENT); + } + } + +} diff --git a/group06/547958234/src/com/coding/basic/BinaryTreeNode.java b/group06/547958234/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4dff1cdb31 --- /dev/null +++ b/group06/547958234/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,55 @@ +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 < this.data) { + if (this.left != null) { + this.left.insert(o); + } else { + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + this.left = node; + } + } + if (o > this.data) { + if (this.right != null) { + this.right.insert(o); + } else { + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + this.right = node; + } + } + return this; + } + +} diff --git a/group06/547958234/src/com/coding/basic/Iterator.java b/group06/547958234/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group06/547958234/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/group06/547958234/src/com/coding/basic/LinkedList.java b/group06/547958234/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..bc4dd44b5b --- /dev/null +++ b/group06/547958234/src/com/coding/basic/LinkedList.java @@ -0,0 +1,116 @@ +package com.coding.basic; + +import sun.jvm.hotspot.debugger.win32.coff.AuxBfEfRecord; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + if (this.size == 0) { + addFirst(o); + } else { + Node node = findNode(size - 1); + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + node.next = newNode; + this.size++; + } + } + + public void add(int index, Object o) { + ensureNoOverStep(index); + if (index == 0) { + addFirst(o); + } else if (index == this.size) { + addLast(o); + } else { + Node beforeNode = findNode(index - 1); + Node newNode = new Node(); + newNode.data = o; + newNode.next = beforeNode.next; + beforeNode.next = newNode; + this.size++; + } + + } + + public Object get(int index) { + ensureNoOverStep(index); + Node node = findNode(index); + return node.data; + } + + public Object remove(int index) { + //只需要把对应节点从链表中摘出来就行了? + ensureNoOverStep(index); + if (index == 0) { + return removeFirst(); + } else if (index == this.size - 1) { + return removeLast(); + } else { + Node beforeNode = findNode(index - 1); + Node selectNode = beforeNode.next; + beforeNode.next = selectNode.next; + this.size--; + return selectNode.data; + } + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node node = new Node(); + node.data = o; + node.next = this.head; + this.head = node; + this.size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + Node node = new Node(); + node = head; + head = head.next; + this.size--; + return node.data; + } + + public Object removeLast() { + Node beforeNode = findNode(this.size - 2); + Node node = beforeNode.next; + beforeNode.next = null; + this.size--; + return node.data; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + } + + private Node findNode(int index) { + ensureNoOverStep(index); + Node node = this.head; + while (index > 0) { + node = node.next; + index--; + } + return node; + } + + private void ensureNoOverStep(int index) { + if (index > this.size) throw new IndexOutOfBoundsException("Index: " + index + ", size: " + this.size); + } +} diff --git a/group06/547958234/src/com/coding/basic/LinkedListTest.java b/group06/547958234/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..afe137351f --- /dev/null +++ b/group06/547958234/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,20 @@ +package com.coding.basic; + +import com.coding.basic.LinkedList; +/** + * Created by mac on 2017/2/21. + */ +public class LinkedListTest { + public static void main(String[] args) { + LinkedList l = new LinkedList(); + l.add(0); + l.add(1); + l.add(2); + l.add(3,3); + Object ret = l.remove(1); + + for(int i=0;i 0) + + System.arraycopy(elementData, index+1, elementData, index ,size - index-1); + elementData[--size] = null; + + + return o; + } + public int size(){ + return size; + + } + public int Capacity(){ + return capacity; + } + private void rangCheck(int index){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + +// private class MyIndexOutOfBoundsException extends RuntimeException{ +// @SuppressWarnings("unused") +// public MyIndexOutOfBoundsException(String e) { +// super(); +// } +// } + private void ensureCapacityInternal(int minCapacity) { + + modCount++; + if (elementData == EMPTY_ELEMENTDATA) { + minCapacity = Math.max(10, minCapacity); + capacity=minCapacity; + } + if (minCapacity - elementData.length > 0) + + grow(minCapacity); + } + private void grow(int minCapacity) { + // overflow-conscious code + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity; + capacity=newCapacity; + + elementData = Arrays.copyOf(elementData, newCapacity); + } + public Iterator iterator() { + return new Itr(); + } + private class Itr implements Iterator { + int cursor; + int lastRet = -1; + int expectedModCount = modCount; + + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = MyArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return elementData[lastRet = i]; + + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + MyArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + + } + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + +} diff --git a/group06/736464448/data_structure/MyBinaryTree.java b/group06/736464448/data_structure/MyBinaryTree.java new file mode 100644 index 0000000000..f7ba05f9cd --- /dev/null +++ b/group06/736464448/data_structure/MyBinaryTree.java @@ -0,0 +1,198 @@ +package data_structure; + + + +public class MyBinaryTree { + + private BinaryTreeNode root; + private int size; + + + + public void add(int key,Object o) { + size++; + BinaryTreeNode target=null; + final BinaryTreeNode parent=root; + final BinaryTreeNode newnode=new BinaryTreeNode(key,o,null,null,null); + if(parent==null) + root=newnode; + else{ + target=compareKey(key,parent); + + if (key < target.key) { + target.left = newnode; + newnode.top = target; + } else if(key > target.key){ + target.right = newnode; + newnode.top = target; + } + else{ + target.data=o; + size--; + } + + } + + } + public Object get(int key){ + BinaryTreeNode target=null; + target=search( key); + if(target==null) + return null; + else + return target.data; + } + private BinaryTreeNode search(int key){ + BinaryTreeNode target=null; + final BinaryTreeNode parent=root; + if(parent==null) + return null; + + else + target=compareKey(key,parent); + if (key == target.key) { + + return target; + } + return null; + } + public Object remove(int key){ + BinaryTreeNode replace=null; + BinaryTreeNode target=null; + BinaryTreeNode oldnode=null; + + target=search( key); + if(target==null) + return null; + else + { + oldnode=target; + if(target.left==null&&target.right==null){ + + changeParent( target,null); + target=null; + } + else if(target.left!=null&&target.right==null){ + // replace=next(target.left); + // target=replace; + // changeParent(target,replace); + // changeChild(target, replace); + // changeParent(replace,null); + // target=null; + + replace=target.left; + changeParent(target,replace); + replace.top=target.top; + target=null; + } + else if(target.left==null&&target.right!=null){ +// replace=prev(target.right); +// target=replace; +// changeParent(target,replace); +// changeChild(target, replace); +// changeParent(replace,null); +// replace=null; + + replace=target.right; + changeParent(target,replace); + replace.top=target.top; + target=null; + } + else if(target.left!=null&&target.right!=null){ + int prev=prev(target.right).key; + int next=next(target.left).key; + if((next-key)>(key-prev)) + replace=prev(target.right); + else + replace=next(target.left); + target=replace; + changeParent(target,replace); + changeChild(target, replace); + changeParent(replace,null); + replace=null; + } + } + size--; + return oldnode.data; + } + private void changeParent(BinaryTreeNode target,BinaryTreeNode child){ + BinaryTreeNode targetparent=null; + targetparent=target.top; + if(targetparent.key>target.key) + targetparent.left=child; + else + targetparent.right=child; + + } + private void changeChild(BinaryTreeNode target,BinaryTreeNode parent){ + BinaryTreeNode targetleftchild=null; + BinaryTreeNode targetrightchild=null; + targetleftchild=target.left; + targetrightchild=target.right; + if(targetleftchild!=null) + targetleftchild.top=parent; + if(targetrightchild!=null) + targetrightchild.top=parent; + + } + //找到前驱节点 + private BinaryTreeNode prev(BinaryTreeNode target){ + // BinaryTreeNode prev=null; + while(target.left!=null){ + target=target.left; + } + return target; + + } + //找到后驱节点 + private BinaryTreeNode next(BinaryTreeNode target){ +// BinaryTreeNode next=null; + while(target.right!=null){ + target=target.right; + } + return target; + + } + public int size(){ + + return size; + } + private BinaryTreeNode compareKey(int key ,BinaryTreeNode node) { + BinaryTreeNode parent=node; + while (parent != null) { + + if (key < parent.key&&parent.left!=null) { + parent = parent.left; + } else if (key > parent.key&&parent.right!=null) { + parent = parent.right; + } else { + return parent; + + } + } + return parent; + + + } + + + + + private static class BinaryTreeNode{ + Object data; + int key; + BinaryTreeNode left; + BinaryTreeNode right; + BinaryTreeNode top; + public BinaryTreeNode(int key,Object o, BinaryTreeNode top, BinaryTreeNode left,BinaryTreeNode right){ + this.key=key; + this.data=o; + this.left=left; + this.right=right; + this.top=top; + + } + + + } +} diff --git a/group06/736464448/data_structure/MyLinkedList.java b/group06/736464448/data_structure/MyLinkedList.java new file mode 100644 index 0000000000..32097115e7 --- /dev/null +++ b/group06/736464448/data_structure/MyLinkedList.java @@ -0,0 +1,209 @@ +package data_structure; + + +import java.util.Iterator; +import java.util.NoSuchElementException; + + + +public class MyLinkedList { + private int size; + private Node head; + private Node last; + + public void add(Object o){ + + linkLast(o); + + } + + public Object get(int index){ + checkPositionIndex(index); + Node node=node(index); + return node.item; + } + public Object remove(int index){ + checkPositionIndex(index); + Node node=node(index); + isnull(node); + Object o=null; + Node before=null; + if(index==0){ + o=node.item; + node.next=head; + node=null; + } + else + { + before=node(index-1); + before.next=node.next; + o=node.item; + node=null; + + } + + return o; + } + public int size(){ + + return size; + } + public void addFirst(Object o){ + + linkFirst(o); + + + } + public void addLast(Object o){ + linkLast(o); + + } + public Object removeFirst(){ + Node f=head; + isnull(f); + final Node next=head.next; + Object o=f.item; + f=null; + head=next; + if(next==null) + last=null; + size--; + + return o; + } + //这个方法用多了也爆炸 + public Object removeLast(){ + Node l=last; + isnull(l); + Object o=null; + if(size>=2){ + final Node before=node(size-1); + o=l.item; + l=null; + last=before; + + if(before==null) + head=null; + } + else{ + o=l.item; + l=null; + last=null; + head=null; + } + + + size--; + return o; + + } + public Iterator iterator(){ + + + return new ListItr(); + } + + + private static class Node{ + Object item; + Node next; + Node(Object o, Node next){ + this.item=o; + this.next=next; + } + } + public void add(int index, Object o) { + checkPositionIndex(index); + + if (index == 0) + linkFirst(o); + else + linkNext(o, node(index-1)); + } + private void linkNext(Object o,Node node){ + final Node next=node.next; + final Node newnode=new Node(o,next); + node.next=newnode; + size++; + } + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + private void linkLast(Object o){ + final Node l=last; + final Node node=new Node(o,null); + last=node; + if(head==null) + head=node; + else + l.next=node; + size++; + } + private void linkFirst(Object o){ + final Node f=head; + final Node node=new Node(o,null); + head=node; + if(last==null) + last=node; + else + head.next=f; + size++; + } + private void isnull(Node node){ + if (node == null) + throw new NoSuchElementException(); + } + Node node(int index) { + // assert isElementIndex(index); + + + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + + + } + + private class ListItr implements Iterator { + private Node next=head; + private int nextIndex; + private Node lastReturned = null; + + public boolean hasNext() { + return nextIndex < size; + } + + public Object next() { + + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + //加上死循环了 +// if(nextIndex==size){ +// next=head; +// nextIndex=0; +// } + return lastReturned.item; + } + + @Override + public void remove() { + // TODO Auto-generated method stub + + } + } + + + +} diff --git a/group06/736464448/data_structure/MyQueue.java b/group06/736464448/data_structure/MyQueue.java new file mode 100644 index 0000000000..8e3f50e0bb --- /dev/null +++ b/group06/736464448/data_structure/MyQueue.java @@ -0,0 +1,21 @@ +package data_structure; + +public class MyQueue { + private MyLinkedList elementData =new MyLinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } + +} diff --git a/group06/736464448/data_structure/MyStack.java b/group06/736464448/data_structure/MyStack.java new file mode 100644 index 0000000000..f442468fb2 --- /dev/null +++ b/group06/736464448/data_structure/MyStack.java @@ -0,0 +1,22 @@ +package data_structure; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + return elementData.get(0); + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group06/736464448/test_data_structure/TestMyArrayList.java b/group06/736464448/test_data_structure/TestMyArrayList.java new file mode 100644 index 0000000000..6ce24c90a8 --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyArrayList.java @@ -0,0 +1,93 @@ +package test_data_structure; + + + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyArrayList; + +public class TestMyArrayList { + MyArrayList list; + + @Before + public void setUp() throws Exception { + list=new MyArrayList(); + System.out.println("begin"); + } + + @After + public void tearDown() throws Exception { + System.out.println("end"); + } + + @Test + public void testMyArrayList() { + + Assert.assertEquals(0, list.Capacity()); + } + + @Test + public void testAddObject() { + list.add(new Integer(10)); + Assert.assertEquals(10, list.get(0)); + } + + @Test + public void testAddIntObject() { + list.add(1); + list.add(1); + list.add(1); + list.add(1,2); + Assert.assertEquals(2, list.get(1)); + + + } + + @Test + public void testGet() { + list.add(1); + list.add(2); + Assert.assertEquals(2, list.get(1)); + } + + @Test + public void testRemove() { + list.add(1); + list.add(2); + list.add(3); + Assert.assertEquals(2, list.remove(1)); + + } + + @Test + public void testSize() { + list.add(1); + list.add(2); + list.add(3); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testCapacity() { + list=new MyArrayList(5); + Assert.assertEquals(5, list.Capacity()); + + } + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + Iterator itr=list.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + + } + +} diff --git a/group06/736464448/test_data_structure/TestMyBinaryTree.java b/group06/736464448/test_data_structure/TestMyBinaryTree.java new file mode 100644 index 0000000000..f56696d4f7 --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyBinaryTree.java @@ -0,0 +1,59 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyBinaryTree; + + +public class TestMyBinaryTree { + MyBinaryTree bt; + + @Before + public void setUp() throws Exception { + System.out.println("开始测试"); + bt=new MyBinaryTree(); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testAdd() { + bt.add(1, "c"); + + } + + @Test + public void testGet() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(2, bt.get(2)); + } + + @Test + public void testRemove() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(2, bt.remove(2)); + + Assert.assertEquals(2, bt.size()); + } + + @Test + public void testSize() { + bt.add(1, 1); + bt.add(2, 2); + bt.add(3, 3); + Assert.assertEquals(3, bt.size()); + } + +} diff --git a/group06/736464448/test_data_structure/TestMyLinkedList.java b/group06/736464448/test_data_structure/TestMyLinkedList.java new file mode 100644 index 0000000000..9280b0f65c --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyLinkedList.java @@ -0,0 +1,111 @@ +package test_data_structure; + + + +import java.util.Iterator; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyLinkedList; + +public class TestMyLinkedList { + MyLinkedList link=null; + + @Before + public void setUp() throws Exception { + link=new MyLinkedList(); + System.out.println("测试开始"); + } + + @After + public void tearDown() throws Exception { + System.out.println("测试结束"); + } + + @Test + public void testAddObject() { + link.add(1); + Assert.assertEquals(1,link.get(0)); + } + + @Test + public void testGet() { + link.add(1); + Assert.assertEquals(1,link.get(0)); + } + + @Test + public void testRemove() { + link.add(1); + Assert.assertEquals(1,link.remove(0)); + } + + @Test + public void testSize() { + link.add(1); + Assert.assertEquals(1,link.size()); + } + + @Test + public void testAddFirst() { + link.add(1); + link.add(1); + link.add(1); + link.addFirst(2); + Assert.assertEquals(2,link.get(0)); + } + + @Test + public void testAddLast() { + link.add(1); + link.add(1); + link.add(1); + link.addLast(2); + Assert.assertEquals(2,link.get(link.size()-1)); + } + + @Test + public void testRemoveFirst() { + link.add(1); + link.add(1); + link.add(1); + link.addFirst(2); + Assert.assertEquals(2,link.removeFirst()); + } + + @Test + public void testRemoveLast() { + link.add(1); + link.add(1); + link.add(1); + link.addLast(2); + Assert.assertEquals(2,link.removeLast()); + } + + @Test + public void testIterator() { + link.add(1); + link.add(2); + link.add(3); + Iterator itr=link.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + + } + + @Test + public void testAddIntObject() { + link.add(1); + link.add(1); + link.add(1); + link.add(2, 3); + Assert.assertEquals(3,link.get(2)); + } + + + +} diff --git a/group06/736464448/test_data_structure/TestMyQueue.java b/group06/736464448/test_data_structure/TestMyQueue.java new file mode 100644 index 0000000000..dd0e96e699 --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyQueue.java @@ -0,0 +1,49 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyQueue; + +public class TestMyQueue { + MyQueue queue; + + @Before + public void setUp() throws Exception { + queue =new MyQueue(); + System.out.println("开始测试"); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testEnQueue() { + queue.enQueue("hello"); + } + + @Test + public void testDeQueue() { + queue.enQueue("hello"); + queue.enQueue("world"); + Assert.assertEquals("hello",queue.deQueue()); + } + + @Test + public void testIsEmpty() { + queue.enQueue("hello"); + Assert.assertEquals(false,queue.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0,queue.size()); + } + +} diff --git a/group06/736464448/test_data_structure/TestMyStack.java b/group06/736464448/test_data_structure/TestMyStack.java new file mode 100644 index 0000000000..7ddaba1e62 --- /dev/null +++ b/group06/736464448/test_data_structure/TestMyStack.java @@ -0,0 +1,60 @@ +package test_data_structure; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import data_structure.MyStack; + +public class TestMyStack { + MyStack mystack; + + @Before + public void setUp() throws Exception { + mystack=new MyStack(); + System.out.println("开始测试"); + } + + @After + public void tearDown() throws Exception { + System.out.println("结束测试"); + } + + @Test + public void testPush() { + mystack.push("Hello"); + mystack.push(","); + mystack.push("World"); + } + + @Test + public void testPop() { + mystack.push("Hello"); + mystack.push(","); + mystack.push("World"); + Assert.assertEquals("World", (String)mystack.pop()); + Assert.assertEquals(",", (String)mystack.pop()); + + } + + @Test + public void testPeek() { + mystack.push("Hello"); + Assert.assertEquals("Hello", (String)mystack.peek()); + } + + @Test + public void testIsEmpty() { + + Assert.assertEquals(true, mystack.isEmpty()); + } + + @Test + public void testSize() { + Assert.assertEquals(0, mystack.size()); + } + +} diff --git a/group06/799237637/secondhomework/src/com/liteStructs/LoginAction.java b/group06/799237637/secondhomework/src/com/liteStructs/LoginAction.java new file mode 100644 index 0000000000..12f5362069 --- /dev/null +++ b/group06/799237637/secondhomework/src/com/liteStructs/LoginAction.java @@ -0,0 +1,5 @@ +package com.liteStructs; + +public class LoginAction { + +} diff --git a/group06/799237637/secondhomework/src/com/liteStructs/Structs.java b/group06/799237637/secondhomework/src/com/liteStructs/Structs.java new file mode 100644 index 0000000000..ab36173eaf --- /dev/null +++ b/group06/799237637/secondhomework/src/com/liteStructs/Structs.java @@ -0,0 +1,5 @@ +package com.liteStructs; + +public class Structs { + +} diff --git a/group06/799237637/secondhomework/src/com/liteStructs/View.java b/group06/799237637/secondhomework/src/com/liteStructs/View.java new file mode 100644 index 0000000000..4942fc8c6c --- /dev/null +++ b/group06/799237637/secondhomework/src/com/liteStructs/View.java @@ -0,0 +1,5 @@ +package com.liteStructs; + +public class View { + +} diff --git a/group06/799237637/secondhomework/src/com/liteStructs/struts.xml b/group06/799237637/secondhomework/src/com/liteStructs/struts.xml new file mode 100644 index 0000000000..cc909972cd --- /dev/null +++ b/group06/799237637/secondhomework/src/com/liteStructs/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/group06/799237637/secondhomework/src/secondhomework/MyArrayUtil.java b/group06/799237637/secondhomework/src/secondhomework/MyArrayUtil.java new file mode 100644 index 0000000000..4b5c626cd2 --- /dev/null +++ b/group06/799237637/secondhomework/src/secondhomework/MyArrayUtil.java @@ -0,0 +1,214 @@ +package secondhomework; + +import java.util.ArrayList; +import java.util.Arrays; + +/* + * ʵArrayUtil + */ +@SuppressWarnings("all") +public class MyArrayUtil { + /** + * һ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 size =origin.length; + + for(int i=0;iinitialcapcity){ + enlarge(); + } + elements[size]=o; + size++; + + } + public Object get(int index){ + if(index<0||index>size-1){ + throw new IndexOutOfBoundsException("Խ磺"); + } + return elements[index]; + + } +//ʵremove()ɾλãԪҪǰƣɾλõԪ + public Object remove(int index){ + Object oldValue=elements[index]; + int eleMoved=size-index-1; //ƶԪصĸ + if(eleMoved>0){ + System.arraycopy(elements, //ԭ + index+1, //ԭʼλãɾԪصĺһλã + elements, //Ŀ + index, //Ŀʼλ + eleMoved);//ij + } + elements[size-1]=null; + size--; + + return oldValue; + } + + public boolean set(int index,Object o){ + if(index<0||index>size-1){ + throw new IndexOutOfBoundsException("Խ"); + } + elements[index]=o; + return true; + } + + //дtoString() + public String toString(){ + StringBuffer sb=new StringBuffer(); + sb.append("["); + for(int i=0;i + + + + + + diff --git a/group06/949319266/.project b/group06/949319266/.project new file mode 100644 index 0000000000..d3a5c7c636 --- /dev/null +++ b/group06/949319266/.project @@ -0,0 +1,17 @@ + + + algorithm + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/949319266/.settings/org.eclipse.jdt.core.prefs b/group06/949319266/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..500f6b26f4 --- /dev/null +++ b/group06/949319266/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Fri Feb 24 09:38:47 CST 2017 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +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.6 diff --git a/group06/949319266/949319266learn/.classpath b/group06/949319266/949319266learn/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group06/949319266/949319266learn/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/949319266/949319266learn/.settings/org.eclipse.jdt.core.prefs b/group06/949319266/949319266learn/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group06/949319266/949319266learn/.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/group06/949319266/RemoteSystemsTempFiles/.project b/group06/949319266/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group06/949319266/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group06/949319266/homework/.classpath b/group06/949319266/homework/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group06/949319266/homework/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/949319266/homework/.gitignore b/group06/949319266/homework/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/949319266/homework/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/949319266/homework/.project b/group06/949319266/homework/.project new file mode 100644 index 0000000000..b4bd3f32d4 --- /dev/null +++ b/group06/949319266/homework/.project @@ -0,0 +1,17 @@ + + + homework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/949319266/homework/.settings/org.eclipse.core.resources.prefs b/group06/949319266/homework/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..8822690b9a --- /dev/null +++ b/group06/949319266/homework/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/cn/ecust/Iitestruts/LoginAction.java=UTF-8 +encoding//src/cn/ecust/Iitestruts/Struts.java=UTF-8 +encoding//src/cn/ecust/Iitestruts/StrutsTest.java=UTF-8 diff --git a/group06/949319266/homework/.settings/org.eclipse.jdt.core.prefs b/group06/949319266/homework/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group06/949319266/homework/.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/group06/949319266/homework/src/cn/ecust/Array/ArrayUtil.java b/group06/949319266/homework/src/cn/ecust/Array/ArrayUtil.java new file mode 100644 index 0000000000..8965b7a1aa --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Array/ArrayUtil.java @@ -0,0 +1,201 @@ +package cn.ecust.Array; + +import java.util.ArrayList; +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 tem = 0; + for(int i = 0; i < origin.length/2; i++) { + tem = origin[i]; + origin[i] = origin[origin.length-1-i]; + origin[origin.length-1-i] = tem; + } + + } + + /** + * µһ飺 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * ҪֵΪ0ȥΪ0ֵһµ飬ɵΪ + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + ArrayList arr = new ArrayList(); + for(int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0) { + arr.add(oldArray[i]); + } + } + oldArray = null; + for(int i = 0; i= array1.length){ //1Ѿ + array3[c++] = array2[b++]; + continue; + } + + if(b >= array2.length){ //2Ѿ + array3[c++] = array1[a++]; + continue; + } + + if(array1[a] > array2[b]){ + array3[c++] = array2[b++]; + }else if(array1[a] < array2[b]){ + array3[c++] = array1[a++]; + }else{ + array3[c++] = array1[a++]; + b++; + } + } + array3 = Arrays.copyOf(array3, c); + return array3; + } + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length+size]; + for(int i = 0 ; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + + public int[] fibonacci(int max){ + ArrayList arr = new ArrayList(); + int i = 1; + int j = 1; + arr.add(i);arr.add(j); + int m=i+j; + while(m arr = new ArrayList(); + for(int i=0;i= result.length) + result = grow(result, result.length); + for(int j = 2; j < i; j++){ + if(i % j == 0) + sum += j; + } + if(sum == i) + result[len++] = sum; + } + result = Arrays.copyOf(result, len); + return result; + } + + /** + * seperator array + * array= [3,8,9], seperator = "-" + * 򷵻ֵΪ"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String result = new String(); + int len = 0; + result = new Integer(array[0]).toString(); + for(int i = 1; i < array.length; i++){ + result += seperator; + result += array[i]; + } + return result; + } + +} diff --git a/group06/949319266/homework/src/cn/ecust/Iitestruts/LoginAction.java b/group06/949319266/homework/src/cn/ecust/Iitestruts/LoginAction.java new file mode 100644 index 0000000000..004f428e2b --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/LoginAction.java @@ -0,0 +1,39 @@ +package cn.ecust.Iitestruts; + +/** + * 杩欐槸涓�涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩�� + * @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/group06/949319266/homework/src/cn/ecust/Iitestruts/Struts.java b/group06/949319266/homework/src/cn/ecust/Iitestruts/Struts.java new file mode 100644 index 0000000000..27c36c6902 --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/Struts.java @@ -0,0 +1,34 @@ +package cn.ecust.Iitestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛� 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛� + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛� 渚嬪parameters涓殑鏁版嵁鏄� + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛� 骞惰幏寰楄繑鍥炲�硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡� getMessage锛�, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂�煎拰灞炴�у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲�硷紝 纭畾鍝竴涓猨sp锛� + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓�� + + */ + + return null; + } + +} diff --git a/group06/949319266/homework/src/cn/ecust/Iitestruts/StrutsTest.java b/group06/949319266/homework/src/cn/ecust/Iitestruts/StrutsTest.java new file mode 100644 index 0000000000..10c0d5c6db --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package cn.ecust.Iitestruts; + +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/group06/949319266/homework/src/cn/ecust/Iitestruts/View.java b/group06/949319266/homework/src/cn/ecust/Iitestruts/View.java new file mode 100644 index 0000000000..dd652dad20 --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/View.java @@ -0,0 +1,23 @@ +package cn.ecust.Iitestruts; + +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/group06/949319266/homework/src/cn/ecust/Iitestruts/struts.xml b/group06/949319266/homework/src/cn/ecust/Iitestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/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/group06/949319266/homework/src/cn/ecust/Test/ArrayUtilTest.java b/group06/949319266/homework/src/cn/ecust/Test/ArrayUtilTest.java new file mode 100644 index 0000000000..0ac38f0256 --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Test/ArrayUtilTest.java @@ -0,0 +1,26 @@ +package cn.ecust.Test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import cn.ecust.Array.ArrayUtil; + +public class ArrayUtilTest { + + @Test + public void test() { + int[] a = {7, 9, 30, 0, 0, 0, 3, 4}; + int[] b = {3,4,5,6,7,8}; + ArrayUtil au = new ArrayUtil(); + au.reverseArray(a); + //au.removeZero(a); + au.merge(a, b); + au.grow(a, 3); + au.fibonacci(25); + au.getPrimes(25); + au.getPerfectNumbers(25); + au.join(a, "-"); + } + +} diff --git "a/group06/949319266/homework/src/\346\226\207\347\253\2402.txt" "b/group06/949319266/homework/src/\346\226\207\347\253\2402.txt" new file mode 100644 index 0000000000..89823c8f5a --- /dev/null +++ "b/group06/949319266/homework/src/\346\226\207\347\253\2402.txt" @@ -0,0 +1 @@ +http://blog.sina.com.cn/s/blog_c20b18280102x3fi.html \ No newline at end of file diff --git a/group06/949319266/src/com/coding/basic/ArrayList.java b/group06/949319266/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9d2dca03c2 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/ArrayList.java @@ -0,0 +1,93 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class ArrayList implements List { + + private int size; + private int current ; + private int point; + private Object[] elementData = null; + private static final int DEFAULT_SIZE=100; + + public ArrayList() { + this(DEFAULT_SIZE); + } + + public ArrayList(int size) { + if(size < 0) { + System.out.println("СС0"); + } else { + this.elementData = new Object[size]; + this.current = 0; + point = size; + } + } + + public void add(E e){ + judgeSize(); + this.elementData[current] = e; + this.current++; + } + public void add(int index, E e){ + judgeIndex(index); + for(int i=0 ; i= index && i+2 < elementData.length) { + elementData[i] = e; + elementData[i+1] = elementData[i+2]; + } + } + current++; + } + + public E get(int index){ + judgeIndex(index); + return (E)this.elementData[index]; + } + + public void remove(int index) { + judgeIndex(index); + for(int i=0;i0) { + return true; + } + return false; + } + public void clear() { + elementData = new Object[DEFAULT_SIZE]; + } + private void judgeSize() { + Object[] newarr = new Object[elementData.length + DEFAULT_SIZE]; + System.arraycopy(elementData, 0, newarr, 0, elementData.length); + this.elementData = newarr; + } + public void judgeIndex(int index) { + if(index < 0 || index > point) { + System.out.println("±Խ"); + } + } + +} diff --git a/group06/949319266/src/com/coding/basic/BinaryTreeNode.java b/group06/949319266/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..941c31bc66 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode getRoot() { + return root; + } + + public Object getData() { + return data; + } + public void setData(int 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 void insert(int value){ + BinaryTreeNode newNode = new BinaryTreeNode(); + if(root == null) { + root=newNode; + root.left = null; + root.right = null; + } else { + BinaryTreeNode currentNode = root; + BinaryTreeNode parentNode; + while(true) { + parentNode = currentNode; + if(newNode.data > currentNode.data) { + currentNode = currentNode.getRight(); + if(currentNode == null) { + parentNode.setRight(newNode); + return; + } + } else { + currentNode = currentNode.getLeft(); + if(currentNode == null) { + parentNode.setLeft(newNode); + return; + } + } + } + + } + } + public boolean find(int key) { + BinaryTreeNode cNode = root; + if(cNode != null) { + while(cNode.data != key) { + if(cNode.data > key) { + cNode = cNode.getLeft(); + } else { + cNode = cNode.getRight(); + } + return true; + } + return true; + } + return false; + } + // + public void inOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + inOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + inOrder(treeNode.getRight()); + } + } + // + public void leftOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + leftOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + leftOrder(treeNode.getRight()); + } + } + // + public void rightOrder(BinaryTreeNode treeNode) { + if(treeNode != null) { + rightOrder(treeNode.getLeft()); + System.out.println(treeNode.data); + rightOrder(treeNode.getRight()); + } + } +} diff --git a/group06/949319266/src/com/coding/basic/LinkedList.java b/group06/949319266/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..ee95a118e0 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/LinkedList.java @@ -0,0 +1,133 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class LinkedList implements List { + + private Node last; + private Node head; + private int xsize = 0; + + public LinkedList() { + init(); + } + + private void init() { + head = new Node(null,null,null); + last = new Node(null,head,null); + head.next = last; + xsize = 0; + } + + //һڲʹ;index֮ǰһڵ + private void add(Node node,int index) { + if(index < 0 || index > xsize) { + throw new IndexOutOfBoundsException("±Խ"); + } + node.pre = getNode(index-1); + getNode(index-1).next = node; + node.next = getNode(index); + getNode(index).pre = node; + xsize++; + } + public void add(E e){ + add(new Node(e,null,null),xsize); + } + + public void add(int index,E e){ + add(new Node(e,null,null),index); + } + private Node getNode(int index){ + Node newNode; + int current; + if(index == -1) { + return head; + } else if (index == xsize ){ + return last; + } else { + current = 0; + newNode = head.next; + while (current < index) { + newNode = newNode.next; + current++; + } + } + return newNode; + } + + public E get(int index) { + return getNode(index).e; + } + + public void remove(int index){ + Node node = getNode(index); + node.pre.next = node.next; + node.next.pre=node.pre; + xsize--; + } + + public int size(){ + return xsize; + } + + public void addFirst(E e){ + add(new Node(e,null,null),0); + } + public void addLast(E e){ + add(new Node(e,null,null),xsize); + } + public void removeFirst(){ + remove(0); + } + public void removeLast(){ + remove(xsize); + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + public E e; + Node next; + Node pre; + public Node (E e,Node pre,Node next) { + this.pre = pre; + this.next = next; + this.e = e; + } + + } + + + public boolean contains(Node node) { + Node mnode = head.next; + while(mnode !=null) { + if(mnode.equals(node)) { + return true; + } + mnode = mnode.next; + } + return false; + } + public boolean isEmpty() { + return xsize == 0 ? true:false; + } + public void clear() { + init(); + } + + public boolean contains(E e) { + // TODO Auto-generated method stub + Node node = null; + while((node=(head.next)) != null) { + if(e.equals(node)) { + return true; + } else { + node = node.next; + } + } + return false; + } + + +} diff --git a/group06/949319266/src/com/coding/basic/List.java b/group06/949319266/src/com/coding/basic/List.java new file mode 100644 index 0000000000..970c0cfc66 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + + +public interface List { + public void add(E e); + public void add(int index, E e); + //public E get(int index); + public void remove(int index); + public boolean contains(E e); + public int size(); + public boolean isEmpty(); + public void clear(); +} diff --git a/group06/949319266/src/com/coding/basic/Queue.java b/group06/949319266/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b090a3626e --- /dev/null +++ b/group06/949319266/src/com/coding/basic/Queue.java @@ -0,0 +1,52 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class Queue { + E[] element; + //Ϊ˲ԷʱΪ5 + private static final int DEFAULT_SIZE=5; + int front,rear;//ֱΪ׺Ͷβ± + + public Queue() { + this(DEFAULT_SIZE); + } + public Queue(int size) { + element = (E[])(new Object[size]); + front = 0; + rear = 0; + } + + public void enQueue(E e){ + if(((rear+1)%element.length) == front) { + System.out.println("޷"); + } else { + element[rear] = e; + rear=(rear+1)%element.length; + } + } + + public E deQueue(){ + if(rear == front) { + return null; + } else { + E e = element[front]; + front = (front + 1)%element.length; + return e; + } + + } + + public boolean isEmpty(){ + return rear == front; + } + + public int size(){ + if(rear > front){ + return rear-front; + } else { + return (element.length+1)-(front-rear); + } + } + +} diff --git a/group06/949319266/src/com/coding/basic/Stack.java b/group06/949319266/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..2a309fde95 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/Stack.java @@ -0,0 +1,36 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class Stack { + private ArrayList element; + int top; + public Stack() { + element = new ArrayList(); + top = 0; + } + + public void push(Object o){ + element.add(o); + top++; + } + + public void pop(){ + if(isEmpty()) { + System.out.println("ջǿյ"); + System.exit(0); + } + element.remove(top-1); + top--; + } + + public Object peek(){ + return element.get(top-1); + } + public boolean isEmpty(){ + return top == 0?true:false; + } + public int size(){ + return top; + } +} diff --git a/group06/949319266/src/com/coding/basic/test/ArrayListTest.java b/group06/949319266/src/com/coding/basic/test/ArrayListTest.java new file mode 100644 index 0000000000..5abd16725f --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/ArrayListTest.java @@ -0,0 +1,39 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.ArrayList; + +public class ArrayListTest { + + @Test + public void test() { + ArrayList list = new ArrayList(); + + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + + System.out.println("±Ϊ3ԪΪ"+list.get(3)); + System.out.println("鳤"+list.size()); + list.remove(2); + System.out.println("remove鳤"+list.size()); + + for(int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + list.add(3, "g"); + System.out.println(""); + System.out.println("Ϊ"); + + for(int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + + + } + +} diff --git a/group06/949319266/src/com/coding/basic/test/LinkedListTest.java b/group06/949319266/src/com/coding/basic/test/LinkedListTest.java new file mode 100644 index 0000000000..2b4c390721 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/LinkedListTest.java @@ -0,0 +1,24 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.Test; + +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + @Test + public void test() { + LinkedList list = new LinkedList(); + list.add("First"); + list.add("Second"); + list.add("Thrid"); + for(int i = 0; i < list.size(); i++) { + System.out.print(list.get(i)+ " "); + } + + } +} diff --git a/group06/949319266/src/com/coding/basic/test/QueueTest.java b/group06/949319266/src/com/coding/basic/test/QueueTest.java new file mode 100644 index 0000000000..6ec8036c35 --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/QueueTest.java @@ -0,0 +1,28 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Queue; + +public class QueueTest { + + @Test + public void test() { + Queue qu = new Queue(); + qu.enQueue("gong"); + qu.enQueue("bo"); + qu.enQueue("jie"); + System.out.println(qu.size()); + qu.deQueue(); + System.out.println(qu.size()); + System.out.println(qu.isEmpty()); + qu.enQueue("gong"); + qu.enQueue("bo"); + qu.deQueue(); + qu.enQueue("jie"); + System.out.println(qu.size()); + } + +} diff --git a/group06/949319266/src/com/coding/basic/test/StackTest.java b/group06/949319266/src/com/coding/basic/test/StackTest.java new file mode 100644 index 0000000000..d76742e8ff --- /dev/null +++ b/group06/949319266/src/com/coding/basic/test/StackTest.java @@ -0,0 +1,26 @@ +package com.coding.basic.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.Stack; + +public class StackTest { + + @Test + public void test() { + Stack s = new Stack(); + s.push("gong"); + s.push("bo"); + s.push("jie"); + s.push("hao"); + s.push("ren"); + System.out.println(s.size()); + System.out.println(s.peek()); + s.pop(); + System.out.println(s.size()); + System.out.println(s.peek()); + } + +} diff --git "a/group06/949319266/\346\226\207\347\253\240.txt" "b/group06/949319266/\346\226\207\347\253\240.txt" new file mode 100644 index 0000000000..f00eb98185 --- /dev/null +++ "b/group06/949319266/\346\226\207\347\253\240.txt" @@ -0,0 +1 @@ +http://blog.sina.com.cn/s/blog_c20b18280102x31y.html \ No newline at end of file diff --git a/group06/group06.md b/group06/group06.md index d3f5a12faa..8b13789179 100644 --- a/group06/group06.md +++ b/group06/group06.md @@ -1 +1 @@ - + diff --git "a/group07/1058267830/git\345\221\275\344\273\244.txt" "b/group07/1058267830/git\345\221\275\344\273\244.txt" deleted file mode 100644 index 8bcf2ffa0f..0000000000 --- "a/group07/1058267830/git\345\221\275\344\273\244.txt" +++ /dev/null @@ -1,11 +0,0 @@ -װgit guigit bash -1.¡ -git clone git@github.com:leijing1992/coding2017.git -2.޸ĵļ -git add -A -3.ύݴ -git commit -m "ύ***" -4.master -git pull origin master -5.ύmaster -git push origin master \ No newline at end of file diff --git a/group07/1058267830/week1/.classpath b/group07/1058267830/week1/.classpath new file mode 100644 index 0000000000..91c6d8f6c5 --- /dev/null +++ b/group07/1058267830/week1/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group07/1058267830/week1/.project b/group07/1058267830/week1/.project new file mode 100644 index 0000000000..d3e8bd8c59 --- /dev/null +++ b/group07/1058267830/week1/.project @@ -0,0 +1,17 @@ + + + week1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1058267830/week1/.settings/org.eclipse.jdt.core.prefs b/group07/1058267830/week1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group07/1058267830/week1/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group07/1058267830/week1/src/com/coding/basic/ArrayList.java b/group07/1058267830/week1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8d05511d8a --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,108 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterator { + + private Object[] obj ; + private int length; // 数组总长度 + private int size; // 元素个数 + private int currentIndex; // 当前索引位置,默认为-1 + + public int getLength() { + return length; + } + + public ArrayList(){ + this.obj = new Object[10]; + this.length = 10; + this.size = 0; + this.currentIndex = -1; + } + + public ArrayList(int initSize){ + this.obj = new Object[initSize]; + this.length = initSize; + this.size = 0; + this.currentIndex = -1; + } + + @Override + public void add(Object o) { + if(this.size < length){ + obj[size] = o; + this.size++; + + }else{ + // 扩容,add数据 + Object[] obj1 = new Object[length * 2]; + System.arraycopy(obj, 0, obj1, 0, length); + this.length = this.length * 2; + this.obj = obj1; + obj[this.size] = o; + this.size++; + } + } + + @Override + public void add(int index, Object o) { + if(index < length){ + // 容量扩1,add数据 + Object[] obj1 = new Object[length + 1]; + System.arraycopy(obj, 0, obj1, 0, index); + System.arraycopy(obj, index, obj1, index+1, length-index); + obj1[index] = o; + this.obj = obj1; + this.length++; + this.size++; + }else{ + // 容量扩到index+1, add数据 + Object[] obj1 = new Object[index + 1]; + System.arraycopy(obj, 0, obj1, 0, length); + obj1[index] = o; + this.obj = obj1; + this.length = index + 1; + this.size++; + } + } + + @Override + public Object get(int index) { + if(index >= length) + throw new RuntimeException("数组越界了..."); + return this.obj[index]; + } + + @Override + public Object remove(int index) { + if(index >= length) + throw new RuntimeException("数组越界了..."); + Object tmp = obj[index];// 取值,最后返回 + Object[] obj1 = new Object[length -1]; + System.arraycopy(obj, 0, obj1, 0, index); + System.arraycopy(obj, index+1, obj1, index, length-index-1); + this.obj = obj1; + this.length--; + this.size--; + return tmp; + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean hasNext() { + if(currentIndex == length-1){ + return false; + }else{ + currentIndex++; + return true; + } + } + + @Override + public Object next() { + return this.get(currentIndex); + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java b/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..e326a21f75 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,78 @@ +package com.coding.basic; + +public class BinaryTree { + private BinaryTreeNode root; // 根节点 + public BinaryTree( ){ + BinaryTreeNode node = new BinaryTreeNode(); + this.root = node; + } + + public boolean isEmpty(){ + return this.root == null; + } + + public void insert(Object o){ + // 如果是第一次添加节点,就是root节点 + if(root.data == null){ + BinaryTreeNode bnode = new BinaryTreeNode(o, null, null); + root = bnode; + }else{ + insert(o, root); + } + } + + // 递归添加非root节点 + private BinaryTreeNode insert(Object o, BinaryTreeNode node) { + if(node == null){ + BinaryTreeNode bnode = new BinaryTreeNode(o, null, null); + return bnode; + } + if((int)o <= (int)node.data){ + node.left = insert(o, node.left); + }else{ + node.right = insert(o, node.right); + } + + return node; + } + + // 中序遍历 + public void middlePrint(){ + middleOrder(this.root); + } + + private void middleOrder(BinaryTreeNode node) { + if(node != null){ + middleOrder(node.left); + System.out.print(node.data + " "); + middleOrder(node.right); + } + } + + // 前序遍历 + public void prePrint(){ + preOrder(this.root); + } + + private void preOrder(BinaryTreeNode node) { + if(node != null){ + System.out.print(node.data + " "); + preOrder(node.left); + preOrder(node.right); + } + } + + // 后序遍历 + public void postPrint(){ + postOrder(this.root); + } + + private void postOrder(BinaryTreeNode node) { + if(node != null){ + postOrder(node.right); + System.out.print(node.data + " "); + postOrder(node.left); + } + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java b/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..a9e6593160 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + protected Object data; + protected BinaryTreeNode left; + protected BinaryTreeNode right; + + public BinaryTreeNode(){} + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right){ + this.data = data; + this.left = left; + this.right = right; + } + +} diff --git a/group07/1058267830/week1/src/com/coding/basic/Iterator.java b/group07/1058267830/week1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..d369bbc50c --- /dev/null +++ b/group07/1058267830/week1/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/group07/1058267830/week1/src/com/coding/basic/LinkedList.java b/group07/1058267830/week1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..df65b13601 --- /dev/null +++ b/group07/1058267830/week1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +public class LinkedList implements List, Iterator{ + private Node head; + private Node tail; + private Node currentNode; + private int size; + + public LinkedList(){ + this.head = new Node(null); + this.tail = head; + this.currentNode = head; + this.size = 0; + } + + @Override + public void add(Object o) { + Node node = new Node(o, null); + tail.setNext(node); + tail = node; + size++; + } + + @Override + public void add(int index, Object o) { + if(index < 0 || index > size+1){ + throw new RuntimeException("插入的位置错误..."); + } + Node pre = head; // 得到待插入位置的前一个节点 + for(int i=0; i= size){ + throw new RuntimeException("index参数错误..."); + } + Node node = head; // 得到待插入位置的前一个节点 + for(int i=0; i<=index; i++){ + node = node.getNext(); + } + return node; + } + + @Override + public Object remove(int index) { + if(index < 0 || index >= size){ + throw new RuntimeException("index参数错误..."); + } + Node pre = head; // 得到待删除位置的前一个节点 + for(int i=0; i + + + + + + + diff --git a/group07/1058267830/week2/.project b/group07/1058267830/week2/.project new file mode 100644 index 0000000000..5b537d7055 --- /dev/null +++ b/group07/1058267830/week2/.project @@ -0,0 +1,17 @@ + + + week2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1058267830/week2/.settings/org.eclipse.jdt.core.prefs b/group07/1058267830/week2/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group07/1058267830/week2/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group07/1058267830/week2/bin/com/coderising/array/ArrayTest.class b/group07/1058267830/week2/bin/com/coderising/array/ArrayTest.class new file mode 100644 index 0000000000..b0d101658f Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/array/ArrayTest.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/array/ArrayUtil.class b/group07/1058267830/week2/bin/com/coderising/array/ArrayUtil.class new file mode 100644 index 0000000000..515999a911 Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/array/ArrayUtil.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/Demo.class b/group07/1058267830/week2/bin/com/coderising/litestruts/Demo.class new file mode 100644 index 0000000000..7d37f20c22 Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/Demo.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/LoginAction.class b/group07/1058267830/week2/bin/com/coderising/litestruts/LoginAction.class new file mode 100644 index 0000000000..794c66e2c7 Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/LoginAction.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/Struts.class b/group07/1058267830/week2/bin/com/coderising/litestruts/Struts.class new file mode 100644 index 0000000000..2e2b15c45f Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/Struts.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/StrutsTest.class b/group07/1058267830/week2/bin/com/coderising/litestruts/StrutsTest.class new file mode 100644 index 0000000000..74b68390bc Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/StrutsTest.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/View.class b/group07/1058267830/week2/bin/com/coderising/litestruts/View.class new file mode 100644 index 0000000000..c28f317aa6 Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/View.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/struts.xml b/group07/1058267830/week2/bin/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group07/1058267830/week2/bin/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/group07/1058267830/week2/lib/dom4j-1.6.1.jar b/group07/1058267830/week2/lib/dom4j-1.6.1.jar new file mode 100644 index 0000000000..c8c4dbb92d Binary files /dev/null and b/group07/1058267830/week2/lib/dom4j-1.6.1.jar differ diff --git a/group07/1058267830/week2/src/com/coderising/array/ArrayTest.java b/group07/1058267830/week2/src/com/coderising/array/ArrayTest.java new file mode 100644 index 0000000000..954ebf54a3 --- /dev/null +++ b/group07/1058267830/week2/src/com/coderising/array/ArrayTest.java @@ -0,0 +1,77 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayTest { + ArrayUtil util = new ArrayUtil(); + + @Test + public void testReverseArray() { + int[] origin = {1,2,3,4,53,52,4,2,423}; + int[] target = util.reverseArray(origin); + int[] expecteds = {423, 2, 4, 52, 53, 4, 3, 2, 1}; + assertArrayEquals(expecteds, target); + } + + @Test + public void testRemoveZero(){ + int[] oldArray = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] target = util.removeZero(oldArray); + int[] expecteds = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5 }; + assertArrayEquals(expecteds, target); + } + + @Test + public void testMerger(){ + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] target = util.merge(a1, a2); + int[] expecteds = {3, 4, 5, 6, 7, 8 }; + assertArrayEquals(expecteds, target); + } + + @Test + public void testGrow(){ + int[] oldArray = {3, 5,7,8}; + int[] target = util.grow(oldArray, 4); + int[] expecteds = {3, 5,7,8, 0, 0, 0,0 }; + assertArrayEquals(expecteds, target); + } + + @Test + public void testFibonacci(){ + int[] target = util.fibonacci(15); + + //int[] expecteds = {1,1}; + int[] expecteds = {1, 1, 2, 3, 5, 8, 13 }; + assertArrayEquals(expecteds, target); + } + + @Test + public void testGetPrimes(){ + int[] target = util.getPrimes(25); + int[] expecteds = {2, 3, 5, 7, 11, 13, 17, 19, 23 }; + assertArrayEquals(expecteds, target); +// for(int i=0; i set = new HashSet(); + for(int i=0; i it = set.iterator(); + while(it.hasNext()){ + target[i++] = it.next(); + } + return target; + } + /** + * 把一个已经存满数据的数组 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(oldArray == null) + return null; + if(size == 0) + return oldArray; + if(size < 0) + throw new RuntimeException("参数错误"); + int length = oldArray.length + size; + int[] target = new int[length]; + System.arraycopy(oldArray, 0, target, 0, oldArray.length); + for(int i=oldArray.length; i< length; i++){ + target[i] = 0; + } + 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 int[] fibonacci(int max){ + if(max < 1) + throw new RuntimeException("参数错误"); + if(max == 1){ + int[] result = {}; + return result; + } + if(max == 2){ + int[] result = {1, 1}; + return result; + } + List list = new ArrayList(); + list.add(1); + list.add(1); + for(int i=2; ; i++){ + int tmp = list.get(i-2) + list.get(i-1); + if(tmp < max){ + list.add(tmp); + }else{ + break; + } + } + int size = list.size(); + int[] result = new int[size]; + for(int i=0; i= max) + break; + } + int[] result = new int[index]; + System.arraycopy(tmp, 0, result, 0, index); + return result; + } + + List list = new ArrayList(); + list.add(2); + list.add(3); + list.add(5); + list.add(7); + list.add(11); + for(int i=13; i list = new ArrayList(); + for(int input = 2; input list = root.elements("action"); + for(Element action_element : list) { + //获取元素action的属性 + if("logout".equals(action_element.attributeValue("name"))){ + System.out.print("action name=" + action_element.attributeValue("name") + " "); + System.out.print("class=" + action_element.attributeValue("class")); + System.out.println(); + + //遍历元素action下所有的result元素 + for(Element rusult_element : (List)action_element.elements("result")) { + //获取元素result的属性 + System.out.print("result name=" + rusult_element.attributeValue("name") + " "); + System.out.println("value=" + rusult_element.getText() ); + } + } + } + } + +} diff --git a/group07/1058267830/week2/src/com/coderising/litestruts/LoginAction.java b/group07/1058267830/week2/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group07/1058267830/week2/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/group07/1058267830/week2/src/com/coderising/litestruts/Struts.java b/group07/1058267830/week2/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..195a63bc8f --- /dev/null +++ b/group07/1058267830/week2/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,64 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +public class Struts { + + @SuppressWarnings("unchecked") + public static View runAction(String actionName, Map parameters) throws Exception{ + + Class viewClass = Class.forName("com.coderising.litestruts.View"); + View view = (View)viewClass.newInstance(); + + SAXReader saxReader = new SAXReader(); + Document doc = saxReader.read("src/com/coderising/litestruts/struts.xml"); + Element root = doc.getRootElement(); + List list = root.elements("action"); + for(Element action_element : list) { + if(actionName.equals(action_element.attributeValue("name"))){ + String className = action_element.attributeValue("class"); + Class clazz = Class.forName(className); + LoginAction la = (LoginAction)clazz.newInstance(); + Class clazz1 = la.getClass(); + + if(parameters.containsKey("name")){ + Method m1 = clazz1.getDeclaredMethod("setName", String.class); + m1.invoke(la, parameters.get("name")); + } + if(parameters.containsKey("password")){ + Method m2 = clazz1.getDeclaredMethod("setPassword", String.class); + m2.invoke(la, parameters.get("password")); + } + + Method m3 = clazz1.getDeclaredMethod("execute"); + String result = (String)m3.invoke(la); + Map map = new HashMap(); + // 这里没有通过反射,后续要改正成反射 + map.put("name", la.getName()); + map.put("password", la.getPassword()); + map.put("message", la.getMessage()); + + view.setParameters(map); + + + for(Element rusult_element : (List)action_element.elements("result")) { + if(result != null && result.equals(rusult_element.attributeValue("name"))){ + view.setJsp(rusult_element.getText()); + break; + } + } + break; + } + } + return view; + } + +} diff --git a/group07/1058267830/week2/src/com/coderising/litestruts/StrutsTest.java b/group07/1058267830/week2/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..cb1be91034 --- /dev/null +++ b/group07/1058267830/week2/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group07/1058267830/week2/src/com/coderising/litestruts/View.java b/group07/1058267830/week2/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group07/1058267830/week2/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/group07/1058267830/week2/src/com/coderising/litestruts/struts.xml b/group07/1058267830/week2/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group07/1058267830/week2/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/group07/1280157271/20170224-01-ArrayList/.classpath b/group07/1280157271/20170224-01-ArrayList/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/1280157271/20170224-01-ArrayList/.gitignore b/group07/1280157271/20170224-01-ArrayList/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/1280157271/20170224-01-ArrayList/.project b/group07/1280157271/20170224-01-ArrayList/.project new file mode 100644 index 0000000000..4c0107dd15 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.project @@ -0,0 +1,17 @@ + + + 20170224-01-ArrayList + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1280157271/20170224-01-ArrayList/.settings/org.eclipse.jdt.core.prefs b/group07/1280157271/20170224-01-ArrayList/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/.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/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java new file mode 100644 index 0000000000..6eb3636be5 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/List.java @@ -0,0 +1,9 @@ +package firstHomework.fan; + +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/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java new file mode 100644 index 0000000000..bd39bfca4e --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myArrayList.java @@ -0,0 +1,60 @@ +package firstHomework.fan; + +public class myArrayList implements List { + + private int size = 0; + private int initLength=10; + private Object[] elementData = new Object[initLength]; + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size++] = o; + } + + public void add(int index, Object o){ + ensureCapacity(size+1); + if(index<0||index>size){ + System.out.println("indexӦ0-size֮䣡"); + }else{ + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + } + + public Object get(int index){ + if(index<0||index>size){ + System.out.println("indexӦ0-size֮䣡"); + }else{ + return elementData[index]; + } + return null; + } + + public Object remove(int index){ + if(index<0||index>size){ + System.out.println("indexӦ0-size֮䣡"); + return null; + } + Object obj = get(index); + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + size--; + return obj; + } + + public int size(){ + return this.size; + } + + + + public void ensureCapacity(int x){ + int oldCapacity = elementData.length; + if(x>oldCapacity){ + Object newEle[] = new Object[oldCapacity+initLength]; + System.arraycopy(elementData, 0, newEle, 0, size); + elementData = newEle; + } + } + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myBinaryTreeNode.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myBinaryTreeNode.java new file mode 100644 index 0000000000..f523215a87 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myBinaryTreeNode.java @@ -0,0 +1,68 @@ +package firstHomework.fan; +import java.util.Comparator; + +public class myBinaryTreeNode { + + private int data; + private myBinaryTreeNode left; + private myBinaryTreeNode right; + + //캯 + public myBinaryTreeNode(){ + } + public myBinaryTreeNode(int value){ + this.data = value; + this.left = null; + this.right = null; + } + public myBinaryTreeNode(int value,myBinaryTreeNode left,myBinaryTreeNode right){ + this.data = value; + this.left = left; + this.right = right; + } + private myBinaryTreeNode root;//ڵ + + //get/set + public Object getData() { + return data; + } + public void setData(int 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 void insert(int o){ + this.root = insert(o,this.root); + + } + public myBinaryTreeNode insert(int value,myBinaryTreeNode t){ + if(t == null){//ڵΪ + return new myBinaryTreeNode(value); + } + //ֵ뵱ǰڵȽϣСڲ뵽ڲ뵽 + if(valuet.data){ + t.right = insert(value,t.right); + } + return t; + } + +} \ No newline at end of file diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java new file mode 100644 index 0000000000..da481e0232 --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myLinkedList.java @@ -0,0 +1,129 @@ +package firstHomework.fan; + + + +public class myLinkedList implements List { + private static class Node{//Ľڵṹ + Object data;// + Node next; //Nodeã൱ָ룬ָһڵ + + Node(Object e, Node next) { + this.data = e; + this.next = next; + } + } + + private Node head,last=null;//ֱָһһڵ + private int size; + + + public void add(Object o){//βӣ൱βڵ + creatLastNode(o); + } + public void add(int index , Object o){//indexǰ + if(index == 0){//˵λΪ0ôͷ + createFirstNode(o); + }else{//ȥҵָλ + Node indexBeforeNode = getNode(index-1);//ﷵصindexǰһڵ + Node newIndex =new Node(o,indexBeforeNode.next) ;//x½ڵ㱣indexBeforeָ + indexBeforeNode.next = newIndex; + size++; + } + } + public Object get(int index){ + return getNode(index).data;//صǽڵеݶ + } + + public Object remove(int index){ + if(index==0){//Ƴͷ + removeFirst(); + }else{//ҵָڵǰһڵ + Node removeNode = getNode(index-1); + removeNode.next = removeNode.next.next;//Ƴindex + size--; + return removeNode.next.data;//Ƴڵݶ + } + return null; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + createFirstNode(o); + } + public void addLast(Object o){ + creatLastNode(o); + } + public Object removeFirst(){ + if(size>0){//бΪգһͷ + Node removeHead = head; + head = head.next; + size--; + return removeHead.data;//ͷݶ + }else{ + System.out.println("Ϊգ"); + } + return null; + } + public Object removeLast(){ + if(size>0){ + Node removeLastBefore = getNode(size-2);//ҵlastڵһڵ + Object returnObj = removeLastBefore.next.data; + removeLastBefore.next = null; + last = removeLastBefore; + size--; + return returnObj; + }else{ + System.out.println("Ϊգ"); + } + return null; + } + + /* + * ͷ + * */ + private void createFirstNode(Object e){ + Node oldHead = head; + Node newHead = new Node(e,oldHead);//ĽڵΪheadڵǰһڵ + head = newHead;//ܿղգheadҪָµͷڵ + if(head == null){//Ϊգheadlastָ½ڵ㣨ΪlastͲҸֵΪȷģ + last = newHead; + }else{//ͷѾ,½ڵΪͷ㣬ԭheadڶlastָlast + newHead.next = head; + } + size++; + } + /* + * β + * */ + private void creatLastNode(Object e){ + Node oldLast = last; + Node newLast = new Node(e,null);//µβڵһڵΪ + last = newLast;//ܿղգlastҪָµβڵ + if(head == null){//Ϊ + head = newLast; + }else{ + oldLast.next = newLast; + } + size++; + } + /* + * Ѱָ + * */ + private Node getNode(int index){ + if(index<0||index>=size){ + System.out.println("indexԽ磡"); + }else{ + Node node=head; + while(index != 0){ + node = node.next; + index--; + } + return node; + } + return null; + } + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java new file mode 100644 index 0000000000..041da7e29b --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myQueue.java @@ -0,0 +1,51 @@ +package firstHomework.fan; + +public class myQueue { + private int iniLength = 10; + private Object[] array = new Object[iniLength]; + private int size = 0; + + public void enQueue(Object o){ + if(size>=array.length){//Ҫ + Object[] newArray = new Object[iniLength+array.length];//arrayԭټ10 + System.arraycopy(array, 0, newArray, 0, size); + array = newArray; + } + array[size++] = o; + } + + public Object deQueue(){//ƳһԪأԪǰλ + Object deQueue = array[0]; + System.arraycopy(array, 1, array, 0, size-1); + size--; + return deQueue; + + } + + public boolean isEmpty(){ + return size==0; + } + + public int size(){ + return this.size; + } + public static void main(String[] args) { + myQueue queue = new myQueue(); + queue.enQueue("A"); + queue.enQueue("B"); + queue.enQueue("C"); + + queue.enQueue("D"); + queue.enQueue("E"); + queue.enQueue("F"); + queue.enQueue("G"); + + while(!queue.isEmpty()){ + System.out.println(queue.deQueue());// + } + + } + + + +} diff --git a/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java new file mode 100644 index 0000000000..623bbe787f --- /dev/null +++ b/group07/1280157271/20170224-01-ArrayList/src/firstHomework/fan/myStack.java @@ -0,0 +1,31 @@ +package firstHomework.fan; + + + +public class myStack { + private myArrayList array = new myArrayList();//myArrayListи̬ + + public void push(Object o){ //ջ + //ȲʱmyArrayListԼ + array.add(o);//¶ + } + + public Object pop(){//ջβϵԪأ Ҫɾ + Object pop = array.get(array.size()-1);//±ҪsizeС1 + array.remove(array.size()-1); + return pop; + } + + public Object peek(){//ֻǵջֵɾ + return array.get(array.size()-1); + } + public boolean isEmpty(){ + return array.size()==0; + } + public int size(){ + return array.size(); + } + + +} + diff --git "a/group07/1280157271/\347\254\254\344\270\200\346\254\241\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" "b/group07/1280157271/\347\254\254\344\270\200\346\254\241\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" new file mode 100644 index 0000000000..1e19f6adda --- /dev/null +++ "b/group07/1280157271/\347\254\254\344\270\200\346\254\241\345\215\232\345\256\242\345\234\260\345\235\200--CPU \345\206\205\345\255\230 \347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273 .txt" @@ -0,0 +1 @@ +http://blog.csdn.net/stromcloud/article/details/56678442 \ No newline at end of file diff --git a/group07/1448276993/JavaStudy/.classpath b/group07/1448276993/JavaStudy/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group07/1448276993/JavaStudy/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/1448276993/JavaStudy/.gitignore b/group07/1448276993/JavaStudy/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/1448276993/JavaStudy/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/1448276993/JavaStudy/.project b/group07/1448276993/JavaStudy/.project new file mode 100644 index 0000000000..cab7f7f249 --- /dev/null +++ b/group07/1448276993/JavaStudy/.project @@ -0,0 +1,17 @@ + + + JavaStudy + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1448276993/JavaStudy/.settings/org.eclipse.jdt.core.prefs b/group07/1448276993/JavaStudy/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group07/1448276993/JavaStudy/.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/group07/1448276993/JavaStudy/src/java1/ArrayList.java b/group07/1448276993/JavaStudy/src/java1/ArrayList.java new file mode 100644 index 0000000000..8623c3ae75 --- /dev/null +++ b/group07/1448276993/JavaStudy/src/java1/ArrayList.java @@ -0,0 +1,57 @@ +package java1; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size++]=o; + + } + public void add(int index, Object o){ + ensureCapacity(size+1); + if(index<0&&index>size){ + System.out.println("Wrong Input"); + }else{ + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index]=o; + size++; + } + + } + + public Object get(int index){ + if(index<0&&index>size){ + System.out.println("Wrong Input"); + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0&&index>size){ + System.out.println("Wrong Input"); + return null; + } + Object a=elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + return a; + } + + public int size(){ + return this.size; + } + + public void ensureCapacity(int size){ + int oldCapacity=elementData.length; + if(size>oldCapacity){ + Object[] newelementData=new Object[size+oldCapacity]; + elementData=newelementData; + } + } + +} + diff --git a/group07/1448276993/JavaStudy/src/java1/LinkedList.java b/group07/1448276993/JavaStudy/src/java1/LinkedList.java new file mode 100644 index 0000000000..45df3db8bc --- /dev/null +++ b/group07/1448276993/JavaStudy/src/java1/LinkedList.java @@ -0,0 +1,129 @@ +package java1; + +public class LinkedList implements List { + + private Node head=null; + private Node last=null; + private int size=0; + + public void add(Object o){ + Node newNode = new Node(o,null); + if(head==null){ + head = newNode; + last = newNode; + }else{ + addLast(o); + } + size++; + } + public void add(int index , Object o){ + if(index<0&&index>size+1){ + System.out.println("Wrong Input!"); + } + Node newNode = new Node(o,null); + if(index==0){ + addFirst(o); + }else if(index==size){ + addLast(o); + }else{ + Node a=head; + for(int i=0;ioldCapacity){ + Object[] newelementData=new Object[size+oldCapacity]; + str=newelementData; + } + } +} diff --git a/group07/1448276993/JavaStudy/src/java1/Stack.java b/group07/1448276993/JavaStudy/src/java1/Stack.java new file mode 100644 index 0000000000..9b785e48d9 --- /dev/null +++ b/group07/1448276993/JavaStudy/src/java1/Stack.java @@ -0,0 +1,34 @@ +package java1; + +public class Stack { + private ArrayList array = new ArrayList(); + + public void push(Object o){ + array.add(o); + } + + public Object pop(){ + if(array.size()>0){ + Object pop = array.get(array.size()-1); + array.remove(array.size()-1); + return pop; + } + return null; + } + + public Object peek(){ + if(array.size()>0){ + return array.get(array.size()-1); + } + return null; + } + public boolean isEmpty(){ + if(array.size()==0){ + return true; + } + return false; + } + public int size(){ + return array.size(); + } +} diff --git a/group07/1519504320/.classpath b/group07/1519504320/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group07/1519504320/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group07/1519504320/.gitignore b/group07/1519504320/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/1519504320/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/1519504320/.idea/.name b/group07/1519504320/.idea/.name new file mode 100644 index 0000000000..5ad528003b --- /dev/null +++ b/group07/1519504320/.idea/.name @@ -0,0 +1 @@ +1519504320Learning \ No newline at end of file diff --git a/group07/1519504320/.idea/compiler.xml b/group07/1519504320/.idea/compiler.xml new file mode 100644 index 0000000000..96cc43efa6 --- /dev/null +++ b/group07/1519504320/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/copyright/profiles_settings.xml b/group07/1519504320/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..e7bedf3377 --- /dev/null +++ b/group07/1519504320/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/libraries/dom4j_1_6_1.xml b/group07/1519504320/.idea/libraries/dom4j_1_6_1.xml new file mode 100644 index 0000000000..933acece89 --- /dev/null +++ b/group07/1519504320/.idea/libraries/dom4j_1_6_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/misc.xml b/group07/1519504320/.idea/misc.xml new file mode 100644 index 0000000000..6a48f33378 --- /dev/null +++ b/group07/1519504320/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/modules.xml b/group07/1519504320/.idea/modules.xml new file mode 100644 index 0000000000..c8fa84b43f --- /dev/null +++ b/group07/1519504320/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/workspace.xml b/group07/1519504320/.idea/workspace.xml new file mode 100644 index 0000000000..d4a1a0f4a8 --- /dev/null +++ b/group07/1519504320/.idea/workspace.xml @@ -0,0 +1,994 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488014668818 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/src/coderising/litestruts/Struts.java + 44 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.project b/group07/1519504320/.project new file mode 100644 index 0000000000..d6fb07be83 --- /dev/null +++ b/group07/1519504320/.project @@ -0,0 +1,17 @@ + + + 1519504320Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1519504320/1519504320Learning.iml b/group07/1519504320/1519504320Learning.iml new file mode 100644 index 0000000000..7a17e1a748 --- /dev/null +++ b/group07/1519504320/1519504320Learning.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/lib/hamcrest-core-1.3.jar b/group07/1519504320/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000000..9d5fe16e3d Binary files /dev/null and b/group07/1519504320/lib/hamcrest-core-1.3.jar differ diff --git a/group07/1519504320/lib/junit-4.12.jar b/group07/1519504320/lib/junit-4.12.jar new file mode 100644 index 0000000000..3a7fc266c3 Binary files /dev/null and b/group07/1519504320/lib/junit-4.12.jar differ diff --git a/group07/1519504320/src/.idea/compiler.xml b/group07/1519504320/src/.idea/compiler.xml new file mode 100644 index 0000000000..96cc43efa6 --- /dev/null +++ b/group07/1519504320/src/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/copyright/profiles_settings.xml b/group07/1519504320/src/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..e7bedf3377 --- /dev/null +++ b/group07/1519504320/src/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/misc.xml b/group07/1519504320/src/.idea/misc.xml new file mode 100644 index 0000000000..0dca27c4c9 --- /dev/null +++ b/group07/1519504320/src/.idea/misc.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/modules.xml b/group07/1519504320/src/.idea/modules.xml new file mode 100644 index 0000000000..f669a0e594 --- /dev/null +++ b/group07/1519504320/src/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/src.iml b/group07/1519504320/src/.idea/src.iml new file mode 100644 index 0000000000..d6ebd48059 --- /dev/null +++ b/group07/1519504320/src/.idea/src.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/vcs.xml b/group07/1519504320/src/.idea/vcs.xml new file mode 100644 index 0000000000..c2365ab11f --- /dev/null +++ b/group07/1519504320/src/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/.idea/workspace.xml b/group07/1519504320/src/.idea/workspace.xml new file mode 100644 index 0000000000..030203141a --- /dev/null +++ b/group07/1519504320/src/.idea/workspace.xml @@ -0,0 +1,759 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Android Lint + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488003825215 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.7 + + + + + + + + src + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/src/coderising/array/ArrayUtil.java b/group07/1519504320/src/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5caa3d54b7 --- /dev/null +++ b/group07/1519504320/src/coderising/array/ArrayUtil.java @@ -0,0 +1,224 @@ +package coderising.array; + +import java.util.Arrays; + +public class ArrayUtil { + + public static void main(String[] args) { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7, 9, 10}; + ArrayUtil aa = new ArrayUtil(); + int[] o = aa.merge(a1, a2); + for (int i = 0; i < o.length; i++) { + System.out.println(o[i]); + } + + } + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] temp = new int[origin.length]; + for (int i = origin.length - 1; i >= 0; i--) { + temp[origin.length - 1 - i] = origin[i]; + } + for (int i = 0; i < origin.length; i++) { + origin[i] = temp[i]; + } + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int[] result; + int zeroNumber = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + zeroNumber++; + } + } + result = new int[oldArray.length - zeroNumber]; + for (int i = 0, j = 0; j < result.length; i++) { + if (oldArray[i] != 0) { + result[j] = oldArray[i]; + j++; + } + } + return result; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int[] result = Arrays.copyOf(array1, array1.length); + outer: + for (int i = 0; i < array2.length; i++) { + for (int j = 0; j < array1.length; j++) { + if (array1[j] == array2[i]) { + continue outer; + } + } + result = Arrays.copyOf(result, result.length + 1); + result[result.length - 1] = array2[i]; + } + + for (int i = 0; i < result.length - 1; i++) { + for (int j = 0; j < result.length - i - 1; j++) { + if (result[j] > result[j+1]) { + int temp = result[j]; + result[j] = result[j+1]; + result[j+1] = temp; + } + } + } + + + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] result = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + result[i] = oldArray[i]; + } + for (int i = oldArray.length; i < result.length; i++) { + result[i] = 0; + } + return result; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int[] initial = new int[]{1, 1}; + if (max == 1) { + return new int[1]; + } else { + while (max > initial[initial.length - 1] + initial[initial.length - 2]) { + initial = addOne(initial); + } + } + return initial; + } + + public int[] addOne(int[] current) { + int[] result = Arrays.copyOf(current, current.length + 1); + result[current.length] = result[current.length - 1] + result[current.length - 2]; + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] result = new int[1]; + outer: + for (int i = 2; i < max; i++) { + for (int j = 2; j < i; j++) { + if (i % j == 0) { + continue outer; + } + } + if (result.length == 1 && result[0] == 0) { + result[0] = i; + } else { + result = Arrays.copyOf(result, result.length + 1); + result[result.length - 1] = i; + } + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] result = new int[1]; + for (int i = 1; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + if (result.length == 1 && result[0] == 0) { + result[0] = i; + } else { + result = Arrays.copyOf(result, result.length + 1); + result[result.length - 1] = i; + } + } + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + sb.append(array[i]); + } else { + sb.append(array[i]); + sb.append(seperator); + } + } + return sb.toString(); + } + + +} \ No newline at end of file diff --git a/group07/1519504320/src/coderising/litestruts/LoginAction.java b/group07/1519504320/src/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..c528df798a --- /dev/null +++ b/group07/1519504320/src/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package 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/group07/1519504320/src/coderising/litestruts/Struts.java b/group07/1519504320/src/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..22b7a291b4 --- /dev/null +++ b/group07/1519504320/src/coderising/litestruts/Struts.java @@ -0,0 +1,106 @@ +package 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.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class Struts { + public static void main(String[] args) { + runAction("login", null); + } + + public static View runAction(String actionName, Map parameters) { + String className = " "; + View view = new View(); + Map map = new HashMap<>(); + String result = " "; + + SAXReader reader = new SAXReader(); + Document document = null; + try { + document = reader.read(new File("src\\coderising\\litestruts\\struts.xml")); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element root = document.getRootElement(); + List eles = root.elements(); + for (Element e : eles) { + if (e.attribute("name").getValue().equals(actionName)) { + className = e.attribute("class").getValue(); + } + } + + try { + Class clz = Class.forName(className); + Object obj = clz.newInstance(); + Method method = clz.getDeclaredMethod("setName",String.class); + method.invoke(obj, parameters.get("name")); + method = clz.getDeclaredMethod("setPassword",String.class); + method.invoke(obj, parameters.get("password")); + method = clz.getDeclaredMethod("execute"); + result = (String) method.invoke(obj); + method = clz.getDeclaredMethod("getMessage"); + map.put("message", (String) method.invoke(obj)); + method = clz.getDeclaredMethod("getName"); + map.put("name", (String) method.invoke(obj)); + method = clz.getDeclaredMethod("getPassword"); + map.put("password", (String) method.invoke(obj)); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + view.setParameters(map); + for (Element e : eles) { + if (e.attribute("name").getValue().equals(actionName)) { + List ele_subs = e.elements(); + for (Element e_sub : ele_subs) { + if (e_sub.attribute("name").getValue().equals(result)) { + view.setJsp(e_sub.getTextTrim()); + } + } + } + } + + + + /* + + 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/group07/1519504320/src/coderising/litestruts/StrutsTest.java b/group07/1519504320/src/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..257f3d5a89 --- /dev/null +++ b/group07/1519504320/src/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package 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/group07/1519504320/src/coderising/litestruts/View.java b/group07/1519504320/src/coderising/litestruts/View.java new file mode 100644 index 0000000000..22fdf877d8 --- /dev/null +++ b/group07/1519504320/src/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package 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/group07/1519504320/src/coderising/litestruts/dom4j-1.6.1.jar b/group07/1519504320/src/coderising/litestruts/dom4j-1.6.1.jar new file mode 100644 index 0000000000..c8c4dbb92d Binary files /dev/null and b/group07/1519504320/src/coderising/litestruts/dom4j-1.6.1.jar differ diff --git a/group07/1519504320/src/coderising/litestruts/struts.xml b/group07/1519504320/src/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..57ad66abd0 --- /dev/null +++ b/group07/1519504320/src/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/group07/1519504320/src/com/coding/basic/ArrayList.java b/group07/1519504320/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..44935deaa3 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/ArrayList.java @@ -0,0 +1,96 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (elementData.length == 101 && elementData[100] == null) { + for (int i = 0; i < elementData.length; i++) { + if (elementData[i] == null) { + elementData[i] = o; + } + } + } else { + Object[] elementData2 = new Object[elementData.length + 1]; + for (int i = 0; i < elementData.length; i++) { + elementData2[i] = elementData[i]; + } + elementData2[elementData2.length - 1] = o; + elementData = elementData2; + } + + } + + public void add(int index, Object o) { + if (index < 0 || index > elementData.length - 1) { + return; + } + if (index <= elementData.length - 1) { + Object[] elementData2 = new Object[elementData.length + 1]; + elementData2[index] = o; + for (int i = 0; i < elementData2.length; i++) { + if (i < index) { + elementData2[i] = elementData[i]; + } + if (i > index) { + elementData2[i + 1] = elementData[i]; + } + } + elementData = elementData2; + } + + } + + public Object get(int index) { + if (elementData.length - 1 < index || index < 0) { + return null; + } else { + return elementData[index]; + } + } + + public Object remove(int index) { + if (index > elementData.length - 1 || index < 0) { + return null; + } else { + Object result = elementData[index]; + for (int i = index; i < elementData.length - 1; i++) { + elementData[index] = elementData[index + 1]; + } + elementData[elementData.length - 1] = null; + return result; + } + } + + public int size() { + return elementData.length; + } + + public Iterator iterator() { + + return new Iterator() { + int cursor = -1; + + + @Override + public boolean hasNext() { + if (cursor < elementData.length - 1) { + return true; + } + return false; + } + + @Override + public Object next() { + + cursor++; + return elementData[cursor]; + } + }; + } + + +} diff --git a/group07/1519504320/src/com/coding/basic/BinaryTreeNode.java b/group07/1519504320/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group07/1519504320/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/group07/1519504320/src/com/coding/basic/Iterator.java b/group07/1519504320/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group07/1519504320/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/group07/1519504320/src/com/coding/basic/LinkedList.java b/group07/1519504320/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..80288820a1 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/LinkedList.java @@ -0,0 +1,157 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o) { + Node n = head.next; + while (n.next != null) { + n = n.next; + } + Node n1 = new Node(); + n1.data = o; + n.next = n1; + } + + public void add(int index, Object o) { + if (index == 0) { + Object o1 = head.data; + head.data = o; + Node next = new Node(); + next.data = o1; + head.next = next; + } + if (index < 0) { + return; + } + if (size() - 1 < index) { + return; + } else { + int flag = 0; + Node pre = head; + while (flag != index - 1) { + pre = pre.next; + flag++; + } + Node current = new Node(); + current.data = o; + Node next = pre.next; + pre.next = current; + current.next = next; + + } + + + } + + public Object get(int index) { + if (index < 0) { + return null; + } + if (size() - 1 < index) { + return null; + } else { + int flag = 0; + Node n = head; + while (flag != index) { + n = n.next; + flag++; + } + return n.data; + } + } + + public Object remove(int index) { + if (index < 0) { + return null; + } + if (size() - 1 < index) { + return null; + } else { + int flag = 0; + Node pre = head; + while (flag != index - 1) { + pre = pre.next; + flag++; + } + Node current = pre.next; + Object c = current.data; + pre.next = current.next; + return c; + } + } + + public int size() { + int size = -1; + if (head.data != null) { + size = 0; + Node n = head.next; + while (n.next != null) { + size++; + n = n.next; + } + } + return size; + } + + public void addFirst(Object o) { + if (head.data != null) { + Node n = new Node(); + n.data = o; + n.next = head; + } else { + head.data = o; + } + + } + + public void addLast(Object o) { + if (head.data == null) { + head.data = o; + } else { + Node n = head.next; + while (n.next != null) { + n = n.next; + } + n.next.data = o; + } + } + + public Object removeFirst() { + if (head.data == null) { + return null; + } else { + Object o = head.data; + head = head.next; + return o; + } + } + + public Object removeLast() { + if (head.data == null) { + return null; + } else { + Node n = head.next; + while (n.next != null) { + n = n.next; + } + Object o = n.data; + n.data = null; + return o; + } + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + + } + + +} diff --git a/group07/1519504320/src/com/coding/basic/List.java b/group07/1519504320/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group07/1519504320/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/group07/1519504320/src/com/coding/basic/Queue.java b/group07/1519504320/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..db969b0139 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + LinkedList a = new LinkedList(); + public void enQueue(Object o){ + a.add(o); + } + + public Object deQueue(){ + return a.removeFirst(); + } + + public boolean isEmpty(){ + if(a.size()==0){ + return true; + } + return false; + } + + public int size(){ + return a.size(); + } +} diff --git a/group07/1519504320/src/com/coding/basic/Stack.java b/group07/1519504320/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..6198465bf3 --- /dev/null +++ b/group07/1519504320/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object result = elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return result; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + if (elementData.size()==0){ + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group07/178007127/001DataStructure/.classpath b/group07/178007127/001DataStructure/.classpath new file mode 100644 index 0000000000..84b630a879 --- /dev/null +++ b/group07/178007127/001DataStructure/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group07/178007127/001DataStructure/.gitignore b/group07/178007127/001DataStructure/.gitignore new file mode 100644 index 0000000000..4a95481e61 --- /dev/null +++ b/group07/178007127/001DataStructure/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/build/ diff --git a/group07/178007127/001DataStructure/.project b/group07/178007127/001DataStructure/.project new file mode 100644 index 0000000000..430da90497 --- /dev/null +++ b/group07/178007127/001DataStructure/.project @@ -0,0 +1,18 @@ + + + 001DataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.springsource.ide.eclipse.gradle.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs new file mode 100644 index 0000000000..67ed2b404e --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.import.prefs @@ -0,0 +1,9 @@ +#org.springsource.ide.eclipse.gradle.core.preferences.GradleImportPreferences +#Sat Feb 25 12:36:04 CST 2017 +addResourceFilters=true +afterTasks=afterEclipseImport; +beforeTasks=cleanEclipse;eclipse; +enableAfterTasks=true +enableBeforeTasks=true +enableDependendencyManagement=true +projects=; diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs new file mode 100644 index 0000000000..f846d71532 --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs @@ -0,0 +1,5 @@ +#org.springsource.ide.eclipse.gradle.core.preferences.GradleProjectPreferences +#Thu Feb 23 15:58:56 CST 2017 +build.family.org.gradle.tooling.model.eclipse.HierarchicalEclipseProject=; +org.springsource.ide.eclipse.gradle.linkedresources= +org.springsource.ide.eclipse.gradle.rootprojectloc= diff --git a/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs new file mode 100644 index 0000000000..13198da612 --- /dev/null +++ b/group07/178007127/001DataStructure/.settings/gradle/org.springsource.ide.eclipse.gradle.refresh.prefs @@ -0,0 +1,8 @@ +#org.springsource.ide.eclipse.gradle.core.actions.GradleRefreshPreferences +#Sat Feb 25 12:36:04 CST 2017 +addResourceFilters=true +afterTasks=afterEclipseImport; +beforeTasks=cleanEclipse;eclipse; +enableAfterTasks=true +enableBeforeTasks=true +useHierarchicalNames=false diff --git a/group07/178007127/001DataStructure/.settings/org.eclipse.jdt.core.prefs b/group07/178007127/001DataStructure/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group07/178007127/001DataStructure/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group07/178007127/001DataStructure/README.md b/group07/178007127/001DataStructure/README.md new file mode 100644 index 0000000000..f0a705d37a --- /dev/null +++ b/group07/178007127/001DataStructure/README.md @@ -0,0 +1,2 @@ +# 001DataStructure +001DataStructure diff --git a/group07/178007127/001DataStructure/build.gradle b/group07/178007127/001DataStructure/build.gradle new file mode 100644 index 0000000000..8450dd647d --- /dev/null +++ b/group07/178007127/001DataStructure/build.gradle @@ -0,0 +1,12 @@ +apply plugin:'java' + +repositories{ + mavenCentral() +} + +dependencies{ + compile group: 'junit', name: 'junit', version: '4.12' + + compile group: 'dom4j', name: 'dom4j', version: '1.6.1' + compile group: 'jaxen', name: 'jaxen', version: '1.1.6' +} \ No newline at end of file diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/array/ArrayUtil.java b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/array/ArrayUtil.java new file mode 100644 index 0000000000..b98e1c1c37 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/array/ArrayUtil.java @@ -0,0 +1,225 @@ +package com.easy.codersing.array; + +import org.junit.Test; + +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 int[] reverseArray(int[] origin){ + int[] dest =new int[origin.length]; + for(int i=0;iarr[j]){ + int temp=arr[i]; + arr[i]=arr[j]; + arr[j]=temp; + } + } + } + return arr; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, newArray, 0, newArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + StringBuilder sb=new StringBuilder(); + + if(max==1){ + return new int[]{}; + }else{ + for(int i=1 ;i<2*max;i++){ + int num = getFibo(i); + if(num parameters) throws Exception { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + SAXReader reader = new SAXReader(); + File file =new File("src/main/java/com/easy/codersing/litestruts/struts.xml"); + Document doc=reader.read(file); + Element el_root=doc.getRootElement(); + Iterator it= el_root.elementIterator(); + while(it.hasNext()){ + Object obj=it.next(); + Element el_action = (Element)obj; + //System.out.println(el_action.attributeValue("name")); + String el_action_name=el_action.attributeValue("name"); + + + if(actionName.equals(el_action_name)){ + String el_action_class=el_action.attributeValue("class"); + Class clazz = Class.forName(el_action_class); + Object actionObj = clazz.newInstance(); + + for(String map_key:parameters.keySet()){ + String set_method_name = "set"+AppUtils.fistLetterToUpper(map_key); + Method set_method =clazz.getMethod(set_method_name, String.class); + set_method.invoke(actionObj,parameters.get(map_key)); + } + + Method execute_method = clazz.getMethod("execute"); + String execute_result = (String)execute_method.invoke(actionObj); + + Method get_message_method = clazz.getMethod("getMessage"); + String get_message_result=(String)get_message_method.invoke(actionObj); + + Map map=new HashMap(); + map.put("message", get_message_result); + + View view =new View(); + view.setParameters(map); + + Iterator it_result = el_action.elementIterator(); + while(it_result.hasNext()){ + Element el_result =(Element)it_result.next(); + if(el_result.attributeValue("name").equals(execute_result)){ + view.setJsp(el_result.getText()); + } + } + + return view; + + + } + } + + return null; + } + + + + +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/StrutsTest.java b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/StrutsTest.java new file mode 100644 index 0000000000..2d167cce90 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.easy.codersing.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/TestApp.java b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/TestApp.java new file mode 100644 index 0000000000..df78950609 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/TestApp.java @@ -0,0 +1,85 @@ +package com.easy.codersing.litestruts; + + +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class TestApp { + public static void test1() throws Exception { + SAXReader reader = new SAXReader(); + File file =new File("src/main/java/com/easy/codersing/litestruts/struts.xml"); + //System.out.println(file.exists()); + Document doc=reader.read(file); + List actions = doc.selectNodes("//*[@name]"); + for(Iterator it=actions.iterator();it.hasNext();){ + Element action = (Element)it.next(); + List list=action.attributes(); + for(Iterator it2=list.iterator();it2.hasNext();){ + Attribute a=(Attribute)it2.next(); + System.out.println(a.getName()+"="+a.getValue()); + } + } + + } + + public static void main(String[] args) throws Exception { + /* Map map=new HashMap<>(); + map.put("name", "test"); + map.put("password", "1234"); + + for (String s : map.keySet()) { + System.out.println(s); + System.out.println(map.get(s)); + }*/ + + + + SAXReader reader = new SAXReader(); + File file =new File("src/main/java/com/easy/codersing/litestruts/struts.xml"); + Document doc=reader.read(file); + Element el_root=doc.getRootElement(); + Iterator it= el_root.elementIterator(); + while(it.hasNext()){ + Object obj=it.next(); + Element el_action = (Element)obj; + //System.out.println(el_action.attributeValue("name")); + //System.out.println(el_action.attributeValue("class")); + + Iterator it_row=el_action.elementIterator(); + if(el_action.attributeValue("name").equals("login")){ + while(it_row.hasNext()){ + Element el_result=(Element)it_row.next(); + System.out.println(el_result.attributeValue("name")); + System.out.println(el_result.getText()); + + } + } + } + + + + } + + public static void test3(String[] args) throws Exception { + Class clazz = Class.forName("com.easy.codersing.litestruts.LoginAction"); + Object obj=clazz.newInstance(); + + Method method1 =clazz.getMethod("setName",String.class); + method1.invoke(obj, "张三"); + + Method method2 = clazz.getMethod("getName",null); + Object result = method2.invoke(obj); + System.out.println(result); + + } +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/View.java b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/View.java new file mode 100644 index 0000000000..8c44204849 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/View.java @@ -0,0 +1,23 @@ +package com.easy.codersing.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/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/struts.xml b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/struts.xml new file mode 100644 index 0000000000..2d1ab0da83 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/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/group07/178007127/001DataStructure/src/main/java/com/easy/core/AppUtils.java b/group07/178007127/001DataStructure/src/main/java/com/easy/core/AppUtils.java new file mode 100644 index 0000000000..e0b216553e --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/core/AppUtils.java @@ -0,0 +1,13 @@ +package com.easy.core; + +public class AppUtils { + public static String fistLetterToUpper(String words){ + String letter = words.substring(0,1).toUpperCase(); + return letter+words.substring(1,words.length()); + } + + public static void main(String[] args) { + String s="abc"; + System.out.println(fistLetterToUpper(s)); + } +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/junit/app/TestApp.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/junit/app/TestApp.java new file mode 100644 index 0000000000..1ab6bdd881 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/junit/app/TestApp.java @@ -0,0 +1,19 @@ +package com.easy.util.junit.app; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import com.easy.util.myarraylist.TestArrayList; +import com.easy.util.mylinkedlist.TestLinkedList; +import com.easy.util.myqueue.TestQueue; +import com.easy.util.mystack.TestStack; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + TestArrayList.class, + TestLinkedList.class, + TestQueue.class, + TestStack.class +}) +public class TestApp { + +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java new file mode 100644 index 0000000000..1bab864a11 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myarraylist/ArrayList.java @@ -0,0 +1,175 @@ +package com.easy.util.myarraylist; + +import com.easy.util.myiterator.Iterator; + +public class ArrayList{ + + private static final Object[] EMPTY_ELEMENTDATA = {}; + + private Object[] elementData; + + private int size; + + + // region 构造函数 + public ArrayList() { + this.elementData = EMPTY_ELEMENTDATA; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) { + throw new IllegalArgumentException("Illegal Capacity:" + initialCapacity); + } + this.elementData = new Object[initialCapacity]; + } + // endregion + + // region add方法 + public boolean add(Object o) { + if (elementData.length <= size) { + grow(1); + } + elementData[size++] = o; + return true; + } + + public void add(int index, Object o) { + rangeCheckForAdd(index); + grow(1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + // endregion + + // region get方法 + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + // endregion + + // region remove方法 + public Object remove(int index) { + rangeCheck(index); + + Object oldValue = elementData[index]; + /* + * int numMoved=size-index-1; if(numMoved>0){ + * System.arraycopy(elementData, index+1, elementData, index, numMoved); + * } elementData[--size]=null; + */ + fastRemove(index); + return oldValue; + } + + public boolean remove(Object o) { + if (o == null) { + for (int index = 0; index < size; index++) { + if (elementData[index] == null) { + fastRemove(index); + return true; + } + } + } else { + for (int index = 0; index < size; index++) { + if (elementData[index].equals(o)) { + fastRemove(index); + return true; + } + } + } + return false; + } + // endregion + + // region size方法 + public int size() { + return size; + } + // endregion + + // region toString方法 + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < size(); i++) { + sb.append(elementData[i] + ","); + } + /* + * for (Object object : elementData) { sb.append(object + ","); } + */ + String temp = sb.toString(); + temp = temp.substring(0, temp.length() - 1); + return "[" + temp + "]"; + } + // endregion + + // region 私有方法 + private void grow(int minCapacity) { + Object[] dest = new Object[elementData.length + minCapacity]; + System.arraycopy(elementData, 0, dest, 0, elementData.length); + elementData = dest; + } + + private void rangeCheck(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + private String outOfBoundsMsg(int index) { + return "Index:" + index + ",Size:" + size; + } + + private void rangeCheckForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + private void fastRemove(int index) { + int numMoved = size - index - 1; + if (numMoved > 0) { + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[--size] = null; + } + + // endregion + + // region 下一版本迭代的功能 + private static final int DEFAULT_CAPACITY = 10; + + private void ensureCapacityInternal(int minCapacity) { + if (elementData == EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + ensureCapacityInternal(minCapacity); + } + + private void ensureExplicitCapacity(int minCapacity) { + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + } + //endregion + + int currentIndex ; + public Iterator iterator(){ + currentIndex=-1; + return new Iterator() { + + @Override + public Object next() { + return elementData[++currentIndex]; + } + + @Override + public boolean hasNext() { + return currentIndex0&&index<=size; + } + + private String outOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + //endregion +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java new file mode 100644 index 0000000000..0a57f448b6 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/util/myqueue/Queue.java @@ -0,0 +1,37 @@ +package com.easy.util.myqueue; + +import com.easy.util.mylinkedlist.LinkedList; + +public class Queue { + + LinkedList linkedList; + public Queue(){ + linkedList=new LinkedList(); + } + + public void enQueue(Object o){ + linkedList.addLast(o); + } + + public Object deQueue(){ + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return size()==0; + } + + public int size(){ + return linkedList.size(); + } + + @Override + public String toString() { + StringBuilder sb=new StringBuilder(); + for(int i=0;i implements List { + + private Object[] elements; + + // 列表长度,默认10个元素 + private int size = 10; + + // 最后一个元素的位置 + private int position = -1; + + public ArrayList() { + elements = new Object[size]; + } + + public ArrayList(final int capacity) { + if (capacity <= 0) + throw new RuntimeException("列表长度不可小于等于0!"); + elements = new Object[capacity]; + } + + /** + * 添加元素 + * + * @param e 要添加的元素 + * @return + */ + @Override + public boolean add(E e) { + + if (++position > size - 1) { + grow(); + } + elements[position] = e; + + return true; + } + + /** + * 删除指定索引的元素 + * + * @param index 索引下标 + * @return + */ + @Override + @SuppressWarnings("unchecked") + public E remove(int index) { + + if (index < 0 || index > position || this.isEmpty()) + throw new IndexOutOfBoundsException("要删除的索引不正确!"); + + E returnElement = (E) elements[index]; + elements[index] = null; + System.arraycopy(elements, index + 1, elements, index, position + 1 - index); + position--; + return returnElement; + } + + /** + * 删除指定的元素 + * + * @param e + * @return + */ + @Override + public boolean remove(E e) { + + if (!this.contains(e)) return false; + int removeIndex = 0; + for(int i = 0; i < this.position; i++) { + if(elements[i].equals(e)) { + removeIndex = i; + break; + } + } + remove(removeIndex); + return true; + } + + /** + * 返回列表长度 + * + * @return + */ + @Override + public int size() { + return position + 1; + } + + /** + * 判断列表是否为空 + * + * @return + */ + @Override + public boolean isEmpty() { + return position == -1; + } + + /** + * 获取指定索引的元素 + * + * @param index + * @return + */ + @Override + @SuppressWarnings("unchecked") + public E get(int index) { + if (index > position) return null; + return (E)elements[index]; + } + + /** + * 重置某个索引的元素 + * + * @param index + * @param o + * @return + */ + @Override + public boolean set(int index, Object o) { + if (index > position) return false; + elements[index] = null; + elements[index] = o; + return true; + } + + /** + * 判断是否包含某个元素 + * + * @param o + * @return + */ + @Override + public boolean contains(Object o) { + + if(this.isEmpty()) return false; + for(int i = 0; i <= position; i++) { + if (elements[i].equals(o)) + return true; + } + return false; + } + + /** + * 清空列表 + */ + @Override + public void clear() { + for(int i = 0; i <= this.size(); i++) { + elements[i] = null; + } + position = -1; + } + + /** + * 取得迭代器 + * + * @return + */ + @Override + public Iterator iterator() { + return new Itr(); + } + + /** + * 数组增长 + */ + private void grow() { + + Object[] newElements = new Object[size << 1]; + System.arraycopy(elements, 0, newElements, 0, this.size); + size = size << 1; + elements = null; + elements = newElements; + } + + private class Itr implements Iterator { + + private int itrIndex = 0; + + @Override + public boolean hasNext() { + return get(itrIndex) != null; + } + + @Override + @SuppressWarnings("unchecked") + public E next() { + return (E) elements[itrIndex++]; + } + + @Override + public void remove() { + ArrayList.this.remove(itrIndex); + } + } + + @Override + public String toString() { + + if(this.isEmpty()) { + return "[]"; + } + StringBuilder strList = new StringBuilder("["); + for(int i = 0; i < this.size(); i++) { + strList.append(elements[i].toString()).append(","); + } + strList.deleteCharAt(strList.length() - 1); + strList.append("]"); + return strList.toString(); + } + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Hello.java b/group07/20409287/src/main/java/xdx/homework/first/Hello.java new file mode 100644 index 0000000000..1abb23045e --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Hello.java @@ -0,0 +1,15 @@ +package xdx.homework.first; + +import java.util.List; + +/** + * @Description: Hello World! + * @author: xdx + * @date: 2017年2月25日 上午11:37:58 + */ +public class Hello { + + public static void main(String[] args) { + System.out.println("Hello WOrl"); + } +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Iterator.java b/group07/20409287/src/main/java/xdx/homework/first/Iterator.java new file mode 100644 index 0000000000..b90db66d5d --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Iterator.java @@ -0,0 +1,11 @@ +package xdx.homework.first; + + +public interface Iterator { + + boolean hasNext(); + + E next(); + + void remove(); +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java b/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java new file mode 100644 index 0000000000..1c31589204 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/LinkedList.java @@ -0,0 +1,246 @@ +package xdx.homework.first; + +/** + * @Description: 链式列表 + */ +public class LinkedList implements List { + + private class Node { + + private E data; // 数据域 + + private Node next; // 指针域 + + public Node() { + } + + private Node(E data) { + this.data = data; + this.next = null; + } + + } + + // 链表大小 + private int size = 0; + + private Node head; + + private Node tail; + + /** + * 添加元素 + * + * @param data + * @return + */ + @Override + public boolean add(E data) { + + if(this.head != null) { + Node newNode = new Node(data); + this.tail.next = newNode; + this.tail = newNode; + } else { + this.head = new Node(data); + this.tail = this.head; + } + size++; + return true; + } + + /** + * 删除指定索引的元素 + * + * @param index@return + */ + @Override + public E remove(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引不正确!"); + } + if (isEmpty()) { + throw new RuntimeException("链表为空!"); + } + Node currentNode = this.head; + Node preNode = currentNode; + for (int i = 0; i < index; i++) { + preNode = currentNode; + currentNode = currentNode.next; + } + preNode.next = currentNode.next; + size--; + return currentNode.data; + } + + /** + * 删除指定的元素 + * + * @param e + * @return + */ + @Override + public boolean remove(E e) { + + if (!this.contains(e)) return false; + + if (this.head.data.equals(e)) { + this.head = this.head.next; + size--; + return true; + } + Node currentNode = this.head; + Node preNode = currentNode; + boolean isFind = false; + for (int i = 0; i < size; i++) { + if(currentNode.data.equals(e)) { + isFind = true; + break; + } + preNode = currentNode; + currentNode = currentNode.next; + } + if (!isFind) return false; + preNode.next = currentNode.next; + size--; + return true; + } + + /** + * 返回列表长度 + * + * @return + */ + @Override + public int size() { + return size; + } + + /** + * 判断列表是否为空 + * + * @return + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * 获取指定索引的元素 + * + * @param index + * @return + */ + @Override + public E get(int index) { + + if (index < 0 || index > size || isEmpty()) { + throw new IndexOutOfBoundsException("索引不正确!"); + } + Node currentNode = this.head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.data; + } + + /** + * 重置某个索引的元素 + * + * @param index + * @param e + * @return + */ + @Override + public boolean set(int index, E e) { + + if (index < 0 || index > size || isEmpty()) { + throw new IndexOutOfBoundsException("索引不正确!"); + } + Node currentNode = this.head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + currentNode.data = e; + return false; + } + + /** + * 判断是否包含某个元素 + * + * @param e + * @return + */ + @Override + public boolean contains(E e) { + + if (isEmpty()) return false; + Node currentNode = this.head; + boolean isFind = false; + for (int i = 0; i < size(); i++) { + if(currentNode.data.equals(e)) { + isFind = true; + } + currentNode = currentNode.next; + } + return isFind; + } + + /** + * 清空列表 + */ + @Override + public void clear() { + this.head = this.tail = null; + size = 0; + } + + @Override + public String toString() { + + if (isEmpty()) return "[]"; + StringBuilder strLinkedList = new StringBuilder("["); + Node currentNode = this.head; + while (currentNode.next != null) { + strLinkedList.append(currentNode.data.toString()).append(","); + currentNode = currentNode.next; + } + strLinkedList.append(currentNode.data.toString()).append("]"); + return strLinkedList.toString(); + } + + /** + * 取得迭代器 + * + * @return + */ + @Override + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + + Node curNode = LinkedList.this.head; + + @Override + public boolean hasNext() { + return curNode != null; + } + + @Override + public E next() { + Node thisNode = curNode; + curNode = curNode.next; + return thisNode.data; + } + + @Override + public void remove() { + LinkedList.this.remove(curNode.data); + curNode = curNode.next; + } + } +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/List.java b/group07/20409287/src/main/java/xdx/homework/first/List.java new file mode 100644 index 0000000000..7c6f1c4e52 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/List.java @@ -0,0 +1,86 @@ +package xdx.homework.first; + +/** + * @param + * @Description: 定义一组操作有序列表的接口 + * @author: xdx + * @date: 2017年2月21日 下午8:53:52 + */ +public interface List { + + /** + * 添加元素 + * + * @param e + * @return + */ + boolean add(E e); + + /** + * 删除指定索引的元素 + * + * @param int 索引下标 + * @return + */ + E remove(int index); + + /** + * 删除指定的元素 + * + * @param e + * @return + */ + boolean remove(E e); + + /** + * 返回列表长度 + * + * @return + */ + int size(); + + /** + * 判断列表是否为空 + * + * @return + */ + boolean isEmpty(); + + /** + * 获取指定索引的元素 + * + * @param index + * @return + */ + E get(int index); + + /** + * 重置某个索引的元素 + * + * @param index + * @param e + * @return + */ + boolean set(int index, E e); + + /** + * 判断是否包含某个元素 + * + * @param e + * @return + */ + boolean contains(E e); + + /** + * 清空列表 + */ + void clear(); + + /** + * 取得迭代器 + * + * @return + */ + Iterator iterator(); + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Queue.java b/group07/20409287/src/main/java/xdx/homework/first/Queue.java new file mode 100644 index 0000000000..5d98cff312 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Queue.java @@ -0,0 +1,97 @@ +package xdx.homework.first; + +/** + * @Description: 链式队列 + */ +public class Queue { + + private class Node { + + private E data; // 数据域 + + private Node next; // 指针域 + + public Node() { + } + + private Node(E data) { + this.data = data; + this.next = null; + } + + } + + private Node front; // 队头 + + private Node rear; // 队尾 + + private int length; // 队长 + + public void enQueue(E data) { + + if(this.front == null) { + this.front = new Node(data); + this.rear = this.front; + } else { + Node newNode = new Node(data); + this.rear.next = newNode; + this.rear = newNode; + } + length++; + } + + public E deQueue() { + + if (isEmpty()) { + return null; + } + Node oldFront = this.front; + this.front = this.front.next; + length--; + return oldFront.data; + } + + public int length() { + return length; + } + + public boolean isEmpty() { + return this.front == this.rear; + } + + public void clear() { + this.length = 0; + this.front = null; + this.rear = null; + } + + public E getFront() { + + if (this.isEmpty()) { + return null; + } + return this.front.data; + } + + public E getRear() { + + if(this.isEmpty()) return null; + return this.rear.data; + } + + public String toString() { + + if (this.length == 0) return "[]"; + + Node node = this.front; + StringBuilder queueToString = new StringBuilder("["); + while (node.next != null) { + queueToString.append(node.data.toString()).append(","); + node = node.next; + } + queueToString.append(node.data.toString()).append("]"); + return queueToString.toString(); + } + + +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/Stack.java b/group07/20409287/src/main/java/xdx/homework/first/Stack.java new file mode 100644 index 0000000000..d898ffeb6e --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/first/Stack.java @@ -0,0 +1,81 @@ +package xdx.homework.first; + +/** + * @Description: + * @author: {User} + * @date: {Date} + */ +public class Stack { + + private class Node { + + private E data; // 数据域 + + private Node next; // 指针域 + + public Node() { + } + + private Node(E data) { + this.data = data; + this.next = null; + } + } + + private int size; + + private Node top; + + public void push(E e) { + if(!isEmpty()) { + Node newNode = new Node(e); + newNode.next = top; + top = newNode; + } else { + top = new Node(e); + } + size++; + } + + public E pop() { + if(isEmpty()) throw new RuntimeException("空栈!"); + Node popNode = top; + top = top.next; + return popNode.data; + } + + public boolean isEmpty() { + return size == 0; + } + + public E peek() { + if(isEmpty()) throw new RuntimeException("空栈!"); + return top.data; + } + + public int size() { + return size; + } + + public void clear() { + top = null; + size = 0; + } + + @Override + public String toString() { + + if(isEmpty()) return "[]"; + StringBuilder stackToString = new StringBuilder("["); + Node itr = this.top; + while(itr != null) { + stackToString.append(itr.data.toString()).append(","); + itr = itr.next; + } + stackToString.deleteCharAt(stackToString.length() - 1).append("]"); + + return stackToString.toString(); + } + + +} diff --git a/group07/20409287/src/main/java/xdx/homework/second/array/ArrayUtil.java b/group07/20409287/src/main/java/xdx/homework/second/array/ArrayUtil.java new file mode 100644 index 0000000000..2fa1237dbb --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/array/ArrayUtil.java @@ -0,0 +1,221 @@ +package xdx.homework.second.array; + +import xdx.homework.first.ArrayList; +import xdx.homework.first.List; +import xdx.homework.first.Queue; +import xdx.homework.first.Stack; + +import java.util.Arrays; + +import static java.lang.StrictMath.sqrt; + +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 end = origin.length - 1; + for (int begin = 0; begin < end; begin++, end--) { + // 交换首尾 + origin[begin] = origin[begin] ^ origin[end]; + origin[end] = origin[begin] ^ origin[end]; + origin[begin] = origin[begin] ^ origin[end]; + } + } + + + /** + * 现在有如下的一个数组: 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[] newArray = new int[oldArray.length]; + int newArrayIndex = 0; + int i = 0; + for (; i < oldArray.length; i++) { + if (oldArray[i] == 0) continue; + newArray[newArrayIndex++] = oldArray[i]; + } + return Arrays.copyOfRange(newArray, 0, newArrayIndex); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + + // 分别放进两个栈中,取小的出栈 + Queue queue1 = new Queue<>(); + Queue queue2 = new Queue<>(); + ArrayList newArray = new ArrayList<>(); + for (int a : array1) { + queue1.enQueue(a); + } + for (int a : array2) { + queue2.enQueue(a); + } + while (!queue1.isEmpty() && !queue2.isEmpty()) { + if (queue1.getFront() < queue2.getFront()) { + newArray.add(queue1.deQueue()); + } else if (queue1.getFront() > queue2.getFront()) { + newArray.add(queue2.deQueue()); + } else { + newArray.add(queue2.deQueue()); + queue1.deQueue(); + } + } + while (!queue1.isEmpty()) newArray.add(queue1.deQueue()); + while (!queue2.isEmpty()) newArray.add(queue2.deQueue()); + + int[] retArray = new int[newArray.size()]; + for (int i = 0; i < newArray.size(); i++) { + retArray[i] = newArray.get(i); + } + return retArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + + int first = 1; + int second = 1; + if (max == 0) return new int[]{0}; + if (max == 1) return new int[]{first, second}; + + List fibList = new ArrayList<>(); + fibList.add(first); + fibList.add(second); + int last = first + second; + while (last < max) { + fibList.add(last); + first = second; + second = last; + last = first + second; + } + + return list2array(fibList); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + + if (max < 2) return new int[]{}; + if (max == 2) return new int[]{2}; + + List primeList = new ArrayList<>(); + primeList.add(2); + for (int i = 3; i <= max; i += 2) { + // 判断i是不是素数 + boolean isPrime = true; + for (int j = 2; j <= Math.sqrt(i); j++) { + if (i % j == 0) isPrime = false; + } + if (isPrime) primeList.add(i); + } + + return list2array(primeList); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + + if (max < 1) return new int[]{}; + + List perfectNums = new ArrayList<>(); + + for (int aPerfectNum = 1; aPerfectNum <= max; aPerfectNum++) { + int sumFactors = 1; + for (int factor = 2; factor <= aPerfectNum / 2; factor++) { + if (aPerfectNum % factor == 0) { + sumFactors += factor; + } + } + if (sumFactors == aPerfectNum) perfectNums.add(aPerfectNum); + } + + return list2array(perfectNums); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + + StringBuilder stringBuilder = new StringBuilder(); + for (int a : array) { + stringBuilder.append(a).append(seperator); + } + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + return stringBuilder.toString(); + } + + private int[] list2array(List list) { + int[] array = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + array[i] = list.get(i); + } + return array; + } + +} diff --git a/group07/20409287/src/main/java/xdx/homework/second/litestruts/LoginAction.java b/group07/20409287/src/main/java/xdx/homework/second/litestruts/LoginAction.java new file mode 100644 index 0000000000..f59067088b --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +package xdx.homework.second.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/group07/20409287/src/main/java/xdx/homework/second/litestruts/Struts.java b/group07/20409287/src/main/java/xdx/homework/second/litestruts/Struts.java new file mode 100644 index 0000000000..0ba4f85313 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/litestruts/Struts.java @@ -0,0 +1,149 @@ +package xdx.homework.second.litestruts; + +import org.jcp.xml.dsig.internal.dom.DOMEnvelopedTransform; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Executable; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + private static Map actionMap; + + private static Map> actionResultMap; + + private static void init() { + + if (actionMap != null) return; + + actionMap = new HashMap<>(); + actionResultMap = new HashMap<>(); + Document strutsDocument = getDocumentFromFile(); + if (strutsDocument == null) throw new RuntimeException("xml文件读取错误,请检查文件路径是否正确!"); + + try { + NodeList nodeList = strutsDocument.getFirstChild().getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + if (nodeList.item(i).getNodeType() != 1) continue; + Element child = (Element) nodeList.item(i); + Class actionClass = Class.forName(child.getAttribute("class")); + actionMap.put(child.getAttribute("name"), (LoginAction) actionClass.newInstance()); + + NodeList resultList = child.getElementsByTagName("result"); + Map params = new HashMap<>(); + for (int j = 0; j < resultList.getLength(); j++) { + Element result = (Element) resultList.item(j); + params.put(result.getAttribute("name"), result.getTextContent()); + } + actionResultMap.put(child.getAttribute("name"), params); + } + + } catch (ClassNotFoundException e) { + System.out.println(e.getMessage()); + throw new RuntimeException("xml文件内容有误!"); + } catch (InstantiationException | IllegalAccessException e) { + System.out.println(e.getMessage()); + throw new RuntimeException("无法创建接口或者抽象类的实例!"); + } + } + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + // 读取struts文件并初始化相关类 + init(); + + // 根据actionName找到相对应的class + LoginAction loginAction = actionMap.get(actionName); + if (loginAction == null) { + System.out.println("找不到该action: " + actionName); + return null; + } + + View view = new View(); + // 据parameters中的数据调用相应的方法 + Class clazz = loginAction.getClass(); + try { + for (String key : parameters.keySet()) { + String value = parameters.get(key); + if (value == null || value.isEmpty()) continue; + PropertyDescriptor descriptor = new PropertyDescriptor(key, clazz); + descriptor.getWriteMethod().invoke(loginAction, value); + } + // 调用对象的exectue方法, 并获得返回值 + String executeReturn = loginAction.execute(); + // 通过反射找到对象的所有getter方法, 把值和属性形成一个HashMap ,放到View对象的parameters + PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); + HashMap params = new HashMap<>(); + for (PropertyDescriptor pd : pds) { + params.put(pd.getName(), pd.getReadMethod().invoke(loginAction).toString()); + } + view.setParameters(params); + // 确定哪一个jsp + view.setJsp(actionResultMap.get(actionName).get(executeReturn)); + } catch (IntrospectionException e) { + e.printStackTrace(); + return null; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + return view; + } + + + private static Document getDocumentFromFile() { + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + Document document = null; + try { + URL fileURL = Struts.class.getResource("struts.xml"); + DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); + document = documentBuilder.parse(new File(fileURL.getFile())); + } catch (ParserConfigurationException e) { + System.out.println("配置转化异常: " + e.getMessage()); + } catch (SAXException e) { + System.out.println("SAX读取异常: " + e.getMessage()); + } catch (IOException e) { + System.out.println("IO异常: " + e.getMessage()); + } + return document; + } + +} diff --git a/group07/20409287/src/main/java/xdx/homework/second/litestruts/StrutsTest.java b/group07/20409287/src/main/java/xdx/homework/second/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a56eec5e28 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/litestruts/StrutsTest.java @@ -0,0 +1,42 @@ +package xdx.homework.second.litestruts; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group07/20409287/src/main/java/xdx/homework/second/litestruts/View.java b/group07/20409287/src/main/java/xdx/homework/second/litestruts/View.java new file mode 100644 index 0000000000..0d7f02f86b --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/litestruts/View.java @@ -0,0 +1,23 @@ +package xdx.homework.second.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/group07/20409287/src/main/java/xdx/homework/second/litestruts/struts.xml b/group07/20409287/src/main/java/xdx/homework/second/litestruts/struts.xml new file mode 100644 index 0000000000..da4d783426 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/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/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java b/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java new file mode 100644 index 0000000000..d19cdb3dec --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java @@ -0,0 +1,182 @@ +package xdx.homework.first; + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * ArrayList Tester. + * + * @author xdx + * @version 1.0 + * @since
2月25日, 2017
+ */ +public class ArrayListTest { + + private List defaultTestList; + + @Before + public void before() throws Exception { + + defaultTestList = new ArrayList<>(); + Assert.assertTrue(defaultTestList.add("《三国演义》")); + Assert.assertTrue(defaultTestList.add("《红楼梦》")); + Assert.assertTrue(defaultTestList.add("《西游记》")); + Assert.assertTrue(defaultTestList.add("《水浒传》")); + } + + @After + public void after() throws Exception { + } + + /** + * Method: add(E e) + */ + @Test + public void testAdd() throws Exception { + + List testList = new ArrayList<>(); + final int NUM = 1000000; + for (Integer i = 0; i < NUM; i++) { + testList.add(i); + } + for (Integer i = 0; i < NUM; i++) { + Assert.assertEquals(i, testList.get(i)); + } + Assert.assertEquals(NUM, testList.size()); + System.out.println(testList.toString()); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemoveIndex() throws Exception { + + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.add(4)); + Assert.assertTrue(testReomoveList.add(5)); + Assert.assertTrue(testReomoveList.add(6)); + System.out.println("删除前: " + testReomoveList); + Integer delElement = testReomoveList.get(0); + Assert.assertTrue(testReomoveList.remove(0).equals(delElement)); + System.out.println("删除后: " + testReomoveList); + } + + /** + * Method: remove(E e) + */ + @Test + public void testRemoveE() throws Exception { + + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.add(7)); + Assert.assertTrue(testReomoveList.add(8)); + Assert.assertTrue(testReomoveList.add(9)); + System.out.println("删除前: " + testReomoveList); + Assert.assertTrue(testReomoveList.remove(new Integer(8))); + System.out.println("删除后: " + testReomoveList); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + int size = defaultTestList.size(); + Assert.assertEquals(4, size); + System.out.println("defaultTest内容:" + defaultTestList); + System.out.println("defaultTest长度:" + size); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + Assert.assertFalse(defaultTestList.isEmpty()); + List testReomoveList = new ArrayList<>(); + Assert.assertTrue(testReomoveList.isEmpty()); + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + Assert.assertTrue("《三国演义》".equals(defaultTestList.get(0))); + Assert.assertFalse("《西游记》".equals(defaultTestList.get(0))); + } + + /** + * Method: set(int index, Object o) + */ + @Test + public void testSet() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + System.out.println("设置前: " + testList); + Assert.assertTrue(testList.set(0, 10)); + System.out.println("设置后: " + testList); + } + + /** + * Method: contains(Object o) + */ + @Test + public void testContains() throws Exception { + Assert.assertTrue(defaultTestList.contains("《红楼梦》")); + Assert.assertFalse(defaultTestList.contains("《聊斋》")); + } + + /** + * Method: clear() + */ + @Test + public void testClear() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + System.out.println("清除前: " + testList); + testList.clear(); + Assert.assertTrue(testList.isEmpty()); + System.out.println("清除后: " + testList); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + + List testList = new ArrayList<>(); + Assert.assertTrue(testList.add(7)); + Assert.assertTrue(testList.add(8)); + Assert.assertTrue(testList.add(9)); + Iterator iterator = testList.iterator(); + Assert.assertNotNull(iterator); + while(iterator.hasNext()) { + System.out.println(iterator.next()); + } + List testListByDel = new ArrayList<>(); + Assert.assertTrue(testListByDel.add("张三")); + Assert.assertTrue(testListByDel.add("李四")); + Assert.assertTrue(testListByDel.add("王五")); + Iterator iteratorByDel = testListByDel.iterator(); + while(iteratorByDel.hasNext()) { + iteratorByDel.remove(); + } + Assert.assertTrue(testListByDel.isEmpty()); + + } + + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/LinkedListTest.java b/group07/20409287/src/test/xdx/homework/first/LinkedListTest.java new file mode 100644 index 0000000000..dabbc04a96 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/LinkedListTest.java @@ -0,0 +1,222 @@ +package xdx.homework.first; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * LinkedList Tester. + * + */ +public class LinkedListTest { + + private LinkedList defaultLinkList; + + @Before + public void before() throws Exception { + + defaultLinkList = new LinkedList<>(); + defaultLinkList.add("1.苹果"); + defaultLinkList.add("2.香蕉"); + defaultLinkList.add("3.菠萝"); + defaultLinkList.add("4.橙子"); + defaultLinkList.add("5.葡萄"); + } + + @After + public void after() throws Exception { + } + + /** + * Method: add(E data) + */ + @Test + public void testAdd() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (Integer i = 0; i < 1000; i++) { + testLinkedList.add(i); + } + System.out.println(testLinkedList); + } + + /** + * Method: remove(int index) + */ + @Test + public void testRemoveIndex() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 100; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("删除前:" + testLinkedList); + for (int i = 0; i < 50; i++) { + testLinkedList.remove(i); + } + System.out.println("删除后:" + testLinkedList); + } + + /** + * Method: remove(E e) + */ + @Test + public void testRemoveE() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("删除前:" + testLinkedList); + testLinkedList.remove("1号"); + testLinkedList.remove("10号"); + System.out.println("删除后:" + testLinkedList); + } + + /** + * Method: size() + */ + @Test + public void testSize() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + int i = 0; + for (; i < 100; i++) { + testLinkedList.add(i + "号"); + } + Assert.assertEquals(i, testLinkedList.size()); + System.out.println("testLinkedList的内容:" + testLinkedList); + System.out.println("testLinkedList的长度:" + testLinkedList.size()); + } + + /** + * Method: isEmpty() + */ + @Test + public void testIsEmpty() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + System.out.println("testLikedList是否为空:" + testLinkedList.isEmpty()); + Assert.assertTrue(testLinkedList.isEmpty()); + String insertElement = "Hello World!"; + System.out.println("插入元素: " + insertElement); + testLinkedList.add(insertElement); + System.out.println("testLikedList是否为空:" + testLinkedList.isEmpty()); + Assert.assertFalse(testLinkedList.isEmpty()); + + } + + /** + * Method: get(int index) + */ + @Test + public void testGet() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + for (int i = 0; i < 10; i++) { + System.out.println("索引为" + i + "处的元素为:" + testLinkedList.get(i)); + Assert.assertEquals((i + "号"), testLinkedList.get(i)); + } + } + + /** + * Method: set(int index, E e) + */ + @Test + public void testSet() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + for (int i = 0; i < 10; i++) { + String nodeValue = i + "号替补"; + testLinkedList.set(i, nodeValue); + Assert.assertEquals(nodeValue, testLinkedList.get(i)); + } + System.out.println("替换后:" + testLinkedList); + } + + /** + * Method: contains(E e) + */ + @Test + public void testContains() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + for (int i = 0; i < 10; i++) { + String containTestValue = i + "号"; + System.out.println("是否包含【" + containTestValue + "】:" + + testLinkedList.contains(containTestValue)); + Assert.assertTrue(testLinkedList.contains(containTestValue)); + } + + } + + /** + * Method: clear() + */ + @Test + public void testClear() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + testLinkedList.clear(); + System.out.println("清空后的链表:" + testLinkedList); + Assert.assertTrue(testLinkedList.isEmpty()); + } + + /** + * Method: iterator() + */ + @Test + public void testIterator() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + Iterator iterator = testLinkedList.iterator(); + Assert.assertNotNull(iterator); + while (iterator.hasNext()) { + System.out.println(iterator.next()); + } + } + + + + /** + * Method: remove() + */ + @Test + public void testRemove() throws Exception { + + LinkedList testLinkedList = new LinkedList<>(); + for (int i = 0; i < 10; i++) { + testLinkedList.add(i + "号"); + } + System.out.println("原链表:" + testLinkedList); + Iterator iterator = testLinkedList.iterator(); + while (iterator.hasNext()) { + iterator.remove(); + } + System.out.println("删除所有元素后:" + testLinkedList); + Assert.assertTrue(testLinkedList.isEmpty()); + + } + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/QueueTest.java b/group07/20409287/src/test/xdx/homework/first/QueueTest.java new file mode 100644 index 0000000000..6e56e3b8b1 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/QueueTest.java @@ -0,0 +1,141 @@ +package xdx.homework.first; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import xdx.homework.first.Queue; + +/** + * Queue Tester. + * + * @author + * @since
二月 25, 2017
+ * @version 1.0 + */ +public class QueueTest { + + private Queue defaultQueue; + + @Before + public void before() throws Exception { + + defaultQueue = new Queue<>(); + defaultQueue.enQueue("赵"); + defaultQueue.enQueue("钱"); + defaultQueue.enQueue("孙"); + defaultQueue.enQueue("李"); + defaultQueue.enQueue("周"); + defaultQueue.enQueue("吴"); + defaultQueue.enQueue("郑"); + defaultQueue.enQueue("王"); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: enQueue(E data) + * + */ + @Test + public void testEnQueue() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + System.out.println(testQueue); + } + + /** + * + * Method: deQueue() + * + */ + @Test + public void testDeQueue() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + System.out.println("删除前:" + testQueue); + System.out.println("删除:" + testQueue.deQueue()); + System.out.println("删除后:" + testQueue); + } + + /** + * + * Method: length() + * + */ + @Test + public void testLength() throws Exception { + Assert.assertEquals(8, defaultQueue.length()); + System.out.println("defaultQueue长度:" + defaultQueue.length()); + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + Assert.assertTrue(!defaultQueue.isEmpty()); + } + + /** + * + * Method: clear() + * + */ + @Test + public void testClear() throws Exception { + + Queue testQueue = new Queue<>(); + testQueue.enQueue("刘备"); + testQueue.enQueue("关羽"); + testQueue.enQueue("张飞"); + + testQueue.clear(); + Assert.assertTrue(testQueue.isEmpty()); + } + + /** + * + * Method: getFront() + * + */ + @Test + public void testGetFront() throws Exception { + Assert.assertEquals("赵", defaultQueue.getFront()); + Assert.assertNotEquals("钱", defaultQueue.getFront()); + } + + /** + * + * Method: getRear() + * + */ + @Test + public void testGetRear() throws Exception { + Assert.assertEquals("王", defaultQueue.getRear()); + Assert.assertNotEquals("钱", defaultQueue.getFront()); + } + + /** + * + * Method: toString() + * + */ + @Test + public void testToString() throws Exception { + System.out.println("defaultQueue内容:" + defaultQueue.toString()); + } + + +} diff --git a/group07/20409287/src/test/xdx/homework/first/StackTest.java b/group07/20409287/src/test/xdx/homework/first/StackTest.java new file mode 100644 index 0000000000..bfc6b2c8f7 --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/first/StackTest.java @@ -0,0 +1,126 @@ +package test.xdx.homework.first; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import xdx.homework.first.Stack; + +/** + * Stack Tester. + * + * @version 1.0 + */ +public class StackTest { + + private Stack defaultStack; + + @Before + public void before() throws Exception { + + defaultStack = new Stack<>(); + defaultStack.push("孙悟空"); + defaultStack.push("唐僧"); + defaultStack.push("猪八戒"); + defaultStack.push("沙僧"); + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: push(E e) + * + */ + @Test + public void testPush() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + Assert.assertEquals("python", testStack.pop()); + Assert.assertEquals("C++", testStack.pop()); + Assert.assertEquals("java", testStack.pop()); + + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println(testStack); + Assert.assertEquals("python", testStack.pop()); + Assert.assertEquals("C++", testStack.pop()); + Assert.assertEquals("java", testStack.pop()); + Assert.assertTrue(testStack.isEmpty()); + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() throws Exception { + Assert.assertEquals("沙僧", defaultStack.peek()); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() throws Exception { + Assert.assertEquals(4, defaultStack.size()); + } + + /** + * + * Method: clear() + * + */ + @Test + public void testClear() throws Exception { + + Stack testStack = new Stack<>(); + testStack.push("java"); + testStack.push("C++"); + testStack.push("python"); + System.out.println("清空前:" + testStack); + testStack.clear(); + System.out.println("清空后:" + testStack); + Assert.assertTrue(testStack.isEmpty()); + + + } + + +} diff --git a/group07/20409287/src/test/xdx/homework/second/ArrayUtilTest.java b/group07/20409287/src/test/xdx/homework/second/ArrayUtilTest.java new file mode 100644 index 0000000000..ffafc94f7d --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/second/ArrayUtilTest.java @@ -0,0 +1,157 @@ +package xdx.homework.second; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import org.testng.Assert; +import xdx.homework.second.array.ArrayUtil; + +import java.util.Arrays; + +/** + * ArrayUtil Tester. + * + * @author + * @since
���� 4, 2017
+ * @version 1.0 + */ +public class ArrayUtilTest { + + ArrayUtil arrayUtil = new ArrayUtil(); + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: reverseArray(int[] origin) + * + */ + @Test + public void testReverseArray() throws Exception { + + int[] array = {1,2,3,4,5,6,7,8,9}; + System.out.println("===================数组反转开始==================="); + System.out.println("原数组: " + Arrays.toString(array)); + arrayUtil.reverseArray(array); + System.out.println("反转后的数组: " + Arrays.toString(array)); + System.out.println("===================数组反转结束===================" + "\n"); + } + + /** + * + * Method: removeZero(int[] oldArray) + * + */ + @Test + public void testRemoveZero() throws Exception { + + System.out.println("===================数组清零开始==================="); + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println("原数组: " + Arrays.toString(oldArr)); + System.out.println("清零后的数组: " + Arrays.toString(arrayUtil.removeZero(oldArr))); + System.out.println("===================数组清零结束===================" + "\n"); + } + + /** + * + * Method: merge(int[] array1, int[] array2) + * + */ + @Test + public void testMerge() throws Exception { + + System.out.println("===================数组合并开始==================="); + int[] array1 = {1,2,4,5}; + int[] array2 = {3,4,5,6,7,8,9}; + System.out.println("原数组1: " + Arrays.toString(array1)); + System.out.println("原数组2: " + Arrays.toString(array2)); + int[] mergedArray = arrayUtil.merge(array1, array2); + System.out.println("合并后的数组: " + Arrays.toString(mergedArray)); + System.out.println("===================数组合并结束===================" + "\n"); + } + + /** + * + * Method: grow(int [] oldArray, int size) + * + */ + @Test + public void testGrow() throws Exception { + + System.out.println("===================数组增长开始==================="); + int[] array2 = {3,4,5,6,7,8,9}; + final int GROW_SIZE = 5; + int[] growArray = arrayUtil.grow(array2, GROW_SIZE); + Assert.assertEquals(array2.length + GROW_SIZE, growArray.length); + System.out.println("原数组: " + Arrays.toString(array2)); + System.out.println("增长" + GROW_SIZE + "个单位后的数组: " + Arrays.toString(growArray)); + System.out.println("===================数组增长结束===================" + "\n"); + } + + /** + * + * Method: fibonacci(int max) + * + */ + @Test + public void testFibonacci() throws Exception { + + System.out.println("===================斐波那契数列开始==================="); + final int MAX = 10000000; + System.out.println(MAX + "以内的斐波那契数列: " + Arrays.toString(arrayUtil.fibonacci(MAX))); + System.out.println("===================斐波那契数列结束===================" + "\n"); + } + + /** + * + * Method: getPrimes(int max) + * + */ + @Test + public void testGetPrimes() throws Exception { + + System.out.println("===================素数计算开始==================="); + final int MAX = 10000; + System.out.println(MAX + "以内的素数: " + Arrays.toString(arrayUtil.getPrimes(MAX))); + System.out.println("===================素数计算结束===================" + "\n"); + } + + /** + * + * Method: getPerfectNumbers(int max) + * + */ + @Test + public void testGetPerfectNumbers() throws Exception { + + System.out.println("===================计算完美数列结束==================="); + final int MAX = 10000; + System.out.println(MAX + "以内的完数分别是: " + Arrays.toString(arrayUtil.getPerfectNumbers(MAX))); + System.out.println("===================计算完美数列结束===================" + "\n"); + } + + /** + * + * Method: join(int[] array, String seperator) + * + */ + @Test + public void testJoin() throws Exception { + + System.out.println("===================数组分隔开始==================="); + int[] array2 = {3,4,5,6,7,8,9}; + final String SEP = "-"; + System.out.println("原数组: " + Arrays.toString(array2)); + System.out.println("分隔符: " + SEP); + System.out.println("分隔后的数组: " + arrayUtil.join(array2, SEP)); + System.out.println("===================数组分隔结束===================" + "\n"); + } + + +} diff --git a/group07/396868934/DataStructure/.classpath b/group07/396868934/DataStructure/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group07/396868934/DataStructure/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group07/396868934/DataStructure/.project b/group07/396868934/DataStructure/.project new file mode 100644 index 0000000000..72a951f7c1 --- /dev/null +++ b/group07/396868934/DataStructure/.project @@ -0,0 +1,17 @@ + + + DataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/396868934/DataStructure/.settings/org.eclipse.jdt.core.prefs b/group07/396868934/DataStructure/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group07/396868934/DataStructure/.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/group07/396868934/DataStructure/bin/.gitignore b/group07/396868934/DataStructure/bin/.gitignore new file mode 100644 index 0000000000..c2d9872a16 --- /dev/null +++ b/group07/396868934/DataStructure/bin/.gitignore @@ -0,0 +1 @@ +/com/ diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYArrayLink.java b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayLink.java new file mode 100644 index 0000000000..450a1ccff1 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayLink.java @@ -0,0 +1,93 @@ +package com.louisly.java; + +public class LYArrayLink { + + private int currentCount = 0; + private LYNode header = null; + private LYNode lastNode = null; + public void addObject(Object obj) { + if (obj == null) return; + + currentCount++; + LYNode node = new LYNode(); + node.data = obj; + + if (lastNode != null) { + lastNode.next = node; + lastNode = node; + } else { + lastNode = node; + } + + if (header == null) { + header = node; + } + } + + public void removeObject(Object obj) { + LYNode lastNode = null; + LYNode node = header; + Object data = null; + while (node != null) { + data = node.data; + if (data == obj) { + if (lastNode != null) { + lastNode.next = node.next; + } else { + // ƳһԪ + header = node.next; + } + + currentCount--; + } else { + lastNode = node; + } + node = node.next; + } + } + + public void removeAtIndex(int index) { + if (header == null) return; // error: out of bounces + + LYNode lastNode = null; + LYNode node = header; + + for (int i = 0; i < index; i++) { + if (node != null) { + lastNode = node; + node = node.next; + } else { + return; // error: out of bounces + } + } + + if (index == 0) { + header = node.next; + } else { + lastNode.next = node.next; + } + currentCount--; + } + + public Object get(int index) { + if (header == null) return null; // error: out of bounces + LYNode node = header; + for (int i = 0; i < index; i++) { + node = node.next; + if (node == null) { + return null; + } + } + return node.data; + } + + public int size() { + return currentCount; + } + + private static class LYNode { + Object data; + LYNode next; + + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYArrayList.java b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayList.java new file mode 100644 index 0000000000..0076cddad3 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayList.java @@ -0,0 +1,96 @@ +package com.louisly.java; +import com.louisly.java.LYIterator; + +public class LYArrayList { + private Object[] elementData = new Object[10]; + private int currentCount = 0; + public void addObject(Object obj) { + if (currentCount >= elementData.length) { + grow(); + } + elementData[currentCount] = obj; + currentCount++; + } + + public boolean removeObject(Object obj) { + boolean existObj = false; + int removeIndex = -1; + int index = currentCount; + for (int i = 0; i < index; i++) { + Object element = elementData[i]; + boolean remove = false; + if (element != null && element.equals(obj)) { + elementData[i] = null; + existObj = true; + remove = true; + // ԷһĵڶԪ + if (removeIndex == -1) { + removeIndex = i; + } + currentCount--; + } + // Ԫǰƶ + if (!remove) { + elementData[removeIndex] = element; + elementData[i] = null; + removeIndex++; + } + } + return existObj; + } + + public boolean removeAtIndex(int index) { + if (index > currentCount) { + return false; + } + elementData[index] = null; + + for (int i = index+1; i < currentCount; i++) { + elementData[i-1] = elementData[i]; + elementData[i] = null; + } + currentCount--; + return true; + } + + public void grow() { + Object[] target = new Object[elementData.length*2]; + System.arraycopy(elementData, 0, target, 0, elementData.length); + elementData = target; + } + + public Object get(int index) { + if (index > currentCount) { + return null; + } + return elementData[index]; + } + + public int size() { + return currentCount; + } + + public LYIterator iterator() { + return new LYArrayListIterator(this); + } + private class LYArrayListIterator implements LYIterator { + LYArrayList arrayList = null; + int position = 0; + public LYArrayListIterator(LYArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + + return false; + } + @Override + public Object next() { + return elementData[position]; + } + public void remove() { + + } + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYArrayListTest.java b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayListTest.java new file mode 100644 index 0000000000..a457211867 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYArrayListTest.java @@ -0,0 +1,50 @@ +package com.louisly.java; + +import static org.junit.Assert.*; + +import java.util.ArrayList; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import junit.framework.Assert; + +public class LYArrayListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + LYArrayList list = new LYArrayList(); + list.addObject(new Integer(10)); + Assert.assertEquals(10, ((Integer)list.get(0)).intValue()); + } + + @Test + public void testRemoveObject() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveAtIndex() { + fail("Not yet implemented"); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYBinaryTree.java b/group07/396868934/DataStructure/src/com/louisly/java/LYBinaryTree.java new file mode 100644 index 0000000000..4ea3141135 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYBinaryTree.java @@ -0,0 +1,45 @@ +package com.louisly.java; +import com.louisly.java.LYObject; + +public class LYBinaryTree { + + private LYBinaryTreeNode headerNode; + + public void addObject(LYObject obj) { + + if (headerNode == null) { + LYBinaryTreeNode node = new LYBinaryTreeNode(); + node.data = obj; + headerNode = node; + return; + } + + this.appendObject(headerNode, obj); + } + + private void appendObject(LYBinaryTreeNode toNode, LYObject obj) { + if (obj.i > toNode.data.i) { + if (toNode.right != null) { + this.appendObject(toNode.right, obj); + } else { + LYBinaryTreeNode node = new LYBinaryTreeNode(); + node.data = obj; + toNode.right = node; + } + } else { + if (toNode.left != null) { + this.appendObject(toNode.left, obj); + } else { + LYBinaryTreeNode node = new LYBinaryTreeNode(); + node.data = obj; + toNode.left = node; + } + } + } + + public static class LYBinaryTreeNode { + private LYObject data; + private LYBinaryTreeNode left; + private LYBinaryTreeNode right; + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYIterator.java b/group07/396868934/DataStructure/src/com/louisly/java/LYIterator.java new file mode 100644 index 0000000000..c2ae34c871 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYIterator.java @@ -0,0 +1,6 @@ +package com.louisly.java; + +public interface LYIterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYObject.java b/group07/396868934/DataStructure/src/com/louisly/java/LYObject.java new file mode 100644 index 0000000000..f0f5aa5d81 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYObject.java @@ -0,0 +1,8 @@ +package com.louisly.java; + +public class LYObject extends Object { + int i = 0; + public LYObject(int i) { + this.i = i; + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYQueue.java b/group07/396868934/DataStructure/src/com/louisly/java/LYQueue.java new file mode 100644 index 0000000000..5f573ae0ac --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYQueue.java @@ -0,0 +1,28 @@ +package com.louisly.java; +import com.louisly.java.LYArrayList; + +public class LYQueue { + LYArrayList list = null; + public LYQueue() { + list = new LYArrayList(); + } + + public void enQueue(Object obj) { + list.addObject(obj); + } + + public Object deQueue() { + if (list.size() != 0) { + return list.get(0); + } + return null; + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/LYStack.java b/group07/396868934/DataStructure/src/com/louisly/java/LYStack.java new file mode 100644 index 0000000000..c615208041 --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/LYStack.java @@ -0,0 +1,34 @@ +package com.louisly.java; +import com.louisly.java.LYArrayList; + +public class LYStack { + private LYArrayList list = new LYArrayList(); +// public LYStack() { +// list = new LYArrayList(); +// } + + public void push(Object obj) { + list.addObject(obj); + } + + public Object pop() { + if (list.size() == 0) return null; + Object obj = list.get(list.size()-1); + list.removeObject(obj); + return obj; + } + + public Object peak() { + if (list.size() == 0) return null; + Object obj = list.get(list.size()-1); + return obj; + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/396868934/DataStructure/src/com/louisly/java/main.java b/group07/396868934/DataStructure/src/com/louisly/java/main.java new file mode 100644 index 0000000000..c613c2f36c --- /dev/null +++ b/group07/396868934/DataStructure/src/com/louisly/java/main.java @@ -0,0 +1,84 @@ +package com.louisly.java; +import com.louisly.java.LYArrayList; +import com.louisly.java.LYObject; +import com.louisly.java.LYArrayLink; +import com.louisly.java.LYBinaryTree; + +public class main { + + public static void main(String[] args) { + +// testBinaryTree(); + testArrayLink(); +// testArrayList(); + } + + public static void testBinaryTree() { + LYBinaryTree tree = new LYBinaryTree(); + tree.addObject(new LYObject(5)); + tree.addObject(new LYObject(7)); + tree.addObject(new LYObject(2)); + tree.addObject(new LYObject(1)); + tree.addObject(new LYObject(6)); + tree.addObject(new LYObject(4)); + tree.addObject(new LYObject(8)); + } + + public static void testArrayLink() { + // 20Ԫ + LYArrayLink list = new LYArrayLink(); + for (int i = 0; i < 20; i++) { + LYObject object = new LYObject(i); + list.addObject(object); + } + + System.out.print("ǰйԪظ" + list.size() + "\n"); + + // ӡĿǰڵÿԪ + for (int i = 0; i < list.size(); i++) { + LYObject obj = (LYObject)list.get(i); + System.out.print(obj.i + "\n"); + } + + System.out.print("======\n"); + + // Ƴ߸Ԫ + list.removeAtIndex(7); + + System.out.print("ǰйԪظ" + list.size() + "\n"); + + // ٴӡĿǰʣЩԪ + for (int i = 0; i < list.size(); i++) { + LYObject obj = (LYObject)list.get(i); + System.out.print(obj.i + "\n"); + } + } + + public static void testArrayList() { + // 20Ԫ + LYArrayList list = new LYArrayList(); + for (int i = 0; i < 20; i++) { + LYObject object = new LYObject(i); + list.addObject(object); + } + + System.out.print("ǰйԪظ" + list.size() + "\n"); + + for (int i = 0; i < list.size(); i++) { + LYObject obj = (LYObject)list.get(i); + System.out.print(obj.i + "\n"); + } + + System.out.print("======\n"); + + + list.removeAtIndex(7); + System.out.print("ǰйԪظ" + list.size() + "\n"); + + for (int i = 0; i < list.size(); i++) { + LYObject obj = (LYObject)list.get(i); + System.out.print(obj.i + "\n"); + } + } + +} diff --git a/group07/43819473/homework/pom.xml b/group07/43819473/homework/pom.xml new file mode 100644 index 0000000000..e9da2d1a08 --- /dev/null +++ b/group07/43819473/homework/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + homework1 + homework + 1.0-SNAPSHOT + + + + + junit + junit + 4.12 + + + + com.alibaba + fastjson + 1.2.7 + + + + dom4j + dom4j + 1.6.1 + + + jaxen + jaxen + 1.1.1 + + + \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/coderising/array/ArrayUtil.java b/group07/43819473/homework/src/main/java/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..8ef221c5cf --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/array/ArrayUtil.java @@ -0,0 +1,238 @@ +package 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 int[] reverseArray(int[] origin) { + int[] result = new int[origin.length]; + for (int i = 0; i <= origin.length - 1; i++) { + result[i] = origin[origin.length - 1 - i]; + } + return result; + } + + /** + * 现在有如下的一个数组: int origin[]={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 origin + * @return + */ + + public static int[] removeZero(int[] origin) { + int[] tempArray = new int[origin.length]; + int j = 0; + for (int i = 0; i <= origin.length - 1; i++) { + if (origin[i] != 0) { + tempArray[j++] = origin[i]; + } + } + + int[] result = new int[j]; + System.arraycopy(tempArray, 0, result, 0, j); + return result; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2) { + if (array1.length == 0) { + return array2; + } + if (array2.length == 0) { + return array1; + } + int[] tempArray = new int[array1.length + array2.length]; + int currentSize = array1.length; + System.arraycopy(array1, 0, tempArray, 0, array1.length); + for (int i = 0; i <= array2.length - 1; i++) { + for (int j = 0; j <= currentSize - 1; j++) { + if (array2[i] == tempArray[j]) { + break; + } else if (array2[i] < tempArray[j]) { + System.arraycopy(tempArray, j, tempArray, j + 1, currentSize - j); + tempArray[j] = array2[i]; + currentSize++; + break; + } + if (j == currentSize - 1) { + tempArray[j + 1] = array2[i]; + currentSize++; + break; + } + } + } + + int[] result = new int[currentSize]; + System.arraycopy(tempArray, 0, result, 0, currentSize); + 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) { + int[] result = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) { + return new int[]{}; + } + + int[] temp = new int[max]; + temp[0] = 1; + temp[1] = 1; + + int i = 2; + while (i >= 2) { + int last = temp[i - 1] + temp[i - 2]; + if (last < max) { + temp[i++] = last; + } else { + break; + } + } + + int[] result = new int[i]; + System.arraycopy(temp, 0, result, 0, i); + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max <= 2) { + return new int[]{}; + } else if (max == 3) { + return new int[]{2}; + } else { + int[] temp = new int[max / 2 + 1]; + temp[0] = 2; + int resultSize = 1; + + for (int i = 3; i < max; i += 2) { + boolean isPrime = true; + for (int j = 0; j <= resultSize - 1; j++) { + if (i % temp[j] == 0) { + isPrime = false; + break; + } + } + if (isPrime) { + temp[resultSize++] = i; + } + } + int[] result = new int[resultSize]; + System.arraycopy(temp, 0, result, 0, resultSize); + + return result; + } + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] tempResult = new int[max]; + int resultSize = 0; + + //对小于max的值逐个循环判断 + for (int i = 2; i < max; i++) { +// System.out.println("==="+i); + int[] factors = new int[max];//因子数组 + factors[0] = 1; + int factorSize = 1; + + //获取因子 + for (int j = 2; j < i / 2 + 1; j++) { + if (i % j == 0) { + factors[factorSize++] = j; + } + } + + //计算因子数组的和是否与当前值相等,相等则放入结果数组中 + if (factorSize > 1) { + int sumValue = 0; + for (int factorIndex = 0; factorIndex <= factorSize - 1; factorIndex++) { + sumValue += factors[factorIndex]; + } + if (sumValue == i) { + tempResult[resultSize++] = i; + } + } + } + + int[] result = new int[resultSize]; + System.arraycopy(tempResult, 0, result, 0, resultSize); + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + int size = array.length; + String result = ""; + if (size > 0) { + if (size == 1) { + result = String.valueOf(array[0]); + } else { + for (int i = 0; i < array.length - 1; i++) { + result += String.valueOf(array[i]) + seperator; + } + result += String.valueOf(array[array.length - 1]); + } + } + + return result; + } + +} diff --git a/group07/43819473/homework/src/main/java/coderising/litestruts/LoginAction.java b/group07/43819473/homework/src/main/java/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..c528df798a --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package 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/group07/43819473/homework/src/main/java/coderising/litestruts/Struts.java b/group07/43819473/homework/src/main/java/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..c2b2ccad51 --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/litestruts/Struts.java @@ -0,0 +1,95 @@ +package coderising.litestruts; + +import org.dom4j.*; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + + +public class Struts { + + /* + 0. 读取配置文件struts.xml + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + 2. 通过反射调用对象的execute 方法, 并获得返回值,例如"success" + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + public static View runAction(String actionName, Map parameters) { + + SAXReader reader = new SAXReader(); + View view = new View(); + + try { + //读取xml,获取class,获取对象 + Document document = reader.read(new File("src\\main\\java\\coderising\\litestruts\\struts.xml")); + Node node = document.selectSingleNode("//struts/action[@name='" + actionName + "']"); + Element element = (Element) node; + String className = element.attribute("class").getText(); + Class clazz = Class.forName(className); + Object object = clazz.newInstance(); + + //调用set方法设置属性 + for (Map.Entry entry : parameters.entrySet()) { + String methodName = "set" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); + Method method = clazz.getMethod(methodName, entry.getValue().getClass()); + method.invoke(object, entry.getValue()); + } + + //调用exectue 方法, 并获得返回值 + Method methodExecute = clazz.getMethod("execute"); + String executeResult = methodExecute.invoke(object).toString(); + + //找到所有getter,调用并将值放到resultMap中 + Map resultMap = new HashMap(); + Method[] getMethods = clazz.getMethods(); + for (Method getMethod : getMethods) { + int index = getMethod.toString().lastIndexOf("."); + String pureName = getMethod.toString().substring(index + 1); + if (pureName.startsWith("get")) { //eg. getName() + String tempStr= pureName.substring(3); //eg. Name() + String attributeName = tempStr.substring(0, 1).toLowerCase() + tempStr.substring(1, tempStr.length() - 2); //eg. name + String attributeResult = getMethod.invoke(object).toString(); + resultMap.put(attributeName, attributeResult); + } + } + + //组装返回对象 + Node resultNode = node.selectSingleNode("result[@name='" + executeResult + "']"); + String resultTarget = resultNode.getText(); + view.setJsp(resultTarget).setParameters(resultMap); + + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } finally { + return view; + } + } + + public static void main(String[] args) { + + } + +} diff --git a/group07/43819473/homework/src/main/java/coderising/litestruts/StrutsTest.java b/group07/43819473/homework/src/main/java/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..024b5f196f --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +package 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/group07/43819473/homework/src/main/java/coderising/litestruts/View.java b/group07/43819473/homework/src/main/java/coderising/litestruts/View.java new file mode 100644 index 0000000000..22fdf877d8 --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package 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/group07/43819473/homework/src/main/java/coderising/litestruts/struts.xml b/group07/43819473/homework/src/main/java/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..57ad66abd0 --- /dev/null +++ b/group07/43819473/homework/src/main/java/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/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java b/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java new file mode 100644 index 0000000000..40ccb13a06 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/ArrayList.java @@ -0,0 +1,87 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + if (size > elementData.length / 2) { + elementData = grow(elementData); + } + + elementData[size] = o; + size++; + } + + private Object[] grow(Object[] datas) { + Object[] elementDataNew = new Object[elementData.length + elementData.length / 4]; + System.arraycopy(datas, 0, elementDataNew, 0, size); + return elementDataNew; + } + + public void add(int index, Object o) { + + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + if (size > elementData.length / 2) { + elementData = grow(elementData); + } + + for (int i = size - 1; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + for (int i = index; i <= size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size - 1] = null; + size--; + return null; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListInterator(); + } + + private class ArrayListInterator implements Iterator { + + private int nowIndex = 0; + + public boolean hasNext() { + if (nowIndex <= size - 1) { + return true; + } + return false; + } + + public Object next() { + return elementData[nowIndex++]; + } + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/BinaryTree.java b/group07/43819473/homework/src/main/java/dataStructure/BinaryTree.java new file mode 100644 index 0000000000..a93c25253e --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/BinaryTree.java @@ -0,0 +1,73 @@ +package dataStructure; + +public class BinaryTree { + + private BinaryTreeNode root; + + public BinaryTreeNode getRoot() { + return root; + } + + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + + public BinaryTreeNode insert(int data) { + BinaryTreeNode node = new BinaryTreeNode(data); + root = insert(root, node); + return root; + } + + private BinaryTreeNode insert(BinaryTreeNode root, BinaryTreeNode newNode) { + if (root == null) { + root = newNode; + } else if (newNode.data > root.data) { + root.right = insert(root.right, newNode); + } else { + root.left = insert(root.left, newNode); + } + return root; + } + + /** + * binary tree node + */ + private class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(int data) { + this.left = null; + this.right = null; + this.data = data; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + } +} + diff --git a/group07/43819473/homework/src/main/java/dataStructure/Iterator.java b/group07/43819473/homework/src/main/java/dataStructure/Iterator.java new file mode 100644 index 0000000000..06ad99316f --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Iterator.java @@ -0,0 +1,9 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java b/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java new file mode 100644 index 0000000000..f6a960698a --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/LinkedList.java @@ -0,0 +1,116 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/21. + */ +public class LinkedList implements List { + + private Node head=new Node(); + private int size = 0; + + public void add(Object o) { + addToNode(head,o); + size++; + } + + public void add(int index, Object o) { + if (index <0 || index > size) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + addToNode(getLastNode(index),o); + size++; + } + + private Node getLastNode(int index){ + + Node nowNode = head; + for (int pos = 1; pos <= index; pos++) { + nowNode = nowNode.next; + } + return nowNode; + } + private void addToNode(Node node,Object o){ + if (node.next == null) { + Node newNode = new Node(); + newNode.data = o; + node.next = newNode; + } else { + Node newNode = new Node(); + newNode.data = o; + newNode.next = node.next; + node.next = newNode; + } + } + + public Object get(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + Node node= getLastNode(index); + return node.next==null?null:node.next.data; + } + + public Object remove(int index) { + if (index <0 || index > size - 1) { + throw new IndexOutOfBoundsException("index out of bound"); + } + + Node node= getLastNode(index); + Node nowNode=node.next; + if(nowNode.next!=null){ + node.next=node.next.next; + }else{ + node.next=null; + } + size--; + return nowNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + add(0,o); + } + + 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 LinkedListInterator(); + } + + private class LinkedListInterator implements Iterator { + + private int nowIndex = 0; + + public boolean hasNext() { + if (nowIndex <= size - 1) { + return true; + } + return false; + } + + public Object next() { + return get(nowIndex++); + } + } + + + private static class Node { + Object data; + Node next; + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/List.java b/group07/43819473/homework/src/main/java/dataStructure/List.java new file mode 100644 index 0000000000..0b1b43fc26 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/List.java @@ -0,0 +1,16 @@ +package dataStructure; + +/** + * Created by zj on 2017/2/20. + */ +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(); +} \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/dataStructure/Queue.java b/group07/43819473/homework/src/main/java/dataStructure/Queue.java new file mode 100644 index 0000000000..943e0e64f6 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Queue.java @@ -0,0 +1,25 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class Queue { + + private LinkedList list=new LinkedList(); + + public void enQueue(Object o) { + list.addFirst(o); + } + + public Object deQueue() { + return list.removeLast(); + } + + public boolean isEmpty() { + return list.size()==0?true:false; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/43819473/homework/src/main/java/dataStructure/Stack.java b/group07/43819473/homework/src/main/java/dataStructure/Stack.java new file mode 100644 index 0000000000..e1a7161317 --- /dev/null +++ b/group07/43819473/homework/src/main/java/dataStructure/Stack.java @@ -0,0 +1,29 @@ +package dataStructure; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class Stack { + + private LinkedList list = new LinkedList(); + + public void push(Object o) { + list.addFirst(o); + } + + public Object pop() { + return list.removeFirst(); + } + + public Object peek() { + return list.get(0); + } + + public boolean isEmpty() { + return list.size() == 0 ? true : false; + } + + public int size() { + return list.size(); + } +} diff --git a/group07/43819473/homework/src/test/java/coderising/array/ArrayUtilTest.java b/group07/43819473/homework/src/test/java/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..a19eb84269 --- /dev/null +++ b/group07/43819473/homework/src/test/java/coderising/array/ArrayUtilTest.java @@ -0,0 +1,81 @@ +package coderising.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +/** + * Created by zj on 2017/3/4. + */ +public class ArrayUtilTest { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void reverseArray() throws Exception { + Assert.assertArrayEquals(new int[]{3, 30, 9, 7}, ArrayUtil.reverseArray(new int[]{7, 9, 30, 3})); + Assert.assertArrayEquals(new int[]{6, 3, 30, 9, 7}, ArrayUtil.reverseArray(new int[]{7, 9, 30, 3, 6})); + } + + @Test + public void removeZero() throws Exception { + Assert.assertArrayEquals(new int[]{1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, + ArrayUtil.removeZero(new int[]{1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5})); + } + + @Test + public void merge() throws Exception { + Assert.assertArrayEquals(new int[]{}, ArrayUtil.merge(new int[]{}, new int[]{})); + Assert.assertArrayEquals(new int[]{1, 2}, ArrayUtil.merge(new int[]{}, new int[]{1, 2})); + Assert.assertArrayEquals(new int[]{1, 2}, ArrayUtil.merge(new int[]{1, 2}, new int[]{})); + Assert.assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, ArrayUtil.merge(new int[]{3, 5, 7, 8}, new int[]{4, 5, 6, 7})); + Assert.assertArrayEquals(new int[]{2, 3, 5, 6, 7, 8, 9}, ArrayUtil.merge(new int[]{3, 5, 7, 8}, new int[]{2, 5, 6, 9})); + Assert.assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, ArrayUtil.merge(new int[]{4, 5, 6, 7}, new int[]{3, 5, 7, 8})); + Assert.assertArrayEquals(new int[]{2, 3, 5, 6, 7, 8, 9}, ArrayUtil.merge(new int[]{2, 5, 6, 9}, new int[]{3, 5, 7, 8})); + } + + @Test + public void grow() throws Exception { + Assert.assertArrayEquals(new int[]{2, 3, 6, 0, 0, 0}, ArrayUtil.grow(new int[]{2, 3, 6}, 3)); + } + + @Test + public void fibonacci() throws Exception { + Assert.assertArrayEquals(new int[]{}, ArrayUtil.fibonacci(1)); + Assert.assertArrayEquals(new int[]{1, 1}, ArrayUtil.fibonacci(2)); + Assert.assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, ArrayUtil.fibonacci(15)); + } + + @Test + public void getPrimes() throws Exception { + Assert.assertArrayEquals(new int[]{}, ArrayUtil.getPrimes(2)); + Assert.assertArrayEquals(new int[]{2}, ArrayUtil.getPrimes(3)); + Assert.assertArrayEquals(new int[]{2, 3}, ArrayUtil.getPrimes(4)); + Assert.assertArrayEquals(new int[]{2, 3, 5, 7, 11, 13, 17, 19}, ArrayUtil.getPrimes(23)); + } + + @Test + public void getPerfectNumbers() throws Exception { + Assert.assertArrayEquals(new int[]{6,28}, ArrayUtil.getPerfectNumbers(100)); +// System.out.println(Arrays.toString(ArrayUtil.getPerfectNumbers(40000000))); + } + + @Test + public void join() throws Exception { + Assert.assertEquals("", ArrayUtil.join(new int[]{}, "-")); + Assert.assertEquals("3", ArrayUtil.join(new int[]{3}, "-")); + Assert.assertEquals("3-8-9", ArrayUtil.join(new int[]{3, 8, 9}, "-")); + } +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/ArrayListTest.java b/group07/43819473/homework/src/test/java/dataStructure/ArrayListTest.java new file mode 100644 index 0000000000..02ea9fbb6e --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/ArrayListTest.java @@ -0,0 +1,79 @@ +package dataStructure; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zj on 2017/2/20. + */ +public class ArrayListTest { + ArrayList list =null; + + + @Before + public void setUp() throws Exception { + list = new ArrayList(); + for (int i = 0; i < 200; i++) { + list.add(i); + } + + System.out.println("=============================before=============================="); + for (int i = 0; i < list.size(); i++) { + try { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=" + list.get(i + 1)); + } catch (Exception e) { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=null"); + } finally { + + } + + } + } + + @After + public void tearDown() throws Exception { + System.out.println("=============================after=============================="); + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i) + ","); + } + } + + @Test + public void add() throws Exception { + } + + @Test + public void add1() throws Exception { + list.add(5, 555); + list.add(12, 1255); + } + + @Test + public void get() throws Exception { + + } + + @Test + public void remove() throws Exception { + list.remove(3); + list.remove(90); + } + + @Test + public void size() throws Exception { + } + + @Test + public void iterator() throws Exception { + + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + ","); + } + + System.out.println(); + System.out.println("---------------------------"); + } + +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/BinaryTreeTest.java b/group07/43819473/homework/src/test/java/dataStructure/BinaryTreeTest.java new file mode 100644 index 0000000000..90698681e7 --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/BinaryTreeTest.java @@ -0,0 +1,34 @@ +package dataStructure; + +import com.alibaba.fastjson.JSON; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by zj on 2017/2/26. + */ +public class BinaryTreeTest { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void insert() throws Exception { + BinaryTree tree=new BinaryTree(); + tree.insert(6); + tree.insert(5); + tree.insert(8); + tree.insert(3); + tree.insert(4); + System.out.println(JSON.toJSONString(tree.getRoot())); + } +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/LinkedListTest.java b/group07/43819473/homework/src/test/java/dataStructure/LinkedListTest.java new file mode 100644 index 0000000000..cc7e2bebcb --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/LinkedListTest.java @@ -0,0 +1,102 @@ +package dataStructure; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class LinkedListTest { + + LinkedList list = null; + + @Before + public void setUp() throws Exception { + list = new LinkedList(); + for (int i = 0; i < 6; i++) { + list.add(i); + } + System.out.println("=============================before=============================="); + for (int i = 0; i < list.size(); i++) { + try { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=" + list.get(i + 1)); + } catch (Exception e) { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=null"); + } finally { + + } + } + } + + @After + public void tearDown() throws Exception { + System.out.println("=============================after=============================="); + for (int i = 0; i < list.size(); i++) { + try { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=" + list.get(i + 1)); + } catch (Exception e) { + System.out.println("index=" + i + ",data=" + list.get(i) + ",next=null"); + } finally { + + } + + } + } + + @Test + public void testAdd() throws Exception { + list.add(300); + } + + @Test + public void testAdd1() throws Exception { + list.add(2, 100); + } + + @Test + public void testGet() throws Exception { + + } + + @Test + public void testRemove() throws Exception { + list.remove(3); + } + + @Test + public void testSize() throws Exception { + + } + + @Test + public void testAddFirst() throws Exception { + list.addFirst(66); + } + + @Test + public void testAddLast() throws Exception { + list.addLast(77); + } + + @Test + public void testRemoveFirst() throws Exception { + list.removeFirst(); + } + + @Test + public void testRemoveLast() throws Exception { + list.removeLast(); + } + + @Test + public void testIterator() throws Exception { + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + ","); + } + + System.out.println(); + System.out.println("---------------------------"); + } +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/QueueTest.java b/group07/43819473/homework/src/test/java/dataStructure/QueueTest.java new file mode 100644 index 0000000000..2d11213d42 --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/QueueTest.java @@ -0,0 +1,51 @@ +package dataStructure; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by LvZhenxing on 2017/2/22. + */ +public class QueueTest { + + Queue list = null; + + @Before + public void setUp() throws Exception { + list = new Queue(); + for (int i = 0; i < 5; i++) { + list.enQueue(i); + } + } + + @After + public void tearDown() throws Exception { + System.out.println("----------------tearDown------------------"); + int count=list.size(); + for (int i = 0; i < count; i++) { + System.out.println("list.deQueue():"+list.deQueue()); + } + } + + @Test + public void testEnQueue() throws Exception { + list.enQueue(11); + } + + @Test + public void testDeQueue() throws Exception { + System.out.println("list.deQueue():"+list.deQueue()); + System.out.println("list.deQueue():"+list.deQueue()); + } + + @Test + public void testIsEmpty() throws Exception { + System.out.println("testIsEmpty:"+list.isEmpty()); + } + + @Test + public void testSize() throws Exception { + System.out.println("testSize:"+list.size()); + } +} \ No newline at end of file diff --git a/group07/43819473/homework/src/test/java/dataStructure/StackTest.java b/group07/43819473/homework/src/test/java/dataStructure/StackTest.java new file mode 100644 index 0000000000..016bdb5811 --- /dev/null +++ b/group07/43819473/homework/src/test/java/dataStructure/StackTest.java @@ -0,0 +1,56 @@ +package dataStructure; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zj on 2017/2/24. + */ +public class StackTest { + Stack list = null; + + @Before + public void setUp() throws Exception { + list = new Stack(); + for (int i = 0; i < 5; i++) { + list.push(i); + } + } + + @After + public void tearDown() throws Exception { + System.out.println("----------------tearDown------------------"); + int count=list.size(); + for (int i = 0; i < count; i++) { + System.out.println("list.pop():"+list.pop()); + } + } + + @Test + public void push() throws Exception { + list.push(11); + } + + @Test + public void pop() throws Exception { + System.out.println("list.pop():"+list.pop()); + System.out.println("list.pop():"+list.pop()); + } + + @Test + public void peek() throws Exception { + System.out.println("list.peek():"+list.peek()); + } + + @Test + public void testIsEmpty() throws Exception { + System.out.println("testIsEmpty:"+list.isEmpty()); + } + + @Test + public void testSize() throws Exception { + System.out.println("testSize:"+list.size()); + } + +} \ No newline at end of file diff --git a/group07/476770768/MyDataStructure/.classpath b/group07/476770768/MyDataStructure/.classpath index 63b7e892d1..b387714202 100644 --- a/group07/476770768/MyDataStructure/.classpath +++ b/group07/476770768/MyDataStructure/.classpath @@ -2,5 +2,6 @@ + diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java index f0c1b3608c..5c8c3bb858 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java @@ -89,7 +89,7 @@ public boolean isFull(){ } public void checkBounds(int index){ - if(index >= size || index < 0){ + if(index > size || index < 0){ //System.out.println("From MyArrayList: Index out of bounds"); throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); } diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java new file mode 100644 index 0000000000..c7d9299934 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayListTest.java @@ -0,0 +1,69 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyArrayListTest { + + + @Test + public void testAddObject() { + MyArrayList mal = new MyArrayList(); + assertEquals(0, mal.size()); + mal.add(new Integer(1)); + assertEquals(1, mal.size()); + } + + @Test + public void testAddIntObject() { + MyArrayList mal = new MyArrayList(); + mal.add(0, new Integer(1)); + assertEquals(1, mal.size()); + int tmp = 0; + try { + mal.add(4, new Integer(4)); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + + } + + @Test + public void testGet() { + MyArrayList mal = new MyArrayList(); + mal.add(new Integer(1)); + assertEquals((Integer)mal.get(0),new Integer(1)); + int tmp = 0; + try { + mal.get(4); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testRemove() { + MyArrayList mal = new MyArrayList(); + mal.add(new Integer(1)); + assertEquals((Integer)mal.get(0),new Integer(1)); + assertEquals(mal.size(),1); + } + + @Test + public void testSize() { + MyArrayList mal = new MyArrayList(); + assertEquals(0, mal.size()); + } + + @Test + public void testIsEmpty() { + MyArrayList mal = new MyArrayList(); + assertTrue(mal.isEmpty()); + mal.add(new Integer(1)); + assertFalse(mal.isEmpty()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java index 297c97a3ed..3fe3693b19 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java @@ -38,9 +38,13 @@ public void add(int index, Object o) { */ public void addFirst(Object o) { Node tmp = new Node(o); - tmp.next = head; - head.prov = tmp; - head = tmp; + if(head == null){ + head = tmp; + }else{ + tmp.next = head; + head.prov = tmp; + head = tmp; + } } /** @@ -97,7 +101,10 @@ public Object remove(int index) { public Node removeFirst() { Node tmp = head; head = head.next; - head.prov = null; + if(head != null){ + head.prov = null; + } + return tmp; } @@ -199,22 +206,22 @@ public MyIterator iterator() { private class LinkedListIterator implements MyIterator{ private MyLinkedList eleIterator; - private Node pos; + private int pos; private LinkedListIterator(MyLinkedList mll){ this.eleIterator = mll; - this.pos = eleIterator.get(0); + this.pos = 0; } @Override public boolean hasNext() { - return pos != null; + return pos <= size; } @Override public Object next() { - Node res = pos; - pos = pos.next; + Node res = eleIterator.get(pos); + pos++; return res; } diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java new file mode 100644 index 0000000000..1da080a16a --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedListTest.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyLinkedListTest { + + @Test + public void testAddObject() { + MyLinkedList mll = new MyLinkedList(); + assertEquals(0, mll.size()); + mll.add(new Integer(1)); + assertEquals(1, mll.size()); + } + + @Test + public void testAddIntObject() { + MyLinkedList mll = new MyLinkedList(); + mll.add(0, new Integer(1)); + assertEquals(1, mll.size()); + int tmp = 0; + try { + mll.add(4, new Integer(4)); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testGet() { + MyLinkedList mll = new MyLinkedList(); + mll.add(new Object()); + assertNotNull(mll.get(0)); + int tmp = 0; + try { + mll.get(4); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + } + + @Test + public void testRemove() { + MyLinkedList mll = new MyLinkedList(); + mll.add(new Object()); + mll.remove(0); + assertEquals(mll.size(),0); + } + + @Test + public void testSize() { + MyLinkedList mll = new MyLinkedList(); + assertEquals(0, mll.size()); + } + + @Test + public void testIsEmpty() { + MyLinkedList mll = new MyLinkedList(); + assertTrue(mll.isEmpty()); + mll.add(new Object()); + assertFalse(mll.isEmpty()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java new file mode 100644 index 0000000000..3d80a95ee4 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyQueueTest.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class MyQueueTest { + + @Test + public void testEnQueue() { + MyQueue mq = new MyQueue(); + assertEquals(mq.size(), 0); + mq.enQueue(new Object()); + assertEquals(mq.size(), 1); + } + + @Test + public void testDeQueue() { + MyQueue mq = new MyQueue(); + int tmp = 0; + try { + mq.deQueue(); + } catch (IndexOutOfBoundsException e) { + tmp = 1; + assertEquals(tmp, 1); + } + mq.enQueue(new Object()); + assertNotNull(mq.deQueue()); + } + + @Test + public void testIsEmpty() { + MyQueue mq = new MyQueue(); + assertTrue(mq.isEmpty()); + mq.enQueue(new Object()); + assertFalse(mq.isEmpty()); + } + + @Test + public void testSize() { + MyQueue mq = new MyQueue(); + assertEquals(mq.size(), 0); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java index 3d9e1ef9a0..3c5b5a6b67 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java @@ -31,7 +31,7 @@ public Object peek(){ } public boolean isEmpty(){ - return top >= 0; + return top < 0; } public int size(){ diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java new file mode 100644 index 0000000000..0722e2d4fa --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/MyStackTest.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import java.util.EmptyStackException; + +import org.junit.Test; + +public class MyStackTest { + + @Test + public void testPush() { + MyStack ms = new MyStack(); + assertEquals(0, ms.size()); + ms.push(new Object()); + assertEquals(1, ms.size()); + } + + @Test + public void testPop() { + MyStack ms = new MyStack(); + ms.push(new Object()); + assertNotNull(ms.pop()); + assertEquals(0, ms.size()); + } + + @Test + public void testPeek() { + MyStack ms = new MyStack(); + int tmp = 0; + try { + ms.peek(); + } catch (EmptyStackException e) { + tmp = 1; + assertEquals(1, tmp); + } + ms.push(new Object()); + assertNotNull(ms.peek()); + assertEquals(1, ms.size()); + } + + @Test + public void testIsEmpty() { + MyStack ms = new MyStack(); + assertTrue(ms.isEmpty()); + ms.push(new Object()); + assertFalse(ms.isEmpty()); + } + + @Test + public void testSize() { + MyStack ms = new MyStack(); + assertEquals(0, ms.size()); + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java b/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java index 1ccabfc977..e75137744b 100644 --- a/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java +++ b/group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java @@ -3,21 +3,6 @@ public class testFile { public static void main(String[] args) { - MyLinkedList mll = new MyLinkedList(); - mll.add(new Integer(5)); - mll.add(new Integer(2)); - mll.add(new Integer(3)); - mll.add(new Integer(4)); - System.out.println(mll); - MyIterator mIt = mll.iterator(); - while(mIt.hasNext()){ - System.out.println(mIt.next()); - } - mll.remove(3); - System.out.println(mll); - - - } } diff --git a/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyArrayList.java b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyArrayList.java new file mode 100644 index 0000000000..fe9f8835f3 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyArrayList.java @@ -0,0 +1,131 @@ +package com.coding.refactor.List; + +public class MyArrayList implements MyList{ + private int size = 0; + private Object[] dataArray = new Object[0]; + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public void add(T o) { + ensureCapacity(size + 1); + dataArray[size++] = o; + } + + @Override + public void add(int index, T o) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + + ensureCapacity(size + 1); + System.arraycopy(dataArray, index, dataArray, index + 1, size - index); + dataArray[index] = o; + } + + @Override + public boolean contains(Object o) { + for(Object ele : dataArray){ + if(o.equals(ele)){ + return true; + } + } + return false; + } + + @Override + public Object[] toArray() { + Object[] newArray = new Object[size]; + System.arraycopy(dataArray, 0, newArray, 0, size); + return newArray; + } + + @Override + public boolean remove(T o) { + int index = indexOf(o); + if(index < 0){ + return false; + } + System.arraycopy(dataArray, index + 1, dataArray, index, size - 1 - index); + dataArray[--size] = null; + return true; + } + + @Override + public T remove(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + + T ele = (T)dataArray[index]; + System.arraycopy(dataArray, index, dataArray, index+1, size-1-index); + dataArray[--size] = null; + return ele; + } + + @Override + public T get(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + T ele = (T)dataArray[index]; + return ele; + } + + @Override + public T set(int index, T element) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + dataArray[index] = element; + return element; + } + + @Override + public void clear() { + for(int i=0; i iterator() { + // TODO Auto-generated method stub + return null; + } + + private void ensureCapacity(int minCapacity) { + if(minCapacity > dataArray.length){ + int newCapacity = Math.max(minCapacity, dataArray.length * 2); + Object[] newDataArray = new Object[newCapacity]; + System.arraycopy(dataArray, 0, newDataArray, 0, dataArray.length); + dataArray = newDataArray; + } + + } + + private String OutOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyIterator.java b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyIterator.java new file mode 100644 index 0000000000..04b3691d1c --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyIterator.java @@ -0,0 +1,12 @@ +package com.coding.refactor.List; + +/** + * + * @author nelson + * + */ +public interface MyIterator { + boolean hasNext(); + + T next(); +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyList.java b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyList.java new file mode 100644 index 0000000000..9daed38086 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyList.java @@ -0,0 +1,35 @@ +package com.coding.refactor.List; + +/** + * + * created by nelson on 2017/03/03 + * + */ +public interface MyList { + + int size(); + + boolean isEmpty(); + + void add(T o); + + void add(int index, T o); + + boolean contains(Object o); + + Object[] toArray(); + + boolean remove(T o); + + T remove(int index); + + T get(int index); + + T set(int index, T element); + + void clear(); + + int indexOf(T o); + + MyIterator iterator(); +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/refactor/List/TestFile.java b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/TestFile.java new file mode 100644 index 0000000000..dbd5317f9c --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/TestFile.java @@ -0,0 +1,11 @@ +package com.coding.refactor.List; + +public class TestFile { + + public static void main(String[] args) { + int[] a = new int[]{1,2,3,4,5}; + System.arraycopy(a, 1, a, 2, 3); + + } + +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/.classpath b/group07/476770768/Week2_HOMEWORK/Coderising/.classpath new file mode 100644 index 0000000000..b387714202 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/.gitignore b/group07/476770768/Week2_HOMEWORK/Coderising/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/.project b/group07/476770768/Week2_HOMEWORK/Coderising/.project new file mode 100644 index 0000000000..9b11b75eff --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/.project @@ -0,0 +1,17 @@ + + + Coderising + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/.settings/org.eclipse.jdt.core.prefs b/group07/476770768/Week2_HOMEWORK/Coderising/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/.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/group07/476770768/Week2_HOMEWORK/Coderising/src/com/Array/ArrayUtil.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/Array/ArrayUtil.java new file mode 100644 index 0000000000..521dbad798 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/Array/ArrayUtil.java @@ -0,0 +1,227 @@ +package com.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) { + if (origin == null) + return; + int head = 0; + int tail = origin.length - 1; + int tmp; + while (head < tail) { + tmp = origin[tail]; + origin[tail] = origin[head]; + origin[head] = tmp; + head++; + tail--; + } + } + + /** + * µһ飺 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 nonZeroNum = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + nonZeroNum++; + } + } + int cnt = 0; + int[] newArray = new int[nonZeroNum]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[cnt++] = oldArray[i]; + } + } + return newArray; + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int[] resultArray = new int[array1.length + array2.length]; + int cnt1 = 0; + int cnt2 = 0; + int cntResult = 0; + resultArray[cntResult++] = array1[0] array2[cnt2]){ + if(resultArray[cntResult-1] != array2[cnt2]){ + //array2[cnt2]еСûظ + //resultArray + resultArray[cntResult++] = array2[cnt2++]; + }else{ + //array2[cnt2]еСظ + cnt2++; + } + }else{ + if(resultArray[cntResult-1] != array1[cnt1]){ + //array1[cnt1]еСûظ + //resultArray + resultArray[cntResult++] = array1[cnt1++]; + }else{ + //array1[cnt1]еСظ + cnt1++; + } + } + } + + //ΪʣಿַresultArray + if(cnt1 == array1.length){ + //array2ʣ + while(cnt2 < array2.length){ + //array2ʣظؼresultArray + if(resultArray[cntResult-1] != array2[cnt2]){ + resultArray[cntResult++] = array2[cnt2++]; + }else{ + cnt2++; + } + } + }else{ + while(cnt1 < array1.length){ + //array1ʣظؼresultArray + if(resultArray[cntResult-1] != array1[cnt1]){ + resultArray[cntResult++] = array1[cnt1++]; + }else{ + cnt1++; + } + } + } + return Arrays.copyOf(resultArray, cntResult); + } + + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for(int i=0; i { + private Node head; + private Node current; + private int size = 0; + + public void add(T o){ + if(head == null){ + head = new Node(o); + current = head; + }else{ + Node tmp = new Node(o); + current.next = tmp; + current = tmp; + } + size++; + } + + public int[] toIntArray(){ + if(size == 0) return null; + int[] intArray = new int[this.size()]; + Node tmp = head; + for(int i=0; i get(int index){ + checkBounds(index); + if (index == 0) { + return head; + } else { + Node pos = findIndexPosition(index); + return pos; + } + } + public Object remove(int index){ + return null; + } + + public void remove(Node n){ + Node former = head; + while(former.next != n) former = former.next; + former.next = n.next; + size--; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + + } + + public void addLast(Object o){ + + } + public Node removeFirst(){ + if(head == null) return null; + Node tmp = head; + head = head.next; + this.size--; + return tmp; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + public void checkBounds(int index) { + if (index < 0 || index > size - 1) { + // System.out.println("From MyLinkedList: Index out of bounds"); + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + } + + public String OutOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + public Node findIndexPosition(int index) { + Node pos = head; + for (int i = 0; i < index; i++) { + pos = pos.next; + } + return pos; + } + + + private static class Node{ + T data; + Node next; + + public Node(T o){ + this.data = o; + this.next = null; + } + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(head == null) return; + Node newHead = head; + head = head.next; + newHead.next = null; + while(head != null){ + Node tmp = head; + head = head.next; + tmp.next = newHead; + newHead = tmp; + } + head = newHead; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int midLen = this.size()/2; + for(int i=0; i= size) return; + if(length < 0 ||length >= size) return; + if(i == 0){ + for(int j=0; j startNode = head; + for(int j=0; j pointer = startNode; + while(cnt101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + Iterator it = list.iterator(); + int[] restArray = new int[list.size()]; + int cnt = 0; + while(it.hasNext()){ + int index = it.next(); + restArray[cnt++] = (int)get(index).data; + } + return restArray; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(head == null) return; + Node cur = head.next; + Node former = head; + while(cur != null){ + if(former.data.equals(cur.data)){ + remove(cur); + cur = cur.next; + }else{ + former = former.next; + cur = cur.next; + } + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if((int)head.data > min){ + head = findMaxLocation(head, max); + }else{ + Node former = head; + while((int)former.data < min && former != null){ + former = former.next; + } + Node behind = findMaxLocation(former.next, max); + former.next = behind; + + } + } + + public Node findMaxLocation(Node n, int max){ + Node tmp = n; + while((int)tmp.data < max && tmp != null){ + tmp = tmp.next; + size--; + } + return tmp; + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection(LinkedList list){ + if(head == null) return list; + + return null; + } +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtilTest.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtilTest.java new file mode 100644 index 0000000000..66302fec61 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtilTest.java @@ -0,0 +1,139 @@ +package com.LinkedList; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListUtilTest { + LinkedListUtil llu; + @Before + public void setUp() throws Exception { + llu = new LinkedListUtil(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverse() { + llu.reverse(); + assertArrayEquals(null, llu.toIntArray()); + llu.add(3); + llu.add(7); + llu.add(10); + llu.reverse(); + assertArrayEquals(new int[]{10,7,3}, llu.toIntArray()); + } + + @Test + public void testRemoveFirstHalf() { + llu.removeFirstHalf(); + assertArrayEquals(null, llu.toIntArray()); + llu.add(2); + llu.add(5); + llu.add(7); + llu.add(8); + llu.removeFirstHalf(); + assertArrayEquals(new int[]{7,8}, llu.toIntArray()); + llu = new LinkedListUtil(); + llu.add(2); + llu.add(5); + llu.add(7); + llu.add(8); + llu.add(10); + llu.removeFirstHalf(); + assertArrayEquals(new int[]{7,8,10}, llu.toIntArray()); + + } + + @Test + public void testRemoveIntInt() { + llu.add(2); + llu.add(5); + llu.add(7); + llu.add(8); + llu.remove(1, 2); + assertArrayEquals(new int[]{2,8}, llu.toIntArray()); + } + + @Test + public void testGetElements() { + llu.add(11); + llu.add(101); + llu.add(201); + llu.add(301); + llu.add(401); + llu.add(501); + llu.add(601); + llu.add(701); + llu.add(801); + LinkedList l = new LinkedList(); + l.add(1); + l.add(3); + l.add(4); + l.add(6); + assertArrayEquals(new int[]{101,301,401,601}, llu.getElements(l)); + } + + @Test + public void testSubtract() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveDuplicateValues() { + llu.add(1); + llu.add(1); + llu.add(2); + llu.add(3); + llu.add(3); + llu.add(5); + llu.add(6); + llu.add(6); + llu.add(8); + llu.removeDuplicateValues(); + assertArrayEquals(new int[]{1,2,3,5,6,8}, llu.toIntArray()); + + } + + @Test + public void testRemoveRange() { + llu.add(1); + llu.add(1); + llu.add(2); + llu.add(3); + llu.add(4); + llu.add(5); + llu.add(6); + llu.add(6); + llu.add(8); + llu.removeRange(2, 5); + assertArrayEquals(new int[]{1,1,2,5,6,6,8}, llu.toIntArray()); + llu = new LinkedListUtil(); + llu.add(2); + llu.add(2); + llu.add(2); + llu.add(3); + llu.add(4); + llu.add(5); + llu.add(6); + llu.add(6); + llu.add(8); + llu.removeRange(1, 5); + assertArrayEquals(new int[]{5,6,6,8}, llu.toIntArray()); + + } + + @Test + public void testIntersection() { + fail("Not yet implemented"); + } + +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/LoginAction.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..d18ebabe40 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author chenran + * + */ +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/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/Struts.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/Struts.java new file mode 100644 index 0000000000..1715392c91 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/Struts.java @@ -0,0 +1,297 @@ +package com.litestruts; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class Struts { + public static View runAction(String actionName, Map parameters) { + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + try { + String path = "./src/com/litestruts/struts.xml"; + Document document = readStruts(path); + NodeList actionList = document.getElementsByTagName("action"); + String actionClassName = getActionClassName(actionName, actionList); + //System.out.println(actionClassName); + Class c = Class.forName(actionClassName); + Object o = c.newInstance(); + String result = executeAction(c, o, actionClassName, parameters); + String message = getMessage(c, o); + String jsp = getJSP(document, result, actionName, actionList); + Map m = new HashMap(); + m.put("message", message); + View view = new View(); + view.setJsp(jsp); + view.setParameters(m); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + return null; + } + + /** + * get jsp message + * @param document + * @param result + * @param actionName + * @param actionList + * @return + */ + private static String getJSP(Document document, String result, String actionName, NodeList actionList) { + Node actionNode = getactionNode(actionName, actionList); + NodeList childNodes = actionNode.getChildNodes(); + String jsp = getContent(result, childNodes); + return jsp; + } + + /** + * + * @param result + * @param nodes: nodelist of childNode for action node + * @return + */ + private static String getContent(String result, NodeList nodes) { + String jsp; + if(result.equals("success")){ + jsp = getResultNodeForSuccess(nodes); + }else{ + jsp = getResultNode(result, nodes); + } + return jsp; + } + + /** + * get the certain result node while result is success + * @param nodes + * @return + */ + private static String getResultNodeForSuccess(NodeList nodes) { + for(int i=0; i parameters){ + try { + + setVar(c, o, parameters); + Method m = c.getMethod("execute"); + String message = (String)m.invoke(o); + return message; + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + + /** + * set variables of action class instance + * @param c + * @param o + * @param parameters + */ + public static void setVar(Class c, Object o, Map parameters){ + Method[] methods = c.getMethods(); + Iterator> entries = parameters.entrySet().iterator(); + while(entries.hasNext()){ + Map.Entry entry = entries.next(); + String methodName = "set" + entry.getKey(); + for(Method m : methods){ + if(m.getName().equalsIgnoreCase(methodName)){ + try { + m.invoke(o, entry.getValue()); + //System.out.println(m.getName()); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + } +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/StrutsTest.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8c8fdeda8d --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +package com.litestruts; + +import org.junit.Test; +import java.util.HashMap; +import java.util.Map; +import org.junit.Assert; + +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/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/View.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/View.java new file mode 100644 index 0000000000..a7494563ef --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/View.java @@ -0,0 +1,23 @@ +package com.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/struts.xml b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/struts.xml new file mode 100644 index 0000000000..daef9740c1 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNodeTest.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNodeTest.java index 9766b03947..73f3a86b64 100644 --- a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNodeTest.java +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyBinaryTreeNodeTest.java @@ -1,8 +1,11 @@ package com.coding2017.group7.homework.c0226; import org.junit.Assert; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runners.MethodSorters; +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MyBinaryTreeNodeTest { diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedList.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedList.java index 6b8a9ed197..53948a1c07 100644 --- a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedList.java +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedList.java @@ -1,5 +1,9 @@ package com.coding2017.group7.homework.c0226; +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MyLinkedList implements MyList { private Node head = new Node(); @@ -96,6 +100,7 @@ private void checkRangeAdd(int index) { throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); } } + private Node find(int index) { Node node = head; int pos = -1; diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedListTest.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedListTest.java index fe7d9ba28b..0fee275cbb 100644 --- a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedListTest.java +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyLinkedListTest.java @@ -3,8 +3,11 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runners.MethodSorters; +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MyLinkedListTest { private MyLinkedList myList = new MyLinkedList(); diff --git a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStackTest.java b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStackTest.java index d29031be60..57c1ab7a57 100644 --- a/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStackTest.java +++ b/group07/562247675/homework/homework-0226/src/main/java/com/coding2017/group7/homework/c0226/MyStackTest.java @@ -3,8 +3,11 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runners.MethodSorters; +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class MyStackTest { private MyStack myStack = new MyStack(); private Object[] elements = new Object[]{1}; diff --git a/group07/562247675/homework/homework-0226/src/main/resources/readme.md b/group07/562247675/homework/homework-0226/src/main/resources/readme.md index e69de29bb2..a99a72ae0c 100644 --- a/group07/562247675/homework/homework-0226/src/main/resources/readme.md +++ b/group07/562247675/homework/homework-0226/src/main/resources/readme.md @@ -0,0 +1,21 @@ +# CPU、内存、硬盘、指令之间的关系 +  以文件复制为例,简单描述四者之间的协同工作。 + +  日常生活中,我们经常会在电脑磁盘之间的复制粘粘文件,是怎么实现的呢?粗读了《程序是怎么跑起来的》,这里简单描述下这个过程。 + +  当我们按下 Ctrl + C 时,CPU 执行 “复制指令” 将硬盘文件的起止地址临时放到内存中,这时内存并没有读取磁盘的所有文件数据。 + +  当我们按下 Ctrl + V 时,CPU 执行 “粘粘指令” 才会真正去拷贝文件,将文件起止地址取出,读取硬盘起止地址的数据到内存中,将内存中的数据,写入到硬盘新的起止地址里。 +   +  这个过程看似简单,但是却有几个地方需要仔细去思考的,下面是我自己的几个自问自答。 + +- 如果文件有 100GB,而内存只有 8GB,内存会不会爆掉? +不会,CPU “复制指令”执行时,总是会先将磁盘的文件读取到内存的缓存区,再从缓存区写入到硬盘中。因为内存的读写速度,是硬盘的好几十万倍(内存读写速率毫微秒级别,硬盘读写速率毫秒级别)所以不会出现硬盘等待内存读取数据再写入硬盘的情况,看到的进度条,也只是反映硬盘的写入速率。 + +- 既然内存读写速率这么快,那硬盘是不是可以被内存替代? +不会,硬盘读写速率慢,但数据写入到硬盘后,是持久化存在的,不会因为关机而导致数据丢失,再次开机时硬盘数据还在。 +但是,内存是带电存储的,虽然读写速率快,但是一旦关机失去电荷,内存中所有的数据将会丢失,内存也不会被硬盘所替代。 +同时,内存容量的造价,也比硬盘要昂贵的多。单个硬盘容量超过 1TB 的很常见,但是单个内存容量超过 64GB 的,生活中是很少见的。 + +- “复制指令” 和 “拷贝指令” 在 CPU 内部是真实存在的吗? +显然不是,这里描述的这两个 “指令”,其实是 CPU 内部多种指令集协同工作完成的,并不存在这样的单独指令。 \ No newline at end of file diff --git a/group07/562247675/homework/homework-0305/pom.xml b/group07/562247675/homework/homework-0305/pom.xml new file mode 100644 index 0000000000..655bab68b6 --- /dev/null +++ b/group07/562247675/homework/homework-0305/pom.xml @@ -0,0 +1,26 @@ + + + + com.coding2017.group7 + homework + 1.0-SNAPSHOT + + 4.0.0 + + com.coding2017.group7 + homework-0305 + + + junit + junit + + + dom4j + dom4j + + + + \ No newline at end of file diff --git a/group07/562247675/homework/homework-0305/src/main/java/com/coderising/array/MyArrayUtil.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/array/MyArrayUtil.java new file mode 100644 index 0000000000..6e60b10eb9 --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/array/MyArrayUtil.java @@ -0,0 +1,267 @@ +package com.coderising.array; + +import java.util.Arrays; + +public final class MyArrayUtil { + + public static void main(String[] args) { + int[] reverse1 = {7, 9, 30, 3}; + int[] reverse2 = {7, 9, 30, 3, 4}; + reverseArray(reverse1); + reverseArray(reverse2); + System.out.println("reverseArray: " + Arrays.toString(reverse1) + " " + Arrays.toString(reverse2)); + int[] removeZero = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + System.out.println("removeZero: " + Arrays.toString(removeZero(removeZero))); + int[] merge1 = {3, 5, 7, 8}; + int[] merge2 = {4, 5, 6, 7}; + System.out.println("merge: " + Arrays.toString(merge(merge1, merge2))); + System.out.println("merge: " + Arrays.toString(merge(merge2, merge1))); + int[] grow = {2, 3, 6}; + System.out.println("grow: " + Arrays.toString(grow(grow, 3))); + System.out.println("fibonacci: f(15)=" + Arrays.toString(fibonacci(15)) + " f(1)=" + Arrays.toString(fibonacci(1))); + System.out.println("getPrimes: g(23)=" + Arrays.toString(getPrimes(23))); + System.out.println("getPerfectNumbers: g(500)=" + Arrays.toString(getPerfectNumbers(500))); + System.out.println("getPerfectNumbers: j(f(50))=" + join(fibonacci(50), "-")); + + } + + /** + * 给定一个整形数组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 len = origin.length; + for (int i = 0; i < len / 2; i++) { + int j = len - 1 - i; + int temp = origin[i]; + origin[i] = origin[j]; + origin[j] = 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) { + int length = oldArray.length; + // 空间换时间复杂度 O(2*n-m) n为数组长度 m为0的个数 + int[] src = new int[length]; + int last = 0; + for (int i = 0; i < length; i++) { + int m = oldArray[i]; + if (m != 0) { + src[last++] = m; + } + } + int[] dest = new int[last]; + System.arraycopy(src, 0, dest, 0, last); + return dest; + } + + /** + * 给定两个已经排序好的整形数组, 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[] src = new int[array1.length + array2.length]; + int i = 0, j = 0, k = 0; //i=数组1游标 j=数组2游标 k=目标数组下标 + // 时间复杂度 O(n+m-k) n为数组1长度 m为数组2长度 k为重复数组个数 + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) { //大小比较入目标数组 + src[k++] = array1[i++]; + } else if (array1[i] > array2[j]) { + src[k++] = array2[j++]; + } else { //等值同时移动,但只入1个目标值 + src[k++] = array1[i++]; + j++; + } + } + // 只要1个数组下表越界,就直接添加另1个数组尾部所有有序值 + while (i < array1.length) { + src[k++] = array1[i++]; + } + while (j < array2.length) { + src[k++] = array2[j++]; + } + int[] dest = new int[k]; + System.arraycopy(src, 0, dest, 0, k); + return dest; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max < 2) { // 额外出口 f(1) = [] + return new int[]{}; + } + if (max == 2) { //递归出口 f(2) = [1, 1] + return new int[]{1, 1}; + } + // f(3) = [1,1,2] + // f(5) = f(4) = [1,1,2,3] + // f(8) = f(7) = f(6) = [1,1,2,3,5] + + // 递归拿上个数列最后的两个数 + int[] src = fibonacci(max - 1); + // 判断这两个数的和值是否在max范围内 + int m = src[src.length - 1] + src[src.length - 2]; + if (m < max) { + int[] dest = new int[src.length + 1]; + System.arraycopy(src, 0, dest, 0, src.length); + dest[src.length] = m; + return dest; + } else { + return src; + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max <= 2) { //特殊出口 g(1) = [] + return new int[]{}; + } + if (max == 3) { //递归出口 g(3) = [2] + return new int[]{2}; + } + // g[3] = [2] + // g[5] = g[4] = [2,3] + // g[7] = g[6] = [2,3,5] + // g[11] = g[10] = g[9] = g[8] = [2,3,5,7] + + // 递归拿上个数组最后的素数 + int[] src = getPrimes(max - 1); + // 找上个素数和最大值之间的素数 + int before = src[src.length - 1]; + int next = before; + int start = 1 + before; + int end = max; + for (int num = start; num < end; num++) { + int i = 2; + for (; i <= num / 2; i++) { + if (num % i == 0) { + break; + } + } + if (i > num / 2) { + next = num; + break; + } + } + // 判断这个值是否存在 + if (next > before) { + int[] dest = new int[src.length + 1]; + System.arraycopy(src, 0, dest, 0, src.length); + dest[src.length] = next; + return dest; + } else { + return src; + } + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max <= 6) { //特殊出口 + return new int[]{}; + } + if (max == 7) { //递归出口 + return new int[]{6}; + } + // 递归拿上个数组最后的完数 + int[] src = getPerfectNumbers(max - 1); + // 找上个完数和最大值之间的完数 + int before = src[src.length - 1]; + int next = before; + int start = 1 + before; + int end = max; + for (int num = start; num < end; num++) { + int count = 0; + for (int i = 1; i <= num / 2; i++) { + if (num % i == 0) { + count += i; + } + } + if (count == num) { + next = num; + break; + } + } + // 判断这个值是否存在 + if (next > before) { + int[] dest = new int[src.length + 1]; + System.arraycopy(src, 0, dest, 0, src.length); + dest[src.length] = next; + return dest; + } else { + return src; + } + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + StringBuilder sBuilder = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sBuilder.append(array[i]).append(seperator); + } + if (sBuilder.length() > 0) { + sBuilder.deleteCharAt(sBuilder.length() - 1); + } + return sBuilder.toString(); + } + + +} diff --git a/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/LoginAction.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..bf31612b74 --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +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/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStruts.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStruts.java new file mode 100644 index 0000000000..74354e8bce --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStruts.java @@ -0,0 +1,200 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + + +public class MyStruts { + + public static View runAction(String actionName, Map parameters) { + MyStrutsActioNode actioNode = strutsActions.get(actionName); + String actionString = actioNode.actionClass; + Class actionClass = null; + Object actionBean = null; + try { + actionClass = Class.forName(actionString); + actionBean = actionClass.newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + if (actionBean == null) return null; // 没有默认构造或没有找到字节码的情况 + Map setterMethods = getSetterMethods(actionClass); + // #1.调用action中的set方法设置值 + if (parameters != null && !parameters.isEmpty()) { + for (Map.Entry entry : parameters.entrySet()) { + String fieldName = entry.getKey(); + String fieldValue = entry.getValue(); + Method setterMethod = setterMethods.get(fieldName); + if (setterMethod != null) { + try { + setterMethod.invoke(actionBean, fieldValue); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + // #2.调用action的execute方法 + Method method = getExecuteMethod(actionClass); + if (method == null) return null; // 未声明execute方法 + String jspName = ""; + try { + Object object = method.invoke(actionBean); // 获取execute方法返回值 + if (object instanceof String) { + jspName = (String) object; + + } + } catch (Exception e) { + e.printStackTrace(); + } + // #3.查找execute方法返回值对应的jsp路径 + View view = new View(); + String jspValue = actioNode.actionResults.get(jspName).value; + view.setJsp(jspValue); + + // #4.获取action中的get方法返回值封装到view对象中 + Map getterMethods = getGetterMethods(actionClass); + if (getterMethods != null && !getterMethods.isEmpty()) { + HashMap resultMap = new HashMap(getterMethods.size()); + for (Map.Entry entry : getterMethods.entrySet()) { + String fieldName = entry.getKey(); + Method getterMethod = entry.getValue(); + try { + Object object = getterMethod.invoke(actionBean); + resultMap.put(fieldName, object); + } catch (Exception e) { + e.printStackTrace(); + } + } + view.setParameters(resultMap); + } + return view; + } + + private static Method getExecuteMethod(Class beanClass) { + Method method = null; + try { + method = beanClass.getMethod("execute"); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + return method; + } + + private static Map getGetterMethods(Class beanClass) { + Map resultMap = Collections.emptyMap(); + Map descriptorMap = getPropertyDescriptors(beanClass); + if (descriptorMap != null && !descriptorMap.isEmpty()) { + resultMap = new HashMap(descriptorMap.size() / 2); + for (PropertyDescriptor descriptor : descriptorMap.values()) { + String fieldName = descriptor.getName(); + Method getterMethod = descriptor.getReadMethod(); + resultMap.put(fieldName, getterMethod); + } + } + return resultMap; + } + + private static Map getSetterMethods(Class beanClass) { + Map resultMap = Collections.emptyMap(); + Map descriptorMap = getPropertyDescriptors(beanClass); + if (descriptorMap != null && !descriptorMap.isEmpty()) { + resultMap = new HashMap(descriptorMap.size() / 2); + for (PropertyDescriptor descriptor : descriptorMap.values()) { + String fieldName = descriptor.getName(); + Method setterMethod = descriptor.getWriteMethod(); + resultMap.put(fieldName, setterMethod); + } + } + return resultMap; + } + + private static Map getPropertyDescriptors(Class beanClass) { + Map resultMap = Collections.emptyMap(); + try { + BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + resultMap = new HashMap(propertyDescriptors.length); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + String fieldName = propertyDescriptor.getName(); + resultMap.put(fieldName, propertyDescriptor); + } + } catch (IntrospectionException e) { + e.printStackTrace(); + } + return resultMap; + } + + private static Map strutsActions; + + static { + InputStream input = null; + try { + // dom4j解析xml文件封装到JavaBean + input = MyStruts.class.getClassLoader().getResourceAsStream("struts.xml"); + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read(input); + strutsActions = new HashMap(); + Element root = document.getRootElement(); + // 标签 + for (Iterator i = root.elementIterator("action"); i.hasNext(); ) { + Element actionXmlNode = (Element) i.next(); + MyStrutsActioNode actionNode = new MyStrutsActioNode(); + actionNode.actionName = actionXmlNode.attributeValue("name"); + actionNode.actionClass = actionXmlNode.attributeValue("class"); + strutsActions.put(actionNode.actionName, actionNode); + actionNode.actionResults = new HashMap(); + // 标签 + for (Iterator j = actionXmlNode.elementIterator("result"); j.hasNext(); ) { + Element resultXmlNode = (Element) j.next(); + MyActionResultNode resultNode = new MyActionResultNode(); + resultNode.name = resultXmlNode.attributeValue("name"); + resultNode.value = resultXmlNode.getStringValue(); + actionNode.actionResults.put(resultNode.name, resultNode); + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + // close quietly.. + } + } + } + } + + public static void main(String[] args) { + + } + + private static class MyStrutsActioNode { + private String actionName; + private String actionClass; + private Map actionResults; + + } + + private static class MyActionResultNode { + private String name; + private String value; + + } + +} diff --git a/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStrutsTest.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStrutsTest.java new file mode 100644 index 0000000000..a7e6b63019 --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class MyStrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + + View view = MyStruts.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 = MyStruts.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/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/View.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..ce596da106 --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +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/group07/562247675/homework/homework-0305/src/main/resources/struts.xml b/group07/562247675/homework/homework-0305/src/main/resources/struts.xml new file mode 100644 index 0000000000..8a9789665d --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group07/562247675/homework/pom.xml b/group07/562247675/homework/pom.xml index 17a2c5f0fd..343c7dbe71 100644 --- a/group07/562247675/homework/pom.xml +++ b/group07/562247675/homework/pom.xml @@ -12,6 +12,7 @@ homework-0226 + homework-0305 pom @@ -22,11 +23,24 @@ + junit junit 4.12 + + + dom4j + dom4j + 1.6.1 + + + + jaxen + jaxen + 1.1.6 + diff --git a/group07/724319952/src/cn/fyl/first/ArrayList.java b/group07/724319952/src/cn/fyl/first/ArrayList.java new file mode 100644 index 0000000000..54d9d609b1 --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/ArrayList.java @@ -0,0 +1,107 @@ +package cn.fyl.first; + +/* + * 动态数组 + */ +public class ArrayList implements List { + + // 记录数组大小 + private int size = 0; + + // 设数组容量为3 + private Object[] elementData = new Object[3]; + + // 扩容 + public void dilatation(int newCapacity) { + + if (newCapacity < size) { + return; + } + + Object[] old = elementData; + elementData = new Object[newCapacity]; + for (int i = 0; i < size; i++) { + elementData[i] = old[i]; + } + + } + + // 插入值 + public void add(Object o) { + add(size(), o); + } + + // 按索引插入值 + public void add(int index, Object o) { + if (elementData.length == size()) { + dilatation(size() * 2 + 1); + } + // 插入的位置小于数组大小,将index位置的值依次向后退一位 + for (int i = size; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = o; + size++; + + } + + // 取值 + public Object get(int index) { + + if (index > size) { + return null; + } else { + return elementData[index]; + } + + } + + // 按索引删除该值 + public Object remove(int index) { + Object o = elementData[index]; + // 删除的位置小于数组大小,将index位置后面的值依次向前挪一位 + for (int i = index; i < elementData.length - 1; i++) { + elementData[i] = elementData[i + 1]; + } + size--; + return o; + } + + // 返回数组大小 + public int size() { + return size; + } + + //迭代器 + class MyIterator implements Iterator { + int current = 0; + + public boolean hasNext() { + return current < size; + } + + public Object next() { + if (hasNext()) + return elementData[current++]; + else + throw new java.util.NoSuchElementException(); + } + + } + + public static void main(String[] args) { + ArrayList a = new ArrayList(); + a.add(0, 1); + a.add(2); + a.add(3); + a.add(4); + a.add(1, 0); + a.remove(3); + ArrayList.MyIterator m = a.new MyIterator(); + System.err.println(a.elementData.length); + while(m.hasNext()){ + System.out.print(m.next()+" "); + } + } + +} diff --git a/group07/724319952/src/cn/fyl/first/BinaryTreeNode.java b/group07/724319952/src/cn/fyl/first/BinaryTreeNode.java new file mode 100644 index 0000000000..5b136abccc --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/BinaryTreeNode.java @@ -0,0 +1,68 @@ +package cn.fyl.first; + +public class BinaryTreeNode { + private Object data; //保存数据 + private BinaryTreeNode left; //左子树 + private BinaryTreeNode right; //右子树 + private BinaryTreeNode root; //根节点 + + public BinaryTreeNode getRoot() { + return root; + } + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + //插入功能,插入的值按比父类结点的值大,插入右子树;小,插入左子树 + public BinaryTreeNode insert(Object o,BinaryTreeNode t){ + if(t == null){ + BinaryTreeNode temp = new BinaryTreeNode(); //新建插入值的结点 + temp.setData(o); + return temp; + } + boolean temp = (int)o > (int)t.getData(); //暂存插入的值跟结点的值比较大小结果 + if(temp){ //ture时(插入的值大于结点的值大),所以插到右子树 + System.err.println(temp); + t.right =insert(o, t.right); + } + else{ + System.out.println(temp); + t.left = insert(o, t.left); + } + return t; + } + + public static void main(String[] args) { + BinaryTreeNode root = new BinaryTreeNode(); + BinaryTreeNode left1 = new BinaryTreeNode(); + BinaryTreeNode right1 = new BinaryTreeNode(); + BinaryTreeNode left2 = new BinaryTreeNode(); + BinaryTreeNode left3 = new BinaryTreeNode(); + root.setData(5); + root.setLeft(left1); left1.setData(2); + root.setRight(right1); right1.setData(7); + left1.setLeft(left2); left2.setData(1); + right1.setLeft(left3); left3.setData(6); + BinaryTreeNode temp = root.insert(4, left1); + System.out.println(left1.getRight().getData()); + } + +} diff --git a/group07/724319952/src/cn/fyl/first/Iterator.java b/group07/724319952/src/cn/fyl/first/Iterator.java new file mode 100644 index 0000000000..7b907d4137 --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/Iterator.java @@ -0,0 +1,7 @@ +package cn.fyl.first; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); +} diff --git a/group07/724319952/src/cn/fyl/first/LinkedList.java b/group07/724319952/src/cn/fyl/first/LinkedList.java new file mode 100644 index 0000000000..e6e5fd71b8 --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/LinkedList.java @@ -0,0 +1,163 @@ +package cn.fyl.first; + +public class LinkedList implements List { + + private Node head,tail; //头尾结点 + private int size; //保存链表大小 + + //将值插入链表 + public void add(Object o) { + add(size(),o); + } + + //将值从index位置插入链表 + public void add(int index, Object o) { + if(index == 0){ + addFirst(o); + } + else if(index >= size){ + addLast(o); + } + else{ + Node current = head; + for (int i = 1; i < index; i++) { + current = current.next; + } + Node temp = current.next; + current.next = new Node(o); + (current.next).next = temp; + size++; + } + } + + //取出index位置的值 + public Object get(int index) { + if(index < 0 || index >=size){ + return null; + } + else if(index == 0){ + return head.data; + } + else if(index == size-1){ + return tail.data; + } + else{ + Node current = head; + for (int i = 1; i < index; i++) { + current = current.next; + } + Node temp = current.next; + return temp.data; + } + } + + //删除index位置的值 + public Object remove(int index) { + if(index < 0 || index >size) + return null; + else if(index ==0) + return head.data; + else if(index == size -1) + return tail.data; + else{ + Node previous = head; + for (int i = 1; i < index; i++) { + previous = previous.next; + } + Node current = previous.next; + previous.next = current.next; + size--; + return current.data; + } + } + + public int size() { + return size; + } + + //添加头结点 + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.next = head; //指向头引用所指结点 + head = newNode; //头引用指向新增结点 + size++; + if(tail == null){ + tail = head; + } + } + + //添加尾结点 + public void addLast(Object o) { + Node newNode = new Node(o); + if(tail == null){ + head = tail = newNode; + } + else{ + tail.next = newNode; + tail = tail.next; + } + size++; + } + + //删除头结点 + public Object removeFirst(){ + if(size == 0){ + return null; + } + else if(size == 1){ + Node temp = head; + head = tail = null; + size = 0; + return temp.data; + } + else{ + Node temp = head; + head = head.next; + size--; + return temp.data; + } + } + + //删除尾结点 + public Object removeLast() { + if(size == 0){ + return null; + } + else if(size ==1){ + Node temp = head; + head =tail =null; + size =0; + return temp.data; + } + else{ + Node current = head; + for (int i = 0; i < size - 2; i++) { + current = current.next; + } + Node temp =tail; + tail = current; + tail.next = null; + size--; + return temp.data; + } + } + + + + private static class Node { + Object data; + Node next; + public Node(Object o){ + data = o; + } + } + + public static void main(String[] arg){ + LinkedList l = new LinkedList(); + l.add(0); + l.add(2); + l.add(4); + l.add(3, 1); + System.out.println(l.removeLast()+" "+l.removeFirst()+" "+l.get(0)+" "+l.get(1)); + } +} diff --git a/group07/724319952/src/cn/fyl/first/List.java b/group07/724319952/src/cn/fyl/first/List.java new file mode 100644 index 0000000000..45a4d4652e --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/List.java @@ -0,0 +1,11 @@ +package cn.fyl.first; + +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/group07/724319952/src/cn/fyl/first/Queue.java b/group07/724319952/src/cn/fyl/first/Queue.java new file mode 100644 index 0000000000..1e9e5ef3ad --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/Queue.java @@ -0,0 +1,37 @@ +package cn.fyl.first; + +public class Queue { + + LinkedList linkedlist = new LinkedList(); + + public void enQueue(Object o){ + linkedlist.addLast(o); + } + + public Object deQueue(){ + return linkedlist.removeFirst(); + } + + public boolean isEmpty(){ + if(linkedlist.size()>0) + return false; + else + return true; + } + + public int size(){ + return linkedlist.size(); + } + + public Object get(int index){ + return linkedlist.get(index); + } + + public static void main(String[] arg){ + Queue q = new Queue(); + q.enQueue(1); + q.enQueue(2); + System.out.println(q.get(1)); + } + +} diff --git a/group07/724319952/src/cn/fyl/first/Stack.java b/group07/724319952/src/cn/fyl/first/Stack.java new file mode 100644 index 0000000000..368178691f --- /dev/null +++ b/group07/724319952/src/cn/fyl/first/Stack.java @@ -0,0 +1,39 @@ +package cn.fyl.first; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + int last; + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + return elementData.remove(last-1); + } + + public Object peek() { + last = elementData.size()-1; + return elementData.get(last); + } + + public boolean isEmpty() { + if(elementData.size() > 0) + return false; + else + return true; + } + + public int size() { + return elementData.size(); + } + + public static void main(String[] args) { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + System.out.println(s.peek()); + } +} diff --git a/group07/724319952/src/cn/fyl/second/ArrayUtil.java b/group07/724319952/src/cn/fyl/second/ArrayUtil.java new file mode 100644 index 0000000000..a816ff4acd --- /dev/null +++ b/group07/724319952/src/cn/fyl/second/ArrayUtil.java @@ -0,0 +1,235 @@ +package cn.fyl.second; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + //保存置换后的数组 + int[] temp = new int[origin.length]; + + for (int i = 0,j = origin.length - 1; i < origin.length; i++,j--) { + //依次将origin的值赋给与temp前后对应位置上 + temp[i] = origin[j]; + System.out.print(temp[i]+" "); + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + //保存oldArray数组去0后的容量 + int size = 0; + + for (int i = 0; i < oldArray.length; i++) { + int temp = oldArray[i]; + if(temp != 0){ + size++; + } + } + + //初始化与oldArray数组去0后容量一样的新数组 + int[] newArray = new int[size]; + + for (int i = 0,j = 0; i < oldArray.length; i++) { + int temp = oldArray[i]; + //将不等于0的值赋给新数组 + if(temp != 0){ + newArray[j] = temp; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, + * 并且仍然是有序的。 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + //保存新数组的容量 + int size = array1.length + array2.length; + + int[] newArray = new int[size]; + + //先将array1数组存入temp数组 + for (int i = 0; i < array1.length; i++) { + newArray[i] = array1[i]; + } + + //增0,若相同的值是数组中倒数的二个的值,则需要一个零将最后一个覆盖掉 + array2 = grow(array2, 1); + + //将array1数组中每一个值依次与array2数组中每一个进行比较,若相同,则将相同的值覆盖掉 + for (int i = 0; i < array1.length; i++) { + for (int j = 0; j < array2.length; j++) { + if(array1[i] == array2[j]){ + for (int k = j; k < array2.length - 1; k++) { + array2[k] = array2[k + 1]; + } + } + } + } + //去0 + array2 = removeZero(array2); + + //将array2的值插入newArray数组中 + for (int i = 0; i < array2.length; i++) { + newArray[array1.length] = array2[i]; + } + + newArray = removeZero(newArray); + + //冒泡排序 + for (int i = 0; i < newArray.length - 1; i++) { + for (int j = 0; j < newArray.length - 1; j++) { + if(newArray[j] > newArray[j + 1]){ + int temp = newArray[j + 1]; + newArray[j+ 1] = newArray[j]; + newArray[j] = temp; + } + } + } + + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] array = {1,1,2,3,5,8,13,21}; + int[] newArray = null; + for (int i = 0; i < array.length; i++) { + if(array[i] > max){ + newArray = new int[i]; + System.arraycopy(array, 0, newArray, 0, i); + break; + } + } + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] array = {0}; + if(max > 2){ + array[0] = 2; + int k = 1; + for (int n = 3; n < max; n++) { + int i = 2; + + while(i < n){ + //若等于0,则不是素数,跳出循环 + if(n % i == 0) + break; + + i++; + } + + //如果i==n则说明n不能被2~n-1整除,是素数 + if (i == n) { + //数组自增一个容量 + array =grow(array, 1); + //将素数加入数组 + array[k] = n; + k++; + } + } + return array; + } + else{ + return null; + } + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + int a =0; + for(int i=2;i arrayList = new ArrayList(); + int i=0,j=0; + while (i arrayList) { + int[] array = new int[arrayList.size()]; + for(int i=0; i arrayList = new ArrayList(); + for(int i=1; i<=Math.sqrt(a); i++) { + if(a % i == 0) { + arrayList.add(i); + arrayList.add(a / i); + } + } + int sum = 0; + for(int item: arrayList) { + sum += item; + } + if(sum-a == a) { + return true; + } + + return false; + } +} + \ No newline at end of file diff --git a/group07/752262774/src/com/coderising/array/ArrayUtilTest.java b/group07/752262774/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..ecad9623b8 --- /dev/null +++ b/group07/752262774/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,95 @@ +package com.coderising.array; + +/** + * Created by yrs on 2017/3/4. + */ +public class ArrayUtilTest { + + @org.junit.Test + public void testReverseArray() throws Exception { + int[] array = new int[5]; + for(int i=0; i<5; i++) { + array[i] = i; + } + ArrayUtil util = new ArrayUtil(); + util.reverseArray(array); + for(int i=0; i<5; i++) { + assert (array[i] == 4-i); + } + } + + @org.junit.Test + public void testRemoveZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + ArrayUtil util = new ArrayUtil(); + int[] newArr = util.removeZero(oldArr); + for(int item: newArr) { + System.out.println(item); + assert (item != 0); + } + } + + @org.junit.Test + public void testMerge() throws Exception { + int[] a1 = {3,5,7,8,22}; + int[] a2 = {4,5,6,7,8,9,11}; + int[] a3 = {3,4,5,6,7,8,9,11,22}; + ArrayUtil util = new ArrayUtil(); + int[] merge = util.merge(a1, a2); + for(int i=0;i targetMap = null; + + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + + 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字段中。 + + */ + + targetMap = getTargetMap(actionName); + if(!targetMap.containsKey(actionName)) { + throw new Exception("this xml file not found" + actionName + "action"); + } + String className = targetMap.get(actionName); + Class classEntity = Class.forName(className); + Object obj = classEntity.newInstance(); + Field[] fields = classEntity.getDeclaredFields(); + + for(Field field: fields) { + field.setAccessible(true); + if(parameters.containsKey(field.getName())) { + field.set(obj, parameters.get(field.getName())); + } + } + + Method execute = classEntity.getMethod("execute"); + String result = (String)execute.invoke(obj); + String jsp = targetMap.get(result); + Field message = classEntity.getDeclaredField("message"); + message.setAccessible(true); + Map parats = new HashMap(); + parats.put("message", (String)message.get(obj)); + + View view = new View(); + view.setJsp(jsp); + view.setParameters(parats); + + return view; + } + + private static Map getTargetMap(String actionName) throws DocumentException { + Map targetMap = new HashMap(); + //创建SAXReader对象 + SAXReader reader = new SAXReader(); + //读取文件 转换成Document + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + //获取根节点元素对象 + Element root = document.getRootElement(); + List actionList = root.elements("action"); + for(Element action: actionList) { + org.dom4j.Attribute attribute = action.attribute("name"); + if(attribute.getValue().equals("login")){ + targetMap.put(attribute.getValue(), action.attribute("class").getValue()); + List resultList = action.elements("result"); + for(Element result: resultList) { + targetMap.put(result.attribute("name").getValue(),""+result.getData()); + } + break; + } + } + return targetMap; + } + +} diff --git a/group07/752262774/src/com/coderising/litestruts/StrutsTest.java b/group07/752262774/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..70c71c2ab0 --- /dev/null +++ b/group07/752262774/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,53 @@ +package com.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group07/752262774/src/com/coderising/litestruts/View.java b/group07/752262774/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group07/752262774/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/group07/752262774/src/com/coderising/litestruts/struts.xml b/group07/752262774/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..6f23f0a83d --- /dev/null +++ b/group07/752262774/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/group07/752262774/src/com/coding/basic/ArrayList.java b/group07/752262774/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..59b22d2c53 --- /dev/null +++ b/group07/752262774/src/com/coding/basic/ArrayList.java @@ -0,0 +1,114 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by yrs on 2017/2/21. + */ +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData; + + public ArrayList() { + this.elementData = new Object[10]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + this.elementData = new Object[initialCapacity]; + } else if (initialCapacity == 0) { + this.elementData = new Object[0]; + } else { + throw new IllegalArgumentException("Illegal Capacity: "+ + initialCapacity); + } + } + + public void add(Object o) { + judegGrow(); + elementData[size++] = o; + } + + public void add(int index, Object o) { + if(index<0 || index>size) { + throw new IndexOutOfBoundsException(); + }else if(index == size) { + add(o); + }else { + judegGrow(); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + } + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object o = elementData[index]; + + int move = size - 1 -index; + if(move > 0) { + System.arraycopy(elementData, index+1, elementData, index, move); + } + elementData[--size] = null; + + return o; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrrayListIterator(this); + } + + private class ArrrayListIterator implements Iterator { + + ArrayList arrayList; + + int pos; + + private ArrrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + return pos != arrayList.size; + } + + @Override + public Object next() { + if(pos < size) { + int i = pos; + pos++; + return elementData[i]; + }else { + throw new NoSuchElementException(); + } + } + } + + private void judegGrow() { + if(size == elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length + 1); + } + } + + private void rangeCheck(int index) { + if(index<0 || index>=size) { + throw new IndexOutOfBoundsException(); + } + } +} + \ No newline at end of file diff --git a/group07/752262774/src/com/coding/basic/BinaryTreeNode.java b/group07/752262774/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..275a1e66f9 --- /dev/null +++ b/group07/752262774/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,49 @@ +package com.coding.basic; + +/** + * Created by yrs on 2017/2/25. + */ +public class BinaryTreeNode { + + private Object data; + + private BinaryTreeNode left; + + private BinaryTreeNode right; + + public BinaryTreeNode(BinaryTreeNode left, Object o, BinaryTreeNode right) { + setData(o); + setLeft(left); + setRight(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; + } + +} + \ No newline at end of file diff --git a/group07/752262774/src/com/coding/basic/Iterator.java b/group07/752262774/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..c13e8e3b35 --- /dev/null +++ b/group07/752262774/src/com/coding/basic/Iterator.java @@ -0,0 +1,12 @@ +package com.coding.basic; + +/** + * Created by yrs on 2017/2/25. + */ +public interface Iterator { + + public boolean hasNext(); + + public Object next(); +} + \ No newline at end of file diff --git a/group07/752262774/src/com/coding/basic/LinkedList.java b/group07/752262774/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..60831251d3 --- /dev/null +++ b/group07/752262774/src/com/coding/basic/LinkedList.java @@ -0,0 +1,210 @@ +package com.coding.basic; + + +import java.util.NoSuchElementException; + +/** + * Created by yrs on 2017/2/23. + */ +public class LinkedList implements List{ + + private int size; + + private Node first; + + private Node last; + + public LinkedList() { + this.first = null; + this.last =null; + } + + public void add(Object o) { + Node l = this.last; + Node newNode = new Node(l, o, null); + this.last = newNode; + if(null == l) { + this.first = newNode; + }else { + l.next = newNode; + } + this.size++; + } + + public void add(int index, Object o) { + if(index<0 || index>size) { + throw new IndexOutOfBoundsException(); + }else if(index == this.size) { + this.add(o); + }else { + Node target = targetNode(index); + Node before = target.prev; + Node newNode = new Node(before, o, target); + target.prev = newNode; + if(null == before) { + this.first = newNode; + }else { + before.next = newNode; + } + this.size++; + } + } + + public Object get(int index) { + rangeCheck(index); + return targetNode(index).data; + } + + public Object remove(int index) { + rangeCheck(index); + Node target = targetNode(index); + Node before = target.prev; + Node after = target.next; + Object o = target.data; + + if(null == before) { + this.first = null; + }else { + before.next = after; + target.prev = null; + } + + if(null == after) { + this.last = before; + }else { + after.prev = before; + target.next = null; + } + target.data = null; + this.size--; + + return o; + } + + public int size() { + return this.size; + } + + public void addFirst(Object o) { + Node node = first; + Node newNode = new Node(null, o, node); + this.first = newNode; + if(null == node) { + this.last = newNode; + }else { + node.prev = newNode; + } + this.size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + Node node = first; + if (node == null) + throw new NoSuchElementException(); + + first = node.next; + Object o = node.data; + if(null == node.next) { + this.last = null; + }else { + first.prev = null; + } + node.data = null; + node.next = null; + this.size--; + return o; + } + + public Object removeLast() { + Node node = last; + if (node == null) + throw new NoSuchElementException(); + + last = node.prev; + Object o = node.data; + if(null == node.prev) { + this.first = null; + }else { + last.next = null; + } + node.data = null; + node.prev = null; + this.size--; + return o; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private class LinkedListIterator implements Iterator { + + LinkedList linkedList; + + int pos; + + private LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return pos != linkedList.size; + } + + @Override + public Object next() { + if(pos < size) { + int i = pos; + pos++; + return linkedList.get(i); + }else { + throw new NoSuchElementException(); + } + } + } + + private void rangeCheck(int index) { + if(index<0 || index>=size) { + throw new IndexOutOfBoundsException(); + } + } + + private Node targetNode(int index) { + //由index值在链表的前半部分还是后半部分,决定是从前向后,还是从后向前查找。 + Node target = new Node(); + if(index < (this.size >> 1)) { + target = this.first; + for(int i=0; iindex; i--) { + target = target.prev; + } + } + return target; + } + + private static class Node{ + Object data; + Node next; + Node prev; + + Node() { + } + + Node(Node prev, Object o, Node next) { + this.data = o; + this.next = next; + this.prev = prev; + } + } + +} + \ No newline at end of file diff --git a/group07/752262774/src/com/coding/basic/List.java b/group07/752262774/src/com/coding/basic/List.java new file mode 100644 index 0000000000..55658370ae --- /dev/null +++ b/group07/752262774/src/com/coding/basic/List.java @@ -0,0 +1,18 @@ +package com.coding.basic; + +/** + * Created by yrs on 2017/2/23. + */ +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/group07/752262774/src/com/coding/basic/Queue.java b/group07/752262774/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..c967b45fa2 --- /dev/null +++ b/group07/752262774/src/com/coding/basic/Queue.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +/** + * Created by yrs on 2017/2/25. + */ +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + +} + \ No newline at end of file diff --git a/group07/752262774/src/com/coding/basic/Stack.java b/group07/752262774/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..d47eb07427 --- /dev/null +++ b/group07/752262774/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +/** + * Created by yrs on 2017/2/25. + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + Object o = elementData.remove(elementData.size()-1); + return o; + } + + public Object peek() { + Object o = elementData.get(elementData.size() - 1); + return o; + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + + public static void main(String [] args) { + Stack stack = new Stack(); + stack.push(1); + System.out.println(stack.size() + " " + stack.peek() + " " + stack.pop() + " " + stack.isEmpty()); + + } + +} + \ No newline at end of file diff --git a/group07/764189149/.classpath b/group07/764189149/.classpath index fb5011632c..1fc7a9a529 100644 --- a/group07/764189149/.classpath +++ b/group07/764189149/.classpath @@ -2,5 +2,9 @@ + + + + diff --git a/group07/764189149/src/com/coderising/download/DownloadThread.java b/group07/764189149/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..d9514ac5cf --- /dev/null +++ b/group07/764189149/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,38 @@ +package com.coderising.download; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier ; + String filePath; + + public DownloadThread(CyclicBarrier barrier , Connection conn, int startPos, int endPos , String filePath){ + + this.barrier = barrier; + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.filePath = filePath; + } + public void run(){ + try{ + System.out.println("begin download startPos="+startPos+",endPos="+endPos); + byte[] buffer = conn.read(startPos , endPos); + RandomAccessFile file = new RandomAccessFile(filePath, "rw"); + file.seek(startPos); + file.write(buffer, 0, buffer.length); + file.close(); + barrier.await(); + }catch(Exception e){ + System.out.println("download error:startPos="+startPos+",endPos="+endPos); + } + + } +} diff --git a/group07/764189149/src/com/coderising/download/FileDownloader.java b/group07/764189149/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..f63b39d0af --- /dev/null +++ b/group07/764189149/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,127 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.concurrent.CyclicBarrier; + +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 { + + boolean isFinished = false; + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + private static final int THREAD_NUM = 3; + + private static final String BASE_PATH = "F:/download/"; + + + public FileDownloader(String _url) { + this.url = _url; + File baseFile = new File(BASE_PATH); + if(!baseFile.exists()){ + baseFile.mkdirs(); + } + + } + + 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方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + CyclicBarrier barrier = new CyclicBarrier(THREAD_NUM, new Runnable() { + + @Override + public void run() { + listener.notifyFinished(); + } + }); + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + String filePath = BASE_PATH + "download."+getFileType(this.url); + //System.out.println(filePath); + + File file = new File(filePath); + if(!file.exists()){ + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + try{ + FileOutputStream fos = new FileOutputStream(file); + fos.write(new byte[length], 0, length);//占位 + fos.close(); + } + catch (IOException e) { + System.out.println(e.getMessage()); + } + + int blockSize = (length % THREAD_NUM == 0 ) ? length / THREAD_NUM : (length / THREAD_NUM + 1); + for (int i = 0; i < THREAD_NUM; i++) { + int startPos = i * blockSize; + int endPos = startPos + blockSize - 1; + if(endPos >= length - 1){ + endPos = length - 1; + } + new DownloadThread(barrier , conn, startPos, endPos , filePath).start(); + } + + } catch (ConnectionException e) { + System.out.println(e.getMessage()); + } finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + private String getFileType(String url) { + int index = url.lastIndexOf("."); + return url.substring(index + 1 , url.length()); + } + + 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/group07/764189149/src/com/coderising/download/FileDownloaderTest.java b/group07/764189149/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..5e325db7a4 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,55 @@ +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 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488796402240&di=8ca9322617d5338cad61232a06f6ed7a&imgtype=0&src=http%3A%2F%2Fjiangsu.china.com.cn%2Fuploadfile%2F2017%2F0212%2F1486868426284307.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/group07/764189149/src/com/coderising/download/api/Connection.java b/group07/764189149/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group07/764189149/src/com/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/group07/764189149/src/com/coderising/download/api/ConnectionException.java b/group07/764189149/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..c2a0b14c82 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + String message; + public ConnectionException(String message){ + super("message:"+message); + this.message = message; + } + +} diff --git a/group07/764189149/src/com/coderising/download/api/ConnectionManager.java b/group07/764189149/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group07/764189149/src/com/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/group07/764189149/src/com/coderising/download/api/DownloadListener.java b/group07/764189149/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group07/764189149/src/com/coderising/download/impl/ConnectionImpl.java b/group07/764189149/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..8881230f80 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,40 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private URLConnection connection; + + @Override + public byte[] read(int startPos, int endPos) throws IOException { +// connection.setAllowUserInteraction(true); +// connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream inputstream = connection.getInputStream(); + byte[] buffer = new byte[endPos - startPos + 1]; + inputstream.skip(startPos); + inputstream.read(buffer); + inputstream.close(); + return buffer; + } + + @Override + public int getContentLength(){ + return connection.getContentLength(); + } + + @Override + public void close() { + } + + public void setConnection(URLConnection connection) { + this.connection = connection; + } + + + +} diff --git a/group07/764189149/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group07/764189149/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..20dfaaad80 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,30 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +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 { + URL urlObj; + ConnectionImpl connection = null; + try { + urlObj = new URL(url); + connection = new ConnectionImpl(); + connection.setConnection(urlObj.openConnection()); + } catch (MalformedURLException e) { + throw new ConnectionException("URL格式错误"); + }catch (IOException e) { + throw new ConnectionException("IO异常"); + } + + return connection; + } + +} diff --git a/group07/764189149/src/com/leijing/coderising/array/ArrayUtil.java b/group07/764189149/src/com/leijing/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..544b788ffb --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/array/ArrayUtil.java @@ -0,0 +1,279 @@ +package com.leijing.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +/** + * @author: leijing + * @date: 2017年3月2日 下午5:30:38 + */ +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 + * @throws Exception + */ + public void reverseArray(int[] origin) throws Exception{ + if(origin.length < 1){ + throw new Exception("array origin is empty"); + } + int size = origin.length; + int[] copy = Arrays.copyOf(origin, size); + for (int i = 0; i < size; i++) { + origin[size - i - 1] = copy[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 newSize = countNotZero(oldArray);//先计算非零元素个数 + int[] newArray = new int[newSize];//创建新数组 + int newIndex = 0;//新数组索引 + int oldIndex = 0;//旧数组索引 + while(newIndex < newSize){ + if(oldArray[oldIndex] != 0){ + newArray[newIndex++] = oldArray[oldIndex]; + } + + oldIndex++; + } + + return newArray; + } + + private int countNotZero(int[] oldArray) { + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + count++; + } + } + return count; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int size = getSize(array1 , array2); + int[] newArray = new int[size]; + + int minIndex1 = 0; + int minIndex2 = 0; + + for (int i = 0; i < newArray.length; i++) { + + if(minIndex1 < array1.length && minIndex2 == array2.length ){//如果array2的元素取完了,就取array1的元素 + newArray[i] = array1[minIndex1++]; + }else if(minIndex1 == array1.length && minIndex2 < array2.length){//如果array1的元素取完了,就取array2的元素 + newArray[i] = array2[minIndex2++]; + }else{//两个数组都没有取完 + if(array1[minIndex1] < array2[minIndex2]){//第一个数组的元素小 + newArray[i] = array1[minIndex1++]; + }else if(array1[minIndex1] == array2[minIndex2]){//元素相等,保留第一个数组的,第二个数组索引加一 + newArray[i] = array1[minIndex1++]; + minIndex2++; + } + else{//第二个数组元素小 + newArray[i] = array2[minIndex2++]; + } + } + } + + return newArray; + } + + private int getSize(int[] array1 , int[] array2) { + Set set = new HashSet(); + int sameNum = 0; + + for (int i = 0; i < array1.length; i++) { + set.add(array1[i]); + } + + for (int i = 0; i < array2.length; i++) { + if(!set.add(array2[i])){ + sameNum++; + } + } + return array1.length + array2.length - sameNum; + } + + /** + * 把一个已经存满数据的数组 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 < 1){ + return oldArray; + } + int[] newArray = new int[oldArray.length + size]; + + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if(max == 1){ + return new int[0]; + } + List list = new ArrayList(); + list.add(1); + list.add(1); + + int next = 0; + int i = 2; + while(next < max) { + next = list.get(i - 1) + list.get(i - 2 ); + list.add(next); + i++; + } + int[] array = new int[list.size()]; + list2Array(list , array); + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + List list = new ArrayList(); + for (int i = 2; i < max; i++) { + if(isPrimes(i)){ + list.add(i); + } + } + int[] array = new int[list.size()]; + list2Array(list , array); + return array; + } + + private boolean isPrimes(int num) { + for (int i = 2; i <= num / 2; i++) { + if(num % i == 0){ + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + List list = new ArrayList(); + for (int i = 1; i < max; i++) { + if(isPerfectNum(i)){ + list.add(i); + } + } + int[] array = new int[list.size()]; + list2Array(list , array); + return array; + } + + private void list2Array(List list, int[] array) { + for (int i = 0; i < list.size(); i++) { + array[i] = list.get(i); + } + } + + private boolean isPerfectNum(int num) { + Set list = new HashSet(); + if(num == 1){ + return false; + }else{ + list.add(1); + } + for (int i = 2; i <= num / 2; i++) { + if(num % i == 0){ + list.add(i); + list.add(num / i); + } + } + Iterator iter = list.iterator(); + int sum = 0; + while (iter.hasNext()) { + Integer val = iter.next(); + sum += val; + } + + if(sum == num){ + System.out.println("num:"+num+","+list.toString()); + return true; + } + return false; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if(i < array.length - 1){ + sb.append(seperator); + } + } + return sb.toString(); + } + + public void printArray(int[] array , String msg){ + System.out.print(msg+"["); + for (int i = 0; i < array.length; i++) { + System.out.print(array[i]); + if(i < array.length - 1){ + System.out.print(","); + } + } + System.out.println("]"); + } + + +} diff --git a/group07/764189149/src/com/leijing/coderising/array/ArrayUtilTest.java b/group07/764189149/src/com/leijing/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..235ffb0f15 --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/array/ArrayUtilTest.java @@ -0,0 +1,77 @@ +package com.leijing.coderising.array; + +import org.junit.Test; + +public class ArrayUtilTest { + private ArrayUtil arrayUtil = new ArrayUtil(); + + @Test + public void testReverseArray() { + int[] origin = {7, 9 , 30, 3 , 18 , 21}; + arrayUtil.printArray(origin,"before:"); + + try { + arrayUtil.reverseArray(origin); + arrayUtil.printArray(origin , "after:"); + } catch (Exception e) { + System.out.println(e); + } + } + + @Test + public void testRemoveZero() { + int[] oldArray={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + arrayUtil.printArray(oldArray , "before:"); + int[] newArray = arrayUtil.removeZero(oldArray); + arrayUtil.printArray(newArray , "after:"); + } + + @Test + public void testMerge() { + int[] array1 = {3, 5, 7,8}; + int[] array2 = {4, 5, 6,7}; + arrayUtil.printArray(array1 , "array1:"); + arrayUtil.printArray(array2 , "array2:"); + int[] newArray = arrayUtil.merge(array1, array2); + arrayUtil.printArray(newArray , "mergre after:"); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int size = 3; + arrayUtil.printArray(oldArray , "grow before:"); + int[] newArray = arrayUtil.grow(oldArray, size); + arrayUtil.printArray(newArray , "grow after:"); + } + + @Test + public void testFibonacci() { + int max = 20; + int[] array = arrayUtil.fibonacci(max); + arrayUtil.printArray(array , max + "以内的所有斐波那契数列:"); + } + + @Test + public void testGetPrimes() { + int max = 100; + int[] array = arrayUtil.getPrimes(max); + arrayUtil.printArray(array , max + "以内的所有素数:"); + } + + @Test + public void testGetPerfectNumbers() { + int max = 1000; + int[] array = arrayUtil.getPerfectNumbers(max); + arrayUtil.printArray(array , max + "以内的所有完数:"); + } + + @Test + public void testJoin() { + int[] array = {3,8,9}; + String seperator = "-"; + String result = arrayUtil.join(array, seperator); + System.out.println(result); + } + +} diff --git a/group07/764189149/src/com/leijing/coderising/litestruts/LoginAction.java b/group07/764189149/src/com/leijing/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..833e1464b4 --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.leijing.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/group07/764189149/src/com/leijing/coderising/litestruts/Struts.java b/group07/764189149/src/com/leijing/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..fbabd397ed --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/litestruts/Struts.java @@ -0,0 +1,134 @@ +package com.leijing.coderising.litestruts; + +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +/** + * @Description: 解析Struts文件、处理action请求 + * @author: leijing + * @date: 2017年3月3日 下午3:03:38 + */ +public class Struts { + private static String STRUTS_XML = "struts.xml"; + + private static Map actionsMap = new HashMap(); + private static Map resultMap = new HashMap(); + private Struts(){ + try { + init(); + } catch (Exception e) { + System.out.println("Struts init error:"+e.getMessage()); + } + + } + + /** + * @Description: 确保配置的是需要的节点 + * @param xmlName + * @param name + * @return: void + * @author: leijing + * @date: 2017年3月3日 下午3:20:31 + */ + private void checkTag(String xmlName , String name) throws Exception{ + if(xmlName == null || name == null || !xmlName.equals(name)){ + throw new Exception("xml parse error,"+xmlName+"can not be parsed,"+name+" is required!"); + } + } + private void init() throws Exception{ + InputStream inputStream = this.getClass().getResourceAsStream(STRUTS_XML); + SAXReader reader = new SAXReader();//创建SAXReader对象 + Document document = reader.read(inputStream); //用SAXReader对象加载配置文件得到Document对象 + Element struts = document.getRootElement();//得到根节点 + checkTag(struts.getName(), "struts"); + List actions = struts.elements();//得到所有action节点 + + for(Iterator it = actions.iterator() ; it.hasNext();){//遍历action节点 + Element action = (Element) it.next(); + checkTag(action.getName(), "action"); + Attribute actionName = action.attribute("name"); + Attribute actionClazz = action.attribute("class"); + actionsMap.put(actionName.getText(), actionClazz.getText()); + System.out.println("add to actionsMap:"+actionName.getText()+":"+actionClazz.getText()); + + List results = action.elements();//得到配置的result节点 + for(Iterator rit = results.iterator() ; rit.hasNext();){//遍历result节点 + Element result = (Element) rit.next(); + checkTag(result.getName(), "result"); + Attribute resultName = result.attribute("name"); + resultMap.put(actionName.getText()+"_"+resultName.getText() , result.getData().toString()); + System.out.println("add to resultMap: result:"+resultName.getText()+",url:"+result.getData()); + } + } + + + } + + public static View runAction(String actionName, Map parameters) throws Exception { + /* + + 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字段中。 + + */ + if(!actionsMap.containsKey(actionName)){ + throw new Exception("action "+actionName+"not found!"); + } + + String className = actionsMap.get(actionName); + Class clazz = Class.forName(className); + Object obj = clazz.newInstance(); + Field[] fields = clazz.getDeclaredFields(); + + for (Field field : fields) { + field.setAccessible(true); + if(parameters.containsKey(field.getName())){ + field.set(obj, parameters.get(field.getName())); + } + } + Method execute = clazz.getDeclaredMethod("execute"); + String result = (String) execute.invoke(obj, new Object[]{}); + String jsp = resultMap.get(actionName+"_"+result); + if(null == jsp){ + throw new Exception("jsp config at action:"+actionName +",result:"+ result+" not found !"); + } + Field message = clazz.getDeclaredField("message"); + message.setAccessible(true); + View view = new View(); + view.setJsp(jsp); + Map params = new HashMap(); + params.put("message", message.get(obj)); + view.setParameters(params); + return view; + } + public static Struts getInstance(){ + return StrutsHoldler.INSTANCE; + } + private static class StrutsHoldler{ + private static Struts INSTANCE = new Struts();//struts对象应该是单例的 + } +} diff --git a/group07/764189149/src/com/leijing/coderising/litestruts/StrutsTest.java b/group07/764189149/src/com/leijing/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..787d2bcd28 --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +package com.leijing.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { +private Struts struts = Struts.getInstance();//struts对象应该是单例的 + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group07/764189149/src/com/leijing/coderising/litestruts/View.java b/group07/764189149/src/com/leijing/coderising/litestruts/View.java new file mode 100644 index 0000000000..3411bf9130 --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/litestruts/View.java @@ -0,0 +1,38 @@ +package com.leijing.coderising.litestruts; + +import java.util.Map; +import java.util.Set; + +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; + } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("jsp:").append(jsp); + if(null == parameters){ + return sb.toString(); + } + Set keys = parameters.keySet(); + + for (String key : keys) { + sb.append(",").append(key).append(":").append(parameters.get(key)); + } + return sb.toString(); + } +} diff --git a/group07/764189149/src/com/leijing/coderising/litestruts/struts.xml b/group07/764189149/src/com/leijing/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0ef54148e1 --- /dev/null +++ b/group07/764189149/src/com/leijing/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/group07/764189149/src/firstHomeWork/util/Stack.java b/group07/764189149/src/firstHomeWork/util/Stack.java index 00dc784170..a217fd0c74 100644 --- a/group07/764189149/src/firstHomeWork/util/Stack.java +++ b/group07/764189149/src/firstHomeWork/util/Stack.java @@ -1,23 +1,22 @@ package firstHomeWork.util; -import java.util.Queue; - -public class Stack { - private ArrayList elementData = new ArrayList(); - public void push(Object o){ +public class Stack { + private ArrayList elementData = new ArrayList(); + public void push(E e){ + elementData.add(e); } public Object pop(){ - return null; + return elementData.remove(elementData.size() - 1); } public Object peek(){ - return null; + return elementData.get(elementData.size() - 1); } public boolean isEmpty(){ - return false; + return elementData.isEmpty(); } public int size(){ - return -1; + return elementData.size(); } } diff --git a/group07/770164810/src/com/coding/basic/ArrayList.java b/group07/770164810/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..265f4398ed --- /dev/null +++ b/group07/770164810/src/com/coding/basic/ArrayList.java @@ -0,0 +1,68 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private int modifyNum = 0; + + private Object[] elementData = new Object[100]; + public void add(Object o){ + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + 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 oj=elementData[index]; + for(int i=index;i= size || index < 0) throw new IndexOutOfBoundsException(); + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node n = first; + first = new Node(o); + if(n != null){ + first.next = n; + }else{ + last = first; + } + size++; + } + + public void addLast(Object o) { + Node n = new Node(o); + if(last != null){ + last.next = n; + }else{ + first = n; + } + last = n; + size++; + } + + public Object removeFirst() { + return null; + } + + public Object removeLast() { + return null; + } + + public Iterator iterator() { + return null; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data){ + this.data = data; + } + + public Node(){} + + } + + public static void main(String[] args) { + LinkedList l = new LinkedList(); + l.add(1); + l.add(2); + l.add(3); + l.add(4); + l.add(2, 5); + l.remove(0); + + for(int i=0;i 计算机整体分为软件和硬件。 +> 软件是由指令和数据组成的程序,硬件包含主板、CPU、内存、硬盘等 + + +指令指的是计算机可以识别的机器语言,每个操作系统都有内置的指令集,计算机可根据不同的指令来驱动计算机工作或是相应的输出。指令是以二进制的形式存储在硬盘中,当CPU执行某个指令的时候,需要将指令先从硬盘加载到内存,CPU从内存中获取执行解释执行。 + +CPU是计算的核心部件,相当于人类的大脑。CPU执行指令的时候其实不关心也不知道指令的意思,只是按照指令执行。由于硬盘读写性能比较差,CPU从硬盘中直接读取数据的时候比较费时,就有的内存的存在。内存比硬盘读写速度快很多,CPU要执行程序时,先将程序加载到内存中在执行。 \ No newline at end of file diff --git a/group08/121027265/0226/src/com/my/list/ArrayList.java b/group08/121027265/0226/src/com/my/list/ArrayList.java new file mode 100644 index 0000000000..b9ca5a31d9 --- /dev/null +++ b/group08/121027265/0226/src/com/my/list/ArrayList.java @@ -0,0 +1,131 @@ +package com.my.list; + +/** + * 实现ArrayList + * + */ +public class ArrayList { + //初始化容量 + private static final int INIT = 5; + //增长因子 + private static final double GROWTH = 0.5; + + //初始化数组 + Object[] baseArr = new Object[INIT]; + //当前下标 + int currentIndex = 0; + //最大下标 + int maxIndex = INIT-1; + + /** + * 添加元素 + * @param obj + * @return + */ + public Object add(Object obj){ + if(judgeCapacity()){ + baseArr = arrayDilatation(baseArr, GROWTH); + maxIndex = baseArr.length-1; + } + baseArr[currentIndex] = obj; + ++currentIndex; + return obj; + } + + /** + * 指定下标添加元素 + * @param index + * @param obj + * @return + */ + public Object add(int index , Object obj){ + if (index < 0 || index > currentIndex) { + throw new IndexOutOfBoundsException(); + } + Object[] dest = new Object[currentIndex+1]; + System.arraycopy(baseArr, 0, dest, 0, index); + dest[index] = obj; + System.arraycopy(baseArr, index, dest, index+1, currentIndex-index); + ++currentIndex; + baseArr = dest; + return obj; + } + + /** + * 指定下标删除元素 + * @param index + * @return + */ + public Object remove(int index){ + if (index < 0 || index >= currentIndex) { + throw new IndexOutOfBoundsException(); + } + Object object = baseArr[index]; + Object[] dest = new Object[currentIndex]; + System.arraycopy(baseArr, 0, dest, 0, index); + System.arraycopy(baseArr, index+1, dest, index, currentIndex-index-1); + --currentIndex; + baseArr = dest; + return object; + } + + /** + * 根据下标获取元素 + * @param index + * @return + */ + public Object get(int index){ + + return baseArr[index]; + } + + /** + * 获取集合大小 + * @return + */ + public int size(){ + return currentIndex; + } + + /** + * 判断容量是否需要增加 + * @return + */ + public boolean judgeCapacity(){ + if(currentIndex > maxIndex){ + return true; + } + return false; + } + + /** + * 数组扩容 + * @param objArr + * @param groth + * @return + */ + public static Object[] arrayDilatation(Object[] objArr , double groth){ + int length = objArr.length; + int newLength = (int) (length * groth + length); + Object[] baseArr = new Object[newLength]; + System.arraycopy(objArr, 0, baseArr, 0, length); + return baseArr; + } + + +} + + + + + + + + + + + + + + + diff --git a/group08/121027265/0226/src/com/my/list/LinkedList.java b/group08/121027265/0226/src/com/my/list/LinkedList.java new file mode 100644 index 0000000000..19b0655287 --- /dev/null +++ b/group08/121027265/0226/src/com/my/list/LinkedList.java @@ -0,0 +1,156 @@ +package com.my.list; + +/** + * 实现 LinkedList + * + */ +public class LinkedList { + + //首节点 + Node first = new Node(); + //集合大小 + int size = 0; + + /** + * 添加元素 + * @param object + * @return + */ + public Object add(Object object){ + if (size == 0) { + first.setObject(object); + }else{ + Node previous = first; + Node temp = first.getNext(); + while (temp != null) { + previous = temp; + temp = temp.getNext(); + } + Node node = new Node(); + node.setObject(object); + previous.setNext(node); + } + ++size; + return object; + } + + /** + * 根据下标添加元素 + * @param index + * @param object + * @return + */ + public Object add(int index , Object object){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + + if (size == 0) { + first.setObject(object); + }else{ + if (index == 0) { + Node temp = new Node(); + temp.setObject(object); + temp.setNext(first); + first = temp; + }else{ + int count = 1; + Node temp = first; + while (temp != null) { + if (count++ == index) { + Node next = temp.getNext(); + Node node = new Node(); + temp.setNext(node); + node.setObject(object); + node.setNext(next); + break; + } + temp = temp.getNext(); + } + } + } + ++size; + return object; + } + + /** + * 根据下标删除元素 + * @param index + * @return + */ + public Object remove(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + Node node = null; + if (index == 0) { + Node next = first.getNext(); + first = next; + node = first; + }else{ + int count = 1; + Node temp = first; + while (temp != null) { + if (count++ == index) { + node = temp.getNext(); + Node next = node.getNext(); + temp.setNext(next); + break; + } + temp = temp.getNext(); + } + } + --size; + return node.getObject(); + } + + /** + * 根据下标获取元素 + * @param index + * @return + */ + public Object get(int index) { + Node temp = first; + int count = 0; + while (temp != null) { + if (count++ == index) { + break; + } + temp = temp.getNext(); + } + return temp.getObject(); + } + + /** + * 获取集合大小 + * @return + */ + public int size() { + return size; + } + + +} + + +/** + * 节点元素 + * + */ +class Node{ + private Object object ; + private Node next; + public Object getObject() { + return object; + } + public void setObject(Object object) { + this.object = object; + } + public Node getNext() { + return next; + } + public void setNext(Node next) { + this.next = next; + } + +} diff --git a/group08/121027265/0226/src/com/my/list/Test.java b/group08/121027265/0226/src/com/my/list/Test.java new file mode 100644 index 0000000000..36b43e20dc --- /dev/null +++ b/group08/121027265/0226/src/com/my/list/Test.java @@ -0,0 +1,65 @@ +package com.my.list; + +public class Test { + + public static void main(String[] args) { + + testArrayList(); + System.out.println("--------------------------------"); + testLinkedList(); + } + + /** + * ArrayList 测试 + */ + public static void testArrayList(){ + System.out.println("ArrayList 测试 --开始"); + ArrayList list = new ArrayList(); + list.add("123"); + list.add("123"); + list.add("123"); + list.add("123"); + list.add("123"); + list.add("123"); + + list.add(1, 111); + + list.remove(1); + + System.out.println(list.baseArr.length); + System.out.println(list.size()); + + for (int i = 0; i < list.size(); i++) { + Object object = list.get(i); + System.out.println(object); + } + System.out.println("ArrayList 测试 --结束"); + } + + /** + * LinkedList 测试 + */ + public static void testLinkedList(){ + System.out.println("LinkedList 测试 --开始"); + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + + list.add(1, 111); + + list.remove(1); + + System.out.println(list.size()); + + for (int i = 0; i < list.size(); i++) { + Object object = list.get(i); + System.out.println(object); + } + System.out.println("LinkedList 测试 --结束"); + } + +} diff --git a/group08/1425809544/02-26/com/xuyangyang/util/BinaryTree.java b/group08/1425809544/02-26/com/xuyangyang/util/BinaryTree.java new file mode 100644 index 0000000000..2e566de023 --- /dev/null +++ b/group08/1425809544/02-26/com/xuyangyang/util/BinaryTree.java @@ -0,0 +1,65 @@ +/** + * Created by 呢喃 on 2017/2/26. + */ +public class BinaryTree { + + + private Node root; + + private static class Node { + Node left; + Node right; + int data; + + Node(int newData){ + left = null; + right = null; + data = newData; + } + } + + public BinaryTree(){ + root = null; + } + + +public void insert(int data){ + root = insert( root,data); +} + +private Node insert(Node node,int data){ + + if (node == null){ + node = new Node(data); + } + else { + if (data<= node.data){ + node.left = insert(node.left,data); + }else { + node.right = insert(node.right,data); + } + } + return node; +} + + public void bulidTree(int[] data){ + for (int i= 0;isize-1){ + return ; + }else{ + if(sizesize-1){ + return null; + }else{ + return elementData[index]; + } + } + + public Object remove(int index){ + if(index<0||index>size-1){ + return null; + }else{ + Object result = elementData[index]; + if(index+1==size){//即index是最后一个元素 + elementData[index] = null; + size--; + return result; + }else{ + System.arraycopy(elementData, index+1, elementData, index, size-1-index); + size--; + return result; + } + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + int current = 0; + @Override + public Object next() { + // TODO Auto-generated method stub + if(current + + + + + diff --git a/group08/283677872/2-26/.gitignore b/group08/283677872/2-26/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group08/283677872/2-26/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group08/283677872/2-26/.project b/group08/283677872/2-26/.project new file mode 100644 index 0000000000..643ca1eaa2 --- /dev/null +++ b/group08/283677872/2-26/.project @@ -0,0 +1,17 @@ + + + 2-26 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group08/283677872/2-26/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\345\205\263\347\263\273.docx" "b/group08/283677872/2-26/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\345\205\263\347\263\273.docx" new file mode 100644 index 0000000000..c5a97b8189 Binary files /dev/null and "b/group08/283677872/2-26/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\345\205\263\347\263\273.docx" differ diff --git a/group08/283677872/2-26/src/com/coding/basic/ArrayList.java b/group08/283677872/2-26/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..b3e60951b1 --- /dev/null +++ b/group08/283677872/2-26/src/com/coding/basic/ArrayList.java @@ -0,0 +1,70 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + + private Object[] elementData = new Object[100]; + + private int stepLength = 50; + + public void add(Object o){ + if(size > elementData.length) + { + Object[] elementDataNew = new Object[elementData.length + stepLength]; + System.arraycopy(elementData, 0, elementDataNew, 0, elementData.length); + elementData = elementDataNew; + } + + elementData[size] = o; + size++; + + } + public void add(int index, Object o){ + if(size > elementData.length) + { + Object[] elementDataNew = new Object[elementData.length + stepLength]; + System.arraycopy(elementData, 0, elementDataNew, 0, elementData.length); + elementData = elementDataNew; + } + System.arraycopy(elementData, index, elementData, index+1, size-index+1); + elementData[index] = o; + size++; + + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + Object obj = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + size--; + return obj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + public String toString() + { + StringBuffer sb = new StringBuffer(); + for(int i=0;i0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group08/283677872/2-26/src/com/coding/basic/Stack.java b/group08/283677872/2-26/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..e911d17866 --- /dev/null +++ b/group08/283677872/2-26/src/com/coding/basic/Stack.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop() throws Exception{ + if(isEmpty()) + { + throw new Exception("Stack pop error"); + } + return elementData.remove(elementData.size()-1); + } + + public Object peek() throws Exception{ + if(isEmpty()) + { + throw new Exception("Stack pop error"); + } + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.size()>0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group08/283677872/test b/group08/283677872/test deleted file mode 100644 index 3d8158f431..0000000000 --- a/group08/283677872/test +++ /dev/null @@ -1 +0,0 @@ -283677872 diff --git a/group08/619057560/2-26/code/com/coding/basic/ArrayList.java b/group08/619057560/2-26/code/com/coding/basic/ArrayList.java index 5fe3123515..70daee785d 100644 --- a/group08/619057560/2-26/code/com/coding/basic/ArrayList.java +++ b/group08/619057560/2-26/code/com/coding/basic/ArrayList.java @@ -1,80 +1,80 @@ -package com.coding.basic; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - if (size + 1 > elementData.length) { - elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); - } - elementData[size++] = o; - } - public void add(int index, Object o){ - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException(); - } - - if (size + 1 > elementData.length) { - elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); - } - - System.arraycopy(elementData, index, elementData, index+1, size-index); - elementData[index] = o; - size++; - } - - public Object get(int index){ - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(); - } - - return elementData[index]; - } - - public Object remove(int index){ - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(); - } - - Object old = elementData[index]; - size--; - System.arraycopy(elementData, index+1, elementData, index, size-index); - elementData[size] = null; - - return old; - } - - public int size(){ - return size; - } - - public Iterator iterator(){ - return new Itr(); - } - - private class Itr implements Iterator { - - int cursor = 0; - - @Override - public boolean hasNext() { - return (cursor < size); - } - - @Override - public Object next() { - if (cursor < 0 || cursor >= size) { - throw new NoSuchElementException(); - } - return elementData[cursor++]; - } - - } - -} +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if (size + 1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); + } + elementData[size++] = o; + } + public void add(int index, Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + + if (size + 1 > elementData.length) { + elementData = Arrays.copyOf(elementData, elementData.length * 3 / 2 + 1); + } + + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + return elementData[index]; + } + + public Object remove(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Object old = elementData[index]; + size--; + System.arraycopy(elementData, index+1, elementData, index, size-index); + elementData[size] = null; + + return old; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator { + + int cursor = 0; + + @Override + public boolean hasNext() { + return (cursor < size); + } + + @Override + public Object next() { + if (cursor < 0 || cursor >= size) { + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + + } + +} diff --git a/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java b/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java index af940807e6..9db8eec800 100644 --- a/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java +++ b/group08/619057560/2-26/code/com/coding/basic/BinaryTreeNode.java @@ -1,47 +1,47 @@ -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 (data == null || ((Integer)data).intValue() == ((Integer)o).intValue()) { - data = o; - return this; - } - else if (((Integer)o).intValue() < ((Integer)data).intValue()) { - if (left == null) { - left = new BinaryTreeNode(); - } - return left.insert(o); - } - else { - if (right == null) { - right = new BinaryTreeNode(); - } - return right.insert(o); - } - } - -} +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 (data == null || ((Integer)data).intValue() == ((Integer)o).intValue()) { + data = o; + return this; + } + else if (((Integer)o).intValue() < ((Integer)data).intValue()) { + if (left == null) { + left = new BinaryTreeNode(); + } + return left.insert(o); + } + else { + if (right == null) { + right = new BinaryTreeNode(); + } + return right.insert(o); + } + } + +} diff --git a/group08/619057560/2-26/code/com/coding/basic/Iterator.java b/group08/619057560/2-26/code/com/coding/basic/Iterator.java index dbe8b9afb2..06ef6311b2 100644 --- a/group08/619057560/2-26/code/com/coding/basic/Iterator.java +++ b/group08/619057560/2-26/code/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group08/619057560/2-26/code/com/coding/basic/LinkedList.java b/group08/619057560/2-26/code/com/coding/basic/LinkedList.java index d2f1422cfb..38a62b2f66 100644 --- a/group08/619057560/2-26/code/com/coding/basic/LinkedList.java +++ b/group08/619057560/2-26/code/com/coding/basic/LinkedList.java @@ -1,137 +1,137 @@ -package com.coding.basic; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - Node pNewNode = new Node(); - pNewNode.data = o; - - Node pNode = head; - - if (head == null) { - head = pNewNode; - return; - } - - while (pNode.next != null) { - pNode = pNode.next; - } - - pNode.next = pNewNode; - } - - public void add(int index , Object o){ - if (index < 0 && index > size()) { - throw new IndexOutOfBoundsException(); - } - - Node pNewNode = new Node(); - pNewNode.data = o; - - if (index == 0) { - pNewNode.next = head; - head = pNewNode; - return; - } - - Node pNode = head; - while (--index > 0) { - pNode = pNode.next; - } - pNewNode.next = pNode.next; - pNode.next = pNewNode; - } - - public Object get(int index){ - if (index < 0 && index >= size()) { - throw new IndexOutOfBoundsException(); - } - - Node pNode = head; - while (index-- > 0) { - pNode = pNode.next; - } - - return pNode.data; - } - - public Object remove(int index){ - if (index < 0 && index >= size()) { - throw new IndexOutOfBoundsException(); - } - - Node pNode = head; - if (index == 0) { - head = head.next; - return pNode.data; - } - - while (--index > 0) { - pNode = pNode.next; - } - Node pTargetNode = pNode.next; - pNode.next = pTargetNode.next; - - return pTargetNode.data; - } - - public int size(){ - Node pNode = head; - int num = 0; - while (pNode != null) { - pNode = pNode.next; - num++; - } - return num; - } - - public void addFirst(Object o){ - add(0, o); - } - - public void addLast(Object o){ - add(o); - } - - public Object removeFirst(){ - if (head == null) { - throw new NoSuchElementException(); - } - return remove(0); - } - - public Object removeLast(){ - if (head == null) { - throw new NoSuchElementException(); - } - - Node pNode = head; - Node pPrevNode = null; - while (pNode.next != null) { - pPrevNode = pNode; - pNode = pNode.next; - } - if (pPrevNode != null) { - pPrevNode.next = pNode.next; - } - else { - head = null; - } - return pNode.data; - } - - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } -} +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + Node pNewNode = new Node(); + pNewNode.data = o; + + Node pNode = head; + + if (head == null) { + head = pNewNode; + return; + } + + while (pNode.next != null) { + pNode = pNode.next; + } + + pNode.next = pNewNode; + } + + public void add(int index , Object o){ + if (index < 0 && index > size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNewNode = new Node(); + pNewNode.data = o; + + if (index == 0) { + pNewNode.next = head; + head = pNewNode; + return; + } + + Node pNode = head; + while (--index > 0) { + pNode = pNode.next; + } + pNewNode.next = pNode.next; + pNode.next = pNewNode; + } + + public Object get(int index){ + if (index < 0 && index >= size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNode = head; + while (index-- > 0) { + pNode = pNode.next; + } + + return pNode.data; + } + + public Object remove(int index){ + if (index < 0 && index >= size()) { + throw new IndexOutOfBoundsException(); + } + + Node pNode = head; + if (index == 0) { + head = head.next; + return pNode.data; + } + + while (--index > 0) { + pNode = pNode.next; + } + Node pTargetNode = pNode.next; + pNode.next = pTargetNode.next; + + return pTargetNode.data; + } + + public int size(){ + Node pNode = head; + int num = 0; + while (pNode != null) { + pNode = pNode.next; + num++; + } + return num; + } + + public void addFirst(Object o){ + add(0, o); + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + if (head == null) { + throw new NoSuchElementException(); + } + return remove(0); + } + + public Object removeLast(){ + if (head == null) { + throw new NoSuchElementException(); + } + + Node pNode = head; + Node pPrevNode = null; + while (pNode.next != null) { + pPrevNode = pNode; + pNode = pNode.next; + } + if (pPrevNode != null) { + pPrevNode.next = pNode.next; + } + else { + head = null; + } + return pNode.data; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group08/619057560/2-26/code/com/coding/basic/List.java b/group08/619057560/2-26/code/com/coding/basic/List.java index 396b1f6416..10d13b5832 100644 --- a/group08/619057560/2-26/code/com/coding/basic/List.java +++ b/group08/619057560/2-26/code/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group08/619057560/2-26/code/com/coding/basic/Queue.java b/group08/619057560/2-26/code/com/coding/basic/Queue.java index 67a080f408..1d240b6ec8 100644 --- a/group08/619057560/2-26/code/com/coding/basic/Queue.java +++ b/group08/619057560/2-26/code/com/coding/basic/Queue.java @@ -1,22 +1,22 @@ -package com.coding.basic; - -public class Queue { - - private LinkedList queueList = new LinkedList(); - - public void enQueue(Object o){ - queueList.addFirst(o); - } - - public Object deQueue(){ - return queueList.removeLast(); - } - - public boolean isEmpty(){ - return queueList.size() == 0; - } - - public int size(){ - return queueList.size(); - } -} +package com.coding.basic; + +public class Queue { + + private LinkedList queueList = new LinkedList(); + + public void enQueue(Object o){ + queueList.addFirst(o); + } + + public Object deQueue(){ + return queueList.removeLast(); + } + + public boolean isEmpty(){ + return queueList.size() == 0; + } + + public int size(){ + return queueList.size(); + } +} diff --git a/group08/619057560/2-26/code/com/coding/basic/Stack.java b/group08/619057560/2-26/code/com/coding/basic/Stack.java index 481c88bed7..03d5fa327c 100644 --- a/group08/619057560/2-26/code/com/coding/basic/Stack.java +++ b/group08/619057560/2-26/code/com/coding/basic/Stack.java @@ -1,23 +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(); - } -} +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/group08/729770920/2-26/src/com/coding/basic/ArrayList.java b/group08/729770920/2-26/src/com/coding/basic/ArrayList.java index 8fe149f4ad..d5cfc1ffa3 100644 --- a/group08/729770920/2-26/src/com/coding/basic/ArrayList.java +++ b/group08/729770920/2-26/src/com/coding/basic/ArrayList.java @@ -1,3 +1,4 @@ +<<<<<<< HEAD package com.coding.basic; public class ArrayList implements List { @@ -24,7 +25,7 @@ public void add(int index, E e){ throw new IndexOutOfBoundsException(Integer.toString(index)); } if (data.length == size) { - ensureCapacity(size * 2 + 1); + ensureCapacity(size + size >> 1 + 1); } for (int i = size++; i > index; --i) { data[i] = data[i - 1]; @@ -109,3 +110,116 @@ public void remove() { } } } +======= +package com.coding.basic; + +public class ArrayList implements List { + + private int size; + + private E[] data; + + public void add(E e){ + add(size, e); + } + + public ArrayList() { + clear(); + } + + public ArrayList(int capacity) { + clear(); + ensureCapacity(capacity); + } + + public void add(int index, E e){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + if (data.length == size) { + ensureCapacity(size * 2 + 1); + } + for (int i = size++; i > index; --i) { + data[i] = data[i - 1]; + } + data[index] = e; + } + + public E get(int index){ + return data[index]; + } + + public E remove(int index){ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + E copy = data[index]; + --size; + for (int i = index; i < size; ++i) { + data[i] = data[i + 1]; + } + return copy; + } + + public boolean contains(E e) { + for (int i = 0; i < size; ++i) { + if (data[i] == e) { + return true; + } + } + return false; + } + + public void clear() { + size = 0; + data = (E[]) new Object[0]; + } + + public int size(){ + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public void ensureCapacity(int capacity) { + E[] newData = (E[]) new Object[capacity]; + for (int i = 0; i < size; ++i) { + newData[i] = data[i]; + } + data = newData; + } + + public void trimToSize() { + E[] newData = (E[]) new Object[size]; + for (int i = 0; i < size; ++i) { + newData[i] = data[i]; + } + data = newData; + } + + private class ArrayListIterator implements Iterator { + int current = 0; + + public boolean hasNext() { + return current < size; + } + + public E next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + return data[current++]; + } + + public void remove() { + ArrayList.this.remove(current); + } + } +} +>>>>>>> master diff --git a/group08/729770920/2-26/src/com/coding/basic/BinaryTree.java b/group08/729770920/2-26/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..0b41529787 --- /dev/null +++ b/group08/729770920/2-26/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,105 @@ +package com.coding.basic; + +public class BinaryTree> { + BinaryTreeNode root = null; + + public BinaryTree() { + } + + public boolean isEmpty() { + return root == null; + } + + public void insert(E e) { + root = insert(root, e); + } + + private BinaryTreeNode insert(BinaryTreeNode node, E e) { + if (node == null) { + node = new BinaryTreeNode(e, null, null); + } + int compareResult = ((Comparable) e).compareTo(node.data); + if (compareResult < 0) { + node.left = insert(node.left, e); + } else if (compareResult > 0) { + node.right = insert(node.right, e); + } + return node; + } + + public void clear() { + root = null; + } + + public boolean contains(E e) { + return contains(root, e); + } + + private boolean contains(BinaryTreeNode node, E e) { + if (node == null) { + return false; + } + int compareResult = ((Comparable) e).compareTo(node.data); + if (compareResult < 0) { + return contains(node.left, e); + } else if (compareResult > 0) { + return contains(node.right, e); + } + // matching + return true; + } + + private BinaryTreeNode findMin(BinaryTreeNode node) { + if (node != null) { + while (node.left != null) { + node = node.right; + } + } + return node; + } + + private BinaryTreeNode findMax(BinaryTreeNode node) { + if (node != null) { + while (node.right != null) { + node = node.right; + } + } + return node; + } + + public void remove(E e) { + root = remove(root, e); + } + + private BinaryTreeNode remove(BinaryTreeNode node, E e) { + if (node == null) { + return node; + } + int compareResult = ((Comparable) e).compareTo(node.data); + if (compareResult < 0) { + node.left = remove(node.left, e); + } else if (compareResult > 0) { + node.right = remove(node.right, e); + } + // matching + if (node.left != null && node.right != null) { // two children + node.data = (E) findMax(node.right).data; + node.right = remove(node.right, node.data); + } else { // one child + node = (node.left != null) ? node.left : node.right; + } + return node; + } + + private class BinaryTreeNode { + E data; + BinaryTreeNode left; + BinaryTreeNode right; + + public BinaryTreeNode(E e, BinaryTreeNode l, BinaryTreeNode r) { + data = e; + left = l; + right = r; + } + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/Iterator.java b/group08/729770920/2-26/src/com/coding/basic/Iterator.java index f0e2eddde8..f432b11910 100644 --- a/group08/729770920/2-26/src/com/coding/basic/Iterator.java +++ b/group08/729770920/2-26/src/com/coding/basic/Iterator.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface Iterator { - boolean hasNext(); - - E next(); - - void remove(); -} +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + + E next(); + + void remove(); +} diff --git a/group08/729770920/2-26/src/com/coding/basic/LinkedList.java b/group08/729770920/2-26/src/com/coding/basic/LinkedList.java index 899e0be08e..6a09c84aae 100644 --- a/group08/729770920/2-26/src/com/coding/basic/LinkedList.java +++ b/group08/729770920/2-26/src/com/coding/basic/LinkedList.java @@ -1,164 +1,164 @@ -package com.coding.basic; - -public class LinkedList implements List { - private int size = 0; - private Node head = new Node<>(); - private Node tail = new Node<>(); - - public LinkedList() { - head.next = tail; - tail.prev = head; - } - - public void add(E e) { - addLast(e); - } - - public void add(int index, E e) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException(Integer.toString(index)); - } - Node cursor; - if (index < size/2) { - cursor = head; - for (int i = 0, num = index; i < num; ++i) { - cursor = cursor.next; - } - } else { - cursor = tail.prev; - for (int i = 0, num = size-index; i < num; ++i) { - cursor = cursor.prev; - } - } - cursor.next = cursor.next.prev = new Node(e, cursor, cursor.next); - ++size; - } - - public E get(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(Integer.toString(index)); - } - Node cursor; - if (index < size/2) { - cursor = head.next; - for (int i = 0; i < index; ++i) { - cursor = cursor.next; - } - } else { - cursor = tail.prev; - for (int i = 0, num = size-index-1; i < num; ++i) { - cursor = cursor.prev; - } - } - return cursor.data; - } - - public E remove(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException(Integer.toString(index)); - } - Node cursor; - if (index < size/2) { - cursor = head.next; - for (int i = 0; i < index; ++i) { - cursor = cursor.next; - } - } else { - cursor = tail.prev; - for (int i = 0, num = size-index-1; i < num; ++i) { - cursor = cursor.prev; - } - } - cursor.prev.next = cursor.next; - cursor.next.prev = cursor.prev; - --size; - return cursor.data; - } - - public int size() { - return size; - } - - public boolean isEmpty() { - return size == 0; - } - - public void addFirst(E e) { - add(0, e); - } - - public void addLast(E e) { - add(size, e); - } - - public E removeFirst() { - return remove(0); - } - - public E removeLast() { - return remove(size-1); - } - - public void clear() { - while (!isEmpty()) { - removeFirst(); - } - } - - public boolean contains(E e) { - Iterator it = this.iterator(); - while (it.hasNext()) { - if (it.next() == e) { - return true; - } - } - return false; - } - - public Iterator iterator() { - return new LinkedListIterator(); - } - - private static class Node { - E data = null; - Node prev = null; - Node next = null; - - public Node() { - } - - public Node(E e, Node p, Node n) { - data = e; - prev = p; - next = n; - } - } - - private class LinkedListIterator implements Iterator { - Node currentNode = head.next; - - public boolean hasNext() { - return currentNode != tail; - } - - public E next() { - if (!hasNext()) { - throw new java.util.NoSuchElementException(); - } - E data = currentNode.data; - currentNode = currentNode.next; - return data; - } - - public void remove() { - if (!hasNext()) { - throw new java.util.NoSuchElementException(); - } - Node nextNode = currentNode.next; - currentNode.next.prev = currentNode.prev; - currentNode.prev.next = currentNode.next; - currentNode = nextNode; - --size; - } - } -} +package com.coding.basic; + +public class LinkedList implements List { + private int size = 0; + private Node head = new Node<>(); + private Node tail = new Node<>(); + + public LinkedList() { + head.next = tail; + tail.prev = head; + } + + public void add(E e) { + addLast(e); + } + + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head; + for (int i = 0, num = index; i < num; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index; i < num; ++i) { + cursor = cursor.prev; + } + } + cursor.next = cursor.next.prev = new Node(e, cursor, cursor.next); + ++size; + } + + public E get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head.next; + for (int i = 0; i < index; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index-1; i < num; ++i) { + cursor = cursor.prev; + } + } + return cursor.data; + } + + public E remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(Integer.toString(index)); + } + Node cursor; + if (index < size/2) { + cursor = head.next; + for (int i = 0; i < index; ++i) { + cursor = cursor.next; + } + } else { + cursor = tail.prev; + for (int i = 0, num = size-index-1; i < num; ++i) { + cursor = cursor.prev; + } + } + cursor.prev.next = cursor.next; + cursor.next.prev = cursor.prev; + --size; + return cursor.data; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E e) { + add(0, e); + } + + public void addLast(E e) { + add(size, e); + } + + public E removeFirst() { + return remove(0); + } + + public E removeLast() { + return remove(size-1); + } + + public void clear() { + while (!isEmpty()) { + removeFirst(); + } + } + + public boolean contains(E e) { + Iterator it = this.iterator(); + while (it.hasNext()) { + if (it.next() == e) { + return true; + } + } + return false; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private static class Node { + E data = null; + Node prev = null; + Node next = null; + + public Node() { + } + + public Node(E e, Node p, Node n) { + data = e; + prev = p; + next = n; + } + } + + private class LinkedListIterator implements Iterator { + Node currentNode = head.next; + + public boolean hasNext() { + return currentNode != tail; + } + + public E next() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + E data = currentNode.data; + currentNode = currentNode.next; + return data; + } + + public void remove() { + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + Node nextNode = currentNode.next; + currentNode.next.prev = currentNode.prev; + currentNode.prev.next = currentNode.next; + currentNode = nextNode; + --size; + } + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/List.java b/group08/729770920/2-26/src/com/coding/basic/List.java index babed5a71f..de1390d243 100644 --- a/group08/729770920/2-26/src/com/coding/basic/List.java +++ b/group08/729770920/2-26/src/com/coding/basic/List.java @@ -1,19 +1,19 @@ -package com.coding.basic; - -public interface List { - void add(E e); - - void add(int index, E e); - - void clear(); - - E get(int index); - - E remove(int index); - - int size(); - - boolean contains(E e); - - Iterator iterator(); -} +package com.coding.basic; + +public interface List { + void add(E e); + + void add(int index, E e); + + void clear(); + + E get(int index); + + E remove(int index); + + int size(); + + boolean contains(E e); + + Iterator iterator(); +} diff --git a/group08/729770920/2-26/src/com/coding/basic/Queue.java b/group08/729770920/2-26/src/com/coding/basic/Queue.java index a660ad6729..c53fc8ed0a 100644 --- a/group08/729770920/2-26/src/com/coding/basic/Queue.java +++ b/group08/729770920/2-26/src/com/coding/basic/Queue.java @@ -1,21 +1,21 @@ -package com.coding.basic; - -public class Queue { - private LinkedList data = new LinkedList<>(); - - public void enQueue(E e){ - data.addFirst(e); - } - - public Object deQueue() { - return data.removeLast(); - } - - public boolean isEmpty() { - return data.size() == 0; - } - - public int size(){ - return data.size(); - } -} +package com.coding.basic; + +public class Queue { + private LinkedList data = new LinkedList<>(); + + public void enQueue(E e){ + data.addFirst(e); + } + + public Object deQueue() { + return data.removeLast(); + } + + public boolean isEmpty() { + return data.size() == 0; + } + + public int size(){ + return data.size(); + } +} diff --git a/group08/729770920/2-26/src/com/coding/basic/Stack.java b/group08/729770920/2-26/src/com/coding/basic/Stack.java index c7e7773de5..950b09e039 100644 --- a/group08/729770920/2-26/src/com/coding/basic/Stack.java +++ b/group08/729770920/2-26/src/com/coding/basic/Stack.java @@ -1,25 +1,25 @@ -package com.coding.basic; - -public class Stack { - private LinkedList data = new LinkedList<>(); - - public void push(E e) { - data.addFirst(e); - } - - public Object pop() { - return data.removeFirst(); - } - - public Object peek() { - return data.get(0); - } - - public boolean isEmpty() { - return data.size() == 0; - } - - public int size() { - return data.size(); - } -} +package com.coding.basic; + +public class Stack { + private LinkedList data = new LinkedList<>(); + + public void push(E e) { + data.addFirst(e); + } + + public Object pop() { + return data.removeFirst(); + } + + public Object peek() { + return data.get(0); + } + + public boolean isEmpty() { + return data.size() == 0; + } + + public int size() { + return data.size(); + } +} diff --git a/group08/769638826/2-27/ArrayList.java b/group08/769638826/2-27/ArrayList.java new file mode 100644 index 0000000000..1e6ccfcfcb --- /dev/null +++ b/group08/769638826/2-27/ArrayList.java @@ -0,0 +1,111 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日13:23:30 + */ +public class ArrayList implements List { + private int size = 0; + private static final int DEFAULT_SIZE = 16; + private Object[] elementData = null; + private int index; + + public ArrayList() { + elementData = new Object[DEFAULT_SIZE]; + } + + public ArrayList(final int size) { + elementData = new Object[size]; + } + + public void add(Object o) { + //如果当前元素个数大于数组长度的2/3 + if (size() > elementData.length * 2 / 3) { + raiseArray(); + } + + elementData[index++] = o; + size++; + } + + public void add(int index, Object o) { + checkParam(index); + + //如果当前元素个数大于数组长度的2/3 + if (size() > elementData.length * 2 / 3) { + raiseArray(); + } + + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkParam(index); + + return elementData[index]; + } + + public Object remove(int index) { + checkParam(index); + + Object o = elementData[index]; + elementData[index] = null; + size--; + return o; + } + + private void raiseArray() { + Object[] newElementData = Arrays.copyOf(elementData, size() * 2); + elementData = newElementData; + } + + private void checkParam(int index) { + if (index < 0 || index > elementData.length - 1) { + throw new IndexOutOfBoundsException(); + } + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator{ + int cursor; + + @Override + public boolean hasNext() { + return cursor != size; + } + + public void remove(){ + throw new UnsupportedOperationException(); + } + + @Override + public Object next() { + if(!hasNext()) { + throw new NoSuchElementException(); + } + + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + + Object[] elementData = ArrayList.this.elementData; + + cursor = i + 1; + + return elementData[i]; + } + } +} diff --git a/group08/769638826/2-27/ArrayListTest.java b/group08/769638826/2-27/ArrayListTest.java new file mode 100644 index 0000000000..199e3d14cb --- /dev/null +++ b/group08/769638826/2-27/ArrayListTest.java @@ -0,0 +1,74 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by huitailang on 17/2/25. + * test arraylist + */ +public class ArrayListTest { + ArrayList arrayList = null; + + @Before + public void setUp() { + arrayList = new ArrayList(); + } + + @Test + public void testArrayLength() { + int[] array = new int[10]; + Assert.assertEquals(10, array.length); + } + + @Test + public void testAddElement() { + arrayList.add(11); + Assert.assertEquals(11, arrayList.get(0)); + printElementSize(arrayList); + + for (int i = 0; i < 18; i++) { + + } + } + + @Test + public void testAriseArray() { + for (int i = 0; i < 18; i++) { + arrayList.add(i + 1); + } + + Assert.assertEquals(18, arrayList.size()); + + for (int i = 0; i < 18; i++) { + System.out.println(arrayList.get(i)); + } + } + + @Test + public void testRemoveElement() { + for (int i = 0; i < 18; i++) { + arrayList.add(i + 1); + } + + Assert.assertEquals(18, arrayList.size()); + + arrayList.remove(17); + + Assert.assertEquals(17, arrayList.size()); + + for (int i = 0; i < 18; i++) { + System.out.println(arrayList.get(i)); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testInValidGet() { + arrayList.get(19); + } + + private void printElementSize(ArrayList arrayList) { + System.out.println("array size => " + arrayList.size()); + } +} diff --git a/group08/769638826/2-27/Iterator.java b/group08/769638826/2-27/Iterator.java new file mode 100644 index 0000000000..ce3da0d118 --- /dev/null +++ b/group08/769638826/2-27/Iterator.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日16:25:42 + */ +public interface Iterator { + public boolean hasNext(); + + public Object next(); +} diff --git a/group08/769638826/2-27/LinkedList.java b/group08/769638826/2-27/LinkedList.java new file mode 100644 index 0000000000..981822c43e --- /dev/null +++ b/group08/769638826/2-27/LinkedList.java @@ -0,0 +1,190 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日13:57:58 + */ +public class LinkedList implements List { + private Node head; + private int size; + + public LinkedList() { + head = null; + size = 0; + } + + @Override + public void add(Object o) { + if (head == null) { + Node newNode = new Node(); + newNode.data = o; + head = newNode; + } + + Node oldhead = head; + head = new Node(); + head.data = o; + head.next = oldhead; + size++; + } + + @Override + public void add(int index, Object o) { + Node newNode = new Node(); + newNode.data = o; + + if (head == null) { + head = newNode; + } + + if (index < 1 || index > size + 1) { + throw new IllegalArgumentException("invalid index, it's should be 1 and" + size + 1); + } + + if (index == 1) { + newNode.next = head; + } else { + Node currentNode = head; + int count = 1; + while (count < index - 1) { + count++; + currentNode = currentNode.next; + } + newNode.next = currentNode.next; + currentNode.next = newNode; + } + } + + @Override + public Object get(int index) { + if (head == null) { + return null; + } + + if (index == 1) { + return head.next.data; + } else { + Node currentNode = head; + int count = 1; + while (count < index - 1) { + count++; + currentNode = currentNode.next; + } + + return currentNode.next.data; + } + } + + @Override + public Object remove(int index) { + Object result = null; + + if (index < 1 || index > size) { + throw new IllegalArgumentException("invalid index, it's should be 1 and " + size); + } + + if (index == 1) { + Node currentNode = head.next; + head = null; + return currentNode; + } else { + Node prevNode = head; + + int count = 1; + while (count < index - 1) { + prevNode = prevNode.next; + count++; + } + Node currentNode = prevNode.next; + prevNode.next = currentNode.next; + result = currentNode.data; + currentNode = null; + } + + return result; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + add(1, o); + } + + public void addLast(Object o) { + add(size + 1, o); + } + + public Object removeFirst() { + return remove(1); + } + + public Object removeLast() { + return remove(size); + } + + public Iterator iterator() { + return new ListIterator(); + } + + public void print() { + if (head == null) { + System.out.println("No elements in the list!"); + } + + Node currentNode = head; + while (currentNode != null) { + System.out.println(currentNode.data + "->"); + currentNode = currentNode.next; + } + + System.out.println(); + } + + public int length() { + int count = 0; + + Node currentNode = head; + while (currentNode != null) { + count++; + currentNode = currentNode.next; + } + + return count; + } + + private static class Node { + Object data; + Node next; + } + + private class ListIterator implements Iterator { + private Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + @Override + public Object next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + + Object o = current.data; + current = current.next; + return o; + } + } +} diff --git a/group08/769638826/2-27/LinkedListTest.java b/group08/769638826/2-27/LinkedListTest.java new file mode 100644 index 0000000000..786e5d93a7 --- /dev/null +++ b/group08/769638826/2-27/LinkedListTest.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:33:40 + */ +public class LinkedListTest { + +} diff --git a/group08/769638826/2-27/List.java b/group08/769638826/2-27/List.java new file mode 100644 index 0000000000..e9eba47da6 --- /dev/null +++ b/group08/769638826/2-27/List.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * + * @author zhangkun + * @date 2017年02月25日13:54:33 + */ +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/group08/769638826/2-27/Queue.java b/group08/769638826/2-27/Queue.java new file mode 100644 index 0000000000..abb7e57112 --- /dev/null +++ b/group08/769638826/2-27/Queue.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:40:02 + */ +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.get(size()); + } + + public boolean isEmpty(){ + return elementData.size() != 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group08/769638826/2-27/QueueTest.java b/group08/769638826/2-27/QueueTest.java new file mode 100644 index 0000000000..83ee1ff30a --- /dev/null +++ b/group08/769638826/2-27/QueueTest.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:44:48 + */ +public class QueueTest { + +} diff --git a/group08/769638826/2-27/Stack.java b/group08/769638826/2-27/Stack.java new file mode 100644 index 0000000000..46f80bc25d --- /dev/null +++ b/group08/769638826/2-27/Stack.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:34:22 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object o = elementData.get(1); + elementData.remove(1); + return o; + } + + public Object peek(){ + return elementData.get(1); + } + + public boolean isEmpty(){ + return elementData.size() != 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group08/769638826/2-27/StackTest.java b/group08/769638826/2-27/StackTest.java new file mode 100644 index 0000000000..5380bbaa0c --- /dev/null +++ b/group08/769638826/2-27/StackTest.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +/** + * Created by huitailang on 17/2/25. + * @author zhangkun + * @date 2017年02月25日17:44:32 + */ +public class StackTest { +} diff --git a/group08/769638826/769638826.md b/group08/769638826/769638826.md deleted file mode 100644 index 4573496090..0000000000 --- a/group08/769638826/769638826.md +++ /dev/null @@ -1,2 +0,0 @@ -###自我介绍 - diff --git a/group08/769638826/README.md b/group08/769638826/README.md new file mode 100644 index 0000000000..ff11754584 --- /dev/null +++ b/group08/769638826/README.md @@ -0,0 +1 @@ +###2017编程能力提高群 diff --git a/group08/group08.md b/group08/group08.md index d3f5a12faa..8b13789179 100644 --- a/group08/group08.md +++ b/group08/group08.md @@ -1 +1 @@ - + diff --git a/group09/1271620150/Work01/.gitignore b/group09/1271620150/Work01/.gitignore new file mode 100644 index 0000000000..1af1a6638d --- /dev/null +++ b/group09/1271620150/Work01/.gitignore @@ -0,0 +1,19 @@ +*.class +*.classpath +*.project + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +/bin/ diff --git a/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java b/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..4e6dc8c929 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/ArrayList.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private Object[] elements; + + private int size; + + public ArrayList(int initialCapacity) { + super(); + if (initialCapacity < 0) + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + this.elements = new Object[initialCapacity]; + } + + public ArrayList() { + this(10); + } + + public void add(Object obj) { + ensureCapacity(size + 1); + elements[size++] = obj; + + } + + public void add(int index, Object obj) { + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elements, index, elements, index + 1, size - index); + elements[index] = obj; + size++; + } + + public Object get(int index) { + rangeCheck(index); + return elements[index]; + } + + public Object remove(int index) { + rangeCheck(index); + Object toRemove = elements[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elements, index + 1, elements, index, numMoved); + elements[--size] = null; + return toRemove; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private void ensureCapacity(int minCapacity) { + int oldCapacity = elements.length; + if (minCapacity > oldCapacity) { + int newCapacity = oldCapacity * 2; + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elements = Arrays.copyOf(elements, newCapacity); + } + } + + private void rangeCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + this.size; + } + + private class ArrayListIterator implements Iterator{ + + private int pos = 0; + + public boolean hasNext() { + return pos != size; + } + + public Object next() { + int i = pos; + if (i >= size) + throw new NoSuchElementException(); + Object[] elements = ArrayList.this.elements; + if (i >= elements.length) + throw new ConcurrentModificationException(); + pos = i + 1; + return (Object) elements[i]; + } + } + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/Iterator.java b/group09/1271620150/Work01/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e60b443310 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java b/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..302d048d74 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/LinkedList.java @@ -0,0 +1,277 @@ +package com.coding.basic; + +import java.util.Collection; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node first; + private Node last; + private int size; + + public LinkedList() { + } + + public LinkedList(Collection c) { + this(); + addAll(size, c); + } + + private void addAll(int index, Collection c) { + checkPositionIndex(size); + + Object[] a = c.toArray(); + int numNew = a.length; + if (numNew == 0) + return; + + Node pred, succ; + if (index == size) { + succ = null; + pred = last; + } else { + succ = node(index); + pred = succ.prev; + } + + for (Object o : a) { + Node newNode = new Node(pred, o, null); + if (pred == null) + first = newNode; + else + pred.next = newNode; + pred = newNode; + } + + if (succ == null) { + last = pred; + } else { + pred.next = succ; + succ.prev = pred; + } + + size += numNew; + } + + public void add(Object o) { + linkLast(o); + } + + private void linkLast(Object o) { + Node l = last; + Node newNode = new Node(l, o, null); + last = newNode; + if (l == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + + } + + public void add(int index, Object o) { + checkPositionIndex(index); + if (index == size) { + linkLast(o); + } else { + Node l = node(index); + linkBefore(o, l); + } + + } + + public void linkBefore(Object o, Node succ) { + final Node pred = succ.prev; + final Node newNode = new Node(pred, o, succ); + succ.prev = newNode; + if (pred == null) + first = newNode; + else + pred.next = newNode; + size++; + } + + public Object get(int index) { + checkElementIndex(index); + return node(index).data; + } + + public Object remove(int index) { + checkElementIndex(index); + return unlink(node(index)); + } + + private Object unlink(Node node) { + final Object element = node.data; + final Node next = node.next; + final Node prev = node.prev; + + if (prev == null) { + first = next; + } else { + prev.next = next; + node.prev = null; + } + + if (next == null) { + last = prev; + } else { + next.prev = prev; + node.next = null; + } + + node.data = null; + size--; + return element; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + linkFirst(o); + } + + private void linkFirst(Object o) { + final Node f = first; + final Node newNode = new Node(null, o, f); + first = newNode; + if (f == null) + last = newNode; + else + f.prev = newNode; + size++; + } + + public void addLast(Object o) { + linkLast(o); + } + + public Object removeFirst() { + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return unlinkFirst(f); + } + + private Object unlinkFirst(Node f) { + final Object element = f.data; + final Node next = f.next; + f.data = null; + f.next = null; + first = next; + if (next == null) + last = null; + else + next.prev = null; + size--; + return element; + } + + public Object removeLast() { + final Node l = last; + if (l == null) + throw new NoSuchElementException(); + return unlinkLast(l); + } + + private Object unlinkLast(Node l) { + final Object element = l.data; + final Node prev = l.prev; + l.data = null; + l.prev = null; + last = prev; + if (prev == null) + first = null; + else + prev.next = null; + size--; + return element; + } + + public Iterator iterator(int index) { + return new LinkListIterator(index); + } + + private static class Node { + Object data; + Node next; + Node prev; + + Node(Node prev, Object obj, Node next) { + this.data = obj; + this.next = next; + this.prev = prev; + } + + } + + Node node(int index) { + // assert isElementIndex(index); + + if (index < (size >> 1)) { + Node x = first; + 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 boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + private class LinkListIterator implements Iterator { + private Node lastReturned = null; + private Node next; + private int nextIndex; + + LinkListIterator(int index) { + next = (index == size) ? null : node(index); + nextIndex = index; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.data; + } + + } + +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/List.java b/group09/1271620150/Work01/src/com/coding/basic/List.java new file mode 100644 index 0000000000..216d97e9ad --- /dev/null +++ b/group09/1271620150/Work01/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(); +} \ No newline at end of file diff --git a/group09/1271620150/Work01/src/com/coding/basic/Queue.java b/group09/1271620150/Work01/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4484081ac6 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Queue.java @@ -0,0 +1,44 @@ +package com.coding.basic; + +public class Queue { + private static final int CAPACITY = 10; + private int size; + private int front; + private int tail; + private Object[] array; + + public Queue(){ + this.size = CAPACITY; + array = new Object[size]; + front = tail = 0; + } + + + public void enQueue(Object o) throws Exception{ + if (size() == size -1) + throw new Exception("Queue is full"); + array[tail] = o; + tail = (tail +1) % size; + } + + public Object deQueue() throws Exception{ + Object o; + if (isEmpty()) + throw new Exception("Queue is empty"); + o = array[front]; + front = (front + 1) % size; + return o; + } + + public boolean isEmpty(){ + return (front==tail); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return (size + tail - front) % size; + } + +} diff --git a/group09/1271620150/Work01/src/com/coding/basic/Stack.java b/group09/1271620150/Work01/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..58322c0130 --- /dev/null +++ b/group09/1271620150/Work01/src/com/coding/basic/Stack.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +public class Stack { + private static final int CAPACITY = 10; + private int capacity; + private int top = -1; + Object[] array; + public Stack(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + } + public void push(Object o) throws Exception{ + if(size()== CAPACITY){ + throw new Exception("Stack is full"); + } + array[++ top] = o; + } + + public Object pop() throws Exception{ + if(isEmpty()){ + throw new Exception("Stack is empty"); + } + return array[top --]; + } + + public Object peek() throws Exception{ + if(isEmpty()){ + throw new Exception("Stack is empty"); + } + return array[top]; + } + + public boolean isEmpty(){ + return (top < 0); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return top + 1; + + } + +} \ No newline at end of file diff --git a/group09/277123057/Week01/ArrayList.java b/group09/277123057/Week01/ArrayList.java new file mode 100644 index 0000000000..5af3f5e6b0 --- /dev/null +++ b/group09/277123057/Week01/ArrayList.java @@ -0,0 +1,55 @@ +package Week01; +/* + * time:2017-2-20 21:51 created + * + */ +public class ArrayList implements List{ + + private int size = 0; + //ٵĿռֻ100 + private Object[] elementData = new Object[100]; + + //ĩλ + public void add(Object o){ + elementData[size++] = o; + } + + //ǰλԪأƶǰλڸλõԪؼкԪ + public void add(int index, Object o){ + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + //ƳָԪ,ұԪ + public Object remove(int index){ + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index+1, elementData, index, numMoved); + elementData[--size] = null; + return elementData[index]; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + private int pos = 0; + + public boolean hashNext() { + return pos < size(); + } + + public Object next() { + return elementData[pos++]; + } + } +} diff --git a/group09/277123057/Week01/BinaryTreeNode.java b/group09/277123057/Week01/BinaryTreeNode.java new file mode 100644 index 0000000000..787b5a1d76 --- /dev/null +++ b/group09/277123057/Week01/BinaryTreeNode.java @@ -0,0 +1,26 @@ +package Week01; +// +/* + *û + * */ +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; + } +} diff --git a/group09/277123057/Week01/Iterator.java b/group09/277123057/Week01/Iterator.java new file mode 100644 index 0000000000..2be5cbc254 --- /dev/null +++ b/group09/277123057/Week01/Iterator.java @@ -0,0 +1,6 @@ +package Week01; +//time +public interface Iterator { + public boolean hashNext(); + public Object next(); +} diff --git a/group09/277123057/Week01/LinkedList.java b/group09/277123057/Week01/LinkedList.java new file mode 100644 index 0000000000..014fe8f149 --- /dev/null +++ b/group09/277123057/Week01/LinkedList.java @@ -0,0 +1,173 @@ +package Week01; + +import java.util.NoSuchElementException; + +/* + * time:2017-2-22 13:00 + * οhttp://blog.csdn.net/jianyuerensheng/article/details/51204598 + * http://www.jianshu.com/p/681802a00cdf + * jdk1.8Դ + * */ + +//õ˫jdk1.6linkedList˫ѭʵ +public class LinkedList implements List { + + private int size = 0; + private Node first; //ָͷ + private Node last; //ָβڵ + + //endԪأԼaddLast() + public void add(Object o){ + addLast(o); + } + + //index,δοͬͬѧ spike + public void add(int index, Object o){ + if (index < 0 || index > size) + throw new IllegalArgumentException(); + size++; + if (index == size){ + addLast(o); + }else{ + Node target = findIndex(index); + Node newNode = new Node(o, target,target.next); + if (last == target){ + last = newNode; + }else{ + //target.next = newNode;ҪҪ + target.next.prev = newNode;//е + } + } + size++; + } + + public Object get(int index){ + if ( index < 0 || index > size){ + throw new IllegalArgumentException(); + } + return findIndex(index).data; + } + //ɾindexָԪ + public Object remove(int index){ + if (index < 0 || index > size){ + throw new IllegalArgumentException(); + } + + Node target = findIndex(index); + if (target == first){ + first = first.next; + first.prev = null; + }else if(target == last){ + last = last.prev; + last.next = null; + }else{ + target.prev.next = target.next; + target.next.prev = target.prev; + } + return target.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + + Node f = first; + Node newNode = new Node(o,null,f); + first = newNode; + if (f == null) + last = newNode; //fΪnull˵ֻlastָ + else + f.prev = newNode; + size++; + } + + public void addLast(Object o){ + Node l = last; + Node newNode = new Node(o, l, null); + last = newNode; + if (l == null) + first = newNode; + else + l.next = newNode; + size++; + } + + + public Object removeFirst() { + if ( first == null) + throw new NoSuchElementException(); + Node f = first; + Object data = f.data; + Node next = f.next; + //ȥԪָΪnull + f.data = null; + f.next = null; + first = next; + if (next == null) + last = null; + else + next.prev = null; + size--; + return data; + } + + public Object removeLast(){ + if (last == null) + throw new NoSuchElementException(); + Node l = last; + Object data = l.data; + Node previous = l.prev; + l.data = null; + l.prev = null; + last = previous; + if (previous == null) + first = null; + else + previous.next = null; + size--; + return data; + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + Node curNode = first; + public boolean hashNext() { + return curNode != null; + } + public Object next() { + if (!hashNext()) + throw new NoSuchElementException(); + Object data = curNode.data; + curNode = curNode.next; + return data; + } + } + private Node findIndex(int index) { + Node target = first; + int i = 0; + while(i < index){ + target = target.next; + i++; + } + return target; + } + + // + private static class Node{ + private Object data; + //ĬҲnull + private Node prev = null; //һԪؽڵ + private Node next = null;//һԪؽڵ + + public Node(Object data, Node pre, Node next){ + this.data = data; + this.prev = pre; + this.next = next; + } + } +} diff --git a/group09/277123057/Week01/List.java b/group09/277123057/Week01/List.java new file mode 100644 index 0000000000..733a02a0d6 --- /dev/null +++ b/group09/277123057/Week01/List.java @@ -0,0 +1,9 @@ +package Week01; +//time: +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/group09/277123057/Week01/Queue.java b/group09/277123057/Week01/Queue.java new file mode 100644 index 0000000000..dd786574b8 --- /dev/null +++ b/group09/277123057/Week01/Queue.java @@ -0,0 +1,29 @@ +package Week01; +/* + * time:2017-2-25 13:46 created by Doen + * + * */ +public class Queue { + private LinkedList elementData = new LinkedList(); + // + + public void enQueue(Object o){ + elementData.add(o); + } + + // + public Object deQueue(){ + if (isEmpty()) + throw new UnsupportedOperationException(); + return elementData.remove(0); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } + +} diff --git a/group09/277123057/Week01/Stack.java b/group09/277123057/Week01/Stack.java new file mode 100644 index 0000000000..9a10468385 --- /dev/null +++ b/group09/277123057/Week01/Stack.java @@ -0,0 +1,30 @@ +package Week01; + +import java.util.NoSuchElementException; + +/* + * time:2017-2-25 13:19 created by Doen + * change + * */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) + throw new NoSuchElementException(); + return elementData.remove(elementData.size()-1); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group09/277123057/Week01/Test.java b/group09/277123057/Week01/Test.java new file mode 100644 index 0000000000..5d4517d9af --- /dev/null +++ b/group09/277123057/Week01/Test.java @@ -0,0 +1,9 @@ +package Week01; +//time +public class Test { + public static void main(String[] args){ + ArrayList arraylist = new ArrayList(); + arraylist.add(1); + arraylist.add("A"); + } +} diff --git "a/group09/396077060/20170226/article/\350\256\241\347\256\227\346\234\272\345\237\272\347\241\200\347\237\245\350\257\206.txt" "b/group09/396077060/20170226/article/\350\256\241\347\256\227\346\234\272\345\237\272\347\241\200\347\237\245\350\257\206.txt" new file mode 100644 index 0000000000..70bbd879af --- /dev/null +++ "b/group09/396077060/20170226/article/\350\256\241\347\256\227\346\234\272\345\237\272\347\241\200\347\237\245\350\257\206.txt" @@ -0,0 +1,9 @@ +现代计算机基本都是冯·诺依曼结构,而冯·诺依曼结构中,最核心的思想便是“存储程序思想”,即:把运算程序存在机器的存储器中。 + +在这种结构下,计算机被分成了五个部分:运算器、控制器、存储器、输入设备和输出设备。其中运算器和控制器构成了CPU的最重要部分。早期的冯·诺依曼结构型计算机是以运算器为核心的,譬如穿孔纸带机,运算核心通过从穿孔纸带中获取控制信息与运算信息完成指定的任务。在这种结构下,任何操作都要先与运算核心打交道。直到后来发明了内存,将大量的控制信息与运算信息存储到集成电路上,让计算机有了飞跃式的发展。内存拥有着大大超过于穿孔纸带的存储效率,于是,计算机的中心慢慢地从运算器转移到了存储器。在这种模式下,运算器、控制器、以至于输入输出设备,都是通过与内存交换信息得到很好的协作。 + +存储器的读写效率确实高,但是它也有着单位造价高和相对容量较小的缺点,虽然现在SSD已经也很便宜了,但是对于以前的工程师来说,这都是不可想象的。于是乎,经过伟大的科学家们与工程师们的探索,他们发现了计算机程序运行过程中的“局部性原理”。“局部性原理”包括时间局部性和空间局部性,时间局部性是指:一段被访问过的程序在不久的时间内很有可能会被再次访问,空间局部性是指:一段被访问过的程序的临近的程序很可能马上被访问到。在有的资料中还提到了顺序局部性,它的意思是指:大部分程序是顺序执行的。在“局部性原理”的指导下,存储分级结构便出现了。存储器被分为若干个级别,越靠近CPU计算速度越快但存储容量小、造价高,如寄存器、Cache,越远离CPU,从内存到硬盘,计算速度越慢,但容量大,造价也越低。采用这种结构,最终使工程师们在性能与造价上得到了很好的平衡。 + +再回到冯·诺依曼结构来,它的“把程序存储起来”到底是什么意思呢?好比我们照着菜谱学做菜,菜谱就是存储好的程序。我们要按照菜谱上的步骤,从选材、买菜、炒菜、出锅、上桌等一系列步骤来达到最终的目标。“存储程序”的思想就是要求把这些基本的步骤存到存储器中,然后当我们要实现某一运算的时候,就将实现这一运算所需要的操作调取出来。菜谱上的一条条步骤,就相当于计算机中的指令,程序指令指挥着计算机各个部件的运行。 + +一个计算机指令包含操作码和操作数两个部分,顾名思义,操作码指示的是计算机需要做的操作,操作数则标识了某一次操作需要用到的数据。由于指令长度有限,操作数的最大寻址范围又远小于内存的寻址范围,于是操作数的获取方式又分为很多种,包括立即数寻址、直接寻址、间接寻址、相对寻址、基址寻址、变址寻址等。一条指令的执行主要包括如下几个步骤:取指令、分析指令、执行指令,有的指令还包括内存写回。 diff --git a/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java new file mode 100644 index 0000000000..c256ab61aa --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/ArrayList.java @@ -0,0 +1,174 @@ +/** +* @Title: ArrayList.java +* @Description: ArrayList的实现 +* @author glorychou +* @date 2017年2月22日 下午10:41:58 +*/ +package per.zyf.bds; + +import java.util.Arrays; + +/** + * ArrayList的存储结构其实就是一维数组 + * 只不过在这个类中将对数组的一些操作(包括添加元素、删除元素等)封装了起来 + * @author glorychou + * + * @param + * @see per.zyf.bds.List + */ +public class ArrayList implements List { + // 默认数组大小 + private static final int DEFAULT_CAPACITY = 10; + + // 数组实际大小 + private int size; + + // 存储元素的数组 + protected Object[] elementData; + + // 一个用来记录初始状态的空数组实例 + private static final Object[] CAPACITY_EMPTY_ELEMENTDATA = {}; + + /*** + * 构造初始元素数组 + */ + public ArrayList() { + this.elementData = CAPACITY_EMPTY_ELEMENTDATA; + } + + /*** + * + * @Description: 在末尾添加元素 + * @param e 元素 + */ + @Override + public boolean add(E e) { + int minCapacity = size + 1; + + // 判断数组中是否有元素 + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + + // 判断是否溢出 + if (minCapacity - elementData.length > 0) + grow(minCapacity); + + // 添加元素 + elementData[size++] = e; + + return true; + } + + /*** + * + * @Description: 在索引指定位置增加元素 + * @param index 索引 + * @param e 元素 + */ + @Override + public boolean add(int index, E e) { + int minCapacity = size + 1; + + // 索引位置不合法抛出异常 + rangeCheck(index); + + // 判断数组中是否有元素 + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + + // 判断是否溢出 + if (minCapacity - elementData.length > 0) + grow(minCapacity); + + // 插入点后的元素后移 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + // 在索引处加入数据 + elementData[index] = e; + + return true; + } + + /*** + * + * @Description: 得到索引指定位置的元素 + * @param index 索引 + * @return E 索引指定的元素 + */ + @Override + @SuppressWarnings("unchecked") + public E get(int index) { + // 索引位置不合法抛出异常 + rangeCheck(index); + + return (E) elementData[index]; + } + + /*** + * + * @Description: 删除索引指定位置的元素 + * @param index 索引 + * @return void + */ + @Override + @SuppressWarnings("unchecked") + public E remove(int index) { + // 索引位置不合法抛出异常 + rangeCheck(index); + + E removeElement = (E) elementData[index]; + + // 将要移除元素后的元素前移 + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + // 数组大小减一,并清除多余元素 + elementData[--size] = null; + + return removeElement; + } + + /* + * @see per.zyf.bds.List#size() + */ + @Override + public int size() { + return size; + } + + + /* + * @see per.zyf.bds.List#isEmpty() + */ + @Override + public boolean isEmpty() { + if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { + return true; + } + return false; + } + + /*** + * + * @Description: 溢出时增长空间 + * @param minCapacity 最小容量 + * @return void + */ + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + // 容量增大一半 + int newCapacity = oldCapacity + (oldCapacity >> 1); + elementData = Arrays.copyOf(elementData, newCapacity); + } + + /*** + * + * @Description: 索引范围检查 + * @param index 索引 + * @return void + */ + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } +} \ No newline at end of file diff --git a/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java new file mode 100644 index 0000000000..062894ec95 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/BinaryTree.java @@ -0,0 +1,96 @@ +/** +* @Title: BinaryTree.java +* @Description: 二叉排序树的实现 +* @author glorychou +* @date 2017年2月25日 下午10:22:03 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public class BinaryTree> { + // 根节点 + private Node root; + // 树大小 + private int size; + + /** + * @Description: 在树中插入元素 + * @param e 节点数据 + * @return boolean 处理情况 + */ + public boolean add(E e) { + + // 创建新节点 + final Node newNode = new Node<>(null, e, null); + + // 按照二叉排序方式插入 + if (root != null) { + Node parentNode = null; + Node compareNode = root; + + while(compareNode != null) { + // 新节点大于比较节点则插入右子树中 + if(e.compareTo(compareNode.item) > 0) { + parentNode = compareNode; + compareNode = compareNode.rightChild; + + if(compareNode == null) + parentNode.rightChild = newNode; + } else {// 新节点小于或等于比较节点则插入左子树中 + parentNode = compareNode; + compareNode = compareNode.leftChild; + + if(compareNode == null) + parentNode.rightChild = newNode; + } + } + } else + root = newNode; + + size++; + return true; + } + + /** + * @Description: 中序遍历输出 + * @return void 返回类型 + */ + public void inorderPrint(Node e) { + if(e == null) return; + inorderPrint(e.leftChild); + System.out.print(e.item.toString() + " "); + inorderPrint(e.rightChild); + } + + /** + * @Description: 判断树是否为空 + * @return boolean 是否为空 + */ + public boolean isEmpty() { + return size > 0 ? false : true; + } + + /** + * @Description: 获取树的节点数 + * @return int 树节点数 + */ + public int size() { + return size; + } + + // 树节点 + private static class Node { + E item; + Node leftChild; + Node rightChild; + + Node(Node l, E e, Node r) { + leftChild = l; + item = e; + rightChild = r; + } + } +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java new file mode 100644 index 0000000000..07eb7d7d46 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/LinkedList.java @@ -0,0 +1,252 @@ +/** +* @Title: LinkedList.java +* @Description: 双向链表的实现 +* @author glorychou +* @date 2017年2月24日 上午12:23:00 +*/ +package per.zyf.bds; + +/** + * LinkedList的存储结构其实就是链表 + * @author glorychou + * + * @param + * @see per.zyf.bds.List + */ +/** + * @author glorychou + * + * @param + */ +/** + * @author glorychou + * + * @param + */ +public class LinkedList implements List { + // 链表头 + private Node first; + + // 链表尾 + private Node last; + + // 链表大小 + private int size; + + /* + * @see per.zyf.bds.List#add(java.lang.Object) + */ + @Override + public boolean add(E e) { + linkLast(e); + return true; + } + + /* + * @see per.zyf.bds.List#add(int, java.lang.Object) + */ + @Override + public boolean add(int index, E e) { + // 判断索引是否在合理的位置 + rangeCheck(index); + + // 索引值为链表长度,则添加到链表末尾 + if (index == size) { + linkLast(e); + } else { + // 找到索引指定的节点,然后将新节点插入该节点后面 + final Node p = node(index); + final Node newNode = new Node<>(p, e, p.next); + p.next = newNode; + size++; + } + + return true; + } + + /* + * @see per.zyf.bds.List#get(int) + */ + @Override + public E get(int index) { + return node(index).item; + } + + + /* (non-Javadoc) + * @see per.zyf.bds.List#remove(int) + */ + @Override + public E remove(int index) { + rangeCheck(index); + + Node p = node(index); + E e = p.item; + + // 所需删除节点的前面有节点,则改变前一节点的下一跳 + if(p.prev != null) + p.prev.next = p.next; + // 所需删除节点的后面有节点,则改变后一节点的上一跳 + if(p.next != null) + p.next.prev = p.prev; + + // 清空数据 + p.prev = null; + p.item = null; + p.next = null; + + size--; + + return e; + } + + /** + * @Description: 在链表头部增加节点 + * @param e 新节点数据 + * @return void 返回类型 + */ + public void addFirst(E e) { + final Node newNode = new Node<>(null, e, first); + first.prev = newNode; + size++; + } + + /** + * @Description: 移除首节点 + * @return E 所删除节点的数据 + */ + public E removeFirst() { + if(first != null) { + final E e = first.item; + final Node n = first.next; + + if(n != null) + n.prev = null; + + // 清空数据,让GC来回收内存 + first.item = null; + first.next = null; + // 指定新的头节点 + first = n; + + size--; + return e; + } else + return null; + } + + /** + * @Description: 删除最后一个节点 + * @return 设定文件 + * @return E 所删除数据 + * @throws + */ + public E removeLast() { + if(last != null) { + final E e = last.item; + final Node p = last.prev; + + if(p != null) + p.next = null; + + // 清空数据,让GC来回收内存 + last.item = null; + last.prev = null; + // 指定新的尾节点 + last = p; + + size--; + return e; + } else + return null; + } + + /* (non-Javadoc) + * @see per.zyf.bds.List#size() + */ + @Override + public int size() { + return size; + } + + /* (non-Javadoc) + * @see per.zyf.bds.List#isEmpty() + */ + @Override + public boolean isEmpty() { + return true; + } + + /** + * @Description: 在链尾增加节点 + * @param e 新节点数据 + * @return void 返回类型 + */ + private void linkLast(E e) { + // 保存旧链尾节点 + final Node p = last; + + // 创建新节点,并将新节点设置为链尾节点 + final Node newNode = new Node<>(p, e, null); + last = newNode; + + // 若链表无节点,则将新节点设置为表头节点 + if (p == null) + first = newNode; + else // 若链表已经有节点,则将新节点设置为表尾节点 + p.next = newNode; + + // 表长度加一 + size++; + } + + /*** + * + * @Description: 索引范围检查 + * @param index 索引 + * @return void + */ + private void rangeCheck(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + + /** + * @Description: 获取索引位置的节点 + * @param index 索引 + * @return Node 指定的节点 + */ + private Node node(int index) { + // 判断索引是否在合理的位置 + rangeCheck(index); + + Node specifyNode; + + // 根据判断索引位置在链表的前半部还是后半部,选择不同的检索方式获取指定节点 + if(index < (size >> 1)) { + specifyNode = first; + for (int i = 0; i < index; i++) + specifyNode = specifyNode.next; + } else { + specifyNode = last; + for (int i = 0; i > index; i--) + specifyNode = specifyNode.prev; + } + + return specifyNode; + } + + // 链表节点 + private static class Node { + E item; + Node next; + Node prev; + + Node(Node prev, E e, Node next) { + this.prev = prev; + this.item = e; + this.next = next; + } + } + +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/List.java b/group09/396077060/20170226/src/per/zyf/bds/List.java new file mode 100644 index 0000000000..d8edbaabce --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/List.java @@ -0,0 +1,55 @@ +/** +* @Title: List.java +* @Description: List接口的实现 +* @author glorychou +* @date 2017年2月24日 下午3:02:34 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public interface List { + /** + * @Description: 添加元素 + * @param e 所需增加元素 + * @return boolean 处理结果 + */ + boolean add(E e); + + /** + * @Description: 在索引指定位置增加元素 + * @param index 索引 + * @param e 所需增加元素 + * @return boolean 处理结果 + */ + boolean add(int index, E e); + + /** + * @Description: 获取指定元素 + * @param index 索引 + * @return E 返回元素 + */ + E get(int index); + + /** + * @Description: 删除指定元素 + * @param index 索引 + * @return E 返回被删除的元素 + */ + E remove(int index); + + /** + * @Description: 获取List容量 + * @return int List容量 + */ + int size(); + + /** + * @Description: 判断是否为空 + * @return boolean 是否为空 + */ + boolean isEmpty(); + +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/Queue.java b/group09/396077060/20170226/src/per/zyf/bds/Queue.java new file mode 100644 index 0000000000..39c66972a0 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/Queue.java @@ -0,0 +1,59 @@ +/** +* @Title: Queue.java +* @Description: 队列的实现 +* @author glorychou +* @date 2017年2月24日 下午3:10:08 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public class Queue { + // 队列元素 + private LinkedList elementData; + + // 队列大小 + private int size; + + public Queue() { + elementData = new LinkedList<>(); + } + + /** + * @Description: 入队 + * @param e 入队数据 + * @return void 返回类型 + */ + public void enQueue(E e) { + elementData.add(e); + size = elementData.size(); + } + + /** + * @Description: 出队 + * @return E 出队的数据 + */ + public E deQueue() { + final E e = elementData.removeFirst(); + size = elementData.size(); + return e; + } + + /** + * @Description: 判断队列是否为空 + * @return boolean 是否为空 + */ + public boolean isEmpty() { + return size > 0 ? false : true; + } + + /** + * @Description: 获取队列大小 + * @return int 队列大小 + */ + public int size() { + return size; + } +} diff --git a/group09/396077060/20170226/src/per/zyf/bds/Stack.java b/group09/396077060/20170226/src/per/zyf/bds/Stack.java new file mode 100644 index 0000000000..8e3f70eb51 --- /dev/null +++ b/group09/396077060/20170226/src/per/zyf/bds/Stack.java @@ -0,0 +1,71 @@ +/** +* @Title: Stack.java +* @Description: 栈的实现 +* @author glorychou +* @date 2017年2月24日 下午3:05:29 +*/ +package per.zyf.bds; + +/** + * @author glorychou + * + */ +public class Stack { + // 栈元素 + private ArrayList elementData; + + // 栈大小 + private int size; + + /** + * 初始化栈 + */ + public Stack() { + elementData = new ArrayList<>(); + } + + /** + * @Description: 压栈 + * @param e 数据 + * @return void 返回类型 + */ + public void push(E e) { + elementData.add(e); + size = elementData.size(); + } + + /** + * @Description: 弹栈 + * @return E 所弹出的数据 + */ + public E pop() { + // 移除最后一项数据 + final E e = elementData.remove(elementData.size()); + size = elementData.size(); + return e; + } + + /** + * @Description: 获取栈顶元素 + * @return E 返回栈顶元素数据 + */ + public E peek() { + return elementData.get(size - 1); + } + + /** + * @Description: 判断栈是否为空 + * @return boolean 返回类型 + */ + public boolean isEmpty() { + return size > 0 ? false : true; + } + + /** + * @Description: 获取栈大小 + * @return int 栈大小 + */ + public int size() { + return size; + } +} diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore new file mode 100644 index 0000000000..e10e727be5 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/.gitignore @@ -0,0 +1 @@ +/.metadata/ diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project new file mode 100644 index 0000000000..47cffea382 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.project @@ -0,0 +1,17 @@ + + + collection-aggregator + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml new file mode 100644 index 0000000000..a09edb357b --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/pom.xml @@ -0,0 +1,13 @@ + + 4.0.0 + com.eulerlcs.collection + collection-aggregator + 0.0.1-SNAPSHOT + pom + + + ../collection-parent + ../collection-lib + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-aggregator/src/site/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath new file mode 100644 index 0000000000..5131f04311 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.classpath @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project new file mode 100644 index 0000000000..69766f62bc --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.project @@ -0,0 +1,23 @@ + + + collection-lib + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..714351aec1 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml new file mode 100644 index 0000000000..6d1654e23a --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + + com.eulerlcs.collection + collection-parent + 0.0.1-SNAPSHOT + ../collection-parent/pom.xml + + collection-lib + + + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + junit + junit + + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java new file mode 100644 index 0000000000..219919719b --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/java/com/eulerlcs/collection/ArrayList.java @@ -0,0 +1,438 @@ +/** + * 90% or more copy from jdk + */ +package com.eulerlcs.collection; + +import java.util.Arrays; +import java.util.Collection; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.RandomAccess; +import java.util.function.Consumer; + +public class ArrayList implements List, RandomAccess { + private static final int MAXQARRAYQSIZE = 1 << 10; + private transient Object[] elementData = new Object[0]; + private int size; + private transient int modCount = 0; + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(Object o) { + if (o == null) { + for (Object obi : elementData) { + if (obi == null) { + return true; + } + } + } else { + for (Object obj : elementData) { + if (o.equals(obj)) { + return true; + } + } + } + return false; + } + + @Override + public boolean containsAll(Collection c) { + for (Object e : c) + if (!contains(e)) + return false; + return true; + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size, elementData.getClass()); + } + + @SuppressWarnings("unchecked") + @Override + public T[] toArray(T[] a) { + if (a.length < size) { + return (T[]) Arrays.copyOf(elementData, size, a.getClass()); + } else { + System.arraycopy(elementData, 0, a, 0, size); + if (a.length > size) + a[size] = null; + return a; + } + } + + @Override + public boolean add(E e) { + ensureExplicitCapacity(size + 1); // Increments modCount!! + elementData[size] = e; + size++; + return true; + } + + @Override + public void add(int index, E element) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + ensureExplicitCapacity(size + 1); // Increments modCount!! + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = element; + size++; + } + + @Override + public E remove(int index) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + modCount++; + @SuppressWarnings("unchecked") + E oldValue = (E) elementData[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null;// clear to let GC do its work + + return oldValue; + } + + @Override + public boolean remove(Object o) { + int index = -1; + + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) { + index = i; + break; + } + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) { + index = i; + break; + } + } + + if (index > 0) { + modCount++; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null;// clear to let GC do its work + + return true; + } + + return false; + } + + @Override + public boolean removeAll(Collection c) { + boolean modified = false; + for (Object obj : c) { + modified |= remove(obj); + } + + return modified; + } + + @Override + public boolean addAll(Collection c) { + Object[] a = c.toArray(); + int numNew = a.length; + ensureExplicitCapacity(size + numNew);// Increments modCount + System.arraycopy(a, 0, elementData, size, numNew); + size += numNew; + return numNew != 0; + } + + @Override + public boolean addAll(int index, Collection c) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + Object[] a = c.toArray(); + int numNew = a.length; + ensureExplicitCapacity(size + numNew);// Increments modCount + + int numMoved = size - index; + if (numMoved > 0) + System.arraycopy(elementData, index, elementData, index + numNew, numMoved); + + System.arraycopy(a, 0, elementData, index, numNew); + size += numNew; + return numNew != 0; + } + + @Override + public boolean retainAll(Collection c) { + final Object[] elementData = this.elementData; + int r = 0, w = 0; + boolean modified = false; + for (; r < size; r++) + if (c.contains(elementData[r])) + elementData[w++] = elementData[r]; + + if (w != size) { + // clear to let GC do its work + for (int i = w; i < size; i++) + elementData[i] = null; + modCount += size - w; + size = w; + modified = true; + } + + return modified; + } + + @Override + public void clear() { + modCount++; + for (int i = 0; i < size; i++) + elementData[i] = null; + + size = 0; + } + + @Override + public List subList(int fromIndex, int toIndex) { + throw new UnsupportedOperationException(); + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + return (E) elementData[index]; + } + + @SuppressWarnings("unchecked") + @Override + public E set(int index, E element) { + if (index >= size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size:" + size); + + E oldValue = (E) elementData[index]; + elementData[index] = element; + return oldValue; + } + + @Override + public int indexOf(Object o) { + if (o == null) { + for (int i = 0; i < size; i++) + if (elementData[i] == null) + return i; + } else { + for (int i = 0; i < size; i++) + if (o.equals(elementData[i])) + return i; + } + + return -1; + } + + @Override + public int lastIndexOf(Object o) { + if (o == null) { + for (int i = size - 1; i >= 0; i--) + if (elementData[i] == null) + return i; + } else { + for (int i = size - 1; i >= 0; i--) + if (o.equals(elementData[i])) + return i; + } + + return -1; + } + + private void ensureExplicitCapacity(int minCapacity) { + modCount++; + + if (elementData.length > minCapacity) { + return; + } else if (minCapacity > MAXQARRAYQSIZE) { + throw new OutOfMemoryError(); + } + + int oldCapacity = elementData.length; + + int newCapacity = oldCapacity == 0 ? 10 : (oldCapacity + (oldCapacity >> 1)); + if (newCapacity > MAXQARRAYQSIZE) { + newCapacity = MAXQARRAYQSIZE; + } + + elementData = Arrays.copyOf(elementData, newCapacity); + } + + @Override + public Iterator iterator() { + return new Itr(); + } + + @Override + public ListIterator listIterator() { + return new ListItr(0); + } + + @Override + public ListIterator listIterator(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: " + index); + return new ListItr(index); + } + + /** + * fully copy from jdk ArrayList.Itr + */ + private class Itr implements Iterator { + int cursor; // index of next element to return + int lastRet = -1; // index of last element returned; -1 if no such + int expectedModCount = modCount; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + @SuppressWarnings("unchecked") + public E next() { + checkForComodification(); + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return (E) elementData[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + @SuppressWarnings("unchecked") + public void forEachRemaining(Consumer consumer) { + Objects.requireNonNull(consumer); + final int size = ArrayList.this.size; + int i = cursor; + if (i >= size) { + return; + } + final Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) { + throw new ConcurrentModificationException(); + } + while (i != size && modCount == expectedModCount) { + consumer.accept((E) elementData[i++]); + } + // update once at end of iteration to reduce heap write traffic + cursor = i; + lastRet = i - 1; + checkForComodification(); + } + + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + /** + * fully copy from jdk ArrayList.ListItr + */ + private class ListItr extends Itr implements ListIterator { + ListItr(int index) { + super(); + cursor = index; + } + + @Override + public boolean hasPrevious() { + return cursor != 0; + } + + @Override + public int nextIndex() { + return cursor; + } + + @Override + public int previousIndex() { + return cursor - 1; + } + + @Override + @SuppressWarnings("unchecked") + public E previous() { + checkForComodification(); + int i = cursor - 1; + if (i < 0) + throw new NoSuchElementException(); + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cursor = i; + return (E) elementData[lastRet = i]; + } + + @Override + public void set(E e) { + if (lastRet < 0) + throw new IllegalStateException(); + checkForComodification(); + + try { + ArrayList.this.set(lastRet, e); + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + @Override + public void add(E e) { + checkForComodification(); + + try { + int i = cursor; + ArrayList.this.add(i, e); + cursor = i + 1; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + } +} diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/resources/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/main/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java new file mode 100644 index 0000000000..b66dd278e3 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/java/com/eulerlcs/collection/TestArrayList.java @@ -0,0 +1,44 @@ +package com.eulerlcs.collection; + +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestArrayList { + + @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 test_foreach() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + + int sum = 0; + for (Integer item : list) { + sum += item; + } + + Assert.assertEquals(sum, 6); + } +} diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/resources/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-lib/src/test/resources/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project new file mode 100644 index 0000000000..3467a254de --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.project @@ -0,0 +1,17 @@ + + + collection-parent + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml new file mode 100644 index 0000000000..6f05e517e2 --- /dev/null +++ b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/pom.xml @@ -0,0 +1,112 @@ + + 4.0.0 + com.eulerlcs.collection + collection-parent + 0.0.1-SNAPSHOT + pom + + + + 1.7.23 + 4.12 + 2.17 + 1.8 + 1.8 + + + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + + + junit + junit + ${junit.version} + test + + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + attach-source + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.4 + + true + 1.8 + protected + UTF-8 + UTF-8 + UTF-8 + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.19.1 + + true + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.17 + + + + + + + org.apache.maven.plugins + maven-source-plugin + + + + \ No newline at end of file diff --git a/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/src/site/.gitkeep b/group09/41689722.eulerlcs/20170226-eulerlcs-collection/collection-parent/src/site/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group09/601862675/Week01/pom.xml b/group09/601862675/Week01/pom.xml new file mode 100644 index 0000000000..d457efbf95 --- /dev/null +++ b/group09/601862675/Week01/pom.xml @@ -0,0 +1,29 @@ + + + + coding2017 + me.sidzh + 1.0-SNAPSHOT + + 4.0.0 + + Week01 + + + junit + junit + 4.12 + test + + + com.github.stefanbirkner + system-rules + 1.16.0 + test + + + + + \ No newline at end of file diff --git a/group09/601862675/Week01/src/main/java/ArrayList.java b/group09/601862675/Week01/src/main/java/ArrayList.java new file mode 100644 index 0000000000..1923ba3592 --- /dev/null +++ b/group09/601862675/Week01/src/main/java/ArrayList.java @@ -0,0 +1,102 @@ +public class ArrayList implements List{ + + private Object[] elements; + + private static final int INITIAL_SIZE = 16; + + public static final int MAX_LIST_SIZE = 48; + + private int size = 0; + + private int capacity = 0; + + public ArrayList() { + elements = new Object[INITIAL_SIZE]; + capacity = INITIAL_SIZE; + } + + public void add(int index, Object obj) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException(); + } + ensureSpace(); + if (index == size) { + add(obj); + } else { + System.arraycopy(elements, index, elements, index + 1, size - index); + elements[index] = obj; + size++; + } + } + + public void add(Object obj) { + ensureSpace(); + elements[size++] = obj; + } + + public int size() { + return size; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("List: [ "); + for (int i = 0; i < size; ++ i) { + builder.append(elements[i]).append(" "); + } + builder.append("]"); + return builder.toString(); + } + + private void ensureSpace() { + if (size == capacity) { + if (size == MAX_LIST_SIZE) { + throw new IndexOutOfBoundsException(); + } + int newCapacity = capacity*2 > MAX_LIST_SIZE ? MAX_LIST_SIZE : capacity*2; + grow(newCapacity); + } + } + + private void grow(int newLength) { + Object[] newElements = new Object[newLength]; + System.arraycopy(elements, 0, newElements, 0, elements.length); + elements = newElements; + capacity = newLength; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException(); + } + + return elements[index]; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException(); + } + Object toRemove = elements[index]; + System.arraycopy(elements, index + 1, elements, index, size - index -1); + --size; + return toRemove; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int pos = 0; + + public boolean hasNext() { + return pos < size(); + } + + public Object next() { + return elements[pos++]; + } + } +} diff --git a/group09/601862675/Week01/src/main/java/BinaryTree.java b/group09/601862675/Week01/src/main/java/BinaryTree.java new file mode 100644 index 0000000000..2aa7d4a43e --- /dev/null +++ b/group09/601862675/Week01/src/main/java/BinaryTree.java @@ -0,0 +1,29 @@ +public class BinaryTree { + + private BinaryTreeNode root; + + private static class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + } + + public BinaryTree getLeft() { + + + return null; + } + + public void setLeft() { + + } + + public BinaryTree getRight() { + + return null; + } + + public void setRight() { + + } +} diff --git a/group09/601862675/Week01/src/main/java/Iterator.java b/group09/601862675/Week01/src/main/java/Iterator.java new file mode 100644 index 0000000000..f4f8fa52cd --- /dev/null +++ b/group09/601862675/Week01/src/main/java/Iterator.java @@ -0,0 +1,7 @@ +/** + * Created by spike on 2/19/17. + */ +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group09/601862675/Week01/src/main/java/LinkedList.java b/group09/601862675/Week01/src/main/java/LinkedList.java new file mode 100644 index 0000000000..dca3992000 --- /dev/null +++ b/group09/601862675/Week01/src/main/java/LinkedList.java @@ -0,0 +1,128 @@ +/** + * Created by spike on 2/19/17. + */ +public class LinkedList implements List { + + private LinkedListNode head; + private LinkedListNode tail; + private int size; + + private static class LinkedListNode { + private Object data; + private LinkedListNode prev; + private LinkedListNode next; + + private LinkedListNode(Object data, LinkedListNode prev, LinkedListNode next) { + this.data = data; + this.prev = prev; + this.next = next; + } + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("List: [ "); + LinkedListNode idx = head; + while (idx != null) { + builder.append(idx.data); + builder.append(" "); + idx = idx.next; + } + + builder.append("]"); + return builder.toString(); + } + + public void add(int index, Object object) { + if (index < 0 || index > size) { + throw new IllegalArgumentException(); + } + if (index == size) { // insert after + add(object); + } else { // insert before + LinkedListNode target = findNodeByIndex(index); + LinkedListNode nd = new LinkedListNode(object, target.prev, target); + if (head == target) { + head = nd; + } else { + target.prev.next = nd; + } + } + ++size; + } + + public void add(Object object) { + if (head == null) { + LinkedListNode nd = new LinkedListNode(object, null, null); + head = tail = nd; + } else { + LinkedListNode nd = new LinkedListNode(object, tail, null); + tail.next = nd; + tail = nd; + } + ++size; + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException(); + } + LinkedListNode target = findNodeByIndex(index); + return target.data; + } + + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IllegalArgumentException(); + } + LinkedListNode target = findNodeByIndex(index); + if (target == head) { + if (head == tail) { + head = tail = null; + } else { + head = head.next; + head.prev = null; + } + } else if (target == tail) { + tail = tail.prev; + tail.next = null; + } else { + target.prev.next = target.next; + target.next.prev = target.prev; + } + target.prev = target.next = null; + --size; + return target.data; + } + + private LinkedListNode findNodeByIndex(int index) { + LinkedListNode target = head; + for (int i = 0; i != index; ++i) { + target = target.next; + } + return target; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + + LinkedListNode cursor = head; + + public boolean hasNext() { + return cursor != null; + } + + public Object next() { + Object toRet = cursor.data; + cursor = cursor.next; + return toRet; + } + } +} diff --git a/group09/601862675/Week01/src/main/java/List.java b/group09/601862675/Week01/src/main/java/List.java new file mode 100644 index 0000000000..2e5e4477f5 --- /dev/null +++ b/group09/601862675/Week01/src/main/java/List.java @@ -0,0 +1,7 @@ +public interface List { + void add(int index, Object object); + void add(Object object); + Object get(int i); + Object remove(int i); + int size(); +} diff --git a/group09/601862675/Week01/src/main/java/Queue.java b/group09/601862675/Week01/src/main/java/Queue.java new file mode 100644 index 0000000000..60f32933a3 --- /dev/null +++ b/group09/601862675/Week01/src/main/java/Queue.java @@ -0,0 +1,35 @@ +public class Queue { + + private LinkedList llist = new LinkedList(); + + public void enQueue(Object o){ + llist.add(o); + } + + public Object deQueue(){ + if (llist.size() == 0) { + throw new UnsupportedOperationException(); + } + return llist.remove(0); + } + + public boolean isEmpty(){ + return llist.size() == 0; + } + + public int size(){ + return llist.size(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("Queue: [ "); + Iterator iter = llist.iterator(); + while (iter.hasNext()) { + builder.append(iter.next()); + builder.append(" "); + } + builder.append("]"); + return builder.toString(); + } +} diff --git a/group09/601862675/Week01/src/main/java/Stack.java b/group09/601862675/Week01/src/main/java/Stack.java new file mode 100644 index 0000000000..0aa097357d --- /dev/null +++ b/group09/601862675/Week01/src/main/java/Stack.java @@ -0,0 +1,38 @@ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (elementData.size() == 0) { + throw new UnsupportedOperationException(); + } + 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(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder("Stack: [ "); + Iterator iter = elementData.iterator(); + while (iter.hasNext()) { + builder.append(iter.next()); + builder.append(" "); + } + builder.append("]"); + return builder.toString(); + } +} \ No newline at end of file diff --git a/group09/601862675/Week01/src/main/java/Watcher.java b/group09/601862675/Week01/src/main/java/Watcher.java new file mode 100644 index 0000000000..eddc2ee28a --- /dev/null +++ b/group09/601862675/Week01/src/main/java/Watcher.java @@ -0,0 +1,26 @@ +import java.nio.file.*; +import java.util.*; +import java.util.List; + +public class Watcher { + public static void main(String[] args) { + Path this_dir = Paths.get("."); + System.out.println("Now watching the current directory ..."); + + try { + WatchService watcher = this_dir.getFileSystem().newWatchService(); + this_dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); + + WatchKey watckKey = watcher.take(); + + List> events = watckKey.pollEvents(); + for (WatchEvent event : events) { + System.out.println("Someone just created the file '" + event.context().toString() + "'."); + + } + + } catch (Exception e) { + System.out.println("Error: " + e.toString()); + } + } +} \ No newline at end of file diff --git a/group09/601862675/Week01/src/test/java/ArrayListTest.java b/group09/601862675/Week01/src/test/java/ArrayListTest.java new file mode 100644 index 0000000000..923c76ce1f --- /dev/null +++ b/group09/601862675/Week01/src/test/java/ArrayListTest.java @@ -0,0 +1,104 @@ +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class ArrayListTest { + + @Rule + public final SystemOutRule log = new SystemOutRule().enableLog(); + + @Test + public void testAddWithIndex() { + log.clearLog(); + ArrayList list = initListWithSize(10); + list.add(3, 10); + System.out.print(list.toString()); + Assert.assertEquals("List: [ 0 1 2 10 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.add(list.size(), 11); + System.out.print(list.toString()); + Assert.assertEquals("List: [ 0 1 2 10 3 4 5 6 7 8 9 11 ]", log.getLog()); + System.out.println(); + } + + @Test + public void testAdd() { + log.clearLog(); + ArrayList list = new ArrayList(); + list.add(10); + System.out.print(list.toString()); + Assert.assertEquals("List: [ 10 ]", log.getLog()); + System.out.println(); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddWithIndexOutOfBoundsException() { + ArrayList list = initListWithSize(ArrayList.MAX_LIST_SIZE); + Assert.assertEquals(48, list.size()); + list.add(1); + } + + @Test + public void testRemove() { + ArrayList list = initListWithSize(10); + + log.clearLog(); + Object removed = list.remove(0); + System.out.print(list); + Assert.assertEquals(0, removed); + Assert.assertEquals(9, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + removed = list.remove(list.size()-1); + System.out.print(list); + Assert.assertEquals(9, removed); + Assert.assertEquals(8, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 7 8 ]", log.getLog()); + System.out.println(); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetWithIllegalArgumentException() { + ArrayList list = new ArrayList(); + list.add(1); + Assert.assertEquals(1, list.size()); + list.get(list.size()); + } + + @Test + public void testSize() { + ArrayList list = new ArrayList(); + list.add(10); + list.add(20); + list.add(30); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testIterator() { + log.clearLog(); + ArrayList list = new ArrayList(); + for (int i = 0; i < 10; ++i) { + list.add(i); + } + Iterator iter = list.iterator(); + while (iter.hasNext()) { + System.out.print(iter.next()); + } + Assert.assertEquals("0123456789", log.getLog()); + System.out.println(); + } + + private ArrayList initListWithSize(int size) { + ArrayList list = new ArrayList(); + for (int i = 0; i < size; ++i) { + list.add(i); + } + return list; + } +} diff --git a/group09/601862675/Week01/src/test/java/LinkedListTest.java b/group09/601862675/Week01/src/test/java/LinkedListTest.java new file mode 100644 index 0000000000..d36f67101e --- /dev/null +++ b/group09/601862675/Week01/src/test/java/LinkedListTest.java @@ -0,0 +1,106 @@ +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class LinkedListTest { + + @Rule + public final SystemOutRule log = new SystemOutRule().enableLog(); + + @Test + public void testAdd() { + log.clearLog(); + LinkedList list = initListWithSize(10); + System.out.print(list); + Assert.assertEquals("List: [ 0 1 2 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + } + + @Test + public void testAddWithIndex() { + log.clearLog(); + LinkedList list = initListWithSize(10); + list.add(0, -1); + System.out.print(list); + Assert.assertEquals(11, list.size()); + Assert.assertEquals("List: [ -1 0 1 2 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.add(list.size()-1, 10); + System.out.print(list); + Assert.assertEquals("List: [ -1 0 1 2 3 4 5 6 7 8 10 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.add(list.size(), 11); + System.out.print(list); + Assert.assertEquals("List: [ -1 0 1 2 3 4 5 6 7 8 10 9 11 ]", log.getLog()); + System.out.println(); + } + + @Test + public void testRemove() { + log.clearLog(); + LinkedList list = initListWithSize(10); + list.remove(0); + System.out.print(list); + Assert.assertEquals(9, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 7 8 9 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.remove(list.size()-1); + System.out.print(list); + Assert.assertEquals(8, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 7 8 ]", log.getLog()); + System.out.println(); + + log.clearLog(); + list.remove(list.size()-2); + System.out.print(list); + Assert.assertEquals(7, list.size()); + Assert.assertEquals("List: [ 1 2 3 4 5 6 8 ]", log.getLog()); + System.out.println(); + } + + @Test + public void testGet() { + log.clearLog(); + LinkedList list = initListWithSize(10); + for (int i = 0; i < list.size(); ++i) { + System.out.print(list.get(i)); + } + Assert.assertEquals("0123456789", log.getLog()); + System.out.println(); + } + + @Test + public void testSize() { + log.clearLog(); + LinkedList list = initListWithSize(10); + Assert.assertEquals(10, list.size()); + System.out.println(); + } + + @Test + public void testIterator() { + log.clearLog(); + LinkedList list = initListWithSize(10); + Iterator iter = list.iterator(); + while (iter.hasNext()) { + System.out.print(iter.next()); + } + Assert.assertEquals("0123456789", log.getLog()); + System.out.println(); + } + + private LinkedList initListWithSize(int size) { + LinkedList list = new LinkedList(); + for (int i = 0; i < size; ++i) { + list.add(i); + } + return list; + } +} diff --git a/group09/601862675/Week01/src/test/java/QueueTest.java b/group09/601862675/Week01/src/test/java/QueueTest.java new file mode 100644 index 0000000000..bd4e24a4e3 --- /dev/null +++ b/group09/601862675/Week01/src/test/java/QueueTest.java @@ -0,0 +1,44 @@ +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class QueueTest { + @Rule + public final SystemOutRule log = new SystemOutRule().enableLog(); + + @Test + public void testEnqueue() { + log.clearLog(); + Queue queue = new Queue(); + queue.enQueue(10); + System.out.print(queue); + Assert.assertEquals("Queue: [ 10 ]", log.getLog()); + } + + @Test + public void testDequeue() { + log.clearLog(); + Queue queue = new Queue(); + queue.enQueue(10); + queue.deQueue(); + System.out.print(queue); + Assert.assertEquals("Queue: [ ]", log.getLog()); + } + + @Test + public void testIsEmpty() { + Queue queue = new Queue(); + queue.enQueue(10); + Assert.assertEquals(false, queue.isEmpty()); + queue.deQueue(); + Assert.assertEquals(true, queue.isEmpty()); + } + + @Test + public void testSize() { + Queue queue = new Queue(); + queue.enQueue(10); + Assert.assertEquals(1, queue.size()); + } +} diff --git a/group09/601862675/Week01/src/test/java/StackTest.java b/group09/601862675/Week01/src/test/java/StackTest.java new file mode 100644 index 0000000000..0afac4c0da --- /dev/null +++ b/group09/601862675/Week01/src/test/java/StackTest.java @@ -0,0 +1,61 @@ +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class StackTest { + @Rule + public final SystemOutRule log = new SystemOutRule().enableLog(); + + @Test + public void testPush() { + log.clearLog(); + Stack stack = new Stack(); + stack.push("1"); + System.out.print(stack); + Assert.assertEquals(1, stack.size()); + Assert.assertEquals("Stack: [ 1 ]", log.getLog()); + } + + @Test + public void testPop() { + Stack stack = new Stack(); + stack.push(10); + Object o = stack.pop(); + Assert.assertEquals(10, o); + Assert.assertEquals(0, stack.size()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testPopWithException() { + Stack stack = new Stack(); + stack.push(10); + stack.pop(); + stack.pop(); + } + + @Test + public void testPeek() { + Stack stack = new Stack(); + stack.push(10); + Object o = stack.peek(); + Assert.assertEquals(10, o); + Assert.assertEquals(1, stack.size()); + } + + @Test + public void testIsEmpty() { + Stack stack = new Stack(); + stack.push(10); + Assert.assertEquals(false, stack.isEmpty()); + stack.pop(); + Assert.assertEquals(true, stack.isEmpty()); + } + + @Test + public void testSize() { + Stack stack = new Stack(); + stack.push(1); + Assert.assertEquals(1, stack.size()); + } +} diff --git a/group09/601862675/pom.xml b/group09/601862675/pom.xml new file mode 100644 index 0000000000..90585b6284 --- /dev/null +++ b/group09/601862675/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + me.sidzh + coding2017 + pom + 1.0-SNAPSHOT + + Week01 + + + + \ No newline at end of file diff --git a/group09/715061147/.gitignore b/group09/715061147/.gitignore new file mode 100644 index 0000000000..920bb9729c --- /dev/null +++ b/group09/715061147/.gitignore @@ -0,0 +1,4 @@ +.metadata +.swp +RemoteSystemsTempFiles/ +target diff --git a/group09/715061147/mvnhomework1/.gitignore b/group09/715061147/mvnhomework1/.gitignore new file mode 100644 index 0000000000..a65ec10715 --- /dev/null +++ b/group09/715061147/mvnhomework1/.gitignore @@ -0,0 +1,4 @@ +.classpath +.settings +.project +target diff --git a/group09/715061147/mvnhomework1/pom.xml b/group09/715061147/mvnhomework1/pom.xml new file mode 100644 index 0000000000..b6702aac65 --- /dev/null +++ b/group09/715061147/mvnhomework1/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + com.qsq.study + mvnhomework1 + 0.0.1-SNAPSHOT + MvnHomeWork1 + MvnHomeWork1 + + + + junit + junit + 4.12 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/ArrayList.java b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/ArrayList.java new file mode 100644 index 0000000000..f2bdc27de9 --- /dev/null +++ b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/ArrayList.java @@ -0,0 +1,143 @@ +package com.qsq.study; + +import java.util.Arrays; + +public class ArrayList implements List { + + private static final int DEFAULT_CAPACITY = 16; + private static final Object[] EMPTY_ELEMENT_DATA = {}; + private Object[] elementData; + private int size = 0; + + /* + * һĬArrayList + */ + public ArrayList() { + this(DEFAULT_CAPACITY); + } + + /* + * һָʼArrayList + * + * @param initialCapacity ʼ + * + * @throws IllegalArgumentException ָʼΪʱ׳Ƿ쳣 + */ + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + this.elementData = new Object[initialCapacity]; + } else if (initialCapacity == 0) { + this.elementData = EMPTY_ELEMENT_DATA; + } else { + throw new IllegalArgumentException("Illegal Capacity " + initialCapacity); + } + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + /* + * elementData + * + * @param capacity µ + */ + private void grow(int capacity) { + if (capacity < elementData.length) { + return; + } + elementData = Arrays.copyOf(elementData, capacity); + } + + @Override + public boolean add(E e) { + if (size == elementData.length) { + grow(size * 2); + } + elementData[size++] = e; + return true; + } + + @Override + public E remove(int index) { + rangeCheck(index); + + // move elements after index forward by 1 + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i + 1]; + } + --size; + + // TODO: JDKʵ: 1.Ч 2.GC + // System.arraycopy(elementData, index + 1, elementData, index, size - + // index - 1); + // elementData[--size] = null; + + return null; + } + + @Override + public boolean remove(Object o) { + int index = indexOf(o); + + if (index < 0) { + return false; + } + + remove(index); + return true; + } + + @SuppressWarnings({ "unchecked" }) + private E elementData(int index) { + return (E) elementData[index]; + } + + private void rangeCheck(int index) { + // TODO: JDKԴδindex<0ΪΣ + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + } + + @Override + public E get(int index) { + rangeCheck(index); + + return elementData(index); + } + + @Override + public E set(int index, E element) { + rangeCheck(index); + + E oldElement = elementData(index); + elementData[index] = element; + return oldElement; + } + + @Override + public int indexOf(Object o) { + if (o == null) { + for (int i = 0; i < size; i++) { + if (elementData[i] == null) { + return i; + } + } + } else { + for (int i = 0; i < size; i++) { + if (o.equals(elementData[i])) { + return i; + } + } + } + return -1; + } + +} diff --git a/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/LinkedList.java b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/LinkedList.java new file mode 100644 index 0000000000..e1b3f0a686 --- /dev/null +++ b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/LinkedList.java @@ -0,0 +1,169 @@ +package com.qsq.study; + +public class LinkedList implements List{ + + private Node first; + private Node last; + private int size; + + private static class Node { + T item; + Node prev; + Node next; + + public Node(Node prev, T item, Node next) { + this.prev = prev; + this.item = item; + this.next = next; + } + } + + /* + * ޲캯 + */ + public LinkedList() { + first = null; + last = first; + size = 0; + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + private void rangeCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); + } + } + + @Override + public boolean add(E e) { + if (first == null) { + // Ϊ + first = new Node(null, e, null); + } else if (last == null) { + // βΪ + last = new Node(first, e, null); + first.next = last; + } else { + Node node = new Node(last, e, null); + last.next = node; + last = node; + } + ++size; + return true; + } + + @Override + public boolean remove(Object o) { + int index = indexOf(o); + if (index < 0 || index > size) { + return false; + } + + remove(index); + return true; + } + + @Override + public E remove(int index) { + rangeCheck(index); + + Node current = first; + while (index > 0) { + --index; + if (current == null) { + return null; + } + current = current.next; + } + + E oldItem = current.item; + + if (current.prev == null) { + // ɾͷ + first = current.next; + if (current.next != null) { + current.next.prev = null; + } + } else { + current.prev.next = current.next; + if (current.next != null) { + current.next.prev = current.prev; + } + } + --size; + + return oldItem; + } + + @Override + public E get(int index) { + rangeCheck(index); + + int i = 0; + Node current = first; + while (i < index) { + if (current == null) { + return null; + } + ++i; + current = current.next; + } + return current.item; + } + + @Override + public E set(int index, E element) { + rangeCheck(index); + + Node current = first; + while (index > 0) { + --index; + current = current.next; + } + E oldElement = current.item; + current.item = element; + + return oldElement; + } + + @Override + public int indexOf(Object o) { + if (first == null) { + return -1; + } + + Node current = first; + int index = 0; + if (o == null) { + while (current != null) { + if (current.item == null) { + return index; + } else { + current = current.next; + ++index; + } + } + } else { + while (current != null) { + if (o.equals(current.item)) { + return index; + } else { + current = current.next; + ++index; + } + } + } + + return -1; + } + +} diff --git a/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/List.java b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/List.java new file mode 100644 index 0000000000..7b509a361e --- /dev/null +++ b/group09/715061147/mvnhomework1/src/main/java/com/qsq/study/List.java @@ -0,0 +1,12 @@ +package com.qsq.study; + +public interface List { + int size(); + boolean isEmpty(); + boolean add(E e); + boolean remove(Object o); + E remove(int index); + E get(int index); + E set(int index, E element); + int indexOf(Object o); +} diff --git a/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/ArrayListTest.java b/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/ArrayListTest.java new file mode 100644 index 0000000000..d1c6353f79 --- /dev/null +++ b/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/ArrayListTest.java @@ -0,0 +1,70 @@ +package com.qsq.study; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void testIsEmpty() { + ArrayList list = new ArrayList<>(); + assertEquals(true, list.isEmpty()); + list.add(1); + assertEquals(false, list.isEmpty()); + } + + @Test + public void testSize() { + ArrayList list = new ArrayList<>(); + assertEquals(0, list.size()); + list.add(1); + assertEquals(1, list.size()); + list.add(2); + assertEquals(2, list.size()); + for (int i=3; i<=20; i++) { + list.add(i); + } + assertEquals(20, list.size()); + + } + @Test + public void testAdd() { + ArrayList list = new ArrayList<>(); + list.add(1); + assertEquals(1, list.size()); + } + + @Test + public void testRemove() { + ArrayList list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + assertEquals(3, list.size()); + list.remove(1); + assertEquals(2, list.size()); + list.remove(1); + assertEquals(1, list.size()); + list.remove(0); + assertEquals(0, list.size()); + } + + @Test + public void testGet() { + ArrayList list = new ArrayList<>(); + list.add(1); + list.add(2); + assertEquals(1, (int)list.get(0)); + assertEquals(2, (int)list.get(1)); + } + + @Test + public void TestSet() { + ArrayList list = new ArrayList<>(); + list.add(1); + assertEquals(1, (int)list.get(0)); + list.set(0, 2); + assertEquals(2, (int)list.get(0)); + } +} diff --git a/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/LinkedListTest.java b/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/LinkedListTest.java new file mode 100644 index 0000000000..7b0bd3807a --- /dev/null +++ b/group09/715061147/mvnhomework1/src/test/java/com/qsq/study/LinkedListTest.java @@ -0,0 +1,70 @@ +package com.qsq.study; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class LinkedListTest { + + @Test + public void testIsEmpty() { + LinkedList list = new LinkedList<>(); + assertEquals(true, list.isEmpty()); + list.add(1); + assertEquals(false, list.isEmpty()); + } + + @Test + public void testSize() { + LinkedList list = new LinkedList<>(); + assertEquals(0, list.size()); + list.add(1); + assertEquals(1, list.size()); + list.add(2); + assertEquals(2, list.size()); + for (int i=3; i<=20; i++) { + list.add(i); + } + assertEquals(20, list.size()); + + } + @Test + public void testAdd() { + LinkedList list = new LinkedList<>(); + list.add(1); + assertEquals(1, list.size()); + } + + @Test + public void testRemove() { + LinkedList list = new LinkedList<>(); + list.add(1); + list.add(2); + list.add(3); + assertEquals(3, list.size()); + list.remove(1); + assertEquals(2, list.size()); + list.remove(1); + assertEquals(1, list.size()); + list.remove(0); + assertEquals(0, list.size()); + } + + @Test + public void testGet() { + LinkedList list = new LinkedList<>(); + list.add(1); + list.add(2); + assertEquals(1, (int)list.get(0)); + assertEquals(2, (int)list.get(1)); + } + + @Test + public void TestSet() { + LinkedList list = new LinkedList<>(); + list.add(1); + assertEquals(1, (int)list.get(0)); + list.set(0, 2); + assertEquals(2, (int)list.get(0)); + } +} diff --git a/group09/790466157/src/com/coding/basic/Iterator.java b/group09/790466157/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..7c02cc6e51 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/List.java b/group09/790466157/src/com/coding/basic/List.java new file mode 100644 index 0000000000..c86b745572 --- /dev/null +++ b/group09/790466157/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(); +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/MyArrayList.java b/group09/790466157/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..cf232c5b72 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,133 @@ +package com.coding.basic; +import java.util.Arrays; +import java.util.NoSuchElementException; + +import com.coding.basic.List; +import com.coding.basic.Iterator; + +public class MyArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE; + + private static final int DEFAULT_CAPACITY = 10; + + + //޳캯 + public MyArrayList(){ + this(DEFAULT_CAPACITY); + } + + public MyArrayList(int size){ + if (size < 0){ + throw new IllegalArgumentException("ĬϵĴС" + size); + } + else{ + elementData = new Object[size]; + } + } + + public void add(Object o){ + isCapacityEnough(size+1); + elementData[size++] = o; + } + + private void isCapacityEnough(int size){ + //жǷ񳬹ʼǷҪ + if (size > DEFAULT_CAPACITY){ + explicitCapacity(size); + } + if (size < 0){ + throw new OutOfMemoryError(); + } + } + + private void explicitCapacity(int capacity){ + int oldCapacity = elementData.length; + //= + /2 1.5Ʋ൱ڳ2 + int newLength = oldCapacity + (oldCapacity >> 1); + if (newLength - capacity < 0){ + newLength = capacity; + } + //жnewLengthij + //涨󳤶жҪҪݿռǷ󳤶 + //newLengthΪ MAX_VALUE Ϊ MAX_ARRAY_LENGTH + if (newLength > (MAX_ARRAY_LENGTH)){ + newLength = (capacity > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH); + } + //copyof + elementData = Arrays.copyOf(elementData, newLength); + } + + public void add(int index, Object o){ + + checkRangeForAdd(index); + isCapacityEnough(size +1); + // elementDataдIndexλÿʼΪsize-indexԪأ + // ±Ϊindex+1λÿʼµelementDataС + // ǰλڸλõԪԼкԪһλá + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + //жǷԽ + private void checkRangeForAdd(int index){ + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("ָindexԽ"); + } + } + + // شбָλϵԪء + public Object get(int index){ + checkRange(index); + return elementData[index]; + } + + //жǷԽ + private void checkRange(int index){ + if (index >= size || index < 0){ + throw new IndexOutOfBoundsException("ָindexԽ"); + } + } + + public Object remove(int index){ + Object value = get(index); + int moveSize = size - index -1; + if (moveSize >0){ + System.arraycopy(elementData, index +1, elementData, index, size - index -1); + + } + elementData[--size] = null; + return value; + } + + public int size(){ + return size; + } + + // + public Iterator iterator(Object o){ + return new ArrayListIterator(); + } + + + private class ArrayListIterator implements Iterator{ + private int currentIndex=0; + + public boolean hasNext() { + return currentIndex < size(); + } + + public Object next() { + if (!hasNext()){ + throw new NoSuchElementException(); + } + return new Object[currentIndex + 1]; + } + } + +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/MyLinkedList.java b/group09/790466157/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..4727db3a89 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,159 @@ +package com.coding.basic; + +public class MyLinkedList { + + private int size; + + private Node first; + + private Node last; + + + public boolean add(E element) { + addAtLast(element); + return true; + } + + private void addAtLast(E element) { + Node l = last; + Node node = new Node(element, null, l); + last = node; + if (l == null) { + first = node; + } else { + l.next = node; + } + size++; + } + + public void add(int index, E element) { + checkRangeForAdd(index); + if (index == size) { + addAtLast(element); + } else { + Node l = node(index); + addBeforeNode(element, l); + } + } + + private void addBeforeNode(E element, Node specifiedNode) { + Node preNode = specifiedNode.prev; + Node newNode = new Node(element, specifiedNode, preNode); + if (preNode == null) { + first = newNode; + } else { + preNode.next = newNode; + } + specifiedNode.prev = newNode; + size++; + } + + + private Node node(int index) { + if (index < (size << 1)) { + Node cursor = first; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor; + } else { + Node cursor = last; + for (int i = size - 1; i > index; i--) { + cursor = cursor.prev; + } + return cursor; + } + } + + private void checkRangeForAdd(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("ָindex"); + } + } + + public E get(int index) { + checkRange(index); + return node(index).item; + } + + private void checkRange(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException("ָindex"); + } + } + + public int indexOf(Object element) { + Node cursor = first; + int count = 0; + while (cursor != null) { + if (element != null) { + if (element.equals(cursor.item)) { + return count; + } + }else{ + if (cursor.item == null) { + return count; + } + } + count ++; + cursor = cursor.next; + } + return -1; + } + + public E remove(int index) { + checkRange(index); + return deleteLink(index); + } + + public boolean remove(Object o) { + int index = indexOf(o); + if (index < 0){ + return false; + } + deleteLink(index); + return true; + } + + private E deleteLink(int index) { + Node l = node(index); + E item = l.item; + Node prevNode = l.prev; + Node nextNode = l.next; + + if (prevNode == null) { + first = nextNode; + }else{ + prevNode.next = nextNode; + l.next = null; + } + + if (nextNode == null) { + last = prevNode; + }else{ + nextNode.prev = prevNode; + l.prev = null; + } + size--; + l.item = null; + return item; + } + + + + public int size(){ + return size; + } + private static class Node { + E item; + Node next; + Node prev; + + public Node(E item, Node next, Node prev) { + this.item = item; + this.next = next; + this.prev = prev; + + } + } +} diff --git a/group09/790466157/src/com/coding/basic/Queue.java b/group09/790466157/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..80d0dc9835 --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Queue.java @@ -0,0 +1,68 @@ +package com.coding.basic; +import java.util.Arrays; + +public class Queue { + private static final int CAPACITY = 10; + private static int capacity; + private static int front; + private static int tail; + private static Object[] array; + + public Queue(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + front = tail = 0; + } + + public void enQueue(Object o) throws ExceptionQueueFull { + if (size() == capacity -1) + throw new ExceptionQueueFull("Queue is full"); + array[tail] = o; + tail = (tail +1) % capacity; + } + + public Object deQueue() throws ExceptionQueueEmpty{ + Object o; + if (isEmpty()) + throw new ExceptionQueueEmpty("Queue is empty"); + o = array[front]; + front = (front + 1) % capacity; + return o; + } + + public boolean isEmpty(){ + return (front == tail); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return (capacity + tail - front) % capacity; + } + + public class ExceptionQueueEmpty extends Exception { + // Constructor + public ExceptionQueueEmpty() { + + } + + // Constructor with parameters + public ExceptionQueueEmpty(String mag) { + System.out.println(mag); + } + } + + public class ExceptionQueueFull extends Exception { + + // Constructor + public ExceptionQueueFull() { + + } + + // Constructor with parameters + public ExceptionQueueFull(String mag) { + System.out.println(mag); + } + } +} \ No newline at end of file diff --git a/group09/790466157/src/com/coding/basic/Stack.java b/group09/790466157/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..324cc5639e --- /dev/null +++ b/group09/790466157/src/com/coding/basic/Stack.java @@ -0,0 +1,73 @@ +package com.coding.basic; +import java.util.Arrays; +import com.coding.basic.MyArrayList; +public class Stack { + private MyArrayList elementData = new MyArrayList(); + private static final int CAPACITY = 10; + private static int capacity; + private static int top = -1; + Object[] array; + + public Stack(){ + this.capacity = CAPACITY; + array = new Object[capacity]; + } + public void push(Object o) throws ExceptionStackFull{ + if(size()== CAPACITY){ + throw new ExceptionStackFull("Stack is full"); + } + array[++ top] = o; + } + + public Object pop() throws ExceptionStackEmpty{ + if(isEmpty()){ + throw new ExceptionStackEmpty("Stack is empty"); + } + return array[top --]; + } + + public Object peek() throws ExceptionStackEmpty{ + if(isEmpty()){ + throw new ExceptionStackEmpty("Stack is empty"); + } + return array[top]; + } + + public boolean isEmpty(){ + return (top < 0); + } + + public int size(){ + if (isEmpty()) + return 0; + else + return top + 1; + + } + + public class ExceptionStackEmpty extends Exception { + + //Constructor + public ExceptionStackEmpty(){ + + } + + //Define myself exception construct with parameters + public ExceptionStackEmpty(String string){ + super(string); + } + } + + public class ExceptionStackFull extends Exception { + + //Constructor + public ExceptionStackFull(){ + + } + + //Define myself exception construct with parameters + public ExceptionStackFull(String string){ + super(string); + } + } +} \ No newline at end of file diff --git a/group09/790466157/test.txt b/group09/790466157/test.txt new file mode 100644 index 0000000000..3b18e512db --- /dev/null +++ b/group09/790466157/test.txt @@ -0,0 +1 @@ +hello world diff --git a/group09/85839593/assignment-0215-0226/pom.xml b/group09/85839593/assignment-0215-0226/pom.xml new file mode 100644 index 0000000000..9d40995970 --- /dev/null +++ b/group09/85839593/assignment-0215-0226/pom.xml @@ -0,0 +1,15 @@ + + + + assignment + assignment + 1.0-SNAPSHOT + + 4.0.0 + + assignment-0215-0226 + + + \ No newline at end of file diff --git a/group09/85839593/assignment-0215-0226/src/main/java/MyArrayList.java b/group09/85839593/assignment-0215-0226/src/main/java/MyArrayList.java new file mode 100644 index 0000000000..0fbd7771b4 --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/main/java/MyArrayList.java @@ -0,0 +1,84 @@ +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * User: guohairui + * Date: 17-2-22 + * Time: 上午12:06 + * To change this template use File | Settings | File Templates. + */ +public class MyArrayList { + public int size = 0; + private Object [] elementData = new Object[5]; + public void add(int index,Object obj){ + if(index>size() ||index<0) + throw new IndexOutOfBoundsException("哎呀我去,不够了."); + elementData[index]=obj; + size++; + } + public void insert(int index,Object obj){ + if(size>elementData.length-1){ + System.out.println("当前size:" + size + " 当前length:" + elementData.length+",再插不够了,需要扩容"); + Object [] tmpData = elementData; + elementData =new Object[size+5] ; + System.out.println("当前size:" + size + " 当前length扩了5后为:" + elementData.length); + System.arraycopy(tmpData,0,elementData,0,index); + elementData[index]=obj; + System.arraycopy(tmpData,index,elementData,index+1,tmpData.length-index); + }else { + if(elementData[index]==null){ + elementData[index]=obj; + }else { + System.out.println("当前size:" + size + " 当前length:" + elementData.length); + System.arraycopy(elementData,index,elementData,index+1,size-index); + elementData[index]=obj; + } + } + size++; + } + public void add(Object obj){ + if(size>=elementData.length){ + System.out.println("当前size:" + size + " 当前length:" + elementData.length); + Object [] tmpData = elementData; + elementData =new Object[size+5] ; + System.out.println("当前size:" + size + " 当前length扩了5后为:" + elementData.length); + System.arraycopy(tmpData,0,elementData,0,size); + elementData[size]=obj; + }else { + System.out.println("当前size:" + size + " 当前length:" + elementData.length); + elementData[size]=obj; + } + size++; + } + public Object get(int index) { + if(index>=size) + throw new IndexOutOfBoundsException("越了"); + return elementData[index]; + } + public Object remove(int index){ + Object delValue = elementData[index]; + int movesize = size-index-1; + System.out.print("size:"+size+" index:"+index+" ,size-index-1:"+movesize); + System.arraycopy(elementData,index+1,elementData,index,movesize); + System.out.print("删除后前移位,数组末位清空"); + elementData[--size]=null; + + return delValue; + } + public int size(){ + return size; + } + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append('['); + for (int i=0;i=size) + throw new RuntimeException("超出范围了"); + Node node = head; + if(index<(size>>1)){//当偏向于前一半时从头找,否则从尾找 + for ( int i=0;i<=index;i++) { + node = node.next; + } + }else { + for (int i=size;i>index;i--){ + node=node.previous; + } + } + return node; + } + + private static class Node{ + Object data;//当前Entry + Node next;//下一个 + Node previous;//前一个 + public Node(Object data,Node next,Node previous){ + this.data=data; + this.next=next; + this.previous=previous; + } + } + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append('['); + Node n =getNode(0) ; + for (int i=0;i>2); + } +} diff --git a/group09/85839593/assignment-0215-0226/src/test/java/MyLinkedListTest.java b/group09/85839593/assignment-0215-0226/src/test/java/MyLinkedListTest.java new file mode 100644 index 0000000000..f0db06e6f8 --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/test/java/MyLinkedListTest.java @@ -0,0 +1,70 @@ +import org.junit.*; +import org.junit.Test; + +/** + * Created with IntelliJ IDEA. + * User: guohairui + * Date: 17-2-26 + * Time: 下午6:12 + * To change this template use File | Settings | File Templates. + */ +public class MyLinkedListTest { + @org.junit.Test + public void testAdd() throws Exception { + MyLinkedList linkedList = new MyLinkedList(); + linkedList.add("abc1"); + linkedList.add("abc2"); + linkedList.add("abc3"); + linkedList.add("abc4"); + System.out.println(linkedList.get(1)); + System.out.println(linkedList); + linkedList.add("abc5"); + System.out.println(linkedList.get(3)); + System.out.println(linkedList.get(4)); + System.out.println(linkedList); + linkedList.add(2,"abcaddtmp"); + System.out.println(linkedList.get(3)); + System.out.println(linkedList.get(4)); + System.out.println(linkedList); + linkedList.remove(2); + System.out.println(linkedList.toString()); + linkedList.removeLast(); + System.out.println(linkedList.toString()); + linkedList.removeFirst(); + System.out.println(linkedList.toString()); + } + @Test + public void testGet() throws Exception { + + } + + @Test + public void testRemove() throws Exception { + + } + + @Test + public void testAddFirst() throws Exception { + + } + + @Test + public void testRemoveFirst() throws Exception { + + } + + @Test + public void testRemoveLast() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } + + @Test + public void testGetNode() throws Exception { + + } +} diff --git a/group09/85839593/assignment-0215-0226/src/test/java/MyQueueTest.java b/group09/85839593/assignment-0215-0226/src/test/java/MyQueueTest.java new file mode 100644 index 0000000000..d93a70ba81 --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/test/java/MyQueueTest.java @@ -0,0 +1,29 @@ +import org.junit.*; +import org.junit.Test; + +/** + * Created with IntelliJ IDEA. + * User: guohairui + * Date: 17-2-26 + * Time: 下午7:33 + * To change this template use File | Settings | File Templates. + */ +public class MyQueueTest { + @org.junit.Test + public void testEnQueue() throws Exception { + MyQueue myQueue=new MyQueue(); + myQueue.enQueue("abc1"); + myQueue.enQueue("abc2"); + myQueue.enQueue("abc3"); + System.out.println(myQueue); + myQueue.enQueue("abc4"); + System.out.println(myQueue); + myQueue.deQueue(); + System.out.println(myQueue); + } + + @Test + public void testDeQueue() throws Exception { + + } +} diff --git a/group09/85839593/assignment-0215-0226/src/test/java/MyStackTest.java b/group09/85839593/assignment-0215-0226/src/test/java/MyStackTest.java new file mode 100644 index 0000000000..b96d77dbbf --- /dev/null +++ b/group09/85839593/assignment-0215-0226/src/test/java/MyStackTest.java @@ -0,0 +1,36 @@ +import org.junit.*; +import org.junit.Test; + +/** + * Created with IntelliJ IDEA. + * User: guohairui + * Date: 17-2-26 + * Time: 下午7:20 + * To change this template use File | Settings | File Templates. + */ +public class MyStackTest { + @org.junit.Test + public void testPush() throws Exception { + MyStack myStack = new MyStack(); + myStack.push("abc1"); + myStack.push("abc2"); + myStack.push("abc3"); + System.out.println(myStack); + myStack.push("abc4"); + System.out.println(myStack); + System.out.println("myStack.peek:"+myStack.peek()); + myStack.pop(); + System.out.println("myStack.size"+myStack.size()); + System.out.println(myStack); + } + + @Test + public void testPop() throws Exception { + + } + + @Test + public void testPeek() throws Exception { + + } +} diff --git a/group09/85839593/pom.xml b/group09/85839593/pom.xml new file mode 100644 index 0000000000..627fff7a1f --- /dev/null +++ b/group09/85839593/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + assignment + assignment + pom + 1.0-SNAPSHOT + + assignment-0215-0226 + + + + \ No newline at end of file diff --git a/group09/group09.md b/group09/group09.md index d3f5a12faa..8b13789179 100644 --- a/group09/group09.md +++ b/group09/group09.md @@ -1 +1 @@ - + diff --git a/group10/1363044717/struts/homework/homework/pom.xml b/group10/1363044717/struts/homework/homework/pom.xml new file mode 100644 index 0000000000..54675f52ac --- /dev/null +++ b/group10/1363044717/struts/homework/homework/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com + code + 1.0 + + + junit + junit + 4.12 + + + dom4j + dom4j + 1.1 + + + + \ No newline at end of file diff --git a/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/array/ArrayUtil.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..aee11e5122 --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/array/ArrayUtil.java @@ -0,0 +1,318 @@ +package com.code.coderising.array; + +import org.junit.Test; +import java.util.Arrays; + +/** + * Created by Mori on 2017/3/2. + */ +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 n = origin.length; + for (int i = 0, j = n >> 1; i < j; i++) { + int temp = origin[i]; + origin[i] = origin[n - i - 1]; + origin[n - i - 1] = temp; + } + } + + @Test + public void testReverseArray() { + int[] a = {7, 9, 30, 3}; + System.out.println("原数组:" + Arrays.toString(a)); + reverseArray(a); + System.out.println("reverseArray后:" + Arrays.toString(a)); + } + + /** + * 现在有如下的一个数组: 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) { + Arrays.sort(oldArray); + int[] newArr = null; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArr = new int[oldArray.length - i]; + System.arraycopy(oldArray, i, newArr, 0, oldArray.length - i); + break; + } + } + return newArr; + } + + @Test + public void testRemoveZero() { + int[] a = {7, 9, 0, 30, 0, 0, 3}; + System.out.println("原数组:" + Arrays.toString(a)); + int[] newArr = removeZero(a); + System.out.println("removeZero后:" + Arrays.toString(newArr)); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int m = array1.length; + int n = array2.length; + int[] newArr = new int[m + n]; + int i = 0, j = 0, k = 0; + for (; i < m && j < n; ) { + if (array1[i] < array2[j]) { + newArr[k++] = array1[i++]; + } else { + newArr[k++] = array2[j++]; + } + + } + while (i < m) { + newArr[k++] = array1[i++]; + } + while (j < n) { + newArr[k++] = array2[j++]; + } + if (newArr.length < 1) + return null; + int slow = 1; + int count = 0; + for (int fast = 1; fast < newArr.length; fast++) { + if (newArr[fast] != newArr[slow - 1]) { + newArr[slow++] = newArr[fast]; + } else { + count++; + } + } + int[] arr = new int[newArr.length - count]; + System.arraycopy(newArr, 0, arr, 0, newArr.length - count); + return arr; + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8, 10, 10, 10, 10, 10}; + int[] a2 = {4, 5, 6, 7, 10, 12}; + int[] newArray = merge(a1, a2); + System.out.println("merge:" + Arrays.toString(newArray)); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + @Test + public void testGrow() { + int[] arr = {2, 3, 6}; + int[] newArray = grow(arr, 3); + System.out.println("grow:" + Arrays.toString(newArray)); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max == 1) { + return new int[0]; + } + int a = 1; + int b = 1; + int temp; + StringBuilder str = new StringBuilder("1,1,"); + while (a + b < max) { + temp = b; + b = a + b; + a = temp; + str.append(b + ","); + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testFibonacci() { + int[] newArray = fibonacci(500); + System.out.println("Fibonacci:" + Arrays.toString(newArray)); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + StringBuilder str = new StringBuilder(); + for (int i = 2; i < max; i++) { + if (i < 4) { + str.append(i + ","); + continue; + } else if (i % 2 == 0) { + continue; + } else if (i < 9) { + str.append(i + ","); + continue; + } else if (i % 3 == 0) { + continue; + } else { + int f = 5; + boolean flag = true; + while (f <= i/f) { + if (i % f == 0) { + flag = false; + break; + } else if (i % (f + 2) == 0) { + flag = false; + break; + } + f += 6; + } + if (flag) { + str.append(i + ","); + continue; + } + } + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testGetPrimes() { + int[] newArray = getPrimes(5000); + System.out.println("getPrimes:" + Arrays.toString(newArray)); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if(max<7){ + return new int[0]; + } + boolean flag = true; + StringBuilder str = new StringBuilder("6,"); + for (int i = 6; i < max; ) { + int sum = 1; + if (i % 3 == 1 && i % 9 == 1) { + //如果以8结尾,那么就肯定是以28结尾 + if(!flag){ + if(i%100!=28){ + i += 8; + flag = true; + continue; + } + } + for (int j = 2; j <= i / j; j++) { + if (i % j == 0) { + sum += j; + sum += i / j; + } + } + if (sum == i) { + str.append(i+","); + } + } + if (flag) { + i += 2; + flag = false; + } else { + i += 8; + flag = true; + } + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testGetPerfectNumbers() { + int[] newArray = getPerfectNumbers(33550337); + System.out.println("getPerfectNumbers:" + Arrays.toString(newArray)); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + if (array == null) + return "null"; + int n = array.length - 1; + if (n == -1) { + return ""; + } + StringBuilder b = new StringBuilder(); + for (int i = 0; ; i++) { + b.append(array[i]); + if (i == n) { + return b.toString(); + } + b.append(seperator); + } + } + + @Test + public void testJoin() { + int[] a1 = {3, 5, 7}; + System.out.println(join(a1, "-")); + } + +} diff --git a/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..cd1753941c --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.code.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/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/Struts.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..262b4bca59 --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/Struts.java @@ -0,0 +1,79 @@ +package com.code.coderising.litestruts; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + SAXReader reader = new SAXReader(); + View view=new View(); + try { + Document e = reader.read(ClassLoader.getSystemResource("struts.xml")); + //得到根节点 + Element struts = e.getRootElement(); + Iterator it = struts.elementIterator(); + while(it.hasNext()) {//action遍历 + Element action = (Element)it.next(); + // 找到 name=actionName 的action + List actionAtr = action.attributes(); + if(!actionAtr.get(0).getValue().equals(actionName)) + continue; + String className= actionAtr.get(1).getValue(); + //得到actionName 对应的class + Class classz=Class.forName(className); + Object o=classz.newInstance(); + Field field; + for(Map.Entry entry:parameters.entrySet()){ + field = classz.getDeclaredField(entry.getKey()); + field.setAccessible(true); + field.set(o, entry.getValue()); + } + Method m = classz.getMethod("execute"); + String resultString=(String)m.invoke(o); + Method m2 = classz.getMethod("getMessage"); + String message=(String)m2.invoke(o); + + Map map=new HashMap<>(); + + map.put("message",message); + view.setParameters(map); + it=action.elementIterator(); + //遍历 + while (it.hasNext()){ + Element result = (Element)it.next(); + String s= result.attribute(0).getValue(); + if(resultString.equals(s)){ + view.setJsp(result.getStringValue()); + return view; + } + } + } + } catch (DocumentException var9) { + var9.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..fe414cf1a7 --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.code.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/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/View.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/View.java new file mode 100644 index 0000000000..f55dae45fa --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.code.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/group10/1363044717/struts/homework/homework/src/main/resources/struts.xml b/group10/1363044717/struts/homework/homework/src/main/resources/struts.xml new file mode 100644 index 0000000000..b9489dad6d --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group10/1363044717/struts/homework/pom.xml b/group10/1363044717/struts/homework/pom.xml new file mode 100644 index 0000000000..54675f52ac --- /dev/null +++ b/group10/1363044717/struts/homework/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com + code + 1.0 + + + junit + junit + 4.12 + + + dom4j + dom4j + 1.1 + + + + \ No newline at end of file diff --git a/group10/1363044717/struts/homework/src/main/java/com/code/coderising/array/ArrayUtil.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..aee11e5122 --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/array/ArrayUtil.java @@ -0,0 +1,318 @@ +package com.code.coderising.array; + +import org.junit.Test; +import java.util.Arrays; + +/** + * Created by Mori on 2017/3/2. + */ +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 n = origin.length; + for (int i = 0, j = n >> 1; i < j; i++) { + int temp = origin[i]; + origin[i] = origin[n - i - 1]; + origin[n - i - 1] = temp; + } + } + + @Test + public void testReverseArray() { + int[] a = {7, 9, 30, 3}; + System.out.println("原数组:" + Arrays.toString(a)); + reverseArray(a); + System.out.println("reverseArray后:" + Arrays.toString(a)); + } + + /** + * 现在有如下的一个数组: 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) { + Arrays.sort(oldArray); + int[] newArr = null; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArr = new int[oldArray.length - i]; + System.arraycopy(oldArray, i, newArr, 0, oldArray.length - i); + break; + } + } + return newArr; + } + + @Test + public void testRemoveZero() { + int[] a = {7, 9, 0, 30, 0, 0, 3}; + System.out.println("原数组:" + Arrays.toString(a)); + int[] newArr = removeZero(a); + System.out.println("removeZero后:" + Arrays.toString(newArr)); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int m = array1.length; + int n = array2.length; + int[] newArr = new int[m + n]; + int i = 0, j = 0, k = 0; + for (; i < m && j < n; ) { + if (array1[i] < array2[j]) { + newArr[k++] = array1[i++]; + } else { + newArr[k++] = array2[j++]; + } + + } + while (i < m) { + newArr[k++] = array1[i++]; + } + while (j < n) { + newArr[k++] = array2[j++]; + } + if (newArr.length < 1) + return null; + int slow = 1; + int count = 0; + for (int fast = 1; fast < newArr.length; fast++) { + if (newArr[fast] != newArr[slow - 1]) { + newArr[slow++] = newArr[fast]; + } else { + count++; + } + } + int[] arr = new int[newArr.length - count]; + System.arraycopy(newArr, 0, arr, 0, newArr.length - count); + return arr; + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8, 10, 10, 10, 10, 10}; + int[] a2 = {4, 5, 6, 7, 10, 12}; + int[] newArray = merge(a1, a2); + System.out.println("merge:" + Arrays.toString(newArray)); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + @Test + public void testGrow() { + int[] arr = {2, 3, 6}; + int[] newArray = grow(arr, 3); + System.out.println("grow:" + Arrays.toString(newArray)); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max == 1) { + return new int[0]; + } + int a = 1; + int b = 1; + int temp; + StringBuilder str = new StringBuilder("1,1,"); + while (a + b < max) { + temp = b; + b = a + b; + a = temp; + str.append(b + ","); + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testFibonacci() { + int[] newArray = fibonacci(500); + System.out.println("Fibonacci:" + Arrays.toString(newArray)); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + StringBuilder str = new StringBuilder(); + for (int i = 2; i < max; i++) { + if (i < 4) { + str.append(i + ","); + continue; + } else if (i % 2 == 0) { + continue; + } else if (i < 9) { + str.append(i + ","); + continue; + } else if (i % 3 == 0) { + continue; + } else { + int f = 5; + boolean flag = true; + while (f <= i/f) { + if (i % f == 0) { + flag = false; + break; + } else if (i % (f + 2) == 0) { + flag = false; + break; + } + f += 6; + } + if (flag) { + str.append(i + ","); + continue; + } + } + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testGetPrimes() { + int[] newArray = getPrimes(5000); + System.out.println("getPrimes:" + Arrays.toString(newArray)); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if(max<7){ + return new int[0]; + } + boolean flag = true; + StringBuilder str = new StringBuilder("6,"); + for (int i = 6; i < max; ) { + int sum = 1; + if (i % 3 == 1 && i % 9 == 1) { + //如果以8结尾,那么就肯定是以28结尾 + if(!flag){ + if(i%100!=28){ + i += 8; + flag = true; + continue; + } + } + for (int j = 2; j <= i / j; j++) { + if (i % j == 0) { + sum += j; + sum += i / j; + } + } + if (sum == i) { + str.append(i+","); + } + } + if (flag) { + i += 2; + flag = false; + } else { + i += 8; + flag = true; + } + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testGetPerfectNumbers() { + int[] newArray = getPerfectNumbers(33550337); + System.out.println("getPerfectNumbers:" + Arrays.toString(newArray)); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + if (array == null) + return "null"; + int n = array.length - 1; + if (n == -1) { + return ""; + } + StringBuilder b = new StringBuilder(); + for (int i = 0; ; i++) { + b.append(array[i]); + if (i == n) { + return b.toString(); + } + b.append(seperator); + } + } + + @Test + public void testJoin() { + int[] a1 = {3, 5, 7}; + System.out.println(join(a1, "-")); + } + +} diff --git a/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..cd1753941c --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.code.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/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/Struts.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..262b4bca59 --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/Struts.java @@ -0,0 +1,79 @@ +package com.code.coderising.litestruts; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + SAXReader reader = new SAXReader(); + View view=new View(); + try { + Document e = reader.read(ClassLoader.getSystemResource("struts.xml")); + //得到根节点 + Element struts = e.getRootElement(); + Iterator it = struts.elementIterator(); + while(it.hasNext()) {//action遍历 + Element action = (Element)it.next(); + // 找到 name=actionName 的action + List actionAtr = action.attributes(); + if(!actionAtr.get(0).getValue().equals(actionName)) + continue; + String className= actionAtr.get(1).getValue(); + //得到actionName 对应的class + Class classz=Class.forName(className); + Object o=classz.newInstance(); + Field field; + for(Map.Entry entry:parameters.entrySet()){ + field = classz.getDeclaredField(entry.getKey()); + field.setAccessible(true); + field.set(o, entry.getValue()); + } + Method m = classz.getMethod("execute"); + String resultString=(String)m.invoke(o); + Method m2 = classz.getMethod("getMessage"); + String message=(String)m2.invoke(o); + + Map map=new HashMap<>(); + + map.put("message",message); + view.setParameters(map); + it=action.elementIterator(); + //遍历 + while (it.hasNext()){ + Element result = (Element)it.next(); + String s= result.attribute(0).getValue(); + if(resultString.equals(s)){ + view.setJsp(result.getStringValue()); + return view; + } + } + } + } catch (DocumentException var9) { + var9.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..fe414cf1a7 --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.code.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/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/View.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/View.java new file mode 100644 index 0000000000..f55dae45fa --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.code.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/group10/1363044717/struts/homework/src/main/resources/struts.xml b/group10/1363044717/struts/homework/src/main/resources/struts.xml new file mode 100644 index 0000000000..b9489dad6d --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" new file mode 100644 index 0000000000..73a0b0ae33 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" @@ -0,0 +1,116 @@ +package com.coding.basic; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + //默认容量 + private static final int DEFAULT_CAPACITY = 10; + + public ArrayList(int capacity){ + if(capacity >= 0){ + elementData = new Object[capacity]; + }else { + throw new IllegalArgumentException("Illegal Capacity: " + + capacity); + } + + } + public ArrayList(){ + this(DEFAULT_CAPACITY); + } + + /** + * 保证集合容量 + * @param minCapacity + */ + private void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + //扩容 + int newCapacity = oldCapacity + (oldCapacity >> 1) + 1; + if(minCapacity - newCapacity > 0){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + private void checkIndexRange(int index) + { + if(index >= size || index < 0) + { + throw new IndexOutOfBoundsException("Index out of bounds, index : " + index); + } + } + public void add(E o){ + ensureCapacity(size+1); + elementData[size++] = o; + } + public void add(int index, E o){ + checkIndexRange(index);//检查下标 + ensureCapacity(size+1);//保证数组容量 + System.arraycopy(elementData,index,elementData,index + 1,size-index);//数组复制,把index后的元素全部向后移一位 + elementData[index] = o;//插入元素值 + size++;//元素size加一 + } + + @Override + public E get(int index) { + checkIndexRange(index);//检查下标 + return (E)elementData[index]; + } + + @Override + public E remove(int index) { + E e = this.get(index); + int numMoved = size - index - 1; + if(numMoved > 0) + { + System.arraycopy(elementData, index+1, elementData, index, numMoved);//数组复制,把index后的元素全部向前移一位 + } + elementData[--size] = null;//最后一位赋值为null,size-1 + return e; + } + + + public int size(){ + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private int endIndex = size - 1; + private int index = 0; + + public ArrayListIterator(){ + } + @Override + public boolean hasNext() { + return this.index < this.endIndex; + } + + @Override + public E next() { + if(!this.hasNext()) { + throw new NoSuchElementException();//没有元素了 + } else { + return (E)elementData[this.index++]; + } + } + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" new file mode 100644 index 0000000000..a1653b6e9f --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/BinaryTreeNode.java" @@ -0,0 +1,68 @@ +package com.coding.basic; + + +public class BinaryTreeNode implements Comparable { + public BinaryTreeNode(Object data) { + this.data = data; + } + + 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) { + //左子节点的值永远比父节点的值小 + //右子节点的值永远比父节点的值大 + BinaryTreeNode node = new BinaryTreeNode(o); + insertNode(node); + return node; + } + private void insertNode(BinaryTreeNode node){ + insertNode(this,node); + } + private void insertNode(BinaryTreeNode parentNode, BinaryTreeNode node) { + if (parentNode.compareTo(node) <= 0) {//数字大于父节点 + if (parentNode.right == null) { + parentNode.right = node; + return; + } + insertNode(parentNode.right, node); + } else { + if (parentNode.left == null) { + parentNode.left = node; + return; + } + insertNode(parentNode.left, node); + } + } + + @Override + public int compareTo(BinaryTreeNode o) { + Integer i = (Integer) this.data; + return i.compareTo((Integer) o.data); + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" new file mode 100644 index 0000000000..09e5b73661 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Iterator.java" @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public E next(); +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" new file mode 100644 index 0000000000..208fa8390d --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/JavaTest.java" @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by Mori on 2017/2/21. + */ +public class JavaTest { + @Test + public void testBinaryTreeNode(){ + BinaryTreeNode node = new BinaryTreeNode(5); + node.insert(4);//左 + node.insert(7);//右 + node.insert(2);//左左 + node.insert(6);//右左 + node.insert(5);//右左左 + node.insert(6);//右左右 + System.out.println(node.getData()); + System.out.println(node.getLeft().getData()); + System.out.println(node.getRight().getData()); + System.out.println(node.getLeft().getLeft().getData()); + System.out.println(node.getRight().getLeft().getData()); + System.out.println(node.getRight().getLeft().getLeft().getData()); + System.out.println(node.getRight().getLeft().getRight().getData()); + } + @Test + public void testArrayList(){ + ArrayList list =new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(5); + Assert.assertEquals((Object) list.get(2),3); + Assert.assertEquals((Object) list.remove(2),3); + Assert.assertEquals((Object) list.get(2),5); + Iterator listIterator = list.iterator(); + while (listIterator.hasNext()){ + System.out.println(listIterator.next()); + } + } + @Test + public void testLinkedList(){ + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(5); + linkedList.addFirst(10); + linkedList.add(1,6); + // linkedList.removeLast(); + //linkedList.removeFirst(); + Iterator linkedListIterator = linkedList.iterator(); + while (linkedListIterator.hasNext()){ + System.out.println(linkedListIterator.next()); + } + System.out.println("----"); + System.out.println(linkedList.remove(0)); + System.out.println(linkedList.remove(2)); + //System.out.println(linkedList.get(3)); + //System.out.println(linkedList.get(4)); + } +} \ No newline at end of file diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" new file mode 100644 index 0000000000..9a1ae01119 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" @@ -0,0 +1,162 @@ +package com.coding.basic; + +import jdk.nashorn.internal.ir.ReturnNode; + +import java.lang.reflect.Array; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head;//链表的头 + private Node tail;//链表的结尾 + private int size;//记录当前元素的size + + public void add(E e) { + Node node = new Node(e); + if (head == null) { + head = node; + } else { + tail.next = node; + } + tail = node; + tail.next = null; + size++; + } + + private void checkIndexRange(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + public void add(int index, E e) { + checkIndexRange(index); + Node node = new Node(e); + Node temp = head; + Node temp2 = null; + for (int i = 0; i < index - 1; i++) { + temp = temp.next; + } + temp2 = temp.next; + temp.next = node; + node.next = temp2; + size++; + } + + @Override + public E get(int index) { + checkIndexRange(index); + Node temp = head; + + for (int i = 0; i < index; i++) { + temp = temp.next; + } + return (E) temp.data; + } + + @Override + public E remove(int index) { + checkIndexRange(index); + if (index == 0) { + E e = removeFirst(); + return e; + } + if (index == size - 1) { + E e = removeLast(); + return e; + } + Node temp = head; + for (int i = 0; i < index - 1; i++) { + temp = temp.next; + } + E e = (E) temp.next.data; + temp.next = temp.next.next; + size--; + return e; + } + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E e) { + Node node = new Node(e); + node.next = head; + head = node; + size++; + } + + public void addLast(E e) { + this.add(e); + } + + public E removeFirst() { + E e = (E) head.data; + head = head.next; + size--; + return e; + } + + public E removeLast() { + Node temp = head; + for (int i = 0; i < size - 1; i++) { + temp = temp.next; + } + temp.next = null; + E e = (E) tail.data; + tail = temp; + size--; + return e; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + E data; + Node next; + public Node(E e) { + this.data = e; + } + } + private class LinkedListIterator implements Iterator{ + + private Node head;//链表的头 + private Node tail;//链表的结尾 + private Node node;//当前遍历的node + private int index; + private int endIndex; + + public LinkedListIterator(LinkedList list){ + this.head=list.head; + this.tail=list.tail; + this.endIndex = list.size() - 1; + node=head; + } + @Override + public boolean hasNext() { + return this.index < this.endIndex; + } + + @Override + public E next() { + if(!this.hasNext()) { + throw new NoSuchElementException();//没有元素了 + } else { + if(index == 0){ + node = head; + }else { + node = node.next; + } + index++; + return (E)node.data; + } + } + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" new file mode 100644 index 0000000000..899ba2bd3e --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/List.java" @@ -0,0 +1,10 @@ +package com.coding.basic; + +public interface List { + public void add(E o); + public void add(int index, E o); + public E get(int index); + public E remove(int index); + public int size(); + boolean isEmpty(); +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" new file mode 100644 index 0000000000..a8d5741846 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Queue.java" @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + /** + * 进队列 + * @param o + */ + public void enQueue(E o){ + elementData.addLast(o);//添加到队尾 + } + + /** + * 出队列 + * @return + */ + public E deQueue(){ + return elementData.removeFirst();//移除队首 + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public int size(){ + return elementData.size(); + } +} diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" new file mode 100644 index 0000000000..de407c8548 --- /dev/null +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/Stack.java" @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + public void push(E o){ + elementData.add(o); + } + + /** + * 移除栈顶并返回他 + * @return + */ + public E pop(){ + return elementData.remove(elementData.size()-1); + } + /** + * 得到栈顶元素 + * @return + */ + public E peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.isEmpty(); + } + public int size(){ + return elementData.size(); + } +} diff --git a/group10/205301442/src/com/coding/week1/ArrayList.java b/group10/205301442/src/com/coding/week1/ArrayList.java new file mode 100644 index 0000000000..db50f7ef46 --- /dev/null +++ b/group10/205301442/src/com/coding/week1/ArrayList.java @@ -0,0 +1,96 @@ +package com.coding.week1; + + + + +public class ArrayList implements List{ + private int size = 0; + + private Object[] elementData = {}; + + public void add(Object o){ + extendIndex(); + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + + if(index>size){ + System.out.println("Exception in thread \""+Thread.currentThread()+"\" java.lang.IndexOutOfBoundsException:Index:"+index+",Size:"+size); + return; + } + int length = elementData.length; + if(size==elementData.length){ + length=elementData.length+1; + } + Object[] newElement = new Object[length]; + System.arraycopy(elementData, 0, newElement, 0, index); + System.arraycopy(elementData, index, newElement,index+1,size-index ); + newElement[index]=o; + elementData = newElement; + size++; + } + + public Object get(int index){ + boolean isRange=rangeCheck(index); + if(!isRange){ + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + boolean isRange=rangeCheck(index); + if(!isRange){ + return null; + } + Object rmData = elementData[index]; + Object[] newElement = new Object[elementData.length]; + System.arraycopy(elementData, 0, newElement, 0, index);; + System.arraycopy(elementData, index+1, newElement,index,size-index-1 ); + elementData = newElement; + size--; + return rmData; + } + + public int size(){ + return size; + } + + public com.coding.week1.Iterator iterator(){ + return new Ito(); + } + public boolean rangeCheck(int index){ + + if(index>size-1||index<0){ + System.err.println("Exception in thread \""+Thread.currentThread()+"\" java.lang.IndexOutOfBoundsException:Index:"+index+",Size:"+size); + return false; + } + return true; + } + public void extendIndex(){ + Object[] newElement = new Object[elementData.length+1]; + System.arraycopy(elementData, 0, newElement, 0, size); + elementData = newElement; + + } + public class Ito implements com.coding.week1.Iterator{ + int cursor; + @Override + public boolean hasNext() { + if(cursor!=size){ + return true; + } + return false; + } + + @Override + public Object next() { + Object o=elementData[cursor]; + cursor++; + return o; + } + } + + +} diff --git a/group10/205301442/src/com/coding/week1/BinaryTreeNode.java b/group10/205301442/src/com/coding/week1/BinaryTreeNode.java new file mode 100644 index 0000000000..547e09d42f --- /dev/null +++ b/group10/205301442/src/com/coding/week1/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package com.coding.week1; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + public BinaryTreeNode(Object data){ + 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; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode newBTN = new BinaryTreeNode(o); + Integer insert = (Integer)o; + + BinaryTreeNode cursor = this; + while(true){ + if(insert.compareTo((Integer)cursor.data)==-1){ + if(cursor.left==null){ + cursor.left = newBTN; + break; + } + cursor = cursor.left; + }else if(insert.compareTo((Integer)cursor.data)==1){ + if(cursor.right==null){ + cursor.right = newBTN; + break; + } + cursor = cursor.right; + } + } + return newBTN; + } + +} diff --git a/group10/205301442/src/com/coding/week1/Iterator.java b/group10/205301442/src/com/coding/week1/Iterator.java new file mode 100644 index 0000000000..7f6f333443 --- /dev/null +++ b/group10/205301442/src/com/coding/week1/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.week1; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group10/205301442/src/com/coding/week1/LinkedList.java b/group10/205301442/src/com/coding/week1/LinkedList.java new file mode 100644 index 0000000000..2c14a0e9cb --- /dev/null +++ b/group10/205301442/src/com/coding/week1/LinkedList.java @@ -0,0 +1,162 @@ +package com.coding.week1; + +public class LinkedList implements List { + private int size; + private Node first; + private Node last; + public static class Node{ + Object data; + Node next; + Node prev; + public Node(Node prev,Object data,Node next){ + this.data = data; + this.next = next; + this.prev = prev; + } + } + + @Override + public void add(Object o) { + final Node l = last; + Node newNode = new Node(last,o,null); + last = newNode; + if(first==null){ + first = newNode; + }else{ + l.next = newNode; + } + size++; + + } + + @Override + public void add(int index, Object o) { + + if(index>size){ + System.out.println("Exception in thread \""+Thread.currentThread()+"\" java.lang.IndexOutOfBoundsException:Index:"+index+",Size:"+size); + return; + } + Node newNode = new Node(null,o,null); + Node nodePre = node(index-1); + Node oldNode = node(index); + if(nodePre!=null){ + nodePre.next =newNode; + newNode.prev = nodePre; + }else{ + first = newNode; + } + if(oldNode!=null){ + oldNode.prev = newNode; + newNode.next = oldNode; + }else{ + last = newNode; + } + size++; + } + + @Override + public Object get(int index) { + if(!rangeCheck(index)){ + return null; + } + + return node(index).data; + } + + @Override + public Object remove(int index) { + if(!rangeCheck(index)){ + return null; + } + Node prevNode = node(index-1); + Node nextNode = node(index+1); + Node rmNode = node(index); + if(prevNode!=null){ + prevNode.next = nextNode; + }else{ + first=nextNode; + } + if(nextNode!=null){ + nextNode.prev = prevNode; + }else{ + last = prevNode; + } + size--; + return rmNode.data; + } + + @Override + public int size() { + // TODO Auto-generated method stub + return size; + } + public Object head(){ + return first.data; + } + public Object last(){ + return last.data; + } + public void addFirst(Object o){ + add(0,o); + } + public void addLast(Object o){ + add(size,o); + } + public Object removeFirst(){ + Node f = first; + remove(0); + return f.data; + } + public Object removeLast(){ + Node l = last; + remove(size-1); + return l.data; + } + public Node node(int index){ + if(index<0){ + return null; + } + Node x =null; + if(index<(size<<1)){ + x = first; + for(int i=0;iindex;i--){ + x = x.prev; + } + } + return x; + } + public boolean rangeCheck(int index){ + + if(index>size-1||index<0){ + System.err.println("Exception in thread \""+Thread.currentThread()+"\" java.lang.IndexOutOfBoundsException:Index:"+index+",Size:"+size); + return false; + } + return true; + } + public Ito iterator(){ + return new Ito(); + } + public class Ito implements Iterator{ + int cursor; + @Override + public boolean hasNext() { + if(cursor!=size){ + return true; + } + return false; + } + + @Override + public Object next() { + Object o=node(cursor).data; + cursor++; + return o; + } + + } +} diff --git a/group10/205301442/src/com/coding/week1/List.java b/group10/205301442/src/com/coding/week1/List.java new file mode 100644 index 0000000000..e06b7c8ab9 --- /dev/null +++ b/group10/205301442/src/com/coding/week1/List.java @@ -0,0 +1,9 @@ +package com.coding.week1; + +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/group10/205301442/src/com/coding/week1/Queue.java b/group10/205301442/src/com/coding/week1/Queue.java new file mode 100644 index 0000000000..e3c4a44a42 --- /dev/null +++ b/group10/205301442/src/com/coding/week1/Queue.java @@ -0,0 +1,23 @@ +package com.coding.week1; + +public class Queue { + ArrayList list = new ArrayList(); + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + return list.remove(0); + } + + public boolean isEmpty(){ + if(list.size()==0){ + return true; + } + return false; + } + + public int size(){ + return list.size(); + } +} diff --git a/group10/205301442/src/com/coding/week1/Stack.java b/group10/205301442/src/com/coding/week1/Stack.java new file mode 100644 index 0000000000..307b3630df --- /dev/null +++ b/group10/205301442/src/com/coding/week1/Stack.java @@ -0,0 +1,28 @@ +package com.coding.week1; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int top = elementData.size()-1; + return elementData.remove(top); + } + + public Object peek(){ + int top = elementData.size()-1; + return elementData.get(top); + } + public boolean isEmpty(){ + if(elementData.size()==0){ + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group10/205301442/src/com/coding/week2/ArrayUtil.java b/group10/205301442/src/com/coding/week2/ArrayUtil.java new file mode 100644 index 0000000000..b11f5ee71d --- /dev/null +++ b/group10/205301442/src/com/coding/week2/ArrayUtil.java @@ -0,0 +1,199 @@ +package com.coding.week2; + +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 int[] reverseArray(int[] origin){ + int j=origin.length-1; + for(int i=0;i=nums.length?nums.length:size; + + System.arraycopy(nums, 0, newNums, 0, length); + return newNums; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + //这里为什么是开平方根 + public static int[] getPrimes(int max){ + int i=2; + int[] nums = new int[max]; + int index=0; + while(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字段中。 + + */ + File f1 = new File("config/struts.xml"); + + File f = new File(f1.getAbsolutePath()); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc=builder.parse(f); + Element root = doc.getDocumentElement(); + + Map> actions = new HashMap>(); + if (root.getNodeType() == Node.ELEMENT_NODE) { + NodeList actionList = root.getChildNodes(); + + for (int i = 0; i < actionList.getLength(); i++) { + Node n = actionList.item(i); + if (n.getNodeType() == Node.ELEMENT_NODE) { + NamedNodeMap nnmap = n.getAttributes(); + Map action = new HashMap(); + action.put(nnmap.item(0).getNodeValue(),nnmap.item(1).getNodeValue()); + NodeList result= n.getChildNodes(); + + for(int j = 0;j requestAction=null; + if(actionName!=null){ + requestAction=actions.get(actionName); + }else{ + System.err.println("没有actionName"); + } + + try { + if(requestAction!=null){ + Class c = Class.forName(requestAction.get(actionName)); + Object co = c.newInstance(); + if("login".equals(actionName)){ + String name = parameters.get("name"); + String password = parameters.get("password"); + Method m1=c.getMethod("setName", String.class); + Method m2=c.getMethod("setPassword", String.class); + Method m3 = c.getMethod("execute"); + m1.invoke(co, name); + m2.invoke(co, password); + String rest = (String)m3.invoke(co); + Method m4 = c.getMethod("getMessage"); + String message = (String)m4.invoke(co); + Map pras =new HashMap(); + pras.put("message", message); + View view = new View(); + view.setJsp(requestAction.get(rest)); + view.setParameters(pras); + return view; + } + }else{ + System.out.println("没有找到对应action"); + } + + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + +} diff --git a/group10/205301442/src/com/coding/week2/litestruts/StrutsTest.java b/group10/205301442/src/com/coding/week2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..99e41bd0f4 --- /dev/null +++ b/group10/205301442/src/com/coding/week2/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coding.week2.litestruts; + +import static org.junit.Assert.*; + +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); + System.out.println( view.getJsp()); + 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); + System.out.println( view.getJsp()); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group10/205301442/src/com/coding/week2/litestruts/View.java b/group10/205301442/src/com/coding/week2/litestruts/View.java new file mode 100644 index 0000000000..bf7bd0d6c8 --- /dev/null +++ b/group10/205301442/src/com/coding/week2/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.week2.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/group10/205301442/test/com/coding/week1/AllTests.java b/group10/205301442/test/com/coding/week1/AllTests.java new file mode 100644 index 0000000000..fb54214d4d --- /dev/null +++ b/group10/205301442/test/com/coding/week1/AllTests.java @@ -0,0 +1,11 @@ +package com.coding.week1; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ TestArrayList.class, TestLinkedList.class, TestQueue.class, TestStack.class,TestBiranyTreeNode.class }) +public class AllTests { + +} diff --git a/group10/205301442/test/com/coding/week1/TestArrayList.java b/group10/205301442/test/com/coding/week1/TestArrayList.java new file mode 100644 index 0000000000..0c7040d335 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestArrayList.java @@ -0,0 +1,31 @@ +package com.coding.week1; + +import static org.junit.Assert.*; +import org.junit.Test; + +public class TestArrayList { + ArrayList arrayList =new ArrayList(); + + @Test + public void Test(){ + //add + arrayList.add("MM"); + arrayList.add(1,"YY"); + arrayList.add(2,"ZZ"); + //get + assertEquals((String)arrayList.get(0), "MM"); + assertEquals(arrayList.size(),3 ); + //remove + assertEquals(arrayList.remove(2), "ZZ"); + assertEquals(arrayList.size(),2 ); + //iterator + Iterator ito = arrayList.iterator(); + int i=0; + while(ito.hasNext()){ + assertEquals(ito.next(), arrayList.get(i)); + i++; + } + } + + +} diff --git a/group10/205301442/test/com/coding/week1/TestBiranyTreeNode.java b/group10/205301442/test/com/coding/week1/TestBiranyTreeNode.java new file mode 100644 index 0000000000..5968ebf562 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestBiranyTreeNode.java @@ -0,0 +1,21 @@ +package com.coding.week1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestBiranyTreeNode { + + @Test + public void test() { + BinaryTreeNode node = new BinaryTreeNode(5); + node.insert(2); + node.insert(7); + node.insert(1); + node.insert(6); + System.out.println(" "+node.getData()); + System.out.println(" "+node.getLeft().getData()+" "+node.getRight().getData()); + System.out.println(node.getLeft().getLeft().getData()+" null "+node.getRight().getLeft().getData()+" null"); + } + +} diff --git a/group10/205301442/test/com/coding/week1/TestLinkedList.java b/group10/205301442/test/com/coding/week1/TestLinkedList.java new file mode 100644 index 0000000000..db6524ac53 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestLinkedList.java @@ -0,0 +1,42 @@ +package com.coding.week1; + +import static org.junit.Assert.*; + + +import org.junit.Test; + +public class TestLinkedList { + LinkedList linkedList = new LinkedList(); + + @Test + public void test() { + //add + linkedList.add("AA"); + linkedList.add(0,"BB"); + linkedList.add(1,"CC"); + linkedList.add(3,"DD"); + + assertEquals(linkedList.get(0), "BB"); + assertEquals(linkedList.get(1), "CC"); + assertEquals(linkedList.get(2), "AA"); + assertEquals(linkedList.last(), "DD"); + //add first last + linkedList.addFirst("EE"); + assertEquals(linkedList.get(0), "EE"); + linkedList.addLast("FF"); + assertEquals(linkedList.get(5), "FF"); + //remove + assertEquals(linkedList.remove(1), "BB"); + assertEquals(linkedList.removeFirst(), "EE"); + assertEquals(linkedList.removeLast(), "FF"); + //iterator + Iterator ito = linkedList.iterator(); + int i=0; + while(ito.hasNext()){ + assertEquals(linkedList.get(i), ito.next()); + i++; + } + assertEquals(i, linkedList.size()); + } + +} diff --git a/group10/205301442/test/com/coding/week1/TestQueue.java b/group10/205301442/test/com/coding/week1/TestQueue.java new file mode 100644 index 0000000000..525eaf5886 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestQueue.java @@ -0,0 +1,17 @@ +package com.coding.week1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestQueue { + + @Test + public void test() { + Queue queue = new Queue(); + queue.enQueue("MM"); + assertEquals(queue.deQueue(), "MM"); + assertEquals(queue.isEmpty(), true); + } + +} diff --git a/group10/205301442/test/com/coding/week1/TestStack.java b/group10/205301442/test/com/coding/week1/TestStack.java new file mode 100644 index 0000000000..ababf23df6 --- /dev/null +++ b/group10/205301442/test/com/coding/week1/TestStack.java @@ -0,0 +1,23 @@ +package com.coding.week1; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class TestStack { + + @Test + public void test() { + Stack stack = new Stack(); + stack.push("AA"); + stack.push("BB"); + stack.push("CC"); + stack.push("DD"); + assertEquals(stack.pop(), "DD"); + assertEquals(stack.pop(), "CC"); + assertEquals(stack.peek(), "BB"); + assertEquals(stack.size(), 2); + assertEquals(stack.isEmpty(),false); + } + +} diff --git "a/group10/205301442/\346\226\207\347\253\240\345\234\260\345\235\200.txt" "b/group10/205301442/\346\226\207\347\253\240\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..fab8d99a58 --- /dev/null +++ "b/group10/205301442/\346\226\207\347\253\240\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +first week http://blog.csdn.net/kellyfun/article/details/57504808 \ No newline at end of file diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.classpath" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.classpath" new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.classpath" @@ -0,0 +1,6 @@ + + + + + + diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.project" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.project" new file mode 100644 index 0000000000..34d5612cf4 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.project" @@ -0,0 +1,17 @@ + + + DataStructure_One + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.settings/org.eclipse.jdt.core.prefs" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.settings/org.eclipse.jdt.core.prefs" new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/ArrayList.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/ArrayList.java" new file mode 100644 index 0000000000..1d3f5bf099 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/ArrayList.java" @@ -0,0 +1,236 @@ +package list; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List{ + private int size ; + private Object [] elementData; + private static final Object [] EMPTY_ELEMENTDATA ={}; + private static final int DEFAULT_CAPACITY = 10; + private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + public ArrayList(){ + this.elementData = EMPTY_ELEMENTDATA; + } + public ArrayList(int limit){ + if(limit<0) + throw new IllegalArgumentException("Illegal Capacity: "+limit); + this.elementData = new Object[limit]; + } + /** + * 添加元素 + * @param o 元素 + * @return 是否成功添加 + */ + public boolean add(Object o){ + ensureCapacityInternal(size+1); + elementData[size++]=o; + return true; + } + /** + * 添加元素 + * @param index 添加位置 + * @param o 元素 + * @return 是否成功添加 + */ + public void add(int index, Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + ensureCapacityInternal(size + 1); + //将A数组的的a位置开始 复制至B数组的a+1位置进行覆盖--->等于将B数组a位置后面的所有元素向后移了一位。 + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + /** + * 删除元素 + * @param o 元素 + * @return 是否成功删除 + */ + public boolean remove(Object o) { + if (o == null) { + for (int index = 0; index < size; index++) { + if (elementData[index] == null) { + fastRemove(index); + return true; + } + } + } else { + for (int i = 0; i < size; i++) { + if(elementData[i].equals(o)){ + fastRemove(i); + return true; + } + } + } + + return false; + } + /** + * 删除元素 + * @param index 需要删除的元素的位置 + * @return 被删除的元素 + */ + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + Object o =elementData[index]; + fastRemove(index); + return o; + } + + + + /** + * 查询元素 + * @param index 位置 + * @return 被查询的元素 + */ + public Object get(int index){ + if(index>=size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + return elementData[index]; + } + + + + + private void fastRemove(int index) { + //需要移动的个数 + int move = size-index-1; + System.arraycopy(elementData, index+1, elementData, index, move); + elementData[size]=null; + size--; + } + private void ensureCapacityInternal(int newLength) { + if (elementData == EMPTY_ELEMENTDATA) { + newLength = Math.max(DEFAULT_CAPACITY, newLength); + } + //如果长度超过了elementData分配的内存上限 则需要继续分配内存 + if(newLength-elementData.length>0){ + grow(newLength); + } + } + + private void grow(int newLength) { + int oldCapacity = elementData.length; + int newCapacity = (oldCapacity >> 1) + oldCapacity; + if (newCapacity - newLength < 0) { + newCapacity = newLength; + } + // int 上限:2147483648 + if (newCapacity > MAX_ARRAY_SIZE) { + newCapacity = newLength > MAX_ARRAY_SIZE ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; + } + // 扩容 + elementData = Arrays.copyOf(elementData, newLength); + } + + @Override + public void clear() { + for (int i = 0; i < size; i++) { + elementData[i]=null; + } + size=0; + } + + + @Override + public boolean contains(Object o) { + if (indexOf(o) >= 0) + return true; + return false; + } + + @Override + public int indexOf(Object o) { + if(o==null){ + for (int i = 0; i < size; i++) { + if(elementData[i]==null) + return i; + } + }else{ + for (int i = 0; i < size; i++) { + if(elementData[i].equals(o)) + return i; + } + } + return -1; + } + + @Override + public boolean isEmpty() { + return size==0; + } + + + @Override + public Object set(int index, Object o) { + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + Object oldO=elementData[index]; + elementData[index]=o; + return oldO; + } + @Override + public int size() { + + return size; + } + + @Override + public Iterator iterator() { + return new ArrayIterator(); + } + private class ArrayIterator implements Iterator{ + + int limit=ArrayList.this.size; + int cursor; + int lastRet = -1; + + @Override + public boolean hasNext() { + return cursor < limit; + } + + @Override + public Object next() { + int i = cursor; + if (cursor >= size) { + throw new NoSuchElementException(); + } + Object[] o = ArrayList.this.elementData; + if (cursor > o.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return o[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + try { + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + limit--; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + } + + + + + + + + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/Iterator.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/Iterator.java" new file mode 100644 index 0000000000..142fc4bafe --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/Iterator.java" @@ -0,0 +1,20 @@ +package list; + +public interface Iterator { + /** + * 是否有第一个值 + * @return + */ + boolean hasNext(); + + /** + * 获取下一个值 + * @return + */ + Object next(); + + /** + * 删除 + */ + void remove(); +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/LinkedList.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/LinkedList.java" new file mode 100644 index 0000000000..c311e183c8 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/LinkedList.java" @@ -0,0 +1,211 @@ +package list; + +public class LinkedList implements List { + + public Node getFirst() { + return first; + } + + public Node getLast() { + return last; + } + + private int size; + private Node first; + private Node last; + + @Override + public void add(int paramInt, Object paramE) { + checkPositionIndex(paramInt); + if (paramInt == size) { + linkLast(paramE); + } else { + linkBefore(paramE, node(paramInt)); + } + + } + + // 寻找到需要插入的那个node + Node node(int paramInt) { + if (paramInt < size >> 1) { + Node x = first; + for (int i = 0; i < paramInt; i++) { + x = x.next; + } + return x; + } else { + Node x = last; + for (int i = size - 1; i > paramInt; i--) { + x = x.prev; + } + return x; + } + } + + private void linkBefore(Object o, Node node) { + Node pred = node.prev; + Node newNode = new Node(pred, o, node); + node.prev = newNode; + if (pred == null) { + first = newNode; + } else { + pred.next = newNode; + } + + } + + private void checkPositionIndex(int paramInt) { + if (paramInt < 0 || paramInt > size) { + throw new IndexOutOfBoundsException("Index: " + paramInt + ", Size: " + size); + } + } + + @Override + public boolean add(Object paramE) { + linkLast(paramE); + return true; + } + + private void linkLast(Object paramE) { + Node l = last; + Node newNode = new Node(l, paramE, null); + last = newNode; + if (l == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + } + + @Override + public void clear() { + for (Node x = first; x != null;) { + Node next = x.next; + x.next = null; + x.prev = null; + x.item = null; + x = next; + } + first = last = null; + size = 0; + } + + @Override + public boolean contains(Object paramObject) { + return false; + } + + @Override + public Object get(int paramInt) { + checkPositionIndex(paramInt); + return node(paramInt).item; + } + + @Override + public int indexOf(Object paramObject) { + return 0; + } + + @Override + public boolean isEmpty() { + return size == 0 ? true : false; + } + + @Override + public Iterator iterator() { + return new LinkedIterator(); + } + + @Override + public Object remove(int paramInt) { + checkPositionIndex(paramInt); + return unlink(node(paramInt)); + } + + private Object unlink(Node node) { + Object item = node.item; + Node prev = node.prev; + Node next = node.next; + if (prev == null) { + first = next; + } else { + prev.next = next; + node.prev = null; + } + if (next == null) { + last = prev; + } else { + next.prev = prev; + node.next = null; + } + node.item = null; + size--; + + return item; + + } + + @Override + public boolean remove(Object paramObject) { + return false; + } + + @Override + public Object set(int paramInt, Object paramE) { + return null; + } + + @Override + public int size() { + return size; + } + + public static class Node { + public Object item; + public Node next; + public Node prev; + + public Node(Node prev, Object item, Node next) { + this.item = item; + this.next = next; + this.prev = prev; + } + + } + + private class LinkedIterator implements Iterator { + + int cursor = 0; + int lastRet = -1; + + @Override + public boolean hasNext() { + return cursor < size(); + } + + @Override + public Object next() { + int i = cursor; + Object node = get(i); + lastRet = i; + cursor = i + 1; + return node; + } + + @Override + public void remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + LinkedList.this.remove(lastRet); + if (lastRet < cursor) { + cursor--; + } + lastRet = -1;// 防止对一个数据多次remove操作 + + } + + } + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/List.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/List.java" new file mode 100644 index 0000000000..05b2c99294 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/List.java" @@ -0,0 +1,32 @@ +package list; + + +public interface List{ + + public abstract void add(int paramInt, Object paramE); + + public abstract boolean add(Object paramE); + + public abstract void clear(); + + public abstract boolean contains(Object paramObject); + + public abstract boolean equals(Object paramObject); + + public abstract Object get(int paramInt); + + public abstract int indexOf(Object paramObject); + + public abstract boolean isEmpty(); + + public abstract Iterator iterator(); + + public abstract Object remove(int paramInt); + + public abstract boolean remove(Object paramObject); + + public abstract Object set(int paramInt, Object paramE); + + public abstract int size(); + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/queue/ListQueue.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/queue/ListQueue.java" new file mode 100644 index 0000000000..80c52f2d53 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/queue/ListQueue.java" @@ -0,0 +1,56 @@ +package queue; + +import list.LinkedList; + +public class ListQueue { + + LinkedList list; + + public ListQueue() { + list = new LinkedList(); + } + + public int size() { + return list.size(); + } + + public boolean isEmpty() { + return list.size() == 0 ? true : false; + } + + public boolean remove(Object o) { + return list.remove(o); + } + + public void clear() { + list.clear(); + } + + public boolean add(Object e) { + return list.add(e); + } + + public boolean offer(Object e) { + return false; + } + + public Object poll() { + if (list.size() == 0) { + return null; + }else{ + Object item = list.getFirst().item; + list.remove(0); + return item; + } + } + + + public Object peek() { + if (list.size() == 0) { + return null; + }else{ + return list.getFirst().item; + } + } + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/stack/Stack.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/stack/Stack.java" new file mode 100644 index 0000000000..4bbffc1e0f --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/stack/Stack.java" @@ -0,0 +1,31 @@ +package stack; + +import java.util.NoSuchElementException; + +import list.ArrayList; +import list.List; + +public class Stack { + + List list ; + public Stack(){ + list = new ArrayList(); + } + + public void push(Object o) { + list.add(o); + } + public Object poll(){ + Object remove = list.remove(list.size()-1); + if(remove==null) + throw new NoSuchElementException(); + return remove; + } + + public Object peak(){ + if(list.size()==0) + throw new NoSuchElementException(); + return list.get(list.size()-1); + } + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/tree/BinaryTree.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/tree/BinaryTree.java" new file mode 100644 index 0000000000..420e131537 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/tree/BinaryTree.java" @@ -0,0 +1,74 @@ +package tree; + +public class BinaryTree { + private TreeNode parent; + + public BinaryTree(){ + parent = new TreeNode(null,null,null); + } + + public void insert(Object o ){ + TreeNode node = new TreeNode(null,o,null); + if(parent.item==null){ + parent= node; + return; + } + insertNode(parent,node); + } + + private void insertNode(TreeNode parentNode, TreeNode newNode) { + if(parentNode.compareTo(newNode)<= 0){ + if(parentNode.right==null){ + parentNode.right = newNode; + }else{ + insertNode(parentNode.right,newNode); + } + + }else{ + if(parentNode.left==null){ + parentNode.left=newNode; + }else{ + insertNode(parentNode.left, newNode); + } + } + } + + public void printTree(){ + printNode(this.parent); + } + + + + + private void printNode(TreeNode node) { + if (node == null) { + System.out.println("node :" + node.item); + printNode(node.left); + printNode(node.right); + } + + } + + + + + class TreeNode implements Comparable{ + Object item; + TreeNode left; + TreeNode right; + TreeNode(TreeNode left,Object item,TreeNode right){ + this.item=item; + this.left =left; + this.right=right; + } + + @Override + public int compareTo(TreeNode o) { + Integer parentItem = (Integer) this.item; + Integer oItem = (Integer) o.item; + + return parentItem.compareTo(oItem); + } + } + +} diff --git a/group10/3314793852/.classpath b/group10/3314793852/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group10/3314793852/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group10/3314793852/.gitignore b/group10/3314793852/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/3314793852/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/3314793852/.project b/group10/3314793852/.project new file mode 100644 index 0000000000..e09255853a --- /dev/null +++ b/group10/3314793852/.project @@ -0,0 +1,17 @@ + + + FirstWeek + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/3314793852/second/.classpath b/group10/3314793852/second/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group10/3314793852/second/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group10/3314793852/second/.gitignore b/group10/3314793852/second/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/3314793852/second/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/3314793852/second/.project b/group10/3314793852/second/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group10/3314793852/second/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs b/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..5855257b83 --- /dev/null +++ b/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/com/coderising/litestruts=UTF-8 +encoding//src/com/coderising/litestruts/Struts.java=UTF-8 +encoding/=UTF-8 diff --git a/group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs b/group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group10/3314793852/second/.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/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java b/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5742ea09f9 --- /dev/null +++ b/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,236 @@ + + package com.coderising.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 size=origin.length-1; + int[] arr=new int[origin.length]; + for(int i=0;iarray1[j]&&array1[i]!=0&&array1[j]!=0){ + int temp; + temp=array1[i]; + array1[i]=array1[j]; + array1[j]=temp; + } + if(array1[i]==array1[j]&&array1[i]!=0&&array1[j]!=0){ + array1[j]=0; + } + } + } + + System.out.println(Arrays.toString(array1)); + + int[] array3=removeZero(array1); //除零操作。 + return array3; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArr=new int[oldArray.length+size]; + for(int i=0;i2){ + arr[0]=1; + arr[1]=1; + while(second aMap=new HashMap(); //一个用来存储。 + + private ArrayList> list=new ArrayList>();; //List集合用来保存Map集合。 + + private String currentTag; + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { + currentTag =qName; + + if("action".equals(currentTag)){ + key=attributes.getValue(0); //action的name属性。 + value=attributes.getValue(1); //action的class属性。 + + //在将属性成对的保存到一个Map集合中。 + aMap.put(key, value); + + //保存后将中间变量变为null. + key=null; + value=null; + } + + if("result".equals(currentTag)){ + key=attributes.getValue(0); //result的name属性。 + } + + } + + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + if("result".equals(currentTag)){ + String name=new String(ch,start,length); + value=name; + //将属性成对的保存到一个Map集合中。 + + aMap.put(key, value); + + //保存后将中间变量清空。 + key=null; + value=null; + currentTag=null; + } + + } + + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if("action".equals(qName)){ + + list.add(aMap); + + + aMap=new HashMap(); + + + } + + } + + //返回list集合。 + public ArrayList> getDate(){ + return list; + } + } diff --git a/group10/3314793852/second/src/com/coderising/litestruts/LoginAction.java b/group10/3314793852/second/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group10/3314793852/second/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/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java b/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java new file mode 100644 index 0000000000..52dd35961d --- /dev/null +++ b/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java @@ -0,0 +1,42 @@ + /* + *该类用于读取struts.xml文件中的数据,并把它存储到ActionType类的对象中去。 + */ + package com.coderising.litestruts; + + import java.io.File; + import java.io.FileInputStream; + import java.io.IOException; + import java.io.InputStream; + import java.util.ArrayList; + import java.util.HashMap; + + import javax.xml.parsers.ParserConfigurationException; + import javax.xml.parsers.SAXParser; + import javax.xml.parsers.SAXParserFactory; + + import org.xml.sax.InputSource; + import org.xml.sax.SAXException; + import org.xml.sax.XMLReader; + + + + public class SAXGetInfo { + + public ArrayList> getDate() throws SAXException, IOException, ParserConfigurationException{ + //创建解析工厂 + SAXParserFactory factory=SAXParserFactory.newInstance(); + + //创建解析器 + SAXParser parser=factory.newSAXParser(); + + //文件地址 + String fileName="E:/CODING2017/Code/coding2017/group10/3314793852/second/src/com/coderising/litestruts/struts.xml"; + //设置内容处理器 + DealWithInfo handler=new DealWithInfo(); + parser.parse(fileName, handler); + + ArrayList> list=handler.getDate(); + return list; //将保存在list集合中的配置信息返回。 + } + + } diff --git a/group10/3314793852/second/src/com/coderising/litestruts/Struts.java b/group10/3314793852/second/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..5312535300 --- /dev/null +++ b/group10/3314793852/second/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,165 @@ + + package com.coderising.litestruts; + + import java.io.IOException; + import java.util.ArrayList; + import java.util.HashMap; + import java.util.Iterator; + import java.util.Map; + + import javax.xml.parsers.ParserConfigurationException; + + import org.xml.sax.SAXException; + + import java.lang.reflect.InvocationTargetException; + import java.lang.reflect.Method; + + + + public class Struts { + + public static View runAction(String actionName, Map parameters) { + View view=new View(); + + String execute=null; //execute返回值。 + String placeOfJsp=null; //JSP地址。 + String classname=getClassName(actionName); + + Class classAction = null; + Object obj=null; + try { + classAction=Class.forName(classname); //根据类名反射实例化class类。 + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + //ClassAction类使用newInstance();方法调用LoginAction类的默认构造方法创建LoginAction类。 + try { + obj=classAction.newInstance(); //Object类型的对象。 + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + //调用对象的setter方法,设置用户名和密码,即把name和password的值设置到当前LoginAction的对象中。 + try { + setter(obj,"name",parameters.get("name"),String.class); + setter(obj,"password",parameters.get("password"),String.class); + } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + e.printStackTrace(); + } + + //调用对象的exectue方法。 + try{ + Method method=classAction.getMethod("execute"); + execute=(String) method.invoke(obj); + }catch(Exception e){ + e.printStackTrace(); + } + + //调用对象的所有getter方法,把值设置到view对象的parameters属性中。 + Map para=new HashMap(); + try{ + para.put("name", getter(obj,"name")); + para.put("password", getter(obj,"password")); + para.put("message", getter(obj,"message")); + }catch(Exception e){ + e.printStackTrace(); + } + view.setParameters(para); + + //根据execute返回值和配置,将JSP地址赋值给View对象的jsp成员中。 + placeOfJsp=getResultName(actionName,execute); + view.setJsp(placeOfJsp); + + //System.out.println(view.getJsp()+" "+view.getParameters()); + return view; + } + + + /* + * 第一个参数为操作对象,第二个参数为操作的数据的数据成员,第三个参数为set的值,第四个参数为参数的类型 + */ + private static void setter(Object obj, String att, Object value, Class type) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + //实例化一个set方法 + Method method=obj.getClass().getMethod("set"+initStr(att), type); + method.invoke(obj, value); + } + + /* + * getter方法 + */ + private static String getter(Object obj,String att) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Method method=obj.getClass().getMethod("get"+initStr(att)); + method.invoke(obj); + return (String) method.invoke(obj); + } + + /* + * 根据java的命名规则,数据成员的第一个单词都要小写,其他单词的首字母大写。 + * 所以把set和get方法后面的单词的首字母大写,如setName中的N + * 和getName中的N. + */ + private static String initStr(String old) { + String str=old.substring(0,1).toUpperCase()+old.substring(1); + //把首字母大写。 + return str; + } + + public static void main(String args[]){ + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + runAction(actionName,params); + } + + //查找action类,查找result时,必须要通过查找action才行。 + private static String getClassName(String actionName){ + String className=getData(actionName).get(actionName); + + return className; + } + + //查找result时,必须要通过查找action才行。 + private static String getResultName(String actionName,String result){ + String placeOfJsp=getData(actionName).get(result); + + return placeOfJsp; + } + + //从配置信息中获取数据,获取 + private static HashMap getData(String actionName) { + SAXGetInfo a=new SAXGetInfo(); + ArrayList> x; + HashMap y=null; + Object[] arr=new Object[10]; + int size=0; + + try { + x = a.getDate(); + Iterator it=x.iterator(); + while(it.hasNext()){ + arr[size]=(Object)it.next(); + size++; + + } + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + if(actionName.equals("login")){ + y=(HashMap) arr[0]; + } + if(actionName.equals("logout")){ + y=(HashMap) arr[1]; + } + return y; + } + + } diff --git a/group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java b/group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group10/3314793852/second/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/group10/3314793852/second/src/com/coderising/litestruts/View.java b/group10/3314793852/second/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group10/3314793852/second/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/group10/3314793852/second/src/com/coderising/litestruts/struts.out.xml b/group10/3314793852/second/src/com/coderising/litestruts/struts.out.xml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group10/3314793852/second/src/com/coderising/litestruts/struts.xml b/group10/3314793852/second/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group10/3314793852/second/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/group10/3314793852/src/myList/MyArrayList.java b/group10/3314793852/src/myList/MyArrayList.java new file mode 100644 index 0000000000..7b5a883190 --- /dev/null +++ b/group10/3314793852/src/myList/MyArrayList.java @@ -0,0 +1,103 @@ +package myList; + +/* + * ArrayListĵײһ飬ͨ´ķ̬ + * ArrayListղݡ + */ + +public class MyArrayList { + private int theSize; //ǰС + private static final int DEFAULT_CAPACITY=10; //Ĭ + private Object[] theArr=new Object[10]; //ײ + + //ʼ + public MyArrayList(){ + clear(); + } + + // + public void clear(){ + theSize=0; + capacityBigger(DEFAULT_CAPACITY); + } + + //ȡС + public int size(){ + return theSize; + } + + //ȡײ + public Object[] getArr(){ + return theArr; + + } + //룬ֱӲ뵽β + public void add(Object a){ + add(theSize, a); + } + + //±ȡ + public Object get(int i){ + if(i<0||i>=theSize){ + throw new ArrayIndexOutOfBoundsException(); + } + return theArr[i]; + } + + //룬ָ±롣 + public void add(int i,Object a){ + + if(theSize==theArr.length){ //ʼΪ10ÿɹһʱsize+1,sizeĴСͬʱ󷽷̬ĴС + capacityBigger(size()); + } + for(int j=theSize-1;j>=i;j--){ + theArr[j+1]=theArr[j]; + } + theArr[i]=a; + + theSize++; + } + + //ɾ,±ɾݡ + public void remove(int i){ + + for(int j=i;j0){ + return contains(x,aNode.left); + } + else if(comparaResult<0){//ݴڵǰڵʱӦڵǰڵҺӽڵС + return contains(x,aNode.right); + } + else{ //ݵڵǰڵʱӦڵǰڵС + return true; + } + } + + //ݡ + public void insert(Object x){ + root=insert(x,root); + } + + public BinaryNode insert(Object x,BinaryNode aNode){ + + if(aNode==null){//ǰΪµݽڵ㣬ΪҶӽڵ㣬ҽڵΪnull. + return new BinaryNode(x,null,null); + } + + //͵ǰĽڵбȽϡ + Integer comparaResult=(Integer)aNode.element-(Integer)x; + + //СڵǰڵʱӦڵǰڵӽڵС + if(comparaResult>0){ + aNode.left= insert(x,aNode.left); + } + else if(comparaResult<0){//ݴڵǰڵʱӦڵǰڵҺӽڵС + aNode.right=insert(x,aNode.right); + } + else{ //ݵڵǰڵʱӦڵǰڵ,κβ + ; + } + return aNode; + } + + //ӡ + public void getData(){ + getData(root); + } + public void getData(BinaryNode root){ + if (root != null) { + // + this.getData(root.left); + + //Һ + this.getData(root.right); + //ڵ + this.print(root); + } + + } + + //ӡڵ㡣 + public void print(BinaryNode root){ + System.out.println( + (Integer)(root.element) + ); + } + } diff --git a/group10/3314793852/src/myList/MyIterator.java b/group10/3314793852/src/myList/MyIterator.java new file mode 100644 index 0000000000..8c97809af8 --- /dev/null +++ b/group10/3314793852/src/myList/MyIterator.java @@ -0,0 +1,40 @@ + + package myList; + + public class MyIterator { + + private Object aData; + private int i=0; + private int l=0; + MyLinkedList.Node node; + public MyIterator(Object aDate){ + this.aData=aDate; + } + + public boolean hasNext(){ + if(aData instanceof MyArrayList){//MyArrayListIterator + + Object[] arr=((MyArrayList) aData).getArr(); + int a=((MyArrayList)aData).size(); + return a>i; + } + else{//MyLinkedListIterator + node=((MyLinkedList)aData).getHeadNode();//ͷڵ + int a=((MyLinkedList)aData).size(); + return a>l; + } + + + } + public Object next(){ + if(aData instanceof MyArrayList){//MyArrayListIterator + + Object[] arr=((MyArrayList) aData).getArr(); + return arr[++i]; + } + else{//MyLinkedListIterator + l++; + return node.getDate(); + } + } + } diff --git a/group10/3314793852/src/myList/MyLinkedList.java b/group10/3314793852/src/myList/MyLinkedList.java new file mode 100644 index 0000000000..0bad8c8953 --- /dev/null +++ b/group10/3314793852/src/myList/MyLinkedList.java @@ -0,0 +1,122 @@ + package myList; + + /* + * õͷڵġ + */ + + public class MyLinkedList { + + private int theSize; //ĴС + private Node headNode; //ͷڵ + + //ڵࡣ + public static class Node{ + + private Object data; + private Node node; + + public Node(){ + + } + + public Node(Object data, Node node) { + this.data = data; + this.node = node; + } + public Object getDate(){ + return data; + } + + } + + //췽ʼʱһͷڵĿյ + public MyLinkedList(){ + clear(); + } + + //ͷڵ + public Node getHeadNode(){ + return headNode; + } + + // + public void clear(){ + headNode=new Node(null,null); //ͷʼdateָnodeȫΪnull. + theSize=0; //ĴС + } + + //ȡĴС + public int size(){ + return theSize; + } + + //ӽڵ㵽β + public void add(Object aData){ + add(theSize+1,aData); + } + + //ӽڵ㵽ָλá + public void add(int idx,Object aDate){ + + //һµĽڵ + Node newNode=new Node(); + newNode.data=aDate; + + //ҵָλõĽڵ㣬½ڵ嵽ָλýڵǰһλá + Node p,q; + p=headNode; + + for(int i=1;i(arr.length-1)){//tailѾβʱͷΪʱµ뵽ͷ + tail=0; + } + theSize++; + } + } + + //pop,С + public Object pop(){ + Object a=null; + if(theSize!=0){ //ΪգܽгеIJ + a=arr[head]; + arr[head]=null; + head++; + if(head>(arr.length-1)){ + head=0; + } + theSize--; + } + return a; + } + + //ӡС + public void print(){ + for(int i=0;i + + + + + + + diff --git a/group10/353261578/.gitignore b/group10/353261578/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/353261578/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/353261578/.project b/group10/353261578/.project new file mode 100644 index 0000000000..251ba32e96 --- /dev/null +++ b/group10/353261578/.project @@ -0,0 +1,17 @@ + + + 353261578Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/353261578/src/com/sx/structures/BinaryNode.java b/group10/353261578/src/com/sx/structures/BinaryNode.java new file mode 100644 index 0000000000..a5d2e45bdd --- /dev/null +++ b/group10/353261578/src/com/sx/structures/BinaryNode.java @@ -0,0 +1,44 @@ +package com.sx.structures; + +public class BinaryNode implements Comparable{ + private Object data; + private BinaryNode left; + private BinaryNode right; + + public BinaryNode() { + } + public BinaryNode(Object o){ + data = o; + left = null; + right = null; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryNode getLeft() { + return left; + } + public void setLeft(BinaryNode left) { + this.left = left; + } + public BinaryNode getRight() { + return right; + } + public void setRight(BinaryNode right) { + this.right = right; + } + @Override + public int compareTo(BinaryNode o) { + Integer to = (Integer)this.data; + Integer co = (Integer)o.data; + if(toco) + return 1; + return 0; + } +} diff --git a/group10/353261578/src/com/sx/structures/BinaryTree.java b/group10/353261578/src/com/sx/structures/BinaryTree.java new file mode 100644 index 0000000000..7af8f8edd5 --- /dev/null +++ b/group10/353261578/src/com/sx/structures/BinaryTree.java @@ -0,0 +1,74 @@ +package com.sx.structures; + +public class BinaryTree { + + private BinaryNode root; + + public BinaryTree(Object o) { + root = new BinaryNode(o); + } + + public void insert(Object o) { + BinaryNode node = new BinaryNode(o); + insert(root, node); + } + + private void insert(BinaryNode root, BinaryNode node) { + if (node.compareTo(root) > 0) { + if (root.getRight() == null) { + root.setRight(node); + return; + } + insert(root.getRight(), node); + } else { + if (root.getLeft() == null) { + root.setLeft(node); + return; + } + insert(root.getLeft(), node); + } + + } + + public void preOrder(BinaryNode root){ + order(root); + if(root.getLeft()!=null) + preOrder(root.getLeft()); + if(root.getRight()!=null) + preOrder(root.getRight()); + } + + public void postOrder(BinaryNode root){ + if(root.getLeft()!=null) + postOrder(root.getLeft()); + if(root.getRight()!=null) + postOrder(root.getRight()); + order(root); + } + + public void inOrder(BinaryNode root){ + if(root.getLeft()!=null) + inOrder(root.getLeft()); + order(root); + if(root.getRight()!=null) + inOrder(root.getRight()); + } + + /** + * 未实现 + * @param root + * @param node + * @return + */ + private boolean remove(BinaryNode root, BinaryNode node){ + return false; + } + + public BinaryNode getRoot(){ + return root; + } + + private void order(BinaryNode root){ + System.out.print(root.getData()+" "); + } +} diff --git a/group10/353261578/src/com/sx/structures/Iterator.java b/group10/353261578/src/com/sx/structures/Iterator.java new file mode 100644 index 0000000000..9965e652bc --- /dev/null +++ b/group10/353261578/src/com/sx/structures/Iterator.java @@ -0,0 +1,6 @@ +package com.sx.structures; + +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group10/353261578/src/com/sx/structures/MyArrayList.java b/group10/353261578/src/com/sx/structures/MyArrayList.java new file mode 100644 index 0000000000..952776b5cb --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyArrayList.java @@ -0,0 +1,83 @@ +package com.sx.structures; + +public class MyArrayList implements MyList { + + private int size; + private int ex=10; + private int last=-1; + private Object [] arr; + + public MyArrayList() { + size = 10; + arr = new Object[size]; + } + + @Override + public void add(Object o) { + last++; + if(last==size){ + size += ex; + Object[] temp = new Object[size]; + System.arraycopy(arr, 0, temp, 0, arr.length); + arr = temp; + } + arr[last]=o; + } + + @Override + public void add(int index, Object o) { + add(o); + for(int i=arr.length-1;i>index;i--) + arr[i]=arr[i-1]; + arr[index]=o; + } + + @Override + public Object get(int index) { + return arr[index]; + } + + @Override + public Object remove(int index) { + Object element = arr[index]; + for(int i=index;ilist.size()) + return false; + return true; + } + + @Override + public Object next() { + if(hasNext()) + return list.get(p-1); + return -1; + } + + } + +} diff --git a/group10/353261578/src/com/sx/structures/MyLinkedList.java b/group10/353261578/src/com/sx/structures/MyLinkedList.java new file mode 100644 index 0000000000..3d9932ee92 --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyLinkedList.java @@ -0,0 +1,105 @@ +package com.sx.structures; + +public class MyLinkedList implements MyList{ + private Node head; + private int size = 0; + + public MyLinkedList() { + head = new Node(); + } + @Override + public void add(Object o) { + Node node = createNode(o); + Node pre = head; + while(pre.next!=null){ + pre = pre.next; + } + pre.next = node; + size++; + } + + @Override + public void add(int index, Object o) { + if(index < 0){ + System.out.println("����Խ��");return; + } + Node node = createNode(o); + Node pointer = head; + while(index>0){ + pointer = pointer.next; + index--; + } + node.next = pointer.next; + pointer.next = node; + size++; + } + + @Override + public Object get(int index) { + Node pointer = head; + while(index>=0){ + pointer = pointer.next; + index--; + } + return pointer.data; + } + + @Override + public Object remove(int index) { + Object data = null; + Node pre = head; + while(index>0){ + pre = pre.next; + index--; + } + data = pre.next.data; + pre.next = pre.next.next; + size--; + return data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o){ + add(0, o); + } + public void addLast(Object o){ +// Node node = createNode(o); +// Node p = head; +// while(p.next!=null) +// p = p.next; +// p.next = node; +// size++; + add(o); + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + Object data = null; + Node re = head; + Node pre = head; + while(re.next!=null){ + re = re.next; + pre = re; + } + data = re.data; + re=null; + pre.next = null; + size--; + return data; + } + private Node createNode(Object o){ + Node node = new Node(); + node.data=o; + return node; + } + private static class Node{ + Object data = null; + Node next = null; + } + +} diff --git a/group10/353261578/src/com/sx/structures/MyList.java b/group10/353261578/src/com/sx/structures/MyList.java new file mode 100644 index 0000000000..c880f5c8ff --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyList.java @@ -0,0 +1,13 @@ +package com.sx.structures; + +public interface MyList { + 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/group10/353261578/src/com/sx/structures/MyQueue.java b/group10/353261578/src/com/sx/structures/MyQueue.java new file mode 100644 index 0000000000..9d0cf9018b --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyQueue.java @@ -0,0 +1,25 @@ +package com.sx.structures; + +public class MyQueue { + private MyLinkedList elements ; + public MyQueue() { + elements = new MyLinkedList(); + } + + public void enQueue(Object o){ + elements.add(o); + } + + public Object deQueue(){ + return elements.removeFirst(); + } + + public boolean isEmpty(){ + if(size()>0) + return false; + return true; + } + public int size(){ + return elements.size(); + } +} diff --git a/group10/353261578/src/com/sx/structures/MyStack.java b/group10/353261578/src/com/sx/structures/MyStack.java new file mode 100644 index 0000000000..b81930604d --- /dev/null +++ b/group10/353261578/src/com/sx/structures/MyStack.java @@ -0,0 +1,37 @@ +package com.sx.structures; + +public class MyStack { + private int pointer; + private MyArrayList element; + public MyStack() { + element = new MyArrayList(); + pointer = -1; + } + + public void push(Object o){ + pointer++; + element.add(pointer); + } + public Object pop(){ + if(pointer<0) + return -1; + Object p = element.get(pointer); + pointer--; + return p; + } + /** + *只返回栈顶元素 + * @return + */ + public Object peek(){ + if(pointer<0) + return -1; + return element.get(pointer); + } + public boolean isEmpty(){ + if(pointer<0) + return true; + return false; + } + +} diff --git a/group10/353261578/test/com/test/BinaryTreeTest.java b/group10/353261578/test/com/test/BinaryTreeTest.java new file mode 100644 index 0000000000..714c71798c --- /dev/null +++ b/group10/353261578/test/com/test/BinaryTreeTest.java @@ -0,0 +1,31 @@ +package com.test; + + +import org.junit.Test; + +import com.sx.structures.BinaryTree; + +public class BinaryTreeTest { + + private BinaryTree bt; + + @Test + public void test() { + bt = new BinaryTree(55); + bt.insert(23); + bt.insert(44); + bt.insert(16); + bt.insert(78); + bt.insert(99); + //先序 + System.out.println("先序遍历:"); + bt.preOrder(bt.getRoot()); + //中序遍历 + System.out.println("\n中序遍历:"); + bt.inOrder(bt.getRoot()); + //后序 + System.out.println("\n后序遍历:"); + bt.postOrder(bt.getRoot()); + } + +} diff --git a/group10/353261578/test/com/test/MyArrayListTest.java b/group10/353261578/test/com/test/MyArrayListTest.java new file mode 100644 index 0000000000..9c8369f91b --- /dev/null +++ b/group10/353261578/test/com/test/MyArrayListTest.java @@ -0,0 +1,64 @@ +package com.test; + + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.sx.structures.MyArrayList; +import com.sx.structures.MyList; + +public class MyArrayListTest { + + private MyArrayList list ; + + @Test + public void testAddObject() { + for(int j=0;j<12;j++){ + list.add(j); + } + } + + @Test + public void testAddIntObject() { + list.add(5, 12); + list.add(10, 11); + } + + @Test + public void testGet() { + System.out.println(list.get(5)); + } + + @Test + public void testRemove() { + System.out.println("\nremoved 5:"+list.remove(5)+"."); + } + + @Test + public void testSize() { + System.out.println("\nlist.size:"+list.size()); + } + + @After + public void Print(){ + System.out.println("最终结果:List:"); + PrintList(list); + } + @Before + public void createlist(){ + list = new MyArrayList(); + for(int j=0;j<12;j++){ + list.add(j); + } + System.out.println("初始list:"); + PrintList(list); + } + + public static void PrintList(MyList list){ + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i)+" "); + } + } + +} diff --git a/group10/353261578/test/com/test/MyLinkedListTest.java b/group10/353261578/test/com/test/MyLinkedListTest.java new file mode 100644 index 0000000000..62dcbbc8d1 --- /dev/null +++ b/group10/353261578/test/com/test/MyLinkedListTest.java @@ -0,0 +1,83 @@ +package com.test; + + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.sx.structures.MyLinkedList; +import com.sx.structures.MyList; + +public class MyLinkedListTest { + + private MyLinkedList list; + + @Test + public void testAddObject() { + list.add(3); + } + + @Test + public void testAddIntObject() { + list.add(0,"t-0"); + list.add(1, "t-1"); + } + + @Test + public void testGet() { + System.out.println(list.get(1)); + } + + @Test + public void testRemove() { + list.remove(0); + } + + @Test + public void testSize() { + System.out.println(); + System.out.println(" list-size="+list.size()); + } + + @Test + public void testAddFirst() { + list.addFirst("t-1"); + } + + @Test + public void testAddLast() { + list.addLast("T-last"); + } + + @Test + public void testRemoveFirst() { + list.removeFirst(); + } + + @Test + public void testRemoveLast() { + list.removeLast(); + } + + @After + public void Print(){ + System.out.println("\n操作之后,List:"); + PrintList(list); + } + @Before + public void createlist(){ + list = new MyLinkedList(); + for(int j=0;j<11;j++){ + list.add(j); + } + System.out.println("初始list:"); + PrintList(list); + } + + public static void PrintList(MyList list){ + for (int i = 0; i < list.size(); i++) { + System.out.print(list.get(i)+" "); + } + } + +} diff --git a/group10/353261578/test/com/test/StaQueTest.java b/group10/353261578/test/com/test/StaQueTest.java new file mode 100644 index 0000000000..fef9ba8ff7 --- /dev/null +++ b/group10/353261578/test/com/test/StaQueTest.java @@ -0,0 +1,44 @@ +package com.test; + + +import org.junit.Test; + +import com.sx.structures.MyQueue; +import com.sx.structures.MyStack; + +public class StaQueTest { + private MyStack s; + private MyQueue queue; + + @Test + public void Stacktest() { + + s = new MyStack(); + + for(int i=0;i<10;i++){ + s.push(i); + } + System.out.println("\npop:"); + while(s.isEmpty()==false){ + System.out.println("-"+s.isEmpty()+":"+s.pop()); + } + + System.out.println("\n"+"-"+s.isEmpty()+":"+s.pop()); + + System.out.println("\npeek"); + for(int i=1;i<3;i++){ + System.out.print(s.peek()+" "); + } + } + + @Test + public void queueTest(){ + queue = new MyQueue(); + for(int i=0;i<10;i++) + queue.enQueue(i); + while(queue.size()>0) + System.out.print(queue.deQueue()+" "); + } + + +} diff --git a/group10/364298692/article.md b/group10/364298692/article.md new file mode 100644 index 0000000000..01af1f5151 --- /dev/null +++ b/group10/364298692/article.md @@ -0,0 +1,3 @@ +# ÿ +## week01 + diff --git a/group10/364298692/cs/.classpath b/group10/364298692/cs/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group10/364298692/cs/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group10/364298692/cs/.gitignore b/group10/364298692/cs/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/364298692/cs/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/364298692/cs/.project b/group10/364298692/cs/.project new file mode 100644 index 0000000000..8536996016 --- /dev/null +++ b/group10/364298692/cs/.project @@ -0,0 +1,17 @@ + + + cs + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/364298692/cs/.settings/org.eclipse.jdt.core.prefs b/group10/364298692/cs/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group10/364298692/cs/.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/group10/364298692/cs/src/com/coding/basic/ArrayList.java b/group10/364298692/cs/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..71761666aa --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/ArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData; + + public ArrayList(int initialCapacity){ + elementData = new Object[initialCapacity]; + } + + public ArrayList(){ + elementData = new Object[10]; + } + + public void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + Object[] oldData = elementData; + int newCapacity = (oldCapacity * 3) / 2 + 1; + if(minCapacity > newCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public void add(Object o){ + ensureCapacity(size + 1); + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + ensureCapacity(size + 1); + for(int i = size-1; i >= index; i--){ + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index > size-1){ + return null; + }else{ + return elementData[index]; + } + } + + public Object remove(int index){ + if(index > size-1){ + return null; + }else{ + Object obj = elementData[index]; + for(int i=index; i o.hashCode()){ + this.right = node; + }else{ + this.left = node; + } + return this; + } + + + +} diff --git a/group10/364298692/cs/src/com/coding/basic/Iterator.java b/group10/364298692/cs/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group10/364298692/cs/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/group10/364298692/cs/src/com/coding/basic/LinkedList.java b/group10/364298692/cs/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..976eb88fa3 --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/LinkedList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + + private int size; + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + Node node = head; + for(int i = 0; i < index; i++){ + node = node.next; + } + Node newNode = new Node(); + newNode.data = o; + newNode.next = node.next; + node.next = newNode; + size++; + } + public Object get(int index){ + if(index > size-1){ + return null; + }else{ + Node node = head; + for(int i = 0; i < index; i++){ + node = node.next; + } + return node.data; + } + } + public Object remove(int index){ + if(index > size-1){ + return null; + }else if(index == 0){ + Object obj = head.data; + head = head.next; + size--; + return obj; + }else{ + Node node = head; + //ñɾڵǰһڵ + for(int i = 0; i < index-1; i++){ + node = node.next; + } + Object obj = node.next.data; + node.next = node.next.next; + size--; + return obj; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newHead = new Node(); + newHead.data = o; + newHead.next = head; + head = newHead; + size++; + } + public void addLast(Object o){ + Node node = head; + while(node.next != null){ + node = node.next; + } + Node newNode = new Node(); + newNode.data = o; + node.next = newNode; + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size); + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group10/364298692/cs/src/com/coding/basic/List.java b/group10/364298692/cs/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group10/364298692/cs/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/group10/364298692/cs/src/com/coding/basic/Queue.java b/group10/364298692/cs/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..1a96515399 --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList linkedList; + + public void enQueue(Object o){ + linkedList.addLast(o); + } + + public Object deQueue(){ + Object obj = linkedList.removeFirst(); + return obj; + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group10/364298692/cs/src/com/coding/basic/Stack.java b/group10/364298692/cs/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7043ba9386 --- /dev/null +++ b/group10/364298692/cs/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(elementData.size(), o); + } + + public Object pop(){ + Object obj = elementData.remove(elementData.size()-1); + return obj; + } + + public Object peek(){ + Object obj = elementData.get(0); + return obj; + } + public boolean isEmpty(){ + return elementData.size()==0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group10/569420966/struct/pom.xml b/group10/569420966/struct/pom.xml new file mode 100644 index 0000000000..bc0c11f1c3 --- /dev/null +++ b/group10/569420966/struct/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.rishy + struct + 1.0-SNAPSHOT + + + junit + junit + 4.12 + test + + + + + \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/litestruts/LoginAction.java b/group10/569420966/struct/src/main/java/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..b41e849acc --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/litestruts/Struts.java b/group10/569420966/struct/src/main/java/com/litestruts/Struts.java new file mode 100644 index 0000000000..875b35d223 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/litestruts/Struts.java @@ -0,0 +1,147 @@ +package com.litestruts; + +import com.myutil.JavaBeanUtil; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.rmi.server.ExportException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class Struts { + + private static Map> xmlInfo = new HashMap<>(); + + public static View runAction(String actionName, Map parameters) { + + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + // 【0】 + getInfo(); + + + Map action = xmlInfo.get(actionName); + Class classForName; + View view = new View(); + try { + // 【1】 + Object aObj = Class.forName(action.get("class")).newInstance(); + classForName = aObj.getClass(); + Method[] methods = classForName.getMethods(); + for (Method method : methods) { + for (String key : parameters.keySet()) { + if (JavaBeanUtil.getSetMethodName(key).equals(method.getName())) { + Method declaredMethod = classForName.getDeclaredMethod(method.getName(), method.getParameterTypes()); + declaredMethod.invoke(aObj, parameters.get(key)); + } + } + } + // 【2】 + Map resultMap = new HashMap<>(); + Method execute = classForName.getDeclaredMethod("execute"); + String invoke = (String) execute.invoke(aObj); + // 【3】 + Field[] fields = classForName.getDeclaredFields(); + for (Field field : fields) { + for (Method method : methods) { + if (JavaBeanUtil.getGetMethodName(field.getName()).equals(method.getName())) { + Method declaredMethod = classForName.getDeclaredMethod(method.getName()); + String invoke1 = (String)declaredMethod.invoke(aObj); + resultMap.put(field.getName(), invoke1); + } + } + } + view.setParameters(resultMap); + // 【4】 + view.setJsp(action.get(invoke)); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return view; + } + + private static void getInfo() { + try { + File file = new File("D:\\Own\\Code\\Java\\coding2017\\group10\\569420966\\struct\\src\\main\\resources\\struts.xml"); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(file); + NodeList nl = doc.getElementsByTagName("struts"); + + if (nl.getLength() > 0) { + NodeList actions = doc.getElementsByTagName("action"); + if (actions.getLength() > 0) { + Map actionInfo; + for (int i = 0; i < actions.getLength(); i++) { + actionInfo = new HashMap<>(); + NamedNodeMap attributes = actions.item(i).getAttributes(); + for (int j = 0; j < attributes.getLength(); j++) { + attributes.item(j).getNodeName(); + actionInfo.put(attributes.item(j).getNodeName(), attributes.item(j).getNodeValue()); + } + NodeList childNodes = actions.item(i).getChildNodes(); + for (int j = 0; j < childNodes.getLength(); j++) { + if (childNodes.item(j).getNodeType() == 1) { // 元素节点 + NodeList childNodes1 = childNodes.item(j).getChildNodes(); + NamedNodeMap attributes1 = childNodes.item(j).getAttributes(); + NodeList childNodes2 = childNodes.item(j).getChildNodes(); + actionInfo.put(attributes1.item(0).getNodeValue(), childNodes2.item(0).getNodeValue()); + } + } + if (actionInfo.get("name") != null) { + xmlInfo.put(actionInfo.get("name"), actionInfo); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + getInfo(); + } + +} \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/litestruts/View.java b/group10/569420966/struct/src/main/java/com/litestruts/View.java new file mode 100644 index 0000000000..5567568114 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/litestruts/View.java @@ -0,0 +1,23 @@ +package com.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/myutil/ArrayList.java b/group10/569420966/struct/src/main/java/com/myutil/ArrayList.java new file mode 100644 index 0000000000..19f0fdc26f --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/ArrayList.java @@ -0,0 +1,162 @@ +package com.myutil; + + +import java.text.MessageFormat; +import java.util.NoSuchElementException; + +/** + * 数组列表 + */ +public class ArrayList implements List { + private Object[] elementData; + private int size = 0; + private static final int DEFAULT_SIZE = 10; + + /** + * 判断边界 + *

+ *

+     *      若 index < 0 或者 index > size 则抛出非法参数异常
+     * 
+ * + * @param index 当前索引 + */ + private void judgeRange(int index) { + if (index < 0) { + throw new IllegalArgumentException( + MessageFormat.format("Index is must be great or equal then 0. index:{0}", index)); + } + if (index >= this.size) { + throw new IllegalArgumentException( + MessageFormat.format("Index is must be less then size(). index:{0}", index)); + } + if (this.size == Integer.MAX_VALUE) { + throw new IllegalArgumentException("Array already can not Expansion."); + } + } + + /** + * 扩充数组容量 + *

+ *

+     *     若 size >= elementData.length 则对数组进行扩容
+     *     扩容至原(elementData.length+1) * 2
+     * 
+ */ + private void capacityExpansion() { + if (this.size >= elementData.length) { + Object[] tmpData = new Object[(elementData.length + 1) * 2]; + System.arraycopy(elementData, 0, tmpData, 0, elementData.length); + elementData = tmpData; + } + } + + public ArrayList() { + elementData = new Object[DEFAULT_SIZE]; + } + + public ArrayList(int capacity) { + if (capacity < 0) { + throw new IllegalArgumentException( + MessageFormat.format("Capacity is must be great or equal 0. capacity:{0}", capacity)); + } + this.elementData = new Object[capacity]; + } + + public void add(T element) { + capacityExpansion(); + elementData[this.size] = element; + this.size++; + } + + public void add(T element, int index) { + judgeRange(index); + capacityExpansion(); + if (this.size - index > 0) { + System.arraycopy(elementData, index, elementData, index + 1, this.size - index); + } + elementData[index] = element; + this.size++; + } + + public T remove(int index) { + judgeRange(index); + T tmpObject = (T) elementData[index]; + if (this.size - index > 0) { + System.arraycopy(elementData, index + 1, elementData, index, this.size - index - 1); + } + this.size--; + return tmpObject; + } + + public T get(int index) { + judgeRange(index); + return (T) elementData[index]; + } + + public int size() { + return this.size; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < this.size; i++) { + sb.append((T) elementData[i]); + if (i < this.size - 1) { + sb.append(","); + } + } + sb.append("]"); + + return sb.toString(); + } + + /** + * 获取迭代器 + * + * @return 迭代器 + */ + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + int position = 0; + int lastRet = -1; + + public boolean hasNext() { + return position < ArrayList.this.size(); + } + + public T next() { + if (position >= size) { + throw new NoSuchElementException(); + } + int i = position; + T element = ArrayList.this.get(position++); + lastRet = i; + return element; + } + + public T remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + T removeElement = ArrayList.this.remove(lastRet); + position = lastRet; + lastRet = -1; + return removeElement; + } + } + + public static void main(String[] args) { + ArrayList ids = new ArrayList<>(); + for (int i = 0; i < 11; i++) { + ids.add(i); + } + Iterator iterator = ids.iterator(); + System.out.println(ids); + } +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/ArrayUtil.java b/group10/569420966/struct/src/main/java/com/myutil/ArrayUtil.java new file mode 100644 index 0000000000..bf7e794488 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/ArrayUtil.java @@ -0,0 +1,257 @@ +package com.myutil; + +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 源数组 + */ + public static void reverseArray(int[] origin) { + if (origin == null) { + return; + } + int originLen = origin.length; + int[] copyArray = new int[originLen]; + System.arraycopy(origin, 0, copyArray, 0, originLen); + for (int i = 0; i < originLen; i++) { + origin[i] = copyArray[originLen - 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 static int[] removeZero(int[] oldArray) { + if (oldArray == null) { + return null; + } + int size = 0; + for (int i : oldArray) { + size += i == 0 ? 0 : 1; + } + int[] newArray = new int[size]; + int newIndex = 0; + for (int old : oldArray) { + if (old != 0) { + newArray[newIndex++] = old; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 第一个数组 + * @param array2 第二个数组 + * @return 合并后的数组 + */ + + public static int[] merge(int[] array1, int[] array2) { + if (array1 == null || array2 == null) { + return null; + } + int len1 = array1.length; + int len2 = array2.length; + int[] newArray = new int[len1 + len2]; + int size = 0; + int i = 0; + int j = 0; + int k = 0; + while (i < len1 && j < len2) { + if (array1[i] < array2[j]) { + if (k == 0 || newArray[k] != array2[i]) { + newArray[k++] = array1[i++]; + size++; + } else { + i++; + } + } else if (array1[i] > array2[j]) { + if (k == 0 || newArray[k] != array2[j]) { + newArray[k++] = array2[j++]; + size++; + } else { + j++; + } + } else { + if (k == 0 || newArray[k] != array2[i]) { + newArray[k++] = array2[j]; + i++; + j++; + size++; + } else { + i++; + j++; + } + } + } + while (i < len1) { + if (k == 0 || newArray[k] != array1[i]) { + newArray[k++] = array1[i++]; + size++; + } else { + i++; + } + } + while (j < len2) { + if (k == 0 || newArray[k] != array2[j]) { + newArray[k++] = array2[j++]; + size++; + } else { + j++; + } + } + int[] resizeArray = new int[size]; + System.arraycopy(newArray, 0, resizeArray, 0, size); + return resizeArray; + } + + /** + * 把一个已经存满数据的数组 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 (oldArray == null || size <= 0) { + return oldArray; + } + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max 最大值 + * @return 小于最大值的fibonacci数列的数组 + */ + public static int[] fibonacci(int max) { + ArrayList fibonacciList = new ArrayList<>(); + int a1 = 1; + int a2 = 1; + int t; + if (a1 < max) { + fibonacciList.add(a1); + } + while (a2 < max) { + fibonacciList.add(a2); + t = a2; + a2 = a1 + a2; + a1 = t; + } + int[] fibonacciArray = new int[fibonacciList.size()]; + for (int i = 0; i < fibonacciList.size(); i++) { + fibonacciArray[i] = fibonacciList.get(i); + } + return fibonacciArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max 最大值 + * @return 小于最大值的所有素数 + */ + public static int[] getPrimes(int max) { + ArrayList primeList = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + primeList.add(i); + } + } + int[] primeArray = new int[primeList.size()]; + for (int i = 0; i < primeList.size(); i++) { + primeArray[i] = primeList.get(i); + } + return primeArray; + } + + private static boolean isPrime(int num) { + for (int i = 2; i <= Math.sqrt(num); i++) { + if (num % i == 0) { + return false; + } + } + + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max 最大值 + * @return 小于最大值的所有完数 + */ + public static int[] getPerfectNumbers(int max) { + ArrayList perfectList = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPrefectNumber(i)) { + perfectList.add(i); + } + } + int[] perfectArray = new int[perfectList.size()]; + for (int i = 0; i < perfectList.size(); i++) { + perfectArray[i] = perfectList.get(i); + } + return perfectArray; + } + + private static boolean isPrefectNumber(int num) { + int sum = 0; + for (int i = 1; i < num; i++) { + if (num % i == 0) { + sum += i; + } + } + return num == sum; + } + + /** + * 用separator 把数组 array给连接起来 + * 例如array= [3,8,9], separator = "-" + * 则返回值为"3-8-9" + * + * @param array 数组 + * @param separator 分隔符 + * @return 用分隔符连接的数字字符串 + */ + public static String join(int[] array, String separator) { + if (array == null || separator == null) { + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i).append(separator); + } + return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : ""; + } + + public static void main(String[] args) { + System.out.println(Arrays.toString(getPerfectNumbers(1000))); + } +} \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/myutil/BinaryTreeNode.java b/group10/569420966/struct/src/main/java/com/myutil/BinaryTreeNode.java new file mode 100644 index 0000000000..0e862a199d --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/BinaryTreeNode.java @@ -0,0 +1,167 @@ +package com.myutil; + +import java.util.Random; + +/** + * 二叉树 + */ +public class BinaryTreeNode> { + private T element; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public T getElement() { + return element; + } + + public void setElement(T element) { + this.element = element; + } + + 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; + } + + /** + * 将元素插入二叉树 + * + * @param element 元素 + * @return 插入后的节点 + */ + public BinaryTreeNode insert(T element) { + if (element == null) { + throw new IllegalArgumentException("Element must be not null."); + } + + BinaryTreeNode currentNode = null; + if (this.element == null) { + currentNode = this; + currentNode.element = element; + } else { + currentNode = compareToElement(element, this); + } + + return currentNode; + } + + private BinaryTreeNode compareToElement(T element, BinaryTreeNode curr) { + if (element.compareTo(curr.element) == -1) { + if (curr.left == null) { + BinaryTreeNode node = new BinaryTreeNode<>(); + node.element = element; + curr.left = node; + return node; + } else { + return compareToElement(element, curr.left); + } + } else { + if (curr.right == null) { + BinaryTreeNode node = new BinaryTreeNode<>(); + node.element = element; + curr.right = node; + return node; + } else { + return compareToElement(element, curr.right); + } + } + } + + /** + * 先序遍历 + * + * @return 按先序遍历顺序展示节点值 + */ + public String preOrderTraversal() { + return concatPreOrder(this); + } + + private String concatPreOrder(BinaryTreeNode node) { + StringBuilder ret = new StringBuilder(); + if (node.left != null) { + ret.append(concatPreOrder(node.left)); + } + + ret.append(node.element).append(" "); + + if (node.right != null) { + ret.append(concatPreOrder(node.right)); + } + + return ret.toString(); + } + + /** + * 中序遍历 + * + * @return 按中序遍历顺序展示节点值 + */ + public String inOrderTraversal() { + return concatInOrder(this); + } + + private String concatInOrder(BinaryTreeNode node) { + StringBuilder ret = new StringBuilder(); + + ret.append(node.element).append(" "); + + if (node.left != null) { + ret.append(concatInOrder(node.left)); + } + + if (node.right != null) { + ret.append(concatInOrder(node.right)); + } + + return ret.toString(); + } + + /** + * 后序遍历 + * + * @return 按后序遍历顺序展示节点值 + */ + public String postOrderTraversal() { + return concatPostOrder(this); + } + + private String concatPostOrder(BinaryTreeNode node) { + StringBuilder ret = new StringBuilder(); + + if (node.right != null) { + ret.append(concatPostOrder(node.right)); + } + + ret.append(node.element).append(" "); + + if (node.left != null) { + ret.append(concatPostOrder(node.left)); + } + + return ret.toString(); + } + + public static void main(String[] args) { + BinaryTreeNode binaryTree = new BinaryTreeNode<>(); + Random random = new Random(); + for (int i = 0; i < 5; i++) { + binaryTree.insert(random.nextInt(100)); + } + + + System.out.println(binaryTree.preOrderTraversal()); + System.out.println(binaryTree.inOrderTraversal()); + System.out.println(binaryTree.postOrderTraversal()); + } +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/Iterator.java b/group10/569420966/struct/src/main/java/com/myutil/Iterator.java new file mode 100644 index 0000000000..412a1ef0aa --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/Iterator.java @@ -0,0 +1,28 @@ +package com.myutil; + +/** + * 迭代器 + */ +public interface Iterator { + + /** + * 是否有下一个元素 + * + * @return true-有 false-无 + */ + boolean hasNext(); + + /** + * 获取下一个元素 + * + * @return 下一个元素 + */ + T next(); + + /** + * 删除当前迭代的元素 + * + * @return 被删除的元素 + */ + T remove(); +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/JavaBeanUtil.java b/group10/569420966/struct/src/main/java/com/myutil/JavaBeanUtil.java new file mode 100644 index 0000000000..614ca57bfb --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/JavaBeanUtil.java @@ -0,0 +1,35 @@ +package com.myutil; + +import java.util.regex.Pattern; + +/** + * javaBean工具 + */ +public class JavaBeanUtil { + public static String getGetMethodName(String attributeName) { + if ((Character.isLowerCase(attributeName.charAt(0)) && Character.isUpperCase(attributeName.charAt(1))) + || (Character.isUpperCase(attributeName.charAt(0)))) { + return "get" + attributeName; + } else if (attributeName.indexOf("is") == 0 && Character.isUpperCase(attributeName.charAt(1))) { + return attributeName; + } else { + char[] chars = attributeName.toCharArray(); + chars[0] -= 32; + return "get" + String.valueOf(chars); + } + } + + public static String getSetMethodName(String attributeName) { + if ((Character.isLowerCase(attributeName.charAt(0)) && Character.isUpperCase(attributeName.charAt(1))) + || (Character.isUpperCase(attributeName.charAt(0)))) { + return "set" + attributeName; + } else if (attributeName.indexOf("is") == 0 && Character.isUpperCase(attributeName.charAt(2))) { + return "set" + attributeName.replace("is", ""); + } else { + char[] chars = attributeName.toCharArray(); + chars[0] -= 32; + return "set" + String.valueOf(chars); + } + } + +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/LinkedList.java b/group10/569420966/struct/src/main/java/com/myutil/LinkedList.java new file mode 100644 index 0000000000..dcbce429ec --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/LinkedList.java @@ -0,0 +1,207 @@ +package com.myutil; + +import java.text.MessageFormat; +import java.util.NoSuchElementException; + +/** + * 链表列表 + */ +public class LinkedList implements List { + private Node header = new Node(); + private int size = 0; + + private Node lastNode() { + return findNode(this.size); + } + + private Node findNode(int index) { + int current = 0; + Node targetNode = header; + while (current < index) { + targetNode = targetNode.next; + current++; + } + return targetNode; + } + + /** + * 判断边界 + *

+ *

+     *      若 index < 0 或者 index > size 则抛出非法参数异常
+     * 
+ * + * @param index 当前索引 + */ + private void judgeRange(int index) { + if (index < 0) { + throw new IllegalArgumentException( + MessageFormat.format("Index is must be great or equal then 0. index:{0}", index)); + } + if (index >= this.size) { + throw new IllegalArgumentException( + MessageFormat.format("Index is must be less then size(). index:{0}", index)); + } + if (this.size == Integer.MAX_VALUE) { + throw new IllegalArgumentException("Array already can not Expansion."); + } + } + + public LinkedList() { + + } + + @Override + public void add(T element) { + Node lastNode = lastNode(); + Node addNode = new Node(); + addNode.element = element; + lastNode.next = addNode; + this.size++; + } + + @Override + public void add(T element, int index) { + judgeRange(index); + Node targetNode = findNode(index); + Node addNode = new Node(); + addNode.element = element; + addNode.next = targetNode.next; + targetNode.next = addNode; + this.size++; + } + + @Override + public T remove(int index) { + judgeRange(index); + Node targetNode = findNode(index); + Node removeNode = targetNode.next; + targetNode.next = targetNode.next.next; + T element = (T) removeNode.element; + this.size--; + return element; + } + + @Override + public T get(int index) { + judgeRange(index); + return (T) findNode(index).next.element; + } + + @Override + public int size() { + return this.size; + } + + /** + * 添加一个元素到最开始的位置 + * + * @param element 元素 + */ + public void addFirst(T element) { + add(element, 0); + } + + /** + * 添加一个元素到最后 + * + * @param element 元素 + */ + public void addLast(T element) { + add(element, this.size - 1); + } + + /** + * 删除第一个元素 + * + * @return 第一个元素 + */ + public T removeFirst() { + if (this.size == 0) { + throw new ArrayIndexOutOfBoundsException("This list is empty, don't to remove."); + } + + return remove(0); + } + + /** + * 删除最后一个元素 + * + * @return 最后一个元素 + */ + public T removeLast() { + if (this.size == 0) { + throw new ArrayIndexOutOfBoundsException("This list is empty, don't to remove."); + } + return remove(this.size - 1); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + Node indexNode = header.next; + while (indexNode != null) { + sb.append((T) indexNode.element); + if (indexNode.next != null) { + sb.append(","); + } + indexNode = indexNode.next; + } + sb.append("]"); + + return sb.toString(); + } + + /** + * 获取迭代器 + * + * @return 迭代器 + */ + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int position = 0; + int lastRet = -1; + + public boolean hasNext() { + return position < LinkedList.this.size(); + } + + public T next() { + if (position >= size) { + throw new NoSuchElementException(); + } + int i = position; + T element = LinkedList.this.get(position++); + lastRet = i; + return element; + } + + public T remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + T removeElement = LinkedList.this.remove(lastRet); + position = lastRet; + lastRet = -1; + return removeElement; + } + } + + private static class Node { + T element; + Node next; + } + + public static void main(String[] args) { + LinkedList ids = new LinkedList<>(); + for (int i = 0; i < 11; i++) { + ids.add(i); + } + Iterator iterator = ids.iterator(); + System.out.println(ids); + } +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/List.java b/group10/569420966/struct/src/main/java/com/myutil/List.java new file mode 100644 index 0000000000..8dced86dc7 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/List.java @@ -0,0 +1,52 @@ +package com.myutil; + +/** + * 列表基本操作 + */ +public interface List { + /** + * 添加一个元素到列表 + * + * @param element 元素 + */ + void add(T element); + + /** + * 添加一个元素至指定位置 + * + *
+     *     指定位置范围: index >= 0 && index < size
+     *     否则回抛出非法参数异常
+     * 
+ * + * @param element 元素 + * @param index 指定位置 + */ + void add(T element, int index); + + /** + * 删除指定位置元素 + * + *
+     *     指定位置范围: index >= 0 && index < size
+     *     否则回抛出非法参数异常
+     * 
+ * + * @param index 指定位置 + * @return 删除的元素的引用 + */ + T remove(int index); + + /** + * 获取指定位置元素 + * @param index 指定位置 + * @return 指定位置的元素 + */ + T get(int index); + + /** + * 获取当前列表的大小 + * @return 当前列表的大小 + */ + int size(); +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/Queue.java b/group10/569420966/struct/src/main/java/com/myutil/Queue.java new file mode 100644 index 0000000000..9097caa7f4 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/Queue.java @@ -0,0 +1,44 @@ +package com.myutil; + +/** + * 队列 + */ +public class Queue { + private LinkedList elementList = new LinkedList<>(); + + /** + * 进入队列 + * + * @param element 进入队列的元素 + */ + public void enQueue(T element) { + elementList.add(element); + } + + /** + * 出队列 + * + * @return 出队列的元素 + */ + public T deQueue() { + return elementList.removeFirst(); + } + + /** + * 队列是否为空 + * + * @return true-是 false-否 + */ + public boolean isEmpty() { + return elementList.size() == 0; + } + + /** + * 获取队列的大小 + * + * @return 队列的大小 + */ + public int size() { + return elementList.size(); + } +} diff --git a/group10/569420966/struct/src/main/java/com/myutil/Stack.java b/group10/569420966/struct/src/main/java/com/myutil/Stack.java new file mode 100644 index 0000000000..d78ae2b39a --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/Stack.java @@ -0,0 +1,61 @@ +package com.myutil; + +/** + * 栈 + */ +public class Stack { + private ArrayList elementList = new ArrayList<>(); + + /** + * 入栈 + * + * @param element 入栈的元素 + */ + public void push(T element) { + elementList.add(element); + } + + /** + * 出栈 + * + * @return 出栈的元素 + */ + public T pop() { + if (elementList.size() == 0) { + throw new ArrayIndexOutOfBoundsException("Stack is empty, don't to pop()."); + } + T element = elementList.get(elementList.size() - 1); + elementList.remove(elementList.size() - 1); + return element; + } + + /** + * 获取栈顶元素 + * + * @return 栈顶元素 + */ + public T peek() { + if (elementList.size() == 0) { + throw new ArrayIndexOutOfBoundsException("Stack is empty, don't to peek()."); + } + return elementList.get(elementList.size() - 1); + } + + /** + * 是否为空栈 + * + * @return true-是 false-否 + */ + public boolean isEmpty() { + return elementList.size() == 0; + } + + /** + * 获取当前栈大小 + * + * @return 当前栈大小 + */ + public int size() { + return elementList.size(); + } +} diff --git a/group10/569420966/struct/src/main/resources/struts.xml b/group10/569420966/struct/src/main/resources/struts.xml new file mode 100644 index 0000000000..cdf0277794 --- /dev/null +++ b/group10/569420966/struct/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group10/569420966/struct/src/test/java/com/litestruts/test/StrutsTest.java b/group10/569420966/struct/src/test/java/com/litestruts/test/StrutsTest.java new file mode 100644 index 0000000000..4a03024cfc --- /dev/null +++ b/group10/569420966/struct/src/test/java/com/litestruts/test/StrutsTest.java @@ -0,0 +1,45 @@ +package com.litestruts.test; + +import java.util.HashMap; +import java.util.Map; + +import com.litestruts.Struts; +import com.litestruts.View; +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} \ No newline at end of file diff --git a/group10/584709796/worka/.classpath b/group10/584709796/worka/.classpath new file mode 100644 index 0000000000..ece376cba2 --- /dev/null +++ b/group10/584709796/worka/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group10/584709796/worka/.gitignore b/group10/584709796/worka/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group10/584709796/worka/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/584709796/worka/.project b/group10/584709796/worka/.project new file mode 100644 index 0000000000..2858b5b710 --- /dev/null +++ b/group10/584709796/worka/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/584709796/worka/.settings/org.eclipse.jdt.core.prefs b/group10/584709796/worka/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group10/584709796/worka/.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/group10/584709796/worka/src/com/coding/basic/ArrayList.java b/group10/584709796/worka/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7d06acbe10 --- /dev/null +++ b/group10/584709796/worka/src/com/coding/basic/ArrayList.java @@ -0,0 +1,75 @@ +//刚开始学JAVA,现在学到JAVA基础类库耐力,迭代器还没学 + +package com.coding.basic; + +public class ArrayList implements List { + + private int size =0; + + private Object[] elementData = new Object[100]; + + public int getSize() {//得到数组大小 + return size; + } + public void setSize(int size) {//设置数组的长度 + this.size = size; + } + + public void ExtendArray(int size){ //插入元素时,数组长度加1,确保数组不越界 + this.size=size+1; + } + + public void add(Object o){//在末尾添加元素 + int length=getSize(); + elementData[length] = o; + ExtendArray(length); + } + + public void add(int index, Object o){ + int length=getSize(); + + for(int k=length-1;k>=index-1;k--){//元素后移 + elementData[k+1]=elementData[k]; + } + elementData[index-1]=o; + ExtendArray(length);//插一个元素,扩充一次 + } + + public Object get(int index){//获取元素 + int length=getSize(); + if(index+1>length||index<0){ + System.out.println("方法 get(int index)的index不在数组索引的范围内"); + } + return (Object)elementData[index]; + } + + public Object remove(int index){//移除元素 + int length=getSize(); + if(index+1>length||index<0){ + System.out.println("方法 remove(int index)的index不在数组索引的范围内"); + } + Object ss=(Object)elementData[index]; + + for(int k=index;k 0){ + newArry[i++] = origin[--j]; + } + return newArry; + } + + /** + * 现在有如下的一个数组: 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[] tempArray = new int[oldArray.length]; + int j = 0; + for(int i = 0; i < oldArray.length;i++){ + if(oldArray[i] > 0){ + tempArray[j++] = oldArray[i]; + } + } + int[] newArray = new int[j]; + System.arraycopy(tempArray, 0, newArray, 0, j); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + int len = array1.length; + int[] newArray = new int[array1.length+array2.length]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + for(int i = 0; i < array2.length; i++){ + boolean flag = true; + for(int j = 0; j < array1.length; j++){ + if(array2[i] == array1[j]){ + flag = false; + } + } + if(flag){ + newArray[len++] = array2[i]; + } + } + int[] aa = new int[len]; + System.arraycopy(newArray, 0, aa, 0, len); + for(int i = 0; i < aa.length; i++){ + for(int j = i+1; j < aa.length; j++){ + if(aa[i] > aa[j]){ + int temp = aa[i]; + aa[i] = aa[j]; + aa[j] = temp; + } + } + } + return aa; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] aa = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, aa, 0, oldArray.length); + return aa; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * Fn = F(n-1)+F(n-2)(n >= 2) + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max <2){ + return new int[0]; + } + int[] temp = new int[8]; + int count = 0; + for(int i = 1;i < max;i ++){ + int len = createFibo(i); + if(len > max) + break; + temp = growInt(temp,count); + temp[count++] = len; + } + int[] res = new int[count]; + System.arraycopy(temp, 0, res, 0, count); + return res; + } + + private static int[] growInt(int[] temp,int count){ + int[] n = temp; + if(count >= temp.length){ + n = new int[temp.length + (temp.length >> 1)]; + System.arraycopy(temp, 0, n, 0, temp.length); + } + return n; + } + + private static int createFibo(int n){ + if(n <= 2){ + return 1; + } + return createFibo(n-1)+createFibo(n-2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + str.append(seperator).append(array[i]); + } + return str.substring(1).toString(); + } + +} diff --git a/group10/595128841/src/org/le/b/LoginAction.java b/group10/595128841/src/org/le/b/LoginAction.java new file mode 100644 index 0000000000..d895f6b5e6 --- /dev/null +++ b/group10/595128841/src/org/le/b/LoginAction.java @@ -0,0 +1,35 @@ +package org.le.b; + +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/group10/595128841/src/org/le/b/Struts.java b/group10/595128841/src/org/le/b/Struts.java new file mode 100644 index 0000000000..303286690f --- /dev/null +++ b/group10/595128841/src/org/le/b/Struts.java @@ -0,0 +1,155 @@ +package org.le.b; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentFactory; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + private static Map actionMap; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + if(actionMap == null){ + actionMap = strutsXmlSax("struts2.xml"); + } + ActionBean actionBean = actionMap.get(actionName); + return processAction(actionBean,parameters); + } + + + private static View processAction(ActionBean actionBean, Map parameters) { + String clazzStr = actionBean.getClazz(); + Map result = actionBean.getResults(); + try { + Class clazz= Class.forName(clazzStr); + Object obj = clazz.newInstance(); + for(String key : parameters.keySet()){ + String name = "set"+(key.charAt(0)+"").toUpperCase()+key.substring(1); + Method method = clazz.getMethod(name, String.class); + method.invoke(obj, parameters.get(key)); + } + Method execute = clazz.getMethod("execute"); + String resultStr = (String)execute.invoke(obj); + String resultJsp = result.get(resultStr); + Map pramMap = new HashMap<>(); + Method meg = clazz.getMethod("getMessage"); + String resultMsg = (String)meg.invoke(obj); + pramMap.put("message", resultMsg); + return new View(resultJsp,pramMap); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + return null; + } + + + public static Map strutsXmlSax(String path){ + DocumentFactory documentFactory = DocumentFactory.getInstance(); + SAXReader saxReader = new SAXReader(documentFactory); + Document doc = null; + try { + doc = saxReader.read(path); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element rootElement = doc.getRootElement(); + Element pack = rootElement.element("package"); + List actions = pack.elements("action"); + Map actionMap = new HashMap<>(); + for(Element action : actions){ + Attribute name = action.attribute("name"); + Attribute clazz = action.attribute("class"); + List results = action.elements("result"); + Map resMap = new HashMap<>(); + for(Element result : results){ + String key = "success"; + String value = result.getTextTrim(); + Attribute rname = result.attribute("name"); + if(rname != null){ + key = rname.getValue(); + } + resMap.put(key, value); + } + actionMap.put(name.getValue(), new ActionBean(name.getValue(),clazz.getValue(),resMap)); + } + return actionMap; + } + + public static class ActionBean{ + private String name; + private String clazz; + private Map results; + + public ActionBean(String name, String clazz, Map results) { + this.name = name; + this.clazz = clazz; + this.results = results; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } + + } + +} diff --git a/group10/595128841/src/org/le/b/StrutsTest.java b/group10/595128841/src/org/le/b/StrutsTest.java new file mode 100644 index 0000000000..3733a6a777 --- /dev/null +++ b/group10/595128841/src/org/le/b/StrutsTest.java @@ -0,0 +1,38 @@ +package org.le.b; + +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/group10/595128841/src/org/le/b/View.java b/group10/595128841/src/org/le/b/View.java new file mode 100644 index 0000000000..39a9f8a644 --- /dev/null +++ b/group10/595128841/src/org/le/b/View.java @@ -0,0 +1,28 @@ +package org.le.b; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + + public View(String resultJsp, Map pramMap) { + this.jsp = resultJsp; + this.parameters = pramMap; + } + 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/group10/595128841/src/org/le/list/ArrayList.java b/group10/595128841/src/org/le/list/ArrayList.java new file mode 100644 index 0000000000..571362606b --- /dev/null +++ b/group10/595128841/src/org/le/list/ArrayList.java @@ -0,0 +1,144 @@ +/** + * + */ +package org.le; + +/** + * @author yue + * @time 2017年2月19日 + */ +public class ArrayList implements List { + + private Object[] elementData; + + private int size; + + public ArrayList(int initCapcity){ + if(initCapcity < 0){ + throw new IllegalArgumentException("initCapcity 必须大于0"); + } + elementData = new Object[initCapcity]; + } + + public ArrayList(){ + elementData = new Object[10]; + } + + @Override + public void add(Object obj) { + grow(size + 1); + elementData[size++] = obj; + } + + @Override + public void add(int index, Object obj) { + rangeCheckForAdd(index); + grow(size + 1); + System.arraycopy(elementData, index, elementData, index+1, size - index); + elementData[index] = obj; + size ++; + } + + @Override + public void remove(Object obj) { + if(obj == null){ + for (int i = 0; i < size; i++) { + if(elementData[i] == null){ + fastRemove(i); + } + } + }else{ + for (int i = 0; i < size; i++) { + if(obj.equals(elementData[i])){ + fastRemove(i); + } + } + } + } + + @Override + public E remove(int index) { + rangeCheck(index); + int movedNum = size - index - 1; + E oldElement = elementData(index); + System.arraycopy(elementData, index+1, elementData, index, movedNum); + elementData[--size] = null; + return oldElement; + } + + @Override + public E get(int index) { + rangeCheck(index); + return elementData(index); + } + + @Override + public E set(int index, E obj) { + rangeCheck(index); + E oldElement = elementData(index); + elementData[index] = obj; + return oldElement; + } + + @Override + public int indexOf(E obj) { + if(obj == null){ + for (int i = 0; i < size; i++) { + if(elementData[i] == null){ + return i; + } + } + }else{ + for (int i = 0; i < size; i++) { + if(obj.equals(elementData[i])){ + return i; + } + } + } + return -1; + } + + /** + * 数组扩容 + * @param minCapacity + */ + private void grow(int minCapacity) { + if(minCapacity <= elementData.length){ + return; + } + int oldCapacity = elementData.length; + int newCapacity = minCapacity + (oldCapacity >> 1); + if(newCapacity < minCapacity){ + newCapacity = minCapacity; + } + if(minCapacity > Integer.MAX_VALUE){ + newCapacity = Integer.MAX_VALUE; + } + Object[] newArray = new Object[newCapacity]; + System.arraycopy(elementData, 0, newArray, 0, newCapacity); + elementData = newArray; + } + + @SuppressWarnings("unchecked") + private E elementData(int index){ + return (E) elementData[index]; + } + + private void fastRemove(int i) { + int numMoved = size - i -1; + if(numMoved > 0){ + System.arraycopy(elementData, i+1, elementData, i, numMoved); + } + elementData[-- size] = null; + } + + private void rangeCheck(int index){ + if(index >= size || index <0) + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); + } + + private void rangeCheckForAdd(int index){ + if(index > size || index <0) + throw new IndexOutOfBoundsException("index:"+index+",size:"+size); + } +} diff --git a/group10/595128841/src/org/le/list/LinkedList.java b/group10/595128841/src/org/le/list/LinkedList.java new file mode 100644 index 0000000000..fbe95017cb --- /dev/null +++ b/group10/595128841/src/org/le/list/LinkedList.java @@ -0,0 +1,299 @@ +/** + * + */ +package org.le; + +import java.util.NoSuchElementException; + +/** + * @author yue + * @time 2017年2月19日 + */ +public class LinkedList implements List { + + private int size = 0; + + private Node first; + + private Node last; + + private static class Node{ + E item; + Node prev; + Node next; + Node(Node prev,E item, Node next) { + super(); + this.item = item; + this.prev = prev; + this.next = next; + } + } + + public LinkedList(){ + + } + + /** + * 头部插入 + */ + private void linkFirst(E e){ + final Node f = first; + final Node newNode = new Node(null,e,f); + first = newNode; + if(f == null) + last = newNode; + else + f.prev = newNode; + size ++; + } + + /** + * 尾部插入 + */ + private void linkLast(E e){ + final Node l = last; + final Node newNode = new Node<>(l,e,null); + last = newNode; + if(last == null) + first = newNode; + else + l.next = newNode; + size ++; + } + + /** + * 某个不为null元素之前插入 + */ + private void linkBefore(E e,Node succ){ + final Node pred = succ.prev; + final Node newNode = new Node<>(pred,e,succ); + succ.prev = newNode; + if(pred == null) + first = newNode; + else + pred.next = newNode; + size ++; + } + + /** + * 删除头部元素 + */ + private E unlinkFirst(Node f){ + final E element = f.item; + final Node next = f.next; + f.item = null; + f.next = null; + first = next; + if(next == null) + last = null; + else + next.prev = null; + size -- ; + return element; + } + /** + * 删除尾部元素 + * @param l + * @return + */ + private E unlinkLast(Node l){ + final E element = l.item; + final Node prev = l.prev; + l.item = null; + l.prev = null; + last = prev; + if(prev == null) + first = null; + else + prev.next = null; + size -- ; + return element; + } + + /** + * 删除指定节点 + * @param e + * @return + */ + private E unlink(Node e){ + final Node prev = e.prev; + final E element = e.item; + final Node next = e.next; + + if(prev == null){ + first = next; + }else{ + prev.next = next; + e.prev = null; + } + + if(next == null){ + last = prev; + }else{ + next.prev = prev; + e.next = null; + } + e.item = null; + size -- ; + return element; + } + + /** + * 该方法默认在尾部添加 + */ + @Override + public void add(E e) { + linkLast(e); + } + + /** + * + */ + @Override + public void add(int index, E e) { + checkPositionIndex(index); + if(index == size){ + linkLast(e); + }else{ + linkBefore(e, node(index)); + } + } + + private Node node(int index) { + //小于容量一半 + if(index < (size >> 1)){ + Node x = first; + 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 checkPositionIndex(int index){ + if(index <0 || index > size){ + throw new IndexOutOfBoundsException("索引越界:index:"+index+",size:"+size); + } + } + + private void checkElementIndex(int index){ + if(index <0 || index >= size){ + throw new IndexOutOfBoundsException("索引越界:index:"+index+",size:"+size); + } + } + + /** + * + */ + @Override + public void remove(E obj) { + if(obj == null){ + for(Node x = first;x != null; x = x.next){ + if(x.item == null){ + unlink(x); + } + } + }else{ + for(Node x = first;x != null;x = x.next){ + if(obj.equals(x.item)){ + unlink(x); + } + } + } + } + + /** + * + */ + @Override + public E remove(int index) { + checkElementIndex(index); + return unlink(node(index)); + } + + /** + * + */ + @Override + public E get(int index) { + checkElementIndex(index); + return node(index).item; + } + + /** + * + */ + @Override + public E set(int index, E obj) { + checkElementIndex(index); + Node x = node(index); + E oldVal = x.item; + x.item = obj; + return oldVal; + } + + /** + * + */ + @Override + public int indexOf(E obj) { + int index = 0; + if(obj == null){ + for(Node x = first;x != null;x = x.next){ + if(x.item == null) + return index; + index ++; + } + }else{ + for(Node x = first; x != null; x = x.next){ + if(obj.equals(x.item)) + return index; + index ++; + } + } + return -1; + } + /** + * 弹出栈顶的元素,不删除元素 + * @param e + * @return + */ + public E peek(){ + final Node e = first; + return e == null ? null : e.item; + } + + /** + * 弹出栈顶元素,删除元素 + * @return + */ + public E poll(){ + final Node e = first; + return (e == null) ? null : unlinkFirst(e); + } + /** + * 入栈,栈顶 + * @param e + */ + public void push(E e){ + linkFirst(e); + } + + /** + * 出栈,删除并返回栈顶元素 + * @return + */ + public E pop(){ + final Node f = first; + if(f == null) + throw new NoSuchElementException(); + return unlinkFirst(f); + } + +} diff --git a/group10/595128841/src/org/le/list/List.java b/group10/595128841/src/org/le/list/List.java new file mode 100644 index 0000000000..5fa9d6799a --- /dev/null +++ b/group10/595128841/src/org/le/list/List.java @@ -0,0 +1,19 @@ +package org.le; + +public interface List { + + void add(E obj); + + void add(int index,E obj); + + void remove(E obj); + + E remove(int index); + + E get(int index); + + E set(int index,E obj); + + int indexOf(E obj); + +} diff --git a/group10/630505243/.classpath b/group10/630505243/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group10/630505243/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group10/630505243/.gitignore b/group10/630505243/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/630505243/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/630505243/.project b/group10/630505243/.project new file mode 100644 index 0000000000..f1cd4e602e --- /dev/null +++ b/group10/630505243/.project @@ -0,0 +1,17 @@ + + + 630505243Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/630505243/src/com/coderising/array/ArrayUtil.java b/group10/630505243/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..6c251f5fa0 --- /dev/null +++ b/group10/630505243/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,238 @@ +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 int[] reverseArray(int[] origin){ + int arrLength = origin.length; + int[] newArray = new int[arrLength]; + //判断数组不为空 + if (arrLength > 0) { + for (int i = 0; i < arrLength; i++) { + newArray[i] = origin[arrLength-1-i]; + } + } + return newArray; + } + + /** + * 现在有如下的一个数组: 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 position = 0; + int zeroCount = 0; + int[] rtnArr = null; + int[] newArray = new int[oldArray.length]; + if(oldArray.length>0){ + for(int i=0;i0){ + rtnArr =new int[oldArray.length-zeroCount]; + System.arraycopy(newArray, 0, rtnArr, 0, rtnArr.length); + } + return rtnArr; + } + + /** + * 给定两个已经排序好的整形数组, 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 repeatCount=0; + int len = array1.length-array2.length>0?array2.length:array1.length; + for(int i=0;i1) + return fibonacciImpl(a-1)+fibonacciImpl(a-2); + return -1; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + int[] primes = new int[max]; + int arrCount = 0; + for(int i=1;i0){ + for(int i=0;i parameters) { + View view = new View(); + //读取配置文件 + try { + File f = new File("src/com/coderising/litestruts/struts.xml"); + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse(f); + + NodeList actionList = doc.getElementsByTagName("action"); + for(int i=0;i> set = parameters.entrySet(); + Map viewparam = new HashMap(); + for(Map.Entry entry:set){ + String key = entry.getKey(); + String value = entry.getValue(); + Method method = clazz.getMethod("set"+key.substring(0,1).toUpperCase()+key.substring(1,key.length()), String.class); + method.invoke(nobj, value); + //viewparam.put(key, value); + } + Method m = clazz.getMethod("execute", null); + String rtnStr = (String) m.invoke(nobj, null); + NodeList resultList = actionElement.getChildNodes(); + for(int j=0;j 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return view; + } + +} diff --git a/group10/630505243/src/com/coderising/litestruts/StrutsTest.java b/group10/630505243/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group10/630505243/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/group10/630505243/src/com/coderising/litestruts/View.java b/group10/630505243/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group10/630505243/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/group10/630505243/src/com/coderising/litestruts/struts.xml b/group10/630505243/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..94b96a89a5 --- /dev/null +++ b/group10/630505243/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/group10/630505243/src/com/coding/basic/ArrayList.java b/group10/630505243/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9d32776b61 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/ArrayList.java @@ -0,0 +1,112 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(o!=null){ + elementData[size] = o; + if(size<100-1){ + size++; + }else{ + Object[] temp = new Object[(size+1)+100]; + System.arraycopy(elementData, 0, temp, 0, size); + elementData = temp; + size++; + } + } + + } + public void add(int index, Object o){ + if(index=index;i--){ + if(i==index){ + temps[index] = tmp; + }else{ + temps[i]=temps[i-1]; + } + } + elementData = temps; + + }else if(index>=size-1){ + elementData[size] = o; + } + }else{ + //Ԫλôұ߽ + Object[] temp = new Object[(size+1)+100]; + System.arraycopy(elementData, 0, temp, 0, size); + elementData = temp; + elementData[index] = o; + } + size++; + } + + public Object get(int index){ + if(index<=elementData.length){ + return elementData[index]; + }else{ + return null; + } + } + + public Object remove(int index){ + Object rtnObj = null; + if(index<=size){ + if(index>{ + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + private LinkedList tree = new LinkedList(); + + public Object 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 insert(T o){ + BinaryTreeNode currNode = null; + if(root==null){ + currNode = new BinaryTreeNode(); + currNode.data = o; + this.root = currNode; + tree.addFirst(currNode); + }else{ + currNode = findTheParentPosition(root,o); + if(o.compareTo(currNode.data)>0){ + BinaryTreeNode tNode = new BinaryTreeNode(); + tNode.data = o; + tree.add(tNode); + }else{ + BinaryTreeNode tNode = new BinaryTreeNode(); + tNode.data = o; + currNode.left = tNode; + tree.add(tNode); + } + } + return currNode; + } + + private BinaryTreeNode findTheParentPosition(BinaryTreeNode node ,T o){ + if(o.compareTo(node.data) >0){ + if(node.getRight()!=null) + return findTheParentPosition(node.getRight(),o); + else + return node; + }else{ + if(node.getLeft()!=null) + return findTheParentPosition(node.getLeft(),o); + else + return node; + } + } + + public void printTree(){ + if(this.tree!=null){ + Iterator i = tree.iterator(); + while(i.hasNext()){ + System.out.println(i.next()); + } + } + } + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("[Node data:"+this.data+"]"); + return sb.toString(); + } + + + +} diff --git a/group10/630505243/src/com/coding/basic/BinaryTreeNodeTest.java b/group10/630505243/src/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..07cf012318 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class BinaryTreeNodeTest { + BinaryTreeNode root = new BinaryTreeNode(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testInsert() { + root.insert(5); + root.insert(3); + root.insert(8); + + root.printTree(); + } + +} diff --git a/group10/630505243/src/com/coding/basic/Iterator.java b/group10/630505243/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group10/630505243/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/group10/630505243/src/com/coding/basic/LinkedList.java b/group10/630505243/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d519789c36 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/LinkedList.java @@ -0,0 +1,166 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head;//ͷڵ + private Node last;//βڵ + private int size=0;// + + public void add(Object o){ + if(last==null){ + head = new Node(o, null); + last = head; + }else{ + Node nod = last; + nod.next = new Node(o,null); + last = nod.next; + } + size++; + } + public void add(int index , Object o){ + if(last==null){ + head = new Node(o,null); + last = head; + }else{ + //ҵindexǰһڵ + Node preNode = getNextNode(head, index-1); + //indexԭнڵ + Node node = getNextNode(preNode,index); + //indexǰڵ֮󣬷ԭindexڵ֮ǰ + preNode.next = new Node(o,node); + } + this.size++; + + } + + public Object get(int index){ + return getNextNode(head,index); + } + public Object remove(int index){ + //ȡɾǰһڵ + Node preNode = getNextNode(head,index-1); + Node node = getNextNode(head,index); + if(node.next!=null){ + preNode.next = getNextNode(head,index+1); + }else{ + preNode.next = null; + } + this.size--; + return node; + } + + /** + * ݹѰһڵ + * @param node սڵ + * @param index սڵľ + * @return + */ + private Node getNextNode(Node node,int index){ + if(index==0){ + return node; + } + Node rtnNode = null; + if(node.next==null){ + return rtnNode;//һڵһڵΪ + }else{ + return getNextNode(node.next,index-1); + } + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node node = new Node(o,head); + this.head = node; + this.last = head; + this.size++; + } + public void addLast(Object o){ + Node newLast = new Node(o, null); + Node node = last; + node.next = newLast; + last = newLast; + this.size++; + } + public Object removeFirst(){ + if(head==null){ + return null; + }else{ + if(head.next==null){ + head.data = null; + size--; + return head; + }else{ + Node node = head; + head = node.next; + size--; + return node; + } + } + } + public Object removeLast(){ + if(last==null){ + return null; + }else{ + //ȡԭڶڵ + Node preLast = getNextNode(head, size-2); + Node orgLast = last; + preLast.next = null; + last = preLast; + size--; + return orgLast; + } + } + + public Iterator iterator(){ + LinkedIterator linkIterator = new LinkedIterator(this); + return linkIterator; + } + + + private static class Node{ + Object data; + Node next; + public Node(Object o,Node next){ + this.data = o; + this.next = next; + } + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("["); + sb.append("data = "+this.data); + if(next!=null){ + sb.append(" ; next = "+this.next.data+" \n"); + }else{ + sb.append(" ; next = null"); + } + sb.append("]"); + return sb.toString(); + } + } + class LinkedIterator implements Iterator{ + private LinkedList list; + private int position=0; + public LinkedIterator(LinkedList list){ + this.list = list; + } + + @Override + public boolean hasNext() { + if(list.get(position)!=null){ + this.position++; + return true; + }else + return false; + } + + @Override + public Object next() { + return list.get(position-1); + } + + } +} diff --git a/group10/630505243/src/com/coding/basic/LinkedListTest.java b/group10/630505243/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..dc1108746d --- /dev/null +++ b/group10/630505243/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,103 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + LinkedList lkLst = null; + @Before + public void setUp() throws Exception { + lkLst = new LinkedList(); + lkLst.add("ABC"); + lkLst.add("CDE"); + lkLst.add("EFG"); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + lkLst.add("HIJ"); + System.out.println(lkLst.get(3)); + fail("Not yet implemented"); + } + + @Test + public void testAddIntObject() { + lkLst.add(1,"OPQ"); + System.out.println(lkLst.get(1)); + fail("Not yet implemented"); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + System.out.println(lkLst.size()); + System.out.println(lkLst.remove(2)); + System.out.println(lkLst.size()); + fail("Not yet implemented"); + } + + @Test + public void testSize() { + System.out.println(lkLst.size()); + lkLst.add("HIJ"); + lkLst.add("JKL"); + lkLst.add("LMN"); + System.out.println(lkLst.size()); + fail("Not yet implemented"); + } + + @Test + public void testAddFirst() { + lkLst.addFirst("000"); + System.out.println(lkLst.get(0)); + fail("Not yet implemented"); + } + + @Test + public void testAddLast() { + System.out.println(lkLst.size()); + lkLst.addLast("XYZ"); + System.out.println(lkLst.size()); + System.out.println(lkLst.get(lkLst.size()-1)); + fail("Not yet implemented"); + } + + @Test + public void testRemoveFirst() { + System.out.println(lkLst.get(0)); + System.out.println(lkLst.size()); + lkLst.removeFirst(); + System.out.println(lkLst.get(0)); + System.out.println(lkLst.size()); + fail("Not yet implemented"); + } + + @Test + public void testRemoveLast() { + System.out.println("ԭlast Node :"+lkLst.get(lkLst.size()-1)); + System.out.println("ɾ Node :"+lkLst.removeLast()); + System.out.println("µ Last Node"+lkLst.get(lkLst.size()-1)); + fail("Not yet implemented"); + } + + @Test + public void testIterator() { + Iterator i = lkLst.iterator(); + while(i.hasNext()){ + System.out.println(i.next()); + } + } + +} diff --git a/group10/630505243/src/com/coding/basic/List.java b/group10/630505243/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group10/630505243/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/group10/630505243/src/com/coding/basic/Queue.java b/group10/630505243/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..eb684306d5 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/Queue.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class Queue { + private int size = 0; + private Object[] datas; + private int capacity = 10; + public void enQueue(Object o){ + if(datas==null){ + datas = new Object[capacity]; + } + if(this.size>=datas.length){ + Object[] newDatas = new Object[capacity+10]; + System.arraycopy(datas,0,newDatas,0,datas.length); + datas = newDatas; + } + datas[size] = o; + size++; + } + + public Object deQueue(){ + Object o = null; + if(datas!=null && datas.length>0){ + o = datas[0]; + }else{ + System.out.println("Ϊ"); + } + for(int i=0;i0){ + return false; + }else{ + return true; + } + } + + public int size(){ + return this.size; + } +} diff --git a/group10/630505243/src/com/coding/basic/QueueTest.java b/group10/630505243/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..3658467490 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/QueueTest.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + Queue queue = null; + String str1 = "First"; + String str2 = "Second"; + String str3 = "Third"; + String str4 = "Forth"; + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @Test + public void testEnQueue() { + queue.enQueue(str1); + queue.enQueue(str2); + queue.enQueue(str3); + queue.enQueue(str4); + assertEquals(queue.size(),4); + assertFalse(queue.isEmpty()); + } + + @Test + public void testDeQueue() { + queue.enQueue(str1); + queue.enQueue(str2); + queue.enQueue(str3); + queue.enQueue(str4); + assertEquals(str1,(String) queue.deQueue()); + assertEquals(queue.size(),3); + assertEquals(str2,(String) queue.deQueue()); + assertEquals(queue.size(),2); + assertEquals(str3,(String) queue.deQueue()); + assertEquals(queue.size(),1); + assertEquals(str4,(String) queue.deQueue()); + assertEquals(queue.size(),0); + assertFalse(queue.isEmpty()); + } + +} diff --git a/group10/630505243/src/com/coding/basic/Stack.java b/group10/630505243/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..dd0a7c61a2 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/Stack.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + /** + * ѹջ + * @param o + */ + public void push(Object o){ + elementData.add(size,o); + size++; + } + + /** + * ƳջĶ󣬲Ϊ˺ֵظö + * @return + */ + public Object pop(){ + if(this.elementData.size()>0){ + Object o = elementData.get(size-1); + elementData.remove(size); + size--; + return o; + }else{ + return null; + } + } + + /** + * 鿴ջĶ󣬵ӶջƳ + * @return + */ + public Object peek(){ + if(size>0){ + return elementData.get(size-1); + }else{ + return null; + } + } + + /** + * ԶջǷΪ + * @return + */ + public boolean isEmpty(){ + return size>0?false:true; + } + + /** + * ȡջС + * @return + */ + public int size(){ + return this.size; + } +} diff --git a/group10/630505243/src/com/coding/basic/StackTest.java b/group10/630505243/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..9ad98e67b5 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/StackTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + + Stack s = null; + String a = "A"; + String b = "B"; + String c = "C"; + @Before + public void setUp() throws Exception { + s = new Stack(); + s.push(a); + //System.out.println(s.peek()); + s.push(b); + //System.out.println(s.peek()); + s.push(c); + //System.out.println(s.peek()); + } + + @Test + public void testPush() { + String d = "D"; + String e = "E"; + s.push(d); + System.out.println(s.peek()); + s.push(e); + System.out.println(s.peek()); + assertEquals(s.size(), 5); + } + + @Test + public void testPop() { + int len = s.size(); + for(int i=0;isize()){ + throw new IndexOutOfBoundsException(); + } + if (index == size) { + add(o); + } else { + Node node = head; + for (int i = 0; i < index-1; i++) { + node = node.next; + } + Node newNode = new Node(o, node); + node.next = newNode; + newNode.next = node; + size++; + } + } + public Object get(int index){ + if(index<0||index>size()){ + throw new IndexOutOfBoundsException(); + } + Node node=head; + for(int i=0;isize()){ + throw new IndexOutOfBoundsException(); + } + return null; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node newNode = new Node(o,null); + newNode.next=head.next; + } + public void addLast(Object o){ + add(o); + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group10/706097141/learning/src/com/hmily/learning/List.java b/group10/706097141/learning/src/com/hmily/learning/List.java new file mode 100644 index 0000000000..02ef056bd3 --- /dev/null +++ b/group10/706097141/learning/src/com/hmily/learning/List.java @@ -0,0 +1,8 @@ +package com.hmily.learning; +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/group10/706097141/learning/src/com/hmily/learning/MyArrayList.java b/group10/706097141/learning/src/com/hmily/learning/MyArrayList.java new file mode 100644 index 0000000000..46e1c564af --- /dev/null +++ b/group10/706097141/learning/src/com/hmily/learning/MyArrayList.java @@ -0,0 +1,91 @@ +package com.hmily.learning; + +public class MyArrayList implements List,Iterator{ + + private int size = 0; + + private Object[] elementData = new Object[100]; + + /** + * Ԫ + */ + public void add(Object o){ + if(size==elementData.length){ + Object[] newElementData = new Object[elementData.length+1]; + for(int i=0;iindex;i--){ + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + size++; + } + /** + * ȡԪ + */ + public Object get(int index){ + if(index>=size()||index<0){ + throw new ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + /** + * ƳԪ + */ + public Object remove(int index){ + if(index<0||index>size()){ + throw new ArrayIndexOutOfBoundsException(); + } + Object o=elementData[index]; + for(int i=index;i + + + + + + + + diff --git a/group10/875867419/.gitignore b/group10/875867419/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/875867419/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/875867419/.project b/group10/875867419/.project new file mode 100644 index 0000000000..b6d8ce6204 --- /dev/null +++ b/group10/875867419/.project @@ -0,0 +1,17 @@ + + + coding2017 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/875867419/src/com/work/week01/MyArrayList.java b/group10/875867419/src/com/work/week01/MyArrayList.java new file mode 100644 index 0000000000..d005800d39 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyArrayList.java @@ -0,0 +1,203 @@ +package com.work.week01; + +import java.io.Serializable; +import java.util.Arrays; + +/** + * ʵListӿڣķʽʵԼArrayList + * @author denghuaijun + * + * @param + */ +public class MyArrayList implements MyList,Serializable { + + private static final long serialVersionUID = 4145346362382387995L; + + /** + * ĬϴС + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * ĬϿ + */ + private static final Object[] EMPTY_ELEMENTDATA = {}; + + transient Object[] elementData; + + /** + * С + */ + private int size; + + public MyArrayList(){ + this.elementData = EMPTY_ELEMENTDATA; + } + + public MyArrayList(int capacity){ + if(capacity > 0){ + this.elementData = new Object[capacity]; + }else if(capacity == 0){ + this.elementData = EMPTY_ELEMENTDATA; + }else{ + throw new IllegalArgumentException("Ƿ"); + } + } + + private void ensureCapacity(int minCapacity){ + if(this.elementData == EMPTY_ELEMENTDATA){ + minCapacity = Math.max(minCapacity, DEFAULT_CAPACITY); + } + if(minCapacity > elementData.length){//λô鳤 + grow(minCapacity); + } + } + + private void grow(int minCapacity){ + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if(newCapacity < minCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + + @Override + public boolean add(E element) { + ensureCapacity(size + 1); + elementData[size++] = element; + return true; + } + + @Override + public void add(int index, E element) { + //ȷindexǷԽ + checkAddRange(index); + //ȷ鳤Ƿ㹻 + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = element; + size++; + } + + private void checkAddRange(int index){ + if(index < 0 || index > size){//index == size Ԫ + throw new IndexOutOfBoundsException("Խ"); + } + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + checkRange(index); + return (E) elementData[index]; + } + + private void checkRange(int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("Խ"); + } + } + + @SuppressWarnings("unchecked") + @Override + public E remove(int index) { + checkRange(index); + E element = (E) elementData[index]; + int numMoved = size - index - 1; + if(numMoved > 0){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + elementData[size--] = null; + return element; + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public int indexOf(Object o) { + if(o == null){ + for(int i=0;i=0;i--){ + if(elementData[i] == null){ + return i; + } + } + }else{ + for(int i=size-1;i>=0;i--){ + if(o.equals(elementData[i])){ + return i; + } + } + } + return -1; + } + + @Override + public MyIterator iterator() { + return new MyIter(); + } + + private class MyIter implements MyIterator{ + + int flag = -1; + + public MyIter(){ + flag = size; //鳤 + } + + @Override + public boolean hasNext() { + return flag > 0; + } + + @SuppressWarnings("unchecked") + @Override + public E next() { + if(!hasNext()){ + throw new IndexOutOfBoundsException("ֵ鷶Χ"); + } + return (E) elementData[size-(flag--)]; + } + + } + public static void main(String[] args) { + MyArrayList array = new MyArrayList(); + array.add("1"); + array.add("2"); + array.add("3"); + array.add("4"); + array.remove(2); + array.add(2, "1"); + System.out.println("size="+array.size()); + System.out.println("indexOf(3)="+array.indexOf("3")); + System.out.println("lastIndexOf(1)="+array.lastIndexOf("1")); + MyIterator itr = array.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + } +} diff --git a/group10/875867419/src/com/work/week01/MyBinaryTree.java b/group10/875867419/src/com/work/week01/MyBinaryTree.java new file mode 100644 index 0000000000..8c6f057648 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyBinaryTree.java @@ -0,0 +1,82 @@ +package com.work.week01; + +public class MyBinaryTree { + + private MyBinaryTreeNode parent; + + public MyBinaryTree(){ + this.parent = new MyBinaryTreeNode(null, null, null); + } + + public void insertNode(E element){ + MyBinaryTreeNode node = new MyBinaryTreeNode(element, null, null); + if(parent.element == null){ + parent = node; + return; + } + insertNode(parent, node); + } + + private void insertNode(MyBinaryTreeNode parentNode, MyBinaryTreeNode newNode){ + if(parentNode.compareTo(newNode) <= 0){// + if(parentNode.right == null){ + parentNode.right = newNode; + }else{ + insertNode(parentNode.right, newNode); + } + }else{ + if(parentNode.left == null){ + parentNode.left = newNode; + }else{ + insertNode(parentNode.left, newNode); + } + } + } + + private void printNode(MyBinaryTreeNode node, int count){ + if(node.left != null){ + printNode(node.left, count++); + } + if(node.right != null){ + printNode(node.right, count++); + } + for(int i=0;i implements Comparable> { + + private T element; + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public MyBinaryTreeNode(T element, MyBinaryTreeNode left, MyBinaryTreeNode right){ + this.element = element; + this.left = left; + this.right = right; + } + + @Override + public int compareTo(MyBinaryTreeNode o) { + Integer src = (Integer) this.element; + Integer dest = (Integer) o.element; + return src.compareTo(dest); + } + } + + public static void main(String[] args) { + MyBinaryTree tree = new MyBinaryTree(); + tree.insertNode(5); + tree.insertNode(7); + tree.insertNode(3); + tree.insertNode(9); + tree.insertNode(4); + tree.printTree(); + } +} diff --git a/group10/875867419/src/com/work/week01/MyIterator.java b/group10/875867419/src/com/work/week01/MyIterator.java new file mode 100644 index 0000000000..78abc20f23 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyIterator.java @@ -0,0 +1,6 @@ +package com.work.week01; + +public interface MyIterator { + boolean hasNext(); + E next(); +} diff --git a/group10/875867419/src/com/work/week01/MyLinkedList.java b/group10/875867419/src/com/work/week01/MyLinkedList.java new file mode 100644 index 0000000000..675323a249 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyLinkedList.java @@ -0,0 +1,169 @@ +package com.work.week01; + +import java.io.Serializable; + + +public class MyLinkedList implements MyList, Serializable{ + + private static final long serialVersionUID = 8700137302944494769L; + + transient int size = 0; + + transient MyNode head; + transient MyNode last; + + public MyLinkedList(){ + head = new MyNode(null, null); + last = new MyNode(null, null); + } + + @Override + public boolean add(E element) { + if(head.element == null){ + head = new MyNode(element, null); + last = head; + }else{ + MyNode node = new MyNode(element, null); + last.next = node; + last = node; + } + size++; + return true; + } + + @Override + public void add(int index, E element) { + if(index < 0 || index -size > 0){ + throw new IndexOutOfBoundsException(""); + } + if(index == 0){ + MyNode node = new MyNode(element, null); + node.next = head; + head = node; + }else{ + MyNode leftNode = getIndexNode(index-1); + MyNode node = new MyNode(element, null); + node.next = leftNode.next; + leftNode.next = node; + } + size++; + } + + private MyNode getIndexNode(int index){ + MyNode node = head; + for(int i=0; i= 0){ + throw new IndexOutOfBoundsException(""); + } + MyNode node = getIndexNode(index); + return node.element; + } + + @Override + public E remove(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(""); + } + if(index == 0){//Ƴͷ + MyNode node = head; + head = head.next; + node.next = null; + size--; + return node.element; + }else{ + MyNode leftNode = getIndexNode(index-1); + MyNode node = leftNode.next; //ƳĽڵ + leftNode.next = node.next; + node.next = null; + size--; + return node.element; + } + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + public void addFirst(E element){ + add(0, element); + } + + public void addLast(E element){ + add(size, element); + } + + public void removeFirst(){ + remove(0); + } + + public void removeLast(){ + remove(size-1); + } + + private static class MyNode{ + E element; + MyNode next; + + MyNode(E element, MyNode next) { + this.element = element; + this.next = next; + } + + } + + @Override + public MyIterator iterator() { + return new MyIter(); + } + + private class MyIter implements MyIterator{ + + int flag = 0; + + public MyIter(){ + flag = size; + } + + @Override + public boolean hasNext() { + return flag > 0; + } + + @Override + public E next() { + if(!hasNext()){ + throw new IndexOutOfBoundsException("ֵΧ"); + } + return get(size-(flag--)); + } + } + + public static void main(String[] args) { + MyLinkedList link = new MyLinkedList(); + link.add("1"); + link.add("2"); + link.add("3"); + link.add("4"); + link.add(3, "1"); + link.removeFirst(); + System.out.println("size="+link.size()); + MyIterator itr = link.iterator(); + while(itr.hasNext()){ + System.out.println(itr.next()); + } + link.remove(4); + } +} diff --git a/group10/875867419/src/com/work/week01/MyList.java b/group10/875867419/src/com/work/week01/MyList.java new file mode 100644 index 0000000000..f7cc918888 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyList.java @@ -0,0 +1,11 @@ +package com.work.week01; + +public interface MyList{ + boolean add(E element); + void add(int index, E element); + E get(int index); + E remove(int index); + int size(); + boolean isEmpty(); + MyIterator iterator(); +} diff --git a/group10/875867419/src/com/work/week01/MyQueue.java b/group10/875867419/src/com/work/week01/MyQueue.java new file mode 100644 index 0000000000..97bca5399a --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyQueue.java @@ -0,0 +1,38 @@ +package com.work.week01; + +public class MyQueue { + private MyArrayList elementData; + + public MyQueue(){ + elementData = new MyArrayList(); + } + + public void enQueue(E element){// + elementData.add(element); + } + + public E deQuene(){// Ƚȳ + return elementData.remove(0); + } + + public int size(){ + return elementData.size(); + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public static void main(String[] args) { + MyQueue queue = new MyQueue(); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + queue.enQueue("5"); + System.out.println("size="+queue.size()); + while(!queue.isEmpty()){ + System.out.println(queue.deQuene()); + } + } +} diff --git a/group10/875867419/src/com/work/week01/MyStack.java b/group10/875867419/src/com/work/week01/MyStack.java new file mode 100644 index 0000000000..f82bbe04c1 --- /dev/null +++ b/group10/875867419/src/com/work/week01/MyStack.java @@ -0,0 +1,43 @@ +package com.work.week01; + +public class MyStack { + private MyArrayList elementData; + + public MyStack(){ + elementData = new MyArrayList<>(); + } + + public void push(E element){ + elementData.add(element); + } + + public E pop(){ //ƳջԪ ȳ + return elementData.remove(elementData.size() - 1); + } + + public E peek(){ //ȡջԪ + return elementData.get(elementData.size() - 1); + } + + public int size(){ + return elementData.size(); + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public static void main(String[] args) { + MyStack stack = new MyStack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + stack.push("4"); + stack.push("5"); + System.out.println("size="+stack.size()); + System.out.println("peekջԪ="+stack.peek()); + while(!stack.isEmpty()){ + System.out.println("popջԪ"+stack.pop()); + } + } +} diff --git "a/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" "b/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..5c63425fba --- /dev/null +++ "b/group10/875867419/src/com/work/week01/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +ҵַhttps://my.oschina.net/u/3080511/blog/846172 \ No newline at end of file diff --git a/group10/875867419/src/com/work/week02/ArrayUtil.java b/group10/875867419/src/com/work/week02/ArrayUtil.java new file mode 100644 index 0000000000..cd22eba6ef --- /dev/null +++ b/group10/875867419/src/com/work/week02/ArrayUtil.java @@ -0,0 +1,198 @@ +package com.work.week02; + +/** + * ڶݽṹҵ + * @author denghuaijun + * + */ +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 int[] reverseArray(int[] origin){ + int[] dest = new int[origin.length]; + for(int i=0;i array2[j]){ + dest[k++] = array2[j++]; + continue; + } + } + //ֻʣarray1ʱ + while(i < array1.length){ + dest[k++] = array1[i++]; + } + //ֻʣarray2ʱ + while(j < array2.length){ + dest[k++] = array2[j++]; + } + return removeZero(dest); + } + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int newLength = oldArray.length + size; + int[] newArray = new int[newLength]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + int[] dest = null; + if(max == 1){ + return dest; + } + dest = new int[max+3]; + dest[0] = dest[1] = 1; + for(int i=2; i parameters){ + View view = new View(); + try{ + StrutsXmlDao dao = loadXmlByDom4j(actionName); + view = reflectCreateObj(dao, parameters); + }catch(Exception e) { + e.printStackTrace(); + } + return view; + } + + /** + * 读取xml文件 + * @throws DocumentException + */ + @SuppressWarnings("unchecked") + private static StrutsXmlDao loadXmlByDom4j(String actionName) throws DocumentException{ + StrutsXmlDao dao = new StrutsXmlDao(); + SAXReader reader = new SAXReader(); //创建SAXReader对象 + Document doc = reader.read(new File("src/com/work/week02/struts.xml")); //创建Document对象 + Element root = doc.getRootElement(); //获取根节点 + + List list = root.elements(); + for (Element element : list) { + if(element.attributeValue("name").equals(actionName)){ + dao.setActionName(element.attributeValue("name")); + dao.setActionClass(element.attributeValue("class")); + List branchs = element.elements(); + List> actionResult = new ArrayList>(); + for (Element branch : branchs) { + Map map = new HashMap(); + map.put(branch.attributeValue("name"), branch.getTextTrim()); + actionResult.add(map); + } + dao.setActionResult(actionResult); + } + + } + + return dao; + } + + /** + * 通过反射创建指定对象 + * @throws ClassNotFoundException + * @throws IllegalAccessException + * @throws InstantiationException + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalArgumentException + */ + private static View reflectCreateObj(StrutsXmlDao dao, Map parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{ + View view = new View(); + Class clazz = Class.forName(dao.getActionClass()); + //实例化对象 + Object obj = clazz.newInstance(); + //调用set方法设置参数 + Set keys = parameters.keySet(); + for (String key : keys) { + String value = parameters.get(key); + Method setMethod = clazz.getDeclaredMethod("set"+key.substring(0, 1).toUpperCase()+key.substring(1), new Class[]{String.class}); + setMethod.invoke(obj, value); + } + + //调用execute()方法执行 + Method execute = clazz.getMethod("execute"); + Object reuslt = execute.invoke(obj); //execute()方法返回值 + List> actionResult = dao.getActionResult(); + //获取返回值对应的跳转jsp + for (Map map : actionResult) { + if(map.containsKey(reuslt)){ + view.setJsp(map.get(reuslt).toString()); + } + } + + Map params = new HashMap(); + //调用get方法将参数存入view中 + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + String name = field.getName(); + Method getMethod = clazz.getDeclaredMethod("get"+name.substring(0, 1).toUpperCase()+name.substring(1)); + String value = getMethod.invoke(obj).toString(); + params.put(name, value); + } + view.setParameters(params); + + return view; + } +} diff --git a/group10/875867419/src/com/work/week02/StrutsTest.java b/group10/875867419/src/com/work/week02/StrutsTest.java new file mode 100644 index 0000000000..b2c3ab11a3 --- /dev/null +++ b/group10/875867419/src/com/work/week02/StrutsTest.java @@ -0,0 +1,40 @@ +package com.work.week02; + +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"); //ԤIJһ + + 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/group10/875867419/src/com/work/week02/StrutsXmlDao.java b/group10/875867419/src/com/work/week02/StrutsXmlDao.java new file mode 100644 index 0000000000..4d355d189a --- /dev/null +++ b/group10/875867419/src/com/work/week02/StrutsXmlDao.java @@ -0,0 +1,49 @@ +package com.work.week02; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +public class StrutsXmlDao implements Serializable { + + private static final long serialVersionUID = -3117497421210403602L; + + private String actionName; + private String actionClass; + private List> actionResult; + + public StrutsXmlDao(){ + super(); + } + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public String getActionClass() { + return actionClass; + } + + public void setActionClass(String actionClass) { + this.actionClass = actionClass; + } + + public List> getActionResult() { + return actionResult; + } + + public void setActionResult(List> actionResult) { + this.actionResult = actionResult; + } + + @Override + public String toString() { + return "StrutsXmlDao [actionName=" + actionName + ", actionClass=" + actionClass + ", actionResult=" + + actionResult + "]"; + } + +} diff --git a/group10/875867419/src/com/work/week02/View.java b/group10/875867419/src/com/work/week02/View.java new file mode 100644 index 0000000000..a4a2830750 --- /dev/null +++ b/group10/875867419/src/com/work/week02/View.java @@ -0,0 +1,30 @@ +package com.work.week02; + + +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; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } + +} diff --git a/group10/875867419/src/com/work/week02/struts.xml b/group10/875867419/src/com/work/week02/struts.xml new file mode 100644 index 0000000000..da643f1f65 --- /dev/null +++ b/group10/875867419/src/com/work/week02/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/group10/875867419/src/com/work/week02/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" "b/group10/875867419/src/com/work/week02/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..d1dace46cf --- /dev/null +++ "b/group10/875867419/src/com/work/week02/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +ܲҵӵַhttps://my.oschina.net/u/3080511/blog/851907 \ No newline at end of file diff --git a/group10/904627477/.classpath b/group10/904627477/.classpath new file mode 100644 index 0000000000..e6a94f0acc --- /dev/null +++ b/group10/904627477/.classpath @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/group10/904627477/.gitignore b/group10/904627477/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/904627477/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/904627477/.project b/group10/904627477/.project new file mode 100644 index 0000000000..9cd9b13ae3 --- /dev/null +++ b/group10/904627477/.project @@ -0,0 +1,23 @@ + + + testGit + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/group10/904627477/.settings/org.eclipse.jdt.core.prefs b/group10/904627477/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bbcbc93486 --- /dev/null +++ b/group10/904627477/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group10/904627477/.settings/org.eclipse.m2e.core.prefs b/group10/904627477/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..14b697b7bb --- /dev/null +++ b/group10/904627477/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group10/904627477/pom.xml b/group10/904627477/pom.xml new file mode 100644 index 0000000000..2346e64026 --- /dev/null +++ b/group10/904627477/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + testGit + testGit + 0.0.1-SNAPSHOT + + + + + + + dom4j + dom4j + 1.6.1 + + + + + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + \ No newline at end of file diff --git a/group10/904627477/src/com/coding/ArrayList.java b/group10/904627477/src/com/coding/ArrayList.java new file mode 100644 index 0000000000..3053e54114 --- /dev/null +++ b/group10/904627477/src/com/coding/ArrayList.java @@ -0,0 +1,107 @@ +package com.coding; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private void addLength(){ + int len = elementData.length; + Object[] temp = new Object[len+50]; + for(int i=0;isize()){ + throw new IndexOutOfBoundsException(); + } + if(size==elementData.length){ + addLength(); + } + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index<0||index>=size()){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size()){ + throw new IndexOutOfBoundsException(); + } + Object temp = elementData[index]; + for (int i = index; i < size-1; i++) { + elementData[i] = elementData[i+1]; + } + elementData[size-1] = null; + size--; + return temp; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private Object[] array; + private int index; + + public ArrayListIterator(){ + array = new Object[size]; + index = 0; + for (int i = 0; i < size; i++) { + array[i] = elementData[i]; + } + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + if(index>=0&&index=array.length){ + throw new NoSuchElementException(); + } + Object temp = array[index]; + index ++; + return temp; + } + + } + + +} diff --git a/group10/904627477/src/com/coding/BinaryTreeNode.java b/group10/904627477/src/com/coding/BinaryTreeNode.java new file mode 100644 index 0000000000..fe50e3d1ae --- /dev/null +++ b/group10/904627477/src/com/coding/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding; + +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/group10/904627477/src/com/coding/Iterator.java b/group10/904627477/src/com/coding/Iterator.java new file mode 100644 index 0000000000..d2c1b79572 --- /dev/null +++ b/group10/904627477/src/com/coding/Iterator.java @@ -0,0 +1,7 @@ +package com.coding; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group10/904627477/src/com/coding/LinkedList.java b/group10/904627477/src/com/coding/LinkedList.java new file mode 100644 index 0000000000..f878f75820 --- /dev/null +++ b/group10/904627477/src/com/coding/LinkedList.java @@ -0,0 +1,175 @@ +package com.coding; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + Node node = new Node(); + node.data = o; + if(index==0){ + addFirst(o); + return ; + } + Node before = getNode(index-1); + Node next = before.next; + before.next = node; + node.next = next; + } + + private Node getLastNode(){ + Node temp = head; + if(head!=null){ + while(true){ + if(temp.next!=null){ + temp = temp.next; + }else{ + break; + } + } + }else{ + throw new NoSuchElementException(); + } + return temp; + } + + private Node getNode(int index){ + if(index<0){ + throw new IndexOutOfBoundsException(); + } + int i = 0; + Node temp = head; + while(true){ + if(temp==null){ + throw new IndexOutOfBoundsException(); + } + if(i==index){ + break; + }else{ + i++; + temp = temp.next; + } + } + return temp; + } + + public Object get(int index){ + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + if(index==0){ + removeFirst(); + } + Node before = getNode(index-1); + Node temp = getNode(index); + before.next = temp.next; + return temp.data; + } + + public int size(){ + int size = 0; + Node temp = head; + while(true){ + if(temp==null){ + break; + }else{ + size++; + temp = temp.next; + } + } + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = head; + head = node; + } + + public void addLast(Object o){ + Node node = new Node(); + node.data = o; + if(head==null){ + head = node; + return; + } + Node last = getLastNode(); + last.next = node; + } + public Object removeFirst(){ + if(head == null){ + throw new NoSuchElementException(); + } + Object obj = head.data; + head = head.next; + return obj; + } + public Object removeLast(){ + if(head == null){ + throw new NoSuchElementException(); + } + if(head.next == null){ + return removeFirst(); + } + Node before = head; + Node temp = head.next; + while(true){ + if(temp.next==null){ + break; + }else{ + before = temp; + temp = temp.next; + } + } + before.next = null; + return temp.data; + } + + public Iterator iterator(){ + return new LinkedIterator(); + } + + + private static class Node{ + Object data; + Node next; + } + + private class LinkedIterator implements Iterator{ + + private Node node; + + public LinkedIterator(){ + node = head; + } + + @Override + public boolean hasNext() { + if(node!=null){ + return true; + } + return false; + } + + @Override + public Object next() { + if(node==null){ + throw new NoSuchElementException(); + }else{ + Object obj = node.data; + node = node.next; + return obj; + } + } + + } +} diff --git a/group10/904627477/src/com/coding/List.java b/group10/904627477/src/com/coding/List.java new file mode 100644 index 0000000000..5e78eced11 --- /dev/null +++ b/group10/904627477/src/com/coding/List.java @@ -0,0 +1,9 @@ +package com.coding; + +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/group10/904627477/src/com/coding/Queue.java b/group10/904627477/src/com/coding/Queue.java new file mode 100644 index 0000000000..8fd01bef96 --- /dev/null +++ b/group10/904627477/src/com/coding/Queue.java @@ -0,0 +1,28 @@ +package com.coding; + +public class Queue { + + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + if(elementData.size()==0){ + return null; + } + return elementData.remove(0); + } + + public boolean isEmpty(){ + if(elementData.size()==0){ + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group10/904627477/src/com/coding/Stack.java b/group10/904627477/src/com/coding/Stack.java new file mode 100644 index 0000000000..3f8b319607 --- /dev/null +++ b/group10/904627477/src/com/coding/Stack.java @@ -0,0 +1,36 @@ +package com.coding; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size()==0){ + throw new EmptyStackException(); + } + Object obj = elementData.remove(size()-1); + return obj; + } + + public Object peek(){ + if(elementData.size()==0){ + throw new EmptyStackException(); + } + Object obj = elementData.get(size()-1); + return obj; + } + public boolean isEmpty(){ + if(elementData.size()==0){ + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group10/904627477/src/com/coding/array/ArrayUtil.java b/group10/904627477/src/com/coding/array/ArrayUtil.java new file mode 100644 index 0000000000..37f230812a --- /dev/null +++ b/group10/904627477/src/com/coding/array/ArrayUtil.java @@ -0,0 +1,215 @@ +package com.coding.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int len = origin.length; + for (int i = 0; i < len/2 ; i++) { + int temp = origin[len-1-i]; + origin[len-1-i] = origin[i]; + origin[i] = 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 int[] removeZero(int[] oldArray){ + int[] tempArr = new int[oldArray.length]; + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + tempArr[size] = oldArray[i]; + size++; + } + } + int[] newArr = new int[size]; + System.arraycopy(tempArr, 0, newArr, 0, size); + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int[] arr3 = new int[array1.length+array2.length]; + int len1 = array1.length; + int len2 = array2.length; + int i=0,j=0,k=0; + while(true){ + if(iarray2[j]){ + arr3[k++]=array2[j++]; + }else{ + arr3[k++] = array1[i++]; + j++; + } + }else if(i>=len1&&j=len2){ + arr3[k++]=array1[i++]; + }else{ + break; + } + } + int[] newArr = new int[k]; + System.arraycopy(arr3, 0, newArr, 0, k); + return newArr; + } + /** + * 把一个已经存满数据的数组 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){ + throw new IllegalArgumentException(); + } + int[] newArr = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArr[i] = oldArray[i]; + } + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if(max<1){ + throw new IllegalArgumentException(); + } + int[] arr = new int[0]; + int temp = 1; + for (int i = 0; max > temp; i++) { + arr = grow(arr, 1); + arr[arr.length-1] = temp; + temp = getFibonacci(i+1); + } + return arr; + } + + public int getFibonacci(int n){ + if(n==0||n==1){ + return 1; + }else{ + return getFibonacci(n-1)+getFibonacci(n-2); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + //对大于1的自然数n,如果用2到 开根号n 之间的所有整数去除,均无法整除,则n为质数 + public int[] getPrimes(int max){ + int[] arr = new int[0]; + if(max<2){ + return arr; + } + for(int i=2;i1; + } + for(int i=2;i<=Math.sqrt(n);i++){ + if(n%i==0){ + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] arr = new int[0]; + if(max<1){ + return arr; + } + for (int i = 0; i < max; i++) { + if(isPerfectNumber(i)){ + arr = grow(arr, 1); + arr[arr.length-1] = i; + } + } + return arr; + } + + public boolean isPerfectNumber(int n){ + if(n<=1){ + return false; + } + int result = 1; + for (int i = 2; i <= Math.sqrt(n); i++) { + if(n%i==0){ + result = result + i + n/i; + } + } + if(result==n){ + return true; + }else{ + return false; + } + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String result = ""; + for (int i = 0; i < array.length; i++) { + result = result + array[i] + seperator; + } + int index = result.lastIndexOf(seperator); + return result.substring(0, index); + } + + +} diff --git a/group10/904627477/src/com/coding/litestruts/LoginAction.java b/group10/904627477/src/com/coding/litestruts/LoginAction.java new file mode 100644 index 0000000000..ec16031fee --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.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/group10/904627477/src/com/coding/litestruts/ReflectUtil.java b/group10/904627477/src/com/coding/litestruts/ReflectUtil.java new file mode 100644 index 0000000000..789275aa02 --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/ReflectUtil.java @@ -0,0 +1,145 @@ +package com.coding.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.regex.Pattern; + +public class ReflectUtil { + + + public static Object exectue(Object o,String methodName){ + Object result = null; + try { + Method m = o.getClass().getMethod(methodName); + result = m.invoke(o); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return result; + } + + public static Object getObject(String className, Map parameters){ + Object action = null; + try { + action = Class.forName(className).newInstance(); + if(parameters==null){ + return action; + } + Iterator ite = parameters.keySet().iterator(); + while(ite.hasNext()){ + String name = ite.next(); + String value = parameters.get(name); + setAttribute(action, name, value); + } + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return action; + } + + /** + * + * @param o + * @param name + * @param value + */ + public static void setAttribute(Object o,String name,String value){ + try { + Class c = o.getClass(); + String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); + Class attType = c.getDeclaredField(name).getType(); + Method m = c.getMethod(methodName, attType); + m.invoke(o, typeCase(value, attType)); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + public static Map getAttributes(Object o){ + Map param = new HashMap(); + Class c = o.getClass(); + Field[] attrs = c.getDeclaredFields(); + for (Field att : attrs) { + String name = att.getName(); + String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); + Object reslut = exectue(o, methodName); + if(reslut!=null){ + param.put(name, reslut); + } + } + return param; + } + + /** + * 将Sting类型数据转换成指定类型数据,暂支持简单和常见类型 + * @param oldValue 需要转换的数据值 + * @param type 目标类型 + * @return 返回转换后的数据 + */ + public static Object typeCase(String oldValue,Class type){ + Object value = null; + String typeName = type.getName(); + if("int".equals(typeName)){ + value = Integer.parseInt(oldValue); + }else if("float".equals(typeName)){ + value = Float.parseFloat(oldValue); + }else if("boolean".equals(typeName)){ + value = Boolean.parseBoolean(oldValue); + }else if("long".equals(typeName)){ + value = Long.parseLong(oldValue); + }else if("byte".equals(typeName)){ + value = Byte.parseByte(oldValue); + }else if("double".equals(typeName)){ + value = Double.parseDouble(oldValue); + }else if("short".equals(typeName)){ + value = Short.parseShort(oldValue); + }else if("char".equals(typeName)){ + value = oldValue.charAt(0); + }else if("java.util.Date".equals(typeName)){ + try { + if(Pattern.matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3}", oldValue)){ + value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse(oldValue); + }else if(Pattern.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}", oldValue)){ + value = new SimpleDateFormat("yyyy-MM-dd").parse(oldValue); + }else{ + value = null; + } + } catch (ParseException e) { + e.printStackTrace(); + } + }else if("java.lang.String".equals(typeName)){ + value = oldValue; + }else{ + throw new ClassCastException(); + } + return value; + } +} diff --git a/group10/904627477/src/com/coding/litestruts/Struts.java b/group10/904627477/src/com/coding/litestruts/Struts.java new file mode 100644 index 0000000000..eb68fa9441 --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/Struts.java @@ -0,0 +1,106 @@ +package com.coding.litestruts; + +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; + + + + +public class Struts { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + public static View runAction(String actionName, Map parameters) { + if(actionName==null){ + return null; + } + View view = new View(); + String path = getStrutsXMLPath(); + Element actionEle = getActionElement(path, actionName); + if(actionEle==null){ + return null; + } + String className = actionEle.attributeValue("class"); + Object action = ReflectUtil.getObject(className, parameters); + if(action==null){ + return null; + } + String methodName = actionEle.attributeValue("method"); + methodName = methodName==null?"execute":methodName; + Object reslut = ReflectUtil.exectue(action, methodName); + String jsp = getElementJsp(actionEle, reslut!=null?reslut.toString():null); + view.setJsp(jsp); + view.setParameters(ReflectUtil.getAttributes(action)); + return view; + } + + private static String getStrutsXMLPath(){ + String path = Struts.class.getResource("").getPath()+"struts.xml"; + path = path.substring(1); + return path; + } + + @SuppressWarnings("unchecked") + public static Element getActionElement(String path,String actionName){ + if(path==null||actionName==null){ + return null; + } + Element actionEle = null; + try { + SAXReader read = new SAXReader(); + Document doc = read.read(path); + Element root = doc.getRootElement(); + List actions = root.elements("action"); + for (Element element : actions) { + String name = element.attributeValue("name"); + if(actionName.equals(name)){ + actionEle = element; + break; + } + } + } catch (SecurityException e) { + e.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } + return actionEle; + } + + @SuppressWarnings("unchecked") + public static String getElementJsp(Element actionEle, String reslut) { + String jsp = null; + if(reslut!=null){ + List results = actionEle.elements("result"); + for (Element reslutEle : results) { + String resName = reslutEle.attributeValue("name"); + resName = resName==null?"success":resName; + if(reslut.equals(resName)){ + jsp = reslutEle.getText().trim(); + } + } + } + return jsp; + } + +} diff --git a/group10/904627477/src/com/coding/litestruts/StrutsTest.java b/group10/904627477/src/com/coding/litestruts/StrutsTest.java new file mode 100644 index 0000000000..fe43a16de8 --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coding.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/group10/904627477/src/com/coding/litestruts/View.java b/group10/904627477/src/com/coding/litestruts/View.java new file mode 100644 index 0000000000..9ee1d76628 --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.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/group10/904627477/src/com/coding/litestruts/struts.xml b/group10/904627477/src/com/coding/litestruts/struts.xml new file mode 100644 index 0000000000..b494d6b52f --- /dev/null +++ b/group10/904627477/src/com/coding/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/group10/904627477/src/com/coding/test/ArrayListTest.java b/group10/904627477/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..d65a692351 --- /dev/null +++ b/group10/904627477/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,71 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.ArrayList; +import com.coding.Iterator; + +public class ArrayListTest { + + private static ArrayList list = new ArrayList(); + + @Before + public void setUp() throws Exception { + list = new ArrayList(); + list.add("1111"); + list.add("2222"); + list.add("3333"); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void testAddObject() { + list.add("4444"); + assertEquals("4444", list.get(3)); + assertEquals(4, list.size()); + } + + @Test + public void testAddIntObject() { + list.add(1, "4444"); + assertEquals("4444", list.get(1)); + assertEquals(4, list.size()); + assertEquals("2222", list.get(2)); + } + + @Test + public void testGet() { + assertEquals("1111", list.get(0)); + } + + @Test + public void testRemove() { + Object sss = list.remove(1); + assertEquals(2, list.size()); + assertEquals("2222", sss); + } + + @Test + public void testSize() { + assertEquals(3, list.size()); + } + + @Test + public void testIterator() { + Iterator it = list.iterator(); + assertEquals(true, it.hasNext()); + assertEquals("1111", it.next()); + assertEquals("2222", it.next()); + assertEquals("3333", it.next()); + assertEquals(false, it.hasNext()); + } + +} diff --git a/group10/904627477/src/com/coding/test/ArrayUtilTest.java b/group10/904627477/src/com/coding/test/ArrayUtilTest.java new file mode 100644 index 0000000000..f6bf45f1cc --- /dev/null +++ b/group10/904627477/src/com/coding/test/ArrayUtilTest.java @@ -0,0 +1,128 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.array.ArrayUtil; + +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + + @Before + public void setUp() throws Exception { + this.arrayUtil = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + this.arrayUtil = null; + } + + @Test + public void testReverseArray() { + int[] origin1 = {7, 9 , 30, 3}; + int[] origin2 = {7, 9, 30, 3, 4}; + this.arrayUtil.reverseArray(origin1); + assertEquals(3, origin1[0]); + assertEquals(30, origin1[1]); + assertEquals(9, origin1[2]); + assertEquals(7, origin1[3]); + this.arrayUtil.reverseArray(origin2); + assertEquals(4, origin2[0]); + assertEquals(3, origin2[1]); + assertEquals(30, origin2[2]); + assertEquals(9, origin2[3]); + assertEquals(7, origin2[4]); + } + + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArr = this.arrayUtil.removeZero(oldArr); + assertEquals(12, newArr.length); + assertEquals(6, newArr[4]); + int oldArr1[]={0,0,0,0}; + int[] newArr1 = this.arrayUtil.removeZero(oldArr1); + assertEquals(0, newArr1.length); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + //[3,4,5,6,7,8] + int[] a3 = this.arrayUtil.merge(a1, a2); + assertEquals(6, a3.length); + assertEquals(3, a3[0]); + assertEquals(8, a3[5]); + int[] arr1 = {3, 5, 7}; + int[] arr2 = {9,12,15,19}; + int[] arr3 = this.arrayUtil.merge(arr1, arr2); + int[] arr4 = this.arrayUtil.merge(arr2, arr1); + assertEquals(7, arr3.length); + assertEquals(3, arr3[0]); + assertEquals(19, arr3[6]); + //assertEquals(arr3, arr4); + assertEquals(7, arr4.length); + assertEquals(3, arr4[0]); + assertEquals(19, arr4[6]); + int[] a = {}; + int[] arr = this.arrayUtil.merge(a, a1); + assertEquals(4, arr.length); + assertEquals(3, arr[0]); + assertEquals(8, arr[3]); + } + + @Test + public void testGrow() { + int[] a1 = {2,3,6}; + int[] a2 = this.arrayUtil.grow(a1, 3); + assertEquals(6, a2.length); + assertEquals(3, a1[1]); + assertEquals(0, a2[3]); + int[] a3 = this.arrayUtil.grow(a1, 0); + assertEquals(3, a3.length); + int[] a4 = this.arrayUtil.grow(new int[0], 3); + assertEquals(3, a4.length); + } + + @Test + public void testFibonacci() { + int[] arr = this.arrayUtil.fibonacci(15); + assertEquals(7, arr.length); + assertEquals(13, arr[6]); + int[] arr1 = this.arrayUtil.fibonacci(1); + assertEquals(0, arr1.length); + } + + @Test + public void testGetPrimes() { + int[] arr = this.arrayUtil.getPrimes(23); + assertEquals(8, arr.length); + assertEquals(true, arr[arr.length-1]<23); + int[] arr1 = this.arrayUtil.getPrimes(2); + assertEquals(0, arr1.length); + } + + @Test + public void testGetPerfectNumbers() { + //6 28 496 8128 33550336 + int[] arr = this.arrayUtil.getPerfectNumbers(10000); + assertEquals(4, arr.length); + assertEquals(8128, arr[3]); + } + + @Test + public void testJoin() { + int[] array= {3,8,9}; + String str = this.arrayUtil.join(array, "-"); + assertEquals("3-8-9", str); + String str1 = this.arrayUtil.join(array, "9"); + assertEquals("39899", str1); + } + +} diff --git a/group10/904627477/src/com/coding/test/LinkedListTest.java b/group10/904627477/src/com/coding/test/LinkedListTest.java new file mode 100644 index 0000000000..af48a6fb0b --- /dev/null +++ b/group10/904627477/src/com/coding/test/LinkedListTest.java @@ -0,0 +1,118 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.Iterator; +import com.coding.LinkedList; + +public class LinkedListTest { + + private static LinkedList link; + + @Before + public void setUp() throws Exception { + link = new LinkedList(); + link.add("111"); + link.add("222"); + link.add("333"); + } + + @After + public void tearDown() throws Exception { + link = null; + } + + @Test + public void testAddObject() { + link.add("444"); + assertEquals(4, link.size()); + assertEquals("444", link.get(3)); + link = new LinkedList(); + link.add("000"); + assertEquals(1, link.size()); + assertEquals("000", link.get(0)); + } + + @Test + public void testAddIntObject() { + link.add(2,"444"); + assertEquals(4, link.size()); + assertEquals("444", link.get(2)); + assertEquals("333", link.get(3)); + assertEquals("222", link.get(1)); + link = new LinkedList(); + link.add(0,"000"); + assertEquals(1, link.size()); + assertEquals("000", link.get(0)); + } + + @Test + public void testGet() { + assertEquals("222", link.get(1)); + } + + @Test + public void testRemove() { + Object obj = link.remove(1); + assertEquals("222", obj); + assertEquals("333", link.get(1)); + assertEquals(2, link.size()); + } + + @Test + public void testSize() { + assertEquals(3, link.size()); + } + + @Test + public void testAddFirst() { + link.addFirst("000"); + assertEquals(4, link.size()); + assertEquals("000", link.get(0)); + link = new LinkedList(); + link.addFirst("000"); + assertEquals(1, link.size()); + assertEquals("000", link.get(0)); + } + + @Test + public void testAddLast() { + link.addLast("444"); + assertEquals(4, link.size()); + assertEquals("444", link.get(3)); + link = new LinkedList(); + link.addLast("444"); + assertEquals(1, link.size()); + assertEquals("444", link.get(0)); + } + + @Test + public void testRemoveFirst() { + Object obj = link.removeFirst(); + assertEquals("111", obj); + assertEquals(2, link.size()); + assertEquals("222", link.get(0)); + } + + @Test + public void testRemoveLast() { + Object obj = link.removeLast(); + assertEquals(2, link.size()); + assertEquals("333", obj); + } + + @Test + public void testIterator() { + Iterator it = link.iterator(); + assertEquals(true, it.hasNext()); + assertEquals("111", it.next()); + assertEquals("222", it.next()); + assertEquals("333", it.next()); + assertEquals(false, it.hasNext()); + } + +} diff --git a/group10/904627477/src/com/coding/test/QueueTest.java b/group10/904627477/src/com/coding/test/QueueTest.java new file mode 100644 index 0000000000..779120c0fb --- /dev/null +++ b/group10/904627477/src/com/coding/test/QueueTest.java @@ -0,0 +1,53 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.Queue; + +public class QueueTest { + + private static Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + queue.enQueue("111"); + queue.enQueue("222"); + queue.enQueue("333"); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void testEnQueue() { + queue.enQueue("444"); + assertEquals(4, queue.size()); + } + + @Test + public void testDeQueue() { + Object obj = queue.deQueue(); + assertEquals(2, queue.size()); + assertEquals("111",obj); + } + + @Test + public void testIsEmpty() { + assertEquals(false, queue.isEmpty()); + queue = new Queue(); + assertEquals(true, queue.isEmpty()); + } + + @Test + public void testSize() { + assertEquals(3, queue.size()); + } + +} diff --git a/group10/904627477/src/com/coding/test/StackTest.java b/group10/904627477/src/com/coding/test/StackTest.java new file mode 100644 index 0000000000..38de36c9d9 --- /dev/null +++ b/group10/904627477/src/com/coding/test/StackTest.java @@ -0,0 +1,61 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.Stack; + +public class StackTest { + + private static Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + stack.push("111"); + stack.push("222"); + stack.push("333"); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void testPush() { + stack.push("444"); + assertEquals(4, stack.size()); + assertEquals("444", stack.pop()); + } + + @Test + public void testPop() { + Object obj = stack.pop(); + assertEquals("333", obj); + assertEquals(2, stack.size()); + } + + @Test + public void testPeek() { + Object obj = stack.peek(); + assertEquals("333", obj); + assertEquals(3, stack.size()); + } + + @Test + public void testIsEmpty() { + assertEquals(false, stack.isEmpty()); + stack = new Stack(); + assertEquals(true, stack.isEmpty()); + } + + @Test + public void testSize() { + assertEquals(3,stack.size()); + } + +} diff --git a/group10/904627477/src/com/coding/test/Test.java b/group10/904627477/src/com/coding/test/Test.java new file mode 100644 index 0000000000..c8024a8c97 --- /dev/null +++ b/group10/904627477/src/com/coding/test/Test.java @@ -0,0 +1,29 @@ +package com.coding.test; + + + + + +public class Test { + + public static void main(String[] args) { + /*int[] origin = {7, 9 , 30, 3}; + ArrayUtil au = new ArrayUtil(); + au.reverseArray(origin); + for (int i : origin) { + System.out.println(i); + } + */ +/* int a = 5; + System.out.println(5/2); + int[] a1 = new int[0]; + System.out.println(a1.length); + System.out.println(Math.sqrt(2));*/ +// int[] a2 = new ArrayUtil().getPerfectNumbers(100); +// for (int i = 0; i < a2.length; i++) { +// System.out.println(a2[i]); +// } + System.out.println(Test.class.getResource("").getPath()); + } + +} diff --git a/group10/group10.md b/group10/group10.md index d3f5a12faa..8b13789179 100644 --- a/group10/group10.md +++ b/group10/group10.md @@ -1 +1 @@ - + diff --git a/group11/1059156023/Array/.classpath b/group11/1059156023/Array/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group11/1059156023/Array/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group11/1059156023/Array/.gitignore b/group11/1059156023/Array/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/Array/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/Array/.project b/group11/1059156023/Array/.project new file mode 100644 index 0000000000..1b5c14fe3f --- /dev/null +++ b/group11/1059156023/Array/.project @@ -0,0 +1,17 @@ + + + Array + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java b/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java new file mode 100644 index 0000000000..2b47957623 --- /dev/null +++ b/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java @@ -0,0 +1,158 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.HashSet; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for (int i = 0; i < origin.length/2; i++) {//前后交换元素 + int temp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1]= temp; + } + System.out.println(Arrays.toString(origin));//输出数组 + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] arr = new int[oldArray.length]; + int count = 0; + for(int i=0;i set1 = new HashSet(Arrays.asList(array1));//以array1建立集合 + HashSet set2 = new HashSet(Arrays.asList(array2)); + set1.addAll(set2);//求并集 + Integer[] arr = set1.toArray(new Integer[set1.size()]);//获取并集后的数组 + Arrays.sort(arr);//数组排序 + for(int i=0;i + + + + + diff --git a/group11/1059156023/dataStructure/.gitignore b/group11/1059156023/dataStructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/dataStructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/dataStructure/.project b/group11/1059156023/dataStructure/.project new file mode 100644 index 0000000000..35bb23a0fc --- /dev/null +++ b/group11/1059156023/dataStructure/.project @@ -0,0 +1,17 @@ + + + dataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java b/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c853dcaea3 --- /dev/null +++ b/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.coding.basic; + +import java.util.List; + +public class ArrayList implements List{ + private int size; + + //设置一个默认容量,当调用默认构造函数实例化数组后,需要扩容时可用 + private static final int DEFAULT_CAPACITY=10; + + //Integer.MAX_VALUE:2147483647,MAX_ARRAY_SIZE:2147483639 + private static final int MAX_ARRAY_SIZE=Integer.MAX_VALUE-8; + + private Object[] elementData; + + //定义一个默认为空的数组,供默认构造函数使用 + private static final Object[] EMPTY_ELEMENTDATA={}; + + //定义默认构造函数,实例化为空数组 + public ArrayList(){ + this.elementData=EMPTY_ELEMENTDATA; + } + + //定义一个有参的构造函数 + public ArrayList(int initialCapacity){ + if(initialCapacity<0) + throw new IllegalArgumentException("Illegal Capacity:"+initialCapacity); + this.elementData = new Object[initialCapacity]; + } + + //定义add(Object o)方法,默认在数组末尾添加 + public boolean add(Object o){ + //要添加一个数,所以用ensureCapacityInternal()判断size+1个的数,数组是否放得下 + ensureCapacityInternal(size+1); + elementData[size++]=o; + return true; + } + + private void ensureCapacityInternal(int minCapacity) { + if(elementData == EMPTY_ELEMENTDATA) + minCapacity = DEFAULT_CAPACITY; + + //如果需要扩容,则调用grow() + if(minCapacity-elementData.length>0) + grow(minCapacity); + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity+(oldCapacity>>1); + + //原始长度是0时,即原来是空数组时 + if(newCapacity-minCapacity<0) + newCapacity = minCapacity; + + //如果新的容量超过了数组最大容量,就调用hugeCapacity()把能给的最大容量给它 + if(newCapacity-MAX_ARRAY_SIZE>0) + newCapacity = hugeCapacity(minCapacity); + + + } + + private static int hugeCapacity(int minCapacity) { + if (minCapacity<0) { + throw new OutOfMemoryError(); //抛出内存溢出异常 + } + //如果minCapacity比MAX_ARRAY_SIZE大,则返回int类型所能表示的最大值,否则返回MAX_ARRAY_SIZE + return (minCapacity>MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; + } + +} diff --git a/group11/1059156023/struts/.classpath b/group11/1059156023/struts/.classpath new file mode 100644 index 0000000000..400cc1471c --- /dev/null +++ b/group11/1059156023/struts/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group11/1059156023/struts/.gitignore b/group11/1059156023/struts/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/struts/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/struts/.project b/group11/1059156023/struts/.project new file mode 100644 index 0000000000..2c00c2049a --- /dev/null +++ b/group11/1059156023/struts/.project @@ -0,0 +1,17 @@ + + + struts + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/struts/src/com/coding/basic/LoginAction.java b/group11/1059156023/struts/src/com/coding/basic/LoginAction.java new file mode 100644 index 0000000000..23683995f2 --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group11/1059156023/struts/src/com/coding/basic/Struts.java b/group11/1059156023/struts/src/com/coding/basic/Struts.java new file mode 100644 index 0000000000..a0d26a207b --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/Struts.java @@ -0,0 +1,59 @@ +package com.coding.basic; + +import java.io.File; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.experimental.theories.Theories; + +import com.sun.corba.se.impl.orbutil.graph.Node; +import com.sun.org.apache.bcel.internal.classfile.Attribute; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException { + //创建SAXReader对象 + SAXReader reader = new SAXReader(); + //读取文件 转换成Document + Document document = reader.read(new File("src/com/coding/basic/struts.xml")); + //获取根节点元素对象 + Element root = document.getRootElement(); + //遍历根节点 + listNodes(root); + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + + public static void listNodes(Element node) { + String name; + List list = node.attributes(); + + } + +} diff --git a/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java b/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java new file mode 100644 index 0000000000..5bac52805d --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class StrutsTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testLoginActionSuccess() throws DocumentException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws DocumentException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} diff --git a/group11/1059156023/struts/src/com/coding/basic/View.java b/group11/1059156023/struts/src/com/coding/basic/View.java new file mode 100644 index 0000000000..9e479018c8 --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/View.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/1059156023/struts/src/com/coding/basic/struts.xml b/group11/1059156023/struts/src/com/coding/basic/struts.xml new file mode 100644 index 0000000000..b3b576c15f --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/1178243325/DataStructure/build.gradle b/group11/1178243325/DataStructure/build.gradle new file mode 100644 index 0000000000..9c6bc859e6 --- /dev/null +++ b/group11/1178243325/DataStructure/build.gradle @@ -0,0 +1,20 @@ + +apply plugin: 'java' +apply plugin: 'eclipse' + +jar { + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }} + manifest { + attributes 'Main-Class' : 'com.Main' + } +} + +repositories { + mavenCentral() +} + +dependencies { + compile 'junit:junit:4.12' + compile 'dom4j:dom4j:1.6.1' +} + diff --git a/group11/1178243325/DataStructure/readme.md b/group11/1178243325/DataStructure/readme.md new file mode 100644 index 0000000000..29ad9d5c06 --- /dev/null +++ b/group11/1178243325/DataStructure/readme.md @@ -0,0 +1 @@ +实现基本的数据结构ArrayList,LinkList,Stack,Queue,Tree,Iterator diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java new file mode 100644 index 0000000000..f5e5a36ebd --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/Main.java @@ -0,0 +1,56 @@ +package com; + +import java.util.*; +import com.coderising.litestruts.*; +import com.coderising.array.*; +public class Main { + public static void main(String[] args) { + int[] array = {1, 2, 3, 4, 5}; + System.out.print("reverseArray测试:"); + ArrayUtil.reverseArray(array); + for (int i : array) + System.out.print(i + " "); + System.out.print("\nremoveZero测试:"); + + int[] oldArray = {1, 3, 4, 5, 0, 0, 8 , 0, 9}; + oldArray = ArrayUtil.removeZero(oldArray); + for (int i : oldArray) { + System.out.print(i + " "); + } + + System.out.print("\nmerge测试:"); + int[] a1 = {3, 5,8}; + int[] a2 = {4, 5, 6,7}; + int[] arrays = ArrayUtil.merge(a1, a2); + for (int i : arrays) + System.out.print(i + " "); + + System.out.print("\ngrow测试:"); + + int[] growArray = ArrayUtil.grow(a1, 5); + for (int i : growArray) + System.out.print(i + " "); + + System.out.print("\nfibonacci测试"); + int[] fArray = ArrayUtil.fibonacci(1); + System.out.print(fArray); + System.out.println(); + fArray = ArrayUtil.fibonacci(15); + for (int i : fArray) + System.out.print(i + " "); + System.out.print("\ngetPrimes测试:"); + int[] primesArray = ArrayUtil.getPrimes(23); + for (int i : primesArray) + System.out.print(i + " "); + System.out.print("\ngetPerfectNumbers测试:"); + int[] pArray = ArrayUtil.getPerfectNumbers(100); + for (int i : pArray) + System.out.print(i + " "); + System.out.print("\njoin测试:"); + int[] jArray = new int[]{2, 3, 8}; + System.out.print(ArrayUtil.join(jArray, "-")); + Map map = new HashMap<>(); + Struts.runAction("login", map); + + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f94d5d01c4 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + if (origin == null) { + return; + } + + int length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) + temp[i] = origin[i]; + for (int i = length - 1, j = 0; i >= 0 && j < length; i--, j++) + origin[j] = temp[i]; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if (oldArray == null) { + return null; + } + + int zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeroCount++; + } + int[] newArray = new int[oldArray.length-zeroCount]; + for (int i = 0, j = 0; i < oldArray.length && j < newArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + if (array1 == null && array2 == null) + return null; + int index1 = 0, index2 = 0; + int[] array3 = new int[array1.length + array2.length]; + int index = 0; + while (index1 != array1.length && index2 != array2.length) { + if (array1[index1] < array2[index2]) { + array3[index++] = array1[index1++]; + } else if (array1[index1] > array2[index2]) { + array3[index++] = array2[index2++]; + } else if (array1[index1] == array2[index2]){ + array3[index++] = array1[index1++]; + index2++; + } + } + + if (index1 == array1.length && index2 != array2.length) { + for (int i = index2; i < array2.length; i++) + array3[index++] = array2[i]; + } else if (index2 == array2.length && index1 != array1.length) { + for (int i = index1; i < array1.length; i++) { + array3[index++] = array1[i]; + } + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array3[i]; + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + if (size <= 0) + return null; + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max < 1) + return null; + if (max == 1) + return null; + int[] array = new int[max]; + int i = 0; + int value = fibonaccis(i+1); + while ( value < max) { + array[i++] = value; + value = fibonaccis(i+1); + } + int[] newArray = new int[i]; + for (int j = 0; j < newArray.length; j++) { + newArray[j] = array[j]; + } + return newArray; + } + + private static int fibonaccis(int n) { + if (n <=0) + return 0; + if (n == 1 || n ==2 ) + return 1; + return fibonaccis(n-1)+fibonaccis(n-2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if (max <= 1) { + return null; + } + int[] array = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + if (i == 2 || i == 3 || i == 5 || i == 7) + array[index++] = i; + if (i%2 !=0 && i%3 != 0 && i%5 != 0 && i%7 != 0) + array[index++] = i; + } + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + if (max <= 0) + return null; + int[] array = new int[max]; + int index = 0; + for (int i = 1; i < max; i++) { + if (isPerfectNumber(i)) + array[index++] = i; + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array[i]; + + return newArray; + } + + private static boolean isPerfectNumber(int n) { + int sum = 0; + int i = 1; + while (i < n) { + if (n%i == 0) + sum += i; + i++; + } + if (sum == n) + return true; + return false; + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + if (array == null) + return null; + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length-1) + str.append(array[i]); + else + str.append(array[i] + seperator); + } + return str.toString(); + } + + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp new file mode 100644 index 0000000000..1f45a5f25e Binary files /dev/null and b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp differ diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..b3bd421435 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,62 @@ +package com.coderising.litestruts; + +import java.util.Map; +import java.util.HashMap; +import java.lang.reflect.Method; +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml*/ + /* + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + try { + String targetClassName = XmlUtil.parseXML("struts.xml", actionName); + Class targetClass = Class.forName(targetClassName); + + Method setName = targetClass.getMethod("setName", String.class); + Method setPassword = targetClass.getMethod("setPassword", String.class); + Object object = targetClass.newInstance(); + + setName.invoke(object, parameters.get("name")); + setPassword.invoke(object, parameters.get("password")); + + Method execute = targetClass.getMethod("execute"); + String result = (String)execute.invoke(object); + + Method getMessage = targetClass.getMethod("getMessage"); + String message = (String)getMessage.invoke(object); + + Map params = new HashMap(); + params.put("message", message); + String jspUrl = XmlUtil.getJspUrl("struts.xml", actionName, result); + View view = new View(); + view.setJsp(jspUrl); + view.setParameters(params); + return view; + + } catch (Exception e) { + e.printStackTrace(); + } + + + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java new file mode 100644 index 0000000000..d200452cc8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts; + +import java.io.*; +import java.util.*; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; +public class XmlUtil { + + public static String parseXML(String filePath, String actionName) { + try { + File file = new File(filePath); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { + Element element = (Element)iter.next(); + Attribute nameAttr = element.attribute("name"); + if (nameAttr.getValue().equals(actionName)) { + Attribute classAttr = element.attribute("class"); + return classAttr.getValue(); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.out.println("parse error"); + } + return null; + } + + public static String getJspUrl(String filePath, String actionName, String resultName) { + try { + File file = new File(filePath); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { + Element element = (Element)iter.next(); + Attribute nameAttr = element.attribute("name"); + if (nameAttr.getValue().equals(actionName)) { + for (Iterator ite = element.elementIterator("result"); ite.hasNext();) { + Element ele = (Element)ite.next(); + Attribute resultAttr = ele.attribute("name"); + if (resultAttr.getValue().equals(resultName)) { + return ele.getText(); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f6cd4c38fc --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,97 @@ +package com.coding.basic; + +import com.coding.basic.exception.*; +public class ArrayList implements List { + + private int size; + private Object[] elementData; + + public ArrayList () { + size = 0; + elementData = new Object[100]; + } + + public void add(Object o){ + add(size(), o); + } + + public void add(int index, Object o){ + if (size() == elementData.length) + ensureCapacity( size() * 2 + 1); + if (index > size() || index < 0) { //index == size时相当于在尾后插入 + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + for (int i = size; i > index; i--) { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + size++; + + } + + private void ensureCapacity(int newCapacity) { + if (newCapacity < size()) + return; + Object[] old = elementData; + elementData = new Object[newCapacity]; + for (int i = 0; i < size(); i++) { + elementData[i] = old[i]; + } + } + + public Object get(int index){ + if (index >= size() || index < 0) { //获取时,index==size()越界 + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + return elementData[index]; + } + + private String outOfBoundsMsg(int index) { + return "Index:" + index + ", Size:" + size; + } + + public Object remove(int index){ + if (index >= size() || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Object old = elementData[index]; + for (int i = index; i < size(); i++) { + elementData[i] = elementData[i+1]; + } + size--; + return old; + } + + /*获取表内容量*/ + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + public class ArrayListIterator implements Iterator { + private final int ONLY_CAPACITY = size; + private int index; + public ArrayListIterator() { + index = 0; + } + + @Override + public boolean hasNext() { + if (ONLY_CAPACITY != size) + throw new ConcurrentModificationException("此对象没有进行修改同步"); + return index != size; + } + + @Override + public Object next() { + if (ONLY_CAPACITY != size) + throw new ConcurrentModificationException("此对象没有进行修改同步"); + if (index >= ONLY_CAPACITY) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + return elementData[index++]; + } + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..1cf38aee30 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,33 @@ +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; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..ff93e30377 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d82349089b --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,135 @@ +package com.coding.basic; + +import com.coding.basic.exception.*; +public class LinkedList implements List { + + private Node head; + private int size; + public LinkedList() { + head = new Node(null, null); + size = 0; + } + + public void add(Object o){ + add(size, o); + } + + public void add(int index , Object o){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node newNode = new Node(o, frontNode.next); + frontNode.next = newNode; + size++; + + } + + private Node getNode(int index) { + Node node = head; + int i = 0; + while(node.next != null && i <= index) { + node = node.next; + i++; + } + return node; + } + + public Object get(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node oldNode = getNode(index); + frontNode.next = oldNode.next; + size--; + return oldNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + //就是这么硬! + add(0, o); + } + + 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 LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int index; + final int capacity = size; + LinkedListIterator() { + index = 0; + } + @Override + public boolean hasNext() { + if (capacity != size) + throw new ConcurrentModificationException("此对象没有修改同步"); + return index < capacity; + } + + @Override + public Object next() { + if (capacity != size) + throw new ConcurrentModificationException("此对象没有修改同步"); + if (index >= capacity) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + return get(index++); + } + } + + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + + void setData(Object data) { + this.data = data; + } + + Object getData() { + return data; + } + + void setNext(Node next) { + this.next = next; + } + + Object getNext() { + return next; + } + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/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/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a5c31f5a09 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; +import com.coding.basic.exception.*; +public class Queue { + + private LinkedList elementData; + + public Queue() { + elementData = new LinkedList(); + } + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + throw new EmptyQueueException("队空"); + } + Object result = elementData.removeFirst(); + return result; + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..e41c662792 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +import com.coding.basic.exception.*; +public class Stack { + + private ArrayList elementData; + public Stack() { + elementData = new ArrayList(); + } + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) { + throw new EmptyStackException("栈空"); + } + Object result = elementData.get(size()-1); + elementData.remove(size()-1); + return result; + } + + public Object peek(){ + if (isEmpty()) { + throw new EmptyStackException("栈空"); + } + return elementData.get(0); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java new file mode 100644 index 0000000000..f1c5c79721 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/ConcurrentModificationException.java @@ -0,0 +1,9 @@ +package com.coding.basic.exception; + +public class ConcurrentModificationException extends RuntimeException { + public ConcurrentModificationException() {} + public ConcurrentModificationException(String msg) { + super(msg); + } +} + diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java new file mode 100644 index 0000000000..2ee7aa4ee7 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyQueueException.java @@ -0,0 +1,8 @@ +package com.coding.basic.exception; + +public class EmptyQueueException extends RuntimeException { + public EmptyQueueException() {} + public EmptyQueueException(String msg) { + super(msg); + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java new file mode 100644 index 0000000000..2a5ae4055d --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/exception/EmptyStackException.java @@ -0,0 +1,8 @@ +package com.coding.basic.exception; + +public class EmptyStackException extends RuntimeException { + public EmptyStackException() {} + public EmptyStackException(String msg) { + super(msg); + } +} diff --git a/group11/1178243325/DataStructure/src/main/resources/struts.xml b/group11/1178243325/DataStructure/src/main/resources/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1178243325/DataStructure/struts.xml b/group11/1178243325/DataStructure/struts.xml new file mode 100644 index 0000000000..0582b7d4ea --- /dev/null +++ b/group11/1178243325/DataStructure/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1310368322/GitHub/.gitignore b/group11/1310368322/GitHub/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group11/1310368322/GitHub/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group11/1310368322/GitHub/src/ArrayList.java b/group11/1310368322/GitHub/src/ArrayList.java new file mode 100644 index 0000000000..68b42b1c03 --- /dev/null +++ b/group11/1310368322/GitHub/src/ArrayList.java @@ -0,0 +1,79 @@ +package Day_2017_2_20_DateStructure; + +public class ArrayList { + + + private int size = 0; + + private Object[] elementData = new Object[10]; + + private Exception Exception; + + public void add(Object o){ + if(size>elementData.length){ + elementData = ArrayList.grow(elementData, 10); + } + for(int i = 0; i < elementData.length; i++){ + if(null == elementData[i]){ + elementData[i] = o; + break; + } + } + size++; + } + public void add(int index, Object o){ + if(size>elementData.length){ + elementData = ArrayList.grow(elementData, 10); + } + if(index<0){ + System.out.println("λ"); + } + int k = -1; + for(int i = index; i < elementData.length; i++){ + if(null==elementData[i]){ + k = i-1; + break; + } + } + for(int i = k; i >= index;i--){ + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + } + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + while(true){ + elementData[index] = elementData[index+++1]; + if(elementData[index]==null){ + break; + } + } + size--; + return null; + } + public int size(){ + return -1; + } + public void getElementData(){ + for(int i = 0; i < elementData.length; i++){ + System.out.println(elementData[i]); + + } + } + public static Object[] grow(Object[] elementData2, int size){ + Object []target = new Object[elementData2.length+size]; + System.arraycopy(elementData2, 0, target, 0, elementData2.length); + return target; + } + + public static void main(String[] args) { + ArrayList a = new ArrayList(); + a.add("a"); + a.getElementData(); + System.out.println(a.size); + } +} diff --git a/group11/1310368322/GitHub/src/testGitHub.java b/group11/1310368322/GitHub/src/testGitHub.java new file mode 100644 index 0000000000..af74e99b53 --- /dev/null +++ b/group11/1310368322/GitHub/src/testGitHub.java @@ -0,0 +1,7 @@ + +public class testGitHub { + private void mian() { + System.out.print("Hello GitHub"); + } + +} diff --git a/group11/171535320/ArrayUtil.java b/group11/171535320/ArrayUtil.java new file mode 100644 index 0000000000..6b6cb818d0 --- /dev/null +++ b/group11/171535320/ArrayUtil.java @@ -0,0 +1,211 @@ +import sun.security.util.Length; + +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; + +/** + * Created by dengdechao on 2017/2/27. + */ +public class ArrayUtil { + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + + public static void reverseArray(int[] origin){ + if(origin == null) { + return ; + } + int i = 0; + int j = origin.length - 1; + while(i != j) { + int temp = origin[i]; + origin[i] = origin[j]; + origin[j] = temp; + i++; + j--; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if(oldArray == null) { + return null; + } + int i = 0; + int j = oldArray.length - 1; + + while(i != j) { + if(oldArray[i] != 0) { + i++; + continue; + } + if(oldArray[j] == 0) { + j--; + continue; + } + int temp = oldArray[i]; + oldArray[i] = oldArray[j]; + oldArray[j] = temp; + } + int[] array = new int[i]; + for(int n = 0; n < i; ++n) { + array[n] = oldArray[n]; + } + return array; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + int[] result = new int[array1.length + array2.length]; + int len1 = 0; + int len2 = 0; + int i = 0; + while(len1 != array1.length || len2 != array2.length) { + if(len1 < array1.length && len2 < array2.length) { + if(array1[len1] < array2[len2]) { + result[i++] = array1[len1++]; + } else if(array1[len1] > array2[len2]) { + result[i++] = array2[len2++]; + } else { + result[i++] = array1[len1]; + len1++; + len2++; + } + } else if(len1 < array1.length){ + result[i++] = array1[len1++]; + } else { + result[i++] = array2[len2++]; + } + + } + int[] last = new int[i]; + System.arraycopy(result,0,last,0,i); + return last; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int len = oldArray.length + size; + int[] result = new int[len]; + for(int i = 0; i < oldArray.length; ++i) { + result[i] = oldArray[i]; + } + return result; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + List list = new ArrayList(); + + for(int i = 2; i <= max; ++i) { + int temp = 2; + while(temp < i) { + if(i % temp == 0) { + break; + } + ++temp; + } + if(i == temp) { + list.add(i); + } + } + + int[] result = new int[list.size()]; + + for(int i = 0; i < list.size(); ++i) { + result[i] = list.get(i); + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + List list = new ArrayList(); + + for(int j = 0; j < max; ++j) { + int temp = 0; + for(int i = 0; i < j / 2 + 1; ++i) { + if(j % i == 0) { + temp += i; + } + } + if(temp == j) { + list.add(j); + } + } + + int[] result = new int[list.size()]; + + for(int i = 0; i < list.size(); ++i) { + result[i] = list.get(i); + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder str = new StringBuilder(); + for(int i = 0; i < array.length; ++i) { + str.append(array[i]); + if(i + 1 < array.length) { + str.append(seperator); + } + } + String s = str.toString(); + return s; + } +} diff --git a/group11/171535320/DataStruct/ArrayList.java b/group11/171535320/DataStruct/ArrayList.java new file mode 100644 index 0000000000..4340deecf4 --- /dev/null +++ b/group11/171535320/DataStruct/ArrayList.java @@ -0,0 +1,64 @@ +package DataStruct; + +import java.util.Arrays; +import java.util.Objects; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private void ensureCapacity(int minCapacity) { + if(minCapacity > elementData.length) { + Object[] temp = elementData; + int newCapacity = elementData.length * 3 / 2 + 1; + Object[] newArray = new Object[newCapacity]; + System.arraycopy(temp, 0, newArray, 0, elementData.length); + elementData = newArray; + } + } + + public void add(Object o){ + + + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o){ + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index >= size || index < 0) { + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if(index < 0 || index >= size) { + return null; + } + Object obj = elementData[index]; + + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + + elementData[--size] = null; + + return obj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group11/171535320/DataStruct/BinaryTreeNode.java b/group11/171535320/DataStruct/BinaryTreeNode.java new file mode 100644 index 0000000000..e0cc417a43 --- /dev/null +++ b/group11/171535320/DataStruct/BinaryTreeNode.java @@ -0,0 +1,45 @@ +package DataStruct; + +public class BinaryTreeNode { + + private Node root = null; + + public void insert(int value) { + if(root == null) { + root = new Node(value); + root.leftNode = null; + root.rightNode = null; + } else { + Node current = root; + Node old = root; + while(true) { + if(value < current.value) { + if(current.leftNode == null) { + current.leftNode = new Node(value); + break; + } + old = current; + current = current.leftNode; + } else { + if(current.rightNode == null) { + current.rightNode = new Node(value); + break; + } + old = current; + current = current.rightNode; + } + } + } + } + +} + +class Node { + int value; + Node leftNode; + Node rightNode; + + public Node(int value) { + this.value = value; + } +} diff --git a/group11/171535320/DataStruct/Iterator.java b/group11/171535320/DataStruct/Iterator.java new file mode 100644 index 0000000000..012d28806a --- /dev/null +++ b/group11/171535320/DataStruct/Iterator.java @@ -0,0 +1,12 @@ +package DataStruct; + +/** + * Created by dengdechao on 2017/2/27. + */ + + public interface Iterator { + public boolean hasNext(); + public Object next(); + + } + diff --git a/group11/171535320/DataStruct/LinkedList.java b/group11/171535320/DataStruct/LinkedList.java new file mode 100644 index 0000000000..24a99466ca --- /dev/null +++ b/group11/171535320/DataStruct/LinkedList.java @@ -0,0 +1,154 @@ +package DataStruct; + +import DataStruct.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + if(head == null) { + head = new Node(); + head.data = o; + } else { + Node temp = head; + while(temp.next != null) { + temp = temp.next; + } + temp.next = new Node(); + temp.next.data = o; + } + } + + public void add(int index , Object o){ + if(index > size()) { + return ; + } + if(index == 0) { + Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + head = newNode; + } else { + int temp = 0; + Node newNode = new Node(); + newNode.data = o; + Node tempNode = head; + while(temp != index - 1) { + tempNode = tempNode.next; + ++temp; + } + Node tempNode2 = tempNode.next; + tempNode.next = newNode; + newNode.next = tempNode2; + } + } + + public Object get(int index){ + if(index > size() || size() == 0) { + return null; + } + Node temp = head; + for(int i = 0; i < index; ++i) { + temp = temp.next; + } + + return temp.data; + } + + + public Object remove(int index){ + if(size() == 0) { + return null; + } + if(size() == 1) { + Object obj = head.data; + head = null; + return obj; + } + if(index == 0) { + Node temp = head; + head = head.next; + temp.next = null; + } else if(index > size()) { + return null; + } else { + int t = 0; + Node temp = head; + while(t != index - 1) { + temp = temp.next; + ++t; + } + Node result = temp.next; + temp.next = temp.next.next; + return result.data; + } + return null; + } + + public int size(){ + int len = 0; + Node temp = head; + while(temp != null) { + temp = temp.next; + ++len; + } + return len; + } + + public void addFirst(Object o){ + Node temp = new Node(); + temp.data = o; + temp.next = head; + head = temp; + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + + if(size() == 0) { + head = newNode; + } + Node temp = head; + while(temp.next != null) { + temp = temp.next; + } + temp.next = newNode; + } + public Object removeFirst(){ + if(size() == 0) { + return null; + } + Node temp = head; + head = head.next; + return temp.data; + } + public Object removeLast(){ + if(size() == 0) { + return null; + } + if(size() == 1) { + Node temp = head.next; + head = head.next; + return temp.data; + } + Node temp1 = head; + Node temp2 = head.next; + while(temp2.next != null) { + temp1 = temp1.next; + temp2 = temp2.next; + } + temp1.next = null; + return temp2.data; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group11/171535320/DataStruct/List.java b/group11/171535320/DataStruct/List.java new file mode 100644 index 0000000000..125407436c --- /dev/null +++ b/group11/171535320/DataStruct/List.java @@ -0,0 +1,9 @@ +package DataStruct; + +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/group11/171535320/stackANDqueue/Queue.java b/group11/171535320/stackANDqueue/Queue.java new file mode 100644 index 0000000000..cbd885e73c --- /dev/null +++ b/group11/171535320/stackANDqueue/Queue.java @@ -0,0 +1,34 @@ +package stackANDqueue; + +import java.util.LinkedList; +import java.util.List; + +/** + * Created by dengdechao on 2017/2/22. + */ +public class Queue { + + private LinkedList queue = new LinkedList(); + + public void enQueue(Object o){ + queue.add(o); + } + + public Object deQueue(){ + if(queue.isEmpty()) { + return null; + } + return queue.removeFirst(); + } + + public boolean isEmpty(){ + if(queue.isEmpty()) { + return true; + } + return false; + } + + public int size(){ + return queue.size(); + } +} diff --git a/group11/171535320/stackANDqueue/Stack.java b/group11/171535320/stackANDqueue/Stack.java new file mode 100644 index 0000000000..17ca9d6298 --- /dev/null +++ b/group11/171535320/stackANDqueue/Stack.java @@ -0,0 +1,52 @@ +package stackANDqueue; + +import java.util.ArrayList; + +/** + * Created by dengdechao on 2017/2/22. + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size() == 0) { + return null; + } + Object obj = null; + for(int i = 0; i < elementData.size(); ++i) { + obj = elementData.get(i); + } + int j = 0; + while(j < elementData.size()) { + ++j; + } + elementData.set(j - 1, null); + return obj; + } + + public Object peek(){ + if(elementData.size() == 0) { + return null; + } + Object obj = null; + for(int i = 0; i < elementData.size(); ++i) { + obj = elementData.get(i); + } + + return obj; + } + public boolean isEmpty(){ + if(elementData.size() == 0) { + return true; + } + + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group11/252308879/dataStructure/.gitignore b/group11/252308879/dataStructure/.gitignore new file mode 100644 index 0000000000..c7175f9b40 --- /dev/null +++ b/group11/252308879/dataStructure/.gitignore @@ -0,0 +1,10 @@ +dataStructure/.classpath +dataStructure/.project +dataStructure/.settings/ + +.project +.settings/ +target/ +# 忽略IntelliJ IDEA配置文件 +*.iml +*.idea/ diff --git a/group11/252308879/dataStructure/pom.xml b/group11/252308879/dataStructure/pom.xml new file mode 100644 index 0000000000..0bf21adba4 --- /dev/null +++ b/group11/252308879/dataStructure/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + + org.apn.coding2017 + dataStructure + 1.0.0-SNAPSHOT + jar + + dataStructure + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.12 + test + + + junit + junit + RELEASE + + + dom4j + dom4j + 1.6.1 + + + diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..82d7073a0f --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java @@ -0,0 +1,282 @@ +package org.pan.coding2017.array; + +import java.util.Arrays; + +/** + * Created by QiPan on 2017/2/27. + */ +public class ArrayUtil { + + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + + // 如果是null, 或者长度小于等于1, 直接返回 + if (origin == null || origin.length <= 1) { + return; + } + for (int i = 0; i < origin.length / 2; i++) { + int tmp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = tmp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + + if (oldArray == null) { + return oldArray; + } + int[] newArray = null; + int count = 0; //统计被移出的数组的个数 + for (int i = 0; i < oldArray.length; i++) { + int num = oldArray[i]; + if (num == 0) { + count++; + System.arraycopy(oldArray, i + 1, oldArray, i, oldArray.length - i - 1); + i--; + } + } + if (count == 0) { + newArray = oldArray; + } else { + newArray = new int[oldArray.length - count]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length - count); + } + return newArray; + } + + /** + * 不用JavaAPI来做 + * + * @param oldArray + * @return + */ + public static int[] removeZero_2(int[] oldArray) { + + if (oldArray == null) { + return oldArray; + } + int count = 0; + for (int num : oldArray) { + if (num == 0) { + count++; + } + } + int[] newArray = new int[oldArray.length - count]; + for (int i = 0, j = 0; i < oldArray.length; i++, j++) { + int num = oldArray[i]; + if (num == 0) { + j--; + } else { + newArray[j] = num; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + public static int[] merge(int[] array1, int[] array2) { + //先初始化一个array3,但不是最后返回的数组 + int[] array3 = new int[array1.length + array2.length]; + int i = 0, j = 0; + int array3Size = 0; // 统计实际上合并数组后的大小 + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) {// 如果array1中元素小,则插入到array3中 + array3[array3Size++] = array1[i]; + ++i; + } else if (array1[i] > array2[j]) {//如果array2中元素小,则插入到array3中 + array3[array3Size++] = array2[j]; + ++j; + } else {//否则随便插入一个,但是计数要同时加1 + array3[array3Size++] = array1[i]; + ++i; + ++j; + } + } + + if (i == array1.length) { //如果array1中全部循环完毕了,那么需要去处理array2中剩余的元素 + for (int n = j; n < array2.length; n++) { + array3[array3Size++] = array2[n]; + } + } else if (j == array2.length) {// 如果array2中全部循环完毕,那么需要去处理array1中剩余的元素 + for (int n = i; n < array1.length; n++) { + array3[array3Size++] = array1[n]; + } + } + int[] returnResultArray = new int[array3Size]; + System.arraycopy(array3, 0, returnResultArray, 0, + array3Size); + return returnResultArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + if (oldArray == null) { + oldArray = new int[size]; + } + oldArray = Arrays.copyOf(oldArray, oldArray.length + size); + return oldArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + int[] arrays = new int[max / 2]; + int firstNum = 1; //第一个数字 + int secondNum = 1; // 第二个数字 + int arraySize = 0; + arrays[arraySize++] = 1; // 初始化第一位 + while (secondNum < max) { + arrays[arraySize++] = secondNum; + int tmpNum = secondNum; // 保存第二个数,得会需要付给第一个数 + secondNum = firstNum + secondNum; // 为前两个数之和 + firstNum = tmpNum; // 第一个数,后移 + } + return Arrays.copyOf(arrays, arraySize); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] returnResultArray = new int[max + 1]; + int arraySize = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + returnResultArray[arraySize++] = i; + } + + } + if (arraySize == returnResultArray.length) { + return returnResultArray; + } + return Arrays.copyOf(returnResultArray, arraySize); + } + + private static boolean isPrime(final int number) { + if (number < 2) { + return false; + } + // 因为不可能将一个数除与所有小于它的数字,只要检查到N的平方根就好了。 + // 但直接开根号还有个精度的问题。这个可能会产生误差。 索性将判断条件写成 i*i<=number + for (int i = 2; i * i <= number; i++) { + if (number % i == 0) {//查看所有小于number平方根的数,能够被整除 + return false; + } + } + // 如果一个都没有,那么就是素数 + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] array = new int[max]; + int arraySize = 0; + for (int n = 0; n < max; n++) { + int fac,// 被除的因子 + sum,// 用来统计因子之和 + num;// 除数的因子,中间变量 + for (sum = 1, num = n, fac = 2; fac < num; fac++) { + + if (n % fac == 0) {// 如果余数为0,那么说明有因子 + sum += fac; // 统计因子和 + num = n / fac; // num=等于除数的最大因子 + if (num == fac) // 如果最大和最小相等跳出循环 + break; + sum += num; // 再统计因子 + } + } + + if (sum == n) { //因子和与整数相等,那么就是一个完美数 + if (n != 1) { + System.out.println(n + "是一个完全数,其因子为:"); + } + for (fac = 1; fac < n; fac++) { + if (n % fac == 0) {// 列出所有的因子 + System.out.print(fac + " "); + } + } + System.out.println(); + array[arraySize++] = n; // 放到数组中 + } + } + return Arrays.copyOf(array, arraySize); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (array == null) { + return null; + } + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + str.append(array[i]); + continue; + } + str.append(array[i]).append(seperator); + } + return str.toString(); + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..73dd4b7a5f --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java @@ -0,0 +1,212 @@ +package org.pan.coding2017.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by QiPan on 2017/2/23. + */ +public class ArrayList implements List { + + private int size; + + // 设置默认容量 不可变 + private static final int DEFAULT_CAPACITY = 10; + + private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + + private Object[] elementData; + + // 定义一个默认的空的数组,引用不可变 + private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; + + public ArrayList(int initialCapacity) { + if (initialCapacity > 0) { + this.elementData = new Object[initialCapacity]; + } else if (initialCapacity == 0) { + this.elementData = new Object[]{}; + } else { + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + } + } + + public ArrayList() { // 如果调用默认构造函数,设置容器为空的默认数组 + this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; + } + + + public boolean add(Object o) { + ensureCapacityInternal(size + 1); + elementData[size++] = o; + return true; + } + + @Override + public boolean add(int index, Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + ensureCapacityInternal(size + 1); + // 从插入位置开发,所有的数据需要往后移动 + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + return true; + } + + public Object set(int index, Object element) { + checkIndexOutOf(index); + Object oldEle = elementData[index]; + elementData[index] = element; + return oldEle; + } + + public Object get(int index) { + checkIndexOutOf(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndexOutOf(index); + Object oldValue = elementData[index]; + + // 计算需要移动的位数 + int needMoveNum = size - index - 1; + + if (needMoveNum > 0) { + /* + * src:源数组; + * srcPos:源数组要复制的起始位置; + * dest:目的数组; + * destPos:目的数组放置的起始位置; + * length:复制的长度。 + */ + System.arraycopy(elementData, index + 1, elementData, index, needMoveNum); + } + // 避免对象游离,是的gcc能工作回收 + elementData[--size] = null; + return oldValue; + } + + public int size() { + return this.size; + } + + public boolean isEmpty() { + return this.size == 0; + } + + public Iterator iterator() { + return new Itr(); + } + + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + /** + * 检查数组越界 + * + * @param index + */ + private void checkIndexOutOf(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + private void ensureCapacityInternal(int minCapacity) { + // 如果是容器是默认的空的数组 + if (this.elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { + // 取得为与默认容量相比的较大值 + minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); + } + // 调用确保有能力放下那么多元素 + ensureExplicitCapacity(minCapacity); + } + + private void ensureExplicitCapacity(int minCapacity) { + // 防止容量溢出。所需的最小容器大于数组长度 + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + } + + /** + * 扩充容量 + * + * @param minCapacity + */ + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + // 得到一个新的容量大小,为 oldCapacity 的1.5倍 + int newCapacity = oldCapacity + (oldCapacity >> 1); + // 如果扩容后的大小比,最小容量(DEFAULT_CAPACITY: 10)小 + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } else if (newCapacity - MAX_ARRAY_SIZE > 0) { // 扩容后比最大的还大,考虑溢出 + // + newCapacity = hugeCapacity(minCapacity); + } + + elementData = Arrays.copyOf(elementData, newCapacity); + } + + /** + * 这个很少用到这个判断,毕竟基本不会使用那么大容量存储 + * + * @param minCapacity + * @return + */ + private static int hugeCapacity(int minCapacity) { + if (minCapacity < 0) { + throw new OutOfMemoryError(); + } + return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; + } + + private class Itr implements Iterator { + + /** + * 当前游标 + */ + int cursor; + int lastRet = -1; // 是否是返回最后一个结果 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + // 游标已经超过数组的大小 + if (i >= size) { + throw new NoSuchElementException(); + } + Object[] elementData = ArrayList.this.elementData; + + // 指向下一个元素 + cursor = i + 1; + Object elementDatum = elementData[i]; + lastRet = i; + return elementDatum; + } + + @Override + public void remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + // 实际上这个时候的 lastRet,为 next方法时候的 i = cursor + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + } + } + + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..80f7f78751 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,76 @@ +package org.pan.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +public class BinaryTreeNode, Value> { + + private Node root; + + private class Node { + private Key key; + private Value value; + private Node left, right; //指向子树的链接 + private int N; // 以该节点为根的子树节点总数 + public Node(Key key, Value value, int N) { + this.key = key; + this.value = value; + this.N = N; + } + } + + public int size() { + return size(root); + } + + private int size(Node x) { + if (x == null) return 0; + else return x.N; + } + + public Value get(Key key){ + return get(root, key); + } + + private Value get(Node x, Key key) { + // 如果根节点也是Null 那么返回null + if (x == null){ + return null; + } + // 拿需要查询的key 与 根节点的 key 比较 + int cmp = key.compareTo(x.key); + if (cmp < 0){ // 如果小于0 那么去左子树上查询,并且递归调用 + return get(x.left, key); + }else if (cmp > 0){// 如果大于0 那么去右子树上查询,并且递归调用 + return get(x.right, key); + }else { + return x.value; + } + } + + public void push(Key key, Value value){ + // 查询key, 找到则更新它的值,否则就创建 + root = put(root, key, value); + } + + private Node put(Node x, Key key, Value value) { + // 如果key 存在于以 x 为根节点的子树中 则更新它的值 + // 否则将以key 和 value 为键值对的新节点插入到该子树中 + + if (x == null){ + return new Node(key, value, 1); + } + int cmp = key.compareTo(x.key); + if (cmp < 0 ){ + x.left = put(x.left, key, value); + }else if (cmp > 0){ + x.right = put(x.right, key, value); + }else { // 存在更新值 + x.value = value; + } + // 重新统计节点总数 + x.N = size(x.left) + size(x.right) + 1; + return x; + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..3d1849f1a0 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java @@ -0,0 +1,12 @@ +package org.pan.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +public interface Iterator { + boolean hasNext(); + + Object next(); + + void remove(); +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..015ac3d59d --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java @@ -0,0 +1,121 @@ +package org.pan.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +public class LinkedList { + + private Node head; + private int size; + public LinkedList() { + head = new Node(null, null); + size = 0; + } + + public void add(Object o){ + add(size, o); + } + + public void add(int index , Object o){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node newNode = new Node(o, frontNode.next); + frontNode.next = newNode; + size++; + + } + + private Node getNode(int index) { + Node node = head; + int i = 0; + while(node.next != null && i <= index) { + node = node.next; + i++; + } + return node; + } + + public Object get(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + Node frontNode = getNode(index-1); + Node oldNode = getNode(index); + frontNode.next = oldNode.next; + size--; + return oldNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add(0, o); + } + + 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 LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + int index; + final int capacity = size; + LinkedListIterator() { + index = 0; + } + @Override + public boolean hasNext() { + return index < capacity; + } + + @Override + public Object next() { + return get(index++); + } + + @Override + public void remove() { + + } + } + + private String outOfBoundsMsg(int index) { + return "index:" + index + ", size:" + size; + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java new file mode 100644 index 0000000000..daa8253313 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java @@ -0,0 +1,24 @@ +package org.pan.coding2017.basic; + + +/** + * Created by QiPan on 2017/2/23. + */ +public interface List { + + boolean add(Object o); + + boolean add(int index, Object o); + + Object set(int index, Object element); + + Object get(int index); + + Object remove(int index); + + int size(); + + boolean isEmpty(); + + Iterator iterator(); +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java new file mode 100644 index 0000000000..af478b4288 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java @@ -0,0 +1,50 @@ +package org.pan.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + * 队列,链表实现版本 + */ +public class Queue { + + private Node first; + private Node last; + private int N; + + private class Node { + E item; + Node next; + } + + public void enQueue(E e) { + // 向表尾添加元素 + Node oldLast = last; + last = new Node(); + last.item = e; + last.next = null; + if (isEmpty()){// 如果是往空的队列里面添加东西。那么首尾链表都是指向第一个元素 + first = last; + }else { + + oldLast.next = last; + } + N++; + } + + public E deQueue() { + E item = first.item; + first = first.next; + if (isEmpty()){ + last = null; + } + N--; + return item; + } + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return N; + } +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java new file mode 100644 index 0000000000..918db8f70d --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java @@ -0,0 +1,51 @@ +package org.pan.coding2017.basic; + +/** + * Created by QiPan on 2017/2/23. + */ +public class Stack { + + private E[] elements; + // 记录元素当前位置 + private int N; + + public Stack(int cap) { + elements = (E[]) new Object[cap]; + } + + public boolean isEmpty() { + return N == 0; + } + + public int size() { + return N; + } + + public void push(E e) { + // 如果 N 和数组的长度已经相同,就进行扩容 + if (N == elements.length) { + resize(2 * elements.length); + } + elements[N++] = e; + } + + public E pop() { + E element = elements[--N]; + elements[N] = null;// 避免对象游离 + // 如果元素值剩下容量的1/4,那么就把数组容量变成现在的一半 + if (N > 0 && N == elements.length / 4) { + resize(elements.length / 2); + } + return element; + } + + private void resize(int max) { + E[] elementTmps = (E[]) new Object[max]; + for (int i = 0; i < N; i++) { + elementTmps[i] = elements[i]; + } + // 指向扩容的数组 + elements = elementTmps; + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java new file mode 100644 index 0000000000..2a056b8bbf --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java @@ -0,0 +1,47 @@ +package org.pan.coding2017.basic; + +/** + * Created by Pan on 2017/2/25. + * 栈(链表实现): 下压栈。操作栈顶元素 + */ +public class Stack2 { + + private Node first; + private int N; + + private class Node { + E item; + Node next; + } + + public boolean isEmpty(){ + return first == null; + } + + public int size(){ + return N; + } + + /** + * 向栈顶添加元素 + * @param element + */ + public void push (E element){ + Node oldFirst = first; + first = new Node(); + first.item = element; + first.next = oldFirst; + N++; + } + + /** + * 弹出栈顶元素 + * @return + */ + public E pop(){ + E item = first.item; + first = first.next; + N--; + return item; + } +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java new file mode 100644 index 0000000000..f38cbcb084 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java @@ -0,0 +1,39 @@ +package org.pan.coding2017.parsingXML; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java new file mode 100644 index 0000000000..90c5443c23 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java @@ -0,0 +1,110 @@ +package org.pan.coding2017.parsingXML; + +import org.pan.coding2017.utils.JaxpDomUtil; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + try { + Document document = JaxpDomUtil.getDocument(); + NodeList actionNodeList = document.getElementsByTagName("action"); + for (int i = 0; i < actionNodeList.getLength(); i++) { + NamedNodeMap attributes = actionNodeList.item(i).getAttributes(); + String methodName = attributes.getNamedItem("name").getTextContent(); + if (!actionName.equals(methodName)) { + continue; + } + // 获取全类名对象,反射创建对象 + String className = attributes.getNamedItem("class").getTextContent(); + Class actionClass = Class.forName(className); + + // 获取反射的方法名称, 因为是public修饰所以用的是getMethod,获取私有方法需要用 getDeclaredMethod + Method setName = actionClass.getMethod("setName", String.class); + Method setPassword = actionClass.getMethod("setPassword", String.class); + // 创建对象 + Object actionObject = actionClass.newInstance(); + + // 调用反射的setter方法,给参数赋值 + setName.invoke(actionObject, parameters.get("name")); + setPassword.invoke(actionObject, parameters.get("password")); + + // 获取execute方法 + Method execute = actionClass.getMethod("execute"); + // 返回结果 + String result = (String) execute.invoke(actionObject); + + // 获取getMessage方法 + Method getMessage = actionClass.getMethod("getMessage"); + String message = (String) getMessage.invoke(actionObject); + // 创建一个Map 用来放置在 View中 + Map params = new HashMap(); + params.put("message", message); + + // 获取返回的JSP路径,这个需要比较result节点的name属性 + //获取action的子节点 + NodeList resultNodes = actionNodeList.item(i).getChildNodes(); + String viewUrl = ""; + for (int n = 0; n < resultNodes.getLength(); n++) { + Node item = resultNodes.item(n); + NamedNodeMap resultAttributes = item.getAttributes(); + if (resultAttributes == null) { + continue; + } + String name = resultAttributes.getNamedItem("name").getTextContent(); + if (result.equals(name)) { + viewUrl = item.getTextContent(); + break; + } + } + View view = new View(); + view.setJsp(viewUrl); + view.setParameters(params); + return view; + } + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return null; + + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java new file mode 100644 index 0000000000..ccdacc514c --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java @@ -0,0 +1,43 @@ +package org.pan.coding2017.parsingXML; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java new file mode 100644 index 0000000000..3271ab4ed1 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java @@ -0,0 +1,23 @@ +package org.pan.coding2017.parsingXML; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java new file mode 100644 index 0000000000..f1679f6bef --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java @@ -0,0 +1,41 @@ +package org.pan.coding2017.utils; + +import java.io.FileWriter; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; + +public class Dom4JUtil { + + public static Document getDocument(String xmlPath) { + + try { + //创建解析器 + SAXReader saxReader = new SAXReader(); + //得到Documment + Document document = saxReader.read(xmlPath); + return document; + } catch (DocumentException e) { + e.printStackTrace(); + } + + return null; + } + + public static void xmlWrite(Document document,String xmlPath){ + + try { + OutputFormat format = OutputFormat.createPrettyPrint(); + XMLWriter xmlWriter = new XMLWriter(new FileWriter(xmlPath),format); + xmlWriter.write(document); + xmlWriter.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java new file mode 100644 index 0000000000..d8fe537e35 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java @@ -0,0 +1,152 @@ +package org.pan.coding2017.utils; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class JaxpDomUtil { + public static final String XMLPATH = JaxpDomUtil.class.getClassLoader().getResource("struts.xml").getPath(); + + /** + * 通过 解析器 获取到 Document + * @return + */ + public static Document getDocument() { + try { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory + .newDocumentBuilder(); + Document document = documentBuilder.parse(XMLPATH); + return document; + } catch (Exception e) { + e.printStackTrace(); + + } + return null; + } + + /** + * 回写 XML 方法 + * @param document + */ + public static void tranFormMethod(Document document) { + try { + TransformerFactory transformerFactory = TransformerFactory + .newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.transform(new DOMSource(document), new StreamResult( + XMLPATH)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 递归调用 获取所有的元素 并打印元素名称 + * @param node + */ + private static void listElement(Node node) { + // 判断是元素类型时才打印 + if (node.getNodeType() == Node.ELEMENT_NODE) { + System.out.println(node.getNodeName()); + } + + // 获得一层子节点 + NodeList nodelist = node.getChildNodes(); + for (int i = 0; i < nodelist.getLength(); i++) { + // 得到每一个子节点 + Node nodeChild = nodelist.item(i); + + // 递归调用 + listElement(nodeChild); + } + + } + + /** + * 获取所有的 元素名称 + */ + public static void getListElement() { + + Document document = JaxpDomUtil.getDocument(); + listElement(document); + } + + /** + * 删除nan节点 + */ + public static void delSex() { + + Document document = JaxpDomUtil.getDocument(); + + // 获取元素 + Node nodeSex = document.getElementsByTagName("sex").item(0); + + // 得到父节点 + Node parent = nodeSex.getParentNode(); + + // 通过父节点删除 + parent.removeChild(nodeSex); + + // 回写XML + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 修改 sex 标签的 内容为nv + */ + public static void modifySex() { + Document document = JaxpDomUtil.getDocument(); + Node nodeSex = document.getElementsByTagName("sex").item(0); + nodeSex.setTextContent("nv"); + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 为第一个p1 增加 nv + */ + public static void addSex(){ + Document document = JaxpDomUtil.getDocument(); + Node p1Node = document.getElementsByTagName("p1").item(0); + + //通过 Document 创建 Element + Element sexElement = document.createElement("sex"); + sexElement.setTextContent("nv"); + p1Node.appendChild(sexElement); + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 查询xml中第一个name元素的值 + */ + public static void selectSin(){ + Document document = JaxpDomUtil.getDocument(); + Node nameNode = document.getElementsByTagName("name").item(0); + String name = nameNode.getTextContent(); + System.out.println(name); + } + + /** + * 查询所有name元素的值 + */ + public static void selectAll(){ + Document document = JaxpDomUtil.getDocument(); + NodeList nodeList = document.getElementsByTagName("name"); + for (int i = 0; i < nodeList.getLength(); i++) { + System.out.println(nodeList.item(i).getTextContent()); + } + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java new file mode 100644 index 0000000000..8756c8ab95 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java @@ -0,0 +1,89 @@ +package org.pan.coding2017.utils; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +public class JaxpSAXPUtil { + public static void main(String[] args) { + /* + * 1、创建解析器工厂 + * 2、创建解析器 + * 3、执行 parse 方法 + * + * 4、自己创建一个类、继承DefaultHandler + * 5、重写类里面的三个方法 + */ + + try { + SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); + SAXParser saxParser = saxParserFactory.newSAXParser(); + saxParser.parse("src/p1.xml",new MyDeafultHandler2() ); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} + +class MyDeafultHandler1 extends DefaultHandler{ + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + System.out.println("<"+qName+">"); + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + System.out.println(new String(ch,start,length)); + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + System.out.println("<"+qName+"/>"); + } + +} + +//实现获取所有的name元素的值 +class MyDeafultHandler2 extends DefaultHandler{ + + boolean flag = false; + int index = 1; + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + //判断qName 是否为 name 元素 + if("name".equals(qName) && index == 2){ + flag = true; + } + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + if(flag == true){ + System.out.println(new String(ch, start, length)); + } + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + if("name".equals(qName)){ + flag = false; + index++ ; + } + } + + +} diff --git a/group11/252308879/dataStructure/src/main/resources/struts.xml b/group11/252308879/dataStructure/src/main/resources/struts.xml new file mode 100644 index 0000000000..35830922ba --- /dev/null +++ b/group11/252308879/dataStructure/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java new file mode 100644 index 0000000000..281a5bf07b --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java @@ -0,0 +1,31 @@ +package org.pan.coding2017; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by QiPan on 2017/2/23. + */ +public class TestJavaUtilArrayList { + + + @Test + public void testAdd() { + List arrayList = new ArrayList(5); + arrayList.add(new Object()); + System.out.println("sssssssssssss"); + } + + @Test + public void testRightShift() { + int x = 5; + + x = x << 1; + x = x >> 1; + System.out.println(x); + } + +} diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java new file mode 100644 index 0000000000..29ab6bf6dc --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java @@ -0,0 +1,89 @@ +package org.pan.coding2017.array; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by QiPan on 2017/2/27. + */ +public class ArrayUtilTest { + + @Test + public void removeZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println("removeZero 移除0之前: "+ Arrays.toString(oldArr)); + int[] newArrays = ArrayUtil.removeZero(oldArr); + System.out.println("removeZero 移除0之后: "+ Arrays.toString(newArrays)); + } + + @Test + public void removeZero_2() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println("removeZero_2 移除0之前: "+ Arrays.toString(oldArr)); + int[] newArrays = ArrayUtil.removeZero_2(oldArr); + System.out.println("removeZero_2 移除0之后: "+ Arrays.toString(newArrays)); + } + + @Test + public void reverseArray() throws Exception { + int[] array = new int[]{7, 9 , 30, 3}; + int[] array2 = new int[] {7, 9, 30, 3, 4}; + System.out.println("置换前: " + Arrays.toString(array)); + ArrayUtil.reverseArray(array); + System.out.println("置换后: "+ Arrays.toString(array)); + System.out.println("置换前: " + Arrays.toString(array2)); + ArrayUtil.reverseArray(array2); + System.out.println("置换后: "+ Arrays.toString(array2)); + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7,8}, a2 = {4, 5, 6,7}; + //则 a3 为[3,4,5,6,7,8] + int[] merge = ArrayUtil.merge(a1, a2); + System.out.println(Arrays.toString(merge)); + } + + @Test + public void grow() throws Exception { + int[] oldArray = {2,3,6} ; + int size = 3; + System.out.println("grow 之前:"+ Arrays.toString(oldArray)); + int[] newArrays = ArrayUtil.grow(oldArray, size); + System.out.println("grow 之后:"+ Arrays.toString(newArrays)); + + } + + @Test + public void fibonacci() throws Exception { + //max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + int[] fibonacci = ArrayUtil.fibonacci(988); + System.out.println(Arrays.toString(fibonacci)); + } + + @Test + public void getPrimes() throws Exception { + //例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + int[] primes = ArrayUtil.getPrimes(23); + System.out.println(Arrays.toString(primes)); + } + + @Test + public void getPerfectNumbers() throws Exception { + int[] primes = ArrayUtil.getPerfectNumbers(10000); + System.out.println(Arrays.toString(primes)); + } + + @Test + public void join() throws Exception { + int [] array= {3,8,9}; + String seperator = "-"; + String result = ArrayUtil.join(array, seperator); + System.out.println(result); + } + + + + +} \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java new file mode 100644 index 0000000000..f93876522a --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java @@ -0,0 +1,74 @@ +package org.pan.coding2017.basic; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Pan on 2017/2/26. + */ +public class ArrayListTest { + + ArrayList arrayList; + + @Before + public void before(){ + arrayList = new ArrayList(); + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + } + + @Test + public void add() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add(3); + System.out.println(arrayList); + } + + @Test + public void set() throws Exception { + arrayList.add(3); + arrayList.set(0, 4); + System.out.println(arrayList); + } + + @Test + public void get() throws Exception { + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + Object o = arrayList.get(1); + System.out.println(o); + } + + @Test + public void remove() throws Exception { + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + arrayList.remove(1); + System.out.println(arrayList); + } + + @Test + public void size() throws Exception { + System.out.println(arrayList.size()); + } + + @Test + public void isEmpty() throws Exception { + System.out.println(arrayList.isEmpty()); + } + + @Test + public void iterator() throws Exception { + Iterator iterator = arrayList.iterator(); + while (iterator.hasNext()){ + Object next = iterator.next(); + System.out.println(next); + iterator.remove(); + } + System.out.println(arrayList.isEmpty()); + } + +} \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..4c3c01cd73 --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java @@ -0,0 +1,38 @@ +package org.pan.coding2017.basic; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Pan on 2017/2/26. + */ +public class BinaryTreeNodeTest { + + BinaryTreeNode binaryTreeNode; + + @Before + public void setUp() throws Exception { + binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode.push(1, "A"); + binaryTreeNode.push(2, "B"); + binaryTreeNode.push(3, "C"); + binaryTreeNode.push(4, "D"); + } + + @Test + public void size() throws Exception { + System.out.println(binaryTreeNode.size()); + } + + @Test + public void get() throws Exception { + System.out.println(binaryTreeNode.get(3)); + } + + @Test + public void push() throws Exception { + binaryTreeNode.push(5, "E"); + System.out.println(binaryTreeNode); + } + +} \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java new file mode 100644 index 0000000000..d10e2a1d48 --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java @@ -0,0 +1,80 @@ +package org.pan.coding2017.basic; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Pan on 2017/2/26. + */ +public class LinkedListTest { + + LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + linkedList.add(0); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + } + + @Test + public void add() throws Exception { + linkedList.add(0); + System.out.println(linkedList); + } + + @Test + public void get() throws Exception { + Object o = linkedList.get(1); + System.out.println(o); + } + + @Test + public void remove() throws Exception { + linkedList.remove(1); + System.out.println(linkedList); + } + + @Test + public void size() throws Exception { + System.out.println(linkedList.size()); + } + + @Test + public void addFirst() throws Exception { + linkedList.addFirst(4); + System.out.println(linkedList); + } + + @Test + public void addLast() throws Exception { + linkedList.addLast(5); + System.out.println(linkedList); + } + + @Test + public void removeFirst() throws Exception { + linkedList.removeFirst(); + System.out.println(linkedList); + } + + @Test + public void removeLast() throws Exception { + linkedList.removeLast(); + System.out.println(linkedList); + } + + @Test + public void iterator() throws Exception { + Iterator iterator = linkedList.iterator(); + while (iterator.hasNext()){ + Object next = iterator.next(); + System.out.println(next); + iterator.remove(); + } + System.out.println(linkedList); + } + +} \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java new file mode 100644 index 0000000000..c720e7d95e --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java @@ -0,0 +1,44 @@ +package org.pan.coding2017.basic; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Pan on 2017/2/26. + */ +public class QueueTest { + + Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + + } + + @Test + public void enQueue() throws Exception { + queue.enQueue(1); + System.out.println(queue); + } + + @Test + public void deQueue() throws Exception { + queue.deQueue(); + System.out.println(queue); + } + + @Test + public void isEmpty() throws Exception { + System.out.println(queue.isEmpty()); + } + + @Test + public void size() throws Exception { + System.out.println(queue.size()); + } + +} \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java new file mode 100644 index 0000000000..df85b797d4 --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java @@ -0,0 +1,45 @@ +package org.pan.coding2017.basic; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Pan on 2017/2/26. + */ +public class StackTest { + + Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(3); + stack.push(1); + stack.push(2); + stack.push(3); + } + + @Test + public void isEmpty() throws Exception { + System.out.println(stack.isEmpty()); + } + + @Test + public void size() throws Exception { + System.out.println(stack.size()); + } + + @Test + public void push() throws Exception { + stack.push(1); + stack.push(2); + stack.push(3); + System.out.println(stack); + } + + @Test + public void pop() throws Exception { + stack.pop(); + System.out.println(stack); + } + +} \ No newline at end of file diff --git a/group11/283091182/src/com/coderising/array/ArrayUtil.java b/group11/283091182/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..93c43d9500 --- /dev/null +++ b/group11/283091182/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,303 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if(origin==null||origin.length==0){ + throw new RuntimeException("invalid array argument!"); + } + if(origin.length>1){ + int temp; + for(int i=0;iarray2[pos2]){ + array3[pos3]=array2[pos2]; + pos2++; + pos3++; + }else if(array1[pos1]3){ + result[pos]=2; + pos++; + } + if(max>4){ + result[pos]=3; + pos++; + } + for(int i=4;i0){ + sb.append(seperator); + } + sb.append(i); + } + return sb.toString(); + } + + private void printArray(String msg,int[] array){ + System.out.print(msg); + for(int i=0;i resultMap= new HashMap(); + public static final String CONST_ACTION = "action"; + public static final String CONST_NAME = "name"; + public static final String CONST_CLASS = "class"; + public static final String CONST_RESULT = "result"; + + private String actionName; + private String actionClass; + + public Action(){}; + + public Action(String actionName,String actionClass){ + this.actionName = actionName; + this.actionClass = actionClass; + } + + public void setActionResultJsp(String result,String dispatcherJsp){ + this.resultMap.put(result, dispatcherJsp); + } + + public String getActionResultJsp(String result){ + return this.resultMap.get(result); + } + + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public String getActionClass() { + return actionClass; + } + + public void setActionClass(String actionClass) { + this.actionClass = actionClass; + } + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append(CONST_ACTION).append(":").append(this.actionName).append(","); + sb.append(CONST_CLASS).append(":").append(this.actionClass).append(","); + sb.append("ResultMap").append(":").append(this.resultMap.toString()); + return sb.toString(); + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/LoginAction.java b/group11/283091182/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/Struts.java b/group11/283091182/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..dbe4691938 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,229 @@ +package com.coderising.litestruts; + +import java.beans.BeanInfo; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + * + */ + + //0. Load Structs.xml and return a map + Map actionMap = loadActionMap(); + + //1.Find and initialize action instance to actionName provided + Action action = actionMap.get(actionName); + Object instance = initializeActionInstance(action.getActionClass(),parameters); + + //2.invoke the execute method and get the result + String result = executeAction(instance); + + //3.Extract info for view + View view = new View(); + view.setParameters(extractInfo(instance)); + + //4.set dispatcher jsp according to execution result + view.setJsp(action.getActionResultJsp(result)); + + return view; + } + + private static Map loadActionMap() { + try { + + InputStream is = Struts.class.getResourceAsStream("struts.xml"); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(is); + Element struct = document.getDocumentElement(); + + return getActions(struct); + + } catch (IOException e) { + throw new RuntimeException("Error Reading Configuration XML",e); + } catch (ParserConfigurationException e) { + throw new RuntimeException("Error Parsing Configuration XML",e); + } catch (SAXException e) { + throw new RuntimeException("Error Parsing Configuration XML",e); + } + } + + /** + * Parse the XML and construct ActionName:Action map + * + * @param Element struct, root of the struct xml + * @return Map + */ + private static Map getActions(Element struct) { + + Map map = new HashMap(); + + NodeList nl = struct.getElementsByTagName(Action.CONST_ACTION); + + for (int i = 0; i < nl.getLength(); i++) + { + Node actionNode = nl.item(i); + // Get action name and corresponding action class from property + String actionClass = getAttribute(actionNode,Action.CONST_CLASS); + String actionName = getAttribute(actionNode,Action.CONST_NAME); + + Action action = new Action(actionName, actionClass); + + // get results under action + NodeList childNodes = actionNode.getChildNodes(); + + for (int j = 0; j < childNodes.getLength(); j++) + { + Node result = childNodes.item(j); + //Only accept if Node Type is element and Node name is "result" + if ((result.getNodeType() == result.ELEMENT_NODE) + && (result.getNodeName() == Action.CONST_RESULT)) + { + String resultName = getAttribute(result,Action.CONST_NAME); + String dispatcherJsp = result.getTextContent(); + action.setActionResultJsp(resultName, dispatcherJsp); + } + } + map.put(action.getActionName(), action); + } + System.out.println(map); + return map; + } + /** + * Get property from given node + * @param node + * @param key + * @return attribute as String + */ + private static String getAttribute(Node node,String key){ + NamedNodeMap map = node.getAttributes(); + Node attriNode = map.getNamedItem(key); + if(attriNode!=null && attriNode.getNodeType()==Node.ATTRIBUTE_NODE){ + return attriNode.getNodeValue(); + } + return null; + } + + /** + * Initialize instance from given class name and parameters map + * @param actionClass + * @param parameters + * @return instance of specified class + */ + private static Object initializeActionInstance(String actionClass,Map parameters){ + try { + Class clazz= Class.forName(actionClass); + //Instantiate by calling constructor + Constructor constructor = clazz.getConstructor(); + + constructor.setAccessible(true); + Object instance = constructor.newInstance(new Object[]{}); + + //Check class propertes with instrospector + BeanInfo beanInfo = Introspector.getBeanInfo(clazz); + PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); + + for(PropertyDescriptor prop:props){ + String propName = prop.getName(); + Method propSetter = prop.getWriteMethod(); + //If there is a setter for the property and also there is a value in parameter map + //then invoke the setter method to set the values + if(propSetter!=null && parameters.containsKey(propName)) + { + propSetter.invoke(instance, parameters.get(propName)); + } + } + + return instance; + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Error initializing instance: ClassName="+actionClass,e); + } + } + + + /** + * Invoke the "execute" method from the action instance + * @param Action instance + * @return execute result as String + */ + private static String executeAction(Object instance){ + Class clazz = instance.getClass(); + try { + //exepct no argument for execute method + Method execute = clazz.getMethod("execute", new Class[0]); + return (String)execute.invoke(instance, new Object[0]); + } catch (Exception e) { + throw new RuntimeException("Error executing action,class name="+clazz.getCanonicalName()); + } + } + + + /** + * Extracting Bean info by calling the getting method in the Action instance + * @param instance + * @return map + */ + private static Map extractInfo(Object instance){ + Map map = new HashMap(); + Class clazz = instance.getClass(); + try{ + Method[] methods = clazz.getMethods(); + for(Method method:methods) + { + String methodName = method.getName(); + if(methodName.startsWith("get")&&method.getParameterTypes().length==0) + { + Object methodReturn = method.invoke(instance, new Object[0]); + //construct the properties name by getter method name,first character toLower case + String propName = methodName.replaceFirst("get", ""); + char[] propNameCharArr = propName.toCharArray(); + propNameCharArr[0]=Character.toLowerCase(propNameCharArr[0]); + + map.put(String.valueOf(propNameCharArr), methodReturn); + } + } + + }catch(Exception e){ + throw new RuntimeException("Error extracting info from Action Insance,class="+clazz.getCanonicalName(),e); + } + return map; + } + +} diff --git a/group11/283091182/src/com/coderising/litestruts/StrutsTest.java b/group11/283091182/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/View.java b/group11/283091182/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/struts.xml b/group11/283091182/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..4c6eeabbd4 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/283091182/src/com/coding/basic/ArrayList.java b/group11/283091182/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..402d05c019 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/ArrayList.java @@ -0,0 +1,113 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private static final int GROW_BY_SIZE = 10; + + private Object[] elementData = new Object[GROW_BY_SIZE]; + + public void add(Object o){ + if(size == elementData.length){ + grow(); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + validate(index); + if(size == elementData.length){ + grow(); + } + for(int i=size;i>index+1;i--){ + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + size++; + } + + public Object get(int index){ + validate(index); + return elementData[index]; + } + + public Object remove(int index){ + validate(index); + Object result = elementData[index]; + for(int i =index;i=size)throw new IndexOutOfBoundsException("Invalid Index:"+pos); + Object result = elementData[pos]; + pos++; + return result; + } + + + } + + private void grow(){ + elementData = Arrays.copyOf(elementData, elementData.length+GROW_BY_SIZE); + } + private void validate(int index){ + if(index<0||index>=size)throw new IndexOutOfBoundsException("Invalid Index:"+index); + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder("["); + for(int i=0;i1)sb.append(","); + sb.append(elementData[i]); + } + sb.append("]size=").append(this.size()); + return sb.toString(); + } + + public static void main(String[] args){ + ArrayList l = new ArrayList(); + for(int i=0;i<12;i++){ + l.add(i+""); + } + System.out.println(l); + l.add("aaa"); + System.out.println("After adding aaa:"+l); + l.add(2,"bbb"); + System.out.println("After adding bbb:"+l); + System.out.println(l.get(2)); + System.out.println("After getting:"+l); + System.out.println(l.remove(2)); + System.out.println("After removing:"+l); + Iterator it = l.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + } + +} diff --git a/group11/283091182/src/com/coding/basic/ArrayListTest.java b/group11/283091182/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..7807fa831e --- /dev/null +++ b/group11/283091182/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,142 @@ +/** + * + */ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Administrator + * + */ +public class ArrayListTest { + + private ArrayList al; + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + System.out.println("SetUp"); + al= new ArrayList(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + System.out.println("TearDown"); + al = null; + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(java.lang.Object)}. + */ + @Test + public final void testAddObject() { + al.add("aaa"); + al.add("bbb"); + al.add("ccc"); + assertEquals("aaa",al.get(0)); + assertEquals("bbb",al.get(1)); + assertEquals("ccc",al.get(2)); + assertEquals(3,al.size()); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test + public final void testAddIntObject() { + al.add("aaa"); + al.add(0,"bbb"); + al.add(1,"ccc"); + assertEquals("bbb",al.get(0)); + assertEquals("ccc",al.get(1)); + assertEquals("aaa",al.get(2)); + assertEquals(3,al.size()); + } + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test(expected=IndexOutOfBoundsException.class) + public final void testAddIntObjectWithException1() { + al.add(-1, "aaa"); + } + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test(expected=IndexOutOfBoundsException.class) + public final void testAddIntObjectWithException2() { + al.add("aaa"); + al.add(1,"bbb"); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGet() { + fail("Not yet implemented"); // TODO + } + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGetWithException1() { + fail("Not yet implemented"); // TODO + } + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGetWithException2() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#remove(int)}. + */ + @Test + public final void testRemove() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#size()}. + */ + @Test + public final void testSize() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#iterator()}. + */ + @Test + public final void testIterator() { + fail("Not yet implemented"); // TODO + } + +} diff --git a/group11/283091182/src/com/coding/basic/BinaryTreeNode.java b/group11/283091182/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..7f2a030983 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,89 @@ +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(Object o,BinaryTreeNode leftChild,BinaryTreeNode rightChild){ + this.data = o; + this.left = leftChild; + this.right = rightChild; + } + public BinaryTreeNode insert(Object o){ + if(!(o instanceof Comparable)){ + throw new RuntimeException("Incompareable Oject:"+o); + } + System.out.println("CurrentNode Value="+data); + if(((Comparable)o).compareTo(data)>0){ + System.out.print(o+" is greater than "+data+",insert to right;"); + if(this.right==null){ + System.out.println("Creating new rightChild;"); + this.right = new BinaryTreeNode(o,null,null); + }else{ + System.out.println("rightChild exists,Conitnue to insert to rightChild"); + this.right.insert(o); + } + } + if(((Comparable)o).compareTo(data)<0){ + System.out.print(o+" is less than "+data+",insert to left;"); + if(this.left==null){ + System.out.println("Creating new leftChild;"); + this.left = new BinaryTreeNode(o,null,null); + }else{ + System.out.println("leftChild exists,Conitnue to insert to leftChild"); + this.left.insert(o); + } + } + return this; + } + + public static void main(String[] args){ + Integer one = new Integer(1); + Integer two = new Integer(2); + System.out.println(one.compareTo(two)); + System.out.println(two.compareTo(one)); + System.out.println(one.compareTo(one)); + BinaryTreeNode btn = new BinaryTreeNode(new Integer(5),null,null); + btn.insert(new Integer(2)); + btn.insert(new Integer(7)); + btn.insert(new Integer(1)); + btn.insert(new Integer(6)); + inOrderTraversal(btn); + btn.insert(new Integer(4)); + btn.insert(new Integer(8)); + inOrderTraversal(btn); + } + //in-order traversal to print all nodes in sorted order + private static void inOrderTraversal(BinaryTreeNode btn){ + if(btn!=null){ + if(btn.left!=null){ + inOrderTraversal(btn.left); + } + System.out.println(btn.data); + if(btn.right!=null){ + inOrderTraversal(btn.right); + } + } + } + +} diff --git a/group11/283091182/src/com/coding/basic/Iterator.java b/group11/283091182/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coding/basic/LinkedList.java b/group11/283091182/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..233c243130 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/LinkedList.java @@ -0,0 +1,178 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head = null; + private int size = 0; + + public void add(Object o){ + if(head==null){ + head = new Node(o); + }else{ + Node temp = head; + while(temp.hasNext()){ + temp = temp.next; + } + temp.next = new Node(o); + } + size++; + + } + public void add(int index , Object o){ + validate(index); + if(index==0){ + Node newNode = new Node(o,head); + head = newNode; + }else{ + Node temp = head; + for(int i=0;i=size)throw new IndexOutOfBoundsException("Invalid Index:"+index); + } + + private static class Node{ + Object data; + Node next; + public boolean hasNext(){ + return (this.next!=null); + } + public Node(Object data,Node next){ + this.data = data; + this.next = next; + } + public Node(Object data){ + this.data = data; + this.next = null; + } + } + @Override + public String toString(){ + StringBuilder sb = new StringBuilder("["); + Node temp = head; + while(temp!=null){ + if(sb.length()>1)sb.append(","); + sb.append(temp.data); + temp = temp.next; + } + sb.append("]size=").append(this.size()); + return sb.toString(); + } + public static void main(String[] args){ + LinkedList l = new LinkedList(); + for(int i=0;i<12;i++){ + l.add(i+""); + } + System.out.println(l); + l.add("aaa"); + System.out.println("After adding aaa:"+l); + l.add(2,"bbb"); + System.out.println("After adding bbb:"+l); + System.out.println(l.get(2)); + System.out.println("After getting:"+l); + System.out.println(l.remove(2)); + System.out.println("After removing:"+l); + l.addFirst("first"); + System.out.println("After add First:"+l); + l.addLast("last"); + System.out.println("After add Last:"+l); + System.out.println(l.removeFirst()); + System.out.println("After remove First:"+l); + System.out.println(l.removeLast()); + System.out.println("After remove Last:"+l); + Iterator it = l.iterator(); + while(it.hasNext()){ + System.out.println(it.next()); + } + //it.next(); + } +} diff --git a/group11/283091182/src/com/coding/basic/List.java b/group11/283091182/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coding/basic/Queue.java b/group11/283091182/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..45fea2a118 --- /dev/null +++ b/group11/283091182/src/com/coding/basic/Queue.java @@ -0,0 +1,42 @@ +package com.coding.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + if(list.size()==0)throw new RuntimeException("Queue is empty."); + return list.removeFirst(); + } + + public boolean isEmpty(){ + return (list.size()==0); + } + + public int size(){ + return list.size(); + } + + @Override + public String toString(){ + return this.list.toString(); + } + + public static void main(String[] args){ + Queue q = new Queue(); + q.enQueue("aaa"); + q.enQueue("bbb"); + System.out.println(q); + System.out.println(q.isEmpty()); + System.out.println(q.size()); + q.deQueue(); + q.deQueue(); + System.out.println(q); + System.out.println(q.isEmpty()); + System.out.println(q.size()); + //q.deQueue(); + } +} diff --git a/group11/283091182/src/com/coding/basic/Stack.java b/group11/283091182/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..915d173b1b --- /dev/null +++ b/group11/283091182/src/com/coding/basic/Stack.java @@ -0,0 +1,48 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size()==0)throw new RuntimeException("Stack is empty."); + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + if(elementData.size()==0)throw new RuntimeException("Stack is empty."); + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return (elementData.size()==0); + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString(){ + return elementData.toString(); + } + + public static void main(String[] args){ + Stack s = new Stack(); + s.push("aaa"); + s.push("bbb"); + s.push("ccc"); + System.out.println(s); + System.out.println(s.isEmpty()); + System.out.println(s.size()); + System.out.println(s.peek()); + System.out.println(s.pop()); + System.out.println(s.pop()); + System.out.println(s.pop()); + System.out.println(s); + System.out.println(s.isEmpty()); + System.out.println(s.size()); + //System.out.println(s.pop()); + } +} diff --git a/group11/395443277/.gitignore b/group11/395443277/.gitignore new file mode 100644 index 0000000000..e02fdb6730 --- /dev/null +++ b/group11/395443277/.gitignore @@ -0,0 +1,111 @@ + + +.metadata + +bin/ + +tmp/ + +*.tmp + +*.bak + +*.swp + +*~.nib + +local.properties + +.settings/ + +.loadpath + +.recommenders + + + +# Eclipse Core + +.project + + + +# External tool builders + +.externalToolBuilders/ + + + +# Locally stored "Eclipse launch configurations" + +*.launch + + + +# PyDev specific (Python IDE for Eclipse) + +*.pydevproject + + + +# CDT-specific (C/C++ Development Tooling) + +.cproject + + + +# JDT-specific (Eclipse Java Development Tools) + +.classpath + + + +# Java annotation processor (APT) + +.factorypath + + + +# PDT-specific (PHP Development Tools) + +.buildpath + + + +# sbteclipse plugin + +.target + + + +# Tern plugin + +.tern-project + + + +# TeXlipse plugin + +.texlipse + + + +# STS (Spring Tool Suite) + +.springBeans + + + +# Code Recommenders + +.recommenders/ + + + +# Scala IDE specific (Scala & Java development for Eclipse) + +.cache-main + +.scala_dependencies + +.worksheet \ No newline at end of file diff --git a/group11/395443277/src/com/coderising/array/ArrayUtil.java b/group11/395443277/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..d73df85b05 --- /dev/null +++ b/group11/395443277/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,254 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int first = 0; + int last = origin.length - 1; + + while (first < last) { + int temp; + temp = origin[first]; + origin[first] = origin[last]; + origin[last] = temp; + + first++; + last--; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + // count how many zeros + int zeroCount = 0; + int len= oldArray.length; + + if (len==0) { + return new int[]{}; + } + + for (int i=0; i parameters) { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + * + */ + + // create a new DocumentBuilderFactory + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + try { + // use the factory to create a documentbuilder + DocumentBuilder builder = factory.newDocumentBuilder(); + InputStream istream = Struts.class.getResourceAsStream("struts.xml"); + Document doc = builder.parse(istream); + + // find jsp page based on the result from execute result + Element element = doc.getDocumentElement(); + NodeList actionNodeList = element.getElementsByTagName("action"); + + for (int i = 0; i < actionNodeList.getLength(); i++) { + NamedNodeMap actionNodeAttr = actionNodeList.item(i).getAttributes(); + + if (actionNodeAttr.getNamedItem("name").getTextContent().equals(actionName)) { + String className = actionNodeAttr.getNamedItem("class").getTextContent(); + + Class cls = Class.forName(className); + Object obj = cls.newInstance(); + + // set name and password + Method setName = cls.getDeclaredMethod("setName", String.class); + Method setPassword = cls.getDeclaredMethod("setPassword", String.class); + setName.invoke(obj, parameters.get("name")); + setPassword.invoke(obj, parameters.get("password")); + + // execute + Method execute = cls.getDeclaredMethod("execute"); + String executeResult = (String) execute.invoke(obj); + + // get message and jsp + Method getMessage = cls.getDeclaredMethod("getMessage"); + String msg = (String) getMessage.invoke(obj); + Map params = new HashMap(); + params.put("message",msg); + + // check result nodes + NodeList resultNodes = actionNodeList.item(i).getChildNodes(); + + String jsp = ""; + for (int j=0; j viewCls = Class.forName("com.coderising.litestruts.View"); + View viewObj = (View) viewCls.newInstance(); + Method setParameters = viewCls.getDeclaredMethod("setParameters", Map.class); + setParameters.invoke(viewObj, params); + Method setJsp = viewCls.getDeclaredMethod("setJsp", String.class); + setJsp.invoke(viewObj, jsp); + + return viewObj; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group11/395443277/src/com/coderising/litestruts/StrutsTest.java b/group11/395443277/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group11/395443277/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/395443277/src/com/coderising/litestruts/View.java b/group11/395443277/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group11/395443277/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/395443277/src/com/coderising/litestruts/struts.xml b/group11/395443277/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..7f8d558286 --- /dev/null +++ b/group11/395443277/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/395443277/src/com/coding/basic/ArrayList.java b/group11/395443277/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..93a2781f3b --- /dev/null +++ b/group11/395443277/src/com/coding/basic/ArrayList.java @@ -0,0 +1,82 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[4]; + + public void add(Object o){ + if (size == elementData.length) { + // double size + doubleSize(); + } + + elementData[size] = o; + size++; + } + + private void doubleSize() { + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + + public void add(int index, Object o){ + // check size + if (size == elementData.length) { + doubleSize(); + } + + //shift and add element + System.arraycopy(elementData, index, elementData, index+1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + if (size == 0) { + return null; + } + + // remove element and shift + Object target = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size - index - 1); + + // reset last element + elementData[size-1] = null; + size--; + return target; + } + + public int size(){ + return size; + } + + public Iterator iterator (){ + return new SeqIterator(); + } + + private class SeqIterator implements Iterator { + int i = 0; + + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + if (!hasNext()) { + return null; + } + return elementData[i++]; + } + + } + +} diff --git a/group11/395443277/src/com/coding/basic/ArrayListTest.java b/group11/395443277/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..75bae320f4 --- /dev/null +++ b/group11/395443277/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,84 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void testAddObject() { + ArrayList list = new ArrayList(); + list.add(5); + assertEquals(5, list.get(0)); + + list.add(4); + list.add(3); + list.add(2); + list.add(1); + assertEquals(1, list.get(4)); + + // size equals to 5 + assertEquals(5, list.size()); + } + + @Test + public void testAddIntObject() { + ArrayList list = new ArrayList(); + list.add(5); + list.add(4); + list.add(3); + list.add(2); + list.add(1); + + // change position 2 element + list.add(2, 10); + + // pos 2 has 10 + assertEquals(10, list.get(2)); + + // last element is 1 + assertEquals(1, list.get(5)); + + // size is 6 + assertEquals(6, list.size()); + } + + @Test + public void testRemove() { + ArrayList list = new ArrayList(); + list.add(5); + list.add(4); + list.add(3); + list.add(2); + list.add(1); + + Object removed = list.remove(2); + assertEquals(removed, 3); + + assertEquals(2, list.get(2)); + assertEquals(4, list.size()); + assertEquals(null, list.get(4)); + + list.add(6); + assertEquals(6, list.get(4)); + } + + @Test + public void testIterator() { + ArrayList list = new ArrayList(); + list.add(5); + list.add(4); + list.add(3); + list.add(2); + list.add(1); + + Iterator it = list.iterator(); + if(it.hasNext()) { + assertEquals(5, it.next()); + assertEquals(4, it.next()); + } + + } + +} diff --git a/group11/395443277/src/com/coding/basic/BinaryTreeNode.java b/group11/395443277/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..907fc23275 --- /dev/null +++ b/group11/395443277/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +public class BinaryTreeNode implements Comparable{ + + 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 (this.compareTo(o)==0) { + return null; + } else { + // current value less than inserted value + // go right + if (this.compareTo(o)<0) { + if (this.right == null) { + BinaryTreeNode nd = new BinaryTreeNode(); + nd.setData(o); + this.setRight(nd); + } else { + this.getRight().insert(o); + } + } + // greater than + // go left + else if(this.compareTo(o)>0) { + if (this.left == null) { + BinaryTreeNode nd = new BinaryTreeNode(); + nd.setData(o); + this.setLeft(nd); + } else { + this.getLeft().insert(o); + } + } + } + + return null; + } + + /** + * oversimplified implementation: only allows int and string + */ + @Override + public int compareTo(Object nd) throws ClassCastException{ + if (!(nd instanceof Object)) { + throw new ClassCastException("An object expected."); + } + + if (nd instanceof String) { + return ((String)this.data).compareTo((String) nd); + } else { + return ((int) this.data) -((int) nd); + } + } + +} diff --git a/group11/395443277/src/com/coding/basic/BinaryTreeNodeTest.java b/group11/395443277/src/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..1c5c637ccd --- /dev/null +++ b/group11/395443277/src/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class BinaryTreeNodeTest { + + @Test + public void testInsert() { + BinaryTreeNode root = new BinaryTreeNode(); + root.setData(3); + + root.insert(2); + BinaryTreeNode left = root.getLeft(); + assertEquals(2,left.getData()); + + root.insert(5); + BinaryTreeNode right = root.getRight(); + assertEquals(5, right.getData()); + + root.insert(7); + BinaryTreeNode rr = right.getRight(); + assertEquals(7, rr.getData()); + } + + @Test + public void testCompareTo() { + BinaryTreeNode n1 = new BinaryTreeNode(); + n1.setData("abc"); + + assertEquals(true, n1.compareTo("cde")<0); + + BinaryTreeNode n3 = new BinaryTreeNode(); + n3.setData(1); + + assertEquals(true, n3.compareTo(2)<0); + } + +} diff --git a/group11/395443277/src/com/coding/basic/Iterator.java b/group11/395443277/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group11/395443277/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/group11/395443277/src/com/coding/basic/LinkedList.java b/group11/395443277/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..fb9fbd81ca --- /dev/null +++ b/group11/395443277/src/com/coding/basic/LinkedList.java @@ -0,0 +1,156 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private Node head; + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + if (head == null) { + head = newNode; + } else { + Node curr = head; + while(curr.next != null) { + curr = curr.next; + } + curr.next = newNode; + } + } + public void add(int index , Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + if (head == null) { + head = newNode; + } else { + Node curr = head; + Node prev = curr; + while(index >0 && curr.next != null) { + prev = curr; + curr = curr.next; + index--; + } + + prev.next = newNode; + newNode.next = curr; + } + } + public Object get(int index){ + Node curr = head; + while(index > 0) { + curr = curr.next; + index--; + } + return curr.data; + } + public Object remove(int index){ + if (index ==0) { + return this.removeFirst(); + } + + Node curr = head; + Node prev = curr; + while(index >0 && curr.next != null) { + prev = curr; + curr = curr.next; + index--; + } + + Object target = curr.data; + prev.next = curr.next; + curr.next = null; + + return target; + } + public int size(){ + int size = 0; + Node curr = head; + while(curr != null) { + size++; + curr = curr.next; + } + return size; + } + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + if (head == null) { + head = newNode; + } else { + newNode.next = head.next; + head.next = newNode; + } + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + + Node curr = head; + if (head == null) { + head = newNode; + } else { + while(curr.next != null) { + curr = curr.next; + } + curr.next = newNode; + } + } + public Object removeFirst(){ + if (head == null) { + return null; + } + + Object target = head.data; + head = head.next; + return target; + } + public Object removeLast(){ + if (head == null) { + return null; + } + Node curr = head; + Node prev = curr; + while(curr.next != null) { + prev = curr; + curr = curr.next; + } + Object target = curr.data; + prev.next = null; + return target; + } + public Iterator iterator(){ + return new SeqIterator(); + } + + private class SeqIterator implements Iterator { + Node curr = head; + + @Override + public boolean hasNext() { + return curr != null; + } + + @Override + public Object next() { + if (!hasNext()) throw new NoSuchElementException(); + Object target = curr.data; + curr = curr.next; + return target; + } + + } + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group11/395443277/src/com/coding/basic/LinkedListTest.java b/group11/395443277/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..af5aa2d3a2 --- /dev/null +++ b/group11/395443277/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,127 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class LinkedListTest { + + @Test + public void testAddObject() { + LinkedList list = new LinkedList(); + list.add(5); + assertEquals(1, list.size()); + assertEquals(5, list.get(0)); + + list.add(4); + list.add(3); + list.add(2); + assertEquals(4, list.size()); + assertEquals(4, list.get(1)); + assertEquals(3, list.get(2)); + assertEquals(2, list.get(3)); + } + + @Test + public void testAddIntObject() { + LinkedList list = new LinkedList(); + list.add(0, 5); + assertEquals(1, list.size()); + assertEquals(5, list.get(0)); + + list.add(4); + list.add(3); + list.add(2); + + list.add(1, 1); + assertEquals(1, list.get(1)); + } + + @Test + public void testRemove() { + LinkedList list = new LinkedList(); + assertEquals(null, list.remove(0)); + list.add(4); + assertEquals(4, list.remove(0)); + + list.add(5); + list.add(-1); + list.add(16); + list.add(2); + list.add(7); + assertEquals(16, list.remove(2)); + assertEquals(4, list.size()); + assertEquals(2, list.get(2)); + } + + @Test + public void testAddFirst() { + LinkedList list = new LinkedList(); + list.addFirst(5); + assertEquals(1, list.size()); + assertEquals(5, list.get(0)); + + list.addFirst(4); + list.addFirst(3); + list.addFirst(2); + assertEquals(4, list.size()); + assertEquals(2, list.get(1)); + assertEquals(3, list.get(2)); + assertEquals(4, list.get(3)); + } + + @Test + public void testAddLast() { + LinkedList list = new LinkedList(); + list.addLast(5); + assertEquals(1, list.size()); + assertEquals(5, list.get(0)); + + list.addLast(4); + list.addLast(3); + list.addLast(2); + assertEquals(4, list.size()); + assertEquals(4, list.get(1)); + assertEquals(3, list.get(2)); + assertEquals(2, list.get(3)); + } + + @Test + public void testRemoveFirst() { + LinkedList list = new LinkedList(); + assertEquals(null, list.removeFirst()); + + list.add(4); + list.add(3); + list.add(2); + assertEquals(4, list.removeFirst()); + assertEquals(3, list.removeFirst()); + assertEquals(2, list.removeFirst()); + } + + @Test + public void testRemoveLast() { + LinkedList list = new LinkedList(); + assertEquals(null, list.removeLast()); + + list.add(4); + list.add(3); + list.add(2); + assertEquals(2, list.removeLast()); + assertEquals(3, list.removeLast()); + } + + @Test + public void testIterator() { + LinkedList list = new LinkedList(); + list.add(4); + list.add(3); + list.add(2); + + Iterator it = list.iterator(); + + assertEquals(4, it.next()); + assertEquals(3, it.next()); + assertEquals(2, it.next()); + } +} diff --git a/group11/395443277/src/com/coding/basic/List.java b/group11/395443277/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group11/395443277/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/group11/395443277/src/com/coding/basic/Queue.java b/group11/395443277/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..f12e73d46d --- /dev/null +++ b/group11/395443277/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group11/395443277/src/com/coding/basic/QueueTest.java b/group11/395443277/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..bd1ec0487a --- /dev/null +++ b/group11/395443277/src/com/coding/basic/QueueTest.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class QueueTest { + + @Test + public void testEnQueue() { + Queue q = new Queue(); + assertEquals(0, q.size()); + + q.enQueue(1); + q.enQueue(2); + q.enQueue(3); + } + + @Test + public void testDeQueue() { + Queue q = new Queue(); + q.enQueue(1); + q.enQueue(2); + q.enQueue(3); + assertEquals(1, q.deQueue()); + assertEquals(2, q.deQueue()); + } + +} diff --git a/group11/395443277/src/com/coding/basic/Stack.java b/group11/395443277/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..28b9e8a203 --- /dev/null +++ b/group11/395443277/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/group11/395443277/src/com/coding/basic/StackTest.java b/group11/395443277/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..7b3c4d2cf6 --- /dev/null +++ b/group11/395443277/src/com/coding/basic/StackTest.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class StackTest { + + @Test + public void testPush() { + Stack st = new Stack(); + st.push(1); + assertEquals(1, st.peek()); + + st.push(2); + st.push(3); + st.push(4); + assertEquals(4, st.peek()); + } + + @Test + public void testPop() { + Stack st = new Stack(); + assertEquals(null, st.pop()); + + st.push(1); + assertEquals(1, st.pop()); + + st.push(2); + st.push(3); + st.push(4); + assertEquals(4, st.pop()); + } + + @Test + public void testIsEmpty() { + Stack st = new Stack(); + assertEquals(true, st.isEmpty()); + + st.push(1); + assertEquals(false, st.isEmpty()); + } + + +} diff --git a/group11/542194147/myDataStructure/.classpath b/group11/542194147/myDataStructure/.classpath new file mode 100644 index 0000000000..ddd63c6d08 --- /dev/null +++ b/group11/542194147/myDataStructure/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group11/542194147/myDataStructure/.gitignore b/group11/542194147/myDataStructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/542194147/myDataStructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/542194147/myDataStructure/.project b/group11/542194147/myDataStructure/.project new file mode 100644 index 0000000000..56bfc21f79 --- /dev/null +++ b/group11/542194147/myDataStructure/.project @@ -0,0 +1,17 @@ + + + myDataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java b/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7512dac816 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,208 @@ +package com.coderising.array; + +import java.util.Arrays; + +/** + * Array集合工具类 + * @author 小摩托 + * + */ +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public int[] reverseArray(int[] origin){ + if(origin.length==0){ + throw new RuntimeException("数组为空"); + } + int[]exchange=new int[origin.length]; + for(int i=origin.length-1;i>=0;i--){ + exchange[origin.length-1-i]=origin[i]; + } + return exchange; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int exchange[]={}; + for(int i=0;i parameters) { + + /* + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + View view=new View(); + try { + SAXReader sr=new SAXReader(); + Document document= sr.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root=document.getRootElement(); + List actions=root.elements("action"); + for(Iterator it=actions.iterator();it.hasNext();){ + Element action =it.next(); + if(action.attribute("name").getText().equals(actionName)){ + LoginAction loginAction=(LoginAction)Class.forName( + action.attribute("class").getText()).newInstance(); + loginAction.setName(parameters.get("name")); + loginAction.setPassword(parameters.get("password")); + String loginMsg=loginAction.execute(); + if(loginMsg.equals("success")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("success")){ + createView(view, loginAction, result); + } + } + } + if(loginMsg.equals("fail")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("fail")){ + createView(view, loginAction, result); + } + } + } + if(loginMsg.equals("error")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("error")){ + createView(view, loginAction, result); + } + } + } + } + } + } catch (Exception e) { + System.out.println("have exception:"+e); + e.printStackTrace(); + } + return view; + } + + private static void createView(View view, LoginAction loginAction, Element result) { + Map msgMap=new HashMap(); + msgMap.put("message", loginAction.getMessage()); + view.setParameters(msgMap); + view.setJsp(result.getText()); + } + +} diff --git a/group11/542194147/myDataStructure/src/com/coderising/litestruts/StrutsTest.java b/group11/542194147/myDataStructure/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..7fa9e9a4e5 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java b/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..610ce0d092 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java @@ -0,0 +1,24 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/542194147/myDataStructure/src/com/coderising/litestruts/struts.xml b/group11/542194147/myDataStructure/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..fbede89fa1 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,133 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class MyArrayList implements MyList { + + /** + * 数组默认大小 + */ + private static final int DEFAULT_SIZE = 10; + /** + * 储存元素的数组 + */ + private Object[] elementData =null; + /** + * 数组大小指针; + */ + private int capacity; + /** + * 当前游标 + */ + private int current; + + public MyArrayList(int size){ + if(size<0){ + throw new RuntimeException("大小不能小于0"); + }else{ + this.elementData= new Object[size]; + this.current=0; + this.capacity=size; + } + } + + public MyArrayList(){ + this(DEFAULT_SIZE); + } + + @Override + public void add(Object o) { + isOverSize();//判断数组容量是否满足,不满足增加空间 + this.elementData[current]=o; + this.current++; + } + + @Override + public void add(int index, Object o) { + isOverSize();//判断数组容量是否满足,不满足增加空间 + isOutOfBoundIndex(index);//判断数组下标是否越界 + System.arraycopy(elementData, index, elementData, index+1, this.elementData.length-index); + this.current++; + } + + @Override + public Object get(int index) { + isOutOfBoundIndex(index);//判断数组下标是否越界 + return this.elementData[index]; + } + + @Override + public Object remove(int index) { + isOutOfBoundIndex(index);//判断数组下标是否越界 + Object o=this.elementData[index]; + if(this.elementData.length>index+1){ + System.arraycopy(elementData, index+1, elementData, index,this.elementData.length-index-1); + } + this.elementData[this.elementData.length-1]=null; + return o; + } + + public Iterator iterator(){ + return new MyArrayListIterator(); + } + + @Override + public int size() { + return this.elementData.length; + } + + /** + * 判断数组容量是否满足,不满足增加空间 + */ + private void isOverSize() { + if(this.current==this.capacity){ + this.capacity+=MyArrayList.DEFAULT_SIZE; + } + Object[]newElementData=new Object[this.capacity]; + for(int i=0;ithis.capacity||index<0){ + throw new RuntimeException("数组下标越界"); + } + } + + /** + * MyArrayList的迭代器 + * @author 小摩托 + * + */ + private class MyArrayListIterator implements Iterator{ + + private int current=0; + + @Override + public boolean hasNext() { + return currentsize||index<0){//检查下标是否越界 + throw new RuntimeException("下标越界"); + } + if(index==this.size){ + addLast(o);//插到队尾 + }else{ + Node l= node(index); + addBeforeNode(o,l);//插到指定下标节点之前 + } + } + + @Override + public Object get(int index) { + if (index >= size || index < 0) { + throw new RuntimeException("下标越界"); + } + return node(index).data; + } + + @Override + public Object remove(int index) { + if (index >= size || index < 0) { + throw new RuntimeException("下标越界"); + } + Node l=node(index); + Node prevNode=l.prev; + Node nextNode=l.next; + if(prevNode==null){ + head=nextNode; + }else{ + prevNode.next=nextNode; + + } + if(nextNode==null){ + last=prevNode; + }else{ + nextNode.prev=prevNode; + } + l.prev =null; + l.next=null; + l.data=null; + return l.data; + } + + @Override + public int size() { + return size; + } + + /** + * 获取对应节点的下标 + * @param element + * @return + */ + public int indexOf(Object element) { + Node current = head; + int count = 0; + while (current != null) { + if (element != null) { + if (element.equals(current.data)) { + return count; + } + }else{ + if (current.data == null) { + return count; + } + } + count ++; + current = current.next; + } + return -1; + } + + /** + * 添加到对应下标的节点之前 + * @param o + * @param theNode + */ + public void addBeforeNode(Object o,Node theNode){ + Node prevNode=theNode.prev; + Node newNode= new Node(o,theNode,prevNode); + theNode.prev=newNode; + if(null==prevNode){ + this.head=newNode; + }else{ + prevNode.next=newNode; + } + size++; + } + + /** + * 默认添加到队尾 + * @param o + */ + public void addLast(Object o){ + Node l=this.last; + Node node= new Node(o,null,l); + if(null!=l){ + l.next=node; + }else{ + this.head=node; + } + size++; + } + + /** + * 查找对应下标的节点并返回 + * @param index + * @return + */ + private Node node(int index){ + if(index<(this.size>>1)){ + Node current=head; + for(int i=0;iindex;i--){ + current=current.prev; + } + return current; + } + } + + public Iterator iterator(){ + return new MyLinkedListIterator(); + } + + private class MyLinkedListIterator implements Iterator{ + private Node current=head; + + @Override + public boolean hasNext() { + + return current!=last; + } + + @Override + public Object next() { + if(hasNext()==false){ + throw new RuntimeException("不存在对应元素"); + } + Object o=current.data; + current=current.next; + return o; + } + + @Override + public void remove() { + int index=MyLinkedList.this.indexOf(current); + MyLinkedList.this.remove(index); + } + + } + /** + * 双向链表 + * @author 小摩托 + * + */ + private static class Node{ + Object data; + Node next; + Node prev; + public Node(Object d,Node n,Node p){ + this.data=d; + this.next=n; + this.prev=p; + } + } + +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..73f331beec --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public interface MyList { + + /** + * 向集合中添加元素 + * @param o 任意类型元素 + */ + public void add(Object o); + /** + * 向集合中添加元素 + * @param index 指定位置下标 + * @param o 任意类型元素 + */ + public void add(int index, Object o); + /** + * 获取对应下标的元素 + * @param index 下标 + * @return 对应下标的元素 + */ + public Object get(int index); + /** + * 移除对应下标的元素 + * @param index 下标 + * @return + */ + public Object remove(int index); + /** + * 获取集合的大小 + * @return 大小数值 + */ + public int size(); + +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..c7fba907d4 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class MyQueue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..705b3ec7e0 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class MyStack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group11/635189253/dataStructure/.classpath b/group11/635189253/dataStructure/.classpath new file mode 100644 index 0000000000..fb5011632c --- /dev/null +++ b/group11/635189253/dataStructure/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group11/635189253/dataStructure/.gitignore b/group11/635189253/dataStructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/635189253/dataStructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/635189253/dataStructure/.project b/group11/635189253/dataStructure/.project new file mode 100644 index 0000000000..35bb23a0fc --- /dev/null +++ b/group11/635189253/dataStructure/.project @@ -0,0 +1,17 @@ + + + dataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/635189253/dataStructure/src/com/coding/basic/ArrayList.java b/group11/635189253/dataStructure/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..033cb6b5f5 --- /dev/null +++ b/group11/635189253/dataStructure/src/com/coding/basic/ArrayList.java @@ -0,0 +1,74 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public boolean add(Object o){ + add(size(), o); + size++; + return true; + } + public boolean add(int index, Object o){ + if (index >= 0 && index < elementData.length) { + for (int i = size(); i > index; i--) { + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + } + size++; + return true; + } + + public Object get(int index){ + if (index >= 0 && index < size()) { + return elementData[index]; + } +// return null; + throw new ArrayIndexOutOfBoundsException(); + } + + public Object remove(int index){ + Object removedItem = elementData[index]; + if (index > size()) { + throw new ArrayIndexOutOfBoundsException(); + } + for (int i = index; i < size() - 1; i++) { + elementData[i] = elementData[i+1]; + } + size--; + return removedItem; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int current = 0; + + public boolean hasNext() { + return current < size(); + } + + public Object next() { +/* if (elementData[current+1] != null) { + return elementData[current+1]; + } else { + return null; + }*/ + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + return elementData[current++]; + } + } + +} diff --git a/group11/635189253/dataStructure/src/com/coding/basic/BinaryTreeNode.java b/group11/635189253/dataStructure/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group11/635189253/dataStructure/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/group11/635189253/dataStructure/src/com/coding/basic/Iterator.java b/group11/635189253/dataStructure/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group11/635189253/dataStructure/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group11/635189253/dataStructure/src/com/coding/basic/LinkedList.java b/group11/635189253/dataStructure/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..26818d8741 --- /dev/null +++ b/group11/635189253/dataStructure/src/com/coding/basic/LinkedList.java @@ -0,0 +1,191 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private int size; + private int modCount = 0; + private Node beginMarker; + private Node endMarker; + + private static class Node{ + public Node( Object d, Node p, Node n) { + data = d; + prev = p; + next = n; + } + public Object data; + public Node prev; + public Node next; + } + + public LinkedList () { + doClear(); + } + + public void clear() { + doClear(); + } + + private void doClear() { + beginMarker = new Node(null, null, null); + endMarker = new Node(null, beginMarker, null); + beginMarker.next = endMarker; + + size = 0; + modCount++; + } + + public boolean add(Object o){ + add(size(), o); + return true; + } + public boolean add(int index , Object o){ + /*if (index < 0 && index > size()) { + throw new IndexOutOfBoundsException(); + }*/ + addBefore( getNode( index, 0, size()), o); + return true; + } + + public void addFirst(Object o){ + add(0, o); + } + + public void addLast(Object o){ + add(size(), o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size()); + } + + public Object get(int index){ + return getNode(index).data; + } + public Object remove(int index){ + return remove(getNode(index)); + } + + public int size(){ + return size; + } + + public boolean isEmpty() { + return size() == 0; + } + + private void addBefore( Node p, Object o ) { + Node newNode = new Node(o, p.prev, p ); + newNode.prev.next = newNode; + p.prev = newNode; +// p.prev = p.prev.next = new Node(o, p.prev, p); + size++; + modCount++; + } + + private Node getNode(int idx) { + return getNode(idx, 0, size() - 1); + } + + private Node getNode(int idx, int lower, int upper) { + Node p; + if (idx < lower || idx > upper) { + throw new IndexOutOfBoundsException(); + } + + if (idx < size() / 2) { + p = beginMarker.next; + for (int i = 0; i < idx; i++) { + p = p.next; + } + } else { + p = endMarker.prev; + for (int i = size(); i > idx; i--) { + p = p.prev; + } + } + return p; + } + + private Object remove( Node p ) { + p.prev.next = p.next; + p.next.prev = p.prev; + modCount++; + size--; + + return p.data; + } + + + public java.util.Iterator iterator() { + return new LinkListIterator(); + } + + private class LinkListIterator implements java.util.Iterator { + + private Node current = beginMarker.next; + private int expectedModCount = modCount; + private boolean okToRemove = false; + + @Override + public boolean hasNext() { + return current != endMarker; + } + + @Override + public Object next() { + if ( modCount != expectedModCount ) { + throw new java.util.ConcurrentModificationException(); + } + if (!hasNext()) { + throw new java.util.NoSuchElementException(); + } + Object nextItem = current.next; + current = current.next; + okToRemove = true; + return nextItem; + } + + public void remove() { + if ( modCount != expectedModCount ) { + throw new java.util.ConcurrentModificationException(); + } + if ( !okToRemove ) { + throw new IllegalStateException(); + } + LinkedList.this.remove(current.prev); + expectedModCount++; + okToRemove = false; + + } + } +} + +class TestLinkLedList { + + public static void main(String[] args) { + LinkedList lst = new LinkedList(); + for (int i = 0; i < 10; i++) { + lst.add(i); + } + for (int i = 20; i < 30; i++) { + lst.add(0, i); + } + + lst.remove(0); + lst.remove(lst.size()-1); + + System.out.println(lst); + + java.util.Iterator itr = lst.iterator(); + while (itr.hasNext()) { + itr.next(); + itr.remove(); + System.out.println(lst); + } + } +} diff --git a/group11/635189253/dataStructure/src/com/coding/basic/List.java b/group11/635189253/dataStructure/src/com/coding/basic/List.java new file mode 100644 index 0000000000..66beb2b1c6 --- /dev/null +++ b/group11/635189253/dataStructure/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public boolean add(Object o); + public boolean add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group11/635189253/dataStructure/src/com/coding/basic/Queue.java b/group11/635189253/dataStructure/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..46890b2b03 --- /dev/null +++ b/group11/635189253/dataStructure/src/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData; + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group11/635189253/dataStructure/src/com/coding/basic/Stack.java b/group11/635189253/dataStructure/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group11/635189253/dataStructure/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/group11/729245768/DataStructure/src/main/coding_170225/ArrayList.java b/group11/729245768/DataStructure/src/main/coding_170225/ArrayList.java new file mode 100644 index 0000000000..d0a81044c6 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170225/ArrayList.java @@ -0,0 +1,86 @@ +import java.util.Arrays; + +/** + * Created by peter on 2017/2/22. + */ +public class ArrayList { + private Object[] elements = new Object[10]; + private int position = 0; + //添加元素 + public void add(Object o){ + //首先判断当前数组是否已满 + if(position==elements.length){ + ensureCapacity();//如果到达则扩展数组长度 + } + elements[position++] = o; + } + //在index位置添加元素 + public void add(int index,Object o){ + if(index<0||index>position){ + System.out.println("invalid index"); + throw new ArrayIndexOutOfBoundsException(); + } + if(position==elements.length){ + //如果此时数组已满 + ensureCapacity(); + } + for(int i=position-1;i>=index;i--){ + elements[i+1] = elements[i];//元素向后移动一位 + } + elements[index] = o; + position++;//数组元素个数加一 + } + + //获取index位置的元素 + public Object get(int index){ + if(index<0||index>position-1){ + System.out.println("不合法的index"); + throw new ArrayIndexOutOfBoundsException(); + } + return elements[index]; + } + + //删除index位置的元素 + public Object remove(int index){ + if(index<0||index>position-1){ + System.out.println("不合法的index"); + throw new ArrayIndexOutOfBoundsException(); + } + position--; + return elements[position+1]; + } + public int size(){ + return position; + } + //返回一个迭代器 + public Iterator getIterator(){ + return new ArrayListIterator(this); + } + //扩展数组大小,在原来基础上扩展一倍 + public void ensureCapacity(){ + elements = Arrays.copyOf(elements,elements.length); + } + private class ArrayListIterator implements Iterator{ + private ArrayList list; + int position=-1; + public ArrayListIterator(ArrayList list){ + this.list=list; + } + @Override + public boolean hasNext() { + if(++position=binaryTreeNode.getData()){ + //插入到左孩子 + parent.setLeft(binaryTreeNode); + }else{ + //插入到右孩子 + parent.setRight(binaryTreeNode); + } + } + //输出二叉树每个节点 + public void printBinaryTreeNode(BinaryTreeNode root){ + if(root==null){ + return; + } + if(root.getLeft()!=null){ + printBinaryTreeNode(root.getLeft()); + } + System.out.println(root.getData()); + if(root.getRight()!=null){ + printBinaryTreeNode(root.getRight()); + } + } + //获取根节点 + public BinaryTreeNode getRoot(){ + return root; + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170225/BinaryTreeNode.java b/group11/729245768/DataStructure/src/main/coding_170225/BinaryTreeNode.java new file mode 100644 index 0000000000..0cea0f59c7 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170225/BinaryTreeNode.java @@ -0,0 +1,40 @@ + +/** + * Created by peter on 2017/2/23. + */ +public class BinaryTreeNode{ + private int data; + private BinaryTreeNode left=null; + private BinaryTreeNode right=null; + public BinaryTreeNode(){ + + } + + public BinaryTreeNode(int data){ + this.data=data; + } + + public int getData() { + return data; + } + + public void setData(int data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } +} diff --git a/group11/729245768/DataStructure/src/main/coding_170225/Iterator.java b/group11/729245768/DataStructure/src/main/coding_170225/Iterator.java new file mode 100644 index 0000000000..a833ba0acd --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170225/Iterator.java @@ -0,0 +1,9 @@ + +/** + * Created by peter on 2017/2/23. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + public Object remove(); +} diff --git a/group11/729245768/DataStructure/src/main/coding_170225/LinkedList.java b/group11/729245768/DataStructure/src/main/coding_170225/LinkedList.java new file mode 100644 index 0000000000..b02e7101b1 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170225/LinkedList.java @@ -0,0 +1,113 @@ + +/** + * Created by peter on 2017/2/22. + */ +public class LinkedList { + private Node head,tail; + private int size=0; + public void add(Object o){ + Node node = new Node(); + node.data = o; + node.next=null; + if(size==0){ + head=node; + tail=node; + }else{ + tail.next = node; + tail = tail.next; + } + size++; + } + public void add(int index,Object o){ + if(index<0||index>size){ + System.out.println("插入下标越界"); + throw new ArrayIndexOutOfBoundsException(); + } + //如果插入的位置是第一个 + if(index==0){ + Node node =new Node(); + node.data = o; + node.next = head; + head = node; + }else { + int i =0;//记录走过的节点 + Node p = head;//移动节点 + while (isize-1){ + System.out.println("访问下标越界"); + throw new ArrayIndexOutOfBoundsException(); + } + Node node =head; + int i=0; + while (isize-1){ + System.out.println("out of array"); + throw new ArrayIndexOutOfBoundsException(); + } + Object data=null;//用来存储返回值 + if(index==0){ + //删除的是第一个节点 + data = head.data; + head=head.next; + size--; + return data; + }else{ + Node p1=head,p2=null;//p1表示移动节点,p2是上一个节点 + int i =0;//表示移动的距离 + while (i list = new ArrayList<>(); + for(int i=6;i<=max;i++){ + if(isPerfectNumbers(i)){ + list.add(i); + } + } + int[] array = new int[list.size()]; + for(int i=0;i list = new ArrayList<>(); + int end =(int) Math.sqrt(element);// + for(int i=2;i<=end;){ + if(element%i==0){ + list.add(i); + element=element/i; + }else{ + i++; + } + } + int sum =1; + for(Integer i:list){ + sum+=i; + } + if(element==sum){ + return true; + }else{ + return false; + } + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for(int i=0;i parameters) throws DocumentException { + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + View view = new View(); + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read(new File("DataStructure/src/main/coding_170302/struts.xml")); + Element root = document.getRootElement();//获取根元素 + List listOfAction = root.elements("action");//获取action子元素 + int i=0; + for(;i> inputs = parameters.entrySet(); + //执行setName和setPassword + for(Map.Entry input:inputs){ + String key = input.getKey(); + String value = input.getValue(); + String methodName = "set"+Character.toUpperCase(key.charAt(0))+key.substring(1); + Method method = c.getDeclaredMethod(methodName,String.class); + method.invoke(o,value); + } + //执行execute方法,并返回message + Method execute = c.getDeclaredMethod("execute"); + String returnMessage = (String)execute.invoke(o); + + Method[] methods = c.getDeclaredMethods(); + //获取所有getter方法,并且将值放到view的parameters中 + Map paraMap = new HashMap<>(); + for(int j=0;j 配置,以及execute的返回值, 确定哪一个jsp, + // 放到View对象的jsp字段中。 + List resultOfElements = action.elements("result"); + for(int k=0;k + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/729245768/DataStructure/tests/main/coding_170225/ArrayListTest.java b/group11/729245768/DataStructure/tests/main/coding_170225/ArrayListTest.java new file mode 100644 index 0000000000..bde9c7a637 --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170225/ArrayListTest.java @@ -0,0 +1,39 @@ +package main.coding_170225; + +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by peter on 2017/2/25. + */ +public class ArrayListTest extends TestCase { + @Test + public void add() throws Exception { + } + + @Test + public void add1() throws Exception { + + } + + @Test + public void get() throws Exception { + + } + + @Test + public void remove() throws Exception { + + } + + @Test + public void testSize() throws Exception { + ArrayList arrayList = new ArrayList(); + arrayList.add(12); + arrayList.add(15); + arrayList.add(17); + Assert.assertEquals(3,arrayList.size()); + } + +} \ No newline at end of file diff --git a/group11/729245768/DataStructure/tests/main/coding_170225/BinaryTreeTest.java b/group11/729245768/DataStructure/tests/main/coding_170225/BinaryTreeTest.java new file mode 100644 index 0000000000..1492323570 --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170225/BinaryTreeTest.java @@ -0,0 +1,37 @@ +package main.coding_170225; + +import junit.framework.TestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by peter on 2017/2/25. + */ +public class BinaryTreeTest extends TestCase { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testInsert() throws Exception { + + } + + @Test + public void testPrintBinaryTreeNode() throws Exception { + + } + + @Test + public void testGetRoot() throws Exception { + + } + +} \ No newline at end of file diff --git a/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java b/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java new file mode 100644 index 0000000000..4d5cc38728 --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java @@ -0,0 +1,78 @@ +package main.coding_170302; + +import junit.framework.TestCase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by peter on 2017/3/2. + */ +public class ArrayUtilTest extends TestCase { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testReverseArray() throws Exception { + int[] array1 = {1,2,3,4,5,6,7}; + int[] array2 = {7,6,5,4,3,2,1}; + ArrayUtil util = new ArrayUtil(); + util.reverseArray(array1); + Assert.assertArrayEquals(array1,array2); + } + + @Test + public void testRemoveZero() throws Exception { + int[] array1 = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] arry2 = {1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(new ArrayUtil().removeZero(array1),arry2); +} + + @Test + public void testMerge() throws Exception { + int[] array1 = {3,5,7,8}; + int[] array2 = {4,5,6,7}; + int[] array = {3,4,5,6,7,8}; + Assert.assertArrayEquals(array,new ArrayUtil().merge(array1,array2)); + } + + @Test + public void testGrow() throws Exception { + int[] array1 ={2,3,6}; + int[] array2 = {2,3,6,0,0,0}; + Assert.assertArrayEquals(array2,new ArrayUtil().grow(array1,3)); + } + + @Test + public void testFibonacci() throws Exception { + int[] array1 = {1,1,2,3,5,8,13}; + Assert.assertArrayEquals(array1,new ArrayUtil().fibonacci(15)); + } + + @Test + public void testGetPrimes() throws Exception { + int[] array1 = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(array1,new ArrayUtil().getPrimes(23)); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] array1 = {6}; + Assert.assertArrayEquals(array1,new ArrayUtil().getPerfectNumbers(10)); + } + + @Test + public void testJoin() throws Exception { + int[] array1 = {3,8,9}; + Assert.assertEquals("3-8-9",new ArrayUtil().join(array1,"-")); + } + +} \ No newline at end of file diff --git a/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java b/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java new file mode 100644 index 0000000000..309bdfc9ba --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java @@ -0,0 +1,42 @@ +package main.coding_170302; + +import junit.framework.TestCase; +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by peter on 2017/3/3. + */ +public class StrutsTest extends TestCase { + @Test + public void testLoginActionSuccess() throws DocumentException { + String actionName = "login"; + + Map params = new HashMap<>(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + @Test + public void testLoginActionFailed() throws DocumentException { + String actionName = "login"; + Map params = new HashMap<>(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} \ No newline at end of file diff --git a/group11/996108220/.classpath b/group11/996108220/.classpath new file mode 100644 index 0000000000..11cc9c31cb --- /dev/null +++ b/group11/996108220/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group11/996108220/.gitignore b/group11/996108220/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group11/996108220/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group11/996108220/.project b/group11/996108220/.project new file mode 100644 index 0000000000..fe4e769dd4 --- /dev/null +++ b/group11/996108220/.project @@ -0,0 +1,17 @@ + + + 996108220Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/996108220/src/com/coderising/array/ArrayUtil.java b/group11/996108220/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..0637eb921c --- /dev/null +++ b/group11/996108220/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,194 @@ +package com.coderising.array; + +import com.coding.basic.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int mid=origin.length%2==1?origin.length/2:origin.length/2-1; + for (int i = 0; i <= mid; i++) { + int vlaue=origin[i]; + origin[i]=origin[origin.length-1-i]; + origin[origin.length-1-i]=vlaue; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + ArrayList list=new ArrayList(); + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i]!=0) { + list.add(oldArray[i]); + } + } + int[] array=new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + array[i]=(int) list.get(i); + } + + return array; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int[] array=new int[array1.length+array2.length]; + int ptr1=0;int ptr2=0;int index=0; + while (ptr1!=array1.length&&ptr2!=array2.length) { + array[index++]=array1[ptr1]>array2[ptr2]?array2[ptr2++]:array1[ptr1++]; + } + if (ptr1==array1.length) { + for (int i = ptr2; i < array2.length; i++)array[index++]=array2[i]; + } + else { + for (int i = ptr1; i < array1.length; i++)array[index++]=array1[i]; + } + return array; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int [] array=new int[oldArray.length+size]; + for (int i = 0; i < oldArray.length; i++) { + array[i]=oldArray[i]; + } + return array; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + + if (max==1) { + return null; + } + else if (max==2) { + return new int[]{1,1}; + } + else { + ArrayList list=new ArrayList(); + list.add(1); + list.add(1); + int next=2; + while (next messageToresult=null; + public String getActionName() { + return actionName; + } + public void setActionName(String actionName) { + this.actionName = actionName; + } + public String getClazzName() { + return clazzName; + } + public void setClazzName(String clazzName) { + this.clazzName = clazzName; + } + public HashMap getMessageToresult() { + return messageToresult; + } + public void setMessageToresult(HashMap messageToresult) { + this.messageToresult = messageToresult; + } + public ActionConfig(String actionName, String clazzName, + HashMap messageToresult) { + super(); + this.actionName = actionName; + this.clazzName = clazzName; + this.messageToresult = messageToresult; + } + + +} diff --git a/group11/996108220/src/com/coderising/litestruts/LoginAction.java b/group11/996108220/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..01b51fdd88 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction implements Action{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group11/996108220/src/com/coderising/litestruts/LogoutAction.java b/group11/996108220/src/com/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..8043fae56e --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/LogoutAction.java @@ -0,0 +1,35 @@ +package com.coderising.litestruts; + +public class LogoutAction implements Action{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "logout successful"; + return "success"; + } + this.message = "logout failed,please try again"; + return "error"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} + diff --git a/group11/996108220/src/com/coderising/litestruts/Struts.java b/group11/996108220/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..8f961f2175 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,167 @@ +package com.coderising.litestruts; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import com.coderising.litestruts.Action; + + + +/* + +0. 读取配置文件struts.xml + +1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) +据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 +("name"="test" , "password"="1234") , +那就应该调用 setName和setPassword方法 + +2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + +3. 通过反射找到对象的所有getter方法(例如 getMessage), +通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , +放到View对象的parameters + +4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, +放到View对象的jsp字段中。 + +*/ + +public class Struts { + //strut.xml文件所在的路径 + public static final String dir="/mycoding2017/group11/996108220/src/com/coderising/litestruts/struts.xml"; + + /** + * 用户提供action动作,以及用户名和密码,对应返回view视图 + * @param actionName 登入登出 + * @param parameters 用户名密码 + */ + public static View runAction(String actionName, Map parameters) throws Exception { + ActionConfig actionConfig=getActionConfig(actionName); + Action action=createAction(actionConfig.getClazzName(),parameters); + String message=getActionMessage(action); + View view=updaView(getALL(action), actionConfig, message); + return view; + } + + /** + * 步骤0:读取配置文件,将文件中的action生成ActionDao + * @param actionName传入action的名字 + */ + private static ActionConfig getActionConfig(String name) throws Exception { + // 生成一个Dom解析器 + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + // 解析XML文件 + Document document = builder.parse(dir); + NodeList actions = document.getElementsByTagName("action"); + int j = 0;String actionName="";String clazzName=""; + for (; j < actions.getLength(); j++) { + actionName= actions.item(j).getAttributes().getNamedItem("name").getNodeValue(); + if (actionName.equals(name)) { + clazzName=actions.item(j).getAttributes().getNamedItem("class").getNodeValue(); + break; + } + } + if (actions.item(0).getNodeType() == Node.ELEMENT_NODE) { + + Element action =(Element) actions.item(j); + NodeList results =action.getElementsByTagName("result"); + HashMap map=new HashMap(); + for (int i = 0; i < results.getLength(); i++) { + String nameString=results.item(i).getAttributes().getNamedItem("name").getNodeValue(); + String pageString=results.item(i).getTextContent(); + map.put(nameString, pageString); + } + return new ActionConfig(actionName, clazzName, map); + } + return null; + } + /** + * 步骤1:反射创建action的对象,将name和password赋值 + * @param clazzName + * @return + */ + private static Action createAction(String clazzName,Map parameters) throws Exception { + Class clazz=Class.forName(clazzName); + Object action = clazz.newInstance() ; + Method nameSetter = action.getClass().getMethod("setName", String.class); + nameSetter .invoke(action, parameters.get("name")); + Method passwordSetter = action.getClass().getMethod("setPassword", String.class); + passwordSetter.invoke(action, parameters.get("password")); + return (Action) action; + } + /** + * 步骤2:反射运行execute方法,获得message + * @param action + * @return message + */ + private static String getActionMessage (Action action) throws Exception { + + return (String) action.getClass().getMethod("execute").invoke(action); + + } + /** + * 步骤3:将action中get方法与get到的值的映射关系记录到view里的Parameters表中 + * @param action + * @return view + */ + private static View getALL(Action action) throws Exception { + HashMap map=new HashMap<>(); + Method nameGetter = action.getClass().getMethod("getName"); + map.put("name", (String) nameGetter.invoke(action)); + Method passwordGetter = action.getClass().getMethod("getPassword"); + map.put("password", (String) passwordGetter.invoke(action)); + Method MessageGetter = action.getClass().getMethod("getMessage"); + map.put("message", (String) MessageGetter.invoke(action)); + View view=new View(); + view.setParameters(map); + return view; + } + /** + * 步骤4:将execute获得的message查找Struts配置文件将对应的页面记录到view中 + * @param view + * @param actionConfig + * @param message + * @return + */ + private static View updaView(View view,ActionConfig actionConfig,String message) { + return view.setJsp(actionConfig.getMessageToresult().get(message)); + } +// public static void main(String[] args) { +// DocumentBuilder builder; +// try { +// builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); +// Document doc = builder.parse(dir); +// NodeList beans = doc.getElementsByTagName("action"); +// for (int j = 0; j < beans.getLength(); j++) { +// System.out.println(beans.item(j).getAttributes().getNamedItem("name").getNodeValue()); +// System.out.println(beans.item(j).getAttributes().getNamedItem("class").getNodeValue()); +// } +// if (beans.item(0).getNodeType() == Node.ELEMENT_NODE) { +// +// Element action =(Element) beans.item(0); +// NodeList results =action.getElementsByTagName("result"); +// HashMap map=new HashMap(); +// System.out.println(results.getLength()); +// for (int i = 0; i < results.getLength(); i++) { +// System.out.println(results.item(i).getAttributes().getNamedItem("name").getNodeValue()); +// System.out.println(results.item(i).getTextContent()); +// } +// } +// +//// NamedNodeMap name = beans.item(0).getAttributes(); +// +// } catch (Exception e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } + + +// } + +} diff --git a/group11/996108220/src/com/coderising/litestruts/StrutsTest.java b/group11/996108220/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..af5f82d275 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,63 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + @Test + public void testLogoutActionSuccess() throws Exception { + + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("logout successful", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionFailed() throws Exception { + String actionName = "logout"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("logout failed,please try again", view.getParameters().get("message")); + } +} diff --git a/group11/996108220/src/com/coderising/litestruts/View.java b/group11/996108220/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group11/996108220/src/com/coderising/litestruts/struts.xml b/group11/996108220/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/996108220/src/com/coding/basic/ArrayList.java b/group11/996108220/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..2445d52ae5 --- /dev/null +++ b/group11/996108220/src/com/coding/basic/ArrayList.java @@ -0,0 +1,132 @@ +package com.coding.basic; + + +public class ArrayList implements List { + + private int size = 0; + private Object[] elementData = new Object[100]; + /** + * 在队尾添加元素 + */ + public void add(Object o){ + if(size+1>elementData.length)this.grow(elementData); + else elementData[size++]=o; + } + /** + * 在index处添加元素,index+1到size-1元素向后移动 + */ + public void add(int index, Object o){ + if(index<0||index>size){ + System.out.println("数组越界"+index); + return; + } + if(size+1>elementData.length)this.grow(elementData); + else { + for(int i=size;i>=index+1;) + { + elementData[i]=elementData[--i]; + } + size++; + elementData[index]=o; + } + + } + /** + * 获得index处的元素elementData[index] + */ + public Object get(int index){ + //TODO越界抛出异常 + if(index<0||index>size){ + System.out.println("数组越界"+index); + return null; + } + else{ + return elementData[index]; + } + } + /** + * 移除index处的元素,将index+1到size-1的元素向前移动 + */ + public Object remove(int index){ + //TODO越界抛出异常 + if(index<0||index>=size){ + System.out.println("数组越界"+index); + return null; + } + else{ + Object value=elementData[index]; + for(int i=index;i= size) + System.out.println("超过size");; + Object[] elementData = ArrayList.this.elementData; + if (i >= elementData.length) + System.out.println("超过length"); + cursor = i + 1; + return elementData[i]; + } + + } + public Object[] toArray() { + Object[] array= new Object[size]; + for (int i = 0; i < elementData.length; i++) { + array[i]=elementData[i]; + } + return array; + } + public void grow(Object[] elementData2){ + int[] elementData=new int[elementData2.length+elementData2.length/2]; + System.arraycopy(elementData2,0,elementData,0,elementData2.length); + } + /** + * 测试方法 + * @param args + */ + public static void main(String args[]){ + ArrayList list=new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + //list.add(2,0); + //list.remove(list.size-1); + System.out.println(list.size()); + Iterator itr=list.iterator(); + while (itr.hasNext()) { + System.out.println(itr.next()); + + } + } + +} + diff --git a/group11/996108220/src/com/coding/basic/BinaryTree.java b/group11/996108220/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..1b6d95b6d1 --- /dev/null +++ b/group11/996108220/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,129 @@ +package com.coding.basic; + +import com.sun.corba.se.impl.orbutil.graph.Node; + +public class BinaryTree { + + private BinaryTreeNode root=null; + private int size=0; + /** + * 插入节点,保持二叉树的性质 + * @param o + */ + public void insert(T o){ + if (size==0) { + root=new BinaryTreeNode(o); + } + else{ + insert(o,root); + } + size++; + } + + private void insert(T o, BinaryTreeNode ptr) { + if ((ptr.right==null&&(ptr.data.compareTo(o)<=0))){ + ptr.right=new BinaryTreeNode(o); + } + else if (ptr.left==null&&(ptr.data.compareTo(o)>0)) { + ptr.left=new BinaryTreeNode(o); + + } + else if (ptr.left!=null&&(ptr.data.compareTo(o)>0)) { + insert(o, ptr.left); + } + else { + insert(o, ptr.right); + } + } + private static class BinaryTreeNode { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode(T o) { + this.data=o; + this.left=null; + this.right=null; + } + private BinaryTreeNode() { + + } + } + /** + * 前序遍历 + */ + public void preOrder(BinaryTreeNode Node) + { + if (Node != null) + { + System.out.print(Node.data + " "); + preOrder(Node.left); + preOrder(Node.right); + } + } + + /** + * 中序遍历 + */ + public void midOrder(BinaryTreeNode Node) + { + if (Node != null) + { + midOrder(Node.left); + System.out.print(Node.data + " "); + midOrder(Node.right); + } + } + + /** + * 后序遍历 + */ + public void posOrder(BinaryTreeNode Node) + { + if (Node != null) + { + posOrder(Node.left); + posOrder(Node.right); + System.out.print(Node.data + " "); + } + } + /** + * @param key查找元素 + * @param node + * @return 返回date的node引用 + */ + public BinaryTreeNode searchNode(T key,BinaryTreeNode node) { + if (node!=null) { + if (node.data.compareTo(key)==0) { + return node; + } + else if (node.data.compareTo(key)>0) { + return searchNode(key,node.left); + } + else { + return searchNode(key,node.right); + } + } + else{ + return null; + } + } + + public static void main(String[] args) { + BinaryTree tree=new BinaryTree(); + tree.insert(5); + tree.insert(3); + tree.insert(1); + tree.insert(6); + tree.insert(5); + tree.insert(2); + tree.preOrder(tree.root); + System.out.println(); + tree.posOrder(tree.root); + System.out.println(); + tree.midOrder(tree.root); + System.out.println(tree.searchNode(1, tree.root).data); + + + } + +} diff --git a/group11/996108220/src/com/coding/basic/Iterator.java b/group11/996108220/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group11/996108220/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/group11/996108220/src/com/coding/basic/LinkedList.java b/group11/996108220/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..be0b354bc2 --- /dev/null +++ b/group11/996108220/src/com/coding/basic/LinkedList.java @@ -0,0 +1,176 @@ +package com.coding.basic; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size=0; +/** + * 增加节点 + */ + public void add(Object o){ + if(size==0){ + head=new Node(); + head.data=o; + head.next=null; + size++; + } + else{ + addLast(o); + } + + } +/** + * 在index(0~size)处添加元素 + */ + public void add(int index , Object o){ + + if(index<0||index>size){ + System.out.println("index超出范围"+index); + return; + } + if(index==0)addFirst( o); + else if(index==size)addLast(o); + else{ + Node ptr=head; + for(int i=1;i=size)return null; + else{ + Node ptr=head; + for(int i=0;i=size)return null; + else if(index==0)return removeFirst(); + else if(index==size-1)return removeLast(); + else{ + Node ptr=head; + for(int i=1;isize){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - grow(); - if(indexsize){ - throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); - } - return elementData[index]; - } - - public Object remove(int index){ - - Object o = elementData[index]; - System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); - size --; - return o; - } - - public int size(){ - return size; - } - - /** - * 扩容,扩容因子为10 - */ - private void grow(){ - - if(size>=elementData.length){//长度不够需要扩容 - newElementData = new Object[size+GENE]; - System.arraycopy(elementData, 0, newElementData, 0, elementData.length); - elementData = newElementData; - } - } - - - public Iterator iterator(){ - - return new Itr(); - } - - private class Itr implements Iterator{ - - int cursor; - @Override - public boolean hasNext() { - return cursor != ArrayList.this.size; - } - - @Override - public Object next() { - - int i = this.cursor; - if (i >= ArrayList.this.size){ - throw new NoSuchElementException(); - } - this.cursor = (i + 1); - return ArrayList.this.elementData[i]; - } - - } -} +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + /*扩容因子*/ + private static final int GENE = 10; + + private Object[] elementData = new Object[10]; + /*扩容引用*/ + private Object[] newElementData; + + public void add(Object o){ + grow(); + elementData[size] = o; + size ++; + } + public void add(int index, Object o){ + + if(index>size){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + grow(); + if(indexsize){ + throw new IndexOutOfBoundsException("Index: "+index+",Size:"+size); + } + return elementData[index]; + } + + public Object remove(int index){ + + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-(index+1)); + size --; + return o; + } + + public int size(){ + return size; + } + + /** + * 扩容,扩容因子为10 + */ + private void grow(){ + + if(size>=elementData.length){//长度不够需要扩容 + newElementData = new Object[size+GENE]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; + } + } + + + public Iterator iterator(){ + + return new Itr(); + } + + private class Itr implements Iterator{ + + int cursor; + @Override + public boolean hasNext() { + return cursor != ArrayList.this.size; + } + + @Override + public Object next() { + + int i = this.cursor; + if (i >= ArrayList.this.size){ + throw new NoSuchElementException(); + } + this.cursor = (i + 1); + return ArrayList.this.elementData[i]; + } + + } +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java index e5fae50203..3449517197 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTree.java @@ -1,64 +1,64 @@ -package com.coding.basic; - -public class BinaryTree { - - //根节点 - private BinaryTreeNode root; - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public > BinaryTreeNode insert(T o){ - - BinaryTreeNode treeNode = new BinaryTreeNode(); - treeNode.setData(o); - if(root == null){ - root = treeNode; - }else{ - BinaryTreeNode currentNode = root; - BinaryTreeNode parent; - while(true){ - parent = currentNode; - if(((Comparable)currentNode.getData()).compareTo(o)>0){//向左放 - currentNode = currentNode.getLeft(); - if(currentNode == null){ - parent.setLeft(treeNode); - treeNode.setParent(parent); - break; - } - }else{//向右放 - currentNode = currentNode.getRight(); - if(currentNode == null){ - parent.setRight(treeNode); - treeNode.setParent(parent); - break; - } - } - } - } - return treeNode; - } - - /** - * 先序遍历 - * @param node - * @return - */ - public List traversalBefore(BinaryTreeNode node){ - //所有数据集合 - List datas = new ArrayList(); - return traversal(node,datas); - } - private List traversal(BinaryTreeNode node,List datas){ - - if(node !=null){ - datas.add(node.getData()); - traversal(node.getLeft(),datas); - traversal(node.getRight(),datas); - } - return datas; - } - - public BinaryTreeNode getRoot() { - return root; - } - -} +package com.coding.basic; + +public class BinaryTree { + + //根节点 + private BinaryTreeNode root; + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public > BinaryTreeNode insert(T o){ + + BinaryTreeNode treeNode = new BinaryTreeNode(); + treeNode.setData(o); + if(root == null){ + root = treeNode; + }else{ + BinaryTreeNode currentNode = root; + BinaryTreeNode parent; + while(true){ + parent = currentNode; + if(((Comparable)currentNode.getData()).compareTo(o)>0){//向左放 + currentNode = currentNode.getLeft(); + if(currentNode == null){ + parent.setLeft(treeNode); + treeNode.setParent(parent); + break; + } + }else{//向右放 + currentNode = currentNode.getRight(); + if(currentNode == null){ + parent.setRight(treeNode); + treeNode.setParent(parent); + break; + } + } + } + } + return treeNode; + } + + /** + * 先序遍历 + * @param node + * @return + */ + public List traversalBefore(BinaryTreeNode node){ + //所有数据集合 + List datas = new ArrayList(); + return traversal(node,datas); + } + private List traversal(BinaryTreeNode node,List datas){ + + if(node !=null){ + datas.add(node.getData()); + traversal(node.getLeft(),datas); + traversal(node.getRight(),datas); + } + return datas; + } + + public BinaryTreeNode getRoot() { + return root; + } + +} diff --git a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java index 557728a02a..a8e6b66edd 100644 --- a/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java +++ b/group12/2258659044/zj-2017/src/com/coding/basic/BinaryTreeNode.java @@ -1,37 +1,37 @@ -package com.coding.basic; -public class BinaryTreeNode { - - private Object data; - //父节点 - private BinaryTreeNode parent; - 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 getParent() { - return parent; - } - public void setParent(BinaryTreeNode parent) { - this.parent = parent; - } -} +package com.coding.basic; +public class BinaryTreeNode { + + private Object data; + //父节点 + private BinaryTreeNode parent; + 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 getParent() { + return parent; + } + public void setParent(BinaryTreeNode parent) { + this.parent = parent; + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java index 0f343ed895..1357cf17cf 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/BinaryTreeTest.java @@ -1,58 +1,58 @@ -package test.com.coding.basic; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.BinaryTree; -import com.coding.basic.BinaryTreeNode; -import com.coding.basic.List; - -public class BinaryTreeTest { - - BinaryTree tree ; - - @Before - public void setup() { - - tree = new BinaryTree(); - Assert.assertEquals(tree.getRoot(), null); - tree.insert(5); - tree.insert(2); - tree.insert(7); - tree.insert(1); - tree.insert(6); - } - @Test - public void insert(){ - - BinaryTreeNode node = tree.insert(4); - Assert.assertEquals(node.getParent().getData(), 2); - Assert.assertEquals(node.getParent().getLeft().getData(), 1); - - BinaryTreeNode node2 = tree.insert(8); - Assert.assertEquals(node2.getParent().getData(), 7); - Assert.assertEquals(node2.getParent().getLeft().getData(), 6); - } - - @Test - public void traversal(){ - - insert(); - //以根节点为起点先序遍历 - List treeList = tree.traversalBefore(tree.getRoot()); - //expected value - int[] exValue = {5,2,1,4,7,6,8}; - for (int i = 0; i < exValue.length; i++) { - Assert.assertEquals(treeList.get(i),exValue[i]); - } - - //以数据2位起点先序遍历 - List treeList2 = tree.traversalBefore(tree.getRoot().getLeft()); - //expected value - int[] exValue2 = {2,1,4}; - for (int i = 0; i < exValue2.length; i++) { - Assert.assertEquals(treeList2.get(i),exValue2[i]); - } - } -} +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.BinaryTree; +import com.coding.basic.BinaryTreeNode; +import com.coding.basic.List; + +public class BinaryTreeTest { + + BinaryTree tree ; + + @Before + public void setup() { + + tree = new BinaryTree(); + Assert.assertEquals(tree.getRoot(), null); + tree.insert(5); + tree.insert(2); + tree.insert(7); + tree.insert(1); + tree.insert(6); + } + @Test + public void insert(){ + + BinaryTreeNode node = tree.insert(4); + Assert.assertEquals(node.getParent().getData(), 2); + Assert.assertEquals(node.getParent().getLeft().getData(), 1); + + BinaryTreeNode node2 = tree.insert(8); + Assert.assertEquals(node2.getParent().getData(), 7); + Assert.assertEquals(node2.getParent().getLeft().getData(), 6); + } + + @Test + public void traversal(){ + + insert(); + //以根节点为起点先序遍历 + List treeList = tree.traversalBefore(tree.getRoot()); + //expected value + int[] exValue = {5,2,1,4,7,6,8}; + for (int i = 0; i < exValue.length; i++) { + Assert.assertEquals(treeList.get(i),exValue[i]); + } + + //以数据2位起点先序遍历 + List treeList2 = tree.traversalBefore(tree.getRoot().getLeft()); + //expected value + int[] exValue2 = {2,1,4}; + for (int i = 0; i < exValue2.length; i++) { + Assert.assertEquals(treeList2.get(i),exValue2[i]); + } + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java index a55b2d5a3f..3a5ff822ad 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/LinkedListTest.java @@ -1,109 +1,109 @@ -package test.com.coding.basic; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.Iterator; -import com.coding.basic.LinkedList; - -public class LinkedListTest { - - LinkedList ls ; - @Before - public void setup() { - ls = new LinkedList(); - } - - /** - * 测试一个参数的add方法 - * ArrayList当数据超过10时进行第一次扩容 - */ - @Test - public void add(){ - - ls.add(3); - ls.add("a"); - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Assert.assertEquals(ls.size(), 12); - Assert.assertEquals(ls.get(1), "a"); - } - - /** - * 两个参数的add方法 - */ - @Test//(expected = IndexOutOfBoundsException.class) - public void add4ToPramter(){ - - ls.add(0, 0); - ls.add(1,1); - ls.add(2, 2); - ls.add(3,3); - for (int i = 0; i < 10; i++) { - ls.add(3,i); - } - Assert.assertEquals(ls.size(), 14); - Assert.assertEquals(ls.get(3), 9); - Assert.assertEquals(ls.get(13), 3); - //打开下面操作抛出异常 - //ls.add(15, "a"); - } - - /** - * get(i) - */ - @Test//(expected = IndexOutOfBoundsException.class) - public void get(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - - Assert.assertEquals(ls.get(9), 9); - //打开下面操作抛出异常 - //ls.get(12); - } - - @Test - public void remove(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Assert.assertEquals(ls.remove(5),5); - Assert.assertEquals(ls.size(),9); - Assert.assertEquals(ls.remove(8),9); - Assert.assertEquals(ls.size(),8); - } - - @Test - public void size(){ - - Assert.assertEquals(ls.size(),0); - ls.add("a"); - Assert.assertEquals(ls.size(),1); - ls.add(0,0); - Assert.assertEquals(ls.size(),2); - ls.remove(0); - Assert.assertEquals(ls.size(),1); - - } - - @Test//(expected = NoSuchElementException.class) - public void iterator(){ - - for (int i = 0; i < 10; i++) { - ls.add(i); - } - Iterator it = ls.iterator(); - Assert.assertEquals(it.hasNext(),true); - for (int i = 0; i < 10; i++) { - it.next(); - } - Assert.assertEquals(it.hasNext(),false); - //打开下面操作抛出异常 - //it.next(); - } -} +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + LinkedList ls ; + @Before + public void setup() { + ls = new LinkedList(); + } + + /** + * 测试一个参数的add方法 + * ArrayList当数据超过10时进行第一次扩容 + */ + @Test + public void add(){ + + ls.add(3); + ls.add("a"); + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.size(), 12); + Assert.assertEquals(ls.get(1), "a"); + } + + /** + * 两个参数的add方法 + */ + @Test//(expected = IndexOutOfBoundsException.class) + public void add4ToPramter(){ + + ls.add(0, 0); + ls.add(1,1); + ls.add(2, 2); + ls.add(3,3); + for (int i = 0; i < 10; i++) { + ls.add(3,i); + } + Assert.assertEquals(ls.size(), 14); + Assert.assertEquals(ls.get(3), 9); + Assert.assertEquals(ls.get(13), 3); + //打开下面操作抛出异常 + //ls.add(15, "a"); + } + + /** + * get(i) + */ + @Test//(expected = IndexOutOfBoundsException.class) + public void get(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + + Assert.assertEquals(ls.get(9), 9); + //打开下面操作抛出异常 + //ls.get(12); + } + + @Test + public void remove(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Assert.assertEquals(ls.remove(5),5); + Assert.assertEquals(ls.size(),9); + Assert.assertEquals(ls.remove(8),9); + Assert.assertEquals(ls.size(),8); + } + + @Test + public void size(){ + + Assert.assertEquals(ls.size(),0); + ls.add("a"); + Assert.assertEquals(ls.size(),1); + ls.add(0,0); + Assert.assertEquals(ls.size(),2); + ls.remove(0); + Assert.assertEquals(ls.size(),1); + + } + + @Test//(expected = NoSuchElementException.class) + public void iterator(){ + + for (int i = 0; i < 10; i++) { + ls.add(i); + } + Iterator it = ls.iterator(); + Assert.assertEquals(it.hasNext(),true); + for (int i = 0; i < 10; i++) { + it.next(); + } + Assert.assertEquals(it.hasNext(),false); + //打开下面操作抛出异常 + //it.next(); + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java index e688d9b41f..75af57b371 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/QueueTest.java @@ -1,64 +1,64 @@ -package test.com.coding.basic; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.Queue; - -public class QueueTest { - - Queue qe ; - - @Before - public void setup() { - qe = new Queue(); - for (int i = 0; i < 10; i++) { - qe.enQueue(i); - } - } - - @Test - public void enQueue(){ - - Assert.assertEquals(qe.size(), 10); - qe.enQueue("abcd"); - Assert.assertEquals(qe.size(), 11); - } - - @Test//(expected = IndexOutOfBoundsException.class) - public void deQueue(){ - - Assert.assertEquals(qe.size(), 10); - for (int i = 0; i < 10; i++) { - Assert.assertEquals(qe.deQueue(), i); - } - Assert.assertEquals(qe.size(), 0); - //打开下列语句与期望异常测试 - //qe.deQueue(); - } - - public void isEmpty(){ - - Assert.assertEquals(qe.isEmpty(),false); - for (int i = 0; i < 10; i++) { - qe.deQueue(); - } - Assert.assertEquals(qe.isEmpty(),true); - Queue qe1 = new Queue(); - Assert.assertEquals(qe1.isEmpty(), true); - } - - public void size(){ - - Assert.assertEquals(qe.size(),10); - qe.enQueue("lk"); - qe.enQueue('h'); - Assert.assertEquals(qe.size(),12); - for (int i = 0; i < 12; i++) { - qe.deQueue(); - } - Assert.assertEquals(qe.size(),0); - Queue qe1 = new Queue(); - Assert.assertEquals(qe1.size(), 0); - } -} +package test.com.coding.basic; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Queue; + +public class QueueTest { + + Queue qe ; + + @Before + public void setup() { + qe = new Queue(); + for (int i = 0; i < 10; i++) { + qe.enQueue(i); + } + } + + @Test + public void enQueue(){ + + Assert.assertEquals(qe.size(), 10); + qe.enQueue("abcd"); + Assert.assertEquals(qe.size(), 11); + } + + @Test//(expected = IndexOutOfBoundsException.class) + public void deQueue(){ + + Assert.assertEquals(qe.size(), 10); + for (int i = 0; i < 10; i++) { + Assert.assertEquals(qe.deQueue(), i); + } + Assert.assertEquals(qe.size(), 0); + //打开下列语句与期望异常测试 + //qe.deQueue(); + } + + public void isEmpty(){ + + Assert.assertEquals(qe.isEmpty(),false); + for (int i = 0; i < 10; i++) { + qe.deQueue(); + } + Assert.assertEquals(qe.isEmpty(),true); + Queue qe1 = new Queue(); + Assert.assertEquals(qe1.isEmpty(), true); + } + + public void size(){ + + Assert.assertEquals(qe.size(),10); + qe.enQueue("lk"); + qe.enQueue('h'); + Assert.assertEquals(qe.size(),12); + for (int i = 0; i < 12; i++) { + qe.deQueue(); + } + Assert.assertEquals(qe.size(),0); + Queue qe1 = new Queue(); + Assert.assertEquals(qe1.size(), 0); + } +} diff --git a/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java b/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java index a0875c8f3c..5d9fcd0f16 100644 --- a/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java +++ b/group12/2258659044/zj-2017/src/test/com/coding/basic/StackTest.java @@ -1,76 +1,76 @@ -package test.com.coding.basic; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.coding.basic.Stack; - -public class StackTest { - - Stack st ; - - @Before - public void setup() { - st = new Stack(); - for (int i = 0; i < 10; i++) { - st.push(i); - } - } - - @Test - public void push(){ - - Assert.assertEquals(st.size(), 10); - st.push(10); - st.push('a'); - Assert.assertEquals(st.size(), 12); - } - - @Test//(expected = IndexOutOfBoundsException.class) - public void pop(){ - - Assert.assertEquals(st.size(), 10); - for (int i = 9; i >= 0; i--) { - Assert.assertEquals(st.pop(), i); - } - //打开下列语句抛出期望异常 - //st.pop(); - } - - @Test - public void peek(){ - - Assert.assertEquals(st.size(), 10); - Assert.assertEquals(st.peek(), 9); - Assert.assertEquals(st.size(), 10); - } - - @Test - public void isEmpty(){ - - Assert.assertEquals(st.isEmpty(), false); - for (int i = 0; i < 10; i++) { - st.pop(); - } - Assert.assertEquals(st.isEmpty(), true); - Stack st1 = new Stack(); - Assert.assertEquals(st1.isEmpty(), true); - } - - public void size(){ - - Assert.assertEquals(st.size(),10); - st.push("lk"); - st.push('h'); - Assert.assertEquals(st.size(),12); - for (int i = 0; i < 12; i++) { - st.pop(); - } - Assert.assertEquals(st.size(),0); - st.peek(); - Assert.assertEquals(st.size(),0); - Stack st1 = new Stack(); - Assert.assertEquals(st1.size(), 0); - } -} +package test.com.coding.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.Stack; + +public class StackTest { + + Stack st ; + + @Before + public void setup() { + st = new Stack(); + for (int i = 0; i < 10; i++) { + st.push(i); + } + } + + @Test + public void push(){ + + Assert.assertEquals(st.size(), 10); + st.push(10); + st.push('a'); + Assert.assertEquals(st.size(), 12); + } + + @Test//(expected = IndexOutOfBoundsException.class) + public void pop(){ + + Assert.assertEquals(st.size(), 10); + for (int i = 9; i >= 0; i--) { + Assert.assertEquals(st.pop(), i); + } + //打开下列语句抛出期望异常 + //st.pop(); + } + + @Test + public void peek(){ + + Assert.assertEquals(st.size(), 10); + Assert.assertEquals(st.peek(), 9); + Assert.assertEquals(st.size(), 10); + } + + @Test + public void isEmpty(){ + + Assert.assertEquals(st.isEmpty(), false); + for (int i = 0; i < 10; i++) { + st.pop(); + } + Assert.assertEquals(st.isEmpty(), true); + Stack st1 = new Stack(); + Assert.assertEquals(st1.isEmpty(), true); + } + + public void size(){ + + Assert.assertEquals(st.size(),10); + st.push("lk"); + st.push('h'); + Assert.assertEquals(st.size(),12); + for (int i = 0; i < 12; i++) { + st.pop(); + } + Assert.assertEquals(st.size(),0); + st.peek(); + Assert.assertEquals(st.size(),0); + Stack st1 = new Stack(); + Assert.assertEquals(st1.size(), 0); + } +} diff --git a/group12/251822722/ArrayList.java b/group12/251822722/ArrayList.java old mode 100755 new mode 100644 diff --git a/group12/251822722/BinaryTreeNode.java b/group12/251822722/BinaryTreeNode.java old mode 100755 new mode 100644 diff --git a/group12/251822722/Iterator.java b/group12/251822722/Iterator.java old mode 100755 new mode 100644 diff --git a/group12/251822722/LinkedList.java b/group12/251822722/LinkedList.java old mode 100755 new mode 100644 diff --git a/group12/251822722/List.java b/group12/251822722/List.java old mode 100755 new mode 100644 diff --git a/group12/251822722/Queue.java b/group12/251822722/Queue.java old mode 100755 new mode 100644 diff --git a/group12/251822722/Stack.java b/group12/251822722/Stack.java old mode 100755 new mode 100644 diff --git a/group12/377401843/learning_1/.gitignore b/group12/377401843/learning_1/.gitignore index 9c47ca7e58..b12088665a 100644 --- a/group12/377401843/learning_1/.gitignore +++ b/group12/377401843/learning_1/.gitignore @@ -1,3 +1,3 @@ -/bin/ +/bin/ /.project /.classpath diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java index a1ee0ee339..880af45da8 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/ArrayList.java @@ -1,171 +1,171 @@ -package com.guodong.datastructure; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - /** - * 按下标顺序新增元素 - * - * @Method add - * @param o - * @see com.guodong.datastructure.List#add(java.lang.Object) - */ - public void add(Object o) { - ensureCapacityInternal(size + 1); - elementData[size] = o; - size++; - } - - /** - * 在指定下标位置插入元素 - * - * @Method add - * @param index - * @param o - * @see com.guodong.datastructure.List#add(int, java.lang.Object) - */ - public void add(int index, Object o) { - checkRangeForAdd(index); - - ensureCapacityInternal(size + 1); - - System.arraycopy(elementData, index, elementData, index + 1, size - index); - - elementData[index] = o; - size++; - } - - /** - * 根据下标获取列表数据 - * - * @Method get - * @param index - * @return - * @see com.guodong.datastructure.List#get(int) - */ - public Object get(int index) { - checkRangeForGetOrRemove(index); - - return elementData[index]; - } - - /** - * 根据下标移除元素,并返回移除的元素值 - * - * @Method remove - * @param index - * @return - * @see com.guodong.datastructure.List#remove(int) - */ - public Object remove(int index) { - checkRangeForGetOrRemove(index); - - Object oldValue = elementData[index]; - - System.arraycopy(elementData, index + 1, elementData, index, size - index -1); - - size--; - elementData[size] = null; - - return oldValue; - } - - /** - * 获得列表长度 - * - * @Method size - * @return - * @see com.guodong.datastructure.List#size() - */ - public int size() { - return size; - } - - /** - * 获取ArrayList的迭代器 - * - * @MethodName iterator - * @author zhaogd - * @date 2017年2月21日 下午8:19:28 - * @return - */ - public Iterator iterator() { - return new ArrayListIterator(); - } - - /** - * 确保数组容量足够,如果不够则扩充数组 - * - * @MethodName ensureCapacityInternal - * @author zhaogd - * @date 2017年2月21日 下午5:06:46 - * @param minCapacity - */ - private void ensureCapacityInternal(int minCapacity) { - if(minCapacity > elementData.length){ - grow(minCapacity); - } - } - - /** - * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 - * - * @MethodName grow - * @author zhaogd - * @date 2017年2月21日 下午5:20:55 - * @param minCapacity - */ - private void grow(int minCapacity) { - minCapacity = elementData.length + elementData.length / 2; - elementData = Arrays.copyOf(elementData, minCapacity); - } - - /** - * 检查Add方法的下标范围是否合法 - * - * @MethodName checkRangeForAdd - * @author zhaogd - * @date 2017年2月21日 下午7:32:55 - * @param index - */ - private void checkRangeForAdd(int index) { - if(index > size || index < 0){ - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - /** - * 检查Get或者Remove方法的下标范围是否合法 - * - * @MethodName checkRangeForGetOrRemove - * @author zhaogd - * @date 2017年2月21日 下午7:33:21 - * @param index - */ - private void checkRangeForGetOrRemove(int index) { - if(index >= size || index < 0){ - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - - private class ArrayListIterator implements Iterator{ - - private int lastIndex = 0; - - @Override - public boolean hasNext() { - return lastIndex < size; - } - - @Override - public Object next() { - return elementData[lastIndex++]; - } - } -} +package com.guodong.datastructure; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + /** + * 按下标顺序新增元素 + * + * @Method add + * @param o + * @see com.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + ensureCapacityInternal(size + 1); + elementData[size] = o; + size++; + } + + /** + * 在指定下标位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkRangeForAdd(index); + + ensureCapacityInternal(size + 1); + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + + elementData[index] = o; + size++; + } + + /** + * 根据下标获取列表数据 + * + * @Method get + * @param index + * @return + * @see com.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkRangeForGetOrRemove(index); + + return elementData[index]; + } + + /** + * 根据下标移除元素,并返回移除的元素值 + * + * @Method remove + * @param index + * @return + * @see com.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkRangeForGetOrRemove(index); + + Object oldValue = elementData[index]; + + System.arraycopy(elementData, index + 1, elementData, index, size - index -1); + + size--; + elementData[size] = null; + + return oldValue; + } + + /** + * 获得列表长度 + * + * @Method size + * @return + * @see com.guodong.datastructure.List#size() + */ + public int size() { + return size; + } + + /** + * 获取ArrayList的迭代器 + * + * @MethodName iterator + * @author zhaogd + * @date 2017年2月21日 下午8:19:28 + * @return + */ + public Iterator iterator() { + return new ArrayListIterator(); + } + + /** + * 确保数组容量足够,如果不够则扩充数组 + * + * @MethodName ensureCapacityInternal + * @author zhaogd + * @date 2017年2月21日 下午5:06:46 + * @param minCapacity + */ + private void ensureCapacityInternal(int minCapacity) { + if(minCapacity > elementData.length){ + grow(minCapacity); + } + } + + /** + * 数组扩充,每次扩充原数组一半的长度,然后把原数组拷贝到新数组 + * + * @MethodName grow + * @author zhaogd + * @date 2017年2月21日 下午5:20:55 + * @param minCapacity + */ + private void grow(int minCapacity) { + minCapacity = elementData.length + elementData.length / 2; + elementData = Arrays.copyOf(elementData, minCapacity); + } + + /** + * 检查Add方法的下标范围是否合法 + * + * @MethodName checkRangeForAdd + * @author zhaogd + * @date 2017年2月21日 下午7:32:55 + * @param index + */ + private void checkRangeForAdd(int index) { + if(index > size || index < 0){ + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查Get或者Remove方法的下标范围是否合法 + * + * @MethodName checkRangeForGetOrRemove + * @author zhaogd + * @date 2017年2月21日 下午7:33:21 + * @param index + */ + private void checkRangeForGetOrRemove(int index) { + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + + private class ArrayListIterator implements Iterator{ + + private int lastIndex = 0; + + @Override + public boolean hasNext() { + return lastIndex < size; + } + + @Override + public Object next() { + return elementData[lastIndex++]; + } + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java index 677eff3dab..2ced039d20 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/BinaryTreeNode.java @@ -1,58 +1,58 @@ -package com.guodong.datastructure; - -public class BinaryTreeNode { - - private int data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public BinaryTreeNode(int data) { - this.data = data; - } - - public int getData() { - return data; - } - - public void setData(int 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(int o) { - - if (o < data) { - if (left != null) { - left.insert(o); - } else { - left = new BinaryTreeNode(o); - return left; - } - } else { - if (right != null) { - right.insert(o); - } else { - right = new BinaryTreeNode(o); - return right; - } - } - - return null; - } - -} +package com.guodong.datastructure; + +public class BinaryTreeNode { + + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(int data) { + this.data = data; + } + + public int getData() { + return data; + } + + public void setData(int 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(int o) { + + if (o < data) { + if (left != null) { + left.insert(o); + } else { + left = new BinaryTreeNode(o); + return left; + } + } else { + if (right != null) { + right.insert(o); + } else { + right = new BinaryTreeNode(o); + return right; + } + } + + return null; + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java index 1a9bc5ad8a..8d486220b0 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Iterator.java @@ -1,7 +1,7 @@ -package com.guodong.datastructure; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.guodong.datastructure; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java index a7dc12694e..976129cc84 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/LinkedList.java @@ -1,285 +1,285 @@ -package com.guodong.datastructure; - -import java.util.NoSuchElementException; - -public class LinkedList implements List { - - private int size; - - private Node head; - - private Node last; - - /** - * 向 链表尾端插入元素 - * - * @Method add - * @param o - * @see com.guodong.datastructure.List#add(java.lang.Object) - */ - public void add(Object o) { - linkLast(o); - } - - /** - * 向链表指定位置插入元素 - * - * @Method add - * @param index - * @param o - * @see com.guodong.datastructure.List#add(int, java.lang.Object) - */ - public void add(int index, Object o) { - checkIndexForAdd(index); - - if (index == size) { - linkLast(o); - } else { - Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 - Node currentNode = getNodeByIndex(index); // 取到当前下标节点 - Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 - - if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 - head = newNode; - } else { - prevNode.next = newNode; - } - size++; - } - } - - /** - * 根据下标获取链表中元素 - * - * @Method get - * @param index - * @return - * @see com.guodong.datastructure.List#get(int) - */ - public Object get(int index) { - checkIndexForGet(index); - return getNodeByIndex(index).data; - } - - public Object getLast() { - return last.data; - } - - /** - * 根据下标移除链表元素 - * - * @Method remove - * @param index - * @return - * @see com.guodong.datastructure.List#remove(int) - */ - public Object remove(int index) { - checkIndexForGet(index); - - Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 - Node currentNode = null; - if (prevNode == null) { - currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 - head = currentNode.next; - } else { - currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 - prevNode.next = currentNode.next; - } - Node nextNode = currentNode.next; - - if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 - last = prevNode; - } else { - currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 - } - Object data = currentNode.data; - currentNode.data = null; // 清空当前节点的值,等待垃圾回收 - - size--; - - return data; - } - - /** - * 返回List长度 - */ - public int size() { - return size; - } - - /** - * 向列表头部添加元素 - * - * @param o - */ - public void addFirst(Object o) { - Node n = head; - Node newNode = new Node(o, n); - - head = newNode; - if (n == null) { - last = newNode; - } - size++; - } - - /** - * 向列表尾部添加元素 - * - * @param o - */ - public void addLast(Object o) { - linkLast(o); - } - - /** - * 移除链表第一个元素 - * - * @return - */ - public Object removeFirst() { - Node n = head; - if (n == null) { - throw new NoSuchElementException(); - } - Object data = n.data; - Node nextNode = n.next; - - n.data = null; - n.next = null; - - head = nextNode; - if (nextNode == null) { - last = null; - } - - size--; - return data; - } - - public Object removeLast() { - Node n = last; - if (n == null) { - throw new NoSuchElementException(); - } - Object data = n.data; - Node prevNode = getNodeByIndex(size - 2); - n.data = null; - if (prevNode == null) { - head = null; - } else { - prevNode.next = null; - } - last = prevNode; - - size--; - return data; - } - - /** - * 根据下标获取对应的节点 - * - * @MethodName getNodeByIndex - * @author zhaogd - * @date 2017年2月23日 下午3:32:48 - * @param index - * @return - */ - private Node getNodeByIndex(int index) { - if (index < 0) { - return null; - } - Node n = head; - for (int i = 0; i < index; i++) { - n = n.next; - } - return n; - } - - /** - * 在链表尾端插入节点 - * - * @MethodName linkLast - * @author zhaogd - * @date 2017年2月23日 下午3:14:28 - * @param o - */ - private void linkLast(Object o) { - Node n = last; // 取出原尾端数据 - Node newNode = new Node(o, null); // 创建新节点 - last = newNode; // 把新节点放入链表尾端 - // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 - // 如果不为空,把原尾端节点指向新节点 - if (n == null) { - head = newNode; - } else { - n.next = newNode; - } - - size++; - } - - /** - * 检查下标是否合法 - * - * @MethodName checkIndexForAdd - * @author zhaogd - * @date 2017年2月23日 下午3:05:07 - * @param index - */ - private void checkIndexForAdd(int index) { - if (index < 0 || index > size) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - /** - * 检查下标是否合法 - * - * @MethodName checkIndexForGet - * @author zhaogd - * @date 2017年2月23日 下午4:21:35 - * @param index - */ - private void checkIndexForGet(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); - } - } - - private static class Node { - Object data; - Node next; - - Node(Object data, Node next) { - this.data = data; - this.next = next; - } - } - - public Iterator iterator() { - return new LinkedListIterator(); - } - - private class LinkedListIterator implements Iterator { - private Node current; - - private int index; - - @Override - public boolean hasNext() { - return index < size; - } - - @Override - public Object next() { - if (current == null) { - current = getNodeByIndex(index); - } - Object data = current.data; - current = current.next; - index++; - return data; - } - } -} +package com.guodong.datastructure; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size; + + private Node head; + + private Node last; + + /** + * 向 链表尾端插入元素 + * + * @Method add + * @param o + * @see com.guodong.datastructure.List#add(java.lang.Object) + */ + public void add(Object o) { + linkLast(o); + } + + /** + * 向链表指定位置插入元素 + * + * @Method add + * @param index + * @param o + * @see com.guodong.datastructure.List#add(int, java.lang.Object) + */ + public void add(int index, Object o) { + checkIndexForAdd(index); + + if (index == size) { + linkLast(o); + } else { + Node prevNode = getNodeByIndex(index - 1); // 取到当前下标的前一个节点 + Node currentNode = getNodeByIndex(index); // 取到当前下标节点 + Node newNode = new Node(o, currentNode); // 创建新节点,新节点的下一个节点为当前下标节点 + + if (prevNode == null) { // 如果前一个节点为空,说明从头部插入 + head = newNode; + } else { + prevNode.next = newNode; + } + size++; + } + } + + /** + * 根据下标获取链表中元素 + * + * @Method get + * @param index + * @return + * @see com.guodong.datastructure.List#get(int) + */ + public Object get(int index) { + checkIndexForGet(index); + return getNodeByIndex(index).data; + } + + public Object getLast() { + return last.data; + } + + /** + * 根据下标移除链表元素 + * + * @Method remove + * @param index + * @return + * @see com.guodong.datastructure.List#remove(int) + */ + public Object remove(int index) { + checkIndexForGet(index); + + Node prevNode = getNodeByIndex(index - 1); // 获取当前index前一个元素 + Node currentNode = null; + if (prevNode == null) { + currentNode = getNodeByIndex(index); // 如果前一个为空,则把下一个元素赋值给链表头 + head = currentNode.next; + } else { + currentNode = prevNode.next; // 如果不为空,则把前一个节点跟后一个节点链接 + prevNode.next = currentNode.next; + } + Node nextNode = currentNode.next; + + if (nextNode == null) { // 如果后一个节点为空,则把链尾赋值为前一个节点 + last = prevNode; + } else { + currentNode.next = null; // 如果后一个节点不为空,不做任何处理,只打断当前节点的链接 + } + Object data = currentNode.data; + currentNode.data = null; // 清空当前节点的值,等待垃圾回收 + + size--; + + return data; + } + + /** + * 返回List长度 + */ + public int size() { + return size; + } + + /** + * 向列表头部添加元素 + * + * @param o + */ + public void addFirst(Object o) { + Node n = head; + Node newNode = new Node(o, n); + + head = newNode; + if (n == null) { + last = newNode; + } + size++; + } + + /** + * 向列表尾部添加元素 + * + * @param o + */ + public void addLast(Object o) { + linkLast(o); + } + + /** + * 移除链表第一个元素 + * + * @return + */ + public Object removeFirst() { + Node n = head; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node nextNode = n.next; + + n.data = null; + n.next = null; + + head = nextNode; + if (nextNode == null) { + last = null; + } + + size--; + return data; + } + + public Object removeLast() { + Node n = last; + if (n == null) { + throw new NoSuchElementException(); + } + Object data = n.data; + Node prevNode = getNodeByIndex(size - 2); + n.data = null; + if (prevNode == null) { + head = null; + } else { + prevNode.next = null; + } + last = prevNode; + + size--; + return data; + } + + /** + * 根据下标获取对应的节点 + * + * @MethodName getNodeByIndex + * @author zhaogd + * @date 2017年2月23日 下午3:32:48 + * @param index + * @return + */ + private Node getNodeByIndex(int index) { + if (index < 0) { + return null; + } + Node n = head; + for (int i = 0; i < index; i++) { + n = n.next; + } + return n; + } + + /** + * 在链表尾端插入节点 + * + * @MethodName linkLast + * @author zhaogd + * @date 2017年2月23日 下午3:14:28 + * @param o + */ + private void linkLast(Object o) { + Node n = last; // 取出原尾端数据 + Node newNode = new Node(o, null); // 创建新节点 + last = newNode; // 把新节点放入链表尾端 + // 如果原尾端为空,说明链表为空,把新节点也放入链表头部 + // 如果不为空,把原尾端节点指向新节点 + if (n == null) { + head = newNode; + } else { + n.next = newNode; + } + + size++; + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForAdd + * @author zhaogd + * @date 2017年2月23日 下午3:05:07 + * @param index + */ + private void checkIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + /** + * 检查下标是否合法 + * + * @MethodName checkIndexForGet + * @author zhaogd + * @date 2017年2月23日 下午4:21:35 + * @param index + */ + private void checkIndexForGet(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + private Node current; + + private int index; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + if (current == null) { + current = getNodeByIndex(index); + } + Object data = current.data; + current = current.next; + index++; + return data; + } + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/List.java b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java index 2471c15d21..1a6f12da52 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/List.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/List.java @@ -1,14 +1,14 @@ -package com.guodong.datastructure; - -public interface List { - - public void add(Object o); - - public void add(int index, Object o); - - public Object get(int index); - - public Object remove(int index); - - public int size(); -} +package com.guodong.datastructure; + +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/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java index b14751aab7..6dc85d8ec0 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Queue.java @@ -1,21 +1,21 @@ -package com.guodong.datastructure; - -public class Queue { - private LinkedList element = new LinkedList(); - - public void enQueue(Object o) { - element.addLast(o); - } - - public Object deQueue() { - return element.removeFirst(); - } - - public boolean isEmpty() { - return element.size() == 0; - } - - public int size() { - return element.size(); - } -} +package com.guodong.datastructure; + +public class Queue { + private LinkedList element = new LinkedList(); + + public void enQueue(Object o) { + element.addLast(o); + } + + public Object deQueue() { + return element.removeFirst(); + } + + public boolean isEmpty() { + return element.size() == 0; + } + + public int size() { + return element.size(); + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java index f743d0dd3b..c2b5049e73 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/Stack.java @@ -1,25 +1,25 @@ -package com.guodong.datastructure; - -public class Stack { - private LinkedList elementData = new LinkedList(); - - public void push(Object o) { - elementData.addLast(o); - } - - public Object pop() { - return elementData.removeLast(); - } - - public Object peek() { - return elementData.getLast(); - } - - public boolean isEmpty() { - return elementData.size() == 0; - } - - public int size() { - return elementData.size(); - } -} +package com.guodong.datastructure; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public void push(Object o) { + elementData.addLast(o); + } + + public Object pop() { + return elementData.removeLast(); + } + + public Object peek() { + return elementData.getLast(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java index ec3a7600a4..f38f58614d 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/ArrayListTest.java @@ -1,135 +1,135 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.ArrayList; -import com.guodong.datastructure.Iterator; - -public class ArrayListTest { - - ArrayList arrayList; - - @Before - public void setUp() throws Exception { - arrayList = new ArrayList(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAddObject() { - // 测试新增 - arrayList.add(0); - assertEquals(0, arrayList.get(0)); - assertEquals(1, arrayList.size()); - - // 测试扩充 - for (int i = 1; i < 101; i++) { - arrayList.add(i); - } - assertEquals(101, arrayList.size()); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd1() { - // 测试新增下标异常时,是否可以正确抛出异常 - arrayList.add(-1, 2); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd2() { - // 测试新增下标异常时,是否可以正确抛出异常 - arrayList.add(1, 3); - } - - @Test - public void testAddIntObject() { - // 测试下标新增 - arrayList.add(0, 1); - arrayList.add(1, 2); - arrayList.add(2, 3); - arrayList.add(3, 4); - assertEquals(4, arrayList.size()); - - // 测试中间插入 - arrayList.add(2, 5); - assertEquals(5, arrayList.size()); // 测试插入之后长度 - assertEquals(5, arrayList.get(2)); - assertEquals(4, arrayList.get(4)); // 测试插入之后原来数据是否后移 - assertEquals(3, arrayList.get(3)); // 测试插入之后原来数据是否后移 - - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet1() { - // 测试Get时,下标异常,是否可以正确抛出异常 - arrayList.get(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet2() { - // 测试Get时,下标异常,是否可以正确抛出异常 - arrayList.get(0); - } - - @Test - public void testGet() { - arrayList.add(1); - arrayList.add(2); - arrayList.add(3); - assertEquals(1, arrayList.get(0)); - assertEquals(2, arrayList.get(1)); - assertEquals(3, arrayList.get(2)); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove1() { - arrayList.remove(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove2() { - arrayList.remove(0); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForRemove3() { - arrayList.remove(1); - } - - @Test - public void testRemove() { - arrayList.add(1); - arrayList.remove(0); - assertEquals(0, arrayList.size()); - - arrayList.add(1); - arrayList.add(2); - arrayList.remove(0); - assertEquals(1, arrayList.size()); - assertEquals(2, arrayList.get(0)); - } - - @Test - public void testSize() { - arrayList.add(1); - assertEquals(1, arrayList.size()); - } - - @Test - public void testIterator() { - Iterator iterator = arrayList.iterator(); - assertFalse(iterator.hasNext()); - - arrayList.add(1); - assertTrue(iterator.hasNext()); - assertEquals(1, iterator.next()); - assertFalse(iterator.hasNext()); - } -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.ArrayList; +import com.guodong.datastructure.Iterator; + +public class ArrayListTest { + + ArrayList arrayList; + + @Before + public void setUp() throws Exception { + arrayList = new ArrayList(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + // 测试新增 + arrayList.add(0); + assertEquals(0, arrayList.get(0)); + assertEquals(1, arrayList.size()); + + // 测试扩充 + for (int i = 1; i < 101; i++) { + arrayList.add(i); + } + assertEquals(101, arrayList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd1() { + // 测试新增下标异常时,是否可以正确抛出异常 + arrayList.add(-1, 2); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + // 测试新增下标异常时,是否可以正确抛出异常 + arrayList.add(1, 3); + } + + @Test + public void testAddIntObject() { + // 测试下标新增 + arrayList.add(0, 1); + arrayList.add(1, 2); + arrayList.add(2, 3); + arrayList.add(3, 4); + assertEquals(4, arrayList.size()); + + // 测试中间插入 + arrayList.add(2, 5); + assertEquals(5, arrayList.size()); // 测试插入之后长度 + assertEquals(5, arrayList.get(2)); + assertEquals(4, arrayList.get(4)); // 测试插入之后原来数据是否后移 + assertEquals(3, arrayList.get(3)); // 测试插入之后原来数据是否后移 + + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet1() { + // 测试Get时,下标异常,是否可以正确抛出异常 + arrayList.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet2() { + // 测试Get时,下标异常,是否可以正确抛出异常 + arrayList.get(0); + } + + @Test + public void testGet() { + arrayList.add(1); + arrayList.add(2); + arrayList.add(3); + assertEquals(1, arrayList.get(0)); + assertEquals(2, arrayList.get(1)); + assertEquals(3, arrayList.get(2)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove1() { + arrayList.remove(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove2() { + arrayList.remove(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForRemove3() { + arrayList.remove(1); + } + + @Test + public void testRemove() { + arrayList.add(1); + arrayList.remove(0); + assertEquals(0, arrayList.size()); + + arrayList.add(1); + arrayList.add(2); + arrayList.remove(0); + assertEquals(1, arrayList.size()); + assertEquals(2, arrayList.get(0)); + } + + @Test + public void testSize() { + arrayList.add(1); + assertEquals(1, arrayList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = arrayList.iterator(); + assertFalse(iterator.hasNext()); + + arrayList.add(1); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); + } +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java index 83972b7776..537cd5961f 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/BinaryTreeNodeTest.java @@ -1,37 +1,37 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.BinaryTreeNode; - -public class BinaryTreeNodeTest { - - private BinaryTreeNode binaryTreeNode; - - @Before - public void setUp() throws Exception { - binaryTreeNode = new BinaryTreeNode(50); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testInsert() { - binaryTreeNode.insert(20); - binaryTreeNode.insert(30); - binaryTreeNode.insert(60); - binaryTreeNode.insert(80); - - assertEquals(20, binaryTreeNode.getLeft().getData()); - assertEquals(30, binaryTreeNode.getLeft().getRight().getData()); - assertEquals(60, binaryTreeNode.getRight().getData()); - assertEquals(80, binaryTreeNode.getRight().getRight().getData()); - } - -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + private BinaryTreeNode binaryTreeNode; + + @Before + public void setUp() throws Exception { + binaryTreeNode = new BinaryTreeNode(50); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testInsert() { + binaryTreeNode.insert(20); + binaryTreeNode.insert(30); + binaryTreeNode.insert(60); + binaryTreeNode.insert(80); + + assertEquals(20, binaryTreeNode.getLeft().getData()); + assertEquals(30, binaryTreeNode.getLeft().getRight().getData()); + assertEquals(60, binaryTreeNode.getRight().getData()); + assertEquals(80, binaryTreeNode.getRight().getRight().getData()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java index 054e8f81b4..52d42b5aa7 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/LinkedListTest.java @@ -1,143 +1,143 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Iterator; -import com.guodong.datastructure.LinkedList; - -public class LinkedListTest { - - private LinkedList linkedList; - - @Before - public void setUp() throws Exception { - linkedList = new LinkedList(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAddObject() { - linkedList.add(1); - assertEquals(1, linkedList.size()); - assertEquals(1, linkedList.get(0)); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd1() { - linkedList.add(-1, 1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForAdd2() { - linkedList.add(1, 1); - } - - @Test - public void testAddIntObject() { - linkedList.add(0, 1); - linkedList.add(1, 2); - assertEquals(1, linkedList.get(0)); - - linkedList.add(1,3); - assertEquals(2, linkedList.get(2)); - assertEquals(3, linkedList.get(1)); - assertEquals(3, linkedList.size()); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet1() { - linkedList.get(-1); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet2() { - linkedList.get(0); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testExceptionForGet3() { - linkedList.get(1); - } - - @Test - public void testGet() { - linkedList.add(0, 1); - linkedList.add(1, 2); - assertEquals(2, linkedList.get(1)); - } - - @Test - public void testGetLast() { - linkedList.add(1); - assertEquals(1, linkedList.getLast()); - - linkedList.add(2); - assertEquals(2, linkedList.getLast()); - } - - @Test - public void testRemove() { - linkedList.add(1); - assertEquals(1, linkedList.remove(0)); - assertEquals(0, linkedList.size()); - } - - @Test - public void testSize() { - linkedList.add(1); - linkedList.add(1); - linkedList.add(1); - assertEquals(3, linkedList.size()); - } - - @Test - public void testAddFirst() { - linkedList.addFirst(1); - assertEquals(1, linkedList.get(0)); - - linkedList.addFirst(2); - linkedList.addFirst(3); - assertEquals(3, linkedList.get(0)); - assertEquals(1, linkedList.getLast()); - } - - @Test - public void testAddLast() { - linkedList.addLast(1); - assertEquals(1, linkedList.getLast()); - assertEquals(1, linkedList.get(0)); - } - - @Test - public void testRemoveFirst() { - linkedList.addFirst(1); - assertEquals(1, linkedList.removeFirst()); - assertEquals(0, linkedList.size()); - } - - @Test - public void testRemoveLast() { - linkedList.addLast(2); - assertEquals(2, linkedList.removeLast()); - assertEquals(0, linkedList.size()); - } - - @Test - public void testIterator() { - Iterator iterator = linkedList.iterator(); - assertFalse(iterator.hasNext()); - - linkedList.add(1); - assertTrue(iterator.hasNext()); - assertEquals(1, iterator.next()); - assertFalse(iterator.hasNext()); - } - -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Iterator; +import com.guodong.datastructure.LinkedList; + +public class LinkedListTest { + + private LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddObject() { + linkedList.add(1); + assertEquals(1, linkedList.size()); + assertEquals(1, linkedList.get(0)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd1() { + linkedList.add(-1, 1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForAdd2() { + linkedList.add(1, 1); + } + + @Test + public void testAddIntObject() { + linkedList.add(0, 1); + linkedList.add(1, 2); + assertEquals(1, linkedList.get(0)); + + linkedList.add(1,3); + assertEquals(2, linkedList.get(2)); + assertEquals(3, linkedList.get(1)); + assertEquals(3, linkedList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet1() { + linkedList.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet2() { + linkedList.get(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testExceptionForGet3() { + linkedList.get(1); + } + + @Test + public void testGet() { + linkedList.add(0, 1); + linkedList.add(1, 2); + assertEquals(2, linkedList.get(1)); + } + + @Test + public void testGetLast() { + linkedList.add(1); + assertEquals(1, linkedList.getLast()); + + linkedList.add(2); + assertEquals(2, linkedList.getLast()); + } + + @Test + public void testRemove() { + linkedList.add(1); + assertEquals(1, linkedList.remove(0)); + assertEquals(0, linkedList.size()); + } + + @Test + public void testSize() { + linkedList.add(1); + linkedList.add(1); + linkedList.add(1); + assertEquals(3, linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.addFirst(1); + assertEquals(1, linkedList.get(0)); + + linkedList.addFirst(2); + linkedList.addFirst(3); + assertEquals(3, linkedList.get(0)); + assertEquals(1, linkedList.getLast()); + } + + @Test + public void testAddLast() { + linkedList.addLast(1); + assertEquals(1, linkedList.getLast()); + assertEquals(1, linkedList.get(0)); + } + + @Test + public void testRemoveFirst() { + linkedList.addFirst(1); + assertEquals(1, linkedList.removeFirst()); + assertEquals(0, linkedList.size()); + } + + @Test + public void testRemoveLast() { + linkedList.addLast(2); + assertEquals(2, linkedList.removeLast()); + assertEquals(0, linkedList.size()); + } + + @Test + public void testIterator() { + Iterator iterator = linkedList.iterator(); + assertFalse(iterator.hasNext()); + + linkedList.add(1); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java index 86a4ebdd68..1773b5027c 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/QueueTest.java @@ -1,62 +1,62 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import java.util.NoSuchElementException; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Queue; - -public class QueueTest { - - private Queue queue; - - @Before - public void setUp() throws Exception { - queue = new Queue(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEnQueue() { - queue.enQueue(1); - assertFalse(queue.isEmpty()); - } - - @Test(expected = NoSuchElementException.class) - public void testDeQueueExecption() { - queue.deQueue(); - } - - @Test - public void testDeQueue() { - queue.enQueue(1); - assertEquals(1, queue.deQueue()); - assertTrue(queue.isEmpty()); - } - - @Test - public void testIsEmpty() { - queue.enQueue(1); - assertFalse(queue.isEmpty()); - - queue.deQueue(); - assertTrue(queue.isEmpty()); - } - - @Test - public void testSize() { - queue.enQueue(1); - queue.enQueue(1); - queue.enQueue(1); - - assertEquals(3, queue.size()); - } - -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import java.util.NoSuchElementException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Queue; + +public class QueueTest { + + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEnQueue() { + queue.enQueue(1); + assertFalse(queue.isEmpty()); + } + + @Test(expected = NoSuchElementException.class) + public void testDeQueueExecption() { + queue.deQueue(); + } + + @Test + public void testDeQueue() { + queue.enQueue(1); + assertEquals(1, queue.deQueue()); + assertTrue(queue.isEmpty()); + } + + @Test + public void testIsEmpty() { + queue.enQueue(1); + assertFalse(queue.isEmpty()); + + queue.deQueue(); + assertTrue(queue.isEmpty()); + } + + @Test + public void testSize() { + queue.enQueue(1); + queue.enQueue(1); + queue.enQueue(1); + + assertEquals(3, queue.size()); + } + +} diff --git a/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java b/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java index 36781c863f..74ac8e7cc7 100644 --- a/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java +++ b/group12/377401843/learning_1/src/com/guodong/datastructure/test/StackTest.java @@ -1,46 +1,46 @@ -package com.guodong.datastructure.test; - -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.guodong.datastructure.Stack; - -public class StackTest { - - private Stack stack; - - @Before - public void setUp() throws Exception { - stack = new Stack(); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testPush() { - stack.push(11); - assertEquals(11, stack.pop()); - assertTrue(stack.isEmpty()); - } - - @Test - public void testPop() { - stack.push(11); - assertEquals(11, stack.pop()); - assertTrue(stack.isEmpty()); - } - - @Test - public void testPeek() { - stack.push(11); - assertEquals(11, stack.peek()); - assertFalse(stack.isEmpty()); - assertEquals(1, stack.size()); - } - -} +package com.guodong.datastructure.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.guodong.datastructure.Stack; + +public class StackTest { + + private Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPush() { + stack.push(11); + assertEquals(11, stack.pop()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testPop() { + stack.push(11); + assertEquals(11, stack.pop()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testPeek() { + stack.push(11); + assertEquals(11, stack.peek()); + assertFalse(stack.isEmpty()); + assertEquals(1, stack.size()); + } + +} diff --git a/group12/441908378/ArrayList.java b/group12/441908378/ArrayList.java old mode 100755 new mode 100644 index 45e495867c..74f49a39d1 --- a/group12/441908378/ArrayList.java +++ b/group12/441908378/ArrayList.java @@ -1,50 +1,50 @@ -import java.util.Arrays; - -public class ArrayList { - -private int size = 0; - -private Object[] elementData = new Object[100]; - -public void enlargeCapacity(int minCapacity){ - int oldCapacity=elementData.length; - if(oldCapacityb){ - return left; - }else{ - return right; - } - } - - -} + +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){ + BinaryTreeNode node; + do{ + node=compare(o); + }while(node==null); + node.data=o; + return node; + } + + public BinaryTreeNode compare(Object o){ + int a=(Integer)data; + int b=(Integer)o; + if(a>b){ + return left; + }else{ + return right; + } + } + + +} diff --git a/group12/441908378/LinkedList.java b/group12/441908378/LinkedList.java old mode 100755 new mode 100644 index 0d0339bc01..456160f154 --- a/group12/441908378/LinkedList.java +++ b/group12/441908378/LinkedList.java @@ -1,121 +1,121 @@ -public class LinkedList { - -private Node head; - -private static class Node{ - Object data; - Node next; -} - -public boolean hasNext(Node a){ - if(a.next!=null){ - return true; - } - return false; -} - -public Node getIndex(int index){ - Node a=head.next; - for(int i=0;i= 0 && index <= size() Ϊ true + * @param index ӵλ + * @param o ӵԪ + */ + public void add(int index, Object o){ + if(index > size || index < 0){ + //sizeԪص±꣬indexsizeҲԡ + throw new ArrayIndexOutOfBoundsException("Index:" + index); + } + checkSize(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + /** + * ȡindexλôԪ + * indexӦ index >= 0 && index < size() Ϊ true + */ + public Object get(int index){ + checkIndex(index); + return elementData[index]; + } + + /** + * ɾindexλôԪ + * indexӦ index >= 0 && index < size() Ϊ true + * @return ɾԪ + */ + public Object remove(int index){ + checkIndex(index); + Object obj = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return obj; + } + + /** + * ȡǰArrayListѾ洢Ԫصĸ + */ + public int size(){ + return this.size; + } + + /** + * ȡǰArrayListĵ + * @return ǰArrayListĵ + */ + public Iterator iterator(){ + return new Iterator(){ + private int count = 0; + + @Override + public boolean hasNext() { + return size > count; + } + + @Override + public Object next() { + if(count == size){ + throw new RuntimeErrorException(null, "ûиԪأ"); + } + return get(count++); + } + + @Override + public void remove() { + ArrayList.this.remove(count - 1); + } + + }; + } + + /** + * getremove±ǷԽ + * @param index ± + */ + private void checkIndex(int index){ + if(index >= size || index < 0){ + //sizeԪص±꣬index±ϻûд洢ݣ޷getremove + throw new ArrayIndexOutOfBoundsException("Index:" + index); + } + } + + /** + * ArrayListǷҪ + * ArrayListҪʱݣÿݽʹ20 + */ + private void checkSize(){ + if(size < elementData.length) return; + elementData = Arrays.copyOf(elementData, elementData.length + 20/*(int)(elementData.length * 1.2)*/); + } + +} diff --git a/group13/1274639949/lesson01/src/com/hans/BinaryTree.java b/group13/1274639949/lesson01/src/com/hans/BinaryTree.java new file mode 100644 index 0000000000..a4bf39dd3a --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/BinaryTree.java @@ -0,0 +1,10 @@ +package com.hans; + +public class BinaryTree { + BinaryTreeNode root = new BinaryTreeNode(); + + public boolean add(Object obj){ + + return true; + } +} diff --git a/group13/1274639949/lesson01/src/com/hans/BinaryTreeNode.java b/group13/1274639949/lesson01/src/com/hans/BinaryTreeNode.java new file mode 100644 index 0000000000..683868be1d --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.hans; + +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/group13/1274639949/lesson01/src/com/hans/Iterator.java b/group13/1274639949/lesson01/src/com/hans/Iterator.java new file mode 100644 index 0000000000..4ec870ed01 --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/Iterator.java @@ -0,0 +1,8 @@ +package com.hans; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public void remove(); +} + diff --git a/group13/1274639949/lesson01/src/com/hans/LinkedList.java b/group13/1274639949/lesson01/src/com/hans/LinkedList.java new file mode 100644 index 0000000000..8e2d421501 --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/LinkedList.java @@ -0,0 +1,148 @@ +package com.hans; + +public class LinkedList implements List { + + private int size; + + private Node head; + + public LinkedList() { + head = new Node(); +// head.data = head; + } + + /** + * һԪ + */ + public void add(Object o){ + Node tail = getTail(); + + Node node = new Node(); + node.data = o; + + tail.next = node; + size++; + return; + } + + /** + * ָλüһԪ + * @param index ָλãӦ index > 0 && index <= size() Ϊ true + */ + public void add(int index , Object o){ + if(index < 0 || index > size) + throw new IndexOutOfBoundsException("Index:" + index); + + Node pos = head; + for(int i = 0; i < index; i++){ + //ҪindexλһԪأֻҪȡ index - 1 λõԪؼ + pos = pos.next; + } + Node node = new Node(); + node.data = o; + node.next = pos.next; + pos.next = node; + size++; + return; + } + + /** + * ȡָλôԪ + * @param index ҪȡԪصλãӦ index > 0 && index < size() Ϊ true + */ + public Object get(int index){ + checkIndex(index); + Node pos = head; + for(int i = 0; i <= index; i++){ + //Ϊ <= ,ΪҪȡindexλ + pos = pos.next; + } + return pos.data; + } + + /** + * ƳָλôԪ + * @param index ҪƳԪصλãӦ index > 0 && index < size() Ϊ true + */ + public Object remove(int index){ + checkIndex(index); + Node pos = head; + for(int i = 0; i < index; i++){ + pos = pos.next; + } + Node temp = pos.next; + pos.next = temp.next; + size--; + return temp.data; + } + + /** + * ȡ洢Ԫصĸ + */ + public int size(){ + return this.size; + } + + /** + * ڵһԪصǰһԪ + * @param o + */ + public void addFirst(Object o){ + add(0, o); + } + + /** + * һԪصĺһԪ + * @param o + */ + public void addLast(Object o){ + add(o); + } + + /** + * ƳһԪ + * @return ƳԪ + */ + public Object removeFirst(){ + return remove(0); + } + + /** + * ƳһԪ + * @return ƳԪ + */ + public Object removeLast(){ + return remove(size - 1); + } + + public Iterator iterator(){ + return null; + } + + + private static final class Node{ + Object data; + Node next; + } + + /** + * ȡһڵ + * @return + */ + private Node getTail(){ + Node tail = head; + while(tail.next != null){ + tail = tail.next; + } + return tail; + } + /** + * getremoveԪǷЧ + * @param index + */ + private void checkIndex(int index) { + if(index < 0 || index >= size) + throw new IndexOutOfBoundsException("Index:" + index); + } +} + diff --git a/group13/1274639949/lesson01/src/com/hans/List.java b/group13/1274639949/lesson01/src/com/hans/List.java new file mode 100644 index 0000000000..f8ffaa8f5e --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/List.java @@ -0,0 +1,10 @@ +package com.hans; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + public Iterator iterator(); +} diff --git a/group13/1274639949/lesson01/src/com/hans/Queue.java b/group13/1274639949/lesson01/src/com/hans/Queue.java new file mode 100644 index 0000000000..2c696cca10 --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/Queue.java @@ -0,0 +1,22 @@ +package com.hans; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group13/1274639949/lesson01/src/com/hans/Stack.java b/group13/1274639949/lesson01/src/com/hans/Stack.java new file mode 100644 index 0000000000..d370221f1a --- /dev/null +++ b/group13/1274639949/lesson01/src/com/hans/Stack.java @@ -0,0 +1,30 @@ +package com.hans; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size() <= 0){ + return null; + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if(elementData.size() <= 0){ + return null; + } + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} + diff --git "a/group13/1274639949/\346\214\207\344\273\244\346\274\253\346\270\270\350\256\260.md" "b/group13/1274639949/\346\214\207\344\273\244\346\274\253\346\270\270\350\256\260.md" new file mode 100644 index 0000000000..a89412cc01 --- /dev/null +++ "b/group13/1274639949/\346\214\207\344\273\244\346\274\253\346\270\270\350\256\260.md" @@ -0,0 +1 @@ +如今计算机已经深入走进我们生活的方方面面,购物、娱乐、餐饮······,可以说我们无时无刻不在利用计算机给我们的 diff --git a/group13/1641296572/lesson1/.gitignore b/group13/1641296572/lesson1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group13/1641296572/lesson1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group13/1641296572/lesson1/src/com/coding/basic/ArrayList.java b/group13/1641296572/lesson1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c9d4661987 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,107 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List +{ + + private int size = 0; + + private Object[] elementData = new Object[3]; + + public void add(Object o) + { + if (elementData.length == size) + { + expand(); + } + elementData[size++] = o; + } + + private void expand() + { + Object[] newDatas = new Object[size * 3 / 2 + 1]; + System.arraycopy(elementData, 0, newDatas, 0, size); + elementData = newDatas; + System.out.println("expand: from :" + size + " to " + size * 2); + + } + + public void add(int index, Object o) + { + if (index > size || index < 0) + { + throw new IndexOutOfBoundsException("index=" + index + " , size=" + size); + } + if (elementData.length == size) + { + expand(); + } + + Object[] tmps = new Object[size - index]; + System.arraycopy(elementData, index, tmps, 0, size - index); + elementData[index] = o; + System.arraycopy(tmps, 0, elementData, index + 1, size - index); + + size++; + } + + public Object get(int index) + { + if (index >= size || index < 0) + { + throw new IndexOutOfBoundsException("index=" + index + " , size=" + size); + } + + return elementData[index]; + } + + public Object remove(int index) + { + if (index >= size || index < 0) + { + throw new IndexOutOfBoundsException("index=" + index + " , size=" + size); + } + Object rt = elementData[index]; + + Object[] tmps = new Object[size - index - 1]; + System.arraycopy(elementData, index + 1, tmps, 0, size - index - 1); + System.arraycopy(tmps, 0, elementData, index, size - index - 1); + elementData[--size] = null; + + return rt; + } + + public int size() + { + return size; + } + + public Iterator iterator() + { + return new Iterator(){ + int pos =0; + @Override + public boolean hasNext() + { + if(pos < size) + { + return true; + } + return false; + } + + @Override + public Object next() + { + if(pos < size) + { + throw new NoSuchElementException(); + } + return elementData[pos++]; + }}; + } + +} + + diff --git a/group13/1641296572/lesson1/src/com/coding/basic/BinarySortTree.java b/group13/1641296572/lesson1/src/com/coding/basic/BinarySortTree.java new file mode 100644 index 0000000000..1aa36825c4 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/BinarySortTree.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +public class BinarySortTree +{ + BinaryTreeNode root = null; + int size =0; + + public void insert(Object o) + { + BinaryTreeNode node = new BinaryTreeNode(); + node.setData(o); + node.setLeft(null); + node.setRight(null); + + if(null == root) + { + root = node; + size++; + } + + BinaryTreeNode p = root; + BinaryTreeNode pre = p; + while(p!= null) + { + if(objectCompare(o, p.getData())<0) + { + pre = p; + p=p.getLeft(); + } + else + { + pre = p; + p=p.getRight(); + } + } + + if(objectCompare(o, pre.getData())<0) + { + pre.setLeft(node); + } + else + { + pre.setRight(node); + } + + size++; + } + + private int objectCompare(Object o1, Object o2) + { +// return o.toString().compareTo(o2.toString()); + return (Integer) o1 - (Integer)o2; + } + + public void print(BinaryTreeNode r) + { + if(r==null) + { + return; + } + System.out.print(r.getData() + "->"); + print(r.getLeft()); + print(r.getRight()); + } + +} diff --git a/group13/1641296572/lesson1/src/com/coding/basic/BinaryTreeNode.java b/group13/1641296572/lesson1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d0ba079f40 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,41 @@ +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; + } + + +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/Iterator.java b/group13/1641296572/lesson1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..4175d7d2dd --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface Iterator +{ + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/LinkedList.java b/group13/1641296572/lesson1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..9bd71ed325 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,220 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List +{ + + private Node head; + private int size = 0; + + public void add(Object o) + { + if (0 == size) + { + head = new Node(); + head.data = o; + head.next = null; + } else + { + Node pNode = head; + while (pNode.next != null) + { + pNode = pNode.next; + } + Node node = new Node(); + node.data = o; + pNode.next = node; + node.next = null; + } + size++; + } + + public void add(int index, Object o) + { + if (index < 0 || index > size) + { + throw new IndexOutOfBoundsException(); + } + Node node = new Node(); + node.data = o; + + if (index != 0) + { + Node nowNode = head; + Node preNode = head; + for (int i = 0; i < index; i++) + { + preNode = nowNode; + nowNode = nowNode.next; + } + preNode.next = node; + node.next = nowNode; + } else + { + node.next = head; + head = node; + + } + size++; + } + + public Object get(int index) + { + if (index < 0 || index >= size) + { + throw new IndexOutOfBoundsException(); + } + Node node = head; + for (int i = 0; i < index; i++) + { + node = node.next; + } + return node.data; + } + + public Object remove(int index) + { + if (index < 0 || index >= size) + { + throw new IndexOutOfBoundsException(); + } + Object rt = null; + if (index == 0) + { + rt = head.data; + Node node = head; + head = head.next; + node.next = null; + node.data = null; + } else + { + Node preNode = head; + Node nowNode = head; + for (int i = 0; i < index; i++) + { + preNode = nowNode; + nowNode = nowNode.next; + } + rt = nowNode.data; + preNode.next = nowNode.next; + nowNode.next = null; + nowNode.data = null; + } + size--; + return rt; + } + + public int size() + { + return size; + } + + public void addFirst(Object o) + { + Node node = new Node(); + node.data = o; + node.next = head; + head = node; + + size++; + } + + public void addLast(Object o) + { + Node node = new Node(); + node.data = o; + node.next = null; + + Node nowNode = head; + + if (size == 0) + { + head = node; + size++; + return; + } + + while (nowNode != null && nowNode.next != null) + { + nowNode = nowNode.next; + } + + nowNode.next = node; + size++; + } + + public Object removeFirst() + { + if (size == 0) + { + throw new NoSuchElementException(); + } + Node node = head; + head = head.next; + Object rt = node.data; + node.next = null; + node.data = null; + size--; + return rt; + } + + public Object removeLast() + { + if (size == 0) + { + throw new NoSuchElementException(); + } + + Node nowNode = head; + Node preNode = head; + while (nowNode.next != null) + { + preNode = nowNode; + nowNode = nowNode.next; + } + + preNode.next = null; + Object rt = nowNode.data; + nowNode.data = null; + size--; + return rt; + } + + public Iterator iterator() + { + return new Iterator() + { + Node node = head; + + @Override + public boolean hasNext() + { + if (null != node) + { + return true; + } + return false; + } + + @Override + public Object next() + { + if (null == node) + { + throw new NoSuchElementException(); + } + Object o = node.data; + node = node.next; + return o; + } + }; + } + + private static class Node + { + Object data; + Node next; + + } +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/List.java b/group13/1641296572/lesson1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..cf5d612814 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/List.java @@ -0,0 +1,14 @@ +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(); +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/Queue.java b/group13/1641296572/lesson1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..695c953324 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/Queue.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Queue +{ + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) + { + list.add(o); + } + + public Object deQueue() + { + Object rt = list.removeFirst(); + return rt; + } + + public boolean isEmpty() + { + return 0==list.size(); + } + + public int size() + { + return list.size(); + } + +} \ No newline at end of file diff --git a/group13/1641296572/lesson1/src/com/coding/basic/Stack.java b/group13/1641296572/lesson1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..424263f0b7 --- /dev/null +++ b/group13/1641296572/lesson1/src/com/coding/basic/Stack.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +public class Stack +{ + private ArrayList elementData = new ArrayList(); + + public void push(Object o) + { + elementData.add(elementData.size(), o); + } + + public Object pop() + { + int size = elementData.size(); + if(size==0) + { + throw new EmptyStackException(); + } + + Object rt = elementData.get(size-1); + elementData.remove(size-1); + return rt; + } + + public Object peek() + { + if(elementData.size()==0) + { + throw new EmptyStackException(); + } + + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty() + { + return 0==elementData.size(); + } + + public int size() + { + return elementData.size(); + } + + public static void main(String []args) + { + Stack st = new Stack(); + System.out.println("is Empty:"+ st.isEmpty()); + for(int i=0;i<10;i++) + { + st.push("s="+ i); + } + + System.out.println("is Empty:"+ st.isEmpty()); + System.out.println(st.peek()); + for(int i=0;i<10;i++) + { + System.out.println("pop->" + st.pop()); + } + System.out.println("is Empty:"+ st.isEmpty()); + } + + +} \ No newline at end of file diff --git a/group13/2729382520/L1/src/io/github/vxzh/ArrayList.java b/group13/2729382520/L1/src/io/github/vxzh/ArrayList.java new file mode 100644 index 0000000000..39feef2cbd --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/ArrayList.java @@ -0,0 +1,135 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 22/02/2017. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData; + + public ArrayList() { + this.elementData = new Object[10]; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public boolean add(Object o) { + int minCapacity = size + 1; + if (minCapacity - elementData.length > 0) + grow(minCapacity); + elementData[size++] = o; + return true; + } + + public boolean add(int index, Object o) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + int minCapacity = size + 1; + if (minCapacity - elementData.length > 0) + grow(minCapacity); + elementData[index] = o; + size++; + return true; + } + + public boolean remove(Object o) { + if (o == null) { + for (int index = 0; index < size; index++) + if (elementData[index] == null) { + int moved = size - index - 1; + if (moved > 0) + copy(elementData, index + 1, elementData, index, moved); + elementData[--size] = null; + return true; + } + } else { + for (int index = 0; index < size; index++) + if (o.equals(elementData[index])) { + int moved = size - index - 1; + if (moved > 0) + copy(elementData, index + 1, elementData, index, moved); + elementData[--size] = null; + return true; + } + } + return false; + } + + public boolean remove(int index) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + int moved = size - index - 1; + if (moved > 0) + copy(elementData, index + 1, elementData, index, moved); + elementData[--size] = null; + return true; + } + + public Object get(int index) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + return elementData[index]; + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity - minCapacity < 0) + newCapacity = minCapacity; + elementData = copy(elementData, newCapacity); + + } + + public Object[] copy(Object[] src, int newCapacity) { + Object[] arr = new Object[newCapacity]; + for (int i = 0; i < src.length; i++) { + arr[i] = elementData[i]; + } + return arr; + } + + public void copy(Object[] src, int srcPost, Object[] dest, int destPost, int length) { + for (int i = 0; i < length; i++) { + dest[destPost + i] = src[srcPost + i]; + } + } + + public Iterator iterator() { + return new ListIterator(this); + } + + public class ListIterator implements Iterator { + + private List list; + private int endIndex = 0; + private int index = 0; + + public ListIterator(ArrayList list) { + this.list = list; + this.endIndex = list.size(); + } + + @Override + public boolean hasNext() { + return this.index < this.endIndex; + } + + @Override + public Object next() { + if (!this.hasNext()) { + throw new RuntimeException("EmptyElementException"); + } else { + return list.get(index++); + } + } + } +} + diff --git a/group13/2729382520/L1/src/io/github/vxzh/BinaryTree.java b/group13/2729382520/L1/src/io/github/vxzh/BinaryTree.java new file mode 100644 index 0000000000..9b6a9d7031 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/BinaryTree.java @@ -0,0 +1,68 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 26/02/2017. + */ +public class BinaryTree { + + private TreeNode root; + + public boolean isEmpty() { + return root == null; + } + + //节点个数 + public int size() { + return size(root); + } + + private int size(TreeNode subTree) { + if (subTree == null) { + return 0; + } else { + return 1 + size(subTree.leftChild) + + size(subTree.rightChild); + } + } + + public void insert(int o) { + TreeNode newNode = new TreeNode(o, null, null); + if (root == null) + root = newNode; + else { + TreeNode current = root; + while (true) { + if (o < current.data) { + current = current.leftChild; + if (current == null) { + current.leftChild = newNode; + return; + } + } else { + current = current.rightChild; + if (current == null) { + current.rightChild = newNode; + return; + } + } + } + + } + + } + + private class TreeNode { + private int data; + private TreeNode leftChild; + private TreeNode rightChild; + + public TreeNode() { + } + + public TreeNode(int o, TreeNode l, TreeNode r) { + this.data = o; + this.leftChild = l; + this.rightChild = r; + } + } +} diff --git a/group16/214074094/src/com/coding/basic/Iterator.java b/group13/2729382520/L1/src/io/github/vxzh/Iterator.java similarity index 74% rename from group16/214074094/src/com/coding/basic/Iterator.java rename to group13/2729382520/L1/src/io/github/vxzh/Iterator.java index 1acc5349a4..9db09eb9d4 100644 --- a/group16/214074094/src/com/coding/basic/Iterator.java +++ b/group13/2729382520/L1/src/io/github/vxzh/Iterator.java @@ -1,4 +1,4 @@ -package coding.basic; +package io.github.vxzh; public interface Iterator { @@ -6,4 +6,4 @@ public interface Iterator { Object next(); -} +} \ No newline at end of file diff --git a/group13/2729382520/L1/src/io/github/vxzh/LinkQueue.java b/group13/2729382520/L1/src/io/github/vxzh/LinkQueue.java new file mode 100644 index 0000000000..9f065c1879 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/LinkQueue.java @@ -0,0 +1,57 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 24/02/2017. + */ +public class LinkQueue implements Queue { + + private int size = 0; + + private Node front; + private Node rear; + + /** + * 入队 + */ + public void enQueue(Object o) { + Node node = rear; + Node newNode = new Node(o, null); + rear = newNode; + if (node == null) + front = newNode; + else + node.next = newNode; + size++; + } + + /** + * 出队 + */ + public Object deQueue() { + if (isEmpty()) + throw new RuntimeException("EmptyQueueException"); + Node node = front; + front = node.next; + size--; + return node.data; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + + Node(Object element, Node next) { + this.data = element; + this.next = next; + } + + } +} diff --git a/group13/2729382520/L1/src/io/github/vxzh/LinkedList.java b/group13/2729382520/L1/src/io/github/vxzh/LinkedList.java new file mode 100644 index 0000000000..e599bf15e7 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/LinkedList.java @@ -0,0 +1,138 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 23/02/2017. + */ +public class LinkedList implements List { + + private int size = 0; + + private Node head; + private Node tail; + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + private void linkLast(Object o) { + Node node = tail; + Node newNode = new Node(tail, o, null); + tail = newNode; + if (node == null) + head = newNode; + else + node.next = newNode; + size++; + } + + private void linkBefore(Object o, Node node) { + Node prev = node.prev; + Node newNode = new Node(prev, o, node); + node.prev = newNode; + if (prev == null) + head = newNode; + else + prev.next = newNode; + size++; + } + + public boolean add(Object o) { + linkLast(o); + return true; + } + + public boolean add(int index, Object o) { + if (index > size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + if (index == size) + linkLast(o); + else + linkBefore(o, node(index)); + return true; + } + + public boolean remove(Object o) { + if (o == null) { + return false; + } else { + for (Node x = head; x != null; x = x.next) { + if (o.equals(x.data)) { + unlink(x); + return true; + } + } + } + return false; + } + + public boolean remove(int index) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + unlink(node(index)); + return true; + } + + public Object get(int index) { + if (index >= size || index < 0) + throw new RuntimeException("IndexOutOfBoundsException"); + return node(index).data; + } + + private void unlink(Node x) { + Node prev = x.prev; + Node next = x.next; + + if (prev == null) { + head = next; + } else { + prev.next = next; + x.prev = null; + } + + if (next == null) { + tail = prev; + } else { + next.prev = prev; + x.next = null; + } + + x.data = null; + size--; + } + + private Node node(int index) { + if (index < (size >> 1)) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } else { + Node x = tail; + for (int i = size - 1; i > index; i--) + x = x.prev; + return x; + } + } + + + private static class Node { + Object data; + Node prev; + Node next; + + Node(Node prev, Object element, Node next) { + this.data = element; + this.next = next; + this.prev = prev; + } + + } + +} + + + diff --git a/group13/2729382520/L1/src/io/github/vxzh/List.java b/group13/2729382520/L1/src/io/github/vxzh/List.java new file mode 100644 index 0000000000..6e52bd58f6 --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/List.java @@ -0,0 +1,22 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 22/02/2017. + */ +public interface List { + + int size(); + + boolean isEmpty(); + + boolean add(Object o); + + boolean add(int index, Object o); + + boolean remove(Object o); + + boolean remove(int index); + + Object get(int index); + +} diff --git a/group13/2729382520/L1/src/io/github/vxzh/Queue.java b/group13/2729382520/L1/src/io/github/vxzh/Queue.java new file mode 100644 index 0000000000..258229c17b --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/Queue.java @@ -0,0 +1,15 @@ +package io.github.vxzh; + +/** + * Created by xuxiaoqing on 26/02/2017. + */ +public interface Queue { + + void enQueue(Object o); + + Object deQueue(); + + boolean isEmpty(); + + int size(); +} diff --git a/group13/2729382520/L1/src/io/github/vxzh/Stack.java b/group13/2729382520/L1/src/io/github/vxzh/Stack.java new file mode 100644 index 0000000000..de7c8ede4d --- /dev/null +++ b/group13/2729382520/L1/src/io/github/vxzh/Stack.java @@ -0,0 +1,41 @@ +package io.github.vxzh; + +/** + * Created by vxzh on 24/02/2017. + */ +public class Stack { + + private ArrayList elementData; + + public Stack() { + this.elementData = new ArrayList(); + } + + public int size() { + return elementData.size(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + Object obj; + int len = elementData.size(); + obj = peek(); + elementData.remove(len - 1); + return obj; + } + + public Object peek() { + int len = elementData.size(); + if (len == 0) + throw new RuntimeException("EmptyStackException"); + return elementData.get(len - 1); + } + +} diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties new file mode 100644 index 0000000000..e23811b654 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties @@ -0,0 +1 @@ +#Sun Feb 19 10:59:39 CST 2017 diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties.lock b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties.lock new file mode 100644 index 0000000000..c5daff8bbe Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/cache.properties.lock differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileHashes.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileHashes.bin new file mode 100644 index 0000000000..6512ad40b8 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileHashes.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileSnapshots.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileSnapshots.bin new file mode 100644 index 0000000000..357f65c480 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/fileSnapshots.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/taskArtifacts.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/taskArtifacts.bin new file mode 100644 index 0000000000..1dcc7e5225 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.1/taskArtifacts/taskArtifacts.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileHashes.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileHashes.bin new file mode 100644 index 0000000000..561100c562 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileHashes.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileSnapshots.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileSnapshots.bin new file mode 100644 index 0000000000..a2d12a13b5 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/fileSnapshots.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.bin b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.bin new file mode 100644 index 0000000000..18c54c8bd5 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.bin differ diff --git a/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.lock b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.lock new file mode 100644 index 0000000000..bed9c81dee Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/.gradle/3.3/taskArtifacts/taskArtifacts.lock differ diff --git a/group13/2931408816/lesson1/data-structure/build.gradle b/group13/2931408816/lesson1/data-structure/build.gradle new file mode 100644 index 0000000000..0049406393 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/build.gradle @@ -0,0 +1,28 @@ +group 'cn.net.pikachu' +version '1.0-SNAPSHOT' + +buildscript { + ext.kotlin_version = '1.0.6' + + repositories { + mavenCentral() + } + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'java' +apply plugin: 'kotlin' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + testCompile group: 'junit', name: 'junit', version: '4.11' + +} diff --git a/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.jar b/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..6ffa237849 Binary files /dev/null and b/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.jar differ diff --git a/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.properties b/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..84514df01f --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Feb 19 11:20:26 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/group13/2931408816/lesson1/data-structure/gradlew b/group13/2931408816/lesson1/data-structure/gradlew new file mode 100644 index 0000000000..9aa616c273 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/gradlew @@ -0,0 +1,169 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/group13/2931408816/lesson1/data-structure/gradlew.bat b/group13/2931408816/lesson1/data-structure/gradlew.bat new file mode 100644 index 0000000000..f9553162f1 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/group13/2931408816/lesson1/data-structure/settings.gradle b/group13/2931408816/lesson1/data-structure/settings.gradle new file mode 100644 index 0000000000..941deed614 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'data-structure' + diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/ArrayList.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/ArrayList.java new file mode 100644 index 0000000000..8cc192feeb --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/ArrayList.java @@ -0,0 +1,97 @@ +package cn.net.pikachu.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private class Itr implements Iterator { + private int curIndex = 0; + + @Override + public boolean hasNext() { + return curIndex < size; + } + + @Override + public Object next() { + return elementData[curIndex++]; + } + } + + public void add(Object o) { + if (size == elementData.length) { + grow(); + } + elementData[size++] = o; + } + + private void grow() { + if (elementData.length < 2048) { + Arrays.copyOf(elementData, elementData.length * 2); + } else { + Arrays.copyOf(elementData, elementData.length + 1024); + } + } + + public void add(int index, Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } else if (index == size) { + add(o); + } else { + if (size == elementData.length) { + grow(); + } + for (int i = size; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + } + size++; + + } + + public Object get(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size--; + return o; + } + + public int size() { + return size; + } + + // 迭代器留在后面写 + public Iterator iterator() { + return new Itr(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("["); + for (int i = 0; i < size - 1; i++) { + builder.append(elementData[i]).append(","); + } + if (size > 0) { + builder.append(elementData[size - 1]); + } + builder.append("]"); + return builder.toString(); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/BinaryTreeNode.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..0c4d545e8a --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/BinaryTreeNode.java @@ -0,0 +1,60 @@ +package cn.net.pikachu.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 instanceof Comparable)){ + throw new ClassCastException(o.toString()); + } + // 根据节点大小放置元素 小的在左边 大的在右边 + Comparable c = (Comparable) o; + if (data==null){ + data=o; + return this; + }else if (c.compareTo(o)>0){ + if (left==null){ + left=new BinaryTreeNode(); + } + return left.insert(o); + }else { + if (right==null){ + right=new BinaryTreeNode(); + } + return right.insert(o); + } + } + public void inOrderTraversal(Visit visit,BinaryTreeNode node){ + if (node.left!=null) + inOrderTraversal(visit,node.left); + if (node!=null) + visit.visit(node.data); + if (node.right!=null) + inOrderTraversal(visit,node.right); + } + public static interface Visit{ + public void visit(Object o); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Iterator.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Iterator.java new file mode 100644 index 0000000000..05e8486430 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Iterator.java @@ -0,0 +1,7 @@ +package cn.net.pikachu.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/LinkedList.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/LinkedList.java new file mode 100644 index 0000000000..2c29d0f085 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/LinkedList.java @@ -0,0 +1,161 @@ +package cn.net.pikachu.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int curSize = 0; + + private class Itr implements Iterator { + Node curNode = head; + + @Override + public boolean hasNext() { + return curNode != null; + } + + @Override + public Object next() { + Node node = curNode; + curNode = curNode.next; + return node.data; + } + } + + public void add(Object o) { + if (head == null) { + head = new Node(o, null); + } else { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.next = new Node(o, null); + } + curSize++; + } + + public void add(int index, Object o) { + // 这里可以等于 + if (index < 0 || index > curSize) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + if (index == 0) { + addFirst(o); + } else { + + Node node = head; + for (int i = 1; i < index; i++) { + node = node.next; + } + node.next = new Node(o, node.next); + curSize++; + } + + } + + public Object get(int index) { + if (index < 0 || index >= curSize) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + if (index < 0 || index >= curSize) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + if (index == 0) { + return removeFirst(); + } + + Node node = head; + for (int i = 1; i < index; i++) { + node = node.next; + } + Node t = node.next; + node.next=t.next; + curSize--; + return t.data; + } + + public int size() { + return curSize; + } + + public void addFirst(Object o) { + Node node = new Node(o, head); + head = node; + curSize++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + Node node = head; + head = head.next; + curSize--; + return node.data; + } + + public Object removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + Node node; + if (head.next == null) { + node = head; + head = null; + } else { + node = head; + while (node.next.next != null) { + node = node.next; + } + Node t = node.next; + node.next = null; + node = t; + } + curSize--; + return node.data; + } + + // 后面再实现 + public Iterator iterator() { + return new Itr(); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("["); + Node node = head; + while (node != null) { + builder.append(node.data).append(","); + node = node.next; + } + if (curSize>0) + builder.deleteCharAt(builder.length() - 1); + builder.append("]"); + return builder.toString(); + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/List.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/List.java new file mode 100644 index 0000000000..4272bde587 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/List.java @@ -0,0 +1,9 @@ +package cn.net.pikachu.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/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Main.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Main.java new file mode 100644 index 0000000000..89eda11fbe --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Main.java @@ -0,0 +1,17 @@ +package cn.net.pikachu.basic; + +/** + * Created by pikachu on 17-2-21. + */ +public class Main { + public static void main(String[] args) { + LinkedList list = new LinkedList(); + for (int i = 0; i < 3; i++) { + list.add(i); + } + Iterator iterator = list.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Queue.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Queue.java new file mode 100644 index 0000000000..7e033d0885 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Queue.java @@ -0,0 +1,30 @@ +package cn.net.pikachu.basic; + +import java.util.NoSuchElementException; + +public class Queue { + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()){ + throw new NoSuchElementException(); + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + return list.size()==0; + } + + public int size(){ + return list.size(); + } + + @Override + public String toString() { + return list.toString(); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Stack.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Stack.java new file mode 100644 index 0000000000..492d782b26 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/basic/Stack.java @@ -0,0 +1,33 @@ +package cn.net.pikachu.basic; + +import java.util.NoSuchElementException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()){ + throw new NoSuchElementException(); + } + 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(); + } + + @Override + public String toString() { + return elementData.toString(); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyArrayList.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyArrayList.java new file mode 100644 index 0000000000..ea3998b7c4 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyArrayList.java @@ -0,0 +1,212 @@ +package cn.net.pikachu.other; + +import java.util.AbstractList; +import java.util.Objects; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyArrayList extends AbstractList{ + // 当前能容纳的最大元素个数 + private int maxSize = 8; + // 当前数组已经有了的元素个数 + private int curSize = 0; + // 存放元素的数组 + private Object[] arr= new Object[curSize]; + /** + * {@inheritDoc} + * + * @param index + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + @SuppressWarnings("unchecked") + public E get(int index) { + if (index<0 || index > curSize) { + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + return (E) arr[index]; + } + + public int size() { + return curSize; + } + + /** + * Appends the specified element to the end of this list (optional + * operation). + *

+ *

Lists that support this operation may place limitations on what + * elements may be added to this list. In particular, some + * lists will refuse to add null elements, and others will impose + * restrictions on the type of elements that may be added. List + * classes should clearly specify in their documentation any restrictions + * on what elements may be added. + *

+ *

This implementation calls {@code add(size(), e)}. + *

+ *

Note that this implementation throws an + * {@code UnsupportedOperationException} unless + * {@link #add(int, Object) add(int, E)} is overridden. + * + * @param e element to be appended to this list + * @return {@code true} + * @throws UnsupportedOperationException if the {@code add} operation + * is not supported by this list + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws NullPointerException if the specified element is null and this + * list does not permit null elements + * @throws IllegalArgumentException if some property of this element + * prevents it from being added to this list + */ + @Override + public boolean add(E e) { + if (curSize>=maxSize){ + int t = maxSize; + int limit = 1024; + if(maxSizeset operation + * is not supported by this list + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws NullPointerException if the specified element is null and + * this list does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this list + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + @SuppressWarnings("unchecked") + public E set(int index, E element) { + if (index>curSize){ + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + E t = (E) arr[index]; + arr[index]=element; + return t; + } + + /** + * {@inheritDoc} + *

+ *

This implementation always throws an + * {@code UnsupportedOperationException}. + * + * @param index + * @throws UnsupportedOperationException {@inheritDoc} + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + @Override + @SuppressWarnings("unchecked") + public E remove(int index) { + if (index>curSize){ + throw new IndexOutOfBoundsException(String.valueOf(index)); + } + E t = (E) arr[index]; + if (index + *

This implementation calls {@code removeRange(0, size())}. + *

+ *

Note that this implementation throws an + * {@code UnsupportedOperationException} unless {@code remove(int + * index)} or {@code removeRange(int fromIndex, int toIndex)} is + * overridden. + * + * @throws UnsupportedOperationException if the {@code clear} operation + * is not supported by this list + */ + @Override + public void clear() { + // 父类使用了迭代器 +// super.clear(); + curSize=0; + } + + /** + * {@inheritDoc} + *

+ *

This implementation first gets a list iterator (with + * {@code listIterator()}). Then, it iterates over the list until the + * specified element is found or the end of the list is reached. + * + * @param o + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException {@inheritDoc} + */ + @Override + public int indexOf(Object o) { + // 父类使用了迭代器 +// return super.indexOf(o); + for (int i = 0; i < curSize; i++) { + if (Objects.equals(o,arr[i])){ + return i; + } + } + return -1; + } + + /** + * {@inheritDoc} + *

+ *

This implementation returns size() == 0. + */ + @Override + public boolean isEmpty() { +// return super.isEmpty(); + return curSize==0; + } + + /** + * {@inheritDoc} + *

+ *

This implementation first gets a list iterator that points to the end + * of the list (with {@code listIterator(size())}). Then, it iterates + * backwards over the list until the specified element is found, or the + * beginning of the list is reached. + * + * @param o + * @throws ClassCastException {@inheritDoc} + * @throws NullPointerException {@inheritDoc} + */ + @Override + public int lastIndexOf(Object o) { + // 父类使用了迭代器 +// return super.lastIndexOf(o); + for (int i = curSize-1; i >=0; i--) { + if (Objects.equals(o,arr[i])){ + return i; + } + } + return -1; + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyLinkedList.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyLinkedList.java new file mode 100644 index 0000000000..9fd8341981 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyLinkedList.java @@ -0,0 +1,449 @@ +package cn.net.pikachu.other; + +import org.jetbrains.annotations.NotNull; + +import java.util.*; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyLinkedList extends AbstractSequentialList implements Deque { + private Node head = new Node(); + private Node tail = head; + private int curSize = 0; + /** + * Returns a list iterator over the elements in this list (in proper + * sequence). + * + * @param index index of first element to be returned from the list + * iterator (by a call to the next method) + * @return a list iterator over the elements in this list (in proper + * sequence) + * @throws IndexOutOfBoundsException {@inheritDoc} + */ + @Override + public ListIterator listIterator(int index) { + // TODO + throw new UnsupportedOperationException(); + } + + /** + * Inserts the specified element at the front of this deque if it is + * possible to do so immediately without violating capacity restrictions, + * throwing an {@code IllegalStateException} if no space is currently + * available. When using a capacity-restricted deque, it is generally + * preferable to use method {@link #offerFirst}. + * + * @param e the element to add + * @throws IllegalStateException if the element cannot be added at this + * time due to capacity restrictions + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public void addFirst(E e) { + head.next= new Node(e,head.next,head); + curSize++; + } + + /** + * Inserts the specified element at the end of this deque if it is + * possible to do so immediately without violating capacity restrictions, + * throwing an {@code IllegalStateException} if no space is currently + * available. When using a capacity-restricted deque, it is generally + * preferable to use method {@link #offerLast}. + *

+ *

This method is equivalent to {@link #add}. + * + * @param e the element to add + * @throws IllegalStateException if the element cannot be added at this + * time due to capacity restrictions + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public void addLast(E e) { + Node t = new Node(e,null,tail); + tail.next=t; + tail=t; + curSize++; + } + + /** + * Inserts the specified element at the front of this deque unless it would + * violate capacity restrictions. When using a capacity-restricted deque, + * this method is generally preferable to the {@link #addFirst} method, + * which can fail to insert an element only by throwing an exception. + * + * @param e the element to add + * @return {@code true} if the element was added to this deque, else + * {@code false} + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public boolean offerFirst(E e) { + throw new UnsupportedOperationException(); + } + + /** + * Inserts the specified element at the end of this deque unless it would + * violate capacity restrictions. When using a capacity-restricted deque, + * this method is generally preferable to the {@link #addLast} method, + * which can fail to insert an element only by throwing an exception. + * + * @param e the element to add + * @return {@code true} if the element was added to this deque, else + * {@code false} + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public boolean offerLast(E e) { + throw new UnsupportedOperationException(); + } + + /** + * Retrieves and removes the first element of this deque. This method + * differs from {@link #pollFirst pollFirst} only in that it throws an + * exception if this deque is empty. + * + * @return the head of this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E removeFirst() { + if (curSize==0){ + throw new NoSuchElementException(); + } + E e = head.e; + head=head.next; + curSize--; + return e; + } + + /** + * Retrieves and removes the last element of this deque. This method + * differs from {@link #pollLast pollLast} only in that it throws an + * exception if this deque is empty. + * + * @return the tail of this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E removeLast() { + if (curSize==0){ + throw new NoSuchElementException(); + } + // 怎么去去掉最后一个呢? + Node t=head; + while (t.next!=tail){ + t=t.next; + } + tail=t; + curSize--; + return t.next.e; + } + + /** + * Retrieves and removes the first element of this deque, + * or returns {@code null} if this deque is empty. + * + * @return the head of this deque, or {@code null} if this deque is empty + */ + @Override + public E pollFirst() { + throw new UnsupportedOperationException(); + } + + /** + * Retrieves and removes the last element of this deque, + * or returns {@code null} if this deque is empty. + * + * @return the tail of this deque, or {@code null} if this deque is empty + */ + @Override + public E pollLast() { + throw new UnsupportedOperationException(); + } + + /** + * Retrieves, but does not remove, the first element of this deque. + *

+ * This method differs from {@link #peekFirst peekFirst} only in that it + * throws an exception if this deque is empty. + * + * @return the head of this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E getFirst() { + return head.e; + } + + /** + * Retrieves, but does not remove, the last element of this deque. + * This method differs from {@link #peekLast peekLast} only in that it + * throws an exception if this deque is empty. + * + * @return the tail of this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E getLast() { + return tail.e; + } + + /** + * Retrieves, but does not remove, the first element of this deque, + * or returns {@code null} if this deque is empty. + * + * @return the head of this deque, or {@code null} if this deque is empty + */ + @Override + public E peekFirst() { + throw new UnsupportedOperationException(); + } + + /** + * Retrieves, but does not remove, the last element of this deque, + * or returns {@code null} if this deque is empty. + * + * @return the tail of this deque, or {@code null} if this deque is empty + */ + @Override + public E peekLast() { + throw new UnsupportedOperationException(); + } + + /** + * Removes the first occurrence of the specified element from this deque. + * If the deque does not contain the element, it is unchanged. + * More formally, removes the first element {@code e} such that + * (o==null ? e==null : o.equals(e)) + * (if such an element exists). + * Returns {@code true} if this deque contained the specified element + * (or equivalently, if this deque changed as a result of the call). + * + * @param o element to be removed from this deque, if present + * @return {@code true} if an element was removed as a result of this call + * @throws ClassCastException if the class of the specified element + * is incompatible with this deque + * (optional) + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * (optional) + */ + @Override + public boolean removeFirstOccurrence(Object o) { + Node t = head; + while (t.next!=null){ + if (Objects.equals(t.next.e,o)){ + t.next=t.next.next; + return true; + } + } + return false; + } + + /** + * Removes the last occurrence of the specified element from this deque. + * If the deque does not contain the element, it is unchanged. + * More formally, removes the last element {@code e} such that + * (o==null ? e==null : o.equals(e)) + * (if such an element exists). + * Returns {@code true} if this deque contained the specified element + * (or equivalently, if this deque changed as a result of the call). + * + * @param o element to be removed from this deque, if present + * @return {@code true} if an element was removed as a result of this call + * @throws ClassCastException if the class of the specified element + * is incompatible with this deque + * (optional) + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * (optional) + */ + @Override + public boolean removeLastOccurrence(Object o) { + return false; + } + + /** + * Inserts the specified element into the queue represented by this deque + * (in other words, at the tail of this deque) if it is possible to do so + * immediately without violating capacity restrictions, returning + * {@code true} upon success and {@code false} if no space is currently + * available. When using a capacity-restricted deque, this method is + * generally preferable to the {@link #add} method, which can fail to + * insert an element only by throwing an exception. + *

+ *

This method is equivalent to {@link #offerLast}. + * + * @param e the element to add + * @return {@code true} if the element was added to this deque, else + * {@code false} + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public boolean offer(E e) { + return false; + } + + /** + * Retrieves and removes the head of the queue represented by this deque + * (in other words, the first element of this deque). + * This method differs from {@link #poll poll} only in that it throws an + * exception if this deque is empty. + *

+ *

This method is equivalent to {@link #removeFirst()}. + * + * @return the head of the queue represented by this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E remove() { + return null; + } + + /** + * Retrieves and removes the head of the queue represented by this deque + * (in other words, the first element of this deque), or returns + * {@code null} if this deque is empty. + *

+ *

This method is equivalent to {@link #pollFirst()}. + * + * @return the first element of this deque, or {@code null} if + * this deque is empty + */ + @Override + public E poll() { + return null; + } + + /** + * Retrieves, but does not remove, the head of the queue represented by + * this deque (in other words, the first element of this deque). + * This method differs from {@link #peek peek} only in that it throws an + * exception if this deque is empty. + *

+ *

This method is equivalent to {@link #getFirst()}. + * + * @return the head of the queue represented by this deque + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E element() { + return null; + } + + /** + * Retrieves, but does not remove, the head of the queue represented by + * this deque (in other words, the first element of this deque), or + * returns {@code null} if this deque is empty. + *

+ *

This method is equivalent to {@link #peekFirst()}. + * + * @return the head of the queue represented by this deque, or + * {@code null} if this deque is empty + */ + @Override + public E peek() { + return null; + } + + /** + * Pushes an element onto the stack represented by this deque (in other + * words, at the head of this deque) if it is possible to do so + * immediately without violating capacity restrictions, throwing an + * {@code IllegalStateException} if no space is currently available. + *

+ *

This method is equivalent to {@link #addFirst}. + * + * @param e the element to push + * @throws IllegalStateException if the element cannot be added at this + * time due to capacity restrictions + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this deque + * @throws NullPointerException if the specified element is null and this + * deque does not permit null elements + * @throws IllegalArgumentException if some property of the specified + * element prevents it from being added to this deque + */ + @Override + public void push(E e) { + + } + + /** + * Pops an element from the stack represented by this deque. In other + * words, removes and returns the first element of this deque. + *

+ *

This method is equivalent to {@link #removeFirst()}. + * + * @return the element at the front of this deque (which is the top + * of the stack represented by this deque) + * @throws NoSuchElementException if this deque is empty + */ + @Override + public E pop() { + return null; + } + + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over the elements in this deque in reverse + * sequential order. The elements will be returned in order from + * last (tail) to first (head). + * + * @return an iterator over the elements in this deque in reverse + * sequence + */ + @NotNull + @Override + public Iterator descendingIterator() { + return null; + } +} +class Node{ + public E e; + public Node next; + public Node prev; + public Node() { + } + + public Node(E e) { + this.e = e; + } + + public Node(E e, Node next, Node prev) { + this.e = e; + this.next = next; + this.prev = prev; + } +} \ No newline at end of file diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyQueue.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyQueue.java new file mode 100644 index 0000000000..0179d5ef63 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyQueue.java @@ -0,0 +1,7 @@ +package cn.net.pikachu.other; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyQueue { +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyStack.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyStack.java new file mode 100644 index 0000000000..714c43e2a3 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyStack.java @@ -0,0 +1,7 @@ +package cn.net.pikachu.other; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyStack { +} diff --git a/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyTree.java b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyTree.java new file mode 100644 index 0000000000..42e226aa5f --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/main/java/cn/net/pikachu/other/MyTree.java @@ -0,0 +1,7 @@ +package cn.net.pikachu.other; + +/** + * Created by pikachu on 17-2-19. + */ +public class MyTree { +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/ArrayListTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/ArrayListTest.java new file mode 100644 index 0000000000..8519eee121 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/ArrayListTest.java @@ -0,0 +1,92 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by pikachu on 17-2-20. + */ +public class ArrayListTest { + ArrayList list; + @Before + public void before(){ + list=new ArrayList(); + } + @Test + public void testAdd(){ + list.add(1); + Assert.assertEquals("[1]",list.toString()); + Assert.assertEquals(1,list.size()); + list.add(0,0); + Assert.assertEquals("[0,1]",list.toString()); + Assert.assertEquals(2,list.size()); + list.add(1,2); + Assert.assertEquals("[0,2,1]",list.toString()); + Assert.assertEquals(3,list.size()); + + } + @Test(expected = IndexOutOfBoundsException.class) + public void testRemove(){ + list.add(1); + list.remove(0); + Assert.assertEquals("[]",list.toString()); + Assert.assertEquals(0,list.size()); + list.add(2); + list.add(3); + list.add(4); + Object o =list.remove(1); + Assert.assertEquals(3,o); + Assert.assertEquals("[2,4]",list.toString()); + Assert.assertEquals(2,list.size()); + list.remove(4); + } + @Test(expected = IndexOutOfBoundsException.class) + public void testGet(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Assert.assertEquals(1,list.get(0)); + Assert.assertEquals(4,list.get(3)); + Assert.assertEquals(4,list.size()); + list.get(4); + } + @Test + public void testSize(){ + Assert.assertEquals(0,list.size()); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Assert.assertEquals(4,list.size()); + } + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Iterator iterator = list.iterator(); + StringBuilder builder = new StringBuilder(); + builder.append("["); + while (iterator.hasNext()){ + builder.append(iterator.next()).append(","); + } + builder.deleteCharAt(builder.length()-1); + builder.append("]"); + Assert.assertEquals(builder.toString(),list.toString()); + } + /* + // 抛出异常的测试 + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveWithException(){ + list.remove(2); + } + // 超时测试 + @Test(timeout = 2000) + public void test(){ + while (true); + } + */ +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/BinaryTreeNodeTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..fcf666412f --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/BinaryTreeNodeTest.java @@ -0,0 +1,56 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** +* BinaryTreeNode Tester. +* +* @author pikachu +* @since

二月 25, 2017
+* @version 1.0 +*/ +public class BinaryTreeNodeTest { + + BinaryTreeNode tree; + @Before + public void before() throws Exception { + tree = new BinaryTreeNode(); + } + + @After + public void after() throws Exception { + + } + + /** + * + * Method: insert(Object o) + * + */ + @Test + public void testInsert() { + for (int i = 0; i < 4; i++) { + tree.insert(i); + } + final StringBuilder builder = new StringBuilder(); + BinaryTreeNode.Visit visit = new BinaryTreeNode.Visit() { + @Override + public void visit(Object o) { + builder.append(o).append(","); + } + }; + builder.append("["); + tree.inOrderTraversal(visit,tree); + if (builder.length()>2){ + builder.deleteCharAt(builder.length()-1); + } + builder.append("]"); + Assert.assertEquals("[0,1,2,3]",builder.toString()); + + } + + +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/LinkedListTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/LinkedListTest.java new file mode 100644 index 0000000000..361007228a --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/LinkedListTest.java @@ -0,0 +1,116 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by pikachu on 17-2-25. + */ +public class LinkedListTest { + + LinkedList list; + @Before + public void before(){ + list = new LinkedList(); + } + @Test + public void testAdd(){ + list.add(1); + Assert.assertEquals("[1]",list.toString()); + Assert.assertEquals(1,list.size()); + list.add(0,0); + Assert.assertEquals("[0,1]",list.toString()); + Assert.assertEquals(2,list.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemove(){ + list.add(1); + list.remove(0); + Assert.assertEquals("[]",list.toString()); + Assert.assertEquals(0,list.size()); + list.add(2); + list.add(3); + list.add(4); + Object o =list.remove(1); + Assert.assertEquals(3,o); + Assert.assertEquals("[2,4]",list.toString()); + Assert.assertEquals(2,list.size()); + list.remove(4); + } + @Test(expected = IndexOutOfBoundsException.class) + public void testGet(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Assert.assertEquals(1,list.get(0)); + Assert.assertEquals(4,list.get(3)); + Assert.assertEquals(4,list.size()); + list.get(4); + } + @Test + public void testAddFirst(){ + list.add(1); + list.add(2); + list.add(3); + list.addFirst(0); + Assert.assertEquals(4,list.size()); + Assert.assertEquals(0,list.get(0)); + Assert.assertEquals("[0,1,2,3]",list.toString()); + } + @Test + public void testAddLast(){ + list.add(1); + list.add(2); + list.add(3); + list.addLast(4); + Assert.assertEquals(4,list.size()); + Assert.assertEquals(4,list.get(3)); + Assert.assertEquals("[1,2,3,4]",list.toString()); + } + @Test + public void testRemoveFirst(){ + list.add(1); + list.add(2); + list.add(3); + list.removeFirst(); + Assert.assertEquals(2,list.size()); + Assert.assertEquals("[2,3]",list.toString()); + } + @Test + public void testRemoveLast(){ + list.add(1); + list.add(2); + list.add(3); + list.removeLast(); + Assert.assertEquals(2,list.size()); + Assert.assertEquals("[1,2]",list.toString()); + } + @Test + public void testSize(){ + Assert.assertEquals(0,list.size()); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Assert.assertEquals(4,list.size()); + } + @Test + public void testIterator(){ + list.add(1); + list.add(2); + list.add(3); + list.add(4); + Iterator iterator = list.iterator(); + StringBuilder builder = new StringBuilder(); + builder.append("["); + while (iterator.hasNext()){ + builder.append(iterator.next()).append(","); + } + builder.deleteCharAt(builder.length()-1); + builder.append("]"); + Assert.assertEquals(builder.toString(),list.toString()); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/QueueTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/QueueTest.java new file mode 100644 index 0000000000..ef115315d8 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/QueueTest.java @@ -0,0 +1,57 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.NoSuchElementException; + +/** + * Created by pikachu on 17-2-25. + */ +public class QueueTest { + Queue queue; + @Before + public void before(){ + queue = new Queue(); + } + @Test + public void testEnQueue(){ + for (int i = 0; i < 4; i++) { + queue.enQueue(i); + } + Assert.assertEquals("[0,1,2,3]",queue.toString()); + Assert.assertEquals(4,queue.size()); + } + @Test(expected= NoSuchElementException.class) + public void testDeQueue(){ + for (int i = 0; i < 4; i++) { + queue.enQueue(i); + } + for (int i=0;i<4;i++) { + Assert.assertEquals(i,queue.deQueue()); + } + queue.deQueue(); + + } + @Test + public void testIsEmpty(){ + Assert.assertEquals(true,queue.isEmpty()); + queue.enQueue(1); + Assert.assertEquals(false,queue.isEmpty()); + queue.deQueue(); + Assert.assertEquals(true,queue.isEmpty()); + } + @Test + public void testSize(){ + for (int i = 0; i < 4; i++) { + Assert.assertEquals(i,queue.size()); + queue.enQueue(i); + } + for (int i = 4; i > 0; i--) { + Assert.assertEquals(i,queue.size()); + queue.deQueue(); + } + Assert.assertEquals(0,queue.size()); + } +} diff --git a/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/StackTest.java b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/StackTest.java new file mode 100644 index 0000000000..deea464a23 --- /dev/null +++ b/group13/2931408816/lesson1/data-structure/src/test/java/cn/net/pikachu/basic/StackTest.java @@ -0,0 +1,105 @@ +package cn.net.pikachu.basic; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import java.util.*; + +/** +* Stack Tester. +* +* @author pikachu +* @since
二月 25, 2017
+* @version 1.0 +*/ +public class StackTest { + + Stack stack; + @Before + public void before() throws Exception { + stack = new Stack(); + } + + @After + public void after() throws Exception { + + } + + /** + * + * Method: push(Object o) + * + */ + @Test + public void testPush() { + Assert.assertEquals("[]",stack.toString()); + for (int i = 0; i < 4; i++) { + stack.push(i); + } + Assert.assertEquals("[0,1,2,3]",stack.toString()); + } + + /** + * + * Method: pop() + * + */ + @Test + public void testPop() { + for (int i = 0; i < 4; i++) { + stack.push(i); + } + for (int i = 3; i >= 0; i--) { + Assert.assertEquals(i,stack.pop()); + } + } + + /** + * + * Method: peek() + * + */ + @Test + public void testPeek() { + for (int i = 0; i < 4; i++) { + stack.push(i); + Assert.assertEquals(i,stack.peek()); + } + } + + /** + * + * Method: isEmpty() + * + */ + @Test + public void testIsEmpty() { + Assert.assertEquals(true,stack.isEmpty()); + for (int i = 0; i < 4; i++) { + stack.push(i); + } + Assert.assertEquals(false,stack.isEmpty()); + for (int i = 0; i < 4; i++) { + stack.pop(); + } + Assert.assertEquals(true,stack.isEmpty()); + } + + /** + * + * Method: size() + * + */ + @Test + public void testSize() { + for (int i = 0; i < 4; i++) { + Assert.assertEquals(i,stack.size()); + stack.push(i); + } + Assert.assertEquals(4,stack.size()); + } + + +} diff --git a/group13/413007522/.gitignore b/group13/413007522/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group13/413007522/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group13/413007522/lesson01/.gitignore b/group13/413007522/lesson01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group13/413007522/lesson01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group13/413007522/lesson01/pom.xml b/group13/413007522/lesson01/pom.xml new file mode 100644 index 0000000000..b90f06b534 --- /dev/null +++ b/group13/413007522/lesson01/pom.xml @@ -0,0 +1,7 @@ + + 4.0.0 + cn.xl + lesson01_code + 0.0.1-SNAPSHOT + lesson01_code + \ No newline at end of file diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyArrayList.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyArrayList.java new file mode 100644 index 0000000000..80464455e4 --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyArrayList.java @@ -0,0 +1,238 @@ +package cn.xl; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; + + +/** + * 1. ArrayList һУ൱ڶ̬顣JavaԱ̬ܶӣ + * 2. ӵӡɾ޸ġȹܡ + * @author XIAOLONG + * + * @param + */ + +public class MyArrayList implements MyList { + + + private int size = 0; + + private final int initialCapacity = 3; + + private Object[] elementData = new Object[100]; + + private static final Object[] EMPTY_ELEMENTDATA = {}; + + private int modCount = 0; + + /** + * һĬϳʼΪ3Ŀб + * + */ + public MyArrayList() { + elementData = new Object[initialCapacity]; + } + + /** + * һָʼĿб + * @param initialCapacity + */ + public MyArrayList(int initialCapacity) { + + if (initialCapacity < 0){ + throw new IllegalArgumentException("Illegal initialCapacity: "+ initialCapacity); + }else if(initialCapacity == 0){ + elementData = EMPTY_ELEMENTDATA; + }else{ + elementData = new Object[this.initialCapacity]; + } + } + + /** + * + * һָcollectionԪصбЩԪذոcollectionĵǵ˳ + * MySubClassMyClassࡣ + * Collection myCollection; + * Collection mySubCollection; + * ArrayList myList = new ArrayList(myCollection); + * ҲԣArrayList myList = new ArrayList(mySubCollection); + * MyClassMyClassCollectionԹArrayList + * @param c + */ + public MyArrayList(Collection c) { + elementData = c.toArray(); + if((size = elementData.length) != 0){ + //c.toArray might (incorrectly) not return Object[] (see 6260652) + //ٷbugתͲȫ + //Object[]Arrays.copyOfתΪObject[] + if (elementData.getClass() != Object[].class) + elementData = Arrays.copyOf(elementData, size, Object[].class); + }else{ + elementData = EMPTY_ELEMENTDATA; + } + } + + + /** + * ָԪбָλϵԪ + * @param index + * @param element + * @return Object(ǰλڸλϵľԪ) + */ + public Object set(int index, Object element) { + if (index >= size()) + throw new RuntimeException("The Index: "+index+" is out of band."); + + Object oldValue = elementData[index]; + elementData[index] = element; + return oldValue; + } + + /** + * Ԫбβ + * @param e + */ + public void add(Object e) { + if (e == null) { + throw new RuntimeException("The value should not be null."); + } + if (size() >= initialCapacity) { + ensureCapacity(size() + 1); + } + elementData [size] = e; + size++; + } + + /** + * Ԫӵбָλ + * @param index + * @param element + */ + public void add(int index, Object o) { + if (index >= size || index < 0) + throw new RuntimeException("The Index: "+index+" is out of band."); + // 鳤Ȳ㣬ݡ + ensureCapacity(size+1); + // elementDataдIndexλÿʼΪsize-indexԪأ + // ±Ϊindex+1λÿʼµelementDataС + // ǰλڸλõԪԼкԪһλá + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + + /** + * شбָλϵԪ + * @param index + * @return + */ + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("The index:" + index + " is out of band."); + } + return elementData [index]; + } + + /** + * ɾָλԪ + * @param ɾԪλã0ʼ + * @return Object(ɾָλϵľԪ) + */ + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("The index:" + index + " is out of band."); + } + modCount++; + Object oldElement = elementData[index]; + //˴ȻҲSystem.arraycopy ʵ + for (int i = index; i < size - 1; i++) { + elementData [i] = elementData [i + 1]; + } + elementData [size - 1] = null; + size--; + return oldElement; + } + + /** + * ݣÿռΪ 50%+1 + * @param ǰСֵ + */ + private void ensureCapacity(int minCapacity) { + modCount++; + int oldCapacity = elementData.length; + if (minCapacity > oldCapacity) { + //λ㣬൱ڳ2Щ int newCapacity = (oldCapacity * 3)/2 + 1; + int newCapacity = oldCapacity + (oldCapacity >> 1); + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + /** + * ListеԪظ. + * @return ListеԪظ + */ + public int size() { + return size; + } + + /** + * ListԪеһ + * @return ListԪеĵ + */ + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor; // һԪصλ + int lastRet = -1; // һԪصλ + int expectedModCount = modCount; + + public boolean hasNext() { + return cursor != size; + } + + public Object next() { + + if (modCount != expectedModCount){ + throw new RuntimeException("This list is being modified."); + + } + + int i = cursor; + if (i >= size){ + throw new RuntimeException("No such element."); + } + Object[] elementData = MyArrayList.this.elementData; + if (i >= elementData.length){ + throw new RuntimeException("This list is being modified."); + } + + cursor = i + 1; + return elementData[lastRet = i]; + } + + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + + if (modCount != expectedModCount){ + throw new RuntimeException("This list is being modified."); + + } + + try { + MyArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + expectedModCount = modCount; + } catch (IndexOutOfBoundsException ex) { + throw new RuntimeException("This list is being modified."); + } + } + } + +} diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyLinkedList.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyLinkedList.java new file mode 100644 index 0000000000..ee4c4e461e --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyLinkedList.java @@ -0,0 +1,218 @@ +package cn.xl; + +import java.util.Iterator; + +/** + * + * @author XIAOLONG + * @param + * + */ +public class MyLinkedList implements MyList { + + private int size = 0; + + private Node first; + + private Node last; + + + /** + * һ޲ι캯һList + * + */ + public MyLinkedList(){ + + } + + + + /** + * Ԫбβ + * @param Object(ӵԪ) + */ + public void add(Object o) { + + addLast(o); + } + + + /** + * бָλԪ,ǾԪ + * @param index λãObject Ԫ + */ + public void add(int index, Object o) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + Node x = node(index); + x.data = o; + + } + + /** + * ȡָλõԪdata + * @param index ҪȡԪλ + */ + public Object get(int index) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + return node(index).data; + } + + + /** + * ɾָλԪ + * @param index ɾбԪλ + */ + public Object remove(int index) { + + if(!(index >= 0 && index <= size())){ + throw new RuntimeException("The index"+index+"is out of band."); + } + + final Node node = node(index); + final Object o = node.data; + if(first.equals(node)){ + removeFirst(); + }else if(last.equals(node)){ + removeLast(); + }else{ + final Node prev = node.prev; + final Node next = node.next; + + prev.next = next; + next.prev = prev; + node.prev = null; + node.next = null; + } + node.data = null; + size --; + return o; + } + + + /** + * ȡбǰsize + */ + public int size() { + return size; + } + + /** + * ͷԪ,ͷԪΪգøԪͬʱΪβԪ + * @param Object ӵͷԪأ + */ + public void addFirst(Object o){ + + final Node f = first; + final Node newNode = new Node(null,o,f); + if(f == null){ + last = newNode; + }else{ + f.prev = newNode; + } + size ++; + } + + /** + * βԪأβԪΪգͬʱøԪΪͷԪ + * @param Object(ӵβԪ) + */ + public void addLast(Object o){ + + final Node l = last; + final Node newNode = new Node(l,o,null); + if(l == null){ + first = newNode; + }else{ + l.next = newNode; + } + size ++; + } + + /** + * ƳһԪأƳԺбΪ first = next = null + * @return ƳԪ + */ + public Object removeFirst(){ + + final Node f = first; + final Object o = f.data; + final Node next = f.next ; + f.next = null; + f.data = null; + first = next; + if(next == null){ + last = next; + }else{ + next.prev = null; + } + size --; + return o; + } + + /** + * ƳһԪ + * @return ƳԪ + */ + public Object removeLast(){ + + final Node l = last; + final Object o = l.data; + final Node prev = l.prev; + l.data = null; + l.prev = null; + last = prev; + if(prev == null){ + last = null; + }else{ + prev.next = null; + } + size --; + return o; + } + public Iterator iterator(){ + return null; + } + + /** + * Nodeڲ + * + */ + private static class Node{ + Object data; + Node next; + Node prev; + + Node(Node prev,Object o,Node next){ + this.data = o; + this.next = next; + this.prev = prev; + } + } + + /** + * һȡ±λnodeķ + * ǰ±Сlistȵһ䣬ͷʼ βʼ + * @param index Ԫصλ + */ + private Node node(int index){ + if (index < (size >> 1)) { + Node x = first; + 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; + } + } +} diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyList.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyList.java new file mode 100644 index 0000000000..f9769ba33a --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyList.java @@ -0,0 +1,9 @@ +package cn.xl; + +public interface MyList { + 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(); +} \ No newline at end of file diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyQueue.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyQueue.java new file mode 100644 index 0000000000..cd61cfd8c8 --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyQueue.java @@ -0,0 +1,110 @@ +package cn.xl; + +/** + * Queue一个先进先出(first in first out,FIFO)得队列 + * 所谓的队列,也是一个含有至少两个基本操作的抽象数据类型:插入新的元素;删除最久时间插入的元素。 + * 遵循FIFO(First in,first out,先进先出)的原则。 + * MyQueue采用环形数组实现 + * @author XIAOLONG + * + */ +public class MyQueue { + + private int size,head,tail; + + private Object[] elementData; + + private final int initialCapacity = 4; + + public MyQueue(){ + + head = tail = -1; + elementData = new Object[initialCapacity]; + + } + + /** + * 向队列中添加元素 + * @param o + */ + public void enQueue(Object o){ + + ensureCapacity(); + + if( head == -1) { + tail = head = 0; + } + size ++; + elementData[tail] = o; + tail ++; + + + } + + /** + * 删除栈顶元素,并返回旧栈顶元素 + * @return 旧栈顶元素 + */ + public Object deQueue(){ + Object element = elementData[head]; + if(head == tail){ + head = tail = -1; + }else if(head == elementData.length-1){ + head = 0; + }else{ + head ++; + } + size --; + return element; + } + + /** + * 判断队列是否为空 + * @return + */ + public boolean isEmpty(){ + return head == -1; + } + + /** + * 返回自身长度 + * @return + */ + public int size(){ + return size; + } + + /** + * 判断队列是否已满 + * @return + */ + public boolean isFull() { + return (head == 0 && tail == elementData.length); + } + + /** + * 扩展容量,如果队列有效数据已经占满空间则增加2,否则覆盖无效数据,重新分配数据空间 + * @param 当前队列所需最小容量size + */ + private void ensureCapacity(){ + + if(isFull()){ + Object [] oldData = elementData; + elementData = new Object[elementData.length + 2]; + System.arraycopy(oldData, head,elementData , 0, oldData.length); + }else if(head > 0){ + Object [] oldData = elementData; + System.arraycopy(oldData, head,elementData , 0, oldData.length-head); + tail = tail - head ; + head = 0; + } + } + + + + public static void main(String[] args) + { + + + } +} diff --git a/group13/413007522/lesson01/src/main/java/cn/xl/MyStack.java b/group13/413007522/lesson01/src/main/java/cn/xl/MyStack.java new file mode 100644 index 0000000000..ac78828f5c --- /dev/null +++ b/group13/413007522/lesson01/src/main/java/cn/xl/MyStack.java @@ -0,0 +1,124 @@ +package cn.xl; + +import java.util.Arrays; +import java.util.EmptyStackException; + +/** + * Stackһȳlast in first outLIFOĶջ + * VectorĻչ5 + * @author XIAOLONG + * + */ +public class MyStack { + + private int elementCount; + + private Object[] elementData; + + /** + * ޲ι췽һջ + * + */ + public MyStack(){ + + } + + + /** + * Ԫջ + * @param item + * @return ջԪ + */ + public synchronized Object push(Object item){ + + ensureCapacity(elementCount+1); + elementData[elementCount] = item; + elementCount ++; + return item; + } + + /** + * ջԪƳظԪ + * @return ջԪ + */ + public synchronized Object pop(){ + Object obj; + + obj = peek(); + elementCount --; + elementData[elementCount] = null; + + return obj; + } + + /** + * 鿴ջԪ + * + * @return ջԪ + * @throws ջΪ ׳ EmptyStackException쳣 . + */ + public synchronized Object peek(){ + int len = elementCount; + + if(len == 0) + throw new EmptyStackException(); + + return elementData[len - 1]; + + } + + /** + * ջǷΪ + * + * @return True or false + */ + public boolean isEmpty(){ + + return elementCount == 0; + } + + /** + * ѯռջǷijԪ + * @param ѯԪ + * @return ԪشڷԪλãջԪλΪ1 + * Ԫջظ򷵻ؾջԪλã + * Ԫջвڣ򷵻 -1 + */ + public synchronized int search(Object o){ + + if(o == null){ + for(int i = elementCount -1;i >= 0; i--){ + if(elementData[i] == null){ + return elementCount - i; + } + } + }else{ + for(int i = elementCount -1;i >= 0; i-- ){ + if(o.equals(elementData[i])){ + return elementCount - i; + } + } + } + + return -1; + } + + /** + * չһ + * @param ǰջСsize + */ + private void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + if(minCapacity > oldCapacity){ + int newCapacity = oldCapacity << 1; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public static void main(String[] args){ + + + + } + +} diff --git a/group13/453070251/lessones01/BinaryTreeNode.java b/group13/453070251/lessones01/BinaryTreeNode.java new file mode 100644 index 0000000000..f5d8c5dacd --- /dev/null +++ b/group13/453070251/lessones01/BinaryTreeNode.java @@ -0,0 +1,23 @@ +public class BinaryTreeNode{ + public BinaryTreeNode left; + public BinaryTreeNode right; + public int value; + public BinaryTreeNode(int arg_value){ + value = arg_value; + } + public void add(int arg_value){ + if(arg_valuevalue){ + if(right == null){ + right = new BinaryTreeNode(arg_value); + }else{ + right.add(arg_value); + } + } + } +} \ No newline at end of file diff --git a/group13/453070251/lessones01/LinkedList.java b/group13/453070251/lessones01/LinkedList.java new file mode 100644 index 0000000000..c1cefc463f --- /dev/null +++ b/group13/453070251/lessones01/LinkedList.java @@ -0,0 +1,116 @@ +public class LinkedList{ + public static void main(String[] args){ + LinkedList arr = new LinkedList(); + arr.add("0"); + System.out.println(arr.get(0)); + + System.out.println(arr.get(1)); + arr.add("1"); + System.out.println(arr.get(1)); + + + System.out.println(arr.get(3)); + arr.add("3",3); + System.out.println(arr.get(3)); + arr.add("before 2",2); + System.out.println(arr.get(2)); + System.out.println(arr.get(4)); + arr.set(4,"4"); + System.out.println(arr.get(4)); + arr.set(8,"8"); + System.out.println(arr.get(8)); + + } + protected Node first; + protected Node lastest; + private int size; + public LinkedList(){ + lastest = first = new Node(null); + size = 0; + } + public T get(int arg_num){ + //throw Error if arg_num<0,unfinished + return _get(arg_num); + } + public void set(int arg_num,T arg_value){ + //throw Error if arg_num<0,unfinished + _set(arg_num,arg_value); + } + public T remove(int arg_num){ + //throw Error if arg_num<0,unfinished + return _remove(arg_num).value; + } + public void add(T arg_value,int arg_num){ + //throw Error if arg_num<0,unfinished + _add(arg_value,arg_num); + } + public void add(T arg_value){ + _add(arg_value,size); + } + public int size(){ + return size; + } + protected Node _remove(int arg_num){ + if(arg_num node = _getNode(arg_num); + node.remove(); + size--; + if(arg_num == size){ + lastest = node.left; + }else if(arg_num == 0 && size != 0){ + first = node.right; + } + return node; + }else{ + return null; + } + } + protected Node _getNode(int arg_num){ + Node node = first; + for(int num = 0;num=size){return null;} + return _getNode(arg_num).value; + } + protected void _set(int arg_num,T arg_value){ + if(arg_num>=size){ + _add(arg_value,arg_num); + }else{ + _getNode(arg_num).value = arg_value; + } + } + protected void _add(T arg_value,int arg_num){ + if(arg_num > size){ + Node n = new Node(null); + lastest.setRight(n); + for(int num=size+1;num(null)); + n = n.right; + } + n.setRight(new Node(arg_value)); + lastest = n.right; + size = arg_num+1; + }else if(arg_num == size){ + if(size == 0){ + lastest.value = arg_value; + }else{ + lastest.setRight(new Node(arg_value)); + lastest = lastest.right; + } + size++; + }else if(arg_num == 0){ + first.setLeft(new Node(arg_value)); + first = first.left; + size++; + }else{ + Node node = _getNode(arg_num); + node.addLeft(new Node(arg_value)); + size++; + } + + } +} \ No newline at end of file diff --git a/group13/453070251/lessones01/LinkedListWithFloat.java b/group13/453070251/lessones01/LinkedListWithFloat.java new file mode 100644 index 0000000000..803faa43d3 --- /dev/null +++ b/group13/453070251/lessones01/LinkedListWithFloat.java @@ -0,0 +1,86 @@ +public class LinkedListWithFloat extends LinkedList{ + public static void main(String[] args){ + LinkedListWithFloat arr = new LinkedListWithFloat(); + arr.add("0"); + System.out.println(arr.get(0)); + arr.add("1"); + System.out.println(arr.get(1)); + + System.out.println(arr.get(3)); + arr.add("3",3); + System.out.println(arr.get(3)); + arr.add("before 2",2); + System.out.println(arr.get(2)); + System.out.println(arr.get(4)); + arr.set(4,"4"); + System.out.println(arr.get(4)); + arr.set(8,"8"); + System.out.println(arr.get(8)); + System.out.println(arr.remove(8)); + System.out.println(arr.get(8)); + System.out.println(arr.remove(2)); + System.out.println(arr.get(2)); + System.out.println(arr.remove(0)); + System.out.println(arr.get(0)); + System.out.println(arr.size()); + } + + private Node floatNode; + private int floatNum; + public LinkedListWithFloat(){ + super(); + floatNode = first; + floatNum = 0; + } + protected Node _getNode(int arg_num){ + int position = arg_num - floatNum; + if(position == 0){return floatNode;} + //arg_num = (int leftPosition = arg_num); + int rightPosition = size() - 1 - arg_num; + boolean floatNear = position * position <= arg_num * rightPosition; + if(floatNear){ + if(position>0){ + return findFloatRight(arg_num); + }else/*if(position<0)*/{ + return findFloatLeft(arg_num); + } + }else if(position<0){ + floatNode = first; + floatNum = 0; + return findFloatRight(arg_num); + }else/*if(position>0)*/{ + floatNode = lastest; + floatNum = rightPosition + arg_num;//size()-1 + return findFloatLeft(arg_num); + } + } + protected void _add(T arg_value,int arg_num){ + super._add(arg_value,arg_num); + if(arg_num <= floatNum){floatNum++;} + } + protected Node _remove(int arg_num){ + Node temp = super._remove(arg_num); + if(temp == null){return temp;} + if(temp.right != null){ + floatNode = temp.right; + }else{ + floatNode = lastest; + floatNum = size()-1; + } + return temp; + } + private Node findFloatLeft(int arg_num){ + for(;floatNum>arg_num;){ + floatNode = floatNode.left; + floatNum--; + } + return floatNode; + } + private Node findFloatRight(int arg_num){ + for(;floatNum{ + public Node left; + public Node right; + public T value; + public Node(T arg_value){ + value = arg_value; + } + public void setLeft(Node arg_another){ + _connect(arg_another,this); + } + public void setRight(Node arg_another){ + _connect(this,arg_another); + } + public void addLeft(Node arg_another){ + _connect(left,arg_another,this); + } + public void addRight(Node arg_another){ + _connect(this,arg_another,right); + } + public Node remove(){ + _connect(left,right); + return this; + } + private void _connect(Node arg_left,Node arg_right){ + if(arg_left != null){arg_left.right = arg_right;} + if(arg_right != null){arg_right.left = arg_left;} + } + private void _connect(Node arg_left,Node node,Node arg_right){ + _connect(arg_left,node); + _connect(node,arg_right); + } +} \ No newline at end of file diff --git a/group13/453070251/lessones01/Queue.java b/group13/453070251/lessones01/Queue.java new file mode 100644 index 0000000000..6e54c1add2 --- /dev/null +++ b/group13/453070251/lessones01/Queue.java @@ -0,0 +1,40 @@ +public class Queue{ + Node first; + Node lastest; + private int size; + public Queue(){ + first = lastest = new Node(null); + size = 0; + } + public int size(){ + return size; + } + public Queue push_back(T arg_value){ + if(size == 0){ + first.value = arg_value; + }else{ + lastest.setRight(new Node(arg_value)); + lastest = lastest.right; + } + size++; + return this; + } + public T pop_front(){ + if(size > 0){size--;}; + if(size == 0){ + T temp; + temp = first.value; + first.value = null; + return temp; + }else{ + first = first.right; + return first.left.remove().value; + } + } + public T front(){ + return first.value; + } + public T back(){ + return lastest.value; + } +} \ No newline at end of file diff --git a/group13/453070251/lessones01/Stack.java b/group13/453070251/lessones01/Stack.java new file mode 100644 index 0000000000..a87cb59268 --- /dev/null +++ b/group13/453070251/lessones01/Stack.java @@ -0,0 +1,40 @@ +public class Stack{ + Node first; + Node lastest; + private int size; + public Stack(){ + first = lastest = new Node(null); + size = 0; + } + public int size(){ + return size; + } + public Stack push(T arg_value){ + if(size == 0){ + lastest.value = arg_value; + }else{ + lastest.setRight(new Node(arg_value)); + lastest = lastest.right; + } + size++; + return this; + } + public T pop(){ + if(size > 0){size--;}; + if(size == 0){ + T temp; + temp = lastest.value; + lastest.value = null; + return temp; + }else{ + lastest = lastest.left; + return lastest.right.remove().value; + } + } + public T peek(){ + return lastest.value; + } + public boolean empty(){ + return size == 0; + } +} \ No newline at end of file diff --git a/group13/568334413/0226/src/Main.java b/group13/568334413/0226/src/Main.java new file mode 100644 index 0000000000..3d3750475a --- /dev/null +++ b/group13/568334413/0226/src/Main.java @@ -0,0 +1,123 @@ +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import com.coding.basic.Queue; + +import java.util.ArrayList; +import java.util.Stack; + +public class Main { + + public static void main(String[] args) { + System.out.println("Hello World!"); + + com.coding.basic.TreeSet treeSet = new com.coding.basic.TreeSet(); + treeSet.add(1); + treeSet.add(2); + treeSet.add(3); + treeSet.add(4); + System.out.println("treeSet = " + treeSet.size); + + + } + + public static void testQueueIterator() { + Queue queue = new Queue(); + queue.enQueue("0"); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + queue.enQueue("4"); + while (queue.iterator.hasNext()) { + System.out.println("next === " + queue.iterator.next()); + } + + } + + public static void testLinkedListIterator() { + com.coding.basic.LinkedList arrayList = new com.coding.basic.LinkedList(); + arrayList.add("0"); + 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("next === " + iterator.next()); + } + } + + + public static void testArrayListIterator() { + com.coding.basic.ArrayList arrayList = new com.coding.basic.ArrayList(); + arrayList.add("0"); + 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("next === " + iterator.next()); + System.out.println("next =2== " + iterator.next()); + } + } + + public static void linkedList() { + Stack stack = new Stack(); + stack.push("1"); + stack.push("2"); + stack.push("3"); + stack.push("4"); + String a = stack.peek(); + System.out.println("a = " + a); + +// LinkedList linkedList = new LinkedList(); +// linkedList.add("0"); +// linkedList.add("1"); +// linkedList.add("2"); +// linkedList.add("3"); +// linkedList.get(1); +// linkedList.size(); + LinkedList linkedList = new LinkedList(); + linkedList.add("0"); + linkedList.add("1"); + linkedList.add(1, "2"); + linkedList.toString(); + } + + public static void arrayList() { + ArrayList arrayList = new ArrayList(); + arrayList.add("1"); + arrayList.add(null); + arrayList.add(null); + arrayList.add(null); + arrayList.add(null); + arrayList.add("2"); + arrayList.remove(1); + +// arrayList.add(7, "100"); + + System.out.println("arrayList = " + arrayList.size()); + java.util.List list = new ArrayList(); + + com.coding.basic.ArrayList myList = new com.coding.basic.ArrayList(); + myList.add("0"); + myList.add("1"); + myList.add("2"); + myList.add("3"); + myList.add("4"); + myList.add("5"); + myList.add("6"); + myList.add("7"); + myList.add(8, "8"); + System.out.println("myList = " + myList.get(9)); + + System.out.println("myList = " + myList.toString()); + myList.remove(1); +// System.out.println("myList = " + myList.size()); + + System.out.println("myList = " + myList.toString()); + + } +} diff --git a/group13/568334413/0226/src/com/coding/basic/ArrayList.java b/group13/568334413/0226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9592af4050 --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,113 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[5]; + + public void add(Object o) { + dilatation(); + size = size + 1; + elementData[size - 1] = o; + } + + + public void add(int index, Object o) { + //是否超出目前大小+1; + //是否需要先扩容 + //移动数组 + checkRange(index); + + dilatation(); + //手动移动素组 + for (int i = size; i >= index; i--) { + Object o1 = elementData[i]; + elementData[i + 1] = elementData[i]; + } + elementData[index] = o; + size = size + 1; + + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + + public Object get(int index) { + checkRange(index); + return elementData[index]; + } + + public Object remove(int index) { + checkRange(index); + + Object object = elementData[index]; + + //native 方法,未知细节 + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size = size - 1; + elementData[size] = null; + return object; + } + + @Override + public int size() { + return this.size; + } + + public void checkRange(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + public void dilatation() { + if (elementData.length == size + 1) { + Object[] tempElementData = elementData; + elementData = new Object[elementData.length * 2]; + for (int i = 0; i < tempElementData.length; i++) { + elementData[i] = tempElementData[i]; + } + } + } + + @Override + public String toString() { + StringBuilder value = new StringBuilder(); + for (int i = 0; i < size; i++) { + Object object = elementData[i]; + value.append(object.toString() + ","); + } + return value.toString(); + } + + Iterator mIterator = new MIterator(); + + public Iterator iterator() { + return mIterator; + } + + class MIterator implements Iterator { + int index = 0; + + @Override + public boolean hasNext() { + if (index < size) { + + return true; + } + index = 0; + return false; + } + + @Override + public Object next() { + Object object = elementData[index]; + index++; + return object; + } + + } +} diff --git a/group13/568334413/0226/src/com/coding/basic/BinaryTreeNode.java b/group13/568334413/0226/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e1f8840552 --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Integer getData() { + return data; + } + + public void setData(Integer 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/group13/568334413/0226/src/com/coding/basic/Iterator.java b/group13/568334413/0226/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..96adcd6d3a --- /dev/null +++ b/group13/568334413/0226/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/group13/568334413/0226/src/com/coding/basic/LinkedList.java b/group13/568334413/0226/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d3ff3fd907 --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.coding.basic; + +public class LinkedList implements List { + private int size = 0; + private Node headNode; + private Node lastNode; + + public LinkedList() { + headNode = new Node(); + lastNode = new Node(); + headNode.next = lastNode; + lastNode.prev = headNode; + } + + public void add(Object o) { + Node node = new Node(); + node.data = o; + if (size == 0) { + headNode.next = node; + lastNode.prev = node; + node.next = lastNode; + node.prev = headNode; + + } else { + Node tempLastNode = lastNode.prev; + tempLastNode.next = node; + + node.next = lastNode; + node.prev = tempLastNode; + lastNode.prev = node; + } + size = size + 1; + + } + + public void add(int index, Object o) { + checkRange(index); + Node preNode = null; + Node nextNode = null; + for (int i = 0; i <= index; i++) { + preNode = headNode.next; + } + nextNode = preNode.next; + Node newNode = new Node(); + newNode.data = o; + + preNode.next = newNode; + nextNode.prev = newNode; + + newNode.next = nextNode; + newNode.prev = preNode; + } + + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + public void checkRange(int index) { + if (index > size) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + } + + public Object get(int index) { + checkRange(index); + Node node = null; + for (int i = 0; i <= index; i++) { + node = headNode.next; + } + return node; + } + + public Object remove(int index) { + return null; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node oldFirstnode = headNode.next; + Node node = new Node(); + node.data = o; + headNode.next = node; + node.next = oldFirstnode; + node.prev = headNode; + oldFirstnode.prev = node; + } + + public void addLast(Object o) { + Node oldLastNode = lastNode.prev; + + Node node = new Node(); + node.data = o; + node.prev = oldLastNode; + node.next = lastNode; + + oldLastNode.next = node; + lastNode.prev = node; + + } + + public Object removeFirst() { + Node firstNode = headNode.next; + Node newFirstNode = firstNode.next; + newFirstNode.prev = headNode; + headNode.next = newFirstNode; + return firstNode; + } + + public Object removeLast() { + Node lastOldNode = lastNode.prev; + lastNode.prev = lastOldNode.prev; + lastOldNode.prev.next = lastNode; + + return lastOldNode; + } + + Iterator iterator = new MIterator(); + + public Iterator iterator() { + return iterator; + } + + @Override + public String toString() { + return super.toString(); + + } + + private static class Node { + Object data; + Node next; + Node prev; + } + + class MIterator implements Iterator { + Node node; + + @Override + public boolean hasNext() { + if (node == null) { + node = headNode.next; + } + if (!node.equals(lastNode)) { + System.out.println("ture"); + + return true; + } + node = null; + return false; + } + + @Override + public Object next() { + if (node.equals(headNode)) { + node = headNode.next; + return node; + } + Node tempNode = node; + node = node.next; + if (node == null) { + throw new IndexOutOfBoundsException("out of "); + } + return tempNode.data; + } + + } +} diff --git a/group13/568334413/0226/src/com/coding/basic/List.java b/group13/568334413/0226/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group13/568334413/0226/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/group13/568334413/0226/src/com/coding/basic/Queue.java b/group13/568334413/0226/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..3113e1b91a --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/Queue.java @@ -0,0 +1,27 @@ +package com.coding.basic; + +public class Queue { + LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o) { + linkedList.addLast(o); + } + + public Object deQueue() { + Object o = linkedList.removeLast(); + return o; + } + + public boolean isEmpty() { + if (linkedList.size() == 0) { + return true; + } + return false; + } + + public int size() { + return linkedList.size(); + } + + public Iterator iterator = linkedList.iterator(); +} diff --git a/group13/568334413/0226/src/com/coding/basic/Stack.java b/group13/568334413/0226/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..d53ba4b075 --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/Stack.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + Object object = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + return object; + } + + public Object peek() { + Object object = elementData.get(elementData.size() - 1); + return object; + } + + public boolean isEmpty() { + if (elementData.size() > 0) { + return false; + } + return true; + + } + + public int size() { + return elementData.size(); + } + + Iterator iterator = elementData.iterator(); +} diff --git a/group13/568334413/0226/src/com/coding/basic/TreeSet.java b/group13/568334413/0226/src/com/coding/basic/TreeSet.java new file mode 100644 index 0000000000..47334ba54f --- /dev/null +++ b/group13/568334413/0226/src/com/coding/basic/TreeSet.java @@ -0,0 +1,40 @@ +package com.coding.basic; + +/** + * Created by laibin on 2017/2/25. + */ +public class TreeSet { + BinaryTreeNode root = null; + public int size = 0; + + public void add(Integer integer) { + + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(); + binaryTreeNode.setData(integer); + if (root == null) { + root = binaryTreeNode; + size++; + return; + } + insert(root, binaryTreeNode); + } + + void insert(BinaryTreeNode node, BinaryTreeNode tempNode) { + if (tempNode.getData() < node.getData()) { + if (node.getLeft() == null) { + node.setLeft(tempNode); + size++; + return; + } + insert(node.getLeft(), tempNode); + } else if (tempNode.getData() > node.getData()) { + if (node.getRight() == null) { + node.setRight(tempNode); + size++; + return; + } + insert(node.getRight(), tempNode); + } + } + +} diff --git a/group13/6023757/lesson2/mytest/.settings/org.eclipse.jdt.core.prefs b/group13/6023757/lesson2/mytest/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group13/6023757/lesson2/mytest/.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/group13/6023757/lesson2/mytest/src/mytest/ArrayList.java b/group13/6023757/lesson2/mytest/src/mytest/ArrayList.java new file mode 100644 index 0000000000..99f2c43a6d --- /dev/null +++ b/group13/6023757/lesson2/mytest/src/mytest/ArrayList.java @@ -0,0 +1,86 @@ +package mytest; + +import java.util.Arrays; + +public class ArrayList { + private int size = 0; + private Object[] elementData; + + public ArrayList(){ + elementData = new Object[10]; + } + + public ArrayList(int InitSize){ + elementData = new Object[InitSize]; + } + public void add(Object o){ + if(this.size == this.elementData.length){ + this.enlarge(); + + } + this.elementData[size] = o; + this.size++; + } + + public void add(int index, Object o){ + if(this.size == this.elementData.length){ + this.enlarge(); + } + if(index >= this.size){ + throw new IndexOutOfBoundsException("Index: "+index+" is out of band"); + } + else{ + Object[] newArray = new Object[this.elementData.length]; + System.arraycopy(this.elementData, 0, newArray, 0, index); + newArray[index] = o; + System.arraycopy(this.elementData, index, newArray, index + 1, this.size - index); + this.elementData = newArray; + this.size++; + } + } + + public Object get(int index){ + if(index > -1 && index < this.size()){ + return elementData[index]; + } + else{ + return null; + } + } + + public void remove(int index){ + if(index >= this.size){ + System.out.println("Index is out of scope"); + return; + } + + if(index == this.size - 1){ + this.elementData[index] = null; + } + else if(index == 0){ + Object[] newArray = new Object[this.elementData.length]; + System.arraycopy(this.elementData, 1, newArray, 0, this.size -1); + this.elementData = newArray; + } + else{ + Object[] newArray = new Object[this.elementData.length]; + System.arraycopy(this.elementData, 0, newArray, 0, index-1); + System.arraycopy(this.elementData, index, newArray, index-1, this.size - index); + this.elementData = newArray; + } + + this.size--; + } + + public int size(){ + return this.size; + } + + public void enlarge(){ + Object[] newArray = new Object[this.elementData.length+5]; + System.arraycopy(this.elementData, 0, newArray, 0, this.elementData.length); + this.elementData = newArray; + } + +} + diff --git a/group13/6023757/lesson2/mytest/src/mytest/LinkedList.java b/group13/6023757/lesson2/mytest/src/mytest/LinkedList.java new file mode 100644 index 0000000000..8cfb0d6d2d --- /dev/null +++ b/group13/6023757/lesson2/mytest/src/mytest/LinkedList.java @@ -0,0 +1,116 @@ +package mytest; + +public class LinkedList { + private int size = 0; + private Node firstNode = null; + private Node lastNode = null; + + public void add(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = null; + if(lastNode == null){ + firstNode = newNode; + lastNode = newNode; + } + else{ + lastNode.next = newNode; + lastNode = newNode; + } + this.size++; + } + public void add(int index , Object o){ + if(index < 0 || index >= this.size){ + throw new IndexOutOfBoundsException("Index: "+index+" is out of band"); + } + if(index == 0){ + addFirst(o); + } + else if(index == this.size-1){ + addLast(o); + } + else{ + Node pointer = this.firstNode; + for(int i = 0;i < index - 1;i++){ + pointer = pointer.next; + } + Node newNode = new Node(); + newNode.data = o; + newNode.next = pointer.next; + pointer.next = newNode; + this.size++; + } + + } + public Object get(int index){ + if(index < 0 || index >= this.size){ + throw new IndexOutOfBoundsException("Index: "+index+" is out of band"); + } + Node pointer = this.firstNode; + for(int i = 0;i < index;i++){ + pointer = pointer.next; + } + return pointer.data; + } + public void remove(int index){ + if(index < 0 || index >= this.size){ + throw new IndexOutOfBoundsException("Index: "+index+" is out of band"); + } + if(index == 0){ + removeFirst(); + } + else if(index == this.size-1){ + removeLast(); + } + else{ + Node pointer = this.firstNode; + for(int i = 0;i < index - 1;i++){ + pointer = pointer.next; + } + Node toBeDelete = pointer.next; + pointer.next = toBeDelete.next; + toBeDelete.next = null; + this.size--; + } + + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = firstNode; + firstNode = newNode; + this.size++; + } + public void addLast(Object o){ + add(o); + } + public void removeFirst(){ + Node NextNode = firstNode.next; + firstNode.next = null; + firstNode = NextNode; + this.size--; + } + public void removeLast(){ + Node pointer = this.firstNode; + for(int i = 0;i < this.size - 2;i++){ + pointer = pointer.next; + } + Node toBeDelete = pointer.next; + pointer.next = null; + lastNode = pointer; + toBeDelete.next = null; + this.size--; + } + + private static class Node{ + Object data; + Node next; + + } + +} diff --git a/group13/6023757/lesson2/mytest/src/mytest/myMain.java b/group13/6023757/lesson2/mytest/src/mytest/myMain.java new file mode 100644 index 0000000000..f66910c724 --- /dev/null +++ b/group13/6023757/lesson2/mytest/src/mytest/myMain.java @@ -0,0 +1,50 @@ +package mytest; + +public class myMain { + + public static void main(String[] args) { + // TODO Auto-generated method stub + myMain myTest = new myMain(); +// myTest.testArrayList(); + myTest.testLinkedList(); + } + + public void testLinkedList(){ + LinkedList myList = new LinkedList(); + myList.add("aa"); + myList.add("bb"); + myList.add("cc"); + myList.add("dd"); + myList.add("ee"); + myList.add("ff"); + myList.add("gg"); + myList.removeLast(); + for(int i = 0;i < myList.size();i++){ + System.out.println(myList.get(i)); + } + + } + + public void testArrayList(){ + ArrayList myArray = new ArrayList(5); + myArray.add("xx1"); + myArray.add("xx2"); + myArray.add("xx3"); + myArray.add("xx4"); + myArray.add("xx5"); + myArray.add("xx6"); + myArray.add("xx7"); + myArray.add("xx8"); + myArray.add("xx9"); + myArray.add("xx10"); + myArray.add("xx11"); + myArray.add("xx12"); + myArray.add("xx13"); + myArray.remove(8); + int i; + for(i=0;i size){ + System.out.println("输入有误"); + return; + } + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index > size) { + System.out.println("出界"); + return null; + } + return elementData[index]; + } + + public Object remove(int index){ + if(index < 0 || index > size){ + System.out.println("输入有误"); + return null; + } + Object o = elementData[index]; + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData[size--] = null; + return o; + } + + public int size(){ + return elementData.length; + } + //判断是否越界 如果越界添加长度 + public void checkLength(int l){ + int oldSize = elementData.length; + if(l > oldSize){ //大于原来的长度创建新的数组 + elementData = Arrays.copyOf(elementData, l+100); + } + } + + public Iterator iterator(){ + return new Iterator(){ + int cursor; + public boolean hasNext() { + + return cursor != size ; + } + + public Object next() { + int i = cursor; + if (i >= size) + throw new NoSuchElementException(); + Object[] elementData_ = elementData; + if (i >= elementData_.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return elementData_[i]; + } + + }; + + } + + + + + +} diff --git a/group13/771992096/src/com/java/gsl/Iterator.java b/group13/771992096/src/com/java/gsl/Iterator.java new file mode 100644 index 0000000000..b2f2a258d8 --- /dev/null +++ b/group13/771992096/src/com/java/gsl/Iterator.java @@ -0,0 +1,7 @@ +package com.java.gsl; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group13/771992096/src/com/java/gsl/LinkedList.java b/group13/771992096/src/com/java/gsl/LinkedList.java new file mode 100644 index 0000000000..2a4b90cbbf --- /dev/null +++ b/group13/771992096/src/com/java/gsl/LinkedList.java @@ -0,0 +1,96 @@ +package com.java.gsl; + + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o){ + + } + public void add(int index , Object o){ + if(index < 0 || index > size ){ + return; + } + Node node = (Node) (index==size ? head : get(index)); + Node prenode = node.pre; +// Node l = last; +// Node newNode = new Node(l, o, null); +// last = newNode; +// if (l == null) +// head = newNode; +// else +// l.next = newNode; +// size++; + + } + public Object get(int index){ + if (index < 0 || index >= size){ + System.out.println("错误"); + return null; + } + // 获取index处的节点。 + // 若index < 双向链表长度的1/2,则从前先后查找; + // 否则,从后向前查找。 + if (index < (size/2 )) { + for (int i = 0; i <= index; i++) + head = head.next; + } else { + for (int i = size; i > index; i--) + head = head.pre; + } + return head; + + } + public Object remove(int index){ + Node x = (Node) get(index); + Node next = x.next; + Node prev = x.pre; + + if (prev == null) { + head = next; + } else { + prev.next = next; + x.pre = null; + } + + if (next == null) { + x.last = prev; + } else { + next.pre = prev; + x.next = null; + } + + size--; + return get(index); + } + + 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; + Node pre; + Node last; + } +} diff --git a/group13/771992096/src/com/java/gsl/List.java b/group13/771992096/src/com/java/gsl/List.java new file mode 100644 index 0000000000..26807394e6 --- /dev/null +++ b/group13/771992096/src/com/java/gsl/List.java @@ -0,0 +1,9 @@ +package com.java.gsl; + +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/group13/771992096/src/com/java/gsl/Queue.java b/group13/771992096/src/com/java/gsl/Queue.java new file mode 100644 index 0000000000..64645156ec --- /dev/null +++ b/group13/771992096/src/com/java/gsl/Queue.java @@ -0,0 +1,20 @@ +package com.java.gsl; + +public class Queue { + LinkedList queue = new LinkedList(); + public void enQueue(Object o){ + queue.add(o); + } + + public Object deQueue(){ + return queue.removeLast(); + } + + public boolean isEmpty(){ + return queue.size() > 0; + } + + public int size(){ + return queue.size(); + } +} diff --git a/group13/771992096/src/com/java/gsl/Stack.java b/group13/771992096/src/com/java/gsl/Stack.java new file mode 100644 index 0000000000..438e07a8ee --- /dev/null +++ b/group13/771992096/src/com/java/gsl/Stack.java @@ -0,0 +1,27 @@ +package com.java.gsl; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int i = elementData.size(); + elementData.remove(i); + return elementData.get(i); + } + + public Object peek(){ + int i = elementData.size(); + return elementData.get(i); + } + public boolean isEmpty(){ + + return elementData.size()>0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group13/group13.md b/group13/group13.md index d3f5a12faa..8b13789179 100644 --- a/group13/group13.md +++ b/group13/group13.md @@ -1 +1 @@ - + diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.classpath" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.classpath" new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.classpath" @@ -0,0 +1,7 @@ + + + + + + + diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.gitignore" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.gitignore" new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.gitignore" @@ -0,0 +1 @@ +/bin/ diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.project" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.project" new file mode 100644 index 0000000000..194f47da7a --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.project" @@ -0,0 +1,17 @@ + + + basicstructuredemo + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.settings/org.eclipse.jdt.core.prefs" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.settings/org.eclipse.jdt.core.prefs" new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/.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/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/ArrayList.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/ArrayList.java" new file mode 100644 index 0000000000..f6d3b4c44a --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/ArrayList.java" @@ -0,0 +1,66 @@ +package com.maple.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //不够了怎么扩容 + elementData[size++]=o; + } + public void add(int index, Object o){ + if(index<0||index>size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + for(int i=size;i>index;i--){ + elementData[i-1]=elementData[i]; + } + elementData[index]=o; + size++; + } + + public Object get(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + Object removeObj=elementData[index]; + for(int i=index;i=size) return false; + return true; + } + }; + } + +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTree.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTree.java" new file mode 100644 index 0000000000..5e63cf4d3c --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTree.java" @@ -0,0 +1,45 @@ +package com.maple.basic; + +import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName; + +public class BinaryTree> { + private BinaryTreeNode root; + + public void traversal(BinaryTreeNode node){ + if(node.getLeft()!=null){ + traversal(node.getLeft()); + } + System.out.println("--"+node.getData()+"--"); + if(node.getRight()!=null){ + traversal(node.getRight()); + } + } + /** + * 如果根节点为null,则作为根节点,否则遍历下去插值 + * @param o + * @return + * 2017年2月23日 下午4:21:51 + * @Author Joy + */ + public BinaryTreeNode insert(T o){ + if(root==null){ + BinaryTreeNode newB=new BinaryTreeNode(); + newB.setData(o); + newB.setLeft(null); + newB.setRight(null); + root=newB; + return root; + } + + return root.insert(o); + } + public BinaryTreeNode getRoot() { + return root; + } + + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + + +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTreeNode.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTreeNode.java" new file mode 100644 index 0000000000..f702d48922 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/BinaryTreeNode.java" @@ -0,0 +1,69 @@ +package com.maple.basic; + +import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName; + +public class BinaryTreeNode>{ + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + /** + * 如果待插入的值等于节点的值,则抛出异常:duplicate value + * 如果小于节点的值,则往左边遍历 + * 如果大于节点的值,则往右边遍历 + * @param o + * @return + * 2017年2月23日 下午4:22:50 + * @Author Joy + */ + public BinaryTreeNode insert(T o){ + //assume that no duplicate key + + if(o.compareTo(data)==0){ + try { + throw new DuplicateName("duplicate value: "+o); + } catch (DuplicateName e) { + e.printStackTrace(); + } + } + BinaryTreeNode newB=new BinaryTreeNode(); + newB.setData(o); + newB.setLeft(null); + newB.setRight(null); + //o更大,在右边 + if(o.compareTo(data)>0){ + if(this.getRight()!=null){ + this.getRight().insert(o); + }else{ + this.setRight(newB); + } + }else if(o.compareTo(data)<0){ + if(this.getLeft()!=null){ + this.getLeft().insert(o); + }else{ + this.setLeft(newB); + } + } + return newB; + } + + 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; + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Iterator.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Iterator.java" new file mode 100644 index 0000000000..ac8ecd6050 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Iterator.java" @@ -0,0 +1,7 @@ +package com.maple.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/LinkedList.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/LinkedList.java" new file mode 100644 index 0000000000..d957f74bdc --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/LinkedList.java" @@ -0,0 +1,154 @@ +package com.maple.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size = 0;//自己加的,觉得需要 + /** + * 与addList()是一样的 + */ + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + if(index<0||index>size){ + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + } + Node prevNode=head; + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + Node newNode=new Node(); + newNode.data=o; + + newNode.next=curNode; + prevNode.next=newNode; + size++; + break; + } + curNode=curNode.next; + prevNode=prevNode.next; + count++; + } + + + } + public Object get(int index){ + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + return curNode.data; + } + curNode=curNode.next; + count++; + } + return null; + } + public Object remove(int index){ + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + Node prevNode=head; + Node curNode=head.next; + int count=0; + while(count<=index){ + if(count==index){ + prevNode.next=curNode.next; + size--; + return curNode.data; + } + curNode=curNode.next; + prevNode=prevNode.next; + count++; + } + return null; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node objNode=new Node(); + objNode.data=o; + if(head==null) head=new Node(); + objNode.next=head.next; + size++; + head.next=objNode; + } + public void addLast(Object o){ + Node objNode=new Node(); + objNode.data=o; + if(head==null) head=new Node(); + + //也可以用iterator迭代,先不用吧 + Node curNode=head; + while(curNode.next!=null){ + curNode=curNode.next; + } + objNode.next=curNode.next; + curNode.next=objNode; + size++; + + } + public Object removeFirst(){ + if(head==null||head.next==null) + throw new NoSuchElementException(); + Node delNode=head.next; + head.next=delNode.next; + size--; + return delNode.data; + } + public Object removeLast(){ + if(head==null||head.next==null) + throw new NoSuchElementException(); + Node prevNode=head; + Node curNode=head.next; + while(curNode!=null){ + if(curNode.next==null){//说明是尾节点 + prevNode.next=curNode.next; + size--; + return curNode.data; + } + curNode=curNode.next; + prevNode=prevNode.next; + } + return null; + } + public Iterator iterator(){ + return new Iterator() { + private Node cur=head!=null?head.next:head; + @Override + public Object next() { + if(cur==null){ + throw new NoSuchElementException(); + } + Object object=cur.data; + cur=cur.next; + return object; + } + + @Override + public boolean hasNext() { + if(cur==null){ + return false; + }else{ + return true; + } + + } + }; + } + + + private static class Node{ + Object data; + Node next; + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/List.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/List.java" new file mode 100644 index 0000000000..99bed9d96b --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/List.java" @@ -0,0 +1,9 @@ +package com.maple.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/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Queue.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Queue.java" new file mode 100644 index 0000000000..278d3dba7f --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Queue.java" @@ -0,0 +1,21 @@ +package com.maple.basic; + +public class Queue { + private LinkedList elementData=new LinkedList(); + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + Object first=elementData.removeFirst(); + return first; + } + + public boolean isEmpty(){ + return elementData.size()<=0; + } + + public int size(){ + return elementData.size(); + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Stack.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Stack.java" new file mode 100644 index 0000000000..cec4599237 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/basic/Stack.java" @@ -0,0 +1,26 @@ +package com.maple.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object object=elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return object; + } + + public Object peek(){ + Object object=elementData.get(elementData.size()-1); + return object; + } + public boolean isEmpty(){ + return elementData.size()<=0; + } + public int size(){ + return elementData.size(); + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAPIDemo.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAPIDemo.java" new file mode 100644 index 0000000000..ee592fbb7b --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAPIDemo.java" @@ -0,0 +1,53 @@ +package com.maple.test; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Stack; + + +/** + * 测试Java中的API + * @author group 14, QQ:1091149131 + */ +public class TestAPIDemo { + public static void main(String[] args) { + + //Stack + /*Stack stack=new Stack(); + stack.push(0); + stack.push(1); + stack.push(2); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.peek());*/ + + //LinkedList + /*LinkedList list2=new LinkedList<>(); + list2.add(0); + list2.add(1); + list2.add(2); + + list2.addLast(3); + list2.remove(0); + //list2.removeFirst(); + Iterator ite2=list2.iterator(); + ite2.next(); + while(ite2.hasNext()){ + System.out.println(ite2.next()); + }*/ + + + //ArrayList + /*ArrayList list1=new ArrayList(); + list1.add(0); + list1.add(1); + //list1.add(3, -1);//error + //list1.remove(2);//error + Iterator ite=list1.iterator(); + while(ite.hasNext()){ + System.out.println(ite.next()); + }*/ + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAll.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAll.java" new file mode 100644 index 0000000000..9fd59e512e --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestAll.java" @@ -0,0 +1,71 @@ +package com.maple.test; + +import org.junit.Test; + +import com.maple.basic.ArrayList; +import com.maple.basic.BinaryTree; +import com.maple.basic.Iterator; +import com.maple.basic.LinkedList; +import com.maple.basic.Queue; +import com.maple.basic.Stack; + +public class TestAll { + @Test + public void testArrayList(){ + ArrayList list1=new ArrayList(); + list1.add(0); + list1.add(1); + //list1.add(3, -1);//error + //list1.remove(2);//error + Iterator ite=list1.iterator(); + while(ite.hasNext()){ + System.out.println(ite.next()); + } + } + @Test + public void testLinkedList(){ + LinkedList list2=new LinkedList(); + list2.add(0); + list2.add(1); + list2.addFirst(-1); + list2.addLast(-2); + + list2.removeFirst(); + list2.removeLast(); + list2.remove(0); + + Iterator ite2=list2.iterator(); + while(ite2.hasNext()){ + System.out.println(ite2.next()); + } + } + @Test + public void testStack(){ + Stack stack=new Stack(); + stack.push(0); + stack.push(1); + stack.push(2); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.peek()); + } + @Test + public void testQueue(){ + Queue queue=new Queue(); + queue.enQueue(0); + queue.enQueue(1); + + System.out.println(queue.deQueue()); + } + @Test + public void testBinaryTree(){ + BinaryTree tree=new BinaryTree<>(); + tree.insert(3); + tree.insert(2); + tree.insert(5); + //tree.insert(5);//error,duplicate + tree.insert(1); + tree.traversal(tree.getRoot()); + } + +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestArrayList.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestArrayList.java" new file mode 100644 index 0000000000..94ecec52f7 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestArrayList.java" @@ -0,0 +1,22 @@ +package com.maple.test; + +import org.junit.Test; + +import com.maple.basic.ArrayList; +import com.maple.basic.Iterator; + +public class TestArrayList{ + + @Test + public void testAdd(){ + ArrayList list1=new ArrayList(); + list1.add(0); + list1.add(1); + //list1.add(3, -1);//error + //list1.remove(2);//error + Iterator ite=list1.iterator(); + while(ite.hasNext()){ + System.out.println(ite.next()); + } + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestMyDemo.java" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestMyDemo.java" new file mode 100644 index 0000000000..639aaa629b --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/basicstructuredemo/src/com/maple/test/TestMyDemo.java" @@ -0,0 +1,75 @@ +package com.maple.test; + +import com.maple.basic.ArrayList; +import com.maple.basic.BinaryTree; +import com.maple.basic.BinaryTreeNode; +import com.maple.basic.Iterator; +import com.maple.basic.LinkedList; +import com.maple.basic.Queue; +import com.maple.basic.Stack; + +/** + * 测试自己写的数据结构 + * @author group 14, QQ:1091149131 + */ +public class TestMyDemo { + public static void main(String[] args) { + //BinaryTree + BinaryTree tree=new BinaryTree<>(); + tree.insert(3); + tree.insert(2); + tree.insert(5); + //tree.insert(5);//error,duplicate + tree.insert(1); + tree.traversal(tree.getRoot()); + + + //Queue + /*Queue queue=new Queue(); + queue.enQueue(0); + queue.enQueue(1); + + System.out.println(queue.deQueue());*/ + + //Stack + /*Stack stack=new Stack(); + stack.push(0); + stack.push(1); + stack.push(2); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.peek());*/ + + //LinkedList + /*LinkedList list2=new LinkedList(); + list2.add(0); + list2.add(1); + list2.addFirst(-1); + list2.addLast(-2); + + list2.removeFirst(); + list2.removeLast(); + list2.remove(0); + + Iterator ite2=list2.iterator(); + while(ite2.hasNext()){ + System.out.println(ite2.next()); + }*/ + + + //ArrayList + /*ArrayList list1=new ArrayList(); + list1.add(0); + list1.add(1); + //list1.add(3, -1);//error + //list1.remove(2);//error + Iterator ite=list1.iterator(); + while(ite.hasNext()){ + System.out.println(ite.next()); + }*/ + + + + + } +} diff --git "a/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/\346\226\207\347\253\240\351\223\276\346\216\245_20170224.txt" "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/\346\226\207\347\253\240\351\223\276\346\216\245_20170224.txt" new file mode 100644 index 0000000000..1ecad55f85 --- /dev/null +++ "b/group14/1091149131/20170226_\344\275\234\344\270\2321_\346\225\260\346\215\256\347\273\223\346\236\204\345\217\212\346\226\207\347\253\240/\346\226\207\347\253\240\351\223\276\346\216\245_20170224.txt" @@ -0,0 +1,5 @@ +CPUڴ棬 Ӳָ̣֮Ĺϵ + +ӣhttp://www.cnblogs.com/qingmaple/p/6437070.html + +QQ1091149131 -Ҷ \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.classpath b/group14/187114392/work_1_20170225/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group14/187114392/work_1_20170225/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group14/187114392/work_1_20170225/.gitignore b/group14/187114392/work_1_20170225/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group14/187114392/work_1_20170225/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group14/187114392/work_1_20170225/.idea/.name b/group14/187114392/work_1_20170225/.idea/.name new file mode 100644 index 0000000000..9769cfad31 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/.name @@ -0,0 +1 @@ +2017Learning \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/compiler.xml b/group14/187114392/work_1_20170225/.idea/compiler.xml new file mode 100644 index 0000000000..96cc43efa6 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/copyright/profiles_settings.xml b/group14/187114392/work_1_20170225/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..e7bedf3377 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/encodings.xml b/group14/187114392/work_1_20170225/.idea/encodings.xml new file mode 100644 index 0000000000..97626ba454 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/junitgenerator-prj-settings.xml b/group14/187114392/work_1_20170225/.idea/junitgenerator-prj-settings.xml new file mode 100644 index 0000000000..b6bb9db746 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/junitgenerator-prj-settings.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/misc.xml b/group14/187114392/work_1_20170225/.idea/misc.xml new file mode 100644 index 0000000000..6a48f33378 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/modules.xml b/group14/187114392/work_1_20170225/.idea/modules.xml new file mode 100644 index 0000000000..f37bb20093 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/uiDesigner.xml b/group14/187114392/work_1_20170225/.idea/uiDesigner.xml new file mode 100644 index 0000000000..e96534fb27 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/work_1_20170225.iml b/group14/187114392/work_1_20170225/.idea/work_1_20170225.iml new file mode 100644 index 0000000000..d6ebd48059 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/work_1_20170225.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.idea/workspace.xml b/group14/187114392/work_1_20170225/.idea/workspace.xml new file mode 100644 index 0000000000..8c5615cc95 --- /dev/null +++ b/group14/187114392/work_1_20170225/.idea/workspace.xml @@ -0,0 +1,1295 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1488028819234 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/.project b/group14/187114392/work_1_20170225/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group14/187114392/work_1_20170225/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group14/187114392/work_1_20170225/.settings/org.eclipse.jdt.core.prefs b/group14/187114392/work_1_20170225/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group14/187114392/work_1_20170225/.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/group14/187114392/work_1_20170225/2017Learning.iml b/group14/187114392/work_1_20170225/2017Learning.iml new file mode 100644 index 0000000000..c4678227ee --- /dev/null +++ b/group14/187114392/work_1_20170225/2017Learning.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/ReadMe.md b/group14/187114392/work_1_20170225/ReadMe.md new file mode 100644 index 0000000000..79077789f6 --- /dev/null +++ b/group14/187114392/work_1_20170225/ReadMe.md @@ -0,0 +1,3 @@ +这个项目用idea直接打开。 +运行里面的test 目录就可以直接单元测试。 +谢谢 diff --git a/group14/187114392/work_1_20170225/src/Main.java b/group14/187114392/work_1_20170225/src/Main.java new file mode 100644 index 0000000000..3e44cc8e16 --- /dev/null +++ b/group14/187114392/work_1_20170225/src/Main.java @@ -0,0 +1,31 @@ +/** + * Created by bshu on 2017/2/25. + */ +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + +import java.util.*; + +/** + * Created by bshu on 2017/2/21. + */ +public class Main { + public static void main(String[] args) { + int size = 0; + System.out.println("1 size " + (size++)); + System.out.println("2 size " + (size)); + LinkedList lnk = new LinkedList(); + java.util.ArrayList list = new java.util.ArrayList(){{add("str0");}}; + lnk.add("kk1"); + lnk.add("kk2"); + lnk.add("kk3"); + lnk.remove(2); + lnk.iterator(); + int count = 0; + for (java.util.Iterator iter = lnk.iterator(); iter.hasNext();) { + System.out.printf("%s is :%s \n",count, iter.next()); + ++count; + } + } +} + diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/ArrayList.java b/group14/187114392/work_1_20170225/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..78f24c6dae --- /dev/null +++ b/group14/187114392/work_1_20170225/src/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; +import java.util.Arrays; +import java.lang.IndexOutOfBoundsException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void ensure_length(int expect_length) { + if (expect_length > elementData.length) { + elementData = Arrays.copyOf(elementData,elementData.length * 2); + } + } + + public void add(Object o){ + ensure_length(size + 1); + elementData[size] = o; + size += 1; + } + + public void add(int index, Object o){ + ensure_length(size + 1); + System.arraycopy(elementData,index, + elementData,index + 1, + size - index + 1); + elementData[index] = o; + size += 1; + } + + public Object get(int index) { + if (index > size) { + throw new IndexOutOfBoundsException("index out of bounds"); + } else { + return elementData[index]; + } + } + + public Object remove(int index){ + Object oldvalue = elementData[index]; + System.arraycopy(elementData,index + 1,elementData,index,size - index - 1); + elementData[--size] = null; + return oldvalue; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator_ip(); + } + + private class Iterator_ip implements Iterator { + private int index = 0; + + @Override + public boolean hasNext() { + return (index + 1) <= size; + } + + @Override + public Object next() { + if (index > size) { +// throw new IndexOutOfBoundsException("iterator next out of bounds"); + return null; + } + return elementData[index++]; + } + } + +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/BinaryTreeNode.java b/group14/187114392/work_1_20170225/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group14/187114392/work_1_20170225/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/group14/187114392/work_1_20170225/src/com/coding/basic/Iterator.java b/group14/187114392/work_1_20170225/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group14/187114392/work_1_20170225/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/group14/187114392/work_1_20170225/src/com/coding/basic/LinkedList.java b/group14/187114392/work_1_20170225/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..89b4523e0a --- /dev/null +++ b/group14/187114392/work_1_20170225/src/com/coding/basic/LinkedList.java @@ -0,0 +1,168 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private Node last_but_one; + private int size = 0; + + public LinkedList() { + tail = new Node(); + tail.data = null; + tail.next = null; + + head = new Node(); + head.next = null; + } + + private void add_elementbyindex(int index , Object o) { + Node new_node = new Node(); + if (index == 0) { + if (size > 0) { + new_node.data = head.data; + } else { +// System.out.println("first new node is null"); + new_node.data = null; + head.next = null; + } + head.data = o; + } else { + new_node.data = o; + } + + int count = 0; + Node k = head; + while (count < index - 1) { +// System.out.printf("add count:%s, k is:%s \n",count,k.data); + k = k.next; + ++count; + } + new_node.next = k.next; + k.next = new_node; + ++size; + } + + private Object remove_elementbyindex(int index) { + int count = 0; + Object remove_ob = null; + Node k = head; + while (count < index - 1) { +// System.out.printf("remove count:%s, k is:%s \n",count,k.data); + k = k.next; + ++count; + } + + if (index == 0) { + remove_ob = head.data; + head.data = head.next.data; + if (size == 1) { + head.data = null; + } + head.next = head.next.next; + } else if ((index - 1) == size) { +// System.out.printf("index:%s == size, count:%s, k is:%s \n",index,count,k.data); + remove_ob = k.data; + k.next = null; + } else { + if (size == 1) { + k.data = null; + } + remove_ob = k.next.data; + k.next = k.next.next; + } + --size; + return remove_ob; + } + + public void add(Object o){ + add_elementbyindex(size(),o); + } + + public void add(int index , Object o){ + // if index larger than size , then we set index equal to size; + if (index > size) { + throw new IndexOutOfBoundsException("index out of bounds"); + } + add_elementbyindex(index,o); + + } + public Object get(int index){ + if (index > size) { + throw new IndexOutOfBoundsException("get index out of bounds"); + } + Node k = head; + int count = 0; + while (count < index) { +// System.out.printf("get count:%s, k is:%s \n",count,k.data); + k = k.next; + ++count; + } +// System.out.printf("get count:%s, k is:%s \n",count,k.data); + return k.data; + } + public Object remove(int index){ + if (index < 0 || index > size || size == 0) { + return null; + } + return remove_elementbyindex(index); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + add_elementbyindex(0,o); + } + public void addLast(Object o){ + add_elementbyindex(size,o); + } + public Object removeFirst(){ + if (size == 0) { + return null; + } + return remove_elementbyindex(0); + } + public Object removeLast(){ + if (size == 0) { + return null; + } + return remove_elementbyindex(size); + } + public Iterator iterator(){ + Iterator_ip iter = new Iterator_ip(head); + return iter; + } + + private static class Node{ + Object data; + Node next; + } + + private class Iterator_ip implements Iterator { + private int index = 0; + private Node node_iter; + + public Iterator_ip(Node node_iter) { + this.node_iter = node_iter; + } + + @Override + public boolean hasNext() { + return node_iter.next != null; + } + + @Override + public Object next() { + if (index > size) { +// throw new IndexOutOfBoundsException("iterator next out of bounds"); + return null; + } + Object now_data = node_iter.data; + node_iter = node_iter.next; + ++index; + return now_data; + } + } +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/List.java b/group14/187114392/work_1_20170225/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group14/187114392/work_1_20170225/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/group14/187114392/work_1_20170225/src/com/coding/basic/Queue.java b/group14/187114392/work_1_20170225/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..ada4130038 --- /dev/null +++ b/group14/187114392/work_1_20170225/src/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList lnk = new LinkedList(); + + public void enQueue(Object o){ + lnk.addLast(o); + } + + public Object deQueue(){ + return lnk.removeFirst(); + } + + public boolean isEmpty(){ + return lnk.size() == 0; + } + + public int size(){ + return lnk.size(); + } +} diff --git a/group14/187114392/work_1_20170225/src/com/coding/basic/Stack.java b/group14/187114392/work_1_20170225/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..d553579b79 --- /dev/null +++ b/group14/187114392/work_1_20170225/src/com/coding/basic/Stack.java @@ -0,0 +1,37 @@ +package com.coding.basic; +import java.lang.ArrayIndexOutOfBoundsException; +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); +// System.out.println("size is " + elementData.size()); + } + + public Object pop() { + int elementData_size = elementData.size(); + if (elementData_size == 0) { + throw new EmptyStackException(); + } + Object top_data = elementData.get(elementData_size - 1); + elementData.remove(elementData_size - 1); + return top_data; + } + + public Object peek() { + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group14/187114392/work_1_20170225/test/ArrayList_Test.java b/group14/187114392/work_1_20170225/test/ArrayList_Test.java new file mode 100644 index 0000000000..fbfbdf1b3a --- /dev/null +++ b/group14/187114392/work_1_20170225/test/ArrayList_Test.java @@ -0,0 +1,52 @@ +import com.coding.basic.ArrayList; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by bshu on 2017/2/25. + */ +public class ArrayList_Test { + @Test + public void add_test() { + ArrayList arr = new ArrayList(); + arr.add("one"); + assertEquals(arr.get(0),"one"); + arr.add("two"); + assertEquals(arr.get(1),"two"); + } + + @Test + public void add_index_test() { + ArrayList arr = new ArrayList(); + arr.add(0,"one"); + assertEquals(arr.get(0),"one"); + arr.add(1,"two"); + arr.add(2,"three"); + arr.add(3,"four"); + arr.add(4,"five"); + assertEquals(arr.get(4),"five"); + arr.add(2,"duplicte two"); + assertEquals(arr.get(2),"duplicte two"); + } + + @Test + public void remove_test() { + ArrayList arr = new ArrayList(); + arr.add(0,"one"); + arr.add(1,"two"); + arr.add(2,"three"); + arr.remove(1); + assertEquals(arr.get(1),"three"); + } + + @Test + public void get_test() { + ArrayList arr = new ArrayList(); + arr.add(0,"one"); + arr.add(1,"two"); + arr.add(2,"three"); + assertEquals(arr.get(1),"two"); + assertEquals(arr.get(3),null); + } +} \ No newline at end of file diff --git a/group14/187114392/work_1_20170225/test/LinkedList_Test.java b/group14/187114392/work_1_20170225/test/LinkedList_Test.java new file mode 100644 index 0000000000..986366cb6a --- /dev/null +++ b/group14/187114392/work_1_20170225/test/LinkedList_Test.java @@ -0,0 +1,122 @@ +import org.junit.Test; +import com.coding.basic.*; +import java.lang.Object; + +import static org.junit.Assert.assertEquals; + +/** + * LinkedList_Test Tester. + * + * @author + * @since
bshu 26, 2017
+ * @version 1.0 + */ +public class LinkedList_Test { + @Test + public void get() { + LinkedList link = new LinkedList(); + link.add("kkk"); + assertEquals(link.get(0),"kkk"); + link.add("kkk1"); + assertEquals(link.get(1),"kkk1"); + } + + @Test + public void add() { + LinkedList link = new LinkedList(); + link.add("kkk"); + link.add("aaa"); + link.add("bbb"); + link.add("ccc"); + link.add("ddd"); + link.add("eee"); + Object[] ob = new Object[]{"kkk","aaa","bbb","ccc","ddd","eee"}; + int count = 0; + for (Iterator iter = link.iterator(); iter.hasNext();) { + Object data = iter.next(); +// System.out.printf("%s is :%s \n",count, data); + assertEquals(data,ob[count]); + ++count; + } + } + + @Test + public void add_index() { + LinkedList link = new LinkedList(); + link.add("kkk"); +// System.out.println("0 is " + link.get(0)); + link.add("kkk2"); + link.add(0,"0kkk"); + link.add(3,"3kkk"); + assertEquals(link.get(0),"0kkk"); + assertEquals(link.get(2),"kkk2"); + } + + @Test + public void add_first() { + LinkedList link = new LinkedList(); + link.add("kkk"); + link.addFirst("F_kkk"); + assertEquals(link.get(0),"F_kkk"); + assertEquals(link.get(1),"kkk"); + } + + @Test + public void add_last() { + LinkedList link = new LinkedList(); + link.add("kkk"); + link.addLast("L_kkk"); + assertEquals(link.get(0),"kkk"); + assertEquals(link.get(link.size() - 1),"L_kkk"); + } + + @Test + public void remove() { + LinkedList link = new LinkedList(); + link.add("kkk"); +// System.out.println("0 is " + link.get(0)); + link.add("kkk1"); + link.add("kkk2"); + link.remove(2); + link.add(link.size(),"2kkk"); + link.add("kkk3"); + link.add("kkk4"); + link.add("kkk5"); + link.remove(link.size()); + link.add("kkk6"); + int count = 0; + Object[] ob = new Object[]{"kkk","kkk1","2kkk","kkk3","kkk4","kkk6"}; + for (Iterator iter = link.iterator(); iter.hasNext();) { + Object data = iter.next(); + System.out.printf("%s is :%s \n",count, data); + assertEquals(data,ob[count]); + ++count; + } + } + + @Test + public void remove_first() { + LinkedList link = new LinkedList(); + link.add("kkk"); + link.add("kkk1"); + link.add("kkk2"); + link.removeFirst(); + assertEquals("kkk1",link.get(0)); + assertEquals("kkk2",link.get(1)); + assertEquals(2,link.size()); + } + + @Test + public void remove_last() { + LinkedList link = new LinkedList(); + link.add("kkk"); + link.add("kkk1"); + link.removeLast(); + assertEquals("kkk",link.get(0)); + link.removeLast(); + link.removeLast(); + link.remove(0); + link.remove(0); + assertEquals(null,link.get(0)); + } +} diff --git a/group14/187114392/work_1_20170225/test/Queue_Test.java b/group14/187114392/work_1_20170225/test/Queue_Test.java new file mode 100644 index 0000000000..319baf6706 --- /dev/null +++ b/group14/187114392/work_1_20170225/test/Queue_Test.java @@ -0,0 +1,44 @@ + +import com.coding.basic.LinkedList; +import com.coding.basic.Queue; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import static org.junit.Assert.assertEquals; + +/** + * Queue_Test Tester. + * + * @author + * @since
bshu 26, 2017
+ * @version 1.0 + */ +public class Queue_Test { + + @Test + public void enQueue() { + Queue que = new Queue(); + que.enQueue("kkk"); + que.enQueue("kkk1"); + assertEquals(2,que.size()); + } + + @Test + public void deQueue() { + Queue que = new Queue(); + que.enQueue("kkk"); + que.enQueue("kkk1"); + Object data = que.deQueue(); + assertEquals("kkk",data); + } + + @Test + public void isempty() { + Queue que = new Queue(); + assertEquals(true,que.isEmpty()); + que.enQueue("kk2"); + assertEquals(false,que.isEmpty()); + } + +} diff --git a/group14/187114392/work_1_20170225/test/Stack_Test.java b/group14/187114392/work_1_20170225/test/Stack_Test.java new file mode 100644 index 0000000000..8a11a20632 --- /dev/null +++ b/group14/187114392/work_1_20170225/test/Stack_Test.java @@ -0,0 +1,72 @@ + +import org.junit.Test; +import com.coding.basic.*; + +import java.util.EmptyStackException; + +import static org.junit.Assert.assertEquals; + +/** + * Stack_Test Tester. + * + * @author + * @since
bshu 26, 2017
+ * @version 1.0 + */ +public class Stack_Test { + @Test + public void pop() { + Stack stk = new Stack(); + stk.push("one"); + stk.push("two"); + assertEquals(stk.pop(),"two"); + } + + @Test + public void pop_empty() { + Stack stk = new Stack(); + try { + stk.pop(); + } + catch (EmptyStackException e) { + assertEquals(e.toString(),"java.util.EmptyStackException"); + } + } + + @Test + public void push() { + Stack stk = new Stack(); + stk.push("one"); + stk.push("two"); + assertEquals(stk.peek(),"two"); + } + + @Test + public void peek() { + Stack stk = new Stack(); + stk.push("one"); + stk.push("two"); + stk.push("three"); + assertEquals(stk.peek(),"three"); + } + + @Test + public void peek_empty() { + Stack stk = new Stack(); + try { + stk.peek(); + } + catch (EmptyStackException e) { + assertEquals(e.toString(),"java.util.EmptyStackException"); + } + } + + @Test + public void isempty() { + Stack stk = new Stack(); + stk.push("one"); + assertEquals(stk.isEmpty(),false); + stk.pop(); + assertEquals(stk.isEmpty(),true); + } +} diff --git "a/group14/190530132/20170219\344\275\234\344\270\232/ArrayList.java" "b/group14/190530132/20170219\344\275\234\344\270\232/ArrayList.java" new file mode 100644 index 0000000000..079540bc4e --- /dev/null +++ "b/group14/190530132/20170219\344\275\234\344\270\232/ArrayList.java" @@ -0,0 +1,67 @@ +package com.coding.basic; + +public class ArrayList { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + + //在ArrayList的尾部添加 + public void add(Object o){ + + size = elementData.length + 1; + Object[] tempData = new Object[size]; + System.arraycopy(elementData, 0, tempData,0, elementData.length); + elementData = tempData; + + elementData[size-1] = o; + + } + + //在ArrayList中的某一个元素后面添加, 这里的关键在于先移动末尾的元素 + public void add(int index, Object o){ + + size = elementData.length + 1; + Object[] tempData = new Object[size]; + System.arraycopy(elementData, 0, tempData,0, elementData.length); + elementData = tempData; + + for (int i=elementData.length-2; i>index; i--) { + elementData[i+1] = elementData[i]; + } + + elementData[index+1] = o; + + } + + //按下标来访问ArrayList中的元素 + public Object get(int index){ + return elementData[index]; + } + + //按下标来删除ArrayList中的元素 + public Object remove(int index){ + Object r = elementData[index]; + for (int i=index; i= size) { + throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size); + } + + Node n = new Node(); + n.data = o; + Node m = get(index); + n.next = m.next; + m.next = n; + size = size + 1; + } + + public Node get(int index){ + //index越界检查 + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index:" + index + ",size:" + size); + } + + Node n = head.next; + int count = 0; + while(count<=index){ + if(count==index){ + return n; + } + n = n.next; + count++; + } + + return null; + } + + public void remove(int index){ + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Joy Index "+index+", Size: "+size); + + Node d = get(index); + Node pred = get(index-1); + pred.next = d.next; + size = size - 1; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node n = new Node(); + n.data = o; + + //避免空链表 + if (head==null) head=new Node(); + + n.next = head.next; + head.next = n; + size = size + 1; + } + + public void addLast(Object o){ + Node n = new Node(); + n.data = o; + + //避免空链表 + if (head==null) head = new Node(); + + //从头部往后顺序查找,找到尾部就添加 + Node m = head; + while (m.next != null){ + m = m.next; + } + n.next = m.next; + m.next = n; + size = size + 1; + } + + public Object removeFirst(){ + if(head==null||head.next==null) + throw new NoSuchElementException(); + Node d = head.next; + head.next = d.next; + size = size - 1; + return d.data; + } + + public Object removeLast(){ + if(head==null||head.next==null) + throw new NoSuchElementException(); + + Node m = head; + Node n = head.next; + while(n.next != null){ + m = n; + n = n.next; + } + size = size - 1; + return m.data; + } + + //public Iterator iterator(){ + // return null; + //} + + private static class Node{ + Node next; + Object data; + } + +} diff --git "a/group14/190530132/20170219\344\275\234\344\270\232/Queue.java" "b/group14/190530132/20170219\344\275\234\344\270\232/Queue.java" new file mode 100644 index 0000000000..365b65aecb --- /dev/null +++ "b/group14/190530132/20170219\344\275\234\344\270\232/Queue.java" @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Queue { + + LinkedList l = new LinkedList(); + public void enQueue(Object o){ + l.addLast(o); + } + + public Object deQueue(){ + return l.removeFirst(); + } + + public boolean isEmpty(){ + if (l.size() == 0) + return true; + else + return false; + } + + public int size(){ + return l.size(); + } + +} diff --git "a/group14/190530132/20170219\344\275\234\344\270\232/Stack.java" "b/group14/190530132/20170219\344\275\234\344\270\232/Stack.java" new file mode 100644 index 0000000000..f73de31abc --- /dev/null +++ "b/group14/190530132/20170219\344\275\234\344\270\232/Stack.java" @@ -0,0 +1,33 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int index = elementData.size() - 1; + Object o = elementData.remove(index); + return o; + } + + public Object peek(){ + int e = elementData.size() - 1; + return elementData.get(e); + } + + public boolean isEmpty(){ + if (elementData.size() == 0 ) + return true; + else + return false; + } + + public int size(){ + return elementData.size(); + } + +} diff --git "a/group14/190530132/20170219\344\275\234\344\270\232/\346\226\207\347\253\240\351\223\276\346\216\245.txt" "b/group14/190530132/20170219\344\275\234\344\270\232/\346\226\207\347\253\240\351\223\276\346\216\245.txt" new file mode 100644 index 0000000000..2c79e20a62 --- /dev/null +++ "b/group14/190530132/20170219\344\275\234\344\270\232/\346\226\207\347\253\240\351\223\276\346\216\245.txt" @@ -0,0 +1 @@ +http://rexwcl.blog.163.com/blog/static/270599039201712651450997/ \ No newline at end of file diff --git a/group14/254659936/.gitignore b/group14/254659936/.gitignore new file mode 100644 index 0000000000..5561991601 --- /dev/null +++ b/group14/254659936/.gitignore @@ -0,0 +1,10 @@ +*.iml +.gradle +.idea +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures +.externalNativeBuild \ No newline at end of file diff --git a/group14/254659936/src/Main.java b/group14/254659936/src/Main.java new file mode 100644 index 0000000000..4d4fbb71ef --- /dev/null +++ b/group14/254659936/src/Main.java @@ -0,0 +1,7 @@ + +public class Main { + + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} diff --git a/group14/254659936/src/com/coding/basic/ArrayList.java b/group14/254659936/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7fb9c076b6 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +import java.util.Objects; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[5]; + + public void add(Object o) { + if (size == elementData.length) { + Object[] newArr = new Object[elementData.length * 2]; + System.arraycopy(newArr, 0, elementData, 0, elementData.length); + elementData = newArr; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + elementData[index] = o; + } + + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("the ArrayList size is short than index"); + } + Object resultObj = elementData[index]; + size--; + for (int i = index; i < size; i++) { + elementData[index] = elementData[index + 1]; + } + return resultObj; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private class ArrayIterator implements Iterator { + + private int iteratorIndex = 0; + + @Override + public boolean hasNext() { + return iteratorIndex < size; + } + + @Override + public Object next() { + if (iteratorIndex >= size) { + throw new RuntimeException("the index is out of the list"); + } + return elementData[iteratorIndex++]; + } + } + +} diff --git a/group14/254659936/src/com/coding/basic/BinaryTreeNode.java b/group14/254659936/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..8b89c37114 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,69 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + 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 insert(T o) { + BinaryTreeNode node = new BinaryTreeNode(); + node.setData(o); + insert(this, node); + return node; + } + + private void insert(BinaryTreeNode parent, BinaryTreeNode child) { + BinaryTreeNode node; + if (child.getData().isLargeThanTarget(parent.getData())) { + // 子节点比父节点大,需要向右插入 + node = getRight(); + if (null == node) { + // 右节点为空则可以直接插入 + parent.setRight(node); + } else { + // 递归检查右边子树的插入位置 + insert(node, child); + } + } else { + // 子节点比父节点小,或者等于父节点,需要向左插入 + node = getLeft(); + if (null == node) { + // 左节点为空,则直接插入 + parent.setLeft(node); + } else { + // 递归检查左子树的插入位置 + insert(node, child); + } + } + } + + public interface Compare { + boolean isLargeThanTarget(Compare target); + } + +} diff --git a/group14/254659936/src/com/coding/basic/Iterator.java b/group14/254659936/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..160f044ad1 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group14/254659936/src/com/coding/basic/LinkedList.java b/group14/254659936/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e0ad67a6d8 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/LinkedList.java @@ -0,0 +1,155 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + Node node = new Node(); + node.data = 0; + if (index == 0) { + node.next = head; + head = node; + } + Node next = head; + int i = 0; + while (null != next) { + i++; + if (index == i) { + node.next = next.next; + next.next = node; + break; + } + next.next = next.next; + } + size++; + } + + public Object get(int index) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + Node next = head; + int i = 0; + while (null != next) { + if (i == index) { + return next; + } + next = next.next; + i++; + } + return null; + } + + public Object remove(int index) { + if (index >= size) { + throw new RuntimeException("the LinkedList size is short than index"); + } + size--; + Node resultNode = null; + if (index == 0) { + resultNode = head; + head = head.next; + return resultNode.data; + } + + Node next = head; + int i = 0; + while (null != next) { + if (i == index) { + resultNode = next.next; + next.next = resultNode.next; + } + } + return resultNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + size++; + Node node = new Node(); + node.data = 0; + node.next = head; + head = node; + } + + public void addLast(Object o) { + size++; + Node node = new Node(); + node.data = o; + if (null == head) { + head = node; + return; + } + Node next = head; + while (null != next.next) { + next = next.next; + } + next.next = node; + } + + public Object removeFirst() { + if (size == 0) { + throw new RuntimeException("the LinkedList is null"); + } + size--; + Object obj = head.data; + head = head.next; + return obj; + } + + public Object removeLast() { + if (size == 0) { + throw new RuntimeException("the LinkedList is null"); + } + Node next = head; + Object obj = null; + for (int i = 0; i < size - 1; i++) { + next = next.next; + } + next.next = null; + size--; + return obj; + } + + public Iterator iterator() { + return null; + } + + + private static class Node { + Object data; + Node next; + } + + private class LinkedListIterator implements Iterator { + + private Node next = head; + + @Override + public boolean hasNext() { + return null != next; + } + + @Override + public Object next() { + if (null == next) { + throw new RuntimeException("the LinkedList is out of index"); + } + Object obj = next.data; + next = next.next; + return obj; + } + } +} diff --git a/group14/254659936/src/com/coding/basic/List.java b/group14/254659936/src/com/coding/basic/List.java new file mode 100644 index 0000000000..01398944e6 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group14/254659936/src/com/coding/basic/Queue.java b/group14/254659936/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..50f99e6d1c --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Queue.java @@ -0,0 +1,45 @@ +package com.coding.basic; + +public class Queue { + + private Node head; + private Node tail; + private int size; + + public void enQueue(Object o) { + Node node = new Node(); + node.data = o; + if (null == head) { + head = node; + tail = node; + } else { + tail.next = node; + tail = tail.next; + } + size++; + } + + public Object deQueue() { + if (size <= 0) { + throw new RuntimeException("the queue is empty"); + } + Object obj = head.data; + head = head.next; + size--; + return obj; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + Node pre; + } +} diff --git a/group14/254659936/src/com/coding/basic/Stack.java b/group14/254659936/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..796a3e2e24 --- /dev/null +++ b/group14/254659936/src/com/coding/basic/Stack.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +public class Stack { + + private Node mStackNode; + private int size; + + public void push(Object o) { + Node node = new Node(); + node.data = o; + if (null == mStackNode) { + mStackNode = node; + } else { + mStackNode.next = node; + mStackNode = node; + } + size++; + } + + public Object pop() { + if (size == 0) { + throw new RuntimeException("the stack is empty"); + } + Object obj = mStackNode.data; + mStackNode = mStackNode.pre; + size--; + return obj; + } + + public Object peek() { + if (size == 0) { + throw new RuntimeException("the stack is empty"); + } + return mStackNode.data; + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } + + private static class Node { + Object data; + Node next; + Node pre; + } +} diff --git a/group14/296933284/DataStructuresTest/.classpath b/group14/296933284/DataStructuresTest/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group14/296933284/DataStructuresTest/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group14/296933284/DataStructuresTest/.gitignore b/group14/296933284/DataStructuresTest/.gitignore new file mode 100644 index 0000000000..e32d56a40c --- /dev/null +++ b/group14/296933284/DataStructuresTest/.gitignore @@ -0,0 +1,14 @@ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ +/bin/ diff --git a/group14/296933284/DataStructuresTest/.project b/group14/296933284/DataStructuresTest/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group14/296933284/DataStructuresTest/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs b/group14/296933284/DataStructuresTest/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group14/296933284/DataStructuresTest/.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/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..116020466d --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/ArrayList.java @@ -0,0 +1,147 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * ArrayList ʵ 14С 296933284 + * + * @author Tonnyson + * + */ +public class ArrayList implements List { + + private int size; + private static final int DEFAULT_CAPACITY = 10; + private Object[] elementData; + + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initCapacity) { + elementData = new Object[initCapacity]; + } + + /** + * ĩβָԪأԶչΪԭȵ + */ + public void add(Object obj) { + + ensureCapacityInternal(size); + + elementData[size] = obj; + size++; + } + + + /** + * ָλòԪ + */ + public void add(int index, Object obj) { + + rangCheckForAdd(index); + ensureCapacityInternal(size + 1); + + for (int i = size - 1; i >= index; i--) + elementData[i + 1] = elementData[i]; + + elementData[index] = obj; + size++; + } + + /** + * + */ + private void ensureCapacityInternal(int minCapacity) { + if (minCapacity - elementData.length > 0) { + int newCapacity = elementData.length * 2; + elementData = Arrays.copyOf(elementData, newCapacity); + // elementData = tempElementData; + } + } + + /** + * add() м±ǷԽ + */ + private void rangCheckForAdd(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException(); + } + + /** + * ָλõԪֵ + */ + public Object get(int index) { + + rangCheck(index); + + return elementData[index]; + } + + /** + * ɾָλõԪأظֵ + */ + public Object remove(int index) { + rangCheck(index); + + Object obj = elementData[index]; + + for (int i = index; i < size; i++) + elementData[i] = elementData[i + 1]; + + size--; + + return obj; + } + + /** + * ±ǷԽ + * + * @param index + */ + private void rangCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(); + } + + /** + * 鳤 + */ + public int size() { + return size; + } + + /** + * + * + * @return + */ + public Iterator iterator() { + return new Iter(); + } + + //ڲ + private class Iter implements Iterator { + int current; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + + int i = current; + rangCheck(i); + current++; + + return elementData[i]; + } + + } + +} + + diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java new file mode 100644 index 0000000000..b74dbe85a2 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java @@ -0,0 +1,154 @@ +package com.coding.basic; + +/** + * BST ʵ 14С 296933284 + * + * @author Tonnyson + * + */ +public class BinarySearchTree implements Comparable { + + private Object data; + private BinarySearchTree leftChild; + private BinarySearchTree rightChild; + + public BinarySearchTree() { + super(); + this.data = null; + this.leftChild = null; + this.rightChild = null; + } + + public BinarySearchTree(Object data) { + this(); + this.data = data; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinarySearchTree getLeftChild() { + return leftChild; + } + + public void setLeftChild(BinarySearchTree leftChild) { + this.leftChild = leftChild; + } + + public BinarySearchTree getRightChild() { + return rightChild; + } + + public void setRightChild(BinarySearchTree rightChild) { + this.rightChild = rightChild; + } + + /** + * вڵ + * + * @param obj + * ڵֵ + */ + public void insert(Object obj) { + insert(obj, this); + } + + private boolean insert(Object obj, BinarySearchTree node) { + + BinarySearchTree bstNode = new BinarySearchTree(obj); + + if (node == null) { + node = bstNode; + return true; + } else if (node.compareTo(obj) == 0) { + return true; + } else if (node.compareTo(obj) > 0) { + + if (node.getLeftChild() != null) { + return insert(obj, node.getLeftChild()); + } + + node.leftChild = bstNode; + + } else if (node.compareTo(obj) < 0) { + + if (node.getRightChild() != null) { + return insert(obj, node.getRightChild()); + } + + node.rightChild = bstNode; + } + + return false; + + } + + /** + * BST Ľڵ㣬ʹ֮ + */ + public void inOrder(BinarySearchTree node) { + + if (node != null) { + inOrder(node.getLeftChild()); + visit(node); + inOrder(node.getRightChild()); + } + + } + + /** + * BST Ľڵֵ + */ + public void levelOrder(BinarySearchTree node) { + Queue queue = new Queue(); + BinarySearchTree bstNode = null; + queue.enQueue(node); + + while (!queue.isEmpty()) { + bstNode = (BinarySearchTree) queue.deQueue(); + visit(bstNode); + + if (bstNode.getLeftChild() != null) { + queue.enQueue(bstNode.getLeftChild()); + } + + if (bstNode.getRightChild() != null) { + queue.enQueue(bstNode.getRightChild()); + } + } + } + + /** + * ָڵֵ + * + * @param node + */ + public void visit(BinarySearchTree node) { + System.out.println(node.getData()); + } + + /** + * Ƚ BST ڵֵС + */ + @Override + public int compareTo(Object obj) { + int result = 0; + + if (obj instanceof Integer) { + Integer value = (Integer) obj; + Integer thisValue = (Integer) this.data; + result = thisValue.compareTo(value); + } else { + String value = obj.toString(); + result = this.data.toString().compareTo(value); + } + + return result; + } + +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java new file mode 100644 index 0000000000..8c4cab01b6 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java @@ -0,0 +1,176 @@ +package com.coding.basic; + +import org.junit.Test; + +public class JavaTest { + + @Test + public void binarySearchTreeTest() { + BinarySearchTree bSTree = new BinarySearchTree(5); + + System.out.println(bSTree.getData()); + + // insert + bSTree.insert(1); + bSTree.insert(2); + bSTree.insert(4); + bSTree.insert(6); + bSTree.insert(7); + bSTree.insert(8); + System.out.println("-----------------"); + // inOrder + bSTree.inOrder(bSTree); + + System.out.println("-----------------"); + // levelOrder + bSTree.levelOrder(bSTree); + } + + @Test + public void queueTest() { + Queue queue = new Queue(); + + // enQueue() + for (int i = 0; i < 10; i++) { + queue.enQueue("hello: " + i); + } + + // size() + System.out.println(queue.size()); // 10 + // isEmpty + System.out.println(queue.isEmpty()); + + // deQueue() + for (int i = 0; i < 10; i++) { + System.out.println(queue.deQueue()); + } + + // size() + System.out.println(queue.size()); // 0 + + // isEmpty + System.out.println(queue.isEmpty()); + } + + @Test + public void stackTest() { + Stack stack = new Stack(); + + // push() + for (int i = 0; i < 10; i++) { + stack.push("hello: " + i); + } + + // size() + System.out.println(stack.size()); + + // pop() + for (int i = 0; i < 10; i++) { + System.out.println(stack.pop()); + } + + // isEmpty() + System.out.println(stack.isEmpty()); + System.out.println(stack.size()); + + stack.push("hello world 1"); + stack.push("hello world 2"); + stack.push("hello world 3"); + stack.push("hello world 4"); + + // peek() + System.out.println(stack.peek()); + + // isEmpty() + System.out.println(stack.isEmpty()); + } + + @Test + public void linkedListTest() { + LinkedList linkedList = new LinkedList(); + + // add() addLast() + for (int i = 0; i < 5; i++) { + linkedList.add("hello: " + i); + } + + // iterator() get() getPreNode() + Iterator iter = linkedList.iterator(); + + while (iter.hasNext()) { + System.out.println(iter.next()); + } + System.out.println("-----------------------"); + LinkedList linkedList1 = new LinkedList(); + + // addFirst() + for (int i = 0; i < 5; i++) { + linkedList1.addFirst("hello: " + i); + } + + Iterator iter1 = linkedList1.iterator(); + + while (iter1.hasNext()) { + System.out.println(iter1.next()); + } + + System.out.println("-----------------------"); + // remove() + System.out.println(linkedList1.remove(0)); // hello: 4 + + System.out.println("-----------------------"); + // removeFirst() removeLast() + System.out.println(linkedList1.removeFirst()); // hello: 3 + System.out.println(linkedList1.removeLast()); // hello: 0 + + System.out.println("-----------------------"); + // size() + System.out.println(linkedList.size()); // 5 + } + + @Test + public void arrayListTest() { + ArrayList arrayList = new ArrayList(); + + // add(obj) + for (int i = 0; i < 10; i++) { + arrayList.add("hello: " + i); + } + + // get(index) + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("-->" + arrayList.get(i)); + } + + // add(index, obj) + arrayList.add(5, "Tennyson"); + + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("++>" + arrayList.get(i)); + } + + // size() + System.out.println("size: " + arrayList.size()); + System.out.println("index 5: " + arrayList.get(5)); + + // remove() + Object value = arrayList.remove(5); + System.out.println("index 5: " + value); + System.out.println("size: " + arrayList.size()); + System.out.println("index5: " + arrayList.get(5)); + + for (int i = 0; i < arrayList.size(); i++) { + System.out.println("index " + i + " : " + arrayList.get(i)); + } + + System.out.println("-------------------------------"); + // iterator + Iterator iter = arrayList.iterator(); + while (iter.hasNext()) { + System.out.println(iter.next()); + } + + System.out.println("-------------------------------"); + + } +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..2a397e9bec --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java @@ -0,0 +1,214 @@ +package com.coding.basic; + +import java.util.Collection; + +import org.junit.Test; + +/** + * LinkedList (ͷĵ) ʵ 14С 296933284 + * + * @author Tonnyson + * + */ +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + super(); + this.head = new Node(); + this.size = 0; + } + + public void add(Object obj) { + addLast(obj); + } + + public void add(int index, Object obj) { + + if (index == size + 1) { + addLast(obj); + } else { + Node r = getPreNode(index); + Node node = new Node(); + node.data = obj; + node.next = r.next; + r.next = node; + size++; + } + } + + + + /** + * ײڵ + * + * @param obj ڵĽڵֵ + * + */ + public void addFirst(Object obj) { + Node node = new Node(); + node.data = obj; + + node.next = head.next; + head.next = node; + size++; + } + + /** + * βڵ + * + * @param obj ڵĽڵֵ + * + */ + public void addLast(Object obj) { + + Node node = new Node(); + node.data = obj; + node.next = null; + + Node r = head; + while (r.next != null) r = r.next; + + r.next = node; + + size++; + + } + + /** + * Ԫذ˳뵥 + * + * @param c Ҫ뵥Ԫصļ + * + */ + public void addAll(Collection c) { + + Iterator iter = (Iterator) c.iterator(); + + while (iter.hasNext()) { + addLast(iter.next()); + } + } + + /** + * ȡָλõĽڵֵ + */ + public Object get(int index) { + // rangCheck(index); + + return getPreNode(index).next.data; + } + + /** + * ɾָλýڵ㣬ؽڵֵ + */ + public Object remove(int index) { + rangCheck(index); + + Node r = getPreNode(index); + + Object result = r.next.data; + + r.next = r.next.next; + size--; + return result; + } + + /** + * ɾһڵ㣬ؽڵֵ + * + * @return һڵֵ + */ + public Object removeFirst() { + return remove(0); + } + + /** + * ɾһڵ㣬ؽڵֵ + * + * @return һڵֵ + */ + public Object removeLast() { + return remove(size - 1); + } + + // ȡָλõǰ㲢 + private Node getPreNode(int index) { + rangCheck(index); + + if (index == 0) { + return head; + } else { + Node r = head; + + for (int i = 0; i < index; i++) + r = r.next; + + return r; + } + + } + + /** + * صij + */ + public int size() { + return size; + } + + /** + * + * + * @return һ + */ + public Iterator iterator() { + return new Iter(); + } + + // ڲ + private class Iter implements Iterator { + int current = 0; + + @Override + public boolean hasNext() { + return current != size; + } + + @Override + public Object next() { + int i = current; + + rangCheck(i); + + current++; + + return get(i); + } + + } + + /** + * ǷԽ + * + * @param index + */ + private void rangCheck(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException(); + } + + private static class Node { + Object data; + Node next; + + public Node() { + super(); + this.data = null; + this.next = null; + } + + + } +} \ No newline at end of file diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java new file mode 100644 index 0000000000..969e6dd82b --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object obj); + public void add(int index, Object obj); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..9257eb04ca --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Queue.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +/** + * Queue ʵ + * First In First Out + * 14С 296933284 + * + * @author Tonnyson + * + */ +public class Queue { + + private LinkedList elementData = new LinkedList(); + + /** + * вԪ + * + * @param obj + */ + public void enQueue(Object obj){ + elementData.addLast(obj); + } + + /** + * ɾԪ + * + * @return + */ + public Object deQueue(){ + return elementData.removeFirst(); + } + + /** + * ж϶ǷΪ + * + * @return + */ + public boolean isEmpty(){ + return elementData.size() == 0; + } + + /** + * ضеԪظ + * + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..e28a9e3760 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/Stack.java @@ -0,0 +1,61 @@ +package com.coding.basic; + +/** + * Stack ʵ + * Last In First Out + * 14С 296933284 + * + * @author Tonnyson + * + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + private int top = 0; + + /** + * ջвԪ + * + * @param obj + */ + public void push(Object obj) { + elementData.add(obj); + top++; + } + + /** + * ջȡԪ + * + * @return + */ + public Object pop() { + return elementData.remove(--top); + } + + /** + * ȡջԪ + * + * @return + */ + public Object peek() { + return elementData.get(top - 1); + } + + /** + * жջǷΪ + * + * @return + */ + public boolean isEmpty() { + return top == 0; + } + + /** + * ȡջԪظ + * + * @return + */ + public int size() { + return top; + } +} diff --git a/group14/296933284/Note/README.md b/group14/296933284/Note/README.md new file mode 100644 index 0000000000..3b92cb5667 --- /dev/null +++ b/group14/296933284/Note/README.md @@ -0,0 +1,4 @@ +# 2017编程提高(Java)学习系列笔记链接 +--- + +1. [漫谈计算机组成 -- 微型计算机的硬件组成 2017-02-26](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) \ No newline at end of file diff --git a/group14/296933284/Note/test.txt b/group14/296933284/Note/test.txt new file mode 100644 index 0000000000..95d09f2b10 --- /dev/null +++ b/group14/296933284/Note/test.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/group14/296933284/README.md b/group14/296933284/README.md new file mode 100644 index 0000000000..023abf1e46 --- /dev/null +++ b/group14/296933284/README.md @@ -0,0 +1,4 @@ +# 2017年编程提高(Java) 作业、练习、总结 + +[DataStructuresTest --- 基本数据结构Java实现](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/DataStructuresTest) +[Note --- 2017编程提高(Java)学习系列笔记链接](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/Note) diff --git a/group14/352504906/test/.classpath b/group14/352504906/test/.classpath new file mode 100644 index 0000000000..18d70f02cb --- /dev/null +++ b/group14/352504906/test/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group14/352504906/test/.project b/group14/352504906/test/.project new file mode 100644 index 0000000000..b0299dbe76 --- /dev/null +++ b/group14/352504906/test/.project @@ -0,0 +1,17 @@ + + + test + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group14/352504906/test/.settings/org.eclipse.core.resources.prefs b/group14/352504906/test/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..e2d1bdfc9f --- /dev/null +++ b/group14/352504906/test/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +#Sun Feb 26 15:56:36 CST 2017 +eclipse.preferences.version=1 +encoding//src/com/coding/basic=UTF-8 +encoding//src/com/coding/basic/SimpleArrayList.java=UTF-8 +encoding/=UTF-8 +encoding/src=UTF-8 diff --git a/group14/352504906/test/.settings/org.eclipse.jdt.core.prefs b/group14/352504906/test/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..06bffba000 --- /dev/null +++ b/group14/352504906/test/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Sun Feb 26 15:31:53 CST 2017 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +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.6 diff --git a/group14/352504906/test/src/com/coding/basic/SimpleArrayList.java b/group14/352504906/test/src/com/coding/basic/SimpleArrayList.java new file mode 100644 index 0000000000..931d0af3aa --- /dev/null +++ b/group14/352504906/test/src/com/coding/basic/SimpleArrayList.java @@ -0,0 +1,118 @@ +package com.coding.basic; + +import java.util.Arrays; + + +/** + * @Description 简单实现ArrayList + */ +public class SimpleArrayList implements SimpleList{ + private int size = 0; + private Object[] elementData = new Object[10]; + /** + * 插入元素o + * @param o 待插入元素 + */ + public void add(Object o){ + //扩容 + if(elementData.length < size + 1){ + grow(size+1); + } + elementData[size++] = o; + } + /** + * 数组扩容 + * @param capacity 数组实际长度 + */ + private void grow(int capacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1);//扩容50% + if(capacity > newCapacity){ + newCapacity = capacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + /** + * 在指定索引初插入元素 + * @param index 索引 + * @param o 待插入元素 + */ + public void add(int index,Object o){ + rangeCheckForAdd(index); + if(elementData.length size || index <0){ + throw new IndexOutOfBoundsException("数组越界异常"); + } + } + /** + * 索引越界处理 + * @param index 索引 + */ + private void rangeCheck(int index) { + if(index >= size || index <0) + throw new IndexOutOfBoundsException("数组越界异常"); + } + /** + * 移除该索引处元素 + * @param index 索引位置 + * @return 移除元素 + */ + public Object remove(int index){ + rangeCheck(index); + Object oldObject = elementData[index]; + if(size > index +1){ + System.arraycopy(elementData, index +1 , elementData, index, size-index-1); + } + elementData[--size] = null; + return oldObject; + } + /** + * 返回集合长度 + * @return 长度 + */ + public int size(){ + return this.size; + } + /** + * 返回指定索引元素 + * @param index 索引 + * @return Object 元素 + */ + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + private class ArrayListIterator implements SimpleIterator{ + SimpleArrayList l = null; + private int iteratorIndex = 0; + + private ArrayListIterator(SimpleArrayList arrayList){ + this.l = arrayList; + } + + @Override + public boolean hasNext() { + return iteratorIndex < size; + } + + @Override + public Object next() { + if (iteratorIndex >= size) { + throw new RuntimeException("数组越界异常"); + } + return elementData[iteratorIndex++]; + } + + } +} diff --git a/group14/352504906/test/src/com/coding/basic/SimpleIterator.java b/group14/352504906/test/src/com/coding/basic/SimpleIterator.java new file mode 100644 index 0000000000..fd8db0aa8f --- /dev/null +++ b/group14/352504906/test/src/com/coding/basic/SimpleIterator.java @@ -0,0 +1,11 @@ +package com.coding.basic; + +/** + *简单迭代器接口 + * + */ +public interface SimpleIterator { + public Object next(); + public boolean hasNext(); + +} diff --git a/group14/352504906/test/src/com/coding/basic/SimpleLinkedList.java b/group14/352504906/test/src/com/coding/basic/SimpleLinkedList.java new file mode 100644 index 0000000000..692fd23f62 --- /dev/null +++ b/group14/352504906/test/src/com/coding/basic/SimpleLinkedList.java @@ -0,0 +1,126 @@ +package com.coding.basic; +/** + * @Description 简单实现linkedList + */ +public class SimpleLinkedList implements SimpleList{ + private Node head; + + private int size = 0; + /** + * 返回集合长度 + * @return 长度 + */ + public int size(){ + return this.size; + } + /** + * 返回指定索引出元素 + * @param index 索引 + * @return Object 元素 + */ + public Object get(int index){ + rangeCheck(index); + Node current = head; + for(int i =0;i size || index < 0){ + throw new RuntimeException("找不到该节点"); + } + } + //头插法 + public void addFirst(Object o){ + Node newNode = new Node(o); + newNode.next = head; + head = newNode; + size++; + } + //尾插法 + public void addLast(Object o){ + Node newNode = new Node(o); + Node current = head;//设定一个当前节点,便于遍历 + while(current.next!=null){ + current = current.next; + } + if(current.next == null)//尾节点 + { + current.next = newNode; + size++; + } + } + /** + * 移除该索引处元素 + * @param index 索引位置 + * @return 移除元素 + */ + public Object remove(int index){ + rangeCheck(index+1); + Node current = head; + if(index == 0){//头部删除 + Object removeObj = head.o; + head = head.next; + size --; + return removeObj; + } + int i; + for(i=0;i newCapacity){ + newCapacity = capacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + /** + * 移除并返回队列头部元素 + * @return 队列头部元素 + */ + public Object deQueue(){ + Object o = elementData[size-1]; + removeElement(0); + return o; + } + /** + * 删除指定索引处元素 + * @param index 索引 + */ + private void removeElement(int index) { + if(index >= 0){ + System.arraycopy(elementData, index+1, elementData, 0, size-index-1); + elementData[--size] = null; + } + } + /** + * 判断队列是否为空 + * @return Boolean + */ + public boolean isEmpty(){ + return size==0; + } + /** + * 返回队列长度 + * @return int 队列长度 + */ + public int size(){ + return this.size; + } +} diff --git a/group14/352504906/test/src/com/coding/basic/SimpleStack.java b/group14/352504906/test/src/com/coding/basic/SimpleStack.java new file mode 100644 index 0000000000..66d5e7dbe1 --- /dev/null +++ b/group14/352504906/test/src/com/coding/basic/SimpleStack.java @@ -0,0 +1,76 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.EmptyStackException; + +/** + * @Description 简单实现stack + */ +public class SimpleStack { + private Object[] elementData = new Object[10]; + private int size; + /** + * 往栈顶添加新的元素 + * @param o 要添加的元素 + */ + public void push(Object o){ + //扩容 + if(elementData.length < size +1){ + grow(size+1); + } + elementData[size++] = o; + } + /** + * 数组扩容 + * @param capacity 数组实际长度 + */ + private void grow(int capacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity *2;//扩容2倍 + if(newCapacity < capacity){ + newCapacity = capacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + /** + * 移除并返回栈顶元素 + * @return Object 返回该移除的元素 + */ + public Object pop(){ + Object o = peek(); + removeElement(size-1); + return o; + } + /** + * 移除栈顶元素 + * @param length 栈的长度 + */ + private void removeElement(int length) { + elementData = Arrays.copyOf(elementData, length); + size --; + } + /** + * 返回栈顶元素 + * @return Object 栈顶元素 + */ + public Object peek(){ + if(size == 0){ + throw new EmptyStackException(); + } + return elementData[size-1]; + } + /** + * 查询并返回栈的长度 + * @return int 栈的长度 + */ + public int size(){ + return this.size; + } + /** + * 判断是否为空栈 + * @return boolean + */ + public boolean isEmpty(){ + return size==0; + } +} diff --git "a/group14/352504906/\346\265\205\350\260\210CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.txt" "b/group14/352504906/\346\265\205\350\260\210CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.txt" new file mode 100644 index 0000000000..d269d4a4ab --- /dev/null +++ "b/group14/352504906/\346\265\205\350\260\210CPU\343\200\201\345\206\205\345\255\230\343\200\201\347\241\254\347\233\230\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.txt" @@ -0,0 +1 @@ +͵ַhttp://www.cnblogs.com/superFish2016/articles/cpu.html \ No newline at end of file diff --git a/group14/598808350/20170219.txt b/group14/598808350/20170219.txt new file mode 100644 index 0000000000..2570935d90 --- /dev/null +++ b/group14/598808350/20170219.txt @@ -0,0 +1,5 @@ + CPUڴ棬 Ӳָ̣֮Ĺϵ + +http://blog.sina.com.cn/s/blog_986d02cd0102xncn.html + +QQ:598808350 \ No newline at end of file diff --git a/group14/598808350/2017project/src/org/learning/container/ArrayList.java b/group14/598808350/2017project/src/org/learning/container/ArrayList.java new file mode 100644 index 0000000000..5755c7cf3a --- /dev/null +++ b/group14/598808350/2017project/src/org/learning/container/ArrayList.java @@ -0,0 +1,186 @@ +package org.learning.container; + + +public class ArrayList { + + private Object [] objs = null; + private int index = -1; + public ArrayList(){ + objs = new Object[5]; + } + public ArrayList(int size){ + objs = new Object[size]; + } + /** + * һµ + * @param src + * @param src_index + * @param dest + * @param dest_index + * @param length + * @return + */ + private static Object[] copy(Object[] src,int src_index,Object[] dest,int dest_index,int length){ + System.arraycopy(src, src_index, dest, dest_index, length); + return dest; + } + + public void add(Object obj){ + if(this.index == objs.length-1) { + Object[] dest = new Object[objs.length+5]; + objs = copy(objs,0,dest,0,objs.length); + } + this.index ++; + objs[this.index] = obj; + } + + public void add(int index,Object obj){ + if(index-1 > this.index || index < 0){ + throw new IndexOutOfBoundsException(); + } + Object[] dest = new Object[objs.length+5]; + if(index == 0){ + dest[index] = obj; + dest =copy(objs,index,dest,index+1,getSize()); + objs = dest; + }else if(index == getSize()){ + objs[index] = obj; + }else{ + dest = copy(objs,0,dest,0,index);//ǰ + dest[index] = obj; //м䲿 + dest =copy(objs,index,dest,index+1,getSize()-index);//󲿷 + objs = dest; + } + this.index++; + } + + public Object get(int index){ + if(index > this.index || index <0){ + throw new IndexOutOfBoundsException(); + } + return objs[index]; + } + + public boolean isEmpty(){ + if(objs == null || this.index == -1){ + return true; + } + return false; + } + + public int getSize(){ + return this.index+1; + } + + public boolean remove(int index){ + if (index <0 || index > objs.length){ + throw new IndexOutOfBoundsException(); + } + Object[] dest = new Object[this.index]; + dest = copy(objs,0,dest,0,index);//ǰ + dest = copy(objs,index+1,dest,index,this.index-index);//󲿷 + objs = dest; + this.index --; + return true; + } + public boolean remove(Object obj){ + for(int i=0;i<=this.index;i++){ + if(obj==null ? get(i)==null : obj.equals(get(i))) { + remove(i); //i ǰԪص±ʶ + return true; + } + } + return false; + } + public static void print(Object obj){ + System.out.println(obj); + } + + public static void main(String [] args){ + ArrayList al = new ArrayList(); + /*print(al.isEmpty()); + al.add("a1"); + print(al.isEmpty()); + print(al.getSize()); + print(al.get(0)); + print(al.get(1));*/ + al.add("a0"); + al.add("a1"); + al.add("a2"); + al.add("a3"); + al.add("a4"); + al.add("a5"); + + //al.remove(0); + //al.remove(5); + //al.remove(2); + /*boolean flag = al.remove("a7"); + print(flag); + for(int i=0;i + + + + + + diff --git a/group14/630254746/2017Leaning/.gitignore b/group14/630254746/2017Leaning/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group14/630254746/2017Leaning/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group14/630254746/2017Leaning/.project b/group14/630254746/2017Leaning/.project new file mode 100644 index 0000000000..af4136644f --- /dev/null +++ b/group14/630254746/2017Leaning/.project @@ -0,0 +1,17 @@ + + + 2017Leaning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group14/630254746/2017Leaning/.settings/org.eclipse.jdt.core.prefs b/group14/630254746/2017Leaning/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group14/630254746/2017Leaning/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group14/630254746/2017Leaning/src/com/leaning/code/ArrayList.java b/group14/630254746/2017Leaning/src/com/leaning/code/ArrayList.java new file mode 100644 index 0000000000..980736245e --- /dev/null +++ b/group14/630254746/2017Leaning/src/com/leaning/code/ArrayList.java @@ -0,0 +1,87 @@ +package com.leaning.code; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size; // ¼еԪظ + + private Object[] elementsData; + + private int totalCount = 1; // ¼ϵĴС + + public ArrayList() { + this.elementsData = new Object[totalCount]; + } + + private void grow() { + if (size >= totalCount) { + // + int oldCapacity = size; + int newCapacity = oldCapacity + oldCapacity << 1; + totalCount = newCapacity; + elementsData = Arrays.copyOf(elementsData, newCapacity); + } + } + + @Override + public void add(Object o) { + if (totalCount > size) { + elementsData[size++] = o; + } else { + grow(); + elementsData[size++] = o; + } + } + + @Override + public void add(int index, Object o) { + if (index < size) { + if (totalCount <= size + 1) { + grow(); + } + System.arraycopy(elementsData, index, elementsData, index + 1, size + - index); + elementsData[index] = 0; + + } else { + throw new RuntimeException("±Խ"); + } + size++; + } + + @Override + public Object get(int index) { + if (index < size) + return elementsData[index]; + else + throw new RuntimeException("±Խ"); + } + + @Override + public Object remove(int index) { + if (index >= size || index < 0) { + throw new RuntimeException("±Խ"); + } + Object o = elementsData[index]; + + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementsData, index + 1, elementsData, index, + numMoved); + elementsData[--size] = null; + + return o; + } + + @Override + public int size() { + return size; + } + + @Override + public String toString() { + return "ArrayList [elementsData=" + Arrays.toString(elementsData) + "]"; + } + +} diff --git a/group14/630254746/2017Leaning/src/com/leaning/code/Iterator.java b/group14/630254746/2017Leaning/src/com/leaning/code/Iterator.java new file mode 100644 index 0000000000..b9ae65aab1 --- /dev/null +++ b/group14/630254746/2017Leaning/src/com/leaning/code/Iterator.java @@ -0,0 +1,8 @@ +package com.leaning.code; + +public interface Iterator { + + public boolean hasNext(); + + public Object Next(); +} diff --git a/group14/630254746/2017Leaning/src/com/leaning/code/LinkedList.java b/group14/630254746/2017Leaning/src/com/leaning/code/LinkedList.java new file mode 100644 index 0000000000..fd4c57a924 --- /dev/null +++ b/group14/630254746/2017Leaning/src/com/leaning/code/LinkedList.java @@ -0,0 +1,150 @@ +package com.leaning.code; + + + + + + +public class LinkedList implements List { + + private Node head; + + private Node last; + + private int size; + + + void linkLast(Object o){ + Node lastNode = last; + Node newNode = new Node(lastNode, o, null); + last = newNode; + if (lastNode == null) + head = newNode; + else + lastNode.next = newNode; + size++; + } + + void linkHead(Object o){ + Node headNode = head; + Node newNode = new Node(null, o, headNode); + head = newNode; + if (head == null) + last = newNode; + else + head.prev = newNode; + size++; + } + + @Override + public void add(Object o) { + linkLast(o); + } + + @Override + public void add(int index, Object o) { + if (index < 0 || index >= size) { + throw new RuntimeException("±Խ"); + } + Node n = find(index); + Node pred = n.prev; + Node newNode = new Node(pred, o, n); + n.prev = newNode; + if (pred == null) + head = newNode; + else + pred.next = newNode; + size++; + } + + @Override + public Object get(int index) { + return find(index).item; + } + + Node find(int index){ + if (index < (size >> 1)) { + Node n = head; + for (int i = 0; i < index; i++) + n = n.next; + return n; + } else { + Node n = last; + for (int i = size - 1; i > index; i--) + n = n.prev; + return n; + } + } + + @Override + public Object remove(int index) { + Node n = find(index); + Object o = n.item; + final Node prev = n.prev; + final Node next = n.next; + if (null != prev) { + prev.next = next; + } + if (null != next) { + next.prev = prev; + } + n.item = null; + n.next = null; + n.prev = null; + size-- ; + return o; + } + + @Override + public int size() { + return size; + } + + public void addFrist(Object o){ + linkHead(o); + } + + public void addLast(Object o){ + linkLast(o); + } + + public Object removeFirst(){ + Object o = head.item; + Node n = head.next; + head = n; + if (n == null) + last = null; + else + n.prev = null; + size --; + return o; + } + + public Object removeLaset(){ + Object o = last.item; + Node p = last.prev; + last = p; + if (p == null) + head = null; + else + p.next = null; + size --; + return o; + } + + public Iterator iterator(){ + return null; + } + + public static class Node{ + Object item; + Node next; + Node prev; + + Node(Node prev, Object element, Node next) { + this.item = element; + this.next = next; + this.prev = prev; + } + } +} diff --git a/group14/630254746/2017Leaning/src/com/leaning/code/List.java b/group14/630254746/2017Leaning/src/com/leaning/code/List.java new file mode 100644 index 0000000000..22d039c4a5 --- /dev/null +++ b/group14/630254746/2017Leaning/src/com/leaning/code/List.java @@ -0,0 +1,15 @@ +package com.leaning.code; + +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/group14/630254746/2017Leaning/src/com/leaning/code/Queue.java b/group14/630254746/2017Leaning/src/com/leaning/code/Queue.java new file mode 100644 index 0000000000..f1272b29cc --- /dev/null +++ b/group14/630254746/2017Leaning/src/com/leaning/code/Queue.java @@ -0,0 +1,22 @@ +package com.leaning.code; + +public class Queue { + + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) { + list.add(o); + } + + public Object deQueue() { + return list.removeFirst(); + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group14/630254746/2017Leaning/src/com/leaning/code/Stack.java b/group14/630254746/2017Leaning/src/com/leaning/code/Stack.java new file mode 100644 index 0000000000..c988f489bb --- /dev/null +++ b/group14/630254746/2017Leaning/src/com/leaning/code/Stack.java @@ -0,0 +1,31 @@ +package com.leaning.code; + +public class Stack { + + private ArrayList list = new ArrayList(); + + private int size; + + + public void push(Object o){ + list.add(o); + size ++; + } + + public Object pop(){ + return list.get(--size); + } + + public Object peek(){ + return list.get(size-1); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } + +} diff --git a/group14/630254746/2017Leaning/src/com/leaning/code/test/ArrayListTest.java b/group14/630254746/2017Leaning/src/com/leaning/code/test/ArrayListTest.java new file mode 100644 index 0000000000..c680d5f8a5 --- /dev/null +++ b/group14/630254746/2017Leaning/src/com/leaning/code/test/ArrayListTest.java @@ -0,0 +1,37 @@ +package com.leaning.code.test; + +import org.junit.Test; + +import com.leaning.code.ArrayList; +import com.leaning.code.LinkedList; + +public class ArrayListTest { + + @Test + public void test01(){ + ArrayList list = new ArrayList(); + list.add("a"); + list.add("b"); + list.add("c"); + + System.out.println(list.remove(0)); + System.out.println(list); + + } + + @Test + public void test02(){ + LinkedList list = new LinkedList(); + list.add("a"); + list.add("b"); + list.add("c"); + + list.add(2, "d"); + + System.out.println(list.remove(0)); + System.out.println(list.get(0)); + System.out.println(list.get(2)); + + + } +} diff --git a/group14/676615845/algo/pom.xml b/group14/676615845/algo/pom.xml new file mode 100644 index 0000000000..d2e1f258a0 --- /dev/null +++ b/group14/676615845/algo/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.mark + algo + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + + + \ No newline at end of file diff --git a/group14/676615845/algo/src/main/java/algo/BinarySearch.java b/group14/676615845/algo/src/main/java/algo/BinarySearch.java new file mode 100644 index 0000000000..3144a37181 --- /dev/null +++ b/group14/676615845/algo/src/main/java/algo/BinarySearch.java @@ -0,0 +1,17 @@ +package algo; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * Created by mark on 17/2/23. + */ +public class BinarySearch { + + public static int rank(int key, int[] a) { + List list = new ArrayList(); + list = new LinkedList(); + return -1; + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Array.java b/group14/676615845/algo/src/main/java/com/coding/basic/Array.java new file mode 100644 index 0000000000..44afce6c25 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Array.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * Created by mark on 17/2/24. + */ +public class Array { + + public static Object[] grow(Object[] src, int size) { + return Arrays.copyOf(src, src.length + size); +// Object[] target = new Object[src.length + size]; +// System.arraycopy(src, 0, target, 0, src.length); + + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java b/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..148bd6da96 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size; // ArrayList 中的实际元素个数 + private Object[] elementData; + + public ArrayList() { + size = 0; + elementData = new Object[100]; + } + + public void add(Object o){ + if (size >= elementData.length) { + elementData = Array.grow(elementData, 100); + } + elementData[size++] = o; + } + + public void add(int index, Object o){ + if (size >= elementData.length) { + elementData = Array.grow(elementData, 100); + } + + if (index > size || index < 0) throw new ArrayIndexOutOfBoundsException(); + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index > size) throw new ArrayIndexOutOfBoundsException(); + return elementData[index]; + } + + public Object remove(int index){ + + if (index >= size || index < 0) throw new ArrayIndexOutOfBoundsException(); + + Object result = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return result; + } + + public int size() { + return size; + } + + public Iterator iterator(){ + + return new Iterator() { + + private int next = 0; // 下一个返回元素所在的位置 + + public boolean hasNext() { + return next < size; + } + + public Object next() { + if (!hasNext()) throw new ArrayIndexOutOfBoundsException(); + return elementData[next++]; + } + + public Object remove() { + if (next <= 0) throw new IllegalStateException(); + return ArrayList.this.remove(--next); + } + }; + } + +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java b/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..5ddd6f5f8a --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class BinaryTreeNode implements Comparable { + + 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; + } + + public int compareTo(Object obj) { + if (obj == null || obj.getClass() != Integer.class) throw new IllegalArgumentException(); + return Integer.compare(((Integer) data).intValue(), ((Integer) obj).intValue()); + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java b/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..a0b91b1a82 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); + Object remove(); +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java b/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a1548a0c23 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,164 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node first = null; + private Node last = null; + private int size = 0; + + public void add(Object o){ + Node node = new Node(o); + if (first == null) { + first = node; + } else { + last.next = node; + node.prev = last; + } + last = node; + size++; + } + + public void add(int index , Object o) { + if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException(); + + Node node = new Node(o); + + if (first == null) { + first = node; + last = node; + } else { + if (index == 0) { + node.next = first; + first.prev = node; + first = node; + } else if (index == size) { + last.next = node; + node.prev = last; + last = node; + } else { + Node temp = first; + while (--index > 0) { + temp = temp.next; + } + node.next = temp.next; + temp.next.prev = node; + temp.next = node; + node.prev = temp; + } + } + size++; + } + public Object get(int index){ + if (index < 0 || index > size - 1) throw new ArrayIndexOutOfBoundsException(); + Node node = first; + while (index-- > 0) { + node = node.next; + } + return node.data; + } + + public Object remove(int index){ + if (index < 0 || index >= size) throw new ArrayIndexOutOfBoundsException(); + + Node node = null; + if (index == 0) { + node = first; + if (size == 1) { + first = null; + last = null; + } else { + first = first.next; + first.prev = null; + } + } else if (index == size - 1) { + node = last; + last = last.prev; + last.next = null; + } else { + node = first; + Node temp = null; + while (index-- > 0) { + node = node.next; + } + temp = node.prev; + temp.next = node.next; + node.next.prev = temp; + } + size--; + return node.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object obj){ + add(0, obj); + } + + public void addLast(Object obj){ + add(size, obj); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size - 1); + } + + public Iterator iterator(){ + + if (first == null || last == null) throw new IllegalStateException(); + + return new InnerIterator(); + } + + private class InnerIterator implements Iterator { + + private Node nextNode = first; + + public boolean hasNext() { + return nextNode != null; + } + + public Object next() { + if (!hasNext()) throw new ArrayIndexOutOfBoundsException(); + Node node = nextNode; + nextNode = nextNode.next; + return node.data; + } + + public Object remove() { + if (nextNode == first) throw new IllegalStateException(); + + Node node = nextNode.prev; + if (nextNode == first.next) { + first = nextNode; + first.prev = null; + } else if (nextNode == null) { + node = last; + last = last.prev; + last.next = null; + } else { + node.prev = node.next; + node.next.prev = node.prev; + } + return node.data; + } + } + + private static class Node{ + + Object data; + Node next; + Node prev; + + public Node(Object data) { + this.data = data; + next = null; + prev = null; + } + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/List.java b/group14/676615845/algo/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..6d380288e5 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + void add(Object o); + void add(int index, Object o); + Object get(int index); + Object remove(int index); + int size(); +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java b/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..60345ca4f6 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o){ + linkedList.add(o); + } + + public Object deQueue(){ + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java b/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a9f0d009f3 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,26 @@ +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/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java b/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java new file mode 100644 index 0000000000..6308e23251 --- /dev/null +++ b/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java @@ -0,0 +1,26 @@ +package algo; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/24. + */ +public class BinarySearchTest { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void rank() throws Exception { + + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..64fd31eb33 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,146 @@ +package com.coding.basic; + +import org.junit.*; +import org.junit.rules.ExpectedException; + +/** + * Created by mark on 17/2/24. + */ +public class ArrayListTest { + + private static ArrayList list; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + list = new ArrayList(); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void add() throws Exception { + // 可以加入元素 + list.add("hello"); + Assert.assertEquals(1, list.size()); + + // 可以自动扩容 + for (int i=0; i<150; i++) { + list.add(i); + } + Assert.assertEquals(151, list.size()); + Assert.assertTrue(149 == ((Integer) list.get(150)).intValue()); + } + + @Test + public void add1() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + list.add(0, "zero"); + list.add(50, "fifty"); + list.add(102, "102"); + Assert.assertEquals("zero", list.get(0)); + Assert.assertEquals("fifty", list.get(50)); + Assert.assertEquals("102", list.get(102)); + + list = new ArrayList(); + for (int i=0; i<100; i++) { + list.add(i); + } + list.add(100, "100"); + Assert.assertEquals("100", list.get(100)); + + thrown.expect(ArrayIndexOutOfBoundsException.class); + list.add(102, "102"); + } + + @Test + public void get() throws Exception { + list.add("hello"); + Object obj = list.get(0); + Assert.assertTrue("hello".equals(obj)); + } + + @Test + public void remove() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + Assert.assertEquals(99, ((Integer) list.remove(99)).intValue()); + Assert.assertEquals(99, list.size()); + + thrown.expect(ArrayIndexOutOfBoundsException.class); + list.remove(100); + list.remove(-1); + } + + @Test + public void size() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + Assert.assertEquals(100, list.size()); + list.add("hello"); + Assert.assertEquals(101, list.size()); + } + + @Test + public void iterator() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + Assert.assertNotNull(iterator.next()); + } +// Assert.assertNotNull(iterator.next()); + + Object[] target = new Object[list.size()]; + int i = 0; + iterator = list.iterator(); + while (iterator.hasNext()) { + target[i++] = iterator.next(); + } + Assert.assertEquals(100, target.length); + + for (int j = 0; j < 100; j++) { + Assert.assertEquals(j, ((Integer) target[j]).intValue()); + } + + // 测试迭代器的 remove() 方法 + list = new ArrayList(); + for (int k=0; k<100; k++) { + list.add(k); + } + iterator = list.iterator(); +// thrown.expect(IllegalStateException.class); +// iterator.remove(); + + iterator.next(); + Object i0 = iterator.remove(); + Assert.assertEquals(0, ((Integer) i0).intValue()); + + for (int j=0; j<50; j++) { + iterator.next(); + } + Object i50 = iterator.remove(); + Assert.assertEquals(50, ((Integer)i50).intValue()); + + for (int j = 0; j < 48; j++) { + iterator.next(); + } + Object i99 = iterator.remove(); + Assert.assertEquals(98, ((Integer)i99).intValue()); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java new file mode 100644 index 0000000000..579b98c585 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/24. + */ +public class ArrayTest { + private Object[] src; + + @Before + public void setUp() throws Exception { + src = new Object[10]; + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void grow() throws Exception { + src = Array.grow(src, 20); + Assert.assertEquals(30, src.length); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..b3d3c7d557 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class BinaryTreeNodeTest { + + private BinaryTreeNode tree; + + @Before + public void setUp() throws Exception { + tree = new BinaryTreeNode(); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void getData() throws Exception { + + } + + @Test + public void setData() throws Exception { + + } + + @Test + public void getLeft() throws Exception { + + } + + @Test + public void setLeft() throws Exception { + + } + + @Test + public void getRight() throws Exception { + + } + + @Test + public void setRight() throws Exception { + + } + + @Test + public void insert() throws Exception { + tree.insert("8"); + tree.insert("1"); + tree.insert("2"); + tree.insert("10"); + tree.insert("4"); + tree.insert("34"); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..24c2a84367 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,115 @@ +package com.coding.basic; + +import org.junit.*; +import org.junit.rules.ExpectedException; + +/** + * Created by mark on 17/2/24. + */ +public class LinkedListTest { + + private LinkedList linkedList; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @Test + public void add() throws Exception { + linkedList.add("first"); + Assert.assertEquals(1, linkedList.size()); + Assert.assertEquals("first", linkedList.get(0)); + + linkedList.add("second"); + linkedList.add("third"); + Assert.assertEquals("third", linkedList.get(2)); + } + + @Test + public void add1() throws Exception { + for (int i=0; i<10; i++) { + linkedList.add(i); + } + linkedList.add(5, "Five"); + Assert.assertEquals("Five", linkedList.get(5)); + Assert.assertEquals(11, linkedList.size()); + + linkedList.add(0, "Zero"); + Assert.assertEquals("Zero", linkedList.get(0)); + + linkedList.add(12, "Last"); + Assert.assertEquals("Last", linkedList.get(12)); + } + + @Test + public void get() throws Exception { + + linkedList.add("hello"); + Assert.assertEquals("hello", linkedList.get(0)); + + linkedList.add("two"); + Assert.assertEquals("two", linkedList.get(1)); + + linkedList = new LinkedList(); + thrown.expect(ArrayIndexOutOfBoundsException.class); + linkedList.get(0); + } + + @Test + public void remove() throws Exception { + Object data = null; + + for (int i=0; i<10; i++) { + linkedList.add("" + i); + } + + data = linkedList.remove(0); + Assert.assertEquals("0", data); + + data = linkedList.remove(8); + Assert.assertEquals("9", data); + + data = linkedList.remove(4); + Assert.assertEquals("5", data); + } + + @Test + public void size() throws Exception { + linkedList.add(0); + Assert.assertEquals(1, linkedList.size()); + } + + @Test + public void addFirst() throws Exception { + + } + + @Test + public void addLast() throws Exception { + + } + + @Test + public void removeFirst() throws Exception { + + } + + @Test + public void removeLast() throws Exception { + + } + + @Test + public void iterator() throws Exception { + + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..76ecb28a48 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class QueueTest { + + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void enQueue() throws Exception { + queue.enQueue("first"); + queue.enQueue("second"); + queue.enQueue("third"); + Assert.assertEquals("first", queue.deQueue()); + Assert.assertEquals("second", queue.deQueue()); + Assert.assertEquals("third", queue.deQueue()); + } + + @Test + public void deQueue() throws Exception { + + } + + @Test + public void isEmpty() throws Exception { + Assert.assertEquals(true, queue.isEmpty()); + queue.enQueue("first"); + Assert.assertEquals(false, queue.isEmpty()); + queue.deQueue(); + Assert.assertEquals(true, queue.isEmpty()); + } + + @Test + public void size() throws Exception { + Assert.assertEquals(0, queue.size()); + queue.enQueue("first"); + Assert.assertEquals(1, queue.size()); + queue.deQueue(); + Assert.assertEquals(0, queue.size()); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..21192c9036 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,70 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class StackTest { + + private Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void push() throws Exception { + stack.push("first"); + stack.push("second"); + Assert.assertEquals("second", stack.pop()); + Assert.assertEquals("first", stack.pop()); + Assert.assertEquals(0, stack.size()); + } + + @Test + public void pop() throws Exception { + + } + + @Test + public void peek() throws Exception { + stack.push("first"); + stack.push("second"); + Assert.assertEquals("second", stack.peek()); + stack.pop(); + Assert.assertEquals("first", stack.peek()); + } + + @Test + public void isEmpty() throws Exception { + Assert.assertEquals(true, stack.isEmpty()); + stack.push("first"); + Assert.assertEquals(false, stack.isEmpty()); + stack.pop(); + Assert.assertEquals(true, stack.isEmpty()); + + } + + @Test + public void size() throws Exception { + Assert.assertEquals(0, stack.size()); + stack.push("first"); + Assert.assertEquals(1, stack.size()); + stack.push("second"); + Assert.assertEquals(2, stack.size()); + stack.pop(); + stack.pop(); + Assert.assertEquals(0, stack.size()); + } + +} \ No newline at end of file diff --git a/group14/775857669/.gitignore b/group14/775857669/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group14/775857669/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group14/775857669/src/com/coding/basic/ArrayList.java b/group14/775857669/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..b560f97afb --- /dev/null +++ b/group14/775857669/src/com/coding/basic/ArrayList.java @@ -0,0 +1,86 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private static final int MIN_EXTEND = 10; + + private Object[] elementData = new Object[100]; + /** + * + * @Author: yuhe + * @Description: 确保数组的容量 + * @param next 要插入的位置 + */ + private void ensureCapacity(int capacity) { + if (capacity < elementData.length) { + return; + } else { + int newLength = capacity + MIN_EXTEND; + elementData = Arrays.copyOf(elementData, newLength); + } + + } + + private void rangeCheckForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + private void rangeCheck(int index) { + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + public void add(Object o){ + ensureCapacity(size+1); + elementData[size++] = o; + } + + public void add(int index, Object o){ + rangeCheckForAdd(index); + ensureCapacity(size +1); + System.arraycopy(elementData, index, elementData, index+1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object ret = elementData[index]; + int numToRemove = size-index-1; + if (numToRemove > 0) + System.arraycopy(elementData, index+1, elementData, index, numToRemove); + elementData[size--] = null; + return ret; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Iterator() { + private int index = 0; + @Override + public Object next() { + return elementData[index++]; + } + + @Override + public boolean hasNext() { + return index < size; + } + }; + } +} diff --git a/group14/775857669/src/com/coding/basic/BinaryTreeNode.java b/group14/775857669/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..913297b0a4 --- /dev/null +++ b/group14/775857669/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,52 @@ +package com.coding.basic; + +public class BinaryTreeNode> { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object 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 void insert(T t) { + BinaryTreeNode node = new BinaryTreeNode<>(); + node.setData(t); + compare(this, node); + } + + private void compare(BinaryTreeNode targetNode, BinaryTreeNode insertNode) { + + if (targetNode.data.compareTo(insertNode.data) < 0) { + if (targetNode.left != null){ + compare(targetNode.getLeft(), insertNode); + } else { + targetNode.left = insertNode; + } + + } else { + if (targetNode.right != null) { + compare(targetNode.getRight(), insertNode); + } else { + targetNode.right = insertNode; + } + + } + } + +} diff --git a/group14/775857669/src/com/coding/basic/Iterator.java b/group14/775857669/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group14/775857669/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/group14/775857669/src/com/coding/basic/JUnitTest.java b/group14/775857669/src/com/coding/basic/JUnitTest.java new file mode 100644 index 0000000000..3cf54bc02c --- /dev/null +++ b/group14/775857669/src/com/coding/basic/JUnitTest.java @@ -0,0 +1,69 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + + +import org.junit.Test; + +public class JUnitTest { + @Test + public void testArrayList() { + ArrayList list = new ArrayList(); + for (int i = 0; i < 300; i++) { + list.add(i); + } + assertTrue(list.size() == 300); + list.add(3, 3); + assertTrue( (int)list.get(3) == 3 ); + assertTrue( (int)list.get(2) == 2 ); + assertTrue( (int)list.get(4) == 3 ); + assertTrue( (int)list.get(299) == 298 ); + assertTrue( (int)list.get(300) == 299 ); + assertTrue(list.size() == 301); + list.remove(3); + assertTrue( (int)list.get(3) == 3 ); + assertTrue( (int)list.get(2) == 2 ); + assertTrue( (int)list.get(4) == 4 ); + assertTrue( (int)list.get(299) == 299 ); + assertTrue(list.size() == 300); + Iterator iterator = list.iterator(); + while(iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + System.out.println(); + LinkedList linkedList = new LinkedList(); + for(int i=0 ; i<10 ; i++){ + linkedList.add(i); + } + assertTrue(linkedList.size() == 10); + Iterator iterator2 = linkedList.iterator(); + while(iterator2.hasNext()) { + System.out.print(iterator2.next() + " "); + } + linkedList.add(0, -1); + linkedList.add(11,10); + + assertTrue(linkedList.size() == 12); + assertTrue((int)linkedList.removeFirst() == -1); + + assertTrue((int)linkedList.removeLast() == 10); + assertTrue((int)linkedList.remove(5) == 5); + assertTrue(linkedList.size() == 9); + + Stack stack = new Stack(); + for (int i = 0; i < 10; i++) { + stack.push(i); + } + assertTrue(stack.size() == 10); + assertFalse(stack.isEmpty()); + assertTrue((int)stack.peek() == 9); + assertTrue((int)stack.pop() == 9); + assertTrue(stack.size() == 9); + System.out.println(); + for (int i=0 ; i<9 ; i++){ + System.out.print(stack.pop() + " "); + } + assertTrue(stack.isEmpty()); + } + +} diff --git a/group14/775857669/src/com/coding/basic/LinkedList.java b/group14/775857669/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..3756e9d59d --- /dev/null +++ b/group14/775857669/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private int size = 0; + + private Node head; + + public LinkedList() { + head = new Node(); + } + + public void add(Object o){ + Node temp = head; + for (int i=0 ; i size) { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + private void checkRange(int index) { + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + private static class Node{ + public Node() { + } + + public Node(Object data, Node next) { + super(); + this.data = data; + this.next = next; + } + + private Object data; + private Node next; + + } +} diff --git a/group14/775857669/src/com/coding/basic/List.java b/group14/775857669/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group14/775857669/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/group14/775857669/src/com/coding/basic/Queue.java b/group14/775857669/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..0772c397fd --- /dev/null +++ b/group14/775857669/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + return list.removeFirst(); + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group14/775857669/src/com/coding/basic/Stack.java b/group14/775857669/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..64b1caebf5 --- /dev/null +++ b/group14/775857669/src/com/coding/basic/Stack.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (!isEmpty()){ + return elementData.remove(elementData.size()-1); + } else { + 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/group14/857999411/FirstHomework/.classpath b/group14/857999411/FirstHomework/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group14/857999411/FirstHomework/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group14/857999411/FirstHomework/.gitignore b/group14/857999411/FirstHomework/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group14/857999411/FirstHomework/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group14/857999411/FirstHomework/.project b/group14/857999411/FirstHomework/.project new file mode 100644 index 0000000000..e93e0072c0 --- /dev/null +++ b/group14/857999411/FirstHomework/.project @@ -0,0 +1,17 @@ + + + FirstHomework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group14/857999411/FirstHomework/.settings/org.eclipse.jdt.core.prefs b/group14/857999411/FirstHomework/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group14/857999411/FirstHomework/.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/group14/857999411/FirstHomework/src/com/coding/basic/MyArrayList.java b/group14/857999411/FirstHomework/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..6e75248b73 --- /dev/null +++ b/group14/857999411/FirstHomework/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +import java.util.*; + +public class MyArrayList implements MyList{ + //定义Object类型数组 + //定义数组元素个数 + private int size=0; + private Object [] elementData =new Object[10]; + + public void add(Object o) { + ensureCapacity(size+1); + elementData[size] = o; + size++; + } + + //添加指定位置的元 + public void add (int index,Object element){ + if(index > size || index < 0) + throw new IndexOutOfBoundsException("数组角标越界"); + ensureCapacity(size+1); + //添加指定位置元素 + //将该位置后的有元素右 + System.arraycopy(elementData,index,elementData,index+1,size-index); + elementData[index] =element; + size++; + } + + //可调整数组的容量 + public void ensureCapacity (int mincapacity){ + int oldlen =elementData.length; + if(mincapacity > oldlen){ + int newlen =(oldlen * 3)/2 + 1; + if(mincapacity > newlen) + newlen =mincapacity; + elementData =Arrays.copyOf(elementData,newlen); + } + } + + + //获取指定位置的元 + public Object get(int index){ + if(index < 0 || index >size-1){ + throw new IndexOutOfBoundsException("数组角标越界"); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >=size || index < 0){ + throw new IndexOutOfBoundsException("数组角标越界"); + } + Object oldelement =elementData[index]; + int numMoved = size-index-1; + if(numMoved > 0){ + System.arraycopy(elementData,index+1,elementData,index,numMoved); + } + size--; + return oldelement; + } + + public void clear(){ + elementData = null; + } + + public boolean isEmpty (){ + return size == 0; + } + + public int size (){ + return size; + } +} diff --git a/group14/857999411/FirstHomework/src/com/coding/basic/MyLinkedList.java b/group14/857999411/FirstHomework/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..221a8c9092 --- /dev/null +++ b/group14/857999411/FirstHomework/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,138 @@ +package com.coding.basic; + +import java.util.*; + +public class MyLinkedList implements MyList{ + //用内部类定义链表中的节点 + private class Node{ + //节点中包含数据和引用 + Object data; + Node next; + + public Node (){ + + } + + //每个节点包含数据和引 + public Node (Object data,Node next){ + this.data =data; + this.next =next; + } + } + //定义头节点和尾节 + public Node head; + public Node tail; + public int size; + + //无参数构造函数创建空链表 + public MyLinkedList(){ + head =null; + tail =null; + } + + //链表中传入元 + public MyLinkedList(Object element){ + head.data =element; + head.next =tail; + size++; + } + + public void add(Object o){ + addLast(o); + } + public void addFirst(Object element) { + + head =new Node(element,head); + if(tail == null){ + tail=head; + } + size++; + } + + public void addLast(Object element) { + if(head == null) { + head =new Node (element,null); + tail =head; + }else{ + Node newNode =new Node(element,null); + tail.next =newNode; + tail=newNode; + } + size++; + + } + + public void add(int index,Object element){ + + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException("索引越界"); + } + if(index == 0) { + head =new Node(element,head); + } + Node frontNode =getNode(index-1); + frontNode.next =new Node(element,frontNode.next); + size++; + } + public Node getNode(int index) + { + if(index < 0 || index > size-1) { + + throw new IndexOutOfBoundsException("索引越界"); + } + Node current=head; + for(int i=0;i < size; i++,current =current.next) { + if(i == index) { + return current; + } + } + return null; + } + + public Object get(int index){ + return getNode(index).data; + } + + public Object remove(int index){ + if(index < 0 || index > size-1) { + throw new IndexOutOfBoundsException("索引越界"); + } + Node delNode =null; + if(index == 0) { + delNode =head; + head =head.next; + }else{ + Node frontNode =getNode(index-1); + delNode =frontNode.next; + frontNode.next =delNode.next; + delNode.next =null; + } + size--; + return delNode.data; + } + + public Object removeFirst(){ + if(head == null || head.next == null) + throw new NoSuchElementException(); + Node oldhead =head; + head =head.next; + oldhead.next =null; + size--; + return oldhead.data; + + } + + public Object removeLast(){ + return remove(size - 1); + + } + + + public int size() { + return size; + } + + +} + + diff --git a/group14/857999411/FirstHomework/src/com/coding/basic/MyList.java b/group14/857999411/FirstHomework/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..03fffb96c3 --- /dev/null +++ b/group14/857999411/FirstHomework/src/com/coding/basic/MyList.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +public interface MyList { + + 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/group14/857999411/FirstHomework/src/com/coding/basic/MyQueue.java b/group14/857999411/FirstHomework/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..3f397f69ec --- /dev/null +++ b/group14/857999411/FirstHomework/src/com/coding/basic/MyQueue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class MyQueue { + + MyLinkedList link =new MyLinkedList(); + + //入队 + public void enQueue(Object o){ + link.addLast(o); + } + //出队 + public Object deQueue(){ + return link.removeFirst(); + } + //判断是否为空 + public boolean isEmpty(){ + return link.size == 0; + } + //获取长度 + public int size(){ + return link.size; + } +} diff --git a/group14/857999411/FirstHomework/src/com/coding/basic/MyStack.java b/group14/857999411/FirstHomework/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..c0d6395726 --- /dev/null +++ b/group14/857999411/FirstHomework/src/com/coding/basic/MyStack.java @@ -0,0 +1,44 @@ +package com.coding.basic; + +import java.util.*; + +public class MyStack { + + + MyArrayList elementData=new MyArrayList(); + + //入栈 + public void push(Object o){ + elementData.add(o); + } + + //出栈 + public Object pop(){ + + Object element =elementData.get(elementData.size() - 1); + elementData.remove(elementData.size()-1); + return element; + } + + //获取栈顶元素 + public Object peek(){ + int len =elementData.size(); + if(len == 0) + throw new EmptyStackException(); + Object element =elementData.get(len - 1); + return element; + } + + public int size(){ + return elementData.size(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public boolean empty(){ + return elementData.isEmpty(); + } + +} diff --git a/group14/857999411/FirstHomework/src/com/coding/test/MyArrayListTest.java b/group14/857999411/FirstHomework/src/com/coding/test/MyArrayListTest.java new file mode 100644 index 0000000000..a489b51623 --- /dev/null +++ b/group14/857999411/FirstHomework/src/com/coding/test/MyArrayListTest.java @@ -0,0 +1,34 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.coding.basic.MyArrayList; + +public class MyArrayListTest { + + @Test + public void test() { + MyArrayList sa =new MyArrayList(); + sa.add(0,0); + sa.add(1,1); + sa.add(2,2); + sa.add(3,3); + + //System.out.println(sa.get(1)); + + for(int i=0; i parameters) { + + try{ + File f=new File("src\\com\\coderising\\litestruts\\struts.xml");//找到xml文件 + DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//得到dom解析器工厂实例 + DocumentBuilder builder =factory.newDocumentBuilder();//从工厂中得到DOM解析器 + Document doc=builder.parse(f);//解析文件得到docunment + Element root=doc.getDocumentElement();//得到xml文档的根节点 + NodeList books=root.getChildNodes();//得到每一个节点 + if(books!=null){//如果不为空节点 + for(int i=0;i 则 + String name=book.getAttributes().getNamedItem("name").getNodeValue();//获取里的 属性值 + String classname=book.getAttributes().getNamedItem("class").getNodeValue(); + if(actionName.equals(name)){//如果传进来的值等于 属性name的值 + Class localclass=Class.forName(classname);//根据class属性的值创建class + Object object =localclass.newInstance();//创建class实例 + for(Entry entry :parameters.entrySet()){//循环遍历传进来的参数 + String methodname=new StringBuffer("set").append(entry.getKey().substring(0,1).toUpperCase()).append(entry.getKey().substring(1)).toString();//根据参数来生成相应的set函数 + Method method=localclass.getMethod(methodname,entry.getKey().getClass()); + method.invoke(object, entry.getValue());//执行函数 + } + Method methods=localclass.getMethod("execute"); + String returnvalue=(String)methods.invoke(object); + Map map=new HashMap(); + String namepara=(String) getter(object,"name"); + String passwordpara=(String)getter(object,"password"); + String message=(String)getter(object,"message"); + map.put("name",namepara); + map.put("password", passwordpara); + map.put("message",message); + View view= new View(); + view.setParameters(map); + String jsp = null; + if(returnvalue.equals(" ")){ + + } + NodeList nl = doc.getElementsByTagName("result"); + for(int w=0;w 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + public static Object getter(Object object,String toGet){ + Method target=null; + Object result=null; + try{ + target=object.getClass().getMethod("get"+upperFirst(toGet), null); + result=target.invoke(object); + return result; + }catch(Exception e){ + e.printStackTrace(); + } + return result; + } + public static String upperFirst(String toUpper){ + return toUpper.substring(0,1).toUpperCase()+toUpper.substring(1); + } +} diff --git a/group15/1501_2535894075/2017code/src/com/coderising/litestruts/StrutsTest.java b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group15/1501_2535894075/2017code/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/group15/1501_2535894075/2017code/src/com/coderising/litestruts/View.java b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1501_2535894075/2017code/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/group15/1501_2535894075/2017code/src/com/coderising/litestruts/struts.xml b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..d60b234c2b --- /dev/null +++ b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,21 @@ + + + + + + + /jsp/homepage.jsp + + /jsp/showLogin.jsp + + + + + + /jsp/welcome.jsp + + /jsp/error.jsp + + + + \ No newline at end of file diff --git a/group20/423184723/src/com/coding/basic/ArrayList.java b/group15/1501_2535894075/2017code/src/com/coding/basic/ArrayList.java similarity index 100% rename from group20/423184723/src/com/coding/basic/ArrayList.java rename to group15/1501_2535894075/2017code/src/com/coding/basic/ArrayList.java diff --git a/group15/1501_2535894075/2017code/src/com/coding/basic/BinaryTreeNode.java b/group15/1501_2535894075/2017code/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group15/1501_2535894075/2017code/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/group15/1501_2535894075/2017code/src/com/coding/basic/Iterator.java b/group15/1501_2535894075/2017code/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group15/1501_2535894075/2017code/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/group15/1501_2535894075/2017code/src/com/coding/basic/LinkedList.java b/group15/1501_2535894075/2017code/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e2c4e5e795 --- /dev/null +++ b/group15/1501_2535894075/2017code/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/group15/1501_2535894075/2017code/src/com/coding/basic/List.java b/group15/1501_2535894075/2017code/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group15/1501_2535894075/2017code/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/group15/1501_2535894075/2017code/src/com/coding/basic/Queue.java b/group15/1501_2535894075/2017code/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group15/1501_2535894075/2017code/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/group15/1501_2535894075/2017code/src/com/coding/basic/Stack.java b/group15/1501_2535894075/2017code/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group15/1501_2535894075/2017code/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/group15/1502_1617273078/src/com/coderising/array/ArrayUtil.java b/group15/1502_1617273078/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..de36e61726 --- /dev/null +++ b/group15/1502_1617273078/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,283 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] a = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + a[i] = origin[origin.length - 1 - i]; + } + for (int i = 0; i < a.length; i++) { + System.out.print(a[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 count = 0; + int index = 0; + //int[] brige = new int[oldArray.length]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + + count++; + } + } + int[] result = new int[count]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + result[index++] = oldArray[i]; + } + + + } + return result; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int alength = array1.length; + int blength = array2.length; + int[] newint = new int[alength + blength]; + for (int i = 0; i < alength; i++) { + newint[i] = array1[i]; + } + int index = alength; + //有相同项为true,没有为false + boolean flag = false; + for (int c = 0; c < blength; c++) { + for (int j = 0; j < alength; j++) { + if (array1[j] == array2[c]) { + + flag = true; + break; + } + + } + if (flag) { + + flag = false; + } else { + newint[index] = array2[c]; + index++; + } + + } + // 去零 + newint = removeZero(newint); + //排序 + + quickSort(newint); + return newint; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newarry = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newarry[i] = oldArray[i]; + } + return newarry; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int count = 0; + for (int i = 0; ; i++) { + if (createfibonacci(i + 1) < max) { + count++; + } else { + break; + + } + } + int[] arry = new int[count]; + for (int a = 0; a < count; a++) { + arry[a] = createfibonacci(a + 1); + } + return arry; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int count = 0; + for (int i = 0; i < max; i++) { + if (isprime(i)) { + count++; + } + } + int[] arry = new int[count]; + int sign = 0; + for (int i = 0; i < max; i++) { + if (isprime(i)) { + arry[sign] = i; + sign++; + } + } + return arry; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int count = 0; + for (int i = 0; i < max; i++) { + if (isperfectnmber(i)) { + count++; + } + } + int[] arry = new int[count]; + int sign = 0; + for (int i = 0; i < max; i++) { + if (isperfectnmber(i)) { + arry[sign] = i; + sign++; + } + } + return arry; + + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator) { + String stringBuilder=new String(String.valueOf(array[0])); + for (int i = 1; i = pivot) --high; + arr[low] = arr[high]; //交换比枢轴小的记录到左端 + while (low < high && arr[low] <= pivot) ++low; + arr[high] = arr[low]; //交换比枢轴小的记录到右端 + } + //扫描完成,枢轴到位 + arr[low] = pivot; + //返回的是枢轴的位置 + return low; + } + + //生成斐波那契数列 + public static int createfibonacci(int n) { + if (n <= 2) { + return 1; + } else { + return createfibonacci(n - 1) + createfibonacci(n - 2); + } + } + + //判断是否是素数 + public static boolean isprime(int a) { + boolean flag = true; + if (a < 2) { + return false; + } else { + for (int i = 2; i <= Math.sqrt(a); i++) { + if (a % i == 0) { + flag = false; + break; + } + } + } + return flag; + } + + //判断是否是完数 + public static boolean isperfectnmber(int a) { + boolean flag = true; + int temp = 0;// 定义因子之和变量 + + for (int n = 1; n < a / 2 + 1; n++) { + if (a % n == 0) { + temp += n;// 能被整除的除数则被加到temp中 + } + } + if (temp == a) {// 如果因子之和与原数相等的话,说明是完数 + //System.out.print(a + " ");// 输出完数 + flag = true; + } else { + flag = false; + } + return flag; + } + +} diff --git a/group15/1502_1617273078/src/com/coderising/array/ArrayUtilTest.java b/group15/1502_1617273078/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..17f810bc0f --- /dev/null +++ b/group15/1502_1617273078/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,163 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** +* ArrayUtil Tester. +* +* @author +* @since
 27, 2017
+* @version 1.0 +*/ +public class ArrayUtilTest { + +@Before +public void before() throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] a = new int[10]; + a[0] = 1; + a[1] = 2; + a[2] = 3; + a[3] = 4; + a[4] = 5; + a[5] = 6; +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: reverseArray(int[] origin) +* +*/ +@Test +public void testReverseArray() throws Exception { +//TODO: Test goes here... + int[] a = new int[10]; + a[0] = 1; + a[1] = 2; + a[2] = 3; + a[3] = 4; + a[4] = 5; + a[5] = 6; + ArrayUtil arrayUtil = new ArrayUtil(); + for (int i = 0; i parameters) throws DocumentException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + HashMap Parameters = new HashMap<>(); + View view = new View(); + SAXReader reader = new SAXReader(); + Document document = reader.read(new File("./src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + Element thiselement = null; + List list = root.elements(); + String classpath = null; + for (Element element : list) { + //System.out.println(element.attribute("name").getValue()); + if (element.attribute("name").getValue().equals(actionName)) { + thiselement = element; + classpath = element.attribute("class").getValue(); + break; + } + } + Class ojbect; + + try { + ojbect=Class.forName(classpath); + Object acionclass = ojbect.newInstance(); + for (String string : parameters.keySet()) { + Method method = ojbect.getMethod("set"+upperCase(string),String.class); + method.invoke(acionclass,parameters.get(string)); + } + Method execute = ojbect.getMethod("execute"); + String result= (String) execute.invoke(acionclass); + //判断返回的jsp + List elementresult = thiselement.elements(); + for (Element element:elementresult){ + if (element.attribute("name").getValue().equals(result)) { + view.setJsp(element.getStringValue()); + break; + } + } + // 获得getter方法,并设置Parameters + Method[] methods = ojbect.getDeclaredMethods(); + + for (Method method : methods) { + if (method.getName().substring(0, 3).equals("get")) { + String attribute= (String) method.invoke(acionclass); + Parameters.put(method.getName().substring(3).toLowerCase(), attribute); + } + } + view.setParameters(Parameters); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + + return view; + } + //首字母大写 + public static String upperCase(String str) { + char[] ch = str.toCharArray(); + if (ch[0] >= 'a' && ch[0] <= 'z') { + ch[0] = (char) (ch[0] - 32); + } + return new String(ch); + } + +} diff --git a/group15/1502_1617273078/src/com/coderising/litestruts/StrutsTest.java b/group15/1502_1617273078/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..05b7a00aa8 --- /dev/null +++ b/group15/1502_1617273078/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 DocumentException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws DocumentException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group15/1502_1617273078/src/com/coderising/litestruts/View.java b/group15/1502_1617273078/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1502_1617273078/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/group15/1502_1617273078/src/com/coderising/litestruts/struts.xml b/group15/1502_1617273078/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group15/1502_1617273078/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/group15/1502_1617273078/src/com/coding/basic/ArrayList.java b/group15/1502_1617273078/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..cdf06c0a6d --- /dev/null +++ b/group15/1502_1617273078/src/com/coding/basic/ArrayList.java @@ -0,0 +1,77 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[5]; + + public void add(Object o) { + add(size(),o); + //size++; + } + + public void add(int index, Object o) { + if (size+1 > elementData.length) { + Object[] newelementData = new Object[elementData.length * 2]; + System.arraycopy(elementData,0,newelementData,0,elementData.length); + System.arraycopy(newelementData,index,newelementData,index+1,newelementData.length-index-1); + newelementData[index] = o; + elementData = newelementData; + newelementData = null; + size = size + 1; +// Arrays.copyOf() + } else { + System.arraycopy(elementData,index,elementData,index+1,elementData.length-index-1); + elementData[index] = o; + size=size+1; + } + +} + + public Object get(int index) { + + return elementData[index]; + } + + public Object remove(int index) { + System.arraycopy(elementData,index+1,elementData,index,size-2); + return elementData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + + return new Iterator() { + int cuindex = 0; + int lastRet = -1; + + @Override + public boolean hasNext() { + return cuindex != size; + } + + @Override + public Object next() { + int i = cuindex; + if (i>=size){ + throw new NoSuchElementException(); + } + if (i >= elementData.length) + throw new ConcurrentModificationException(); + cuindex = i + 1; + return elementData[lastRet=i]; + } + + }; + } + + +} diff --git a/group15/1502_1617273078/src/com/coding/basic/Iterator.java b/group15/1502_1617273078/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group15/1502_1617273078/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/group15/1502_1617273078/src/com/coding/basic/LinkedList.java b/group15/1502_1617273078/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..f10225fb5e --- /dev/null +++ b/group15/1502_1617273078/src/com/coding/basic/LinkedList.java @@ -0,0 +1,163 @@ +package com.coding.basic; + +public class LinkedList implements List { + private int thesize; + private Node head; + public void add(Object o){ + if (head == null) { + head = new Node(); + head.data = o; + head.next = null; + thesize++; + } else { + addLast(o); + //thesize++; + } + } + public void add(int index , Object o){ + if (index > thesize) { + throw new IndexOutOfBoundsException(); + } else if (index == thesize) { + addLast(o); + //thesize++; + } else if(index= thesize) { + throw new IndexOutOfBoundsException(); + } else{ + Node x = head; + int i=0; + do { + x = x.next; + i++; + } while (i == index); + return x.data; + } + } + public Object remove(int index){ + Node x = head; + for (int i = 1; i =length){ + grow(100); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + size++; + if(size>=length){ + grow(100); + } + System.arraycopy(elementData,index,elementData,index+1,size-index-1); + elementData[index]=o; + } + + public Object get(int index){ + if(index=size) + throw new IndexOutOfBoundsException(); + size--; + Object a=elementData[index]; + //刚好最后一个 + if (index+1==size){ + return a; + } + System.arraycopy(elementData,index+1,elementData,index,size); + return a; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + private class ArrayListIterator implements Iterator{ + private int index=0; + @Override + public boolean hasNext() { + if(index+1o){ + if(right==null){ + right=b; + return b; + } + right.insert(o); + } + return b; + } + + public void showAll(){ + if(right!=null){ + right.showAll(); + } + System.out.print(data+" "); + if(left!=null){ + left.showAll(); + } + + } + + + +} diff --git a/group15/1503_1311822904/myCollection/src/Iterator.java b/group15/1503_1311822904/myCollection/src/Iterator.java new file mode 100644 index 0000000000..96a43dbe0a --- /dev/null +++ b/group15/1503_1311822904/myCollection/src/Iterator.java @@ -0,0 +1,5 @@ +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group15/1503_1311822904/myCollection/src/LinkedList.java b/group15/1503_1311822904/myCollection/src/LinkedList.java new file mode 100644 index 0000000000..69fad47ace --- /dev/null +++ b/group15/1503_1311822904/myCollection/src/LinkedList.java @@ -0,0 +1,182 @@ +public class LinkedList implements List { + private int size = 0; + private Node head=new Node(null); + + public void add(Object o){ + if(head.data==null){ + head.data=o; + return; + } + Node n=head; + while (n.next!=null){ + n=n.next; + } + n.next=new Node(o); + size++; + } + public void add(int index , Object o){ + Node n=getNode(index); + Node newN=new Node(o); + newN.next=n.next; + n.next=newN; + size++; + } + public Object get(int index){ + return getNode(index).data; + } + private Node getNode(int index){ + Node n=head; + for (int i=0;i-1;i--){ + origin[i]=reverse[i]; + } + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + int length=oldArray.length; + int[] index=new int[length]; + int i=0; + int j=0; + for(;i= 0 && arr[j] > temp; j --){ + arr[j+1] = arr[j]; + } + arr[j+1] = temp; + } + } + //去重合 + int j=0; + for(int i = 1;i parameters) { + SAXReader reader = new SAXReader(); + View view=new View(); + try { + File f= new File("src/struts.xml");//E:\mygit\coding2017\liuxin\src\com\coderising\litestruts\struts.xml + Document e = reader.read(f); + //得到根节点 + Element struts = e.getRootElement(); + Iterator it = struts.elementIterator(); + + while(it.hasNext()) {//action遍历 + Element action = (Element)it.next(); + // 找到 name=actionName 的action + List actionAtr = action.attributes(); + if(!actionAtr.get(0).getValue().equals(actionName)) + continue; + String className= actionAtr.get(1).getValue(); + //得到actionName 对应的class + Class aClass=Class.forName(className); + Object o=aClass.newInstance(); + Field field; + for(Map.Entry entry:parameters.entrySet()){ + field = aClass.getDeclaredField( entry.getKey()); + field.setAccessible(true); + field.set(o, entry.getValue()); + } + + Method m = aClass.getMethod("execute"); + String resultString=(String)m.invoke(o); + Method m2 = aClass.getMethod("getMessage"); + String message=(String)m2.invoke(o); + + Map map=new HashMap<>(); + + map.put("message",message); + view.setParameters(map); + it=action.elementIterator(); + //遍历 + while (it.hasNext()){ + Element result = (Element)it.next(); + String s= result.attribute(0).getValue(); + if(resultString.equals(s)){ + view.setJsp(result.getStringValue()); + return view; + } + + } + + //Element results = (Element)it.next(); + //List results = action.attributes(); + + } + } catch (DocumentException var9) { + var9.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (ClassNotFoundException 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 null; + } + +} diff --git a/group15/1503_1311822904/week2/test/ArrayUtilTest.java b/group15/1503_1311822904/week2/test/ArrayUtilTest.java new file mode 100644 index 0000000000..8ba10c75f7 --- /dev/null +++ b/group15/1503_1311822904/week2/test/ArrayUtilTest.java @@ -0,0 +1,114 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** +* ArrayUtil Tester. +* +* @author +* @since
���� 1, 2017
+* @version 1.0 +*/ +public class ArrayUtilTest { + int[] a={0,1,2,3,4,5,6,7,8,9}; +@Before +public void before() throws Exception { +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: reverseArray(int[] origin) +* +*/ +@Test +public void testReverseArray() throws Exception { + + ArrayUtil.reverseArray(a); + System.out.println(Arrays.toString(a)); + +} + +/** +* +* Method: removeZero(int[] oldArray) +* +*/ +@Test +public void testRemoveZero() throws Exception { + int[] a={0,1,0,3,4,0,0,7,8,0,10}; + System.out.println(Arrays.toString(ArrayUtil.removeZero(a))); +} + +/** +* +* Method: merge(int[] array1, int[] array2) +* +*/ +@Test +public void testMerge() throws Exception { + int[] a={0,1,0,3,4,-90,0,0,7,8,0,10}; + int[] b={-90,0,10}; + System.out.println(Arrays.toString(ArrayUtil.merge(a,b))); + +//TODO: Test goes here... +} + +/** +* +* Method: grow(int [] oldArray, int size) +* +*/ +@Test +public void testGrow() throws Exception { + int[] b= ArrayUtil.grow(a,4); + System.out.println(Arrays.toString(b)); +} + +/** +* +* Method: fibonacci(int max) +* +*/ +@Test +public void testFibonacci() throws Exception { + System.out.println(Arrays.toString(ArrayUtil.fibonacci(2))); +} + +/** +* +* Method: getPrimes(int max) +* +*/ +@Test +public void testGetPrimes() throws Exception { + System.out.println(Arrays.toString(ArrayUtil.getPrimes(99))); +} + +/** +* +* Method: getPerfectNumbers(int max) +* +*/ +@Test +public void testGetPerfectNumbers() throws Exception { + System.out.println(Arrays.toString(ArrayUtil.getPerfectNumbers(99))); +} + +/** +* +* Method: join(int[] array, String seperator) +* +*/ +@Test +public void testJoin() throws Exception { + System.out.println(ArrayUtil.join(a,"@")); +} + + +} diff --git a/group15/1506_1084478979/1506_1084478979/.classpath b/group15/1506_1084478979/1506_1084478979/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group15/1506_1084478979/1506_1084478979/.gitignore b/group15/1506_1084478979/1506_1084478979/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group15/1506_1084478979/1506_1084478979/.project b/group15/1506_1084478979/1506_1084478979/.project new file mode 100644 index 0000000000..13c7a33b93 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/.project @@ -0,0 +1,17 @@ + + + 1506_1084478979 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1506_1084478979/1506_1084478979/.settings/org.eclipse.core.resources.prefs b/group15/1506_1084478979/1506_1084478979/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..ac8e1d1ea4 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/banshee/WorkTwo=GBK +encoding//src/banshee/WorkTwo/ArrayUtil.java=UTF-8 +encoding/=UTF-8 diff --git a/group15/1506_1084478979/1506_1084478979/.settings/org.eclipse.jdt.core.prefs b/group15/1506_1084478979/1506_1084478979/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/.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/group15/1506_1084478979/1506_1084478979/RemoteSystemsTempFiles/.project b/group15/1506_1084478979/1506_1084478979/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group15/1506_1084478979/1506_1084478979/src/RemoteSystemsTempFiles/.project b/group15/1506_1084478979/1506_1084478979/src/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/ArrayList.java b/group15/1506_1084478979/1506_1084478979/src/banshee/ArrayList.java new file mode 100644 index 0000000000..5cc79a3457 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/ArrayList.java @@ -0,0 +1,70 @@ +package banshee; + +import java.util.Arrays; +public class ArrayList { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + ensureCapacity(size + 1); + elementData[size++] = o; + } + public void add(int index, Object o){ + rangeCheck(index); + ensureCapacity(size+1); + System.arraycopy(elementData, index, elementData, index + 1, + size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object oldValue = elementData[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementData, index + 1, elementData, index, + numMoved); + elementData[--size] = null; + return oldValue; + } + + public int size(){ + return -1; + } + +// public Iterator iterator(){ +// //TODO +// //���ᡣ���� +// return null; +// } + + + private void rangeCheck( int index) { + if (index >= size || index < 0){ + throw new IndexOutOfBoundsException("ָ����index��������"); + } + } + + + public void ensureCapacity(int minCapacity) { + int oldCapacity = elementData.length; + if (minCapacity > oldCapacity) { + //�����µ�������С��Ϊ��ǰ������1.5�� + int newCapacity = (oldCapacity * 3) / 2 + 1; + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + +} + + diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/LinkedList.java b/group15/1506_1084478979/1506_1084478979/src/banshee/LinkedList.java new file mode 100644 index 0000000000..9462e7fbae --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/LinkedList.java @@ -0,0 +1,155 @@ +package banshee; + +import java.util.NoSuchElementException; + + +public class LinkedList { + + private Node head; + private Node last; + private int size = 0; + + public void add(Object o){ + addAtLast(o); + } + public void add(int index , Object o){ + rangeCheck(index); + if (index == size) { + addAtLast(o); + }else{ + linkBrfore(o, node(index)); + } + } + public Object get(int index){ + rangeCheck(index); + return node(index); + } + public Object remove(int index){ + Node e = node(index); + remove(e); + return null; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + final Node h = head ; + final Node newNode = new Node(null, o, h); + if (h == null) { + last = newNode; + }else{ + h.prev = newNode; + } + size++; + } + public void addLast(Object o){ + addAtLast(o); + } + public Object removeFirst(){ + final Node h = head; + final Object e = h.data; + Node next = h.next; + h.data = null; + h.next = null; + head = next; + if (next == null) + last = null; + else + next.prev = null; + size--; + return e; + } + public Object removeLast(){ + final Node l = last; + final Object e = l.data; + Node newL = l.prev; + l.data = null; + l.prev = null; + last = newL; + if (newL == null) + head = null; + else + newL.next = null; + size--; + return e; + } +// public Iterator iterator(){ +// //TODO +// //����... +// return null; +// } + + + private static class Node{ + Object data; + Node next; + Node prev; + + Node(Node prev, Object element,Node next){ + this.data = element ; + this.next = next; + this.prev = prev ; + } + } + + private void addAtLast(Object element){ + Node l = last; + Node newLink = new Node(l, element, null); + last = newLink; + if (l == null) { + head = newLink; + }else { + l.next = newLink; + } + size++; + } + + private void linkBrfore(Object element , Node spNode ){ + final Node pred = spNode.prev; + final Node newNode = new Node(pred, element, spNode); + spNode.prev = newNode; + if (pred == null) { + head = newNode; + }else{ + pred.next = newNode; + } + size++; + } + + private void rangeCheck(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("ָ����index��������"); + } + } + + private Node node(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 Object remove(Node e) { + if (e == head ) + throw new NoSuchElementException(); + Object result = e. data; + e. prev.next = e.next; + e. next.prev = e.prev; + e. next = e.prev = null; + e. data = null; + size--; + return result; + } + +} + + diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/Queue.java b/group15/1506_1084478979/1506_1084478979/src/banshee/Queue.java new file mode 100644 index 0000000000..2886d458f3 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/Queue.java @@ -0,0 +1,20 @@ +package banshee; + +public class Queue { + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0 ? true : false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/RemoteSystemsTempFiles/.project b/group15/1506_1084478979/1506_1084478979/src/banshee/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/Stack.java b/group15/1506_1084478979/1506_1084478979/src/banshee/Stack.java new file mode 100644 index 0000000000..e1ec7a9ab3 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/Stack.java @@ -0,0 +1,23 @@ +package banshee; + +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?true:false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtil.java b/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtil.java new file mode 100644 index 0000000000..3bac4a642c --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtil.java @@ -0,0 +1,165 @@ +package banshee.WorkTwo; + +import java.util.Arrays; +import java.util.ArrayList; + + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int j = origin.length - 1; + int temp; + for(int i = 0; i < origin.length /2; i++,j--){ + temp = origin[j]; + origin[j] = origin[i]; + origin[i] = 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 int[] removeZero(int[] oldArray){ + int[] newArr = new int[oldArray.length]; + int j = 0; + for (int i = 0; i < newArr.length; i++) { + if (oldArray[i] != 0){ + newArr[j] = oldArray[i]; + j++; + } + } + + return Arrays.copyOfRange(newArr, 0, j) ; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + + int i = 0,j = 0, k = 0; + int[] array3 = new int[array1.length + array2.length]; + + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) { + array3[k++] = array1[i++]; + } else if (array1[i] > array2[j]) { + array3[k++] = array2[j++]; + }else { + array3[k++] = array1[i++]; + j++; + } + } + while (i < array1.length) { + array3[k++] = array1[i++]; + } + while (j < array2.length) { + array3[k++] = array2[j++]; + } + return Arrays.copyOf(array3, k); + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int newCapacity = oldArray.length + size; + int[] newArr = Arrays.copyOf(oldArray, newCapacity); + return newArr; + } + + /** + * 斐波那契数列为: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){ + ArrayList list = new ArrayList(); + if (max == 1) { + return new int[0]; + }else{ + list.add(1); + list.add(1); + int temp = 0, a = 1,b = 1; + while (a + b < max) { + list.add(a + b); + temp = a; + a = b; + b = temp + b; + } + } + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + + arr[i] = (Integer) list.get(i); + + } + return arr; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int element : array) { + sb.append(element).append(seperator); + } + sb.setLength(sb.length() - 1); + return sb.toString(); + } + + + + +} diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtilTest.java b/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtilTest.java new file mode 100644 index 0000000000..4b0c7cd328 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtilTest.java @@ -0,0 +1,72 @@ +package banshee.WorkTwo; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + private ArrayUtil util; + + @Before + public void setUp() throws Exception { + util = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() throws Exception { + int[] origin1 = {7, 9 , 30, 3}; + int[] origin2 = {7, 9, 30, 3, 4}; + util.reverseArray(origin1); + util.reverseArray(origin2); + assertArrayEquals(new int[]{3, 30, 9,7}, origin1); + assertArrayEquals(new int[]{4,3, 30 , 9,7}, origin2); + } + + @Test + public void testRemoveZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + assertArrayEquals(new int[]{1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, util.removeZero(oldArr)); + } + + @Test + public void testMerge() throws Exception{ + int[] arr1 = new int[]{3, 5, 7, 8}; + int[] arr2 = new int[]{4, 5, 6, 7}; + assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, util.merge(arr1, arr2)); + } + + @Test + public void testGrow() throws Exception{ + int[] old = new int[]{2, 3, 6}; + assertArrayEquals(util.grow(old, 3), new int[]{2, 3, 6, 0, 0, 0}); + } + + @Test + public void testFibonacci() throws Exception { + assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, util.fibonacci(15)); + } + + @Test + public void testGetPrimes() { + fail("Not yet implemented"); + } + + @Test + public void testGetPerfectNumbers() { + fail("Not yet implemented"); + } + + @Test + public void testJoin() throws Exception { + int[] arr = new int[]{3, 8, 9}; + assertEquals("3-8-9", util.join(arr, "-")); + } + +} diff --git a/group15/1506_1084478979/RemoteSystemsTempFiles/.project b/group15/1506_1084478979/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group15/1506_1084478979/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group15/1507_977996067/README.md b/group15/1507_977996067/README.md new file mode 100644 index 0000000000..78a3f19258 --- /dev/null +++ b/group15/1507_977996067/README.md @@ -0,0 +1 @@ +### qq 977996067 的作业文件夹 \ No newline at end of file diff --git a/group15/1507_977996067/src/task1/MyArrayList.java b/group15/1507_977996067/src/task1/MyArrayList.java new file mode 100644 index 0000000000..e4d01a2c06 --- /dev/null +++ b/group15/1507_977996067/src/task1/MyArrayList.java @@ -0,0 +1,95 @@ +package task1; + +import java.util.Arrays; +import java.util.Iterator; + +/** + * ArrayList实现 + */ +public class MyArrayList implements MyList { + + //列表元素的个数 + private int size; + //列表存放的元素 + private Object[] elements; + //初始容量10 + private static final int DEFAULT_CAPACITY = 10; + + public MyArrayList() { + elements = new Object[DEFAULT_CAPACITY]; + } + + public MyArrayList(int capacity) { + elements = new Object[capacity <= DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity]; + } + + @Override + public void add(T o) { + add(size, o); + } + + @Override + public void add(int index, T o) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("index " + index + " 不合法"); + if (size >= elements.length) + ensureCapacity((size >> 1) + size); + elements[index] = o; + size++; + } + + @Override + @SuppressWarnings("unchecked") + public T get(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index " + index + " 越界"); + return (T) elements[index]; + } + + @Override + @SuppressWarnings("unchecked") + public T remove(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index " + index + " 越界"); + T removeElement = (T) elements[index]; + System.arraycopy(elements, index + 1, elements, index, elements.length - index - 1); + size--; + return removeElement; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator() { + return new MyItr(); + } + + private void ensureCapacity(int newCapacity) { + elements = Arrays.copyOf(elements, newCapacity); + } + + private class MyItr implements Iterator { + + private int current = 0; + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + @SuppressWarnings("unchecked") + public T next() { + return (T) elements[current++]; + } + + @Override + public void remove() { + if (current == 0) + throw new IllegalStateException("先调用next后才能再调用remove"); + MyArrayList.this.remove(--current); + } + } +} diff --git a/group15/1507_977996067/src/task1/MyLinkedList.java b/group15/1507_977996067/src/task1/MyLinkedList.java new file mode 100644 index 0000000000..fa5d651078 --- /dev/null +++ b/group15/1507_977996067/src/task1/MyLinkedList.java @@ -0,0 +1,130 @@ +package task1; + +import java.util.Iterator; + +/** + * LinkedList 实现 + */ +public class MyLinkedList implements MyList { + + //存放的元素数量 + private int size; + + private Node head; + + public MyLinkedList() { + head = new Node<>(null, null); + } + + @Override + public void add(T o) { + add(size, o); + } + + @Override + public void add(int index, T o) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("index " + index + " 不合法"); + Node targetNode = new Node<>(null, o); + Node targetPrevNode = getPrevNode(index); + targetNode.next = targetPrevNode.next; + targetPrevNode.next = targetNode; + size++; + } + + @Override + public T get(int index) { + checkIndexRange(index); + return getPrevNode(index).next.data; + } + + + @Override + public T remove(int index) { + checkIndexRange(index); + Node prevNode = getPrevNode(index); + Node nodeToRemove = prevNode.next; + prevNode.next = nodeToRemove.next; + size--; + return nodeToRemove.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(T o) { + add(0, o); + + } + + public void addLast(T o) { + add(size, o); + } + + public T removeFirst() { + return remove(0); + } + + public T removeLast() { + return remove(size - 1); + } + + + public Iterator iterator() { + return new MyLinkedItr(); + } + + /** + * 找到位置为index的前一个node + * + * @param index 索引值 + */ + + private Node getPrevNode(int index) { + Node targetPrevNode = head; + for (int i = 0; i < index; i++) { + targetPrevNode = targetPrevNode.next; + } + return targetPrevNode; + } + + /** + * 检查索引是否越界 + * + * @param index 索引值 + */ + private void checkIndexRange(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index " + index + " 越界"); + } + + private static class Node { + private Node next; + private T data; + + private Node(Node next, T data) { + this.next = next; + this.data = data; + } + } + + private class MyLinkedItr implements Iterator { + + private Node currentNode = head; + + @Override + public boolean hasNext() { + return currentNode.next != null; + } + + @Override + public T next() { + Node nextNode = currentNode.next; + T data = nextNode.data; + currentNode = nextNode; + return data; + } + } +} diff --git a/group15/1507_977996067/src/task1/MyList.java b/group15/1507_977996067/src/task1/MyList.java new file mode 100644 index 0000000000..e68f445174 --- /dev/null +++ b/group15/1507_977996067/src/task1/MyList.java @@ -0,0 +1,16 @@ +package task1; + +/** + * List 接口 + */ +public interface MyList { + public void add(T o); + + public void add(int index, T o); + + public T get(int index); + + public T remove(int index); + + public int size(); +} diff --git a/group15/1507_977996067/src/task1/MyQueue.java b/group15/1507_977996067/src/task1/MyQueue.java new file mode 100644 index 0000000000..2ae3d8529f --- /dev/null +++ b/group15/1507_977996067/src/task1/MyQueue.java @@ -0,0 +1,25 @@ +package task1; + +/** + * Queue 实现 + */ +public class MyQueue { + private MyLinkedList elementData = new MyLinkedList(); + + public void enQueue(T o) { + elementData.addFirst(o); + } + + public T deQueue() { + return elementData.removeLast(); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group15/1507_977996067/src/task1/MyStack.java b/group15/1507_977996067/src/task1/MyStack.java new file mode 100644 index 0000000000..476caf67e2 --- /dev/null +++ b/group15/1507_977996067/src/task1/MyStack.java @@ -0,0 +1,29 @@ +package task1; + +/** + * Stack 实现 + */ +public class MyStack { + private MyLinkedList elementData = new MyLinkedList(); + + public void push(T o) { + elementData.addFirst(o); + } + + public T pop() { + return elementData.removeFirst(); + } + + public T peek() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group15/1507_977996067/src/task1/README.md b/group15/1507_977996067/src/task1/README.md new file mode 100644 index 0000000000..d012aef953 --- /dev/null +++ b/group15/1507_977996067/src/task1/README.md @@ -0,0 +1 @@ +###2.26作业:实现简单的数据结构 \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/README.md b/group15/1507_977996067/src/task2/README.md new file mode 100644 index 0000000000..8c99b55977 --- /dev/null +++ b/group15/1507_977996067/src/task2/README.md @@ -0,0 +1,3 @@ +## 3.5 作业文件夹 +1. ArrayUtil +2. struts \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/array/ArrayUtil.java b/group15/1507_977996067/src/task2/array/ArrayUtil.java new file mode 100644 index 0000000000..21cb4286c7 --- /dev/null +++ b/group15/1507_977996067/src/task2/array/ArrayUtil.java @@ -0,0 +1,215 @@ +package task2.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 + */ + public void reverseArray(int[] origin) { + int length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) { + temp[i] = origin[length - i - 1]; + } + System.arraycopy(temp, 0, origin, 0, length); + } + + /** + * 现在有如下的一个数组: 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 length = oldArray.length; + for (int i = 0; i < length; i++) { + int element = oldArray[i]; + if (element != 0) { + oldArray[count] = element; + count++; + } + } + return Arrays.copyOf(oldArray, count); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int firstPos = 0; + int secondPos = 0; + int firstLength = array1.length; + int secondLength = array2.length; + int[] tempArray = new int[firstLength + secondLength]; + int tempArrayPos = 0; +/* 比较两个数组第一个元素的大小,小的元素放到tempArray中且数组索引加一,然后继续比较. + 直到有一个数组遍历完,把另外一个数组剩下的元素依次放到tempArray中*/ + while (firstPos < firstLength && secondPos < secondLength) { + int firstElement = array1[firstPos]; + int secondElement = array2[secondPos]; + if (firstElement < secondElement) { + tempArray[tempArrayPos++] = array1[firstPos++]; + } else if (firstElement > secondElement) { + tempArray[tempArrayPos++] = array2[secondPos++]; + } else { + tempArray[tempArrayPos++] = array1[firstPos++]; + secondPos++; + } + } + if (firstPos == firstLength && secondPos < secondLength) { + System.arraycopy(array2, secondPos, tempArray, tempArrayPos, secondLength - secondPos); + tempArrayPos += secondLength - secondPos; + } else if (secondPos == secondLength && firstPos < firstLength) { + System.arraycopy(array1, firstPos, tempArray, tempArrayPos, firstLength - firstPos); + tempArrayPos += firstLength - firstPos; + } + return Arrays.copyOf(tempArray, tempArrayPos); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] temp = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, temp, 0, oldArray.length); + return temp; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max == 1) + return new int[0]; + int[] result = new int[max + 1]; + result[0] = 1; + result[1] = 1; + return getFibonacci(result, 2, max); + } + + private int[] getFibonacci(int[] arr, int index, int max) { + if (arr[index - 1] >= max) { + return Arrays.copyOf(arr, index - 1); + } + arr[index] = arr[index - 1] + arr[index - 2]; + return getFibonacci(arr, ++index, max); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max == 2) + return new int[]{2}; + else if (max >= 3 && max <= 4) + return new int[]{2, 3}; + else if (max > 4) { + int[] arr = new int[max]; + arr[0] = 2; + arr[1] = 3; + int pos = 2; + for (int i = 5; i < max; i++) { + //去掉能被2、3整除的数:6n+2 6n+3 6n+4 + if ((i + 1) % 6 == 0 || (i - 1) % 6 == 0) { + if (isPrime(i)) { + arr[pos++] = i; + } + } + } + return Arrays.copyOf(arr, pos); + } + return null; + } + + private boolean isPrime(int number) { + //去掉偶数 + if (number % 2 == 0) + return false; + //只能被自身整除的为素数 + for (int i = 2; i < number; i++) { + if (number % i == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] arr = new int[max]; + int pos = 0; + for (int i = 4; i < max; i++) { + //去掉素数 + if (!isPrime(i)) { + List list = new ArrayList<>(); + for (int j = 1; j < i; j++) { + if (i % j == 0) + list.add(j); + } + if (i == list.stream().reduce(0, (a, b) -> a + b)) + arr[pos++] = i; + } + } + return Arrays.copyOf(arr, pos); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param separator + * @return + */ + public String join(int[] array, String separator) { + StringBuilder sb = new StringBuilder(); + for (int element : array) { + sb.append(element).append(separator); + } + sb.setLength(sb.length() - 1); + return sb.toString(); + } + +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/array/ArrayUtilTest.java b/group15/1507_977996067/src/task2/array/ArrayUtilTest.java new file mode 100644 index 0000000000..8a38dd59ce --- /dev/null +++ b/group15/1507_977996067/src/task2/array/ArrayUtilTest.java @@ -0,0 +1,75 @@ +package task2.array; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +public class ArrayUtilTest { + + private ArrayUtil util; + + @Before + public void pre() { + util = new ArrayUtil(); + } + + @Test + public void reverseArray() throws Exception { + int[] i = {7, 9, 40, 30, 3}; + int[] ii = {7, 9, 40, 30, 3, 11}; + util.reverseArray(i); + util.reverseArray(ii); + assertArrayEquals(new int[]{3, 30, 40, 9, 7}, i); + assertArrayEquals(new int[]{11, 3, 30, 40, 9, 7}, ii); + } + + @Test + public void removeZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + assertArrayEquals(new int[]{1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, util.removeZero(oldArr)); + } + + @Test + public void merge() throws Exception { + int[] arr1 = new int[]{3, 5, 7, 8}; + int[] arr2 = new int[]{4, 5, 6, 7}; + assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, util.merge(arr1, arr2)); + } + + @Test + public void grow() throws Exception { + int[] old = new int[]{2, 3, 6}; + assertArrayEquals(util.grow(old, 3), new int[]{2, 3, 6, 0, 0, 0}); + } + + @Test + public void fibonacci() throws Exception { + assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, util.fibonacci(15)); + } + + @Test + public void getPrimes() throws Exception { + int[] primes = util.getPrimes(33); + assertArrayEquals(new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}, primes); + } + + @Test + public void getPerfectNumbers() throws Exception { + assertArrayEquals(new int[]{6,28,496,8128}, util.getPerfectNumbers(10000)); + } + + @Test + public void join() throws Exception { + int[] arr = new int[]{3, 8, 9}; + assertEquals("3-8-9", util.join(arr, "-")); + } + + private void printArray(int[] arr) { + Arrays.stream(arr).forEach(System.out::println); + } + + +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/litestruts/LogOutAction.java b/group15/1507_977996067/src/task2/litestruts/LogOutAction.java new file mode 100644 index 0000000000..b62d25ecc9 --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/LogOutAction.java @@ -0,0 +1,34 @@ +package task2.litestruts; + +public class LogOutAction { + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "logout successful"; + return "success"; + } + this.message = "logout failed,please check your user/pwd"; + return "error"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/litestruts/LoginAction.java b/group15/1507_977996067/src/task2/litestruts/LoginAction.java new file mode 100644 index 0000000000..abde57860a --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/LoginAction.java @@ -0,0 +1,34 @@ +package task2.litestruts; + +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/litestruts/Struts.java b/group15/1507_977996067/src/task2/litestruts/Struts.java new file mode 100644 index 0000000000..c2c8a61c3a --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/Struts.java @@ -0,0 +1,80 @@ +package task2.litestruts; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + +// 0. 读取配置文件struts.xml,引用Jsoup + Document document = null; + try { + document = Jsoup.connect("http://my.977996067.cn/2017_2/struts.xml").get(); + } catch (IOException e) { + System.err.println("xml解析失败"); + } +// 获取所有的action标签 + Elements actions = document != null ? document.select("action") : null; + if (actions == null) + return null; + for (Element actionElement : actions) { + String name = actionElement.attr("name"); +// 反射action + if (actionName.equals(name)) { + try { + Class actionClass = Class.forName(actionElement.attr("class")); + Object o = actionClass.getConstructor().newInstance(); + parameters.forEach((k, v) -> { + try { + Field field = actionClass.getDeclaredField(k); + field.setAccessible(true); + //赋值 + field.set(o, v); + } catch (Exception e) { + e.printStackTrace(); + } + }); +// 执行execute方法,获取返回值 + Method method = actionClass.getMethod("execute"); + String returnValue = (String) method.invoke(o); + View view = new View(); + Map map = new HashMap(); + Arrays.stream(actionClass.getMethods()) + .forEach(((Method m) -> { + try { + String methodName = m.getName(); + if (methodName.startsWith("get")) + map.put(methodName.substring(methodName.indexOf("get") + 3).toLowerCase(), m.invoke(o)); + } catch (Exception e) { + e.printStackTrace(); + } + })); + view.setParameters(map); +// 获取返回视图名 + Elements children = actionElement.children().select("result"); + for (Element aChildren : children) { + if (returnValue.equals(aChildren.attr("name"))) { + view.setJsp(aChildren.text()); + } + } + System.out.println(view); + return view; + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return null; + } +} diff --git a/group15/1507_977996067/src/task2/litestruts/StrutsTest.java b/group15/1507_977996067/src/task2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..ec7d992cf2 --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package task2.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/group15/1507_977996067/src/task2/litestruts/View.java b/group15/1507_977996067/src/task2/litestruts/View.java new file mode 100644 index 0000000000..667068ef37 --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/View.java @@ -0,0 +1,33 @@ +package task2.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; + } + + @Override + public String toString() { + return "View{" + "jsp='" + jsp + '\'' + + ", parameters=" + parameters + + '}'; + } +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/litestruts/struts.xml b/group15/1507_977996067/src/task2/litestruts/struts.xml new file mode 100644 index 0000000000..8716d96c5a --- /dev/null +++ b/group15/1507_977996067/src/task2/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/group15/1510_739253131/src/com/bruce/homework0226/ArrayListV00.java b/group15/1510_739253131/src/com/bruce/homework0226/ArrayListV00.java new file mode 100644 index 0000000000..3137c7568b --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0226/ArrayListV00.java @@ -0,0 +1,177 @@ +package com.bruce.homework0226; + +import com.bruce.utils.MyException; + +import java.io.Serializable; +import java.util.Arrays; + +/** + * 用数组实现ArrayList基本功能:add,remove,size,contains,toArray方法 + * @Version: 0.0 + * Created by Bruce.Jiao on 17-2-23. + */ +public class ArrayListV00 implements Serializable { + + /** + * 存放集合元素的数组 + */ + private transient Object[] elementData; + /** + * 集合中元素的个数 + */ + private int size; + + /** + * 空构造,默认数组长度为10 + */ + public ArrayListV00() throws MyException { + this(10); + } + + /** + * 有参构造 + * + * @param initCapacity + * 用户传入的集合大小,底层数组的初始化大小 + */ + public ArrayListV00(int initCapacity) throws MyException{ + if(initCapacity < 0){ + throw new MyException("集合大小不能小于0"); + } + elementData = new Object[initCapacity]; + } + + /** + * 向集合中添加元素 + * + * @param value + * 添加的元素,允许添加null + * @return true:添加成功 ; false:添加失败 + */ + public boolean add(Object value) { + // 添加元素之前,对数组长度进行判断,此处需要传入当前元素个数+1, + ensureCapacity(size + 1); + elementData[size++] = value; + return true; + } + + /** + * 返回指定位置的元素 数组和集合,下标从1开始 + * + * @param index + * 用户指定的位置 + * @return + */ + public Object get(int index) throws MyException { + // 判断是否越界,注意:此处判断依据是size,而不能是elementData.length, + // 集合元素个数size小于等于elementData.length + if (index >= size || index < 0) { + throw new MyException("给定数值超出集合范围"); + } + return elementData[index]; + } + + /** + * 删除指定位置的元素 + * + * @param index + * 用户指定位置,从0开始 + * @return 返回删除掉的指定位置的元素 + */ + public Object remove(int index) throws MyException { + if (index >= size || index < 0) { + throw new MyException("给定数值超出集合范围"); + } + Object value = elementData[index]; + // 数组中被删除元素后边的所有元素的个数,此处不能使用elementData.length + int length = size - 1 - index; + // 被删除位置后还有元素,将数组中被删除位置往后(不包含被删除位置)的所有元素往前移动一位 + if (length > 0) { + System.arraycopy(elementData, index + 1, elementData, index, length); + } + elementData[--size] = null; + return value; + } + + /** + * 判断集合中是否包含指定的元素 + * + * @param value + * 用户制定的元素 + * @return true:包含指定元素;false:不包含指定元素 + */ + public boolean contains(Object value) { + for (int i = 0; i < elementData.length; i++) { + if (value == null) { + if (elementData[i] == null) { + return true; + } + } else { + if (value.equals(elementData[i])) { + return true; + } + } + } + return false; + } + + /** + * 得到集合对应的静态数组 + * + * @return 底层数组 + */ + public Object[] toArray() { + //elementData可能会包含null元素,不能直接返回,需返回一个包含集合所有元素的新数组 +// return elementData; + return Arrays.copyOf(elementData,size); + } + + /** + * 返回集合大小 + * + * @return + */ + public int size() { + return size; + } + + /** + * 传入的数值与数组长度进行比较,长度小于传入数值,对数组进行扩容 + * + * @param minCapacity + * 传入的数值 + */ + public void ensureCapacity(int minCapacity) { + int oldCapacity = elementData.length; + // 如果传入数值大于现有数组长度,对现有数组进行扩容 + if (minCapacity > oldCapacity) { + // 此处用新的局部变量引用指向原有数组的内存地址,仅为了避免复制数组元素到新数组时候,发生原有数组内存地址被覆盖的情况 + Object[] oldArray = elementData; + // 先得到现有数组长度1.5倍的值 + int newCapacity = oldCapacity + oldCapacity >> 1; + // 如果增加1.5倍后的数值仍然小于传入的数值,将传入的数值赋给新数组长度 + if (minCapacity > newCapacity) { + newCapacity = minCapacity; + } + // 将elementData引用指向一个新的扩容后的数组,并且将原有数组的元素复制到新数组中 + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + /** + * 重写toString方法,查看集合具体内容 + * @return + */ + @Override + public String toString() { + return Arrays.toString(elementData); + } + + /** + * 仅仅作为自己查看底层数组长度的方法, + * @return + */ + int arrayLength() { + return elementData.length; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0226/JuintTest.java b/group15/1510_739253131/src/com/bruce/homework0226/JuintTest.java new file mode 100644 index 0000000000..b0aa452f6d --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0226/JuintTest.java @@ -0,0 +1,141 @@ +package com.bruce.homework0226; + +import com.bruce.utils.MyException; +import junit.framework.TestCase; +import org.junit.Test; +import java.util.Arrays; +import java.util.Random; + +/** + * Created by Bruce.Jiao on 17-2-23. + */ +public class JuintTest extends TestCase{ + + @Test + public void testArrayList(){ + try { + ArrayListV00 arrayList = new ArrayListV00(0); + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("fff"); + arrayList.add("ggg"); + System.out.println("集合下标2处的元素:"+arrayList.get(2)); + System.out.println("是否包含ccc这个元素:"+arrayList.contains("ccc")); + System.out.println("是否包含ddd这个元素:"+arrayList.contains("ddd")); + System.out.println("删除前集合大小为:"+arrayList.size()); + System.out.println("删除下标2处元素前底层数组:"+arrayList); + arrayList.remove(2); + System.out.println("删除下标2处元素后底层数组:"+arrayList); + System.out.println("删除一个元素后集合大小为:"+arrayList.size()); + arrayList.remove(2); + System.out.println("再删除下标2处元素后底层数组:"+arrayList); + System.out.println("集合为:"+ Arrays.toString(arrayList.toArray())); + System.out.println("集合底层数组长度:"+ arrayList.arrayLength()); +// System.out.println("集合下标-1处的元素:"+arrayList.get(-1)); + } catch (MyException e) { + System.out.println("发生异常>>>"+e); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testLinkedList(){ + try { + LinkedListV00 linkedList = new LinkedListV00<>(); + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("ddd"); + System.out.println("删除index=2的元素前:"+linkedList); + System.out.println("链表尺寸"+linkedList.size()); + System.out.println("拿到index=2的元素"+linkedList.get(2)); + linkedList.remove(2); + System.out.println("删除index=2的元素后:"+linkedList); + } catch (MyException e) { + System.out.println(e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testStack(){ + try { + StackV00 stack = new StackV00(); + stack.push("ccc"); + stack.push(null); + stack.push("bbb"); + stack.push("aaa"); + System.out.println("栈的大小:"+stack.size()); + System.out.println("栈是否为空:"+stack.isEmpty()); + System.out.println("栈是否为空:"+stack); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + stack.clear(); + System.out.println("清空后,栈大小:"+stack.size()); + System.out.println("栈是否为空:"+stack.isEmpty()); + } catch (MyException e) { + System.out.println(e); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testQueue(){ + try { + QueueV00 queue = new QueueV00(); + System.out.println("队列是否为空:"+queue.isEmpty()); + queue.add("aaa"); + queue.add("bbb"); + queue.add("ccc"); + queue.add("ddd"); + System.out.println(queue); + System.out.println("queue.peek结果:"+queue.peek()); + System.out.println("peek后队列长度:"+queue.length()); + System.out.println("queue.poll结果:"+queue.poll()); + System.out.println("poll后队列长度:"+queue.length()); + System.out.println("队列是否为空:"+queue.isEmpty()); + } catch (MyException e) { + System.out.println(e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testArrayLinked(){ + try { + ArrayListV00 arrayList = new ArrayListV00(); + LinkedListV00 linkedList = new LinkedListV00(); + long start1 = System.currentTimeMillis(); + for(int i = 0;i<10000;i++){ + arrayList.add("abc"+i); + } + long end1 = System.currentTimeMillis(); + for(int i = 0;i<10000;i++){ + linkedList.add("abc"+i); + } + long end2 = System.currentTimeMillis(); + System.out.println("ArrayList的时间:"+(end1-start1)); + System.out.println("LinkedList的时间:"+(end2-end1)); + } catch (MyException e) { + e.printStackTrace(); + } + } + + public String getRandomString(int length){ + String base = "abcdefghijklmnopqrstuvwxyz0123456789"; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + for(int i = 0;i implements Serializable { + + /** + * 双向链表中节点实例的个数 + */ + private transient int size = 0; + + /** + * 前一个节点 + */ + private transient Node first; + + /** + * 后一个节点 + */ + private transient Node last; + + /** + * 空构造 + */ + public LinkedListV00(){} + + /** + * 添加一个节点 + * @param element + * @return + */ + public boolean add(E element){ + linkNext(element); + return true; + } + + /** + * 拿到制定位置的元素 + * @param index + * @return + */ + public E get(int index) throws MyException{ + if(index < 0 || index > size){ + throw new MyException("索引越界"); + } + return node(index).element; + } + + /** + * 删除指定位置的元素 + * 将index-1处节点的next指向index+1处节点,将index+1处节点的previous指向index-1节点 + * @param index 节点位置 + * @return 删除节点的element数据 + */ + public E remove(int index) throws MyException{ + if(index < 0 || index > size){ + throw new MyException("节点索引越界"); + } + return unlink(node(index)); + } + + /** + * 返回链表的长度 + * @return 双向链表中节点实例的个数 + */ + public int size(){ + return size; + } + + /** + * 拿到链表对应的数组 + * @return 链表各节点的element元素组成的数组 + */ + public Object[] toArray(){ + Object[] array = new Object[size]; + for(int i = 0;i x = first; x != null; x = x.next) +// result[i++] = x.element; +// return result; + } + + /** + * 表示一个节点的内部类 + * @param 链表元素泛型 + */ + private static class Node{ + E element; + Node previous; + Node next; + Node(Node previous,E element,Node next){ + this.element = element; + this.previous = previous; + this.next = next; + } + } + + /** + * 根据索引拿对应的节点 + * @param index 索引号 + * @return 索引号对应的节点 + */ + private Node node(int index){ + Node x; + //如果index小于size的一半,即目标节点在链表前半部分 + if(index < (size >> 1)){ + //从第一个节点挨个向后查找,一直到(index-1)处,将其next赋值给x + x = first; + for(int i = 0; iindex;i--){ + x = x.previous; + } + } + //返回x + return x; + } + + /** + * 链接下一个 + * @param e 新节点的element + */ + private void linkNext(E e){ + //将当前的last节点赋值给n + final Node n = last; + //创建一个新的Node节点,其previous为n,next为null + final Node newNode = new Node(n,e,null); + //创建一个新的节点后,则last更新为新节点newNode + last = newNode; + //如果n为null,说明还是一个空的双向链表,将新节点newNode赋值给first + //否则,将newNode赋值给n的next + if(n == null){ + first = newNode; + }else{ + n.next = newNode; + } + //添加一个新节点后,size加1 + size++; + } + + /** + * 从链表解除一个节点 + * @param node 将要被链表接触关联的目标节点 + * @return 目标节点的element元素 + */ + private E unlink(Node node){ + //拿到传入节点的元素 + final E element = node.element; + //拿到传入节点的next节点 + final Node next = node.next; + //拿到传入节点的previous节点 + final Node previous = node.previous; + //如果传入节点的previous=null,说明是第一个节点 + if(previous == null){ + //将链表第一个节点指向本节点的下一个节点next,即把原有的第一个节点解除 + first = next; + }else{ + //将本节点前一个节点的next指向本节点后一个节点,即跳过了本节点 + previous.next = next; + //将本节点的previous节点设置为null + node.previous = null; + } + //如果传入节点的next=null,说明是最后一个节点 + if(next == null){ + //将链表最后一个节点指向本节点的前一个节点,即把原来的最后一个节点解除 + last = previous; + }else{ + //将本节点下一个节点的previous节点指向本节点的前一个节点,即跳过了本节点 + next.previous = previous; + //本节点的next节点设置为null + node.next = null; + } + node.element = null; + size--; + return element; + } + + /** + * 仅作为临时方法,打印链表元素使用,方面查看 + * @return + */ + @Override + public String toString() { + return Arrays.toString(toArray()); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0226/QueueV00.java b/group15/1510_739253131/src/com/bruce/homework0226/QueueV00.java new file mode 100644 index 0000000000..7e86244d66 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0226/QueueV00.java @@ -0,0 +1,117 @@ +package com.bruce.homework0226; + +import com.bruce.utils.MyException; + +import java.util.Arrays; + +/** + * 实现Queue的基本功能:peek,poll,add,length,isEmpty + * Created by Bruce.Jiao on 2017/2/25. + */ +public class QueueV00 { + /** + * 队列元素的数组 + */ + private Object[] elementData; + + /** + * 队列容量 + */ + private int max_size; + + /** + * 队列头,允许删除 + */ + private int head; + + /** + * 队列尾,允许插入 + */ + private int tail; + + /** + * 无参构造,默认的初始长度10 + */ + public QueueV00() throws MyException{ + this(10); + } + + /** + * 有参构造 + * @param initCapacity 用户自定的初始长度 + */ + public QueueV00(int initCapacity) throws MyException{ + if(initCapacity < 0){ + throw new MyException("队列长度不能为负数"); + } + this.max_size = initCapacity; + elementData = new Object[initCapacity]; + head = tail = 0; + } + + /** + * 向队列里添加元素 + * @param value 添加的元素 + * @return true:添加成功;false:添加失败 + * @throws MyException 加入添加完成后元素个数超过队列最大尺寸,抛出异常 + */ + public boolean add(Object value) throws MyException{ + if(tail == max_size){ + throw new MyException("队列已满,不能继续插入"); + } + elementData[tail++] = value; + return true; + } + + /** + * 返回队列的第一个元素,但不从队列中删除该元素 + * @return 队列的第一个元素,以插入顺序为先后标准 + */ + public Object peek() throws MyException{ + if(isEmpty()){ + throw new MyException("队列为空队列"); + } + return elementData[head]; + } + + /** + * 返回队列的第一个元素,并且从队列中将该元素删除 + * @return 队列的第一个元素 + * @throws MyException 队列为空,抛出异常 + */ + public Object poll() throws MyException{ + if(isEmpty()){ + throw new MyException("队列为空队列"); + } + //将队列的第一个元素暂存 + Object result = elementData[head]; + //将队列的第一个元素设置为null,并且将head加1 + elementData[head++] = null; + return result; + } + + /** + * 队列长度 + * @return 队列中元素个数 + */ + public int length(){ + return tail-head; + } + + /** + * 判断队列是否为空 + * @return true:队列为空;false:队列不为空 + */ + public boolean isEmpty(){ + return tail == head; + } + + /** + * 临时方法,仅作为测试阶段打印队列元素使用 + * @return + */ + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(elementData,length())); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0226/StackV00.java b/group15/1510_739253131/src/com/bruce/homework0226/StackV00.java new file mode 100644 index 0000000000..29cfe7e9fd --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0226/StackV00.java @@ -0,0 +1,144 @@ +package com.bruce.homework0226; + +import com.bruce.utils.MyException; + +import java.io.Serializable; +import java.util.Arrays; + +/** + * 用数组实现一个栈的基本功能:push,pop,isEmpty,size,clear方法 + * @Version: 0.0 + * Created by Bruce.Jiao on 17-2-24. + */ +public class StackV00 implements Serializable{ + + /** + * 底层存放栈元素的数组 + */ + private Object[] elementData; + + /** + * 栈中元素的个数 + */ + private int size; + + /** + * 每次扩容增加的大小 + */ + private int capacityIncrement; + + /** + * 空构造,数组初始长度为10 + */ + public StackV00() throws MyException{ + this(10); + } + + /** + * 有参构造 + * @param initCapacity 用户指定的栈空间初始大小(底层数组的初始大小) + * @throws MyException 对传入参数进行判断,不符合要求抛出异常 + */ + public StackV00(int initCapacity) throws MyException{ + this(initCapacity,0); + } + + /** + * 有参构造 + * @param initCapacity 用户指定的栈空间初始大小(底层数组的初始大小) + * @param capacityIncrement 用户指定的每次扩容大小(当空间不足时,每一次扩容增加的大小) + * @throws MyException 对传入参数进行判断,不符合要求抛出异常 + */ + public StackV00(int initCapacity, int capacityIncrement) throws MyException{ + if(initCapacity < 0 || capacityIncrement <0){ + throw new MyException(initCapacity < 0?"栈空间大小不能为负数":"扩容参数不能为负数"); + } + elementData = new Object[initCapacity]; + } + + /** + * 向栈中添加元素 + * @param value 添加的元素,可以为null + * @return 添加成功后的元素 + */ + public Object push(Object value){ + ensureCapacity(size+1); + //将新增的元素放在size索引处,并且将size加1 + elementData[size++] = value; + return value; + } + + /** + * 从栈中获取元素,拿到当前所有元素中最后添加进来的元素 + * @return 最后的元素 + */ + public Object pop(){ + //拿到最后的元素,在栈中将该元素删除,将size减1 + return elementData[--size]; + } + + /** + * 判断栈是否为空 + * @return true:空栈,无元素;false:有元素 + */ + public boolean isEmpty(){ + return size == 0; + } + + /** + * 获取栈大小(元素数量,包括null元素) + * @return 栈中元素大小 + */ + public int size(){ + return size; + } + + /** + * 清空栈中元素 + */ + public void clear(){ + int oldCapacity = elementData.length; + size = 0; + elementData = new Object[oldCapacity]; + } + + /** + * 判断数组尺寸 + * @param minCapacity + */ + public void ensureCapacity(int minCapacity){ + int oldCapacity = elementData.length; + //如果传入值大于当前数组尺寸,对数组进行扩容 + if(minCapacity > oldCapacity){ + //如果capacityIncrement大于0,每次扩容用户指定的大小,否则每次将当前数组尺寸扩大一倍 + int newCapacity = oldCapacity + capacityIncrement > 0 ? capacityIncrement : oldCapacity; + //元素数组扩容,并且将原有的元素复制到新数组中 + elementData = Arrays.copyOf(elementData,newCapacity); + } + } + + /** + * 拿到底层的静态数组 + * @return 底层元素数组 + */ + public Object[] toArray(){ + return Arrays.copyOf(elementData,size); + } + + /** + * toString方法,可以直接打印出栈底层的数组 + * @return + */ + @Override + public String toString() { + return Arrays.toString(toArray()); + } + + /** + * 仅仅作为自己查看底层数组长度的方法, + * @return 数组长度,大于等于元素个数 + */ + int arrayLength() { + return elementData.length; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java b/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java new file mode 100644 index 0000000000..85276ce4da --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java @@ -0,0 +1,232 @@ +package com.bruce.homework0305.array; + +import com.bruce.homework0226.LinkedListV00; + +import java.util.*; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public int[] reverseArray(int[] origin){ + if(origin != null && origin.length > 0) { + int temp; + for (int i = 0; i < origin.length / 2; i++) { + temp = origin[i]; + origin[i] = origin[origin.length-1 - i]; + origin[origin.length-1 - i] = 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[] result = null; + if(oldArray != null && oldArray.length >= 0){ + int index = 0; + LinkedList linkedList = new LinkedList(); + for(int i = 0;i list = new LinkedList<>(); + getElementFromArray(array1,list); + getElementFromArray(array2,list); + Collections.sort(list); + int[] result = new int[list.size()]; + for(int n = 0;n Integer.MAX_VALUE) { + return result;//抛出异常 + } else if (max == 1) { + result = new int[0]; + } else { + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + for (int i = 2;i max){ + break; + } + list.add(i,(list.get(i-2)+list.get(i-1))); + } + result = new int[list.size()]; + for (int j=0;j Integer.MAX_VALUE) { + return result;//抛出异常 + } else if(max == 1){ + result = new int[0]; + }else { + ArrayList list = new ArrayList<>(); + for (int i=2 ; i<=max ; i++) { + if(isPrimes(i)){ + list.add(i); + } + } + result = new int[list.size()]; + for(int m = 0 ; m Integer.MAX_VALUE){ + return result; + } else { + LinkedList list = new LinkedList<>(); + for(int i = 1 ; i < max ; i++){ + if (isPerfectNumber(i)) { + list.add(i); + } + } + result = new int[list.size()]; + for (int i = 0 ; i < list.size() ; i++) { + result[i] = list.get(i); + } + } + return result; + } + + //判断一个数是否是完数,true:是完数;false:不是完数。 + public boolean isPerfectNumber(int value){ + int sum = 0; + for(int i = 1 ; i < value ; i++){ + if(value % i == 0){ + sum += i; + } + } + return sum == value; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer sb = new StringBuffer(); + if (array != null && array.length >= 1) { + for (int i = 0 ; i < array.length ; i++) { + sb.append(array[i]); + if (i < array.length-1) { + sb.append(seperator); + } + } + } + return sb.toString(); + } + + +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java b/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java new file mode 100644 index 0000000000..eb5487e76d --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java @@ -0,0 +1,84 @@ +package com.bruce.homework0305.array; + +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by Bruce.Jiao on 2017/3/2. + */ +public class JuintArrayUtil extends TestCase { + + @Test + public void testReverse(){ + ArrayUtil au = new ArrayUtil(); + int[] demo0 = {}; + int[] demo1 = {6}; + int[] demo = {7, 9, 30, 3, 4, 6}; + int[] nullArray = au.reverseArray(null); + int[] reverseArray0 = au.reverseArray(demo0); + int[] reverseArray1 = au.reverseArray(demo1); + int[] reverseArray = au.reverseArray(demo); + System.out.println(Arrays.toString(nullArray)); + System.out.println(Arrays.toString(reverseArray0)); + System.out.println(Arrays.toString(reverseArray1)); + System.out.println(Arrays.toString(reverseArray)); + } + + @Test + public void testRemoveZero(){ + ArrayUtil au = new ArrayUtil(); + int[] one = {0}; + int[] many = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println(Arrays.toString(au.removeZero(one))); + System.out.println(Arrays.toString(au.removeZero(many))); + System.out.println(Arrays.toString(au.removeZero(null))); + System.out.println(Arrays.toString(au.removeZero(new int[0]))); + } + + @Test + public void testMerge(){ + ArrayUtil au = new ArrayUtil(); + int[] arr1 = {3,4,5,6,7,8,9}; + int[] arr2 = {1,3,5,6,7,9,10,12,13}; + int[] arr3 = null; + int[] arr4 = new int[0]; + System.out.println(Arrays.toString(au.merge(arr1,arr2))); + System.out.println(Arrays.toString(au.merge(arr1,arr3))); + System.out.println(Arrays.toString(au.merge(arr2,arr4))); + } + + @Test + public void testGrow(){ + ArrayUtil au = new ArrayUtil(); + int[] arr = {3,4,5,6,7,8,9}; + System.out.println(Arrays.toString(au.grow(arr,5))); + } + + @Test + public void testFibonacci(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.fibonacci(15))); + } + + @Test + public void testPrimes(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.getPrimes(23))); + } + + @Test + public void testPerfectNumbers(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.getPerfectNumbers(23))); + } + + @Test + public void testJoin(){ + ArrayUtil au = new ArrayUtil(); + int[] array = {1,6,8,8,8,8,8,8,8,8,8,}; + System.out.println(au.join(array,"-")); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java new file mode 100644 index 0000000000..4b509b2102 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.bruce.homework0305.mystruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java new file mode 100644 index 0000000000..ab54fd0625 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java @@ -0,0 +1,81 @@ +package com.bruce.homework0305.mystruts; + +import com.sun.deploy.util.StringUtils; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException, + ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { + + /* + 0. 读取配置文件struts.xml + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + SAXReader reader = new SAXReader(); + Document document = reader.read(new File("src/com/bruce/homework0305/mystruts/struts.xml")); + Element root = document.getRootElement(); + Iterator actions = root.elementIterator("action"); + HashMap map = new HashMap<>(); + View view = new View(); + while(actions.hasNext()){ + Element next = actions.next(); + List attributes = next.attributes(); + Attribute action_name = next.attribute("name"); + if(actionName.equals(action_name.getValue())){ + //找到name="login"的action,拿到其class路径 + Attribute aClass = next.attribute("class"); + //通过反射拿到LoginAction类 + Class clazz = Class.forName(aClass.getValue()); + LoginAction login = (LoginAction) clazz.newInstance(); + //从parameters中拿到所有的key,通过这些key拿到对应的值,并且传入LoginAction对应的setter方法 + Set keys = parameters.keySet();parameters.entrySet(); + for(String key : keys){ + //首字母大写,拿到setter方法,并将parameters中对应该key的value拿出来,反射传入相应方法 + String setter = "set"+ key.substring(0,1).toUpperCase()+key.substring(1,key.length()); + Method method = clazz.getMethod(setter,String.class); + method.invoke(login,parameters.get(key)); + } + //反射拿到execute方法,并拿到执行结果 + Method execute = clazz.getMethod("execute"); + String result = (String) execute.invoke(login); + //反射拿到getMessage方法,结果以map格式保存,并保存在view对象中 + Method getMessage = clazz.getMethod("getMessage"); + String message = (String)getMessage.invoke(login); + map.put("message",message); + view.setParameters(map); + //根据execute方法的执行结果,找到相关result的jsp路径,将该路径保存在view对象中 + Iterator results = next.elementIterator("result"); + while(results.hasNext()){ + Element element = results.next(); + if(result.equals(element.attribute("name").getValue())){ + view.setJsp(element.getText()); + break; + } + } + } + } + return view; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java new file mode 100644 index 0000000000..e54809e4b6 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java @@ -0,0 +1,61 @@ +package com.bruce.homework0305.mystruts; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + try { + 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")); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + @Test + public void testLoginActionFailed() { + try { + 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")); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java new file mode 100644 index 0000000000..47abd77a37 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java @@ -0,0 +1,23 @@ +package com.bruce.homework0305.mystruts; + +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/group15/1510_739253131/src/com/bruce/homework0305/mystruts/struts.xml b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/struts.xml new file mode 100644 index 0000000000..41c7be3d2f --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/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/group15/1510_739253131/src/com/bruce/utils/MyException.java b/group15/1510_739253131/src/com/bruce/utils/MyException.java new file mode 100644 index 0000000000..2e2fa7833e --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/utils/MyException.java @@ -0,0 +1,29 @@ +package com.bruce.utils; + +/** + * Created by Bruce.Jiao on 15-6-23. + */ +public class MyException extends Exception{ + private int code; + private String message; + + public MyException() { + } + + public MyException(String message) { + this.message = message; + } + + public MyException(int code, String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } +} diff --git a/group15/1511_714512544/.idea/checkstyle-idea.xml b/group15/1511_714512544/.idea/checkstyle-idea.xml new file mode 100644 index 0000000000..9d5b48d873 --- /dev/null +++ b/group15/1511_714512544/.idea/checkstyle-idea.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/encodings.xml b/group15/1511_714512544/.idea/encodings.xml new file mode 100644 index 0000000000..cfca28230a --- /dev/null +++ b/group15/1511_714512544/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/misc.xml b/group15/1511_714512544/.idea/misc.xml new file mode 100644 index 0000000000..05483570e0 --- /dev/null +++ b/group15/1511_714512544/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/modules.xml b/group15/1511_714512544/.idea/modules.xml new file mode 100644 index 0000000000..0ed960e3bf --- /dev/null +++ b/group15/1511_714512544/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/uiDesigner.xml b/group15/1511_714512544/.idea/uiDesigner.xml new file mode 100644 index 0000000000..e96534fb27 --- /dev/null +++ b/group15/1511_714512544/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/vcs.xml b/group15/1511_714512544/.idea/vcs.xml new file mode 100644 index 0000000000..b2bdec2d71 --- /dev/null +++ b/group15/1511_714512544/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/.idea/workspace.xml b/group15/1511_714512544/.idea/workspace.xml new file mode 100644 index 0000000000..cb4932867a --- /dev/null +++ b/group15/1511_714512544/.idea/workspace.xml @@ -0,0 +1,1566 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + project + + + true + + + + DIRECTORY + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1487829138626 + + + 1487829266287 + + + 1487829432867 + + + 1487856987141 + + + 1487857271746 + + + 1487857304368 + + + 1488004359192 + + + 1488019343082 + + + 1488019461939 + + + 1488157251542 + + + 1488157483941 + + + 1488331339727 + + + 1488372676888 + + + 1488535940734 + + + 1488543482582 + + + 1488544609996 + + + 1488547243625 + + + 1488603987319 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + 1511_714512544 + + + + + + + + 1.8 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/1511_714512544.iml b/group15/1511_714512544/1511_714512544.iml new file mode 100644 index 0000000000..1868349e70 --- /dev/null +++ b/group15/1511_714512544/1511_714512544.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class new file mode 100644 index 0000000000..e84a8b4b40 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtilTest.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtilTest.class new file mode 100644 index 0000000000..f7b0cd1b22 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtilTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/LoginAction.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/LoginAction.class new file mode 100644 index 0000000000..d781f45ce9 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/LoginAction.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/Struts.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/Struts.class new file mode 100644 index 0000000000..02b3ad4da2 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/Struts.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/StrutsTest.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/StrutsTest.class new file mode 100644 index 0000000000..ab32147b2a Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/StrutsTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/View.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/View.class new file mode 100644 index 0000000000..e7a5112144 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/View.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/struts.xml b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group15/1511_714512544/out/production/1511_714512544/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/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$1.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$1.class new file mode 100644 index 0000000000..c4306e1489 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$1.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$ListIterator.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$ListIterator.class new file mode 100644 index 0000000000..1d423f2cc8 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$ListIterator.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList.class new file mode 100644 index 0000000000..b3a53ec4c1 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTree.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTree.class new file mode 100644 index 0000000000..5998f97032 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTree.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTreeNode.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTreeNode.class new file mode 100644 index 0000000000..2fbda2a306 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTreeNode.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Iterator.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Iterator.class new file mode 100644 index 0000000000..16c3f1aded Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Iterator.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$1.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$1.class new file mode 100644 index 0000000000..648241c965 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$1.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class new file mode 100644 index 0000000000..4097ca6433 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class new file mode 100644 index 0000000000..466c28ba4f Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class new file mode 100644 index 0000000000..7ac1a0fa80 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/List.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/List.class new file mode 100644 index 0000000000..82055051ce Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/List.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Queue.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Queue.class new file mode 100644 index 0000000000..60e7cea803 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Queue.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Stack.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Stack.class new file mode 100644 index 0000000000..c309e6c5e7 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Stack.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/ArrayListTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/ArrayListTest.class new file mode 100644 index 0000000000..435d12baf4 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/ArrayListTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/BinarySearchTreeTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/BinarySearchTreeTest.class new file mode 100644 index 0000000000..167f83c32c Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/BinarySearchTreeTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class new file mode 100644 index 0000000000..2b2fb7b768 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/QueueTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/QueueTest.class new file mode 100644 index 0000000000..fcd2281a17 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/QueueTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/StackTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/StackTest.class new file mode 100644 index 0000000000..1b08c84113 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/StackTest.class differ diff --git a/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java b/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..cd48c18e32 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,183 @@ +package com.coderising.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 len = origin.length; + int temp = 0; + for (int i = 0; i < len/2; i++) { + temp = origin[i]; + origin[i] = origin[len -1 -i]; + origin[len -1 -i] = 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 int[] removeZero(int[] oldArray){ + int[] temp = new int[oldArray.length]; + int index = 0; + for (int i : oldArray) { + if(i != 0){ + temp[index++] = i; + } + } + if(index == 0){ + return new int[]{}; + } + return Arrays.copyOf(temp, index); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int[] array3 = new int[array1.length+array2.length]; + for (int i = 0; i < array1.length; i++) { + array3[i] = array1[i]; + } + int index = array1.length; + for (int i = 0; i < array2.length; i++) { + boolean flag = true; //a2元素不在a3中 + for (int j = 0; j < index; j++) { + if(array3[j] == array2[i]){ + flag = false; + } + } + if(flag){ //a2元素不在a3中 + array3[index++] = array2[i]; + } + } + int[] temp = Arrays.copyOf(array3, index); + Arrays.sort(temp); + return temp; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return Arrays.copyOf(oldArray, oldArray.length+size); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if(max == 1) return new int[]{}; //max=1,返回空数组 + int[] arr = new int[max+1]; + arr[0] = 1; + arr[1] = 1; + + int sum = 2; + for (int i = 2; i < arr.length; i++) { + arr[i] = arr[i-1] + arr[i-2]; + if(arr[i] >= max){ + break; + } + sum ++; + } + return Arrays.copyOf(arr, sum); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max<=2) return new int[]{}; + + int[] temp = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + boolean flag = true; + for (int j = 2; j <= Math.sqrt(i); j++) { + if(i % j == 0){ + flag = false; + } + } + if(flag){temp[index++] = i;} + } + return Arrays.copyOf(temp, index); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 2) return new int[]{}; + + int[] temp = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + int sum = 0; + for (int j = 1; j <= Math.sqrt(i); j++) { + if(j == 1) { + sum += 1; + }else{ + if(i % j == 0){ + sum += j + i/j; + } + } + } + if(sum == i) temp[index++] = i; + } + return Arrays.copyOf(temp, index); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + if(array.length == 0){ + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i).append(seperator); + } + String temp = sb.toString(); + return temp.substring(0,temp.length()-1); + } + + +} \ No newline at end of file diff --git a/group15/1511_714512544/src/com/coderising/array/ArrayUtilTest.java b/group15/1511_714512544/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..939c5c7ac6 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +/** + * ArrayList测试 + */ +public class ArrayUtilTest { + private ArrayUtil util = null; + + @Before + public void setUp() throws Exception { + util = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + util = null; + } + + @Test + public void reverseArray() throws Exception { + int[] arr = new int[]{7, 9, 30, 3, 4}; + util.reverseArray(arr); + assertArrayEquals(new int[]{4,3, 30 , 9,7}, arr); + } + + @Test + public void removeZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArr = util.removeZero(oldArr); + assertArrayEquals(new int[]{1,3,4,5,6,6,5,4,7,6,7,5}, newArr); + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + assertArrayEquals(new int[]{3,4,5,6,7,8}, util.merge(a1, a2)); + } + + @Test + public void grow() throws Exception { + int[] oldArray = {2,3,6}; + int[] newArr = util.grow(oldArray, 3); + assertArrayEquals(new int[]{2,3,6,0,0,0}, newArr); + } + + @Test + public void fibonacci() throws Exception { + assertArrayEquals(new int[]{1,1,2,3,5,8,13}, util.fibonacci(15) ); + } + + @Test + public void getPrimes() throws Exception { + assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, util.getPrimes(23)); + } + + @Test + public void getPerfectNumbers() throws Exception { + assertArrayEquals(new int[]{6} , util.getPerfectNumbers(7)); + } + + @Test + public void join() throws Exception { + int[] array= {3,8,9}; + assertEquals("3-8-9" ,util.join(array, "-")); + } + +} \ No newline at end of file diff --git a/group15/1511_714512544/src/com/coderising/litestruts/LoginAction.java b/group15/1511_714512544/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group15/1511_714512544/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/group15/1511_714512544/src/com/coderising/litestruts/Struts.java b/group15/1511_714512544/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..8822339c00 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +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.Map; +import java.util.Set; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + /* + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 根据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + + try { + //0读取配置文件struts.xml + SAXReader reader = new SAXReader(); + Document doc = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root = doc.getRootElement(); //根元素 + Map action = new HashMap<>(); + Map loginResult = new HashMap<>(); + Map logoutResult = new HashMap<>(); + java.util.Iterator iterator = root.elementIterator("action"); + while(iterator.hasNext()){ + Element actionNode = (Element) iterator.next(); + String key = actionNode.attributeValue("name"); + String value = actionNode.attributeValue("class"); + action.put(key,value); + java.util.Iterator it = actionNode.elementIterator("result"); + while(it.hasNext()){ + Element resultNode = (Element) it.next(); + String k = resultNode.attributeValue("name"); + String v = resultNode.getText(); + if(key.equals("login")){ + loginResult.put(k,v); + }else { + logoutResult.put(k,v); + } + } + } + + //1 + String className = action.get(actionName); //获取类名 + Object o = Class.forName(className).newInstance(); //创建对象 + if(o instanceof LoginAction){ + LoginAction loginAction = (LoginAction) o; + Set> set = parameters.entrySet(); + for (Map.Entry en : set) { + if(en.getKey().equals("name")){ + loginAction.setName(en.getValue()); + }else if(en.getKey().equals("password")){ + loginAction.setPassword(en.getValue()); + } + } + + //2 + Class clazz = (Class) loginAction.getClass(); + Method m = clazz.getDeclaredMethod("execute",null); + m.setAccessible(true); + String message = (String) m.invoke(loginAction,null); + + //3 + View view = new View(); + Map params = new HashMap(); + Method[] ms = clazz.getDeclaredMethods(); + for (Method method : ms) { + method.setAccessible(true); + if(method.getName().equals("getName")){ + String value = (String) method.invoke(loginAction,null); + params.put("name",value); + }else if(method.getName().equals("getPassword")){ + String value = (String) method.invoke(loginAction,null); + params.put("password",value); + }else if(method.getName().equals("getMessage")){ + String value = (String) method.invoke(loginAction,null); + params.put("message",value); + } + } + view.setParameters(params); + + //4 + String jsp = loginResult.get(message); + view.setJsp(jsp); + + return view; + }else { + return null; + } + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + } + +} diff --git a/group15/1511_714512544/src/com/coderising/litestruts/StrutsTest.java b/group15/1511_714512544/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..d28d29c0e2 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +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/group15/1511_714512544/src/com/coderising/litestruts/View.java b/group15/1511_714512544/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1511_714512544/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/group15/1511_714512544/src/com/coderising/litestruts/struts.xml b/group15/1511_714512544/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group15/1511_714512544/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/group15/1511_714512544/src/com/coding/basic/ArrayList.java b/group15/1511_714512544/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..27911198d2 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/ArrayList.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + private int size = 0; //表内现有元素个数 + private Object[] elementData = new Object[2]; //数据结构--数组 + + public void add(Object o){ //在后面追加 + if(size == elementData.length) elementData = grow(elementData, 2* elementData.length); //先扩容 + elementData[size++] = o; + } + + public void add(int index, Object o){ //在指定位置插入 + if(size == elementData.length) elementData = grow(elementData, 2* elementData.length); //先扩容 + if(index > size || index < 0) throw new ArrayIndexOutOfBoundsException(); //保证最后一个元素后面也可以插 + System.arraycopy(elementData,index, elementData,index+1,size-index); + elementData[index] = o; + size ++; + } + + public Object get(int index){ //返回指定索引的元素 + if(index > size - 1 || index < 0) throw new ArrayIndexOutOfBoundsException(); + return elementData[index]; + } + + public Object remove(int index){ //删除指定索引处的元素 + if(index > size - 1 || index < 0) throw new ArrayIndexOutOfBoundsException(); + Object o = elementData[index]; + System.arraycopy(elementData,index+1, elementData,index,size-index-1); + elementData[size-1] = null; + size --; + return o; + } + + public int size(){ //number of elements + return size; + } + + public Iterator iterator(){ //迭代器 + return new ListIterator(); + } + + private class ListIterator implements Iterator{ //实例内部类 + private int i = 0; + + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public Object next() { + if(size() == 0) throw new NoSuchElementException("Stack Underflow"); + return elementData[i++]; + } + } + + //数组扩容 + private Object[] grow(Object[] src, int newLength){ + assert src.length < newLength; //断言 + return Arrays.copyOf(src,newLength); + } + +} diff --git a/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java b/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java new file mode 100644 index 0000000000..7f30ecd124 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java @@ -0,0 +1,318 @@ +package com.coding.basic; + +import edu.princeton.cs.algs4.BinarySearch; + +import java.util.Stack; + +/** + 二叉树 + 5 + / \ + 2 7 + / \ /\ +1 4 6 8 + + 1.前序遍历: 5 2 1 4 7 6 8 + 2.中序遍历: 1 2 4 5 6 7 8 + 3.后序遍历: 1 4 2 6 8 7 5 + */ +public class BinarySearchTree> { + private BinarySearchTreeNode root; //root node + + public BinarySearchTreeNode getRoot() { + return root; + } + + public void setRoot(BinarySearchTreeNode root) { + this.root = root; + } + + /** + * 二叉树插入节点 + * @param data 节点元素 + * @return 插入的节点 + */ + public BinarySearchTreeNode insert(T data){ + if(root == null){ + root = new BinarySearchTreeNode(data); + return root; + } + //root非空 + BinarySearchTreeNode current = root; + while(true){ + //当前节点的数据小于data + if(current.getData().compareTo(data) >= 0){ + if(current.getLeft() != null){ + current = current.getLeft(); + }else { + BinarySearchTreeNode child = new BinarySearchTreeNode(data); + current.setLeft(child); + child.setParent(current); + return child; + } + }else {//当前节点数据大于root + if(current.getRight() != null){ + current = current.getRight(); + }else { + BinarySearchTreeNode child = new BinarySearchTreeNode(data); + current.setRight(child ); + child.setParent(current); + return child; + } + } + } + } + + /** + * 查找元素data + * @param data 要查找的元素 + * @return 返回true找到、false未找到 + */ + public boolean contains(T data){ + if(root == null){ + return false; + } + BinarySearchTreeNode current = root; + while(true){ + if(current.getData().compareTo(data) > 0){ + if(current.getLeft() != null){ + current = current.getLeft(); + }else{ + return false; + } + }else if(current.getData().compareTo(data) < 0){ + if(current.getRight() != null){ + current = current.getRight(); + }else { + return false; + } + }else { + return true; + } + } + } + + /** + * 前序遍历递归实现 根节点 左子树 右子树 + * @param n 根节点 + */ + public void preOrder(BinarySearchTreeNode n){ + System.out.print(n.getData()+" "); + if(n.getLeft() != null){ + preOrder(n.getLeft()); + } + if(n.getRight() != null){ + preOrder(n.getRight()); + } + } + + /** + * 中序遍历递归实现 左子树 根节点 右子树 + * @param n 根节点 + */ + public void midOrder(BinarySearchTreeNode n){ + if(n.getLeft() != null){ + midOrder(n.getLeft()); + } + System.out.print(n.getData()+" "); + if(n.getRight() != null){ + midOrder(n.getRight()); + } + } + + /** + * 后序遍历递归实现 左子树 右子树 根节点 + * @param n 根节点 + */ + public void postOrder(BinarySearchTreeNode n){ + if(n.getLeft() != null){ + postOrder(n.getLeft()); + } + if(n.getRight() != null){ + postOrder(n.getRight()); + } + System.out.print(n.getData()+" "); + } + + /** + * 非递归实现前序遍历 + */ + public void preOrderWithoutRecursion(){ + if(root == null){ //根节点为空 + return; + } + + Stack> stack = new Stack<>(); //存放未执行完的节点 + stack.push(root); //首先push根节点 + BinarySearchTreeNode current = null; + + while(!stack.isEmpty()){ //栈内还有节点 + current = stack.peek(); //得到栈顶节点 + if(current.getState() == 0){ + System.out.print(current.getData() + " "); //打印数据 + current.setState(1); + }else if(current.getState() == 1){ + if(current.getLeft() != null){ + stack.push(current.getLeft()); + } + current.setState(2); //确认是否有左子树 + }else if(current.getState() == 2){ + if(current.getRight() != null){ + stack.push(current.getRight()); + } + current.setState(3); //确认是否有右子树 + }else if(current.getState() == 3){ + stack.pop(); //删除栈顶节点(该节点已经执行完所有程序) + current.setState(0); + } + } + } + + /** + * 非递归实现中序遍历 + */ + public void midOrderWithoutRecursion(){ + if(root == null){ //根节点为空 + return; + } + + Stack> stack = new Stack<>(); //存放未执行完的节点 + stack.push(root); //首先push根节点 + BinarySearchTreeNode current = null; + + while(!stack.isEmpty()){ //栈内还有节点 + current = stack.peek(); //得到栈顶节点 + if(current.getState() == 0){ + if(current.getLeft() != null){ + stack.push(current.getLeft()); //确认是否有左子树 + } + current.setState(1); + }else if(current.getState() == 1){ + System.out.print(current.getData() + " "); //打印数据 + current.setState(2); + }else if(current.getState() == 2){ + if(current.getRight() != null){ + stack.push(current.getRight()); + } + current.setState(3); //确认是否有右子树 + }else if(current.getState() == 3){ + stack.pop(); //删除栈顶节点 + current.setState(0); + } + } + } + + /** + * 非递归实现后序遍历 + */ + public void postOrderWithoutRecursion(){ + if(root == null){ //根节点为空 + return; + } + + Stack> stack = new Stack<>(); //存放未执行完的节点 + stack.push(root); //首先push根节点 + BinarySearchTreeNode current = null; + + while(!stack.isEmpty()){ //栈内还有节点 + current = stack.peek(); //得到栈顶节点 + if(current.getState() == 0){ + if(current.getLeft() != null){ + stack.push(current.getLeft()); + } + current.setState(1); + }else if(current.getState() == 1){ + if(current.getRight() != null){ + stack.push(current.getRight()); + } + current.setState(2); //确认是否有左子树 + }else if(current.getState() == 2){ + System.out.print(current.getData() + " "); //打印数据 + current.setState(3); //确认是否有右子树 + }else if(current.getState() == 3){ + stack.pop(); //删除栈顶节点 + current.setState(0); + } + } + } + + //删除某个节点n + public void delete(BinarySearchTreeNode n){ + BinarySearchTreeNode p = n.getParent(); //节点的父节点 + BinarySearchTreeNode child; //节点的子节点 + + //该节点没有任何子节点。// 叶子结点,直接删除即可。要考虑待删除结点是root的情况。 + if(n.getLeft()==null && n.getRight()==null){ + //该节点是根节点 + if(n == root){ + root = null; + return ; + } + //非根节点 + if(n == p.getLeft()){ + p.setLeft(null); + }else if(n == p.getRight()){ + p.setRight(null); + } + } + + // 内部结点,把它的后继的值拷进来,然后递归删除它的后继。 + else if(n.getLeft()!=null && n.getRight()!=null){ + BinarySearchTreeNode next = successor(n); //找到n的中序后继节点 + n.setData(next.getData()); + delete(next); //中序后继节点 + } + + //只有一个孩子的结点,把它的孩子交给它的父结点即可 + else { + if(n.getLeft() != null){ //得到子节点 + child = n.getLeft(); + }else { + child = n.getRight(); + } + + if(n == root){ // n是根节点的情况 + child.setParent(null); + root = child; + return; + } + //非根节点 + if(n == p.getLeft()){ + p.setLeft(child); + child.setParent(p); + }else{ + p.setRight(child); + child.setParent(p); + } + + } + } + + //找到n的中序后继节点 + public BinarySearchTreeNode successor(BinarySearchTreeNode n){ + if( n == null) return null; + if( n.getRight() == null ) return null; + return findMin(n.getRight()); + } + + //查找n树的最小值 + public BinarySearchTreeNode findMin(BinarySearchTreeNode n){ + BinarySearchTreeNode current = n; + while(current.getLeft() != null){ + current = current.getLeft(); + } + return current; + } + + //查找n树的最大值 + public BinarySearchTreeNode findMax(BinarySearchTreeNode n){ + BinarySearchTreeNode current = n; + while(current.getRight() != null){ + current = current.getRight(); + } + return current; + } + + + +} diff --git a/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java b/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java new file mode 100644 index 0000000000..323a040832 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +/** + * 二叉树BST结点 + */ +public class BinarySearchTreeNode{ + private T data; + private BinarySearchTreeNode parent; //父节点 + private BinarySearchTreeNode left; + private BinarySearchTreeNode right; + private int state; //递归状态(非递归遍历表示一个节点运行到的状态) + + public BinarySearchTreeNode(T data) { + this.data = data; + this.left = null; + this.right = null; + this.parent = null; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinarySearchTreeNode getLeft() { + return left; + } + + public void setLeft(BinarySearchTreeNode left) { + this.left = left; + } + + public BinarySearchTreeNode getRight() { + return right; + } + + public void setRight(BinarySearchTreeNode right) { + this.right = right; + } + + public int getState() { + return state; + } + + public void setState(int state) { + this.state = state; + } + public BinarySearchTreeNode getParent() { + return parent; + } + + public void setParent(BinarySearchTreeNode parent) { + this.parent = parent; + } +} diff --git a/group15/1511_714512544/src/com/coding/basic/Iterator.java b/group15/1511_714512544/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..b8c8e0ce6a --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group15/1511_714512544/src/com/coding/basic/LinkedList.java b/group15/1511_714512544/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e806c0e6a2 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/LinkedList.java @@ -0,0 +1,157 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private Node head; //首节点 + private int size; //节点个数 + + public void add(Object o){ //在链表尾部添加node + if(head == null){ + head = new Node(o, null); + }else { + Node last = head; + while(last.next != null){ + last = last.next; + } + last.next = new Node(o, null); + } + size ++; + } + + public void add(int index , Object o){ //在指定索引处插入node + if(index > size || index < 0) throw new RuntimeException("IndexOutOfBounds"); + if(head == null){ + head = new Node(o, null); + }else { + if(index == 0){ //插入位置在头部 + head = new Node(o, head); + }else { //后面位置插入 + Node temp = head; + int i = 0; + while(i != index - 1){ + temp = temp.next; + i ++; + } + Node tempNext = temp.next; + temp.next = new Node(o, tempNext); + } + } + size ++; + } + + public Object get(int index){ //取出指定节点处的元素,从0开始 + if(index > size -1 || index < 0) throw new RuntimeException("IndexOutOfBounds"); + int i = 0; + Node temp = head; + while(i != index){ + i ++; + temp = temp.next; + } + return temp.data; + } + + public Object remove(int index){ //删除指定索引处的节点 + if(index > size -1 || index < 0) throw new RuntimeException("IndexOutOfBounds"); + if(index == 0) { //第一个元素或只有一个元素 + Object o = head.data; + head = head.next; + size --; + return o; + }else { //其他元素 + int i = 0; + Node temp = head; //被删除节点之前的节点 + while(i != index - 1){ + i ++; + temp = temp.next; + } + Node delete = temp.next; //被删除的节点 + Object o = delete.data; + temp.next = delete.next; //删除 + size --; + return o; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ //在表头添加节点 + head = new Node(o, head); + size ++; + } + + public void addLast(Object o){ //在链表尾部添加节点 + if(head == null){ + head = new Node(o,null); + size ++; + return; + } + Node last = head; + while(last.next != null){ + last = last.next; + } + last.next = new Node(o, null); + size ++; + } + + public Object removeFirst(){ //在链表头部删除节点 + if(size() == 0) throw new RuntimeException("Underflow"); + Object o = head.data; + head = head.next; + size --; + return o; + } + + public Object removeLast(){ //在链表尾部删除节点 + if(size() == 0) throw new RuntimeException("Underflow"); + if(size() == 1){ + Object o = head.data; + head = null; + size --; + return o; + } + Node temp = head; + int i = 0; + while(i != size-2){ + temp = temp.next; + i ++; + } + Object o = temp.next.data; + temp.next = null; + size --; + return o; + } + + public Iterator iterator(){ //迭代器 + return new ListIterator(); + } + + private class ListIterator implements Iterator{ //实例内部类 + private Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Object next() { + if(size() == 0) throw new NoSuchElementException("Underflow"); + Object o = current.data; + current = current.next; + return o; + } + } + + //这里内部类须为static,在类级别上一一对应,非实例级别 + private static class Node{ + Object data; + Node next; + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group15/1511_714512544/src/com/coding/basic/List.java b/group15/1511_714512544/src/com/coding/basic/List.java new file mode 100644 index 0000000000..4cc4f6fe5b --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/List.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +/** + * List可以随机访问,不需要遍历 + */ +public interface List { + void add(Object o); + + void add(int index, Object o); + + Object get(int index); + + Object remove(int index); + + int size(); +} diff --git a/group15/1511_714512544/src/com/coding/basic/Queue.java b/group15/1511_714512544/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..00a69823b5 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/Queue.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * 队列,先进先出 + */ +public class Queue { + private LinkedList list = new LinkedList(); //链表数据结构 + + //入队 + public void enQueue(Object o){ + list.addLast(o); + } + + //出队 + public Object deQueue(){ + if(list.size() == 0) throw new NoSuchElementException("队列无元素"); + return list.removeFirst(); + } + + //是否为空 + public boolean isEmpty(){ + return list.size() == 0; + } + + //队列内元素 + public int size(){ + return list.size(); + } + + //迭代器 + public Iterator iterator(){ + return list.iterator(); + } + +} diff --git a/group15/1511_714512544/src/com/coding/basic/Stack.java b/group15/1511_714512544/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..d54b896356 --- /dev/null +++ b/group15/1511_714512544/src/com/coding/basic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; +//LIFO +public class Stack { + private ArrayList elementData = new ArrayList(); //使用刚实现的ArrayList + + //入栈 + public void push(Object o){ + elementData.add(o); + } + + //出栈 + public Object pop(){ + if(elementData.size() == 0) throw new NoSuchElementException("Stack Underflow"); + return elementData.remove(elementData.size()-1); + } + + //栈顶元素 + public Object peek(){ + if(elementData.size() == 0) throw new NoSuchElementException("Stack Underflow"); + return elementData.get(elementData.size()-1); + } + + //是否为空 + public boolean isEmpty(){ + return elementData.size() == 0; + } + + //栈内元素个数 + public int size(){ + return elementData.size(); + } + +} diff --git a/group15/1511_714512544/src/test/com/coding/basic/ArrayListTest.java b/group15/1511_714512544/src/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..8e6cc5a64d --- /dev/null +++ b/group15/1511_714512544/src/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,74 @@ +package test.com.coding.basic; + +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * ArrayList Test + */ +public class ArrayListTest { + @Test + public void add() throws Exception { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + assertEquals(3, list.size()); + } + + @Test + public void addByIndex() throws Exception { + ArrayList list = new ArrayList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + assertEquals(3,list.get(0)); + } + + @Test + public void get() throws Exception { + ArrayList list = new ArrayList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + assertEquals(3,list.get(0)); + } + + @Test + public void remove() throws Exception { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.remove(0); + assertEquals(2, list.get(0)); + } + + @Test + public void size() throws Exception { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + assertEquals(5, list.size()); + } + + @Test + public void iterator() throws Exception { + ArrayList list = new ArrayList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + +} \ No newline at end of file diff --git a/group15/1511_714512544/src/test/com/coding/basic/BinarySearchTreeTest.java b/group15/1511_714512544/src/test/com/coding/basic/BinarySearchTreeTest.java new file mode 100644 index 0000000000..3b7102e5cd --- /dev/null +++ b/group15/1511_714512544/src/test/com/coding/basic/BinarySearchTreeTest.java @@ -0,0 +1,113 @@ +package test.com.coding.basic; + +import static org.junit.Assert.*; + +import com.coding.basic.BinarySearchTree; +import org.junit.Test; + +public class BinarySearchTreeTest { + + @Test + public void testInsert() { + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + } + + @Test + public void testContains() { + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + assertEquals(true,bst.contains(8)); + } + + @Test + public void testPreOrder(){ + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.preOrder(bst.getRoot()); + } + + @Test + public void testPreOrderWithoutRecursion(){ + BinarySearchTree bst = new BinarySearchTree<>(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.preOrderWithoutRecursion(); + } + + @Test + public void testMidOrder(){ + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.midOrder(bst.getRoot()); + } + + @Test + public void testMidOrderWithoutRecursion(){ + BinarySearchTree bst = new BinarySearchTree<>(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.midOrderWithoutRecursion(); + } + + @Test + public void testPostOrder(){ + BinarySearchTree bst = new BinarySearchTree(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.postOrder(bst.getRoot()); + } + + @Test + public void testPostOrderWithoutRecursion(){ + BinarySearchTree bst = new BinarySearchTree<>(); + bst.insert(5); + bst.insert(2); + bst.insert(7); + bst.insert(1); + bst.insert(6); + bst.insert(4); + bst.insert(8); + bst.postOrderWithoutRecursion(); + } + +} diff --git a/group15/1511_714512544/src/test/com/coding/basic/LinkedListTest.java b/group15/1511_714512544/src/test/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..e5a2617247 --- /dev/null +++ b/group15/1511_714512544/src/test/com/coding/basic/LinkedListTest.java @@ -0,0 +1,149 @@ +package test.com.coding.basic; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * LinkedList Test + */ +public class LinkedListTest { + @Test + public void add() throws Exception { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + assertEquals(3,list.size()); + } + + @Test + public void addByIndex() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.println(iterator.next()); + } + assertEquals(3, list.get(0)); + } + + @Test + public void get() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + assertEquals(3,list.get(0)); + } + + @Test + public void remove() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + list.add(3,4); + list.add(4,5); + list.remove(0); + System.out.println(list.size()); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void size() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + list.add(3,4); + list.add(4,5); + assertEquals(5, list.size()); + } + + @Test + public void addFirst() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + list.add(3,4); + list.add(4,5); + list.addFirst(0); + System.out.println(list.size()); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void addLast() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + list.add(3,4); + list.add(4,5); + list.addLast(0); + System.out.println(list.size()); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void removeFirst() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + list.add(3,4); + list.add(4,5); + list.removeFirst(); + System.out.println(list.size()); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void removeLast() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + list.add(3,4); + list.add(4,5); + list.removeLast(); + System.out.println(list.size()); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + + @Test + public void iterator() throws Exception { + LinkedList list = new LinkedList(); + list.add(0,1); + list.add(1,2); + list.add(0,3); + list.add(3,4); + list.add(4,5); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + +} \ No newline at end of file diff --git a/group15/1511_714512544/src/test/com/coding/basic/QueueTest.java b/group15/1511_714512544/src/test/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..597966d62e --- /dev/null +++ b/group15/1511_714512544/src/test/com/coding/basic/QueueTest.java @@ -0,0 +1,67 @@ +package test.com.coding.basic; + +import com.coding.basic.Iterator; +import com.coding.basic.Queue; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * QueueTest + */ +public class QueueTest { + @Test + public void enQueue() throws Exception { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + queue.enQueue(4); + assertEquals(1, queue.deQueue()); + } + + @Test + public void deQueue() throws Exception { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + queue.enQueue(4); + assertEquals(1, queue.deQueue()); + } + + @Test + public void isEmpty() throws Exception { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + queue.enQueue(4); + assertEquals(false, queue.isEmpty()); + } + + @Test + public void size() throws Exception { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + queue.enQueue(4); + assertEquals(4 , queue.size()); + } + + @Test + public void iterator() throws Exception { + Queue queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(3); + queue.enQueue(4); + queue.enQueue(4); + Iterator iterator = queue.iterator(); + while(iterator.hasNext()){ + System.out.print(iterator.next() + " "); + } + } + +} \ No newline at end of file diff --git a/group15/1511_714512544/src/test/com/coding/basic/StackTest.java b/group15/1511_714512544/src/test/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..96eabc523d --- /dev/null +++ b/group15/1511_714512544/src/test/com/coding/basic/StackTest.java @@ -0,0 +1,59 @@ +package test.com.coding.basic; + +import com.coding.basic.Stack; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Stack Test + */ +public class StackTest { + @Test + public void push() throws Exception { + Stack stack = new Stack(); + stack.push(1); + assertEquals(1,stack.pop()); + } + + @Test + public void pop() throws Exception { + Stack stack = new Stack(); + stack.push(1); + assertEquals(1,stack.pop()); + } + + @Test + public void peek() throws Exception { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + stack.push(5); + assertEquals(5,stack.peek()); + } + + @Test + public void isEmpty() throws Exception { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + stack.push(5); + assertEquals(false,stack.isEmpty()); + } + + @Test + public void size() throws Exception { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + stack.push(5); + assertEquals(5,stack.size()); + } + +} \ No newline at end of file diff --git "a/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" "b/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" new file mode 100644 index 0000000000..3a34a9a3fb --- /dev/null +++ "b/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" @@ -0,0 +1,3 @@ +(1)介绍CPU,内存,硬盘,指令以及他们之间的关系的文章地址:http://www.jianshu.com/p/f86ca5072c5d + +(2)程序的机器及表示: http://www.jianshu.com/p/1eed6fe682cd diff --git a/group15/1512_656512403/.idea/compiler.xml b/group15/1512_656512403/.idea/compiler.xml new file mode 100644 index 0000000000..217af471a9 --- /dev/null +++ b/group15/1512_656512403/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/group15/1512_656512403/.idea/copyright/profiles_settings.xml b/group15/1512_656512403/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000000..e7bedf3377 --- /dev/null +++ b/group15/1512_656512403/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/group15/1512_656512403/.idea/description.html b/group15/1512_656512403/.idea/description.html new file mode 100644 index 0000000000..db5f129556 --- /dev/null +++ b/group15/1512_656512403/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/group15/1512_656512403/.idea/encodings.xml b/group15/1512_656512403/.idea/encodings.xml new file mode 100644 index 0000000000..e206d70d85 --- /dev/null +++ b/group15/1512_656512403/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/group15/1512_656512403/.idea/inspectionProfiles/Project_Default.xml b/group15/1512_656512403/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000000..e9844ba05b --- /dev/null +++ b/group15/1512_656512403/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/group15/1512_656512403/.idea/inspectionProfiles/profiles_settings.xml b/group15/1512_656512403/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000..3b312839bf --- /dev/null +++ b/group15/1512_656512403/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/group15/1512_656512403/.idea/misc.xml b/group15/1512_656512403/.idea/misc.xml new file mode 100644 index 0000000000..1b063fd81b --- /dev/null +++ b/group15/1512_656512403/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group15/1512_656512403/.idea/modules.xml b/group15/1512_656512403/.idea/modules.xml new file mode 100644 index 0000000000..a2753a29d2 --- /dev/null +++ b/group15/1512_656512403/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group15/1512_656512403/.idea/vcs.xml b/group15/1512_656512403/.idea/vcs.xml new file mode 100644 index 0000000000..def6a6a184 --- /dev/null +++ b/group15/1512_656512403/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group15/1512_656512403/.idea/workspace.xml b/group15/1512_656512403/.idea/workspace.xml new file mode 100644 index 0000000000..49276d46a4 --- /dev/null +++ b/group15/1512_656512403/.idea/workspace.xml @@ -0,0 +1,768 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1487995674848 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group15/1512_656512403/1512_656512403.iml b/group15/1512_656512403/1512_656512403.iml new file mode 100644 index 0000000000..d5c0743275 --- /dev/null +++ b/group15/1512_656512403/1512_656512403.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/group15/1512_656512403/1512_656512403.md b/group15/1512_656512403/1512_656512403.md new file mode 100644 index 0000000000..08b657df23 --- /dev/null +++ b/group15/1512_656512403/1512_656512403.md @@ -0,0 +1 @@ +//这是1512深的文件夹 diff --git a/group15/1512_656512403/src/Iterator.java b/group15/1512_656512403/src/Iterator.java new file mode 100644 index 0000000000..4af68013b5 --- /dev/null +++ b/group15/1512_656512403/src/Iterator.java @@ -0,0 +1,7 @@ +/** + * Created by wangtiegang on 2017/2/25. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group15/1512_656512403/src/List.java b/group15/1512_656512403/src/List.java new file mode 100644 index 0000000000..c952972cea --- /dev/null +++ b/group15/1512_656512403/src/List.java @@ -0,0 +1,10 @@ +/** + * Created by wangtiegang on 2017/2/25. + */ +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/group15/1512_656512403/src/Main.java b/group15/1512_656512403/src/Main.java new file mode 100644 index 0000000000..db9b7162b6 --- /dev/null +++ b/group15/1512_656512403/src/Main.java @@ -0,0 +1,7 @@ +public class Main { + + public static void main(String[] args) { + System.out.println("Hello World!"); + + } +} diff --git a/group15/1512_656512403/src/MyArrayList.java b/group15/1512_656512403/src/MyArrayList.java new file mode 100644 index 0000000000..125cbfe052 --- /dev/null +++ b/group15/1512_656512403/src/MyArrayList.java @@ -0,0 +1,198 @@ +import java.io.Serializable; +import java.util.Collection; + +/** + * Created by wangtiegang on 2017/2/18. + */ +public class MyArrayList implements List,Serializable{ + private int size; + private Object[] elementData; + + //空构造方法,初始容量10 + public MyArrayList(){ + this(10); + } + + //构造指定容量的空列表 + public MyArrayList(int size){ + this.elementData = new Object[size]; + } + + //构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。 + public MyArrayList(Collection collection){ + this.size = collection.size(); + this.elementData = new Object[collection.size()]; + //复制元素到数组中 + } + + //添加值到列表尾 + public void add(Object obj){ + //先判断长度够不够 + this.ensureCapacity(size+1); + //添加到数组 + elementData[size++] = obj; + } + + //添加值到指定位置 + public void add(int index,Object obj){ + this.ensureCapacity(size+1); + //添加元素到指定位置,将所有指定位置开始的值都往后移一位 + for(int i = index;i=0;i--){ + if(obj.equals(elementData[i])){ + return i; + } + } + return -1; + } + + //没有元素则返回true + public boolean isEmpty(){ + return size == 0 ? true : false; + } + + //remove指定位置元素 + public Object remove(int index){ + if(index > size){ + throw new IndexOutOfBoundsException(); + }else { + Object removeObj = elementData[index]; + for(int i=index;i size) || (toIndex < 0 || toIndex > size) ){ + throw new IndexOutOfBoundsException(); + }else{ + if(fromIndex < toIndex){ + for(int i = fromIndex ; i < size ; i++){ + if(toIndex + 1 <= size){ + elementData[i] = elementData[toIndex+1]; + }else{ + elementData[i] = null; + } + } + size = size - (toIndex - fromIndex) - 1; + }else if(fromIndex > toIndex){ + for(int i = toIndex ; i < size ; i++){ + if(fromIndex + 1 <= size){ + elementData[i] = elementData[fromIndex+1]; + }else{ + elementData[i] = null; + } + } + size = size - (fromIndex - toIndex) - 1; + } + } + } + + //替换指定位置元素 + public Object set(int index,Object obj){ + if(index > size || index < 0){ + throw new IndexOutOfBoundsException(); + }else { + Object oldObj = elementData[index]; + elementData[index] = obj; + return oldObj; + } + } + + public Object[] toArray(){ + Object[] arr = new Object[elementData.length]; + for(int i = 0 ; i elementData.length){ + Object[] oldData = elementData; + int newCapacity = (elementData.length*3)/2 + 1; + if(newCapacity < minCapacity){ + newCapacity = minCapacity; + } + Object[] newData = new Object[newCapacity]; + //复制元素到新的数组中 + elementData = this.copyArray(elementData,newCapacity); + } + } + + public Object[] copyArray(Object[] arr,int newCapacity){ + Object[] newArray = new Object[newCapacity]; + + for(int i = 0 ; i < arr.length ; i++){ + newArray[i] = arr[i]; + } + + return newArray; + } + + public int size(){ + return this.size; + } + + public Object get(int index){ + return elementData[index]; + } + +} diff --git a/group15/1512_656512403/src/MyBinaryTreeNode.java b/group15/1512_656512403/src/MyBinaryTreeNode.java new file mode 100644 index 0000000000..186a4c182f --- /dev/null +++ b/group15/1512_656512403/src/MyBinaryTreeNode.java @@ -0,0 +1,27 @@ +/** + * Created by wangtiegang on 2017/2/25. + */ +public class MyBinaryTreeNode { + private Node root; + + private static class Node{ + Object data; + Node right; + Node left; + + public Node(Object obj,Node right,Node left){ + this.data = obj; + this.right = right; + this.left = left; + } + } + + public Object insert(Object o){ + if(root == null){ + root = new Node(o,null,null); + }else{ + + } + return null; + } +} diff --git a/group15/1512_656512403/src/MyLinkedList.java b/group15/1512_656512403/src/MyLinkedList.java new file mode 100644 index 0000000000..30dda78d17 --- /dev/null +++ b/group15/1512_656512403/src/MyLinkedList.java @@ -0,0 +1,119 @@ +/** + * Created by wangtiegang on 2017/2/25. + */ +public class MyLinkedList implements List { + //当前size + private int size; + //第一个节点 + private Node firstNode; + //最后一个节点 + private Node lastNode; + + + //构造存放数据及指针的Node + private static class Node{ + E data; + Node preNode; + Node nextNode; + + public Node(E data,Node preNode,Node nextNode){ + this.data = data; + this.preNode = preNode; + this.nextNode = nextNode; + } + } + + @Override + public void add(Object o) { + + //将最后一个node放到preNode + Node node = new Node(o,lastNode,null); + //将最后一个node的nextNode设置成o + if(lastNode != null){ + lastNode.nextNode = node; + } + + lastNode = node; + + if(firstNode == null){ + firstNode = node; + } + //size增加 + size++; + } + + @Override + public void add(int index, Object o) { + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException(); + } + + if(index == size){ + this.add(o); + }else { + //找到index个 + Node node = firstNode; + for(int i = 0;i= size){ + throw new IndexOutOfBoundsException(); + } + Node node = firstNode; + for(int i = 0;i= size){ + throw new IndexOutOfBoundsException(); + } + + //找到index个 + Node node = firstNode; + for(int i = 0;i + + + + + diff --git a/group15/1513_121469918/HomeWork01/.gitignore b/group15/1513_121469918/HomeWork01/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group15/1513_121469918/HomeWork01/.project b/group15/1513_121469918/HomeWork01/.project new file mode 100644 index 0000000000..2865c24f37 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/.project @@ -0,0 +1,17 @@ + + + HomeWork01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs b/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..bcb09d4d64 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +encoding//src/coding/ArrayList.java=GBK +encoding//src/coding/BinaryTreeNode.java=GBK +encoding//src/coding/Iterator.java=GBK +encoding//src/coding/LinkedList.java=GBK +encoding//src/coding/List.java=GBK +encoding//src/coding/Queue.java=GBK +encoding//src/coding/Stack.java=GBK diff --git a/group15/1513_121469918/HomeWork01/.settings/org.eclipse.jdt.core.prefs b/group15/1513_121469918/HomeWork01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group15/1513_121469918/HomeWork01/src/coding/ArrayList.java b/group15/1513_121469918/HomeWork01/src/coding/ArrayList.java new file mode 100644 index 0000000000..f85c800084 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/src/coding/ArrayList.java @@ -0,0 +1,118 @@ +package coding; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + int len = size + 1; + // жlistijǷ + if (len > elementData.length) { + // + Object[] newElemDate = new Object[elementData.length + 1]; + // ƾԪص + System.arraycopy(elementData, 0, newElemDate, 0, elementData.length); + elementData = newElemDate; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + // ǷԽ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + // Ԫصĩβֱӵadd + if (index == size) { + add(o); + } else { + // + Object[] newElemData = new Object[elementData.length + 1]; + // indexǰԪص + System.arraycopy(elementData, 0, newElemData, 0, index); + newElemData[index] = o; + // index ԺԪص + System.arraycopy(elementData, index, newElemData, index + 1, size - index); + + elementData = newElemData; + size++; + } + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Object removeElement = elementData[index]; + //һԪصֵҪ + if(index != (size-1)){ + // + Object[] newElemData = new Object[elementData.length]; + // indexǰԪص + System.arraycopy(elementData, 0, newElemData, 0, index); + // index ԺԪص + System.arraycopy(elementData, index+1, newElemData, index, size - index -1); + } + //һԪصֱֵӼlist + size--; + return removeElement; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(this); + } + + private class MyIterator implements Iterator { + private int poi = -1; + private ArrayList array = null; + + private MyIterator(ArrayList array) { + this.array = array; + } + + @Override + public boolean hasNext() { + return (poi + 1) < array.size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + poi++; + if (poi >= array.size) { + poi--; + throw new IndexOutOfBoundsException(); + } + + return array.get(poi); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (poi < 0) { + throw new NoSuchElementException(); + } + Object val = array.remove(poi); + poi--; + return val; + } + + } +} diff --git a/group15/1513_121469918/HomeWork01/src/coding/BinaryTreeNode.java b/group15/1513_121469918/HomeWork01/src/coding/BinaryTreeNode.java new file mode 100644 index 0000000000..8e40fa1d90 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/src/coding/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package coding; + +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 (data == null) { + setData(o); + } else { + Integer i = (Integer) o; + // ǰڵжҽڵ + if (i.compareTo((Integer) data) == -1) { + if(right == null) + right = new BinaryTreeNode(); + return right.insert(i); + } else if (i.compareTo((Integer) data) == 1) { + if(left == null) + left = new BinaryTreeNode(); + return left.insert(i); + } + return null; + } + return null; + } + +} diff --git a/group15/1513_121469918/HomeWork01/src/coding/Iterator.java b/group15/1513_121469918/HomeWork01/src/coding/Iterator.java new file mode 100644 index 0000000000..26ca2a672a --- /dev/null +++ b/group15/1513_121469918/HomeWork01/src/coding/Iterator.java @@ -0,0 +1,7 @@ +package coding; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public Object remove(); +} diff --git a/group15/1513_121469918/HomeWork01/src/coding/LinkedList.java b/group15/1513_121469918/HomeWork01/src/coding/LinkedList.java new file mode 100644 index 0000000000..5d15f141f7 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/src/coding/LinkedList.java @@ -0,0 +1,170 @@ +package coding; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private Node head; + private int size; + + public void add(Object o) { + // жͷǷ + if (head == null) { + head = new Node(o, null); + } else { + Node newNode = head; + while (newNode.next != null) { + newNode = newNode.next; + } + newNode.next = new Node(o, null); + } + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Node node = head; + if (index != 0) { + // ǵһֵҵֵǰһڵ + for (int i = 1; i < index; i++) { + node = node.next; + } + Node newNode = new Node(o, node.next); + node.next = newNode; + size++; + } else { + // һֵͽͷڵָ + Node newNode = new Node(o, head); + head = newNode; + size++; + } + } + + public Object get(int index) { + indexCheck(index); + Node node = head; + for (int i = 1; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + indexCheck(index); + + Node node = head; + Node removeNode; + if (index == 0) { + //ɾһڵͰͷڵָԭĵڶڵ + removeNode = head; + head = head.next; + } else { + //ҵֵǰһڵ + for (int i = 1; i < index; i++) { + node = node.next; + } + removeNode = node.next; + //ǰһڵָ룬ָɾڵָĽڵ + node.next = removeNode.next; + } + size--; + return removeNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, head.next); + head.next = newNode; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + //ûԪؾ쳣 + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Object val = head.data; + head = head.next; + size--; + return val; + } + + public Object removeLast() { + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Node node = head; + while (node.next != null) { + node = node.next; + } + Object val = node.data; + node = null; + size--; + return val; + } + + public Iterator iterator() { + return new MyIterator(this); + } + + private class MyIterator implements Iterator{ + private int poi = -1; + private LinkedList list ; + private MyIterator(LinkedList list) { + this.list= list; + } + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return (poi + 1) < list.size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + poi++; + if (poi >= list.size) { + poi--; + throw new IndexOutOfBoundsException(); + } + + return list.get(poi); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (poi < 0) { + throw new NoSuchElementException(); + } + Object val = list.removeLast(); + poi--; + return val; + } + + } + + private void indexCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group15/1513_121469918/HomeWork01/src/coding/List.java b/group15/1513_121469918/HomeWork01/src/coding/List.java new file mode 100644 index 0000000000..8b85bc5b37 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/src/coding/List.java @@ -0,0 +1,9 @@ +package coding; + +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/group15/1513_121469918/HomeWork01/src/coding/Queue.java b/group15/1513_121469918/HomeWork01/src/coding/Queue.java new file mode 100644 index 0000000000..f4b6faaa8a --- /dev/null +++ b/group15/1513_121469918/HomeWork01/src/coding/Queue.java @@ -0,0 +1,35 @@ +package coding; + +import java.util.NoSuchElementException; + +public class Queue { + private int size; + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o);; + size++; + } + + public Object deQueue(){ + if(size<=0){ + throw new NoSuchElementException(); + } + Object val = list.removeFirst(); + size--; + return val; + } + + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + + public int size(){ + return size; + } + +} diff --git a/group15/1513_121469918/HomeWork01/src/coding/Stack.java b/group15/1513_121469918/HomeWork01/src/coding/Stack.java new file mode 100644 index 0000000000..742a6c4e40 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/src/coding/Stack.java @@ -0,0 +1,41 @@ +package coding; + +import java.util.NoSuchElementException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + Object val = elementData.remove(len); + size--; + return val; + } + + public Object peek(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + return elementData.get(len); + } + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + public int size(){ + return size; + } +} diff --git a/group15/1513_121469918/HomeWork20170305/.classpath b/group15/1513_121469918/HomeWork20170305/.classpath new file mode 100644 index 0000000000..a851141e9a --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group15/1513_121469918/HomeWork20170305/.project b/group15/1513_121469918/HomeWork20170305/.project new file mode 100644 index 0000000000..0e4fe1fc91 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/.project @@ -0,0 +1,17 @@ + + + HomeWork20170305 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..67f156c482 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +encoding//src/com/coderising/array/ArrayUtil.java=UTF-8 +encoding//src/com/coding/basic/ArrayList.java=GBK +encoding//src/com/coding/basic/BinaryTreeNode.java=GBK +encoding//src/com/coding/basic/Iterator.java=GBK +encoding//src/com/coding/basic/LinkedList.java=GBK +encoding//src/com/coding/basic/List.java=GBK +encoding//src/com/coding/basic/Queue.java=GBK +encoding//src/com/coding/basic/Stack.java=GBK diff --git a/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.jdt.core.prefs b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group15/1513_121469918/HomeWork20170305/bin/com/coderising/litestruts/struts.xml b/group15/1513_121469918/HomeWork20170305/bin/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a7cb57e188 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/bin/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/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..985ca47709 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,281 @@ +package com.coderising.array; + + +import java.util.Arrays; +import java.util.TreeSet; +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int len = origin.length; + for (int i = 0; i < len / 2; i++) { + int temp = origin[i]; + origin[i] = origin[len - 1 - i]; + origin[len - 1 - i] = 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 int[] removeZero(int[] oldArray) { + int count = 0; + + // 创建一个临时数组装没有零的旧数组 + int[] temp = new int[oldArray.length - count]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + // 如果值为0,统计、跳过不加入新数组 + count++; + continue; + } else { + temp[i - count] = oldArray[i]; + } + } + // 定义返回数组的长度 + int len = oldArray.length - count; + int[] resultArray = new int[len]; + System.arraycopy(temp, 0, resultArray, 0, len); + return resultArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { +/* int[] result = array1; + // 去除重复元素 + for (int i = 0; i < array2.length; i++) { + boolean sameVal = false; + for (int j = 0; j < array1.length; j++) { + if (array1[j] == array2[i]) { + sameVal = true; + break; + } + } + if(sameVal == false){ + result = grow(result, 1); + result[result.length-1] = array2[i]; + } + } + //冒泡排序 + for (int i = 0; i < result.length-1; i++) { + for (int j = i+1; j < result.length; j++) { + if(result[i]>result[j]){ + int temp = result[i]; + result[i] = result[j]; + result[j] =temp; + } + } + } + return result; +*/ + int len1=0,len2=0;//arr1长度len1 + ArrayList list = new ArrayList(); + for (int k=0;k < array1.length+array2.length; k++) { + //如果两个数组都还有元素 + if(len1array2[len2]){ + list.add(array2[len2]); + len2++; + }else{ + list.add(array1[len1]); + len1++; + len2++; + } + }else if(len1==array1.length && len2 < array2.length){ + //如果数组1没有元素,并且2有元素 + list.add(array2[len2]); + len2++; + }else if(len2==array2.length && len1 < array1.length){ + //数组2没有元素,并且1有元素 + list.add(array1[len1]); + len1++; + }else{ + break; + } + } + //list转数组 + int[] result = new int[list.size()]; + Iterator it = list.iterator(); + int index = 0; + while(it.hasNext()){ + result[index++] = ((Integer)it.next()).intValue(); + } + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] resultArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, resultArray, 0, oldArray.length); + return resultArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } else { + int[] temp = new int[max]; + temp[0] = 1; + temp[1] = 1; + // 定义返回数组的长度变量 + int len = 2; + for (int i = 2; i < max; i++) { + int last = temp[i - 1] + temp[i - 2]; + if (last >= max) { + break; + } else { + temp[i] = last; + len = i + 1; + } + } + return Arrays.copyOf(temp, len); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + + if (max <= 1) { + return new int[0]; + } else { + // 创建临时数组 + int[] temp = new int[max]; + int count = 0; + // 从零开始遍历到max,如果有是素数就加入临时数组。 + for (int i = 0; i < max; i++) { + if (isPrimes(i)) { + temp[i - count] = i; + } else { + count++; + } + } + // max -1 -count是最后一个元素索引 + int len = max - count; + int[] resultArray = Arrays.copyOf(temp, len); + return resultArray; + } + } + + boolean isPrimes(int x) { + if (x <= 1) { + return false; + } else { + for (int i = 2; i < x; i++) { + if (x % i == 0) { + return false; + } + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max < 1) { + return new int[0]; + } else { + ArrayList array = new ArrayList(); + for (int i = 1; i <= max; i++) { + if (isPerfectNumber(i)) { + array.add(i); + } + } + int[] result = new int[array.size()]; + Iterator it = array.iterator(); + int index = 0; + while (it.hasNext()) { + result[index] = ((Integer) it.next()).intValue(); + index++; + } + return result; + } + } + + boolean isPerfectNumber(int x) { + if (x < 1) { + return false; + } else { + int count = 0; + for (int i = 1; i < x; i++) { + if (x % i == 0) { + count += i; + } + } + if (x == count) { + return true; + } + return false; + } + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + if(i == array.length-1){ + sb.append(String.valueOf(array[i])); + break; + } + sb.append(String.valueOf(array[i])+seperator); + } + String result = sb.toString(); + return result; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/LoginAction.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/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/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..a0dd465d25 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,116 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +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) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + + + //读取配置文件 + try { + Document doc =new SAXReader().read(new File("./src/com/coderising/litestruts/struts.xml")); + //获取XML中的action标签 + Element loginName = (Element)doc.selectSingleNode("//action[1]"); + Element logoutName = (Element)doc.selectSingleNode("//action[2]"); + //判断action的name属性内容 + if(actionName.equals(loginName.attributeValue("name"))){ + //传入的actionName内容为login则进行login操作 + + //获取class路径 + String s = loginName.attributeValue("class"); + Class c = Class.forName(s); + Constructor con = c.getConstructor(); + //实例化LoginAction + LoginAction login =(LoginAction) con.newInstance(); + + //通过parameters获取name,password + String executeName = parameters.get("name"); + String executePassword = parameters.get("password"); + + //获取 setter 方法,并调用 + Method m = c.getMethod("setName", String.class); + m.invoke(login, executeName); + m =c.getMethod("setPassword", String.class); + m.invoke(login, executePassword); + + //调用LoginAction 的exectue 方法 exectueResult值为:帐号密码正确返回success,反之为fail + m = c.getMethod("execute"); + String exectueResult = (String)m.invoke(login); + + //获取 getMessage 方法 + m = c.getMethod("getMessage"); + String message =(String) m.invoke(login); + m = c.getMethod("getName"); + String name =(String) m.invoke(login); + m = c.getMethod("getPassword"); + String password =(String) m.invoke(login); + + //创建HashMap,将登录操作返回的3个变量放到Map + HashMap hm = new HashMap(); + hm.put("message", message); + hm.put("name", name); + hm.put("password", password); + + //创建View对象 + View view = new View(); + view.setParameters(hm); + + //读取struts.xml中的 配置 + List list =(List) doc.selectNodes("//action[@name = 'login']/result"); + for(Element e : list){ + String resultName = e.attributeValue("name"); + //exectue返回值比对XML下result 对应的值返回对应的jsp + if(resultName.equals(exectueResult)){ + //将对应的jsp 放到view对象中 + view.setJsp(e.getText()); + } + } + return view; + + }else if(actionName.equals(logoutName.attributeValue("name"))){ + //actionName是logout则进行logout操作 + } + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/StrutsTest.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/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/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/View.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/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/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/struts.xml b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a7cb57e188 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/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/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..951b1ce0e9 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java @@ -0,0 +1,118 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + int len = size + 1; + // жlistijǷ + if (len > elementData.length) { + // + Object[] newElemDate = new Object[elementData.length + 1]; + // ƾԪص + System.arraycopy(elementData, 0, newElemDate, 0, elementData.length); + elementData = newElemDate; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + // ǷԽ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + // Ԫصĩβֱӵadd + if (index == size) { + add(o); + } else { + // + Object[] newElemData = new Object[elementData.length + 1]; + // indexǰԪص + System.arraycopy(elementData, 0, newElemData, 0, index); + newElemData[index] = o; + // index ԺԪص + System.arraycopy(elementData, index, newElemData, index + 1, size - index); + + elementData = newElemData; + size++; + } + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Object removeElement = elementData[index]; + //һԪصֵҪ + if(index != (size-1)){ + // + Object[] newElemData = new Object[elementData.length]; + // indexǰԪص + System.arraycopy(elementData, 0, newElemData, 0, index); + // index ԺԪص + System.arraycopy(elementData, index+1, newElemData, index, size - index -1); + } + //һԪصֱֵӼlist + size--; + return removeElement; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(this); + } + + private class MyIterator implements Iterator { + private int poi = -1; + private ArrayList array = null; + + private MyIterator(ArrayList array) { + this.array = array; + } + + @Override + public boolean hasNext() { + return (poi + 1) < array.size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + poi++; + if (poi >= array.size) { + poi--; + throw new IndexOutOfBoundsException(); + } + + return array.get(poi); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (poi < 0) { + throw new NoSuchElementException(); + } + Object val = array.remove(poi); + poi--; + return val; + } + + } +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b3a1ee65b6 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,53 @@ +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 (data == null) { + setData(o); + } else { + Integer i = (Integer) o; + // ǰڵжҽڵ + if (i.compareTo((Integer) data) == -1) { + if(right == null) + right = new BinaryTreeNode(); + return right.insert(i); + } else if (i.compareTo((Integer) data) == 1) { + if(left == null) + left = new BinaryTreeNode(); + return left.insert(i); + } + return null; + } + return null; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..f5cf74673d --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public Object remove(); +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d8f759fdcb --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private Node head; + private int size; + + public void add(Object o) { + // жͷǷ + if (head == null) { + head = new Node(o, null); + } else { + Node newNode = head; + while (newNode.next != null) { + newNode = newNode.next; + } + newNode.next = new Node(o, null); + } + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Node node = head; + if (index != 0) { + // ǵһֵҵֵǰһڵ + for (int i = 1; i < index; i++) { + node = node.next; + } + Node newNode = new Node(o, node.next); + node.next = newNode; + size++; + } else { + // һֵͽͷڵָ + Node newNode = new Node(o, head); + head = newNode; + size++; + } + } + + public Object get(int index) { + indexCheck(index); + Node node = head; + for (int i = 1; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + indexCheck(index); + + Node node = head; + Node removeNode; + if (index == 0) { + //ɾһڵͰͷڵָԭĵڶڵ + removeNode = head; + head = head.next; + } else { + //ҵֵǰһڵ + for (int i = 1; i < index; i++) { + node = node.next; + } + removeNode = node.next; + //ǰһڵָ룬ָɾڵָĽڵ + node.next = removeNode.next; + } + size--; + return removeNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, head.next); + head.next = newNode; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + //ûԪؾ쳣 + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Object val = head.data; + head = head.next; + size--; + return val; + } + + public Object removeLast() { + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Node node = head; + while (node.next != null) { + node = node.next; + } + Object val = node.data; + node = null; + size--; + return val; + } + + public Iterator iterator() { + return new MyIterator(this); + } + + private class MyIterator implements Iterator{ + private int poi = -1; + private LinkedList list ; + private MyIterator(LinkedList list) { + this.list= list; + } + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return (poi + 1) < list.size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + poi++; + if (poi >= list.size) { + poi--; + throw new IndexOutOfBoundsException(); + } + + return list.get(poi); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (poi < 0) { + throw new NoSuchElementException(); + } + Object val = list.removeLast(); + poi--; + return val; + } + + } + + private void indexCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/List.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/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/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..ea25224bd2 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class Queue { + private int size; + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o);; + size++; + } + + public Object deQueue(){ + if(size<=0){ + throw new NoSuchElementException(); + } + Object val = list.removeFirst(); + size--; + return val; + } + + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + + public int size(){ + return size; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..9a28ee4d36 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + Object val = elementData.remove(len); + size--; + return val; + } + + public Object peek(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + return elementData.get(len); + } + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + public int size(){ + return size; + } +} diff --git a/group15/1514_616019420/.classpath b/group15/1514_616019420/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group15/1514_616019420/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group15/1514_616019420/.gitignore b/group15/1514_616019420/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group15/1514_616019420/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group15/1514_616019420/.project b/group15/1514_616019420/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group15/1514_616019420/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1514_616019420/src/com/coderising/array/ArrayUtil.java b/group15/1514_616019420/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..d636e46186 --- /dev/null +++ b/group15/1514_616019420/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,205 @@ +package com.coderising.array; + +import java.lang.reflect.Array; +import java.util.*; +import com.coding.basic.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public int[] reverseArray(int[] origin) { + + if (origin != null && origin.length > 0) { + int size = origin.length; + int[] intarray = new int[size]; + for (int i = 0; i < size; i++) { + intarray[i] = origin[size - 1 - i]; + } + origin = intarray; + + } else if (origin != null && origin.length == 0) { + + } else { + throw new NullPointerException(); + } + 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) { + + if (oldArray != null) { + int[] intarry = new int[oldArray.length]; + int x = 0; + int y = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + intarry[y] = oldArray[i]; + y++; + } else { + x++; + } + } + int[] newarray = new int[y]; + System.arraycopy(intarry, 0, newarray, 0, y); + return newarray; + } else { + throw new NullPointerException(); + } + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int count = array2.length; + array2 = grow(array2, array1.length); + System.arraycopy(array1, 0, array2, count, array1.length); + Arrays.sort(array2); + int[] array3 = new int[array2.length]; + array3[0] = array2[0]; + int x = 0; + for (int i = 1; i < array2.length; i++) { + if (array2[i] != array3[x]) { + array3[x + 1] = array2[i]; + x++; + } + } + + int[] array4 = new int[x + 1]; + System.arraycopy(array3, 0, array4, 0, x + 1); + return array4; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int[] array, int max) { + int[] array0 = new int[array.length]; + int x = 0; + int y = 0; + for (int i = 0; i < array.length; i++) { + if (array[i] < max) { + array0[y] = array[i]; + y++; + } else { + x++; + } + } + int[] array1 = new int[y]; + return Arrays.copyOf(array0, y); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + + + if(max<=2) + {return new int[]{}; + + } + int[] temp = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + boolean flag = true; + for (int j = 2; j <= Math.sqrt(i); j++) { + if(i % j == 0){ + flag = false; + } + } + if(flag){temp[index++] = i;} + } + return Arrays.copyOf(temp, index); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + + if(max <= 2) return new int[]{}; + + int[] array = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + int x = 0; + for (int j = 1; j <= Math.sqrt(i); j++) { + if(j == 1) { + x += 1; + }else{ + if(i % j == 0){ + x += j + i/j; + } + } + } + if(x == i) array[index++] = i; + } + return Arrays.copyOf(array, index); + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + if(array.length == 0){ + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i).append(seperator); + } + String temp = sb.toString(); + return temp.substring(0,temp.length()-1); + } + +} diff --git a/group15/1514_616019420/src/com/coderising/array/ArrayUtilTest.java b/group15/1514_616019420/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..0f244f272a --- /dev/null +++ b/group15/1514_616019420/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,110 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + private ArrayUtil u; + int[] array; + + @Before + public void setUp() throws Exception { + + u = new ArrayUtil(); + + array = new int[100]; + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testReverseArray() { + fail("Not yet implemented"); + for (int i = 0; i < 100; i++) { + array[i] = i; + + } + array = u.reverseArray(array); + System.out.println("testReverseArray:" + Arrays.toString(array)); + + } + + @Test + public void testRemoveZero() { + fail("Not yet implemented"); + for (int i = 0; i < 100; i++) { + if (i < 50) { + array[i] = 0; + } else { + array[i] = i; + } + } + array = u.removeZero(array); + System.out.println("testRemoveZero:" + Arrays.toString(array)); + } + + @Test + public void testMerge() { + //fail("Not yet implemented"); + int[] intarray={0,1,2,3,4,5,6,7}; + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.merge(intarray, intarray0); + System.out.println("testMerge:" + Arrays.toString(intarray1)); + + } + + @Test + public void testGrow() { + //fail("Not yet implemented"); + int[] intarray={0,1,2,3,4,5,6,7}; + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.grow(intarray, 10); + System.out.println("testGrow:" + Arrays.toString(intarray1)); + + } + + @Test + public void testFibonacci() { + //fail("Not yet implemented"); + int[] intarray={0,1,2,3,4,5,6,7}; + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.fibonacci(intarray0, 17); + System.out.println("testFibonacci:" + Arrays.toString(intarray1)); + } + + @Test + public void testGetPrimes() { + //fail("Not yet implemented");int[] intarray={0,1,2,3,4,5,6,7}; + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.getPrimes(10); + System.out.println("testGetPrimes:" + Arrays.toString(intarray1)); + + } + + @Test + public void testGetPerfectNumbers() { + // fail("Not yet implemented"); + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.getPerfectNumbers(10); + System.out.println("testGetPerfectNumbers:" + Arrays.toString(intarray1)); + } + + @Test + public void testJoin() { + // fail("Not yet implemented"); + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + String str=u.join(intarray0, "++"); + System.out.println("testJoin:" + str); + } + +} diff --git a/group15/1514_616019420/src/com/coderising/litestruts/LoginAction.java b/group15/1514_616019420/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group15/1514_616019420/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/group15/1514_616019420/src/com/coderising/litestruts/Struts.java b/group15/1514_616019420/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..9081b94e78 --- /dev/null +++ b/group15/1514_616019420/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +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.Map; +import java.util.Set; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + /* + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 根据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + + try { + //0读取配置文件struts.xml + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read(new File("src/com/coderising/litestruts/struts.xml")); + Element rootElement = document.getRootElement(); //根元素 + Map actionMap = new HashMap<>(); + Map loginResult = new HashMap<>(); + Map logoutResult = new HashMap<>(); + java.util.Iterator iterator = rootElement.elementIterator("action"); + while(iterator.hasNext()){ + Element actionNode = (Element) iterator.next(); + String key = actionNode.attributeValue("name"); + String value = actionNode.attributeValue("class"); + action.put(key,value); + java.util.Iterator it = actionNode.elementIterator("result"); + while(it.hasNext()){ + Element resultNode = (Element) it.next(); + String k = resultNode.attributeValue("name"); + String v = resultNode.getText(); + if(key.equals("login")){ + loginResult.put(k,v); + }else { + logoutResult.put(k,v); + } + } + } + + //1 + String className = actionMap.get(actionName); //获取类名 + Object o = Class.forName(className).newInstance(); //创建对象 + if(o instanceof LoginAction){ + LoginAction loginAction = (LoginAction) o; + Set> set = parameters.entrySet(); + for (Map.Entry en : set) { + if(en.getKey().equals("name")){ + loginAction.setName(en.getValue()); + }else if(en.getKey().equals("password")){ + loginAction.setPassword(en.getValue()); + } + } + + //2 + Class clazz = (Class) loginAction.getClass(); + Method m = clazz.getDeclaredMethod("execute",null); + m.setAccessible(true); + String message = (String) m.invoke(loginAction,null); + + //3 + View view = new View(); + Map params = new HashMap(); + Method[] ms = clazz.getDeclaredMethods(); + for (Method method : ms) { + method.setAccessible(true); + if(method.getName().equals("getName")){ + String value = (String) method.invoke(loginAction,null); + params.put("name",value); + }else if(method.getName().equals("getPassword")){ + String value = (String) method.invoke(loginAction,null); + params.put("password",value); + }else if(method.getName().equals("getMessage")){ + String value = (String) method.invoke(loginAction,null); + params.put("message",value); + } + } + view.setParameters(params); + + //4 + String jsp = loginResult.get(message); + view.setJsp(jsp); + + return view; + }else { + return null; + } + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + } + +} diff --git a/group15/1514_616019420/src/com/coderising/litestruts/StrutsTest.java b/group15/1514_616019420/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group15/1514_616019420/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/group15/1514_616019420/src/com/coderising/litestruts/View.java b/group15/1514_616019420/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1514_616019420/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/group15/1514_616019420/src/com/coderising/litestruts/struts.xml b/group15/1514_616019420/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group15/1514_616019420/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/group15/1514_616019420/src/com/coding/basic/ArrayList.java b/group15/1514_616019420/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..e4f5d255c5 --- /dev/null +++ b/group15/1514_616019420/src/com/coding/basic/ArrayList.java @@ -0,0 +1,79 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + 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 oj = elementData[index]; + for (int i = index; i < size; i++) { + elementData[i] = elementData[i + 1]; + } + elementData[size--] = null; + return oj; + } + + public int size() { + + return size; + } + + public Iterator iterator() { + return new MyIterator(); + } + + public class MyIterator implements Iterator { + + int i = 0; + + @Override + public boolean hasNext() { + while (elementData[i] != null) { + return true; + } + return false; + } + + @Override + public Object next() { + return elementData[i++]; + } + } + + + public static void main(String[] args) { + ArrayList a = new ArrayList(); + a.add(1); + a.add(2); + a.add(3); + + MyIterator b = (MyIterator) a.iterator(); + + while(b.hasNext()){ + System.out.println(b.next()); + } + } + + + +} diff --git a/group15/1514_616019420/src/com/coding/basic/BinaryTreeNode.java b/group15/1514_616019420/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..763f79c06c --- /dev/null +++ b/group15/1514_616019420/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,46 @@ +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) { + BinaryTreeNode node = new BinaryTreeNode(); + if (left == null && right != null) { + right = node; + } else if (right == null & left != null) { + left = node; + } else { + return null; + } + return node; + } + +} diff --git a/group15/1514_616019420/src/com/coding/basic/Iterator.java b/group15/1514_616019420/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group15/1514_616019420/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/group15/1514_616019420/src/com/coding/basic/LinkedList.java b/group15/1514_616019420/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..0e3bed357f --- /dev/null +++ b/group15/1514_616019420/src/com/coding/basic/LinkedList.java @@ -0,0 +1,136 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o) { + Node n = new Node(); + n.data = o; + if (head != null) { + n.next = head; + size++; + } else { + size = 1; + } + head = n; + } + + public void add(int index, Object o) { + + Node n1 = head; + Node n2 = new Node(); + for (int i = size - 1; i >= index; i--) { + if (i == index) { + n2.next = n1.next; + n2.data = 0; + n1.next = n2; + } else { + n1 = n1.next; + } + + } + + size++; + } + + public Object get(int index) { + Node n1 = head; + Object o = null; + for (int i = size - 1; i >= index; i--) { + n1 = n1.next; + if (i == index) { + o = n1.data; + } + + } + + return o; + } + + public Object remove(int index) { + Node n1 = head; + Node n2 = new Node(); + Node n3 = new Node(); + Object o = null; + for (int i = size - 1; i >= index; i--) { + if (i == index + 1) { + n2 = n1.next; + } else if (i == index) { + n3 = n2.next; + o = n3.data; + n1 = n3.next; + } else { + n1 = n1.next; + } + + } + n2.next = n1; + size--; + return o; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node n=new Node(); + Node n1=head; + + for(int i=size-1;i>=0;i--) + { + n1=n1.next; + if(i==0){ + n=n1.next; + n.data=o; + } + + } + } + + public void addLast(Object o) { + Node n=new Node(); + n.data=o; + n.next=head; + head=n; + + } + + public Object removeFirst() { + Object o= new Object(); + Node n1=head; + + for(int i=size-1;i>=0;i--) + { + n1=n1.next; + if(i==1){ + o=n1.next.data; + n1.next=null; + } + + } + return o; + } + + public Object removeLast() { + Object o= new Object(); + Node n=head; + head=n.next; + o=n.data; + n.next=null; + return o; + } + + public Iterator iterator() { + + return null; + } + + private static class Node { + Object data; + Node next; + + } +} diff --git a/group15/1514_616019420/src/com/coding/basic/List.java b/group15/1514_616019420/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group15/1514_616019420/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/group15/1514_616019420/src/com/coding/basic/Queue.java b/group15/1514_616019420/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..9f55b2a4fa --- /dev/null +++ b/group15/1514_616019420/src/com/coding/basic/Queue.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList list =new LinkedList(); + + public void enQueue(Object o){ + + list.addFirst(o); + } + + public Object deQueue(){ + return list.removeLast(); + } + + public boolean isEmpty(){ + return list.size()==0; + } + + public int size(){ + return list.size(); + } +} diff --git a/group15/1514_616019420/src/com/coding/basic/Stack.java b/group15/1514_616019420/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..88589d98c7 --- /dev/null +++ b/group15/1514_616019420/src/com/coding/basic/Stack.java @@ -0,0 +1,24 @@ +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/group15/1514_616019420/src/com/coding/basic/readme.txt b/group15/1514_616019420/src/com/coding/basic/readme.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group15/1515_337959725/.classpath b/group15/1515_337959725/.classpath new file mode 100644 index 0000000000..6a4228528e --- /dev/null +++ b/group15/1515_337959725/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group15/1515_337959725/.gitignore b/group15/1515_337959725/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group15/1515_337959725/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group15/1515_337959725/.project b/group15/1515_337959725/.project new file mode 100644 index 0000000000..6730933705 --- /dev/null +++ b/group15/1515_337959725/.project @@ -0,0 +1,17 @@ + + + coding0305 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1515_337959725/.settings/org.eclipse.jdt.core.prefs b/group15/1515_337959725/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..060c5ee3d2 --- /dev/null +++ b/group15/1515_337959725/.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.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +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.5 diff --git a/group15/1515_337959725/src/com/coderising/array/ArrayUtil.java b/group15/1515_337959725/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..3273e77f7b --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,184 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * һa , Ըֵû + 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] + a = [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int a; + int length=origin.length; + for(int i=0;iarray2[j]){ + array3[k]=array1[j]; + j++; + k++; + } + } + return array3; + } + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray =new int[oldArray.length+size]; + int i; + for(i=0;i parameters) { + + /* + + 0. ȡļstruts.xml + + 1. actionNameҵӦclass LoginAction, ͨʵ + parametersеݣösetter parametersе + ("name"="test" , "password"="1234") , + ǾӦõ setNamesetPassword + + 2. ͨöexectue ÷ֵ"success" + + 3. ͨҵgetter getMessage, + ͨã ֵγһHashMap , {"message": "¼ɹ"} , + ŵViewparameters + + 4. struts.xmlе ,Լexecuteķֵ ȷһjsp + ŵViewjspֶС + + */ + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + View view = null; + try { + DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); + File f = new File("E:/gitProject/coding2017/group15/1515_337959725/src/com/coderising/litestruts/struts.xml"); + Document document = documentBuilder.parse(f); + NodeList actionList = document.getElementsByTagName("action"); + Node node = null; + String className=""; + for(int i=0;i 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"); //ԤIJһ + + 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/group15/1515_337959725/src/com/coderising/litestruts/View.java b/group15/1515_337959725/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1515_337959725/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/group15/1515_337959725/src/com/coderising/litestruts/struts.xml b/group15/1515_337959725/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..6f23f0a83d --- /dev/null +++ b/group15/1515_337959725/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/group15/1515_337959725/src/com/coding/basic/ArrayListTest.java b/group15/1515_337959725/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..dbdbbbc406 --- /dev/null +++ b/group15/1515_337959725/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,65 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayListTest implements ListTest{ + private Object[] obj; + + public ArrayListTest() { + obj=new Object[0]; + } + + public void add(Object o) { + obj = Arrays.copyOf(obj, obj.length+1); + obj[obj.length-1]=o; + } + + public void add(int index, Object o) { + obj = Arrays.copyOf(obj, obj.length+1); + for(int i=index;inode2.data){ + if(node1.left==null){ + node1.left=node2; + }else{ + compareNode(node1.left,node2); + } + }else{ + if(node1.right==null){ + node1.right=node2; + }else{ + compareNode(node1.right,node2); + } + } + } + +} diff --git a/group15/1515_337959725/src/com/coding/basic/IteratorTest.java b/group15/1515_337959725/src/com/coding/basic/IteratorTest.java new file mode 100644 index 0000000000..f8fcdcaa9c --- /dev/null +++ b/group15/1515_337959725/src/com/coding/basic/IteratorTest.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface IteratorTest { + public boolean hasNext(); + public Object next(); +} diff --git a/group15/1515_337959725/src/com/coding/basic/LinkedListTest.java b/group15/1515_337959725/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..03b4e1946c --- /dev/null +++ b/group15/1515_337959725/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,100 @@ +package com.coding.basic; + +public class LinkedListTest implements ListTest { + private Node headNode; + private Node tailNode; + + class Node{ + private Object obj; +// private Node proNode; + private Node nextNode; + + public Node(Object obj) { + this.obj = obj; + } + + + } + + public void add(Object o) { + if(headNode==null){ + headNode=new Node(o); + tailNode=headNode; + }else{ + tailNode.nextNode=new Node(o); + tailNode=tailNode.nextNode; + } + } + + public void add(int index, Object o) { + Node newNode=new Node(o); + Node node=headNode; + for(int i=0;i array2[j]){ + retTmp[k++] = array2[j++]; + }else{ + j++; + sameCount++; + } + } + //insert remainder array + while(i < array1.length){ + retTmp[k++] = array1[i++]; + } + while(j < array2.length){ + retTmp[k++] = array2[j++]; + } + int[] ret = new int[retTmp.length - sameCount]; + if(sameCount > 0){ + System.arraycopy(retTmp, 0, ret, 0, retTmp.length - sameCount); + } + return ret; + } + + /** + * 4把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + for(int i=0; i0; i--){ + //get divide numbers + MyArrayList divideNumArrayList = new MyArrayList(10); + for(int j=1; j parameters) { + View view = new View(); + /* + * 0. 读取配置文件struts.xml + * */ + SAXReader reader = new SAXReader(); + try { + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + Iterator iter = root.elementIterator(); + while(iter.hasNext()){ + Element secondNode = (Element) iter.next(); + String nameStr = secondNode.attributeValue("name"); + String classStr = secondNode.attributeValue("class"); + + /* + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + */ + if(nameStr.equals(actionName)){ + Class cls = Class.forName(classStr); + Object obj = cls.newInstance(); + Method mtd1 = cls.getDeclaredMethod("setName", String.class); + mtd1.invoke(obj, parameters.get("name")); + + Method mtd2 = cls.getDeclaredMethod("setPassword", new Class[]{String.class}); + mtd2.invoke(obj, parameters.get("password")); + + /* + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * */ + Method execute = cls.getDeclaredMethod("execute"); + String runStatus = (String) execute.invoke(obj); + + /* + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + */ + Method mtd3 = cls.getDeclaredMethod("getMessage"); + String getMes = (String) mtd3.invoke(obj); + Map params = new HashMap(); + params.put("message",getMes); + + /* + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + Iterator iterSecond = secondNode.elementIterator(); + while(iterSecond.hasNext()){ + Element thirdNode = (Element) iterSecond.next(); + String resultNameStr = thirdNode.attributeValue("name"); + String pageStr = thirdNode.getText(); + + if(runStatus.equals(resultNameStr)){ + view.setJsp(pageStr); + view.setParameters(params); + break; + } + } + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } +} diff --git a/group15/1517_279137987/src/com/coderising/litestruts/StrutsTest.java b/group15/1517_279137987/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9f20bbfd59 --- /dev/null +++ b/group15/1517_279137987/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group15/1517_279137987/src/com/coderising/litestruts/View.java b/group15/1517_279137987/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1517_279137987/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/group15/1517_279137987/src/com/coderising/litestruts/struts.xml b/group15/1517_279137987/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ea46090bc9 --- /dev/null +++ b/group15/1517_279137987/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/group15/1517_279137987/src/my/collection/linear/MyArrayList.java b/group15/1517_279137987/src/my/collection/linear/MyArrayList.java new file mode 100644 index 0000000000..d84b583dcb --- /dev/null +++ b/group15/1517_279137987/src/my/collection/linear/MyArrayList.java @@ -0,0 +1,104 @@ +package my.collection.linear; + +import java.util.Arrays; + +public class MyArrayList implements MyList{ + + private int size = 0; + private int CAPACITY = 10; + private Object[] elementData = new Object[CAPACITY]; + + public MyArrayList(int len){ + this.elementData = new Object[len]; + } + + public void add(Object obj){ + add(size,obj); + } + + public void add(int index, Object obj){ + if(index < 0){ + System.out.println("insert position illegal."); + }else{ + if(size + 1 < elementData.length){ + System.arraycopy(elementData, 0, this.elementData, 0, index); + System.arraycopy(elementData, index, this.elementData, index+1, elementData.length-index-1); + this.elementData[index] = obj; + }else{ + Object[] newElementData = Arrays.copyOf(elementData, elementData.length + CAPACITY); + System.arraycopy(elementData, index, newElementData, index+1, elementData.length - index); + newElementData[index] = obj; + this.elementData = newElementData; + } + size++; + } + } + + public Object remove(int index){ + if(index == 0){ + System.arraycopy(elementData, 1, this.elementData, 0, elementData.length-1); + }else{ + System.arraycopy(elementData, 0, this.elementData, 0, index); + System.arraycopy(elementData, index + 1, this.elementData, index, elementData.length-index-1); + } + size--; + return elementData[index]; + } + + public Object get(int index){ + if(index < 0 || index > elementData.length-1){ + return "get position illegal."; + }else{ + return elementData[index]; + } + } + + public int size(){ + return size; + } + + public String toString(){ + String str ="toString():"; + for(int i=0; i size){ + return false; + }else{ + return true; + } + } + + public Object next() { + return get(pos); + } + + public Object remove(){ //? + return MyArrayList.this.remove(this.pos); + } + + } +} diff --git a/group15/1517_279137987/src/my/collection/linear/MyBinaryTreeNode.java b/group15/1517_279137987/src/my/collection/linear/MyBinaryTreeNode.java new file mode 100644 index 0000000000..8b52c6f838 --- /dev/null +++ b/group15/1517_279137987/src/my/collection/linear/MyBinaryTreeNode.java @@ -0,0 +1,83 @@ +package my.collection.linear; + +public class MyBinaryTreeNode implements Comparable{ + + 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){ + //cast to MyBinaryTreeNode + MyBinaryTreeNode newNode = new MyBinaryTreeNode(); + newNode.setData(o); + newNode.setLeft(null); + newNode.setRight(null); + + //insert to current node + if(data == null){ + this.setData(o); + this.setLeft(null); + this.setRight(null); + }else{ + //insert to left child + if(compareTo(o) == -1){ + if(this.getLeft() == null){ + this.setLeft(newNode); + }else{ + this.data = this.getLeft().data; + this.setLeft(this.getLeft().getLeft()); + this.setRight(this.getRight().getRight()); + insert(o); + } + //insert to right child + }else if(compareTo(o) == 1){ + if(this.getRight() == null){ + this.setRight(newNode); + }else{ + this.data = this.getLeft().data; + this.setLeft(this.getLeft().getLeft()); + this.setRight(this.getRight().getRight()); + insert(o); + } + //can't insert node which has same data. + }else{ + + } + } + return newNode; + } + + public int compareTo(Object o) { + int compareFlag = 0; + if(o instanceof Integer){ + if(Integer.valueOf(o.toString()) < Integer.valueOf(data.toString())){ + compareFlag = -1; + }else if(Integer.valueOf(o.toString()) > Integer.valueOf(data.toString())){ + compareFlag = 1; + }else{ + compareFlag = 0; + } + } + return compareFlag; + } + +} diff --git a/group15/1517_279137987/src/my/collection/linear/MyIterator.java b/group15/1517_279137987/src/my/collection/linear/MyIterator.java new file mode 100644 index 0000000000..5eb9528fd7 --- /dev/null +++ b/group15/1517_279137987/src/my/collection/linear/MyIterator.java @@ -0,0 +1,8 @@ +package my.collection.linear; + +public interface MyIterator { + + public boolean hasNext(); //if has next element + + public Object next(); //get next element +} diff --git a/group15/1517_279137987/src/my/collection/linear/MyLinkedList.java b/group15/1517_279137987/src/my/collection/linear/MyLinkedList.java new file mode 100644 index 0000000000..d72ec475a6 --- /dev/null +++ b/group15/1517_279137987/src/my/collection/linear/MyLinkedList.java @@ -0,0 +1,95 @@ +package my.collection.linear; + +public class MyLinkedList implements MyList { + + private Node head; + + private int size = 0; + + public void add(Object obj) { + add(this.size, obj); + } + + public void add(int index, Object obj) { + Node curNode = head; + Node addNode = new Node(obj); + if(index == 0){ + addNode.next = head; + head = addNode; + }else{ + for(int i=0; i0){ + for(int i=0;i=array1.length&&b=array2.length&&am)list.add(n); + } + int[] arr = new int[list.size()]; + for(int i=0;i + + + + + diff --git a/group15/1519_137845093/.gitignore b/group15/1519_137845093/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group15/1519_137845093/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group15/1519_137845093/.project b/group15/1519_137845093/.project new file mode 100644 index 0000000000..7371704aff --- /dev/null +++ b/group15/1519_137845093/.project @@ -0,0 +1,17 @@ + + + helloworld + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1519_137845093/3.5_homework/array/ArrayUtil.java b/group15/1519_137845093/3.5_homework/array/ArrayUtil.java new file mode 100644 index 0000000000..e8660ed7ce --- /dev/null +++ b/group15/1519_137845093/3.5_homework/array/ArrayUtil.java @@ -0,0 +1,210 @@ +package com.coderising.array; +import java.util.*; +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if(origin.length > 0 && origin != null){ + int temp; + for (int i = 0; i < origin.length / 2; i++) { + temp = origin[i]; + origin[i] = origin[origin.length-1 - i]; + origin[origin.length-1 - i] = temp; + } + } + else { + throw new IndexOutOfBoundsException("原数组有错" ); + } + } + + /** + * 现在有如下的一个数组: 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){ + if(oldArray.length == 0){ + return oldArray; + } + int index = 0; + for(int i =0; i< oldArray.length; i++){ + if(oldArray[index] != 0){ + oldArray[index] = oldArray[i]; + index++; + } + } + return Arrays.copyOf(oldArray, index); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int length1 = array1.length; + int length2 = array2.length; + int newArrayLength = length1 + length2; + int[] result = new int[newArrayLength]; + int i = 0, j = 0, k = 0; //i:用于标示1数组 j:用来标示2数组 k:用来标示传入的数组 + + while (i < length1 && j < length2) { + /* 去重复元素 */ + if (array1[i] < array2[j]) { + result[k++] = array1[i++]; + } else if (array1[i] == array2[j]) { + result[k++] = array1[i]; + //在某个位置上2个值相等的话,取哪个都一样, + // 然后这个相等的位置的2个值都直接向后移动1,继续比较 + j++; + i++; + } else { + result[k++] = array2[j++]; + } + } + /* 后面while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入结果数组 */ + while (i < length1) { + result[k++] = array1[i++]; + } + + while (j < length2) { + result[k++] = array2[j++]; + } + return Arrays.copyOf(result, k); + } + + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] array = new int[]{}; + for(int i=1; getfb(i) k) { + if (i % k == 0) + break; + k++; + } + //扩容 + prime = grow(prime, 1); + prime[j++] = i; + } + return prime; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] array = new int[0]; + int j = 0; + for (int i = 1; i < max; i++) { + if (isPerfect(i)) { + //加入到数组中 + array = grow(array, 1); + array[j] = i; + j++; + } + } + return array; + } + + //判断一个数是否是“完数” + private static boolean isPerfect(int max) { + int i = 1; + int n = 0; + while (i < max) { + if (max % i == 0) { + n += i; + } + i++; + } + if (n == max) + return true; + return false; + } + + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + if (array == null) + return null; + String s = ""; + for (int i = 0; i < array.length; i++) { + s += array[i]; + if (i != array.length - 1) + s += seperator; + } + return s; + } + +} + + diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/ArrayList.java b/group15/1519_137845093/src_1st_homework_1519_137845093/ArrayList.java new file mode 100644 index 0000000000..e0ef246f9b --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/ArrayList.java @@ -0,0 +1,117 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + //Ԫظ + private int size = 0; + //ʼΪ10 + private Object[] elementData = new Object[10]; + + public void add(Object o) { + int len = size + 1; + // жlistijǷ鳤 + if (len > elementData.length) { + // + Object[] newElemData = new Object[elementData.length + 1]; + // ƾԪص + System.arraycopy(elementData, 0, newElemData, 0, elementData.length); + elementData = newElemData; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + // ±ǷԽ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "Խ磻" ); + } + // Ԫصĩβֱӵadd + if (index == size) { + add(o); + } else { + // + Object[] newElemData = new Object[elementData.length + 1]; + // indexǰԪص + System.arraycopy(elementData, 0, newElemData, 0, index); + newElemData[index] = o; + // index ԺԪص + System.arraycopy(elementData, index, newElemData, index + 1, size - index); + + elementData = newElemData; + size++; + } + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "Խ磻"); + } + return elementData[index]; + } + + public Object remove(int index) { + //±鳤ȵģ׳쳣 + if (index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "Խ磻"); + } + //indexһԪصֵҪɾ + if(index != (size-1)){ + // + Object[] newElemData = new Object[elementData.length]; + // indexǰԪص + System.arraycopy(elementData, 0, newElemData, 0, index); + // index ԺԪص + System.arraycopy(elementData, index+1, newElemData, index, size - index -1); + } + Object removeElement = elementData[index]; + //Сij + size--; + return removeElement; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyItr(this); + } + + private class MyItr implements Iterator { + private int l = -1; + private ArrayList array = null; + + private MyItr(ArrayList array) { + this.array = array; + } + + @Override + public boolean hasNext() { + return (l + 1) < array.size; + } + + @Override + public Object next() { + l++; + if (l >= array.size) { + l = array.size - 1 ; + throw new IndexOutOfBoundsException(); + } + + return array.get(l); + } + + @Override + public Object remove() { + if (l < 0) { + throw new NoSuchElementException(); + } + Object val = array.remove(l); + l--; + return val; + } + + } +} \ No newline at end of file diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/BinaryTreeNode.java b/group15/1519_137845093/src_1st_homework_1519_137845093/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/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/group15/1519_137845093/src_1st_homework_1519_137845093/Iterator.java b/group15/1519_137845093/src_1st_homework_1519_137845093/Iterator.java new file mode 100644 index 0000000000..2d4150183f --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public Object remove(); + +} diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/LinkedList.java b/group15/1519_137845093/src_1st_homework_1519_137845093/LinkedList.java new file mode 100644 index 0000000000..7f06138ec1 --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/LinkedList.java @@ -0,0 +1,180 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + //жûͷ + if(head == null) + head = new Node(o,null); + else { + Node newNode = head; + while(newNode.next != null){ + newNode = newNode.next; + } + newNode.next = new Node(o,null); + + } + + } + public void add(int index , Object o){ + //±ǷԽ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("index:" + index + "Խ磻"); + } + Node node = head; + //ǵһͷ + if(index == 0){ + Node newNode = new Node(o,head); + head = newNode; + size ++; + } + else{ + for(int i = 1; i < index; i++){ + node = node.next; + } + //indexoҽonextڵΪnode.next + Node newNode = new Node(o, node.next); + node.next = newNode; + size++; + } +} + + public Object get(int index){ + //±ǷԽ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("index:" + index + "Խ磻"); + } + Node node = head; + for (int i = 1; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index){ + //±ǷԽ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("index:" + index + "Խ磻"); + } + Node node = head; + Node removeNode; + if (index == 0) { + //һڵֱӽͷڵָһڵ + removeNode = head; + head = head.next; + } + else { + //ҵֵǰһڵ + for (int i = 1; i < index; i++) { + node = node.next; + } + removeNode = node.next; + //ǰһڵָ룬ָɾڵָĽڵ + node.next = removeNode.next; + } + size--; + return removeNode.data; + } + + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newNode = new Node(o, head.next); + head.next = newNode; + size++; + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + if(size <= 0){ + throw new IndexOutOfBoundsException("ûԪأ"); + } + Node node = head; + head = head.next; + size--; + return node.data; + } + + public Object removeLast(){ + if(size <= 0){ + throw new IndexOutOfBoundsException("ûԪأ"); + } + Node node = head; + while(node.next != null){ + node = node.next; + } + Object val = node.data; + node = null; + size--; + return val; + } + private static class Node{ + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + + } + } + + + + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int l = -1; + private LinkedList list; + private Itr(LinkedList linkedList) { + // TODO Auto-generated constructor stub + this.list = list; + + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return l < list.size - 1; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + l++; + if (l >= list.size) { + l--; + throw new IndexOutOfBoundsException(); + } + + return list.get(l); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (l < 0) { + throw new NoSuchElementException(); + } + Object val = list.removeLast(); + l--; + return val; + } + + } + + } diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/List.java b/group15/1519_137845093/src_1st_homework_1519_137845093/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/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/group15/1519_137845093/src_1st_homework_1519_137845093/Queue.java b/group15/1519_137845093/src_1st_homework_1519_137845093/Queue.java new file mode 100644 index 0000000000..3433125a8b --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; +import java.util.NoSuchElementException; + +public class Queue { + private int size; + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o); + size++; + } + + public Object deQueue(){ + if(size<=0){ + throw new NoSuchElementException(); + } + Object deQueue = list.removeLast(); + size--; + return deQueue; + } + + public boolean isEmpty(){ + return (size>=0); + } + + public int size(){ + return size; + } +} diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/Stack.java b/group15/1519_137845093/src_1st_homework_1519_137845093/Stack.java new file mode 100644 index 0000000000..036baafc73 --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + private Object removeElement; + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int l = size - 1; + removeElement = elementData.remove(l); + size--; + return removeElement; + } + + public Object peek(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + return elementData.get(len); + } + + public boolean isEmpty(){ + return (size>=0); + } + + public int size(){ + return size; + } +} diff --git a/group15/1521_653895972/.gitignore b/group15/1521_653895972/.gitignore new file mode 100644 index 0000000000..6c9d8afec3 --- /dev/null +++ b/group15/1521_653895972/.gitignore @@ -0,0 +1,3 @@ +/out +.idea +*.iml \ No newline at end of file diff --git a/group15/1521_653895972/src/com/coding/basic/ArrayList.java b/group15/1521_653895972/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..2b692ab814 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/ArrayList.java @@ -0,0 +1,150 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by wanc on 2017/2/21. + * 实现ArrayList + */ +public class ArrayList implements List { + /** + * 实例化空数组 不用每次都new + */ + private static final Object[] Empty_elementData = {}; + /** + * 计数 + */ + private int size = 0; + /** + * 数据存放 + */ + private Object[] elementData = new Object[100]; + + public ArrayList() { + this.elementData = Empty_elementData; + } + + /** + * 检查是否越界 + */ + private void checkLenght(int index) { + if (index - size > 0) + throw new IndexOutOfBoundsException(); + } + + /** + * 增加数组容量 + */ + private void kuorong() { + elementData = Arrays.copyOf(elementData, size + 1); + } + + /** + * 添加数据 + * + * @param o + */ + public void add(Object o) { + //扩容 + kuorong(); + //添加数据 + elementData[size++] = o; + } + + /** + * 在指定索引添加数据 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + //扩容 + kuorong(); + //移动数据 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + //添加数据 + elementData[index] = o; + size++; + } + + /** + * 获取指定索引数据 + * + * @param index + * @return + */ + public Object get(int index) { + //检查是否越界 + checkLenght(index); + return elementData[index]; + } + + /** + * 移除指定索引数据 + * + * @param index + * @return + */ + public Object remove(int index) { + //检查是否越界 + checkLenght(index); + Object element = elementData[index]; + //计算移除该元素后,要前移的个数 + int movesize = size - index - 1; + //移动数据 + System.arraycopy(elementData, index + 1, elementData, index, movesize); + //删除末尾元素 + elementData[--size] = null; + return element; + } + + /** + * 返回数量 + * + * @return + */ + public int size() { + return size; + } + + /** + * 获取迭代器 + * + * @return + */ + public Iterator iterator() { + return new ArrayItr(); + } + + //迭代器实现类部类 + private class ArrayItr implements Iterator { + int cursor;//游标 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + if (i > size) throw new NoSuchElementException(); + Object[] newElementData = ArrayList.this.elementData; + if (i > newElementData.length) throw new IndexOutOfBoundsException(); + cursor = i + 1; + return newElementData[i]; + } + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + Object[] s = Arrays.copyOf(elementData, size); + return Arrays.toString(s); + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/BasicTest.java b/group15/1521_653895972/src/com/coding/basic/BasicTest.java new file mode 100644 index 0000000000..a181087104 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/BasicTest.java @@ -0,0 +1,184 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by wanc on 2017/2/21. + */ +public class BasicTest { + + @Test + public void test() { + //测试 + testArrayList(); + testLinkedList(); + testBinaryTreeNode(); + testStack(); + testQueue(); + } + + + public void testQueue(){ + Queue queue = new Queue(); + queue.enQueue("S"); + queue.enQueue("Y"); + queue.enQueue(5); + System.out.println(queue); + System.out.println("queue.size()="+queue.size()); + System.out.println("queue.deQueue()="+queue.deQueue()); + System.out.println(queue); + System.out.println("queue.isEmpty()="+queue.isEmpty()); + System.out.println(queue); + } + public void testStack(){ + Stack stack = new Stack(); + stack.push("S"); + stack.push("Y"); + stack.push(5); + System.out.println("stack.size()="+stack.size()); + System.out.println("stack.peek()="+stack.peek()); + System.out.println(stack); + System.out.println("stack.isEmpty()="+stack.isEmpty()); + stack.pop(); + System.out.println(stack); + } + public void testBinaryTreeNode(){ + System.out.println("-------------------BinaryTreeNode 测试开始-------------------"); + System.out.println("new 一个实例"); + BinaryTreeNode root = new BinaryTreeNode(); + root.insert(5); + root.insert(6); + root.insert(9); + root.insert(3); + root.insert(3); + root.insert(2); + root.insert(10); + System.out.println(root); + System.out.println("-------------------LinkedList 测试结束-------------------"); + } + public void testLinkedList() { + System.out.println("-------------------LinkedList 测试开始-------------------"); + + System.out.println("new 一个实例"); + LinkedList list = new LinkedList(); + + System.out.println("添加元素----A"); + list.add("A"); + Assert.assertEquals(list.get(list.size()-1),"A"); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("添加元素----B"); + list.add("B"); + Assert.assertEquals(list.get(list.size()-1),"B"); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("添加元素----3"); + list.add(3); + Assert.assertEquals(list.get(list.size()-1),3); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("在下标1插入元素----3"); + list.add(1, 3); + Assert.assertEquals(list.get(1),3); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("在下标3插入元素----6"); + list.add(3, 6); + Assert.assertEquals(list.get(3),6); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("删除下标0元素"); + list.remove(0); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("获取size"); + System.out.println("结果:"+list.size()); + + System.out.println(); + System.out.println("在首位前插入F"); + list.addFirst("F"); + Assert.assertEquals(list.get(0),"F"); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("在末位前插入K"); + list.addLast("K"); + Assert.assertEquals(list.get(list.size()-1),"K"); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("删除首位"); + list.removeFirst(); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("删除末尾"); + list.removeLast(); + System.out.println("结果:"+list); + + System.out.println(); + System.out.println("迭代器输出:"); + Iterator i = list.iterator(); + while (i.hasNext()){ + System.out.print(i.next()+" "); + } + System.out.println("-------------------LinkedList 测试结束-------------------"); + } + + /** + * 测试 ArrayList + */ + public void testArrayList() { + System.out.println("-------------------ArrayList 测试开始-------------------"); + + System.out.println("new 一个实例"); + ArrayList list = new ArrayList(); + + System.out.println("添加元素 A"); + list.add("A"); + Assert.assertEquals(list.get(list.size()-1),"A"); + + System.out.println("添加元素 B"); + list.add("B"); + Assert.assertEquals(list.get(list.size()-1),"B"); + + System.out.println("添加元素 3"); + list.add(3); + Assert.assertEquals(list.get(list.size()-1),3); + System.out.println("输出:"+list); + + System.out.println("添加元素 3 到索引 1"); + list.add(1, 3); + Assert.assertEquals(list.get(1),3); + System.out.println("输出:"+list); + + System.out.println("添加元素 6 到索引 3"); + list.add(3, 6); + Assert.assertEquals(list.get(3),6); + System.out.println("输出:"+list); + + System.out.println("移除 索引 4 元素"); + Object rm = list.remove(4); + System.out.println("输出:"+list); + + System.out.println("获取 索引 4 元素"); + Object get = list.get(4); + Assert.assertNotEquals(rm,get); + + System.out.println("输出:"+list); + System.out.println("数量:"+list.size()); + Iterator i = list.iterator(); + System.out.print("迭代器输出:"); + while (i.hasNext()){ + System.out.print(i.next()+" "); + } + System.out.println("-------------------ArrayList 测试结束-------------------"); + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java b/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..34d76db083 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,105 @@ +package com.coding.basic; + +/** + * 实现二叉树 + * left总比父节点小 + * right总比父节点大 + */ +public class BinaryTreeNode { + private Node root; + private int size = 0; + + /** + * 插入数据 + * @param data + */ + public void insert(int data) { + final Node newNode = new Node(data); + if (root == null) {//根节点为空 直接插入数据到根节点 + root = newNode; + } else { + Node current = root; + while (true) {//循环判断 + Node parent = current; + if (data < current.data) {//比父节点小 就是left + current = current.left; + //直到left节点不存在 + if (current == null) { + //插入数据 + parent.left = newNode; + return; + } + } else {//比父节点大 也就是right + current = current.right; + //直到right节点不存在 + if (current == null) { + //插入数据 + parent.right = newNode; + return; + } + } + } + } + size++; + } + + + /** + * 返回数量 + * @return + */ + public int size() { + return size; + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "["+midTraverse(root)+"]"; + } + + /** + * 节点内部类 用于保存数据 + */ + private static class Node { + int data; + Node left; + Node right; + + Node(int data) { + this.data = data; + this.left = null; + this.right = null; + } + } + + //先序遍历 + private String preTraverse(Node node) { + if (node == null) + return ""; + else + return node.data + preJointComma(preTraverse(node.left)) + preJointComma(preTraverse(node.right)); + } + //中序遍历 + private String midTraverse(Node node) { + if (node == null) + return ""; + else + return midTraverse(node.left)+" "+node.data+" " +midTraverse(node.right); + } + //后序遍历 + private String posTraverse(Node node) { + if (node == null) + return ""; + else + return posTraverse(node.left)+" " +posTraverse(node.right)+" "+node.data; + } + + private String preJointComma(String str) { + return str == "" ? "" : "," + str; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/Iterator.java b/group15/1521_653895972/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group15/1521_653895972/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/group15/1521_653895972/src/com/coding/basic/LinkedList.java b/group15/1521_653895972/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..f1f942590d --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/LinkedList.java @@ -0,0 +1,271 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * Created by wanc on 2017/2/21. + * 实现单向链表集合 + */ +public class LinkedList implements List { + /** + * 首节点 + */ + private Node head; + /** + * 计数 + */ + private int size = 0; + + /** + * 检查是否越界 利用jdk源码的检测方法 + */ + private boolean isElementIndex(int index) { + return index >= 0 && index < size; + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private boolean isPositionIndex(int index) { + return index >= 0 && index <= size; + } + + /** + * JDK 源码 错误信息 + * + * @param index + * @return + */ + private String outOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private void checkElementIndex(int index) { + if (!isElementIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * JDK 源码检测方法 + * + * @param index + * @return + */ + private void checkPositionIndex(int index) { + if (!isPositionIndex(index)) + throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); + } + + /** + * 获取对应下标的节点 + */ + Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) + x = x.next; + return x; + } + + /** + * 在末尾添加数据 + * + * @param o + */ + public void add(Object o) { + + if (head == null) + head = new Node(o, null); + else { + final Node lastNode = node(size - 1); + final Node newNode = new Node(o, null); + lastNode.next = newNode; + } + size++; + } + + /** + * 指定位置添加数据 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + checkPositionIndex(index); + if (size == index) + add(o); + else { + final Node prevNode = node(index - 1); + final Node nextNode = prevNode.next; + final Node newNode = new Node(o, nextNode); + prevNode.next = newNode; + size++; + } + } + + /** + * 获取指定索引数据 + * + * @param index + * @return + */ + public Object get(int index) { + return node(index).data; + } + + /** + * 移除指定索引数据 + * + * @param index + * @return + */ + public Object remove(int index) { + checkElementIndex(index); + final Node prevNode = node(index - 1); + final Node x = prevNode.next; + if (index - 1 < 0) { + prevNode.next = null; + head = x; + } else { + final Node nextNode = x.next; + prevNode.next = nextNode; + x.next = null; + } + size--; + return x.data; + } + + /** + * 返回数量 + * + * @return + */ + public int size() { + return size; + } + + /** + * 在链首添加数据 + * + * @return + */ + public void addFirst(Object o) { + final Node h = head; + final Node newNode = new Node(o, h); + head = newNode; + size++; + } + + /** + * 在链尾添加数据 + * + * @return + */ + public void addLast(Object o) { + add(o); + } + + /** + * 移除链首数据 + * + * @return + */ + public Object removeFirst() { + final Node h = head; + if (h == null) + throw new NoSuchElementException(); + final Node newFirst = h.next; + h.next = null; + head = newFirst; + size--; + return h.data; + } + + /** + * 移除链尾数据 + * + * @return + */ + public Object removeLast() { + final Node prev = node(size - 1 - 1); + final Node l = prev.next; + prev.next = null; + l.next = null; + size--; + return l.data; + } + + /** + * 获取迭代器 + * + * @return + */ + public Iterator iterator() { + return new LinkedItr(); + } + + /** + * 迭代器实现内部类 + * + * @return + */ + private class LinkedItr implements Iterator { + int cursor;//游标 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + int i = cursor; + if (i > size - 1) throw new NoSuchElementException(); + Node current = node(i); + if (current == null) throw new IndexOutOfBoundsException(); + cursor = i + 1; + return current.data; + } + } + + /** + * 节点内部类 用于保存数据 + */ + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + String result = "["; + for (int i = 0; i < size; i++) { + Node n = node(i); + if (i == 0) + result += n.data; + else + result += "," + n.data; + + } + + return result + "]"; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/List.java b/group15/1521_653895972/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group15/1521_653895972/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/group15/1521_653895972/src/com/coding/basic/Queue.java b/group15/1521_653895972/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4add2be9a4 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/Queue.java @@ -0,0 +1,60 @@ +package com.coding.basic; + +/** + * Created by wanc on 2017/2/21. + * 利用LinkedList 实现队列 + */ +public class Queue { + /** + * 利用LinkedList 保存数据 + */ + private LinkedList elementData = new LinkedList(); + + /** + * 入队 + * + * @param o + */ + public void enQueue(Object o) { + elementData.add(o); + } + + /** + * 出队 + * + * @return + */ + public Object deQueue() { + return elementData.removeFirst(); + } + + /** + * 是否为空 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + /** + * 返回队列长度 + * + * @return + */ + public int size() { + return elementData.size(); + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "Queue{" + + "elementData=" + elementData + + '}'; + } +} diff --git a/group15/1521_653895972/src/com/coding/basic/Stack.java b/group15/1521_653895972/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..23c5ba6a7b --- /dev/null +++ b/group15/1521_653895972/src/com/coding/basic/Stack.java @@ -0,0 +1,64 @@ +package com.coding.basic; +/** + * Created by wanc on 2017/2/21. + * 利用ArrayList 实现栈 + */ +public class Stack { + /** + * 利用ArrayList 保存数据 + */ + private ArrayList elementData = new ArrayList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o) { + elementData.add(o); + } + + /** + * 出栈 + * @return + */ + public Object pop() { + elementData.remove(elementData.size()-1); + return null; + } + + /** + * 返回栈顶数据 + * @return + */ + public Object peek() { + return elementData.get(elementData.size()-1); + } + + /** + * 是否为空 + * @return + */ + public boolean isEmpty() { + return elementData.size()==0?true:false; + } + + /** + * 返回栈长度 + * @return + */ + public int size() { + return elementData.size(); + } + + /** + * 重写toString 方便打印 + * + * @return + */ + @Override + public String toString() { + return "Stack{" + + "elementData=" + elementData + + '}'; + } +} diff --git a/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java b/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..92c581254b --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java @@ -0,0 +1,73 @@ +package com.coding.coderising.array; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by wanc on 2017/2/28. + */ +public class ArrayUtilTest { + + @Test + public void testReverseArray() throws Exception { + int[] arr = {7, 9, 30, 3}; + SimpleArrayUtil.reverseArray(arr); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------置换 end-----------------------------"); + } + + + @Test + public void testRemoveZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int newArr[] = SimpleArrayUtil.removeZero(oldArr); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------去零 end-----------------------------"); + } + + @Test + public void testMerge() throws Exception { + int arr1[] = {3, 5, 7, 8}; + int arr2[] = {4, 5, 6, 7}; + int arr3[] = SimpleArrayUtil.merge(arr1, arr2); + System.out.println(Arrays.toString(arr3)); + System.out.println("----------------------merge end-----------------------------"); + } + + @Test + public void testGrow() throws Exception { + int arr1[] = {3, 5, 7, 8}; + int[] newArr = SimpleArrayUtil.grow(arr1, 3); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------扩展 end-----------------------------"); + } + + @Test + public void testFibonacci() throws Exception { + int[] arr = SimpleArrayUtil.fibonacci(15); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------斐波那契 end-----------------------------"); + } + + @Test + public void testGetPrimes() throws Exception { + int[] arr = SimpleArrayUtil.getPrimes(23); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------素数 end-----------------------------"); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] newArr = SimpleArrayUtil.getPerfectNumbers(50); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------完数 end-----------------------------"); + } + + @Test + public void testJoin() throws Exception { + int arr1[] = {3, 5, 7, 8}; + System.out.println(SimpleArrayUtil.join(arr1, "-")); + System.out.println("----------------------Join end-----------------------------"); + } +} \ No newline at end of file diff --git a/group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java b/group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java new file mode 100644 index 0000000000..0aa491fb16 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java @@ -0,0 +1,250 @@ +package com.coding.coderising.array; + +public class SimpleArrayUtil { + + private static void checkNull(int[] array) { + if (array == null) + throw new NullPointerException("array is null"); + } + + /** + * 给定一个整形数组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) { + checkNull(origin); + int i = 0; + for (int j = origin.length - 1; j > i; ++i) { + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + --j; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + checkNull(oldArray); + if (0 == oldArray.length) + return oldArray; + int[] temp = new int[oldArray.length]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + temp[index++] = oldArray[i]; + } + } + int[] tem2 = new int[index]; + + System.arraycopy(temp, 0, tem2, 0, Math.min(oldArray.length, index)); + return tem2; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + public static int[] merge(int[] array1, int[] array2) { + if (array1 == null && array2 == null) + return null; + if (array1.length == 0) + return array2.clone(); + if (array2.length == 0) + return array1.clone(); + //判断排序方向 + boolean sort = array1[0] < array1[array1.length - 1]; + //去重 + for (int i = 0; i < array2.length; i++) { + boolean flag = true; + for (int j = 0; j < array1.length; j++) { + if (array2[i] == array1[j]) { + flag = false; + break; + } + } + if (flag) { + //扩容 + array1 = grow(array1, 1); + array1[array1.length - 1] = array2[i]; + } + } + //排序 + if (sort) {//小到大 + int tmp; + //冒泡排序 + for (int i = 0; i < array1.length; i++) { + for (int j = i + 1; j < array1.length; j++) { + if (array1[i] > array1[j]) { + tmp = array1[i]; + array1[i] = array1[j]; + array1[j] = tmp; + } + } + } + } else {//大到小 + int tmp; + //冒泡排序 + for (int i = 0; i < array1.length; i++) { + for (int j = i + 1; j < array1.length; j++) { + if (array1[i] < array1[j]) { + tmp = array1[i]; + array1[i] = array1[j]; + array1[j] = tmp; + } + } + } + } + return array1; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + return newArr; + } + + /** + * 斐波那契数列为: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[] arr = new int[]{}; + int i = 1; + while (true) { + int a = fb(i); + if (a > 15) + break; + //扩容 + arr = grow(arr, 1); + arr[i - 1] = a; + i++; + } + return arr; + } + + /** + * 获取斐波那契数 + * F(0)=1,F(1)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*) + */ + public static int fb(int i) { + i = i < 1 ? 1 : i; + if (i == 1 || i == 2) { + return 1; + } + return fb(i - 1) + fb(i - 2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] arr = new int[]{2}; + if (max < 1) throw new IllegalArgumentException("不是大于1的自然数"); + int j = 1; + for (int n = 3; n < max; n++) { + int k = 2; + while (n > k) { + if (n % k == 0) + break; + k++; + } + //扩容 + arr = grow(arr, 1); + arr[j++] = n; + } + return arr; + } + + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] arr = new int[0]; + int a = 0; + for (int i = 1; i < max; i++) { + if (isPerfect(i)) { + //扩容 + arr = grow(arr, 1); + arr[a++] = i; + } + } + return arr; + } + + //判断 “完数” + public static boolean isPerfect(int max) { + int i = 1; + int n = 0; + while (i < max) { + if (max % i == 0) { + n += i; + } + i++; + } + if (n == max) + return true; + return false; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (array == null) + return null; + String s = ""; + for (int i = 0; i < array.length; i++) { + s += array[i]; + if (i != array.length - 1) + s += seperator; + } + return s; + } + + +} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..273741bbfb --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.coding.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author wanc + */ +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/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..ed20c588fe --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java @@ -0,0 +1,145 @@ +package com.coding.coderising.litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + //解析xml + Map xmlInfo = parsersXml(actionName); + //获取类名 + String allName = (String) xmlInfo.get("className"); + try { + //加载类 + Class cls = Class.forName(allName); + //实例化 + Object object = cls.newInstance(); + for (String key : parameters.keySet()) { + //拼接set方法名 + String setName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Method setMethod = cls.getDeclaredMethod(setName, String.class); + //放射执行set方法 + setMethod.invoke(object, parameters.get(key)); + } + //执行execute方法 + Method exectue = cls.getDeclaredMethod("execute", null); + String reslut = (String) exectue.invoke(object, null); + //执行getMessage方法 + Method getMessage = cls.getDeclaredMethod("getMessage", null); + String message = (String) getMessage.invoke(object, null); + //获取xml配置想返回结果 + String jsp = (String) xmlInfo.get(reslut); + //组装view + Map map = new HashMap(); + map.put("message", message); + View view = new View(); + view.setJsp(jsp); + view.setParameters(map); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + + + private static Map parsersXml(String actionName) { + File file = new File(Struts.class.getResource("").getPath() + "/struts.xml"); + //1.获取DOM解析器工厂 + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + HashMap map = new HashMap<>(); + try { + //2.获取解析器 + DocumentBuilder builder = factory.newDocumentBuilder(); + //3.加载xml文档 + Document document = builder.parse(file); + //4.获取指定action的action集合 + NodeList actionlist = document.getElementsByTagName("action"); + //5.如果actionName重复 只去第一个 + for (int j = 0; j < actionlist.getLength(); j++) { + Element actionElement = (Element) actionlist.item(j); + if (actionElement.getAttribute("name") != null && actionElement.getAttribute("name").equalsIgnoreCase(actionName)) { + //6.获取 类全限定名 + String className = actionElement.getAttribute("class"); + map.put("className", className); + //7.获取 action子节点 + NodeList childList = actionElement.getChildNodes(); + int lenght = childList.getLength(); + for (int i = 0; i < lenght; i++) { + Node node = childList.item(i); + //判断为element节点 排除空格 换行 + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element child = (Element) node; + switch (child.getAttribute("name")) { + case "success": + map.put("success", child.getTextContent()); + break; + case "fail": + map.put("fail", child.getTextContent()); + break; + case "error": + map.put("error", child.getTextContent()); + break; + } + } + } + break; + } + + } + + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return map; + } +} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8699cc3eca --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coding.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java new file mode 100644 index 0000000000..2c909058cb --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package com.coding.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/struts.xml b/group15/1521_653895972/src/com/coding/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..bec462fdf5 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/struts.xml @@ -0,0 +1,12 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group15/group15.md b/group15/group15.md index d3f5a12faa..8b13789179 100644 --- a/group15/group15.md +++ b/group15/group15.md @@ -1 +1 @@ - + diff --git a/group16/1154151360/.classpath b/group16/1154151360/.classpath index fb5011632c..9746a6c933 100644 --- a/group16/1154151360/.classpath +++ b/group16/1154151360/.classpath @@ -2,5 +2,8 @@ + + + diff --git a/group16/1154151360/src/com/array/ArrayUtil.java b/group16/1154151360/src/com/array/ArrayUtil.java new file mode 100644 index 0000000000..39f587c847 --- /dev/null +++ b/group16/1154151360/src/com/array/ArrayUtil.java @@ -0,0 +1,266 @@ +package com.array; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.sun.org.apache.xalan.internal.xsltc.compiler.sym; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public int[] reverseArray(int[] origin){ + + int [] temp = new int [origin.length]; + + for (int i = 0; i < temp.length; i++){ + temp [i] = origin [temp.length - 1 - i]; + } + + origin = temp; + + return origin; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + + int newLength = 0; + + for (int i = 0; i < oldArray.length ; i++){ + if (oldArray[i] == 0) + newLength ++; + } + + int [] newArray = new int [oldArray.length - newLength]; + int index = 0; + for (int j = 0; j < oldArray.length; j++){ + if (oldArray[j] != 0) + newArray[index++] = oldArray[j]; + } + + newArray = sort(newArray, 0, newArray.length - 1); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + + int newLength = array1.length + array2.length; + int [] newArray = new int [newLength]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + System.arraycopy(array2, 0, newArray, array1.length, array2.length); + Set arraySet = new HashSet(); + for (int i = 0; i < newArray.length; i++){ + arraySet.add(newArray[i]); + } + int [] tempArray = new int[arraySet.size()]; + Iterator iterator = arraySet.iterator(); + int index = 0; + while (iterator.hasNext()){ + tempArray[index++] = iterator.next(); + } + newArray = sort(tempArray, 0, tempArray.length - 1); + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int [] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, size); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if (max > 1){ + List array = new ArrayList(); + array.add(1); + array.add(1); + for(int i = 2;;i++){ + array.add(array.get(i - 1) + array.get(i - 2)); + if (array.get(i) > max){ + array.remove(i); + break; + } + } + int [] result = returnArray(array); + return result; + }else{ + int [] b = new int[]{}; + return b; + } + + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList list1 = new ArrayList(); + ArrayList list2 = new ArrayList(); + for (int i = 2; i < max; i++){ + list1.add(i); + } + + for (int m = 0; m < list1.size(); m++){ + boolean flag = true; + for (int n = 2; n < list1.get(m); n++){ + if (list1.get(m) % n == 0){ + flag = false; + break; + } + + } + if (flag) + list2.add(list1.get(m)); + } + + int [] result = returnArray(list2); + + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + ArrayList array = new ArrayList(); + Map> arrayMap = new HashMap>(); + ArrayList array2 = new ArrayList(); + for (int i = 2; i < max; i++){ + array.add(i); + } + for(int m = 0; m < array.size(); m++){ + ArrayList tempArray = new ArrayList(); + for (int n = 2; n < array.get(m);n++){ + if (array.get(m) % n == 0) + tempArray.add(n); + } + arrayMap.put(array.get(m), tempArray); + } + for(Map.Entry> entry: arrayMap.entrySet()){ + Integer key = entry.getKey(); + ArrayList tempArray = entry.getValue(); + Integer tempInt = 0; + for (Integer i:tempArray){ + tempInt += i; + } + if (key == tempInt){ + array2.add(key); + } + } + int [] result = returnArray(array2); + + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < array.length; i++){ + builder.append(array[i]); + builder.append(seperator); + } + builder.deleteCharAt(builder.length() - 1); + return builder.toString(); + } +//***************************工具类***************************************** + public int [] returnArray( List list){ + int [] result = new int[list.size()]; + int index = 0; + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + result[index++] = iterator.next(); + } + return result; + } + + //快速排序算法 + public int [] sort (int [] array, int low, int height ){ + + int start = low; + int end = height; + int key = array[low]; + + while (end > start){ + //从后往前遍历 + while (end > start && array[end] >= key) + end--; + + if (array[end] <= key){ + int temp = array[end]; + array[end] = array[start]; + array[start] = temp; + } + + //从前往后遍历 + while(end > start && array[start] <= key) + start++; + + if (array[start] >= key){ + int temp = array[start]; + array[start] = array[end]; + array[end] = temp; + } + } + + if(start > low)sort(array, low, start - 1); + if (end < height)sort(array, end + 1, height); + + return array; + } +//******************************************************************8 +} diff --git a/group16/1154151360/src/com/array/ArrayUtilTest.java b/group16/1154151360/src/com/array/ArrayUtilTest.java new file mode 100644 index 0000000000..159d497b6d --- /dev/null +++ b/group16/1154151360/src/com/array/ArrayUtilTest.java @@ -0,0 +1,102 @@ +package com.array; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +@SuppressWarnings("deprecation") +public class ArrayUtilTest { + + ArrayUtil util; + + @Before + public void init(){ + util = new ArrayUtil(); + } + + @Test + public void test_reverseArray() { + int [] a = {7, 9, 30, 3, 4}; + a = util.reverseArray(a); + Assert.assertEquals("[4,3,30,9,7]", toString(a)); + } + + @Test + public void test_removeZero(){ + int [] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + + oldArr = util.removeZero(oldArr); + + Assert.assertEquals("[1,3,4,5,6,6,5,4,7,6,7,5]", toString(oldArr)); + } + + + @Test + public void test_merge(){ + int [] a1 = {3, 5, 7,8}; + int [] a2 = {4, 5, 6,7}; + int [] a3 = util.merge(a1, a2); + + Assert.assertEquals("[3,4,5,6,7,8]", toString(a3)); + + } + + @Test + public void test_grow(){ + int [] oldArray = {2,3,6}; + int size = 3; + int [] newArray = util.grow(oldArray, size); + + Assert.assertEquals("[2,3,6,0,0,0]", toString(newArray)); + } + + + @Test + public void test_fibonacci(){ + + int [] array = util.fibonacci(15); + + Assert.assertEquals("[1,1,2,3,5,8,13]", toString(array)); + + } + + + @Test + public void test_getPrimes(){ + + int [] array = util.getPrimes(23); + Assert.assertEquals("[2,3,5,7,11,13,17,19]", toString(array)); + + } + + @Test + public void test_getPerfectNumbers(){ + int [] array = util.getPerfectNumbers(10); + + Assert.assertEquals("[6]", toString(array)); + + } + + @Test + public void test_join(){ + + int [] array = {3,8,9}; + String result = util.join(array, "-"); + Assert.assertEquals("3-8-9", result); + } + + + public String toString(int [] array){ + StringBuilder builder = new StringBuilder(); + builder.append("["); + for(int item: array){ + builder.append(item) + .append(","); + } + builder.replace(builder.length() - 1, builder.length(), ""); + builder.append("]"); + return builder.toString(); + + } +} diff --git a/group16/1154151360/src/com/litestruts/LoginAction.java b/group16/1154151360/src/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..24369135f9 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction { + + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group16/1154151360/src/com/litestruts/Struts.java b/group16/1154151360/src/com/litestruts/Struts.java new file mode 100644 index 0000000000..49254d6a5f --- /dev/null +++ b/group16/1154151360/src/com/litestruts/Struts.java @@ -0,0 +1,139 @@ +package com.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + + + //创建SAXreader对象 + SAXReader reader = new SAXReader(); + //将读取的文件封装成document对象 + Document document = reader.read(new File("src/com/litestruts/struts.xml")); + //获取文档的根节点 + Element root = document.getRootElement(); + + List elist = listNode(root); + + Map > actionMap = getAction(elist, actionName); + + for(Map.Entry> entity: actionMap.entrySet()){ + String className = entity.getKey(); + Class clazz = Class.forName(className); + Method setName = clazz.getMethod("setName", String.class); + Method setPassword = clazz.getMethod("setPassword", String.class); + Method getMessage = clazz.getMethod("getMessage"); + Method execute = clazz.getMethod("execute"); + Object object = clazz.newInstance(); + setName.invoke(object, parameters.get("name")); + setPassword.invoke(object, parameters.get("password")); + String status = (String) execute.invoke(object); + String message = (String) getMessage.invoke(object); + + String jsp = entity.getValue().get(status); + + Map parameter = new HashMap(); + + parameter.put("message", message); + + View view = new View(); + + view.setJsp(jsp); + view.setParameters(parameter); + + return view; + } + + return null; + } + + //获取节点的所有子节点 + private static List listNode(Element node){ + + Iterator iterator = node.elementIterator(); + List elist = new ArrayList(); + while (iterator.hasNext()){ + Element e = iterator.next(); + elist.add(e); + } + if (elist.isEmpty()) + return null; + else + return elist; + } + + //获取对应的Action信息 + private static Map > getAction(List elist,String actionName){ + + for (Element node:elist){ + List attributes = node.attributes(); + if (attributes.isEmpty()) + getAction(listNode(node),actionName); + else{ + Attribute attribute = node.attribute("name"); + if (attribute.getValue().equals(actionName)){ + String className = node.attribute("class").getValue(); + List childElements = listNode(node); + Map resMap =install(childElements); + Map > actionMap = new HashMap>(); + actionMap.put(className, resMap); + return actionMap; + } + } + } + return null; + } + + //组装action的result + private static Map install(List elements){ + Map resultMap = new HashMap(); + for (Element node: elements){ + Attribute attribute = node.attribute("name"); + String value; + if (node.getTextTrim().isEmpty()) + value = ""; + else + value = node.getTextTrim(); + + resultMap.put(attribute.getValue(), value); + + } + return resultMap; + } + +} diff --git a/group16/1154151360/src/com/litestruts/StrutsTest.java b/group16/1154151360/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9317cc5e26 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/StrutsTest.java @@ -0,0 +1,51 @@ +package com.litestruts; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import junit.framework.Assert; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} diff --git a/group16/1154151360/src/com/litestruts/View.java b/group16/1154151360/src/com/litestruts/View.java new file mode 100644 index 0000000000..af63dce301 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/View.java @@ -0,0 +1,23 @@ +package com.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/1154151360/src/com/litestruts/struts.xml b/group16/1154151360/src/com/litestruts/struts.xml new file mode 100644 index 0000000000..ddbcbc38da --- /dev/null +++ b/group16/1154151360/src/com/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java index e287419dc0..f82d064e2a 100644 --- a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java +++ b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java @@ -1,63 +1,63 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o) { - IncrementsCapacity(size + 1); - elementData[size++] = o; - - } - - public void add(int index, Object o) { - checkIndex(index); - IncrementsCapacity(size + 1); - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - size++; - } - - public Object get(int index) { - checkIndex(index); - return elementData[index]; - } - - public Object remove(int index) { - checkIndex(index); - Object o = elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - elementData[--size] = null; - return o; - } - - public int size() { - return size; - } - - public int length() { - return elementData.length; - } - - // - private void IncrementsCapacity(int num) { - if (num > elementData.length) { - int oldCapacity = elementData.length; // ǰ鳤 - int newCapacity = ((num + oldCapacity) * 3) >> 2; // ǰ鳤ȵ1.5 - if (newCapacity - num < 0) { - newCapacity = num; // Dz,ֱΪֵ - } - Object[] oldelements = elementData; - elementData = new Object[newCapacity]; - System.arraycopy(oldelements, 0, elementData, 0, size); - } - } - - // ±Խж - private void checkIndex(int index) { - if (index >= size || index < 0) - throw new IndexOutOfBoundsException("±Խ"); - } -} +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + IncrementsCapacity(size + 1); + elementData[size++] = o; + + } + + public void add(int index, Object o) { + checkIndex(index); + IncrementsCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public int length() { + return elementData.length; + } + + // + private void IncrementsCapacity(int num) { + if (num > elementData.length) { + int oldCapacity = elementData.length; // ǰ鳤 + int newCapacity = ((num + oldCapacity) * 3) >> 2; // ǰ鳤ȵ1.5 + if (newCapacity - num < 0) { + newCapacity = num; // Dz,ֱΪֵ + } + Object[] oldelements = elementData; + elementData = new Object[newCapacity]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + // ±Խж + private void checkIndex(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("±Խ"); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Iterator.java b/group16/1287642108/0226/src/com/coding/basic/Iterator.java index e7cbd474ec..ff93e30377 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Iterator.java +++ b/group16/1287642108/0226/src/com/coding/basic/Iterator.java @@ -1,6 +1,6 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java index 99c92fb9f1..fc206b4cec 100644 --- a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java +++ b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java @@ -1,101 +1,101 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private Node tail; - private int size = 0 ; - - public void add(Object o){ - addLast(o); - } - - public void add(int index, Object o) { - if (index == 0) { - addFirst(o); - } else if (index >= size) { - addLast(o); - } else { - Node node = new Node(); - node.data = o; - node.next = getNode(index); - getNode(index - 1).next = node; - size++; - } - } - - public Object get(int index) { - Node node = getNode(index); - return node.data; - } - - public Object remove(int index){ - Node currentNode = getNode(index); - Node prevNode = getNode(index - 1); - Node lastNode = getNode(index + 1); - prevNode.next = lastNode; - size--; - return currentNode.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node node=new Node(); - node.data = o; - node.next = head; - head = node; - size++; - } - public void addLast(Object o){ - Node node=new Node(); - node.data = o; - node.next = null; - Node lastNode = getNode(size-1); - lastNode.next = node; - size++; - } - public Object removeFirst(){ - Object obj = getNode(0).data; - Node node = getNode(1); - node.next = head; - size--; - return obj; - } - public Object removeLast(){ - Object obj = getNode(size - 1).data; - Node node = getNode(size - 2); - node.next = null; - size--; - return obj; - } - - //ȡڵ - public Node getNode(int index){ - checkIndex(index); - if(index == 0 ){ - return head; - }else if(index == size -1 ){ - return tail; - }else{ - Node node = head; - for(int i=0;i= size || index < 0) - throw new IndexOutOfBoundsException("±Խ"); - } - - private static class Node { - Object data; - Node next; - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size = 0 ; + + public void add(Object o){ + addLast(o); + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + } else if (index >= size) { + addLast(o); + } else { + Node node = new Node(); + node.data = o; + node.next = getNode(index); + getNode(index - 1).next = node; + size++; + } + } + + public Object get(int index) { + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + Node currentNode = getNode(index); + Node prevNode = getNode(index - 1); + Node lastNode = getNode(index + 1); + prevNode.next = lastNode; + size--; + return currentNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node=new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + Node node=new Node(); + node.data = o; + node.next = null; + Node lastNode = getNode(size-1); + lastNode.next = node; + size++; + } + public Object removeFirst(){ + Object obj = getNode(0).data; + Node node = getNode(1); + node.next = head; + size--; + return obj; + } + public Object removeLast(){ + Object obj = getNode(size - 1).data; + Node node = getNode(size - 2); + node.next = null; + size--; + return obj; + } + + //ȡڵ + public Node getNode(int index){ + checkIndex(index); + if(index == 0 ){ + return head; + }else if(index == size -1 ){ + return tail; + }else{ + Node node = head; + for(int i=0;i= size || index < 0) + throw new IndexOutOfBoundsException("±Խ"); + } + + private static class Node { + Object data; + Node next; + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/List.java b/group16/1287642108/0226/src/com/coding/basic/List.java index 10d13b5832..396b1f6416 100644 --- a/group16/1287642108/0226/src/com/coding/basic/List.java +++ b/group16/1287642108/0226/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Queue.java b/group16/1287642108/0226/src/com/coding/basic/Queue.java index 95dee3d81b..418e42826e 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Queue.java +++ b/group16/1287642108/0226/src/com/coding/basic/Queue.java @@ -1,28 +1,28 @@ -package com.coding.basic; - -public class Queue { - private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o){ - elementData.addLast(o); - } - - public Object deQueue(){ - if (isEmpty()) { - return null; - }else{ - return elementData.removeFirst(); - } - } - - public boolean isEmpty(){ - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + return null; + }else{ + return elementData.removeFirst(); + } + } + + public boolean isEmpty(){ + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Stack.java b/group16/1287642108/0226/src/com/coding/basic/Stack.java index c0b7da89f8..6138bc6973 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Stack.java +++ b/group16/1287642108/0226/src/com/coding/basic/Stack.java @@ -1,38 +1,38 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o) { - if (isEmpty()) { - elementData.add(elementData.length() - 1, o); - } - elementData.add(elementData.length() - elementData.size() - 1, o); - } - - public Object pop() { - if (isEmpty()) { - return null; - } - return elementData.remove(elementData.length() - elementData.size() - 1); - } - - public Object peek() { - if (isEmpty()) { - return null; - } - return elementData.get(elementData.length() - elementData.size() - 1); - } - - public boolean isEmpty() { - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size() { - return elementData.size(); - } - -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + if (isEmpty()) { + elementData.add(elementData.length() - 1, o); + } + elementData.add(elementData.length() - elementData.size() - 1, o); + } + + public Object pop() { + if (isEmpty()) { + return null; + } + return elementData.remove(elementData.length() - elementData.size() - 1); + } + + public Object peek() { + if (isEmpty()) { + return null; + } + return elementData.get(elementData.length() - elementData.size() - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..8e0d8af87b --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,205 @@ +package com.coderising.array; + +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = {7, 9 , 30, 3} , 置换后为 {3, 30, 9,7} 如果 a = + * {7, 9, 30, 3, 4} , 置换后为 {4,3, 30 , 9,7} + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] temp = new int[origin.length]; + int index = 0; + for (int i = origin.length - 1; i >= 0; i--) { + temp[index++] = origin[i]; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + int countZero = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] == 0){ + countZero++; + } + } + + int[] temp = new int[oldArray.length - countZero]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + temp[index++] = oldArray[i]; + } + } + return temp; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * {3, 5, 7,8} a2 = {4, 5, 6,7} 则 a3 为{3,4,5,6,7,8} , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public Integer[] merge(int[] array1, int[] array2) { + Integer[] temp = new Integer[array1.length + array2.length]; + int i = 0,j = 0; + int index = 0; + + while(i < array1.length && j < array2.length){ + if(array1[i] <= array2[j]){ + temp[index++] = array1[i++]; + }else{ + temp[index++] = array2[j++]; + } + } + + while(i < array1.length){ + temp[index++] = array1[i++]; + } + while(j < array2.length){ + temp[index++] = array2[j++]; + } + return removeRepetition(temp); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = {2,3,6} , size = 3,则返回的新数组为 + * {2,3,6,0,0,0} + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] temp = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, temp, 0, oldArray.length); + return temp; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max){ + int[] temp = new int[max]; + if(max == 1){ + return null; + } + int index = 0; + for(int i = 0;i temp = new ArrayList<>(); + for(int i=2; i < max; i++){ + isPrime = true; + for (int j = 2; j < i; j++){ + if ((i % j) == 0) { + isPrime = false; + break; + } + } + if(isPrime){ + temp.add(i); + } + } + int[] array = new int[temp.size()]; + int index = 0; + for(Integer te : temp){ + array[index++] = te; + } + return array; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static Integer[] getPerfectNumbers(int max) { + Integer[] temp = new Integer[max]; + int index = 0; + for (int i = 2; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + if (sum == i){ + temp[index++] = i; + } + } + } + return removeRepetition(temp); + } + + /** + * 用seperator 把数组 array给连接起来 例如array= {3,8,9}, seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String temp = ""; + for(int i = 0;i < array.length -1; i++){ + temp += array[i] + seperator; + } + temp += array[array.length-1]; + return temp; + } + + public static Integer[] removeRepetition(Integer[] oldArray){ + ArrayList temp = new ArrayList<>(); + for(int i=0;i parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + //创建SAXReader读取器,专门用于读取xml + SAXReader saxReader = new SAXReader(); + Document document = null; + try { + document = saxReader.read(new File("D:/DemoSpace/coding2017/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + //根据节点名称找节点 + Node node = root.selectSingleNode("action[@name='"+actionName+"']"); + String classPath = ((Element) node).attributeValue("class"); + //根据类名反射实例化 + Class onwClass = Class.forName(classPath); + Object o = onwClass.newInstance(); + Method setName = onwClass.getMethod("setName",String.class); + Method setPassword = onwClass.getMethod("setPassword",String.class); + Method execute = onwClass.getMethod("execute"); + Method getName = onwClass.getMethod("getName"); + Method getPassword = onwClass.getMethod("getPassword"); + Method getMessage = onwClass.getMethod("getMessage"); + setName.invoke(o,parameters.get("name")); + setPassword.invoke(o,parameters.get("password")); + String result = (String) execute.invoke(o); + //组装params参数 + HashMap map = new HashMap<>(); + String name = (String) getName.invoke(o); + String password = (String) getPassword.invoke(o); + String message = (String) getMessage.invoke(o); + map.put("name", name); + map.put("password", password); + map.put("message", message); + //组装view数据 + View view = new View(); + view.setParameters(map); + //根据execute的返回值,找对应的jsp页面路径 + String jspPath = node.valueOf("//result[@name='"+result+"']"); + view.setJsp(jspPath); + return view; + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..2a8ed76022 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/View.java b/group16/1287642108/0305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e22003fc12 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/1325756593/src/com/coderising/array/ArrayUtil.java b/group16/1325756593/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..0f67e6b54f --- /dev/null +++ b/group16/1325756593/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,273 @@ +package com.coderising.array; + +import static org.hamcrest.CoreMatchers.nullValue; + +import java.util.Arrays; + +import com.dong.week1.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if(origin==null||origin.length==0){ + return; + } + int len = origin.length; + for(int i=0;iarray2[index2]){ + arrayList.add(array2[index2]); + index2++; + }else if(array1[index1]==array2[index2]){ + arrayList.add(array2[index2]); + index1++; + index2++; + } + else{ + arrayList.add(array1[index1]); + index1++; + } + } + if(index1==len1){ + for(int i=index2;i=max){ + break; + } + arrayList.add(third); + first = second; + second=third; + + + } + return ListToArray(arrayList); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList arrayList = new ArrayList(); + for(int i=2;i resultAndViewMap; + public String getActioName() { + return actioName; + } + public void setActioName(String actioName) { + this.actioName = actioName; + } + @Override + public String toString() { + return "Action [actioName=" + actioName + ", className=" + className + ", resultAndViewMap=" + resultAndViewMap + + "]"; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public Map getResultAndViewMap() { + return resultAndViewMap; + } + public void setResultAndViewMap(Map resultAndViewMap) { + this.resultAndViewMap = resultAndViewMap; + } + + + + +} diff --git a/group16/1325756593/src/com/coderising/litestruts/LoginAction.java b/group16/1325756593/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group16/1325756593/src/com/coderising/litestruts/Struts.java b/group16/1325756593/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..ae6289d5f0 --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,246 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + + + +public class Struts { + + public static String strutsPath; + + + public static String getStrutsPath() { + return strutsPath; + } + + public static void setStrutsPath(String strutsPath) { + Struts.strutsPath = strutsPath; + } + + + + + public static View runAction(String actionName, Map parameters) throws ParserConfigurationException, SAXException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + if(strutsPath==null||strutsPath.equals("")){ + strutsPath = initStrutsPath(); + } + List actions = parseXml(strutsPath); + Action action = getAction(actions,actionName); + NotBeNull(action); + + + String actionClassName = action.getClassName(); + Class actionClass = Class.forName(actionClassName); + Object actionClassObject = actionClass.newInstance(); + + doSetterMethod(parameters, actionClass, actionClassObject); + + Method excuteMethod = actionClass.getMethod("execute", null); + String result = excuteMethod.invoke(actionClassObject, null).toString(); + String viewPath = action.getResultAndViewMap().get(result); + View view = new View(); + view.setJsp(viewPath); + Map parametersMap = doGetterMethod(actionClass, actionClassObject); + view.setParameters(parametersMap); + + return view; + } + + + /** + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + * @param actionClass + * @param actionClassObject + * @return + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static Map doGetterMethod(Class actionClass, Object actionClassObject) + throws IllegalAccessException, InvocationTargetException { + Map parametersMap = new HashMap<>(); + for(Method method :actionClass.getMethods()){ + if(method.getName().startsWith("get")){ + String getValue = getMethodValue(method.getName()); + parametersMap.put(getValue, method.invoke(actionClassObject, null)); + } + } + return parametersMap; + } + + /** + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + * @param parameters + * @param actionClass + * @param actionClassObject + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static void doSetterMethod(Map parameters, Class actionClass, Object actionClassObject) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for( Map.Entry entry: parameters.entrySet()){ + String param = entry.getKey(); + Method methodKeySet = actionClass.getMethod(getMethodSet(param), String.class); + methodKeySet.invoke(actionClassObject, entry.getValue()); + } + } + + /** + * 初始化strutsPath + * @return + */ + private static String initStrutsPath() { + String path = Struts.class.getClassLoader().getResource("").getPath(); + String strutsXmlPath = path+"/com/coderising/litestruts/struts.xml"; + return strutsXmlPath; + } + + + /** + * 通过name构造setName + * @param name + * @return + */ + public static String getMethodSet(String name){ + String firstChar = name.substring(0, 1).toUpperCase(); + return "set"+firstChar+name.substring(1); + } + + /** + * 获取getMessage对应的message + * @param name + * @return + */ + public static String getMethodValue(String name){ + if(name.length()>=3){ + name = name.substring(3); + } + String firstChar = name.substring(0, 1).toLowerCase(); + return firstChar+name.substring(1); + } + + + + /** + * 将xml解析成为Action的List结构 + * @param xmlPath + * @return + * @throws ParserConfigurationException + * @throws SAXException + * @throws IOException + */ + public static List parseXml(String xmlPath) throws ParserConfigurationException, SAXException, IOException{ + List retActionList = new ArrayList<>(); + File xmlFile = new File(xmlPath); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(xmlFile); + NodeList actionLists = doc.getElementsByTagName("action"); + for(int i=0;i resultAndViewMap=getResultAndViewNodeList(currentNode); + action.setResultAndViewMap(resultAndViewMap); + retActionList.add(action); + } + return retActionList; + } + + /** + * 获取该Node对应的result和对应的jsppath + * @param currentNode + * @param resultAndViewMap + */ + private static HashMap getResultAndViewNodeList(Node currentNode) { + Map resultAndViewMap = new HashMap<>(); + NodeList resultAndViewNodeList = currentNode.getChildNodes(); + + for(int j=0;j) resultAndViewMap; + } + + + private static void NotBeNull(Object result) { + if(result==null){ + throw new IllegalArgumentException(); + } + } + + + + public static String getAttribute(Node node ,String name){ + NamedNodeMap nameNodeMap = node.getAttributes(); + NotBeNull(nameNodeMap); + Node nameNode = nameNodeMap.getNamedItem(name); + NotBeNull(nameNode); + String actionName =nameNode.getNodeValue(); + return actionName; + } + + public static Action getAction(List actions ,String actionName){ + for(Action action:actions){ + if(action.getActioName().equals(actionName)){ + return action; + } + } + return null; + + } + + + +} diff --git a/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java b/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..487f05542c --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,48 @@ +package com.coderising.litestruts; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, ParserConfigurationException, SAXException, IOException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, ParserConfigurationException, SAXException, IOException { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/1325756593/src/com/coderising/litestruts/View.java b/group16/1325756593/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/1325756593/src/com/coderising/litestruts/struts.xml b/group16/1325756593/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/214074094/src/com/reading/blog_test.txt b/group16/214074094/src/com/reading/blog_test.txt deleted file mode 100644 index b7e5cbfe14..0000000000 --- a/group16/214074094/src/com/reading/blog_test.txt +++ /dev/null @@ -1 +0,0 @@ -just test new fork \ No newline at end of file diff --git a/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java b/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..c2b0c62a79 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java @@ -0,0 +1,213 @@ +package study.coderising.array; + + +import study.coding.basic.ArrayList; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + int tmp; + for (int i = 0; i < origin.length / 2; i++) { + tmp = origin[i]; + origin[i] = origin[origin.length - i - 1]; + origin[origin.length - i - 1] = tmp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray) { + int[] newArray = new int[oldArray.length]; + int k = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + newArray[k++] = oldArray[i]; + } + } + return Arrays.copyOf(newArray, k); + } + + /** + * 给定两个已经排序好的整形数组,a1和a2,创建一个新的数组a3,使得a3包含a1和a2的所有元素,并且仍然是有序的 + * 例如 a1 = {3, 5, 7, 8},a2 = {4, 5, 6, 7},则 a3 为[3,4,5,6,7,8],注意:已经消除了重复 + * + * @param a1 + * @param a2 + * @return + */ + + public static int[] merge(int[] a1, int[] a2) { + Set set = new HashSet(); + + for (int i = 0; i < a1.length + a2.length; i++) { + if (i < a1.length) { + set.add(a1[i]); + } else { + set.add(a2[i - a1.length]); + } + } + + int[] a3 = new int[set.size()]; + + Iterator it = set.iterator(); + int i = 0; + while (it.hasNext()) { + a3[i++] = (Integer) it.next(); + } + return a3; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + return Arrays.copyOf(oldArray, oldArray.length + size); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max == 1) { + return new int[0]; + } + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + for (int i = 0; ; i++) { + int tmp = list.get(i) + list.get(i + 1); + if (tmp >= max) { + break; + } + list.add(i + 2, tmp); + } + + return convertIntegerArray2Int(list); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + ArrayList list = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + return convertIntegerArray2Int(list); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max < 2) { + return null; + } + ArrayList list = new ArrayList<>(); + + int count = 0; + //如果p是质数,且2^p-1也是质数,那么(2^p-1)X2^(p-1)便是一个完全数 + for (int i = 2; i <= max / 2; i++) { + count++; + if (isPrime(i) && isPrime((int) Math.pow(2, i) - 1)) { + System.out.println("count " + i + ":" + (int) (Math.pow(2, i) - 1) + " * " + (int) Math.pow(2, (i - 1))); + int perfectNum = (int) ((Math.pow(2, i) - 1) * Math.pow(2, (i - 1))); + if (perfectNum > max) { + break; + } + if (perfectNum < max) { + list.add(perfectNum); + } + } + } + System.out.println("total count : " + count); + return convertIntegerArray2Int(list); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (null == array || array.length < 1) { + return null; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]).append(seperator); + } + return sb.substring(0, sb.length() - 1); + } + + private static boolean isPrime(int n) { + if (n == 2) { + return true; + } + for (int j = 2; j <= Math.sqrt(n); j++) { + if (n % j == 0) { + return false; + } + } + return true; + } + + private static int[] convertIntegerArray2Int(ArrayList list) { + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i).intValue(); + } + return arr; + } + + +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java b/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..043df200d8 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package study.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author liuxin + */ +public class LoginAction { + + private String name; + private String password; + private String message; + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java b/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..e3a10a6726 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java @@ -0,0 +1,39 @@ +package study.coderising.litestruts; + +/** + * @Author shane + * @Time 2017/3/4 12:13 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class LogoutAction { + + private String name; + + private String message; + + public String execute() { + if ("test".equalsIgnoreCase(name)) { + message = name + " logout success"; + return "success"; + } + message = name + " logout fail"; + return "error"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java b/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..9a70e7a525 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java @@ -0,0 +1,147 @@ +package study.coderising.litestruts; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import study.coderising.litestruts.bean.Action; +import study.coderising.litestruts.bean.Result; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +public class Struts { + + private static final String EXECUTE = "execute"; + + public static View runAction(String actionName, Map parameters) + throws DocumentException, ClassNotFoundException, NoSuchMethodException, + InvocationTargetException, IllegalAccessException, InstantiationException { + + //0. 读取配置文件struts.xml + SAXReader reader = new SAXReader(); + Document doc = reader.read("./src/main/resources/struts.xml"); + Element root = doc.getRootElement(); + Action action = getAction(actionName, root); + + //1. 根据actionName找到相对应的class + Class clz = Class.forName(action.getClassPath()); + //获取一个实例,方便接下来对同一个对象赋值 + Object obj = clz.newInstance(); + + //1.1 据parameters中的数据,调用对象的setter方法 + for (Map.Entry entry : parameters.entrySet()) { + String setFunctionName = getFunctionName("set", entry.getKey()); + Method set = clz.getDeclaredMethod(setFunctionName, String.class); + set.invoke(obj, entry.getValue()); + } + + //2. 通过反射调用对象的exectue方法, 并获得返回值 + Method execute = clz.getDeclaredMethod(EXECUTE); + String response = execute.invoke(obj).toString(); + + //3. 通过反射找到对象的所有getter方法并调用, 把值和属性形成一个HashMap + Method[] methods = clz.getDeclaredMethods(); + Map map = new HashMap<>(); + for (Method m : methods) { + if (m.getName().startsWith("get")) { + String paramName = m.getName().replaceFirst("get", ""); + map.put(paramName.toLowerCase(), m.invoke(obj)); + } + } + + //3.1 放到View对象的parameters + View view = new View(); + view.setParameters(map); + + //4. 根据struts.xml中的配置,以及execute的返回值,确定jsp,放到View对象的jsp字段中。 + for (Result result : action.getResults()) { + if (response.equalsIgnoreCase(result.getResult())) { + view.setJsp(result.getJumpPath()); + break; + } + } + + return view; + } + + /** + * @Author: shane + * @Time: 2017/3/4 23:53 + * @Email: stevenchenguang@gmail.com + * @param: begin, key + * @Return: String + * @Throw: + * @Desc: 根据开始名称和key获取方法名 + * e.g.: begin: get, key: name, return getName + */ + private static String getFunctionName(String begin, String key) { + if (key == null || "".equals(key)) { + return null; + } + StringBuffer sb = new StringBuffer(begin); + if (key.length() < 2) { + sb.append(key.toUpperCase()); + } else { + String first = String.valueOf(key.charAt(0)); + sb.append(first.toUpperCase()); + sb.append(key.substring(1, key.length())); + } + return sb.toString(); + } + + /** + * @Author: shane + * @Time: 2017/3/4 23:55 + * @Email: stevenchenguang@gmail.com + * @param: actionName, node + * @Return: Action + * @Throw: + * @Desc: 根据actionName和xml节点获取Action + */ + private static Action getAction(String actionName, Element node) { + Action action = new Action(); + + Iterator iterator = node.elementIterator(); + boolean flag = false; + while (iterator.hasNext()) { + Element e = iterator.next(); + List list = e.attributes(); + + //遍历属性节点 + for (Attribute attr : list) { + if ("name".equalsIgnoreCase(attr.getName())) { + if (!actionName.equalsIgnoreCase(attr.getValue())) { + continue; + } else { + flag = true; + } + action.setName(attr.getValue()); + } + if ("class".equalsIgnoreCase(attr.getName())) { + action.setClassPath(attr.getValue()); + } + + List results = new ArrayList<>(); + + Iterator it = e.elementIterator(); + while (it.hasNext()) { + Result result = new Result(); + Element el = it.next(); + result.setResult(el.attribute(0).getValue()); + result.setJumpPath(el.getText()); + results.add(result); + } + action.setResults(results); + } + if (flag) { + break; + } + } + return action; + } + +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java b/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..db1b37de1b --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java @@ -0,0 +1,109 @@ +package study.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "123456"); //密码和预设的不一致 + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + @Test + public void testLogoutSuccess() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name", "test"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("test logout success", view.getParameters().get("message")); + } + + @Test + public void testLogoutFail() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name", "unknownUser"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("unknownUser logout fail", view.getParameters().get("message")); + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/View.java b/group16/214074094/src/main/java/study/coderising/litestruts/View.java new file mode 100644 index 0000000000..af3df5b9e9 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package study.coderising.litestruts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameters; + } + + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java new file mode 100644 index 0000000000..12aa461cfc --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java @@ -0,0 +1,43 @@ +package study.coderising.litestruts.bean; + + +import java.util.List; + +/** + * @Author shane + * @Time 2017/3/4 21:49 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class Action { + + private String name; + + private String classPath; + + private List results; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassPath() { + return classPath; + } + + public void setClassPath(String classPath) { + this.classPath = classPath; + } + + public List getResults() { + return results; + } + + public void setResults(List results) { + this.results = results; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java new file mode 100644 index 0000000000..649844ff31 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java @@ -0,0 +1,30 @@ +package study.coderising.litestruts.bean; + +/** + * @Author shane + * @Time 2017/3/4 21:50 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class Result { + + private String result; + + private String jumpPath; + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + + public String getJumpPath() { + return jumpPath; + } + + public void setJumpPath(String jumpPath) { + this.jumpPath = jumpPath; + } +} diff --git a/group16/214074094/src/com/coding/basic/ArrayList.java b/group16/214074094/src/main/java/study/coding/basic/ArrayList.java similarity index 84% rename from group16/214074094/src/com/coding/basic/ArrayList.java rename to group16/214074094/src/main/java/study/coding/basic/ArrayList.java index 158c866d45..ebe04dd177 100644 --- a/group16/214074094/src/com/coding/basic/ArrayList.java +++ b/group16/214074094/src/main/java/study/coding/basic/ArrayList.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; import java.util.Arrays; @@ -10,7 +10,7 @@ * @Email stevenchenguang@gmail.com * @Desc OwnArrayList */ -public class ArrayList implements List { +public class ArrayList implements List { private int size = 0; @@ -28,22 +28,22 @@ public ArrayList() { } @Override - public void add(Object o) { + public void add(E e) { if (elementData == EMPTY_ELEMENTDATA) { elementData = Arrays.copyOf(elementData, DEFAULT_CAPACITY); - elementData[0] = o; + elementData[0] = e; } else if (size < elementData.length) { - elementData[size] = o; + elementData[size] = e; } else { _grow(); - elementData[size] = o; + elementData[size] = e; } size++; _analyze(); } @Override - public void add(int index, Object o) { + public void add(int index, E e) { if (index < 0) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } @@ -52,40 +52,40 @@ public void add(int index, Object o) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } else { elementData = new Object[DEFAULT_CAPACITY]; - elementData[0] = o; + elementData[0] = e; } } else if (index > size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } else if (index == size) { _grow(); - elementData[size] = o; + elementData[size] = e; size++; } else { if (elementData.length == size) { _grow(); } System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; + elementData[index] = e; size++; } _analyze(); } @Override - public Object get(int index) { - if (index < 0 || index > size) { + public E get(int index) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } - return elementData[index]; + return (E) elementData[index]; } @Override - public Object remove(int index) { + public E remove(int index) { - if (index < 0 || index > size) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } - Object oldValue = elementData[index]; + E oldValue = (E) elementData[index]; //需要复制的长度 int needMoveLength = size - index - 1; //如果该长度小于0, 说明只有一个元素, 直接置空即可 @@ -102,6 +102,11 @@ public int size() { return size; } + @Override + public E[] toArray() { + return (E[]) elementData; + } + public Iterator iterator() { return new ArrayListItrator(); } diff --git a/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java b/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4eae81c97a --- /dev/null +++ b/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java @@ -0,0 +1,95 @@ +package study.coding.basic; + +/** + * @Author shane + * @Time 2017/2/26 19:30 + * @Email stevenchenguang@gmail.com + * @Desc Own BinaryTreeNode + */ +public class BinaryTreeNode { + + private Object data; + + private BinaryTreeNode left; + + private BinaryTreeNode right; + + public BinaryTreeNode insert(Object o) { + if (null == data) { + data = o; + return this; + } + if (bigger(data, o)) { + if (null == left) { + left = new BinaryTreeNode(); + left.data = o; + } else { + left.insert(o); + } + } else if (smaller(data, o)) { + if (null == right) { + right = new BinaryTreeNode(); + right.data = o; + } else { + right.insert(o); + } + } else { + throw new RuntimeException("The value has exists"); + } + return this; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + private boolean bigger(Object data1, Object data2) { + return data1.toString().compareTo(data2.toString()) > 0; + } + + private boolean smaller(Object data1, Object data2) { + return data1.toString().compareTo(data2.toString()) < 0; + } + + private ArrayList list = new ArrayList(); + + /** + * 对二叉树进行遍历 结果存储到list中 + */ + private void sort(BinaryTreeNode node) { + + list.add(node.data); + if(null != node.left){ + sort(node.left); + } + if(null != node.right){ + sort(node.right); + } + } + + @Override + public String toString() { + sort(this); + return list.toString(); + } +} diff --git a/group16/214074094/src/main/java/study/coding/basic/Iterator.java b/group16/214074094/src/main/java/study/coding/basic/Iterator.java new file mode 100644 index 0000000000..0befe6a209 --- /dev/null +++ b/group16/214074094/src/main/java/study/coding/basic/Iterator.java @@ -0,0 +1,9 @@ +package study.coding.basic; + +public interface Iterator { + + boolean hasNext(); + + Object next(); + +} diff --git a/group16/214074094/src/com/coding/basic/LinkedList.java b/group16/214074094/src/main/java/study/coding/basic/LinkedList.java similarity index 97% rename from group16/214074094/src/com/coding/basic/LinkedList.java rename to group16/214074094/src/main/java/study/coding/basic/LinkedList.java index 9108c2b6fe..b6fede6824 100644 --- a/group16/214074094/src/com/coding/basic/LinkedList.java +++ b/group16/214074094/src/main/java/study/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane @@ -50,6 +50,7 @@ public Object remove(int index) { } else if (index == size - 1) { return removeLast(); } + Node curr = _node(index); Object data = curr.data; final Node prev = curr.prev; @@ -120,6 +121,11 @@ public int size() { return size; } + @Override + public Object[] toArray() { + return new Object[0]; + } + public Iterator iterator() { return null; } diff --git a/group16/214074094/src/main/java/study/coding/basic/List.java b/group16/214074094/src/main/java/study/coding/basic/List.java new file mode 100644 index 0000000000..e62dd86d52 --- /dev/null +++ b/group16/214074094/src/main/java/study/coding/basic/List.java @@ -0,0 +1,16 @@ +package study.coding.basic; + +public interface List { + + void add(E e); + + void add(int index, E e); + + E get(int index); + + E remove(int index); + + int size(); + + E[] toArray(); +} diff --git a/group16/214074094/src/com/coding/basic/Queue.java b/group16/214074094/src/main/java/study/coding/basic/Queue.java similarity index 95% rename from group16/214074094/src/com/coding/basic/Queue.java rename to group16/214074094/src/main/java/study/coding/basic/Queue.java index 869d0f7333..b15950ac36 100644 --- a/group16/214074094/src/com/coding/basic/Queue.java +++ b/group16/214074094/src/main/java/study/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane diff --git a/group16/214074094/src/com/coding/basic/Stack.java b/group16/214074094/src/main/java/study/coding/basic/Stack.java similarity index 95% rename from group16/214074094/src/com/coding/basic/Stack.java rename to group16/214074094/src/main/java/study/coding/basic/Stack.java index 7ef1c9ad06..188de74ee9 100644 --- a/group16/214074094/src/com/coding/basic/Stack.java +++ b/group16/214074094/src/main/java/study/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane diff --git a/group16/214074094/src/main/java/study/reading/blog.txt b/group16/214074094/src/main/java/study/reading/blog.txt new file mode 100644 index 0000000000..f112f8ea6a --- /dev/null +++ b/group16/214074094/src/main/java/study/reading/blog.txt @@ -0,0 +1 @@ +学习-CPU、内存、硬盘、指令及它们之间的关系: https://stevenshane.github.io/study/2017/02/27/coding2017-reading.html \ No newline at end of file diff --git a/group16/214074094/src/main/resources/struts.xml b/group16/214074094/src/main/resources/struts.xml new file mode 100644 index 0000000000..f93213e241 --- /dev/null +++ b/group16/214074094/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/214074094/src/test/coding/basic/AbstractTest.java b/group16/214074094/src/test/java/study/AbstractTest.java similarity index 71% rename from group16/214074094/src/test/coding/basic/AbstractTest.java rename to group16/214074094/src/test/java/study/AbstractTest.java index 80eaaa6fe5..42737e270f 100644 --- a/group16/214074094/src/test/coding/basic/AbstractTest.java +++ b/group16/214074094/src/test/java/study/AbstractTest.java @@ -1,4 +1,6 @@ -package coding.basic; +package study; + +import com.alibaba.fastjson.JSON; /** * @Author shane @@ -16,4 +18,7 @@ protected void printHyphen() { System.out.println("--------------------------------------------"); } + protected void printJson(Object obj) { + System.out.println(JSON.toJSONString(obj)); + } } diff --git a/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java b/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java new file mode 100644 index 0000000000..24cdeadb64 --- /dev/null +++ b/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java @@ -0,0 +1,82 @@ +package study.coderising; + +import org.junit.Assert; +import org.junit.Test; +import study.AbstractTest; +import study.coderising.array.ArrayUtil; + +/** + * @Author shane + * @Time 2017/3/1 20:29 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class ArrayUtilTest extends AbstractTest { + + @Test + public void testReverseArray(){ + int[] a = {7, 9 , 30, 3}; + printJson(a); + ArrayUtil.reverseArray(a); + printJson(a); + } + + @Test + public void testremoveZero(){ + int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int newArr[] = ArrayUtil.removeZero(oldArr); + printJson(oldArr); + printJson(newArr); + } + + @Test + public void testMerge(){ + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + + int[] a3 = ArrayUtil.merge(a1, a2); + printJson(a1); + printJson(a2); + printJson(a3); + } + + @Test + public void testGrow(){ + int[] oldArray = {2,3,6}; + printJson(oldArray); + int[] newArray = ArrayUtil.grow(oldArray, 3); + printJson(newArray); + } + + @Test + public void testFibonacci(){ + int[] onlyOne = {}; + int[] before15 = {1, 1, 2, 3, 5, 8, 13}; + int[] before21 = {1, 1, 2, 3, 5, 8, 13}; + Assert.assertArrayEquals(onlyOne, ArrayUtil.fibonacci(1)); + Assert.assertArrayEquals(before15, ArrayUtil.fibonacci(15)); + Assert.assertArrayEquals(before21, ArrayUtil.fibonacci(21)); + } + + @Test + public void testGetPrimes(){ + int[] _19 = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(_19, ArrayUtil.getPrimes(23)); + int[] _32 = {2,3,5,7,11,13,17,19,23,29,31}; + Assert.assertArrayEquals(_32, ArrayUtil.getPrimes(32)); + } + + @Test + public void testGetPerfectNumbers(){ + int[] arr = {6,28,496,8128,33550336}; + Assert.assertArrayEquals(arr, ArrayUtil.getPerfectNumbers(33550337)); + } + + @Test + public void testJoin(){ + int[] arr = {3,8,9}; + String seperator = "-"; + Assert.assertEquals("3-8-9", ArrayUtil.join(arr, seperator)); + } + +} diff --git a/group16/214074094/src/test/coding/basic/ArrayListTest.java b/group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java similarity index 96% rename from group16/214074094/src/test/coding/basic/ArrayListTest.java rename to group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java index 2f03342d61..6ffe2e5715 100644 --- a/group16/214074094/src/test/coding/basic/ArrayListTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java b/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..370b339434 --- /dev/null +++ b/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,33 @@ +package study.coding.basic; + +import org.junit.Test; +import study.AbstractTest; + +/** + * @Author shane + * @Time 2017/2/26 19:57 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class BinaryTreeNodeTest extends AbstractTest { + + @Test + public void test(){ + BinaryTreeNode node = new BinaryTreeNode(); + node.insert(8); + node.insert(5); + node.insert(9); + node.insert(1); + node.insert(6); + node.insert(11); + node.insert(10); + node.insert(15); + node.insert(13); + node.insert(19); + + printStar(); + System.out.println(node.getData()); + System.out.println(node); + printStar(); + } +} diff --git a/group16/214074094/src/test/coding/basic/LinkedListTest.java b/group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java similarity index 96% rename from group16/214074094/src/test/coding/basic/LinkedListTest.java rename to group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java index bc78728a25..f22d1cc611 100644 --- a/group16/214074094/src/test/coding/basic/LinkedListTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/coding/basic/QueueTest.java b/group16/214074094/src/test/java/study/coding/basic/QueueTest.java similarity index 95% rename from group16/214074094/src/test/coding/basic/QueueTest.java rename to group16/214074094/src/test/java/study/coding/basic/QueueTest.java index 12302783b3..cdfe5c95ab 100644 --- a/group16/214074094/src/test/coding/basic/QueueTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/QueueTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/coding/basic/StackTest.java b/group16/214074094/src/test/java/study/coding/basic/StackTest.java similarity index 95% rename from group16/214074094/src/test/coding/basic/StackTest.java rename to group16/214074094/src/test/java/study/coding/basic/StackTest.java index f289744a67..8269967dff 100644 --- a/group16/214074094/src/test/coding/basic/StackTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/StackTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/2816977791/.gitignore b/group16/2816977791/.gitignore new file mode 100644 index 0000000000..4ede1404d2 --- /dev/null +++ b/group16/2816977791/.gitignore @@ -0,0 +1,181 @@ +# Created by https://www.gitignore.io/api/java,intellij,eclipse,emacs,svn,maven + +### Java ### +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/workspace.xml +.idea/tasks.xml +.idea/dictionaries +.idea/vcs.xml +.idea/jsLibraryMappings.xml +*.idea + +# Sensitive or high-churn files: +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml + +# Gradle: +.idea/gradle.xml +.idea/libraries + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +*.iml + + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +### SVN ### +.svn/ + + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +Servers/ diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java b/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7ac196b700 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java @@ -0,0 +1,94 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkForLength(index); + + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkForLength(index); + return elementData[index]; + } + + public Object remove(int index) { + checkForLength(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return oldValue; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private void checkForLength(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("out of memory"); + } + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1);//增大容量 + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } + elementData = copyOf(elementData, newCapacity); + } + + private Object[] copyOf(Object[] src, int newCapacity) { + Object[] target = new Object[newCapacity]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + String num = "num"; + for (int i = 0; i < 100; i++) { + list.add(num + String.valueOf(i)); + System.out.println(String.valueOf(i) + ":size:" + list.size()); + System.out.println(String.valueOf(i) + ":length:" + list.elementData.length); + } + System.out.println(list.size()); + + for (int i = 0; i < 100; i++) { + list.add(i, num + String.valueOf(i)); + System.out.println(String.valueOf(i) + ":size:" + list.size()); + System.out.println(String.valueOf(i) + ":length:" + list.elementData.length); + } + System.out.println(list.size()); + + for (int i = 0; i < 200; i++) { + System.out.println(list.remove(0)); + } + + System.out.println(list.size()); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java b/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e34a68b3c2 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(o, null, null); + if (root == null) { + root = binaryTreeNode; + } else { + add(o, root); + } + return binaryTreeNode; + } + + private BinaryTreeNode add(Object o, BinaryTreeNode target) { + if (target == null) { + target = new BinaryTreeNode(o, null, null); + } else { + if (compare(o, target.data) > 0) { + target.right = add(o, target.right); + } else if (compare(o, target.data) < 0) { + target.left = add(o, target.left); + } + } + return target; + } + + private int compare(Object src, Object target) { + return ((String) src).compareTo((String) target); + } + +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java b/group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java b/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..be5b236700 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node last; + + private int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkPosition(index); + if (index == size) { + addLast(o); + } else { + addIndex(index, o); + } + } + + public Object get(int index) { + return node(index).data; + } + + public Object remove(int index) { + checkPosition(index); + Object old = get(index); + if (index == 0) { + removeFirst(); + } else if (index == size - 1) { + removeLast(); + } else { + node(index - 1).next = node(index + 1); + size--; + } + return old; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node h = head; + Node newNode = new Node(o, h); + head = newNode; + if (h == null) { + last = newNode; + } + size++; + } + + public void addLast(Object o) { + Node l = last; + Node newNode = new Node(o, null); + last = newNode; + if (l == null) { + head = newNode; + } else { + l.next = newNode; + } + size++; + } + + public Object removeFirst() { + Node old = head; + head = old.next; + size--; + return old.data; + } + + public Object removeLast() { + Node old = last; + Node prev = node(size - 2); + last = prev; + size--; + return old.data; + } + + public Iterator iterator() { + return null; + } + + private void checkPosition(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("out of memory"); + } + } + + private void addIndex(int index, Object o) { + checkPosition(index); + Node newNode = new Node(o, node(index)); + if (index != 0) { + node(index - 1).next = newNode; + } else { + head = newNode; + } + size++; + } + + private Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.addLast("last"); + System.out.println(list.size()); + list.addFirst("head"); + System.out.println(list.size()); + list.add(0, "0Object"); + System.out.println(list.size()); + list.add(2, "2Object"); + System.out.println(list.size()); + list.removeLast(); + System.out.println(list.size()); + list.removeFirst(); + System.out.println(list.size()); + list.removeLast(); + System.out.println(list.size()); + list.get(0); + System.out.println(list.size()); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/List.java b/group16/2816977791/firstExercise/src/com/coding/basic/List.java new file mode 100644 index 0000000000..01398944e6 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java b/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..f2475fcfd6 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java b/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..12a89eb613 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(0, o); + } + + public Object pop() { + return elementData.remove(0); + } + + public Object peek() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java b/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..3572229739 --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,258 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int i = 0; + while (i < origin.length - 1 - i) { + int temp = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = origin[i]; + origin[i] = temp; + i++; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int point = 0; + for (int i : oldArray) { + if (i != 0) { + oldArray[point++] = i; + } + } + int[] newArray = new int[point]; + System.arraycopy(oldArray, 0, newArray, 0, point); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + //两个指针 + int pos1 = 0; + int pos2 = 0; + int point = 0; + int[] newArray = new int[array1.length + array2.length]; + while (pos1 < array1.length && pos2 < array2.length) { + if (array1[pos1] > array2[pos2]) { + newArray[point++] = array2[pos2++]; + } else if (array1[pos1] < array2[pos2]) { + newArray[point++] = array1[pos1++]; + } else { + newArray[point++] = array1[pos1++]; + pos2++; + } + } + while (pos1 < array1.length) { + newArray[point++] = array1[pos1++]; + } + while (pos1 < array2.length) { + newArray[point++] = array1[pos2++]; + } + int[] array = new int[point]; + System.arraycopy(newArray, 0, array, 0, point); + return array; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int a = 0; + int b = 1; + int[] array = new int[max]; + int i = 0; + if (max >= 2) { + array[i++] = 1; + } + while (a + b < max) { + int temp = b; + b = a + b; + a = temp; + array[i++] = b; + } + int[] newArray = new int[i]; + System.arraycopy(array, 0, newArray, 0, i); + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] array = new int[max / 2 + 1]; + int pos = 0; + for (int i = 1; i < max; i++) { + if (prime(i)) { + array[pos++] = i; + } + } + int[] newArray = new int[pos]; + System.arraycopy(array, 0, newArray, 0, pos); + return newArray; + } + + private boolean prime(int value) { + if (value < 2) { + return false; + } else if (value == 2) { + return true; + } else { + for (int i = 2; i < value / 2 + 1; i++) { + if (value % 2 == 0) { + return false; + } + } + return true; + } + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int pos = 0; + int[] array = new int[max]; + for (int i = 1; i < max; i++) { + if (perfectNumber(i)) { + array[pos++] = i; + } + } + int[] newArray = new int[pos]; + System.arraycopy(array, 0, newArray, 0, pos); + return newArray; + } + + private boolean perfectNumber(int value) { + if (value == 1 || prime(value)) { + return false; + } else { + int sum = 0; + for (int i = 1; i <= value / 2; i++) { + if (value % i == 0) { + sum += i; + } + } + if (sum == value) { + return true; + } + return false; + } + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + String out = ""; + for (int i = 0; i < array.length; i++) { + if (i == 0) { + out += String.valueOf(i); + } else { + out += seperator + String.valueOf(i); + } + } + return out; + } + + + public static void main(String[] args) { + ArrayUtil arrayUtil = new ArrayUtil(); + //reverse + int[] a = {7, 9, 30, 3}; + arrayUtil.reverseArray(a); + System.out.println(a); + + //remove zero + int[] zero = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int[] afterZero = arrayUtil.removeZero(zero); + System.out.println(afterZero); + + //merge + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] merge = arrayUtil.merge(a1, a2); + System.out.println(merge); + + //grow + int[] oldArray = {2, 3, 6}; + int[] grow = arrayUtil.grow(oldArray, 3); + System.out.println(grow); + + //fibonacci + int[] fibonacci = arrayUtil.fibonacci(2); + System.out.println(fibonacci); + + //primes + int[] primes = arrayUtil.getPrimes(15); + System.out.println(primes); + + //perfect + int[] perfect = arrayUtil.getPerfectNumbers(500); + System.out.println(perfect); + + //join + int[] joinArray = {2}; + String join = arrayUtil.join(joinArray, "-"); + System.out.println(join); + + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java new file mode 100644 index 0000000000..55988befbe --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.util.Map; + +/** + * @author nvarchar + * date 2017/3/1 + */ +public class ActionXml { + private String name; + private String className; + private Map map; + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..f169cb1cbb --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,150 @@ +package com.coderising.litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + private static Map actionXmlMap; + + static { + //0. 读取配置文件struts.xml + actionXmlMap = parserXml("src/com/coderising/litestruts/struts.xml"); + System.out.println("parse end"); + } + + public static View runAction(String actionName, Map parameters) { + + try { + ActionXml actionXml = actionXmlMap.get(actionName); + if (actionName == null) { + return null; + } else { + String className = actionXml.getClassName(); + Class cls = Class.forName(className); + Object obj = cls.newInstance(); + /** + 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + */ + Method[] methods = cls.getMethods(); + for (String field : parameters.keySet()) { + for (Method method : methods) { + if (method.getName().startsWith("set") && method.getName().toLowerCase().endsWith(field.toLowerCase())) { + method.invoke(obj, parameters.get(field)); + break; + } + } + } + Method action = cls.getMethod("execute"); + //通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + String result = (String) action.invoke(obj); + /** + 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + */ + Field[] fields = cls.getDeclaredFields(); + Map params = new HashMap<>(); + for (Field field : fields) { + for (Method method : methods) { + if (method.getName().startsWith("get") && method.getName().toLowerCase().endsWith(field.getName().toLowerCase())) { + String viewResult = (String) method.invoke(obj); + params.put(field.getName(), viewResult); + break; + } + } + } + /** + 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + View view = new View(); + view.setParameters(params); + view.setJsp(actionXmlMap.get(actionName).getMap().get(result)); + return view; + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return null; + } catch (InstantiationException e) { + e.printStackTrace(); + return null; + } catch (IllegalAccessException e) { + e.printStackTrace(); + return null; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return null; + } catch (InvocationTargetException e) { + e.printStackTrace(); + return null; + } + } + + //解析xml文件 + private static Map parserXml(String fileName) { + try { + Map map = new HashMap<>(); + //create documentBuilder + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + //create document + Document document = db.parse(fileName); + //extract root element + Element root = document.getDocumentElement(); + System.out.println("Root element :" + document.getDocumentElement().getNodeName()); + NodeList nodeList = document.getElementsByTagName("action"); + System.out.println("----------------------------"); + for (int temp = 0; temp < nodeList.getLength(); temp++) { + Node nNode = nodeList.item(temp); + System.out.println("\nCurrent Element :" + + nNode.getNodeName()); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + ActionXml actionXml = new ActionXml(); + Element eElement = (Element) nNode; + System.out.println("action name : " + + eElement.getAttribute("name")); + System.out.println("class name : " + + eElement.getAttribute("class")); + actionXml.setName(eElement.getAttribute("name")); + actionXml.setClassName(eElement.getAttribute("class")); + NodeList result = eElement.getElementsByTagName("result"); + Map results = new HashMap<>(); + for (int i = 0; i < result.getLength(); i++) { + Node resultNode = result.item(i); + if (resultNode.getNodeType() == Node.ELEMENT_NODE) { + Element resultElement = (Element) resultNode; + System.out.println("result name:" + resultElement.getAttribute("name")); + System.out.println("result context:" + resultElement.getTextContent()); + results.put(resultElement.getAttribute("name"), resultElement.getTextContent()); + } + actionXml.setMap(results); + } + map.put(actionXml.getName(), actionXml); + } + } + return map; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static void main(String[] args) { +// parserXml("src/com/coderising/litestruts/struts.xml"); + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml b/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java index eaaa690fa6..3bec144013 100644 Binary files a/group16/313001956/src/com/coding/basic/ArrayList.java and b/group16/313001956/src/com/coding/basic/ArrayList.java differ diff --git a/group16/313001956/src/com/coding/basic/LinkedList.java b/group16/313001956/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..de886c9084 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Խ磡"); + } + if (head == null) { + head = new Node(); + head.data = o; + } else { + + Node n = getNodebyIndex(index); + + Node newnode = new Node(); + newnode.data = o; + newnode.next = n.next; + n.next = newnode; + } + size++; + } + + public Object get(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Խ磡"); + } + if (head == null) + return null; + Node n = getNodebyIndex(index); + return n.data; + } + + public Object remove(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Խ磡"); + } + Node n = getNodebyIndex(index - 1); + Object o = n.next.data; + n.next = n.next.next; + size--; + return o; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + } else { + Node newnode = new Node(); + newnode.data = o; + newnode.next = head; + head = newnode; + } + size++; + } + + public void addLast(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + } else { + Node n = getNodebyIndex(size); + Node newnode = new Node(); + newnode.data = o; + n.next = newnode; + } + size++; + } + + public Object removeFirst() { + if (head == null) + return null; + + head = head.next; + size--; + return head.data; + } + + public Object removeLast() { + if (head == null) + return null; + Node n = getNodebyIndex(size - 1); + Object o = n.next.data; + n.next = null; + size--; + return o; + } + + public Iterator iterator() { + return null; + } + + private Node getNodebyIndex(int index) { + int i = 0; + Node n = head; + while (i < index) { + n = n.next; + i++; + } + return n; + } + + private static class Node { + Object data; + Node next; + + Object Getdata() { + return data; + } + + void Setdata(Object o) { + data = o; + } + + Node Getnext() { + return next; + } + + void Setnext(Node n) { + next = n; + } + } +} + diff --git a/group16/313001956/src/com/coding/basic/Queue.java b/group16/313001956/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4b9f311f99 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + Object o= elementData.get(0); + elementData.removeFirst(); + return o; + } + + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/313001956/src/com/coding/basic/Stack.java b/group16/313001956/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..6ec157201e --- /dev/null +++ b/group16/313001956/src/com/coding/basic/Stack.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public Stack() { + // TODO Auto-generated constructor stub + } + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + int size = elementData.size(); + Object o = elementData.get(size - 1); + elementData.remove(size - 1); + return o; + } + + public Object peek() { + int size = elementData.size(); + Object o = elementData.get(size - 1); + + return o; + } + + public boolean isEmpty() { + if (elementData.size() == 0) + return true; + return false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/420355244/Homework1/.classpath b/group16/420355244/Homework1/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group16/420355244/Homework1/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group16/420355244/Homework1/.gitignore b/group16/420355244/Homework1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/420355244/Homework1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/420355244/Homework1/.project b/group16/420355244/Homework1/.project new file mode 100644 index 0000000000..ec1134f33a --- /dev/null +++ b/group16/420355244/Homework1/.project @@ -0,0 +1,17 @@ + + + Homework1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs b/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java b/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..6534c3c029 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,91 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size < elementData.length){ + elementData[size] = o; + }else{ + Object[] newElementData = new Object[elementData.length + elementData.length/2]; + System.arraycopy(elementData, 0, newElementData, 0, size); + newElementData[size] = o; + this.elementData = newElementData; + } + size++; + + } + public void add(int index, Object o){ + if(index >= 0 && index <= size){ + //1.不扩容 + if(index == size - 1){ + //1.1 加在最后 + elementData[index] = o; + }else{ + //1.2 加在前面 + //index的位置的数值变为改对象,index以后位置的都往后挪一位 + Object[] newElementData = new Object[elementData.length]; + System.arraycopy(elementData, 0, newElementData, 0, index); + newElementData[index] = o ; + System.arraycopy(elementData, index, newElementData, index + 1, size - index); + this.elementData = newElementData; + } + size++; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public Object get(int index){ + if(index < size){ + return elementData[index]; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public Object remove(int index){ + if(index < size){ + Object obj = elementData[index]; + Object[] newElementData = new Object[elementData.length]; + if(size != 1){ + //1.若集合长度为1 + if(0 == index){ + //1.1.如果remove的是0索引的 + System.arraycopy(elementData, 1, newElementData, 0, size - 1); + }else if(index == size -1){ + //1.2.如果remove的是最后索引的 + System.arraycopy(elementData, 0, newElementData, 0, size - 1); + }else{ + //1.3.在中间 + System.arraycopy(elementData, 0, newElementData, 0, index); + System.arraycopy(elementData, index + 1, newElementData, index, size - index - 1); + } + } + this.elementData = newElementData; + size--; + return obj; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + @Override + public String toString() { + return "ArrayList [size=" + size + ", elementData=" + Arrays.toString(elementData) + "]"; + } + + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java b/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..420c412a7b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + private static ArrayList arrayList = new ArrayList(); + + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + for(int i = 0 ;i < 150; i++){ + arrayList.add("aaa"); + } + System.out.println(arrayList); + System.out.println(arrayList.size()); + } + + @Test + public void testAddIntObject() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + arrayList.add(1,"eee"); + System.out.println(arrayList); + } + + @Test + public void testGet() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + Object object = arrayList.get(0); + System.out.println(object); + } + + @Test + public void testRemove() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + arrayList.remove(0); + System.out.println(arrayList); + } + + @Test + public void testSize() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + System.out.println(arrayList.size()); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java b/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/Iterator.java b/group16/420355244/Homework1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java b/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..9af35a399d --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,224 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + + private Node first; + + private Node last; + + private int size = 0; + + public void add(Object o){ + if(null == first){ + //当链表元素为空时,新建一个Node + Node node = new Node(); + node.data = o; + node.next = null; + first = node; + last = node; + size ++; + }else{ + addLast(o); + } + } + public void add(int index , Object o){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果加在头上 + addFirst(o); + }else{ + //2.加在中间位置 + Node node = first.next; + int nodeIndex = 1; + if(nodeIndex == index){ + //如果是第二个位置的话 + Node nodeAdd = new Node(); + nodeAdd.data = o; + first.next = nodeAdd; + nodeAdd.next = node; + last = node; + size ++; + } + //第三个位置及以后、开始遍历所有的索引 + while(null != node.next){ + //保留遍历中node之前的结点 + Node nodeLast = node; + node = node.next; + nodeIndex++; + if(nodeIndex == index){ + Node nodeAdd = new Node(); + nodeAdd.data = o; + nodeLast.next = nodeAdd; + nodeAdd.next = node; + size ++; + break; + } + } + } + } + + } + public Object get(int index){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果加在头上 + return first.data; + } + Node node = first.next; + int nodeIndex = 1; + if(nodeIndex == index){ + //如果是第二个位置的话 + return node.data; + } + //第三个位置及以后、开始遍历所有的索引 + while(null != node.next){ + //保留遍历中node之前的结点 + node = node.next; + nodeIndex++; + if(nodeIndex == index){ + return node.data; + } + } + } + throw new IndexOutOfBoundsException(); + } + public Object remove(int index){ + if(index < 0 || index >= size){ + //数组越界异常 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.如果移除头 + removeFirst(); + }else if(index == (size - 1)){ + //2.移除尾 + removeLast(); + }else{ + //3.移除中间位置 + Node node = first.next; + //从first的零号索引开始 + int nodeIndex = 1; + + //开始遍历所有的索引,记住要移除的索引位数据的前后结点 + Node lastNode = first; + if(index == nodeIndex){ + //第一次不匹配则后续的循环执行 + Object o = node.data; + lastNode.next = node.next; + size--; + return o; + }else{ + while(null != node.next){ + lastNode = node; + node = node.next; + nodeIndex++; + if(index == nodeIndex){ + Object o = node.data; + lastNode.next = node.next; + size--; + return o; + } + } + } + } + } + throw new IndexOutOfBoundsException(); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o ; + node.next = first; + first = node; + size++; + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o ; + node.next = null; + last.next = node; + last = node; + size++; + } + public Object removeFirst(){ + Object o = first.data; + Node node = first.next; + first = node; + size--; + return o; + } + public Object removeLast(){ + if(0 == size){ + throw new NoSuchElementException(); + + }else if(1 == size){ + //只有一个元素 + removeFirst(); + }else{ + //第二个元素 + Node node = first.next; + if(null == node.next){ + Object o = node.data; + last = first; + first.next = null; + return o; + }else{ + while(null != node.next){ + //若不止只有2个 ,记录最后一个结点的前一个。 + Node lastNode = node; + node = node.next; + if(null == node.next){ + Object o = node.data; + lastNode.next = null; + last = lastNode; + size--; + return o; + } + } + } + } + throw new NoSuchElementException(); + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if(null != first){ + sb.append(first.data.toString() + ","); + Node node = first.next; + sb.append(node.data.toString() + ","); + while(null != node.next){ + node = node.next; + sb.append(node.data.toString() + ","); + } + } + return sb.toString(); + } + + + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java b/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..2caea9679b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,112 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + private static LinkedList linkedList = new LinkedList(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + linkedList.add("aaa"); + linkedList.add("bbb"); + System.out.println(linkedList); + } + + @Test + public void testAddIntObject() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add(2,"ddd"); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + + @Test + public void testGet() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); +// System.out.println(linkedList.size()); + System.out.println(linkedList.get(3)); + } + + @Test + public void testRemove() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); + linkedList.remove(5); + linkedList.remove(1); + linkedList.remove(2); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + + @Test + public void testSize() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); + System.out.println(linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.addFirst("sss"); + System.out.println(linkedList); + } + + @Test + public void testAddLast() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + System.out.println(linkedList); + } + + @Test + public void testRemoveFirst() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.removeFirst(); + linkedList.addFirst("eee"); + linkedList.removeFirst(); + System.out.println(linkedList); + } + + @Test + public void testRemoveLast() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.removeLast(); + linkedList.add("eee"); + linkedList.addFirst("xxx"); + System.out.println(linkedList); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/List.java b/group16/420355244/Homework1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/420355244/Homework1/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/group16/420355244/Homework1/src/com/coding/basic/Queue.java b/group16/420355244/Homework1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a2f9577b7b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Queue.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +public class Queue { + private Node first; + private Node last; + private int size = 0; + public void enQueue(Object o){ + if(null == first){ + Node node = new Node(); + node.data = o; + node.next = null; + first = node; + last = node; + }else{ + Node node = new Node(); + node.data = o; + node.next = null; + last.next = node; + last = node; + } + size++; + } + + public Object deQueue(){ + Node second = first.next; + Object o = first.data; + if(null != second){ + first = second; + return o; + }else{ + first = null; + size = 0; + return o; + } + } + + public boolean isEmpty(){ + if(size > 0){ + return false; + }else{ + return true; + } + } + + public int size(){ + return size; + } + static class Node{ + Node next; + Object data; + } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if(null != first){ + sb.append(first.data.toString() + ","); + Node node = first.next; + sb.append(node.data.toString() + ","); + while(null != node.next){ + node = node.next; + sb.append(node.data.toString() + ","); + } + } + return sb.toString(); + } +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java b/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..50d7fc0903 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + + public static Queue queue = new Queue(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testEnQueue() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("aaa"); + System.out.println(queue); + } + + @Test + public void testDeQueue() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("ddd"); + queue.enQueue("eee"); + queue.deQueue(); + System.out.println(queue); + } + + @Test + public void testIsEmpty() { + System.out.println(queue.isEmpty()); + + } + + @Test + public void testSize() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("ddd"); + queue.enQueue("eee"); + System.out.println(queue.size()); + } + + @Test + public void testToString() { + fail("Not yet implemented"); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/Stack.java b/group16/420355244/Homework1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..5c59bf34fc --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + public void push(Object o){ + //入栈在栈顶进入最后压入 + elementData.add(o); + size ++; + } + + public Object pop(){ + Object object = elementData.get(size -1); + elementData.remove(size -1); + size --; + return object; + } + + public Object peek(){ + Object object = elementData.get(size -1); + return object; + } + public boolean isEmpty(){ + if(size <= 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return size; + } + + @Override + public String toString() { + return elementData.toString(); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/StackTest.java b/group16/420355244/Homework1/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..b436785574 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/StackTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + private static Stack stack = new Stack(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testPush() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + System.out.println(stack); + } + + @Test + public void testPop() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + Object pop = stack.pop(); + System.out.println(pop); + System.out.println(stack); + } + + @Test + public void testPeek() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + Object peek = stack.peek(); + System.out.println(peek); + } + + @Test + public void testIsEmpty() { + System.out.println(stack.isEmpty()); + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + System.out.println(stack.isEmpty()); + stack.pop(); + stack.pop(); + stack.pop(); + System.out.println(stack.isEmpty()); + } + + @Test + public void testSize() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + stack.pop(); + stack.pop(); + System.out.println(stack.size()); + } + +} diff --git a/group16/420355244/Homework2/.classpath b/group16/420355244/Homework2/.classpath new file mode 100644 index 0000000000..f5d4a033ec --- /dev/null +++ b/group16/420355244/Homework2/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group16/420355244/Homework2/.gitignore b/group16/420355244/Homework2/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/420355244/Homework2/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/420355244/Homework2/.project b/group16/420355244/Homework2/.project new file mode 100644 index 0000000000..1a13fc592d --- /dev/null +++ b/group16/420355244/Homework2/.project @@ -0,0 +1,17 @@ + + + Homework2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java b/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5496d8084d --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5fd5f1efba --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,120 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + for(int i = 0;i < origin.length/2; i++){ + int x = origin[i]; + origin[i] = origin[origin.length - i -1]; + origin[origin.length - i -1] = x; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + for(int i = 0;i < oldArray.length ;i++){ + if(oldArray[i] == 0){ + int[] a = {}; + System.arraycopy(oldArray, 0, a, 0, i); + System.arraycopy(oldArray, 0, a, i, oldArray.length); + oldArray = a; + removeZero(oldArray); + } + } + return oldArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + return null; + } + + public static void main(String[] args) { + /*int[] a = {7, 9 , 30, 3}; + reverseArray(a); + for (int i : a) { + System.out.print(i+","); + }*/ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ; + removeZero(oldArr); + for (int i : oldArr) { + System.out.print(i+","); + } + } +} diff --git a/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java b/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..40af955dfa --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,133 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + //0. 读取配置文件struts.xml + SAXReader reader = new SAXReader(); + try { + //0.2 读取文件 + Document doc = reader.read(new File("./src/com/coderising/litestruts/struts.xml")); + //0.3 得到根标签 + Element rootElement = doc.getRootElement(); + //0.4 得到根标签下的所有action标签 + Iterator elementIterator = rootElement.elementIterator("action"); + while(elementIterator.hasNext()){ + Element element = elementIterator.next(); + String nameValue = element.attributeValue("name"); + try { + if(null != actionName && actionName.trim() != ""){ + if(actionName.equals(nameValue)){ + View view = new View(); + //进入该action标签内,结束后停止循环 + String classValue = element.attributeValue("class"); + //1.1 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)据parameters中的数据 + Class clazz =Class.forName(classValue); + Object instance = clazz.newInstance(); + Method[] methods = clazz.getMethods(); + for (Entry entry : parameters.entrySet()) { + String methodName = "set" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); + //1.2调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , "password"="1234") ,那就应该调用 setName和setPassword方法 + for (Method setterMethod : methods) { + if(methodName.equals(setterMethod.getName())){ + setterMethod.invoke(instance,entry.getValue()); + break; + } + } + } + //2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method method = clazz.getMethod("execute", null); + Object exectueResult = method.invoke(instance, null); + Iterator resultElement = element.elementIterator("result"); + while(resultElement.hasNext()){ + Element result = resultElement.next(); + if(exectueResult.equals(result.attributeValue("name"))){ + String jsp = result.getText(); + view.setJsp(jsp); + break; + } + } + /*3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters*/ + HashMap hashMap = new HashMap<>(); + for (Method getterMethod : methods) { + if(getterMethod.getName().contains("get")){ + Object resultValue = getterMethod.invoke(instance,null); + String resultKey = getterMethod.getName().replace("get", "").substring(0,1).toLowerCase() + + getterMethod.getName().replace("get", "").substring(1); + hashMap.put(resultKey, resultValue); + } + } + view.setParameters(hashMap); + return view; + } + } + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + public static void main(String[] args) { + runAction("login",null); + } + +} diff --git a/group16/420355244/Homework2/src/com/coderising/litestruts/StrutsTest.java b/group16/420355244/Homework2/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/420355244/Homework2/src/com/coderising/litestruts/View.java b/group16/420355244/Homework2/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/420355244/Homework2/src/com/coderising/litestruts/struts.xml b/group16/420355244/Homework2/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/502059278/.classpath b/group16/502059278/.classpath index fb5011632c..c0abaf014f 100644 --- a/group16/502059278/.classpath +++ b/group16/502059278/.classpath @@ -2,5 +2,7 @@ + + diff --git "a/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" "b/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" deleted file mode 100644 index 31dfe4c14b..0000000000 Binary files "a/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" and /dev/null differ diff --git a/group16/502059278/src/cn/mark/MyArrayList.java b/group16/502059278/src/cn/mark/work0219/MyArrayList.java similarity index 99% rename from group16/502059278/src/cn/mark/MyArrayList.java rename to group16/502059278/src/cn/mark/work0219/MyArrayList.java index 9e0e406274..3d7a064919 100644 --- a/group16/502059278/src/cn/mark/MyArrayList.java +++ b/group16/502059278/src/cn/mark/work0219/MyArrayList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; import java.util.ArrayList; import java.util.Arrays; diff --git a/group16/502059278/src/cn/mark/MyLinkedList.java b/group16/502059278/src/cn/mark/work0219/MyLinkedList.java similarity index 97% rename from group16/502059278/src/cn/mark/MyLinkedList.java rename to group16/502059278/src/cn/mark/work0219/MyLinkedList.java index 7f9c3856a2..896d7bcb79 100644 --- a/group16/502059278/src/cn/mark/MyLinkedList.java +++ b/group16/502059278/src/cn/mark/work0219/MyLinkedList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; /** * 自定义实现LinkedList数据结构 * @author hilih diff --git a/group16/502059278/src/cn/mark/MyList.java b/group16/502059278/src/cn/mark/work0219/MyList.java similarity index 96% rename from group16/502059278/src/cn/mark/MyList.java rename to group16/502059278/src/cn/mark/work0219/MyList.java index 19bc3f92fe..91f26ff8d5 100644 --- a/group16/502059278/src/cn/mark/MyList.java +++ b/group16/502059278/src/cn/mark/work0219/MyList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; public interface MyList { /** diff --git a/group16/502059278/src/cn/mark/work0226/ArrayUtil.java b/group16/502059278/src/cn/mark/work0226/ArrayUtil.java new file mode 100644 index 0000000000..94e253e8bb --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/ArrayUtil.java @@ -0,0 +1,166 @@ +package cn.mark.work0226; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int[] target = new int[origin.length];//声明置换后数组 + int temp = target.length - 1;//记录置换后下标位置 + for( int i = 0; i < origin.length; i++ ){ + target[temp] = origin[i]; + temp--; + } + System.out.println("置换前:"+Arrays.toString(origin)); + System.out.println("置换后:"+Arrays.toString(target)); + + + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int[] target = new int[1]; + boolean flag = true; + for( int i = 0; i < oldArray.length; i++ ){ + if ( oldArray[i] == 0 ){ // 跳过值为0的元素 + continue; + } + + if ( flag ){ + //首位赋值无需扩容 + target[target.length-1] = oldArray[i]; + flag = false; + }else{ + //确定值不是0才能进入扩容步骤 + target = Arrays.copyOf(target, target.length+1); + target[target.length-1] = oldArray[i]; + } + + + + } + System.out.println("去0前:"+Arrays.toString(oldArray)); + System.out.println("去0后:"+Arrays.toString(target)); + return target; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + //1.去重 + int[] array3 = Arrays.copyOf(array1, array1.length); + for( int i = 0; i < array1.length; i++ ){ + for( int j = 0; j < array2.length ; j++ ){ + if ( array1[i] == array2[j] ){ + + } + } + + + + + } + + System.out.println(Arrays.toString(array1)); + System.out.println(Arrays.toString(array2)); + //2.合并 + + //3.排序 + + + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = Arrays.copyOf(oldArray, oldArray.length+size); + System.out.println("扩容后:"+Arrays.toString(newArray)); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for( int i = 0; i < array.length; i++ ){ + if ( i == 0 ){ + sb.append(array[i]); + }else{ + sb.append(seperator+array[i]); + } + } + return sb.toString(); + } + + + public static void main(String[] args) { + int[] a1 =new int[]{3,8}, a2 = new int[]{4, 5, 6,7}; + System.out.println(new ArrayUtil().join(a1, "*")); + + } +} diff --git a/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java b/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java new file mode 100644 index 0000000000..da1abd3b38 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java @@ -0,0 +1,35 @@ +package cn.mark.work0226; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestArrayUtil { + ArrayUtil arrayUtil = null; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void reverseArray() { + int[] origin = new int[]{1,2,8,31}; + arrayUtil.reverseArray(origin); + } + + @Test + public void removeZero() { + int[] origin = new int[]{0,1,2,0,3,0,4,7,0}; + Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 7}, arrayUtil.removeZero(origin)); + } + +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java b/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java new file mode 100644 index 0000000000..4966d7d170 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package cn.mark.work0226.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java b/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java new file mode 100644 index 0000000000..93a449adaf --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java @@ -0,0 +1,84 @@ +package cn.mark.work0226.litestruts; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + String classPath = null; + String className = null; + + Document dom = XMLUtils.getDocument("bin"+File.separator+"struts.xml"); + Element ele = XMLUtils.getElement(dom, actionName); + Attribute classAttr = ele.attribute("class"); + classPath = classAttr.getValue(); + className = classPath.substring(classPath.lastIndexOf(".")+1); + System.out.println(className); + + + + try { + Class clz = Class.forName(classPath); + System.out.println(clz.getName()); + + + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + return null; + } + + + public static void main(String[] args) { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + Struts.runAction(actionName,params); + } +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java b/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java new file mode 100644 index 0000000000..1a60626460 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package cn.mark.work0226.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/View.java b/group16/502059278/src/cn/mark/work0226/litestruts/View.java new file mode 100644 index 0000000000..31e3df3427 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/View.java @@ -0,0 +1,23 @@ +package cn.mark.work0226.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java b/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java new file mode 100644 index 0000000000..2eeb6e624e --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java @@ -0,0 +1,49 @@ +package cn.mark.work0226.litestruts; + +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class XMLUtils { + /** + * 获取Document + * @param filePath 配置文件路径名 + * @return Document对象 + */ + public static Document getDocument(String filePath){ + //1.创建解析器 + SAXReader reader = new SAXReader(); + //2.解析XML文档,返回document对象 + Document dom = null; + try { + dom = reader.read(filePath); + } catch (DocumentException e) { + e.printStackTrace(); + } + return dom; + } + /** + * 获取指定action元素 + * @param doc Document + * @param actionName 要获取的元素属性名 + * @return 包含所要属性的元素 + */ + public static Element getElement(Document doc , String actionName){ + Element result = null; + Element root = doc.getRootElement(); + List elements = root.elements(); + for(Element e : elements){ + Attribute attr = e.attribute("name"); + if(attr.getValue().equals(actionName)){ + result = e; + return result; + } + } + return result; + } + +} diff --git a/group16/502059278/src/struts.xml b/group16/502059278/src/struts.xml new file mode 100644 index 0000000000..1aaa6ea5a0 --- /dev/null +++ b/group16/502059278/src/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/542087872/out/production/coding2017/com/coderising/litestruts/struts.xml b/group16/542087872/out/production/coding2017/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group16/542087872/out/production/coding2017/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/542087872/out/production/coding2017/net/coding/coderising/litestruts/struts.xml b/group16/542087872/out/production/coding2017/net/coding/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..848f04cfba --- /dev/null +++ b/group16/542087872/out/production/coding2017/net/coding/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/542087872/src/com/coding/basic/ArrayList.java b/group16/542087872/src/net/coding/basic/ArrayList.java similarity index 98% rename from group16/542087872/src/com/coding/basic/ArrayList.java rename to group16/542087872/src/net/coding/basic/ArrayList.java index 1b10b441cf..a0286827d6 100644 --- a/group16/542087872/src/com/coding/basic/ArrayList.java +++ b/group16/542087872/src/net/coding/basic/ArrayList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; import java.util.Arrays; diff --git a/group16/542087872/src/com/coding/basic/BinaryTreeNode.java b/group16/542087872/src/net/coding/basic/BinaryTreeNode.java similarity index 97% rename from group16/542087872/src/com/coding/basic/BinaryTreeNode.java rename to group16/542087872/src/net/coding/basic/BinaryTreeNode.java index df167343a0..4cac873d08 100644 --- a/group16/542087872/src/com/coding/basic/BinaryTreeNode.java +++ b/group16/542087872/src/net/coding/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class BinaryTreeNode { diff --git a/group16/542087872/src/net/coding/basic/Iterator.java b/group16/542087872/src/net/coding/basic/Iterator.java new file mode 100644 index 0000000000..ca3fd054ae --- /dev/null +++ b/group16/542087872/src/net/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package net.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/542087872/src/com/coding/basic/LinkedList.java b/group16/542087872/src/net/coding/basic/LinkedList.java similarity index 99% rename from group16/542087872/src/com/coding/basic/LinkedList.java rename to group16/542087872/src/net/coding/basic/LinkedList.java index 144af4ec8d..0f9d326545 100644 --- a/group16/542087872/src/com/coding/basic/LinkedList.java +++ b/group16/542087872/src/net/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class LinkedList implements List { diff --git a/group16/542087872/src/net/coding/basic/List.java b/group16/542087872/src/net/coding/basic/List.java new file mode 100644 index 0000000000..189fe091d8 --- /dev/null +++ b/group16/542087872/src/net/coding/basic/List.java @@ -0,0 +1,9 @@ +package net.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group16/542087872/src/com/coding/basic/Queue.java b/group16/542087872/src/net/coding/basic/Queue.java similarity index 92% rename from group16/542087872/src/com/coding/basic/Queue.java rename to group16/542087872/src/net/coding/basic/Queue.java index 8e4285464b..d233a02617 100644 --- a/group16/542087872/src/com/coding/basic/Queue.java +++ b/group16/542087872/src/net/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class Queue { diff --git a/group16/542087872/src/com/coding/basic/Stack.java b/group16/542087872/src/net/coding/basic/Stack.java similarity index 94% rename from group16/542087872/src/com/coding/basic/Stack.java rename to group16/542087872/src/net/coding/basic/Stack.java index bfe98dd8b7..4a8099bcee 100644 --- a/group16/542087872/src/com/coding/basic/Stack.java +++ b/group16/542087872/src/net/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group16/542087872/src/com/coding/basic/Test.java b/group16/542087872/src/net/coding/basic/Test.java similarity index 99% rename from group16/542087872/src/com/coding/basic/Test.java rename to group16/542087872/src/net/coding/basic/Test.java index 2db5b2f9ab..d973793899 100644 --- a/group16/542087872/src/com/coding/basic/Test.java +++ b/group16/542087872/src/net/coding/basic/Test.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; /** diff --git a/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java b/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7cf4e1fd3f --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java @@ -0,0 +1,203 @@ +package net.coding.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int l = 0, r = origin.length - 1; + while (l < r) { + int tmp = origin[l]; + origin[l] = origin[r]; + origin[r] = tmp; + l++; + r--; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int cntZero = 0; + for (int i = 0; i < oldArray.length;i++) { + if (oldArray[i] == 0) { + cntZero ++; + } + } + if (cntZero == 0) { + return oldArray; + } + + int[] newArray = new int[oldArray.length - cntZero]; + int j = 0; + for (int i = 0; i < oldArray.length;i++) { + if (oldArray[i] != 0) { + newArray[j++] = oldArray[i]; + } + } + + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int[] result = new int[array1.length + array2.length]; + int l = 0; + int r = 0; + + int cnt = 0; + while (true) { + if (l >= array1.length && r >= array2.length) { + break; + } + if (l >= array1.length) { + result[cnt++] = array2[r]; + r++; + } else if (r >= array1.length) { + result[cnt++] = array1[l]; + l++; + } else { + if (array1[l] < array2[r]) { + result[cnt++] = array1[l]; + l++; + } else { + result[cnt++] = array2[r]; + r++; + } + } + } + + return result; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] result = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] result = {}; + int a = 0; + int b = 1; + + int cnt = 0; + while (b < max) { + result = grow(result, 1); + result[cnt++] = b; + int tmp = a + b; + a = b; + b = tmp; + } + + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[1,2,3,5,7,11,13,17,19] + * @param max + * @return + */ + private boolean isPrime(int n) { + for (int i = 2; i <= Math.sqrt(n + 1.0); i++) { + if (n % i == 0) { + return false; + } + } + return true; + } + public int[] getPrimes(int max){ + int[] result = {}; + int cnt = 0; + for (int i = 1; i < max; i++) { + if (isPrime(i)) { + result = grow(result, 1); + result[cnt++] = i; + } + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + private boolean isPerfect(int n) { + int total = 0; + for (int i = 1; i < n; i++) { + if (n % i == 0) { + total += i; + } + } + + return total == n; + } + public int[] getPerfectNumbers(int max){ + int[] result = {}; + int cnt = 0; + for (int i = 1; i < max; i++) { + if (isPerfect(i)) { + result = grow(result, 1); + result[cnt++] = i; + } + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (sb.length() > 0) { + sb.append(seperator); + } + sb.append(array[i]); + } + return sb.toString(); + } + + +} diff --git a/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java b/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..0ae290bbfd --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package net.coding.coderising.array; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by xiaoyuan on 02/03/2017. + */ +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] nums = {1, 2, 3}; + new ArrayUtil().reverseArray(nums); + Assert.assertArrayEquals(nums, new int[]{3, 2, 1}); + } + // removeZero + @Test + public void testRemoveZero() { + int[] nums = {0, 1, 0, 2, 3}; + int[] ans = new ArrayUtil().removeZero(nums); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3}); + } + + // merge + @Test + public void testMerge() { + int[] nums1 = {1, 3, 9}; + int[] nums2 = {2, 4, 5}; + int[] ans = new ArrayUtil().merge(nums1, nums2); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3, 4, 5, 9}); + } + + // grow + @Test + public void testGrow() { + int[] nums = {1, 3, 9}; + int[] ans = new ArrayUtil().grow(nums, 2); + Assert.assertArrayEquals(ans, new int[]{1, 3, 9, 0, 0}); + } + + // fibonacci + @Test + public void testFibonacci() { + int[] ans = new ArrayUtil().fibonacci(10); + Assert.assertArrayEquals(ans, new int[]{1, 1, 2, 3, 5, 8}); + } + + + // getPrimes + @Test + public void testgetPrimes() { + int[] ans = new ArrayUtil().getPrimes(10); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3, 5, 7}); + } + + + // getPerfectNumbers + @Test + public void testGetPerfectNumbers() { + int[] ans = new ArrayUtil().getPerfectNumbers(10); + Assert.assertArrayEquals(ans, new int[]{6}); + } + + + // join + + @Test + public void testJoin() { + String ans = new ArrayUtil().join(new int[]{1, 3, 4}, "-"); + Assert.assertEquals(ans, "1-3-4"); + } + + + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java b/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..5e4f956c2f --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package net.coding.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java b/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..560d8f4fd8 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java @@ -0,0 +1,36 @@ +package net.coding.coderising.litestruts; + +/** + * Created by xiaoyuan on 02/03/2017. + */ +public class LogoutAction { + + String ifLogout; + String message; + + public String execute() { + if (ifLogout.equals("yes")) { + this.message = "success"; + return "success"; + } else { + this.message = "error"; + return "error"; + } + } + + public String getIfLogout() { + return ifLogout; + } + + public void setIfLogout(String ifLogout) { + this.ifLogout = ifLogout; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/Struts.java b/group16/542087872/src/net/coding/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..1d9337f4f2 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/Struts.java @@ -0,0 +1,126 @@ +package net.coding.coderising.litestruts; + + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + Map name2ClassMap = new HashMap(); + Map result2JSPMap = new HashMap(); + + + try { + File xmlFile = new File("group16/542087872/src/net/coding/coderising/litestruts/struts.xml"); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = factory.newDocumentBuilder(); + Document doc = documentBuilder.parse(xmlFile); + + doc.getDocumentElement().normalize(); + + NodeList actionList = doc.getElementsByTagName("action"); + for (int i = 0; i < actionList.getLength(); i++) { + Node node = actionList.item(i); + System.out.println(node.getNodeName()); + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element element = (Element)node; + String acName = element.getAttribute("name"); + String acClass = element.getAttribute("class"); + name2ClassMap.put(acName, acClass); + + NodeList resultList = element.getElementsByTagName("result"); + for (int j = 0; j < resultList.getLength(); j++) { + Element resultElemet = (Element)(resultList.item(j)); + String acResultName = resultElemet.getAttribute("name"); + String acResultJSP = resultElemet.getTextContent(); + + result2JSPMap.put(acName + "_" + acResultName, acResultJSP); + } + } + } + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("parse XML ERROR!"); + } + + String classStr = name2ClassMap.get(actionName); + if (classStr == null) { + throw new RuntimeException("ACTION FOUND ERROR!"); + } + + try { + Class actionClass = Class.forName(classStr); + Object actionObj = actionClass.newInstance(); + for (String key : parameters.keySet()) { + String value = parameters.get(key); + Method theMethod = actionClass.getMethod("set" + key.substring(0, 1).toUpperCase() + key.substring(1), String.class); + theMethod.invoke(actionObj, value); + } + + // execut + Method exeMethod = actionClass.getMethod("execute"); + String result = (String)exeMethod.invoke(actionObj); + + // find JSP + String JSPPath = result2JSPMap.get(actionName + "_" + result); + View view = new View(); + view.setJsp(JSPPath); + + // generate map + Map map = new HashMap(); + for(Method method: actionClass.getMethods()) { + if (method.getName().startsWith("get")) { + Object key = method.getName().substring(3).toLowerCase(); + Object value = method.invoke(actionObj); + map.put(key, value); + } + } + view.setParameters(map); + + return view; + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + + public static void main(String[] args) { + runAction(null, null); + } + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java b/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8c4de18ac4 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java @@ -0,0 +1,70 @@ +package net.coding.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + + @Test + public void testLogoutActionSuccess() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("ifLogout","yes"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("success", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionError() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("ifLogout","no"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("error", view.getParameters().get("message")); + } + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/View.java b/group16/542087872/src/net/coding/coderising/litestruts/View.java new file mode 100644 index 0000000000..63fb1d025e --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package net.coding.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/struts.xml b/group16/542087872/src/net/coding/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..848f04cfba --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/63912401/.classpath b/group16/63912401/.classpath new file mode 100644 index 0000000000..74368f11c3 --- /dev/null +++ b/group16/63912401/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group16/63912401/.gitignore b/group16/63912401/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/63912401/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/63912401/.project b/group16/63912401/.project new file mode 100644 index 0000000000..ec6117b543 --- /dev/null +++ b/group16/63912401/.project @@ -0,0 +1,17 @@ + + + 63912401 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/63912401/src/com/coderising/action/LoginAction.java b/group16/63912401/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..64d4d355b6 --- /dev/null +++ b/group16/63912401/src/com/coderising/action/LoginAction.java @@ -0,0 +1,45 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/63912401/src/com/coderising/array/ArrayUtil.java b/group16/63912401/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..c8510b08e1 --- /dev/null +++ b/group16/63912401/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,270 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + + +/** + * ArrayUtil + * @author greenhills + * 2017年2月28日 下午10:49:41 + */ +public class ArrayUtil { + + public static void main(String[] args) { +// int[] array1={5,8,9,0,-4}; +// int[] array2={4,5,6,7,8,9}; +// int[] array3=ArrayUtil.merge(array1, array2); +// for(Integer t:array3){ +// System.out.print(t +"\t"); +// } + +// int[] array3={4,5,6,7,8,9}; +// array3 = ArrayUtil.grow(array3,5); +// for(int t:array3){ +// System.out.print(t +"\t"); +// } + +// int[] array4=ArrayUtil.fibonacci(100); +// for(Integer t:array4){ +// System.out.print(t +"\t"); +// } + +// int[] array5=ArrayUtil.getPrimes(2); +// for(Integer t:array5){ +// System.out.print(t +"\t"); +// } + + int[] array6=ArrayUtil.getPerfectNumbers(2000); + for(Integer t:array6){ + System.out.print(t +"\t"); + } + + } + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + for (int i = 0; i < origin.length>>1; i++) { + origin[i]^=origin[origin.length - 1 - i]^(origin[origin.length - 1 - i]=origin[i]); + } + + //方法2 可以使用Collections.reverse(list)方法,但是int 和 Integer数组之间转化消耗性能 + //Collections.reverse(list); + } + + /** + * 现在有如下的一个数组: Integer oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + List list=new ArrayList(); + for(Integer data:oldArray){ + if(data != 0){ + list.add(data); + } + } + int[] newArray=new int[list.size()]; + for(Integer i=0;i set=new HashSet(); + for(Integer t:array1){ + set.add(t); + } + for(Integer t:array2){ + set.add(t); + } + List list=new ArrayList(set); + java.util.Collections.sort(list); + int[] result =new int[list.size()]; + for(int i=0;i 1){ + List list=new ArrayList(); + list.add(1); + list.add(1); + int i=0; + int temp=list.get(i)+list.get(i+1); + while(temp < max){ + list.add(temp); + i++; + temp=list.get(i)+list.get(i+1); + } + + result=new int[list.size()]; + + i=0; + for(int d:list){ + result[i]=d; + i++; + } + } + + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + List list=new ArrayList(); + for(int i=2;i list=new ArrayList(); + for(int i=1;i resultMappings=new HashMap(); + + public ActionMapping() {} + + public ActionMapping(String name, String className, String method) { + this.name = name; + this.className = className; + this.method = StringUtils.isBlank(method) ? "execute" : method; //未配置时,默认查找execute方法执行 + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public String getMethod() { + return method; + } + public void setMethod(String method) { + this.method = method; + } + public Map getResultMappings() { + return resultMappings; + } + public void setResultMappings(Map resultMappings) { + this.resultMappings = resultMappings; + } + + //扩展的方法,用于保存 + public void setResultMappings(String key, ResultMapping value) { + this.resultMappings.put(key, value); + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java b/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java new file mode 100644 index 0000000000..55204d1b38 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java @@ -0,0 +1,83 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * 解析XML文件 + * ConfigurationManager + * @author greenhills + * 2017年2月27日 下午10:18:10 + */ +public class ConfigurationManager { + /** + * 读取xml文件的文档对象 + */ + private static Document document; + /** + * 配置文件路径 + */ + static String configureFileName="struts.xml"; + /** + * 当前类路径 + */ + static String currentPath; + static{ + currentPath = ConfigurationManager.class.getResource("").getPath().substring(1); + } + + static{ + try { + SAXReader sax=new SAXReader(); + document = sax.read(new File(currentPath+configureFileName)); + } catch (DocumentException e) { + e.printStackTrace(); + } + } + + /** + * 解析配置文件 + * ConfigurationManager.java + * @param @return + * @author greenhills + * 2017年2月27日 下午11:28:59 + */ + public static Map loadXml(){ + Map actionMappings=new HashMap(); + + Element root=document.getRootElement(); + List actionList= root.elements("action"); + + for(Element actionElemnt:actionList){ + ActionMapping actionMapping=new ActionMapping( + actionElemnt.attributeValue("name"), + actionElemnt.attributeValue("class"), + actionElemnt.attributeValue("method") + ); + //获取action下的result节点 + List resultList = actionElemnt.elements("result"); + for(Element resultElemnt:resultList){ + ResultMapping resultMapping=new ResultMapping( + resultElemnt.attributeValue("name"), + resultElemnt.attributeValue("type"), + resultElemnt.getTextTrim() + ); + + //保存ResultMapping(以result标签的name为key) + actionMapping.setResultMappings(resultMapping.getName(),resultMapping); + } + + //保存ActionMapping(以action标签的name为key) + actionMappings.put(actionMapping.getName(), actionMapping); + } + + return actionMappings; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/DefaultAction.java b/group16/63912401/src/com/coderising/litestruts/DefaultAction.java new file mode 100644 index 0000000000..c654a11a02 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/DefaultAction.java @@ -0,0 +1,121 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author greenhills + * @version 创建时间:2017年2月27日 下午11:48:56 + * + */ +public class DefaultAction { + private ActionMapping actionMapping; + private Object targetAction; //由DefaultAction反射调用的目标对象 + + //构造方法,实例化对象 + public DefaultAction(ActionMapping actionMapping) { + this.actionMapping = actionMapping; + //实例化对象 + try { + this.targetAction = Class.forName(this.actionMapping.getClassName()).newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 初始化参数 + * DefaultAction.java + * @param + * @author greenhills + * 2017年2月27日 下午11:58:05 + */ + public void initParam(Map parameters){ + Class clazz=this.targetAction.getClass(); + Set keys=parameters.keySet(); + try { + for(String key:keys){ + String _key = getFirstUpper(key); + //调用set方法赋值 + Method method=clazz.getDeclaredMethod("set"+_key,clazz.getDeclaredField(key).getType()); + method.invoke(this.targetAction, parameters.get(key)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 调用实例方法 + * DefaultAction.java + * @param @return + * @author greenhills + * 2017年2月27日 下午11:53:56 + */ + public String runMethod(){ + String methodName=this.actionMapping.getMethod(); + Class clazz=this.targetAction.getClass(); + String result="success"; + //调用set方法赋值 + try { + Method method=clazz.getDeclaredMethod(methodName); + result = (String) method.invoke(this.targetAction); + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + /** + * 获取action字段的值 + * DefaultAction.java + * @param @param fields + * @param @return + * @author greenhills + * 2017年2月28日 上午12:39:01 + */ + public Map getFieldValue(String[] fields){ + Map result=new HashMap(); + try { + Class clazz=this.targetAction.getClass(); + for(String field:fields){ + String _field = getFirstUpper(field); + //调用get方法获取值 + Method method=clazz.getDeclaredMethod("get"+_field); + result.put(field,method.invoke(this.targetAction)); + } + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + + + /** + * 将首字母改为大写 + * DefaultAction.java + * @param @param val + * @param @return + * @author greenhills + * 2017年2月28日 上午12:24:34 + */ + private String getFirstUpper(String val){ + return val.substring(0, 1).toUpperCase()+val.substring(1); + } + + + public ActionMapping getActionMapping() { + return actionMapping; + } + public void setActionMapping(ActionMapping actionMapping) { + this.actionMapping = actionMapping; + } + public Object getTargetAction() { + return targetAction; + } + public void setTargetAction(Object targetAction) { + this.targetAction = targetAction; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/ResultMapping.java b/group16/63912401/src/com/coderising/litestruts/ResultMapping.java new file mode 100644 index 0000000000..9a2650db09 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/ResultMapping.java @@ -0,0 +1,48 @@ +package com.coderising.litestruts; + +import org.apache.commons.lang3.StringUtils; + +/** + * @author greenhills + * @version 创建时间:2017年2月27日 下午10:39:20 + * + */ +public class ResultMapping { + /** + * 映射结构:/jsp/homepage.jsp + */ + private String name; + private String type; + private String urlPath; + + public ResultMapping() { + super(); + } + + public ResultMapping(String name, String type, String urlPath) { + super(); + this.name = StringUtils.isBlank(name) ? "success" : name; //默认成功 + this.type = StringUtils.isBlank(type) ? "dispatcher" : type; //默认转发 + this.urlPath = urlPath; + } + + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getUrlPath() { + return urlPath; + } + public void setUrlPath(String urlPath) { + this.urlPath = urlPath; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/Struts.java b/group16/63912401/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..06a23bb864 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { +// 0. 读取配置文件struts.xml + Map actionMappings = ConfigurationManager.loadXml(); + +// 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) +// 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 +// ("name"="test" , "password"="1234") , +// 那就应该调用 setName和setPassword方法 + ActionMapping actionMapping = actionMappings.get(actionName); + DefaultAction action=new DefaultAction(actionMapping); + action.initParam(parameters); + +// 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + String result= action.runMethod(); + +// 3. 通过反射找到对象的所有getter方法(例如 getMessage), +// 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , +// 放到View对象的parameters + + Map messageMaps=action.getFieldValue(new String[]{"message"}); + +// 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, +// 放到View对象的jsp字段中。 + View view =new View(); + view.setJsp(actionMapping.getResultMappings().get(result).getUrlPath()); + view.setParameters(messageMaps); + return view; + } + +} diff --git a/group16/63912401/src/com/coderising/litestruts/StrutsTest.java b/group16/63912401/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..4061019b2e --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/View.java b/group16/63912401/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/struts.xml b/group16/63912401/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..76cc617a8a --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/63912401/src/com/coding/basic/ArrayList.java b/group16/63912401/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f86a2e7a8a --- /dev/null +++ b/group16/63912401/src/com/coding/basic/ArrayList.java @@ -0,0 +1,206 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * ArrayList + * @author greenhills + * @version 创建时间:2017年2月19日 下午10:54:02 + * @param + * + */ +public class ArrayList implements List { + /** + * 默认容量 + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * 数据存放区 + */ + private Object[] elementData; + + /** + * 真实的数据数量 + */ + private int size = 0; + + /** + * 无参构造函数 + */ + public ArrayList(){ + this.elementData=new Object[DEFAULT_CAPACITY]; + } + + /** + * 带初始大小的构造函数 + * @param beginSize + */ + public ArrayList(int beginSize){ + if(beginSize<0) + this.elementData=new Object[DEFAULT_CAPACITY]; + else + this.elementData=new Object[beginSize]; + } + + /** + * 在后面追加数据 + */ + @Override + public void add(Object o){ + autoGrow(size+1); + this.elementData[size++] = o; //在尾部追加数据 + } + + /** + * 把数据加入指定索引处 + */ + @Override + public void add(int index, Object o){ + rangeCheck(index); + autoGrow(size+1); + //把index处的所有数据往后移 + //System.arraycopy(elementData, index, elementData, index+1, size-index); + + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + + this.elementData[index] = o; //使数据连续加入 + size++; + } + + /** + * 获取指定索引处的数据 + */ + @Override + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + /** + * 获取末尾数据 + */ + public Object getLast(){ + return elementData[this.size-1]; + } + + /** + * 移除索引处数据 + */ + @Override + public Object remove(int index){ + rangeCheck(index); + + Object removed = elementData[index]; + int num=size - index - 1; //移动数量 + if(num>0) { + System.arraycopy(elementData, index+1, elementData, index,num); + } + elementData[--size] = null; //清除最后一个数据位 + return removed; + } + + /** + * 移除末尾数据 + */ + public Object removeLast(){ + return remove(this.size-1); + } + + /** + * 获取数据量 + */ + @Override + public int size(){ + return this.size; + } + + /** + * 获取存储数据的容量大小 + */ + @Override + public int capacity() { + return this.elementData.length; + } + + /** + * 判断是否为空 + */ + @Override + public boolean isEmpty() { + return this.size==0; + } + + /** + * 空间容量自增长 + * @param minCapacity 增长后最小容量 + */ + private void autoGrow(int minCapacity){ + int oldCapacity = elementData.length; + if (minCapacity >= oldCapacity) { + int newCapacity = oldCapacity<<1; //空间翻倍 + if (newCapacity < minCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + /** + * 判断是否为有效索引 + * @param @param index + * @param @return + */ + private void rangeCheck(int index) { + if (!isEffectiveIndex(index)) + throw new IndexOutOfBoundsException("Index: "+index+" Out Of Bounds, 有效数据索引范围:0~"+(this.size-1)); + } + + /** + * 判断是否为有效索引 + * @param @param index + * @param @return + */ + private boolean isEffectiveIndex(int index){ + return index >-1 && index < this.size; + } + + /** + * 返回遍历数据对象 + * @param @return + * @author greenhills + * 2017年2月25日 下午9:55:31 + */ + public Iterator iterator(){ + return new Its(); + } + + /** + * 实现Iterator的内部实现类 + * Its + * @author greenhills + * 2017年2月25日 下午9:54:54 + */ + private class Its implements Iterator { + private int index=0; + + public Its(){ + //this.len = size; //逆向遍历 + } + + @Override + public boolean hasNext() { +// return this.len > 0; //逆向遍历 + return this.index < size; //正向遍历 + } + + @Override + public Object next() { +// return get(--this.len); //逆向遍历 +// return elementData[--this.len];//逆向遍历 + return get(this.index++); //正向遍历 + } + } +} diff --git a/group16/63912401/src/com/coding/basic/BinaryTreeNode.java b/group16/63912401/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..3ab1e431c0 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,99 @@ +package com.coding.basic; + +/** + * 二叉树数据结构 + * BinaryTreeNode + * @author greenhills + * 2017年2月25日 下午9:51:05 + */ +public class BinaryTreeNode implements Comparable{ + + private int height=0; //当前树高度 + private Object data; //当前节点数据 + private BinaryTreeNode left; //小于当前节点数据data的节点 + private BinaryTreeNode right; //大于当前节点数据data的节点 + + public BinaryTreeNode() { + } + + public BinaryTreeNode(Object data) { + this.data = data; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode newNode=new BinaryTreeNode(o); + BinaryTreeNode that = findNode(o); + int result=that.compareTo(o); + + if(result<0){//节点数据小于插入数据,进右树 + that.setRight(newNode); + }else if(result>0){ //当前节点数据大于插入数据,进左树 + that.setLeft(newNode); + }else{ + return null; + } + newNode.height++; //树高度加1 + return newNode; + } + + private BinaryTreeNode findNode(Object data){ + int result=this.compareTo(data); + BinaryTreeNode that = new BinaryTreeNode(); //空节点 + if(result<0){ //当前节点数据小于插入数据,进右树 + if(this.right==null){ + that = this; + }else{ + that = findNode(this.getRight()); + } + }else if(result>0){ //当前节点数据大于插入数据,进左树 + if(this.left==null){ + that = this; + }else{ + that = findNode(this.getLeft()); + } + }else{ + System.out.println("无效数据"); + } + return that; + } + + public int getTreeMaxHeight(){ + int h=0; + //TODO + + return h; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + @Override + public int compareTo(Object o) { + if(this.data==null || o==null) return 0; + return Double.valueOf(this.data.toString()).compareTo(Double.valueOf(o.toString())); + } +} diff --git a/group16/63912401/src/com/coding/basic/Iterator.java b/group16/63912401/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..fa815258c1 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/63912401/src/com/coding/basic/LinkedList.java b/group16/63912401/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..2120a5a4b8 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/LinkedList.java @@ -0,0 +1,320 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * 链表数据结构 + * LinkedList + * @author greenhills + * 2017年2月22日 下午11:52:41 + */ +public class LinkedList implements List { + /** + * 链表数据量 + */ + private int size=0; + /** + * 链表头节点 + */ + private Node head; + /** + * 链表尾节点 + */ + private Node tail; + + + /** + * 在数据链尾部添加数据 + */ + @Override + public void add(Object o) { + addLast(o); + } + + /** + * 在数据链指定位置添加数据 + */ + @Override + public void add(int index, Object data) { + checkIndex(index); + Node old=getNode(index); + link2Before(old,data); + } + + /** + * 获取指定索引处数据 + */ + @Override + public Object get(int index) { + checkIndex(index); + return getNode(index).data; + } + + /** + * 移除指定索引位置的节点 + */ + @Override + public Object remove(int index) { + checkIndex(index); + return unlink(getNode(index)); + } + + /** + * 获取数据链的数据量 + */ + @Override + public int size() { + return this.size; + } + + /** + * 获取数据链的数据量 + */ + @Override + @Deprecated + public int capacity() { + return size(); + } + + /** + * 判断是否为空 + */ + @Override + public boolean isEmpty() { + return this.size==0; + } + + /** + * 在数据链头部追加数据 + * @param data + */ + public void addFirst(Object data){ + final Node oldNode=this.head; + final Node newNode=new Node(null,data,oldNode);//形成新节点,并指向第一个节点 + this.head = newNode; //变更集合保存的首节点 + + if(oldNode == null){ //没有数据 + this.tail = newNode; //变更集合保存的尾节点 + }else{ + oldNode.prev = newNode; //原首节点指向新的首节点 + } + this.size++;//数据量加1 + } + + /** + * 在数据链尾部追加数据 + * @param data + */ + public void addLast(Object data){ + final Node oldNode=this.tail; + final Node newNode=new Node(oldNode,data,null);//形成新节点,并指向最后一个节点 + this.tail = newNode; //变更集合保存的尾节点 + + if(oldNode == null){ //没有数据 + this.head = newNode; //变更集合保存的首节点 + }else{ + oldNode.next = newNode;//原尾节点指向新的尾节点 + } + this.size++;//数据量加1 + } + + /** + * 把指定数据链接到指定节点前面 + */ + void link2Before(Node node,Object data){ + //传进来的node就是后节点 + final Node prev=node.prev; //指定节点的上一个节点(前节点) + //生成新节点,并指向前后的节点 + final Node newNode=new Node(prev,data,node);//生成新节点 + //后节点指向新节点 + node.prev = newNode; + //前节点指向新节点 + if(prev == null){//没有前节点了(当前节点已是首节点) + this.head = newNode;//把新的节点作为首节点 + }else{ + prev.next = newNode; + } + this.size++;//数据量加1 + } + + /** + * 把指定数据链接到指定节点后面 + */ + void link2Last(Node node,Object data){ + //传进来的node就是前节点 + final Node next=node.next; //指定节点的下一个节点(后节点) + //生成新节点,并指向前后的节点 + final Node newNode=new Node(node,data,next); + //前节点指向新节点 + node.next = newNode; + //后节点指向新节点 + if(next == null){//没有后节点了(当前节点已是尾节点) + this.tail = newNode;//把新的节点作为尾节点 + }else{ + next.prev = newNode; + } + this.size++;//数据量加1 + } + + /** + * 移除首节点 + * @return + */ + public Object removeFirst(){ + return unlink(getNode(0)); + } + + /** + * 移除尾节点 + * @return + */ + public Object removeLast(){ + return unlink(getNode(this.size-1)); + } + + /** + * 移除节点 + * @return + */ + Object unlink(Node node){ + final Object element = node.data; + final Node next = node.next; //下一个节点 + final Node prev = node.prev;//前一个节点 + + if (prev == null) {//待删除节点是首节点 + head = next; + } else { + prev.next = next; + node.prev = null;//解除待删除节点的引用关系 + } + + if (next == null) {//待删除节点是尾节点 + tail = prev; + } else { + next.prev = prev; + node.next = null;//解除待删除节点的引用关系 + } + + node.data = null;//清除节点数据 + size--; + return element;//返回清除节点数据 + } + + /** + * 在中间位置创建Iterator遍历 + * @return + */ + public Iterator iterator() { + return new Its(this.size>>1); + } + + /** + * 在指定位置创建Iterator遍历 + * @return + */ + public Iterator iterator(int index) { + checkIndex(index); + return new Its(index); + } + + /** + * 获取指定索引的节点对象 + * @param @param index + * @param @return + */ + private Node getNode(int index){ + if (index < (this.size >> 1)) { //在前半部分 + Node node = this.head; + for (int i = 0; i < index; i++){ + node = node.next; //向后查找 + } + return node; + } else { //在后半部分 + Node node = this.tail; + for (int i = this.size - 1; i > index; i--){ + node = node.prev; //向前查找 + } + return node; + } + } + + /** + * 判断是否为有效索引 + * @param @param index + * @param @return + */ + private boolean isEffectiveIndex(int index){ + return (index>=0 && index < size); + } + + /** + * 检测索引有效性,无效时抛出异常 + * @param index + */ + private void checkIndex(int index) { + if (!isEffectiveIndex(index)) + throw new IndexOutOfBoundsException("Index: "+index+" Out Of Bounds, 最大索引: "+(size-1)); + } + + + /** + * 实现Iterator的内部实现类 + */ + private class Its implements Iterator { + private Node lastReturned = null; + private Node node;//当前节点 + private int index;//当前节点索引 + + public Its(int index){ + node = isEffectiveIndex(index) ? getNode(index) : null; + this.index = index; + } + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = node; + node = node.next; + index++; + return lastReturned.data; + + } + + public boolean hasPrevious() { + return index >= 0; + } + + public Object previous() { + if (!hasPrevious()) + throw new NoSuchElementException(); + + lastReturned = node; + node = node.prev; + index--; + return lastReturned.data; + } + } + + /** + * 节点数据 + * Node + */ + private static class Node{ + Object data; + Node prev; + Node next; + + Node(Node prev,Object data,Node next){ + this.prev=prev; + this.data=data; + this.next=next; + } + } +} diff --git a/group16/63912401/src/com/coding/basic/List.java b/group16/63912401/src/com/coding/basic/List.java new file mode 100644 index 0000000000..7fe012ccab --- /dev/null +++ b/group16/63912401/src/com/coding/basic/List.java @@ -0,0 +1,16 @@ +package com.coding.basic; +/** + * 集合接口 + * @author greenhills + * @version 创建时间:2017年2月19日 下午10:49:40 + * + */ +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + public int capacity(); + boolean isEmpty(); +} diff --git a/group16/63912401/src/com/coding/basic/Queue.java b/group16/63912401/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..c6007c9e99 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Queue.java @@ -0,0 +1,43 @@ +package com.coding.basic; + +/** + * 队列数据结构 + * Queue + * @author greenhills + * 2017年2月25日 下午9:50:04 + */ +public class Queue { + private LinkedList elementData = new LinkedList(); + + /** + * 入队 + * @param o + */ + public void enQueue(Object o){ + elementData.addLast(o); + } + + /** + * 出队 + * @return + */ + public Object deQueue(){ + return elementData.removeFirst(); + } + + /** + * 判断是否为空 + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 获取栈内数据量 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/63912401/src/com/coding/basic/Stack.java b/group16/63912401/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b343c6de1d --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Stack.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +/** + * 栈数据结构 + * Stack + * @author greenhills + * 2017年2月25日 下午9:49:41 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + + /** + * 出栈 + * @return + */ + public Object pop(){ + return elementData.removeLast(); + } + + /** + * 获取栈顶数据 + * @return + */ + public Object peek(){ + return elementData.getLast(); + } + + /** + * 判断是否为空 + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 获取栈内数据量 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/63912401/src/com/coding/basic/Stack2.java b/group16/63912401/src/com/coding/basic/Stack2.java new file mode 100644 index 0000000000..8c10ccdf06 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Stack2.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +/** + * 栈数据结构 + * Stack + * @author greenhills + * 2017年2月25日 下午9:49:41 + */ +public class Stack2 { + private LinkedList elementData = new LinkedList(); + + /** + * 入栈 + * @param o + */ + public void push(Object o){ + elementData.addFirst(o); + } + + /** + * 出栈 + * @return + */ + public Object pop(){ + return elementData.removeFirst(); + } + + /** + * 获取栈顶数据 + * @return + */ + public Object peek(){ + return elementData.get(0); + } + + /** + * 判断是否为空 + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 获取栈内数据量 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/932886072/djj/ArrayList.java b/group16/932886072/djj/ArrayList.java new file mode 100644 index 0000000000..df3a11c386 --- /dev/null +++ b/group16/932886072/djj/ArrayList.java @@ -0,0 +1,64 @@ +package djj; + + +import java.util.Arrays; + +/** + * Created by jerry on 2017/2/26. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size>elementData.length*0.8){ + Arrays.copyOf(elementData,elementData.length*2); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + if (size>=index){ + Object[] temp=new Object[elementData.length]; + System.arraycopy(elementData,0,temp,0,index); + temp[index]=o; + System.arraycopy(elementData,index,temp,index+1,size-index); + elementData=temp; + }else if(sizesize){ + throw new RuntimeException("越界"); + }else{ + return elementData[index]; + } + } + + public Object remove(int index){ + Object tempObj=null; + if(index<=size){ + Object[] temp=new Object[elementData.length]; + System.arraycopy(elementData,0,temp,0,index); + tempObj=elementData[index]; + System.arraycopy(elementData,index+1,temp,index,size-index-1); + elementData=temp; + } + size--; + return tempObj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group16/932886072/djj/BinaryTreeNode.java b/group16/932886072/djj/BinaryTreeNode.java new file mode 100644 index 0000000000..d697be6ffc --- /dev/null +++ b/group16/932886072/djj/BinaryTreeNode.java @@ -0,0 +1,34 @@ +package djj; + +public class BinaryTreeNode { + + private BinaryTreeNode root; + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode insert(Object o){ + + return null; + } + + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } +} diff --git a/group16/932886072/djj/Iterator.java b/group16/932886072/djj/Iterator.java new file mode 100644 index 0000000000..4482e3a408 --- /dev/null +++ b/group16/932886072/djj/Iterator.java @@ -0,0 +1,9 @@ +package djj; + +/** + * Created by jerry on 2017/2/26. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/932886072/djj/LinkedList.java b/group16/932886072/djj/LinkedList.java new file mode 100644 index 0000000000..6e40d5cc69 --- /dev/null +++ b/group16/932886072/djj/LinkedList.java @@ -0,0 +1,96 @@ +package djj; + +public class LinkedList implements List { + //头节点 + private Node head; + //尾节点 +// private Node tail; + //当前游标节点 + private Node curNode; + public int size=0; + + + public void add(Object o){ + if(head==null){ + head=new Node(o); + head.next=null; + }else{ + curNode=head; + while(curNode.next!=null){ + curNode=curNode.next; + } + curNode.next=new Node(o); + } + size++; + } + public void add(int index , Object o){ + if(index>size||index<=0){ + throw new RuntimeException("越界"); + }else{ + curNode=head; + for(int i=0;isize||index<=0){ + throw new RuntimeException("越界"); + } + Node temp=head; + for(int i=0;isize||index<=0){ + throw new RuntimeException("越界"); + } + Node temp=head; + for(int i=0;i + + + + + diff --git a/group17/102228177/work2_19/.gitignore b/group17/102228177/work2_19/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/102228177/work2_19/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/102228177/.project b/group17/102228177/work2_19/.project similarity index 100% rename from group17/102228177/.project rename to group17/102228177/work2_19/.project diff --git a/group17/102228177/src/data2_19/ArrayList.java b/group17/102228177/work2_19/src/data2_19/ArrayList.java similarity index 100% rename from group17/102228177/src/data2_19/ArrayList.java rename to group17/102228177/work2_19/src/data2_19/ArrayList.java diff --git a/group17/102228177/src/data2_19/BinaryTreeNode.java b/group17/102228177/work2_19/src/data2_19/BinaryTreeNode.java similarity index 100% rename from group17/102228177/src/data2_19/BinaryTreeNode.java rename to group17/102228177/work2_19/src/data2_19/BinaryTreeNode.java diff --git a/group17/102228177/src/data2_19/Iterator.java b/group17/102228177/work2_19/src/data2_19/Iterator.java similarity index 100% rename from group17/102228177/src/data2_19/Iterator.java rename to group17/102228177/work2_19/src/data2_19/Iterator.java diff --git a/group17/102228177/src/data2_19/LinkedList.java b/group17/102228177/work2_19/src/data2_19/LinkedList.java similarity index 100% rename from group17/102228177/src/data2_19/LinkedList.java rename to group17/102228177/work2_19/src/data2_19/LinkedList.java diff --git a/group17/102228177/src/data2_19/List.java b/group17/102228177/work2_19/src/data2_19/List.java similarity index 100% rename from group17/102228177/src/data2_19/List.java rename to group17/102228177/work2_19/src/data2_19/List.java diff --git a/group17/102228177/src/data2_19/Queue.java b/group17/102228177/work2_19/src/data2_19/Queue.java similarity index 100% rename from group17/102228177/src/data2_19/Queue.java rename to group17/102228177/work2_19/src/data2_19/Queue.java diff --git a/group17/102228177/src/data2_19/Stack.java b/group17/102228177/work2_19/src/data2_19/Stack.java similarity index 100% rename from group17/102228177/src/data2_19/Stack.java rename to group17/102228177/work2_19/src/data2_19/Stack.java diff --git a/group17/102228177/work2_26/.classpath b/group17/102228177/work2_26/.classpath new file mode 100644 index 0000000000..80437ca942 --- /dev/null +++ b/group17/102228177/work2_26/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group17/102228177/work2_26/.project b/group17/102228177/work2_26/.project new file mode 100644 index 0000000000..2ae5966b60 --- /dev/null +++ b/group17/102228177/work2_26/.project @@ -0,0 +1,17 @@ + + + work2_26 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/102228177/work2_26/.settings/org.eclipse.jdt.core.prefs b/group17/102228177/work2_26/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group17/102228177/work2_26/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java b/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..60949d043e --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,202 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for (int i = 0; i < origin.length/2; i++) { + int temp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1] = temp; + } + } + + public static void main(String[] args) { + ArrayUtil util = new ArrayUtil(); + int[] origin = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; +// System.out.println(Arrays.toString(util.removeZero(origin))); + + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; +// System.out.println(Arrays.toString(util.merge(a1, a2))); + System.out.println(Arrays.toString(util.fibonacci(15))); + + System.out.println(Arrays.toString(util.getPrimes(23))); + } + + /** + * 现在有如下的一个数组: 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[] newArray = new int[oldArray.length]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + newArray[j] = oldArray[i]; + j++; + } + } + return Arrays.copyOf(newArray, j); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + + //Set是不允许重复的,所以将数组的值全部放在Set对象中 + Set set = new HashSet(); + + for(int i = 0; i < array1.length ; i++){ + set.add(array1[i]); + } + + for(int i = 0; i < array2.length ; i++){ + set.add(array2[i]); + } + + Iterator i = set.iterator(); + int[] arrays = new int[set.size()]; + int num=0; + while(i.hasNext()){ + int a = (Integer)i.next(); + arrays[num] = a; + num = num + 1; + } + + //对结果进行排序 + Arrays.sort(arrays); + return arrays; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, newArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + List list = new ArrayList(); + int f1 = 1, f2 = 1, f = 0; + list.add(f1); + list.add(f2); + while(f < max){ + f = f1+f2; + f1 = f2; + f2 = f; + list.add(f); + } + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i); + } + return arr; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + List list = new ArrayList(); + for (int i = 2; i < max; i++) { + boolean flag = true; + for (int j = 2; j < i; j++) { + if ( i % j == 0) { + flag = false; + break; + } + } + if(flag){ + list.add(i); + } + } + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i); + } + return arr; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + List list = new ArrayList(); + for (int i = 1; i <= max; i++){ + int sum=0; + for (int j = 1; j < i; j++){ + if(i%j==0){ + sum+=j; + } + } + if(i==sum){ + list.add(sum); + } + } + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i); + } + return arr; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String str = ""; + for (int i = 0; i < array.length; i++) { + str += seperator+array[i]; + } + return str.substring(1); + } +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/BeanUtil.java b/group17/102228177/work2_26/src/com/coderising/litestruts/BeanUtil.java new file mode 100644 index 0000000000..96e6a9f867 --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/BeanUtil.java @@ -0,0 +1,27 @@ +package com.coderising.litestruts; + +/** + * javaBean 工具 + * @author ren + * + */ +public class BeanUtil { + /** + * 返回set方法名 + * @param filedName 属性名 + * @return set方法名 + */ + public static String setter(String filedName){ + return "set"+filedName.substring(0, 1).toUpperCase()+filedName.substring(1); + } + + /** + * 返回get方法名 + * @param filedName 属性名 + * @return get方法名 + */ + public static String getter(String filedName){ + return "get"+filedName.substring(0, 1).toUpperCase()+filedName.substring(1); + } + +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/Dom4jUtil.java b/group17/102228177/work2_26/src/com/coderising/litestruts/Dom4jUtil.java new file mode 100644 index 0000000000..3eefec6d3d --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/Dom4jUtil.java @@ -0,0 +1,101 @@ +package com.coderising.litestruts; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * Dom4J 解析XML文件 + * @author ren + * + */ +public class Dom4jUtil { + + /** + * 传入文件路径解析xml文件获取根节点 + * @param path xml文件的绝对路径 + * @return Element 根节点 + */ + public static Element parseXml(String path){ + InputStream is = null; + Map map = new HashMap(); + try { + is = new FileInputStream(path); + //创建SAXReader读取XML + SAXReader reader = new SAXReader(); + //根据saxReader的read重写方法可知,既可以通过inputStream输入流来读取,也可以通过file对象来读取 + Document document = reader.read(is); + //获取根节点对象 + Element rootElement = document.getRootElement(); + return rootElement; + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + finally{ + if( is!=null ){ + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return null; + } + + /** + * 获取子节点的属性值并添加到map中并返回 + * @param element xml文件根节点对象 + * @return map 封装的属性值map对象 + */ + public static Map getAttribute(Element element){ + Map map = new HashMap(); + List elements = element.elements(); + for (Element ele : elements) { + String name = ele.attributeValue("name"); + String clas = ele.attributeValue("class"); + map.put(name, clas); + } + return map; + } + + /** + * 根据传入的Action名返回结果JSP + * @param element 根节点 + * @param actionName 标签name属性的value + * @return map 封装返回结果jsp的map对象 + */ + public static Map getJspMap(Element element,String actionName){ + Map map = new HashMap(); + List actions = element.elements(); + for (Element action : actions) { + if(actionName.equals(action.attributeValue("name"))){ + List results = action.elements(); + for (Element result : results) { + String name = result.attributeValue("name"); + String text = result.getText(); + map.put(name, text); + } + } + } + return map; + } + + public static void main(String[] args) { + String path = Dom4jUtil.class.getResource("").getPath()+"struts.xml"; + System.out.println(path); + Element element = parseXml(path); + Map attribute = getAttribute(element); + System.out.println(getJspMap(element,"login").get("success")); + } +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/LoginAction.java b/group17/102228177/work2_26/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group17/102228177/work2_26/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/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java b/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..0aeaf71537 --- /dev/null +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,77 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.Element; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + String path = Struts.class.getResource("").getPath()+"struts.xml"; + Element element = Dom4jUtil.parseXml(path); + Map attribute = Dom4jUtil.getAttribute(element); + String className = attribute.get(actionName); + try { + Class clazz = Class.forName(className); + Object o = clazz.newInstance(); + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + Field field = clazz.getDeclaredField(key); + Method method = clazz.getDeclaredMethod(BeanUtil.setter(field.getName()),String.class); + method.invoke(o, value); + } + Method method = clazz.getDeclaredMethod("execute"); + String str = (String) method.invoke(o); + Field[] fields = clazz.getDeclaredFields(); + Map map = new HashMap(); + for (Field field : fields) { + String fieldName = field.getName(); + Method method2 = clazz.getDeclaredMethod(BeanUtil.getter(fieldName)); + String ret = (String) method2.invoke(o); + map.put(fieldName, ret); + } + View view = new View(); + view.setParameters(map); + Map result = Dom4jUtil.getJspMap(element, actionName); + view.setJsp(result.get(str)); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + runAction(actionName, params); + } +} diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/StrutsTest.java b/group17/102228177/work2_26/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group17/102228177/work2_26/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/group17/102228177/work2_26/src/com/coderising/litestruts/View.java b/group17/102228177/work2_26/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group17/102228177/work2_26/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/group17/102228177/work2_26/src/com/coderising/litestruts/struts.xml b/group17/102228177/work2_26/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0141537ddb --- /dev/null +++ b/group17/102228177/work2_26/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/group17/1158154002/.classpath b/group17/1158154002/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group17/1158154002/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group17/1158154002/.gitignore b/group17/1158154002/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group17/1158154002/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group17/1158154002/.project b/group17/1158154002/.project new file mode 100644 index 0000000000..95ef0b2be8 --- /dev/null +++ b/group17/1158154002/.project @@ -0,0 +1,17 @@ + + + 1158154002 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group17/1158154002/src/test01/arrayList/ArrayListTest.java b/group17/1158154002/src/test01/arrayList/ArrayListTest.java new file mode 100644 index 0000000000..ae61880ccc --- /dev/null +++ b/group17/1158154002/src/test01/arrayList/ArrayListTest.java @@ -0,0 +1,34 @@ +package test01.arrayList; + +import org.junit.Test; + +public class ArrayListTest { + + @Test + public void cetrinWArrayListTest(){ + CetrinwList list = new CetrinwArrayList(); + + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + + System.out.println("下标为3的元素为"+list.get(3)); + System.out.println("数组size:"+list.size()); + list.remove(2); + System.out.print("remove后的数组size:"+list.size()); + + System.out.print("remove后的数组:"); + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + + list.insert(3,"gg"); + + System.out.println(""); + System.out.print("insert后的数组:"); + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + } +} diff --git a/group17/1158154002/src/test01/arrayList/CetrinwArrayList.java b/group17/1158154002/src/test01/arrayList/CetrinwArrayList.java new file mode 100644 index 0000000000..a69e617454 --- /dev/null +++ b/group17/1158154002/src/test01/arrayList/CetrinwArrayList.java @@ -0,0 +1,124 @@ +package test01.arrayList; + +public class CetrinwArrayList implements CetrinwList { + + /** + * 数组默认长度 + */ + private static final int DEFAULT_SIZE=10; + + /** + * 存储队列中的元素 + */ + private Object[] elements=null; + + /** + * 数组大小指针 + */ + private int capacity; + + /** + * 当前游标 + */ + private int current; + + public CetrinwArrayList() { + this(DEFAULT_SIZE); + } + + public CetrinwArrayList(int size) { + if (size<0) { + throw new RuntimeException("数组大小不能小于0"); + } else { + this.elements=new Object[size]; + this.current=0; + this.capacity=size; + } + } + + @Override + public E get(int index) { + confirmIndex(index); + return (E)elements[index]; + } + + @Override + public void add(E e) { + confirmSize(); + this.elements[current]=e; + this.current++; + } + + @Override + public void remove(int index) { + confirmIndex(index); + for (int i = index; i < elements.length; i++) { + if (i+1=index; i--) { + elements[i+1]=elements[i]; + } + elements[index]=e; + current++; + } + + @Override + public boolean contains(Object o) { + for (Object object : elements) { + if (object.equals(o)) { + return true; + } + } + return false; + } + + @Override + public int size() { + return this.current; + } + + @Override + public boolean isEmpty() { + if (current>0) { + return false; + } + return true; + } + + @Override + public void clearList() { + for (int i = 0; i < current; i++) { + elements[i]=null; + } + } + + /** + * 确认当前数组的容量,如果满足,则不操作,如果不满足,则增加空间 + */ + private void confirmSize(){ + if (this.current==this.capacity) { + this.capacity=this.capacity*3/2; + Object[] newElements=new Object[this.capacity]; + System.arraycopy(elements, 0, newElements, 0, elements.length); + this.elements=newElements; + } + } + + + /** + * 判断下标是否越界 + */ + private void confirmIndex(int index){ + if (index>capacity||index<0) { + throw new RuntimeException("下标越界"); + } + } +} diff --git a/group17/1158154002/src/test01/arrayList/CetrinwList.java b/group17/1158154002/src/test01/arrayList/CetrinwList.java new file mode 100644 index 0000000000..7badabf807 --- /dev/null +++ b/group17/1158154002/src/test01/arrayList/CetrinwList.java @@ -0,0 +1,43 @@ +package test01.arrayList; + +public interface CetrinwList { + /** + * 取得数据 + */ + E get(int index); + + /** + *新增数据 + */ + void add(E e); + + /** + * 移除数据 + */ + void remove(int index); + + /** + * 插入数据 + */ + void insert(int index,E e); + + /** + * 是否存在数据 + */ + boolean contains(Object o); + + /** + * 获得List长度 + */ + int size(); + + /** + * 是否为空 + */ + boolean isEmpty(); + + /** + * 清空 + */ + void clearList(); +} diff --git a/group17/1158154002/src/test01/linkedList/MyLinkedList.java b/group17/1158154002/src/test01/linkedList/MyLinkedList.java new file mode 100644 index 0000000000..9a39ab16c3 --- /dev/null +++ b/group17/1158154002/src/test01/linkedList/MyLinkedList.java @@ -0,0 +1,172 @@ +package test01.linkedList; + +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class MyLinkedList implements Iterable { + private int theSize; + private int modCount = 0; + private Node beginMarker; + private Node endMarker; + + private static class Node { + public T data; + public Node prev; + public Node next; + + public Node(T d, Node p, Node n) { + data = d; + prev = p; + next = n; + } + } + + public MyLinkedList() { + clear(); + } + + public void clear() { + beginMarker = new Node(null, null, null); + endMarker = new Node(null, beginMarker, null); + beginMarker.next = endMarker; + theSize = 0; + modCount++; + } + + public int size() { + return theSize; + } + + public boolean isEmpty() { + return size() == 0; + } + + public void add(T x) { + add(size(), x); + } + + public void add(int idx, T x) { + addBefore(getNode(idx), x); + } + + public T get(int idx) { + return getNode(idx).data; + } + + public T set(int idx, T newVal) { + Node p = getNode(idx); + T oldVal = p.data; + p.data = newVal; + return oldVal; + } + + public T remove(int idx) { + return remove(getNode(idx)); + } + + private void addBefore(Node p, T x) { + Node newNode = new Node(x, p.prev, p); + newNode.prev.next = newNode; + p.prev = newNode; + theSize++; + modCount++; + } + + private T remove(Node p) { + + final T element = p.data; + final Node next = p.next; + final Node prev = p.prev; + + if (prev == null) { + beginMarker = next; + } else { + prev.next = next; + p.prev = null; + } + + if (next == null) { + endMarker = prev; + } else { + next.prev = prev; + p.next = null; + } + + p.data = null; + theSize--; + modCount++; + return p.data; + } + + private Node getNode(int idx) { +// Node p; +// if (idx < 0 || idx > size()) { +// throw new IndexOutOfBoundsException(); +// } +// if (idx < size() / 2) { +// p = beginMarker.next; +// for (int i = 0; i < idx; i++) { +// p = p.next; +// } +// } else { +// p = endMarker; +// for (int i = size(); i < idx; i--) { +// p = p.prev; +// } +// } +// + if (idx < (size() >> 1)) { + Node p = beginMarker; + for (int i = 0; i < idx; i++) + p = p.next; + return p; + } else { + Node p = endMarker; + for (int i = size() - 1; i > idx; i--) + p = p.prev; + return p; + } + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + private Node current = beginMarker.next; + private int expectedModCount = modCount; + private boolean okToRemove = false; + + @Override + public boolean hasNext() { + return current != endMarker; + } + + @Override + public T next() { + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + if (!hasNext()) { + throw new NoSuchElementException(); + } + T nextItem = current.data; + current = current.next; + okToRemove = true; + return nextItem; + } + + public void remove() { + if (modCount != expectedModCount) { + throw new ConcurrentModificationException(); + } + if (!okToRemove) { + throw new IllegalStateException(); + } + MyLinkedList.this.remove(current.prev); + okToRemove = false; + expectedModCount++; + } + } +} diff --git a/group17/1158154002/src/test01/linkedList/MyLinkedListTest.java b/group17/1158154002/src/test01/linkedList/MyLinkedListTest.java new file mode 100644 index 0000000000..9903188289 --- /dev/null +++ b/group17/1158154002/src/test01/linkedList/MyLinkedListTest.java @@ -0,0 +1,37 @@ +package test01.linkedList; + +import org.junit.Test; + +public class MyLinkedListTest { + + @Test + public void MyLinkedListTest(){ + MyLinkedList list = new MyLinkedList(); + + list.add("a"); + list.add("b"); + list.add("c"); + list.add("d"); + + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + System.out.println("下标为3的元素为"+list.get(3)); + System.out.println("数组size:"+list.size()); + list.remove(2); + System.out.print("remove后的数组size:"+list.size()); + + System.out.print("remove后的数组:"); + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + + list.add(3,"gg"); + + System.out.println(""); + System.out.print("insert后的数组:"); + for (int i = 0; i < list.size() ; i++) { + System.out.print(list.get(i)+","); + } + } +} diff --git a/group17/1158154002/src/test01/queue/Queue.java b/group17/1158154002/src/test01/queue/Queue.java new file mode 100644 index 0000000000..002f05e3e8 --- /dev/null +++ b/group17/1158154002/src/test01/queue/Queue.java @@ -0,0 +1,105 @@ +package test01.queue; + +import java.util.Arrays; +import java.util.Collection; + +/** + *

+ * structure for queue + *

+ *

+ * 用1个数组存数据,数组 小index 是 head,大index是tail,
+ * 插入在 tail 处,删除在 head 处, + *

+ * + * @author eric + * @param + */ +public class Queue { + /** 初始容量 */ + public static final int DEFAULT_SIZE = 10; + /** 容量不足时翻倍数 */ + public static final float DEFAULT_INCREMENT = 1.5f; + /** 数据 */ + private Object[] elementData; + /** 元素个数 */ + private int elementCount; + /** 数组的头部,即 下次删除数据的 index */ + private int head; + /** 数组的尾部,即 下次插入数据的 index */ + private int tail; + + public Queue() { + this(DEFAULT_SIZE); + } + + public Queue(int size) { + this.elementData = new Object[size]; + this.elementCount = 0; + this.head = 0; + this.tail = 0; + } + + public Queue(Object[] data) { + this.elementData = data; + this.elementCount = data.length; + this.head = 0; + this.tail = 0; + } + + public Queue(Collection c) { + this(c.toArray()); + } + + /** + * 添加数据 到尾部 + * + * @param ele + * @return + */ + public synchronized E add(E ele) { + if (tail >= elementData.length) { + adjustData(); + } + elementData[tail] = ele; + elementCount++; + tail++; + return ele; + }; + + /** + * 删除数据 从头部 + * + * @return + */ + @SuppressWarnings("unchecked") + public synchronized E remove() { + E e = (E) elementData[head]; + elementData[head] = null; + elementCount--; + head++; + return e; + }; + + /** + * 获得当前的数据 + * + * @return + */ + public synchronized Object[] getData() { + return Arrays.copyOfRange(this.elementData, this.head, this.tail); + } + + public synchronized void adjustData() { + if (tail >= elementData.length) { // tail 处空间不足时调用 + // head 的空位去掉 + int newSize = (elementData.length == elementCount) ? (int) Math.ceil(elementCount * DEFAULT_INCREMENT) + : elementData.length; + elementData = Arrays.copyOfRange(elementData, head, elementData.length); + // 调整空间 + elementData = Arrays.copyOf(elementData, newSize); + tail = elementCount; + head = 0; + } + } +} diff --git a/group17/1158154002/src/test01/queue/QueueTest.java b/group17/1158154002/src/test01/queue/QueueTest.java new file mode 100644 index 0000000000..6abb1aa5c6 --- /dev/null +++ b/group17/1158154002/src/test01/queue/QueueTest.java @@ -0,0 +1,41 @@ +package test01.queue; + +import org.junit.Test; + +public class QueueTest{ + @Test + public void test() { + Queue queueOne = new Queue(); + // 第1次 加入个数 + int addCountOne = 30; + // 第1次 删除个数 + int removeCountOne = 20; + // 第2次 加入个数 + int addCountTwo = 10; + + for (int i = 0; i < addCountOne; i++) { + queueOne.add(i); + } + Object[] data = queueOne.getData(); + for (int i = 0; i < data.length; i++) { + System.out.println((Integer) data[i] == i); + } + + for (int i = 0; i < removeCountOne; i++) { + System.out.println(queueOne.remove() == i); + } + + for (int i = 0; i < addCountTwo; i++) { + queueOne.add(i * 10); + } + Object[] data2 = queueOne.getData(); + int baseCount = addCountOne - removeCountOne; + for (int i = 0; i < addCountTwo; i++) { + System.out.println((Integer) data2[baseCount + i] == i * 10); + } + } + + public static void main(String[] args) { + new QueueTest().test(); + } +} diff --git a/group17/1158154002/src/test01/stack/MyStack.java b/group17/1158154002/src/test01/stack/MyStack.java new file mode 100644 index 0000000000..766a8083b2 --- /dev/null +++ b/group17/1158154002/src/test01/stack/MyStack.java @@ -0,0 +1,111 @@ +package test01.stack; + +import java.util.Iterator; + +import com.sun.org.apache.bcel.internal.generic.POP; +import com.sun.org.apache.bcel.internal.generic.PUSH; + +public class MyStack implements Iterable { + private static final int DEFAULT_SIZE=10; + private int size; + private T[] item; + private int top; + + public MyStack() { + clear(); + } + + public void clear(){ + size=0; + top=-1; + ensureCapacity(DEFAULT_SIZE); + } + + public int size(){ + return size; + } + + public boolean isEmpty(){ + return size()==0; + } + + public void trumToSize(){ + ensureCapacity(size()); + } + + public void ensureCapacity(int capacity){ + if (capacity iterator() { + // TODO Auto-generated method stub + return new StackIterator(); + } + + private class StackIterator implements Iterator{ + private int current=0; + + @Override + public boolean hasNext() { + return current<=top; + } + + @Override + public T next() { + if (!hasNext()) { + throw new NullPointerException(); + } + return item[current++]; + } + + } + + public static void main(String[] args) { + MyStack stack = new MyStack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.pop(); + stack.push(4); + stack.push(5); + Iterator iterator = stack.iterator(); + while (iterator.hasNext()) { + System.out.print(iterator.next() + " "); + } + } +} diff --git a/group17/1158154002/src/test01/tree/Test.java b/group17/1158154002/src/test01/tree/Test.java new file mode 100644 index 0000000000..489862534c --- /dev/null +++ b/group17/1158154002/src/test01/tree/Test.java @@ -0,0 +1,20 @@ +package test01.tree; + +public class Test { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Tree tree = new Tree(); + tree.addNode(null, "string"); + tree.addNode(tree.getNode("string"), "hello"); + tree.addNode(tree.getNode("string"), "world"); + tree.addNode(tree.getNode("hello"), "sinny"); + tree.addNode(tree.getNode("hello"), "fredric"); + tree.addNode(tree.getNode("world"), "Hi"); + tree.addNode(tree.getNode("world"), "York"); + tree.showNode(tree.root); + + System.out.println("end of the test"); + } + +} diff --git a/group17/1158154002/src/test01/tree/Tree.java b/group17/1158154002/src/test01/tree/Tree.java new file mode 100644 index 0000000000..2c4f1ea6b4 --- /dev/null +++ b/group17/1158154002/src/test01/tree/Tree.java @@ -0,0 +1,56 @@ +package test01.tree; + +public class Tree { + + public TreeNode root; + + public Tree(){} + + public void addNode(TreeNode node, T newNode){ + //增加根节点 + if(null == node){ + if(null == root){ + root = new TreeNode(newNode); + } + }else{ + TreeNode temp = new TreeNode(newNode); + node.nodelist.add(temp); + } + } + + /* 查找newNode这个节点 */ + public TreeNode search(TreeNode input, T newNode){ + + TreeNode temp = null; + + if(input.t.equals(newNode)){ + return input; + } + + for(int i = 0; i < input.nodelist.size(); i++){ + + temp = search(input.nodelist.get(i), newNode); + + if(null != temp){ + break; + } + } + + return temp; + } + + public TreeNode getNode(T newNode){ + return search(root, newNode); + } + + public void showNode(TreeNode node){ + if(null != node){ + //循环遍历node的节点 + System.out.println(node.t.toString()); + + for(int i = 0; i < node.nodelist.size(); i++){ + showNode(node.nodelist.get(i)); + } + } + } +} \ No newline at end of file diff --git a/group17/1158154002/src/test01/tree/TreeNode.java b/group17/1158154002/src/test01/tree/TreeNode.java new file mode 100644 index 0000000000..7114095d45 --- /dev/null +++ b/group17/1158154002/src/test01/tree/TreeNode.java @@ -0,0 +1,21 @@ +package test01.tree; + +import java.util.ArrayList; +import java.util.List; + +public class TreeNode { + public T t; + private TreeNode parent; + + public List> nodelist; + + public TreeNode(T stype){ + t = stype; + parent = null; + nodelist = new ArrayList>(); + } + + public TreeNode getParent() { + return parent; + } +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/array/ArrayUtil.java b/group17/1158154002/src/test02/array/ArrayUtil.java new file mode 100644 index 0000000000..1757a08958 --- /dev/null +++ b/group17/1158154002/src/test02/array/ArrayUtil.java @@ -0,0 +1,241 @@ +package test02.array; + +import java.util.ArrayList; +import java.util.Arrays; + +import com.sun.javafx.image.impl.IntArgb; + +public class ArrayUtil { + + public static void main(String[] args) { +// int[] arr={7, 9, 30, 3, 4}; +// reverseArray(arr); + +// int[] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; +// removeZero(oldArr); + +// int[] a1={3, 5, 7,8}; +// int[] a2={4, 5, 6,7}; +// merge(a1,a2); + +// int[] a1={3, 5, 7,8}; +// grow(a1, 3); + +// fibonacci(15); + +// getPrimes(23); + +// getPerfectNumbers(4000); + + int[] a={1,2,3,4}; + join(a, "-"); + } + + /** + * 给定一个整形数组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[] newArr=new int[origin.length]; + int j=0; + for (int i = origin.length-1; i >=0; i--) { + newArr[j++]=origin[i]; + } + System.out.println(Arrays.toString(newArr)); + } + + /** + * 现在有如下的一个数组: 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){ + ArrayList list=new ArrayList<>(); + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i]!=0) { + list.add(oldArray[i]); + } + } + + int[] newArr=new int[list.size()]; + int j=0; + for (int i : list) { + newArr[j++]=i; + } + System.out.println(Arrays.toString(newArr)); + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, 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){ + ArrayList list=new ArrayList<>(); + for (Integer arr1 : array1) { + list.add(arr1); + } + + for (Integer arr2 : array2) { + if (!list.contains(arr2)) { + list.add(arr2); + } + } + + int[] newArr=new int[list.size()]; + int i=0; + for (int one : list) { + newArr[i++]=one; + } + + for (int j = 0; j < newArr.length-1; j++) { + for (int k = 0; k < newArr.length-1-j; k++) { + + if (newArr[k]>newArr[k+1]) { + int temp=newArr[k]; + newArr[k]=newArr[k+1]; + newArr[k+1]=temp; + } + } + } + System.out.println(Arrays.toString(newArr)); + return newArr; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] newArr=new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + System.out.println(Arrays.toString(newArr)); + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max>1) { + ArrayList list=new ArrayList<>(); + list.add(1); + list.add(1); + while (list.get(list.size()-1) list = new ArrayList<>(); + + for (int i = 2; i < max; i++) { + int j = 2; + while (j < i) { +// System.out.println("i:"+i+",j:"+j+",i%j:"+(i%j)); + if (i%j==0) { + break; + } + j++; + } + if (j==i) { + list.add(i); + } + } + + int[] newArr = new int[list.size()]; + int i = 0; + for (int item : list) { + newArr[i++] = item; + } + System.out.println(Arrays.toString(newArr)); + + return newArr; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + ArrayList list = new ArrayList<>(); + for (int i = 1; i < max; i++) { + int j = 1; + int sum=0; + while (j < i) { +// System.out.println("i:"+i+",j:"+j+",i%j:"+(i%j)); + if (i%j==0) { + sum=sum+j; + } + j++; + } + + if (sum==i) { + list.add(i); + } + } + + int[] newArr = new int[list.size()]; + int i = 0; + for (int item : list) { + newArr[i++] = item; + } + System.out.println(Arrays.toString(newArr)); + + return newArr; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + String result=Arrays.toString(array).replace(" ", "").replace("[", "").replace("]", "").replace(",", seperator); + System.out.println(result); + return result; + } +} diff --git a/group17/1158154002/src/test02/litestruts/Action.java b/group17/1158154002/src/test02/litestruts/Action.java new file mode 100644 index 0000000000..d7a88fac57 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/Action.java @@ -0,0 +1,22 @@ +package test02.litestruts; + +import java.util.HashMap; + +public class Action { + String className; + HashMap resultJspMap; + + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public HashMap getResultJspMap() { + return resultJspMap; + } + public void setResultJspMap(HashMap resultJspMap) { + this.resultJspMap = resultJspMap; + } + +} diff --git a/group17/1158154002/src/test02/litestruts/LoginAction.java b/group17/1158154002/src/test02/litestruts/LoginAction.java new file mode 100644 index 0000000000..037c6f6d58 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package test02.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/Struts.java b/group17/1158154002/src/test02/litestruts/Struts.java new file mode 100644 index 0000000000..cb556a5652 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/Struts.java @@ -0,0 +1,60 @@ +package test02.litestruts; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import test02.litestruts.sax.SAXParserDemo; +import test02.litestruts.util.StringUtil; + +public class Struts { + + public static View runAction(String actionName, Map parameters){ + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + Action action = SAXParserDemo.run(); + View view=new View(); + try { + Class clazz = Class.forName(action.getClassName()); + Object obj = clazz.newInstance(); + for (String element : parameters.keySet()) { + Method method = clazz.getMethod("set" + StringUtil.captureName(element), String.class); + method.invoke(obj, parameters.get(element)); + } + Method exectue = clazz.getMethod("execute", null); + String result = (String) exectue.invoke(obj, null); + view.setJsp(action.getResultJspMap().get(result)); + + Method getMsg = clazz.getMethod("getMessage", null); + String msg=(String) getMsg.invoke(obj, null); + Map map=new HashMap<>(); + map.put("message", msg); + System.out.println(map); + view.setParameters(map); + + } catch (Exception e) { + e.printStackTrace(); + } + return view; + } + +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/StrutsTest.java b/group17/1158154002/src/test02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..ad3c0f2b80 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package test02.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed(){ + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/View.java b/group17/1158154002/src/test02/litestruts/View.java new file mode 100644 index 0000000000..73fc11eb13 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/View.java @@ -0,0 +1,23 @@ +package test02.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/group17/1158154002/src/test02/litestruts/sax/SAXParser.java b/group17/1158154002/src/test02/litestruts/sax/SAXParser.java new file mode 100644 index 0000000000..4efcd0b5cb --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/sax/SAXParser.java @@ -0,0 +1,221 @@ +package test02.litestruts.sax; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; + +import test02.litestruts.Action; + +public class SAXParser { + private static SAXParser parserInstance = new SAXParser(); + private static SAXParserHandler parserHandler; + private SAXParser(){} // Singleton Pattern, a private constructor + private static SAXParserState state = SAXParserState.OUT_OF_TAG; // initial state + + public static SAXParser getInstance() { + return parserInstance; + } + + public Action parse(String path){ + try { + BufferedReader br = new BufferedReader(new FileReader(path)); + int currentCharCode; + // callback start document + parserHandler.startDocument(); + try { + while((currentCharCode = br.read()) != -1){ + char currentChar = (char)currentCharCode; + handleParser(currentChar); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return parserHandler.endDocument(); + } + + public void setHandler(SAXParserHandler handler){ + parserHandler = handler; + } + + private static void handleParser(char c) { + // This SAX Parser will ignore any line wrap. + if(c == '\n'){ + return; + } + switch (state){ + case OUT_OF_TAG:{ + if(c == '<'){ + if(SAXParsedData.innerText.trim().length() != 0) { + parserHandler.innerText(SAXParsedData.innerText); + } + SAXParsedData.innerText = ""; + SAXParsedData.tagName = ""; + state = SAXParserState.BEGIN_START_OR_END_TAG; + } else if (c == '>') { + state = SAXParserState.SYNTAX_ERROR; + } else { + SAXParsedData.innerText += c; + } + break; + } + case BEGIN_START_OR_END_TAG:{ + if(c == '/') { + SAXParsedData.tagName = ""; + state = SAXParserState.IN_END_TAG; + }else if(c == '?' || c == '!'){ + state = SAXParserState.METADATA; + }else{ + SAXParsedData.tagName += c; + state = SAXParserState.IN_START_TAG; + } + break; + } + case IN_START_TAG:{ + if(c == ' '){ + state = SAXParserState.SPACE_IN_START_TAG; + }else if(c == '>'){ + // callback startElement event; + parserHandler.startElement(SAXParsedData.tagName, SAXParsedData.attributes); + SAXParsedData.clear(); + state = SAXParserState.CLOSE_START_TAG; + }else { + SAXParsedData.tagName += c; + } + break; + } + case SPACE_IN_START_TAG:{ + if(SAXParsedData.tagName.length() > 0){ + if(c != ' '){ + SAXParsedData.attribKey += c; + state = SAXParserState.IN_ATTRIB_KEY; + } + } + break; + } + case IN_ATTRIB_KEY:{ + if(c == '='){ + state = SAXParserState.IN_ATTRIB_EQUAL; + }else{ + SAXParsedData.attribKey += c; + } + break; + } + case IN_ATTRIB_EQUAL:{ + if(c == '"'){ + state = SAXParserState.IN_ATTRIB_VALUE; + } + break; + } + case IN_ATTRIB_VALUE:{ + if(c == '"'){ + SAXParsedData.newAttribute(); + state = SAXParserState.IN_START_TAG; + }else{ + SAXParsedData.attribValue += c; + } + break; + } + case CLOSE_START_TAG:{ + if(c == '<') { + state = SAXParserState.BEGIN_START_OR_END_TAG; + }else{ + SAXParsedData.innerText += c; + state = SAXParserState.OUT_OF_TAG; + } + break; + } + case IN_END_TAG:{ + if(c == '>'){ + // callback endElement event + parserHandler.endElement(SAXParsedData.tagName); + state = SAXParserState.CLOSE_END_TAG; + }else{ + SAXParsedData.tagName += c; + } + break; + } + case CLOSE_END_TAG:{ + if(c == ' '){ + state = SAXParserState.OUT_OF_TAG; + }else if(c == '<'){ + SAXParsedData.tagName = ""; + state = SAXParserState.BEGIN_START_OR_END_TAG; + } + break; + } + case METADATA:{ + if(c == '>'){ + state = SAXParserState.CLOSE_END_TAG; + } + break; + } + case SYNTAX_ERROR:{ + try { + throw new Exception(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + } + + private enum SAXParserState { + // The state when parser meets "<". This is a pending state. + // If the next char is "/", the state will be IN_END_TAG, + // Otherwise, the state will be IN_START_TAG + BEGIN_START_OR_END_TAG, + + // The state when parser is reading between start tag(<...>). + // When parser meets ">", callback "startElement" event + IN_START_TAG, + + // The state when parser is reading between end tag(). + // When parser meets "<", callback "endElement" event + IN_END_TAG, + + // The state when parser meets " ", and is in IN_START_TAG state. + // If the length of tag_name is non-zero, finish parsing tag_name. + // Otherwise, finish parsing a key/value attribute. + SPACE_IN_START_TAG, + IN_ATTRIB_KEY,IN_ATTRIB_EQUAL,IN_ATTRIB_VALUE, + CLOSE_START_TAG,CLOSE_END_TAG, + + // The state when parser is reading any char at the outside of , or between two . This is a pending state. + // Contents between will be recorded, but if the contents consist only spaces, the content will be discarded. + // Otherwise, callback "innerText" event. + OUT_OF_TAG, + + METADATA, SYNTAX_ERROR + } + + private static class SAXParsedData{ + private static String tagName = ""; + private static String attribKey = ""; + private static String attribValue = ""; + private static String innerText = ""; + private static HashMap attributes = new HashMap<>(); + + private static void clear(){ + tagName = ""; + attribKey = ""; + attribValue = ""; + innerText = ""; + attributes.clear(); + } + + private static void newAttribute(){ + attributes.put(attribKey, attribValue); + attribKey = ""; + attribValue = ""; + } + } +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/sax/SAXParserDemo.java b/group17/1158154002/src/test02/litestruts/sax/SAXParserDemo.java new file mode 100644 index 0000000000..88c3056a07 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/sax/SAXParserDemo.java @@ -0,0 +1,76 @@ +package test02.litestruts.sax; + +import java.util.HashMap; + +import test02.litestruts.Action; + +public class SAXParserDemo{ + + public static Action run( ){ + //创建实例 + SAXParser parser = SAXParser.getInstance(); + //为解析器设置好各个事件的回调函数 + parser.setHandler(new SAXParserHandler() { + //创建好自定义变量,用以记录XML文档中需要的数据 + String resultName = ""; + String className = ""; + HashMap resultJspMap = new HashMap<>(); + + boolean foundClass = false; + + //当解析开始时调用 + @Override + public void startDocument() { + System.out.println("Start parsing"); + } + + //当完成一个XML开始标签(例如)的解析时调用 + @Override + public void startElement(String tagName, HashMap attributes) { + if(tagName.equals("action")){ + if(attributes.get("name").equals("login")){ + className = attributes.get("class"); + foundClass = true; + }else{ + foundClass = false; + } + }else if(tagName.equals("result") && foundClass){ + resultName = attributes.get("name"); + } + } + + //当完成一个XML结束标签(例如)的解析时调用 + @Override + public void endElement(String tagName) { + + } + + //当一段XML标签之间的内容被解析完成时调用 + @Override + public void innerText(String innerText) { + if(foundClass){ + String jsp = innerText; + resultJspMap.put(resultName,jsp); + } + } + + @Override + //当解析器读到XML文档结尾时调用 + public Action endDocument() { + + System.out.println(className); + System.out.println(resultJspMap); + System.out.println("End parsing"); + + Action action=new Action(); + action.setClassName(className); + action.setResultJspMap(resultJspMap); + return action; + } + }); + + //调用此方式,开始解析 + return parser.parse("src/test02/litestruts/struts.xml"); + } + +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/sax/SAXParserHandler.java b/group17/1158154002/src/test02/litestruts/sax/SAXParserHandler.java new file mode 100644 index 0000000000..d93bdb0133 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/sax/SAXParserHandler.java @@ -0,0 +1,13 @@ +package test02.litestruts.sax; + +import java.util.HashMap; + +import test02.litestruts.Action; + +public interface SAXParserHandler { + void startDocument(); + void startElement(String tagName, HashMap attributes); + void endElement(String tagName); + Action endDocument(); + void innerText(String innerText); +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/struts.xml b/group17/1158154002/src/test02/litestruts/struts.xml new file mode 100644 index 0000000000..238ddf58a2 --- /dev/null +++ b/group17/1158154002/src/test02/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/group17/1158154002/src/test02/litestruts/util/StringUtil.java b/group17/1158154002/src/test02/litestruts/util/StringUtil.java new file mode 100644 index 0000000000..63bc8f2f74 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/util/StringUtil.java @@ -0,0 +1,13 @@ +package test02.litestruts.util; + +public class StringUtil { + + //首字母大写 + public static String captureName(String name) { + // name = name.substring(0, 1).toUpperCase() + name.substring(1); + // return name; + char[] cs=name.toCharArray(); + cs[0]-=32; + return String.valueOf(cs); + } +} diff --git a/group17/1204187480/.gitignore b/group17/1204187480/.gitignore new file mode 100644 index 0000000000..2a5296f902 --- /dev/null +++ b/group17/1204187480/.gitignore @@ -0,0 +1,21 @@ +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ +*.iml +rebel.* +.rebel.* + +target diff --git a/group17/1204187480/code/homework/coderising/pom.xml b/group17/1204187480/code/homework/coderising/pom.xml new file mode 100644 index 0000000000..b457f36dab --- /dev/null +++ b/group17/1204187480/code/homework/coderising/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + coderising + + com.coding + parent + 1.0-SNAPSHOT + ../parent/pom.xml + + + + 2.1 + 1.2.24 + + + + + commons-digester + commons-digester + ${commons-digester.version} + + + com.alibaba + fastjson + ${fastjson.version} + + + + + + \ No newline at end of file diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5496d8084d --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/array/ArrayUtil.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..9aa6404d2a --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,252 @@ +package com.coderising.array; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ArrayUtil { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + /** + * 给定一个整形数组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; + int mid = length >> 1; + for (int i = 0; i < mid; i++) { + int hIndex = length - 1 -i; + int l = origin[i]; + int h = origin[hIndex]; + origin[hIndex] = l; + origin[i] = h; + } + + } + + /** + * 现在有如下的一个数组: 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 removeValue = 0; + return removeValue(oldArray, removeValue); + } + + private int[] removeValue(int[] oldArray, int removeValue) { + int length = oldArray.length; + int[] dest = new int[length]; + int j = 0; + for(int i = 0; i < length; i++) { + int v = oldArray[i]; + if (v != removeValue) { + dest[j++] = v; + } + } + + int[] retArray = new int[j]; + System.arraycopy(dest, 0, retArray, 0, j); + return retArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * todo 数组 a1, b1 自身去重 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int length1 = array1.length; + int length2 = array2.length; + int length = length1 + length2; + int[] newArray = new int[length]; + + return findAndSetLeastWithOutDuplicate(array1, array2, 0, 0, 0, 0, newArray); + } + + /** + * todo 优化递归出口判断, 优化三个条件判断为一个 + * @param array1 + * @param array2 + * @param i + * @param j + * @param k + * @param duplicate + * @param newArray + * @return + */ + private int[] findAndSetLeastWithOutDuplicate(int[] array1, int[] array2, int i, int j, int k, int duplicate, int[] newArray) { + + if (i == array1.length && j < array2.length) { + System.arraycopy(array2, j, newArray, k, array2.length - j); + return copyLastValues(newArray, duplicate); + } + if (j == array2.length && i < array1.length) { + System.arraycopy(array1, i, newArray, k, array1.length - i); + return copyLastValues(newArray, duplicate); + } + if (j == array2.length && i == array1.length) { + return copyLastValues(newArray, duplicate); + } + + int v1 = array1[i]; + int v2 = array2[j]; + if (v1 < v2) { + newArray [k] = v1; + return findAndSetLeastWithOutDuplicate(array1, array2, ++i, j, ++k, duplicate, newArray); + } else if (v1 > v2){ + newArray [k] = v2; + return findAndSetLeastWithOutDuplicate(array1, array2, i, ++j, ++k, duplicate, newArray); + } else { + newArray [k] = v2; + return findAndSetLeastWithOutDuplicate(array1, array2, ++i, ++j, ++k, ++duplicate, newArray); + } + + } + + private int[] copyLastValues(int[] newArray, int duplicate) { + int[] retArray = new int[newArray.length - duplicate]; + System.arraycopy(newArray, 0, retArray, 0, retArray.length); + return retArray; + } + + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if (max <= 1) { + return new int[]{}; + } + int [] newArray = new int[max]; + newArray[0] = 1; + return fibonacciN(1, 1, 1, max, newArray); + } + + private int[] fibonacciN(int size, int current, int next, int max, int[] newArray) { + if (next >= max) { + int[] retArray = new int[size]; + System.arraycopy(newArray, 0, retArray, 0, size); + return retArray; + } else { + newArray[++size - 1] = next; + return fibonacciN(size, next, current + next, max, newArray); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * todo 使用已有的质数序列 优化质数验证 + * @param max + * @return + */ + public int[] getPrimes(int max){ + if (max <= 2) { + return new int[]{}; + } + + int[] newArray = new int[max]; + int j = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + newArray[j++] = i; + } + } + int[] retArray = new int[j]; + System.arraycopy(newArray, 0, retArray, 0, j); + return retArray; + } + + private boolean isPrime(int number) { + int limit = Double.valueOf(Math.sqrt(number)).intValue(); + for(int i = 2; i <= limit; i++) { + if (number % i == 0 ) { + return false; + } + } + return true; + + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] newArray = new int[48]; // 经过不少数学家研究,到2013年2月6日为止,一共找到了48个完全数。 + int j = 0; + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { + if (j >= newArray.length) { + newArray = this.grow(newArray, 1); + } + newArray[j++] = i; + + } + } + int[] retArray = new int[j]; + System.arraycopy(newArray, 0, retArray, 0, j); + return retArray; + } + + private boolean isPerfectNumber(int number) { + int sum = 0; + for (int i = 1; i < number; i++) { + if (number % i == 0) { + sum += i; + } + } + + return sum == number; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder builder = new StringBuilder(20); + int length = array.length; + for (int i = 0; i< length; i++) { + builder.append(array[i]).append(seperator); + } + builder.deleteCharAt(builder.length() - seperator.length()); + return builder.toString(); + } + + +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..4e1cf74391 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,122 @@ +package com.coderising.litestruts; + +import com.coderising.litestruts.parser.ActionConfig; +import com.coderising.litestruts.parser.DefaultStrutsParser; +import com.coderising.litestruts.parser.StrutsConfig; +import com.coderising.litestruts.parser.StrutsParser; +import com.coderising.litestruts.util.BeanUtils; + +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + private static StrutsParser strutsParser = new DefaultStrutsParser(); + + private static final String STRUTS_CONFIG_PATH = "struts.xml"; + private static final BeanUtils beanUtils = new BeanUtils(); + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + /** + * 0. 读取配置文件struts.xml + */ + StrutsConfig strutsConfig = strutsParser.parser(STRUTS_CONFIG_PATH); + ActionConfig actionConfig = strutsConfig.getActions().get(actionName); + /** + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + */ + Object action = setPropertiesForAction(actionConfig, actionName, parameters); + + /** + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + */ + String resultName = doExecute(action); + /** + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + */ + View view = createViewAndSetParameters(action); + /** + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + setViewValue(view, resultName, actionConfig); + return view; + } + + private static void setViewValue(View view, String resultName, ActionConfig config) { + view.setJsp(config.getResults().get(resultName).getView()); + } + + private static View createViewAndSetParameters(Object action) { + View view = new View(); + view.setParameters(beanUtils.describe(action)); + return view; + } + + private static String doExecute(Object action) { + return (String) beanUtils.invokeWithNoParamter("execute", action); + } + + private static Object setPropertiesForAction(ActionConfig actionConfig, String actionName, Map parameters) { + Object action = createInstance(findActionClass(actionConfig.getClassName())); + for (Map.Entry entry : parameters.entrySet()) { + setProperty(entry.getKey(), entry.getValue(), action); + } + return action; + } + + /** + * todo 校验 key 是否存在 + * + * @param key + * @param value + * @param action + */ + private static void setProperty(String key, String value, Object action) { + beanUtils.setPara(value, key, action); + } + + private static Object createInstance(Class classValue) { + try { + return classValue.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + + private static Class findActionClass(String className) { + try { + return Class.forName(className); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(e); + } + } + +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java new file mode 100644 index 0000000000..4aaba284fb --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java @@ -0,0 +1,45 @@ +package com.coderising.litestruts.parser; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ActionConfig { + private String name; + private String className; + private Map results = new HashMap<>(10); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } + + public void addResult(Result result) { + this.results.put(result.getName(), result); + } + + +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java new file mode 100644 index 0000000000..ea58507738 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java @@ -0,0 +1,52 @@ +package com.coderising.litestruts.parser; + +import com.alibaba.fastjson.JSON; +import org.apache.commons.digester.Digester; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +import java.io.File; +import java.io.IOException; + +/** + * 解析 struts.xml 文件 + * @apiNote 借鉴 http://www.everycoding.com/coding/78.html; http://blog.csdn.net/caihaijiang/article/details/5944955 + * Created by luoziyihao on 3/5/17. + */ +public class DefaultStrutsParser implements StrutsParser { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Override + public StrutsConfig parser(String filePathInClasspath) { + String path = this.getClass().getClassLoader().getResource(filePathInClasspath).getPath(); + File input = new File(path); + Digester digester = new Digester(); + // 创建 StrutsConfig 对象 + digester.addObjectCreate("struts", StrutsConfig.class); + // 将 struts 节点上的attribute属性映射到 StrutsConfig 对象的属性上 + digester.addSetProperties("struts"); + digester.addObjectCreate("struts/action", ActionConfig.class); + // 将 struts/action 节点上的attribute属性映射到 Action 对象的属性上, 并自定义属性映射 + digester.addSetProperties("struts/action" + , new String[]{"name", "class"}, new String[]{"name", "className"}); + digester.addObjectCreate("struts/action/result", Result.class); + digester.addSetProperties("struts/action/result" + , new String[]{"name"}, new String[]{"name"}); + // 将 struts/action/result 节点上的body属性映射到 Result 对象的属性上 + digester.addCallMethod("struts/action/result", "setView", 0); + // 对应struts/action/result 生成的对象添加到 Action中 + digester.addSetNext("struts/action/result", "addResult"); + // 对应struts/action 生成的对象添加到 Struts中 + digester.addSetNext("struts/action", "addAction"); + + try { + StrutsConfig strutsConfig = (StrutsConfig) digester.parse(input); + logger.debug("strutsConfig={}", JSON.toJSONString(strutsConfig)); + return strutsConfig; + } catch (IOException | SAXException e) { + throw new IllegalStateException(e); + } + + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java new file mode 100644 index 0000000000..c402418e6b --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java @@ -0,0 +1,22 @@ +package com.coderising.litestruts.parser; + +public class Result { + private String name; + private String view; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getView() { + return view; + } + + public void setView(String view) { + this.view = view; + } +} \ No newline at end of file diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java new file mode 100644 index 0000000000..88f769157e --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts.parser; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StrutsConfig { + public Map actions = new HashMap<>(10); + + public Map getActions() { + return actions; + } + + public void setActions(Map actions) { + this.actions = actions; + } + + public void addAction(ActionConfig action) { + this.actions.put(action.getName(), action); + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java new file mode 100644 index 0000000000..ab7358c777 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java @@ -0,0 +1,8 @@ +package com.coderising.litestruts.parser; + +/** + * Created by luoziyihao on 3/5/17. + */ +public interface StrutsParser { + StrutsConfig parser(String filePathInClasspath); +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/BeanUtils.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/BeanUtils.java new file mode 100755 index 0000000000..775b005466 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/BeanUtils.java @@ -0,0 +1,144 @@ +package com.coderising.litestruts.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by luoziyihao on 5/25/16. + */ +public class BeanUtils { + + + public static final String SET = "set"; + public static final String GET = "get"; + // 日志输出类 + private final Logger log = LoggerFactory.getLogger(this.getClass()); + private final StringUtils stringUtils = new StringUtils(); + + public Object setInvoke(Object para, String methodName, Object obj) { + Method method = null; + Object returnObj = null; + try { + + method = obj.getClass().getMethod(methodName, para.getClass()); + returnObj = method.invoke(obj, para); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + public Object getInvoke(String methodName, Object obj) { + Method method = null; + Object returnObj = null; + try { + method = obj.getClass().getMethod(methodName); + returnObj = method.invoke(obj); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + public Object invokeWithNoParamter(String methodName, Object obj) { + Method method; + Object returnObj = null; + try { + method = obj.getClass().getMethod(methodName); + returnObj = method.invoke(obj); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + + + public Object getPara(String paraName, Object object) { + if (stringUtils.isSpaceOrNull(paraName)) { + throw new RuntimeException("paraname is null or space"); + } + String methodName = new StringBuilder().append(GET).append(stringUtils.toUpperCase(paraName, 0)).toString(); + return getInvoke(methodName, object); + } + + + + public Object setPara(Object para, String paraName, Object object) { + if (stringUtils.isSpaceOrNull(paraName)) { + throw new RuntimeException("paraname is null or space"); + } + String methodName = new StringBuilder().append(SET).append(stringUtils.toUpperCase(paraName, 0)).toString(); + return setInvoke(para, methodName, object); + } + + public T get(String paraName, Object object, Class clazz) { + return (T) getPara(paraName, object); + } + + public Integer getInt(String paraName, Object object) { + return (Integer) getPara(paraName, object); + } + + public Long getLong(String paraName, Object object) { + return (Long) getPara(paraName, object); + } + + public Double getDouble(String paraName, Object object) { + return (Double) getPara(paraName, object); + } + + public BigDecimal getBigDecimal(String paraName, Object object) { + return (BigDecimal) getPara(paraName, object); + } + + public String getString(String paraName, Object object) { + return getPara(paraName, object).toString(); + } + + public Date getDate(String paraName, Object object) { + return (Date) getPara(paraName, object); + } + + public Long getLongByString(String paraName, Object object) { + return Long.parseLong(getString(paraName, object)); + } + + public Integer getIntByString(String paraName, Object object) { + return Integer.parseInt(getString(paraName, object)); + } + + public Double getDoubleByString(String paraName, Object object) { + return Double.parseDouble(getString(paraName, object)); + } + + public BigDecimal getBigDecimalByString(String paraName, Object object) { + return new BigDecimal(getString(paraName, object)); + } + + private final static String GETTER_PRE = "get"; + public Map describe(Object model) { + Method[] methods = model.getClass().getDeclaredMethods(); //获取实体类的所有属性,返回Field数组 + Map properties = new HashMap<>(); + for (Method method : methods) { + String methodName = method.getName(); + if (methodName.startsWith(GETTER_PRE)) { + try { + Object o = method.invoke(model); + String valueName = stringUtils.toLowwerCase(methodName.substring(GETTER_PRE.length()), 0); + properties.put(valueName, o); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new IllegalStateException(e); + } + } + } + return properties; + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/StringUtils.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/StringUtils.java new file mode 100644 index 0000000000..7fe2ed5ac4 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/StringUtils.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts.util; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StringUtils { + + /** + * 改变指定位置的 char的大小写 + */ + public String toUpperCase(String str, int index) { + char[] chars = str.toCharArray(); + if (index + 1 > chars.length) { + throw new RuntimeException("the char at the index don't exist"); + } + chars[index] = Character.toUpperCase(chars[index]); + return new String(chars); + } + + /** + * 改变指定位置的 char的大小写 + */ + public String toLowwerCase(String str, int index) { + char[] chars = str.toCharArray(); + if (index + 1 > chars.length ) {throw new RuntimeException("the char at the index don't exist");} + chars[index] = Character.toLowerCase(chars[index]); + return new String(chars); + } + + public boolean isSpaceOrNull(String paraName) { + return (paraName == null || paraName.trim().isEmpty()); + } + +} diff --git a/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml b/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java new file mode 100644 index 0000000000..0ea94e4183 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java @@ -0,0 +1,19 @@ +package com.coderising.api; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ComputeTest { + + @Test + public void testDivisionExactly(){ + System.out.println( 7 >> 1); + } + + @Test + public void testSqrt() { + System.out.println(Math.sqrt(10)); + } +} diff --git a/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java new file mode 100644 index 0000000000..6abb5d925d --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java @@ -0,0 +1,25 @@ +package com.coderising.api; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class CycleTest { + + /** + * checkIndex will be excuted in each cycle + */ + @Test + public void testForSize() { + int[] arr = new int[]{1, 2, 3, 4, 54}; + for (int i = 0; checkIndex(i, arr); i++ ) { + + } + } + + private boolean checkIndex(int i, int[] arr) { + System.out.println(i); + return i < arr.length; + } +} diff --git a/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/array/ArrayUtilTest.java b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..be29b01d42 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package com.coderising.array; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ArrayUtilTest { + + private ArrayUtil creatArrayUtil(){ + return new ArrayUtil(); + } + + @Test + public void reverseArray() throws Exception { + int[] origin = new int[]{1, 2, 3}; + int[] destArray = new int[]{3, 2, 1}; + creatArrayUtil().reverseArray(origin); + Assert.assertArrayEquals(destArray, origin); + } + + @Test + public void removeZero() throws Exception { + int[] origin = new int[]{1, 2, 3, 0, 10}; + int[] destArray = new int[]{1, 2, 3, 10}; + int[] retArray = creatArrayUtil().removeZero(origin); + Assert.assertArrayEquals(destArray, retArray); + } + + @Test + public void merge() throws Exception { + int[] a = new int[]{1, 2, 3}; + int[] b = new int[]{2, 3}; + + int[] newArray = creatArrayUtil().merge(a, b); + info(newArray); + assertArrayEquals(new int[]{1, 2, 3}, newArray); + } + + @Test + public void grow() throws Exception { + assertArrayEquals(new int[]{1, 2, 0, 0}, creatArrayUtil().grow(new int[]{1, 2}, 2)); + } + + @Test + public void fibonacci() throws Exception { + assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8}, creatArrayUtil().fibonacci(10)); + } + + @Test + public void getPrimes() throws Exception { + int max = Double.valueOf(Math.pow(2, 4)).intValue(); + assertArrayEquals(new int[]{2, 3, 5, 7, 11, 13}, creatArrayUtil().getPrimes(max)); + } + + @Test + public void getPerfectNumbers() throws Exception { + int max = Double.valueOf(Math.pow(2, 16)).intValue(); + assertArrayEquals(new int[]{6, 28, 496, 8128}, creatArrayUtil().getPerfectNumbers(max)); + + } + + @Test + public void join() throws Exception { + assertEquals("1_2_3_10", creatArrayUtil().join(new int[]{1, 2, 3, 10}, "_")); + } + + private void info(int[] array) { + System.out.println(Arrays.toString(array)); + } +} \ No newline at end of file diff --git a/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java new file mode 100644 index 0000000000..72e841a230 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java @@ -0,0 +1,14 @@ +package com.coderising.litestruts.parser; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StructsParserTest { + @Test + public void parser() throws Exception { + new DefaultStrutsParser().parser("struts.xml"); + } + +} \ No newline at end of file diff --git a/group17/1204187480/code/homework/pom.xml b/group17/1204187480/code/homework/pom.xml index df0626e528..c8b94c2bc2 100644 --- a/group17/1204187480/code/homework/pom.xml +++ b/group17/1204187480/code/homework/pom.xml @@ -9,6 +9,7 @@ parent basic + coderising diff --git "a/group17/1204187480/note/homework/cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244-\346\274\253\350\260\210\350\256\241\347\256\227\346\234\272" "b/group17/1204187480/note/homework/cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244-\346\274\253\350\260\210\350\256\241\347\256\227\346\234\272" new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group17/1264835468/src/assignment/List.java b/group17/1264835468/src/assignment/List.java index ba31dc333b..5ab03b0fdf 100644 --- a/group17/1264835468/src/assignment/List.java +++ b/group17/1264835468/src/assignment/List.java @@ -1,14 +1,14 @@ -package assignment; - -// -public interface List { - public void add(E o); - - public void add(int index, E o); - - public E get(int index); - - public E remove(int index); - - public int size(); -} +package assignment; + +// +public interface List { + public void add(E o); + + public void add(int index, E o); + + public E get(int index); + + public E remove(int index); + + public int size(); +} diff --git a/group17/1264835468/src/assignment2_26/ArrayUtil.java b/group17/1264835468/src/assignment2_26/ArrayUtil.java new file mode 100644 index 0000000000..89d2a0db29 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/ArrayUtil.java @@ -0,0 +1,183 @@ +package assignment2_26; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.TreeSet; + +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 mid = origin.length / 2; + for (int i = 0; i < mid; i++) { + int temp = origin[i]; + int reversePosition = origin.length - 1; + origin[i] = origin[reversePosition]; + origin[reversePosition] = 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) { + int count = 0; + for (int i : oldArray) { + if (i != 0) + count++; + } + int[] newArray = new int[count]; + int currentPos = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) + newArray[currentPos++] = oldArray[i]; + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2) { + TreeSet set = new TreeSet<>(); + for (Integer integer : array1) { + set.add(integer); + } + for (Integer integer : array2) { + set.add(integer); + } + int[] result = new int[set.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = set.pollFirst(); + } + 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) { + return Arrays.copyOf(oldArray, size); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) + return new int[0]; + List fList = new ArrayList<>(); + fList.add(1); + fList.add(1); + int last = fList.size() - 1; + while (fList.get(last) < max) { + fList.add(fList.get(last) + fList.get(last - 1)); + last++; + } + int[] result = new int[fList.size() - 1]; + for (int i = 0; i < result.length; i++) { + result[i] = fList.get(i); + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + boolean[] isPrime = new boolean[max]; + List primes = new ArrayList<>(); + for (int i = 0; i < isPrime.length; i++) { + isPrime[i] = true; + } + for (int i = 2; i * i < max; i++) { + for (int j = i; i * j < max; j++) + isPrime[i * j] = false; + } + for (int i = 2; i < isPrime.length; i++) { + if (isPrime[i]) + primes.add(i); + } + int[] result = new int[primes.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = primes.get(i); + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int sum = 0; + ArrayList perfectNumbers = new ArrayList<>(); + for (int i = 1; i < max; i++) { + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) + perfectNumbers.add(i); + sum = 0; + } + + int[] result = new int[perfectNumbers.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = perfectNumbers.get(i); + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i : array) { + stringBuilder.append(i + seperator); + } + stringBuilder.delete(stringBuilder.length() - seperator.length(), stringBuilder.length()); + + return stringBuilder.toString(); + } + +} diff --git a/group17/1264835468/src/assignment2_26/ArrayUtilTest.java b/group17/1264835468/src/assignment2_26/ArrayUtilTest.java new file mode 100644 index 0000000000..753c026796 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/ArrayUtilTest.java @@ -0,0 +1,100 @@ +package assignment2_26; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] array = new int[] {}; + ArrayUtil.reverseArray(array); + assertArrayEquals(new int[] {}, array); + + array = new int[] { 1 }; + ArrayUtil.reverseArray(array); + assertArrayEquals(new int[] { 1 }, array); + + array = new int[] { 1, 2, 3 }; + ArrayUtil.reverseArray(array); + assertArrayEquals(new int[] { 3, 2, 1 }, array); + } + + @Test + public void testRemoveZero() { + int[] array = new int[] {}; + assertArrayEquals(new int[] {}, ArrayUtil.removeZero(array)); + + array = new int[] { 0 }; + assertArrayEquals(new int[] {}, ArrayUtil.removeZero(array)); + + array = new int[] { 1 }; + assertArrayEquals(new int[] { 1 }, ArrayUtil.removeZero(array)); + + array = new int[] { 1, 2, 0, 0, 3 }; + assertArrayEquals(new int[] { 1, 2, 3 }, ArrayUtil.removeZero(array)); + + array = new int[] { 1, 2, 3 }; + assertArrayEquals(new int[] { 1, 2, 3 }, ArrayUtil.removeZero(array)); + } + + @Test + public void testMerge() { + int[] array1 = { 3, 5, 7, 8 }; + int[] array2 = { 4, 5, 6, 7 }; + assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8 }, ArrayUtil.merge(array1, array2)); + } + + @Test + public void testGrow() { + int[] array = { 3, 5, 7 }; + assertArrayEquals(new int[] { 3, 5, 7, 0, 0 }, ArrayUtil.grow(array, 5)); + assertArrayEquals(new int[] { 3, 5, 7 }, ArrayUtil.grow(array, 3)); + } + + @Test + public void testFibonacci() { + assertArrayEquals(new int[] {}, ArrayUtil.fibonacci(1)); + + assertArrayEquals(new int[] { 1, 1 }, ArrayUtil.fibonacci(2)); + + assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, ArrayUtil.fibonacci(15)); + } + + @Test + public void testGetPrimes() { + assertArrayEquals(new int[] {}, ArrayUtil.getPrimes(1)); + + assertArrayEquals(new int[] {}, ArrayUtil.getPrimes(2)); + + assertArrayEquals(new int[] { 2 }, ArrayUtil.getPrimes(3)); + + assertArrayEquals(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }, ArrayUtil.getPrimes(20)); + } + + @Test + public void testGetPerfectNumbers() { + assertArrayEquals(new int[] { 6 }, ArrayUtil.getPerfectNumbers(10)); + + assertArrayEquals(new int[] { 6, 28 }, ArrayUtil.getPerfectNumbers(100)); + + assertArrayEquals(new int[] { 6, 28, 496 }, ArrayUtil.getPerfectNumbers(1000)); + + assertArrayEquals(new int[] { 6, 28, 496, 8128 }, ArrayUtil.getPerfectNumbers(10000)); + + } + + @Test + public void testJoin() { + assertEquals("3-4-5", ArrayUtil.join(new int[] { 3, 4, 5 }, "-")); + + assertEquals("345", ArrayUtil.join(new int[] { 3, 4, 5 }, "")); + + assertEquals("3", ArrayUtil.join(new int[] { 3 }, "")); + + assertEquals("3--4--5", ArrayUtil.join(new int[] { 3, 4, 5 }, "--")); + } + +} diff --git a/group17/1264835468/src/assignment2_26/LoginAction.java b/group17/1264835468/src/assignment2_26/LoginAction.java new file mode 100644 index 0000000000..3101d322b8 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/LoginAction.java @@ -0,0 +1,42 @@ +package assignment2_26; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @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/group17/1264835468/src/assignment2_26/Struts.java b/group17/1264835468/src/assignment2_26/Struts.java new file mode 100644 index 0000000000..ad704e0433 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/Struts.java @@ -0,0 +1,99 @@ +package assignment2_26; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Struts { + + private static File configFile = new File("./src/struts.xml");; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + // 0 + XmlParser phraser = new XmlParser(configFile); + String className = phraser.getClassNameByActionName(actionName); + View view = new View(); + try { + // 1 + Class actionClass = Class.forName(className); + Constructor constructor = actionClass.getConstructor(); + Object instance = constructor.newInstance(); + invokeSetters(actionClass, instance, parameters); + + // 2 + String result = invokeExecute(actionClass, instance); + + // 3 + Map getterResult = invokeGetters(actionClass, instance); + view.setParameters(getterResult); + + // 4 + String resultJsp = phraser.getResultJsp(actionName, result); + view.setJsp(resultJsp); + + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException + | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + + return view; + } + + private static Map invokeGetters(Class actionClass, Object instance) + throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Map result = new HashMap<>(); + for (Method method : actionClass.getDeclaredMethods()) { + if (method.getName().matches("get.*")) { + String fieldName = method.getName().substring(3).toLowerCase(); + String value = (String) (method.invoke(instance)); + result.put(fieldName, value); + } + } + return result; + } + + private static String invokeExecute(Class actionClass, Object instance) throws NoSuchMethodException, + SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method method = actionClass.getDeclaredMethod("execute"); + return (String) method.invoke(instance); + } + + private static void invokeSetters(Class actionClass, Object instance, Map parameters) + throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, + InvocationTargetException { + for (String fieldName : parameters.keySet()) { + invokeSetter(actionClass, instance, fieldName, parameters.get(fieldName)); + } + } + + private static void invokeSetter(Class actionClass, Object instance, String fieldName, String value) + throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, + InvocationTargetException { + String setterName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); + Method setter = actionClass.getDeclaredMethod(setterName, String.class); + setter.invoke(instance, value); + } +} diff --git a/group17/1264835468/src/assignment2_26/StrutsTest.java b/group17/1264835468/src/assignment2_26/StrutsTest.java new file mode 100644 index 0000000000..f47410209b --- /dev/null +++ b/group17/1264835468/src/assignment2_26/StrutsTest.java @@ -0,0 +1,38 @@ +package assignment2_26; + +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/group17/1264835468/src/assignment2_26/View.java b/group17/1264835468/src/assignment2_26/View.java new file mode 100644 index 0000000000..e96197fad6 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/View.java @@ -0,0 +1,26 @@ +package assignment2_26; + +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/group17/1264835468/src/assignment2_26/XmlParser.java b/group17/1264835468/src/assignment2_26/XmlParser.java new file mode 100644 index 0000000000..aa34335cb6 --- /dev/null +++ b/group17/1264835468/src/assignment2_26/XmlParser.java @@ -0,0 +1,72 @@ +package assignment2_26; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class XmlParser { + private File file; + List actionElements; + + public XmlParser(File file) { + this.file = file; + actionElements = new ArrayList<>(); + getActionsFromFile(); + } + + public String getClassNameByActionName(String actionName) { + Element action = getElementByActionName(actionName); + return action.getAttribute("class"); + } + + public String getResultJsp(String actionName, String resultName) { + Element action = getElementByActionName(actionName); + NodeList results = action.getChildNodes(); + for (int i = 0; i < results.getLength(); i++) { + Node child = results.item(i); + if (child instanceof Element) { + Element result = (Element) child; + if (result.getAttribute("name").equals(resultName)) + return result.getTextContent(); + } + } + throw new RuntimeException("not found result named:" + resultName); + } + + private Element getElementByActionName(String actionName) { + for (Element element : actionElements) { + if (element.getAttribute("name").equals(actionName)) { + return element; + } + } + throw new RuntimeException("no such element named " + actionName); + } + + private void getActionsFromFile() { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(file); + Element root = document.getDocumentElement(); + NodeList children = root.getChildNodes(); + for (int i = 0; i < children.getLength(); i++) { + Node child = children.item(i); + if (child instanceof Element) + actionElements.add((Element) child); + } + } catch (ParserConfigurationException | SAXException | IOException e) { + e.printStackTrace(); + } + } +} diff --git a/group17/1264835468/src/struts.xml b/group17/1264835468/src/struts.xml new file mode 100644 index 0000000000..ad43b47967 --- /dev/null +++ b/group17/1264835468/src/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group17/1282579502/.settings/org.eclipse.jdt.core.prefs b/group17/1282579502/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..a698e59674 --- /dev/null +++ b/group17/1282579502/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +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/group17/1282579502/src/com/coderising/array/ArrayUtil.java b/group17/1282579502/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..af9bde1136 --- /dev/null +++ b/group17/1282579502/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if(origin == null) return; + int tmp; + for(int i = 0; i< origin.length; i++){ + int end = origin.length - 1 -i; + if(end>i){ + tmp = origin[i]; + origin[i] = origin[end]; + origin[end] = 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 int[] removeZero(int[] oldArray){ + LinkedList newArray = new LinkedList<>(); + + for(int i = 0; i< oldArray.length; i++){ + if(oldArray[i] != 0){ + newArray.add(oldArray[i]); + } + } + int[] ret = new int[newArray.size()]; + for(int i = 0; i= array2[j]){ + merged[index] = array2[j]; + j++;} +// }else{ +// merged[index] = array1[i]; +// i++; +// j++; +// } + + index++; + } + //o(n) + while(i store = new LinkedList<>(); + for(int candidate = 2; candidate< max; candidate++ ){ + int sum = 0; + for(int i = 1; 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字段中。 + + */ + String path = "src/com/coderising/litestruts/struts.xml"; + File rootFile = new File(""); + File rootDir = new File(rootFile.getAbsolutePath()); + File configFile = findFile(rootDir, "struts.xml"); + if(configFile != null){ + //System.out.println("find: " + configFile.getAbsolutePath()); + path = configFile.getAbsolutePath(); + } + View retView = new View(); + XmlParser parser = new XmlParser(path); + //parser.dump(); + List branchNodesList = parser.getActionNodeList(); + try { + for(eNode node : branchNodesList){ + //System.out.println(node); + String classPath = node.attributeMap.get("class"); + Class actionObjectClass = getClassObj(classPath); + if(actionObjectClass != null){ + Constructor actionConstructorObject = createClass(actionObjectClass); + Object newInstance = null; + newInstance = actionConstructorObject.newInstance(); + + Method[] methods = actionObjectClass.getDeclaredMethods(); +// for(Method m : methods){ +// System.out.println("method: " + m.getName()); +// } + Iterator> itr = parameters.entrySet().iterator(); + while(itr.hasNext()){ + Entry call = itr.next(); + //System.out.println(call.getKey() + " " + call.getValue()); + String key = call.getKey(); + String val = call.getValue(); + String setMethodName = "set" + Character.toUpperCase(key.charAt(0)) + key.substring(1); + Method setMethod = getMethod(actionObjectClass, setMethodName, String.class); + if(newInstance != null){ + setMethod.invoke(newInstance, val); + } + } + Method exeMethod = actionObjectClass.getDeclaredMethod("execute", null); + Class returnType = exeMethod.getReturnType(); + Object returnValue = exeMethod.invoke(newInstance, null); + HashMap viewHashParam = new HashMap<>(); + for(Method m:methods){ + if(m.getName().startsWith("get")){ + Object v = m.invoke(newInstance, null); + String getMethodName = m.getName().substring(3); + getMethodName = Character.toLowerCase(getMethodName.charAt(0)) + getMethodName.substring(1); + //System.out.println("get method: " + getMethodName); + viewHashParam.put(getMethodName, v); + } + } + String viewStr = null; + for(eNode resultNode : node.subNodes){ + String nameAttr = resultNode.attributeMap.get("name") ; + if(nameAttr.equals(returnValue)){ + //System.out.println("node: " + resultNode.element_name + " " + resultNode.attributeMap.get("name") + " matches"); + for(eNode jspNode : resultNode.subNodes){ + if(jspNode.isLeaf){ + viewStr = jspNode.element_raw_content; + //System.out.println("jsp str: " + viewStr); + break; + } + } + } + } + + retView.setJsp(viewStr); + retView.setParameters(viewHashParam); + + } + } + } catch (IllegalAccessException | IllegalArgumentException + | InvocationTargetException | InstantiationException | NoSuchMethodException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return retView; + } + + public static Class getClassObj(String classPath){ + Class classObject = null; + try { + classObject = Class.forName(classPath); + } catch (ClassNotFoundException e) { + //System.err.println(e.getMessage()); + } + return classObject; + } + + public static Constructor createClass(Class classObject){ + Constructor constructor = null; + try { + constructor = classObject.getConstructor(); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + return constructor; + } + + public static Method getMethod(Class clazz, String methodName, Class paramClass){ + Method ret = null; + try { + ret = clazz.getMethod(methodName, paramClass); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + return ret; + } + + public static void main(String[] args){ + + + //System.out.println("absolute path: " + absPath); + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + String actionName = "login"; + + View view = Struts.runAction(actionName,params); + } + + public static File findFile(File targetDir, String targetFileName){ + //System.out.println("targetDir: " + targetDir.getAbsolutePath() + " " + targetDir.isDirectory()); + File configFile = null; + File[] files = targetDir.listFiles(); + + if(files != null){ + for(File f: files){ + //System.out.println("test: " + f.getName()); + if(f.isDirectory()){ + //System.out.println("Dir: " + f.getName()); + configFile = findFile(f, targetFileName); + if(configFile != null){ + break; + } + } + else{ + //System.out.println("File: " + f.getName()); + if(f.getAbsolutePath().endsWith(targetFileName)){ + configFile = f; + break; + } + } + } + } + return configFile; + } + + public static FilenameFilter createFilter(String targetName){ + return new FilenameFilter(){ + + @Override + public boolean accept(File dir, String name) { + // TODO Auto-generated method stub + System.out.println("testing: " + dir + " name: " + name); + return name.endsWith(targetName); + } + + }; + } + + public static List getDirectories(File targetFile){ + List childDirs = new LinkedList<>(); + if(targetFile.isDirectory()){ + File[] files = targetFile.listFiles(); + for(File f: files){ + if(f.isDirectory()) childDirs.add(f); + } + } + + return childDirs; + } + +} diff --git a/group17/1282579502/src/com/coderising/litestruts/StrutsTest.java b/group17/1282579502/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group17/1282579502/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/group17/1282579502/src/com/coderising/litestruts/View.java b/group17/1282579502/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group17/1282579502/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/group17/1282579502/src/com/coderising/litestruts/struts.xml b/group17/1282579502/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group17/1282579502/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/group17/1282579502/src/com/coderising/parser/TestDriver.java b/group17/1282579502/src/com/coderising/parser/TestDriver.java new file mode 100644 index 0000000000..a4decf8986 --- /dev/null +++ b/group17/1282579502/src/com/coderising/parser/TestDriver.java @@ -0,0 +1,22 @@ +package com.coderising.parser; + +import com.coderising.parser.XmlParser.eNode; + +public class TestDriver { + + public static void main(String[] args) { + XmlParser testParser = new XmlParser("src/xml/input.xml"); + String input = ""; + System.out.println(); + //eNode e = testParser.parseRawData(input); + //XmlParser.dumpNodePreOrder(e); +// try{ +// testParser.loadFile(testParser.getFile()); +// }catch(Exception e){ +// e.printStackTrace(); +// } + } + + + +} diff --git a/group17/1282579502/src/com/coderising/parser/XmlParser.java b/group17/1282579502/src/com/coderising/parser/XmlParser.java new file mode 100644 index 0000000000..f78be14555 --- /dev/null +++ b/group17/1282579502/src/com/coderising/parser/XmlParser.java @@ -0,0 +1,277 @@ +package com.coderising.parser; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class XmlParser { + + private File targetFile = null; + private eNode root = null; + + public XmlParser(String path){ + setTargetFile(path); + try { + String content = loadFile(targetFile); + parseString(content); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setTargetFile(String path){ + targetFile = new File(path); + if(! targetFile.isFile() || ! targetFile.exists()){ + throw new IllegalArgumentException("Path: " + path + " is NOT valid."); + } + } + + public File getFile(){ + return targetFile; + } + + protected String loadFile(File f) throws FileNotFoundException{ + StringBuilder sb = new StringBuilder(); + FileReader fr = new FileReader(f); + BufferedReader br = new BufferedReader(fr); + String line = null; + try{ + while((line = br.readLine()) != null){ + line = line.trim(); + if(!line.startsWith("#") && !line.startsWith(" cache = new LinkedList<>(); + int bracketStart=0; + int contentStart=0; + for(int i=0; i< str.length(); i++){ + if(str.charAt(i) == '<'){ + bracketStart = i; + if(i>contentStart){ + String tagVal = str.substring(contentStart, i).trim(); + if(tagVal.length() > 0){ + //System.out.println("content: " + tagVal); + cache.add(tagVal); + } + } + } + else if(str.charAt(i) == '>'){ + String elementTag = str.substring(bracketStart, i+1); + //System.out.println("pushing: " + elementTag); + cache.add(elementTag); + contentStart=i+1; + + } + } + + //debug information + //dumpStack(cache); + + root = processElementStack(cache); + //System.out.println(); + //System.out.println("Dump node tree"); + //dumpNodePreOrder(root); + return root; + } + + private eNode processElementStack(Queue stk){ + + if(!stk.isEmpty()){ + String token = stk.remove(); + if(token.startsWith("'){ + String elementName = str.substring(i+1, j); + branchNode.element_name = elementName.trim(); + //System.out.println("element name: " + elementName); + i = j; + break; + } + } + } + else if(Character.isAlphabetic(c) || c == '"'){ + + if(!foundAttributeName){ + for(int j=i; j'){ + String attr_val = str.substring(i, k).trim(); + if(attr_name != null && attr_val != null){ + //System.out.println("Element: " + branchNode.element_name + " put in: " + attr_name + " " + attr_val); + branchNode.attributeMap.put(attr_name, attr_val.replaceAll("\"", "")); + } + foundAttributeName = false; + i = k; + break; + } + } + } + + } + } + //System.out.println(); + return branchNode; + } + + public List getActionNodeList(){ + return root.subNodes; + } + + private void dumpNodeBFS(eNode root){ + Stack stk = new Stack(); + stk.push(root); + while(!stk.isEmpty()){ + eNode cur = stk.pop(); + dump(cur, stk); + } + + } + + public static void dumpNodePreOrder(eNode root){ + if(root.isLeaf){ + System.out.println("Leaf: "+root.element_raw_content); + } + else{ + System.out.println("Branch: " + root.element_name); + Iterator itr = root.attributeMap.keySet().iterator(); + while(itr.hasNext()){ + String key = itr.next(); + System.out.println("["+ key + "]=[" + root.attributeMap.get(key)+"]"); + } + } + for(eNode i : root.subNodes){ + dumpNodePreOrder(i); + } + } + + private void dump(eNode node, Stack stk){ + System.out.println(node.element_raw_content); + for(int i = 0 ; i< node.subNodes.size(); i++){ + stk.push(node.subNodes.get(i)); + } + } + + private void dumpStack(Collection cache){ + Iterator itr = cache.iterator(); + while(itr.hasNext()){ + String item = itr.next(); + System.out.println(""+item+""); + } + } + + public void dump(){ + dumpNodePreOrder(root); + } + +// private String parseOpenElement(String str){ +// +// } +// +// private String parseCloseElment(String str){ +// +// } + + + + public class eNode{ + public boolean isLeaf = false; + public String element_raw_content = null; + public HashMap attributeMap = null; + public String element_name = null; + public List subNodes = null; + + public eNode(){ + _init(); + } + + private void _init(){ + attributeMap = new HashMap<>(); + subNodes = new LinkedList<>(); + } + + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append("element_name: ").append(element_name).append("\n"); + sb.append("isLeaf: ").append(isLeaf).append("\n"); + sb.append("name attr: ").append(attributeMap.get("name")).append("\n"); + if(attributeMap.get("class") != null) + sb.append("class attr: ").append(attributeMap.get("class")).append("\n"); + return sb.toString(); + } + } + +// protected class eGraph{ +// public eNode root = null; +// +// public eGraph(eNode root){ +// this.root = root; +// } +// } + +} diff --git a/group17/240094626/warm-up/src/com/array/ArrayUtil.java b/group17/240094626/warm-up/src/com/array/ArrayUtil.java new file mode 100644 index 0000000000..214ecae3bb --- /dev/null +++ b/group17/240094626/warm-up/src/com/array/ArrayUtil.java @@ -0,0 +1,243 @@ +package com.coderising.array; + +import com.coding.basic.impl.ArrayList; +import com.coding.basic.impl.LinkedList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for(int i=0; i < origin.length; i++){ + if(i >= origin.length-1-i){ + break; + } + int tmp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1] = tmp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int size = 0, // 记录非0数量 + tmp = 0; // 需要比较的下一个位置 + for(int i=0; i < oldArray.length;){ + if(oldArray[i] == 0){ + int j=tmp==0?(i+1):tmp; + for(;j < oldArray.length; j++){ + if(oldArray[j] == 0){ + continue; + }else{ + oldArray[i] = oldArray[j]; + oldArray[j] = 0; + size++; + tmp = j+1; + i=j; + break; + } + } + }else{ + i++; + size++; + continue; + } + } + int a[] = new int[size]; + System.arraycopy(oldArray, 0, a, 0, size); + return a; + } + + /** + * 给定两个已经排序好的整形数组, 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[] a3 = new int[array1.length+array2.length]; + int i = array1.length-1, + j = array2.length-1, + k = a3.length-1; + while(i >= 0 && j >= 0){ + if(array1[i] > array2[j]){ + a3[k--] = array1[i--]; + }else{ + a3[k--] = array2[j--]; + } + } + while(j >= 0){ + a3[k--] = array2[j--]; + } + while(i >= 0){ + a3[k--] = array1[i--]; + } + + return a3; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int a[] = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, a, 0, oldArray.length); + return a; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max == 1){ + int[] a = {0}; + return a; + } + if(max == 2){ + int[] a = {0,1}; + return a; + } + if(max == 3){ + int[] a = {0,1,1,2}; + return a; + } + ArrayList list = new ArrayList(); + list.add(0); + list.add(1); + list.add(1); + list.add(2); + int size = 4; + for(int i = 3; i < max ; i++){ + if(i == ((int)list.get(size-1)+(int)list.get(size-2))){ + list.add(i); + size++; + } + } + int[] a = new int[size]; + for(int i = 0; i < size; i++){ + a[i] = (int) list.get(i); + } + return a; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 2){ + int[] a ={}; + return a; + } + if(max == 2){ + int[] a ={2}; + return a; + } + LinkedList list = new LinkedList(); + list.add(2); + for(int n = 3; n < max; n=n+2){ + int i=3; + boolean flag = true; + for(;i*i <= n;i=i+2){ + // 先排除偶数 + if(n%i == 0){ + flag = false; + break; + } + } + if(flag){ + list.add(n); + } + } + int[] a = new int[list.size()]; + for(int i = 0; i < list.size(); i++){ + a[i] = (int) list.get(i); + } + return a; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 6){ + int a[] = {}; + return a; + } + LinkedList list = new LinkedList(); + int p = 6,sum; + for(; p < max; p++){ + sum = 0; + for(int i=1; i < p % 2; i++){ + if(p % i == 0){ + sum += i; + } + } + if(sum == p){ + list.add(p); + } + } + int a[] = new int[list.size()]; + for(int i = 0; i < list.size(); i++){ + a[i] = (int) list.get(i); + } + return a; + + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + for(int i=0;i 0){ + s.append("-"); + } + s.append(a); + } + seperator = s.toString();*/ + return seperator; + } + +} diff --git a/group17/240094626/warm-up/src/com/coderising/litestruts/Struts.java b/group17/240094626/warm-up/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..d3dd810e13 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,165 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; + +import com.coderising.litestruts.exception.StrutsException; +import com.coderising.litestruts.util.FileUtils; +import com.coding.basic.impl.ArrayList; +import com.sun.org.apache.xml.internal.security.encryption.XMLEncryptionException; + + + +public class Struts { + + public static final String DETAULT_XML = "struts.xml"; + public static final String ACTION_TAGNAME = "action"; + public static final String RESULT_TAGNAME = "result"; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + + + /** + * 获取struts中配置的所有action + * 通过dom4j实现读xml文件 + * Struts action元素的是2层结构,故,这里只解析2层 + * @param path + * @return + * @return:ArrayList + */ + private static ArrayList getActionBeans(String dir,String fileName){ + if(!FileUtils.createDir(dir)){ + throw new IllegalArgumentException("文件路径不存在:"+dir); + } + File f = new File(dir); + if(null == fileName || "".endsWith(fileName)){ + fileName = DETAULT_XML; + } + SAXReader saxReader = new SAXReader(); + try { + f = new File(f.getPath()+"\\"+fileName); + Document document = saxReader.read(f); + Element element = document.getRootElement(); + ArrayList actions = new ArrayList(); + Iterator iterator = element.elementIterator(ACTION_TAGNAME); + // 遍历每一个action + while(iterator.hasNext()){ + Element actionEle = (Element) iterator.next(); + ActionBean bean = new ActionBean(); + bean.setName(actionEle.attributeValue("name")); + bean.setClazz(actionEle.attributeValue("class")); + Iterator it = actionEle.elementIterator(RESULT_TAGNAME); + ArrayList results = new ArrayList(); + // 遍历每一个result + while(it.hasNext()){ + Element resultEle = (Element) it.next(); + Result result = new Result(); + result.setName(resultEle.attributeValue("name")); + result.setValue(resultEle.getText()); + results.add(result); + System.out.println(result); + } + bean.setResults(results); + System.out.println(bean); + actions.add(bean); + } + System.out.println(); + } catch (DocumentException e) { + e.printStackTrace(); + throw new StrutsException("xml文件解析失败:,dir="+dir+",fileName="+fileName,e); + } + + return null; + } + + + + private static class ActionBean{ + String name; + String clazz; + ArrayList results; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClazz() { + return clazz; + } + public void setClazz(String clazz) { + this.clazz = clazz; + } + public ArrayList getResults() { + return results; + } + public void setResults(ArrayList results) { + this.results = results; + } + @Override + public String toString() { + return "ActionBean [name=" + name + ", clazz=" + clazz + + ", results=" + results + "]"; + } + + + } + + private static class Result{ + String name; + String value; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + @Override + public String toString() { + return "Result [name=" + name + ", value=" + value + "]"; + } + + + } + + public static void main(String[] args) { + Struts.getActionBeans("src/com/coderising/litestruts/",""); + } +} diff --git a/group17/240094626/warm-up/src/com/coderising/litestruts/exception/StrutsException.java b/group17/240094626/warm-up/src/com/coderising/litestruts/exception/StrutsException.java new file mode 100644 index 0000000000..bd1fe8011c --- /dev/null +++ b/group17/240094626/warm-up/src/com/coderising/litestruts/exception/StrutsException.java @@ -0,0 +1,61 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsException extends RuntimeException { + + private static final long serialVersionUID = 5481955299021871754L; + + private String message; + private String stackTrace; + private Throwable t; + + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/warm-up/src/com/coderising/litestruts/struts.xml b/group17/240094626/warm-up/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..fadf460c49 --- /dev/null +++ b/group17/240094626/warm-up/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/group17/240094626/warm-up/src/com/coderising/litestruts/util/FileUtils.java b/group17/240094626/warm-up/src/com/coderising/litestruts/util/FileUtils.java new file mode 100644 index 0000000000..f369fc6cdb --- /dev/null +++ b/group17/240094626/warm-up/src/com/coderising/litestruts/util/FileUtils.java @@ -0,0 +1,25 @@ +package com.coderising.litestruts.util; + +import java.io.File; + +public class FileUtils { + + public static boolean createDir(String dir){ + File f = null; + f = new File(dir); + boolean ok = true; + if(!f.exists()){ + ok = f.mkdirs(); + } + return ok; + } + + public static String getProjectPath(){ + return System.getProperty("user.dir"); + } + + public static void main(String[] args) { + System.out.println(createDir("src/com/coderising/litestruts/")); + System.out.println(getProjectPath()); + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/LoginAction.java b/group17/240094626/warm-up/src/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group17/240094626/warm-up/src/com/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/group17/240094626/warm-up/src/com/litestruts/Struts.java b/group17/240094626/warm-up/src/com/litestruts/Struts.java new file mode 100644 index 0000000000..44e6398b80 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/Struts.java @@ -0,0 +1,194 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import com.coderising.litestruts.exception.StrutsRunActionException; +import com.coderising.litestruts.exception.StrutsXMLLoaderException; + + + +/** + * 模拟Struts读取xml文件,解析ation,并执行exectue方法 + * @author 240094626 + * + */ +public class Struts { + /**单例对象*/ + private static Struts instance = null; + /**默认配置文件目录*/ + private static final String STRUTS_CONFIG_DIR = "src/com/coderising/litestruts/"; + /**默认配置文件名*/ + private static final String STRUTS_CONFIG_XML = "struts.xml"; + /**默认编码字符集*/ + private static final String ENCODE = "UTF-8"; + /**action文档节点名称*/ + private static final String DOC_ACTION = "action"; + /**result文档节点名称*/ + private static final String DOC_ACTION_RESULT = "result"; + + /** + * 单例实现,双check + * @return + */ + public static Struts getInstance(){ + if(instance == null){ + synchronized (Struts.class) { + if(instance == null){ + instance = new Struts(); + } + } + } + return instance; + } + + public View runAction(String actionName, Map parameters) throws StrutsRunActionException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + View view = new View(); + try { + // 0. 读取配置文件struts.xml + Map actions = getActionByDom4J(); + + // 1. 找到LoginAction 反射实例化,setName 和setPassword + Action action = actions.get(actionName); + Class clz = Class.forName(action.clazz); + Object actionObj = clz.newInstance(); + Method m1 = clz.getDeclaredMethod("setName", String.class); + m1.setAccessible(true); + m1.invoke(actionObj, parameters.get("name")); + Method m2 = clz.getDeclaredMethod("setPassword", String.class); + m2.setAccessible(true); + m2.invoke(actionObj, parameters.get("password")); + + // 2.通过反射调用对象的exectue 方法 + Method mExectue = clz.getDeclaredMethod("execute", null); + String result = (String) mExectue.invoke(actionObj, null); + + // 3. 通过反射找到对象的所有getter方法,通过反射来调用, 把值和属性形成一个HashMap + Field[] fs = clz.getDeclaredFields(); + Map pMap = new HashMap(10); + for(Field f : fs){ + String methodName = "get"+toUpperCase(f.getName()); + Method m = clz.getDeclaredMethod(methodName, null); + m.setAccessible(true); + pMap.put(f.getName(), m.invoke(actionObj, null)); + } + view.setParameters(pMap); + // 4.根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + view.setJsp(action.results.get(result)); + System.out.println(view); + } catch (StrutsXMLLoaderException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new StrutsRunActionException("Action执行失败",e); + } + + + return view; + } + + + /** + * dom4j读xml + * @return + * @throws StrutsXMLLoaderException + */ + private Map getActionByDom4J() throws StrutsXMLLoaderException{ + return getActionByDom4J(STRUTS_CONFIG_DIR,STRUTS_CONFIG_XML,ENCODE); + } + /** + * dom4j读xml + * @param dir + * @param fileName + * @param encode + * @return + * @throws StrutsXMLLoaderException + */ + private Map getActionByDom4J(String dir,String fileName,String encode) throws StrutsXMLLoaderException { + File f = new File(dir); + if(!f.exists()){ + throw new StrutsXMLLoaderException("路径不存在:"+dir); + } + f = new File(f.getPath()+"\\"+fileName); + if(!f.exists()){ + throw new StrutsXMLLoaderException("路径:"+dir+"中文件不存在:"+fileName); + } + SAXReader saxReader = new SAXReader(); + saxReader.setEncoding(encode); + Map actions = new HashMap(10); + try { + Document document = saxReader.read(f); + Element root =document.getRootElement(); + Iterator it = root.elementIterator(DOC_ACTION); + while(it.hasNext()){ + Action action = new Action(); + Element element = it.next(); + action.name = element.attributeValue("name"); + action.clazz = element.attributeValue("class"); + Map results = new HashMap(10); + Iterator rIt = element.elementIterator(DOC_ACTION_RESULT); + while(rIt.hasNext()){ + Element rElement = rIt.next(); + results.put(rElement.attributeValue("name"), rElement.getText()); + } + action.results = results; + actions.put(action.name, action); + } + } catch (Exception e) { + throw new StrutsXMLLoaderException("xml文件解析失败",e); + } + + return actions; + } + + /** + * 首个字符大写 + * @param fieldName + * @return + */ + private String toUpperCase(String fieldName){ + return fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); + } + + + + /** + * Action类 + */ + private static class Action{ + String name; + String clazz; + Map results; + } + + + +} diff --git a/group17/240094626/warm-up/src/com/litestruts/StrutsTest.java b/group17/240094626/warm-up/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..09b00c8c3b --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/StrutsTest.java @@ -0,0 +1,57 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.litestruts.exception.StrutsRunActionException; + + + + + +public class StrutsTest { + Struts struts = Struts.getInstance(); + + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = struts.runAction(actionName,params); + } catch (StrutsRunActionException e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = null; + try { + view = struts.runAction(actionName,params); + } catch (StrutsRunActionException e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/View.java b/group17/240094626/warm-up/src/com/litestruts/View.java new file mode 100644 index 0000000000..880a7be260 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/View.java @@ -0,0 +1,28 @@ +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; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } + +} diff --git a/group17/240094626/warm-up/src/com/litestruts/exception/StrutsRunActionException.java b/group17/240094626/warm-up/src/com/litestruts/exception/StrutsRunActionException.java new file mode 100644 index 0000000000..5c5a7b9006 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/exception/StrutsRunActionException.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsRunActionException extends Exception { + + private static final long serialVersionUID = -242506476923032524L; + private String message; + private String stackTrace; + private Throwable t; + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsRunActionException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsRunActionException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsRunActionException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/exception/StrutsXMLLoaderException.java b/group17/240094626/warm-up/src/com/litestruts/exception/StrutsXMLLoaderException.java new file mode 100644 index 0000000000..4efb8b9152 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/exception/StrutsXMLLoaderException.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsXMLLoaderException extends Exception { + + private static final long serialVersionUID = 5481955299021871754L; + private String message; + private String stackTrace; + private Throwable t; + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsXMLLoaderException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsXMLLoaderException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsXMLLoaderException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/struts.xml b/group17/240094626/warm-up/src/com/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group17/240094626/work_0226/src/com/coderising/array/ArrayUtil.java b/group17/240094626/work_0226/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..214ecae3bb --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,243 @@ +package com.coderising.array; + +import com.coding.basic.impl.ArrayList; +import com.coding.basic.impl.LinkedList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for(int i=0; i < origin.length; i++){ + if(i >= origin.length-1-i){ + break; + } + int tmp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1] = tmp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int size = 0, // 记录非0数量 + tmp = 0; // 需要比较的下一个位置 + for(int i=0; i < oldArray.length;){ + if(oldArray[i] == 0){ + int j=tmp==0?(i+1):tmp; + for(;j < oldArray.length; j++){ + if(oldArray[j] == 0){ + continue; + }else{ + oldArray[i] = oldArray[j]; + oldArray[j] = 0; + size++; + tmp = j+1; + i=j; + break; + } + } + }else{ + i++; + size++; + continue; + } + } + int a[] = new int[size]; + System.arraycopy(oldArray, 0, a, 0, size); + return a; + } + + /** + * 给定两个已经排序好的整形数组, 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[] a3 = new int[array1.length+array2.length]; + int i = array1.length-1, + j = array2.length-1, + k = a3.length-1; + while(i >= 0 && j >= 0){ + if(array1[i] > array2[j]){ + a3[k--] = array1[i--]; + }else{ + a3[k--] = array2[j--]; + } + } + while(j >= 0){ + a3[k--] = array2[j--]; + } + while(i >= 0){ + a3[k--] = array1[i--]; + } + + return a3; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int a[] = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, a, 0, oldArray.length); + return a; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max == 1){ + int[] a = {0}; + return a; + } + if(max == 2){ + int[] a = {0,1}; + return a; + } + if(max == 3){ + int[] a = {0,1,1,2}; + return a; + } + ArrayList list = new ArrayList(); + list.add(0); + list.add(1); + list.add(1); + list.add(2); + int size = 4; + for(int i = 3; i < max ; i++){ + if(i == ((int)list.get(size-1)+(int)list.get(size-2))){ + list.add(i); + size++; + } + } + int[] a = new int[size]; + for(int i = 0; i < size; i++){ + a[i] = (int) list.get(i); + } + return a; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 2){ + int[] a ={}; + return a; + } + if(max == 2){ + int[] a ={2}; + return a; + } + LinkedList list = new LinkedList(); + list.add(2); + for(int n = 3; n < max; n=n+2){ + int i=3; + boolean flag = true; + for(;i*i <= n;i=i+2){ + // 先排除偶数 + if(n%i == 0){ + flag = false; + break; + } + } + if(flag){ + list.add(n); + } + } + int[] a = new int[list.size()]; + for(int i = 0; i < list.size(); i++){ + a[i] = (int) list.get(i); + } + return a; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 6){ + int a[] = {}; + return a; + } + LinkedList list = new LinkedList(); + int p = 6,sum; + for(; p < max; p++){ + sum = 0; + for(int i=1; i < p % 2; i++){ + if(p % i == 0){ + sum += i; + } + } + if(sum == p){ + list.add(p); + } + } + int a[] = new int[list.size()]; + for(int i = 0; i < list.size(); i++){ + a[i] = (int) list.get(i); + } + return a; + + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + for(int i=0;i 0){ + s.append("-"); + } + s.append(a); + } + seperator = s.toString();*/ + return seperator; + } + +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/LoginAction.java b/group17/240094626/work_0226/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group17/240094626/work_0226/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/group17/240094626/work_0226/src/com/coderising/litestruts/Struts.java b/group17/240094626/work_0226/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..44e6398b80 --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,194 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import com.coderising.litestruts.exception.StrutsRunActionException; +import com.coderising.litestruts.exception.StrutsXMLLoaderException; + + + +/** + * 模拟Struts读取xml文件,解析ation,并执行exectue方法 + * @author 240094626 + * + */ +public class Struts { + /**单例对象*/ + private static Struts instance = null; + /**默认配置文件目录*/ + private static final String STRUTS_CONFIG_DIR = "src/com/coderising/litestruts/"; + /**默认配置文件名*/ + private static final String STRUTS_CONFIG_XML = "struts.xml"; + /**默认编码字符集*/ + private static final String ENCODE = "UTF-8"; + /**action文档节点名称*/ + private static final String DOC_ACTION = "action"; + /**result文档节点名称*/ + private static final String DOC_ACTION_RESULT = "result"; + + /** + * 单例实现,双check + * @return + */ + public static Struts getInstance(){ + if(instance == null){ + synchronized (Struts.class) { + if(instance == null){ + instance = new Struts(); + } + } + } + return instance; + } + + public View runAction(String actionName, Map parameters) throws StrutsRunActionException { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + View view = new View(); + try { + // 0. 读取配置文件struts.xml + Map actions = getActionByDom4J(); + + // 1. 找到LoginAction 反射实例化,setName 和setPassword + Action action = actions.get(actionName); + Class clz = Class.forName(action.clazz); + Object actionObj = clz.newInstance(); + Method m1 = clz.getDeclaredMethod("setName", String.class); + m1.setAccessible(true); + m1.invoke(actionObj, parameters.get("name")); + Method m2 = clz.getDeclaredMethod("setPassword", String.class); + m2.setAccessible(true); + m2.invoke(actionObj, parameters.get("password")); + + // 2.通过反射调用对象的exectue 方法 + Method mExectue = clz.getDeclaredMethod("execute", null); + String result = (String) mExectue.invoke(actionObj, null); + + // 3. 通过反射找到对象的所有getter方法,通过反射来调用, 把值和属性形成一个HashMap + Field[] fs = clz.getDeclaredFields(); + Map pMap = new HashMap(10); + for(Field f : fs){ + String methodName = "get"+toUpperCase(f.getName()); + Method m = clz.getDeclaredMethod(methodName, null); + m.setAccessible(true); + pMap.put(f.getName(), m.invoke(actionObj, null)); + } + view.setParameters(pMap); + // 4.根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + view.setJsp(action.results.get(result)); + System.out.println(view); + } catch (StrutsXMLLoaderException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new StrutsRunActionException("Action执行失败",e); + } + + + return view; + } + + + /** + * dom4j读xml + * @return + * @throws StrutsXMLLoaderException + */ + private Map getActionByDom4J() throws StrutsXMLLoaderException{ + return getActionByDom4J(STRUTS_CONFIG_DIR,STRUTS_CONFIG_XML,ENCODE); + } + /** + * dom4j读xml + * @param dir + * @param fileName + * @param encode + * @return + * @throws StrutsXMLLoaderException + */ + private Map getActionByDom4J(String dir,String fileName,String encode) throws StrutsXMLLoaderException { + File f = new File(dir); + if(!f.exists()){ + throw new StrutsXMLLoaderException("路径不存在:"+dir); + } + f = new File(f.getPath()+"\\"+fileName); + if(!f.exists()){ + throw new StrutsXMLLoaderException("路径:"+dir+"中文件不存在:"+fileName); + } + SAXReader saxReader = new SAXReader(); + saxReader.setEncoding(encode); + Map actions = new HashMap(10); + try { + Document document = saxReader.read(f); + Element root =document.getRootElement(); + Iterator it = root.elementIterator(DOC_ACTION); + while(it.hasNext()){ + Action action = new Action(); + Element element = it.next(); + action.name = element.attributeValue("name"); + action.clazz = element.attributeValue("class"); + Map results = new HashMap(10); + Iterator rIt = element.elementIterator(DOC_ACTION_RESULT); + while(rIt.hasNext()){ + Element rElement = rIt.next(); + results.put(rElement.attributeValue("name"), rElement.getText()); + } + action.results = results; + actions.put(action.name, action); + } + } catch (Exception e) { + throw new StrutsXMLLoaderException("xml文件解析失败",e); + } + + return actions; + } + + /** + * 首个字符大写 + * @param fieldName + * @return + */ + private String toUpperCase(String fieldName){ + return fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); + } + + + + /** + * Action类 + */ + private static class Action{ + String name; + String clazz; + Map results; + } + + + +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/StrutsTest.java b/group17/240094626/work_0226/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..09b00c8c3b --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,57 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.litestruts.exception.StrutsRunActionException; + + + + + +public class StrutsTest { + Struts struts = Struts.getInstance(); + + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = struts.runAction(actionName,params); + } catch (StrutsRunActionException e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = null; + try { + view = struts.runAction(actionName,params); + } catch (StrutsRunActionException e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/View.java b/group17/240094626/work_0226/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..880a7be260 --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/View.java @@ -0,0 +1,28 @@ +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; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } + +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsRunActionException.java b/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsRunActionException.java new file mode 100644 index 0000000000..5c5a7b9006 --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsRunActionException.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsRunActionException extends Exception { + + private static final long serialVersionUID = -242506476923032524L; + private String message; + private String stackTrace; + private Throwable t; + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsRunActionException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsRunActionException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsRunActionException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsXMLLoaderException.java b/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsXMLLoaderException.java new file mode 100644 index 0000000000..4efb8b9152 --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsXMLLoaderException.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsXMLLoaderException extends Exception { + + private static final long serialVersionUID = 5481955299021871754L; + private String message; + private String stackTrace; + private Throwable t; + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsXMLLoaderException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsXMLLoaderException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsXMLLoaderException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/struts.xml b/group17/240094626/work_0226/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group17/240094626/work_0226/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/group17/516886559/ArrayList.java b/group17/516886559/ArrayList.java new file mode 100644 index 0000000000..75222aecce --- /dev/null +++ b/group17/516886559/ArrayList.java @@ -0,0 +1,103 @@ +package com.rd.p2p.common.util.liuxin; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(E o){ + if(size >= elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + elementData[size++] = o; + size++; + } + + public void add(int index, E o){ + if(index > size){ + throw new ArrayIndexOutOfBoundsException("插入索引数不能大于数列总长度 " + index + ">" + size); + } + if(size >= elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + Object[] tempData = Arrays.copyOfRange(elementData, index, size); + elementData[index] = o; + for(int i = 1; i <= tempData.length; i++){ + elementData[i+index] = tempData[i-1]; + } + size++; + + } + + public Object get(int index){ + if(index >= size){ + throw new ArrayIndexOutOfBoundsException("取出数组索引不能大于等于数组总长度 " + index + ">=" + size); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >= size){ + throw new ArrayIndexOutOfBoundsException("移除索引数不能大于等于数列总长度 " + index + ">=" + size); + } + Object data = get(index); + Object[] tempData = Arrays.copyOfRange(elementData, index+1, size); + for(int i = 0; i < tempData.length; i++){ + elementData[index+i] = tempData[i]; + } + elementData[size - 1] = null; + size--; + return data; + } + + public int size(){ + return size; + } + + @Override + public String toString() { + for (Object object : elementData) { + System.out.println(object); + } + return null; + } + + //迭代器 + public Iterator iterator(){ + return new Iterator() { + + private int index = 0; + + @Override + public Object next() { + if(index >= size){ + throw new ArrayIndexOutOfBoundsException("取出数组索引不能大于等于数组总长度 " + index + ">=" + size); + } + return get(index++); + } + + @Override + public boolean hasNext() { + if(size > index){ + return true; + }else{ + return false; + } + } + }; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3,3); + Iterator in = list.iterator(); + while(in.hasNext()){ + System.out.println(in.next()); + } + } +} \ No newline at end of file diff --git a/group17/516886559/BinaryTreeNode.java b/group17/516886559/BinaryTreeNode.java new file mode 100644 index 0000000000..75c17d1198 --- /dev/null +++ b/group17/516886559/BinaryTreeNode.java @@ -0,0 +1,68 @@ +package com.rd.p2p.common.util.liuxin; + +/** + * 用Integer易于比较和插入 + * @author jhn + * time:2017年2月24日 + */ +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(){ + + } + + public BinaryTreeNode(Integer integer){ + this.data = integer; + } + + public Integer getData() { + return data; + } + public void setData(Integer 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(Integer o){ + if(data == null){ + data = o; + return this; + } + BinaryTreeNode node = new BinaryTreeNode(o); + BinaryTreeNode tempBinaryTreeNode = this; + boolean begin = true; + while(begin){ + if(o < data){ + tempBinaryTreeNode = tempBinaryTreeNode.getLeft(); + if(tempBinaryTreeNode.getLeft() == null){ + tempBinaryTreeNode.setLeft(node); + begin = false;; + } + }else{ + tempBinaryTreeNode = tempBinaryTreeNode.getRight(); + if(tempBinaryTreeNode.getRight() == null){ + tempBinaryTreeNode.setRight(node); + begin = false;; + } + } + + } + return node; + } + +} diff --git a/group17/516886559/Iterator.java b/group17/516886559/Iterator.java new file mode 100644 index 0000000000..d29475ed43 --- /dev/null +++ b/group17/516886559/Iterator.java @@ -0,0 +1,7 @@ +package com.rd.p2p.common.util.liuxin; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group17/516886559/LinkedList.java b/group17/516886559/LinkedList.java new file mode 100644 index 0000000000..1b0345de7d --- /dev/null +++ b/group17/516886559/LinkedList.java @@ -0,0 +1,150 @@ +package com.rd.p2p.common.util.liuxin; + +public class LinkedList implements List { + + private Node head; + + private int size; + + public void add(E o){ + if(size == 0){ + head = new Node(); + head.data = o; + }else{ + Node node = new Node(); + node.data = o; + getNode(size-1).next = node; + } + size ++; + } + + public void add(int index , E o){ + Node node = new Node(); + node.data = o; + if(index == 0){ + node.next = head; + head = node; + }else{ + Node indexNode = getNode(index - 1); + indexNode.next = node; + if(index < size){ + node.next = getNode(index); + } + } + size ++; + } + + public Object get(int index){ + return getNode(index).data; + } + + public Object remove(int index){ + if(index > size - 1){ + throw new ArrayIndexOutOfBoundsException("移除索引超出数组索引边界 " + index + ">" + (size - 1)); + } + if(index < 0){ + throw new ArrayIndexOutOfBoundsException("索引不能为负数"); + } + Node returnNode = null; + if(index == 0){ + returnNode = head; + if(head.next != null){ + head = head.next; + }else{ + head = null; + } + }else{ + returnNode = getNode(index); + if(returnNode.next != null){ + Node preNode = getNode(index-1); + Node nextNode = getNode(index+1); + preNode.next = nextNode; + } + } + size--; + return returnNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(E o){ + add(0,o); + } + + public void addLast(E o){ + add(size-1,o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size - 1); + } + + private Node getNode(int index){ + if(index > size - 1){ + throw new ArrayIndexOutOfBoundsException("查询索引超出数组索引边界 " + index + ">" + (size - 1)); + } + if(index < 0){ + throw new ArrayIndexOutOfBoundsException("索引不能为负数"); + } + Node tempNode = head; + if(index == 0){ + tempNode = head; + }else{ + for(int i = 0; i < index; i++){ + tempNode = tempNode.next; + } + } + return tempNode; + } + + private static class Node{ + Object data; + Node next; + } + + @Override + public String toString() { + for (int i = 0; i < size; i++) { + System.out.println(get(i)); + } + return null; + } + + //迭代器 + public Iterator iterator(){ + return new Iterator() { + private int index = 0; + + @Override + public Object next() { + if(index >= size){ + throw new ArrayIndexOutOfBoundsException("取出数组索引不能大于等于数组总长度 " + index + ">=" + size); + } + return get(index++); + } + + @Override + public boolean hasNext() { + if(size > index){ + return true; + }else{ + return false; + } + } + }; + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.toString(); + } +} diff --git a/group17/516886559/List.java b/group17/516886559/List.java new file mode 100644 index 0000000000..fd6e7883ba --- /dev/null +++ b/group17/516886559/List.java @@ -0,0 +1,9 @@ +package com.rd.p2p.common.util.liuxin; + +public interface List { + public void add(E o); + public void add(int index, E o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group17/516886559/Queue.java b/group17/516886559/Queue.java new file mode 100644 index 0000000000..d077a71a32 --- /dev/null +++ b/group17/516886559/Queue.java @@ -0,0 +1,30 @@ +package com.rd.p2p.common.util.liuxin; + +public class Queue { + + ArrayList arrayList = new ArrayList(); + + public void enQueue(T o){ + arrayList.add(o); + } + + public T deQueue(){ + if(arrayList.size() > 0){ + @SuppressWarnings("unchecked") + T t = (T) arrayList.get(0); + arrayList.remove(0); + return t; + }else{ + return null; + } + } + + public boolean isEmpty(){ + return arrayList.size() == 0 ? true : false; + } + + public int size(){ + return arrayList.size(); + } +} + diff --git a/group17/516886559/Stack.java b/group17/516886559/Stack.java new file mode 100644 index 0000000000..53ffc21e09 --- /dev/null +++ b/group17/516886559/Stack.java @@ -0,0 +1,52 @@ +package com.rd.p2p.common.util.liuxin; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(T o){ + elementData.add(o); + } + + public T pop(){ + if(elementData.size() > 0){ + @SuppressWarnings("unchecked") + T obj = (T)elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return obj; + }else{ + return null; + } + } + + @SuppressWarnings("unchecked") + public T peek(){ + if(elementData.size() > 0){ + return (T)elementData.get(elementData.size()-1); + }else{ + return null; + } + } + public boolean isEmpty(){ + return elementData.size() == 0 ? true : false; + } + public int size(){ + return elementData.size(); + } + + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + + System.out.println(stack.pop()); + System.out.println(stack.peek()); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.peek()); + } +} + diff --git a/group17/82427129/.gitignore b/group17/82427129/.gitignore index ccfa635638..2d0da18be3 100644 --- a/group17/82427129/.gitignore +++ b/group17/82427129/.gitignore @@ -1,7 +1,16 @@ + /.metadata/ -/RemoteSystemsTempFiles/ + +/RemoteSystemsTempFiles/ + + + +/JavaUtil/.settings/ + +/JavaUtil/target/ + -/JavaUtil/.settings/ .classpath + .project diff --git a/group17/82427129/JavaUtil/pom.xml b/group17/82427129/JavaUtil/pom.xml index 6c95dbb077..fb857bcd00 100644 --- a/group17/82427129/JavaUtil/pom.xml +++ b/group17/82427129/JavaUtil/pom.xml @@ -23,5 +23,10 @@ 4.7 test + + dom4j + dom4j + 1.6.1 + \ No newline at end of file diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/array/ArrayUtil.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..00ec1757ba --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,343 @@ +package com.coderising.array; + +import java.util.Arrays; + +import com.coding.basic.ArrayList; +import com.coding.basic.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static int[] reverseArray(int[] origin){ + int length = origin.length; + if(length <= 1) + return origin; + + int[] newArray = new int[length]; + for (int i = 0; i < length; i++) { + newArray[i] = origin[length-i-1]; + } + return newArray; + } + + /** + * 现在有如下的一个数组: 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 length = oldArray.length; + if(length == 0) + return oldArray; + + int[] newArray = new int[length]; + int oi = 0; + int ni = 0; + while(oi < length){ + if(oldArray[oi] == 0){ + oi++; + }else{ + newArray[ni++] = oldArray[oi++]; + } + } + if(ni == 0) + return null;//输入的数组全部都是0时 + if(ni == length) + return newArray;//输入的数组不含0时,不需要截取 + + return Arrays.copyOf(newArray, ni); + } + + /** + * 给定两个已经排序好的整形数组, 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){//时间复杂度O(n) + int length1 = array1.length; + int length2 = array2.length; + if(length1 == 0 ||(length1 == 0 && length2 ==0)){ + return array2; + } + if(length2 == 0){ + return array1; + } + + int i1 = 0;//始终指向array1中剩余的最小元素 + int i2 = 0;//始终指向array2中剩余的最小元素 + ArrayList stack = new ArrayList();//“过去的”最小值放入栈中 + + while (i1 < length1 || i2 < length2){//判定,只要还有一个数组中有未入栈元素 + if(i1 == length1){//判定,array1元素都已经入栈 + stack.add(array2[i2++]); + continue; + } + if(i2 == length2){//判定,array2元素都已经入栈 + stack.add(array1[i1++]); + continue; + } + + int comRes = compare(array1[i1], array2[i2]); + if(comRes > 0){ + stack.add(array2[i2++]); + }else if(comRes <0){ + stack.add(array1[i1++]); + }else{ + stack.add(array1[i1]); + i1++; + i2++; + } + } + + int[] result = new int[stack.size()]; + for (int i = 0; i < stack.size(); i++) { + result[i] = (int) stack.get(i); + } + return result; + } + /** + * when i < j returns -1; + * when i = j returns 0; + * when i > j returns 1; + * @param i + * @param j + * @return + */ + private static int compare(int i, int j){ + if(i < j){ + return -1; + }else if(i == j){ + return 0; + }else{ + return 1; + } + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max <= 1) + return new int[0]; + + ArrayList l = new ArrayList(); + for (int i = 0; max>fibonacciNum(i); i++) { + l.add(fibonacciNum(i)); + } + + int[] result = new int[l.size()]; + for (int i = 0; i < l.size(); i++) { + result[i] = (int) l.get(i); + } + return result; + } + /** + * 私有方法 + * 根据 斐波那契数列 + * 通过下标获取数列元素 + * + * 例如 [1,1,2,3,5,8,13,...] fibonacciNum(1) = 1,fibonacciNum(4) = 3 + * @param index 下标 + * @return 斐波那契数列元素 + */ + private static int fibonacciNum(int index){ + if(index < 0 ) + throw new IndexOutOfBoundsException("下标越界,index>=0"); + + if(index == 0||index == 1) + return 1; + return fibonacciNum(index-1)+fibonacciNum(index-2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if(max<2) + return new int[0]; + + ArrayList list = new ArrayList(); + for (int i = 0; i < max; i++) { + if(isPrimeNum(i)) + list.add(i); + } + + int[] result = new int[list.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = (int) list.get(i); + } + return result; + } + /** + * 判断一个整数是否是质数 + * @param num + * @return + */ + private static boolean isPrimeNum(int num){ + if(num < 2)//质数都是正数,并且大于等于2 + return false; + + for (int i = 2; i <= Math.sqrt(num); i++) { + if(num % i ==0){ + return false; + } + } + return true; + + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + ArrayList list = new ArrayList(); + for (int i = 6; i < max; i++) { + if(isPerfectNumber(i)) + list.add(i); + } + + int[] result = new int[list.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = (int) list.get(i); + } + return result; + } + /** + * 判断一个整数是否是完数 + * + * 因为所有的完数都是三角形数,所有我们通过遍历三角形数判断,如果是三角形数,再判断是否所有因子的积为这个整数 + * @param num + * @return + */ + private static boolean isPerfectNumber(int num){ + if(num < 6)// the first perfect number is 6 + return false; + if(!isEndWith6or8(num))//perfect number is only end with 6 or 8 + return false; + + /*if(!isTriangularNumber(num)) + return false;*/ + + List list = new ArrayList(); + + double sqr = Math.sqrt(num); + list.add(1); + for (int i = 2; i <= sqr; i++) { + if(num % i == 0){ + list.add(num/i); + list.add(i); + } + if(i == sqr){ + list.add(i); + break; + } + } + int sum = 0; + for (int i = 0; i < list.size(); i++) { + Integer in = (Integer) list.get(i); + sum += in; + } + if(sum == num) + return true; + else + return false; + } + + private static boolean isEndWith6or8(int num){ + if(num == 6) + return true; + + Integer integer = new Integer(num); + String string = integer.toString(); + String lastIndex = string.substring(string.length()-1); + if(lastIndex.equals("6") || lastIndex.equals("8")) + return true; + else + return false; + } + private static boolean isTriangularNumber(int num){ + if(num < 1 ) + return false; + + int i = 0; + while(triangularNumber(i)<=num){ + if(triangularNumber(i)==num) + return true; + i++; + } + + return false; + } + /** + * 获取三角形数,从数列中获取。 + * [1,3,6,10,...] 下标从0开始,例如triangularNumber(0) = 1,triangularNumber(1) = 3,triangularNumber(2)=6 + * @param index + * @return + */ + private static int triangularNumber(int index){ + if(index < 0) + throw new IndexOutOfBoundsException("下标越界,index>=0"); + if(index == 0) + return 1; + return triangularNumber(index-1)+index+1; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + if(array == null) + return null; + if(array.length == 0) + return ""; + + String result = String.valueOf(array[0]); + for (int i = 1; i < array.length; i++) { + result += seperator + array[i]; + } + return result; + } + + +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/LoginAction.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/Struts.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..b63cca0b77 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,149 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + private static SAXReader reader = new SAXReader(); + private static Document document; + private static Element root; + private static List actionList; + static{ + try { + document = reader.read(new File("src/main/resources/liteStruts.xml")); + } catch (DocumentException e) { + e.printStackTrace(); + } + root = document.getRootElement(); + actionList = root.elements("action"); + } + + /* + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + public static View runAction(String actionName, Map parameters) { + + if(actionName == null || actionName.equals("")){ + System.out.println("请求名为空,错误!"); + return null; + } + + View view = new View(); + + String className = null; + try { + Map actionInfo = getActionInfo(actionName); + + className = actionInfo.get("className"); + Class actionClass = Class.forName(className); + Object obj =actionClass.newInstance(); + for(Map.Entry entry : parameters.entrySet()){ + Method setMethod = actionClass.getDeclaredMethod(("set"+toUpperCaseFirstLetter(entry.getKey())), String.class); + setMethod.invoke(obj, entry.getValue()); + } + Method execute = actionClass.getDeclaredMethod("execute"); + String msg = (String) execute.invoke(obj); + view.setJsp(actionInfo.get(msg)); + + Method[] m = actionClass.getMethods(); + Map map = new HashMap(); + for (Method method : m) { + String methodName = method.getName(); + if(methodName.startsWith("get") + && !methodName.endsWith("Class") + && method.getParameterTypes().length == 0){ + map.put(toSubString_LowerCaseFirstLetter(methodName), method.invoke(obj)); + } + } + view.setParameters(map); + + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + + return null; + } + private static Map getActionInfo(String actionName){ + + HashMap hashMap = new HashMap(); + + for (Element action : actionList) { + Attribute attr = action.attribute("name"); + if(actionName.equals(attr.getStringValue())){ + Attribute attrClass = action.attribute("class"); + String className = attrClass.getStringValue(); + hashMap.put("className", className); + + @SuppressWarnings("unchecked") + List resultList = action.elements("result"); + for (Element result : resultList) { + Attribute attrResult = result.attribute("name"); + hashMap.put(attrResult.getStringValue(), result.getText()); + } + break; + } + } + return hashMap; + } + + /** + * return a String upcased the first letter + * @param param + * @return + */ + private static String toUpperCaseFirstLetter(String param){ + if(param == null || param.length() == 0) + return ""; + if(param.length() == 1) + return param.substring(0, 1).toUpperCase(); + + return param.substring(0, 1).toUpperCase()+param.substring(1); + } + private static String toSubString_LowerCaseFirstLetter(String param){ + if(!param.startsWith("get")) + return null; + int length = param.length(); + if(length < 4) + return ""; + + param = param.substring(3); + return param.substring(0, 1).toLowerCase()+param.substring(1); + } + +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/View.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..41b10d8a68 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java index d9f3d217f5..2a1a99bc9d 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java @@ -2,7 +2,7 @@ import java.util.Arrays; -public class ArrayList implements List { +public class ArrayList implements List { private int size = 0; @@ -35,34 +35,48 @@ public void add(int index, Object o){ } public Object set(int index, Object o){ - rangeCheck(index); + elementIndexCheck(index); Object oldValue = elementData[index]; elementData[index] = o; return oldValue; } - public Object get(int index){ - rangeCheck(index); - return elementData[index]; + public E get(int index){ + elementIndexCheck(index); + return elementData(index); } - public Object remove(int index){ - rangeCheck(index); - Object oldValue = elementData[index]; + @SuppressWarnings("unchecked") + E elementData(int index){ + return (E) elementData[index]; + } + + public E remove(int index){ + elementIndexCheck(index); + E oldValue = elementData(index); int movedLength = size - index - 1; - if(movedLength > 0)//ҪɾһԪʱҪƶ飬ֻҪһԪnull + if(movedLength > 0)//当要删除最后一个元素时,不需要移动数组,只需要把最后一个元素置null System.arraycopy(elementData, index+1, elementData, index, size-index-1); elementData[--size] = null; return oldValue; } - + /** + * range check, + * permit the range [0,size] + * @param index + */ private void rangeCheckForAdd(int index){ if( index > size || index<0 ){ throw new IndexOutOfBoundsException(outofIndex(index)); } } - private void rangeCheck(int index){ + /** + * element's index check, + * permit the index [0,size) + * @param index + */ + private void elementIndexCheck(int index){ if( index >= size || index < 0){ throw new IndexOutOfBoundsException(outofIndex(index)); } @@ -78,7 +92,7 @@ public Iterator iterator(){ private void ensureCapacity(int minCapacity){ if(elementData == EMPTY_ELEMENTDATA){ - minCapacity = Math.max(minCapacity, INITIALCAPACITY);//addall״ӵͱINITIALCAPACITY + minCapacity = Math.max(minCapacity, INITIALCAPACITY);//针对addall首次增加的数量就比INITIALCAPACITY多 } if(minCapacity - elementData.length > 0){ grow(minCapacity); diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java index 06ef6311b2..dbe8b9afb2 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java index 2a63eee0fc..f9934963e7 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java @@ -2,17 +2,17 @@ import java.util.NoSuchElementException; -public class LinkedList implements List { +public class LinkedList implements List { private int size = 0; - private Node first; + private Node first; - private Node last; + private Node last; - public void add(Object o){ + public void add(E o){ add(size,o); } - public void add(int index , Object o){ + public void add(int index , E o){ rangeCheck(index); if(index == size){ @@ -21,9 +21,9 @@ public void add(int index , Object o){ linkBefore(o, indexOf(index)); } } - private void linkBefore(Object o ,Node succ){ - final Node prev = succ.prev; - final Node newNode = new Node(prev, o, succ); + private void linkBefore(E o ,Node succ){ + final Node prev = succ.prev; + final Node newNode = new Node(prev, o, succ); succ.prev = newNode; if(prev == null){ first = newNode; @@ -32,9 +32,9 @@ private void linkBefore(Object o ,Node succ){ } size++; } - private void linkLast(Object o){ - final Node succ = last; - final Node newNode = new Node(succ, o, null); + private void linkLast(E o){ + final Node succ = last; + final Node newNode = new Node(succ, o, null); last = newNode; if(succ == null){ first = newNode; @@ -43,29 +43,38 @@ private void linkLast(Object o){ } size++; } + /** + * range check, + * permit the range [0,size] + * @param index + */ private void rangeCheck(int index) { if(index > size|| index < 0 ) throw new IndexOutOfBoundsException("Size"+size+":index"+index); } + /** + * element's index check, + * permit the index [0,size) + * @param index + */ private void elementIndexCheck(int index){ if(index >=size||index < 0) throw new IndexOutOfBoundsException("Size"+size+":index"+index); } /** - * ȡ±ꡱΪindexֵ, - * indexΪsizeʱnull + * * @param index * @return */ - private Node indexOf(int index){ + private Node indexOf(int index){ if(index < (this.size>>1) ){ - Node x = first; + Node x = first; for (int i = 0; i < index; i++) { x = x.next; } return x; }else{ - Node x = last; + Node x = last; for (int i = this.size-1; i > index; i--) { x = x.prev; } @@ -73,12 +82,12 @@ private Node indexOf(int index){ } } - public Object get(int index){ + public E get(int index){ elementIndexCheck(index); - return indexOf(index); + return indexOf(index).data; } - public Object remove(int index){ + public E remove(int index){ elementIndexCheck(index); if(index == 0){ @@ -90,10 +99,10 @@ public Object remove(int index){ } } - private Object unlinkNode(Node node) { - final Node next = node.next; - final Node prev = node.prev; - final Object element = node.data; + private E unlinkNode(Node node) { + final Node next = node.next; + final Node prev = node.prev; + final E element = node.data; if(next == null){ last = node; }else{ @@ -115,20 +124,20 @@ public int size(){ return size; } - public void addFirst(Object o){ + public void addFirst(E o){ linkBefore(o, first); } - public void addLast(Object o){ + public void addLast(E o){ linkLast(o); } - public Object removeFirst(){ + public E removeFirst(){ if(first == null) throw new NoSuchElementException("first is null"); - Object oldData = first.data; - final Node next = first.next; + E oldData = first.data; + final Node next = first.next; first.data = null; first.next = null;//GC first = next; @@ -143,12 +152,12 @@ public Object removeFirst(){ return oldData; } - public Object removeLast(){ + public E removeLast(){ if(last == null) throw new NoSuchElementException("last is null"); - Object oldData = last.data; - final Node prev = last.prev; + E oldData = last.data; + final Node prev = last.prev; last.prev = null; last.data = null;//GC last = prev; @@ -167,11 +176,11 @@ public Iterator iterator(){ return null; } - private static class Node{ - Object data; - Node next; - Node prev; - Node(Node prev,Object data,Node next){ + private static class Node{ + E data; + Node next; + Node prev; + Node(Node prev,E data,Node next){ this.data = data; this.next = next; this.prev = prev; diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java index 10d13b5832..e79d164654 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java @@ -1,9 +1,25 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +/** + * 代码进行了多次重构,按照JDK源码的思路进行编写,
+ *
+ * 重点1:rangeCheck和elementIndexCheck 完全管理了所有的Index的检测工作,这一点就 省去了其他地方关于下标是否越界的检测
+ *
+ * 重点2:arrayList的elementData(int i)以及LinkedList的node(int i)方法通过重构统一了通过下标获取元素的操作
+ * + * @author Walker + * + * @param + */ +public interface List { + + void add(E o); + + void add(int index, E o); + + Object get(int index); + + Object remove(int index); + + int size(); +} diff --git a/group17/82427129/JavaUtil/src/main/resources/liteStruts.xml b/group17/82427129/JavaUtil/src/main/resources/liteStruts.xml new file mode 100644 index 0000000000..a4ca47c733 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/resources/liteStruts.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/group17/82427129/JavaUtil/src/test/java/com/coderising/array/ArrayUtilTest.java b/group17/82427129/JavaUtil/src/test/java/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..4401629b5d --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,80 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() { + int[] a = {7, 9 , 30, 3}; + int[] ra = {3, 30, 9,7}; + int[] reverseArray = ArrayUtil.reverseArray(a); + Assert.assertArrayEquals(ra, reverseArray); + } + + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(newArr, ArrayUtil.removeZero(oldArr)); + } + + @Test + public void testMerge() { + int[] a1 = {3,5,7,8}; + int[] a2 = {4,5,6,7}; + int[] a3 = {3,4,5,6,7,8}; + Assert.assertArrayEquals(a3, ArrayUtil.merge(a1, a2)); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int[] newArray = {2,3,6,0,0,0,0}; + Assert.assertArrayEquals(newArray, ArrayUtil.grow(oldArray, 4)); + } + + @Test + public void testFibonacci() { + int max = 15; + int[] fibonacci = ArrayUtil.fibonacci(max); + int[] f = {1,1,2,3,5,8,13}; + Assert.assertArrayEquals(f, fibonacci); + } + + @Test + public void testGetPrimes() { + int max = 23; + int[] primes = ArrayUtil.getPrimes(max); + int[] expecteds = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(expecteds, primes); + } + + @Test + public void testGetPerfectNumbers() { + int max = 8129; + int[] perfectNumbers = ArrayUtil.getPerfectNumbers(max); + int[] expected = {6,28,496,8128}; + Assert.assertArrayEquals(expected, perfectNumbers); + } + + @Test + public void testJoin() { + int[] array = {3,8,9}; + String sep = "-"; + String join = ArrayUtil.join(array, sep); + Assert.assertEquals("3-8-9", join); + } + +} diff --git a/group17/82427129/JavaUtil/src/test/java/com/coderising/litestruts/StrutsTest.java b/group17/82427129/JavaUtil/src/test/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/82427129/JavaUtil/src/test/java/com/coding/basic/ArrayListTest.java b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..19ce5fbbd8 --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testArrayList() { + fail("Not yet implemented"); + } + + @Test + public void testArrayListInt() { + fail("Not yet implemented"); + } + + @Test + public void testAddObject() { + fail("Not yet implemented"); + } + + @Test + public void testAddIntObject() { + fail("Not yet implemented"); + } + + @Test + public void testSet() { + 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 testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..6020e772c9 --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,81 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + private LinkedList list; + + @Before + public void setUp() throws Exception { + list = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void testAddObject() { + list.add(100); + Assert.assertEquals(1, list.size()); + list.add(100); + Assert.assertEquals(2, list.size()); + list.add(100); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testAddIntObject() { + for (int i = 0; i < 5; i++) { + list.add(i, i); + } + Assert.assertEquals(5, list.size()); + } + + @Test + public void testGet() { + + } + + @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/group17/82427129/JavaUtil/src/test/java/com/coding/basic/Test.java b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/Test.java new file mode 100644 index 0000000000..572907fd34 --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/Test.java @@ -0,0 +1,14 @@ +package com.coding.basic; + +public class Test { + public static void main(String[] args) { + System.out.println(new A().e == new A().e); + } +} +class A{ + public A() { + e = E; + } + Object[] e; + static Object[] E = {}; +} diff --git a/group17/article/20170226-20170305.md b/group17/article/20170226-20170305.md new file mode 100644 index 0000000000..77e9b85142 --- /dev/null +++ b/group17/article/20170226-20170305.md @@ -0,0 +1,56 @@ +# 自由写作 + +## 须知 +--- + +交作业时请在QQ 号后面填上各自的文章链接, 比如: + +51075907 http://m.blog.csdn.net/article/details?id=57083764 + +## 文章 +--- + +1204187480 + +102228177 + +876385982 + +785396327 + +1059107701 + +240094626 + +82427129 + +296910598 + +1264835468 http://www.jianshu.com/p/634ca8cdd6e3 + +516886559 + +1282579502 https://www.evernote.com/shard/s413/sh/142601dd-edc3-4e37-871e-37a7489d7634/a092bf080e1aefbaeab96d34edac8cf0 + +614982500 + +865797761 + +1540186032 + +176653813 + +116665530 + +51075907 + +1158154002 + +345450234 + +919764878 + +1368331120 + +517970312 + diff --git a/group17/article/template.md b/group17/article/template.md new file mode 100644 index 0000000000..d92ffef426 --- /dev/null +++ b/group17/article/template.md @@ -0,0 +1,56 @@ +# XXXX + +## 须知 +--- + +交作业时请在QQ 号后面填上各自的文章链接, 比如: + +51075907 http://m.blog.csdn.net/article/details?id=57083764 + +## 文章 +--- + +1204187480 + +102228177 + +876385982 + +785396327 + +1059107701 + +240094626 + +82427129 + +296910598 + +1264835468 + +516886559 + +1282579502 + +614982500 + +865797761 + +1540186032 + +176653813 + +116665530 + +51075907 + +1158154002 + +345450234 + +919764878 + +1368331120 + +517970312 + diff --git "a/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" "b/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" index 36b44b4aa8..df34ed9d6d 100644 --- "a/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" +++ "b/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" @@ -13,6 +13,7 @@ 1204187480 102228177 http://note.youdao.com/noteshare?id=74a51e7f93461dfb77c69a1cf4755624&sub=004F10FA5D2046ABAA060F19C0D2A18F + http://note.youdao.com/noteshare?id=6d117ad0ead79eafee2a5308c00d6a3a 876385982 http://www.totoro-fly.com/?p=59 @@ -20,9 +21,9 @@ 1059107701 -240094626 +240094626 http://note.youdao.com/noteshare?id=d129c169ed611f6099b96e8e0f3f5c51 -82427129 http://blog.csdn.net/walk_er/article/details/57406278 +82427129 http://blog.csdn.net/walk_er/article/details/57406278 ;http://blog.csdn.net/walk_er/article/details/60475450 296910598 @@ -30,7 +31,7 @@ 516886559 -1282579502 https://www.evernote.com/shard/s413/sh/3af5f6a4-a580-4a49-b63c-90f5b178aca4/7cf052e3789c862e38d6d6b3cce1ceed +1282579502 (文章1)https://www.evernote.com/shard/s413/sh/3af5f6a4-a580-4a49-b63c-90f5b178aca4/7cf052e3789c862e38d6d6b3cce1ceed (文章2)https://www.evernote.com/shard/s413/sh/142601dd-edc3-4e37-871e-37a7489d7634/a092bf080e1aefbaeab96d34edac8cf0 614982500 @@ -44,7 +45,7 @@ 51075907 http://m.blog.csdn.net/article/details?id=57083764 -1158154002 +1158154002 http://blog.csdn.net/shengren12/article/details/57400858 345450234 @@ -53,4 +54,3 @@ 1368331120 517970312 - diff --git a/group17/count/homework.md b/group17/count/homework.md new file mode 100644 index 0000000000..c37e964c5e --- /dev/null +++ b/group17/count/homework.md @@ -0,0 +1,21 @@ +# 作业统计 + +## 须知 +--- + +日期下面的链接是每次作业的统计, 大家作业完成后点击链接即可进入文档页面填写. + +## 文档 +--- + +### 20170219-20170226 + +[文章统计](https://github.com/luoziyihao/coding2017/blob/master/group17/article/%E5%86%99%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0%E4%BB%8B%E7%BB%8Dcpu%2C%20%E5%86%85%E5%AD%98%2C%20%E7%A3%81%E7%9B%98%2C%20%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E4%BB%96%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB_20170226.md) + +[作业统计](https://shimo.im/sheet/LQaZ3eVFgZYh8c1X/「第17组作业完成情况_2017...0226」) + +### 20170226-20170205 + +[文章统计](https://github.com/luoziyihao/coding2017/blob/master/group17/article/20170226-20170305.md) + +[作业统计](https://shimo.im/sheet/8T7YWAlIJpg6apn6/「第17组作业完成情况_2017...0305」) diff --git a/group17/group17.md b/group17/group17.md index d3f5a12faa..8b13789179 100644 --- a/group17/group17.md +++ b/group17/group17.md @@ -1 +1 @@ - + diff --git a/group18/1049843090/.classpath b/group18/1049843090/.classpath new file mode 100644 index 0000000000..68a547a96e --- /dev/null +++ b/group18/1049843090/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/group18/1049843090/.gitignore b/group18/1049843090/.gitignore new file mode 100644 index 0000000000..117a0b15d5 --- /dev/null +++ b/group18/1049843090/.gitignore @@ -0,0 +1,7 @@ +*.idea +*.iml +*.eml +.settings/ +target/ +build/ +out/ \ No newline at end of file diff --git a/group18/1049843090/.project b/group18/1049843090/.project new file mode 100644 index 0000000000..978233664c --- /dev/null +++ b/group18/1049843090/.project @@ -0,0 +1,15 @@ + + + 1049843090 + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1049843090/src/com/coderising/array/ArrayUtil.java b/group18/1049843090/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..23e5bd2215 --- /dev/null +++ b/group18/1049843090/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,197 @@ +package com.coderising.array; + + +import com.coding.basic.Queue; +import com.coding.basic.Stack; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (isEmptyOrNull(origin)) { + return; + } + //solution 1 move element + //for (int i = 0; i <= origin.length >> 2; i++) { + // int temp = origin[i]; + // origin[i] = origin[origin.length - 1 - i]; + // origin[origin.length - 1 - i] = temp; + //} + + //solution 2 use Stack + Stack stack = new Stack<>(); + for (int i : origin) { + stack.push(i); + } + for (int i = 0; i < origin.length; i++) { + origin[i]=stack.pop(); + } + } + + /** + * 现在有如下的一个数组: 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) { + if (isEmptyOrNull(oldArray)) { + return null; + } + //solution 1 use Queue OR Stack + //Queue queue = new Queue<>(); + //for (int i : oldArray) { + // if (i != 0) { + // queue.enQueue(i); + // } + //} + //int[] newArray = new int[queue.size()]; + //for (int i = 0; i < newArray.length; i++) { + // newArray[i] = queue.deQueue(); + //} + //return newArray; + + + //solution 2 use Array + int[] tempArray = new int[oldArray.length]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + tempArray[index++] = oldArray[i]; + } + } + int[] newArray = new int[index]; + System.arraycopy(tempArray,0,newArray,0,index); + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + + return null; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + if (isEmptyOrNull(oldArray)) { + return null; + } + int[] newArray = new int[oldArray.length + size]; + //solution 1 use System.arraycopy + //System.arraycopy(oldArray,0,newArray,0, oldArray.length); + + //solution 2 use loop + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator 分隔符 + * @return + */ + public String join(int[] array, String seperator) { + if (isEmptyOrNull(array)) { + return ""; + } + if (array.length < 2) { + return array[0] + ""; + } + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < array.length - 1; i++) { + stringBuffer.append(array[i] + seperator); + } + stringBuffer.append(array[array.length - 1]); + return stringBuffer.toString(); + } + + /** + * 检查数组是否为空或者为null + * + * @param array + * @return + */ + private boolean isEmptyOrNull(int[] array) { + if (array == null || array.length == 0) { + return true; + } + return false; + } + + public static void main(String[] args) { + int[] a = {7,9,5}; + ArrayUtil arrayUtil = new ArrayUtil(); + arrayUtil.reverseArray(a); + for (int i : a) { + System.out.println(i); + } + } + +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/ArrayList.java b/group18/1049843090/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f6b3007a7d --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/ArrayList.java @@ -0,0 +1,148 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * A Simple ArrayList + */ +public class ArrayList implements List { + + /** + * 当前list的元素个数 + */ + private int size; + + /** + * 默认数组大小 + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * 存储元素的数组 + */ + private Object[] elementData; + + /** + * 追加一个元素 + * + * @param e + */ + public void add(E e) { + grow(); + elementData[size++] = e; + } + + private void grow() { + if (elementData.length == size) { + elementData = Arrays.copyOf(elementData, size + 10); + } + } + + /** + * 插入一个元素到指定位置 + * + * @param index + * @param e + */ + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("下标越界"); + } + grow(); + int movedNum = size - index; + if (movedNum > 0) { + System.arraycopy(elementData, index, elementData, index + 1, movedNum); + } + elementData[index] = e; + size++; + } + + /** + * 获取指定位置的元素 + * + * @param index + * @return + */ + public E get(int index) { + checkIndex(index); + return getElement(index); + } + + + /** + * 删除指定位置的元素 + * + * @param index + * @return + */ + public E remove(int index) { + checkIndex(index); + E delEle = getElement(index); + int movedNum = size - index - 1;//是不是最后一个元素 + if (movedNum > 0) { + System.arraycopy(elementData, index + 1, elementData, index, movedNum); + } + elementData[--size] = null; + return delEle; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("下标越界"); + } + } + + private E getElement(int index) { + return (E) elementData[index]; + } + + /** + * list中元素的个数 + * + * @return + */ + public int size() { + return this.size; + } + + + public ArrayList() { + this.elementData = new Object[DEFAULT_CAPACITY]; + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int cursor;//游标 + + private int lastRet = -1;//可被删除元素下标 + + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public E next() { + int i = cursor; + cursor++; + return (E) elementData[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + cursor = lastRet;//游标等于当前删除元素的下标 或者 cursor--; + ArrayList.this.remove(lastRet); + lastRet = -1;//重置可删元素下标 + + } + } + +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/BinaryTree.java b/group18/1049843090/src/com/coding/basic/BinaryTree.java new file mode 100644 index 0000000000..7f476b1d74 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/BinaryTree.java @@ -0,0 +1,95 @@ +package com.coding.basic; + + +/** + * + */ +public class BinaryTree { + + private Node root; + + public boolean insert(int data) { + Node newNode = new Node(data); + if (root == null) { + root = newNode; + return true; + } + + return add(data); + //return add(root,new Node(data)); + } + + private boolean add(Node currentNode,Node newNode) { + if (currentNode.data == newNode.data) { + return false; + } + if (currentNode.data < newNode.data) { + if (currentNode.right == null) { + currentNode.right = newNode; + return true; + } else { + return add(currentNode.right, newNode); + } + } + if (currentNode.left == null) { + currentNode.left = newNode; + return true; + } else { + return add(currentNode.left, newNode); + } + } + + private boolean add(int data) { + Node newNode = new Node(data); + boolean result = false; + Node cursorNode = root; + Node parentNode = null; + while (true) { + parentNode = cursorNode; + if (cursorNode.data == data) { + break; + } + if (cursorNode.data < data) { + cursorNode = cursorNode.right; + if (cursorNode == null) { + parentNode.right = newNode; + result = true; + break; + } + } else { + cursorNode = cursorNode.left; + if (cursorNode == null) { + parentNode.left = newNode; + result = true; + break; + } + } + } + return result; + } + + + private static class Node { + int data; + Node left, right; + + public Node(int data) { + this.data = data; + this.left = null; + this.right = null; + } + } + + public static void main(String[] args) { + BinaryTree binaryTree = new BinaryTree(); + binaryTree.insert(5); + binaryTree.insert(6); + binaryTree.insert(4); + binaryTree.insert(8); + binaryTree.insert(7); + binaryTree.insert(3); + + System.out.println("finsh"); + } + +} diff --git a/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java b/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..20f90e9239 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package com.coding.basic; + +/** + * Binary Tree + */ +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; + } + + public BinaryTreeNode(Object o){ + this.setData(o); + } + +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Iterator.java b/group18/1049843090/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..f4938a46c7 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Iterator.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +/** + * Iterator + */ +public interface Iterator { + + /** + * 可迭代对象是否还有值 + * + * @return + */ + boolean hasNext(); + + /** + * 返回迭代对象的下一个元素 + * + * @return + */ + E next(); + + /** + * 移除当前元素 + */ + void remove(); +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/LinkedList.java b/group18/1049843090/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..7b4a378301 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/LinkedList.java @@ -0,0 +1,238 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * A Simple LinkedList + * + * @param element + */ +public class LinkedList implements List { + /** + * 链表head + */ + private Node head; + + + /** + * 链表中元素的个数 + */ + private int size; + + + /** + * 追加一个元素到链表尾 + * + * @param e + */ + public void add(E e) { + Node newNode = new Node(e, null); + if (this.head == null) { + this.head = newNode; + } else { + Node end = index(size - 1); + end.next = newNode; + } + size++; + } + + /** + * 插入一个元素到链表指定位置 + * + * @param index + * @param e + */ + public void add(int index, E e) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("下标越界"); + } + if (index == 0) { + addFirst(e); + } else if (index == size) { + addLast(e); + } else { + Node indexNode = index(index); + Node next = indexNode.next; + Node newNode = new Node(e, next); + index(index - 1).next = newNode; + indexNode = null; + size++; + } + } + + /** + * 获取指定位置的元素 + * + * @param index + * @return + */ + public E get(int index) { + checkIndex(index); + return index(index).data; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("下标越界"); + } + } + + /** + * 移除指定位置的元素 + * + * @param index + * @return + */ + public E remove(int index) { + checkIndex(index); + if (index == 0) { + return removeFirst(); + } else if (index == (size - 1)) { + return removeLast(); + } else { + Node delNode = index(index); + E e = delNode.data; + Node prev = index(index - 1); + prev.next = index(index + 1); + delNode = null; + size--; + return e; + } + } + + /** + * 当前链表的元素个数 + * + * @return + */ + public int size() { + return this.size; + } + + /** + * 添加到链表的头 + * + * @param e + */ + public void addFirst(E e) { + Node newNode = new Node(e, null); + if (this.head != null) { + newNode.next = this.head; + } + this.head = newNode; + size++; + } + + /** + * 添加到链表的尾 + * + * @param e + */ + public void addLast(E e) { + Node newNode = new Node(e, null); + if (this.head == null) { + this.head = newNode; + } else { + Node end = index(size - 1); + end.next = newNode; + } + size++; + } + + /** + * 获取指定位置的节点 + * + * @param index + * @return + */ + private Node index(int index) { + Node node = this.head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + /** + * 删除链表第一个元素 + * + * @return + */ + public E removeFirst() { + if (head == null) { + throw new NoSuchElementException(); + } + E e = head.data; + head = head.next; + size--; + return e; + } + + /** + * 删除链表最后一个元素 + * + * @return + */ + public E removeLast() { + if (head == null) { + throw new NoSuchElementException(); + } + Node end = index(size - 1); + E e = end.data; + end = null; + end = index(size - 2); + end.next = null; + size--; + return e; + } + + /** + * 节点数据 + * + * @param + */ + private static class Node { + //当前节点存储的数据 + E data; + //下一个节点 + Node next; + + public Node(E data, Node next) { + this.data = data; + this.next = next; + } + } + + public Iterator iterator(){ + return new LinkedListIterator<>(); + } + + private class LinkedListIterator implements Iterator{ + + private int cursor;//游标 + + private int lastRet = -1;//可被删除元素下标 + + @Override + public boolean hasNext() { + return cursor!=size; + } + + @Override + public E next() { + int i = cursor; + cursor++; + return (E) LinkedList.this.get(lastRet=i); + } + + @Override + public void remove() { + if(lastRet<0){ + throw new IllegalStateException(); + } + cursor = lastRet; + LinkedList.this.remove(lastRet); + lastRet = -1; + } + } +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/List.java b/group18/1049843090/src/com/coding/basic/List.java new file mode 100644 index 0000000000..962fcf4b77 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/List.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +/** + * A Simple List Interface + */ +public interface List { + + /** + * 追加一个元素 + * + * @param e + */ + void add(E e); + + /** + * 插入一个元素到指定位置 + * + * @param index + * @param e + */ + void add(int index, E e); + + /** + * 获取指定位置元素 + * + * @param index + * @return + */ + E get(int index); + + /** + * 移除指定位置元素 + * + * @param index + * @return + */ + E remove(int index); + + + /** + * 当前List中元素的个数 + * + * @return + */ + int size(); +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Queue.java b/group18/1049843090/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..94759f0b5b --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Queue.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +/** + * A Simple Queue + */ +public class Queue { + + private LinkedList elementData; + + /** + * 入列 + * + * @param e + */ + public void enQueue(E e) { + elementData.addLast(e); + } + + /** + * 出列 + * + * @return + */ + public E deQueue() { + return elementData.removeFirst(); + } + + /** + * 查看第一个元素 + * + * @return + */ + public E peek() { + return elementData.size()==0?null:elementData.get(0); + } + + /** + * 队列是否有元素 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0; + } + + /** + * 队列中元素个数 + * + * @return + */ + public int size() { + return elementData.size(); + } + + public Queue() { + elementData = new LinkedList(); + } +} \ No newline at end of file diff --git a/group18/1049843090/src/com/coding/basic/Stack.java b/group18/1049843090/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..61561f1450 --- /dev/null +++ b/group18/1049843090/src/com/coding/basic/Stack.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import java.util.EmptyStackException; + +/** + * A Simple Stack + */ +public class Stack { + private ArrayList elementData; + + /** + * 压入栈顶 + * + * @param e + */ + public void push(E e) { + elementData.add(e); + } + + /** + * 取出栈顶元素 + * + * @return + */ + public E pop() { + return elementData.remove(elementData.size() - 1); + } + + /** + * 查看栈顶元素 + * + * @return + */ + public E peek() { + if(isEmpty()){ + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + + /** + * 栈内是否有元素 + * + * @return + */ + public boolean isEmpty() { + return elementData.size() == 0; + } + + /** + * 栈顶内元素个数 + * + * @return + */ + public int size() { + return elementData.size(); + } + + public Stack() { + elementData = new ArrayList(); + } +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/ArrayListTest.java b/group18/1049843090/test/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..25b2a018df --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/ArrayListTest.java @@ -0,0 +1,89 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * ArrayList Test + */ +public class ArrayListTest { + + ArrayList list; + + @Before + public void setUp() throws Exception { + list = new ArrayList<>(); + + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void add() throws Exception { + list.add("first"); + assertEquals("first", list.get(0)); + } + + @Test + public void add1() throws Exception { + list.add(0, "first"); + assertEquals("插入第一条", "first", list.get(0)); + list.add(0, "insert"); + assertEquals("插入第二条", "insert", list.get(0)); + list.add(2, "position_2"); + assertEquals("position_2", list.get(2)); + assertEquals(3, list.size()); + } + + @Test + public void get() throws Exception { + list.add("first"); + list.add("second"); + list.add("third"); + assertEquals("first", list.get(0)); + assertEquals("second", list.get(1)); + assertEquals("third", list.get(2)); + + } + + @Test + public void remove() throws Exception { + list.add("first"); + list.add("second"); + list.add("third"); + list.add("fourth"); + assertEquals("first", list.remove(0)); + assertEquals(3, list.size()); + assertEquals("third", list.remove(1)); + assertEquals("fourth", list.remove(1)); + assertEquals(1, list.size()); + + } + + @Test + public void size() throws Exception { + list.add("first"); + assertEquals(1,list.size()); + list.add("second"); + assertEquals( 2,list.size()); + } + + + @Test + public void iterator() throws Exception { + Iterator iterator = list.iterator(); + assertEquals(false,iterator.hasNext()); + list.add("A"); + assertEquals(true,iterator.hasNext()); + assertEquals("A",iterator.next()); + iterator.remove(); + assertEquals(0,list.size()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/LinkedListTest.java b/group18/1049843090/test/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..fb962287d3 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/LinkedListTest.java @@ -0,0 +1,129 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * LinkedList Test + */ +public class LinkedListTest { + + LinkedList linkedList; + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList<>(); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @Test + public void add() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + assertEquals(2, linkedList.size()); + + } + + @Test + public void add1() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + assertEquals("first", linkedList.get(0)); + assertEquals("second", linkedList.get(1)); + assertEquals(2, linkedList.size()); + } + + @Test + public void get() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + assertEquals("first", linkedList.get(0)); + assertEquals("second", linkedList.get(1)); + assertEquals("third", linkedList.get(2)); + } + + @Test + public void remove() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + linkedList.add("fourth"); + assertEquals("first", linkedList.remove(0)); + assertEquals("third", linkedList.remove(1)); + assertEquals("fourth", linkedList.remove(1)); + assertEquals(1, linkedList.size()); + + } + + @Test + public void size() throws Exception { + linkedList.add(0, "first"); + linkedList.add(1, "second"); + linkedList.add("third"); + linkedList.add("fourth"); + assertEquals(4, linkedList.size()); + } + + @Test + public void addFirst() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.addFirst("first first"); + assertEquals("first first", linkedList.get(0)); + + } + + @Test + public void addLast() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.addLast("last"); + assertEquals("last", linkedList.get(2)); + } + + @Test + public void removeFirst() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.add("third"); + assertEquals("first", linkedList.removeFirst()); + assertEquals("second", linkedList.removeFirst()); + assertEquals(1, linkedList.size()); + assertEquals("third", linkedList.get(0)); + assertEquals("third", linkedList.removeFirst()); + assertEquals(0, linkedList.size()); + + } + + @Test + public void removeLast() throws Exception { + linkedList.add("first"); + linkedList.add("second"); + linkedList.add("third"); + assertEquals("third", linkedList.removeLast()); + assertEquals("second", linkedList.removeLast()); + assertEquals("first", linkedList.removeLast()); + assertEquals(0, linkedList.size()); + + } + + @Test + public void iterator() throws Exception { + Iterator iterator = linkedList.iterator(); + assertEquals(false,iterator.hasNext()); + linkedList.add("A"); + assertEquals(true,iterator.hasNext()); + assertEquals("A",iterator.next()); + iterator.remove(); + assertEquals(0,linkedList.size()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/QueueTest.java b/group18/1049843090/test/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..2652d1e214 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/QueueTest.java @@ -0,0 +1,61 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Queue Test + */ +public class QueueTest { + + Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue<>(); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void enQueue() throws Exception { + queue.enQueue("A"); + assertEquals("A",queue.deQueue()); + } + + @Test + public void peek() throws Exception { + assertEquals(null,queue.peek()); + queue.enQueue("A"); + assertEquals("A",queue.peek()); + } + + @Test + public void deQueue() throws Exception { + queue.enQueue("A"); + queue.enQueue("B"); + assertEquals("A",queue.deQueue()); + + } + + @Test + public void isEmpty() throws Exception { + assertEquals(true,queue.isEmpty()); + queue.enQueue("A"); + assertEquals(false,queue.isEmpty()); + } + + @Test + public void size() throws Exception { + queue.enQueue("A"); + queue.enQueue("B"); + assertEquals(2,queue.size()); + } + +} \ No newline at end of file diff --git a/group18/1049843090/test/com/coding/basic/StackTest.java b/group18/1049843090/test/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..e2587ba7f2 --- /dev/null +++ b/group18/1049843090/test/com/coding/basic/StackTest.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Stack Test + */ +public class StackTest { + Stack stack; + @Before + public void setUp() throws Exception { + stack = new Stack<>(); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void push() throws Exception { + stack.push("A"); + assertEquals("A",stack.pop()); + } + + @Test + public void pop() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals("C",stack.pop()); + assertEquals("B",stack.pop()); + assertEquals("A",stack.pop()); + assertEquals(0,stack.size()); + + + } + + @Test + public void peek() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals("C",stack.peek()); + + } + + @Test + public void isEmpty() throws Exception { + assertEquals(true,stack.isEmpty()); + stack.push("A"); + assertEquals(false,stack.isEmpty()); + } + + @Test + public void size() throws Exception { + stack.push("A"); + stack.push("B"); + stack.push("C"); + assertEquals(3,stack.size()); + } + +} \ No newline at end of file diff --git a/group18/1057617027/.classpath b/group18/1057617027/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group18/1057617027/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group18/1057617027/.gitignore b/group18/1057617027/.gitignore new file mode 100644 index 0000000000..5e56e040ec --- /dev/null +++ b/group18/1057617027/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/group18/1057617027/.project b/group18/1057617027/.project new file mode 100644 index 0000000000..3189c0c10d --- /dev/null +++ b/group18/1057617027/.project @@ -0,0 +1,17 @@ + + + 1057617027Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1057617027/src/com/coding/basic/ArrayList.java b/group18/1057617027/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..2c11c0a3fa --- /dev/null +++ b/group18/1057617027/src/com/coding/basic/ArrayList.java @@ -0,0 +1,77 @@ +package com.coding.basic; + + +@SuppressWarnings("rawtypes") +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData = new Object[100]; + public void kuorong(int size){ + if(size>elementData.length){ + Object[] elementDataTemp = new Object[size*2]; + System.arraycopy(elementData, 0, elementDataTemp, 0, elementData.length); + elementData = elementDataTemp; + } + } + public void add(Object o){ + kuorong(size); + elementData[size] = o; + ++size; + } + public void add(int index, Object o){ + if(index>size||index<0) + throw new IndexOutOfBoundsException("ȷindexֵ"+size+"ҲС0"); + kuorong(++size); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + } + + public Object get(int index){ + + return elementData[index]; + } + + public Object remove(int index){ + if(index>size||index<0) + throw new IndexOutOfBoundsException("ȷindexֵ"+size+"ҲС0"); + + System.arraycopy(elementData, index+1, elementData, index, size-index); + size--; + return elementData; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + + return new myiterator(); + } + public class myiterator implements Iterator{ + private int nextIndex; + public boolean hasNext(){ + return nextIndex!=size; + } + public Object next(){ + return elementData[nextIndex++]; + } + + } + public static void main(String[] args) { + ArrayList al = new ArrayList(); + + al.add(1); + al.add(2); + al.add(3); + al.add(4); + al.add(2,5); + al.remove(2); + for(int i= 0;i<5;i++){ + System.out.println(al.get(i));} + System.out.println(al.size()); + + } + +} diff --git a/group18/1057617027/src/com/coding/basic/BinaryTreeNode.java b/group18/1057617027/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..bcb0a4af65 --- /dev/null +++ b/group18/1057617027/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode head; + private BinaryTreeNode node; + BinaryTreeNode(Object data,BinaryTreeNode left,BinaryTreeNode right){ + this.data = data; + this.left = left; + this.right = right; + } + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + if(node==null){ + node = new BinaryTreeNode(o, null, null); + }else{ + if(Integer.parseInt(String.valueOf(o))<=Integer.parseInt(String.valueOf(node.data))){ + node.left = insert(node.left,o ); + node = node.left; + }else{ + node.right = insert(node.right,o); + node = node.right; + } + } + return node; + } + public BinaryTreeNode insert(BinaryTreeNode node,Object o){ + if(node==null){ + node = new BinaryTreeNode(o, null, null); + }else{ + if(Integer.parseInt(String.valueOf(o))<=Integer.parseInt(String.valueOf(node.data))){ + node.left = insert(node.left,o ); + node.left =node; + }else{ + node.right = insert(node.right,o ); + node.right =node; + } + } + return node; + } +public static void main(String[] args){ + BinaryTreeNode node = new BinaryTreeNode(null, null, null); + + System.out.println(node.insert(6).data); + System.out.println(node.insert(5).data); + System.out.println(node.insert(11).data); + System.out.println(node.insert(7).data); + System.out.println(node.insert(2).data); + System.out.println(node); + +} +} \ No newline at end of file diff --git a/group18/1057617027/src/com/coding/basic/Iterator.java b/group18/1057617027/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..40c0906495 --- /dev/null +++ b/group18/1057617027/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/group18/1057617027/src/com/coding/basic/LinkedList.java b/group18/1057617027/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..b4c3325814 --- /dev/null +++ b/group18/1057617027/src/com/coding/basic/LinkedList.java @@ -0,0 +1,145 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head = new Node(null,null); + private Node last; + private int size; + private static class Node{ + Object data; + Node next; + Node(Object data,Node next){ + this.data = data; + this.next = next; + } + } + public void add(Object o){ + if(size==0){ + Node node = new Node(o,null); + head = node; + last = node; + size++; + }else{ + Node node = new Node(o,null); + last.next = node; + last = node; + size++; + } + + } + public void add(int index , Object o){ + if(index>size){ + System.out.println(""+index+"ڵǰ"+size); + } + Node n = head; + Node n1 = head; + for(int i=0;i diff --git a/group18/1159828430/20170219/.project b/group18/1159828430/20170219/.project index da66c3a498..93c92379ce 100644 --- a/group18/1159828430/20170219/.project +++ b/group18/1159828430/20170219/.project @@ -1,17 +1,17 @@ - - - 20170219 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - + + + 20170219 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java index c8db730110..44f6b3402d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java +++ b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java @@ -1,3 +1,4 @@ + package com.coding.basic; import java.util.Arrays; @@ -99,8 +100,9 @@ public boolean isEmpty(){ return size == 0; } + //迭代器 public Iterator iterator(){ - return null; + return new ArrayListIterator(this); } //动态增加ArrayList大小 @@ -128,6 +130,35 @@ private void fastRemove(int index) { } elementData[--size] = null; } + + private class ArrayListIterator implements Iterator{ + private ArrayList list = null; + private int cursor = 0; + private int lastRet = -1; + + public ArrayListIterator(ArrayList list){ + this.list = list; + } + @Override + public boolean hasNext() { + return cursor != list.size; + } + + @Override + public Object next() { + lastRet = cursor; + Object o = list.get(lastRet); + cursor ++; + return o; + } + @Override + public void remove() { + list.remove(lastRet); + cursor = lastRet; + lastRet = -1; + } + + } } class testArrayList{ @@ -138,8 +169,16 @@ public static void main(String[] args) { } arrayList.add(5,15); arrayList.remove(11); + Iterator it = arrayList.iterator(); + while(it.hasNext()) { + Integer o = (Integer)it.next(); + if(o == 8){ + it.remove(); + } + } for (int i = 0; i < arrayList.size(); i++) { System.out.println("value is "+arrayList.get(i)); } + } } diff --git a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java index 0ce3f762ba..58064fdfae 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java +++ b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java @@ -1,3 +1,4 @@ + package com.coding.basic; /** * @author Hipple @@ -7,5 +8,5 @@ public interface Iterator { public boolean hasNext(); public Object next(); + public void remove(); -} \ No newline at end of file diff --git a/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java b/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java index 433715d5ad..4586f0549d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java +++ b/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java @@ -1,5 +1,6 @@ package com.coding.basic; +import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; /** @@ -88,7 +89,7 @@ public Object getFirst() { } public Iterator iterator(){ - return null; + return new LinkedListIterator(); } //头部增加节点 @@ -225,6 +226,41 @@ private boolean isPositionIndex(int index) { return index >= 0 && index <= size; } + //迭代器 + private class LinkedListIterator implements Iterator{ + private Node lastReturned = null; + private Node next; + private int nextIndex; + + public boolean hasNext() { + return nextIndex < size; + } + + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.data; + } + + public void remove() { + if (lastReturned == null) + throw new IllegalStateException(); + + Node lastNext = lastReturned.next; + unlink(lastReturned); + if (next == lastReturned) + next = lastNext; + else + nextIndex--; + lastReturned = null; + } + } + + //节点对象 private static class Node{ Object data; Node next; diff --git a/group18/1159828430/20170219/src/com/coding/basic/List.java b/group18/1159828430/20170219/src/com/coding/basic/List.java index 08e0f9e2d5..78674d202d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/List.java +++ b/group18/1159828430/20170219/src/com/coding/basic/List.java @@ -1,13 +1,13 @@ -package com.coding.basic; -/** - * @author Hipple - * @Time:2017年2月20日 下午8:52:08 - * @version 1.0 - */ -public interface List { - public boolean add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); +package com.coding.basic; +/** + * @author Hipple + * @Time:2017年2月20日 下午8:52:08 + * @version 1.0 + */ +public interface List { + public boolean add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); } \ No newline at end of file diff --git a/group18/1159828430/20170219/src/com/coding/basic/Queue.java b/group18/1159828430/20170219/src/com/coding/basic/Queue.java index e91779fdb2..a5de938d6d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/Queue.java +++ b/group18/1159828430/20170219/src/com/coding/basic/Queue.java @@ -1,31 +1,31 @@ -package com.coding.basic; -/** - * @author Hipple - * @Time:2017年2月23日 下午11:00:00 - * @version 1.0 - */ - -public class Queue { - private LinkedList elementData = new LinkedList(); - - public Queue(){ - - } - - public void enQueue(Object o){ - elementData.addLast(o); - } - - public Object deQueue(){ - elementData.getFirst(); - return null; - } - - public boolean isEmpty(){ - return elementData.isEmpty(); - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; +/** + * @author Hipple + * @Time:2017年2月23日 下午11:00:00 + * @version 1.0 + */ + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public Queue(){ + + } + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + elementData.getFirst(); + return null; + } + + public boolean isEmpty(){ + return elementData.isEmpty(); + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group18/1159828430/20170219/src/com/coding/basic/Stack.java b/group18/1159828430/20170219/src/com/coding/basic/Stack.java index ef5b637050..eae2f6637d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/Stack.java +++ b/group18/1159828430/20170219/src/com/coding/basic/Stack.java @@ -1,57 +1,57 @@ -package com.coding.basic; - -import java.util.EmptyStackException; - -/** - * @author Hipple - * @Time:2017年2月23日 下午10:59:39 - * @version 1.0 - */ -public class Stack { - private ArrayList elementData = new ArrayList(); - - public Stack(){ - - } - - //入栈 - public void push(Object o){ - elementData.add(o); - } - - //出栈 - public Object pop(){ - if (elementData.isEmpty()) { - throw new EmptyStackException(); - } - final Object o = peek(); - elementData.remove(o);//重新写根据对象remove - return o; - } - - public Object peek(){ - if (elementData.isEmpty()) { - throw new EmptyStackException(); - } - final Object o = elementData.get(elementData.size()-1); - return o; - } - public boolean isEmpty(){ - return size() == 0; - } - public int size(){ - return elementData.size(); - } -} -class TestStack { - public static void main(String[] args){ - Stack myStack=new Stack(); - myStack.push("a"); - myStack.push(2); - myStack.push("123"); - myStack.push("ahu"); - while(!myStack.isEmpty()){ - System.out.println(myStack.pop()); - } - } -} +package com.coding.basic; + +import java.util.EmptyStackException; + +/** + * @author Hipple + * @Time:2017年2月23日 下午10:59:39 + * @version 1.0 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public Stack(){ + + } + + //入栈 + public void push(Object o){ + elementData.add(o); + } + + //出栈 + public Object pop(){ + if (elementData.isEmpty()) { + throw new EmptyStackException(); + } + final Object o = peek(); + elementData.remove(o);//重新写根据对象remove + return o; + } + + public Object peek(){ + if (elementData.isEmpty()) { + throw new EmptyStackException(); + } + final Object o = elementData.get(elementData.size()-1); + return o; + } + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} +class TestStack { + public static void main(String[] args){ + Stack myStack=new Stack(); + myStack.push("a"); + myStack.push(2); + myStack.push("123"); + myStack.push("ahu"); + while(!myStack.isEmpty()){ + System.out.println(myStack.pop()); + } + } +} diff --git a/group18/1159828430/README.md b/group18/1159828430/README.md index 387ebebf6d..8ff2ffc95e 100644 --- a/group18/1159828430/README.md +++ b/group18/1159828430/README.md @@ -1,2 +1,2 @@ -# 2017编程提高群 -这里是1159828430 大连—书生 的代码提交区 +# 2017编程提高群 +这里是1159828430 大连—书生 的代码提交区 diff --git a/group18/1787597051/.classpath b/group18/1787597051/.classpath new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ b/group18/1787597051/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group18/1787597051/.gitignore b/group18/1787597051/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/1787597051/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/1787597051/.project b/group18/1787597051/.project new file mode 100644 index 0000000000..2abc06efd7 --- /dev/null +++ b/group18/1787597051/.project @@ -0,0 +1,17 @@ + + + 1787597051 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1787597051/.settings/org.eclipse.jdt.core.prefs b/group18/1787597051/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group18/1787597051/.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.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group18/1787597051/src/com/coding/basic/BinaryTreeNode.java b/group18/1787597051/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group18/1787597051/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/group18/1787597051/src/com/coding/basic/MyArrayList.java b/group18/1787597051/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..77b14150c1 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,75 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class MyArrayList implements MyList { + private final int GROW = 4; + private int size = 0; + + private Object[] elementData = new Object[4]; + + public void add(Object o) { + if (size > elementData.length - 1) { + elementData = Arrays.copyOf(elementData, elementData.length + GROW); + elementData[size] = o; + } else { + elementData[size] = o; + } + size++; + } + + public void add(int index, Object o) { + Object[] target = new Object[elementData.length - index]; + for (int x = index, y = 0; x < elementData.length; x++, y++) { + target[y] = elementData[x]; + } + elementData = Arrays.copyOf(elementData, elementData.length + 1); + size = index; + // elementData[index] = o; + elementData[size] = o; + size++; + for (int y = 0; y < target.length; y++) { + // add(target[y]); + elementData[size] = target[y]; + size++; + } + } + + public Object get(int index) { + return elementData[index]; + } + + public Object remove(int index) { + Object removeData = elementData[index]; + elementData[index] = null; + Object[] target = Arrays.copyOfRange(elementData, index + 1, elementData.length); + for (int x = index, y = 0; y < target.length; y++, x++) { + elementData[x] = target[y]; + } + size--; + return removeData; + } + + public int size() { + return size; + } + + public MyIteratorImpl iterator() { + return new MyIteratorImpl(); + } + + private class MyIteratorImpl implements MyIterator { + int index; + + public boolean hasNext() { + return index != size; + } + + public Object next() { + int i = index; + index = i + 1; + return elementData[i]; + } + + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyIterator.java b/group18/1787597051/src/com/coding/basic/MyIterator.java new file mode 100644 index 0000000000..59af236aab --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyIterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface MyIterator { + public abstract boolean hasNext(); + public abstract Object next(); +} diff --git a/group18/1787597051/src/com/coding/basic/MyLinkedList.java b/group18/1787597051/src/com/coding/basic/MyLinkedList.java new file mode 100644 index 0000000000..862a0b9f38 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyLinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic; + +public class MyLinkedList implements MyList { + private int size; + private Node head; + + public MyLinkedList() { + head = new Node(); + head.data = "ͷ"; + head.next = null; + } + + public void add(Object o) { + Node p = head; + while (p.next != null) { + p = p.next; + } + Node p3 = new Node(); + p3.data = o; + p.next = p3; + size++; + } + + public void add(int index, Object o) { + int num = 0; + Node p = head; + while (p.next != null) { + if (num == index) { + Node p2 = new Node(); + p2.data = o; + p2.next = p.next; + p.next = p2; + size++; + } + p = p.next; + num++; + } + } + + public Object get(int index) { + int num = 0; + Node p = head.next; + while (p != null) { + if (num == index) { + return p.data; + } + p = p.next; + num++; + } + return null; + } + + public Object remove(int index) { + int num = 0; + Node p = head; + while (p.next != null) { + if (num == index) { + Node p2 = p.next; + p.next = p.next.next; + size--; + return p2.data; + } + p = p.next; + num++; + } + return null; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node p = new Node(); + p.data = o; + p.next = head.next; + head.next = p; + size++; + } + + public void addLast(Object o) { + Node p = head; + while (p.next != null) { + p = p.next; + } + Node p2 = new Node(); + p2.data = o; + p.next = p2; + size++; + } + + public Object removeFirst() { + Node p = head; + if (p.next != null) { + Node p2 = head.next; + p.next = p.next.next; + size--; + return p2.data; + } + return null; + } + + public Object removeLast() { + Node p = head; + if (p.next != null) { + while (p.next.next != null) { + p = p.next; + } + Node p2 = new Node(); + p2 = p.next; + p.next = null; + size--; + return p2.data; + } + return null; + } + /* + * public Iterator iterator(){ return null; } + */ + + private static class Node { + Object data; + Node next; + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyList.java b/group18/1787597051/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..afb20940ea --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyList.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface MyList { + public abstract void add(Object o); + public abstract void add(int index, Object o); + public abstract Object get(int index); + public abstract Object remove(int index); + public abstract int size(); +} diff --git a/group18/1787597051/src/com/coding/basic/MyQueue.java b/group18/1787597051/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..3161f6b4e9 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyQueue.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class MyQueue { + private int size; + MyLinkedList mll = new MyLinkedList(); + public MyQueue() { + } + public void enQueue(Object o){ + mll.add(o); + size++; + } + + public Object deQueue(){ + size--; + return mll.removeFirst(); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group18/1787597051/src/com/coding/basic/MyStack.java b/group18/1787597051/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..36c9aaffa5 --- /dev/null +++ b/group18/1787597051/src/com/coding/basic/MyStack.java @@ -0,0 +1,33 @@ +package com.coding.basic; + +public class MyStack { + private MyArrayList elementData = new MyArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + if (elementData.size() > 0) { + Object data = elementData.get(elementData.size() - 1); + elementData.remove(elementData.size() - 1); + return data; + } + return null; + } + + public Object peek() { + if (elementData.size() > 0) { + return elementData.get(elementData.size() - 1); + } + return null; + } + + public boolean isEmpty() { + return elementData.size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group18/542330964/.classpath b/group18/542330964/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group18/542330964/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group18/542330964/.gitignore b/group18/542330964/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/542330964/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/542330964/.project b/group18/542330964/.project new file mode 100644 index 0000000000..3a99e1d0ad --- /dev/null +++ b/group18/542330964/.project @@ -0,0 +1,17 @@ + + + 542330964learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/542330964/src/basicstruct/ArrayList.java b/group18/542330964/src/basicstruct/ArrayList.java new file mode 100644 index 0000000000..b56c61c4d1 --- /dev/null +++ b/group18/542330964/src/basicstruct/ArrayList.java @@ -0,0 +1,80 @@ +package basicstruct; + + + +public class ArrayList implements List{ + + private int size = 0; + + private Object[] elementData ; + + private static final int DEFAULT_CAPACITY = 10; + + public ArrayList() { + elementData=new Object [DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if(initialCapacity>=0){ + elementData=new Object[initialCapacity]; + }else { + throw new IllegalArgumentException("initialCapacity"+ + initialCapacity+"不能为负数"); + } + } + + public void add(Object o){ + ensureCapacity(); + elementData[size++] = o; + } + public void add(int index, Object o){ + if(index<0||index>size){ + throw new ArrayIndexOutOfBoundsException("index:"+index); + } + ensureCapacity(); + System.arraycopy(elementData, index, elementData, index + 1,size - index); + elementData[index] = o; + size++; + } + + private void rangeCheck(int index) { + if(index<0||index>=size){ + throw new ArrayIndexOutOfBoundsException("index:"+index); + } + } + private void ensureCapacity() { + if(size == elementData.length) { + Object[] newArray = new Object[size * 2 + 1]; + System.arraycopy(elementData, 0, newArray, 0, elementData.length); + elementData = newArray; + } + } + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index){ + rangeCheck(index); + Object movedValue = elementData[index]; + //被删除元素后的元素数目 + int numMoved = size - index - 1; + //后面有元素 + if (numMoved > 0){ + System.arraycopy(elementData, index+1, elementData, index,numMoved); + } + //恰为最后一个元素 + size--; + elementData[size] = null; //垃圾回收 + return movedValue; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group18/542330964/src/basicstruct/BinaryTreeNode.java b/group18/542330964/src/basicstruct/BinaryTreeNode.java new file mode 100644 index 0000000000..5fba822d2f --- /dev/null +++ b/group18/542330964/src/basicstruct/BinaryTreeNode.java @@ -0,0 +1,31 @@ +package basicstruct; +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; + } + +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/Iterator.java b/group18/542330964/src/basicstruct/Iterator.java new file mode 100644 index 0000000000..f7a094dd14 --- /dev/null +++ b/group18/542330964/src/basicstruct/Iterator.java @@ -0,0 +1,8 @@ +package basicstruct; + +public interface Iterator { + public boolean hasNext(); + + public Object next(); + +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/LinkedList.java b/group18/542330964/src/basicstruct/LinkedList.java new file mode 100644 index 0000000000..b124e9f8b9 --- /dev/null +++ b/group18/542330964/src/basicstruct/LinkedList.java @@ -0,0 +1,175 @@ +package basicstruct; + +import java.util.NoSuchElementException; + + +public class LinkedList implements List { + + private Node head; + + private Node tail; + + private int size=0; + + public void add(Object o){ + addLast(o); + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index: "+index); + } + if (index == size) { + addLast(o); + } else { + Node temp = node(index); + final Node pred = temp.previous; + final Node newNode = new Node(o, temp, pred); + temp.previous = newNode; + if (pred == null){ + head = newNode; + } + else{ + pred.next = newNode; + } + size++; + } + } + + public Node node(int index) { + //二分法查找 + if (index < (size >> 1)) { + Node temp = head; + for (int i = 0; i < index; i++){ + temp = temp.next; + } + return temp; + } else { + Node temp = tail; + for (int i = size - 1; i > index; i--){ + temp = temp.previous; + } + return temp; + } + } + + public Object get(int index){ + if (index < 0 || index >=size) { + throw new IndexOutOfBoundsException("index: "+index); + } + return node(index).data; + } + + public Object remove(int index){ + if (index < 0 || index >=size) { + throw new IndexOutOfBoundsException("index: "+index); + } + return deleteElement(node(index)); + } + + private Object deleteElement(Node node) { + Object element = node.data; + Node next = node.next; + Node prev = node.previous; + if (prev == null) { + head = next; + }else{ + prev.next = next; + node.previous = null; + } + if(next == null) { + tail = prev; + }else { + next.previous = prev; + node.next = null; + } + node.data = null; + size--; + return element; + } + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node h = head; + Node newNode = new Node(o, h, null); + head = newNode; + if (h == null){ + tail = newNode; + }else{ + h.previous = newNode; + } + size++; + } + + public void addLast(Object o){ + Node t = tail; + Node node = new Node(o, null, t); + tail = node; + if (t == null) { + head = node; + } else { + t.next = node; + } + size++; + } + + public Object removeFirst(){ + final Node h = head; + if (h == null) { + throw new NoSuchElementException("No such element"); + } + final Object element = h.data; + final Node next = h.next; + h.data = null; + h.next = null; + head = next; + if (next == null) { + tail = null; + } else { + next.previous = null; + } + size--; + return element; + } + + public Object removeLast(){ + Node t = tail; + if (t == null){ + throw new NoSuchElementException("No such element"); + } + final Object element = t.data; + final Node prev = t.previous; + t.data = null; + t.previous = null; + tail = prev; + if (prev == null) { + head = null; + } else { + prev.next = null; + } + size--; + return element; + } + public boolean isEmpty(){ + return size==0; + } + public Iterator iterator(){ + return null; + } + + private static class Node{ + Object data; + Node next; + Node previous; + public Node() { + super(); + } + public Node(Object data, Node next, Node previous) { + super(); + this.data = data; + this.next = next; + this.previous = previous; + } + } +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/List.java b/group18/542330964/src/basicstruct/List.java new file mode 100644 index 0000000000..abe8ec3613 --- /dev/null +++ b/group18/542330964/src/basicstruct/List.java @@ -0,0 +1,13 @@ +package basicstruct; + +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(); +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/Queue.java b/group18/542330964/src/basicstruct/Queue.java new file mode 100644 index 0000000000..5cb8ff6f12 --- /dev/null +++ b/group18/542330964/src/basicstruct/Queue.java @@ -0,0 +1,22 @@ +package basicstruct; + +public class Queue { + + private LinkedList queue = new LinkedList(); + //进队列 + public void enQueue(Object o) { + queue.addLast(o); + } + //出队列 + public Object deQueue() { + return queue.removeFirst(); + } + + public int size() { + return queue.size(); + } + + public boolean isEmpty() { + return queue.isEmpty(); + } +} \ No newline at end of file diff --git a/group18/542330964/src/basicstruct/Stack.java b/group18/542330964/src/basicstruct/Stack.java new file mode 100644 index 0000000000..41f32af82f --- /dev/null +++ b/group18/542330964/src/basicstruct/Stack.java @@ -0,0 +1,24 @@ +package basicstruct; + +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(); + } +} \ No newline at end of file diff --git a/group18/542330964/src/junittest/ArraylistTest.java b/group18/542330964/src/junittest/ArraylistTest.java new file mode 100644 index 0000000000..bfaa41a4b0 --- /dev/null +++ b/group18/542330964/src/junittest/ArraylistTest.java @@ -0,0 +1,52 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.ArrayList; + +public class ArraylistTest { + + + ArrayList list = new ArrayList(); + @Before + public void setUp() throws Exception { + list.add(1); + list.add(1,"boy next door"); + list.add(222); + list.add("333"); + list.add(4,"444"); + list.add(1232); + list.add("555"); + } + + + @Test + public void testAdd() { + System.out.println(list.get(0)); + System.out.println(list.get(3)); + System.out.println(list.get(5)); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + list.remove(0); + list.remove(5); + System.out.println(list.get(0)); + System.out.println(list.size()); +// System.out.println(list.get(5)); + } + + @Test + public void testSize() { + System.out.println(list.size()); + } + +} diff --git a/group18/542330964/src/junittest/LinkedListTest.java b/group18/542330964/src/junittest/LinkedListTest.java new file mode 100644 index 0000000000..8572b7a524 --- /dev/null +++ b/group18/542330964/src/junittest/LinkedListTest.java @@ -0,0 +1,65 @@ +package junittest; + +import java.util.Date; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.LinkedList; + +public class LinkedListTest { + + LinkedList l = new LinkedList(); + @Before + public void setUp() throws Exception { + l.add(1); + l.add("2"); + l.add(new Date()); + l.add(22); + l.add(33); + l.add(1, 3); + } + + @Test + public void testAddObject() { + System.out.println(l.get(1)); + System.out.println(l.get(3)); + System.out.println(l.get(200)); + } + + @Test + public void testRemove() { + l.remove(1); + System.out.println(l.get(1)); + } + + @Test + public void testSize() { + System.out.println(l.size()); + } + + @Test + public void testAddFirst() { + l.addFirst(0); + System.out.println(l.get(0)); + } + + @Test + public void testAddLast() { + l.addLast(999); + System.out.println(l.get(l.size()-1)); + } + + @Test + public void testRemoveFirst() { + l.removeFirst(); + System.out.println(l.get(0)); + } + + @Test + public void testRemoveLast() { + l.removeLast(); + System.out.println(l.get(l.size()-1)); + } + +} diff --git a/group18/542330964/src/junittest/QueueTest.java b/group18/542330964/src/junittest/QueueTest.java new file mode 100644 index 0000000000..a3488563e1 --- /dev/null +++ b/group18/542330964/src/junittest/QueueTest.java @@ -0,0 +1,33 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.Queue; + +public class QueueTest { + + Queue q = new Queue(); + @Before + public void setUp() throws Exception { + q.enQueue(11); + q.enQueue(22); + q.enQueue(33); + q.enQueue(44); + q.enQueue(55); + } + + @Test + public void testDeQueue() { + q.deQueue(); + System.out.println(q.size()); + } + + @Test + public void testIsEmpty() { + System.out.println(q.isEmpty()); + } + +} diff --git a/group18/542330964/src/junittest/StackTest.java b/group18/542330964/src/junittest/StackTest.java new file mode 100644 index 0000000000..96372bb957 --- /dev/null +++ b/group18/542330964/src/junittest/StackTest.java @@ -0,0 +1,39 @@ +package junittest; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import basicstruct.Stack; + +public class StackTest { + + Stack s= new Stack(); + @Before + public void setUp() throws Exception { + s.push(11); + s.push(22); + s.push(33); + s.push("44"); + s.push(55); + } + + @Test + public void testPop() { + System.out.println(s.peek()); + s.pop(); + System.out.println(s.peek()); + } + + @Test + public void testIsEmpty() { + System.out.println(s.isEmpty()); + } + + @Test + public void testSize() { + System.out.println(s.size()); + } + +} diff --git a/group18/564673292/com/coding/basic/ArrayList.java b/group18/564673292/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..4bb16148b7 --- /dev/null +++ b/group18/564673292/com/coding/basic/ArrayList.java @@ -0,0 +1,92 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterable{ + private E[] array; + private int lastIndex; + private int length; + //Constructor + @SuppressWarnings("unchecked") + public ArrayList(){ + length = 10; + array = (E[])new Object[length]; + lastIndex = 0; + } + + public void add(E object){ + if(lastIndex == length){ + this.grow(); + } + array[lastIndex] = object; + lastIndex++; + } + + @SuppressWarnings("unchecked") + private void grow(){ + E[] tempArray = (E[])new Object[length + 10]; + System.arraycopy(array, 0, tempArray, 0, length); + array = tempArray; + length = length + 10; + } + + public void insert(int index, E o) { + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + for (int i = lastIndex; i > index; i--) { + array[i] = array[i - 1]; + } + array[index] = o; + length++; + } + + public E get(int index) { + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + return array[index]; + } + + public E remove(int index){ + if(index > lastIndex - 1) throw new IndexOutOfBoundsException(); + E removed = array[index]; + for (int i = index; i < lastIndex - 1; i++) { + array[i] = array[i + 1]; + } + lastIndex--; + array[lastIndex] = null; + return removed; + } + + public int size(){ + return lastIndex; + } + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int itrCurIndex; + private ArrayList arrayList; + // constructor + public Itr(ArrayList arrayList){ + this.arrayList = arrayList; + itrCurIndex = -1; + } + + public boolean hasNext(){ + return (itrCurIndex + 1) > lastIndex - 1 ? false: true; + } + + @SuppressWarnings("unchecked") + public E next(){ + if(this.hasNext()){ + return (E)this.arrayList.get(++itrCurIndex); + }else{ + itrCurIndex = -1; + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(){ + return (E)this.arrayList.remove(itrCurIndex); + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/BinaryTreeNode.java b/group18/564673292/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..713e8e4409 --- /dev/null +++ b/group18/564673292/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,72 @@ +// This is a node in a customized binaryTree. The tree have 2 extra features comparing to general binary trees. +// 1. The data of each node are in number class. +// 2. The left child node has a smaller number data than root node, and the right child node has a larger number data that root node. + +package com.coding.basic; + +public class BinaryTreeNode { + + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + // constructor + public BinaryTreeNode(E data){ + this.data = data; + } + + public E getData() { + return this.data; + } + + public void setData(E data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return this.left; + } + + public boolean setLeft(BinaryTreeNode left) { + if(this.compareWithRoot(left.data) >= 0 || this.left != null){ + System.err.println("The left node data should be smaller than root node."); + return false; + }else{ + this.left = left; + return true; + } + } + + public BinaryTreeNode getRight() { + return this.right; + } + + public boolean setRight(BinaryTreeNode right) { + if(this.compareWithRoot(right.data) <= 0 || this.right != null) { + System.err.println("The right node data should be larger than root node."); + + return false; + }else{ + this.right = right; + return true; + } + } + + private int compareWithRoot(E o){ + return (Integer)o - (Integer)this.getData(); + } + + @SuppressWarnings("unchecked") + public void insert(E o){ + BinaryTreeNode newNode = new BinaryTreeNode(o); + if(!this.setLeft(newNode)){ + if(!this.setRight(newNode)){ + if(this.left.getData() == o){ + this.right.insert(o); + }else{ + this.left.insert(o); + } + } + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Iterable.java b/group18/564673292/com/coding/basic/Iterable.java new file mode 100644 index 0000000000..e80308012a --- /dev/null +++ b/group18/564673292/com/coding/basic/Iterable.java @@ -0,0 +1,5 @@ +package com.coding.basic; + +public interface Iterable{ + public Iterator iterator(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Iterator.java b/group18/564673292/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..27d475265e --- /dev/null +++ b/group18/564673292/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public E next(); + public E remove(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/LinkedList.java b/group18/564673292/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..19b12d12da --- /dev/null +++ b/group18/564673292/com/coding/basic/LinkedList.java @@ -0,0 +1,123 @@ +package com.coding.basic; + +public class LinkedList implements List, Iterable { + + private Node head; + private Node last; + private int length; + + private class Node{ + public E data; + public Node next; + + // constructor + private Node(E o, Node n){ + data = o; + next = n; + } + } + + // constructor + public LinkedList(){ + head = new Node(null, null); + last = head; + length = 0; + } + + public void add(E o){ + Node newNode = new Node(o, null); + last.next = newNode; + last = newNode; + length++; + } + + public void insert(int index , E o){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node prevNode = this.getNode(index - 1); + Node nextNode = this.getNode(index); + Node nodeToInsert = new Node(o,nextNode); + prevNode.next = nodeToInsert; + length++; + } + + private Node getNode(int index){ + int count = 0; + Node currentNode = head; + while(currentNode.next != null && count <= index){ + currentNode = currentNode.next; + count++; + } + return currentNode; + } + + public E get(int index){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node nodeAtIndex = this.getNode(index); + return nodeAtIndex.data; + } + + public E remove(int index){ + if(index > length - 1) throw new IndexOutOfBoundsException(); + Node nodeToRemove = this.getNode(index); + Node prevNode = this.getNode(index - 1); + Node nextNode = this.getNode(index + 1); + prevNode.next = nextNode; + E removedData = nodeToRemove.data; + nodeToRemove = null; + length--; + return removedData; + } + + public int size(){ + return length; + } + + public void addFirst(E o){ + this.insert(0, o); + } + public void addLast(E o){ + this.add(o); + + } + public E removeFirst(){ + return this.remove(0); + } + public E removeLast(){ + return this.remove(length - 1); + } + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int itrCurIndex; + private Node currentNode; + private LinkedList linkedList; + + public Itr(LinkedList linkedList){ + itrCurIndex = -1; + currentNode = head; + this.linkedList = linkedList; + } + + public boolean hasNext(){ + return (itrCurIndex + 1) > length - 1 ? false: true; + } + + @SuppressWarnings("unchecked") + public E next(){ + if(this.hasNext()){ + return (E)this.linkedList.get(++itrCurIndex); + }else{ + itrCurIndex = -1; + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(){ + return (E)this.linkedList.remove(itrCurIndex); + } + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/List.java b/group18/564673292/com/coding/basic/List.java new file mode 100644 index 0000000000..04a7ac992e --- /dev/null +++ b/group18/564673292/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(E o); + public void insert(int index, E o); + public E get(int index); + public E remove(int index); + public int size(); +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Queue.java b/group18/564673292/com/coding/basic/Queue.java new file mode 100644 index 0000000000..b40f06afc8 --- /dev/null +++ b/group18/564673292/com/coding/basic/Queue.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +public class Queue { + private LinkedList linkedList; + + // constructor + public Queue(){ + linkedList = new LinkedList(); + } + + public void enQueue(E o){ + linkedList.addLast(o); + } + + public E deQueue(){ + return linkedList.removeFirst(); + } + + public E peek(){ + return linkedList.get(0); + } + + public boolean isEmpty(){ + return linkedList.size() == 0 ? true : false; + } + + public int size(){ + return linkedList.size(); + } +} \ No newline at end of file diff --git a/group18/564673292/com/coding/basic/Stack.java b/group18/564673292/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b1b129fc40 --- /dev/null +++ b/group18/564673292/com/coding/basic/Stack.java @@ -0,0 +1,50 @@ +package com.coding.basic; + +public class Stack{ + private ArrayList arrayList; + + // constructor + public Stack(){ + arrayList = new ArrayList(); + } + + public void push(E o){ + arrayList.add(o); + } + + public E pop(){ + return arrayList.remove(arrayList.size() - 1); + } + + public E peek(){ + return arrayList.get(arrayList.size() - 1); + } + + public boolean isEmpty(){ + return arrayList.size() == 0 ? true: false; + } + + public int size(){ + return arrayList.size(); + } + + // public Iterator iterator(){ + // return new Itr(); + // } + + // private class Itr implements Iterator{ + // Iterator arrayListItr = arrayList.iterator(); + // public boolean hasNext(){ + // return arrayListItr.hasNext(); + // } + + // public E next(){ + // return arrayListItr.next(); + // } + + // @Override // Stack iterator can only remove the last element + // public E remove(){ + // return arrayList.pop(); + // } + // } +} \ No newline at end of file diff --git "a/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" "b/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" new file mode 100644 index 0000000000..d2a21619bf --- /dev/null +++ "b/group18/935542673/Blog/CPU\357\274\214\345\206\205\345\255\230\357\274\214\347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\273\245\345\217\212\345\256\203\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.md" @@ -0,0 +1,41 @@ +## CPU,内存,硬盘,指令以及它们之间的关系(2017.2.25) + +原文:[CPU,内存,硬盘,指令以及它们之间的关系](http://ikookblog.com/2017/02/25/cpu-ram-detailed/) + +> 本人对于CPU、内存、硬盘以及指令等一系列计算机核心组件理解甚浅。并且对其也不是很来感,不过身为一名软件专业学生以及未来的程序猿,还是硬着头皮研究研究。以下是对其学习的一个总结吧算是,有什么不正确的地方还请指出,不喜勿喷。 + +相信大家都知道计算机是由控制器、运算器、存储器、输入设备、输出设备五大部分组成。控制器 + 运算器组成了CPU,存储器即内存,当然硬盘也属于存储器,不过硬盘和内存存储形式有所不同,详细见下。 + +说明:点击以下标题查看维基百科详解 + +### [CPU](https://zh.wikipedia.org/wiki/%E4%B8%AD%E5%A4%AE%E5%A4%84%E7%90%86%E5%99%A8) + +CPU即中央处理器(Central Processing Unit)。 + +它是计算机的主要设备之一,可以是说是计算机最重要的组成部分。常听到有人说CPU是"计算机的大脑",但我觉得这句话是不完全正确的。为什么这么说呢,CPU没有存储能力,只有为数不多的寄存器能临时存储一点东西,人可是有存储能力的,像最强大脑上有些人更拥有超强的记忆力。所以在存储能力这方面来讲,我觉得把CPU比作”大脑“的说法不太正确。就像刘欣前辈在其公号上讲解[CPU](http://mp.weixin.qq.com/s?__biz=MzAxOTc0NzExNg==&mid=2665513017&idx=1&sn=5550ee714abd36d0b580713f673e670b&scene=21#wechat_redirect)说的那样,"上帝为你关闭了一扇门,就一定会为你打开一扇窗",CPU虽然“脑容量”很小,但是它拥有超强的运算力。拿内存和硬盘来说,CPU比内存要快100倍,比硬盘快1000多万倍。这种运算速度可是人望尘莫及的了。所以在运算力这方面讲,又可以把CPU比作“超强的大脑”。 + +它负责处理、运算计算机内部的所有数据。 + +### [内存](https://zh.wikipedia.org/wiki/%E9%9A%8F%E6%9C%BA%E5%AD%98%E5%8F%96%E5%AD%98%E5%82%A8%E5%99%A8) + +内存即RAM,随机存取存储器(Random Access Memory)。 + +内存是计算机的主存,主存(Main memory)即电脑内部最重要的存储器,是与CPU直接交换数据的内部存储器。它用来加载各种各样的数据和程序以供CPU直接运行与运用。它是可以随时读写的,并且速度也很快,仅仅比CPU慢100倍。它通常作为操作系统或其他正在运行中的程序的临时数据存储媒介。通俗点说,内存就是用来临时存储数据,用来给CPU提供CPU要处理的东西。但是内存不会长期保存数据,只是临时存储,程序和数据处理完后就释放空间。 + +### [硬盘](https://zh.wikipedia.org/wiki/%E7%A1%AC%E7%9B%98) + +硬盘即HDD(Hard Disk Drive)。 + +硬盘是计算机上使用坚硬的旋转盘片为基础的非挥发性存储设备,它在平整的磁性表面存储和检索数字数据,信息通过离磁性表面很近的磁头,由电磁流来改变极性方式被电磁流写到磁盘上,信息可以通过相反的方式读取,例如读头经过纪录数据的上方时磁场导致线圈中电气信号的改变。硬盘的读写是采用随机存取的方式,因此可以以任意顺序读取硬盘中的数据。以上来自维基百科。各种专业名词,我相信你已经看厌倦了快。说白了,硬盘就是存东西的,长期存,也就是具有记忆力。不像CPU和内存,“一觉醒来就忘了以前的事情”,所有的数据全都清空。硬盘会长期存储数据,只要是不人为删除,不出现硬件故障,东西就不会丢。 + +### [指令](https://zh.wikipedia.org/wiki/%E6%8C%87%E4%BB%A4) + +指令,怎么说呢,指令就是任何可执行程序的元素的表述。指令一般会包含一个操作码和零到多个操作数。操作码指定了要进行什么样的操作。操作数可能指定了参与操作的寄存器、内存地址或立即数,它可能还会包含寻址方式,寻址方式确定操作数的含义。说白了,指令就是CPU的命令,CPU通过寄存器中的指令来进行数据的操作。 + +另外,指令一般有四种:加载、存储、操作和跳转。 + +### 它们之间的关系 + +CPU从内存或缓存中取出指令,放入指令寄存器,并对指令译码进行分解,进而对数据进行处理。这么说吧,计算机中所有的程序运行都是在内存中进行的,因此内存对计算机性能的影响非常大。数据由传输速度较慢的硬盘通过内存传送到CPU进行处理。不过内存是带电存储的(断电数据就会消失),而且容量十分有限,所以要长时间储存程序或数据就需要使用硬盘。 + +所以计算机处理数据大概就通过以上几个部分:数据(硬盘)——>内存——>CPU——>CPU通过指令处理数据 \ No newline at end of file diff --git a/group18/935542673/Blog/README.md b/group18/935542673/Blog/README.md new file mode 100644 index 0000000000..0533a45f85 --- /dev/null +++ b/group18/935542673/Blog/README.md @@ -0,0 +1,4 @@ +## 2017编程提高社群博客 + +2017.2.25 [CPU,内存,硬盘,指令以及它们之间的关系](https://github.com/china-kook/coding2017/blob/master/group18/935542673/Blog/CPU%EF%BC%8C%E5%86%85%E5%AD%98%EF%BC%8C%E7%A1%AC%E7%9B%98%EF%BC%8C%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E5%AE%83%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB.md) + diff --git a/group18/935542673/Coding/.classpath b/group18/935542673/Coding/20170219/.classpath similarity index 100% rename from group18/935542673/Coding/.classpath rename to group18/935542673/Coding/20170219/.classpath diff --git a/group18/935542673/Coding/20170219/.gitignore b/group18/935542673/Coding/20170219/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/935542673/Coding/20170219/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/935542673/Coding/.project b/group18/935542673/Coding/20170219/.project similarity index 100% rename from group18/935542673/Coding/.project rename to group18/935542673/Coding/20170219/.project diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md b/group18/935542673/Coding/20170219/README.md similarity index 82% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md rename to group18/935542673/Coding/20170219/README.md index a565fb7148..a001a83d24 100644 --- a/group18/935542673/Coding/src/com/ikook/basic_data_structure/README.md +++ b/group18/935542673/Coding/20170219/README.md @@ -1,8 +1,10 @@ ## 2017编程提高社群作业:实现基本的数据结构(2017.2.19) +#### [所有基本数据结构实现类及接口](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure) + 1. 实现了ArrayList、LinkedList、Queue(依靠LinkedList实现)、Stack(依靠ArrayList实现) - 1.1 ArrayList实现了一下方法: + 1.1 ArrayList实现了以下方法: ``` (1)add(Object): 添加元素到集合; @@ -16,7 +18,7 @@ (9)iterator(): 返回一个迭代器的实现类。 ``` - 1.2 LinkedList实现了一下方法: + 1.2 LinkedList实现以下方法: ``` (1)addFirst(Object): 在链表头部插入新的元素; @@ -36,7 +38,7 @@ (15)iterator(): 返回一个迭代器的实现类。 ``` - 1.3 Queue实现了一下方法: + 1.3 Queue实现了以下方法: ``` (1)enQueue(Object): 入队操作; @@ -45,7 +47,7 @@ (4)isEmpty(): 判断队列是否为空。 ``` - 1.4 Stack实现了一下方法: + 1.4 Stack实现了以下方法: ``` (1)push(Object):入栈操作; @@ -58,7 +60,7 @@ 2. 实现了BinarySearchTree、Iterator接口 - 2.1 BinarySearchTree实现了一下方法: + 2.1 BinarySearchTree实现了以下方法: ``` (1)insert(int):插入操作; @@ -67,7 +69,7 @@ (4)inorderTraverse(Node):遍历操作,采用中序遍历。 ``` - 2.2 Iterator定义了一下方法: + 2.2 Iterator定义了以下方法: ``` (1)hasNext():判断是否有元素没有被遍历; @@ -76,8 +78,6 @@ ``` ​ -3. 对应以上类做了单元测试 + #### [所有基本数据结构测试类](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure) 说明:由于作业以实现基本的数据结构为主,则在实现单元测试时,只对正常情况进行了测试,一些异常情况并进行编写测试用例。 - - 点击查看: [unit test](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/junit/com/ikook/basic_data_structure) \ No newline at end of file diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyArrayListTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyArrayListTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyArrayListTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyBinarySearchTreeTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyLinkedListTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyLinkedListTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyLinkedListTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyQueueTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyQueueTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyQueueTest.java diff --git a/group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java b/group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyStackTest.java similarity index 100% rename from group18/935542673/Coding/junit/com/ikook/basic_data_structure/MyStackTest.java rename to group18/935542673/Coding/20170219/junit/com/ikook/basic_data_structure/MyStackTest.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java similarity index 95% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java index 6891a76879..79ec2865c3 100644 --- a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyArrayList.java +++ b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyArrayList.java @@ -51,7 +51,9 @@ public void add(Object obj) { * @param obj */ public void add(int index, Object obj) { - rangeCheck(index); + if (index > size || index < 0) + throw new IndexOutOfBoundsException("索引越界异常"); + ensureCapacity(); System.arraycopy(elementData, index, elementData, index + 1, size-index); @@ -151,11 +153,7 @@ private void ensureCapacity() { */ private void rangeCheck(int index) { if(index < 0 || index >= size) { - try { - throw new Exception("索引异常"); - } catch (Exception e) { - e.printStackTrace(); - } + throw new IndexOutOfBoundsException("索引越界异常"); } } diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyBinarySearchTree.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyBinarySearchTree.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyBinarySearchTree.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyIterator.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyIterator.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyIterator.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyLinkedList.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyLinkedList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyLinkedList.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyList.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyList.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyList.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyQueue.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyQueue.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyQueue.java diff --git a/group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java b/group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyStack.java similarity index 100% rename from group18/935542673/Coding/src/com/ikook/basic_data_structure/MyStack.java rename to group18/935542673/Coding/20170219/src/com/ikook/basic_data_structure/MyStack.java diff --git a/group18/935542673/Coding/README.md b/group18/935542673/Coding/README.md new file mode 100644 index 0000000000..77e4706a07 --- /dev/null +++ b/group18/935542673/Coding/README.md @@ -0,0 +1,3 @@ +## 2017编程提高社群Coding + +2017.2.19 [实现基本的数据结构](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding/20170219) \ No newline at end of file diff --git "a/group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" "b/group18/935542673/Datum/\345\205\263\344\272\216 .metadata \347\233\256\345\275\225\345\222\214 .gitignore \344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" similarity index 100% rename from "group18/935542673/Datum/\345\205\263\344\272\216.metadata\347\233\256\345\275\225\345\222\214.gitignore\344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" rename to "group18/935542673/Datum/\345\205\263\344\272\216 .metadata \347\233\256\345\275\225\345\222\214 .gitignore \344\275\277\347\224\250\347\232\204\351\227\256\351\242\230.docx" diff --git a/group18/935542673/README.md b/group18/935542673/README.md new file mode 100644 index 0000000000..d98a632c03 --- /dev/null +++ b/group18/935542673/README.md @@ -0,0 +1,15 @@ +## 2017编程提高社群 + +群昵称:青岛-ikook + +QQ号码:935542673 + +------------------------------------------------------------------------------------------------------------------------------ + +**代码库结构说明:** + +[Blog](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Blog) 目录为本人在社群中所编写的所有博客,可在 README.md 中直接点击某篇查看 + +[Coding](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Coding) 目录为本人在社群中所编写的所有代码,可在 README.md 中直接点击查看 + +[Datum](https://github.com/china-kook/coding2017/tree/master/group18/935542673/Datum) 目录为本人在社群中产出的所有资料 \ No newline at end of file diff --git a/group19/1294642551/.gitignore b/group19/1294642551/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group19/1294642551/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group19/1294642551/readme.txt b/group19/1294642551/readme.txt new file mode 100644 index 0000000000..747e8550dc --- /dev/null +++ b/group19/1294642551/readme.txt @@ -0,0 +1 @@ +JaneZhou91's files \ No newline at end of file diff --git a/group19/1294642551/src/arrayTests/ArrayUtil.java b/group19/1294642551/src/arrayTests/ArrayUtil.java new file mode 100644 index 0000000000..a9f23394e9 --- /dev/null +++ b/group19/1294642551/src/arrayTests/ArrayUtil.java @@ -0,0 +1,270 @@ +package arrayTests; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.TreeSet; + +public class ArrayUtil { + + /** + * һa , Ըֵû + 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] + a = [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * @param origin + * @return + */ + public int[] reverseArray(int[] origin){ + + int len = origin.length; + int[] arr = new int[len]; + + for(int i = 0; i < len; i++) + { + arr[i] = origin[ len -1 - i]; + } + + return arr; + } + + /** + * µһ飺 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * ҪֵΪ0ȥΪ0ֵһµ飬ɵΪ + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + + ArrayList al = new ArrayList(); + + int len = oldArray.length; + for(int i = 0; i < len; i++) + { + if(oldArray[i] != 0) + { + al.add(oldArray[i]); + } + } + + int arrLen = al.size(); + int[] arr = new int[arrLen]; + for(int i = 0; i < arrLen; i++) + { + arr[i] = al.get(i); + } + + return arr; + + + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + TreeSet tr = new TreeSet(); + for(int i = 0; i < array1.length; i++) + { + tr.add(array1[i]); + } + for(int j = 0; j < array2.length; j++) + { + tr.add(array2[j]); + } + + int arrLen = tr.size(); + int[] arr = new int[arrLen]; + int index = 0; + + Iterator it = tr.iterator(); + while(it.hasNext()) + { + arr[index] = (int) it.next(); + index++; + } + + return arr; + } + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ + * oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + + int len = oldArray.length; + int arrLen = len + size; + int[] arr = new int[arrLen]; + + for(int i = 0; i < arrLen; i++) + { + if (i < len) + arr[i] = oldArray[i]; + else + arr[i] = 0; + } + + return arr; + + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + + ArrayList al = new ArrayList(); + int first = 1; + int second = 1; + int value = 0; + if(max >= 2) + { + al.add(first); + al.add(second); + } + do + { + value = first + second; + if(value < max) + { + al.add(value); + first = second; + second = value; + } + }while(value < max); + + int arrLen = al.size(); + int[] arr = new int[arrLen]; + for(int i = 0; i < arrLen; i++) + { + arr[i] = al.get(i); + } + + return arr; + + } + + /** + * Сڸֵmax + * max = 23, صΪ[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + ArrayList al = new ArrayList(); + if(max > 2) + al.add(2); + + int value = 3; + while(value < max) + { + int flag = 1; + for(int i = 2; i < value; i++) + { + if(value % i == 0) + { + flag = 0; + break; + } + } + + if (flag == 1) + al.add(value); + + value++; + } + + int arrLen = al.size(); + int[] arr = new int[arrLen]; + for(int i = 0; i < arrLen; i++) + { + arr[i] = al.get(i); + } + + return arr; + + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 + * һֵmax һ飬 Сmax + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + ArrayList al = new ArrayList(); + for(int i = 1; i < max; i++) + { + if (isPerfectNumber(i)) + al.add(i); + } + + int arrLen = al.size(); + int[] arr = new int[arrLen]; + for(int i = 0; i < arrLen; i++) + { + arr[i] = al.get(i); + } + + return arr; + } + + public boolean isPerfectNumber(int number) + { + ArrayList al = new ArrayList(); + + for(int i = 1; i < number; i++) + { + if(number % i == 0) + al.add(i); + } + + int value = 0; + for(int j = 0; j < al.size(); j++) + { + value = value + al.get(j); + } + + return value == number; + } + + /** + * seperator array + * array= [3,8,9], seperator = "-" + * 򷵻ֵΪ"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + String str = ""; + int len = array.length; + for(int i = 0; i < len-1; i++) + { + str = str + array[i] + seperator; + } + str = str + array[len-1]; + + return str; + } + + + +} diff --git a/group19/1294642551/src/data_Structures/ArrayList.java b/group19/1294642551/src/data_Structures/ArrayList.java new file mode 100644 index 0000000000..fe0a8c1557 --- /dev/null +++ b/group19/1294642551/src/data_Structures/ArrayList.java @@ -0,0 +1,57 @@ +package data_Structures; + + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + + public void add(Object o){ + + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + + int temp = size; + while(temp > index) + { + elementData[temp] = elementData[temp-1]; + temp--; + } + + elementData[index] = o; + + size++; + + + } + + public Object get(int index){ + return elementData[index]; + } + + public Object remove(int index){ + + int temp = index; + while(temp < size-1) + { + elementData[temp] = elementData[temp+1]; + temp++; + } + size--; + + return elementData[index]; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group19/1294642551/src/data_Structures/LinkedList.java b/group19/1294642551/src/data_Structures/LinkedList.java new file mode 100644 index 0000000000..5a1efb6d71 --- /dev/null +++ b/group19/1294642551/src/data_Structures/LinkedList.java @@ -0,0 +1,125 @@ +package data_Structures; + + +public class LinkedList implements List { + + private Node head; + private int size; + + LinkedList() + { + size = 0; + } + + public void add(Object o){ + + Node newNode = new Node(o); + + if(head == null) + { + head = newNode; + } + else + { + Node tempNode = head; + while(tempNode.next != null) + { + tempNode = tempNode.next; + } + tempNode.next = newNode; + + } + + size++; + } + + public void add(int index , Object o){ + + Node newNode = new Node(o, getNode(index)); + getNode(index-1).next = newNode; + + size++; + + } + + public Node getNode(int index) + { + Node tempNode = head; + int i = 0; + while(i < index) + { + tempNode = tempNode.next; + i++; + } + + return tempNode; + } + + + + public Object get(int index){ + return getNode(index).data; + } + public Object remove(int index){ + + Node tempNode = getNode(index); + getNode(index - 1).next = getNode(index+1); + tempNode.next = null; + tempNode.data = null; + size--; + return getNode(index).data; + + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + + Node tempNode = head; + head = new Node(o); + head.next = tempNode; + + size++; + } + public void addLast(Object o){ + add(o); + + } + public Object removeFirst(){ + + Node tempNode = head; + head = getNode(1); + size--; + + return tempNode.data; + } + public Object removeLast(){ + getNode(size-1).next = null; + size--; + return getNode(size).data; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + Node(Object data, Node next ) + { + this.data = data; + this.next = next; + } + + Node(Object data) + { + this.data = data; + this.next = null; + } + + } +} diff --git a/group19/1294642551/src/data_Structures/List.java b/group19/1294642551/src/data_Structures/List.java new file mode 100644 index 0000000000..2b7b51d97f --- /dev/null +++ b/group19/1294642551/src/data_Structures/List.java @@ -0,0 +1,9 @@ +package data_Structures; + +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/group19/1294642551/src/data_Structures/Queue.java b/group19/1294642551/src/data_Structures/Queue.java new file mode 100644 index 0000000000..0926e3a31c --- /dev/null +++ b/group19/1294642551/src/data_Structures/Queue.java @@ -0,0 +1,28 @@ +package data_Structures; + +public class Queue { + + private LinkedList ll; + + Queue() + { + ll = new LinkedList(); + } + + public void enQueue(Object o){ + ll.add(o);; + } + + public Object deQueue(){ + return ll.removeFirst(); + } + + public boolean isEmpty(){ + int size = ll.size(); + return (size == 0); + } + + public int size(){ + return ll.size(); + } +} diff --git a/group19/1294642551/src/data_Structures/Stack.java b/group19/1294642551/src/data_Structures/Stack.java new file mode 100644 index 0000000000..575e451451 --- /dev/null +++ b/group19/1294642551/src/data_Structures/Stack.java @@ -0,0 +1,29 @@ +package data_Structures; + +public class Stack { +// private ArrayList elementData = new ArrayList(); + + private LinkedList ll; + Stack() + { + ll = new LinkedList(); + } + + public void push(Object o){ + ll.addLast(o); + } + + public Object pop(){ + return ll.removeLast(); + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return ll.size()==0; + } + public int size(){ + return ll.size(); + } +} diff --git a/group19/1294642551/src/struts_Reflect/Struts.java b/group19/1294642551/src/struts_Reflect/Struts.java new file mode 100644 index 0000000000..1694cf369c --- /dev/null +++ b/group19/1294642551/src/struts_Reflect/Struts.java @@ -0,0 +1,142 @@ +package struts_Reflect; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + + 0. ȡļstruts.xml + + 1. actionNameҵӦclass LoginAction, ͨʵ + parametersеݣösetter parametersе + ("name"="test" , "password"="1234") , + ǾӦõ setNamesetPassword + + 2. ͨöexectue ÷ֵ"success" + + 3. ͨҵgetter getMessage, + ͨã ֵγһHashMap , {"message": "¼ɹ"} , + ŵViewparameters + + 4. struts.xmlе ,Լexecuteķֵ ȷһjsp + ŵViewjspֶС + + * + */ + + //classNameַ + String className = null; + + //---------XMLļ--------------------------------------------------------------------------- + + // saxReader + SAXReader reader = new SAXReader(); + // ͨreadȡһļ תDocument + Document document = reader.read(new File("src/struts_Reflect/Struts.xml")); + //ȡڵԪض + Element root = document.getRootElement(); + //System.out.println("Root: " + root.getName()); + + // ȡԪnameֵΪloginclassԵֵ浽classNameС + for (Iterator iter = root.elementIterator(); iter.hasNext();) + { + Element e = (Element) iter.next(); + String name = e.attributeValue("name"); + + if(name.equals("login")) + { + className = e.attributeValue("class"); + } + + } + + //-----------ȡLoginActionʵ䷽--------------------------------------------------------- + + //ȡʵ + Class clazz = Class.forName("struts_Reflect.LoginAction"); + Object obj = clazz.newInstance(); + + //setName setPassword + Method mSetName = clazz.getMethod("setName", String.class); + Method mSetPassWord = clazz.getMethod("setPassword", String.class); + mSetName.invoke(obj, parameters.get("name")); + mSetPassWord.invoke(obj, parameters.get("password")); + + //excute + Method mExecute = clazz.getMethod("execute", null); + String result = (String) mExecute.invoke(obj, null); + System.out.println(result); + + //ҵgetterԺֵŵparameterMapС + Method[] methods = clazz.getDeclaredMethods(); +// HashMap paraMap = new HashMap(); + + ArrayList al = new ArrayList(); + al.add("name"); + al.add("password"); + al.add("message"); + String key = null; + String value = null; + Field field = null; + + for(int i = 0; i < al.size(); i++) + { + key = al.get(i); + field = clazz.getDeclaredField(key); + field.setAccessible(true); + value = (String) field.get(obj); + parameters.put(key, value); + System.out.println(key+"---"+value); + } + + + + View view = new View(); + view.setParameters(parameters); + + //----------JSPֶηŵView------------------------------------- + + //ȡΪactionԪؽڵ + Element actionE = root.element("action"); + + // ȡԪresultе + String rValue = null; + + for (Iterator iter = actionE.elementIterator(); iter.hasNext();) + { + Element e = (Element) iter.next(); + String name = e.attributeValue("name"); + + if(name.equals(result)) + { + rValue = e.getText(); + view.setJsp(rValue); + System.out.println(rValue); + } + + } + + return view; + + } + +} \ No newline at end of file diff --git a/group19/1294642551/test/arrayTests/ArrayUtilTest.java b/group19/1294642551/test/arrayTests/ArrayUtilTest.java new file mode 100644 index 0000000000..d20c9a6c46 --- /dev/null +++ b/group19/1294642551/test/arrayTests/ArrayUtilTest.java @@ -0,0 +1,101 @@ +package arrayTests; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + ArrayUtil au = new ArrayUtil(); + int[] origin = {7, 9, 30, 3, 4}; + int[] expecteds ={4, 3, 30, 9, 7}; + int[] actuals = au.reverseArray(origin); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testRemoveZero() { + ArrayUtil au = new ArrayUtil(); + int[] oldArray = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] expecteds = {1,3,4,5,6,6,5,4,7,6,7,5}; + int[] actuals = au.removeZero(oldArray); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testMerge() { + + ArrayUtil au = new ArrayUtil(); + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] expecteds = {3,4,5,6,7,8}; + int[] actuals = au.merge(a1, a2); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testGrow() { + + ArrayUtil au = new ArrayUtil(); + int[] oldArray = {2,3,6}; + int size = 3; + int[] expecteds = {2,3,6,0,0,0}; + int[] actuals = au.grow(oldArray, size); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testFibonacci() { + + ArrayUtil au = new ArrayUtil(); + int max = 15; + int[] expecteds = {1, 1, 2, 3, 5, 8, 13}; + int[] actuals = au.fibonacci(max); + + Assert.assertArrayEquals(expecteds, actuals); + } + + + + @Test + public void testGetPrimes() { + ArrayUtil au = new ArrayUtil(); + int max = 23; + int[] expecteds = {2,3,5,7,11,13,17,19}; + int[] actuals = au.getPrimes(max); + + Assert.assertArrayEquals(expecteds, actuals); + + } + + @Test + public void testGetPerfectNumbers() { + + ArrayUtil au = new ArrayUtil(); + int max = 30; + int[] expecteds = {6, 28}; + int[] actuals = au.getPerfectNumbers(max); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testJoin() { + + ArrayUtil au = new ArrayUtil(); + int[] array = {3, 8, 9}; + String seperator = "-"; + String expecteds = "3-8-9"; + String actuals = au.join(array, seperator); + + Assert.assertEquals(expecteds, actuals); + } + +} diff --git a/group19/1592562638/.gitignore b/group19/1592562638/.gitignore new file mode 100644 index 0000000000..0aca6d5fe8 --- /dev/null +++ b/group19/1592562638/.gitignore @@ -0,0 +1,9 @@ +/bin/ +*.class +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* \ No newline at end of file diff --git a/group19/1592562638/src/ArrayList_self.java b/group19/1592562638/src/ArrayList_self.java new file mode 100644 index 0000000000..eb46833962 --- /dev/null +++ b/group19/1592562638/src/ArrayList_self.java @@ -0,0 +1,126 @@ +/*ƣ + * ԭļƣ + * Ҫ㣺 + * 1. ʵֻݽṹࣺArrayList + + */ +public class ArrayList_self implements KIList { + /***ʼС***/ + private final static int INIT_CAPACITY=12; + private Object[] mList=null; + + /***ǰ***/ + private int mCurrentCapacity=0; + /***Ԫظ***/ + private int mSize=0; + + public ArrayList_self(){ + mList=new Object[INIT_CAPACITY]; + mCurrentCapacity=INIT_CAPACITY; + } + + /** + * һԪصArrayListβ + * @param item + * */ + @Override + public void add(T item) { + // TODO Auto-generated method stub + if(mSize==mCurrentCapacity){ + expansion();// + } + mList[mSize]=item; + mSize++; + } + + /** + * һԪصָλãӲλüԪƶһλ + * @param index Ҫλ + * @param item + * */ + @Override + public void add(int index, T item) { + // TODO Auto-generated method stub + if (index<0 || index>=mSize) {//indexС0index >= 鵱ǰС + throw new IndexOutOfBoundsException();//׳Խ쳣 + } + if(mSize==mCurrentCapacity){ + expansion();// + } + Object[] newList=new Object[mCurrentCapacity]; + System.arraycopy(mList, 0, newList, 0, index); + System.arraycopy(mList, index, newList, index+1, mSize-index); + newList[index]=item; + mList=newList; + mSize++; + } + /** + * ƳָλõԪأԪǰƶһλ + * @param index + * */ + @Override + public T remove(int index) { + // TODO Auto-generated method stub + if(index<0 || index>=mSize){ + throw new IndexOutOfBoundsException(); + } + Object[] newList=new Object[mCurrentCapacity]; + System.arraycopy(mList, 0, newList, 0, index); + System.arraycopy(mList, index+1, newList, index, mSize - index); + + T tempT=(T) mList[index]; + mList=newList; + mSize--; + + return tempT; + } + /** + * ָλõԪ + * @param index + * @param item + * */ + @Override + public void set(int index, T item) { + // TODO Auto-generated method stub + if(index<0 || index>=mSize){ + throw new IndexOutOfBoundsException(); + } + mList[index]=item; + } + /** + * ȡָλõԪ + * @param index + * @return + * */ + @Override + public T get(int index) { + // TODO Auto-generated method stub + if(index<0 || index>=mSize){ + throw new IndexOutOfBoundsException(); + } + + return (T)mList[index]; + } + /** + * ȡǰij + * @return int + * */ + @Override + public int size() { + // TODO Auto-generated method stub + return mSize; + } + + /** + * ݣ mSize == mCurrentCapacity ʱ;ʱÿӵǰ50% + * */ + private void expansion() { + // TODO Auto-generated method stub + Object[] oldList=mList; + Object[] newList=new Object[mCurrentCapacity + (mCurrentCapacity >> 1)]; + System.arraycopy(oldList, 0, newList, 0, oldList.length); + mList=newList; + mCurrentCapacity=mCurrentCapacity + (mCurrentCapacity >> 1); + } + +} diff --git a/group19/1592562638/src/BinaryTreeNode_self.java b/group19/1592562638/src/BinaryTreeNode_self.java new file mode 100644 index 0000000000..53ccf3bbdf --- /dev/null +++ b/group19/1592562638/src/BinaryTreeNode_self.java @@ -0,0 +1,71 @@ +/*ƣ + * ԭļƣ + * Ҫ㣺 + * 1. ʵֻݽṹࣺ + + */ +public class BinaryTreeNode_self { + private T data; + private BinaryTreeNode_self left; + private BinaryTreeNode_self right; + + //ȡڵ + public T getData(){ + return data; + } + + //ýڵ + public void setData(T item){ + this.data=item; + } + + //ȡڵ + public BinaryTreeNode_self getLeft(){ + return left; + } + + //ڵ + public void setLeft(BinaryTreeNode_self left){ + this.left=left; + } + + //ȡҽڵ + public BinaryTreeNode_self getRight(){ + return right; + } + + //ҽڵ + public void setRight(BinaryTreeNode_self right){ + this.right=right; + } + + //ӽڵ(֤ڵ<ڵ<ҽڵ) + public BinaryTreeNode_self insert(T item){ + Comparable co=(Comparable)item; + Comparable coData=(Comparable)data; + BinaryTreeNode_self result=null; + if(co.compareTo(data)>0){ + if(right==null){ + right=new BinaryTreeNode_self<>(); + right.data=item; + result=right; + return result; + } + else{ + right.insert(item); + } + } + else{ + if(left==null){ + left=new BinaryTreeNode_self<>(); + left.data=item; + result=left; + return result; + } + else{ + left.insert(item); + } + } + return result; + } +} diff --git a/group19/1592562638/src/CollectionTest.java b/group19/1592562638/src/CollectionTest.java new file mode 100644 index 0000000000..9e6162949a --- /dev/null +++ b/group19/1592562638/src/CollectionTest.java @@ -0,0 +1,50 @@ +/*ƣ + * ԭļƣ + * Ҫ㣺 + * 1. ʵֻݽṹࣺArrayListLinkedListQueueStackTree + + */ +public class CollectionTest { + + public static void main(String[] args) { + // TODO Auto-generated method stub + //ArrayList + ArrayList_self arrayList1=new ArrayList_self(); + for(int i=0;i<15;i++){ + arrayList1.add(new Name("An"+i, "Array")); + if(i>6){ + arrayList1.set(i, new Name("Bo"+i, "Array")); + } + System.out.println(arrayList1.get(i)); + } + + //LinkedList + LinkedList_self linkedList1=new LinkedList_self(); + for(int i=0;i<8;i++){ + linkedList1.add(new Name("An"+i, "Linked")); + if(i>3){ + linkedList1.set(i, new Name("Bo"+i, "Linked")); + } + System.out.println(linkedList1.get(i)); + } + + //Stack + Stack_self stack1=new Stack_self(); + for(int i=0;i<6;i++){ + stack1.push(new Name("An"+i, "Stack")); + if(i>3){ + System.out.println(stack1.peek()); + } + } + + //Queue + Queue_self queue1=new Queue_self(); + for(int i=0;i<6;i++){ + queue1.enQueue(new Name("An"+i, "Queue")); + if(i>3){ + System.out.println(queue1.deQueue()); + } + } + } + +} diff --git a/group19/1592562638/src/KIList.java b/group19/1592562638/src/KIList.java new file mode 100644 index 0000000000..6dc4607499 --- /dev/null +++ b/group19/1592562638/src/KIList.java @@ -0,0 +1,18 @@ +/*ƣ + * ԭļƣ + * Ҫ㣺 + * 1. ˳ӿ:ɾġ鹦 + + */ +public interface KIList { + public void add(T item); + public void add(int index, T item); + + public T remove(int index);//ֵɾԪ + + public void set(int index, T item); + + public T get(int index); + public int size(); + +} diff --git a/group19/1592562638/src/LinkedList_self.java b/group19/1592562638/src/LinkedList_self.java new file mode 100644 index 0000000000..ca394337fb --- /dev/null +++ b/group19/1592562638/src/LinkedList_self.java @@ -0,0 +1,199 @@ +/*ƣ + * ԭļƣ + * Ҫ㣺 + * 1. ʵֻݽṹࣺLinkedList + + */ +public class LinkedList_self implements KIList { + // һڲNode Node ʵĽڵ + public class Node { + public T data;// ڵ + public Node next;// ָһڵ + + // ޲ι + public Node() { + }; + + // ʼȫԹ + public Node(T data, Node next) { + this.data = data; + this.next = next; + } + } + + // ͷڵ + public Node header; + // βڵ + public Node tail; + // Ľڵ + public int size = 0; + + // + public LinkedList_self() { + header = null; + tail = null; + } + + // һָԪص + public LinkedList_self(T element) { + header = new Node(element, tail); + tail = header;// ֻһڵ㣬header tail ָýڵ + size++; + } + + /** + * һԪصArrayListβ + * @param item + */ + @Override + public void add(T item) { + // TODO Auto-generated method stub + // + if (header == null) { + header = new Node(item, tail); + tail = header; + } else { + // ½ڵ + Node newNode = new Node(item, null); + // βڵָ½ڵ + tail.next = newNode; + tail = newNode; + } + size++; + } + + /** + * һԪصָλãӲλüԪƶһλ + * + * @param index + * Ҫλ + * @param item + */ + @Override + public void add(int index, T item) { + // TODO Auto-generated method stub + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + // ǿ + if (header == null) { + add(item); + } else { + // indexΪ0ʱڱͷ + if (index == 0) { + addAtHeader(item); + } + else{ + //ȡǰýڵ + Node prev=getNodeByIndex(index-1); + //prevָ½ڵ㣬½ڵnextָprevnext + prev.next=new Node(item,prev.next); + size++; + } + } + } + + /** + * ƳָλõԪأԪǰƶһλ + * + * @param index + */ + @Override + public T remove(int index) { + // TODO Auto-generated method stub + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + + Node del = null; + // ɾͷ + if (index == 0) { + del = header; + header = header.next; + } else { + // ȡɾڵǰýڵ + Node prev = getNodeByIndex(index - 1); + del = prev.next; + prev.next = del.next; + del.next = null; + } + size--; + return del.data; + } + + /** + * ָλõԪ + * + * @param index + * @param item + */ + @Override + public void set(int index, T item) { + // TODO Auto-generated method stub + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + // headerڵ㿪ʼ + Node current = header; + for (int i = 0; i < size && current != null; i++) { + if (i == index) { + current.data = item; + } + current = current.next; + } + } + + /** + * ȡָλõԪ + * + * @param index + * @return + */ + @Override + public T get(int index) { + // TODO Auto-generated method stub + return getNodeByIndex(index).data; + } + + /** + * ij + * + * @param item + */ + @Override + public int size() { + // TODO Auto-generated method stub + return size; + } + + // indexȡָλõĽڵ + private Node getNodeByIndex(int index) { + // TODO Auto-generated method stub + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + // headerڵ㿪ʼ + Node current = header; + for (int i = 0; i < size && current != null; i++) { + if (i == index) { + return current; + } + current = current.next; + } + return null; + } + + // ͷ巨Ϊ½ڵ + private void addAtHeader(T item) { + // TODO Auto-generated method stub + //½ڵ㣬½ڵnext ָheader + //½ڵΪheader + Node newNode=new Node(item,header); + header=newNode; + //ǰǿ + if(tail==null){ + tail=header; + } + size++; + } +} diff --git a/group19/1592562638/src/Name.java b/group19/1592562638/src/Name.java new file mode 100644 index 0000000000..2e3890133e --- /dev/null +++ b/group19/1592562638/src/Name.java @@ -0,0 +1,36 @@ + +public class Name implements Comparable { + private String firstName,lastName; + public Name(String firstName,String lastName){ + this.firstName=firstName; + this.lastName=lastName; + } + public String getFirstName() { + return firstName; + } + public void setFirstName(String firstName) { + this.firstName = firstName; + } + public String getLastName() { + return lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } + public String toString(){return firstName+" "+lastName;}//дtoString + + //дequalshashCode + public boolean equals(Name name){ + return (firstName.equals(name.firstName) && lastName.equals(name.lastName)); + } + public int hashCode(){ + return firstName.hashCode(); + } + + //дcompareTo + public int compareTo(Name o){ + int lastCmp=lastName.compareTo(o.lastName); + return (lastCmp!=0?lastCmp:firstName.compareTo(o.firstName)); + } + +} diff --git a/group19/1592562638/src/Queue_self.java b/group19/1592562638/src/Queue_self.java new file mode 100644 index 0000000000..387b005060 --- /dev/null +++ b/group19/1592562638/src/Queue_self.java @@ -0,0 +1,37 @@ +/*ƣ + * ԭļƣ + * Ҫ㣺 + * 1. ʵֻݽṹࣺQueue + + */ +public class Queue_self { + private LinkedList_self data; + private int size; + + //ʼ + public Queue_self(){ + data=new LinkedList_self(); + } + + // + public void enQueue(Object item){ + data.add((T)item); + size++; + } + + // + public T deQueue(){ + size--; + return data.remove(0); + } + + //ǷΪն + public boolean isEmpty(){ + return (size==0); + } + + //г + public int size(){ + return size; + } +} diff --git a/group19/1592562638/src/Stack_self.java b/group19/1592562638/src/Stack_self.java new file mode 100644 index 0000000000..e003c1be4a --- /dev/null +++ b/group19/1592562638/src/Stack_self.java @@ -0,0 +1,42 @@ +/*ƣ + * ԭļƣ + * Ҫ㣺 + * 1. ʵֻݽṹࣺStack + + */ +public class Stack_self { + private ArrayList_self elementData=new ArrayList_self(); + private int size=0; + + //ջ + public void push(Object item){ + elementData.add((T)item); + size++; + } + + //ջ + public Object pop(){ + if(size>0){ + size--; + return elementData.remove(size); + } + else{ + return null; + } + } + + //ȡջԪ + public Object peek(){ + return elementData.get(size-1); + } + + //жǷΪ + public boolean isEmpty(){ + return (size==0); + } + + //size + public int size(){ + return size; + } +} diff --git a/group19/1592562638/src/com/coderising/action/LoginAction.java b/group19/1592562638/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..01af400247 --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; +/** + * һչʾ¼ҵ࣬ еû붼Ӳġ + * @author liuxin + * + */ + +public class LoginAction { + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group19/1592562638/src/com/coderising/action/LogoutAction.java b/group19/1592562638/src/com/coderising/action/LogoutAction.java new file mode 100644 index 0000000000..569a109dad --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/LogoutAction.java @@ -0,0 +1,6 @@ +package com.coderising.action; + +public class LogoutAction { + LogoutAction(){ + } +} diff --git a/group19/1592562638/src/com/coderising/action/TestMain.java b/group19/1592562638/src/com/coderising/action/TestMain.java new file mode 100644 index 0000000000..f6f2e60ef2 --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/TestMain.java @@ -0,0 +1,112 @@ +package com.coderising.action; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.filter.Filters; +import org.jdom2.input.SAXBuilder; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; + +/* +0. ȡļstruts.xml + +1. actionNameҵӦclass LoginAction, ͨʵ +parametersеݣösetter parametersе +("name"="test" , "password"="1234") , +ǾӦõ setNamesetPassword + +2. ͨöexectue ÷ֵ"success" + +3. ͨҵgetter getMessage, +ͨã ֵγһHashMap , {"message": "¼ɹ"} , +ŵViewparameters + +4. struts.xmlе ,Լexecuteķֵ ȷһjsp +ŵViewjspֶС + +*/ +//ͨJDOMȡxmlļ +public class TestMain { + + private static String xmlSource=System.getProperty("user.dir")+"\\src\\com\\coderising\\action\\struts.xml"; + private static Map container = new HashMap();//ȡxmlnameַclass + private static Map containerStr = new HashMap();//ȡxmlnameclassַ + private static Map containerBak = new HashMap();//ȡgetter + + private static View view; + private static String resultStr; + private static LoginAction lAction; + + public static void main(String[] args) throws JDOMException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, InvocationTargetException { + // TODO Auto-generated method stub + + // + SAXBuilder builder=new SAXBuilder(); + //StringReader reader=new StringReader(System.getProperty("user.dir")+"\\src\\com\\second\\xml/struts.xml"); + + Document document=builder.build(xmlSource); + + XPathFactory xFactory=XPathFactory.instance(); + XPathExpression expr=xFactory.compile("//struts//action",Filters.element()); + + List links=expr.evaluate(document); + for(Element linkElement:links){ + String name=linkElement.getAttributeValue("name"); + String clazz=linkElement.getAttributeValue("class"); + //÷ʵ Ȼװ + Object o=Class.forName(clazz).newInstance(); + container.put(name,o); + containerStr.put(name, clazz); + + System.out.println("name: "+name+", class: "+clazz); + } + + /************** + 3.ͨҵgetter getMessage, + ͨã ֵγһHashMap , {"message": "¼ɹ"} , + ŵViewparameters + */ + if(container.containsKey("login")){ + lAction=(LoginAction)container.get("login"); + lAction.setName("test"); + lAction.setPassword("1234"); + + resultStr=lAction.execute();//ִexecute + System.out.println("name="+lAction.getName()+" password="+lAction.getPassword()+"->¼"+resultStr); + + Class clazz=Class.forName(containerStr.get("login")); + Method[] methods=clazz.getMethods(); + for(Method method:methods){ + String methodName=method.getName(); + //int indexTmp=methodName.indexOf("get"); + //if(indexTmp!=-1){ + if(methodName.startsWith("get")){ + //getķ + //String methodStr=methodName.substring(indexTmp+3); + String methodStr=methodName.substring(3); + System.out.println(methodStr); + + //System.out.println(method.invoke(lAction)); + + containerBak.put(methodName, (String)method.invoke(lAction)); + } + } + view.setParameters(containerBak);//浽view + } + + /************** + 4.struts.xmlе ,Լexecuteķֵ ȷһjsp + ŵViewjspֶ + */ + + } +} diff --git a/group19/1592562638/src/com/coderising/action/View.java b/group19/1592562638/src/com/coderising/action/View.java new file mode 100644 index 0000000000..8a80428d33 --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/View.java @@ -0,0 +1,22 @@ +package com.coderising.action; +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/group19/1592562638/src/com/coderising/action/struts.xml b/group19/1592562638/src/com/coderising/action/struts.xml new file mode 100644 index 0000000000..fb0c2be3de --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/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/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/1\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206/\350\257\273\344\271\246\347\254\224\350\256\260_\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206.pdf" "b/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/1\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206/\350\257\273\344\271\246\347\254\224\350\256\260_\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206.pdf" new file mode 100644 index 0000000000..35e06b434a Binary files /dev/null and "b/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/1\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206/\350\257\273\344\271\246\347\254\224\350\256\260_\350\256\241\347\256\227\346\234\272\347\273\204\346\210\220\345\216\237\347\220\206.pdf" differ diff --git "a/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/2\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237[\350\275\254]/\350\257\273\344\271\246\347\254\224\350\256\260_\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237.pdf" "b/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/2\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237[\350\275\254]/\350\257\273\344\271\246\347\254\224\350\256\260_\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237.pdf" new file mode 100644 index 0000000000..065c59f54f Binary files /dev/null and "b/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/2\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237[\350\275\254]/\350\257\273\344\271\246\347\254\224\350\256\260_\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237.pdf" differ diff --git a/group19/2558178127/.gitignore b/group19/2558178127/.gitignore new file mode 100644 index 0000000000..ee46440140 --- /dev/null +++ b/group19/2558178127/.gitignore @@ -0,0 +1,9 @@ +/bin/ +*.class +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* diff --git a/group19/2558178127/src/com/cn/kevin/Test.java b/group19/2558178127/src/com/cn/kevin/Test.java new file mode 100644 index 0000000000..0fea033fc1 --- /dev/null +++ b/group19/2558178127/src/com/cn/kevin/Test.java @@ -0,0 +1,5 @@ +package com.cn.kevin; + +public class Test { + +} diff --git a/group19/2558178127/src/com/coderising/action/LoginAction.java b/group19/2558178127/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..72e00894bb --- /dev/null +++ b/group19/2558178127/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group19/2558178127/src/com/coderising/array/ArrayUtil.java b/group19/2558178127/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..fb6a84f255 --- /dev/null +++ b/group19/2558178127/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,189 @@ +package com.coderising.array; + + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (origin == null || origin.length <= 1) { + return; + } + int tem = 0; + for (int i = 0, len = origin.length; i < len / 2; i++) { + tem = origin[i]; + origin[i] = origin[len - i + 1]; + origin[len - i + 1] = tem; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + if(oldArray == null){ + return null; + } + int [] ret = new int[oldArray.length]; + int cou = 0; + for(int i=0;itemint[j]){ + int temp = temint[i]; + int p = index[i]; + temint[i] = temint[j]; + index[i] = index[j]; + temint[j] = temp; + index[j] = p; + } + } + } + + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] res = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0,res,0,oldArray.length); + return res; + } + + /** + * 斐波那契数列为: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[] a = new int[max]; + int[] retf = null; + if (max < 3) { + retf =new int[]{1,1}; + } else if (max >= 3) { + a[0] = a[1] = 1; + for (int i = 2; i < max; i++) { + a[i] = a[i - 1] + a[i - 2]; + if(a[i]>max){ + break; + } + } + retf = removeZero(a); + } + return retf; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] prime = new int[max]; + int sum = 0; + for (int i = 2; i <= max; i++) {// 从2开始是因为,1既不是素数也不是合数 + boolean sign = true; + for (int j = 2; j < i; j++) { + if (i % j == 0) {// 能被除了1和自己整除的数肯定不是素数,因此只要有一个就可以跳过循环 + sign = false; + continue; + } + } + if (sign) { + prime[i] = i; + } + } + int[] retf = null; + retf = removeZero(prime); + return retf; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer sbf = new StringBuffer(""); + for(int i=0;i parameters) throws Exception{ + + /* + + 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字段中。 + + */ + + new XmlService().readDocument(); //读取xml。把xml信息存入map + + String clsName = readxml(actionName,"class");//获取action对应class对象 + Class c1 = Class.forName(clsName); + Object obj = c1.newInstance(); + //action属性赋值 + Iterator iter = parameters.keySet().iterator(); + while (iter.hasNext()) { + String key = iter.next(); + String namval = parameters.get((String) key); + Method m = c1.getMethod("set" + key.substring(0, 1).toUpperCase() + + key.substring(1), String.class); + m.invoke(obj, namval); + } + + String result = ""; + Method mt=c1.getMethod("execute");//调用执行方法 + result = (String) mt.invoke(obj); + View view = new View(); + view.setJsp(readxml(actionName,result)); + return view; + } + /** + * 读取Map中xml信息 + * @param name + * @param attr + * @return + */ + public static String readxml(String name,String attr){ + + Map xmlMap = XmlService.map; + + return (String) xmlMap.get(name+attr); + } + + +} diff --git a/group19/2558178127/src/com/coderising/litestruts/StrutsTest.java b/group19/2558178127/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..3fedbec428 --- /dev/null +++ b/group19/2558178127/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,53 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception{ + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + public static void main(String [] args)throws Exception{ + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + System.out.println(view.getJsp()); + } + @Test + public void testLoginActionFailed() throws Exception{ + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group19/2558178127/src/com/coderising/litestruts/View.java b/group19/2558178127/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group19/2558178127/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/group19/2558178127/src/com/coderising/litestruts/struts.xml b/group19/2558178127/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..fb0c2be3de --- /dev/null +++ b/group19/2558178127/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/group19/2558178127/src/com/coderising/litestruts/xml/read/XmlService.java b/group19/2558178127/src/com/coderising/litestruts/xml/read/XmlService.java new file mode 100644 index 0000000000..fc4ad6c23e --- /dev/null +++ b/group19/2558178127/src/com/coderising/litestruts/xml/read/XmlService.java @@ -0,0 +1,88 @@ +package com.coderising.litestruts.xml.read; + + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class XmlService { + + private File file; + + public static Map map = new HashMap(); + + public XmlService() { + file = new File("E:/2017learn/coding2017/group19/2558178127/src/com/coderising/litestruts/struts.xml"); + + } + + public Document getDocument() { + SAXReader reader = new SAXReader(); + + Document document = null; + + try { + document = reader.read(file); + } catch (Exception e) { + + e.printStackTrace(); + } + return document; + } + + //读取xml + public void readDocument() { + + List sonElementList = getDocument().getRootElement().elements(); + + Element root = getDocument().getRootElement(); + getElement(sonElementList); + System.out.println(map); + } + + private List list = new ArrayList(); + + private void getElement(List sonElemetList) { + for (Element sonElement : sonElemetList) { + if (sonElement.elements().size() != 0) { + getNodes(sonElement,list); + getElement(sonElement.elements()); + }else{ + getNodes(sonElement,list); + System.out.println(sonElement.getName() + ":"+ sonElement.getText()); + } + + + } + } + + public void getNodes(Element node,List pname){ + //当前节点的名称、文本内容和属性 + System.out.println("当前节点名称:"+node.getName());//当前节点名称 + System.out.println("当前节点的内容:"+node.getTextTrim());//当前节点内容 + List listAttr=node.attributes();//当前节点的所有属性的list + String classValue=""; + for(Attribute attr:listAttr){//遍历当前节点的所有属性 + String name=attr.getName();//属性名称 + String value=attr.getValue();//属性的值 + System.out.println("属性名称:"+name+"属性值:"+value); + if("login".equals(value) || "logout".equals(value)){ + pname.add(0,value); + }else{ + map.put(pname.get(0)+value, node.getTextTrim()); + } + if("class".equals(name)&&pname!=null){ + classValue=value; + map.put(pname.get(0)+"class", value); + } +// + } + } +} diff --git a/group19/2558178127/src/com/coding/basic/ArrayList.java b/group19/2558178127/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..fb889ccc06 --- /dev/null +++ b/group19/2558178127/src/com/coding/basic/ArrayList.java @@ -0,0 +1,99 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + private static final int DEFAULT_SIZE = 100 ; + private Object[] elements = new Object[DEFAULT_SIZE]; + private int capacity = size; + + public void add(Object o) { + addSize(size); + this.elements[size] = o; + this.size++; + } + + public void add(int index, Object o) { + checkIndex(index); + for (int i = index; i < elements.length; i++) { + if (i + 1 < elements.length) { + elements[i] = elements[i + 1]; + } + } + size--; + } + + public Object get(int index) { + checkIndex(index); + return this.elements[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object o = elements[index];; + for (int i = index; i < elements.length; i++) { + + if (i + 1 < elements.length) { + elements[i] = elements[i + 1]; + } + } + size--; + return o; + } + + public int size() { + return this.size; + } + + public Iterator iterator() { + return new IteratorImpl(); + } + + /** + * жǷԽ + */ + private void checkIndex(int index) { + if (index > size || index < 0) { + throw new RuntimeException("Խ"); + } + } + + /** + * ȷǰӡ + */ + private void addSize(int index) { + if (index > size && size< elements.length-1) { + this.capacity = this.size + this.DEFAULT_SIZE; + Object[] newElemets = new Object[this.capacity]; + + System.arraycopy(elements,0,newElemets,0,elements.length); + + this.elements = newElemets; + } + } + + class IteratorImpl implements Iterator { + + private int curi = 0; + + @Override + public boolean hasNext() { + boolean bn = false; + if (curi < size) { + bn = true; + } + return bn; + } + + @Override + public Object next() { + if (!hasNext()) { + return null; + } + curi++; + return elements[curi]; + } + + } + +} diff --git a/group19/2558178127/src/com/coding/basic/Iterator.java b/group19/2558178127/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group19/2558178127/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/group19/2558178127/src/com/coding/basic/LinkedList.java b/group19/2558178127/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..415aa046d0 --- /dev/null +++ b/group19/2558178127/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + //ͷԪ + private Node first; + //βԪ + private Node end; + + private int modcount; + + public void add(Object o){ + add(size(), o); + } + public void add(int index , Object o){ + addBefore(getNode(index),0); + } + public Object get(int index){ + return getNode(index).data; + } + + public Object remove(int index) { + Node n = getNode(index); + n.prev.next = n.next; + n.next.prev = n.prev; + size--; + modcount++; + return n.data; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node node = getNode(0); + node.data = o; + node.next = head; + head = node; + size++; + modcount++; + } + public void addLast(Object o){ + add(size(), o); + } + public Object removeFirst(){ + return remove(head.next); + } + public Object removeLast(){ + return remove(head.prev); + } + public Iterator iterator(){ + return new LinkedListIterator(); + } + + //ִ + private void addBefore (Node p, Object o) { + Node newNode = new Node(o, p.prev, p); + newNode.prev.next = newNode; + p.prev = newNode; + modcount++; + } + + //ҽڵ + private Node getNode(int idx) { + Node p; + + if(idx <0 || idx > size()) { + throw new IndexOutOfBoundsException(); + } + + if(idx < size()/2) { + p = first.next; + for (int i = 0;i < idx;i++) { + p = p.next; + } + }else { + p = end; + for (int i = size();i>idx;i--) { + p = p.prev; + } + } + return p; + } + + public boolean remove(Object o) { + if (o==null) { + for (Node e = head.next; e != head; e = e.next) { + if (e.data==null) { + remove(e); + return true; + } + } + } else { + for (Node e = head.next; e != head; e = e.next) { + if (o.equals(e.data)) { + remove(e); + return true; + } + } + } + return false; + } + + + private static class Node { + Object data; + Node next; + Node prev; + + public Node(Object data, Node next, Node prev) { + this.data = data; + this.prev = prev; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + + private Node current = first.next; + private int expectedModCount = modcount; + private boolean okToRemove = false; + + @Override + public boolean hasNext() { + return current != end; + } + + @Override + public Object next() { + Object nextData = current.data; + current = current.next; + okToRemove = true; + return nextData; + } + + } +} diff --git a/group19/2558178127/src/com/coding/basic/List.java b/group19/2558178127/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group19/2558178127/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/group19/2558178127/src/com/coding/basic/Queue.java b/group19/2558178127/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..85ad907c4e --- /dev/null +++ b/group19/2558178127/src/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + private LinkedList list = new LinkedList(); + private int size = 0; + + public void enQueue(Object o){ + size++; + list.addLast(o); + } + + public Object deQueue(){ + size--; + return list.removeFirst(); + } + + public boolean isEmpty(){ + return size>0; + } + + public int size(){ + return size; + } +} diff --git a/group19/2558178127/src/com/coding/basic/Stack.java b/group19/2558178127/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..8e50a38b38 --- /dev/null +++ b/group19/2558178127/src/com/coding/basic/Stack.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(size>0){ + size--; + return elementData.remove(size); + } + return null; + } + + public Object peek(){ + if(size>0) + elementData.get(size-1); + return null; + } + public boolean isEmpty(){ + return size>0; + } + public int size(){ + return size; + } +} diff --git a/group19/376248270/0-20170215-20170226/README.md b/group19/376248270/0-20170215-20170226/README.md new file mode 100644 index 0000000000..6a92672f18 --- /dev/null +++ b/group19/376248270/0-20170215-20170226/README.md @@ -0,0 +1,11 @@ +### 0x00 (Deadline: 2017-02-26) +#### 1. 实现基本的数据结构 +- [x] ArrayList +- [] LinkedList +- [] Queue +- [] Stack +- [] BinaryTree(可选) +- [] Interor(可选) + +#### 2. 文章 +- [x] [建议写一篇有关CPU、内存、硬盘、指令的文章](http://bosschow.github.io/2017/02/24/recursion-and-tail-recursion/) diff --git a/group19/376248270/0-20170215-20170226/arraylist/ArrayList.java b/group19/376248270/0-20170215-20170226/arraylist/ArrayList.java new file mode 100644 index 0000000000..8b4a30c5ff --- /dev/null +++ b/group19/376248270/0-20170215-20170226/arraylist/ArrayList.java @@ -0,0 +1,97 @@ +package arraylist; +import java.util.Arrays; + +import base.List; + +public class ArrayList implements List { + public static void main(String[] args) { + ArrayList arr = new ArrayList(); + System.out.println(arr.size()); + arr.add("ele1"); + arr.add("ele2"); + + for (int i = 0; i < arr.size(); i++) { + System.out.println((String)arr.get(i)); + } + + System.out.println("============"); + arr.remove(0); + + for (int i = 0; i < arr.size(); i++) { + System.out.println((String)arr.get(i)); + } + } + + // 初始容量 + static final int DEFAULT_INITIAL_CAPACITY = 10; + + // 数组扩展速度 + static final int INCRE_SPEED = 2; + + // 元素数组 + private Object[] elementData = new Object[DEFAULT_INITIAL_CAPACITY]; + + // 元素数量 + private int size = 0; + + /** + * 添加元素到指定位置 + */ + public void add(int index, Object obj) { + add(elementData[size-1]); + for (int i = size - 1; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = obj; + } + + /** + * 删除指定位置的元素 + */ + public Object remove(int index) { + Object result = elementData[index]; + + while (index < size) { + elementData[index] = elementData[index + 1]; + index++; + } + size--; + return result; + } + + /** + * 获取指定位置的元素 + */ + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: "+index); + } + return elementData[index]; + } + + /** + * 获取当前元素数量 + */ + public int size() { + return size; + } + + /** + * 添加元素到尾部 + */ + public void add(Object obj) { + elementData[size++] = obj; + if (size == elementData.length) { + resize(); + } + + } + + /** + * 元素数组自动扩展 + */ + private void resize() { + elementData = Arrays.copyOf(elementData, elementData.length * INCRE_SPEED); + } + +} diff --git a/group19/376248270/0-20170215-20170226/arraylist/ArrayListTest.java b/group19/376248270/0-20170215-20170226/arraylist/ArrayListTest.java new file mode 100644 index 0000000000..c48142416e --- /dev/null +++ b/group19/376248270/0-20170215-20170226/arraylist/ArrayListTest.java @@ -0,0 +1,57 @@ +package arraylist; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + ArrayList arr = null; + + @Before + public void setup() { + arr = new ArrayList(); + } + + @Test + public void testAdd() { + arr.add("ele-1"); + arr.add("ele-2"); + printArrayList(); + arr.add(0, "ele-0"); + printArrayList(); + } + + @Test + public void testGet() { + arr.add("ele-1"); + arr.add("ele-2"); + printArrayList(); + arr.remove(0); + printArrayList(); + System.out.println((String)arr.get(1)); + } + + @Test + public void testRemove() { + + for (int i = 0; i < 10; i++) { + arr.add("ele-"+i); + } + + printArrayList(); + arr.remove(1); + System.out.println("After Remove"); + printArrayList(); + } + + /** + * 打印ArrayList + * @param arr + */ + private void printArrayList() { + System.out.print("["); + for (int i = 0; i < arr.size(); i++) { + System.out.print((String)arr.get(i) + ", "); + } + System.out.println("]"); + } +} diff --git a/group19/376248270/0-20170215-20170226/base/List.java b/group19/376248270/0-20170215-20170226/base/List.java new file mode 100644 index 0000000000..8ca593dec2 --- /dev/null +++ b/group19/376248270/0-20170215-20170226/base/List.java @@ -0,0 +1,8 @@ +package base; +public interface List { + public void add(Object obj); + public int size(); + public Object get(int index); + public Object remove(int index); + public void add(int index, Object obj); +} diff --git a/group19/376248270/0-20170215-20170226/linkedlist/LinkedList.java b/group19/376248270/0-20170215-20170226/linkedlist/LinkedList.java new file mode 100644 index 0000000000..da357f7164 --- /dev/null +++ b/group19/376248270/0-20170215-20170226/linkedlist/LinkedList.java @@ -0,0 +1,25 @@ +package linkedlist; + +public class LinkedList implements List { + private Node head; + + private int size; + + public void add(Object obj) { + Node current = new + } + + /** + * 获取元素数量 + */ + public int size() { + return size; + } + + private static class Node { + + Object data; + Node next; + + } +} diff --git a/group19/376248270/1-20170226-20170305/README.md b/group19/376248270/1-20170226-20170305/README.md new file mode 100644 index 0000000000..1bbe089b52 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/README.md @@ -0,0 +1,8 @@ +### 0x01 (Deadline: 2017-03-05) + +#### 1. coding +- [x] 实现简单的类似与Struts的操作,需要通过Junit测试。 +- [x] 实现ArrayUtil + +#### 2. 文章 +- [x] [写一篇文章,自主定题目](http://bosschow.github.io/2017/03/05/get-prime-number-1/) diff --git a/group19/376248270/1-20170226-20170305/array/ArrayUtil.java b/group19/376248270/1-20170226-20170305/array/ArrayUtil.java new file mode 100644 index 0000000000..a64eb8da05 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/array/ArrayUtil.java @@ -0,0 +1,231 @@ +package array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Stack; + +public class ArrayUtil { + + public static void main(String[] args) { + int[] origin = new int[]{1, 2, 3, 4}; + System.out.println(Arrays.toString(getPrimes(23))); + } + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果: a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + /** + * 1. 通过双指针实现反转 + * @param origin + * @return + */ + public static int[] reverseArray_1(int[] origin){ + int i = 0; + int j = origin.length - 1; + for (;i < j;) { + int swap = origin[i]; + origin[i] = origin[j]; + origin[j] = swap; + i++; + j--; + } + + return origin; + } + + /** + * 2. 通过栈后进先出的特点实现反转 + * @param origin + * @return + */ + public static int[] reverseArray_2(int[] origin) { + Stack stack = new Stack<>(); + + for (int num : origin) { + stack.push(num); + } + for (int i = 0; i < origin.length; i++) { + origin[i] = stack.pop(); + } + return origin; + } + + /** + * 3. 通过反序遍历实现反转 + * @param origin + * @return + */ + public static int[] reverseArray_3(int[] origin) { + int[] result = new int[origin.length]; + for (int i = origin.length - 1; i >= 0; i--) { + result[origin.length - i - 1] = origin[i]; + } + return result; + } + + /** + * 现在有如下的一个数组: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_1(int[] oldArray){ + List list = new ArrayList<>(); + for (int num : oldArray) { + if (num != 0) { + list.add(num); + } + } + + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + + /** + * 通过双指针实现 + * @param oldArray + * @return + */ + public static int[] removeZero_2(int[] oldArray) { + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + oldArray[j++] = oldArray[i]; + } + } + + return Arrays.copyOf(oldArray, j); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + return Arrays.copyOf(oldArray, oldArray.length + size); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + + List list = new ArrayList<>(); + int current = 0; + for (int i = 0; i < max; i++) { + if (i <= 1) { + current = 1; + } else { + current = list.get(i - 1) + list.get(i - 2); + } + if (current >= max) break; + list.add(current); + } + + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * 写了一篇文章:http://bosschow.github.io/2017/03/05/get-prime-number-1/ + * @param max + * @return + */ + public static int[] getPrimes(int max){ + List list = new ArrayList<>(); + + for (int i = 2; i < max; i++) { + boolean isPrime = true; + for (Integer primeNum : list) { + if (primeNum <= Math.sqrt(i)) { + if (i % primeNum == 0) { + isPrime = false; + break; + } + } else { + break; + } + } + + if (isPrime) { + list.add(i); + } + } + + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + stringBuilder.append(array[i]); + } else { + stringBuilder.append(array[i] + seperator); + } + } + + return stringBuilder.toString(); + } + + +} diff --git a/group19/376248270/1-20170226-20170305/com/coderising/action/LoginAction.java b/group19/376248270/1-20170226-20170305/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5496d8084d --- /dev/null +++ b/group19/376248270/1-20170226-20170305/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group19/376248270/1-20170226-20170305/struts/Struts.java b/group19/376248270/1-20170226-20170305/struts/Struts.java new file mode 100644 index 0000000000..c431672037 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/struts/Struts.java @@ -0,0 +1,245 @@ +package struts; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class Struts { + + private static final String CONFIG_FILE = "struts/struts.xml"; + private static final String SETTER_METHOD_PREFIX = "set"; + private static final String GETTER_METHOD_PREFIX = "get"; + + private static Map actionList; + + static { + System.out.println("Loading struts config file: " + CONFIG_FILE); + actionList = parseConfigFile(CONFIG_FILE); + System.out.println("Load struts config success!"); + } + + /** + * 自测 + * @param args + */ + public static void main(String[] args) { + Map map = new HashMap<>(); + map.put("name", "test"); + map.put("password", "1234"); + runAction("login", map); + } + + /** + * 运行指定action + * @param actionName + */ + public static View runAction(String actionName, Map parameters) { + Action action = getActionByName(actionName); + View view = new View(); + + try { + Class cls = Class.forName(action.getClassName()); + Object actionInstance = cls.newInstance(); + + // 设置参数 + setParameters(cls, actionInstance, parameters); + + // 执行execute方法 + String executeResult = execute(cls, actionInstance); + + // 获取属性 + String[] attributes = new String[]{"message"}; + Map results = getAttributes(cls, actionInstance, attributes); + + // 获取JSP + String resultJsp = getResultJSP(action, executeResult); + + // 组装视图 + view.setJsp(resultJsp); + view.setParameters(results); + } catch (Exception e) { + e.printStackTrace(); + } + + return view; + } + + /** + * 获取需要返回的JSP名称 + * @param action + * @param executeResult + * @return + */ + private static String getResultJSP(Action action, String executeResult) { + Map resultMap = action.getResultMap(); + String resultJSP = ""; + + if (resultMap.containsKey(executeResult)) { + resultJSP = resultMap.get(executeResult); + } + return resultJSP; + } + + /** + * 执行action的execute方法 + * @param cls + * @param instance + * @return + */ + private static String execute(Class cls, Object instance) { + String result = null; + try { + Method method = cls.getMethod("execute"); + result = (String) method.invoke(instance); + } catch(Exception e) { + e.printStackTrace(); + } + return result; + } + + /** + * 通过getter方法获取实例属性 + * @param cls + * @param instance + * @param parameterName + * @return + */ + private static Map getAttributes(Class cls, Object instance, String[] attributes) { + Map result = new HashMap<>(); + + try { + for (String attr : attributes) { + String methodName = GETTER_METHOD_PREFIX + capitalizeFirstLetter(attr); + Method method = cls.getMethod(methodName); + String returnValue = (String)method.invoke(instance); + result.put(attr, returnValue); + } + } catch(Exception e) { + e.printStackTrace(); + } + return result; + } + + /** + * 为action实例设置属性 + * @param cls + * @param instance + * @param parameters + */ + private static void setParameters(Class cls, Object instance, Map parameters) { + for (String name : parameters.keySet()) { + String methodName = SETTER_METHOD_PREFIX + capitalizeFirstLetter(name); + try { + Method method = cls.getMethod(methodName, String.class); + method.invoke(instance, parameters.get(name)); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * 将传入的字符串首字母大写 + * @param inputString + * @return + */ + private static String capitalizeFirstLetter(String inputString) { + if (inputString == null || inputString.length() == 0) { + return inputString; + } + + return inputString.substring(0, 1).toUpperCase() + inputString.substring(1); + } + + /** + * 解析struts配置文件 + * @param configFile + * @return + */ + private static Map parseConfigFile(String configFile) { + Map parseResult = new HashMap<>(); + + try { + File inputFile = new File(configFile); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputFile); + NodeList nList = doc.getElementsByTagName("action"); + + for (int i = 0; i < nList.getLength(); i++) { + Action action = new Action(); + Map resultList = new HashMap<>(); + String actionName = ""; + + Node nNode = nList.item(i); + + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element)nNode; + actionName = eElement.getAttribute("name"); + String className = eElement.getAttribute("class"); + + action.setName(actionName); + action.setClassName(className); + + NodeList nodeList = eElement.getElementsByTagName("result"); + for (int j = 0; j < nodeList.getLength(); j++) { + String resultName = ((Element)nodeList.item(j)).getAttribute("name"); + String resultValue = ((Element)nodeList.item(j)).getTextContent(); + resultList.put(resultName, resultValue); + } + action.setResultMap(resultList); + } + + parseResult.put(actionName, action); + } + } catch(Exception e) { + e.printStackTrace(); + } + + return parseResult; + } + + /** + * 返回指定的action + * @param actionName + * @return + */ + private static Action getActionByName(String actionName) { + return actionList.get(actionName); + } + + private static class Action { + String name; + String className; + Map resultMap; + + public String getName() { + return name; + } + public String getClassName() { + return className; + } + public Map getResultMap() { + return resultMap; + } + public void setName(String name) { + this.name = name; + } + public void setClassName(String className) { + this.className = className; + } + public void setResultMap(Map resultList) { + this.resultMap = resultList; + } + } + +} diff --git a/group19/376248270/1-20170226-20170305/struts/StrutsTest.java b/group19/376248270/1-20170226-20170305/struts/StrutsTest.java new file mode 100644 index 0000000000..3a4d69ee20 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/struts/StrutsTest.java @@ -0,0 +1,43 @@ +package 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/group19/376248270/1-20170226-20170305/struts/View.java b/group19/376248270/1-20170226-20170305/struts/View.java new file mode 100644 index 0000000000..4e006ba71e --- /dev/null +++ b/group19/376248270/1-20170226-20170305/struts/View.java @@ -0,0 +1,23 @@ +package 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/group19/376248270/1-20170226-20170305/struts/struts.xml b/group19/376248270/1-20170226-20170305/struts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/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/group19/376248270/README.md b/group19/376248270/README.md new file mode 100644 index 0000000000..e7b15ad6f0 --- /dev/null +++ b/group19/376248270/README.md @@ -0,0 +1,3 @@ +### Profile +- nickname: kian +- QQ: 376248270 diff --git a/group19/398129523/ArrayList b/group19/398129523/ArrayList new file mode 100644 index 0000000000..4557672f10 --- /dev/null +++ b/group19/398129523/ArrayList @@ -0,0 +1,88 @@ +package com.coding.work; + +import java.util.Arrays; +import java.util.Iterator; + + +public class ArrayList { + private Object[] elementData; + private int size; + + + public ArrayList(int init) { + // TODO Auto-generated constructor stub + if (init <= 0) { + throw new IllegalArgumentException(); + } + this.elementData = new Object[init]; + } + + public ArrayList(){ + this(10); //调用重载的构造函数 + } + + public void changeCapacity(int curCapacity) { + int oldCapacity = elementData.length; + if(curCapacity > oldCapacity){ + Object[] oldData = elementData; + int newCapacity = oldCapacity + oldCapacity >> 1; //右移一位除以2,相当于扩 扩大到1.5倍 + if (newCapacity elementData.length - 1) { + changeCapacity(index + 1); + } + elementData[index] = e; + size++; + return true; + } + + @SuppressWarnings("unchecked") + public E get(int index) { + if (index <= size - 1) { + return (E) elementData[index]; + + }else { + return null; + } + } + + @SuppressWarnings("unchecked") + public E remove(int index) { + if (index >= 0 && index <= size - 1) { + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return (E) elementData[index]; + } + else { + return null; + } + } + + + + public int size() { + return size;//全局变量 + } + + //不会实现 + public Iterator iterator() { + return null; + + } + +} diff --git a/group19/398129523/LinkedList b/group19/398129523/LinkedList new file mode 100644 index 0000000000..8cf3803e2b --- /dev/null +++ b/group19/398129523/LinkedList @@ -0,0 +1,46 @@ +package com.coding.work; + +public class LinkedList{ + private int size; + + private static class Node { + Object data; + Node next; + } + private Node head = new Node(); + private Node lastNode; + + public LinkedList(){ + head.next = null; + head.data = null; + lastNode = head; + } + + public void add(Object o) { + Node curnode = new Node(); + curnode.data = o; + curnode.next = null; + if (head.next == null) { + head.next = curnode; + lastNode = curnode; + size++; + }else { + lastNode.next = curnode; + size++; + } + + + } + public void add(int index , Object o) { + if (index > size - 1) { + for(int i = size - 1; i <= index; i++){ + Node curnode = new Node(); + lastNode.next = curnode; + lastNode = curnode; + } + lastNode.data = o; + } + + } + +} diff --git a/group19/398129523/Queue b/group19/398129523/Queue new file mode 100644 index 0000000000..e98ef838f4 --- /dev/null +++ b/group19/398129523/Queue @@ -0,0 +1,38 @@ +package com.coding.work; + +import java.util.NoSuchElementException; + + + +public class Queue { + private Object[] elementdate; + private int size; + private int index; + + public void enQueue(Object o) { + if(o == null){ + throw new IllegalArgumentException(); + } + elementdate[index++] = o; + size = index + 1; + } + + public Object deQueue(){ + if (size == 0) { + throw new NoSuchElementException(); + }else{ + Object out = elementdate[0]; + System.arraycopy(elementdate, 1, elementdate, 0, size - 1); + return out; + } + } + + public boolean isEmpty(){ + + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group19/398129523/Stack b/group19/398129523/Stack new file mode 100644 index 0000000000..6d4d44dab9 --- /dev/null +++ b/group19/398129523/Stack @@ -0,0 +1,36 @@ +package com.coding.work; + +import java.util.NoSuchElementException; + +public class Stack { + private Object[] elementdata; + private int size; + private int index; + + public void push(Object o) { + if (o == null) { + throw new IllegalArgumentException(); + + } + size = index + 1; + elementdata[index++] = o; + } + + public Object pop() { + if (size == 0) { + throw new NoSuchElementException(); + } + size --; + return elementdata[index--]; + } + + @SuppressWarnings("unused") + private boolean isEmpty() { + return size == 0; + } + + private int size() { + return size; + } + +} diff --git a/group19/527220084/xukai_coding/.gitignore b/group19/527220084/xukai_coding/.gitignore new file mode 100644 index 0000000000..a269ef720f --- /dev/null +++ b/group19/527220084/xukai_coding/.gitignore @@ -0,0 +1,19 @@ +*target +*.classpath +*.project +*.settings +*iml +.idea +*gen-* + +*.class + +.metadata +.recommenders + +#macOS +.DS_Store + +rebel.* +.rebel.* + diff --git a/group19/527220084/xukai_coding/coding-common/pom.xml b/group19/527220084/xukai_coding/coding-common/pom.xml new file mode 100644 index 0000000000..023093e4d7 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/pom.xml @@ -0,0 +1,109 @@ + + + + xukai.coding + org.xukai.coding + 1.0-SNAPSHOT + + 4.0.0 + jar + coding.common + + + + junit + junit + + + commons-beanutils + commons-beanutils + 1.8.0 + + + org.apache.commons + commons-compress + 1.8.1 + + + commons-lang + commons-lang + + + commons-logging + commons-logging + 1.2 + + + org.apache.commons + commons-lang3 + + + commons-io + commons-io + + + com.google.guava + guava + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + org.slf4j + jcl-over-slf4j + + + + + + + + + com.google.code.findbugs + jsr305 + 3.0.1 + + + cglib + cglib + + + joda-time + joda-time + + + dom4j + dom4j + + + jaxen + jaxen + + + + + + + + + javax.servlet + javax.servlet-api + + + org.springframework + spring-web + + + com.alibaba + fastjson + + + + \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/array/ArrayUtil.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..3edaebe9ef --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/array/ArrayUtil.java @@ -0,0 +1,306 @@ +package org.xukai.coderising.array; + +import org.junit.Test; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for (int i = 0; i < origin.length; i++) { + if (i == origin.length/2) { + break; + } + swap(origin,i,origin.length - 1 -i); + } + } + private void swap(int[] array,int a,int b){ + array[a] = array[a] + array[b]; + array[b] = array[a] - array[b]; + array[a] = array[a] - array[b]; + } + + @Test + public void testReverseArray() { + int[] test = new int[]{7, 9, 30,-11, 3, 4}; + reverseArray(test); + for (int i = 0; i < test.length; i++) { + System.out.println(test[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 nullIndex = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + nullIndex ++; + } + } + int[] newArray = new int[nullIndex]; + nullIndex = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[nullIndex] = oldArray[i]; + nullIndex++; + } + } + return newArray; + } + @Test + public void removeZero() { + int[] test = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArray = removeZero(test); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int index_a = 0; + int index_b = 0; + int[] array = new int[array1.length + array2.length]; + int i = 0 ; + while(index_a < array1.length && index_b < array2.length){ + if (array1[index_a] < array2[index_b]) { + array[i] = array1[index_a]; + index_a++; + i++; + } else if(array1[index_a] > array2[index_b]) { + array[i] = array2[index_b]; + index_b++; + i++; + } else { + array[i] = array1[index_a]; + i++; + index_a++; + index_b++; + } + } + if (index_a == array1.length) { + for (int j = index_b; j < array2.length; j++) { + array[i] = array2[j]; + i++; + } + } else { + for (int j = index_a; j < array1.length; j++) { + array[i] = array1[j]; + i++; + } + } + int[] result = new int[i]; + System.arraycopy(array,0,result,0,i); + return result; + } + + @Test + public void testMerge() { + int[] test1 = new int[]{2,3, 5, 7,8}; + int[] test2 = new int[]{4, 5, 6,7,8,21,33}; + int[] newArray = merge(test1,test2); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray,0,newArray,0,oldArray.length); + + + return newArray; + } + + @Test + public void testGrow() { + int[] test1 = new int[]{2,3, 5, 7,8}; + int[] newArray = grow(test1, 5); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if (max <= 1) { + return new int[0]; + } + int index_a = 1; + int index_b = 1; + int temp = 0; + int count = 2; + while ((temp = index_a + index_b) < max) { + index_a = index_b; + index_b = temp; + count++; + } + int[] newArray = new int[count]; + for (int i = count-1; i > -1 ; i--) { + newArray[i] = index_b; + temp = index_b - index_a; + index_b = index_a; + index_a = temp; + } + return newArray; + } + + @Test + public void testfibonacci() { + int[] newArray = fibonacci(15); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] container = new int[5]; + int count = 0; + for (int i = 3; i < max; i++) { + if (isShusu(i)) { + if (count == container.length) { + container = grow(container,container.length << 1); + } + container[count] = i; + count++; + } + + } + int[] array = new int[count]; + System.arraycopy(container,0,array,0,count); + return array; + } + @Test + public void testGetPrimes() { + int[] newArray = getPrimes(4); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + private boolean isShusu(int num){ + int sqrt = 1; + while (sqrt * sqrt < num){ + sqrt++; + } + for (int i = 2; i < sqrt; i++) { + if (num % i == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] container = new int[5]; + int count = 0; + for (int i = 1; i < max; i++) { + if (isWanshu(i)) { + if (count == container.length) { + container = grow(container,container.length << 1); + } + container[count] = i; + count++; + } + + } + int[] array = new int[count]; + System.arraycopy(container,0,array,0,count); + return array; + } + @Test + public void testGetPerfectNumbers() { + int[] newArray = getPerfectNumbers(29); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } +// isWanshu(6); + } + private boolean isWanshu(int num){ + int sqrt = 1; + while (sqrt * sqrt < num){ + sqrt++; + } + int sum = 1; + for (int i = 2; i < sqrt; i++) { + if (num % i == 0 ) { + sum = sum + i + (num/i); + } + } + if (sum == num) { + return true; + } + return false; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator){ + String result = ""; + + for (int i = 0; i < array.length; i++) { + if (i != 0) { + result = result + seperator; + } + result = result + array[i]; + } + + return result; + } + @Test + public void testJoin() { + int[] test = {1, 5, 8, 4}; + String seperator = "-"; + String join = join(test, seperator); + System.out.println(join); + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Action.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Action.java new file mode 100644 index 0000000000..3a354754a4 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Action.java @@ -0,0 +1,41 @@ +package org.xukai.coderising.litestruts; + +import java.util.Map; + +/** + * @author xukai + * @desc + * @date 2017-02-27-下午 2:59 + */ +public class Action { + + private String name; + + private Class aClass; + + private Map resultMapping; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Class getaClass() { + return aClass; + } + + public void setaClass(Class aClass) { + this.aClass = aClass; + } + + public Map getResultMapping() { + return resultMapping; + } + + public void setResultMapping(Map resultMapping) { + this.resultMapping = resultMapping; + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/BusinessException.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/BusinessException.java new file mode 100644 index 0000000000..29a74ec3be --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/BusinessException.java @@ -0,0 +1,29 @@ +package org.xukai.coderising.litestruts; + +/** + * 业务逻辑异常 + */ +public class BusinessException extends Exception { + + private static final long serialVersionUID = 1L; + + public BusinessException() { + + } + + public BusinessException(String message) { + super(message); + + } + + public BusinessException(Throwable cause) { + super(cause); + + } + + public BusinessException(String message, Throwable cause) { + super(message, cause); + + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LoginAction.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..2681d32a1c --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package org.xukai.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/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LogoutAction.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..a40fe43dc4 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LogoutAction.java @@ -0,0 +1,39 @@ +package org.xukai.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LogoutAction { + 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/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Struts.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..524b0634e8 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Struts.java @@ -0,0 +1,133 @@ +package org.xukai.coderising.litestruts; + +import org.apache.commons.lang.StringUtils; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.xukai.coderising.util.ReflectUtil; +import org.xukai.coderising.util.XmlParseHelper; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + + +public class Struts { + + private static final ConcurrentHashMap action= new ConcurrentHashMap(); + + private static String strutsPath = Thread.currentThread().getContextClassLoader().getResource("struts.xml").getPath().substring(1); + + private static List actions; + + static { + try { + actions = initalStruts(); + System.out.println(actions.size()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static View runAction(String actionName, Map parameters) { + if (actionName != null && !actionName.equals("")) { + for(Action action : actions){ + //根据请求参数定位到需要执行的action + if (action.getName().equals(actionName)) { + Class cls = action.getaClass(); + View view = new View(); + try { + //创建action实例 + Object obj = ReflectUtil.newInstance(cls); + for(Map.Entry entry : parameters.entrySet()){ + ReflectUtil.setValue(obj,entry.getKey(),entry.getValue()); + } + Method method = ReflectUtil.getMethod(cls,"execute"); + if (method != null) { + //执行execute方法 + Object result = ReflectUtil.invokeMethod(obj, method); + if (result instanceof String) { + Map mapping = action.getResultMapping(); + //根据映射关系将值放入 + view.setJsp(mapping.get(result)); + List gets = ReflectUtil.getMethodBeginWith(cls, "get"); + HashMap models = new HashMap<>(); + for(Method getMethod : gets){ + //调用所有getter方法 + Object getFieldResult = ReflectUtil.invokeMethod(obj, getMethod); + if (result instanceof String) { + String getFieldName = StringUtils.lowerCase((String) getMethod.getName()).substring(3); + models.put(getFieldName,(String) getFieldResult); + } + } + view.setParameters(models); + } + } + } catch (Exception e) { + e.printStackTrace(); + view.setJsp("500"); + } + return view; + } + } + } + //请求的参数不正确或者没有对应实例返回404 + View view = new View(); + view.setJsp("404"); + return view; + } + + private static List initalStruts() throws ClassNotFoundException, DocumentException { + SAXReader sr = new SAXReader();//获取读取方式 + + Document doc = sr.read(strutsPath); + XmlParseHelper helper = new XmlParseHelper(doc); + List actions = helper.getNodeByPath("//action"); + ArrayList actionsList = new ArrayList<>(); + for (Element action : actions){ + Action obj = new Action(); + String nameAttr = helper.getNodeAttrValue(action, "name"); + String classAttr = helper.getNodeAttrValue(action, "class"); + obj.setName(nameAttr); + obj.setaClass(Class.forName(classAttr)); + List results = helper.getChildNodeByName(action, "result"); + HashMap map = new HashMap<>(); + for (Element result : results){ + String resultNameAttr = helper.getNodeAttrValue(result, "name"); + String resultValue = helper.getNodeValue(result); + map.put(resultNameAttr,resultValue); + } + obj.setResultMapping(map); + actionsList.add(obj); + } + + return actionsList; + } + + + /* + + 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字段中。 + + */ + + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/StrutsTest.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..53bf155c33 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/StrutsTest.java @@ -0,0 +1,81 @@ +package org.xukai.coderising.litestruts; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Assert; +import org.junit.Test; +import org.xukai.coderising.util.XmlParseHelper; + + +public class StrutsTest { + + private String strutsPath = Thread.currentThread().getContextClassLoader().getResource("struts.xml").getPath() + .substring(1); + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + @Test + public void testInital() throws ClassNotFoundException { + SAXReader sr = new SAXReader();//获取读取方式 + try { + Document doc = sr.read(strutsPath); + XmlParseHelper helper = new XmlParseHelper(doc); + List actions = helper.getNodeByPath("//action"); + ArrayList actionsList = new ArrayList<>(); + for (Element action : actions){ + Action obj = new Action(); + String nameAttr = helper.getNodeAttrValue(action, "name"); + String classAttr = helper.getNodeAttrValue(action, "class"); + obj.setName(nameAttr); + obj.setaClass(Class.forName(classAttr)); + List results = helper.getChildNodeByName(action, "result"); + for (Element result : results){ + String resultNameAttr = helper.getNodeAttrValue(result, "name"); + String resultValue = helper.getNodeValue(result); + HashMap map = new HashMap<>(); + map.put("name",resultNameAttr); + map.put("viewPath",resultValue); + obj.setResultMapping(map); + } + actionsList.add(obj); + } + + } catch (DocumentException e) { + e.printStackTrace(); + } + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/View.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/View.java new file mode 100644 index 0000000000..4ce909e87c --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package org.xukai.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/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/ReflectUtil.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/ReflectUtil.java new file mode 100644 index 0000000000..7bca0d6506 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/ReflectUtil.java @@ -0,0 +1,148 @@ +package org.xukai.coderising.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +/** + * @author xukai + * @desc + * @date 2017-02-27-下午 4:19 + */ +public class ReflectUtil { + + private static final Logger logger = LoggerFactory.getLogger(ReflectUtil.class); + + public static Object newInstance(Class cls){ + Object instance = null; + try { + instance = cls.newInstance(); + } catch (Exception e) { + logger.error("new instance failure",e); + throw new RuntimeException(e); + } + return instance; + } + + + public static Object invokeMethod(Object obj, Method method,Object... args){ + Object result = null; + try { + method.setAccessible(true); + result = method.invoke(obj, args); + } catch (Exception e) { + logger.error("invoke method failure",e); + throw new RuntimeException(e); + } + return result; + } + + public static Method getMethod(Class cls, String methodName){ + Method result = null; + try { + Method[] methods = cls.getDeclaredMethods(); + for(Method method : methods){ + if (method.getName().equals(methodName)) { + result = method; + } + } + } catch (Exception e) { + logger.error("get method failure",e); + throw new RuntimeException(e); + } + return result; + } + + public static List getMethodBeginWith(Class cls, String methodName){ + ArrayList methodsList = new ArrayList<>(); + try { + Method[] methods = cls.getDeclaredMethods(); + for(Method method : methods){ + if (method.getName().startsWith(methodName)) { + methodsList.add(method); + } + } + } catch (Exception e) { + logger.error("get methods failure",e); + throw new RuntimeException(e); + } + return methodsList; + } + + public static void setField(Object obj, Field field, Object values){ + try { + field.setAccessible(true); + field.set(obj,values); + } catch (Exception e) { + logger.error("set field failure",e); + throw new RuntimeException(e); + } + } + + /** + * 通过反射取对象指定字段(属性)的值 + * @param target 目标对象 + * @param fieldName 字段的名字 + * @throws RuntimeException 如果取不到对象指定字段的值则抛出异常 + * @return 字段的值 + */ + public static Object getValue(Object target, String fieldName) { + Class clazz = target.getClass(); + String[] fs = fieldName.split("\\."); + + try { + for(int i = 0; i < fs.length - 1; i++) { + Field f = clazz.getDeclaredField(fs[i]); + f.setAccessible(true); + target = f.get(target); + clazz = target.getClass(); + } + + Field f = clazz.getDeclaredField(fs[fs.length - 1]); + f.setAccessible(true); + return f.get(target); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 通过反射给对象的指定字段赋值 + * @param target 目标对象 + * @param fieldName 字段的名称 + * @param value 值 + */ + public static void setValue(Object target, String fieldName, Object value) { + Class clazz = target.getClass(); + String[] fs = fieldName.split("\\."); + try { + for(int i = 0; i < fs.length - 1; i++) { + Field f = clazz.getDeclaredField(fs[i]); + f.setAccessible(true); + Object val = f.get(target); + if(val == null) { + Constructor c = f.getType().getDeclaredConstructor(); + c.setAccessible(true); + val = c.newInstance(); + f.set(target, val); + } + target = val; + clazz = target.getClass(); + } + + Field f = clazz.getDeclaredField(fs[fs.length - 1]); + f.setAccessible(true); + f.set(target, value); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/XmlParseHelper.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/XmlParseHelper.java new file mode 100644 index 0000000000..71b200aff8 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/XmlParseHelper.java @@ -0,0 +1,44 @@ +package org.xukai.coderising.util; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.xpath.DefaultXPath; + +import java.util.List; + +/** + * @author xukai + * @desc + * @date 2017-02-27-下午 2:28 + */ +public class XmlParseHelper { + + private Document doc; + + private DefaultXPath xPath; + + public XmlParseHelper(Document doc) { + if (doc == null) { + throw new RuntimeException("构造xml解析器出错"); + } + this.doc = doc; + } + + public List getNodeByPath(String path){ + xPath = new DefaultXPath(path); + return xPath.selectNodes(doc); + } + + public List getChildNodeByName(Element parentNode,String childNodeName){ + xPath = new DefaultXPath("./" + childNodeName); + return xPath.selectNodes(parentNode); + } + + public String getNodeAttrValue(Element node,String attrName){ + return node.attributeValue(attrName); + } + + public String getNodeValue(Element node){ + return node.getTextTrim(); + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/ArrayList.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/ArrayList.java new file mode 100644 index 0000000000..0a8601c8ee --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/ArrayList.java @@ -0,0 +1,102 @@ +package org.xukai.common; + + + +public class ArrayList implements List { + + private int size = 0; + + private static final int DEFAULT_CAPICITY = 10; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPICITY]; + } + + public ArrayList(int size) { + elementData = new Object[size]; + } + + private Object[] elementData ; + + public void add(Object o){ + elementData[size] = o; + size++; + } + public void add(int index, Object o){ + if (index < 0 || index > size ) { + throw new ArrayIndexOutOfBoundsException(); + } else { + if (isNeedResize(index)) { + elementData = grown(elementData, Math.max(elementData.length << 1, index + 1)); + } + if(index < size){ + System.arraycopy(elementData,index,elementData,index+1,size-index); + } + elementData[index] = o; + } + size++; + } + + + + public Object get(int index){ + if (index > size-1 || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if (index > size-1 || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + Object result = elementData[index]; + size--; + System.arraycopy(elementData,index+1,elementData,index,size-index); + return result; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + class ArrayListIterator implements Iterator { + + private int pos = -1; + + @Override + public boolean hasNext() { + return pos < size - 1; + } + + @Override + public Object next() { + pos++; + return elementData[pos]; + } + } + + public void display(){ + System.out.print("["); + Iterator iterator = iterator(); + while (iterator.hasNext()){ + System.out.print(" " + iterator.next()); + } + System.out.print(" ]"); + } + + private boolean isNeedResize(int index){ + return index > elementData.length-1 || size > elementData.length-1; + } + + private Object[] grown(Object[] src,int capacity){ + Object[] des = new Object[capacity]; + System.arraycopy(elementData,0,des,0,elementData.length); + return des; + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/BinaryTreeNode.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/BinaryTreeNode.java new file mode 100644 index 0000000000..2ff876cac5 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/BinaryTreeNode.java @@ -0,0 +1,53 @@ +package org.xukai.common; + +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(Comparable o){ + if (data == null) { + data = o; + return this; + } else { + BinaryTreeNode node = new BinaryTreeNode(); + node.data = o; + if (o.compareTo(data) < 0) { + if (left == null) { + left = node; + return node; + } else { + return left.insert(o); + } + } else { + if (right == null) { + right = node; + return node; + } else { + return right.insert(o); + } + } + } + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Iterator.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Iterator.java new file mode 100644 index 0000000000..d81ade6ba5 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Iterator.java @@ -0,0 +1,7 @@ +package org.xukai.common; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/LinkedList.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/LinkedList.java new file mode 100644 index 0000000000..cb1585cecf --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/LinkedList.java @@ -0,0 +1,163 @@ +package org.xukai.common; + +public class LinkedList implements List { + + private Node head; + + private int size = 0; + + public void add(Object o){ + Node node = new Node(); + node.data = o; + if (head == null) { + head = node; + } else { + Node next = head.next; + if (next == null) { + head.next = node; + } else { + while (next.next != null){ + next = next.next; + } + next.next = node; + } + } + size++; + } + + public void add(int index , Object o){ + if (index < 0 || index > size ) { + throw new ArrayIndexOutOfBoundsException(); + } else { + size++; + Node node = new Node(); + node.data = o; + if (index == 0) { + node.next = head; + head = node; + return; + } + int pos = 1; + Node next = head; + //index 2 + while(index > pos){ + next = next.next; + pos++; + } + node.next = next.next; + next.next = node; + } + } + public Object get(int index){ + if (index < 0 || index > size ) { + throw new ArrayIndexOutOfBoundsException(); + } else { + if (index == 0) { + return head.data; + } + int pos = 1; + Node next = head; + //index 2 + while(index > pos){ + next = next.next; + pos++; + } + return next.data; + } + } + + public Object remove(int index){ + if (index < 0 || index > size - 1 ) { + throw new ArrayIndexOutOfBoundsException(); + } else { + if (index == 0) { + Node result = head; + head = head.next; + return result.data; + } + int pos = 1; + Node next = head; + //index 1 + while(index > pos){ + next = next.next; + pos++; + } + Node result = next.next; + next.next = next.next.next; + size--; + return result.data; + } + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + Node result = head; + head = head.next; + size--; + return result.data; + } + + public Object removeLast(){ + return remove(size-1); + } + + public Iterator iterator(){ + return new LinkedListIterator(); + } + + class LinkedListIterator implements Iterator { + + private Node currentNode ; + + @Override + public boolean hasNext() { + if (currentNode == null) { + if (head != null) { + return true; + } else { + return false; + } + } + return currentNode.next != null; + } + + @Override + public Object next() { + if (currentNode == null) { + currentNode = head; + return currentNode.data; + } + currentNode = currentNode.next; + return currentNode.data; + } + } + + public void display(){ + System.out.print("["); + Iterator iterator = iterator(); + while (iterator.hasNext()){ + System.out.print(" " + iterator.next()); + } + System.out.print(" ]"); + } + + private static class Node{ + Object data; + Node next; + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/List.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/List.java new file mode 100644 index 0000000000..ab5dcb4178 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/List.java @@ -0,0 +1,9 @@ +package org.xukai.common; + +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/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Queue.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Queue.java new file mode 100644 index 0000000000..ad77cee9ae --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Queue.java @@ -0,0 +1,27 @@ +package org.xukai.common; + +import java.util.NoSuchElementException; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + if (isEmpty()) { + throw new NoSuchElementException(); + } + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Stack.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Stack.java new file mode 100644 index 0000000000..de705e1fec --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/common/Stack.java @@ -0,0 +1,34 @@ +package org.xukai.common; + +import java.util.EmptyStackException; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size()-1); + } + + public Object peek(){ + if (isEmpty()) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/resources/struts.xml b/group19/527220084/xukai_coding/coding-common/src/main/resources/struts.xml new file mode 100644 index 0000000000..248d8bf7a5 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp> + /jsp/error.jsp> + + \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/ArrayListTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/ArrayListTest.java new file mode 100644 index 0000000000..b14feb5ab4 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/ArrayListTest.java @@ -0,0 +1,70 @@ +package org.xukai.common; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 2:02 + */ +public class ArrayListTest { + + @Test + public void testAdd() throws Exception { + ArrayList list = new ArrayList(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + Assert.assertTrue(list.size() == 5); + list.add(0,"3"); + Assert.assertTrue(list.get(0).equals("3")); + Assert.assertTrue(list.size() == 6); + list.remove(5); + Assert.assertTrue(list.size() == 5); + list.display(); + } + + @Test + public void testAdd1() throws Exception { + + } + + @Test + public void testGet() throws Exception { + + } + + @Test + public void testRemove() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } + + @Test + public void testIterator() throws Exception { + ArrayList list = new ArrayList(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + Iterator iterator = list.iterator(); + while (iterator.hasNext()){ + System.out.println(iterator.next()); + } + } + + @Test + public void testDisplay() throws Exception { + + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/BinaryTreeNodeTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..cc85baaa97 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/BinaryTreeNodeTest.java @@ -0,0 +1,28 @@ +package org.xukai.common; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 5:02 + */ +public class BinaryTreeNodeTest { + + + @Test + public void testInsert() throws Exception { + BinaryTreeNode node = new BinaryTreeNode(); + node.insert(5); + node.insert(9); + node.insert(3); + node.insert(7); + node.insert(2); + node.insert(8); + node.insert(4); + node.insert(6); + node.insert(1); + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/LinkedListTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/LinkedListTest.java new file mode 100644 index 0000000000..d31e3a70ec --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/LinkedListTest.java @@ -0,0 +1,91 @@ +package org.xukai.common; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 3:54 + */ +public class LinkedListTest { + + @Test + public void testAdd() throws Exception { + LinkedList list = new LinkedList(); + list.add("0"); + list.add("1"); + list.add("2"); + list.add("3"); + list.add("4"); + Assert.assertTrue(list.size() == 5); + list.add(0,"000"); + Assert.assertTrue( (list.get(0)).equals("000")); + Assert.assertTrue(list.size() == 6); + list.addFirst("111"); + Assert.assertTrue(list.get(0).equals("111")); + list.remove(5); + Assert.assertTrue(list.size() == 6); + list.addLast("111"); + Assert.assertTrue(list.size() == 7); + list.removeFirst(); + Assert.assertTrue(list.size() == 6); + list.removeLast(); + Assert.assertTrue(list.size() == 5); + list.remove(4); + Assert.assertTrue(list.size() == 4); + list.display(); + } + + @Test + public void testAdd1() throws Exception { + + } + + @Test + public void testGet() throws Exception { + + } + + @Test + public void testRemove() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } + + @Test + public void testAddFirst() throws Exception { + + } + + @Test + public void testAddLast() throws Exception { + + } + + @Test + public void testRemoveFirst() throws Exception { + + } + + @Test + public void testRemoveLast() throws Exception { + + } + + @Test + public void testIterator() throws Exception { + + } + + @Test + public void testDisplay() throws Exception { + + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/QueueTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/QueueTest.java new file mode 100644 index 0000000000..fa1a6e9f2c --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/QueueTest.java @@ -0,0 +1,46 @@ +package org.xukai.common; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 4:36 + */ +public class QueueTest { + + @Test + public void testEnQueue() throws Exception { + Queue queue = new Queue(); + Assert.assertTrue(queue.isEmpty()); + queue.enQueue("0"); + queue.enQueue("1"); + queue.enQueue("2"); + queue.enQueue("3"); + Assert.assertTrue(!queue.isEmpty()); + Assert.assertTrue(queue.deQueue().equals("0")); + Assert.assertTrue(queue.deQueue().equals("1")); + Assert.assertTrue(queue.size() == 2); + queue.deQueue(); + queue.deQueue(); + Assert.assertTrue(queue.isEmpty()); + } + + @Test + public void testDeQueue() throws Exception { + + } + + @Test + public void testIsEmpty() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/StackTest.java b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/StackTest.java new file mode 100644 index 0000000000..69fc9dee6e --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/test/java/org/xukai/common/StackTest.java @@ -0,0 +1,52 @@ +package org.xukai.common; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author xukai + * @desc + * @date 2017-02-20-下午 4:20 + */ +public class StackTest { + + @Test + public void testPush() throws Exception { + Stack stack = new Stack(); + Assert.assertTrue(stack.isEmpty()); + stack.push("0"); + stack.push("1"); + stack.push("2"); + stack.push("3"); + Assert.assertTrue(!stack.isEmpty()); + Assert.assertTrue(stack.peek().equals("3")); + Assert.assertTrue(stack.pop().equals("3")); + Assert.assertTrue(stack.size() == 3); + stack.pop(); + stack.pop(); + stack.pop(); + Assert.assertTrue(stack.isEmpty()); + } + + @Test + public void testPop() throws Exception { + + } + + @Test + public void testPeek() throws Exception { + + } + + @Test + public void testIsEmpty() throws Exception { + + } + + @Test + public void testSize() throws Exception { + + } +} \ No newline at end of file diff --git a/group19/527220084/xukai_coding/pom.xml b/group19/527220084/xukai_coding/pom.xml new file mode 100644 index 0000000000..fc66b7c29c --- /dev/null +++ b/group19/527220084/xukai_coding/pom.xml @@ -0,0 +1,429 @@ + + + 4.0.0 + + org.xukai.coding + xukai.coding + pom + 1.0-SNAPSHOT + + coding-common + + + + UTF-8 + 3.15.0-GA + 3.7.0.Final + 4.1.6.RELEASE + 2.6 + 3.4 + 1.7 + 2.3.21 + 3.2.8 + 1.2.2 + 5.1.29 + + 1.3 + 17.0 + 1.0.11 + 4.11 + 2.1.1 + 2.1.2 + 3.4.5 + 1.1.38 + 2.2.2 + 1.2-GUAHAO + 1.1.1 + 2.3.2 + 2.4.4 + 1.9 + 2.9.6 + 1.8 + 10.2.0.4.0 + 2.2.1 + 1.7.2 + 1.3 + 1.6.6 + 1.6.6 + 3.1 + 4.0 + 2.8.0 + 1.1.1 + 1.6.1 + + + + + + + org.slf4j + jcl-over-slf4j + ${jcl-over-slf4j.version} + + + + cglib + cglib + ${cglib.version} + + + org.quartz-scheduler + quartz + ${quartz.version} + + + org.apache.zookeeper + zookeeper + ${zookeeper.version} + + + com.github.sgroschupf + zkclient + 0.1 + + + org.springframework + spring-org.xukai.core.org.xukai.core.aop + ${spring.version} + + + org.springframework + spring-aspects + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} + + + org.springframework + spring-websocket + ${spring.version} + + + org.springframework + spring-messaging + ${spring.version} + + + org.springframework + spring-aop + ${spring.version} + + + org.springframework + spring-core + ${spring.version} + + + org.springframework + spring-webmvc + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-context-support + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-tx + ${spring.version} + + + org.springframework + spring-beans + ${spring.version} + + + org.springframework + spring-expression + ${spring.version} + + + + + + + + org.springframework + spring-oxm + ${spring.version} + + + org.springframework + spring-test + ${spring.version} + + + org.aspectj + aspectjweaver + ${aspectj.version} + + + commons-fileupload + commons-fileupload + ${commons-fileupload.version} + + + com.google.guava + guava + ${guava.version} + + + org.mybatis + mybatis-spring + ${mybatis.spring.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j-log4j12.version} + + + org.mybatis + mybatis + ${mybatis.version} + + + + mysql + mysql-connector-java + ${mysql.java.version} + + + com.oracle + ojdbc14 + ${oracle.version} + + + org.apache.velocity + velocity + ${velocity.version} + + + org.freemarker + freemarker + ${freemarker.version} + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + + + commons-lang + commons-lang + ${commons.lang.version} + + + org.apache.commons + commons-lang3 + ${commons.lang3.version} + + + commons-dbcp + commons-dbcp + ${commons.dbcp.version} + + + com.google.code.kaptcha + kaptcha + ${kaptcha.version} + + + com.alibaba + druid + ${druid.version} + + + org.slf4j + slf4j-api + 1.7.7 + + + org.apache.velocity + velocity-tools + 2.0 + + + log4j + log4j + 1.2.16 + + + dom4j + dom4j + ${dom4j.version} + + + javax.servlet + servlet-api + 2.5 + + + commons-codec + commons-codec + ${commons.codec} + + + commons-io + commons-io + 1.4 + + + javax.servlet + javax.servlet-api + 3.0.1 + + + junit + junit + ${junit.version} + + + org.apache.maven.plugins + maven-resources-plugin + 2.4.3 + + + com.alibaba + fastjson + ${fastjson.version} + + + com.greenline.common + greenline-common-util + ${greenline.common.util} + + + joda-time + joda-time + ${v.joda.time.version} + + + + javax.servlet + jsp-api + 2.0 + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + redis.clients + jedis + ${jedis.version}} + + + jaxen + jaxen + ${jaxen.version} + + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${v.p.mvn.compiler} + + ${project.ud.jdk} + ${project.ud.jdk} + UTF-8 + + ${project.build.sourceDirectory} + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8 + + true + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.8 + + utf-8 + utf-8 + + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-source-plugin + 2.3 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18.1 + + true + + + + + + + \ No newline at end of file diff --git "a/group19/604322962/cpu\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.txt" "b/group19/604322962/cpu\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.txt" new file mode 100644 index 0000000000..f20b27f7b0 --- /dev/null +++ "b/group19/604322962/cpu\345\206\205\345\255\230\347\241\254\347\233\230\346\214\207\344\273\244.txt" @@ -0,0 +1,3 @@ + 指令就是计算机直接用来控制计算机运行的代码,我们编写的java代码,在实际运行时也会被计算机在底层转化为各式各样的指令。 + 而cpu就是负责读取并解释这些指令的设备,cpu主要由几大部分组长,包括运算器、控制器和寄存器,控制器负责把指令、数据读取寄存器,运算器负责运算寄存器中的数据。但由于内存容量有限,成本高,并且断电之后里面的一切数据都会丢失,导致内存并不能作为我们永久存储程序的地方,而硬盘的价格便宜、容量大、断电后仍能保存数据,就可以作为我们长期存储程序和其他数据的地方。当我们需要运行某一个程序的时候,就从硬盘中将对应的数据读取到内存中,然后CPU再从内存中读取执行。 + cpu如果直接读取硬盘数据,由于硬盘的速度比内存慢了太多,会导致计算机运算能力严重降低。 diff --git a/group19/604322962/learning2017/src/main/java/com/coding/basic/ArrayList.java b/group19/604322962/learning2017/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..3f2fa618c5 --- /dev/null +++ b/group19/604322962/learning2017/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,53 @@ +package main.java.com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private static final int GORW_CAPACITY = 10; + public void add(Object o){ + if (sizeelementData.length) + throw new IndexOutOfBoundsException(); + } + +} diff --git a/group19/604322962/learning2017/src/main/java/com/coding/basic/BinaryTreeNode.java b/group19/604322962/learning2017/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..2701270b5d --- /dev/null +++ b/group19/604322962/learning2017/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package main.java.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/group19/604322962/learning2017/src/main/java/com/coding/basic/Iterator.java b/group19/604322962/learning2017/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..86643482fb --- /dev/null +++ b/group19/604322962/learning2017/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package main.java.com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group19/604322962/learning2017/src/main/java/com/coding/basic/LinkedList.java b/group19/604322962/learning2017/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..1d8b56ede4 --- /dev/null +++ b/group19/604322962/learning2017/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,143 @@ +package main.java.com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + private Node tail; + public void add(Object obj){ + linkLast(obj); + } + + public void add(int index , Object obj){ + if (index<0 || index>size) + throw new IndexOutOfBoundsException(); + else { + if (index == size) + linkLast(obj); + else { + Node node = linkIndex(index);//获取指定index的Node + Node pred = node.prev; + Node newNode = new Node(node.prev,obj,node); + node.prev = newNode; + if (pred==null) + head = newNode; + else + node.prev.next = newNode;//原链表指定节点前一个的节点的next指向新节点 + size++; + } + } + } + public Object get(int index){ + return linkIndex(index); + } + public Object remove(int index){ + Node node = linkIndex(index);//获取指定index的Node + Node prev = node.prev; + Node next = node.next; + if (prev==null) { + //此时删除的节点为头节点 + head = next; + } else { + prev.next = next; + node.prev = null; + } + if (next==null) { + //此时删除节点为尾节点 + tail = prev; + } else { + next.prev = prev; + node.next = null; + } + node.data = null; + size--; + return node; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newNode = new Node(null,o, head); + if (head == null) { + head = newNode; + } else { + newNode.next = head; + } + size++; + } + public void addLast(Object o){ + Node newNode = new Node(head,o, null); + if (head == null) { + head = newNode; + } else { + newNode.next = head; + } + size++; + } + public Object removeFirst(){ + if (head == null) { + throw new NoSuchElementException(); + } else { + Node next = head.next; + Object obj = head.data; + head.next = null; + head.data = null; + head = next; + if (next == null) { + tail = null; + } else { + next.prev = null; + } + size--; + return obj; + } + } + public Object removeLast(){ + tail = tail.prev; + head.next = null; + head.data = null; + return tail; + } + public Iterator iterator(){ + return null; + } + + /** + * 尾部添加一个节点 + * @param obj + */ + private void linkLast(Object obj){ + Node newNode = new Node(tail,obj, null); + if (tail == null) { + head = newNode; + tail = newNode; + } else + tail.next = newNode; + size++; + } + /** + * 获取指定index索引的Node节点 + * @param index + * @return + */ + private Node linkIndex(int index){ + Node n = head; + for (int i=0;i results; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getActionClass() { + return actionClass; + } + public void setActionClass(String actionClass) { + this.actionClass = actionClass; + } + public Map getResults() { + return results; + } + public void setResults(Map results) { + this.results = results; + } + +} diff --git a/group19/709960951/CodeLearning/src/com/coderising/litestruts/Struts.java b/group19/709960951/CodeLearning/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..536a6be705 --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,91 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + StrutsManager sm=StrutsManager.getInstance(); + StructsAction actionConfig=sm.getAction(actionName); + Class actionClz; + try { + actionClz = Class.forName(actionConfig.getActionClass()); + Object actionObj=actionClz.newInstance(); + //设置参数值 + for(String key:parameters.keySet()) + { + if(key!=null && key.length()>0) + { + String value=parameters.get(key); + String methodName="set"+key.substring(0, 1).toUpperCase()+key.substring(1); + Method method=actionClz.getMethod(methodName, String.class); + method.invoke(actionObj, value); + } + } + //执行 + Method executeMethod=actionClz.getMethod("execute"); + String result=(String)executeMethod.invoke(actionObj); + + //生成View对象 + View view=new View(); + String jsp=actionConfig.getResults().get(result); + view.setJsp(jsp); + //设置View的parameters + Map vParams=new HashMap<>(); + for(Method method:actionClz.getMethods()) + { + String methodName=method.getName(); + if(methodName.startsWith("get")) + { + Object paramValue=method.invoke(actionObj); + String paramKey=methodName.substring(3, 4).toLowerCase()+methodName.substring(4); + vParams.put(paramKey, paramValue); + } + } + view.setParameters(vParams); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return new View(); + } + +} diff --git a/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsManager.java b/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsManager.java new file mode 100644 index 0000000000..1af4b38bdc --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsManager.java @@ -0,0 +1,93 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +public class StrutsManager { + private static StrutsManager instance; + private Map actions; + private StrutsManager(){} + public static StrutsManager getInstance() + { + if(instance==null) + { + synchronized (StrutsManager.class) { + if(instance==null) + { + instance=new StrutsManager(); + instance.readStrutsXml(); + } + + } + } + return instance; + } + + /** + * 读取struts配置文件 + */ + private void readStrutsXml() { + actions = new HashMap<>(); + String path = StrutsManager.class.getResource("struts.xml").getFile(); + SAXReader reader = new SAXReader(); + Document doc; + try { + doc = reader.read(new File(path)); + Element root = doc.getRootElement(); + //遍历action节点 + for(Iterator childIter=root.elementIterator("action");childIter.hasNext();) + { + StructsAction action=new StructsAction(); + Element ele=(Element)childIter.next(); + String name=ele.attributeValue("name"); + String className=ele.attributeValue("class"); + //System.out.println(name+":"+className); + action.setName(name); + action.setActionClass(className); + Map results=new HashMap<>(); + //遍历result节点 + for(Iterator resultIter=ele.elementIterator("result");resultIter.hasNext();) + { + Element resultEle=(Element)resultIter.next(); + String resultName=resultEle.attributeValue("name"); + String resultUrl=resultEle.getText(); + results.put(resultName, resultUrl); + //System.out.println(resultName+":"+resultUrl); + } + action.setResults(results); + actions.put(action.getName(), action); + } + } catch (DocumentException e) { + e.printStackTrace(); + } + } + + public StructsAction getAction(String name) + { + return actions.get(name); + } + + public static void main(String[] argv) { + + StrutsManager instance=StrutsManager.getInstance(); + StructsAction actionObj=instance.getAction("login"); + System.out.println(actionObj.getName()); + System.out.println(actionObj.getActionClass()); + System.out.println(actionObj.getResults().get("success")); + System.out.println(actionObj.getResults().get("fail")); + try { + Class.forName("com.coderising.litestruts.LoginAction"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } +} diff --git a/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsTest.java b/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group19/709960951/CodeLearning/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/group19/709960951/CodeLearning/src/com/coderising/litestruts/View.java b/group19/709960951/CodeLearning/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group19/709960951/CodeLearning/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/group19/709960951/CodeLearning/src/com/coderising/litestruts/struts.xml b/group19/709960951/CodeLearning/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group19/709960951/CodeLearning/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/group19/709960951/CodeLearning/src/com/coding/basic/ArrayList.java b/group19/709960951/CodeLearning/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..9fa2bb7b02 --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coding/basic/ArrayList.java @@ -0,0 +1,109 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + private static final int INI_SIZE = 100; + private static final int EXTENDED_SIZE = 100; // 每次扩容的大小 + private int size = 0; + private Object[] elements = new Object[INI_SIZE]; + + @Override + public void add(int index, Object o) { + if (size < 1) { + index = 0; + } else { + if (index < 0) { + index = 0; + } + if (index > size - 1) { + index = size - 1; + } + } + + ensureCapacity(); + for (int i = size - 1; i >= index; i--) { + elements[i + 1] = elements[i]; + } + elements[index] = o; + size++; + } + + @Override + public void add(Object o) { + ensureCapacity(); + elements[size] = o; + size++; + } + + @Override + public Object get(int index) { + if (size < 1 || index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException(); + } + return elements[index]; + } + + @Override + public Object remove(int index) { + if (size < 1 || index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException(); + } + Object object = elements[index]; + for (int i = index; i < size - 1; i++) { + elements[i] = elements[i + 1]; + } + elements[size - 1] = null; + size--; + adjustCapacity(); // 调整数组至合适大小 + return object; + } + + @Override + public int size() { + return size; + } + + // 底层数组最多有2*EXTENDED_SIZE个多余空间 + private void adjustCapacity() { + if ((size + 2 * EXTENDED_SIZE) < elements.length) { + elements = Arrays.copyOf(elements, size + 2 * EXTENDED_SIZE); + } + } + + // 每次添加元素时,检查底层数组的长度,保证存储空间 + private void ensureCapacity() { + if (size == elements.length) { + elements = Arrays.copyOf(elements, elements.length + EXTENDED_SIZE); + } + } + + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + + private int curIndex = 0; + + @Override + public boolean hasNext() { + if (size > 0 && curIndex < size) { + return true; + } + return false; + } + + @Override + public Object next() { + if (!hasNext()) { + throw new IndexOutOfBoundsException(); + } + Object object = elements[curIndex]; + curIndex++; + return object; + } + + } + +} diff --git a/group19/709960951/CodeLearning/src/com/coding/basic/Iterator.java b/group19/709960951/CodeLearning/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..b8c8e0ce6a --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); +} diff --git a/group19/709960951/CodeLearning/src/com/coding/basic/LinkedList.java b/group19/709960951/CodeLearning/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..40374287c2 --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coding/basic/LinkedList.java @@ -0,0 +1,183 @@ +package com.coding.basic; + +public class LinkedList implements List { + private static class Node { + Object data; + Node next; + } + + private int size = 0; // 初始化size=0 + private Node head = null; + + @Override + public void add(Object o) { + Node node = new Node(); + node.data = o; + node.next = null; + if (head == null) { + head = node; + } else { + Node tail = head; + while (tail.next != null) { + tail = tail.next; + } + tail.next = node; + } + size++; + } + + @Override + public void add(int index, Object o) { + if (size < 1) { + index = 0; + } else { + if (index < 0) { + index = 0; + } + if (index > size - 1) { + index = size - 1; + } + } + Node p = null;// 插入位置的前一节点 + Node q = head; + while (index > 0) { + p = q; + q = q.next; + index--; + } + Node node = new Node(); + node.data = o; + node.next = null; + if (p == null) { + node.next = head; + head = node; + } else { + node.next = p.next; + p.next = node; + } + size++; + } + + @Override + public Object get(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException(); + } + Node p = head; + while (index > 0) { + p = p.next; + index--; + } + return p.data; + } + + @Override + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException(); + } + Node removeObj; + Node p = null; + Node q = head; + while (index > 0) { + p = q; + q = q.next; + index--; + } + if (p == null) { + removeObj = head; + head = head.next; + } else { + removeObj = p.next; + p.next = removeObj.next; + } + size--; + return removeObj.data; + } + + @Override + public int size() { + return size; + } + + public void addFirst(Object o) { + Node node = new Node(); + node.data = o; + + node.next = head; + + head = node; + size++; + } + + public void addLast(Object o) { + Node node = new Node(); + node.data = o; + + if (head == null) { + head = node; + } else { + Node p = head; + while (p.next != null) { + p = p.next; + } + p.next = node; + } + size++; + } + + public Object removeFirst() { + if (size < 1) { + throw new IndexOutOfBoundsException(); + } + Node removeObj = head; + head = head.next; + size--; + return removeObj.data; + } + + public Object removeLast() { + if (size < 1) { + throw new IndexOutOfBoundsException(); + } + Node removeObj; + if (head.next == null) { + removeObj = head; + head = null; + } else { + Node p = head; + while (p.next.next != null) { + p = p.next; + } + removeObj = p.next; + p.next = null; + } + size--; + return removeObj.data; + } + + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + + Node curNode = head; + + @Override + public boolean hasNext() { + return curNode != null; + } + + @Override + public Object next() { + if (!hasNext()) { + throw new IndexOutOfBoundsException(); + } + Object object = curNode.data; + curNode = curNode.next; + return object; + } + + } +} diff --git a/group19/709960951/CodeLearning/src/com/coding/basic/List.java b/group19/709960951/CodeLearning/src/com/coding/basic/List.java new file mode 100644 index 0000000000..e2f9e34aed --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +package com.coding.basic; + +public interface List { + void add(Object o); + + void add(int index, Object o); + + Object get(int index); + + Object remove(int index); + + int size(); +} diff --git a/group19/709960951/CodeLearning/src/com/coding/basic/Queue.java b/group19/709960951/CodeLearning/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..8ef14734c7 --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coding/basic/Queue.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + + public void enQueue(Object o) { + list.addFirst(o); + } + + public Object deQueue() { + if (isEmpty()) { + throw new IndexOutOfBoundsException(); + } + Object object = list.removeLast(); + return object; + } + + public boolean isEmpty() { + return list.size() == 0; + } + + public int size() { + return list.size(); + } +} diff --git a/group19/709960951/CodeLearning/src/com/coding/basic/Stack.java b/group19/709960951/CodeLearning/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..046ce549f1 --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coding/basic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic; + + +public class Stack { + + private ArrayList list=new ArrayList(); + public void push(Object o) + { + list.add(o); + } + public Object pop() + { + if(isEmpty()) + { + throw new IndexOutOfBoundsException(); + } + return list.remove(list.size()-1); + } + public Object peak() + { + if(isEmpty()) + { + throw new IndexOutOfBoundsException(); + } + return list.get(list.size()-1); + } + + public boolean isEmpty() { + return list.size()==0; + } + + public int size() { + return list.size(); + } +} diff --git a/group19/972815123/.classpath b/group19/972815123/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group19/972815123/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group19/972815123/.project b/group19/972815123/.project new file mode 100644 index 0000000000..aec36208a6 --- /dev/null +++ b/group19/972815123/.project @@ -0,0 +1,17 @@ + + + Homework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group19/972815123/.settings/org.eclipse.jdt.core.prefs b/group19/972815123/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group19/972815123/.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/group19/972815123/src/com/coderising/array/ArrayUtil.java b/group19/972815123/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..fca1de56e4 --- /dev/null +++ b/group19/972815123/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,221 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if(origin == null || origin.length <= 1){ + return; + } + int tem = 0; + for(int i = 0, len = origin.length; i < len/2; i ++){ + tem = origin[i]; + origin[i] = origin[len - i + 1]; + origin[len - i + 1] = tem; + } + + } + + /** + * 现在有如下的一个数组: 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 withoutZeroSize = 0; + for(int i = 0, len = oldArray.length; i < len; i ++ ){ + if( oldArray[i] == 0){ + withoutZeroSize ++; + } + } + int[] newArray = new int[withoutZeroSize]; + int point = 1; + for(int i = 0 ,len = oldArray.length; i < len; i ++ ){ + if( oldArray[i] != 0){ + newArray[point] = oldArray[i]; + point ++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int point2 = 0; + int[] result = new int[array1.length + array2.length]; + int point1 = 0, len1 = array1.length; + while(point1 < len1){ + if(array1[point1] < array2[point2]){ + result[point1 + point2] = array1[point1]; + point1 ++; + }else{ + result[point1 + point2] = array2[point2]; + point2 ++; + } + } + + return result; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + for(int i = 0, len = oldArray.length; i < len; i ++){ + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int[] result = {1,1,2}; + int a1 = 1, a2 = 2; + if(max <= 1){ + return null; + }else if(max == 2){ + return result; + }else{ + result = grow(result, 10); + int index = 2; + while(result[index] > max){ + if(result.length < index + 2){ + result = grow(result, 10); + } + result[index + 1] = result[index -1] + result[index]; + index ++; + } + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] temArr = null; + if(max < 2){ + return null; + }else if (max == 2){ + int[] re = {2}; + return re; + }else{ + temArr = new int[max/2]; + temArr[0] = 2; + int index = 1; + for(int i = 3; i < max ; i= i+2){ + boolean flag = true; + int isql = (int) Math.sqrt(i); + for(int j = 3; j < isql; j++){ + if(i % j == 0){ + flag = false; + } + } + if(flag){ + temArr[index] = i; + index ++; + } + } + } + temArr = this.removeZero(temArr); + return temArr; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] result = new int[10]; + int index = 0; + if(max < 6){ + return null; + }else{ + } + for (int n = 6; n <= max ; n ++){ + int[] allPrimeFactore = getPrimeFactors(n); + int sum = 0; + for(int i = 0, len = allPrimeFactore.length; i < len; i ++){ + sum += allPrimeFactore[i]; + } + if(sum == n){ + if(result.length < index + 1){ + result = this.grow(result, 1); + } + result[index] = n; + index ++; + } + + } + return removeZero(result); + } + + private int[] getPrimeFactors(int n){ + int[] allPrimes = getPrimes(n); + int[] result = new int[allPrimes.length]; + int index = 0; + for(int i = 0, len = allPrimes.length; i < len; i ++){ + int devide = n; + while(devide % allPrimes[i] == 0){ + devide = devide / allPrimes[i]; + result[index] = allPrimes[i]; + index ++; + } + } + return this.removeZero(result); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer sb = new StringBuffer(); + for(int i = 0, len = array.length; i < len; i ++){ + if(i != 0){ + sb.append(seperator); + } + sb.append(array[i]); + } + return sb.toString(); + } + + +} diff --git a/group19/972815123/src/com/coderising/litestruts/LoginAction.java b/group19/972815123/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..22f70d59e8 --- /dev/null +++ b/group19/972815123/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,45 @@ +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(){ + System.out.println("execute"); + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + System.out.println("successful"); + + return "success"; + } + System.out.println("failed"); + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + System.out.println("***name***"); + } + public void setPassword(String password){ + System.out.println("***pswd***"); + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group19/972815123/src/com/coderising/litestruts/Main.java b/group19/972815123/src/com/coderising/litestruts/Main.java new file mode 100644 index 0000000000..66f3e67edd --- /dev/null +++ b/group19/972815123/src/com/coderising/litestruts/Main.java @@ -0,0 +1,9 @@ +package com.coderising.litestruts; + +public class Main { + + public static void main(String[] args) { + Struts.runAction(null, null); + } + +} diff --git a/group19/972815123/src/com/coderising/litestruts/Struts.java b/group19/972815123/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..7d908282b6 --- /dev/null +++ b/group19/972815123/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,155 @@ +package com.coderising.litestruts; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import com.coding.basic.Iterator; + + + +public class Struts { + private static String url = "C:\\Users\\alioc\\workspace\\Homework\\src\\com\\coderising\\litestruts\\struts.xml"; + private static int SET = 1; + private static int GET = -1; + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + public static View runAction(String actionName, Map parameters) { + HashMap> xml = parseStrutsXml(url); + HashMap login = xml.get("login"); + String loginClassName = login.get("class"); + System.out.println(loginClassName); + + ClassLoader loader = ClassLoader.getSystemClassLoader(); + try { + Class clazz = Class.forName(loginClassName); + Object obj = clazz.newInstance(); + java.util.Iterator iter = parameters.keySet().iterator(); + while(iter.hasNext()){ + String key = iter.next(); + String val = parameters.get(key); + Method method = clazz.getDeclaredMethod(getEncapsulateMethodName(SET, key), String.class); + method.invoke(obj, val); + } + Method executeMethod = clazz.getDeclaredMethod("execute"); + String logResult = (String) executeMethod.invoke(obj); + if("success".equals(logResult)){ + View view = new View(); + view.setJsp(login.get("success")); + + Method getMessageMethod = clazz.getDeclaredMethod(getEncapsulateMethodName(GET, "message")); + String message = (String) getMessageMethod.invoke(obj); + +// Field messageField = clazz.getDeclaredField("message"); +// messageField.setAccessible(true); +// String message = (String) messageField.get(clazz); + Map map = new HashMap<>(); + map.put("message", message); + view.setParameters(map); + + return view; + } + } catch (Exception e) { + e.printStackTrace(); + } + + + + + + + return null; + } + + private static HashMap> parseStrutsXml(String url){ + + HashMap> result = new HashMap>(); + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = dbf.newDocumentBuilder(); + InputStream is = new FileInputStream(url); + Document doc = builder.parse(is); + Element root = doc.getDocumentElement(); + + NodeList actionNodes = root.getChildNodes(); + for(int i = 0; i < actionNodes.getLength(); i ++){ + Node actionNode = actionNodes.item(i); + if(actionNode.getNodeType() == Node.ELEMENT_NODE){ + if(actionNode.getAttributes() != null){ + HashMap action = new HashMap<>(); + String name = actionNode.getAttributes().getNamedItem("name").getNodeValue(); + String clazz = actionNode.getAttributes().getNamedItem("class").getNodeValue(); + result.put(name, action); + action.put("class", clazz); + + + NodeList resultNodes = actionNode.getChildNodes(); + + for(int j = 0; j < resultNodes.getLength(); j ++){ + Node resultNode = resultNodes.item(j); + if(resultNode.getNodeType() == Node.ELEMENT_NODE){ + String fieldName = resultNode.getAttributes().getNamedItem("name").getNodeValue(); + String fieldValue = resultNode.getTextContent(); + action.put(fieldName, fieldValue); + } + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("--------------------"); + System.out.println(result); + return result; + } + + private static String getEncapsulateMethodName(int type, String methodName){ + StringBuffer sb = new StringBuffer(); + if(type < 0){ + sb.append("get"); + }else{ + sb.append("set"); + } + sb.append(methodName.substring(0, 1).toUpperCase()); + sb.append(methodName.substring(1)); + return sb.toString(); + } +} diff --git a/group19/972815123/src/com/coderising/litestruts/StrutsTest.java b/group19/972815123/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..561885ccaf --- /dev/null +++ b/group19/972815123/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +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); + System.out.println(view.getJsp()); + + 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/group19/972815123/src/com/coderising/litestruts/View.java b/group19/972815123/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group19/972815123/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/group19/972815123/src/com/coderising/litestruts/struts.xml b/group19/972815123/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group19/972815123/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/group19/972815123/src/com/coding/basic/ArrayList.java b/group19/972815123/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..3e8afb1ffc --- /dev/null +++ b/group19/972815123/src/com/coding/basic/ArrayList.java @@ -0,0 +1,106 @@ +package com.coding.basic; + +public class ArrayList implements List, Iterator { + + private int size; + private Object[] data; + public ArrayList() { + data = new Object[10]; + } + + @Override + public void add(Object o) { + size = size(); + if(data.length <= size){ + grow(); + } + data[size] = o; + size++; + } + + /* (non-Javadoc) + * @see dataStructure.List#add(java.lang.Object, int) + * 在第index元素前插入元素 + */ + @Override + public void add(int index, Object o){ + if (index >= size()){ + return; + } + size = size(); + if(data.length <= size){ + grow(); + } + for(int i = size , len = size - index; i < len; i -- ){ + data[i] = data[i -1]; + } + data[index] = o; + size++; + } + + @Override + public Object get(int index) { + return data[index]; + } + + @Override + public int size() { + return size; + } + + @Override + public Object remove(int index) { + if (index >= size()){ + return null; + }else{ + Object o = data[index]; + for(int i = index; i < size; i ++){ + data[i] = data[i + 1]; + } + data[size] = null; + size--; + return o; + } + + } + + private void grow(){ + size = size(); + int length = 0; + if(size < 10000){ + length = size * 2; + }else{ + length = (int)(size * 1.5); + } + size = length; + + Object[] temData = new Object[length]; + for(int i = 0, j = data.length; i < j; i ++){ + temData[i] = data[i]; + } + data = temData; + } + + private int index = 0; + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + index++; + return data[index - 1]; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + for(int i = 0; i < size; i++){ + sb.append(data[i].toString() + ","); + } + sb.append("]"); + return sb.toString(); + } + +} diff --git a/group19/972815123/src/com/coding/basic/BinaryTreeNode.java b/group19/972815123/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d6518342d4 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,52 @@ +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){ + Comparable co = (Comparable)o; + Comparable coData = (Comparable)data; + BinaryTreeNode result = null; + if(co.compareTo(data) > 0){ + if(null == right){ + right = new BinaryTreeNode(); + right.data = o; + result = right; + return right; + }else{ + right.insert(o); + } + }else{ + if(null == left){ + left = new BinaryTreeNode(); + left.data = o; + result = left; + return left; + }else{ + left.insert(o); + } + } + return result; + } +} diff --git a/group19/972815123/src/com/coding/basic/Iterator.java b/group19/972815123/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..e7cbd474ec --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Iterator.java @@ -0,0 +1,6 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group19/972815123/src/com/coding/basic/LinkedList.java b/group19/972815123/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..04de763349 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/LinkedList.java @@ -0,0 +1,134 @@ +package com.coding.basic; + +public class LinkedList implements List,Iterator { + + private Node head; + private Node last; + private int size = 0; + + public LinkedList() { + head = new Node(); + } + + @Override + public void add(Object o) { + Node newNode = new Node(); + Node last = head; + while(last.next != null){ + last = last.next; + } + last.next = newNode; + newNode.prev = last; + last = newNode; + size++; + } + + @Override + public void add(int index, Object o) { + Node newNode = new Node(); + Node indexNode = head ; + int i = 0; + while(i == index){ + indexNode = indexNode.next; + i++; + } + Node indexNextNode = indexNode.next; + indexNode.next = newNode; + newNode.prev = indexNode; + newNode.next = indexNextNode; + indexNextNode.prev = newNode; + size ++; + } + + @Override + public Object get(int index) { + Node indexNode = head; + int i = 0; + while(i == index){ + indexNode = indexNode.next; + i++; + } + return indexNode; + } + + @Override + public int size() { + return size; + } + + @Override + public Object remove(int index) { + Node indexNode = head ; + int i = 0; + while(i == index){ + + indexNode = indexNode.next; + i++; + } + Object o = indexNode.prev; + Node indexNextNode = indexNode.next; + Node indexPrevNode = indexNode.prev; + + indexNextNode.prev = indexPrevNode; + indexPrevNode.next = indexNextNode; + + indexNode.next = null; + indexNode.prev = null; + size--; + return o; + } + + public void addFirst(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.next = head; + head.prev = newNode; + head = newNode; + size ++; + } + public void addLast(Object o){ + Node newNode = new Node(); + newNode.data = o; + newNode.prev = last; + last.next = newNode; + last = newNode; + size ++; + } + public Object removeFirst(){ + Node ret = head; + head = head.next; + head.prev = null; + size--; + return ret; + } + public Object removeLast(){ + Node ret = last; + last = last.prev; + last.next = null; + size--; + return ret; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + Node prev; + } + + private Node index = head; + @Override + public boolean hasNext() { + return index != null; + } + + @Override + public Object next() { + Node tem = index; + index = index.next; + return tem; + } +} diff --git a/group19/972815123/src/com/coding/basic/List.java b/group19/972815123/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group19/972815123/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/group19/972815123/src/com/coding/basic/MainTest.java b/group19/972815123/src/com/coding/basic/MainTest.java new file mode 100644 index 0000000000..504e2759e8 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/MainTest.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class MainTest { + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add("this is the fifth"); + System.out.println(list); + System.out.println(list.size()); + System.out.println(list.get(3)); + + while(list.hasNext()){ + System.out.println(list.next()); + } + } + +} diff --git a/group19/972815123/src/com/coding/basic/Queue.java b/group19/972815123/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..6f03f6052b --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Queue.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +public class Queue { + private LinkedList data; + private int size; + + public Queue(){ + data = new LinkedList(); + } + + public void enQueue(Object o){ + data.addLast(o); + size++; + } + + public Object deQueue(){ + size --; + return data.removeFirst(); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group19/972815123/src/com/coding/basic/Stack.java b/group19/972815123/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..fcfda97a71 --- /dev/null +++ b/group19/972815123/src/com/coding/basic/Stack.java @@ -0,0 +1,31 @@ +package com.coding.basic; + +public class Stack { + +private ArrayList elementData = new ArrayList(); +private int size = 0; + + public void push(Object o){ + elementData.add(o); + size ++; + } + + public Object pop(){ + if(size > 0){ + size--; + return elementData.remove(size); + }else{ + return null; + } + } + + public Object peek(){ + return elementData.get(size); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } +} diff --git a/group19/972815123/test.txt b/group19/972815123/test.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group19/976180558/.gitignore b/group19/976180558/.gitignore new file mode 100644 index 0000000000..3e2fcc7171 --- /dev/null +++ b/group19/976180558/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group19/976180558/src/com/coding/basic/ArrayList.java b/group19/976180558/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..57412dcf7f --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/BinaryTreeNode.java b/group19/976180558/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/Iterator.java b/group19/976180558/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/LinkedList.java b/group19/976180558/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..1fd99bf26b --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/List.java b/group19/976180558/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/Queue.java b/group19/976180558/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..08d2d86b14 --- /dev/null +++ b/group19/976180558/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/group19/976180558/src/com/coding/basic/Stack.java b/group19/976180558/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..4bfe28057f --- /dev/null +++ b/group19/976180558/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/group19/group19.md b/group19/group19.md index d3f5a12faa..8b13789179 100644 --- a/group19/group19.md +++ b/group19/group19.md @@ -1 +1 @@ - + diff --git a/group20/1040154728/1040154728Learning/src/ArrayList.java b/group20/1040154728/1040154728Learning/src/ArrayList.java new file mode 100644 index 0000000000..77fed2b0f4 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/ArrayList.java @@ -0,0 +1,46 @@ +public class ArrayList implements List { + //private fields + private int size = 0; + private Object[] elementData = new Object[100]; + //check if list is empty + public boolean isEmpty() { + return size == 0; + } + + public void add(Object o){ + elementData[size++] = o; + } + public void add(int index, T o){ + /* Not familiar with array copy, copied from GitMori */ + System.arraycopy(elementData, index, elementData, + index + 1, size-index); + elementData[index] = o; + size++; + } + + public T get(int index){ + return (T) elementData[index]; + } + + public T remove(int index){ + T t = this.get(index); + int position = size - index - 1; //why ? + if (position > 0){ + System.arraycopy(elementData, index+1, elementData, + index, position); + } + elementData[--size] = null; + return t; + } + + public int size(){ + return size; + } + + //?? + public Iterator iterator(){ + return null; + } + + +} diff --git a/group20/1040154728/1040154728Learning/src/BinaryTreeNode.java b/group20/1040154728/1040154728Learning/src/BinaryTreeNode.java new file mode 100644 index 0000000000..425894819d --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/BinaryTreeNode.java @@ -0,0 +1,28 @@ +public class BinaryTreeNode { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + 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; + } + + + +} diff --git a/group20/1040154728/1040154728Learning/src/Iterator.java b/group20/1040154728/1040154728Learning/src/Iterator.java new file mode 100644 index 0000000000..0e1a09433b --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/Iterator.java @@ -0,0 +1,5 @@ +public interface Iterator { + public boolean hasNext(); + public T next(); + +} diff --git a/group20/1040154728/1040154728Learning/src/LinkedList.java b/group20/1040154728/1040154728Learning/src/LinkedList.java new file mode 100644 index 0000000000..18f6fddf37 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/LinkedList.java @@ -0,0 +1,110 @@ +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size; + + public void add(T o){ + Node node = new Node(o); + if(head == null) + { + head = node; + } + else + { + tail.next = node; + } + tail = node; + tail.next = null; + size++; + + } + public void add(int index , T o){ + Node node = new Node(o); + Node temp = head; + Node tempTemp = null; + for(int i = 0; i <= index; i++) + { + temp = temp.next; + } + tempTemp = temp.next; + temp.next = node; + node.next = tempTemp; + size++; + } + public T get(int index){ + Node temp = head; + for(int i = 0; i <= index; i++) + { + temp = temp.next; + } + return (T)temp.data; + } + public T remove(int index){ + if(index == 0){ + T o = (T) removeFirst(); + return o; + } + Node temp = head; + Node tempTemp = null; + for(int i = 0; i <= index; i++) + { + temp = temp.next; + } + T o = (T) temp.next.data; + tempTemp = temp.next.next; + temp.next = null; + temp.next = tempTemp; + size--; + return o; + } + + public int size(){ + return size; + } + + @Override + public boolean isEmpty() { + return false; + } + + public void addFirst(T o){ + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + public void addLast(T o){ + this.add(o); + } + public T removeFirst(){ + T o = (T) head.data; + head = head.next; + size--; + return o; + } + public Object removeLast(){ + Node temp = head; + for(int i = 0; i <= size; i++) + { + temp = temp.next; + } + temp.next = null; + T o = (T) tail.data; + tail = temp; + size--; + return o; + } + public Iterator iterator(){ + return null; + } + + + private class Node{ + T data; + Node next; + + public Node(T o) { + } + } +} diff --git a/group20/1040154728/1040154728Learning/src/List.java b/group20/1040154728/1040154728Learning/src/List.java new file mode 100644 index 0000000000..eb27d39f6b --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/List.java @@ -0,0 +1,8 @@ +public interface List { + public void add(T object); + public void add(int index, T object); + public T get(int index); + public T remove(int index); + public int size(); + boolean isEmpty(); +} diff --git a/group20/1040154728/1040154728Learning/src/Queue.java b/group20/1040154728/1040154728Learning/src/Queue.java new file mode 100644 index 0000000000..b7e03881fe --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/Queue.java @@ -0,0 +1,22 @@ +public class Queue { + + private LinkedList element = new LinkedList(); + + + + public void enQueue(T o){ + element.addLast(o); + } + + public T deQueue(){ + return element.removeFirst(); + } + + public boolean isEmpty(){ + return element.isEmpty(); + } + + public int size(){ + return element.size(); + } +} diff --git a/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java b/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java deleted file mode 100644 index 7cdf1e7f26..0000000000 --- a/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java +++ /dev/null @@ -1,75 +0,0 @@ -package SinglyLinkedList; - -/** - * Created by Honoka on 2/16/2017. - */ -public class LinkedList0 { - //Node class represents a list node - private class Node - { - String value; - Node next; - /** - * Constructor - * @param val The element to store in this node. - * @param n The reference to the next node. - */ - Node (String val, Node n) - { - value = val; - next = n; - } - - /** - * Constructor - * @param val The element to store in this node. - */ - Node(String val) - { - value = val; - next = null; - } - } - //Reference to the first node in the list - private Node first = null; - /** - * Constructor - * Builds a linked list - */ - public LinkedList0() - { - //test - first = new Node("Apple"); - first.next = new Node("Peach"); - first.next.next = new Node("Kiwi"); - first = new Node("Blueberry",first); - - //Using an array to add elements into list - String[] fruits = {"Banana", "Cherry"}; - for (String f : fruits) - { - first = new Node(f, first); - } - } - /** - * print method - * traverses the list and prints all elements - */ - public void print() - { - Node reference = first; - while(reference != null) - { - System.out.println(reference.value + " "); - reference = reference.next; - } - } - - //Main test method - public static void main(String [] args) - { - LinkedList0 list = new LinkedList0(); - System.out.println("The elements inside this list are "); - list.print(); - } -} diff --git a/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java b/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java deleted file mode 100644 index 8c93bbc640..0000000000 --- a/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java +++ /dev/null @@ -1,242 +0,0 @@ -package SinglyLinkedList2; -/** - * Created by Honoka on 2/16/2017. - */ -public class LinkedList1 { - private class Node - { - String value; - Node next; - - Node(String val, Node n) - { - value = val; - next = n; - } - Node(String val) - { - //Call the other(daddy(or sister(whatever))) constructor. - this(val, null); - } - } - - private Node first; // head - private Node last; //the last element in list - - public LinkedList1() - { - first = null; - last = null; - } - - /**This method checks to see - * if the list is empty - * @return true if list is empty - */ - public boolean isEmpty() - { - return first == null; - } - - /** - * size method returns the length of the list - * @return The number of the elements in the list - */ - public int size() - { - int counter = 0; - Node p = first; - while (p != null) - { - counter ++; - p = p.next; - } - return counter; - } - - /** - * add method add an element to the end of the list - * @param element the value to add - */ - public void add(String element) - { - if (isEmpty()) - { - //Obviously, add the element to the first position in the list - first = new Node(element); - last = first; - } - else - { - //add to the end of existing list - last.next = new Node(element); - last = last.next; - } - } - - /** - * add method, or you might call it insert method since it can - * add element to a specific position - * @param index The position at which to add the element - * @param element you should know what is this - */ - public void add (int index, String element) - { - if (index < 0 || index > size()) - { - String message = String.valueOf(index); - throw new IndexOutOfBoundsException(message); - } - - //index is at least 0 - if(index == 0) - { - //new element add to the head - first = new Node(element, first); - if (last == null) - { - last = first; - } - return; - } - //set a reference predecessor to point to the node that - //will be the predecessor of the new node - Node predecessor = first; - for (int k = 1; k <= index - 1; k++) - { - predecessor = predecessor.next; - } - //Splice in a node containing the new element - predecessor.next = new Node(element, predecessor.next); - - //if there is a new last element - if(predecessor.next.next == null) - last = predecessor.next; - } - - /** - * toString method, like print method, hopefully it will display the contents of the list - * @return say something I'm giving up on you( - */ - public String toString() - { - StringBuffer strBuilder = new StringBuffer(); - //Use p to walk down the list - Node p = first; - while (p != null) - { - strBuilder.append(p.value + "\n"); - p = p.next; - } - return strBuilder.toString(); - } - - /** - * remove method removes the element with the position you want - * @param index the position of the element that you want to remove - * @return the removed element - */ - public String remove (int index) - { - /* Index out of bounds */ - if (index < 0 || index >= size()) - { - String message = String.valueOf(index); - throw new IndexOutOfBoundsException(message); - } - String element = null; - if(index == 0) - { - //Removal of first item in the list - element = first.value; - first = first.next; - if (first == null) - { - last = null; - } - } - else - { - /* find the predecessor of the element to be removed */ - Node predecessor = first; - - /* Move predecessor forward index - 1 times */ - for (int k = 1; k <= index - 1; k++) - { - predecessor = predecessor.next; - /* Store the value to return */ - element = predecessor.next.value; - /* Route link around the node to be removed */ - predecessor.next = predecessor.next.next; - /* Check if predecessor is now last */ - if(predecessor.next == null) - { - last = predecessor; - } - } - } - return element; - } - - /** - * The remove method removes an element - * @param element the element to remove - * @return true if the remove succeeded - */ - public boolean remove(String element) - { - if (isEmpty()) - { - return false; - } - - if (element.equals(first.value)) - { - //Removal of first element in the list - first = first.next; - if(first == null) - { - last = null; - } - return true; - } - - /* Find the predecessor of the element to remove */ - Node predecessor = first; - while (predecessor.next != null && - !predecessor.next.value.equals(element)) - { - predecessor = predecessor.next; - } - /* predecessor.next == null OR predecessor.next.value is element */ - if(predecessor.next == null) - { - return false; - } - /* predecessor.next.value is element */ - predecessor.next = predecessor.next.next; - - /* check if predecessor is now last */ - if (predecessor.next == null) - { - last = predecessor; - } - return true; - } - - public static void main (String [] args) - { - LinkedList1 testList = new LinkedList1(); - testList.add("Apple"); - testList.add("Banana"); - testList.add(0,"Blueberry"); - testList.add(2,"Cherry"); - testList.add(4,"Peach"); - System.out.println("The list has : "); - System.out.println(testList); - testList.remove("Cherry"); - testList.remove(2); - System.out.println("The list has : "); - System.out.println(testList); - } -} diff --git a/group20/1040154728/1040154728Learning/src/Stack.java b/group20/1040154728/1040154728Learning/src/Stack.java new file mode 100644 index 0000000000..959b081239 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/Stack.java @@ -0,0 +1,21 @@ +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.isEmpty(); + } + public int size(){ + return elementData.size(); + } +} diff --git a/group20/1107837739/1107837739Learning/lib/dom4j-1.6.1.jar b/group20/1107837739/1107837739Learning/lib/dom4j-1.6.1.jar new file mode 100644 index 0000000000..c8c4dbb92d Binary files /dev/null and b/group20/1107837739/1107837739Learning/lib/dom4j-1.6.1.jar differ diff --git a/group20/1107837739/1107837739Learning/lib/fastjson-1.2.7.jar b/group20/1107837739/1107837739Learning/lib/fastjson-1.2.7.jar new file mode 100644 index 0000000000..ce431a92c3 Binary files /dev/null and b/group20/1107837739/1107837739Learning/lib/fastjson-1.2.7.jar differ diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtil.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..87ea998621 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtil.java @@ -0,0 +1,258 @@ +package org.korben.coderising.array; + +import java.util.ArrayList; +import java.util.List; + +/** + * ArrayUtil + * + * Created by Korben on 26/02/2017. + */ +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + */ + public static void reverseArray(int[] origin) { + ensureNotNull(origin); + + int length = origin.length; + for (int i = 0; i < length / 2; i++) { + int 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} + */ + public static int[] removeZero(int[] oldArray) { + ensureNotNull(oldArray); + + int nonZeroCount = 0; + for (int i : oldArray) { + if (i != 0) { + nonZeroCount++; + } + } + + int newArr[] = new int[nonZeroCount]; + int newArrIndex = 0; + for (int i : oldArray) { + if (i != 0) { + newArr[newArrIndex++] = i; + } + } + + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + */ + public static int[] merge(int[] array1, int[] array2) { + ensureNotNull(array1); + ensureNotNull(array2); + + int maxArraySize = array1.length + array2.length; + int[] mergedArray = new int[maxArraySize]; + + int index1 = 0; + int index2 = 0; + int mergedIndex = -1; + for (int i = 0; i < maxArraySize; i++) { + if (index1 == array1.length) { + System.arraycopy(array2, index2, mergedArray, mergedIndex + 1, array2.length - index2); + mergedIndex += array2.length - index2; + break; + } else if (index2 == array2.length) { + System.arraycopy(array1, index1, mergedArray, mergedIndex + 1, array1.length - index1); + mergedIndex += array1.length - index1; + break; + } else { + int compare = Integer.compare(array1[index1], array2[index2]); + if (compare < 0) { + mergedArray[++mergedIndex] = array1[index1++]; + } else if (compare > 0) { + mergedArray[++mergedIndex] = array2[index2++]; + } else { + mergedArray[++mergedIndex] = array1[index1++]; + index2++; + } + } + } + + // 清除数组多余部分 + if (mergedIndex + 1 < maxArraySize) { + int[] resultArray = new int[mergedIndex + 1]; + System.arraycopy(mergedArray, 0, resultArray, 0, mergedIndex + 1); + return resultArray; + } + + return mergedArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + */ + public static int[] grow(int[] oldArray, int size) { + ensureNotNull(oldArray); + + if (size < 0) { + throw new IllegalArgumentException("size must > 0"); + } + + int[] newArray = new int[oldArray.length + size]; + + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + for (int i = oldArray.length; i < newArray.length; i++) { + newArray[i] = 0; + } + + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + */ + public static int[] fibonacci(int max) { + if (max == 1) { + int[] array = new int[1]; + array[1] = 1; + return array; + } + + List list = new ArrayList<>(); + + for (int i = 1; ; i++) { + int fibonacciNumber = getFibonacciNumber(i); + if (fibonacciNumber <= max) { + list.add(fibonacciNumber); + } else { + break; + } + } + + return list2Array(list); + } + + private static int getFibonacciNumber(int index) { + if (index == 1) { + return 1; + } + if (index == 2) { + return 1; + } + return getFibonacciNumber(index - 2) + getFibonacciNumber(index - 1); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + */ + public static int[] getPrimes(int max) { + List primeList = new ArrayList<>(); + if (max <= 1) { + return new int[0]; + } + + if (max >= 2) { + primeList.add(2); + } + + // 所有偶数都不是素数, 所以这里采用 i += 2 + for (int i = 3; i < max; i += 2) { + if (isPrimeNumber(i, primeList)) { + primeList.add(i); + } + } + + return list2Array(primeList); + } + + private static boolean isPrimeNumber(int number, List primeList) { + for (Integer prime : primeList) { + if (number % prime == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + */ + public static int[] getPerfectNumbers(int max) { + if (max <= 1) { + return new int[0]; + } + + List perfectNumberList = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { + perfectNumberList.add(i); + } + } + return list2Array(perfectNumberList); + } + + private static boolean isPerfectNumber(int number) { + int sum = 1; + for (int i = 2; i <= number / 2; i++) { + if (number % i == 0) { + sum += i; + } + } + + return sum == number; + } + + /** + * 用separator 把数组 array给连接起来 + * 例如array= [3,8,9], separator = "-" + * 则返回值为"3-8-9" + */ + public static String join(int[] array, String separator) { + ensureNotNull(array); + + if (separator == null) { + throw new NullPointerException(); + } + + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + stringBuilder.append(array[i]); + if (i != array.length - 1) { + stringBuilder.append(separator); + } + } + + return stringBuilder.toString(); + } + + private static int[] list2Array(List list) { + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + + private static void ensureNotNull(int[] array) { + if (array == null) { + throw new NullPointerException(); + } + } +} \ No newline at end of file diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtilTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..9f23e2c341 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtilTest.java @@ -0,0 +1,185 @@ +package org.korben.coderising.array; + +import org.junit.Assert; +import org.junit.Test; + +/** + * ArrayUtil test + * + * Created by Korben on 26/02/2017. + */ +public class ArrayUtilTest { + + @Test + public void reverseArray() throws Exception { + // test reverse even number + { + int[] testArray = new int[5]; + for (int i = 0; i < 5; i++) { + testArray[i] = i; + } + ArrayUtil.reverseArray(testArray); + for (int i = 0; i < 5; i++) { + Assert.assertEquals(5 - 1 - i, testArray[i]); + } + } + + // test reverse odd number + { + int[] testArray = new int[4]; + for (int i = 0; i < 4; i++) { + testArray[i] = i; + } + ArrayUtil.reverseArray(testArray); + for (int i = 0; i < 4; i++) { + Assert.assertEquals(4 - 1 - i, testArray[i]); + } + } + } + + @Test + public void removeZero() throws Exception { + // 测试非空数组 + { + int[] testArray = new int[20]; + for (int i = 0; i < 20; i++) { + if (i % 5 == 0) { + testArray[i] = 0; + } else { + testArray[i] = i; + } + } + + int[] newArray = ArrayUtil.removeZero(testArray); + Assert.assertNotNull(newArray); + for (int i = 0; i < 20; i++) { + if (i % 5 == 0) { + continue; + } + + Assert.assertEquals(testArray[i], i); + } + } + + // 测试空数组 + { + int[] testArray = new int[5]; + for (int i = 0; i < 5; i++) { + testArray[i] = 0; + } + + int[] newArray = ArrayUtil.removeZero(testArray); + Assert.assertNotNull(newArray); + Assert.assertEquals(newArray.length, 0); + } + } + + @Test + public void merge() throws Exception { + // 构建数组 + int[] array1 = new int[10]; + int[] array2 = new int[11]; + array2[10] = 100; + for (int i = 0; i < 10; i++) { + if (i % 2 == 0) { + array1[i / 2] = i; // 0, 2, 4, 6, 8 + } else { + array2[i / 2] = i; // 1, 3, 5, 7, 9 + } + } + + for (int i = 10; i < 15; i++) { + array1[i - 5] = i; // 10, 11, 12, 13, 14, 15 + array2[i - 5] = i; // 10, 11, 12, 13, 14, 15 + } + + // 测试merge + { + int[] merge = ArrayUtil.merge(array1, array2); + Assert.assertNotNull(merge); + Assert.assertEquals(merge.length, 16); + for (int i = 0; i < 15; i++) { + Assert.assertEquals(merge[i], i); + } + Assert.assertEquals(merge[15], 100); + } + // 调换数组顺序 + { + int[] merge = ArrayUtil.merge(array2, array1); + Assert.assertNotNull(merge); + Assert.assertEquals(merge.length, 16); + for (int i = 0; i < 15; i++) { + Assert.assertEquals(merge[i], i); + } + Assert.assertEquals(merge[15], 100); + } + // 测试空数组 + { + int[] array3 = new int[0]; + int[] merge1 = ArrayUtil.merge(array1, array3); + Assert.assertArrayEquals(merge1, array1); + + int[] merge2 = ArrayUtil.merge(array3, array1); + Assert.assertArrayEquals(merge2, array1); + } + // 测试相同数组 + { + int[] merge = ArrayUtil.merge(array1, array1); + Assert.assertArrayEquals(merge, array1); + } + } + + @Test + public void grow() throws Exception { + int[] oldArray = new int[5]; + for (int i = 0; i < 5; i++) { + oldArray[i] = i; + } + + int[] newArray = ArrayUtil.grow(oldArray, 5); + for (int i = 0; i < 10; i++) { + if (i < 5) { + Assert.assertEquals(newArray[i], i); + } else { + Assert.assertEquals(newArray[i], 0); + } + } + } + + @Test + public void fibonacci() throws Exception { + int[] fibonacciArray = {1, 1, 2, 3, 5, 8, 13, 21}; + + int[] calculatedFibonacci = ArrayUtil.fibonacci(22); + Assert.assertArrayEquals(fibonacciArray, calculatedFibonacci); + } + + @Test + public void getPrimes() throws Exception { + int[] expected = {2, 3, 5, 7, 11, 13, 17, 19}; + int[] primes = ArrayUtil.getPrimes(23); + Assert.assertArrayEquals(primes, expected); + } + + @Test + public void getPerfectNumbers() throws Exception { + int[] perfectNumbers = {6, 28, 496, 8128}; + int[] calculatedPerfectNumbers = ArrayUtil.getPerfectNumbers(8220); + Assert.assertArrayEquals(perfectNumbers, calculatedPerfectNumbers); + } + + @Test + public void join() throws Exception { + { + int[] array = {1}; + String joinStr = ArrayUtil.join(array, "-"); + Assert.assertEquals("1", joinStr); + } + + { + int[] array = {1, 2, 3}; + String joinStr = ArrayUtil.join(array, "-"); + Assert.assertEquals("1-2-3", joinStr); + } + } +} \ No newline at end of file diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/LoginAction.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..20fa9e766b --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +package org.korben.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author liuxin + */ +public class LoginAction { + private String name; + private String password; + private String message; + + 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 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 getMessage() { + return this.message; + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/Struts.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..7ac88522bf --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/Struts.java @@ -0,0 +1,111 @@ +package org.korben.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import org.korben.coderising.litestruts.dom.StrutsAction; +import org.korben.coderising.litestruts.util.StrutsParser; + +public class Struts { + + /** + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + * ("name"="test" , "password"="1234") , + * 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的execute 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + * 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + * 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + public static View runAction(String actionName, Map parameters) { + Map actionMap = StrutsParser.doParse(); + StrutsAction action = actionMap.get(actionName); + + if (action == null) { + System.out.println("couldn't get action: " + actionName + ", return"); + return null; + } + + try { + // 通过反射, 创建实例对象 + Class actionClass = Class.forName(action.getActionClassName()); + Object actionObj = actionClass.newInstance(); + + // 调用 parameters 中的 set 方法 + for (Map.Entry parameterEntry : parameters.entrySet()) { + Method[] methods = actionClass.getMethods(); + for (Method method : methods) { + if (method.getName().equalsIgnoreCase("set" + parameterEntry.getKey())) { + method.invoke(actionObj, parameterEntry.getValue()); + } + } + } + //for (Map.Entry parameterEntry : parameters.entrySet()) { + // for (PropertyDescriptor propertyDescriptor : + // Introspector.getBeanInfo(actionClass).getPropertyDescriptors()) { + // if (propertyDescriptor.getDisplayName().equals(parameterEntry.getKey())) { + // Method writeMethod = propertyDescriptor.getWriteMethod(); + // writeMethod.invoke(actionObj, parameterEntry.getValue()); + // } + // } + //} + + // 调用 execute 方法 + Method executeMethod = actionClass.getMethod("execute"); + Object executeResult = executeMethod.invoke(actionObj); + + // 根据 execute 方法的结果, 获取 xml 配置的 jsp 页面 + String jsp = action.getAttributes().get(Objects.toString(executeResult)); + + // 调用 get 方法 + Map actionFieldMap = new HashMap<>(); + Field[] actionFields = actionClass.getDeclaredFields(); + for (Field actionFiled : actionFields) { + Method[] methods = actionClass.getMethods(); + for (Method method : methods) { + if (method.getName().equalsIgnoreCase("get" + actionFiled.getName())) { + method.invoke(actionObj); + actionFieldMap.put(actionFiled.getName(), Objects.toString(method.invoke(actionObj))); + } + } + } + //for (PropertyDescriptor propertyDescriptor : + // Introspector.getBeanInfo(actionClass).getPropertyDescriptors()) { + // Method readMethod = propertyDescriptor.getReadMethod(); + // Object readMethodResult = readMethod.invoke(actionObj); + // actionFieldMap.put(propertyDescriptor.getDisplayName(), Objects.toString(readMethodResult)); + //} + + // 返回 View 对象 + View view = new View(); + view.setParameters(actionFieldMap); + view.setJsp(jsp); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + }/* catch (IntrospectionException e) { + e.printStackTrace(); + }*/ + + return null; + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/StrutsTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..024f678100 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/StrutsTest.java @@ -0,0 +1,37 @@ +package org.korben.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")); + } +} \ No newline at end of file diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/View.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/View.java new file mode 100644 index 0000000000..f4af03febc --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +package org.korben.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; + } +} \ No newline at end of file diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/dom/StrutsAction.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/dom/StrutsAction.java new file mode 100644 index 0000000000..c16de22c44 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/dom/StrutsAction.java @@ -0,0 +1,27 @@ +package org.korben.coderising.litestruts.dom; + +import java.util.Map; + +/** + * Created by Korben on 03/03/2017. + */ +public class StrutsAction { + private String actionClassName; + private Map attributes; + + public String getActionClassName() { + return actionClassName; + } + + public void setActionClassName(String actionClassName) { + this.actionClassName = actionClassName; + } + + public Map getAttributes() { + return attributes; + } + + public void setAttributes(Map attributes) { + this.attributes = attributes; + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/StrutsParser.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/StrutsParser.java new file mode 100644 index 0000000000..239ac2e4cd --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/StrutsParser.java @@ -0,0 +1,82 @@ +package org.korben.coderising.litestruts.util; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.korben.coderising.litestruts.dom.StrutsAction; + +/** + * 解析struts.xml + * + * Created by Korben on 03/03/2017. + */ +public class StrutsParser { + + private static final String STRUTS_XML = "struts.xml"; + + public static void main(String[] args) { + Map strutsActions = doParse(); + System.out.println(strutsActions.size()); + } + + public static Map doParse() { + Map resultMap = new HashMap<>(); + + SAXReader reader = new SAXReader(); + InputStream in = getStrutsInputStream(); + try { + Document document = reader.read(in); + Element rootElement = document.getRootElement(); + + // parse action element + List elementActions = rootElement.elements(); + for (Element elementAction : elementActions) { + StrutsAction action = new StrutsAction(); + + // parse "name" attribute from action element + resultMap.put(elementAction.attribute("name").getValue(), action); + + // parse "class" attribute from action element + action.setActionClassName(elementAction.attribute("class").getValue()); + + // parse sub elements in action element + List elements = elementAction.elements(); + Map map = new HashMap<>(); + for (Element element : elements) { + map.put(element.attribute("name").getValue(), element.getStringValue()); + } + action.setAttributes(map); + } + + return resultMap; + } catch (DocumentException e) { + throw new IllegalStateException("failed to parse " + STRUTS_XML, e); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private static InputStream getStrutsInputStream() { + StrutsParser.class.getPackage().getName(); + InputStream in = StrutsParser.class.getClassLoader() + .getResourceAsStream("org/korben/coderising/litestruts/util/" + STRUTS_XML); + if (in == null) { + throw new IllegalStateException(STRUTS_XML + " doesn't exist"); + } + + return in; + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/struts.xml b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/struts.xml new file mode 100644 index 0000000000..1cb3f4a5df --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/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/group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KArrayList.java similarity index 99% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KArrayList.java index 4d6236b537..0f443462ed 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KArrayList.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; import java.util.Objects; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KIterator.java similarity index 75% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KIterator.java index c29e566178..b5245ecca0 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KIterator.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; /** * Created by Korben on 24/02/2017. diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KLinkedList.java similarity index 98% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KLinkedList.java index e14efad19c..2ca4452981 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KLinkedList.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; import java.util.Objects; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KList.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KList.java similarity index 91% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KList.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KList.java index e5d33b984b..e5b166094e 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KList.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KList.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; /** * Korben's List diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListIteratorTest.java similarity index 94% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListIteratorTest.java index 994538732f..7017e0ed2c 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListIteratorTest.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; import org.junit.Assert; import org.junit.Before; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListTest.java similarity index 99% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListTest.java index ed3055b74e..2c6febecc3 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListTest.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; import java.util.Objects; import org.junit.Assert; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KArrayQueue.java similarity index 98% rename from group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KArrayQueue.java index 3e975058f4..eea57cf035 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KArrayQueue.java @@ -1,4 +1,4 @@ -package org.korben.queue; +package org.korben.coding.basic.queue; import java.util.NoSuchElementException; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueue.java similarity index 84% rename from group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueue.java index 14763efd99..9d8146d50d 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueue.java @@ -1,4 +1,4 @@ -package org.korben.queue; +package org.korben.coding.basic.queue; /** * Korben's Queue Interface diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueueTest.java similarity index 97% rename from group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueueTest.java index 3d9557748f..17c3703a64 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueueTest.java @@ -1,4 +1,4 @@ -package org.korben.queue; +package org.korben.coding.basic.queue; import java.util.NoSuchElementException; import org.junit.Assert; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStack.java similarity index 97% rename from group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStack.java index 8dc39e4efd..eb83eb47cd 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStack.java @@ -1,4 +1,4 @@ -package org.korben.stack; +package org.korben.coding.basic.stack; import java.util.EmptyStackException; import java.util.Objects; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStackTest.java similarity index 97% rename from group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStackTest.java index e3f173eb3f..20b451a7ef 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStackTest.java @@ -1,4 +1,4 @@ -package org.korben.stack; +package org.korben.coding.basic.stack; import java.util.EmptyStackException; import org.junit.Assert; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNode.java similarity index 98% rename from group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNode.java index 30c613edd0..3cfcacc37c 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package org.korben.tree; +package org.korben.coding.basic.tree; /** * Korben's BinaryTreeNode diff --git a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNodeTest.java similarity index 97% rename from group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNodeTest.java index a6fb4ed4e9..05873872a7 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNodeTest.java @@ -1,4 +1,4 @@ -package org.korben.tree; +package org.korben.coding.basic.tree; import org.junit.Assert; diff --git a/group20/1107837739/korben.md b/group20/1107837739/korben.md index 983f6f47b6..91ee50808b 100644 --- a/group20/1107837739/korben.md +++ b/group20/1107837739/korben.md @@ -5,3 +5,5 @@ | Blog Title | Date| | ---------- | -----------| | [初窥计算机程序的运行](http://korben-chy.github.io/2017/02/26/%E5%88%9D%E7%AA%A5%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%90%E8%A1%8C/) | 2017/02/26 | +| +[程序在计算机中的运行过程简析](http://korben-chy.github.io/2017/03/06/%E7%A8%8B%E5%BA%8F%E5%9C%A8%E8%AE%A1%E7%AE%97%E6%9C%BA%E4%B8%AD%E7%9A%84%E8%BF%90%E8%A1%8C%E8%BF%87%E7%A8%8B%E7%AE%80%E6%9E%90) | 2017/03/05 | diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/ArrayList.java b/group20/1430208241/1430208241leaning/src/GithubWork/ArrayList.java new file mode 100644 index 0000000000..f74501b5ba --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/ArrayList.java @@ -0,0 +1,73 @@ +package GithubWork; + +import java.util.Arrays; + +public class ArrayList implements List { + private int size = 0; + private Object[] elementdata = new Object[100]; + + public void add(Object o) { + if (elementdata.length <= size) { + ensureCapacity(size + 1); + } + elementdata[size++] = o; + } + + private void ensureCapacity(int minCapacity) { + int oldCapacity = elementdata.length; + if (oldCapacity < minCapacity) { + + int newCapacity = (int) (oldCapacity * 1.5); + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elementdata = Arrays.copyOf(elementdata, newCapacity); + } + } + + public void add(int index, Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + + } + + ensureCapacity(size + 1); + System.arraycopy(elementdata, index, elementdata, index, size - index); + elementdata[index] = o; + size++; + } + + public Object get(int index) { + RangeCheck(index); + + return elementdata[index]; + } + + public Object remove(int index) { + RangeCheck(index); + Object oldvalue = elementdata[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementdata, index + 1, elementdata, index, numMoved); + elementdata[--size] = null; + return oldvalue; + } + + private void RangeCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(); + } + + public int size() { + int i; + for (i = 0; i < elementdata.length; i++) { + size++; + if (null == elementdata[i]) { + break; + } + + } + return size; + + } + +} diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/JunitTest.java b/group20/1430208241/1430208241leaning/src/GithubWork/JunitTest.java new file mode 100644 index 0000000000..b011de10f0 --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/JunitTest.java @@ -0,0 +1,47 @@ +package GithubWork; + +import org.junit.Test; + +public class JunitTest { + @Test + public void ArrayList(){ + ArrayList a=new ArrayList(); + a.add(1); + a.add(2); + a.add(3); + a.add(4); + a.get(2); + + System.out.println(a); + + + } + @Test + public void Queue(){ + Queue q=new Queue(4); + q.enQueue(1); + q.enQueue(2); + q.enQueue(3); + q.enQueue(4); + + while(!q.isEmpty()){ + int i=(int) q.deQueue(); + System.out.println(i); + } + System.out.println(q.size()); + } + @Test + public void LinkedList(){ + LinkedList ls=new LinkedList(); + ls.add(1); + ls.add(7); + ls.add(3); + ls.add(4, 5); + ls.get(2); + ls.addFirst(0); + + ls.remove(3); + System.out.println(ls); + + } +} diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/LinkedList.java b/group20/1430208241/1430208241leaning/src/GithubWork/LinkedList.java new file mode 100644 index 0000000000..657ca117dc --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/LinkedList.java @@ -0,0 +1,136 @@ +package GithubWork; + +import java.util.Iterator; + +public class LinkedList implements List { + + private Node head = null;// ͷڵ + private int size = 0; + private Node last = null; + + /* + * в (non-Javadoc) + * + * @see GithubWork.List#add(java.lang.Object) + */ + public void add(Object o) { + Node newNode = new Node(0);// ʵһڵ + if (head == null) { + head = newNode; + return; + } + Node tmp = head; + while (tmp.next != null) { + tmp = tmp.next; + } + tmp.next = newNode; + size++; + } + + public void add(int index, Object o) { + Node newNode = new Node(0); + Node indexNode = head; + int i = 0; + while (i == index) { + indexNode = indexNode.next; + i++; + } + Node indexNextNode = indexNode.next; + indexNode.next = newNode; + newNode.pre = indexNode; + newNode.next = indexNextNode; + indexNextNode.pre = newNode; + size++; + } + + public Object get(int index) { + Node indexNode = head; + int i = 0; + while (i == index) { + indexNode = indexNode.next; + i++; + } + return indexNode; + } + + public Object remove(int index) { + if (index < 1 || index > size()) { + throw new IndexOutOfBoundsException(); + } + if (index == 1) { + head = head.next; + return head; + } + int i = 1; + Node preNode = head; + Node curNode = preNode.next; + while (curNode != null) { + if (i == index) { + preNode.next = curNode.next; + + } + preNode = curNode; + curNode = curNode.next; + i++; + } + return curNode; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.data = o; + newNode.next = head; + head.pre = newNode; + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o); + newNode.data = o; + + newNode.pre = last; + last.next = newNode; + last = newNode; + size++; + + } + + public Object removeFirst() { + Node ref = head; + head = head.next; + head.pre = null; + size--; + return ref; + } + + public Object removeLast() { + Node rel = last; + last = last.pre; + last.next = null; + size--; + return rel; + + } + + public Iterator iterator() { + + return null; + } + + private static class Node { + Object data;// ڵ + Node next = null;// ͷڵ + Node pre = null; + + public Node(Object data) { + this.data = data; + } + + } + +} diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/List.java b/group20/1430208241/1430208241leaning/src/GithubWork/List.java new file mode 100644 index 0000000000..169fc14fd1 --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/List.java @@ -0,0 +1,10 @@ +package GithubWork; + +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/group20/1430208241/1430208241leaning/src/GithubWork/Queue.java b/group20/1430208241/1430208241leaning/src/GithubWork/Queue.java new file mode 100644 index 0000000000..792ca233f5 --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/Queue.java @@ -0,0 +1,43 @@ +package GithubWork; + +public class Queue { + private int maxSize; + private Object[] array;//Ԫ + private int front;//ǰһԪ + private int rear;//һԪ + private int items=0;//Ԫظ + //󲢳ʼ + public Queue(int s){ + maxSize=s; + array=new Object[maxSize]; + front=0; + rear=-1; + + } + public void enQueue(Object o){ + if(rear==maxSize-1){ + rear=-1; + } + array[++rear]=o; + items++; + + } + + public Object deQueue(){ + Object temp =array[front++]; + if(front==maxSize){ + front=0; + } + items--; + return temp; + } + + public boolean isEmpty(){ + + return items==0; + } + + public int size(){ + return array.length; + } +} diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/Stack.java b/group20/1430208241/1430208241leaning/src/GithubWork/Stack.java new file mode 100644 index 0000000000..43f138021c --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/Stack.java @@ -0,0 +1,22 @@ +package GithubWork; + +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/group20/1430208241/1430208241leaning/src/\346\226\207\347\253\240\345\234\260\345\235\200" "b/group20/1430208241/1430208241leaning/src/\346\226\207\347\253\240\345\234\260\345\235\200" new file mode 100644 index 0000000000..472bb524e9 --- /dev/null +++ "b/group20/1430208241/1430208241leaning/src/\346\226\207\347\253\240\345\234\260\345\235\200" @@ -0,0 +1 @@ +http://blog.csdn.net/wifi619/article/details/57510982 \ No newline at end of file diff --git a/group20/2421586846/.project b/group20/2421586846/.project new file mode 100644 index 0000000000..b0d9d8a3f0 --- /dev/null +++ b/group20/2421586846/.project @@ -0,0 +1,11 @@ + + + 2421586846 + + + + + + + + diff --git a/group20/2421586846/DS/.project b/group20/2421586846/DS/.project new file mode 100644 index 0000000000..ca3bc54b4a --- /dev/null +++ b/group20/2421586846/DS/.project @@ -0,0 +1,17 @@ + + + DS + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group20/2421586846/DS/src/basic/ArrayList.java b/group20/2421586846/DS/src/basic/ArrayList.java new file mode 100644 index 0000000000..7e9f70169d --- /dev/null +++ b/group20/2421586846/DS/src/basic/ArrayList.java @@ -0,0 +1,77 @@ +package basic; + + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[2]; + + public void EnsureEnoughSize(){ + if (size == elementData.length ) + { + elementData = Arrays.copyOf(elementData, elementData.length +10); + + } + } + + public void add(Object o){ + EnsureEnoughSize(); + + // elementData = Arrays.copyOf(elementData, elementData.length +1); + //Object[] NewelementData = new Object[size+1]; + //System.arraycopy( elementData,0, NewelementData, 0, elementData.length ); + + + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + if (index >= size || index < 0){ + return; + } + + EnsureEnoughSize(); + + for (int i = elementData.length-1; i>index;i --){ + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + size++; + } + + public Object get(int index){ + if (index >= size || index < 0){ + return null; + } + else { + return elementData[index]; + } + + } + + public Object remove(int index){ + if (index >= size || index < 0){ + return null; + } + else { + Object o = elementData[index]; + for (int i =index; i< size-1; i++){ + elementData[i]= elementData[i+1]; + } + size--; + return o; + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group20/2421586846/DS/src/basic/BinaryTreeNode.java b/group20/2421586846/DS/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e8e37a9a41 --- /dev/null +++ b/group20/2421586846/DS/src/basic/BinaryTreeNode.java @@ -0,0 +1,33 @@ +package 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/group20/2421586846/DS/src/basic/Iterator.java b/group20/2421586846/DS/src/basic/Iterator.java new file mode 100644 index 0000000000..9154df57bf --- /dev/null +++ b/group20/2421586846/DS/src/basic/Iterator.java @@ -0,0 +1,7 @@ +package basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group20/2421586846/DS/src/basic/LinkedList.java b/group20/2421586846/DS/src/basic/LinkedList.java new file mode 100644 index 0000000000..8bdc7ec661 --- /dev/null +++ b/group20/2421586846/DS/src/basic/LinkedList.java @@ -0,0 +1,177 @@ +package basic; + + +public class LinkedList implements List { + + private Node head; + + private int size; + //加到最后 + public void add(Object o){ + Node newNode= new Node(); + newNode.data = o; + newNode.next = null; + + if (head == null ){ + head= newNode; + size++; + return; + } + + Node currentNode = head; + + while (currentNode.next != null ){ + currentNode = currentNode.next ; + } + /* + for (int i=0;i< size;i++){ + currentNode = currentNode.next ; + } + */ + currentNode.next =newNode; + size++; + } + + public void add(int index , Object o){ + if (index >= size ||index < 0) { + return; + } + + Node newNode= new Node(); + newNode.data = o; + Node PrevNode =null; + + if (index ==0 ){ + + Node tempNode = head ; + head = newNode; + newNode.next = tempNode; + + } + else { + Node currentNode = head; + for (int i=0; i = size ||index < 0) { + return null; + } + Node currentNode = head; + for (int i=0; i = size ||index < 0) { + return null; + } + + Node currentNode = head; + if (index ==0 ){ + head =head.next; + size--; + return currentNode.data; + } + Node PrevNode = null; + for (int i=0; i =maxSize){ + if(index<0||index>size-1) + throw new IndexOutOfBoundsException("数组下标越界异常。"); + if (size==maxSize){ Object[] targt = new Object[++maxSize]; - System.arraycopy(elementData,0,targt,0,maxSize); - for (int j = targt.length;j>=index;j--){ - targt[j-1] = targt[j-2]; + System.arraycopy(elementData,0,targt,0,elementData.length); + for (int j = targt.length-2;j>=index;j--){ + targt[j+1] = targt[j]; } targt[index] = o; size++; + elementData = targt; }else if(size=index;j--){ - elementData[j-1] = elementData[j-2]; + for (int j = size-1;j>=index;j--){ + elementData[j+1] = elementData[j]; } elementData[index] = o; size++; @@ -50,28 +55,35 @@ public void add(int index, Object o) { */ @Override public void add(Object o) { - if (size>=maxSize){ + if (size==maxSize){ Object[] targt = new Object[++maxSize]; - System.arraycopy(elementData,0,targt,0,maxSize); + System.arraycopy(elementData,0,targt,0,elementData.length); targt[maxSize-1] = o; size++; + elementData = targt; }else if(sizesize-1) + throw new IndexOutOfBoundsException("数组下标越界异常"); + Object o= elementData[index]; + return o; } @Override public Object remove(int index) { + if (index<0||index>size-1) + throw new IndexOutOfBoundsException("数组下表越界异常"); Object temp = elementData[index]; - for (int i = index;i>size-1;i++){ + for (int i = index;i<=size-1;i++){ elementData[i] = elementData[i+1]; } + size--; return temp; } @@ -81,6 +93,10 @@ public int size() { } - - + @Override + public String toString() { + return "ArrayList{" + + "elementData=" + Arrays.toString(elementData) + + '}'; + } } diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java b/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java index b2cc5f8668..52695305fb 100644 --- a/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java +++ b/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java @@ -5,36 +5,119 @@ */ public class LinkedList implements List{ - private Node head; + private Node head;//链表的头节点 + private Node tail;//链接的尾节点 + private int size;//定义了链表中的元素的个数 + /** + * 定义了NODE节点的数据结构 + */ private static class Node{ Object data; Node next; - } + private Node(){ + } + private Node(Object o,Node node){ + this.data = o; + this.next = node; + } + } + /** + * 在指定的索引写入新的节点 + * 1.找到给位置上的节点node0 + * 2.修改node0的next指向新的节点node + * 3.新节点node指向原先node0的后继节点node1 + * 4.长度++ + * 5.完结 + * @param index 索引 从0开始 + * @param o + */ @Override public void add(int index, Object o) { - + if (index<0||index>size-1) + throw new IndexOutOfBoundsException("单链表越界异常。"); + Node node = new Node(); + node.data = o; + Node nodeIndex = findNodeByIndex(index); + if(node==null) + throw new NullPointerException("空指针异常。"); + Node temp = nodeIndex.next; + nodeIndex.next = node; + node.next = temp; + size++; + } + + /** + * 根据索引号查询节点 + * @param index 索引 + * @return + */ + private Node findNodeByIndex(int index) { + if (index<0||index>size-1) + throw new IndexOutOfBoundsException("单链表越界异常。"); + Node current = head; + if (1<=index&&index<=size-1){//索引检查 + for (int i = 0;i>=size-1&¤t!=null;i++,current = current.next) + if (i==index){ + return current; + } + } + return null; } @Override public void add(Object o) { - + Node node = new Node(); + node.data = o; + if(head==null){ + head = node; + node.next = tail; + size++; + }else{ + Node temp = tail; + node.next = temp; + temp.next = node; + size++; + } } @Override public Object get(int index) { - return null; + if(index<0||index>size-1) + throw new IndexOutOfBoundsException("单链表越界。"); + Node node = findNodeByIndex(index); + if(node==null) + throw new NullPointerException("空指针异常。"); + return node.data; } + /** + * 删除指定索引的元素 + * @param index + * @return + */ @Override public Object remove(int index) { - return null; + if(index<0||index>size-1) + throw new IndexOutOfBoundsException("单链表越界。"); + Node node = findNodeByIndex(index); + if(node==null) + throw new NullPointerException("空指针异常。"); + Node prvNode = findNodeByIndex(index-1); + if(prvNode==null) + throw new NullPointerException("空指针异常。"); + Node nextNode = node.next; + node.next = null; + prvNode.next = nextNode; + size--; + return node.data; + } @Override public int size() { - return 0; + return size; } } diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/List.java b/group20/286166752/src/wiki/liven/code/dataStructures/List.java index 2d1840ef0f..57d5c217ce 100644 --- a/group20/286166752/src/wiki/liven/code/dataStructures/List.java +++ b/group20/286166752/src/wiki/liven/code/dataStructures/List.java @@ -1,7 +1,7 @@ package wiki.liven.code.dataStructures; /** - * Created by leven on 2017/2/21. + * Created by leven on 2017/2/26. */ public interface List { @@ -11,4 +11,5 @@ public interface List { public Object remove(int index); public int size(); + } diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java b/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java index b8c8430daa..d52c0de6f5 100644 --- a/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java +++ b/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java @@ -2,6 +2,63 @@ /** * Created by leven on 2017/2/21. + * 队列 + * 只允许一端进行出队操作,另一端进行入队操作。 + * 特性是:先进先出 + * 本质上是一种操作受限制的线性表 + * 本实现线性表采用自己实现的ArrayList, + * ArrayList理论上会受到JVM分配的内存大小,从而其空间会有上限。 + * 但是,该错误,将有JVM抛出。 + * 所以,本实现,队列忽略执行判断满队操作。 */ public class Queue { + + private ArrayList list; + private int head;//队头指针 + private int foot;//队尾指针 + + + public Queue(){ + head = foot = 0; + } + + /** + * 判空 + * @return + */ + public boolean queueEmpty(){ + if (head==0&&foot==0){ + return true; + }else { + return false; + } + } + + /** + * 入队列 + * 1.先把元素放置到队列中 + * 2.将队尾指针加1 + * @param o + */ + public void enQueue(Object o){ + list.add(foot,o); + foot++; + } + + /** + * 删除队头元素 + * 0.先判断队列是否为空 + * 1.先取出队头的元素 + * 2.然后将队头的指针加1 + * @return + */ + public int deQueue(){ + if (queueEmpty()==true) + throw new IndexOutOfBoundsException("队列为空,无法执行该操作。"); + list.remove(head); + return head++; + } + + + } diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java b/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java index 59cb18c416..cc33ed2985 100644 --- a/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java +++ b/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java @@ -2,6 +2,61 @@ /** * Created by leven on 2017/2/21. + * 栈:只允许在一端进行删除或者增加操作的线性表。 + * 本实现,采用ArrayList。 */ public class Stack { + + + private ArrayList list;//线性表 + private int top = -1;//栈顶指针,默认指向栈低 + + + /** + * 元素进栈 + * @param o + * 1.指针先加1 + * 2.将元素放入栈中 + * + */ + public void push(Object o){ + top++; + list.add(o); + } + + /** + * 栈顶元素出栈,返回栈顶指针的值 + * @return + */ + public int pop(){ + if (top==-1) + throw new IndexOutOfBoundsException("栈为空,无法执行出栈操作。"); + list.remove(top); + top--; + return top; + } + + /** + * 获取栈顶元素的值 + * @return + */ + public Object getTop() { + if (top==-1) + throw new IndexOutOfBoundsException("栈为空,无法执行出栈操作。"); + Object o = list.get(top); + return o; + } + + /** + * 判空 + * @return + */ + public boolean stackEmpty(){ + if (top==-1){ + return true; + }else { + return false; + } + } + } diff --git a/group20/286166752/src/wiki/liven/code/test/SomeDemos.java b/group20/286166752/src/wiki/liven/code/test/SomeDemos.java deleted file mode 100644 index 90a74edc35..0000000000 --- a/group20/286166752/src/wiki/liven/code/test/SomeDemos.java +++ /dev/null @@ -1,15 +0,0 @@ -package wiki.liven.code.test; - -/** - * Created by leven on 2017/2/21. - */ -public class SomeDemos { - - public static void main(String[] args){ - int[] a = new int[10]; - a[0] = 4; - System.out.println(a.length); - - } - -} diff --git a/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java b/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java index 0d960ee3d8..590fd3209e 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java +++ b/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java @@ -1,122 +1,122 @@ -package com.coding.basic; - -import java.util.Arrays; - -public class ArrayList implements List { - - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - int n = elementData.length; - int i = 0; - while (elementData[i] != null) { - i++; - } - if (i < n) { - elementData[i] = o; - } else { - Object[] temp = Arrays.copyOf(elementData, n + 1); - temp[n] = o; - elementData = temp; - } - size++; - } - public void add(int index, Object o){ - int n = elementData.length; - int i = 0; - while (elementData[i] != null) { - i++; - } - if (index < 0 || index > i) { - System.out.println(index + " is invalid index!"); - return; - } - if (i < n) { - for (int j = i; j > index; j--) { - elementData[j] = elementData[j - 1]; - } - elementData[index] = o; - } else { - Object[] temp = Arrays.copyOf(elementData, n + 1); - for (int j = i; j > index; j--) { - temp[j] = temp[j - 1]; - } - temp[index] = o; - elementData = temp; - } - size++; - } - - public Object get(int index){ - int i = 0; - while (elementData[i] != null) { - i++; - } - if (index < 0 || index >= i) { - System.out.println(index + " is invalid index!"); - return null; - } else { - return elementData[index]; - } - } - - public Object remove(int index){ - int i = 0; - while (elementData[i] != null) { - i++; - } - if (index < 0 || index >= i) { - System.out.println(index + " is invalid index!"); - return null; - } - Object result = elementData[index]; - for (int j = index; j < i; j++) { - elementData[j] = elementData[j + 1]; - } - size--; - return result; - } - - public int size(){ - return size; - } - - public String toString() { - int i = 0; - while (elementData[i] != null) { - i++; - } - String result = ""; - for (int j = 0; j < i - 1; j++) { - result += elementData[j].toString() + ", "; - } - result += elementData[size - 1].toString(); - return result; - } - - public Iterator iterator(){ - return null; - } - - public static void main(String args[]){ - ArrayList list1 = new ArrayList(); - list1.add("a"); - list1.add("b"); - list1.add("c"); - System.out.println(list1.toString()); - list1.add(5, "d"); - list1.add(1, "d"); - System.out.println(list1.toString()); - list1.add(4, "e"); - System.out.println(list1.toString()); - list1.get(5); - System.out.println(list1.get(0)); - list1.remove(2); - System.out.println(list1.toString()); - System.out.println(list1.size()); - } - -} +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + int n = elementData.length; + int i = 0; + while (elementData[i] != null) { + i++; + } + if (i < n) { + elementData[i] = o; + } else { + Object[] temp = Arrays.copyOf(elementData, n + 1); + temp[n] = o; + elementData = temp; + } + size++; + } + public void add(int index, Object o){ + int n = elementData.length; + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index > i) { + System.out.println(index + " is invalid index!"); + return; + } + if (i < n) { + for (int j = i; j > index; j--) { + elementData[j] = elementData[j - 1]; + } + elementData[index] = o; + } else { + Object[] temp = Arrays.copyOf(elementData, n + 1); + for (int j = i; j > index; j--) { + temp[j] = temp[j - 1]; + } + temp[index] = o; + elementData = temp; + } + size++; + } + + public Object get(int index){ + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index >= i) { + System.out.println(index + " is invalid index!"); + return null; + } else { + return elementData[index]; + } + } + + public Object remove(int index){ + int i = 0; + while (elementData[i] != null) { + i++; + } + if (index < 0 || index >= i) { + System.out.println(index + " is invalid index!"); + return null; + } + Object result = elementData[index]; + for (int j = index; j < i; j++) { + elementData[j] = elementData[j + 1]; + } + size--; + return result; + } + + public int size(){ + return size; + } + + public String toString() { + int i = 0; + while (elementData[i] != null) { + i++; + } + String result = ""; + for (int j = 0; j < i - 1; j++) { + result += elementData[j].toString() + ", "; + } + result += elementData[size - 1].toString(); + return result; + } + + public Iterator iterator(){ + return null; + } + + public static void main(String args[]){ + ArrayList list1 = new ArrayList(); + list1.add("a"); + list1.add("b"); + list1.add("c"); + System.out.println(list1.toString()); + list1.add(5, "d"); + list1.add(1, "d"); + System.out.println(list1.toString()); + list1.add(4, "e"); + System.out.println(list1.toString()); + list1.get(5); + System.out.println(list1.get(0)); + list1.remove(2); + System.out.println(list1.toString()); + System.out.println(list1.size()); + } + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java b/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java index 266eff3d56..d7ac820192 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java +++ b/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java @@ -1,32 +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; - } - -} +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/group20/331798361/assignment1/src/com/coding/basic/Iterator.java b/group20/331798361/assignment1/src/com/coding/basic/Iterator.java index dbe8b9afb2..06ef6311b2 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/Iterator.java +++ b/group20/331798361/assignment1/src/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java b/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java index e029beaeb4..13f423cc24 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java +++ b/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java @@ -1,148 +1,148 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private int size; - - public void add(Object o){ - if (head.data == null) { - head.data = o; - head.next = null; - size++; - return; - } - Node node = new Node(o); - Node curr = head; - while (curr.next != null) { - curr = curr.next; - } - curr.next = node; - size++; - } - public void add(int index , Object o){ - - if (index < 0 || index > size) { - System.out.println(index + " is invalid index!"); - return; - } - if (head.data == null) { - if (index == 0) { - head.data = o; - head.next = null; - size++; - return; - } else { - System.out.println("invalid index!"); - return; - } - } - Node node = new Node(o); - Node curr = head; - for (int i = 0; i < index - 1; i++) { - curr = curr.next; - } - Node temp = curr.next; - curr.next = node; - node.next = temp; - size++; - } - public Object get(int index){ - if (index < 0 || index > size) { - System.out.println(index + " is invalid index!"); - return null; - } - Node result = head; - for (int i = 0; i < index; i++) { - result = result.next; - } - return result; - } - public Object remove(int index){ - if (index < 0 || index > size) { - System.out.println(index + " is invalid index!"); - return null; - } - Node curr = head; - for (int i = 0; i < index - 1; i++) { - curr = curr.next; - } - Node result = curr.next; - curr.next = curr.next.next; - size--; - return result; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - if (head.data == null) { - head.data = o; - head.next = null; - size++; - return; - } - Node temp = head; - head = new Node(o); - head.next = temp; - size++; - } - - public void addLast(Object o){ - if (head.data == null) { - head.data = o; - head.next = null; - size++; - return; - } - Node node = new Node(o); - Node curr = head; - while (curr.next != null) { - curr = curr.next; - } - curr.next = node; - size++; - } - - public Object removeFirst(){ - if (head.data == null) { - return null; - } - Node result = head; - head = head.next; - size--; - return result; - } - - public Object removeLast(){ - if (head.data == null) { - return null; - } - Node curr = head; - for (int i = 0; i < size - 1; i++) { - curr = curr.next; - } - Node result = curr.next; - curr.next = null; - size--; - return result; - } - - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - Node(Object o) { - data = o; - next = null; - } - - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + public void add(int index , Object o){ + + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return; + } + if (head.data == null) { + if (index == 0) { + head.data = o; + head.next = null; + size++; + return; + } else { + System.out.println("invalid index!"); + return; + } + } + Node node = new Node(o); + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node temp = curr.next; + curr.next = node; + node.next = temp; + size++; + } + public Object get(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node result = head; + for (int i = 0; i < index; i++) { + result = result.next; + } + return result; + } + public Object remove(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = curr.next.next; + size--; + return result; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node temp = head; + head = new Node(o); + head.next = temp; + size++; + } + + public void addLast(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + + public Object removeFirst(){ + if (head.data == null) { + return null; + } + Node result = head; + head = head.next; + size--; + return result; + } + + public Object removeLast(){ + if (head.data == null) { + return null; + } + Node curr = head; + for (int i = 0; i < size - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = null; + size--; + return result; + } + + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + Node(Object o) { + data = o; + next = null; + } + + } +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/List.java b/group20/331798361/assignment1/src/com/coding/basic/List.java index 396b1f6416..10d13b5832 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/List.java +++ b/group20/331798361/assignment1/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Queue.java b/group20/331798361/assignment1/src/com/coding/basic/Queue.java index c4991a07f5..102fc025f5 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/Queue.java +++ b/group20/331798361/assignment1/src/com/coding/basic/Queue.java @@ -1,30 +1,30 @@ -package com.coding.basic; - - -public class Queue { - private LinkedList list = new LinkedList(); - - public void enQueue(Object o){ - list.add(o); - } - - public Object deQueue(){ - int length = list.size(); - if (length == 0) { - return null; - } - return list.removeFirst(); - } - - public boolean isEmpty(){ - if (list.size() == 0) { - return true; - } else { - return false; - } - } - - public int size(){ - return list.size(); - } -} +package com.coding.basic; + + +public class Queue { + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + int length = list.size(); + if (length == 0) { + return null; + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + if (list.size() == 0) { + return true; + } else { + return false; + } + } + + public int size(){ + return list.size(); + } +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Stack.java b/group20/331798361/assignment1/src/com/coding/basic/Stack.java index 0a48545cea..3a62e3817e 100644 --- a/group20/331798361/assignment1/src/com/coding/basic/Stack.java +++ b/group20/331798361/assignment1/src/com/coding/basic/Stack.java @@ -1,37 +1,37 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o) { - elementData.add(o); - } - - public Object pop(){ - int length = elementData.size(); - if (length == 0) { - return null; - } - return elementData.remove(length - 1); - } - - public Object peek(){ - int length = elementData.size(); - if (length == 0) { - return null; - } - return elementData.get(length - 1); - } - - public boolean isEmpty(){ - if (elementData.size() != 0) { - return false; - } else { - return true; - } - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(o); + } + + public Object pop(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.remove(length - 1); + } + + public Object peek(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.get(length - 1); + } + + public boolean isEmpty(){ + if (elementData.size() != 0) { + return false; + } else { + return true; + } + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group20/331798361/assignment2/src/array/ArrayUtil.java b/group20/331798361/assignment2/src/array/ArrayUtil.java new file mode 100644 index 0000000000..3fc1eabfa8 --- /dev/null +++ b/group20/331798361/assignment2/src/array/ArrayUtil.java @@ -0,0 +1,150 @@ +package array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if (origin.length == 0) { + return; + } + int n = origin.length - 1; + int temp; + for (int i = 0; i < n/2; i++) { + temp = origin[i]; + origin[i] = origin[n - i]; + origin[n - i] = 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 int[] removeZero(int[] oldArray){ + if (oldArray.length == 0) { + return null; + } + int n = oldArray.length; + int zeros = 0; + for (int i = 0; i < n - 1; i++) { + if (oldArray[i] == 0) { + zeros++; + } + } + int[] result = new int[n - zeros]; + int j = 0; + for (int i = 0; i < n - zeros - 1; i++) { + while (oldArray[j] == 0) { + j++; + } + result[i] = oldArray[j]; + j++; + } + return result; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max <= 1) { + return new int[0]; + } + int n = 1; + int init = helpFibonacci(n); + while (init < max) { + n++; + init = helpFibonacci(n); + } + int[] result = new int[n - 1]; + for (int i = 0; i < n - 1; i++) { + result[i] = helpFibonacci(i + 1); + } + return result; + } + + public static int helpFibonacci(int n) { + if(n == 0) + return 0; + else if(n == 1) + return 1; + else + return helpFibonacci(n -1 ) + helpFibonacci(n - 2); + } + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + public static void main(String[] args) { + int[] a = fibonacci(15); + for (int i = 0; i < a.length; i++) { + System.out.println(a[i]); + } + } +} diff --git a/group20/331798361/assignment2/src/litestruts/LoginAction.java b/group20/331798361/assignment2/src/litestruts/LoginAction.java new file mode 100644 index 0000000000..bba4c11c9f --- /dev/null +++ b/group20/331798361/assignment2/src/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group20/331798361/assignment2/src/litestruts/Struts.java b/group20/331798361/assignment2/src/litestruts/Struts.java new file mode 100644 index 0000000000..e6b77ef929 --- /dev/null +++ b/group20/331798361/assignment2/src/litestruts/Struts.java @@ -0,0 +1,76 @@ +package litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + try { + // parse xml file + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse("src/litestruts/struts.xml"); + doc.getDocumentElement().normalize(); + + // get action items + NodeList list = doc.getElementsByTagName("action"); + for (int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + Element element = (Element) node; + // look for action-related class + if (element.getAttribute("name").equals(actionName)){ + + Class c = Class.forName(element.getAttribute("class")); + Object o = c.newInstance(); + + //set name + Method setName = c.getDeclaredMethod("setName", String.class); + setName.invoke(o, parameters.get("name")); + + //set password + Method setPassword = c.getDeclaredMethod("setPassword", String.class); + setPassword.invoke(o, parameters.get("password")); + + //execute + Method execute = c.getDeclaredMethod("execute", null); + // login result + String result = execute.invoke(o).toString(); + + //get login messsage + Method getMessage = c.getDeclaredMethod("getMessage", null); + HashMap map = new HashMap<>(); + map.put("message", getMessage.invoke(o).toString()); + + // new view with parameter map + View view = new View(); + view.setParameters(map); + NodeList list1 = element.getElementsByTagName("result"); + for (int j = 0; j < list1.getLength(); j++) { + Node node1 = list1.item(j); + Element element1 = (Element) node1; + if (element1.getAttribute("name").equals(result)) { + view.setJsp(node1.getTextContent()); + } + } + return view; + } + } + } catch (Exception e) { + System.out.println("parse error"); + } + return null; + } +} diff --git a/group20/331798361/assignment2/src/litestruts/StrutsTest.java b/group20/331798361/assignment2/src/litestruts/StrutsTest.java new file mode 100644 index 0000000000..f227801865 --- /dev/null +++ b/group20/331798361/assignment2/src/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +package litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name", "test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + +} diff --git a/group20/331798361/assignment2/src/litestruts/View.java b/group20/331798361/assignment2/src/litestruts/View.java new file mode 100644 index 0000000000..1eed614744 --- /dev/null +++ b/group20/331798361/assignment2/src/litestruts/View.java @@ -0,0 +1,23 @@ +package litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group20/331798361/assignment2/src/litestruts/struts.xml b/group20/331798361/assignment2/src/litestruts/struts.xml new file mode 100644 index 0000000000..d6da22a638 --- /dev/null +++ b/group20/331798361/assignment2/src/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group20/404130810/src/com/basic/datastructure/ArrayList.java b/group20/404130810/src/com/basic/datastructure/ArrayList.java index 86c001b2c1..e91eb9ce5e 100644 --- a/group20/404130810/src/com/basic/datastructure/ArrayList.java +++ b/group20/404130810/src/com/basic/datastructure/ArrayList.java @@ -1,110 +1,110 @@ -package com.basic.datastructure; - -public class ArrayList implements List { - - private Object[] elementData; - private int size; - - private int enableCapacity; - - public ArrayList() { - this.enableCapacity = 10; - this.elementData = new Object[enableCapacity]; - } - - @Override - public void add(Object o) { - growIfNeeded(); - elementData[size] = o; - this.size++; - } - - @Override - public void add(int index, Object o) { - rangeCheckForAdd(index); - growIfNeeded(); - - Object[] tmpObjects = new Object[elementData.length]; - System.arraycopy(elementData, 0, tmpObjects, 0, index); - tmpObjects[index] = o; - System.arraycopy(elementData, index, tmpObjects, index + 1, elementData.length - index - 1); - - elementData = tmpObjects; - - this.size++; - } - - @Override - public Object get(int index) { - if (index > size) { - throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); - } - return elementData[index]; - } - - @Override - public Object remove(int index) { - Object removedObj = this.get(index); - rangeCheck(index); - Object[] tmpObjects = new Object[elementData.length]; - - System.arraycopy(elementData, 0, tmpObjects, 0, index); - System.arraycopy(elementData, index + 1, tmpObjects, index, elementData.length - index - 1); - - elementData = tmpObjects; - - return removedObj; - } - - @Override - public int size() { - return size; - } - - @Override - public String toString() { - for (int i = 0; i < elementData.length; i++) { - System.out.print(elementData[i] + ","); - } - return ""; - } - - private void rangeCheck(int paramInt) { - if ((paramInt < 0) || (paramInt >= size)) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); - } - } - - private void rangeCheckForAdd(int paramInt) { - if ((paramInt < 0) || (paramInt > size)) { - throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); - } - } - - private String outOfBoundsMsg(int paramInt) { - return "Index: " + paramInt + ", Size: " + size; - } - - private Object[] growIfNeeded() { - if (enableCapacity <= this.size) { - enableCapacity = enableCapacity * 2; - Object[] largerElementData = new Object[enableCapacity]; - System.arraycopy(elementData, 0, largerElementData, 0, elementData.length); - elementData = largerElementData; - } - return elementData; - } - - public static void main(String[] args) { - ArrayList list = new ArrayList(); - for (int i = 0; i <= 11; i++) { - list.add(String.valueOf(i)); - } - System.out.println(list.get(11)); - //list.add(10,"test"); - //list.get(10); - //list.remove(10); - //System.out.println(list); - } - -} +package com.basic.datastructure; + +public class ArrayList implements List { + + private Object[] elementData; + private int size; + + private int enableCapacity; + + public ArrayList() { + this.enableCapacity = 10; + this.elementData = new Object[enableCapacity]; + } + + @Override + public void add(Object o) { + growIfNeeded(); + elementData[size] = o; + this.size++; + } + + @Override + public void add(int index, Object o) { + rangeCheckForAdd(index); + growIfNeeded(); + + Object[] tmpObjects = new Object[elementData.length]; + System.arraycopy(elementData, 0, tmpObjects, 0, index); + tmpObjects[index] = o; + System.arraycopy(elementData, index, tmpObjects, index + 1, elementData.length - index - 1); + + elementData = tmpObjects; + + this.size++; + } + + @Override + public Object get(int index) { + if (index > size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + return elementData[index]; + } + + @Override + public Object remove(int index) { + Object removedObj = this.get(index); + rangeCheck(index); + Object[] tmpObjects = new Object[elementData.length]; + + System.arraycopy(elementData, 0, tmpObjects, 0, index); + System.arraycopy(elementData, index + 1, tmpObjects, index, elementData.length - index - 1); + + elementData = tmpObjects; + + return removedObj; + } + + @Override + public int size() { + return size; + } + + @Override + public String toString() { + for (int i = 0; i < elementData.length; i++) { + System.out.print(elementData[i] + ","); + } + return ""; + } + + private void rangeCheck(int paramInt) { + if ((paramInt < 0) || (paramInt >= size)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); + } + } + + private void rangeCheckForAdd(int paramInt) { + if ((paramInt < 0) || (paramInt > size)) { + throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); + } + } + + private String outOfBoundsMsg(int paramInt) { + return "Index: " + paramInt + ", Size: " + size; + } + + private Object[] growIfNeeded() { + if (enableCapacity <= this.size) { + enableCapacity = enableCapacity * 2; + Object[] largerElementData = new Object[enableCapacity]; + System.arraycopy(elementData, 0, largerElementData, 0, elementData.length); + elementData = largerElementData; + } + return elementData; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + for (int i = 0; i <= 11; i++) { + list.add(String.valueOf(i)); + } + System.out.println(list.get(11)); + //list.add(10,"test"); + //list.get(10); + //list.remove(10); + //System.out.println(list); + } + +} diff --git a/group20/404130810/src/com/basic/datastructure/LinkedList.java b/group20/404130810/src/com/basic/datastructure/LinkedList.java index 43ba7ddd59..4fc92fddec 100644 --- a/group20/404130810/src/com/basic/datastructure/LinkedList.java +++ b/group20/404130810/src/com/basic/datastructure/LinkedList.java @@ -1,176 +1,176 @@ -package com.basic.datastructure; - -public class LinkedList implements List { - private Node first; - private Node last; - - private int size; - - public void add(Object item) { - addLast(item); - } - - public void add(int index, Object item) { - checkRange(index); - if (index == 0) { - addFirst(item); - } else if (index == size) { - addLast(item); - } else { - Node tmpNode = new Node(item); - Node preNode = get(index - 1); - Node nextNode = get(index + 1); - preNode.next = tmpNode; - tmpNode.next = nextNode; - size ++; - } - } - - public Node get(int index) { - checkRange(index); - if(size > 0){ - int loopTimes = 0; - Node p = first; - while(index > loopTimes){ - p = p.next; - loopTimes ++; - } - return p; - } - - return null; - } - - public Object remove(int index) { - checkRange(index); - Node tmpNode = null; - if(index == 0){ - removeFirst(); - }else if(index == size -1){ - removeLast(); - }else{ - tmpNode = get(index); - Node preNode = get(index-1); - Node nextNode = get(index + 1); - preNode.next = nextNode; - size --; - } - - return tmpNode; - } - - public int size() { - return size; - } - - public void addFirst(Object item) { - if (size == 0) { - first = new Node(item); - last = first; - } else { - Node tmpNode = new Node(item); - tmpNode.next = first; - first = tmpNode; - } - size++; - } - - public void addLast(Object item) { - if (size == 0) { - first = new Node(item); - last = first; - } else { - last.next = new Node(item); - last = last.next; - } - size++; - } - - public Object removeFirst() { - Node tmpNode = first; - if(tmpNode == null){ - last = null; - }else if(size == 2){ - first = last; - last = null; - size --; - }else{ - first = first.next; - size--; - } - return tmpNode; - } - - public Object removeLast() { - Node tmpNode = null; - if(size == 1){ - this.removeFirst(); - }else if(size >0){ - int index = size - 1; - tmpNode = last; - get(index - 1).next = null; - last = get(index - 1); - size --; - } - return tmpNode; - } - - private void checkRange(int index) { - if (index > size || index < 0) { - throw new IndexOutOfBoundsException("Index: " + index + "Size: " + size); - } - } - - public String toString() { - StringBuilder sb = new StringBuilder(); - Node p = first; - while (p != null) { - sb.append(p.item + "\n"); - p = p.next; - } - return sb.toString(); - } - - private static class Node { - private Object item; - private Node next; - Node(Object item) { - this.item = item; - } - } - - public static void main(String[] args) { - - /*Test add - LinkedList list = new LinkedList(); - for (int i = 0; i <= 5; i++) { - list.add(i); - } - list.add(3, "test"); - System.out.println(list); - */ - - /*Test remove - list.remove(3); - System.out.println(list); - */ - - /*Test removeLast and removeFirst - System.out.println(list); - list.removeLast(); - System.out.println(list); - list.removeLast(); - System.out.println(list); - list.removeLast(); - System.out.println(list); - */ - - /*Test from Java API - java.util.LinkedList linkedList = new java.util.LinkedList(); - linkedList.add("test"); - linkedList.removeFirst(); - System.out.println(linkedList); - */ - - } -} +package com.basic.datastructure; + +public class LinkedList implements List { + private Node first; + private Node last; + + private int size; + + public void add(Object item) { + addLast(item); + } + + public void add(int index, Object item) { + checkRange(index); + if (index == 0) { + addFirst(item); + } else if (index == size) { + addLast(item); + } else { + Node tmpNode = new Node(item); + Node preNode = get(index - 1); + Node nextNode = get(index + 1); + preNode.next = tmpNode; + tmpNode.next = nextNode; + size ++; + } + } + + public Node get(int index) { + checkRange(index); + if(size > 0){ + int loopTimes = 0; + Node p = first; + while(index > loopTimes){ + p = p.next; + loopTimes ++; + } + return p; + } + + return null; + } + + public Object remove(int index) { + checkRange(index); + Node tmpNode = null; + if(index == 0){ + removeFirst(); + }else if(index == size -1){ + removeLast(); + }else{ + tmpNode = get(index); + Node preNode = get(index-1); + Node nextNode = get(index + 1); + preNode.next = nextNode; + size --; + } + + return tmpNode; + } + + public int size() { + return size; + } + + public void addFirst(Object item) { + if (size == 0) { + first = new Node(item); + last = first; + } else { + Node tmpNode = new Node(item); + tmpNode.next = first; + first = tmpNode; + } + size++; + } + + public void addLast(Object item) { + if (size == 0) { + first = new Node(item); + last = first; + } else { + last.next = new Node(item); + last = last.next; + } + size++; + } + + public Object removeFirst() { + Node tmpNode = first; + if(tmpNode == null){ + last = null; + }else if(size == 2){ + first = last; + last = null; + size --; + }else{ + first = first.next; + size--; + } + return tmpNode; + } + + public Object removeLast() { + Node tmpNode = null; + if(size == 1){ + this.removeFirst(); + }else if(size >0){ + int index = size - 1; + tmpNode = last; + get(index - 1).next = null; + last = get(index - 1); + size --; + } + return tmpNode; + } + + private void checkRange(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + "Size: " + size); + } + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + Node p = first; + while (p != null) { + sb.append(p.item + "\n"); + p = p.next; + } + return sb.toString(); + } + + private static class Node { + private Object item; + private Node next; + Node(Object item) { + this.item = item; + } + } + + public static void main(String[] args) { + + /*Test add + LinkedList list = new LinkedList(); + for (int i = 0; i <= 5; i++) { + list.add(i); + } + list.add(3, "test"); + System.out.println(list); + */ + + /*Test remove + list.remove(3); + System.out.println(list); + */ + + /*Test removeLast and removeFirst + System.out.println(list); + list.removeLast(); + System.out.println(list); + list.removeLast(); + System.out.println(list); + list.removeLast(); + System.out.println(list); + */ + + /*Test from Java API + java.util.LinkedList linkedList = new java.util.LinkedList(); + linkedList.add("test"); + linkedList.removeFirst(); + System.out.println(linkedList); + */ + + } +} diff --git a/group20/404130810/src/com/basic/datastructure/List.java b/group20/404130810/src/com/basic/datastructure/List.java index 13fc25acfd..dcc0c18e18 100644 --- a/group20/404130810/src/com/basic/datastructure/List.java +++ b/group20/404130810/src/com/basic/datastructure/List.java @@ -1,10 +1,10 @@ -package com.basic.datastructure; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); - -} +package com.basic.datastructure; + +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/group20/404130810/src/com/basic/datastructure/Queue.java b/group20/404130810/src/com/basic/datastructure/Queue.java index f83d937e5c..8e2cb7f4d9 100644 --- a/group20/404130810/src/com/basic/datastructure/Queue.java +++ b/group20/404130810/src/com/basic/datastructure/Queue.java @@ -1,33 +1,33 @@ -package com.basic.datastructure; - -public class Queue { - LinkedList list = new LinkedList(); - private int size; - - public void enQueue(Object o){ - list.add(o); - size ++; - } - - public Object deQueue(){ - size --; - return list.removeLast(); - } - - public boolean isEmpty(){ - return list.size() == 0; - } - - public int size(){ - return size; - } - - public static void main(String[] args) { - Queue queue = new Queue(); - for (int i = 0; i < 10; i++) { - queue.enQueue(i); - } - queue.deQueue(); - System.out.println("Finished"); - } -} +package com.basic.datastructure; + +public class Queue { + LinkedList list = new LinkedList(); + private int size; + + public void enQueue(Object o){ + list.add(o); + size ++; + } + + public Object deQueue(){ + size --; + return list.removeLast(); + } + + public boolean isEmpty(){ + return list.size() == 0; + } + + public int size(){ + return size; + } + + public static void main(String[] args) { + Queue queue = new Queue(); + for (int i = 0; i < 10; i++) { + queue.enQueue(i); + } + queue.deQueue(); + System.out.println("Finished"); + } +} diff --git a/group20/404130810/src/com/basic/datastructure/Stack.java b/group20/404130810/src/com/basic/datastructure/Stack.java index 7a58fd49e6..bfff4d0633 100644 --- a/group20/404130810/src/com/basic/datastructure/Stack.java +++ b/group20/404130810/src/com/basic/datastructure/Stack.java @@ -1,41 +1,41 @@ -package com.basic.datastructure; - -public class Stack { - private ArrayList elementData = new ArrayList(); - private int size; - - public void push(Object o){ - elementData.add(o); - size++; - } - public Object pop(){ - size --; - return elementData.remove(elementData.size() - 1); - } - - /** - * Looks at the object at the top of this stack without removing it from the stack. - * @return Object - */ - public Object peek(){ - return elementData.get(elementData.size() - 1); - } - public boolean isEmpty(){ - return size == 0; - } - public int size(){ - return size; - } - - public static void main(String[] args) { - Stack stack = new Stack(); - for (int i = 0; i < 10; i++) { - stack.push(i); - } - System.out.println(stack.peek()); - - stack.pop(); - System.out.println("Finished"); - } - -} +package com.basic.datastructure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + public Object pop(){ + size --; + return elementData.remove(elementData.size() - 1); + } + + /** + * Looks at the object at the top of this stack without removing it from the stack. + * @return Object + */ + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return size == 0; + } + public int size(){ + return size; + } + + public static void main(String[] args) { + Stack stack = new Stack(); + for (int i = 0; i < 10; i++) { + stack.push(i); + } + System.out.println(stack.peek()); + + stack.pop(); + System.out.println("Finished"); + } + +} diff --git a/group20/404130810/src/com/basic/practice/PolymorphicInJava.java b/group20/404130810/src/com/basic/practice/PolymorphicInJava.java index 6bf9996da3..c8da98e61f 100644 --- a/group20/404130810/src/com/basic/practice/PolymorphicInJava.java +++ b/group20/404130810/src/com/basic/practice/PolymorphicInJava.java @@ -1,41 +1,41 @@ -package com.basic.practice; - -/** - * - * @author Wu Alvin - * Java Polymorphic Only represent in method level - * - */ - -class Fruit{ - String name = "Fruit"; - public void print(int i){ - System.out.println("Fruit" + i); - } -} - - -class Apple extends Fruit{ - String name = "Apple"; - public void print(int i){ - System.out.println("Apple" + i); - } -} - - -public class PolymorphicInJava { - - public static void main(String[] args) { - Apple apple = new Apple(); - apple.print(100); - //Apple100 - System.out.println(apple.name); - //Apple - Fruit fruit = apple; - fruit.print(100); - //Apple100 - System.out.println(fruit.name); - //Fruit - } - -} +package com.basic.practice; + +/** + * + * @author Wu Alvin + * Java Polymorphic Only represent in method level + * + */ + +class Fruit{ + String name = "Fruit"; + public void print(int i){ + System.out.println("Fruit" + i); + } +} + + +class Apple extends Fruit{ + String name = "Apple"; + public void print(int i){ + System.out.println("Apple" + i); + } +} + + +public class PolymorphicInJava { + + public static void main(String[] args) { + Apple apple = new Apple(); + apple.print(100); + //Apple100 + System.out.println(apple.name); + //Apple + Fruit fruit = apple; + fruit.print(100); + //Apple100 + System.out.println(fruit.name); + //Fruit + } + +} diff --git a/group20/404130810/src/com/basic/practice/ValuePassInJava.java b/group20/404130810/src/com/basic/practice/ValuePassInJava.java index 6187b5e0fc..c162ba4fc8 100644 --- a/group20/404130810/src/com/basic/practice/ValuePassInJava.java +++ b/group20/404130810/src/com/basic/practice/ValuePassInJava.java @@ -1,44 +1,44 @@ -package com.basic.practice; - -public class ValuePassInJava { - - /* - * Pass the Simple value, etc int String... - * Change will NOT happened - * - */ - public static void main(String[] args) { - String s = new String("123"); - int i = 1; - changeVal(i); - System.out.println(i); - - } - - private static void changeVal(int i){ - i = 2; - } - - /* - * Pass whole OBJECT, but change the Member variable - * Change will happened - */ - - /* - public static void main(String[] args) { - Person p = new Person(); - p.age = 10; - changeAge(p); - System.out.println(p.age); - } - - private static void changeAge(Person p){ - p.age = 20; - } - */ - -} - -class Person{ - int age; -} +package com.basic.practice; + +public class ValuePassInJava { + + /* + * Pass the Simple value, etc int String... + * Change will NOT happened + * + */ + public static void main(String[] args) { + String s = new String("123"); + int i = 1; + changeVal(i); + System.out.println(i); + + } + + private static void changeVal(int i){ + i = 2; + } + + /* + * Pass whole OBJECT, but change the Member variable + * Change will happened + */ + + /* + public static void main(String[] args) { + Person p = new Person(); + p.age = 10; + changeAge(p); + System.out.println(p.age); + } + + private static void changeAge(Person p){ + p.age = 20; + } + */ + +} + +class Person{ + int age; +} diff --git a/group20/404130810/src/com/coderising/array/ArrayUtil.java b/group20/404130810/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..22017ef9d0 --- /dev/null +++ b/group20/404130810/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,155 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayUtil { + + /** + * һa , Ըֵû 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] a = + * [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] reversedArray = new int[origin.length]; + for (int i = origin.length - 1, j = 0; i >= 0 && j < origin.length; i--, j++) { + reversedArray[j] = origin[i]; + } + origin = reversedArray; + } + + /** + * µһ飺 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 zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + zeroCount++; + } + } + int[] removedZeroArray = new int[oldArray.length - zeroCount]; + + for (int i = 0, j = 0; i < oldArray.length; i++, j++) { + if (oldArray[i] == 0) { + j--; + continue; + } + removedZeroArray[j] = oldArray[i]; + } + return removedZeroArray; + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + List mergedArrayList = new ArrayList<>(); + return null; + } + + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] resultArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, resultArray, 0, oldArray.length); + return resultArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ 磬 max = 15 , + * 򷵻صӦΪ [11235813] max = 1, 򷵻ؿ [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + + return null; + } + + private static int fibonacciNum(int n) { + if (n <= 2) { + return 1; + } else { + return fibonacciNum(n - 1) + fibonacciNum(n - 2); + } + } + + /** + * Сڸֵmax max = 23, صΪ[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 һֵmax һ飬 Сmax + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * seperator array array= [3,8,9], seperator = "-" 򷵻ֵΪ"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if (i != array.length - 1) { + sb.append(seperator); + } + } + return sb.toString(); + } + + public static void main(String[] args) { + /* + * int[] origin = { 0, 1, 2, 0, 12 }; new + * ArrayUtil().removeZero(origin); + */ + /* + * int[] array1 = { 3, 5, 7, 8 }; int[] array2 = { 4, 5, 6, 7 }; new + * ArrayUtil().merge(array1, array2); + */ + /* + * int[] array = { 3, 8, 9, 10, 12 }; new ArrayUtil().grow(array, 9); + */ + /* + * int[] array = { 3, 8, 9, 10, 12 }; String seperator = "-"; + * System.out.println(new ArrayUtil().join(array, seperator)); + */ + } + +} \ No newline at end of file diff --git a/group20/404130810/src/com/coderising/litestruts/Struts.java b/group20/404130810/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..6e1e1bab9f --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,87 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.coderising.litestruts.utils.StrutsUtil; +import com.coderising.litestruts.view.View; + +public class Struts { + + public static View runAction(String actionName, Map params) { + + /* + * + * 0. ȡļstruts.xml + * + * 1. actionNameҵӦclass LoginAction, ͨʵ + * parametersеݣösetter parametersе ("name"="test" , + * "password"="1234") , ǾӦõ setNamesetPassword + * + * 2. ͨöexectue ÷ֵ"success" + * + * 3. ͨҵgetter getMessage, ͨã ֵγһHashMap , + * {"message": "¼ɹ"} , ŵViewparameters + * + * 4. struts.xmlе ,Լexecuteķֵ ȷһjsp + * ŵViewjspֶС + * + */ + View view = new View(); + String className = readActionInConfig(actionName); + List rtnList = invokeMethod(className, params); + String result = rtnList.get(0); + String msg = rtnList.get(1); + view.setParameters(buildViewParams(msg)); + view.setJsp(buildViewJsp(result,actionName)); + return view; + } + + + + private static String readActionInConfig(String actionName) { + StrutsUtil util = new StrutsUtil(); + return util.invokedAction(actionName); + } + + private static List invokeMethod(String className, Map params) { + + List rtnList = new ArrayList(); + try { + String name = params.get("name"); + String password = params.get("password"); + // Invoke set method + Class actionClass = Class.forName(className); + Method setNameMethod = actionClass.getMethod("setName", String.class); + Method setPasswordMethod = actionClass.getMethod("setPassword", String.class); + Object action = actionClass.newInstance(); + setNameMethod.invoke(action, name); + setPasswordMethod.invoke(action, password); + // Invoke execute method and add to the return List as first element + Method executeMethod = actionClass.getMethod("execute"); + rtnList.add(executeMethod.invoke(action).toString()); + // Invoke getMessage method and add to the return List as second element + Method getMessageMethod = actionClass.getMethod("getMessage"); + rtnList.add(getMessageMethod.invoke(action).toString()); + + } catch (Exception e) { + e.printStackTrace(); + } + return rtnList; + } + + private static Map buildViewParams(String msg) { + Map viewParams = new HashMap(); + viewParams.put("message", msg); + return viewParams; + } + + private static String buildViewJsp(String result, String actionName) { + StrutsUtil util = new StrutsUtil(); + return util.invokeResult(actionName,result); + } + +} \ No newline at end of file diff --git a/group20/404130810/src/com/coderising/litestruts/action/LoginAction.java b/group20/404130810/src/com/coderising/litestruts/action/LoginAction.java new file mode 100644 index 0000000000..3e252d26ee --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/action/LoginAction.java @@ -0,0 +1,42 @@ +package com.coderising.litestruts.action; + +/** + * һչʾ¼ҵ࣬ еû붼Ӳġ + * + * @author liuxin + * + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} \ No newline at end of file diff --git a/group20/404130810/src/com/coderising/litestruts/struts.xml b/group20/404130810/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e05b09a433 --- /dev/null +++ b/group20/404130810/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/group20/404130810/src/com/coderising/litestruts/test/StrutsTest.java b/group20/404130810/src/com/coderising/litestruts/test/StrutsTest.java new file mode 100644 index 0000000000..300b6efe6a --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/test/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts.test; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.litestruts.Struts; +import com.coderising.litestruts.view.View; + + +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"); //ԤIJһ + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} \ No newline at end of file diff --git a/group20/404130810/src/com/coderising/litestruts/utils/StrutsUtil.java b/group20/404130810/src/com/coderising/litestruts/utils/StrutsUtil.java new file mode 100644 index 0000000000..3d26041ef4 --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/utils/StrutsUtil.java @@ -0,0 +1,76 @@ +package com.coderising.litestruts.utils; + +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class StrutsUtil { + + public final static String CONFIG_PATH = "../struts.xml"; + + public final static String CONFIG_NODE_ACTION = "action"; + + public final static String CONFIG_ATTR_NAME = "name"; + + public final static String CONFIG_ATTR_CLASS = "class"; + + public String invokedAction(String actionName){ + if(null != actionName && !"".equals(actionName)){ + Document doc = generateDoc(); + NodeList nodeList = doc.getElementsByTagName(CONFIG_NODE_ACTION); + for (int i = 0; i < nodeList.getLength(); i++) { + String actionNameConfiged = nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue(); + if(actionName.equals(actionNameConfiged)){ + return nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_CLASS).getNodeValue(); + } + } + } + throw new RuntimeException("actionName can't be found"); + } + + private Document generateDoc(){ + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + Document doc = null; + try { + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + doc = docBuilder.parse (lookupConfigFile()); + } catch (Exception e) { + e.printStackTrace(); + } + return doc; + } + + private File lookupConfigFile(){ + URL url = getClass().getResource(CONFIG_PATH); + return new File(url.getPath()); + } + + + public String invokeResult(String actionName, String result) { + Document doc = generateDoc(); + NodeList nodeList = doc.getElementsByTagName("result"); + List subNodeList = new ArrayList(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node parentNode = nodeList.item(i).getParentNode(); + if(parentNode.getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue().equals(actionName)){ + subNodeList.add(nodeList.item(i)); + } + } + for (int i = 0; i < subNodeList.size(); i++) { + Node node = subNodeList.get(i); + if(node.getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue().equals(result)){ + return node.getTextContent(); + } + } + throw new RuntimeException("result can't be found"); + } + +} diff --git a/group20/404130810/src/com/coderising/litestruts/view/View.java b/group20/404130810/src/com/coderising/litestruts/view/View.java new file mode 100644 index 0000000000..ff1a735948 --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/view/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts.view; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} \ No newline at end of file diff --git "a/group20/423184723/Test/\345\215\232\345\256\242\345\234\260\345\235\200" "b/group20/423184723/Test/\345\215\232\345\256\242\345\234\260\345\235\200" new file mode 100644 index 0000000000..b5457af5b6 --- /dev/null +++ "b/group20/423184723/Test/\345\215\232\345\256\242\345\234\260\345\235\200" @@ -0,0 +1 @@ +http://www.cnblogs.com/yyssyh213/p/6442285.html \ No newline at end of file diff --git a/group20/423184723/src/basic/ArrayList.java b/group20/423184723/src/basic/ArrayList.java new file mode 100644 index 0000000000..64a19bea8e --- /dev/null +++ b/group20/423184723/src/basic/ArrayList.java @@ -0,0 +1,78 @@ +package com.coding.basic; + +import com.sun.media.sound.EmergencySoundbank; + +public class ArrayList implements List { + /** + * 列表中元素的个数 + */ + private int size = 0; + + /** + * 初始化数组大小 + */ + private int arraySize = 100; + /** + * 初始化数组 + */ + private Object[] elementData = new Object[arraySize]; + + /** + * 添加方法 + */ + public void add(Object o){ + if(size>=(arraySize*0.75)){ + Object [] target = new Object[(int) (arraySize*1.5)]; + System.arraycopy(elementData,0,target,0,arraySize); + target[size-1] = o; + size++; + }else if(size<(arraySize*0.75)){ + elementData[size-1]=o; + size++; + } + } + /** + * 根据索引添加方法 + */ + public void add(int index, Object o){ + if(size >= arraySize*0.75){ + Object [] target = new Object[(int) (arraySize*1.5)]; + System.arraycopy(elementData,0,target,0,arraySize); + for (int j = target.length;j>=index;j--){ + target[j-1] = target[j-2]; + } + target[index] = o; + size++; + }else if(size < arraySize*0.75){ + for (int j = elementData.length;j>=index;j--){ + elementData[j-1] = elementData[j-2]; + } + elementData[index] = o; + size++; + } + } + /** + * 根据索引获取对象 + */ + public Object get(int index){ + return elementData[index]; + } + /** + * 根据索引移除对象 + */ + public Object remove(int index){ + for (int i = index; i < elementData.length; i++) { + elementData[i]=elementData[i+1]; + size++; + } + return elementData[index]; + } + /** + * 获取数组大小 + */ + public int size(){ + return this.size; + } + + +} diff --git a/group20/423184723/src/basic/BinaryTreeNode.java b/group20/423184723/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group20/423184723/src/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/group20/423184723/src/basic/Iterator.java b/group20/423184723/src/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group20/423184723/src/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group20/423184723/src/basic/LinkedList.java b/group20/423184723/src/basic/LinkedList.java new file mode 100644 index 0000000000..9caf065304 --- /dev/null +++ b/group20/423184723/src/basic/LinkedList.java @@ -0,0 +1,146 @@ +package com.coding.basic; + +import com.coding.basic.LinkedList.Node; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + System.out.println(index + "无效指数"); + return; + } + if (head.data == null) { + if (index == 0) { + head.data = o; + head.next = null; + size++; + return; + } else { + System.out.println("无效指数"); + return; + } + } + Node node = new Node(o); + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node temp = curr.next; + curr.next = node; + node.next = temp; + size++; + } + public Object get(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node result = head; + for (int i = 0; i < index; i++) { + result = result.next; + } + return result; + } + public Object remove(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = curr.next.next; + size--; + return result; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node temp = head; + head = new Node(o); + head.next = temp; + size++; + } + public void addLast(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + public Object removeFirst(){ + if (head.data == null) { + return null; + } + Node result = head; + head = head.next; + size--; + return result; + } + public Object removeLast(){ + if (head.data == null) { + return null; + } + Node curr = head; + for (int i = 0; i < size - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = null; + size--; + return result; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + public Node(Object o) { + data = o; + next = null; + } + + + + } +} diff --git a/group20/423184723/src/basic/List.java b/group20/423184723/src/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group20/423184723/src/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/group20/423184723/src/basic/Queue.java b/group20/423184723/src/basic/Queue.java new file mode 100644 index 0000000000..bb4e6bef5c --- /dev/null +++ b/group20/423184723/src/basic/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + int length = list.size(); + if (length == 0) { + return null; + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + if (list.size() == 0) { + return true; + } else { + return false; + } + } + + public int size(){ + return list.size; + } +} diff --git a/group20/423184723/src/basic/Stack.java b/group20/423184723/src/basic/Stack.java new file mode 100644 index 0000000000..6f3def6f0f --- /dev/null +++ b/group20/423184723/src/basic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.remove(length - 1); + } + + public Object peek(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.get(length - 1); + } + public boolean isEmpty(){ + if (elementData.size() != 0) { + return false; + } else { + return true; + } + } + public int size(){ + return elementData.size(); + } +} diff --git a/group20/452472201/src/com/coding/basic/ArrayList.java b/group20/452472201/src/com/coding/basic/ArrayList.java index 1af26ce934..80746f1675 100644 --- a/group20/452472201/src/com/coding/basic/ArrayList.java +++ b/group20/452472201/src/com/coding/basic/ArrayList.java @@ -4,10 +4,9 @@ public class ArrayList implements List { private int size=0; - private Object[] elementData =new Object[10]; + private Object[] elementData =new Object[5]; - //数组扩容 private void ensureCapacityInternal(){ if(size==elementData.length){ Object[] newArray = new Object[size*2]; @@ -33,8 +32,8 @@ public void add(int index, Object o){ } System.arraycopy(elementData, index, elementData, index+1,size-index ); - elementData[index]=o; - size++; + elementData[index]=o; + size++; } public Object get(int index){ @@ -75,24 +74,41 @@ public int size(){ private class Iter implements Iterator { - //计数器 + private int coursor=-1; - //判断是否存在下一个 + public boolean hasNext(){ return coursor+1> 1); i++) { + int num = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = num; + } + } + + /** + * 现在有如下的一个数组: 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 count = 0;// 计数器 + /* + * 利用冒泡,将0元素向后排 {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * {1,3,4,5,0,6,6,0,5,4,7,6,7,0,5,0} {1,3,4,5,6,6,0,5,4,7,6,7,0,5,0,0} + * .... + */ + for (int i = 0; i < oldArray.length - count; i++) { + // 索引为i的元素为0,则依次将索引i的元素与i+1的元素对换 + if (oldArray[i] == 0) { + for (int j = i; j < oldArray.length - 1 - count; j++) { + int num = oldArray[j]; + oldArray[j] = oldArray[j + 1]; + oldArray[j + 1] = num; + } + count++;// 计数器+1 + i--;// 防止原索引i+1位置的元素为0, + } + } + // 创建新数组 + int[] newArray = new int[oldArray.length - count]; + System.arraycopy(oldArray, 0, newArray, 0, newArray.length); + return newArray; + } + + /** + * 现在有如下的一个数组: 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[] removeZero2(int[] oldArray) { + int count = 0;// 计数器 + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + count++;// 计数器+1 + } + // 创建新数组 + int[] newArray = new int[oldArray.length - count]; + for (int i = 0, j = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2) { + int[] newArray = new int[array1.length + array2.length]; + int i = 0, j = 0, k = 0; + while (i < array1.length && j < array2.length) { + // <= 都取 array1 + if (array1[i] <= array2[j]) { + // 等于时,将array2下标++ + if (array1[i] == array2[j]) + j++; + newArray[k++] = array1[i++]; + } else + newArray[k++] = array2[j++]; + + } + // 将没有循环完毕的元素插入 + while (i < array1.length) + newArray[k++] = array1[i++]; + while (j < array2.length) + newArray[k++] = array2[j++]; + int[] result = newArray; + // 长度缩短则新建数组 + if (k < newArray.length) { + result = new int[k]; + for (int l = 0; l < result.length; l++) + result[l] = newArray[l]; + } + 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) { + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) + return new int[] {}; + int[] nums = new int[max]; + nums[0] = nums[1] = 1; + int flag; + for (flag = 0; (flag < max - 2 && nums[flag] + nums[flag + 1] < max); flag++) { + nums[flag + 2] = nums[flag] + nums[flag + 1]; + } + // 创建新数组 + int[] newArray = nums; + if (newArray.length != flag + 2) { + newArray = new int[flag + 2]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = nums[i]; + } + } + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] array = new int[max>>1]; + int flag = 0; + for (int i = 2; i < max; i++) { + int j; + for (j = 2; j <= (i >> 1); j++) { + if (i % j == 0) + break; + + } + //如果大于,则证明j++有运行,已经完整对比 + if(j > i>>1) + array[flag++] = i; + + } + int[] newArray = array; + if(flag < array.length){ + newArray = new int[flag]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + } + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] array = new int[max]; + int flag = 0; + for (int i = 1; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) + sum+=j; + } + //如果大于,则证明j++有运行,已经完整对比 + if(sum == i) + array[flag++] = i; + + } + int[] newArray = array; + if(flag < array.length){ + newArray = new int[flag]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + } + return newArray; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + String str = ""; + for (int i = 0; i < array.length; i++) + str += i != array.length - 1 ? array[i] + seperator : array[i]; + return str; + } +} diff --git a/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java b/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java new file mode 100644 index 0000000000..009dec2938 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java @@ -0,0 +1,98 @@ +package org.wsc.array; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] nums = new int[]{7, 9 , 30, 3, 5}; + ArrayUtil.reverseArray(nums); + assertArrayEquals(nums, new int[]{5, 3 ,30, 9, 7}); + } + + /** + * 删除0 + */ + @Test + public void testRemoveZero() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + nums = ArrayUtil.removeZero(nums); + assertArrayEquals(nums, new int[]{7, 9 ,30, 3, 5}); + } + + /** + * 删除0 + */ + @Test + public void testRemoveZero2() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + nums = ArrayUtil.removeZero2(nums); + assertArrayEquals(nums, new int[]{7, 9 ,30, 3, 5}); + } + + /** + * 拼接 + */ + @Test + public void testJoin() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + String str = ArrayUtil.join(nums,"-"); + assertEquals(str, "0-7-9-0-0-30-0-3-5-0"); + } + + /** + * 扩容 + */ + @Test + public void testGrow() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5}; + nums = ArrayUtil.grow(nums,3); + assertTrue(nums.length==12); + } + + /** + * 合并 + */ + @Test + public void testMerge() { + int[] nums = new int[]{3, 5, 7,8}; + int[] nums2 = new int[]{4, 5, 6,7,8}; + nums = ArrayUtil.merge(nums,nums2); + assertTrue(nums.length==6); + assertArrayEquals(nums, new int[]{3, 4 ,5, 6, 7,8}); + } + + /** + *斐波那契数列 + */ + @Test + public void testFibonacci() { + int[] nums = ArrayUtil.fibonacci(15); + assertTrue(nums.length==7); + assertArrayEquals(nums, new int[]{1,1,2,3,5,8,13}); + } + + /** + * 素数 + */ + @Test + public void testGetPrimes() { + int[] nums = ArrayUtil.getPrimes(23); + assertTrue(nums.length==8); + assertArrayEquals(nums, new int[]{2,3,5,7,11,13,17,19}); + } + + /** + * 完数 + */ + @Test + public void testGetPerfectNumbers() { + int[] nums = ArrayUtil.getPerfectNumbers(10000); + assertTrue(nums.length==4); + assertArrayEquals(nums, new int[]{6,28,496,8128}); + } + +} diff --git a/group20/592146505/coderising/src/org/wsc/litestruts/Action.java b/group20/592146505/coderising/src/org/wsc/litestruts/Action.java new file mode 100644 index 0000000000..083519bac6 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/Action.java @@ -0,0 +1,21 @@ +package org.wsc.litestruts; + +import java.util.Set; + +public class Action { + + /** 标签名 */ + private String tag; + /** 属性 */ + private String name; + + /** 类名 */ + private String className; + + /** 子标签 */ + private Set statusElement; + + /** 文本内容 */ + private String textContent; + +} diff --git a/group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java b/group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java new file mode 100644 index 0000000000..bb23f5eebb --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package org.wsc.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/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java b/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java new file mode 100644 index 0000000000..30a418979f --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java @@ -0,0 +1,124 @@ +package org.wsc.litestruts; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.wsc.litestruts.util.DocumentUtil; +import org.xml.sax.SAXException; + +public class Struts { + private static final DocumentUtil DOCUMENT_UTIL; + private static Document document; + static{ + /* 0. 读取配置文件struts.xml */ + DOCUMENT_UTIL = DocumentUtil.newInstance(); + try { + document = DOCUMENT_UTIL.getDocument("src/struts.xml"); + } catch (ParserConfigurationException | SAXException | IOException e) { + e.printStackTrace(); + } + } + + /** + * + * @param actionName + * @param parameters + * @return + */ + public static View runAction(String actionName, Map parameters) { + String className = null; + String jsp = null; + Map results = new HashMap(); + Node struts = document.getDocumentElement();// 获取根节点 + NodeList actions = struts.getChildNodes();// 获取子节点 + for (int i = 0; i < actions.getLength(); i++) { + // 过滤空节点 + if (actions.item(i).getNodeType() != Node.ELEMENT_NODE) + continue; + Element action = (Element) actions.item(i); + if (!action.getAttribute("name").equals(actionName)) + continue; + className = action.getAttribute("class"); + /* + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + */ + Class clazz = null; + Object instance = null; + try { + clazz = Class.forName(className); + instance = clazz.getConstructor().newInstance(); + Set keySet = parameters.keySet(); + for (String key : keySet) { + Object parameter = parameters.get(key); + Method method = instance.getClass().getMethod( + "set" + (key.substring(0, 1).toUpperCase() + key.substring(1)), parameter.getClass()); + method.invoke(instance, parameter); + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + + /* 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" */ + String rt = null; + try { + Method method = clazz.getMethod("execute"); + rt = (String) method.invoke(instance); + } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /* + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap + * , 例如 {"message": "登录成功"} , 放到View对象的parameters + */ + Field[] fields = instance.getClass().getDeclaredFields(); + Method[] methods = instance.getClass().getMethods(); + for (Field field : fields) { + String fieldName = field.getName(); + for (Method method : methods) { + if (method.getName() + .equals(("get" + (fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1))))) { + try { + results.put(fieldName, (String) method.invoke(instance)); + break; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + NodeList resultNodes = action.getChildNodes(); + for (int j = 0; j < resultNodes.getLength(); j++) { + if (resultNodes.item(j).getNodeType() != Node.ELEMENT_NODE) + continue; + Element result = (Element) resultNodes.item(j); + if (!result.getAttribute("name").equals(rt)) + continue; + jsp = result.getTextContent(); + } + + } + /* + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + View view = new View(); + view.setJsp(jsp); + view.setParameters(results); + return view; + } + +} diff --git a/group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java b/group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8101e95fe3 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package org.wsc.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/group20/592146505/coderising/src/org/wsc/litestruts/View.java b/group20/592146505/coderising/src/org/wsc/litestruts/View.java new file mode 100644 index 0000000000..f21906d1a5 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/View.java @@ -0,0 +1,26 @@ +package org.wsc.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/group20/592146505/coderising/src/org/wsc/litestruts/util/DocumentUtil.java b/group20/592146505/coderising/src/org/wsc/litestruts/util/DocumentUtil.java new file mode 100644 index 0000000000..9125b244cd --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/util/DocumentUtil.java @@ -0,0 +1,69 @@ +package org.wsc.litestruts.util; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +/** + * DOM解析工具类 + * 懒汉单例模式 + * @author Administrator + * @date 2017年2月28日下午9:45:39 + * @version v1.0 + * + */ +public class DocumentUtil { + private static DocumentUtil documentUtil; + + private DocumentUtil() { + super(); + } + + /** + * 获取实例 + * @return + */ + public static DocumentUtil newInstance(){ + if(documentUtil == null) + synchronized (DocumentUtil.class) { + if(documentUtil == null) + documentUtil = new DocumentUtil(); + } + return documentUtil; + } + + + /** + * 解析XML文件获取DOM树 + * @param fileUrl + * XML文件路径 + * @return + * @throws IOException + * @throws SAXException + * @throws ParserConfigurationException + */ + public Document getDocument(String fileUrl) throws ParserConfigurationException, SAXException, IOException{ + return getDocument(new File(fileUrl)); + } + /** + * 解析XML文件获取DOM树 + * @param file + * XML文件实例 + * @return + * @throws ParserConfigurationException + * @throws IOException + * @throws SAXException + */ + public Document getDocument(File xmlFile) throws ParserConfigurationException, SAXException, IOException{ + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//解析器工厂 + DocumentBuilder db = dbf.newDocumentBuilder();//解析器 + return db.parse(xmlFile);//DOM树 + } + +} diff --git a/group20/592146505/coderising/src/struts.xml b/group20/592146505/coderising/src/struts.xml new file mode 100644 index 0000000000..b709ec6636 --- /dev/null +++ b/group20/592146505/coderising/src/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group20/592146505/data _structure/src/org/wsc/exception/NullElementException.java b/group20/592146505/data _structure/src/org/wsc/exception/NullElementException.java new file mode 100644 index 0000000000..aa7763ef00 --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/exception/NullElementException.java @@ -0,0 +1,23 @@ +package org.wsc.exception; + +/** + * + * 空元素异常 + * @author Administrator + * @date 2017年2月26日下午4:15:49 + * @version v1.0 + * + */ +public class NullElementException extends RuntimeException { + + private static final long serialVersionUID = 4729177529481680909L; + + public NullElementException() { + super(); + } + + public NullElementException(String message) { + super(message); + } + +} diff --git a/group20/592146505/data _structure/src/org/wsc/exception/RepeatingElementException.java b/group20/592146505/data _structure/src/org/wsc/exception/RepeatingElementException.java new file mode 100644 index 0000000000..a5b66c54a1 --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/exception/RepeatingElementException.java @@ -0,0 +1,23 @@ +package org.wsc.exception; + +/** + * + * 重复元素异常 + * @author Administrator + * @date 2017年2月26日下午4:15:49 + * @version v1.0 + * + */ +public class RepeatingElementException extends RuntimeException { + + private static final long serialVersionUID = 4729177529481680909L; + + public RepeatingElementException() { + super(); + } + + public RepeatingElementException(String message) { + super(message); + } + +} diff --git a/group20/592146505/data _structure/src/cn/wsc/util/ArrayList.java b/group20/592146505/data _structure/src/org/wsc/list/ArrayList.java similarity index 95% rename from group20/592146505/data _structure/src/cn/wsc/util/ArrayList.java rename to group20/592146505/data _structure/src/org/wsc/list/ArrayList.java index 94bb1f3217..28b8db4132 100644 --- a/group20/592146505/data _structure/src/cn/wsc/util/ArrayList.java +++ b/group20/592146505/data _structure/src/org/wsc/list/ArrayList.java @@ -1,9 +1,11 @@ -package cn.wsc.util; +package org.wsc.list; import java.util.Arrays; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; +import javafx.stage.StageStyle; + /** * ArrayList类 * @@ -93,17 +95,16 @@ public void remove() { } - // @Override - // public Object[] toArray() { - // // TODO Auto-generated method stub - // return null; - // } - // - // @Override - // public T[] toArray(T[] a) { - // // TODO Auto-generated method stub - // return null; - // } + @SuppressWarnings("unchecked") + @Override + public E[] toArray() { + return (E[]) elementData; + } + + @Override + public T[] toArray(T[] a) { + return null; + } @Override public boolean add(E element) { diff --git a/group20/592146505/data _structure/src/org/wsc/list/Iterator.java b/group20/592146505/data _structure/src/org/wsc/list/Iterator.java new file mode 100644 index 0000000000..59590bbf7e --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/list/Iterator.java @@ -0,0 +1,21 @@ +package org.wsc.list; + +public interface Iterator { + /** + * 是否存在下一个元素 + * @return + */ + boolean hasNext(); + + /** + * 获取下一个元素 + * @return + */ + E next(); + + /** + * 删除当前元素 + */ + void remove(); + +} diff --git a/group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java b/group20/592146505/data _structure/src/org/wsc/list/LinkedList.java similarity index 90% rename from group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java rename to group20/592146505/data _structure/src/org/wsc/list/LinkedList.java index ccb9ff2783..b909cfeabc 100644 --- a/group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java +++ b/group20/592146505/data _structure/src/org/wsc/list/LinkedList.java @@ -1,18 +1,19 @@ -package cn.wsc.util; +package org.wsc.list; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; /** * LinkedList类 - * + * 实现List接口和Queue接口 + * 基于链表的集合 * @author Administrator * @date 2017年2月25日上午10:52:41 * @version v1.0 * * @param */ -public class LinkedList implements List { +public class LinkedList implements List,Queue { private int size; Node first; // 链表的头节点 @@ -224,6 +225,14 @@ public E remove(int index) { return unlink(node(index)); } + public E removeFirst() { + //获取头节点 + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return remove(0); + } + /** * 删除节点 * @@ -252,6 +261,16 @@ E unlink(Node x) { return element; } + @Override + public void enQueue(E e) { + linkLast(e); + } + + @Override + public E deQueue() { + return removeFirst(); + } + /** * 位置范围检查 >0 && <=size * @@ -281,4 +300,17 @@ private void checkElementIndex(int index) { private String outOfBoundsMsg(int index) { return "Index: " + index + ", Size: " + this.size; } + + @Override + public E[] toArray() { + // TODO Auto-generated method stub + return null; + } + + @Override + public T[] toArray(T[] a) { + // TODO Auto-generated method stub + return null; + } + } diff --git a/group20/592146505/data _structure/src/cn/wsc/util/List.java b/group20/592146505/data _structure/src/org/wsc/list/List.java similarity index 83% rename from group20/592146505/data _structure/src/cn/wsc/util/List.java rename to group20/592146505/data _structure/src/org/wsc/list/List.java index 9b61278fc7..2fd90c6395 100644 --- a/group20/592146505/data _structure/src/cn/wsc/util/List.java +++ b/group20/592146505/data _structure/src/org/wsc/list/List.java @@ -1,4 +1,4 @@ -package cn.wsc.util; +package org.wsc.list; /** * List接口 @@ -38,20 +38,21 @@ public interface List { * @return */ Iterator iterator(); - // - // /** - // * 返回集合数组对象 - // * - // * @return - // */ - // Object[] toArray(); - // - // /** - // * 将集合元素复制到新数组中 - // * @param a - // * @return - // */ - // T[] toArray(T[] a); + + /** + * 返回集合数组对象 + * + * @return + */ + Object[] toArray(); + + /** + * 将集合元素复制到新数组中 + * + * @param a + * @return + */ + T[] toArray(T[] a); /** * 在集合末尾追加元素 diff --git a/group20/592146505/data _structure/src/org/wsc/list/Queue.java b/group20/592146505/data _structure/src/org/wsc/list/Queue.java new file mode 100644 index 0000000000..2087335e58 --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/list/Queue.java @@ -0,0 +1,42 @@ +package org.wsc.list; + +/** + * + * 队列 + * + * @author Administrator + * @date 2017年2月25日下午6:08:01 + * @version v1.0 + * + * @param + */ +public interface Queue { + + /** + * 入列 + * + * @param e + */ + public void enQueue(E e); + + /** + * 出列 + * + * @return + */ + public E deQueue(); + + /** + * 是否为空 + * + * @return + */ + public boolean isEmpty(); + + /** + * 元素长度 + * + * @return + */ + public int size(); +} diff --git a/group20/592146505/data _structure/src/org/wsc/stack/Stack.java b/group20/592146505/data _structure/src/org/wsc/stack/Stack.java new file mode 100644 index 0000000000..96b931ca07 --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/stack/Stack.java @@ -0,0 +1,24 @@ +package org.wsc.stack; + +import org.wsc.list.ArrayList; + +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/group20/592146505/data _structure/src/org/wsc/tree_node/BinaryTreeNode.java b/group20/592146505/data _structure/src/org/wsc/tree_node/BinaryTreeNode.java new file mode 100644 index 0000000000..b68784541d --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/tree_node/BinaryTreeNode.java @@ -0,0 +1,166 @@ +package org.wsc.tree_node; + +import org.wsc.exception.NullElementException; +import org.wsc.exception.RepeatingElementException; + +/** + * BinaryTreeNode 二叉树结构 + * + * + * @author Administrator + * @date 2017年2月26日下午5:47:32 + * @version v1.0 + * + * @param + * 必须实现Comparable接口 + */ +@SuppressWarnings("rawtypes") +public class BinaryTreeNode { + + /** 左节点 */ + private BinaryTreeNode left; + /** 数据区 */ + private E data; + /** 右节点 */ + private BinaryTreeNode right; + + /** + * 插入 + * + * @param data + * @return + */ + @SuppressWarnings("unchecked") + public BinaryTreeNode insert(E data) { + if (data == null) + throw new NullElementException("Do not insert a null"); + // 当前数据区为空,则将data放入数据区 + if (this.data == null) { + this.data = data; + return this; + } + // 对比当前数据区数据和data大小 + int result = this.data.compareTo(data); + // 如果相等,则抛出异常 + if (result == 0) + throw new RepeatingElementException("Do not insert duplicate element"); + // 当前数据区数据大于data,将data递归放入左节点 + if (result > 0) { + // 左节点为空,则将数据置入左节点 + if (left == null) + left = new BinaryTreeNode(data); + else// 左节点不为空,则将数据递归置入左节点 + left.insert(data); + } else { + // 右节点为空,则将数据置入右节点 + if (right == null) + right = new BinaryTreeNode(data); + else// 右节点不为空,则将数据递归置入右节点 + right.insert(data); + } + return this; + } + + /** + * 查询 + * + * @param data + * @return + */ + @SuppressWarnings("unchecked") + public BinaryTreeNode seek(E data) { + checkCurrElement(); + if (data == null) + return null; + // 对比当前数据区数据和data大小 + int result = this.data.compareTo(data); + if (result == 0) { + return this; + } else if (result > 0) {// 当前数据区数据大于data,递归对比左节点 + return left == null ? null : left.seek(data); + } else {// 当前数据区数据小于data,递归对比右节点 + return right == null ? null : right.seek(data); + } + + } + + /** + * 删除 + * + * @param data + * @return + */ + public BinaryTreeNode remove(E data) { + return removeChild(null, data); + } + + @SuppressWarnings("unchecked") + public BinaryTreeNode removeChild(BinaryTreeNode supNode, E data) { + checkCurrElement(); + if (data == null) + return null; + // 对比当前数据区数据和data大小 + int result = this.data.compareTo(data); + // 如果相同,将通过父节点将子节点引用置为null + if (supNode != null && result == 0) { + if (supNode.left == this) + supNode.left = null; + else + supNode.right = null; + } else if (result > 0) {// 当前数据区数据大于data,递归对比左节点 + return left == null ? null : left.removeChild(this, data); + } else {// 当前数据区数据小于data,递归对比右节点 + return right == null ? null : right.removeChild(this, data); + } + return this; + } + + /** + * 检查当前节点元素是否有效 + */ + private void checkCurrElement() { + if (this.data == null) + throw new NullElementException("The current node element is null"); + } + + public BinaryTreeNode() { + super(); + } + + public BinaryTreeNode(E data) { + super(); + this.data = data; + } + + public BinaryTreeNode(BinaryTreeNode left, E data, BinaryTreeNode right) { + super(); + this.left = left; + this.data = data; + this.right = right; + } + + public E getData() { + return data; + } + + public void setData(E data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + +} diff --git "a/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" "b/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..82dd8add37 --- /dev/null +++ "b/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +week02:http://www.jianshu.com/p/22d2cbefdaa1 diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java b/group20/755659358/week01/src/liuxincourse/ArrayList.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java rename to group20/755659358/week01/src/liuxincourse/ArrayList.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java b/group20/755659358/week01/src/liuxincourse/LinkedList.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java rename to group20/755659358/week01/src/liuxincourse/LinkedList.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java b/group20/755659358/week01/src/liuxincourse/List.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java rename to group20/755659358/week01/src/liuxincourse/List.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java b/group20/755659358/week01/src/liuxincourse/Queue.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java rename to group20/755659358/week01/src/liuxincourse/Queue.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java b/group20/755659358/week01/src/liuxincourse/Stack.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java rename to group20/755659358/week01/src/liuxincourse/Stack.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java b/group20/755659358/week01/test/liuxincourse/ArrayListTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java rename to group20/755659358/week01/test/liuxincourse/ArrayListTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java b/group20/755659358/week01/test/liuxincourse/LinkedListTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java rename to group20/755659358/week01/test/liuxincourse/LinkedListTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java b/group20/755659358/week01/test/liuxincourse/QueueTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java rename to group20/755659358/week01/test/liuxincourse/QueueTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java b/group20/755659358/week01/test/liuxincourse/StackTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java rename to group20/755659358/week01/test/liuxincourse/StackTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java b/group20/755659358/week01/test/liuxincourse/SuiteTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java rename to group20/755659358/week01/test/liuxincourse/SuiteTest.java diff --git a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java new file mode 100644 index 0000000000..e4985900cb --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java @@ -0,0 +1,219 @@ +package com.coderising.practice.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int j = origin.length - 1; + for (int i = 0; i <= j; i++, j--) { + int temp = origin[i]; + origin[i] = origin[j]; + origin[j] = 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 int[] removeZero(int[] oldArray) { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < oldArray.length; i++) { + list.add(oldArray[i]); + } + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + Integer integer = (Integer) iterator.next(); + if (integer.equals(0)) { + iterator.remove(); + } + } + + return integerListToArray(list); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < array1.length; i++) { + if (list.contains(Integer.valueOf(array1[i]))) { + continue; + } + list.add(Integer.valueOf(array1[i])); + } + for (int i = 0; i < array2.length; i++) { + if (list.contains(Integer.valueOf(array2[i]))) { + continue; + } + list.add(Integer.valueOf(array2[i])); + } + Collections.sort(list); + return integerListToArray(list); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + + return Arrays.copyOf(oldArray, oldArray.length + size); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max < 2) { + return new int[0]; + } + ArrayList list = new ArrayList<>(); + list.add(1); + list.add(1); + int nextFibo = list.get(list.size() - 2) + list.get(list.size() - 1); + while (nextFibo < max) { + list.add(nextFibo); + nextFibo = list.get(list.size() - 2) + list.get(list.size() - 1); + } + + return integerListToArray(list); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max<2) { + return new int[]{}; + } + ArrayList list = new ArrayList<>(); + + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + return integerListToArray(list); + } + + public int[] integerListToArray(List list){ + int len = list.size(); + int[] result = new int[len]; + for (int i = 0; i < len; i++) { + result[i] = list.get(i); + } + return result; + } + + public boolean isPrime(int a) { + boolean flag = true; + for (int i = 2; i <= Math.sqrt(a); i++) { + if (a % i == 0) { + flag = false; + break; + } + } + return flag; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList list=new ArrayList<>(); + for (int i = 2; i < max; i++) { + int [] splits=numSplit(i); + if (sumArray(splits)==i) { + list.add(i); + } + } + + return integerListToArray(list); + } + + public int sumArray(int[] arr){ + int sum=0; + for (int i = 0; i < arr.length; i++) { + sum+=arr[i]; + } + return sum; + } + + public int[] numSplit(int x){ + if (x<=1) { + return new int[]{}; + } + if (x==2) { + return new int[]{1,2}; + } + int k=1; + ArrayList list=new ArrayList<>(); + while (k<=x&&(x/k>=2)) { + if (x%k==0) { + list.add(k); + } + k++; + } + + + return integerListToArray(list); + } + + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb=new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if (i!=array.length-1) { + sb.append(seperator); + } + } + return sb.toString(); + } + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java new file mode 100644 index 0000000000..2984b2719c --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java @@ -0,0 +1,73 @@ +package com.coderising.practice.array; + +import static org.junit.Assert.*; + +import java.util.ArrayList; + +import org.junit.Before; +import org.junit.Test; + +import com.coderising.practice.array.ArrayUtil; + +public class ArrayUtisTest { + + private ArrayUtil util; + + @Before + public void init(){ + util=new ArrayUtil(); + } + + + @Test + public void testReverse(){ + int [] origin={1,2,3,4,6}; + ArrayList list=new ArrayList<>(); + list.add(1); + list.add(2); + util.reverseArray(origin); + assertArrayEquals(new int[]{6,4,3,2,1}, origin); + } + + @Test + public void testRomoveZero(){ + int [] origin={1,2,3,0,4,0,6}; + assertArrayEquals(new int[]{1,2,3,4,6}, util.removeZero(origin)); + } + + @Test + public void testMerge(){ + int [] a1={3,0,4,6}; + int [] a2={3,6,8,10}; + assertArrayEquals(new int[]{0,3,4,6,8,10}, util.merge(a1,a2)); + } + + @Test + public void testGrow(){ + int [] a1={3,0,4,6}; + assertArrayEquals(new int[]{3,0,4,6,0,0}, util.grow(a1,2)); + } + + @Test + public void testFibo(){ + assertArrayEquals(new int[]{1,1,2,3}, util.fibonacci(4)); + } + + @Test + public void testGetPrime(){ + assertArrayEquals(new int[]{2,3,5,7,11}, util.getPrimes(13)); + } + + @Test + public void testGetPerfectNum(){ + + assertArrayEquals(new int[]{6,28,496}, util.getPerfectNumbers(1000)); + } + + @Test + public void testJoin(){ + + assertEquals("3-5-8", util.join(new int[]{3,5,8}, "-")); + } + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java new file mode 100644 index 0000000000..71f5c939f2 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.practice.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/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java new file mode 100644 index 0000000000..069faad8bb --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java @@ -0,0 +1,140 @@ +package com.coderising.practice.litestruts; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.SAXException; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + + + View view=new View(); + Map viewMap=new HashMap(); + Map xmlMap=parseXML(actionName); + + try { + + Class clazz= Class.forName(xmlMap.get("className")); + + Object object=clazz.newInstance(); + if (object instanceof LoginAction) { + LoginAction action =(LoginAction) object; + action.setName(parameters.get("name")); + action.setPassword(parameters.get("password")); + } + Method execute = clazz.getMethod("execute"); + String executResult=(String) execute.invoke(object); + Field[] fields=clazz.getDeclaredFields(); + Method[] methods=clazz.getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().contains("get")) { + String resultString=(String) method.invoke(object); + for (Field field : fields) { + field.setAccessible(true); + if (field.get(object).equals(resultString)) { + viewMap.put(field.getName(), resultString); + } + } + } + } + + view.setJsp(xmlMap.get(executResult)); + + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + view.setParameters(viewMap); + + + return view; + } + + public static Map parseXML(String actionName){ + SAXParserFactory factory=SAXParserFactory.newInstance(); + StrutsHandler hander=new StrutsHandler(actionName); + SAXParser parser; + try { + parser = factory.newSAXParser(); + parser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/coderising/practice/litestruts/struts.xml"),hander); + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return hander.getResult(); + } + + public static void setPara(Map map){ + + + } + + public static String executAction(String className){ + + return null; + } + + + + + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java new file mode 100644 index 0000000000..2b3d3ab88b --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java @@ -0,0 +1,57 @@ +package com.coderising.practice.litestruts; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +public class StrutsHandler extends DefaultHandler { + + private String actionName; + private String key; + private String value; + + private String currentActionName; + + private Map result = new HashMap(); + + public StrutsHandler(String actionName) { + this.actionName = actionName; + } + + public Map getResult() { + return result; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + if ("action".equals(qName)) { + currentActionName = attributes.getValue(attributes.getIndex("name")); + if (currentActionName.equals(actionName)) { + value = attributes.getValue(attributes.getIndex("class")); + result.put("className", value); + } + + } + if ("result".equals(qName) && actionName.equals(currentActionName)) { + key = attributes.getValue("name"); + } + + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if ("result".equals(qName) && actionName.equals(currentActionName)) { + result.put(key, value); + } + } + + public void characters(char[] ch, int start, int length) throws SAXException { + if (actionName.equals(currentActionName)) { + value = new String(ch, start, length); + } + }; +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java new file mode 100644 index 0000000000..cc57e4c6a8 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java @@ -0,0 +1,53 @@ +package com.coderising.practice.litestruts; + +import java.io.File; +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")); + } + + @Test + public void testParse() { + String actionName = "login"; + + System.out.println(Struts.parseXML(actionName)); + } + + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java new file mode 100644 index 0000000000..66643290d4 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.practice.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/group20/755659358/week02/src/com/coderising/practice/litestruts/struts.xml b/group20/755659358/week02/src/com/coderising/practice/litestruts/struts.xml new file mode 100644 index 0000000000..7b2a62ef58 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/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/group20/872045674/20170226/src/com/coding/basic/ArrayList.java b/group20/872045674/20170226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8b495831fe --- /dev/null +++ b/group20/872045674/20170226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,87 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[7]; + + public void add(Object o){ + if(size>elementData.length-1){ + ensureCapacity(size); + } + elementData[size++] = o; + } + + public void add(int index, Object o){ + System.out.println(elementData.length+" length"); + System.out.println(size+" size"); + size++; + if(index<0||index>size||index>Integer.MAX_VALUE){ + System.out.println("add 位置输入错误,请输入合理的位置"); + return; + } + if(size>elementData.length-1){ + ensureCapacity(size); + } + System.arraycopy(elementData,index,elementData,index+1,size-index-1); + elementData[index] = o; + } + + public Object get(int index){ + if(index<0||index>size-1){ + System.out.println("get 位置输入错误,请输入合理的位置"); + return null; + } + + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>size-1){ + System.out.println("remove 位置输入错误,请输入合理的位置"); + return false; + } + System.arraycopy(elementData,index+1,elementData,index,size-index-1); + elementData[--size]=null; + return true; + } + + public int size(){ + return size; + } + + private void ensureCapacity(int nimCapacity){ + int oldCapacity = elementData.length; + int newCapacity = oldCapacity+(oldCapacity/2+1); + if(newCapacity < nimCapacity){ + newCapacity = nimCapacity; + } + if(newCapacity>Integer.MAX_VALUE){ + newCapacity = Integer.MAX_VALUE; + } + elementData = Arrays.copyOf(elementData,newCapacity); + } + + public static void main(String[] args) { + ArrayList list=new ArrayList(); + + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(2,10); + list.remove(3); + for(int i=0;i> { private T data; diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" new file mode 100644 index 0000000000..42bda4b210 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" @@ -0,0 +1,213 @@ +package org.Ralf.ArrayUtil; + +import java.util.ArrayList; +import java.util.Arrays; + +import javax.naming.spi.DirStateFactory.Result; + +public class ArrayUtil { + + public static void reverseArray(int[] origin) { + /** + * һa , Ըֵû 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] + * a = [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * + * @param origin + * @return + * + */ + + for (int i = 0; i < origin.length / 2; i++) { + int temp = origin[i]; + origin[i] = origin[origin.length - i - 1]; + origin[origin.length - i - 1] = temp; + } + + } + + /** + * µһ飺 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 int[] + */ + public static int[] removeZero(int[] oldArr) { + + if (oldArr == null) { + return null; + } + int[] newArr = new int[oldArr.length]; + int size = 0; + + for (int i = 0; i < oldArr.length; i++) { + if (oldArr[i] != 0) { + newArr[size] = oldArr[i]; + size++; + } + } + return Arrays.copyOf(newArr, size); + + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ 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) { + + // method + ArrayList arrayList = new ArrayList<>(); + for (int i = 0; i < array1.length; i++) { + if (!arrayList.contains(array1[i])) { + arrayList.add(array1[i]); + } + } + for (int i = 0; i < array2.length; i++) { + if (!arrayList.contains(array2[i])) {// listindexҵжǷԪ + arrayList.add(array2[i]); + } + } + int[] newArr = new int[arrayList.size()]; + // arrayList.toArray(newArr); + for (int i = 0; i < arrayList.size(); i++) { + newArr[i] = arrayList.get(i); + } + Arrays.sort(newArr);// ð򣬲򣬿򷨵ʵ + return newArr; + } + + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ 磬 max = 15 , + * 򷵻صӦΪ [11235813] max = 1, 򷵻ؿ [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + int[] newArray = {}; + if (max == 1) { + return newArray; + } + newArray = new int[2 * max]; + int size = 0; + int a = 1; + int b = 1; + newArray[size++] = a; + newArray[size++] = b; + while (b <= max) { + int temp = b; + b = a + b; + newArray[size++] = b; + a = temp; + } + + return Arrays.copyOf(newArray, size - 1); + } + + /** + * Сڸֵmax max = 23, صΪ[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + int[] aar = new int[max]; + int size = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + aar[size++] = i; + } + } + return Arrays.copyOf(aar, size); + } + + private static boolean isPrime(int aar) { + boolean flag = true; + for (int i = 2; i <= Math.sqrt(aar); i++) { + if (aar % i == 0) { + flag = false; + break; + } + } + return flag; + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 һֵmax һ飬 Сmax + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max < 1) { + return null; + } + int[] arr = new int[max]; + int size = 0; + for (int i = 1; i <= max; i++) { + if (isPerfectNumber(i)) { + arr[size++] = i; + } + } + return Arrays.copyOf(arr, size); + } + + private static boolean isPerfectNumber(int num) { + int sum = 0; + for (int i = 1; i < num; i++) { + if (num % i == 0) { + sum += i; + } + + } + if (sum == num) { + return true; + } else + return false; + } + + /** + * seperator array array= [3,8,9], seperator = "-" 򷵻ֵΪ"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + if (array.length < 1) { + return null; + } + StringBuilder string = new StringBuilder(); + for (int i = 0; i < array.length - 1; i++) { + string.append(array[i]).append(seperator); + } + string.append(array[array.length - 1]); + return string.toString(); + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" new file mode 100644 index 0000000000..48f12a3337 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" @@ -0,0 +1,100 @@ +package org.Ralf.ArrayUtilTest; + +import static org.junit.Assert.*; + +import org.Ralf.ArrayUtil.ArrayUtil; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ArrayUtilTest { + + + @Before + public void setUp() throws Exception { + } + + @Test + public void reverseArray() { + int[] origin = {9,8,7,6,5,4,3,2,1}; + int[] originCopy = origin; + int[] reverse = {1,2,3,4,5,6,7,8,9}; + ArrayUtil.reverseArray(origin); + Assert.assertArrayEquals(origin, reverse); + ArrayUtil.reverseArray(origin); + Assert.assertArrayEquals(origin, originCopy); + } + + @Test + public void removeZero(){ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newarr = ArrayUtil.removeZero(oldArr); + int[] realArr = {1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(newarr, realArr); + } + + @Test + public void merge(){ + + int[] a1 ={3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] newarr = ArrayUtil.merge(a1, a2); + int[] realArr = {3,4,5,6,7,8}; + Assert.assertArrayEquals(newarr, realArr); + } + + @Test + public void grow(){ + int[] oldArray = {2,3,6}; + int[] realArr = {2,3,6,0,0,0}; + int[] newArray = ArrayUtil.grow(oldArray, 3); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void fibonacci(){ + + int[] realArr = {1,1,2,3,5,8,13}; + int[] newArray = ArrayUtil.fibonacci(15); + Assert.assertArrayEquals(newArray, realArr); + } + @Test + public void getPrimes(){ + int[] realArr = {2,3,5,7,11,13,17,19}; + int[] newArray = ArrayUtil.getPrimes(23); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void getPerfectNumbers(){ + int[] realArr = {6, 28, 496, 8128}; + int[] newArray = ArrayUtil.getPerfectNumbers(10000); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void join(){ + int[] realArr = {6, 28, 496, 8128}; + int[] newArray = ArrayUtil.getPerfectNumbers(10000); + Assert.assertArrayEquals(newArray, realArr); + } + + + + + + + + + + + + + + + + + + +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" new file mode 100644 index 0000000000..40b5de161a --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +public class LoginAction { + + private String name; + private String passWord; + private String message; + 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; + } + 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"; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" new file mode 100644 index 0000000000..e971e779b6 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" @@ -0,0 +1,71 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class ReadXml { + + private Document document = null; + private HashMap hashMap; + + public ReadXml(String filename) { + try { + document = new SAXReader().read((filename)); + hashMap = new HashMap(); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public String parseXml(String actionName) { + + // List actions = document.selectNodes("//struts/action"); + String className = null; + Element root = document.getRootElement(); + List actions = root.elements("action"); + if (actions.isEmpty()) { + return null; + } + for (Iterator iter = actions.iterator(); iter.hasNext();) { + Element element = (Element) iter.next(); + Attribute attr1 = element.attribute("name"); + if (attr1.getValue().equals(actionName)) { + Attribute attr2 = element.attribute("class"); + className = attr2.getValue(); + //ȡԪصĵֵ + for (Iterator iterator = element.elementIterator(); iterator + .hasNext();) { + Element childElement = (Element) iterator.next(); + Attribute childAttribute = childElement.attribute("name"); + hashMap.put(childAttribute.getValue(), + childElement.getText()); + } + } + + } + return className; + } + + public String getJsp(String result) { + if (result == null) { + return null; + } + String string_jsp = null; + if (!hashMap.isEmpty()) { + for (String string : hashMap.keySet()) { + if (result.equals(string)) { + string_jsp = hashMap.get(string); + } + } + } + return string_jsp; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" new file mode 100644 index 0000000000..2892617845 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" @@ -0,0 +1,93 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Struts { + + private static final String NAME = "name"; + private static final String PASSWORD = "password"; + private static String excuteString; + private static Object object = null;// طʵ + private static Class actionClass = null;// ȡ + + @SuppressWarnings("unchecked") + public static View runAction(String actionName, + Map parameters) { + // ȡļstruts.xml + View view = new View(); + ReadXml readXml = new ReadXml("E:\\struts.xml"); + String classNameString = readXml.parseXml(actionName);//ȡxml + object = initAction(classNameString);//ͨʼ + + excuteMethod(parameters);//ִsetterexcute + + view.setParameterMap(setMapParameter());//ȡеgetterִк󽫷ͽ浽view + String jspResult = readXml.getJsp(excuteString);//ȡjsp + view.setJsp(jspResult); + + return view; + } + + public static Object initAction(String classNameString) { + System.out.println(classNameString); + try { + actionClass = Class.forName(classNameString); + } catch (ClassNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + Object newObject = null; + try { + newObject = actionClass.newInstance(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return newObject; + } + + public static void excuteMethod(Map parameters) { + + try { + Method methodOfName = actionClass + .getMethod("setName", String.class); + methodOfName.invoke(object, parameters.get(NAME)); + // + Method methodOfPassword = actionClass.getMethod("setPassWord", + String.class); + methodOfPassword.invoke(object, parameters.get(PASSWORD)); + + Method excuteMethod = actionClass.getMethod("execute"); + excuteString = (String) excuteMethod.invoke(object); + + } catch (Exception e) { + // TODO: handle exception + } + } + + public static Map setMapParameter() { + + Method[] getterMethods = actionClass.getMethods(); + HashMap hashMap = new HashMap<>(); + + for (int i = 0; i < getterMethods.length; i++) { + String getterName = getterMethods[i].getName(); + if (getterName.startsWith("get")) { + try { + String value = (String) getterMethods[i].invoke(object); + hashMap.put(getterName.substring(3).toLowerCase(), value); + //System.out.println("----" + getterName.substring(2)); + } catch (Exception e) { + // TODO: handle exception + } + + } + } + return hashMap; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" new file mode 100644 index 0000000000..b7f0884f41 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //ԤIJһ + + 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/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" new file mode 100644 index 0000000000..bda8419e5f --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" @@ -0,0 +1,28 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameter; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameter; + } + + public View setParameterMap(Map parameter) { + this.parameter = parameter; + return this; + } + +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" new file mode 100644 index 0000000000..d4f3154c38 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" @@ -0,0 +1 @@ +http://blog.csdn.net/u011371324/article/details/60329949 \ No newline at end of file diff --git a/group20/group20.md b/group20/group20.md index d3f5a12faa..8b13789179 100644 --- a/group20/group20.md +++ b/group20/group20.md @@ -1 +1 @@ - + diff --git a/group21/group21.md b/group21/group21.md index d3f5a12faa..8b13789179 100644 --- a/group21/group21.md +++ b/group21/group21.md @@ -1 +1 @@ - + diff --git a/group22/group22.md b/group22/group22.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group22/group22.md @@ -0,0 +1 @@ + diff --git a/group23/group23.md b/group23/group23.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group23/group23.md @@ -0,0 +1 @@ + diff --git a/group24/group24.md b/group24/group24.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group24/group24.md @@ -0,0 +1 @@ + diff --git a/group25/group25.md b/group25/group25.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group25/group25.md @@ -0,0 +1 @@ + diff --git a/group26/group26.md b/group26/group26.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/group26.md @@ -0,0 +1 @@ + diff --git a/group27/group27.md b/group27/group27.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group27/group27.md @@ -0,0 +1 @@ + diff --git a/liuxin/.classpath b/liuxin/.classpath index 373dce4005..2d7497573f 100644 --- a/liuxin/.classpath +++ b/liuxin/.classpath @@ -1,7 +1,7 @@ - - - - - - - + + + + + + + diff --git a/liuxin/.gitignore b/liuxin/.gitignore index 3e2fcc7171..ae3c172604 100644 --- a/liuxin/.gitignore +++ b/liuxin/.gitignore @@ -1 +1 @@ -/bin/ +/bin/ diff --git a/liuxin/.project b/liuxin/.project index 2858b5b710..fab8d7f04c 100644 --- a/liuxin/.project +++ b/liuxin/.project @@ -1,17 +1,17 @@ - - - 2017Learning - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/liuxin/src/com/coderising/array/ArrayUtil.java b/liuxin/src/com/coderising/array/ArrayUtil.java index 78845d06d0..e5ddb476a6 100644 --- a/liuxin/src/com/coderising/array/ArrayUtil.java +++ b/liuxin/src/com/coderising/array/ArrayUtil.java @@ -1,96 +1,96 @@ -package com.coderising.array; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - return null; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - return null; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - return null; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - return null; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - return null; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - return null; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - return null; - } - - -} +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/liuxin/src/com/coderising/download/DownloadThread.java b/liuxin/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..1456314140 --- /dev/null +++ b/liuxin/src/com/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/liuxin/src/com/coderising/download/FileDownloader.java b/liuxin/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..f5d7999eb4 --- /dev/null +++ b/liuxin/src/com/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/liuxin/src/com/coderising/download/FileDownloaderTest.java b/liuxin/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..8171ee5763 --- /dev/null +++ b/liuxin/src/com/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/liuxin/src/com/coderising/download/api/Connection.java b/liuxin/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..9710e270e1 --- /dev/null +++ b/liuxin/src/com/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/liuxin/src/com/coderising/download/api/ConnectionException.java b/liuxin/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..8dbfe95dda --- /dev/null +++ b/liuxin/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/liuxin/src/com/coderising/download/api/ConnectionManager.java b/liuxin/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..fb44ede457 --- /dev/null +++ b/liuxin/src/com/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/liuxin/src/com/coderising/download/api/DownloadListener.java b/liuxin/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..4cd0b3eab1 --- /dev/null +++ b/liuxin/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/liuxin/src/com/coderising/download/impl/ConnectionImpl.java b/liuxin/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..32f03efdc7 --- /dev/null +++ b/liuxin/src/com/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/liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java b/liuxin/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..046f7c49a4 --- /dev/null +++ b/liuxin/src/com/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/liuxin/src/com/coderising/litestruts/LoginAction.java b/liuxin/src/com/coderising/litestruts/LoginAction.java index 1005f35a29..dcdbe226ed 100644 --- a/liuxin/src/com/coderising/litestruts/LoginAction.java +++ b/liuxin/src/com/coderising/litestruts/LoginAction.java @@ -1,39 +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; - } -} +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/liuxin/src/com/coderising/litestruts/Struts.java b/liuxin/src/com/coderising/litestruts/Struts.java index 44cc35bf01..85e2e22de3 100644 --- a/liuxin/src/com/coderising/litestruts/Struts.java +++ b/liuxin/src/com/coderising/litestruts/Struts.java @@ -1,34 +1,34 @@ -package com.coderising.litestruts; - -import java.util.Map; - - - -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - return null; - } - -} +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/liuxin/src/com/coderising/litestruts/StrutsTest.java b/liuxin/src/com/coderising/litestruts/StrutsTest.java index a44c1878ac..b8c81faf3c 100644 --- a/liuxin/src/com/coderising/litestruts/StrutsTest.java +++ b/liuxin/src/com/coderising/litestruts/StrutsTest.java @@ -1,43 +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")); - } -} +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/liuxin/src/com/coderising/litestruts/View.java b/liuxin/src/com/coderising/litestruts/View.java index 0194c681f6..07df2a5dab 100644 --- a/liuxin/src/com/coderising/litestruts/View.java +++ b/liuxin/src/com/coderising/litestruts/View.java @@ -1,23 +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; - } -} +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/liuxin/src/com/coderising/litestruts/struts.xml b/liuxin/src/com/coderising/litestruts/struts.xml index 99063bcb0c..0582b7d4ea 100644 --- a/liuxin/src/com/coderising/litestruts/struts.xml +++ b/liuxin/src/com/coderising/litestruts/struts.xml @@ -1,11 +1,11 @@ - + /jsp/homepage.jsp /jsp/showLogin.jsp - - /jsp/welcome.jsp - /jsp/error.jsp + + /jsp/welcome.jsp + /jsp/error.jsp - \ No newline at end of file + diff --git a/liuxin/src/com/coding/basic/ArrayList.java b/liuxin/src/com/coding/basic/ArrayList.java index 57412dcf7f..1f185736f9 100644 --- a/liuxin/src/com/coding/basic/ArrayList.java +++ b/liuxin/src/com/coding/basic/ArrayList.java @@ -1,32 +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; - } - -} +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/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/src/com/coding/basic/BinaryTreeNode.java index 266eff3d56..d7ac820192 100644 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ b/liuxin/src/com/coding/basic/BinaryTreeNode.java @@ -1,32 +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; - } - -} +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/liuxin/src/com/coding/basic/Iterator.java b/liuxin/src/com/coding/basic/Iterator.java index dbe8b9afb2..06ef6311b2 100644 --- a/liuxin/src/com/coding/basic/Iterator.java +++ b/liuxin/src/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/liuxin/src/com/coding/basic/LinkedList.java b/liuxin/src/com/coding/basic/LinkedList.java index 1fd99bf26b..d6f6ffebec 100644 --- a/liuxin/src/com/coding/basic/LinkedList.java +++ b/liuxin/src/com/coding/basic/LinkedList.java @@ -1,5 +1,7 @@ package com.coding.basic; + + public class LinkedList implements List { private Node head; @@ -43,4 +45,80 @@ private static class Node{ Node next; } + + /** + * 把该链表逆置 + * 例如链表为 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/liuxin/src/com/coding/basic/List.java b/liuxin/src/com/coding/basic/List.java index 396b1f6416..10d13b5832 100644 --- a/liuxin/src/com/coding/basic/List.java +++ b/liuxin/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/liuxin/src/com/coding/basic/Queue.java b/liuxin/src/com/coding/basic/Queue.java index 08d2d86b14..36e516e266 100644 --- a/liuxin/src/com/coding/basic/Queue.java +++ b/liuxin/src/com/coding/basic/Queue.java @@ -1,19 +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; - } -} +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/liuxin/src/com/coding/basic/Stack.java b/liuxin/src/com/coding/basic/Stack.java index 4bfe28057f..a5a04de76d 100644 --- a/liuxin/src/com/coding/basic/Stack.java +++ b/liuxin/src/com/coding/basic/Stack.java @@ -1,22 +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; - } -} +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; + } +}