|
Java programing
|
View this Thread in Original format
| PHALPAX |
| I'm in the middle of a java code quiz and was wondering if anyone knows whats an "off-by-one" error when using arrays. |
|
|
| Fundamental |
Hmmm... If you exceed the set length of an array in Java you should be getting a 'NullPointerException'.
:conf: |
|
|
| PHALPAX |
ahahaha thanks whiskers:D :) |
|
|
| cviper |
| quote: | Originally posted by whiskers
i'm taking a wild guess - your program is going beyond the size of an array, thinking it's 1 unit bigger than it really is.
|
Yeah, java starts counting at 0 when indexing arrays, which also means that the last element is (size-1).
It's another thing java got from C/C++: in C a[i] is equivalent to *(a+i). 'a' decays to a pointer which points to the first element in the array (i.e. the first element is *(a+0) or a[0]). |
|
|
| PHALPAX |
| quote: | Originally posted by Fundamental
Hmmm... If you exceed the set length of an array in Java you should be getting a 'NullPointerException'.
:conf: |
Yeah the nullexception would work, but the question i was asked was asking the about anatomy of an off-by-one error.
And they are not letting me use a compiler! :sadgreen: |
|
|
|
|