使用java.util.Iterator <e>对象和while循环显示修改后的列表

时间:2017-09-24 20:51:15

标签: java iterator abstract

A)在MyList中定义以下方法,并在MyAbstractList中实现它。

// 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)

B)将代码添加到“TestMyArrayList.java”以证明此方法正常工作。

使用java.util.Iterator对象和while循环显示修改后的列表。

好的,所以我已经完成了A部分,我在B部分感到困惑。我不明白我的教授希望我做什么。我已经做了一些东西,但我很确定它不正确。有人知道我是否正确地做到了吗?

这是我的MyList类

package listapi;

/**
 * @author Y. Daniel Liang
 */
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();

public boolean addAll(MyList<E> otherList);
}

这是我的MyAbstractList类

package listapi;

/**
 * @author Y. Daniel Liang
 */
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
/**
 * Add a new element at the end of this list
 */
public void add(E e) {
    add(size, e);
}

@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.
 */
public boolean remove(E e) {
    if (indexOf(e) >= 0) {
        remove(indexOf(e));
        return true;
    } else {
        return false;
    }
}

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


}

这是我需要添加代码以通过使用java.lang.iterator和while循环显示修改后的列表来证明我的addAll方法正常工作的地方

package listapi;

/**
 * @author Y. Daniel Liang
 */
public class TestMyArrayList {

public TestMyArrayList() {
    // Create a list
    MyList<String> list = new MyArrayList<>();

    // Add elements to the list
    list.add("America"); // Add it to the list
    System.out.println("(1) " + list);

    list.add(0, "Canada"); // Add it to the beginning of the list
    System.out.println("(2) " + list);

    list.add("Russia"); // Add it to the end of the list
    System.out.println("(3) " + list);

    list.add("France"); // Add it to the end of the list
    System.out.println("(4) " + list);

    list.add(2, "Germany"); // Add it to the list at index 2
    System.out.println("(5) " + list);

    list.add(5, "Norway"); // Add it to the list at index 5
    System.out.println("(6) " + list);

    // Remove elements from the list
    list.remove("Canada"); // Same as list.remove(0) in this case
    System.out.println("(7) " + list);

    list.remove(2); // Remove the element at index 2
    System.out.println("(8) " + list);

    list.remove(list.size() - 1); // Remove the last element
    System.out.print("(9) " + list + "\n(10) ");

    for (String s : list) {
        System.out.print(s.toUpperCase() + " ");
    }
Iterator itr = list.iterator();
    while(itr.hasNext()){
        Object a = itr.next();
        System.out.println(a);

}

public static void main(String[] args){
    new MyArrayList();

}

}

0 个答案:

没有答案