Wil*_*Boy 0 java arrays equality reference contains
我有一个int数组的ArrayList,当我询问它是否包含指定的坐标时返回false.它确实包含我请求的坐标,因此它应该返回TRUE.
这是我的代码.
//debug code
for (int i = 0; i < Locations.size(); i++)
{
int[] TestLoc = Locations.get(i);
System.out.print(TestLoc[0] + " " + TestLoc[1] + " " + TestLoc[2] + " == " + Location[0] + " " + Location[1] + " " + Location[2] + "? - ");
if (Location == TestLoc)
{
System.out.println("TRUE");
}
else
{
System.out.println("FALSE");
}
}
//real code
if (Locations.contains(Location))
{
Locations.remove(Location);
}
else
{
System.out.println("FAIL");
}
Run Code Online (Sandbox Code Playgroud)
并输出,当列表包含4个坐标时请求坐标57,64,105.
56 64 105 == 57 64 105? - 假
56 64 106 == 57 64 105? - 假
56 64 107 == 57 64 105? - 假
57 64 105 == 57 64 105? - 假
是什么赋予了???
Java的数组等于身份相等.您需要创建一个实际的Coordinate类.
换一种方式:
int[] c1 = new int[] { 1, 2 };
int[] c2 = new int[] { 1, 2 };
System.out.println(c1.equals(c2)); // prints false
Run Code Online (Sandbox Code Playgroud)