import java.util.Arrays;
/**
* 简单模拟ArrayList
*/
public class MyArrayList {
private Object array[] ;
private int index; // 当前位置
private int size; // 设置初始总长度为10
public MyArrayList(){
this.index = 0;
this.size = 8;
array = new Object[size];
}
@Override
public String toString(){
return Arrays.toString(array);
}
/**
* 在末尾新增
* @param value
*/
public void insert(Object value){
if(index > size * 0.75){ // 需要扩容
this.size = size * 2 + 1;
Object newArray[] = new Object[size];
System.arraycopy(array, 0, newArray, 0, array.length); // 将原数组的所有值赋值到新的数组上
newArray[index++] = value; // 将新值放入到新数组的当前位置的下一位
this.array = newArray; // 将新数组赋给array
} else if (index == 0) { // 长度为空,存储到第一位
array[0] = value;
this.index++; // 当前存储的数据量加一
} else {
array[index++] = value; // 将值存储到index的下一位
}
}
/**
* 在指定位置新增
* @param value
*/
public void insert(int postion, Object value){
if(index > size * 0.75){ // 需要扩容
size = size * 2 + 1;
Object[] newArray = new Object[size];
System.arraycopy(array, 0, newArray, 0, array.length); // 将原数组的所有值赋值到新的数组上
for (int i = size - 1; i > postion;){
newArray[i] = newArray[--i]; // 后移操作
}
newArray[postion++] = value; // 将新值放入到新数组的当前位置的下一位
this.array = newArray; // 将新数组赋给array
} else {
for (int i = size - 1; i > postion;){
array[i] = array[--i]; // 后移操作
}
array[postion] = value; // 将值存储到index的下一位
}
index++;
}
/**
* 删除指定位置的元素
* @param postion
*/
public void delete(int postion){
for (int i = postion; i < index; i++){
array[postion] = array[++postion];
}
this.index--; // 当前list长度减一
}
/**
* 更新指定位置的元素
* @param postion
* @param value
*/
public void update(int postion, Object value){
array[postion] = value;
}
/**
* 查询指定位置的值
* @param postion
* @return
*/
public Object query(int postion){
return array[postion];
}
}
class TestMyArrayList{
public static void main(String[] args) {
MyArrayList myArrayListTest = new MyArrayList();
// 测试末尾增加元素并扩容
myArrayListTest.insert("a");
myArrayListTest.insert("b");
myArrayListTest.insert("c");
myArrayListTest.insert("d");
myArrayListTest.insert("e");
myArrayListTest.insert("f");
myArrayListTest.insert("g");
myArrayListTest.insert("h");
System.out.println(myArrayListTest.toString());
// 测试中间增加元素并扩容
myArrayListTest.insert(2,"cc");
myArrayListTest.insert(4,"dd");
myArrayListTest.insert(6,"ee");
myArrayListTest.insert(8,"ff");
myArrayListTest.insert(10,"gg");
myArrayListTest.insert(12,"hh");
System.out.println(myArrayListTest.toString());
// 删除指定位置元素
myArrayListTest.delete(10);
System.out.println(myArrayListTest.toString());
// 更新指定位置元素
myArrayListTest.update(3,"ccc");
System.out.println(myArrayListTest.toString());
// 查询指定位置元素
System.out.println(myArrayListTest.query(3));
}
}
执行结果:
[a, b, c, d, e, f, g, h, null, null, null, null, null, null, null, null, null]
[a, b, cc, c, dd, d, ee, e, ff, f, gg, g, hh, h, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
[a, b, cc, c, dd, d, ee, e, ff, f, g, hh, h, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
[a, b, cc, ccc, dd, d, ee, e, ff, f, g, hh, h, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
ccc