ArrayList.indexOf()
doesn't accept two integers as argument. You must input one object, which is supposed to be a Point
object.
If you still want to call ArrayList.indexOf(int, int)
, then you must create a subclass of ArrayList
, implementing indexOf(int,int)
.
The following code should find the wanted object for you. First, you'll need to override the equals method from the Object
class in the Point
class, in order to compare two points.
public class Point {
private int x;
private int y;
@Override
public boolean equals(Object anotherObject) {
if (!(anotherObject instanceof Point)) {
return false;
}
Point p = (Point) anotherObject;
return (this.x == p.x && this.y == p.y);
}
}
Second, you can call indexOf(Object)
:
ArrayList<Point> p = new ArrayList<Point>();
// Create the point to find in the list.
Point findMe = new Point(1,2);
// Search the array and save the found index.
int index = p.indexOf(findMe);
PS: You should follow the Java naming conventions; classes must start with an uppercase letter.
object
, you'd need to pass in apoint
object you are looking to find. – Florenceflorencia