We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! it is possible to put in an arraylist different types of objects, right?
e.g.
ArrayList al;
al.add (new Circle(0,0,10)); al.add (new Rect(0,0,10,10));
How can I figure out which class is stored when I iterate through that array?
for (int i=0; i<al.size(); i++) CLASS? obj = (CLASS?) ai.get(i);
Answers
If we don't define which object type is gonna be stored in some Collection, such as an ArrayList,
for each reading, using get() method for example, the returning type is Object!
So an extra
(cast)
operator is necessary to force the real data-type outta it! :-<Problem is, if we happen to cast the wrong type, we'd get ClassCastException!
To mend that, we gotta use relational operator
instanceof
in order to make sure our type guess is indeed right! :-SSThere are other inventive ways as well. For example, if we're gonna store 2 diff. types only, like Circle & Rect,
we can decide that the former goes to even indices and the latter to odd indices.
Thus we're certain about which
(cast)
to use for each reading! :-BHowever IMHO, stick to 1 object type Collection as much as possible: :(|)
Hello!
Hope this helps:
This is also possible:
very helpfully! thank's a lot!!
<Object> is completely redundant! Since w/o specifying the generics, it's assumed to be an <Object> already: ;)
final ArrayList shapes = new ArrayList();
good to know! ;-)
This might be a little bit picky (especially for Processing)... but I find that if you have to use
instanceOf
, then you're probably doing something wrong. It's better to use OOP principles - in this case, abstraction:Abstraction is your friend. I find a way to use it all the time (maybe even too much...). There are interfaces, abstract classes, anonymous classes... it's just something to think about.
I am not so far familiar in java as in c++. but if abstraction works in java then that's the right way! thank's a lot!
@calsign : I just try to implement your interface, but I fail....
[Edited]
troubles in line 35 and 36 (I don't know where to place the vars x,y etc. in the class / interface). Or do I have to use a setter method for x and y?
Can you explain or correct my mcve?
Thank you!
Best, Chrisir ;-)
You have an ArrayList of Node objects. The only thing about Node objects the compiler is aware of is that it has the display() method implemented. In this situation you would be better off with straight inheritance.
It would work with getters and setters, or casting I think...
See this: http://forum.processing.org/two/discussion/12310/accessing-member-fields-using-interfaces
It's part of another bigger project and I thought for a great diversity of classes, interface would be better (because all classes can be very different from each other)
Maybe an
incrementBy(float amount){}
,decrementBy(float amount){}
methods in the Interface.yeah, or just
or increment, you are right............
it works now
thank you!
Chrisir ;-)
.