In general, yes. If you work in a language with C-like syntax (C, C ++, Java), then arrays are indexed with a zero mark, and most data structures with random access (vectors, massive lists, etc.) will have zero indexes as well .
Running indexes at zero means that the size of the data structure will always be larger than the last valid index in the data structure. People often want to know the size of things, of course, and therefore it is more convenient to talk about size than to talk about the last valid index. People are used to talking about final indexing in an exclusive way, because the array a[] , which has n element long, has its last valid element in a[n-1] .
There is another advantage to using an exclusive index for the final index, which is that you can calculate the size of the sublist by subtracting the inclusive starting index from the exclusive ending index. If I call myList.sublist(3, 7) , then I get a sublist with 7 - 3 = 4 elements in it. If the sublist() method used inclusive indexes at both ends of the list, then I would need to add an extra 1 to calculate the size of the sublist.
This is especially convenient when the starting index is a variable: getting the substring myList starting with i , which is 5 elements, is just myList.sublist(i, i + 5) .
That being said, you should always read the API documentation, and not assume that a given starting index or ending index will be inclusive or exclusive. Likewise, you should document your own code to indicate whether any restrictions are included or excluded.
Joe carnahan
source share