1 package Eric.ADT; 2 3 import org.junit.Test; 4 5 /** 6 * <p> 7 * Title:MyVector 8 * </p> 9 * <p> 10 * Description:Implement vector by java 11 * </p> 12 * <p> 13 * Location:Frostburg 14 * </p> 15 * 16 * @author: Eric.Chen 17 * @date:2017年9月17日上午9:15:19 18 */ 19 public class MyVector implements JavaVector, Cloneable { 20 private static final int InicialCapacity = 100; 21 private int minCapacity; 22 private Object[] elementData; 23 private int size; 24 25 public MyVector() { 26 elementData = new Object[InicialCapacity]; 27 size = 0; 28 minCapacity += elementData.length;//扩容时的增量 29 } 30 31 // 1.添加元素 32 @Override 33 public void append(Object element) { 34 if (size == elementData.length) { 35 ensureCapacity(InicialCapacity); 36 } else { 37 elementData[size] = element; 38 size++; 39 } 40 41 } 42 43 // 2.清空向量 44 @Override 45 public void clear() { 46 int size = elementData.length; 47 for (int i = 0; i < elementData.length; i++) { 48 elementData[i] = (Integer) null; 49 size--; 50 } 51 52 } 53 54 // 3.判断向量中是否含有特定元素 55 @Override 56 public boolean contains(Object element) { 57 for (int i = 0; i < elementData.length; i++) { 58 if (element.equals(elementData[i])) 59 return true; 60 } 61 return false; 62 } 63 64 // 4.查找指定元素的角标 65 @Override 66 public Object elementAt(int index) { 67 for (int i = 0; i < elementData.length; i++) { 68 if (elementData[i].equals(elementData[index])) { 69 return elementData[i]; 70 } 71 } 72 return null; 73 } 74 75 // 5.查找指点角标对应的值 76 @Override 77 public int indexOf(Object element) { 78 for (int i = 0; i < elementData.length; i++) { 79 if (elementData[i].equals(element)) 80 return i; 81 } 82 return -1; 83 } 84 85 // 6.判断向量是否为空 86 @Override 87 public boolean isEmpty() { 88 if (size() == 0) 89 return false; 90 else 91 return true; 92 } 93 94 // 7.删除指定角标的元素 95 @Override 96 public void removeAt(int index) { 97 for (int i = index; i < elementData.length - 1; i++) { 98 elementData[i] = elementData[i + 1]; 99 } 100 Object[] temp = new Object[elementData.length - 1]; 101 for (int i = 0; i < elementData.length - 1; i++) { 102 temp[i] = elementData[i]; 103 } 104 elementData = temp; 105 106 } 107 108 // 8.移除指定元素 109 @Override 110 public void remove(Object element) { 111 // TODO Auto-generated method stub 112 for (int i = 0; i < elementData.length; i++) { 113 if (element.equals(elementData[i])) { 114 removeAt(i); 115 } 116 } 117 118 } 119 120 // 9.用指定的元素代替指定角标的值 121 @Override 122 public void replace(int index, Object element) { 123 // TODO Auto-generated method stub 124 elementData[index] = element; 125 126 } 127 128 // 10.返回向量中的元素个数,不包括null 129 @Override 130 public int size() { 131 int num = 0; 132 for (int i = 0; i < elementData.length; i++) { 133 if (elementData[i] != null) { 134 num++; 135 } 136 } 137 return num; 138 } 139 140 // 11.当数组长度不够时进行扩容 141 @Override 142 143 public boolean ensureCapacity(int minCapacity) { 144 Object[] temp = new Object[elementData.length + minCapacity]; 145 for (int i = 0; i < temp.length; i++) { 146 temp[i] = elementData[i]; 147 } 148 elementData = temp; 149 return true; 150 } 151 152 // 12.反转数组 153 @Override 154 public void reverse() { 155 Object[] data = new Object[elementData.length]; 156 int j = data.length - 1; 157 for (int i = 0; i < size(); i++) { 158 data[j] = elementData[i]; 159 j--; 160 } 161 elementData = data; 162 } 163 164 // 13.克隆数组 165 @Override 166 public Object clone() { 167 Object[] cloneArray = new Object[size()]; 168 for (int i = 0; i < size(); i++) { 169 cloneArray[i] = elementData[i]; 170 } 171 return cloneArray; 172 173 } 174 175 // 14.移除指定区域的元素 包括开头,不包括结尾 176 @Override 177 public void removeRange(int fromIndex, int toIndex) { 178 Object[] temp = new Object[size() - (toIndex - fromIndex)]; 179 int j = 0; 180 for (int i = 0; i < size(); i++) { 181 182 if (i < fromIndex || i >= toIndex) { 183 temp[j] = elementData[i]; 184 j++; 185 } 186 } 187 188 elementData = temp; 189 } 190 191 // 15.没看懂要干什么,哈哈哈 192 @Override 193 public void merge(MyVector vector2) { 194 // TODO Auto-generated method stub 195 } 196 197 // 16.在指定位置插入指定元素 198 @Override 199 public void insertAt(int index, Object element) { 200 for (int i = size() - 1; i >= index; i--) { 201 elementData[i + 1] = elementData[i]; 202 } 203 elementData[index] = element; 204 size++; 205 } 206 207 // 17.将数组转化成字符串返回 208 @Override 209 public String toString() { 210 StringBuffer sb = new StringBuffer(); 211 for (int i = 0; i < size(); i++) { 212 sb.append(elementData[i]); 213 sb.append(","); 214 } 215 String substring = sb.substring(0); 216 return substring; 217 } 218 219 // 18.单元测试块 220 @Test 221 public void Test() { 222 append(20); 223 append(30); 224 append(40); 225 append(50); 226 append(60); 227 append(10); 228 append(70); 229 String strArray = toString(); 230 System.out.println(strArray); 231 } 232 }
时间: 2024-11-03 05:31:40