This week's book giveaways are in the Cloud and AI/ML forums. We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line! See this thread and this one for details.
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.