我需要有关 Java 作业问题的帮助。我有两个袋子,比如说包含bag1
字符串A
、和 ,B
并且包含字符串、和。我需要为这两个包的联合编写一个 BagInterface ,然后是一个类调用。C
D
bag2
E
F
G
H
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);
}