dimanche 18 mai 2014

Java - convertir un tableau d'objet à <E>type MultiSet - Stack Overflow


I am working on collections of generic arraysE[]. I'm currently stuck on the union of two sets and trying to convert a MultiSet<E> aSet into a generic array. I'm trying to compare the generic array of the current class, or the 'this' array, to the one being accepted in the union method. Here is my current partition of code:


@SuppressWarnings("unchecked")
@Override
public MultiSet<E> union(MultiSet<E> aSet) {

MyBag<E> set = (MyBag<E>) aSet;
E[] temp;
temp = (E[]) new Keyed[2*length];
// ...
return new MyBag<E>(temp);
} // Generate the union of two bags.

Would gladly clarify any questions needed. Thank you




I don't totally understand, but it seems as if you can simply copy everything to the temp array with two for loops, one to copy the aSet in and the other to copy the 'this' array. Then the temp array would contain all the elements of each, and the new Bag would have everything. The way you currently have it set up though, aSet would have to have the same length as this.length



I am working on collections of generic arraysE[]. I'm currently stuck on the union of two sets and trying to convert a MultiSet<E> aSet into a generic array. I'm trying to compare the generic array of the current class, or the 'this' array, to the one being accepted in the union method. Here is my current partition of code:


@SuppressWarnings("unchecked")
@Override
public MultiSet<E> union(MultiSet<E> aSet) {

MyBag<E> set = (MyBag<E>) aSet;
E[] temp;
temp = (E[]) new Keyed[2*length];
// ...
return new MyBag<E>(temp);
} // Generate the union of two bags.

Would gladly clarify any questions needed. Thank you



I don't totally understand, but it seems as if you can simply copy everything to the temp array with two for loops, one to copy the aSet in and the other to copy the 'this' array. Then the temp array would contain all the elements of each, and the new Bag would have everything. The way you currently have it set up though, aSet would have to have the same length as this.length


0 commentaires:

Enregistrer un commentaire