indexOf() will not find a custom object type
Asked Answered
B

4

10

The following code does not give me right answer.

class Point {

    int x; int y;
    public Point(int a,int b){
        this.x=a;this.y=b;
    }
}

class A{

    public static void main(String[] args){

        ArrayList<Point> p=new ArrayList<Point>();
        p.add(new Point(3,4));
        p.add(new Point(1,2));
        System.out.println(p.indexOf(1,2));

    }
}

This gives -1;

In general if arraylist of point is given, how can we find index of a particular point in in array ?

Brackely answered 25/7, 2014 at 14:14 Comment(5)
what does indexof do? did you use point class from java or one you defined? one you defined does not have constructor?Olympia
More specifically, have you read the JavaDoc for the indexOf method? If not, that should be your first port of call. The Java API is well documented on the whole.Consumedly
indexOf takes in an object, you'd need to pass in a point object you are looking to find.Florenceflorencia
@Consumedly it is not clear what he used in his code. point class from java or point class that he definedOlympia
The code above wouldn't even compile. Read the javadoc (hint: it talks specifically about equals()), respect the Java naming conventions (classes start with an uppercase letter), and indent your code properly.Chapiter
B
9

indexOf requires the object as input. If it does not find the object you are passing in, it will return -1. You need to pass the object whose location in the arraylist you are looking for as the input into the indexOf function. You should also override hashcode and equals for your class in this case.

Override hashcode and equals in your class Point. Then once you create instances of this class Point (using the new keyword) and add them to the arrayList, you can use the indexOf call on the arrayList using any of the Point objects as a parameter to the indexOf call.

Class Point

public class Point {

        int x; 
        int y;

        public Point(int a, int b) {
        this.x=a;this.y=b;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + x;
            result = prime * result + y;
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Point other = (Point) obj;
            if (x != other.x)
                return false;
            if (y != other.y)
                return false;
            return true;
        }       
}

Class Test (you called it "a"):

import java.util.ArrayList;

public class Test {

     public static void main(String[] args){

            ArrayList<Point> p=new ArrayList<Point>();

            Point p1 = new Point(3,4);
            Point p2 = new Point(1,2);

            p.add(new Point(3,4));
            p.add(new Point(1,2));

            System.out.println(p.indexOf(p1));
     }

}
Braze answered 25/7, 2014 at 14:17 Comment(0)
C
3

You need to create a point to pass into the indexOf method.

p.indexOf(new Point(1,2));

But that change by itself will still return -1. See the api doc for indexOf:

public int indexOf(Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

It's using equals to decide whether it's found a match. You haven't overridden the equals method on your point class, so it's using the default implementation in java.lang.Object, which compares the references, and only returns true if the two references point to the same object.

Override equals and hashcode on your point class, like:

@Override public boolean equals(Object other) {
    if (!(other instanceof point)) {
        return false;
    }
    point otherPoint = (point)other;
    return otherPoint.x == this.x && otherPoint.y == this.y;
}

@Override public int hashCode() {
    return x + y; // same values should hash to the same number
}

and that way two different instances of the class can be compared by value.

Crimp answered 25/7, 2014 at 14:17 Comment(0)
E
3

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.

Equable answered 25/7, 2014 at 14:19 Comment(2)
if anotherObject is null then (anotherObject instanceof Point) will return false, so the check of anotherObject == null is useless.Harrar
It should be noted that if you override equals(Object) then you really should also override hashCode().Consumedly
A
0

how can we find index of a particular point in in array ?

ArrayList<point> p=new ArrayList<point>();
point p1 = new point(3,4));
point p2 = new point(1,2));
p.add(p1);
p.add(p2);

System.out.println(p.indexOf(p1));

The argument to indexOf() is an Object. Pass it one of your point objects.

Austere answered 25/7, 2014 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.