From 820adc20a7800ee6ac5f367e7666d373fee95fb0 Mon Sep 17 00:00:00 2001 From: zhoubofeng <598808350@qq.com> Date: Sun, 26 Feb 2017 19:16:48 +0800 Subject: [PATCH] ArrayList,LinkedList,Queue,Stack about ArrayList,LinkedList,Queue,Stack --- group14/598808350/20170219.txt | 5 + .../src/org/learning/container/ArrayList.java | 186 ++++++++++++++++++ .../org/learning/container/LinkedList.java | 183 +++++++++++++++++ .../src/org/learning/container/Queue.java | 47 +++++ .../src/org/learning/container/Stack.java | 44 +++++ 5 files changed, 465 insertions(+) create mode 100644 group14/598808350/20170219.txt create mode 100644 group14/598808350/2017project/src/org/learning/container/ArrayList.java create mode 100644 group14/598808350/2017project/src/org/learning/container/LinkedList.java create mode 100644 group14/598808350/2017project/src/org/learning/container/Queue.java create mode 100644 group14/598808350/2017project/src/org/learning/container/Stack.java diff --git a/group14/598808350/20170219.txt b/group14/598808350/20170219.txt new file mode 100644 index 0000000000..2570935d90 --- /dev/null +++ b/group14/598808350/20170219.txt @@ -0,0 +1,5 @@ +描述 CPU,内存, 硬盘,指令之间的关系 + +http://blog.sina.com.cn/s/blog_986d02cd0102xncn.html + +QQ:598808350 \ No newline at end of file diff --git a/group14/598808350/2017project/src/org/learning/container/ArrayList.java b/group14/598808350/2017project/src/org/learning/container/ArrayList.java new file mode 100644 index 0000000000..5755c7cf3a --- /dev/null +++ b/group14/598808350/2017project/src/org/learning/container/ArrayList.java @@ -0,0 +1,186 @@ +package org.learning.container; + + +public class ArrayList { + + private Object [] objs = null; + private int index = -1; + public ArrayList(){ + objs = new Object[5]; + } + public ArrayList(int size){ + objs = new Object[size]; + } + /** + * 返回一个新的数组 + * @param src + * @param src_index + * @param dest + * @param dest_index + * @param length + * @return + */ + private static Object[] copy(Object[] src,int src_index,Object[] dest,int dest_index,int length){ + System.arraycopy(src, src_index, dest, dest_index, length); + return dest; + } + + public void add(Object obj){ + if(this.index == objs.length-1) { + Object[] dest = new Object[objs.length+5]; + objs = copy(objs,0,dest,0,objs.length); + } + this.index ++; + objs[this.index] = obj; + } + + public void add(int index,Object obj){ + if(index-1 > this.index || index < 0){ + throw new IndexOutOfBoundsException(); + } + Object[] dest = new Object[objs.length+5]; + if(index == 0){ + dest[index] = obj; + dest =copy(objs,index,dest,index+1,getSize()); + objs = dest; + }else if(index == getSize()){ + objs[index] = obj; + }else{ + dest = copy(objs,0,dest,0,index);//前部分 + dest[index] = obj; //中间部分 + dest =copy(objs,index,dest,index+1,getSize()-index);//后部分 + objs = dest; + } + this.index++; + } + + public Object get(int index){ + if(index > this.index || index <0){ + throw new IndexOutOfBoundsException(); + } + return objs[index]; + } + + public boolean isEmpty(){ + if(objs == null || this.index == -1){ + return true; + } + return false; + } + + public int getSize(){ + return this.index+1; + } + + public boolean remove(int index){ + if (index <0 || index > objs.length){ + throw new IndexOutOfBoundsException(); + } + Object[] dest = new Object[this.index]; + dest = copy(objs,0,dest,0,index);//前部分 + dest = copy(objs,index+1,dest,index,this.index-index);//后部分 + objs = dest; + this.index --; + return true; + } + public boolean remove(Object obj){ + for(int i=0;i<=this.index;i++){ + if(obj==null ? get(i)==null : obj.equals(get(i))) { + remove(i); //i 即 当前元素的下标识 + return true; + } + } + return false; + } + public static void print(Object obj){ + System.out.println(obj); + } + + public static void main(String [] args){ + ArrayList al = new ArrayList(); + /*print(al.isEmpty()); + al.add("a1"); + print(al.isEmpty()); + print(al.getSize()); + print(al.get(0)); + print(al.get(1));*/ + al.add("a0"); + al.add("a1"); + al.add("a2"); + al.add("a3"); + al.add("a4"); + al.add("a5"); + + //al.remove(0); + //al.remove(5); + //al.remove(2); + /*boolean flag = al.remove("a7"); + print(flag); + for(int i=0;i