diff --git a/group24/330657387/.project b/group24/330657387/.project index fab8d7f04c..e929d098c9 100644 --- a/group24/330657387/.project +++ b/group24/330657387/.project @@ -1,6 +1,6 @@ - 2017Learning + 2017Learning_330657387 diff --git a/group24/330657387/src/com/coding/basic/ArrayList.java b/group24/330657387/src/com/coding/basic/ArrayList.java deleted file mode 100644 index 1728a28291..0000000000 --- a/group24/330657387/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.coding.basic; - -import java.util.Arrays; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[10]; - - public void ensureCapacity(int input) { - if (input > elementData.length) { - growCapacity(); - } - } - - private void growCapacity(){ - elementData = Arrays.copyOf(elementData, size * 2); - } - - 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 ++; - } - - private void rangeCheck(int index){ - if (index > size || index < 0){ - throw new IndexOutOfBoundsException(); - } - } - - public Object get(int index){ - rangeCheck(index); - return elementData[index]; - } - - 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 null; - } - -} diff --git a/group24/330657387/src/com/coding/basic/BinaryTreeNode.java b/group24/330657387/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d7ac820192..0000000000 --- a/group24/330657387/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -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/group24/330657387/src/com/coding/basic/Queue.java b/group24/330657387/src/com/coding/basic/Queue.java deleted file mode 100644 index 36e516e266..0000000000 --- a/group24/330657387/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,19 +0,0 @@ -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/group24/330657387/src/com/coding/basic/Stack.java b/group24/330657387/src/com/coding/basic/Stack.java deleted file mode 100644 index a5a04de76d..0000000000 --- a/group24/330657387/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,22 +0,0 @@ -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/group24/330657387/src/main/week01/data_structure/ArrayList.java b/group24/330657387/src/main/week01/data_structure/ArrayList.java new file mode 100644 index 0000000000..9a2f4d2c8b --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/ArrayList.java @@ -0,0 +1,88 @@ +package main.week01.data_structure; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + private void ensureCapacity(int input) { + if (input > elementData.length) { + growCapacity(); + } + } + + private void growCapacity() { + elementData = Arrays.copyOf(elementData, size * 2); + } + + private void rangeCheck(int index) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + } + } + + 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 dest = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size + - index - 1); + elementData[size---1]=null;//��ֹ�ڴ�й© + return dest; + } + + public int size() { + return size; + } + + public class ArrayListIterator implements Iterator { + + private ArrayList list; + + private int position = 0; + + private ArrayListIterator() { + } + + private ArrayListIterator(ArrayList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return position + 1 <= list.size; + } + + @Override + public Object next() { + return list.get(position++); + } + + } + + public ArrayListIterator iterator() { + return new ArrayListIterator(this); + } + +} diff --git a/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java new file mode 100644 index 0000000000..296744c188 --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java @@ -0,0 +1,121 @@ +package main.week01.data_structure; + +import com.sun.org.apache.regexp.internal.recompile; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + + public BinaryTreeNode(){} + + public BinaryTreeNode(T data) + { + this.data=data; + this.left=null; + this.right=null; + } + + 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; + } + + /** + * data�������ظ� + * @param data + * @return + */ + public BinaryTreeNode insert(T data){ + int compareResult = this.data.compareTo(data); + if(compareResult == 0){ + return this; + } + if(compareResult > 0){ + if(this.left == null){ + this.left = new BinaryTreeNode(data); + return this.left; + }else{ + return this.left.insert(data); + } + }else{ + if(this.right == null){ + this.right = new BinaryTreeNode(data); + return this.right; + }else{ + return this.right.insert(data); + } + } + } + + /** + * �����ڵ�Ϊ�� + * @param data + * @return + */ + @SuppressWarnings("unchecked") + public BinaryTreeNode search(T data){ + if(this.data == null){ + return null; + } + int compareResult = this.data.compareTo(data); + if (compareResult > 0) { + if (this.left == null) { + return null; + } else { + return this.left.search(data); + } + } else if (compareResult < 0) { + if (this.right == null) { + return null; + } else { + return this.right.search(data); + } + } else { + return this; + } + + } + + private BinaryTreeNode findMin() { + if (this.data == null) { + return null; + } + if (this.left == null) { + return this; + } + return this.left.findMin(); + } + + private BinaryTreeNode findMax() { + if (this.data == null) { + return null; + } + if (this.right == null) { + return this; + } + return this.right.findMin(); + } + +} diff --git a/group24/330657387/src/com/coding/basic/Iterator.java b/group24/330657387/src/main/week01/data_structure/Iterator.java similarity index 69% rename from group24/330657387/src/com/coding/basic/Iterator.java rename to group24/330657387/src/main/week01/data_structure/Iterator.java index 06ef6311b2..194891e56a 100644 --- a/group24/330657387/src/com/coding/basic/Iterator.java +++ b/group24/330657387/src/main/week01/data_structure/Iterator.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package main.week01.data_structure; public interface Iterator { public boolean hasNext(); diff --git a/group24/330657387/src/com/coding/basic/LinkedList.java b/group24/330657387/src/main/week01/data_structure/LinkedList.java similarity index 51% rename from group24/330657387/src/com/coding/basic/LinkedList.java rename to group24/330657387/src/main/week01/data_structure/LinkedList.java index 8357e13254..b61df9f254 100644 --- a/group24/330657387/src/com/coding/basic/LinkedList.java +++ b/group24/330657387/src/main/week01/data_structure/LinkedList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package main.week01.data_structure; import java.util.NoSuchElementException; @@ -9,13 +9,10 @@ public class LinkedList implements List { public void add(Object o) { if (isEmpty()) { - head = new Node(o); + addFirst(o); } else { - Node tail = (Node)get(size-1); - Node node = new Node(o); - tail.next = node; + addLast(o); } - size++; } public boolean isEmpty() { @@ -24,40 +21,55 @@ public boolean isEmpty() { public void add(int index, Object o) { rangeCheck(index); - if (index ==0) { - Node node = new Node(o); - node.next = head; - head = node; + if (index == 0) { + addFirst(o); + } else if (index == size) { + addLast(o); } else { - Node pre = (Node)get(index-1); + Node pre = getNode(index - 1); Node node = new Node(o); node.next = pre.next; pre.next = node; + size++; } } - - private void rangeCheck(int index){ - if (index >= size || index <0){ - throw new IndexOutOfBoundsException(); + + private void rangeCheck(int index) { + if (index >= size || index < 0) { + throw new IndexOutOfBoundsException(); } } - + public Object get(int index) { rangeCheck(index); Node dest = head; - for (int i = 0; i< index; i++){ + for (int i = 0; i < index; i++) { dest = dest.next; } return dest.data; } + public Node getNode(int index) { + rangeCheck(index); + Node dest = head; + for (int i = 0; i < index; i++) { + dest = dest.next; + } + return dest; + } + public Object remove(int index) { rangeCheck(index); - Node pre = (Node)get(index); + if (index == 0) { + return removeFirst(); + }else if(index == size){ + return removeLast(); + } + Node pre = getNode(index - 1); Node dest = pre.next; pre.next = dest.next; - size --; - return dest; + size--; + return dest.data; } public int size() { @@ -68,11 +80,11 @@ public void addFirst(Object o) { Node node = new Node(o); node.next = head; head = node; - size ++; + size++; } public void addLast(Object o) { - Node last = (Node)get(size-1); + Node last = getNode(size - 1); Node node = new Node(o); last.next = node; size++; @@ -83,32 +95,20 @@ public Object removeFirst() { throw new NoSuchElementException(); } Node newhead = head; + Node dest = head; head = head.next; - size --; - return newhead; + size--; + return dest.data; } public Object removeLast() { - if (head == null) { - throw new NoSuchElementException(); - } - if (head.next == null) { - Node tmp = head; - head = null; - size --; - return tmp; - } - Node newLastNode = (Node)get(size-2); + Node newLastNode = getNode(size - 2); Node oldLastNode = newLastNode.next; newLastNode.next = null; - size --; + size--; return oldLastNode; } - public Iterator iterator() { - return null; - } - private static class Node { Object data; Node next; @@ -119,4 +119,32 @@ private static class Node { } } + public class LinkedListIterator implements Iterator { + + private LinkedList list; + + private int position = 0; + + private LinkedListIterator() { + } + + private LinkedListIterator(LinkedList list) { + this.list = list; + } + + @Override + public boolean hasNext() { + return position + 1 <= list.size; + } + + @Override + public Object next() { + return list.get(position++); + } + + } + + public LinkedListIterator iterator() { + return new LinkedListIterator(this); + } } diff --git a/group24/330657387/src/com/coding/basic/List.java b/group24/330657387/src/main/week01/data_structure/List.java similarity index 82% rename from group24/330657387/src/com/coding/basic/List.java rename to group24/330657387/src/main/week01/data_structure/List.java index 10d13b5832..6cbc15f8ab 100644 --- a/group24/330657387/src/com/coding/basic/List.java +++ b/group24/330657387/src/main/week01/data_structure/List.java @@ -1,6 +1,7 @@ -package com.coding.basic; +package main.week01.data_structure; public interface List { + public void add(Object o); public void add(int index, Object o); public Object get(int index); diff --git a/group24/330657387/src/main/week01/data_structure/Queue.java b/group24/330657387/src/main/week01/data_structure/Queue.java new file mode 100644 index 0000000000..7da6edf433 --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/Queue.java @@ -0,0 +1,23 @@ +package main.week01.data_structure; + +public class Queue { + + private LinkedList elementData; + private int size = 0; + + public void enQueue(Object o){ + elementData.add(size++,o); + } + + public Object deQueue(){ + return elementData.remove(size---1); + } + + public boolean isEmpty(){ + return size == 0; + } + + public int size(){ + return size; + } +} diff --git a/group24/330657387/src/main/week01/data_structure/Stack.java b/group24/330657387/src/main/week01/data_structure/Stack.java new file mode 100644 index 0000000000..5fc5410fde --- /dev/null +++ b/group24/330657387/src/main/week01/data_structure/Stack.java @@ -0,0 +1,28 @@ +package main.week01.data_structure; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + public void push(Object o) { + elementData.add(size++,o); + } + + public Object pop() { + Object temp = elementData.get(size-1); + elementData.remove(size---1); + return temp; + } + + public Object peek() { + return elementData.get(size-1); + } + + public boolean isEmpty() { + return size == 0; + } + + public int size() { + return size; + } +} diff --git a/group24/330657387/src/main/week02/practice/ArrayUtil.java b/group24/330657387/src/main/week02/practice/ArrayUtil.java new file mode 100644 index 0000000000..3ab82a31e8 --- /dev/null +++ b/group24/330657387/src/main/week02/practice/ArrayUtil.java @@ -0,0 +1,115 @@ +package main.week02.practice; + +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 <= 1) { + return; + } + int i = 0, j = origin.length - 1; + int temp; + while (j > i) { + origin[i] = origin[i] ^ origin[j]; + origin[j] = origin[i] ^ origin[j]; + origin[i] = origin[i] ^ origin[j]; + 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 i = 0; + for (int j = 0; j < oldArray.length; j++) { + if (0 != oldArray[j]) { + oldArray[i++] = oldArray[j]; + } + } + int[] newArray = new int[i]; + System.arraycopy(oldArray, 0, newArray, 0,newArray.length); + 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) { + 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/group24/330657387/src/test/week01/data_structure/ArrayListTest.java b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java new file mode 100644 index 0000000000..d9d63339b2 --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/ArrayListTest.java @@ -0,0 +1,84 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; +import main.week01.data_structure.ArrayList; +import main.week01.data_structure.ArrayList.ArrayListIterator; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + public static ArrayList list; + + @Before + public void setUp() throws Exception { + list = new ArrayList(); + } + + @Test + public void testAddObject() { + list.add(1); + list.add(2); + list.add(2); + assertEquals(3,list.size()); + } + + @Test + public void testAddIntObject() { + list.add(0,1); + list.add(1,2); + list.add(2,2); + list.add(0,2); + assertEquals(2,list.get(0)); + try{ + list.add(-1 , "test"); + fail("-1 can't be index"); + list.add(1000, "test"); + fail("out of range"); + }catch (Exception e){ + + } + } + + @Test + public void testGet() { + list.add("songhao"); + assertEquals("songhao", list.get(0)); + } + + @Test + public void testRemove() { + list.add("songhao"); + assertEquals("songhao", list.remove(0)); + } + + @Test + public void testSize(){ + list.add(0,1); + list.add(1,2); + list.add(2,2); + list.add(0,2); + assertEquals(4,list.size()); + } + + @Test + public void testIterator() { + list.add(0,1); + list.add(1,2); + list.add(2,3); + list.add(0,4); + ArrayListIterator iter = list.iterator(); + assertTrue(iter.hasNext()); + assertEquals(4, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(1, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(2, iter.next()); + assertTrue(iter.hasNext()); + assertEquals(3, iter.next()); + assertFalse(iter.hasNext()); + + } + +} diff --git a/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java b/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..515da2e7d4 --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/BinaryTreeNodeTest.java @@ -0,0 +1,28 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; +import main.week01.data_structure.BinaryTreeNode; + +import org.junit.Before; +import org.junit.Test; + +public class BinaryTreeNodeTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testInsert() { + BinaryTreeNode head=new BinaryTreeNode(); + + head.setData(5); + head.insert(2); + head.insert(7); + head.insert(1); + head.insert(4); + head.insert(3); + assertEquals(3,head.getLeft().getRight().getLeft().getData()); + } + +} diff --git a/group24/330657387/src/test/week01/data_structure/LinkedListTest.java b/group24/330657387/src/test/week01/data_structure/LinkedListTest.java new file mode 100644 index 0000000000..cf374a6dcb --- /dev/null +++ b/group24/330657387/src/test/week01/data_structure/LinkedListTest.java @@ -0,0 +1,54 @@ +package test.week01.data_structure; + +import static org.junit.Assert.*; +import main.week01.data_structure.LinkedList; +import main.week01.data_structure.LinkedList.LinkedListIterator; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + + private LinkedList list; + + @Before + public void setUp() throws Exception { + list = new LinkedList(); + } + + @Test + public void testGet() { + list.add("A"); + list.add("B"); + list.add(0, "C"); + assertEquals("C", list.get(0)); + } + + @Test + public void testRemove() { + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.add(0, "E"); + assertEquals("E", list.remove(0)); + assertEquals("D", list.remove(list.size()-1)); + assertEquals(3, list.size()); + } + + @Test + public void testIterator() { + LinkedListIterator iter = list.iterator(); + list.add("A"); + list.add("B"); + list.add(0, "C"); + assertTrue(iter.hasNext()); + assertEquals("C", iter.next()); + assertTrue(iter.hasNext()); + assertEquals("A", iter.next()); + assertTrue(iter.hasNext()); + assertEquals("B", iter.next()); + assertFalse(iter.hasNext()); + } + +} diff --git a/group24/330657387/src/test/week02/practice/ArrayUtilTest.java b/group24/330657387/src/test/week02/practice/ArrayUtilTest.java new file mode 100644 index 0000000000..1bf97fced3 --- /dev/null +++ b/group24/330657387/src/test/week02/practice/ArrayUtilTest.java @@ -0,0 +1,79 @@ +package test.week02.practice; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import main.week02.practice.ArrayUtil; + +import org.junit.Before; +import org.junit.Test; + + +public class ArrayUtilTest { + + ArrayUtil util; + + @Before + public void setUp() throws Exception { + util = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[][] origin = {{1,20,5,3,65,4,6,9,7}, + {1}, + {1,2,3}, + {}, + {23,32}}; + for(int[] a : origin){ + System.out.println("前:"+Arrays.toString(a)); + util.reverseArray(a); + System.out.println("后:"+Arrays.toString(a)); + } + } + + @Test + public void testRemoveZero() { + int[][] origin = {{1,20,0,0,5,3,65,4,0,6,9,7}, + {1,0}, + {1,0,2,3,0}, + {}, + {23,0,0,32}}; + for(int[] a : origin){ + System.out.println("前:"+Arrays.toString(a)); + System.out.println("后:"+Arrays.toString(util.removeZero(a))); + } + } + + @Test + public void testMerge() { + fail("Not yet implemented"); + } + + @Test + public void testGrow() { + fail("Not yet implemented"); + } + + @Test + public void testFibonacci() { + fail("Not yet implemented"); + } + + @Test + public void testGetPrimes() { + fail("Not yet implemented"); + } + + @Test + public void testGetPerfectNumbers() { + fail("Not yet implemented"); + } + + @Test + public void testJoin() { + fail("Not yet implemented"); + } + +} diff --git a/liuxin/.settings/org.eclipse.core.resources.prefs b/liuxin/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/liuxin/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8