如何搜索内容与另一个数组匹配的一个数组?

发布于 2024-11-28 05:58:10 字数 961 浏览 1 评论 0原文

我有一个 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");
    }

并输出,当列表包含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? - FALSE

什么给出了???

I have an ArrayList of int arrays that is returning false when I ask if it contains the specified coordinates. It does contain the coordinates I request so it should return TRUE.

Here's my code.

    //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");
    }

And output, requesting the coordinates 57, 64, 105 when the list contains 4 coordinates.

56 64 105 == 57 64 105? - FALSE

56 64 106 == 57 64 105? - FALSE

56 64 107 == 57 64 105? - FALSE

57 64 105 == 57 64 105? - FALSE

What gives???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泪意 2024-12-05 05:58:10

Java 的数组 equals 是恒等相等。您需要创建一个实际的坐标类。

换句话说:

int[] c1 = new int[] { 1, 2 };
int[] c2 = new int[] { 1, 2 };
System.out.println(c1.equals(c2)); // prints false

Java's arrays equals are identity equality. You need to create an actual Coordinate class.

Put another way:

int[] c1 = new int[] { 1, 2 };
int[] c2 = new int[] { 1, 2 };
System.out.println(c1.equals(c2)); // prints false
鹿童谣 2024-12-05 05:58:10

最好的方法是获取一个数据类,例如 Cooperative 来存储数据并覆盖 equals 方法。如果您只存储电话号码,另一种选择可能是使用字符串来表示格式良好的电话号码,并使用 String 类的 equals 方法。

The best would be to get a data class, for instance Coordinate to store your data and override the equals method. Another option, if you just store phone numbers, could be to use strings to reprensent well formatted phone numbers, and the use the equals method of the String class.

留蓝 2024-12-05 05:58:10

Java 中的数组是对象。

当您使用 == 运算符时,您正在比较 Location 是否与 Testloc 是同一个数组,但事实并非如此。您真正想要的是比较每个数组中的值以查看它们是否相等。

您可以使用 Arrays.equals() 静态方法来比较两者是否相等。

Arrays in Java are objects.

When you use the == operator, you are comparing whether Location is the same array as Testloc, which it isn't. What you really want is to compare the values in each array to see if they are equal.

Rather than writing your own, you can use the Arrays.equals() static method to compare the two for equality.

那请放手 2024-12-05 05:58:10

问题似乎出在以下行:

if (Location == TestLoc)

大概,TestLoc 是一个整数数组,并且 Location 也绑定到一个数组。

仅当 TestLocLocation 变量都指向同一个数组实例时,上面的测试才会返回 true,如果这两个变量则不会返回 true变量指向不同的数组实例,它们恰好在相同位置具有相同的整数。您正在上面测试“引用相等”,仅询问这两个事物是否相同事物,而不是“值相等”,后者询问两个事物是否等效 ,无论它们是否在计算机内存空间中表示为两个不同的对象。

有些编程语言缺乏这样的区别,有些允许定义新类型,这些新类型最好被视为值(其中身份无关紧要),而不是实体,而实体可能比任何可能的价值等价物都更重要。 Java 使用一种独特的方法 - Object#equals() - 来查询 Object 实例的等价性或值相等性,而 == 运算符始终执行此操作只有一件事:它评估任何两个事物的值相等,即使这些事物是对象引用。

因此,当比较两个数组实例时,它们都是某种类型的Object==运算符不会询问这两个对象是否指向这些引用是否等效,而是引用本身的是否等效。如果它们碰巧指向同一个目标对象,那么它们是等价的,但如果不是,则两个不同的目标对象的值是否看起来相似并不重要; == 返回 false,因为两个目标对象由不同的引用表示。

The problem appears to be with the following line:

if (Location == TestLoc)

Presumably, TestLoc is is an array of integers, and Location is also bound to an array.

The test above will only return true if the TestLoc and Location variables both point to the same array instance, and will not return true if those two variables point to different array instances that both happen to have the same integers in the same positions. You're testing "reference equality" above—asking only if these two things are the same thing—as opposed to "value equality," which asks whether two things are equivalent, irrespective of whether they are represented as two distinct objects in the computer's memory space.

Some programming languages lack such a distinction, and some allow one to define new types that are better treated as values—where identity is immaterial—than as entities, where identity may be more important than any would-be value equivalence. Java uses a distinct method—Object#equals()—to query equivalence or value equality of Object instances, while the == operator always does just one thing: it evaluates value equality of any two things, even if those things are object references.

Hence, when comparing two array instances as you are here, both of which are some type of Object, the == operator asks not whether the two things pointed to by those references are equivalent, but rather whether the value of the references themselves are equivalent. If they happen to point to the same target object, they're equivalent, but if they don't, it doesn't matter whether the two distinct target objects would seem similar in value; == returns false because the two target objects are represented by distinct references.

烏雲後面有陽光 2024-12-05 05:58:10

通过使用 == 比较数组,您可以检查它们是否是同一个数组。您必须循环遍历每个数组并检查 TestLoc[i] == Location[i]。您也许可以使用 .equals(),我不记得 java 是否有 .equals() 用于数组

By comparing the arrays with ==, you are checking to see if they are the same array. You would have to loop through each array and check TestLoc[i] == Location[i]. You might be able to use .equals(), I don't recall if java has .equals() for arrays

夏了南城 2024-12-05 05:58:10

这对我有用:

List<int[]> l = new ArrayList<int[]>();
    l.add(new int[] {56, 64, 105});
    l.add(new int[] {56, 64, 105});
    l.add(new int[] {56, 64, 105});
    for (int i = 0; i < l.size(); i++)
    {
        int[] t = l.get(i);

        if (l.get(i) == t)
        {
            System.out.println("TRUE");
        }

        else
        {
            System.out.println("FALSE");
        }
    }

This works for me:

List<int[]> l = new ArrayList<int[]>();
    l.add(new int[] {56, 64, 105});
    l.add(new int[] {56, 64, 105});
    l.add(new int[] {56, 64, 105});
    for (int i = 0; i < l.size(); i++)
    {
        int[] t = l.get(i);

        if (l.get(i) == t)
        {
            System.out.println("TRUE");
        }

        else
        {
            System.out.println("FALSE");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文