栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

顺序表方法的实现,范式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

顺序表方法的实现,范式

package about_list;

import java.util.*;

//MyArrayList是一个泛型,E代表一个数据类型变量。
//implement List,实现一个接口,E也是数据类型变量,其取决区MyArrayList传入的类型。
public class MyArrayList implements List {
    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 iterator() {
        throw new UnsupportedOperationException();
    }

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

    @Override
    public  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 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 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 listIterator() {
        throw new UnsupportedOperationException();
    }

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

    @Override//o(n)
    public List subList(int fromIndex, int toIndex) {
        //throw new UnsupportedOperationException();
        //没有浅拷贝
        if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) {
            throw new ArrayIndexOutOfBoundsException();
        }
            List sublist = new ArrayList<>();
            for (int i = fromIndex; i < toIndex; i++) {
                sublist.add(get(i));
            }
            return sublist;
        }
    //o(n)
    public void ensuerCapacity(){
      if(size
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763607.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号