java - Different objects in array -


good time of day reading,

i ask if following situation possible in java:

what want - create 2d array can have different objects in (i.e. in [0][2] have preroom object, in [0][3] - room object) , these object accessible (obviously).

how doing this?

1) declare 2d array of object type:

object[][] map = new object[151][151]; 

2) create object:

map[0][2] = new preroom(r, true); 

3) after i'm unable use method of preroom/get property, i.e. unable do:

map[0][2].x; //or map[0][2].getr; 

am doing wrong? or it's impossible , therefore solution create unified object current 2?

thanks in advance.

edit: solution found, replies.

you declare object[][] map = new object[151][151]; store in object (obviously), how know object in map[0][2] of preroom? need cast such can use methods of preroom

((preroom)map[0][2]).methodname(); 

be careful in future if storing numerous types in map. may find instanceof useful if ever need iterate through map not knowing values of what.

for example;

if(map[x][y] instanceof preroom) {    //map[x][y] instance of preroom class    preroom preroomobj = (preroom) map[x][y]; } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -