This site requires JavaScript, please enable it in your browser!
Greenfoot back
darkmist255
darkmist255 wrote ...

2012/10/27

instanceof for a List of classes

darkmist255 darkmist255

2012/10/27

#
I've got a question (and tell me if there is a better way to do this), I'm working on a for within a for to check the class of the tile for the list of tiles to see if any of then are not passable. I have two lists, List<worldObject> atTile, and List<Class> nonpassableClasses.
1
2
3
4
5
6
7
8
9
10
for(worldObject cur : atTile)
                {
                    for(Class ccls : nonpassableClasses)
                    {
                        if(cur instanceof ccls)
                        {
                            return false;
                        }
                    }
                }
When I compile it tells me "cannot find symbol - class ccls". Is there a way to check a list of classes for instanceof?
danpost danpost

2012/10/27

#
I was thinking of two possible ways of handling this: (1) since 'passable' is an attribute that the tiles have, just add an instance boolean variable in the tile class to track each tile's state. You can have the sub-classes set the state in there constructors. or, (2) nah, better to use (1) above, rather than complicating things with more classes.
darkmist255 darkmist255

2012/10/27

#
Thanks, funny that's how I (and I assume others) do these types of checks all of the time, I just for some reason didn't this time. Danpost, always the voice of reason.
You need to login to post a reply.