简单实现ArrayList功能

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值