本文介绍了Java中两个物品袋的联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要Java家庭作业问题的帮助。我有两个包,比如 bag1 包含字符串 A B C D bag2 包含字符串 E F G ħ。我需要为这两个包的联合写一个BagInterface然后一个类调用 ArrayBag< T>实现BagInterface< T>

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我在想这样的事情:

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

喜欢说如果我有这个

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


推荐答案

根据你的例子,

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

当你打电话时, union on bag1 传递 bag2 作为参数,

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

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

现在你可以写一些东西了。无需从此方法返回。 bag1 将使用union进行更新。

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

示例java代码如下所示:

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

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

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

这篇关于Java中两个物品袋的联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 07:44