How to get the assigned variable back out of an Array?
I created an array of objects:
Object[] r = new Object[2];
I assigned a variable of a class to r:
r[0] = Start;
Start is of type SDTNode
which is a class I created.
Now I want to get the value back:
SDTNode end = r[0];
It is giving me an erro开发者_高级运维r. Is there a way to do this?
If you know that all elements of r
are SDTNode
objects, then you should have defined it like this:
SDTNode[] r = new SDTNode[2];
Otherwise, the type information about the content will be "some Object
, we don't know the specifics" and you'll have to cast:
SDTNode end = (SDTNode) r[0];
This will always compile, but will fail at runtime if r[0]
does not in fact reference a SDTNode
.
You need to cast it:
SDTNode end = (SDTNode)r[0];
精彩评论