繁体   English   中英

最好的方法来实现retainAll()方法

[英]Best way to implement retainAll() method

我有这个自定义类名为MyAbstractList它实现MyList接口。 这是代码:

public abstract class MyAbstractList<E> implements MyList<E> {
    protected int size = 0; // The size of the list

    protected MyAbstractList() {
    }

    protected MyAbstractList(E[] objects) {
        for (int i = 0; i < objects.length; i++)
            add(objects[i]);
    }

    public void add(E e) {
        add(size, e);
    }

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

    public int size() {
        return size;
    }

    public boolean addAll(MyList<E> otherList) {
        for (E e : otherList) {
            add(e);
        }
        if (otherList.size() > 0)
            return true;
        return false;
    }

    public boolean removeAll(MyList<E> otherList) {
        boolean removed = false;
        for (E e : otherList) {
            if (remove(e) && !removed)
                removed = true;
        }
        return removed;
    }

    public boolean remove(E e) {
        if (indexOf(e) >= 0) {
            remove(indexOf(e));
            return true;
        } else
            return false;
    }

    /** Retains the elements in this list that are also in otherList
     *  Returns true if this list changed as a result of the call      */
    public boolean retainAll(MyList<E> otherList) {

    }
}

如何实现retainAll()方法?

MyList界面:

public interface MyList<E> extends java.lang.Iterable<E> {
  /** Add a new element at the end of this list */
  public void add(E e);

  /** Add a new element at the specified index in this list */
  public void add(int index, E e);

  /** Clear the list */
  public void clear();

  /** Return true if this list contains the element */
  public boolean contains(E e);

  /** Return the element from this list at the specified index */
  public E get(int index);

  /** Return the index of the first matching element in this list.
   *  Return -1 if no match. */
  public int indexOf(E e);

  /** Return true if this list contains no elements */
  public boolean isEmpty();

  /** Return the index of the last matching element in this list
   *  Return -1 if no match. */
  public int lastIndexOf(E e);

  /** Remove the first occurrence of the element o from this list.
   *  Shift any subsequent elements to the left.
   *  Return true if the element is removed. */
  public boolean remove(E e);

  /** Remove the element at the specified position in this list
   *  Shift any subsequent elements to the left.
   *  Return the element that was removed from the list. */
  public E remove(int index);

  /** Replace the element at the specified position in this list
   *  with the specified element and returns the new set. */
  public Object set(int index, E e);

  /** Return the number of elements in this list */
  public int size();

  /** Adds the elements in otherList to this list.
  * Returns true if this list changed as a result of the call */
  public boolean addAll(MyList<E> otherList);

  /** Removes all the elements in otherList from this list
  * Returns true if this list changed as a result of the call */
  public boolean removeAll(MyList<E> otherList);

  /** Retains the elements in this list that are also in otherList
  * Returns true if this list changed as a result of the call */
  public boolean retainAll(MyList<E> otherList);

  /** Return an iterator for the list */
  public java.util.Iterator<E> iterator();
}

如果元素不可Comparable ,则只能搜索参数中不存在的列表元素。

public boolean retainAll(MyList<E> otherList) {
    boolean changed = false;
    for (int i = size() - 1; i >= 0; i--) {
        Object obj = get(i);
        if (!otherList.contains(obj)) {
            remove(i);
            changed = true;
        }
    }
    return changed;
}

注意:此算法在O(n ^ 2)中完成,如果您有Comparable列表,则可以转到O(n log(n))

第二个注意事项:不要使用迭代器来循环列表,因为列表内容的更改可能会引发Exception。


对Saud建议的编辑的评论: 无需更新size 这必须通过方法remove来完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM