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)); + } +}