开发者

How do I search for one array whose contents match another?

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("FA开发者_开发技巧LSE");
        }
    }

    //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???


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


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.


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.


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜