swegbun
swegbun

Reputation: 23

Catch when a grid space contains something

So I am trying to use a for-loop in my Java program but I can't figure out how to return true if two values match. I am using a List<int[]> and a int[]. Here is my code:

public class ClickableObject 
{
    List<int[]> objectCoords;
    public ClickableObject(List<int[]> gridSpaces)
    {
        objectCoords = gridSpaces;
    }
    public boolean isClicked(int[] clickCoords)
    {
        // This loop is not working
        for(int i = 0; i < objectCoords.size(); i++)
        {
            if(clickCoords == objectCoords.get(i))
            return true;
        }
        return false;
    }
}

The method isClicked(int[] clickCoords) takes in a two-integer array, which has the x and y coordinates of the click. It should run it through and find if an int[] in objectCoords matches the clickCoords.

Upvotes: 2

Views: 42

Answers (1)

Stanley Kirdey
Stanley Kirdey

Reputation: 631

You should use

Arrays.deepEquals(clickCoords, objectCoords.get(i))

http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object[],%20java.lang.Object[]%29

Upvotes: 1

Related Questions