简体   繁体   中英

Finding the union between two array that are part of different class instances

I am having trouble here. The question is:

The union of two collections consists of their contents combined into a new collection. Add a method union to the interface BagInterface for the ADT bag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method's one argument. Include sufficient comments to fully specify the method. Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. Specifically, suppose that bag1 and bag2 are Bag objects, where Bag implements BagInterface; bag1 contains the String objects a, b, and c; and bag2 contains the String objects b, b, d, and e. After the statement BagInterface everything = bag1.union(bag2); executes, the bag everything contains the strings a, b, b, b, c, d, and e. Note that union does not affect the contents of bag1 and bag2.

So essentially I have a class called ResizableArrayClass that specifies T[] bag in it's data field, and is essentially resizable per other methods within the class. The method header for "union" as defined in my interface is as follow's:

public BagInterface union(BagInterface anotherBag); 

Normally, finding the union between two array's would be very simple. But I am trying to find the union between two array's that are part of two separate objects (bag1, bag2 of ResizableArrayClass). My question is, how would I go about finding the union between two array's in this way when using the following statement in a demo program:

BagInterface<String> everything = bag1.union(bag2);

Interface (sorry for comments):

public int getCurrentSize();

    /** Sees whether this bag is empty.
         @return  True if the bag is empty, or false if not. */
    public boolean isEmpty();

    /** Adds a new entry to this bag.
        @param newEntry  The object to be added as a new entry.
        @return  True if the addition is successful, or false if not. */
    public boolean add(T newEntry);

    /** Removes one unspecified entry from this bag, if possible.
       @return  Either the removed entry, if the removal.
                was successful, or null. */
    public T remove();

    /** Removes one occurrence of a given entry from this bag.
       @param anEntry  The entry to be removed.
       @return  True if the removal was successful, or false if not. */
   public boolean remove(T anEntry);

    /** Removes all entries from this bag. */
    public void clear();

    /** Counts the number of times a given entry appears in this bag.
         @param anEntry  The entry to be counted.
         @return  The number of times anEntry appears in the bag. */    

    public int getFrequencyOf(T anEntry);

    /** Tests whether this bag contains a given entry.
         @param anEntry  The entry to locate.
         @return  True if the bag contains anEntry, or false if not. */
    public boolean contains(T anEntry);

    /** Retrieves all entries that are in this bag.
         @return  A newly allocated array of all the entries in the bag.
                Note: If the bag is empty, the returned array is empty. */
    public T[] toArray();
    //public <T> T[] toArray();  // Alternate
    //public Object[] toArray(); // Alternate

    /** Creates a new bag that combines the contents of this bag and anotherBag.
        @param anotherBag  The bag that is to be added.
        @return  A combined bag. */
    public BagInterface union(BagInterface anotherBag);

I think you could use BagInterface 's methods only:

public BagInterface<T> union(BagInterface<T> anotherBag) {
    BagInterface<T> result = new ResizableArrayClass<T>();
    T[] mine = this.toArray();
    for (T elem : mine) {
        result.add(elem);
    }
    T[] others = anotherBag.toArray();
    for (T elem : others) {
        result.add(elem);
    }
    return result;
}

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