Today I tried to compare two arrays using equals(), it obviously failed, I started diggin' etc etc, and after some research I can't quite figure out: at which point in time the class for an array is being created? Cuz I know that int [] a leads to creation of an array class for integers. Who creates it? Why it was impossible to override Object.equals() so that it doesn't compare two objects and compares elements of arrays instead? Would be very grateful if someone could explain or provide a link, which would help to understand that.
Array doesn't override equals() of the Object class.
So it will not use the equals() method of the objects that it holds when equals() is invoked on an Array.
If you want to compare two arrays of integer, you can use Arrays.equals() method.
If it doesn't suit you, create your own method to compare two array of integers.
But overriding equals() of Array is not possible as the class is final and besides it seems rather a weird approach.
Related
I am reading Oracle Java Certification book. I'm finding following sentences hard to grasp.
Two arrays with the same content are not equal but ArrayLists equal.
And If you call remove(0) using an empty ArrayList object, it will
compile successfully :/
What does it mean ? Wonder why and how ? Can anyone explain it ?
because equals on arrays test for reference equality, wether or not the two variables are actually references to the same object. See https://stackoverflow.com/a/8777279/2442804 ArrayList implements it differently and checks its contents
Why should it not? It is just some method call one some object that is not allowed at runtime. But that is nothing the compiler is supposed to check - period. The call compiles fine, but at runtime the code detects that you are not allowed to call that method in the given situation.
I read somewhere that equals() method is used to determine if two objects in HashSet are considered to be same or not.I am bit confused with my understanding.Can anyone explain the scenario please. Thanks in advance.
equals is used to determine if the object you are trying to add or search for already exists in the HashSet. It's not used to determine if two objects in the HashSet are considered the same, since two such objects can't both be in the HashSet.
if i have an ArrayList<int[]> example and I want to check if {2,4} is in it how would I do this?
exaple.contains({2,4}); //doesn't work
and
exaple.contains(2,4); //doesn't work either
what is wrong with the code?
Arrays don't override the Object.equals() method (that ArrayList.contains() uses to compare objects). So an array is only equal to itself. You'll have to loop through the list and compare each element with your array using Arrays.equals().
What I suspect, though, is that you shouldn't have a List<int[]>, but a List<Coordinate>. The Coordinate class could then override equals() and hashCode(), and you would be able to use
example.contains(new Coordinate(2, 4))
You could also use a List<List<Integer>>, but if what I suspect is true (i.e. you're using arrays to hold two coordinates that should be in class), then go with the custom Coordinate class.
Even if you did it properly, ie:
int[] vals = {2, 4};
exaple.contains(vals);
You would not return true, because the contains method will use the equals method. Unless you have overridden the equals method, then this will always resolve to false, unless you pass in the exact same array.
You might want to take a look at this. And please post a bit of you're code.
i need to find a integer data in arraylist?
The contains() method checks, if ArrayList contains a specific object, it doesn't concern itself with the values inside an object. In other words, if you instantiate two {2,4} arrays, it will be two different objects and contains() method will diferentiate between them.
What you need to do is either override the contains() method to look on the content of arrays, instead of only the reference, or you can drop the contains() method completely and check it by hand.
I was testing my shuffling class and came across one issue I cannot understand. Why does the following assert statement:
assertEquals(new int[]{1}, new int[]{1});
results in an AssertionError? Naturally, the correct answer is "because they are not equal!", but could someone explain me why? And how to test a method, for which I would like the two such objects to be equal?
but could someone explain me why
Sure - arrays don't override equals, therefore they inherit the behaviour from Object, where any two distinct objects are non-equal.
It's even simpler than the version you showed if you use a 0-element array:
System.out.println(new int[0].equals(new int[0])); // false
That's why when checking for equality in non-test code you use Arrays.equals, and when checking for equality in test code you use a dedicated assertXyz method (where the exact method depends on the version of JUnit etc).
assertEquals calls the equals object in one of the objects to compare it with the other.
Arrays need to be compared using Arrays.equals() if you want a full comparison of the two arrays, otherwise unfortunately they just use the Object equals method.
See also: equals vs Arrays.equals in Java
Because you create 2 different objects and they point to different locations in the memory.
When comparing objects, you use the equals() method inherited from the class Object. Now, if you don't override the method in your class, you will have the default behaviour which is the comparison of objects address. In the code you create 2 arrays, but even though their content is the same, not the content is tested for being equal, but the objects reference by using the inherited equals() method from Object class.
I need to check whether a list/set of 2d char arrays contains an exact duplicate of another 2d char array that I pass as a parameter. My idea was to make a HashSet and then use the contains() method, but that's not really working; I assume it has to do with me passing the method 2D arrays. If possible, I don't want iterate through every item in the list because that is too costly. Please help me out here. Thanks!
That would never work because the hashCode of the arrays is based on the Object's so unless you are trying to find same references, you will never get a contains to evaluate to true.
You should create a wrapper for your arrays and override the hashCode and equals. You can use Arrays.deepEquals for this purpose