Java indexOf()将找不到自定义对象类型
下面的代码没有给我正确的答案Java indexOf()将找不到自定义对象类型,java,arraylist,indexof,Java,Arraylist,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
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));
}
}
类点{
int x;int y;
公共点(内部a、内部b){
这个.x=a;这个.y=b;
}
}
甲级{
公共静态void main(字符串[]args){
ArrayList p=新的ArrayList();
p、 增加(新的第(3,4)点);
p、 增加(新的第(1,2)点);
系统输出println(p.indexOf(1,2));
}
}
这就给出了-1
通常,如果给定了点的arraylist,我们如何在数组中找到特定点的索引?indexOf需要对象作为输入。如果它没有找到您要传递的对象,它将返回-1。需要将其在arraylist中的位置作为输入传递给indexOf函数。在这种情况下,还应该重写类的hashcode和equals 重写类点中的hashcode和equals。然后,创建此类Point的实例(使用new关键字)并将其添加到arrayList后,可以使用任何Point对象作为indexOf调用的参数,在arrayList上使用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));
}
}
import java.util.ArrayList;
公开课考试{
公共静态void main(字符串[]args){
ArrayList p=新的ArrayList();
点p1=新点(3,4);
点p2=新点(1,2);
p、 增加(新的第(3,4)点);
p、 增加(新的第(1,2)点);
系统输出println(p.indexOf(p1));
}
}
您需要创建一个点以传递到indexOf方法
p.indexOf(new Point(1,2));
但这一变化本身仍将返回-1。有关indexOf的信息,请参见api文档:
public int indexOf(对象o)
返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回-1。更正式地说,返回最低的索引i(o==null?get(i)==null:o.equals(get(i)),或者如果没有这样的索引,返回-1
它使用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
}
这样就可以通过值来比较类的两个不同实例
如何在数组中找到特定点的索引
ArrayList p=new ArrayList();
点p1=新点(3,4));
点p2=新点(1,2));
p、 添加(p1);
p、 添加(p2);
系统输出println(p.indexOf(p1));
indexOf()
的参数是一个对象。将它传递给一个点对象。ArrayList.indexOf()不接受两个整数作为参数。必须输入一个对象,该对象应为点
对象
如果仍要调用ArrayList.indexOf(int,int)
,则必须创建ArrayList
的子类,实现indexOf(int,int)
下面的代码应该为您找到想要的对象。首先,您需要覆盖点
类中对象
类中的equals方法,以便比较两个点
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);
}
}
其次,您可以调用indexOf(Object)
:
ArrayList p=new ArrayList();
//创建要在列表中查找的点。
点findMe=新点(1,2);
//搜索数组并保存找到的索引。
int index=p.indexOf(findMe);
PS:您应该遵循Java命名约定;类必须以大写字母开头。indexof做什么?您使用的是java中的point类还是您定义的?您定义的一个没有构造函数?更具体地说,您读过indexOf方法的JavaDoc吗?如果没有,那应该是您的第一个呼叫端口。Java API总体上有很好的文档记录。indexOf接受一个
对象
,您需要传入一个要查找的点
对象。@JonK不清楚他在代码中使用了什么。java中的point类或他定义的point类上面的代码甚至不会编译。阅读javadoc(提示:它专门讨论equals()),遵守Java命名约定(类以大写字母开头),并正确缩进代码。如果另一个对象为null,则(另一个对象instanceof Point)将返回false,因此,检查另一个对象==null是无用的。应该注意的是,如果覆盖equals(Object)
,那么实际上也应该覆盖hashCode()
。
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);
}
}
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);