From 09a0635ffe843620e03ed842a60ec49d573a11a2 Mon Sep 17 00:00:00 2001 From: Yate Date: Mon, 24 Jun 2019 02:40:29 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E8=BF=99=E6=98=AF=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group01/245205213/.classpath | 6 ++++++ group01/245205213/.gitignore | 1 + group01/245205213/.project | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 group01/245205213/.classpath create mode 100644 group01/245205213/.gitignore create mode 100644 group01/245205213/.project 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 + + From d1d299fafc56601d418a15598d94827efa661020 Mon Sep 17 00:00:00 2001 From: Yate Date: Tue, 25 Jun 2019 05:49:10 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group01/245205213/src/JavaTest.java | 8 ++ .../src/com/collection/list/ArrayList.java | 78 +++++++++++++++++++ .../src/com/collection/list/LinkedList.java | 46 +++++++++++ .../src/com/collection/list/List.java | 15 ++++ .../src/com/collection/list/Queue.java | 19 +++++ .../src/com/collection/list/Stack.java | 25 ++++++ .../com/collection/map/BinaryTreeNode.java | 7 ++ .../src/com/collection/set/HashSet.java | 5 ++ 8 files changed, 203 insertions(+) create mode 100644 group01/245205213/src/JavaTest.java create mode 100644 group01/245205213/src/com/collection/list/ArrayList.java create mode 100644 group01/245205213/src/com/collection/list/LinkedList.java create mode 100644 group01/245205213/src/com/collection/list/List.java create mode 100644 group01/245205213/src/com/collection/list/Queue.java create mode 100644 group01/245205213/src/com/collection/list/Stack.java create mode 100644 group01/245205213/src/com/collection/map/BinaryTreeNode.java create mode 100644 group01/245205213/src/com/collection/set/HashSet.java diff --git a/group01/245205213/src/JavaTest.java b/group01/245205213/src/JavaTest.java new file mode 100644 index 0000000000..3c095814cb --- /dev/null +++ b/group01/245205213/src/JavaTest.java @@ -0,0 +1,8 @@ + +public class JavaTest { + + public static void main(String[] args) { + + } + +} 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..f057e85da6 --- /dev/null +++ b/group01/245205213/src/com/collection/list/ArrayList.java @@ -0,0 +1,78 @@ +package com.collection.list; + +import java.util.Arrays; + +public class ArrayList implements List{ + public static void main(String[] args) { + 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); + } + + private int size = 0; + private Object[] elementData = new Object[10]; + + public String toString() { + return Arrays.toString(elementData); + } + + public void add(Object o) { + if (size < 10) { + for (int i = 0; i < elementData.length; i++) { + if (elementData[i] == null) { + elementData[i] = o; + size++; + return; + } + } + } else { + elementData = Arrays.copyOf(elementData, ++size); + elementData[size-1] = o; + } + } + + public void add(int index, Object o) { + if (index > size) { + System.out.println("索引超出范围"); + return; + } + Object[] copyArr = Arrays.copyOfRange(elementData, index, elementData.length); + elementData[index] = o; + elementData = Arrays.copyOf(elementData, elementData.length+1); + System.arraycopy(copyArr, 0, elementData, index+1, copyArr.length); + size++; + } + + public Object get(int index) { + if (index > size-1) { + System.out.println("索引超出范围"); + return null; + } + return elementData[index]; + } + + public Object remove(int index) { + if (index > size-1) { + System.out.println("索引超出范围"); + return null; + } + Object target = elementData[index]; + Object[] copyArr = Arrays.copyOfRange(elementData, index+1, elementData.length); + elementData = Arrays.copyOf(elementData, elementData.length-1); + System.arraycopy(copyArr, 0, elementData, index, copyArr.length); + size--; + return target; + } + + public int size() { + return size; + } +} 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..fb8257bd2e --- /dev/null +++ b/group01/245205213/src/com/collection/list/LinkedList.java @@ -0,0 +1,46 @@ +package com.collection.list; + +public class LinkedList implements List { + private Node head; + + //如果存在头结点 + public void add(Object o) { + if (head == null) { + + } else { + + } + } + + 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 Object removeFirst() { + return null; + } + + private static class Node { + Object data; + Node next; + Node prev; + } + + @Override + public void add(int index, Object o) { + // TODO Auto-generated method stub + + } +} 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..ffdb31dd44 --- /dev/null +++ b/group01/245205213/src/com/collection/list/List.java @@ -0,0 +1,15 @@ +package com.collection.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(); + +} 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..bf781c3eef --- /dev/null +++ b/group01/245205213/src/com/collection/list/Queue.java @@ -0,0 +1,19 @@ +package com.collection.list; + +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/group01/245205213/src/com/collection/list/Stack.java b/group01/245205213/src/com/collection/list/Stack.java new file mode 100644 index 0000000000..6b3d686c70 --- /dev/null +++ b/group01/245205213/src/com/collection/list/Stack.java @@ -0,0 +1,25 @@ +package com.collection.list; + +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 void name() { + + } +} 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..a55b07621c --- /dev/null +++ b/group01/245205213/src/com/collection/map/BinaryTreeNode.java @@ -0,0 +1,7 @@ +package com.collection.map; + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; +} 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 { + +} From 5e6cee7ab157fc0c5c59583d40dfa410fd5497c1 Mon Sep 17 00:00:00 2001 From: Yate Date: Tue, 25 Jun 2019 06:03:26 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=B3=A8=E9=87=8Amain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/collection/list/ArrayList.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/group01/245205213/src/com/collection/list/ArrayList.java b/group01/245205213/src/com/collection/list/ArrayList.java index f057e85da6..84d57eb2ce 100644 --- a/group01/245205213/src/com/collection/list/ArrayList.java +++ b/group01/245205213/src/com/collection/list/ArrayList.java @@ -3,19 +3,14 @@ import java.util.Arrays; public class ArrayList implements List{ - public static void main(String[] args) { - 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); - } + /* + * public static void main(String[] args) { 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); } + */ private int size = 0; private Object[] elementData = new Object[10]; From 4bf754b5a2de6c6781ee20ec2aac272a1350835b Mon Sep 17 00:00:00 2001 From: Yate Date: Wed, 26 Jun 2019 00:59:58 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group01/245205213/src/JavaTest.java | 69 +++++++++++++- .../src/com/collection/list/ArrayList.java | 50 +++++------ .../src/com/collection/list/LinkedList.java | 90 +++++++++++++++++-- .../src/com/collection/list/Queue.java | 17 +++- .../src/com/collection/list/Stack.java | 22 +++-- .../com/collection/map/BinaryTreeNode.java | 28 ++++++ 6 files changed, 230 insertions(+), 46 deletions(-) diff --git a/group01/245205213/src/JavaTest.java b/group01/245205213/src/JavaTest.java index 3c095814cb..eb994113b7 100644 --- a/group01/245205213/src/JavaTest.java +++ b/group01/245205213/src/JavaTest.java @@ -1,8 +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 index 84d57eb2ce..a1e6972c99 100644 --- a/group01/245205213/src/com/collection/list/ArrayList.java +++ b/group01/245205213/src/com/collection/list/ArrayList.java @@ -2,23 +2,17 @@ import java.util.Arrays; -public class ArrayList implements List{ - /* - * public static void main(String[] args) { 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); } - */ - +public class ArrayList implements List { + private int size = 0; private Object[] elementData = new Object[10]; - + public String toString() { - return Arrays.toString(elementData); + Object[] oArr = new Object[size]; + oArr = Arrays.copyOf(elementData, size); + return Arrays.toString(oArr); } - + public void add(Object o) { if (size < 10) { for (int i = 0; i < elementData.length; i++) { @@ -26,47 +20,47 @@ public void add(Object o) { elementData[i] = o; size++; return; - } - } + } + } } else { elementData = Arrays.copyOf(elementData, ++size); - elementData[size-1] = o; + elementData[size - 1] = o; } } - + public void add(int index, Object o) { if (index > size) { System.out.println("索引超出范围"); - return; +// return; } Object[] copyArr = Arrays.copyOfRange(elementData, index, elementData.length); elementData[index] = o; - elementData = Arrays.copyOf(elementData, elementData.length+1); - System.arraycopy(copyArr, 0, elementData, index+1, copyArr.length); + elementData = Arrays.copyOf(elementData, elementData.length + 1); + System.arraycopy(copyArr, 0, elementData, index + 1, copyArr.length); size++; } - + public Object get(int index) { - if (index > size-1) { + if (index > size - 1) { System.out.println("索引超出范围"); - return null; +// return null; } return elementData[index]; } - + public Object remove(int index) { - if (index > size-1) { + if (index > size - 1) { System.out.println("索引超出范围"); return null; } Object target = elementData[index]; - Object[] copyArr = Arrays.copyOfRange(elementData, index+1, elementData.length); - elementData = Arrays.copyOf(elementData, elementData.length-1); + Object[] copyArr = Arrays.copyOfRange(elementData, index + 1, elementData.length); + elementData = Arrays.copyOf(elementData, elementData.length - 1); System.arraycopy(copyArr, 0, elementData, index, copyArr.length); size--; return target; } - + public int size() { return size; } diff --git a/group01/245205213/src/com/collection/list/LinkedList.java b/group01/245205213/src/com/collection/list/LinkedList.java index fb8257bd2e..cfd3c6993c 100644 --- a/group01/245205213/src/com/collection/list/LinkedList.java +++ b/group01/245205213/src/com/collection/list/LinkedList.java @@ -1,35 +1,104 @@ package com.collection.list; +import java.util.Arrays; + public class LinkedList implements List { + + public String toString() { + int index = 0; + Object[] objs = new Object[size]; + Node node = head; + while (node != null) { + objs[index++] = node.data; + node = node.next; + } + return Arrays.toString(objs); + } private Node head; + private int size = 0; //如果存在头结点 public void add(Object o) { if (head == null) { - + head = new Node(); + head.data = o; + size++; } else { - + Node node = getLastNode(head); + node.next = new Node(); + node.next.data = o; + size++; } } + private Node getLastNode(Node n) { + if (n.next != null) { + return getLastNode(n.next); + } + return n/*.next = new Node() */; + } + public Object get(int index) { - return null; + if (index > size-1 || index < 0) { + System.out.println("索引超出范围!"); +// return null; + } + return getNode(head,index).data; + } + + private Node getNode(Node n, int index) { + if (index > 0) { + return getNode(n.next, index-1); + } else if (index < 0) { + return null; + } + return n; } public Object remove(int index) { - return null; + Object o = getNode(head, index).data; + getPrevNode(head, index).next = getNextNode(head, index); + size--; + return o; + } + + private Node getPrevNode(Node n, int index) { + if (index > 1) { + return getPrevNode(n.next, index-1); + } + return n; + } + + private Node getNextNode(Node n, int index) { + if (index > size-1) { + System.out.println("索引超出范围!"); + return null; + } + return getNode(head, index).next; } public int size() { - return -1; + return size; } public void addFirst(Object o) { - +// Node node = head; +// head.next = node; +// head.data = o; +// size++; + Node node = new Node(); + node.next = head; + node.data = o; + head = node; + size++; } public Object removeFirst() { - return null; + Object o = head.data; + Node node = head.next; + head = node; + size--; + return o; } private static class Node { @@ -38,9 +107,12 @@ private static class Node { Node prev; } - @Override public void add(int index, Object o) { - // TODO Auto-generated method stub + Node node = new Node(); + node.data = o; + node.next = getNode(head, index); + getPrevNode(head, index).next = node; + size++; } } diff --git a/group01/245205213/src/com/collection/list/Queue.java b/group01/245205213/src/com/collection/list/Queue.java index bf781c3eef..6c3ea63e46 100644 --- a/group01/245205213/src/com/collection/list/Queue.java +++ b/group01/245205213/src/com/collection/list/Queue.java @@ -1,19 +1,30 @@ package com.collection.list; public class Queue { + LinkedList elementData = new LinkedList(); + + public String toString() { + return elementData.toString(); + } + public void enQueue(Object o) { - + elementData.add(o); } public Object deQueue() { - return null; +// int index = elementData.size()-1; + Object o = elementData.removeFirst(); + return o; } public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } return false; } public int size() { - return -1; + return elementData.size(); } } diff --git a/group01/245205213/src/com/collection/list/Stack.java b/group01/245205213/src/com/collection/list/Stack.java index 6b3d686c70..de9dcb27c4 100644 --- a/group01/245205213/src/com/collection/list/Stack.java +++ b/group01/245205213/src/com/collection/list/Stack.java @@ -3,23 +3,35 @@ public class Stack { private ArrayList elementData = new ArrayList(); + public String toString() { + return elementData.toString(); + } + public void push(Object o) { - + elementData.add(o); } public Object pop() { - return null; + int indexLast = elementData.size()-1; + Object o = elementData.get(indexLast); + elementData.remove(indexLast); + return o; } public Object peek() { - return null; + int indexLast = elementData.size()-1; + Object o = elementData.get(indexLast); + return o; } public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } return false; } - public void name() { - + public int size() { + return elementData.size(); } } diff --git a/group01/245205213/src/com/collection/map/BinaryTreeNode.java b/group01/245205213/src/com/collection/map/BinaryTreeNode.java index a55b07621c..88ac8905b0 100644 --- a/group01/245205213/src/com/collection/map/BinaryTreeNode.java +++ b/group01/245205213/src/com/collection/map/BinaryTreeNode.java @@ -4,4 +4,32 @@ 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; + } } From dab596e3a6916905de0e00cb09013ef8adbe6242 Mon Sep 17 00:00:00 2001 From: Yate Date: Fri, 28 Jun 2019 23:38:03 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/collection/list/ArrayList.java | 93 +++++++++++-------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/group01/245205213/src/com/collection/list/ArrayList.java b/group01/245205213/src/com/collection/list/ArrayList.java index a1e6972c99..9922f3aa93 100644 --- a/group01/245205213/src/com/collection/list/ArrayList.java +++ b/group01/245205213/src/com/collection/list/ArrayList.java @@ -1,67 +1,80 @@ package com.collection.list; import java.util.Arrays; +import java.util.Collection; -public class ArrayList implements List { +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() { - Object[] oArr = new Object[size]; - oArr = Arrays.copyOf(elementData, size); - return Arrays.toString(oArr); + return Arrays.toString(Arrays.copyOf(elementData, size)) ; } - public void add(Object o) { - if (size < 10) { - for (int i = 0; i < elementData.length; i++) { - if (elementData[i] == null) { - elementData[i] = o; - size++; - return; - } - } - } else { - elementData = Arrays.copyOf(elementData, ++size); - elementData[size - 1] = o; - } + public boolean add(E e) { + ensureCapacity(); + elementData[size++] = e; + return true; } - public void add(int index, Object o) { - if (index > size) { - System.out.println("索引超出范围"); -// return; + public boolean add(int index, E e) { + if (index > size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); } - Object[] copyArr = Arrays.copyOfRange(elementData, index, elementData.length); - elementData[index] = o; - elementData = Arrays.copyOf(elementData, elementData.length + 1); - System.arraycopy(copyArr, 0, elementData, index + 1, copyArr.length); + ensureCapacity(); + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = e; size++; + return true; } - public Object get(int index) { - if (index > size - 1) { - System.out.println("索引超出范围"); -// return null; + public E get(int index) { + if (index >= size) { + throw new ArrayIndexOutOfBoundsException(); } - return elementData[index]; + return elementData(index); } - public Object remove(int index) { - if (index > size - 1) { - System.out.println("索引超出范围"); - return null; + @SuppressWarnings("unchecked") + private E elementData(int index) { + return (E) elementData[index]; + } + + public E remove(int index) { + if (index >= size || index < 0) { + throw new ArrayIndexOutOfBoundsException(); } - Object target = elementData[index]; - Object[] copyArr = Arrays.copyOfRange(elementData, index + 1, elementData.length); - elementData = Arrays.copyOf(elementData, elementData.length - 1); - System.arraycopy(copyArr, 0, elementData, index, copyArr.length); - size--; - return target; + 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; + } } From c3cbfc0debf07379bdac4d90d4f00715251dfce1 Mon Sep 17 00:00:00 2001 From: Yate Date: Fri, 28 Jun 2019 23:38:20 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/collection/list/ArrayListTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 group01/245205213/src/com/collection/list/ArrayListTest.java 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..763e82167d --- /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 { + static 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)); + } + +} From 2d9e8f1b939dd8039c3bf9b734aa306418a716a3 Mon Sep 17 00:00:00 2001 From: Yate Date: Mon, 1 Jul 2019 22:30:28 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/collection/list/ArrayListTest.java | 2 +- .../src/com/collection/list/LinkedList.java | 164 +++++++++--------- .../src/com/collection/list/List.java | 12 +- .../src/com/collection/list/Queue.java | 7 +- .../src/com/collection/list/Stack.java | 36 ++-- 5 files changed, 119 insertions(+), 102 deletions(-) diff --git a/group01/245205213/src/com/collection/list/ArrayListTest.java b/group01/245205213/src/com/collection/list/ArrayListTest.java index 763e82167d..a2717e3602 100644 --- a/group01/245205213/src/com/collection/list/ArrayListTest.java +++ b/group01/245205213/src/com/collection/list/ArrayListTest.java @@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test; class ArrayListTest { - static ArrayList al; + private ArrayList al; @BeforeEach void setUp() throws Exception { al = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8,9,10,11)); diff --git a/group01/245205213/src/com/collection/list/LinkedList.java b/group01/245205213/src/com/collection/list/LinkedList.java index cfd3c6993c..10cb8a2b05 100644 --- a/group01/245205213/src/com/collection/list/LinkedList.java +++ b/group01/245205213/src/com/collection/list/LinkedList.java @@ -1,118 +1,126 @@ package com.collection.list; import java.util.Arrays; +import java.util.NoSuchElementException; -public class LinkedList implements List { - - public String toString() { - int index = 0; - Object[] objs = new Object[size]; - Node node = head; - while (node != null) { - objs[index++] = node.data; - node = node.next; - } - return Arrays.toString(objs); - } - private Node head; +public class LinkedList implements List { + private Node first; + private Node last; private int size = 0; - //如果存在头结点 - public void add(Object o) { - if (head == null) { - head = new Node(); - head.data = o; + public boolean add(E e) { + if (first == null) { + first = new Node(); + first.data = e; + last = first; size++; } else { - Node node = getLastNode(head); - node.next = new Node(); - node.next.data = o; + Node node = new Node(); + node.data = e; + node.pre = last; + last.next = node; + last = node; size++; } + return true; } - private Node getLastNode(Node n) { - if (n.next != null) { - return getLastNode(n.next); - } - return n/*.next = new Node() */; + public E getFirst() { + return first.data; } - public Object get(int index) { - if (index > size-1 || index < 0) { - System.out.println("索引超出范围!"); -// return null; - } - return getNode(head,index).data; + public E getLast() { + return last.data; } - private Node getNode(Node n, int index) { - if (index > 0) { - return getNode(n.next, index-1); - } else if (index < 0) { - return null; - } - return n; + public E get(int index) { + isNotElementIndex(index); + return getNode(first,index).data; } - - public Object remove(int index) { - Object o = getNode(head, index).data; - getPrevNode(head, index).next = getNextNode(head, index); - size--; - return o; + + private void isNotElementIndex(int index) { + if (index > size-1 || index < 0) { + throw new NoSuchElementException(); + } } - private Node getPrevNode(Node n, int index) { - if (index > 1) { - return getPrevNode(n.next, index-1); + private Node getNode(Node n, int index) { + if (index == 0) { + return n; } - return n; + return getNode(n.next, index-1); } - private Node getNextNode(Node n, int index) { - if (index > size-1) { - System.out.println("索引超出范围!"); - return null; - } - return getNode(head, index).next; + 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(Object o) { -// Node node = head; -// head.next = node; -// head.data = o; -// size++; - Node node = new Node(); - node.next = head; - node.data = o; - head = node; + public void addFirst(E e) { + Node node = new Node(); + node.next = first; + node.data = e; + first.pre = node; + first = node; size++; } - public Object removeFirst() { - Object o = head.data; - Node node = head.next; - head = node; + public E removeFirst() { + E e = first.data; + Node next = first.next; + first.next = null; + first.data = null; + first = next; size--; - return o; + return e; } - private static class Node { - Object data; - Node next; - Node prev; + private static class Node { + E data; + Node next; + Node pre; } - public void add(int index, Object o) { - Node node = new Node(); - node.data = o; - node.next = getNode(head, index); - getPrevNode(head, index).next = node; + 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/List.java b/group01/245205213/src/com/collection/list/List.java index ffdb31dd44..3596d9dc79 100644 --- a/group01/245205213/src/com/collection/list/List.java +++ b/group01/245205213/src/com/collection/list/List.java @@ -1,14 +1,16 @@ package com.collection.list; -public interface List { +public interface List { - public void add(Object o); + public boolean add(E e); - public void add(int index, Object o); + public boolean add(int index, E e); + + public E get(int index); - public Object get(int index); + public E remove(int index); - public Object 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 index 6c3ea63e46..500dd8ff26 100644 --- a/group01/245205213/src/com/collection/list/Queue.java +++ b/group01/245205213/src/com/collection/list/Queue.java @@ -1,18 +1,17 @@ package com.collection.list; -public class Queue { - LinkedList elementData = new LinkedList(); +public class Queue { + LinkedList elementData = new LinkedList(); public String toString() { return elementData.toString(); } - public void enQueue(Object o) { + public void enQueue(E o) { elementData.add(o); } public Object deQueue() { -// int index = elementData.size()-1; Object o = elementData.removeFirst(); return o; } diff --git a/group01/245205213/src/com/collection/list/Stack.java b/group01/245205213/src/com/collection/list/Stack.java index de9dcb27c4..62e3e1f9f5 100644 --- a/group01/245205213/src/com/collection/list/Stack.java +++ b/group01/245205213/src/com/collection/list/Stack.java @@ -1,34 +1,42 @@ package com.collection.list; -public class Stack { - private ArrayList elementData = new ArrayList(); +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); public String toString() { return elementData.toString(); } - public void push(Object o) { - elementData.add(o); + public E push(E e) { + elementData.add(e); + return e; } - public Object pop() { + public synchronized E pop() { int indexLast = elementData.size()-1; - Object o = elementData.get(indexLast); + isNotIndex(indexLast); + E e = elementData.get(indexLast); elementData.remove(indexLast); - return o; + return e; } - public Object peek() { + public synchronized E peek() { int indexLast = elementData.size()-1; - Object o = elementData.get(indexLast); - return o; + isNotIndex(indexLast); + E e = elementData.get(indexLast); + return e; + } + + private void isNotIndex(int indexLast) { + if (indexLast == -1) { + throw new EmptyStackException(); + } } public boolean isEmpty() { - if (elementData.size() == 0) { - return true; - } - return false; + return size() == 0; } public int size() { From 7ac843677466ad88a63fa8578ffff289d3804cb7 Mon Sep 17 00:00:00 2001 From: Yate Date: Mon, 1 Jul 2019 22:35:19 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/collection/list/LinkedListTest.java | 36 ++++++++++++ .../src/com/collection/list/QueueTest.java | 32 +++++++++++ .../src/com/collection/list/StackTest.java | 56 +++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 group01/245205213/src/com/collection/list/LinkedListTest.java create mode 100644 group01/245205213/src/com/collection/list/QueueTest.java create mode 100644 group01/245205213/src/com/collection/list/StackTest.java 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/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/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()); + } + } + +}