I have a class we can call Super and a subclass (of Super) that we can call SubClass(SubClass extends Super). And then somewhere else I have two arraylists, one of type Super and another of type SubClass. (ArrayList< Super > a1, ArrayList< SubClass > a2)
And what I'd like to do is add all the elements from a2 into a1, since all a2 elements are indeed type Super. What I tried doing was using
a1.addAll(a2);
But when I use that it returns false (.addAll() returns true if the a1 gets modified at all, and false if not)
I'm sure it can be done simply by iterating through the a2 list and adding each element using a1.add() and a cast but I'd like to know if the addAll() can be used in such case to shrink the code a little, Thanks in advance!
from Newest questions tagged java - Stack Overflow http://ift.tt/1Id9zOj
via IFTTT
Aucun commentaire:
Enregistrer un commentaire