• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

OCA book Array-list doubt

 
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: 69418
276
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
       System.out.println(valueRemove);

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
 
Campbell Ritchie
Marshal
Posts: 69418
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Master Rancher
Posts: 3537
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Enthuware Software Support
Posts: 4428
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please mention book details.

The question is badly worded because it is not clear what they mean by equal. == or equals and also because of the point illustrated by Mike in the post above.

Don't spend too much time thinking about it.
 
Campbell Ritchie
Marshal
Posts: 69418
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Master Rancher
Posts: 3537
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic