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); } }