0
点赞
收藏
分享

微信扫一扫

自定义ArrayList集合

/**
 * 自定义ArrayList集合
 * @author yancheng
 * @since 2022/2/9
 */
class NewArrayList<T> {

    private Object[] instance = new Object[0];

    private int size;

    public T get(int index) {
        if (index < 0 || index >= size)
            throw new IndexOutOfBoundsException();
        return (T) instance[index];
    }

    public void set(int index, T newInstance) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
        instance[index] = newInstance;
    }

    public void add(T newInstance) {
        instance = Arrays.copyOf(instance, size + 1);
        size++;
        instance[size - 1] = newInstance;
    }

    public T removeAt(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }

        T oldValue = (T) instance[index];

        if (index == size - 1) {
            instance[index] = null;
            size--;
            return oldValue;
        }

        for (int i = index; i < size; i++) {
            if (i < size - 1)
                instance[i] = instance[i + 1];
        }
        instance[size - 1] = null;
        size--;
        return oldValue;
    }

    public boolean remove(Object element) {
        if (element == null) {
            throw new IllegalArgumentException();
        }

        int index = -1;
        for (int i = 0; i < size; i++) {
            if (element.equals(instance[i])) {
                index = i;
            }
            if (index != -1 && i < size - 1)
                instance[i] = instance[i + 1];
        }
        if (index != -1) {
            instance[size - 1] = null;
            size--;
            return true;
        } else {
            return false;
        }

    }

    public void clear() {
        Arrays.fill(instance, null);
        size = 0;
    }

    public int size() {
        return size;
    }

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

    @Override
    public String toString() {
        if (instance == null)
            return "null";

        int iMax = instance.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            if (instance[i] != null)
                b.append(String.valueOf(instance[i]));
            if (i == iMax)
                return b.append(']').toString();
            if (instance[i] != null && i != size - 1)
                b.append(", ");
        }
    }
}

测试功能:

        val list = NewArrayList<String>()
        list.add("张三")
        list.add("李四")
        list.add("赵武")
        list.add("张飞")
        Log.i("cyc","list: ${list.javaClass.name + "@" + Integer.toHexString(list.hashCode())}")
        Log.i("cyc", "打印集合: $list")
        Log.i("cyc", "size: ${list.size()}")
        var c = false
        c = list.remove("张飞")
        var cc: Any? = null
        cc = list.removeAt(1)
        Log.i("cyc", "remove: $c")
        Log.i("cyc", "removeAt: $cc")
        Log.i("cyc", "打印集合: $list")
        Log.i("cyc", "size: ${list.size()}")
        list.clear()
        Log.i("cyc", "打印集合: $list")
        Log.i("cyc", "isEmpty: ${list.isEmpty}")
举报

相关推荐

0 条评论