手写ArrayList实例

效果

在这里插入图片描述

代码块:

package com.example.javase;

/**
 * @Description: ArrayList demo
 * @author: YZD
 * @Date: 2020-02-11 15:48
 * @Version: 1.0
 */
public class arrayListDemo<E> {
    /**
     * 特点:
     * 查询效率高(index)
     * 增删效率低(System.arraycopy)
     * 线程不安全
     * <p>
     * 数组扩容
     */

    private Object[] elementData;
    private int size = 0;

    //默认长度
    private static final int DEAFULT_CAPACITY = 10;

    public arrayListDemo() {
        elementData = new Object[DEAFULT_CAPACITY];
    }

    public Object[] getElementData() {
        return elementData;
    }

    public void setElementData(Object[] elementData) {
        this.elementData = elementData;
    }

    public void add(E object) {
        //扩容
        if (size == elementData.length) {
            Object[] newObj = new Object[size + (size >> 1)];
            System.arraycopy(elementData, 0, newObj, 0, elementData.length);
            elementData = newObj;
        }
        elementData[size++] = object;
    }

    public Object get(int index) throws Exception {
        check(index);
        return elementData[index];
    }

    public void set(E e, int index) throws Exception {
        check(index);
        elementData[index] = e;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }


    public void remove(E e) throws Exception {
        for (int i = 0; i < size; i++) {
            if (e.equals(get(i))) {
                // 删除
                remove(i);
            }
        }
    }

    public void remove(int index) throws Exception {
        check(index);
        System.arraycopy(elementData, index + 1, elementData, index, elementData.length - index - 1);
        elementData[--size] = null;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        int count = 0;
        for (int i = 0; i < elementData.length; i++) {
            if (elementData[i] != null) {
                count += elementData[i].toString().length() + 1;
                stringBuilder.append(elementData[i]).append(",");
            }
        }
        stringBuilder.setCharAt(count, ']');
        return stringBuilder.toString();
    }

    private void check(int index) throws Exception {
        if (index < 0 || index > size) {
            throw new Exception("索引不合法");
        }
    }

    public static void main(String[] args) throws Exception {
        arrayListDemo list = new arrayListDemo();
        list.add("a");
        list.add("b");
        list.add("c");
        System.out.println(list.toString());
//        list.set("set", 0);
        System.out.println(list.get(0));
        list.remove(1);
        list.remove("a");
        System.out.println(list.toString());

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳宗德

您的鼓励是我进步的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值