1 package cn.njupt.mdj; 2 3 class linerTable<E>{ 4 private Object[] data = null; 5 private int capacity; 6 private int current; 7 8 //默认大小 9 public linerTable(){ 10 this(10); 11 } 12 13 //初始化大小 开辟空间 14 public linerTable(int size){ 15 if(size >= 0){ 16 this.capacity = size; 17 this.data = new Object[this.capacity]; 18 this.current = 0; 19 }else{ 20 throw new RuntimeException("初始化大小需要大于0 :"+ size); 21 } 22 } 23 24 //添加元素,由于是顺序表,元素依次向后填加 25 public boolean add(E object){ 26 //注意这里不是 this.current == this.capacity-1,原因,添加元素后,this.current++了,使得比较的时候直接索引加了1位 27 if(this.current == this.capacity){ 28 //扩充容量 29 this.capacity *= 2; 30 //将原有的元素拷贝 31 Object[] newdata = new Object[this.capacity]; 32 for(int i=0;i<this.current;i++){ 33 newdata[i] = data[i]; 34 } 35 this.data = newdata; 36 } 37 //容量扩充后,或则本来空间就够,则向末尾添加元素 38 this.data[current] = object; 39 this.current++; 40 return true; 41 } 42 43 //有增加当然就有删除元素,删除,则肯定是删除指定位置的某个元素 44 public boolean delete(int index){ 45 //首先删除之前,肯定是要判断index是否合理的 46 if(index < 0 || index > current){ 47 throw new RuntimeException("无效的下标:" + index); //越界 48 } 49 //下标既然合理,开始删除指定的元素,删除,即把该位置元素去掉 50 if(index == this.current-1){//末尾元素 51 this.data[current-1] = null; //直接删除 52 } 53 //不是末尾元素,则所有元素向前补洞。 54 for(int i = index ;i<this.current-1;i++){ 55 this.data[i] = this.data[i+1]; 56 } 57 --this.current; 58 return true; 59 } 60 61 //有添加 删除 当然有插入 和查找 62 //插入元素,像指定位置插入 指定的元素 63 public boolean insert(int index,E object){ 64 //插入元素,首先要保证插入的index是合理的数组范围内 65 if(index < 0 || index > current){ 66 throw new RuntimeException("无效的下标:" + index); //越界 67 } 68 //但同时要保证元素有空间可以给要插入的元素 69 if(this.current == this.capacity){ 70 //扩充容量 71 this.capacity *= 2; 72 //将原有的元素拷贝 73 Object[] newdata = new Object[this.capacity]; 74 for(int i=0;i<this.current;i++){ 75 newdata[i] = data[i]; 76 } 77 this.data = newdata; 78 } 79 80 //有了容量,开始插入,当然如果是向末尾插入,那就是添加元素了,但如果不是末尾,则就是整体元素后移了,所以获取当前元素位置 81 //System.out.println(current); 82 for(int i= current; i > index ; i--){ 83 this.data[i] = this.data[i-1]; 84 } 85 //空出的位置即是待插入的位置,若current<= index ,那么就是向末尾添加元素了 86 this.data[index] = object; 87 this.current++; 88 return true; 89 } 90 91 //查找元素,直接给定下标,返回要的元素 92 public E get(int index){ 93 if(index < 0 || index > current){ 94 throw new RuntimeException("无效的下标:" + index); //越界 95 } 96 return (E)this.data[index]; 97 } 98 99 //获取当前元素的个数 100 public int size() { 101 return current; 102 } 103 104 public void display(){ 105 if(data == null || data.length == 0){ 106 System.out.println("没有数据"); 107 } 108 109 for(int i=0;i < current;i++){ 110 System.out.print(data[i].toString() + "、"); 111 } 112 } 113 114 } 115 116 117 public class ArrayList { 118 119 /** 120 * @param args 121 */ 122 public static void main(String[] args) { 123 // TODO Auto-generated method stub 124 linerTable l = new linerTable(20); 125 126 //add 127 l.add(1); 128 l.add(2); 129 l.add(3); 130 l.add(5); 131 l.add(6); 132 l.add(7); 133 l.add(9); 134 135 l.display(); 136 137 System.out.println(""); 138 139 //insert 140 l.insert(6,8); 141 142 l.display(); 143 System.out.println(""); 144 //delete 145 l.delete(6); 146 l.display(); 147 System.out.println(""); 148 //get 149 System.out.println(l.get(2)); 150 } 151 152 }