I am not sure how to describe this in title, I am a beginner of Java, here is a little sample code:
My target is similar to this question: Union of two object bags in Java, the difference is in the parameter, the solution provided in this question has a T[] item
, in my case, it is BagInterface<T> anotherBag
Interface: http://people.cs.pitt.edu/~ramirez/cs445/handouts/BagInterface.java
ArrayBag.java: in union(), I wish to add all items up in the 2 bags (sets of data) to 1.
public class ArrayBag<T> implements BagInterface<T>
{
private int length;
...
/* union of 2 bags */
public BagInterface<T> union(BagInterface<T> anotherBag) // This has to be stay like that.
{
int total = length + anotherBag.getSize();
BagInterface<T> items = (T[]) new Object[total]; // this may be faulty
for (int i = 0; i < length; i++)
items.add(bag[i]); // bag is current bag
for (int i = 0; i < anotherBag.getSize(); i++) // this is definitely wrong
items.add(anotherBag[i]);
return items;
}
}
What can I do to solve this?