避免多次通话意味着什么?

时间:2016-03-19 19:22:03

标签: java list

所以我在一个算法类中,我被要求重写一个方法,以避免在myAbstractList类中使用remove(E e)方法时多次调用indexOf(E e)。的java。

  

1.重写MyAbstractList类的方法remove(E e),以避免多次调用indexOf(E e)。

我真的不是要求任何人为我编写/完成我的项目。老实说,如果我愿意的话,我可以从内存中重新编写这两个类。

在我看来,indexOf(E e)无论如何都不会被称为多次次,所以我无法真正看到我将如何改变它。我想如果有人能告诉我如何多次调用该方法会有所帮助吗?

代码

MyAbstractList:

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

/**Create a default list*/
protected MyAbstractList() {
}

/**Create a list from an array of objects*/
protected MyAbstractList(E[] objects) {
    for (int i = 0; i < objects.length; i++) {
        add(objects[i]);
    }
}

@Override
/**Return true if this list contains no elements*/
public boolean isEmpty() {
    return size == 0;
}

@Override
/**Return the number of elements in this list*/
public int size() {
    return size;
}

@Override
// **************************************************************
// **************************************************************
/**Remove the first occurrence of the element e from this list. Shift any
 * subsequent elements to the left. Return true if the element is removed.
 * Must be overridden to avoid multiple calls to indexOf.
 */
// **************************************************************
// **************************************************************
 public boolean remove(E e) {
    if (indexOf(e) >= 0) {
        remove(indexOf(e));
        return true;
    } else {
        return false;
    }
 }
}

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

2 个答案:

答案 0 :(得分:2)

div id="alttext-container"

所以你在remove方法中有2次调用indexOf()。将值提取到局部变量以避免多次调用。

像这样:

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

答案 1 :(得分:1)

方法remove目前两次调用indexOf。将结果保存在局部变量中并调用一次。像,

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