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

2014/4/15

Is there a way to find the number of objects in a list?

wasdodinian wasdodinian

2014/4/15

#
public void getPlantCount()
    {
        List plant = getObjects(Plant.class);
        plantCount = plant.numberOfObjects();
    }
This is what I've tried so far
danpost danpost

2014/4/15

#
The 'numberOfObjects' method is from the World class and can be found in the World class API. You need to find the correct method to use from the List class API.
bilzard bilzard

2014/4/16

#
import java.util.List;

public void getPlantCount()
    {
        List plant = getObjects(Plant.class);
        int plantSize = plant.size();
    }
This gives you the size of your list 'plant'.
danpost danpost

2014/4/16

#
bilzard wrote...
import java.util.List;

public void getPlantCount()
    {
        List plant = getObjects(Plant.class);
        int plantSize = plant.size();
    }
This gives you the size of your list 'plant'.
or just use:
int plantCount = getObjects(Plant.class).size();
You need to login to post a reply.