简体   繁体   中英

check out my Java Generic's Wildcard

In the below code , I do not know if I coded correctly the last method which is public boolean addAll(SimpleSet<? extends E> s) { Can you check it out if it is correct (please explain to me how (SimpleSet<? extends E> s) works) and look at the other methods as well !

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

}

The basic of "? extends SomeClass", is that the "?" stands in for objects that are instances of SomeClass, or any subclass of SomeClass.

The only problem with your addAll is that it accepts "SimpleSet<? extends E>" whereas the definition of Collection.addAll expects "Collection<? extends E>". You are breaking the interface contract by requiring a specific subclass of Collection ("SimpleSet") rather than any kind of Collection. This is probably a compile time error.

There are several problems with your code:

  1. your loop will never go beyond the first iteration because you are returning true in the first iteration!
  2. You have an infinite recursion in your iterator method. It just calls itself!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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