简体   繁体   中英

got the .equals() on incompatible types problem, how do i make my ArrayList <Point> and point compatible and the answer into an boolean

im making a Snake game and dont want my cherry to spawn inside the snakes body but when i want to use equals to figure out if it inside it or not it cant cause they are not compatible. is it any way to make them compatible or do i have to make it in another way enter image description here

public boolean collisionCheck1 (Point cherry){


     if (snakeParts.equals(cherry)){
     return true;

     }
     else 
         return false;
 }

this is the checking part snakeParts is the arraylist and cherry is the point

ArrayList is a class from java.util package , which is a child class of java.util.Collection interface. It is used to hold a list(to understand you can assume as an array of objects but they are not actually the same) of objects of any type if you don't specify the generic type.

In your case, snakeParts is an ArrayList<Point> object and which contains a list of Point type objects (not just one Point object).

But here, snakeParts.equals(cherry) you are trying to compare ArrayList type object to Point type object.

I am unable to understand what actually you trying to do, but if you want to compare you have to do something like this.

snakeParts.get(i).equals(Point class object)

where i is the index of ArrayList .

To check whether snakeParts ArrayList contains cherry object or not you can use contains(your object) method of java.util.List interface like this

snakeParts.contains(cherry)

It returns true if snakeParts ArrayList contains cherry Point object otherwise returns false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM