19. Which of the following are true? (Choose all that apply)
A. Two arrays with the same content are equal.
B. Two ArrayLists with the same content are equal.
C. If you call remove(0) using an empty ArrayList object, it will compile successfully.
D. If you call remove(0) using an empty ArrayList object, it will run successfully.
E. None of the above.
B, C. An array does not override equals() and so uses object equality. ArrayList does override equals() and defines it as the same elements in the same order. The compiler does not know when an index is out of bounds and thus can’t give you a compiler error. The code will throw an exception at runtime, though.
My doubt is Option B,
ArralyList Equals Method working fine for List<String>,List<Integer> ,List<Double> ..
I mean this one is working Class which overrides equals Method . For Example String class Overrides Equals method.
Class Which is not overriding Equals method -> List Equals is not working,
I tried for Custom Class without overriding equals method with same content same order -> Not working Obviously it should not work
Go through the documentation for ArrayList and read the details of the equals() method. It assumes equality is defined by the equals() method, which means your class must correctly override equals() in order for two instances to be recognised as equal. Otherwise it will use the version of equals() inherited unchanged from Object which tests whether o1 == o2. If you have two custom objects with different contents or have made a mistake overriding equals(), your two Lists will be recognised as not equal. That shows that B C is indeed correct. If you expected equality without overriding equals() please review the definition of equality.
what about C?
C If you call remove(0) using an empty ArrayList object, it will compile successfully
I didnt compile
ArrayList<String> arrayListString = new ArrayList<>();
String valueRemove = arrayListString.remove(0);
tired :at ide Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
tried: java Ch3Q19.java
Error: Could not find or load main class Ch3Q19.java
posted 1 month ago
Welcome to the Ranch
fugitiva La Madrid wrote:. . . it will compile successfully
Yes, that is what question C is asking about.
I didnt compile . . . Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 . . .
If you got that exception, then the code did compile and start execution successfully. If you tried to execute the same code from the command line/terminal as with an IDE, you might have suffered the class not found exception because you are in the wrong directory.
So, I think we may have missed the point of the original poster, which was not clear. Or maybe others understood correctly, and only I am confused. But...
I think the point of the original poster was, why is option B listed as correct, when it seems (to the original poster) that it is not correct. I don't think they had any concerns about the other options.
For option B, I think the problem is that it's not immediately clear what is meant by "same content". The concern is, if the content is of a class that does not override equals, there may be a problem.
For example, consider:
This would print true, because the two lists have the same content. However:
This would print false, because StringBuilder does not implement its own equals(), and so when equals() is called it returns false here because the instances are different. This may look like the ArrayLists are not equal, for the same content. So, how can option B be true?
That is what I think the original poster was asking.
The answer is, we need to look at what we mean by "same content". Although two objects created using new StringBuilder("foo") may seem like they are the same content, they are not. That's not because of ArrayList, but because of StringBuilder. To create "same content" with StringBuilder, we'd have to do something like this:
Now we have two ArrayLists with the same content. And, they are equal. As they should be.
Of course, if that's not what the original poster was asking, then just ignore all that.
Yes, that question's wording isn't clear on its own. But the readers are expected to know the definition of equality used by the Collections Framework. That is equal according to Object#equals(Object). By that definition two StringBuilder objects with identical content are not equal because they don't test that feature in the equals() method.
posted 1 month ago
Which is my point, assuming I understood the poster's intended question correctly.
Remember to always leap before you look. But always take the time to smell the tiny ads:
Devious Experiments for a Truly Passive Greenhouse!