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

2021/2/27

How to use Lists

Znarf Znarf

2021/2/27

#
I'm trying to make a method that tells you if there is a specific Object at a specific location
1
2
3
4
5
6
7
8
public boolean occupied(int x, int y, Class clls){
     List objects = getWorld().getObjectsAt(x, y, clls);
      
     if (objects.isEmpty){
         return false;
     }
     else {return true;}
 }
I've seen this with List a few times now but when I use it it doesn't work. Greenfoot tells me that List is no actor. So I'm wondering do I need to create a List or do I need to import something so that this will work
danpost danpost

2021/2/27

#
You could:
1
import java.util.List;
or begin line 2 with:
1
java.util.List objects = ...
or, more simply:
1
2
3
public boolean occupied(int x, int y, Class clls) {
    return ! getWorld().getObjectsAt(x, y, clls).isEmpty();
}
You need to login to post a reply.