看看我的Java Generic的通配符

时间:2015-09-12 18:52:24

标签: java

在下面的代码中,我不知道我是否正确编码了public boolean addAll(SimpleSet<? extends E> s) {的最后一个方法 你能看看它是否正确(请向我解释(SimpleSet<? extends E> s)如何运作)并查看其他方法!

package set;
    import java.util.ArrayList;
    import java.util.Iterator;

public class ArraySet<E> implements SimpleSet<E> {
    private ArrayList<E> data=new ArrayList<E>();

    /**
     * Constructs a new empty set.
     */
    public ArraySet() {

    }

    /** 
     * Adds the specified element to this set, if it is not already present.
     * post: x is added to the set if it is not already present
     * @param  x the element to be added
     * @return true if the specified element was added
     */
    public boolean add(E x) {
        for(int i=0; i<data.size(); i++){
            if(data.get(i).equals(x)){
                System.out.println("x is already added");
            } else {
                data.add(x);
                System.out.println("x is added to the set");

                return true;
            }
        }
        return false;
    }

    /** 
     * Removes the specified element from this set if it is present. 
     * post:    x is removed if it was present
     * @param   x the element to remove - if present
     * @return true if the set contained the specified element
     */
    public boolean remove(Object x) {   
        for(int i=0; i<data.size(); i++){
            if(data.get(i).equals(x)){
                data.remove(x);
                return false;
            }
    }
        return true;
    }

    /** 
     * Returns true if this set contains the specified element.
     * @param   x the element whose presence is to be tested
     * @return  true if this set contains the specified element
     */
    public boolean contains(Object x) {     
        for(int i=0; i<data.size(); i++){
            if(data.get(i).equals(x)){
                return true;
            }
    }       
        return false;
    }


    /** 
     * Returns true if this set contains no elements.
     * @return true if this set contains no elements
     */
    public boolean isEmpty() {  
        if(data.size()==0){
            return true;
        }
        return false;
    }

    /** 
     * Returns the number of elements in this set.
     * @return the number of elements in this set
     */
    public int size() {
        return data.size();
    }

    /** 
     * Returns an iterator over the elements in this set.
     * @return an iterator over the elements in this set
     */
    public Iterator<E> iterator() {
        return this.iterator();

    }

    /**
    * Adds all of the elements in the specified set, for which it is
    * possible, to this set.
    * post: all elements, for which it is possible, in the
    * specified set are added to this set.
    * @return true if this set changed as a result of the call
    */
    public boolean addAll(SimpleSet<? extends E> s) {
        for(E e : s){
            data.add(e);
            System.out.println("all elements, for which it is possible, in the specified set are added to this set. ");
            return true;
        }
        return false;
    }

}

2 个答案:

答案 0 :(得分:1)

“扩展SomeClass”的基础,是“?”代表SomeClass实例的对象,或SomeClass的任何子类。

你的addAll唯一的问题是它接受“SimpleSet&lt;?extends E&gt;”而Collection.addAll的定义需要“Collection&lt;?extends E&gt;”。您通过要求Collection(“SimpleSet”)的特定子类而不是任何类型的Collection来破坏接口契约。这可能是编译时错误。

答案 1 :(得分:0)

您的代码存在一些问题:

  1. 你的循环永远不会超越第一次迭代,因为你在第一次迭代中返回true!
  2. 迭代器方法中有无限递归。它只是自称!