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

2012/3/17

How do i use java.util.List's

1
2
Duta Duta

2012/3/19

#
TheNightStrider wrote...
"How do i cast 'getObjectsInRange(10, Enemy.class)' into an Enemy in a loop ? for (Enemy a : getObjectsInRange(10, Enemy.class)) { // code to execute }" I am not quite sure what exactly you are trying to do. Using that List<Enemy> l = getObjectsInRange(10, Enemy.class); if(!l.isEmpty()){ for(int i=0;i<=(l.size());i++){ Enemy e = l.get(i); //do stuff with enemy e! }} I think that is what you need to do. (Hopefully I have written that out right!). If you need any more info, check out this link HERE This assumes a lists index starts at 0, not 1.
for(Object o : A list or an array) is a way of iterating through a list -> its essentially the same as: for(int i = 0; i < list.size()/array.length; i++) Object o = list.get(i)/array Oh and if you're looping through the list, then you don't need that if(!list.isEmpty()) check - if the list is empty, then the list's size will be 0 and as such for(int i = 0; i < list.size(); i++) won't run in to any errors, it'll just immediately exit the loop... Imagine it like this:
boolean check = false;
while(check) //Or if(check == true), they come to the same thing
{
    //This code won't execute
}
//But this will
You need to login to post a reply.
1
2