Union of two object bags in Java

谁说胖子不能爱 提交于 2019-12-10 18:16:58

问题


I need help with a Java homework problem. I have two bags, say bag1 containing the strings A, B, C and D and bag2 containing strings E, F, G and H. I need to write a BagInterface for the union of those two bag then a class call ArrayBag<T> implements BagInterface<T>.

BagInterface I was thinking something like this:

public interface BagInterface<T> {

    public T union(T[] item);
}

public class ArrayBag<T> implements BagInterface<T> {

    private final static int DEFAULT_CAP = 4;
    private int numElements;
    private T[] bag;

    public ArrayBagR(int cap) {
        bag = (T[]) new Object[cap];
        this.numElements = 0;
    }

    public T union(T[] item) {

        // Not sure how I should write this so I can pass
        // another class object in the parameter

        // Like say if I write a main to run this I could
        // do something like Bag1.union(Bag2)
        // and get something like A B C D E F G H
    }
}

Like say if I have this

public static void main(String[] args) {
    BagInterface bag1 = new ArrayBag(n);
    BagInterface bag2 = new ArrayBag(m);
    BagInterface<String> everything = bag1.union(bag2);
}

回答1:


Per your example,

BagInterface bag1 = new ArrayBag(n);
BagInterface bag2 = new ArrayBag(m);
BagInterface<T> everything =  bag1.union(bag2);

When you call, union on bag1 passing bag2 as argument,

 this.bag -> represents bag1
 item in the argument represent bag2

Now you can write something line. No need to return from this method. bag1 will be updated with union.

 public BagInterface<T> union(T[] item) {
    T[] everything = thi.bag;
    for(T elem: item){
       if(not(this.bag contains elem )){
          everything  -> add(elem);
       }
    }
    return this;
 }

Please note: This is a pseudo code to share the concept (not a code).

A sample java code can be like below:

 public BagInterface<T>  union(T[] item) {
        List<T> unionList = Arrays.asList(this.bag);
        for(T elem: item){
           boolean present = false;
           for(T elem1: this.bag){
              if(elem1.equals(elem)){
                  present = true;
              }
           }
           if(!present){
               unionList.add(elem);
           }
        }
       this.bag = unionList.toArray(new Bag[unionList.size()]);
       return this;
     }

Also you can further use List method contains to simplify the code as :

       public BagInterface<T> union(T[] item) {
        List<T> unionList = Arrays.asList(this.bag);
        for(T elem: item){
           if(!unionList.contains(elem)){
               unionList.add(elem);
           }
        }
        this.bag = unionList.toArray(new Bag[unionList.size()]);
        return this;
     }

If you don't want to update bag1 contents, you should have method like this:

       public BagInterface<T> union(T[] item2) {
                BigInterface<T> everything = new BagArray<T>();
        List<T> unionList = Arrays.asList(this.bag);
        for(T elem: item){
           if(!unionList.contains(elem)){
               unionList.add(elem);
           }
        }
        everything.setBags(unionList.toArray(new Bag[unionList.size()]));
        return everything;
     }


来源:https://stackoverflow.com/questions/12806064/union-of-two-object-bags-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!