Page 1 of 1

About Question enthuware.ocajp.i.v8.2.989 :

Posted: Sat Jul 29, 2017 3:39 pm
by bari2009
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!

Re: About Question enthuware.ocajp.i.v8.2.989 :

Posted: Sat Jul 29, 2017 10:29 pm
by admin
I see that option 1 i.e. sb.delete(0,sb.length()) is indeed set as the correct answer. sb.clear() is incorrect.

Re: About Question enthuware.ocajp.i.v8.2.989 :

Posted: Fri Jan 19, 2018 8:12 am
by Arold Aroldson
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..

Re: About Question enthuware.ocajp.i.v8.2.989 :

Posted: Fri Oct 26, 2018 4:54 am
by tosidis
I think that clear() is for ArrayList and for the StringBuilder you can use delete()