• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mistake in book

 
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all ,

Is there any mistake in K&S book for strings .It is written that StringBuilders insert method is zero index based but it is not zero based it is 1 based.

Please refer chapter 6 for this one.



Let me know if its my mistake while reading
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chetan just try this program and justify that insert method uses zero or one based index

 
chetan deol
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that its is 1 based but read the book carefully.It is written that the index is zero based.


Thanks
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why are you thinking that its 1 based?
StringBuilder's insert method uses zero based index.

If we take the above example, the result would be 01hello234.
When we call insert(2,"hello") on str, it inserts "hello" at index 2(means 3rd character).

Refer to the Java API documentation for more information!
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a subtitle "Important methods in the StringBuffer and StringBuilder Classes":

under method delete() I found:
"...The starting index of the SUBSTRING to be removed is defined by the first argument (which is zero-based), and the ending index of the substring to be removed is defined by the second argument (but it is one-based)!..."

under method insert():
"...the offset is zero-based..."

Or is there any other places you saw that?
And I'm glad you asked it, cause I thought that StringBuffer/Builder is zero-based after I read the chapter (don't know why).
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chetan dhumane wrote:I know that its is 1 based but read the book carefully.It is written that the index is zero based.



Hey I never said that its one based index . I gave the example to show that it is based on zero based index. Just read how indexing should be looked at here...
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thanks for your suggestion.

my blog My blog
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For String/StringBuilder/StringBuffer functions, can we say index parameter are all 0-based, but the first index will always be inclusive, and the second index(if present) are always exclusive?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jia Tan wrote:For String/StringBuilder/StringBuffer functions, can we say index parameter are all 0-based, but the first index will always be inclusive, and the second index(if present) are always exclusive?


Jia, that's exactly the way I think about it, and I think it's the most logical and easiest way to remember how it works.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic