• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

OCA book Array-list doubt  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 3 : (Page number 158)

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.

Answer is:
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

So this line is wrong right ?

Please correct me if I am wrong.
 
Marshal
Posts: 64475
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall add your question to our exam forum.

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!