顺序表方法的实现,范式

本文详细介绍了Java中自定义的MyArrayList类,它作为List接口的实现,支持基本的增删查改操作,包括元素计数、判断空闲、查找、添加、删除等,并探讨了容量管理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package about_list;

import java.util.*;

//MyArrayList是一个泛型,E代表一个数据类型变量。
//implement List<E>,实现一个接口,E也是数据类型变量,其取决区MyArrayList传入的类型。
public class MyArrayList<E> implements List<E> {
    public int size;
    public E[] array;

    public MyArrayList() {
        array = (E[]) new Object[16];
        size = 0;
    }

    @Override//O(1)
    public int size() {
        // throw new UnsupportedOperationException();
        return size;
    }

    @Override
    public boolean isEmpty() {
        //throw new UnsupportedOperationException();
        return size == 0;
    }

    @Override
    public boolean contains(Object o) {
        //throw new UnsupportedOperationException();;
        if (o == null) {
            for (int i = 0; i < size; i++) {
                if (array[i] == o) {
                    return true;
                }
            }
            return false;
        } else {
            for (int i = 0; i < size; i++) {
                if (o.equals(array[i])) {//不能确定array【i】全部都不是null//contain要用equals
                    return true;
                }
            }
            return false;
        }
    }

    @Override
    public Iterator<E> iterator() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object[] toArray() {
        throw new UnsupportedOperationException();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        throw new UnsupportedOperationException();
    }

    @Override//o(1)
    public boolean add(E e) {//尾插
        //throw new UnsupportedOperationException();
        ensuerCapacity();
        array[size++] = e;
        return true;
    }

    @Override//o(n)
    public boolean remove(Object o) {//删除
        // throw new UnsupportedOperationException();
        if (o == null) {
            for (int i = 0; i < size; i++) {
                if (array[i] == o) {
                    System.arraycopy(array, i + 1, array, i, size - i - 1);
                    array[--size] = null;
                    return true;
                }
            }
            return false;
        } else {
            for (int i = 0; i < size; i++) {
                if (o.equals(array[i])) {
                    System.arraycopy(array, i + 1, array, i, size - i - 1);
                    array[--size] = null;
                    return true;
                }
            }
            return false;
        }
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    @Override//最坏的结果o(n)*o(m)
    //平均的结果o(m)
    // m是c中元素个数
    //list的元素
    public boolean addAll(Collection<? extends E> c) {
        //throw new UnsupportedOperationException();
        //遍历c中所有元素,尾插到this list。
        //1、按照迭代器方法,遍历c中元素。
        for (E e : c) {
            //尾插到list
            add(e);
        }
        return true;
    }

    @Override
    //o(n)*o(m)
    // m是c中元素个数
    //list的元素
    public boolean addAll(int index, Collection<? extends E> c) {
        //throw new UnsupportedOperationException();
        for (E e : c) {
            add(index, e);
        }
        return true;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        //throw new UnsupportedOperationException();
        Arrays.fill(array, null);//把array中的元素全置换成null,
        // 使对象可以被回收,否则对象不能被应用,叫对象泄露。(内存泄漏)
        size = 0;
    }

    @Override//o(1)
    public E get(int index) {
        //throw new UnsupportedOperationException();
        if (index < 0 && index > size) {
            throw new ArrayIndexOutOfBoundsException(String.format("size=%d,index=%d", size, index));
        }
        return array[index];
    }

    @Override//o(1)
    public E set(int index, E element) {
        //throw new UnsupportedOperationException();
        if (index < 0 && index > size) {
            throw new ArrayIndexOutOfBoundsException(String.format("size=%d,index=%d", size, index));
        }
        E oldValue = array[index];
        array[index] = element;
        return oldValue;
    }

    @Override//o(n)
    public void add(int index, E element) {
        // throw new UnsupportedOperationException();size
        if (index < 0 && index > size) {
            throw new ArrayIndexOutOfBoundsException(String.format("size=%d,index=%d", size, index));
        }
        ensuerCapacity();
        //index的后面元素全部后移
        System.arraycopy(array, index, array, index + 1, size - 1);
        array[index] = element;
        size++;
    }

    @Override//o(n)
    public E remove(int index) {
        //throw new UnsupportedOperationException();
        if (index < 0 && index > size) {
            throw new ArrayIndexOutOfBoundsException(String.format("size=%d,index=%d", size, index));
        }
        E oldValue = array[index];
        System.arraycopy(array, index + 1, array, index, size - 1 - index);
        array[size] = null;
        size--;
        return oldValue;
    }

    @Override//o(n)
    public int indexOf(Object o) {
        // throw new UnsupportedOperationException();
        if (o == null) {
            for (int i = 0; i < size; i++) {
                if (array[i] == o) {
                    return i;
                }
            }
            return -1;
        } else {
            for (int i = 0; i < size; i++) {
                if (o.equals(array[i])) {
                    return i;
                }
            }
            return -1;
        }
    }

    @Override//o(n)
    public int lastIndexOf(Object o) {
        //throw new UnsupportedOperationException();
        if (o == null) {
            for (int i = size - 1; i >= 0; i--) {
                if (array[i] == o) {
                    return i;
                }
            }
            return -1;
        } else {
            for (int i = size - 1; i >= 0; i--) {
                if (o.equals(array[i])) {
                    return i;
                }
            }
            return -1;
        }
    }


    @Override
    public ListIterator<E> listIterator() {
        throw new UnsupportedOperationException();
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        throw new UnsupportedOperationException();
    }

    @Override//o(n)
    public List<E> subList(int fromIndex, int toIndex) {
        //throw new UnsupportedOperationException();
        //没有浅拷贝
        if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) {
            throw new ArrayIndexOutOfBoundsException();
        }
            List<E> sublist = new ArrayList<>();
            for (int i = fromIndex; i < toIndex; i++) {
                sublist.add(get(i));
            }
            return sublist;
        }
    //o(n)
    public void ensuerCapacity(){
      if(size<array.length){
          return;
      }
      array=Arrays.copyOf(array,array.length*2);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值