diff --git a/group01/245205213/.classpath b/group01/245205213/.classpath new file mode 100644 index 0000000000..d171cd4c12 --- /dev/null +++ b/group01/245205213/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group01/245205213/.gitignore b/group01/245205213/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/245205213/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/245205213/.project b/group01/245205213/.project new file mode 100644 index 0000000000..288d4da929 --- /dev/null +++ b/group01/245205213/.project @@ -0,0 +1,17 @@ + + + 245205213Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/245205213/src/JavaTest.java b/group01/245205213/src/JavaTest.java new file mode 100644 index 0000000000..eb994113b7 --- /dev/null +++ b/group01/245205213/src/JavaTest.java @@ -0,0 +1,75 @@ +import com.collection.list.ArrayList; +import com.collection.list.LinkedList; +import com.collection.list.Queue; +import com.collection.list.Stack; + +public class JavaTest { + + /** + * @param args + */ + /** + * @param args + */ + public static void main(String[] args) { + //测试ArrayList +// ArrayList arrList = new ArrayList(); +// for (int i = 0; i < 5; i++) { +// arrList.add(i); +// } +// arrList.add(5, 10); +// System.out.println("size:" + arrList.size()); +// System.out.println("get:" + arrList.get(2)); +// System.out.println(arrList); +// System.out.println("remove:" + arrList.remove(2)); +// System.out.println("remove:" + arrList.remove(5)); +// System.out.println(arrList); + //测试LinkedList +// LinkedList linkedList = new LinkedList(); +// for (int i = 0; i < 20; i++) { +// linkedList.add(i); +// } +// System.out.println("size:"+ linkedList.size()); +// System.out.println("get:" + linkedList.get(1)); +// System.out.println(linkedList); +// System.out.println("remove:" + linkedList.remove(1)); +// System.out.println("remove:" + linkedList.remove(15)); +// System.out.println(linkedList); +// linkedList.addFirst(22); +// System.out.println("size:"+ linkedList.size()); +// System.out.println(linkedList); +// System.out.println("removeFirst:"+ linkedList.removeFirst()); +// System.out.println(linkedList); +// linkedList.add(5, 999); +// System.out.println(linkedList); + //测试Stack +// Stack stack = new Stack(); +// System.out.println("isEmpty:" + stack.isEmpty()); +// for (int i = 0; i < 10; i++) { +// stack.push(i); +// } +// System.out.println("size:" + stack.size()); +// System.out.println("isEmpty:" + stack.isEmpty()); +// System.out.println(stack); +// System.out.println("pop:" + stack.pop()); +// System.out.println("pop:" + stack.pop()); +// System.out.println(stack); +// System.out.println("peek:" + stack.peek()); +// System.out.println("peek:" + stack.peek()); +// System.out.println(stack); +// System.out.println("size:" + stack.size()); + //测试Queue + Queue queue = new Queue(); + System.out.println("isEmpty:" + queue.isEmpty()); + for (int i = 0; i < 10; i++) { + queue.enQueue(i); + } + System.out.println("isEmpty:" + queue.isEmpty()); + System.out.println("size:" + queue.size()); + System.out.println(queue); + System.out.println("deQueue:" + queue.deQueue()); + System.out.println(queue); + + } + +} diff --git a/group01/245205213/src/com/collection/list/ArrayList.java b/group01/245205213/src/com/collection/list/ArrayList.java new file mode 100644 index 0000000000..9922f3aa93 --- /dev/null +++ b/group01/245205213/src/com/collection/list/ArrayList.java @@ -0,0 +1,80 @@ +package com.collection.list; + +import java.util.Arrays; +import java.util.Collection; + +public class ArrayList implements List { + + private int size = 0; + private Object[] elementData = new Object[10]; + + public ArrayList() { + System.out.println("test1"); + } + public ArrayList(Collection c) { + size = c.size(); + elementData = c.toArray(); + } + + private void ensureCapacity() { + if (elementData.length+1 > size) { + elementData = Arrays.copyOf(elementData, size*3/2+1); + } + } + + public String toString() { + return Arrays.toString(Arrays.copyOf(elementData, size)) ; + } + + public boolean add(E e) { + ensureCapacity(); + elementData[size++] = e; + return true; + } + + public boolean add(int index, E e) { + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + ensureCapacity(); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = e; + size++; + return true; + } + + public E get(int index) { + if (index >= size) { + throw new ArrayIndexOutOfBoundsException(); + } + return elementData(index); + } + + @SuppressWarnings("unchecked") + private E elementData(int index) { + return (E) elementData[index]; + } + + public E remove(int index) { + if (index >= size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); + } + E oldValue = elementData(index); + System.arraycopy(elementData, index+1, elementData, index, size-index-1); + elementData[--size] = null; + return oldValue; + } + + public int size() { + return size; + } + + + public boolean clear() { + for (int i = 0; i < elementData.length; i++) { + elementData[i] = null; + } + size = 0; + return true; + } +} diff --git a/group01/245205213/src/com/collection/list/ArrayListTest.java b/group01/245205213/src/com/collection/list/ArrayListTest.java new file mode 100644 index 0000000000..a2717e3602 --- /dev/null +++ b/group01/245205213/src/com/collection/list/ArrayListTest.java @@ -0,0 +1,51 @@ +package com.collection.list; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ArrayListTest { + private ArrayList al; + @BeforeEach + void setUp() throws Exception { + al = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11)); + } + @AfterEach + void tearDown() throws Exception { + System.out.println(al); + } + + @Test + void add() { + System.out.println("add:***********"); + Assert.assertEquals(true, al.add(2)); + } + + @Test + void addElement() { + System.out.println("addElement:***********"); + Assert.assertEquals(true, al.add(1,3)); + } + + @Test + void get() { + System.out.println("get:***********"); + Assert.assertEquals(new Integer(1), al.get(0)); + } + + @Test + void clear() { + System.out.println("clear:***********"); + Assert.assertEquals(true, al.clear()); + } + + @Test + void remove() { + System.out.println("remove:***********"); + Assert.assertEquals(new Integer(2), al.remove(1)); + } + +} diff --git a/group01/245205213/src/com/collection/list/LinkedList.java b/group01/245205213/src/com/collection/list/LinkedList.java new file mode 100644 index 0000000000..10cb8a2b05 --- /dev/null +++ b/group01/245205213/src/com/collection/list/LinkedList.java @@ -0,0 +1,126 @@ +package com.collection.list; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private Node first; + private Node last; + private int size = 0; + + public boolean add(E e) { + if (first == null) { + first = new Node(); + first.data = e; + last = first; + size++; + } else { + Node node = new Node(); + node.data = e; + node.pre = last; + last.next = node; + last = node; + size++; + } + return true; + } + + public E getFirst() { + return first.data; + } + + public E getLast() { + return last.data; + } + + public E get(int index) { + isNotElementIndex(index); + return getNode(first,index).data; + } + + private void isNotElementIndex(int index) { + if (index > size-1 || index < 0) { + throw new NoSuchElementException(); + } + } + + private Node getNode(Node n, int index) { + if (index == 0) { + return n; + } + return getNode(n.next, index-1); + } + + public E remove(int index) { + isNotElementIndex(index); + Node node = getNode(first, index); + E e = node.data; + node.pre.next = node.next; + node.next.pre = node.pre; + node.data = null; + node.next = null; + node.pre = null; + size--; + return e; + } + + public int size() { + return size; + } + + public void addFirst(E e) { + Node node = new Node(); + node.next = first; + node.data = e; + first.pre = node; + first = node; + size++; + } + + public E removeFirst() { + E e = first.data; + Node next = first.next; + first.next = null; + first.data = null; + first = next; + size--; + return e; + } + + private static class Node { + E data; + Node next; + Node pre; + } + + public boolean add(int index, E e) { + Node node = new Node(); + Node pre = getNode(first, index).pre; + node.data = e; + node.next = getNode(first, index); + node.next.pre = node; + node.pre = pre; + node.pre.next = node; + size++; + return true; + + } + + public String toString() { + int index = 0; + Object[] objs = new Object[size]; + Node node = first; + while (node != null) { + objs[index++] = node.data; + node = node.next; + } + return Arrays.toString(objs); + } + + + @Override + public boolean clear() { + // TODO Auto-generated method stub + return true; + } +} diff --git a/group01/245205213/src/com/collection/list/LinkedListTest.java b/group01/245205213/src/com/collection/list/LinkedListTest.java new file mode 100644 index 0000000000..d478d81803 --- /dev/null +++ b/group01/245205213/src/com/collection/list/LinkedListTest.java @@ -0,0 +1,36 @@ +package com.collection.list; + +import org.junit.Assert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class LinkedListTest { + LinkedList ll = new LinkedList(); + + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void add() { + ll.add(2); + ll.add(2); + Assert.assertEquals(true, ll.add(1)); + Assert.assertEquals(new Integer(2), ll.getFirst()); + Assert.assertEquals(new Integer(1), ll.getLast()); + Assert.assertEquals(new Integer(2), ll.get(1)); + Assert.assertEquals(new Integer(3), new Integer(ll.size())); + Assert.assertEquals(new Integer(2), ll.remove(1)); + ll.add(4); + ll.addFirst(4); + ll.add(2, 3); + Assert.assertEquals(new Integer(4), ll.removeFirst()); + System.out.println(ll); + } + +} diff --git a/group01/245205213/src/com/collection/list/List.java b/group01/245205213/src/com/collection/list/List.java new file mode 100644 index 0000000000..3596d9dc79 --- /dev/null +++ b/group01/245205213/src/com/collection/list/List.java @@ -0,0 +1,17 @@ +package com.collection.list; + +public interface List { + + public boolean add(E e); + + public boolean add(int index, E e); + + public E get(int index); + + public E remove(int index); + + public boolean clear(); + + public int size(); + +} diff --git a/group01/245205213/src/com/collection/list/Queue.java b/group01/245205213/src/com/collection/list/Queue.java new file mode 100644 index 0000000000..500dd8ff26 --- /dev/null +++ b/group01/245205213/src/com/collection/list/Queue.java @@ -0,0 +1,29 @@ +package com.collection.list; + +public class Queue { + LinkedList elementData = new LinkedList(); + + public String toString() { + return elementData.toString(); + } + + public void enQueue(E o) { + elementData.add(o); + } + + public Object deQueue() { + Object o = elementData.removeFirst(); + return o; + } + + public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/245205213/src/com/collection/list/QueueTest.java b/group01/245205213/src/com/collection/list/QueueTest.java new file mode 100644 index 0000000000..d54d3e6855 --- /dev/null +++ b/group01/245205213/src/com/collection/list/QueueTest.java @@ -0,0 +1,32 @@ +package com.collection.list; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class QueueTest { + + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void test() { + Queue queue = new Queue(); + System.out.println(queue); + for (int i = 0; i < 10; i++) { + queue.enQueue(i); + System.out.println(queue); + } + System.out.println(queue); + for (int i = 0; i < 10; i++) { + queue.deQueue(); + System.out.println(queue); + } + } + +} diff --git a/group01/245205213/src/com/collection/list/Stack.java b/group01/245205213/src/com/collection/list/Stack.java new file mode 100644 index 0000000000..62e3e1f9f5 --- /dev/null +++ b/group01/245205213/src/com/collection/list/Stack.java @@ -0,0 +1,45 @@ +package com.collection.list; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public String toString() { + return elementData.toString(); + } + + public E push(E e) { + elementData.add(e); + return e; + } + + public synchronized E pop() { + int indexLast = elementData.size()-1; + isNotIndex(indexLast); + E e = elementData.get(indexLast); + elementData.remove(indexLast); + return e; + } + + public synchronized E peek() { + int indexLast = elementData.size()-1; + isNotIndex(indexLast); + E e = elementData.get(indexLast); + return e; + } + + private void isNotIndex(int indexLast) { + if (indexLast == -1) { + throw new EmptyStackException(); + } + } + + public boolean isEmpty() { + return size() == 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group01/245205213/src/com/collection/list/StackTest.java b/group01/245205213/src/com/collection/list/StackTest.java new file mode 100644 index 0000000000..6e6f9236fa --- /dev/null +++ b/group01/245205213/src/com/collection/list/StackTest.java @@ -0,0 +1,56 @@ +package com.collection.list; + +import org.junit.Assert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class StackTest { + + static Stack stack = new Stack(); + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void test() { + Assert.assertEquals(1, (int)stack.push(1)); + Assert.assertEquals(1, (int)stack.peek()); + System.out.println(stack); + Assert.assertEquals(1, (int)stack.pop()); + System.out.println(stack); + for (int i = 0; i < 10; i++) { + stack.push(i); + System.out.println(stack); + } + System.out.println(stack); + } + + @Test + void test2() { + try { + Thread t = new Thread(new Runnable() { + @Override + public void run() { + for (int i = 0; i < 10; i++) { + stack.pop(); + System.out.println(stack); + } + } + }); + while (true) { + t.start(); + } + } catch (Exception e) { + // TODO: handle exception + } finally { + System.out.println(stack.size()); + Assert.assertEquals(true, stack.isEmpty()); + } + } + +} diff --git a/group01/245205213/src/com/collection/map/BinaryTreeNode.java b/group01/245205213/src/com/collection/map/BinaryTreeNode.java new file mode 100644 index 0000000000..88ac8905b0 --- /dev/null +++ b/group01/245205213/src/com/collection/map/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package com.collection.map; + +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 left; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + return null; + } +} diff --git a/group01/245205213/src/com/collection/set/HashSet.java b/group01/245205213/src/com/collection/set/HashSet.java new file mode 100644 index 0000000000..e3ba439b2f --- /dev/null +++ b/group01/245205213/src/com/collection/set/HashSet.java @@ -0,0 +1,5 @@ +package com.collection.set; + +public class HashSet { + +}