Help with an arraylist of subclass using generics

A

Alessandro

Hi all,

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())
{

if(iter1.next() instanceof RicDataCrossElement){
RicDataCrossElement crossElm=
(RicDataCrossElement )iter1.next();
...
}
else if(iter1.next() instanceof RicDataTassiElement){
RicDataTassiElement tassiElm=(RicDataTassiElement)
iter1.next();
...

}
else if(iter1.next() instanceof RicDataUpdElement){
RicDataUpdElement updElm=(RicDataUpdElement)iter1.next
();
...
}
}
}
//END CODE


Thanks for any help and best regards,
Alessandro
 
M

Mark Space

Alessandro said:
Iterator<? extends RicDataElement> iter1 = buffer.iterator();

How about just:

Iterator<RichDataElement> iter1 = buffer.iterator();


if(iter1.next() instanceof RicDataCrossElement){
RicDataCrossElement crossElm=
(RicDataCrossElement )iter1.next();

"instanceof" is used for evil here, but I understand what you are trying
to do. If there's a way to use polymorphism to accomplish this, I
encourage you to do so.

Let's say you have an existing class RichDataElement that you don't have
source access to. What about making some sort of wrapper for it?

class MyRdeWrapper {
RichDataElement r;
MyRdeWrapper( RichDataElement r ) {
this.r = r;
}
// polymorphic methods go here...
}

Now you can wrap RichDataElements (and children) in this class and you
can delegate evil un-polymorphic stuff to the wrapper class, thus
cleaning up your higher level code. Depending on the structure of the
code, it may not buy you anything, but I thought I'd at least mention
this idea.
 
D

Donkey Hottie

Hi all,

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())
{

if(iter1.next() instanceof RicDataCrossElement){
RicDataCrossElement crossElm=
(RicDataCrossElement )iter1.next();
...
}
else if(iter1.next() instanceof RicDataTassiElement){
RicDataTassiElement
tassiElm=(RicDataTassiElement)
iter1.next();
...

}
else if(iter1.next() instanceof RicDataUpdElement){
RicDataUpdElement
updElm=(RicDataUpdElement)iter1.next
();
...
}
}
}
//END CODE

Every time you call Iterator.next(), it advances to the next element. You
call next() many many times in that while loop, and it does not work how
you intend.

Just put a

RicDataElement element = iter1.next() ;
if (element instanceof RicDataCrossElement) {
RicDataCrossElement crossElm = (RicDataCrossElement)element;
...
 
L

Lew

Alessandro said:
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 said:
                    if(iter1.next() instanceof RicDataCrossElement){
                    else if(iter1.next() instanceof RicDataTassiElement){
                  else if(iter1.next() instanceof RicDataUpdElement){

As others have mentioned, the presence of the 'instanceof' test
indicates that you have severely misused object orientation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top