Java list! How do I get an object to determine if there is another object

337 Views Asked by At
// test for rock
        List rocks = getWorld().getObjectsAt(x , y, Rock.class);

        /* Look up the List class (java.util.List) in the Java API
         * and determine what method to use with the 'rocks' List
         * to determine if there was a rock. Put the correct test
         * in the 'if()' statement below.
         */

        if () {
            return true;
        }

        return false;

Have to fill in the "if()" statement above return true; Really confused how to get the if statement with lst. Please help! and send knowledge of how to do! Thank you so much guys!

2

There are 2 best solutions below

1
On

If I understand correctly, what you want is to check if a concrete object Rock is on the list 'rocks'. Then you just have to use the method "contains". public boolean List.contains(Object o) checks if the object 'o' exists on the list.

Rock myRock = //whatever sentence you use to create the rock you're checking

if (rocks.contains(myRock) {
    return true;
}

return false;
0
On

what about:

if (!rocks.isEmpty()) { //is true if your list with rocks is not empty
    return true;
}