This question asks about emptying the contents of a StringBuilder called sb. The answer I chose is "wrong", but I tried the code and it works:
sb.delete(0,sb.length());
The "right" answer is:
sb.clear();
Why isn't sb.delete(0,sb.length()) correct also?
I appreciate any clarification on this. Thank you so much!
About Question enthuware.ocajp.i.v8.2.989 :
Moderator: admin
-
- Posts: 3
- Joined: Sat Jul 15, 2017 1:22 pm
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.989 :
I see that option 1 i.e. sb.delete(0,sb.length()) is indeed set as the correct answer. sb.clear() is incorrect.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.989 :
It's really wierd that delete() method doesn't throw an IndexOutOfBound error when we pass a value exceeding length of characters in StringBuilder. Like sb.delete(0,10000000) //compiles and runs without any issues..
-
- Posts: 3
- Joined: Fri Oct 26, 2018 4:45 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.989 :
I think that clear() is for ArrayList and for the StringBuilder you can use delete()
Who is online
Users browsing this forum: No registered users and 93 guests