So I have a List of Component.java, right? Inside of this list, I want to store a few types of classes all of which extends the Component.java class. See, that part I can do just fine. At present, I’m storing Wire.java’s and Pipe.java’s.
However, when it comes to accessing the different types inside of the list, I’m faced with a dilemma.
First I tried it like this: for (Wire w : component_list)
However, it can’t iterate over only a single type of component inside of the list; it’s looking for Wires when the iterator only knows to return Components.
Seeing this, I tried:
for (Component c : component_list) {
if (c.equals(Wire.class)) {
Wire w = (Wire) c;
}
}
However, I’m not sure that it’s working properly. I’m wondering, is the second option possible?