Java indexOf() 将找不到自定义对象类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24957813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
indexOf() will not find a custom object type
提问by ajay
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
;
这给出了-1
;
In general if arraylist of point is given, how can we find index of a particular point in in array ?
一般来说,如果给出了点的数组列表,我们如何在数组中找到特定点的索引?
回答by ali haider
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.
indexOf 需要对象作为输入。如果它没有找到您传入的对象,它将返回 -1。您需要将您正在查找的数组列表中位置的对象作为输入传递给 indexOf 函数。在这种情况下,您还应该覆盖您的类的 hashcode 和 equals。
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.
覆盖类 Point 中的哈希码和等于。然后,一旦您创建了此类 Point 的实例(使用 new 关键字)并将它们添加到 arrayList 中,您就可以使用任何 Point 对象作为 indexOf 调用的参数对 arrayList 使用 indexOf 调用。
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"):
类测试(你称之为“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));
}
}
回答by Nathan Hughes
You need to create a point to pass into the indexOf method.
您需要创建一个点以传递给 indexOf 方法。
p.indexOf(new Point(1,2));
But that change by itself will still return -1. See the api doc for indexOf:
但是这种变化本身仍然会返回-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.
公共 int indexOf(Object o)
返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回 -1。更正式地,返回最低索引 i 使得 (o==null ? get(i)==null : o.equals(get(i))),如果没有这样的索引,则返回 -1。
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.
它使用 equals 来决定是否找到匹配项。您没有覆盖点类上的 equals 方法,因此它使用 java.lang.Object 中的默认实现,它比较引用,并且仅当两个引用指向同一个对象时才返回 true。
Override equals and hashcode on your point class, like:
覆盖点类上的 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
}
and that way two different instances of the class can be compared by value.
这样就可以按值比较该类的两个不同实例。
回答by whiskeyspider
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.
的参数indexOf()
是一个对象。将您的点对象之一传递给它。
回答by MC Emperor
ArrayList.indexOf()
doesn't accept two integers as argument. You must input one object, which is supposed to be a Point
object.
ArrayList.indexOf()
不接受两个整数作为参数。您必须输入一个对象,该对象应该是一个Point
对象。
If you still want to call ArrayList.indexOf(int, int)
, then you must create a subclass of ArrayList
, implementing indexOf(int,int)
.
如果仍要调用ArrayList.indexOf(int, int)
,则必须创建 的子类ArrayList
,实现indexOf(int,int)
.
The following code should find the wanted object for you. First, you'll need to overridethe equals method from the Object
class in the Point
class, in order to compare two points.
以下代码应该为您找到想要的对象。首先,您需要从类中的类中覆盖equals 方法,以便比较两点。Object
Point
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)
:
其次,您可以调用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.
PS:你应该遵循Java命名约定;类必须以大写字母开头。