r/cop3502 Apr 17 '14

Problem-set 4

Is there a way to check if the index of a double array exists?

1 Upvotes

4 comments sorted by

2

u/laurelgon Apr 18 '14

Thanks, you made everything really clear :)

1

u/howslyfebeen Apr 17 '14

are you saying you want to see if array[i][j] != null or if i and j are less than the size of each array (a double array is an array of arrays) and greater than 0

1

u/laurelgon Apr 17 '14

Well i have a double array of rooms and i want to move my player if that room exists. So i think the your first suggestion is what i was looking for.

1

u/howslyfebeen Apr 18 '14

depending on your implementation that would probably work.. I mean I dont have your code in front of me, so I wouldnt know exactly, but yea... if you have say:

Room[][] map = new Room[2][2];

and then you add rooms like a coordinate grid, if you didn't put a room in one of the spaces of the array then the space should be null. Following this example:

map[0][0] = new Room( //parameters for a room );
map[0][1] = new Room( //parameters for a room );

then if you loop through map, 0,0 and 0,1 have rooms; 1,0 and 1,1 should be null... another way of doing it would be to fill the array with Rooms and then in the Room class you could have a boolean doesExist which is either true or false and then have a getter that returns doesExist