Java中两个对象包的并集

Java中两个对象包的并集,java,object,interface,union,bag,Java,Object,Interface,Union,Bag,我需要一个Java作业问题的帮助。我有两个袋子,比如说bag1里面装着字符串A,B,C和D,bag2里面装着字符串E,F,G和H。我需要为这两个包的联合编写一个BagInterface,然后一个类调用ArrayBag实现BagInterface 我当时想的是这样的: public interface BagInterface<T> { public T union(T[] item); } public class ArrayBag<T> implements

我需要一个Java作业问题的帮助。我有两个袋子,比如说
bag1
里面装着字符串
A
B
C
D
bag2
里面装着字符串
E
F
G
H
。我需要为这两个包的联合编写一个BagInterface,然后一个类调用
ArrayBag实现BagInterface

我当时想的是这样的:

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
    }
}
       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;
     }
公共接口BagInterface{
公共T联盟(T[]项);
}
公共类ArrayBag实现了BagInterface{
私有最终静态int默认值_CAP=4;
私人住宅;
私人旅行袋;
公共ArrayBagR(内部cap){
bag=(T[])新对象[cap];
这个数值等于0;
}
公共T联合体(T[]项){
//不知道该怎么写才能通过考试
//参数中的另一个类对象
//比如说,如果我写一个main来运行它,我可以
//做一些类似Bag1.union(Bag2)的事情
//然后得到类似于B C D E F G H的东西
}
}
比如说如果我有这个

public static void main(String[] args) {
    BagInterface bag1 = new ArrayBag(n);
    BagInterface bag2 = new ArrayBag(m);
    BagInterface<String> everything = bag1.union(bag2);
}
publicstaticvoidmain(字符串[]args){
bag1=新阵列Bag(n);
bag2=新阵列Bag(m);
BagInterface everything=bag1.union(bag2);
}
根据您的示例

BagInterface bag1 = new ArrayBag(n);
BagInterface bag2 = new ArrayBag(m);
BagInterface<T> everything =  bag1.union(bag2);
现在你可以写一些东西了。不需要从此方法返回
bag1
将使用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;
 }
如果您不想更新
bag1
内容,您应该有如下方法:

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
    }
}
       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;
     }
公共接口联合(T[]项2){ BigInterface everything=新建BagArray(); List unionList=Arrays.asList(this.bag); 对于(T元素:项目){ 如果(!unionList.contains(elem)){ 联合列表。添加(elem); } } everything.setBags(unionList.toArray(新包[unionList.size()]); 归还一切; }
工会做什么?它是否创建了一个新的
BagInterface
实例?它是否执行参数与当前包的并集?是的,它应该创建一个新的BagInterface实例,并将两个当前包的字符串添加到新包中。对不起,我真的很想理解这一点,但我就是不能。我试着让它进入到书中显示的BagInterface everything=bag1.union(bag2)的步骤;它执行包“everything”以包含来自两个包的字符串。我使用方法更新答案以返回值。如果仍然没有帮助,请告诉我。
       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;
     }