有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java indexOf()找不到自定义对象类型

下面的代码没有给出正确的答案

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));

    }
}

这就产生了-1

一般来说,如果给定点的arraylist,我们如何在数组中找到特定点的索引


共 (2) 个答案

  1. # 1 楼答案

    indexOf需要对象作为输入。如果它没有找到你要传递的对象,它将返回-1。需要将其在arraylist中的位置作为输入传递给indexOf函数。在这种情况下,还应该重写类的hashcode和equals

    在你的类中重写hashcode和equals。然后,创建此类Point的实例(使用new关键字)并将其添加到arrayList后,可以使用arrayList上的indexOf调用,使用任何Point对象作为indexOf调用的参数

    课程积分

    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;
            }       
    }
    

    类测试(你称之为“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));
         }
    
    }
    
  2. # 2 楼答案

    需要创建一个点以传递到indexOf方法

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

    但这种变化本身仍将返回-1。有关indexOf,请参见api文档:

    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.

    它使用equals来决定是否找到匹配项。您尚未在point类上重写equals方法,因此它使用java中的默认实现。lang.Object,它比较引用,仅当两个引用指向同一个对象时才返回true

    重写point类上的equals和hashcode,如:

    @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
    }
    

    这样就可以通过值来比较类的两个不同实例