【问题标题】:Union of two object bags in JavaJava中两个对象包的联合
【发布时间】:2012-09-30 03:22:14
【问题描述】:

我需要帮助解决 Java 作业问题。我有两个包,比如说bag1,其中包含字符串A、B、C 和D 和bag2,其中包含字符串E、F、G 和@987654330我需要为这两个包的联合编写一个 BagInterface,然后是一个类调用ArrayBag<T> implements BagInterface<T>。

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 static void main(String[] args) {
    BagInterface bag1 = new ArrayBag(n);
    BagInterface bag2 = new ArrayBag(m);
    BagInterface<String> everything = bag1.union(bag2);
}

【问题讨论】:

  • union 是做什么的?它会创建一个新的BagInterface 实例吗?它是否执行参数与当前包的联合?
  • 是的,它应该创建一个新的 BagInterface 实例并将两个当前包的字符串添加到新包中。

标签: java object interface union bag


【解决方案1】:

根据你的例子,

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

当您调用时,union on bag1 传递 bag2 作为参数,

 this.bag -> represents bag1
 item in the argument represent 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;
 }

请注意:这是共享概念的伪代码(不是代码)。

示例java代码如下:

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

您还可以进一步使用List 方法contains 将代码简化为:

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

如果你不想更新bag1的内容,你应该有这样的方法:

       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 Everything = bag1.union(bag2); 的步骤。它执行包“一切”以包含两个包中的字符串。
  • 我用返回值的方法更新了答案。如果仍然没有帮助,请告诉我。
猜你喜欢
  • 2014-02-10
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
相关资源
最近更新 更多