数据结构--ArrayList的实现

此文章仅作为自己学习过程中的记录和总结,同时会有意地去用英文来做笔记,一些术语的英译不太准确,内容如有错漏也请多指教,谢谢!


Implementations of related methods of ArrayList:

- ArrayList属性和构造方法:

package list.demo;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @param <T> 
 * @author Zay
 * @version 2021/1/4 16:51
 */
public class ArrayList_demo<T> implements List<T> {

    int size;                    // keeps track of the number of elements

    private T[] array;           // stores the elements

    /**
     * Instantiate an array of T[] and initialize its size.
     */
    public ArrayList_demo() {
        // Cannot instantiate an array of T[], but you can instantiate an
        // array of Object and then typecast it.
        array = (T[]) new Object[10];
        size = 0;
    }
    
}

- ArrayList的相关方法:

  • public boolean add(T element)
    /**
     * Appends the specified element to the end of this list.
     *
     * @param element The specified element.
     */
    @Override
    public boolean add(T element) {
        // If the current size is full, copy the whole list to another new bigger list.
        if (size >= array.length) {
            T[] newList = (T[]) new Object[array.length * 2];
            System.arraycopy(array, 0, newList, 0, array.length);
            array = newList;
        }
        array[size++] = element;
        return true;
    }
  • public void add(int index, T element)
/**
     * Inserts the specified element at the specified position in this list.
     *
     * @param index   The index of the specified position in this list.
     * @param element The specified element.
     */
    @Override
    public void add(int index, T element) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException();
        }
        // add the element to get the resizing
        add(element);
        // shift the elements
        for (int i = size - 1; i > index; i--) {
            array[i] = array[i - 1];
        }
        // put the new one in the right place
        array[index] = element;
    }
  • public boolean addAll(Collection<? extends T> collection)
/**
     * Appends all of the elements in the specified collection to the end of this list,
     * in the order that they are returned by the specified collection's Iterator.
     *
     * @param collection The specified collection.
     * @return Whether adding successfully or not.
     */
    @Override
    public boolean addAll(Collection<? extends T> collection) {
        boolean flag = true;
        for (T element : collection) {
            flag &= add(element);
        }
        return flag;
    }
  • public boolean remove(Object obj)
    /**
     * Removes the first occurrence of the specified element from this list, if it is present.
     *
     * @param obj The first occurrence of the specified element from this list, if it is present.
     * @return Whether removing successfully or not.
     */
    @Override
    public boolean remove(Object obj) {
        int index = indexOf(obj);
        if (index == -1) {
            return false;
        }
        remove(index);
        return true;
    }
  • public T remove(int index)
    /**
     * Removes the element at the specified position in this list.
     *
     * @param index The index of the specified position.
     * @return The removed element.
     */
    @Override
    public T remove(int index) {
        T element = get(index);
        for (int i = index; i < size - 1; i++) {
            array[i] = array[i + 1];
        }
        size--;
        return element;
    }
  • public boolean removeAll(Collection<?> collection)
   /**
     * Removes from this list all of its elements that are contained in the specified collection.
     *
     * @param collection The specified collection.
     * @return Whether removing successfully or not.
     */
    @Override
    public boolean removeAll(Collection<?> collection) {
        boolean flag = true;
        for (Object obj : collection) {
            flag &= remove(obj);
        }
        return flag;
    }
  • public void clear()
    /**
     * Removes all of the elements from this list.
     */
    @Override
    public void clear() {
        // note: this version does not actually null out the references
        // in the array, so it might delay garbage collection.
        size = 0;
    }
  • public boolean contains(Object obj)
   /**
     * Returns true if this list contains the specified element.
     *
     * @param obj The specified element.
     * @return Whether the arraylist contains the specified element.
     */
    @Override
    public boolean contains(Object obj) {
        return indexOf(obj) != -1;
    }
  • public boolean containsAll(Collection<?> collection)
    /**
     * Judge whether the arraylist contains all the elements in the specified collection.
     *
     * @param collection The specified collection to be judged.
     * @return Whether the arraylist contains all the elements in the specified collection.
     */
    @Override
    public boolean containsAll(Collection<?> collection) {
        for (Object element : collection) {
            if (!contains(element)) {
                return false;
            }
        }
        return true;
    }
  • public T get(int index)
    /**
     * Returns the element at the specified position in this list.
     *
     * @param index The index of the specified element.
     * @return The element at the specified position in this list.
     */
    @Override
    public T get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
        return array[index];
    }

-public int indexOf(Object target)

    /**
     * Returns the index of the first occurrence of the specified element in this list,
     * or -1 if this list does not contain the element.
     *
     * @param target The specified element to be found.
     * @return The index of the first occurrence of the specified element in this list,
     * or -1 if this list does not contain the element.
     */
    @Override
    public int indexOf(Object target) {
        for (int i = 0; i < size; i++) {
            if (equals(target, array[i])) {
                return i;
            }
        }
        return -1;
    }
  • public int lastIndexOf(Object target)
   /**
     * Returns the index of the last occurrence of the specified element in this list,
     * or -1 if this list does not contain the element.
     *
     * @param target The specified element.
     * @return The index of the last occurrence of the specified element in this list.
     */
    @Override
    public int lastIndexOf(Object target) {
        // see notes on indexOf
        for (int i = size - 1; i >= 0; i--) {
            if (equals(target, array[i])) {
                return i;
            }
        }
        return -1;
    }
  • private boolean equals(Object target, Object element)
    /**
     * Checks whether an element of the array is the target.
     * <p>
     * Handles the special case that the target is null.
     *
     * @param target
     * @param element
     */
    private boolean equals(Object target, Object element) {
        if (target == null) {
            return element == null;
        } else {
            return target.equals(element);
        }
    }
  • public boolean isEmpty()
    /**
     * Returns true if this list contains no elements.
     *
     * @return Whether the arraylist contains elements or not.
     */
    @Override
    public boolean isEmpty() {
        return size == 0;
    }
  • public Iterator iterator()
    /**
     * Returns an iterator over the elements in this list (in proper sequence).
     *
     * @return An iterator over the elements in this list.
     */
    @Override
    public Iterator<T> iterator() {
        // make a copy of the array
        T[] copy = Arrays.copyOf(array, size);
        // make a list and return an iterator
        return Arrays.asList(copy).iterator();
    }
  • public ListIterator listIterator()
    /**
     * Returns a list iterator over the elements in this list (in proper sequence).
     *
     * @return A list iterator over the elements in this list.
     */
    @Override
    public ListIterator<T> listIterator() {
        // make a copy of the array
        T[] copy = Arrays.copyOf(array, size);
        // make a list and return an iterator
        return Arrays.asList(copy).listIterator();
    }
  • public ListIterator listIterator(int index)
    /**
     * Returns a list iterator over the elements in this list (in proper sequence),
     * starting at the specified position in the list.
     *
     * @param index The index of the specified position in the list.
     * @return A list iterator over the elements in this list.
     */
    @Override
    public ListIterator<T> listIterator(int index) {
        // make a copy of the array
        T[] copy = Arrays.copyOf(array, size);
        // make a list and return an iterator
        return Arrays.asList(copy).listIterator(index);
    }
  • public T set(int index, T element)
    /**
     * Replaces the element at the specified position in this list with the specified element.
     *
     * @param index   The index of the specified position.
     * @param element The specified element.
     * @return The element previously at the specified position.
     */
    @Override
    public T set(int index, T element) {
        T previous = get(index);
        array[index] = element;
        return previous;
    }
  • public int size()
    /**
     * Returns the number of elements in this list.
     *
     * @return The number of elements in this list.
     */
    @Override
    public int size() {
        return size;
    }
  • public List subList(int fromIndex, int toIndex)
    /**
     * Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
     * (If fromIndex and toIndex are equal, the returned list is empty.)
     * The returned list is backed by this list,
     * so non-structural changes in the returned list are reflected in this list,
     * and vice-versa. The returned list supports all of the optional list operations.
     *
     * @param fromIndex Low endpoint (inclusive) of the subList.
     * @param toIndex   High endpoint (exclusive) of the subList.
     * @return A view of the specified range within this list.
     */
    @Override
    public List<T> subList(int fromIndex, int toIndex) {
        if (fromIndex < 0 || toIndex >= size || fromIndex > toIndex) {
            throw new IndexOutOfBoundsException();
        }
        T[] copy = Arrays.copyOfRange(array, fromIndex, toIndex);
        return Arrays.asList(copy);
    }
  • public Object[] toArray()
    /**
     * Returns an array containing all of the elements in this list in proper sequence (from first to last element).
     *
     * @return An array containing all of the elements in this list in proper sequence.
     */
    @Override
    public Object[] toArray() {
        return Arrays.copyOf(array, size);
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值