查找属于不同类实例的两个数组之间的并集

时间:2015-01-27 00:04:47

标签: java arrays class object

我在这里遇到麻烦。问题是:

  

两个集合的联合包括它们的内容组合成一个新集合。将方法联合添加到ADT包的接口BagInterface,作为新包返回接收呼叫的包的联合   方法和方法的一个参数包。包括足够的注释以完全指定方法。请注意,两个行李的并集可能包含重复项目。例如,如果对象x在一个中出现五次   袋子和另外两次,这些袋子的结合包含x七次。具体来说,假设bag1和bag2是Bag对象,Bag实现BagInterface; bag1包含String对象a,b和c;和bag2包含   String对象b,b,d和e。语句BagInterface之后的一切= bag1.union(bag2);执行时,包中的所有内容都包含字符串a,b,b,b,c,d和e。请注意,union不会影响内容   bag1和bag2。

基本上我有一个名为ResizableArrayClass的类,它在其数据字段中指定T [] bag,并且基本上可以根据类中的其他方法进行调整。 " union"的方法标题在我的界面中定义如下:

public BagInterface union(BagInterface anotherBag); 

通常,找到两个数组之间的联合将非常简单。但我试图找到两个数组之间的联合,这两个数组是两个独立对象(bag1,ResizableArrayClass的bag2)的一部分。我的问题是,在演示程序中使用以下语句时,我将如何以这种方式找到两个数组之间的联合:

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

界面(抱歉评论):

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

1 个答案:

答案 0 :(得分:0)

我认为您只能使用BagInterface方法:

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