Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when java is 0 indexed

 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for example ...

String s = "abc";
s.lenght(); // 3 /* 1 indexed */
s.charAt(1); // b /* 0 indexed */

how to know when java is 0 indexed and when is 1 indexed ?

obviously if i look in the javadoc i get that information, but this is for the SCJP exam ... is there any method to know ?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, java arrays are indexed at 0. [edit:] As Rob points are below, there are some exceptions with some methods that use non-zero based arrays in their implementations. In this post, I am talking about arrays when you declare an array in your code.[/edit] So the first item always has an index of zero. You are confusing the length with the last index value. Regardless of where the index starts, be it 0, 1, or even if it started at 100, the length is the length:



for any non-empty array, the length is always:
length = endingIndex - startingIndex + 1

For a zero based array, like in Java and C, the length is always:
length = endingIndex + 1

This is because 0 can be substituted into the first equation; so it becomes
length = endingIndex - 0 + 1
which simplifies to:
length = endingIndex + 1

Ans by using a little algebra, we know that (for a zero based array):
endingIndex = length - 1
[ July 26, 2008: Message edited by: Mark Vedder ]
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS Mark, i understood the first part

what do you mean with, "non-empty array" and "zero based array" ?



this is the question that confused me ... i don't understand the part in bolds ...



A. -1
B. 5
C. 6
D. 7
E. Compilation fails.
F. An exception is thrown at runtime.

Option D is correct. String indexes are 0-based, and in delete the end is exclusive.
[ July 26, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Vedder:
Java is always indexed at 0.

Actually not. There are a few methods in the API that are indexed at 1. Think of JDBC or DateFormatSymbols.getWeekDays().

However, in those cases, the API specifies that you should use a different index. For java.text.DateFormatSymbols.getWeekDays() for instance it is clearly specified that you should use Calendar.SUNDAY to Calendar.SATURDAY as indexes. The java.sql.ResultSet interface clearly specifies that columns start at 1.


So unless the API clearly specifies different indexing, always start at 0.
[ July 26, 2008: Message edited by: Rob Prime ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Juan Manuel Alberto de los Santos
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the detailed explanation ... now is more than cleared
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic