Originally posted by Rob Prime:
There are a few methods in the API that are indexed at 1. Think of JDBC or DateFormatSymbols.getWeekDays().
. . .
So unless the API clearly specifies different indexing, always start at 0.
[ July 26, 2008: Message edited by: Rob Prime ]
Excellent point Rob. I of course was thinking of the default behavior when you declare an array. I've edited my above comment to make it clearer.
Originally posted by Juan Manuel Alberto de los Santos:
what do you mean with, "non-empty array"
I mean an array that does not have any values/elements in it. For example:
For an empty array, there are no elements, and therefore no indexes. So the formulas I gave in my first post are not valid. Not only is there nothing at index 0, index 0 does not exist.
Originally posted by Juan Manuel Alberto de los Santos:
what do you mean ... "zero based array" ?
Saying "zero-based array" means I am referring to an array who's first element has an index of 0. An "one-based array" means the first index is 1. And the third type is an "n-based array" which means I am referring to an array who's first element has a programmer-specified value for its index. (n-based arrays are more common in languages other than Java). You can take a look at the Wikipedia article on
Arrays for more information. Keep in mind that that article is talking about the general computer science topic of arrays, and not their specific implementation in Java. For that, you can refer to the Java Tutorial's lesson on
arrays.
Originally posted by Juan Manuel Alberto de los Santos:
this is the question that confused me ... i don't understand the part in bolds ... String indexes are 0-based, and in delete the end is exclusive.
"0-based" is the same as "zero-based" which I just discussed. So lets look at what is meant by "the end [index] is exclusive". When methods that deal with Arrays (or buffers that are backed by arrays) take index values, they need to tell you whether the index value is inclusive or exclusive. That is, does it include the element at the index or not.
For example, in the example you provided, after the insert on line 5, the String buffer content is "012345678 abcdef" (Notice on line 3 the original value had a space at the end; thus the reason for the space between the '8' and 'a'). If we look at that as an array, we have:
When the delete is done, 3 is passed in as the start index, and 8 as the end index. After the delete, the value is: "0128 abcdef" So notice the '3' at index 3 was deleted, but the '8' at index 8 was not. That is because for the delete method, the start index is inclusive (meaning the value at that index is included in the action) and the end index is exclusive (meaning the value is excluded from the action).
If the delete method used the end index inclusively, the value after the delete would have been "012 abcdef", because the '8' at index 8 would have been deleted. But in reality, the delete method treats the end index parameter exclusively. So if we actually wanted to delete the last character (and only the last character), we would do this:
I hope that helps.
[ July 26, 2008: Message edited by: Mark Vedder ]