From e00abc3e248cf1e8ba92414757f80ecd27c6c18b Mon Sep 17 00:00:00 2001 From: panda <1311822904@qq.com> Date: Sat, 25 Feb 2017 21:25:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../myCollection/src/ArrayList.java | 99 ++++++++++ .../myCollection/src/Iterator.java | 5 + .../myCollection/src/LinkedList.java | 182 ++++++++++++++++++ .../myCollection/src/List.java | 7 + .../myCollection/src/Queue.java | 44 +++++ .../myCollection/src/Stack.java | 53 +++++ 6 files changed, 390 insertions(+) create mode 100644 group15/1503_1311822904/myCollection/src/ArrayList.java create mode 100644 group15/1503_1311822904/myCollection/src/Iterator.java create mode 100644 group15/1503_1311822904/myCollection/src/LinkedList.java create mode 100644 group15/1503_1311822904/myCollection/src/List.java create mode 100644 group15/1503_1311822904/myCollection/src/Queue.java create mode 100644 group15/1503_1311822904/myCollection/src/Stack.java diff --git a/group15/1503_1311822904/myCollection/src/ArrayList.java b/group15/1503_1311822904/myCollection/src/ArrayList.java new file mode 100644 index 0000000000..38f08431ce --- /dev/null +++ b/group15/1503_1311822904/myCollection/src/ArrayList.java @@ -0,0 +1,99 @@ +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + private int length=3; + private Object[] elementData = new Object[length]; + + public void add(Object o){ + if(size>=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+1