Reputation: 1795
I know that this question has been asked a million times. And I feel like the solution will be fairly obvious to someone who hasn't been staring at it for a couple of hours. But I can't make head or tails of my out of bound exception. Here is the error:
exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 207493, Size: 207493
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at affysureselect.AffySureSelect.main(AffySureSelect.java:92)
Java Result: 1
I was thinking perhaps this might be happening due to the size of the arraylist, but if that were the case I would have expected the error to be when adding, rather than the getting. Here is the code where it is dying:
String chrompos;
ArrayList<String> chromnum = new ArrayList<String>();
while ((input2 = sbuff.readLine()) != null) {
prse = input2.split("\t");
chromnum.add(prse[0]);
...
chrompos = prse[7];
}
int cnt = 0;
int cnt2 = 0;
if (chromnum.get(cnt).equals(chrompos)) { // line causing my untimely death
end = Integer.parseInt(chromposend.get(cnt2));
start = Integer.parseInt(chromposstart.get(cnt2));
...
I even tried adding:
if (cnt <= chromnum.size()) { //this line
if (chromnum.get(cnt).equals(chrompos)) { /before the dying line
But it dies anyway, on the get, not the add. What am I missing?
Upvotes: 0
Views: 97
Reputation: 19362
1-based like Pascal: United States, English-speaking Canada.
0-based like C/Java: Quebec, (most of?) Europe.
That's the answer. The question is, How are floors indexed in an elevator.
Upvotes: 0
Reputation: 896
Remember that lists start with 0, so if you have a list of N items the last item will be N - 1 (because 0 is the 1st element, 1 is the 2nd and so on)
So you should write
if (cnt < chromnum.size()) {
instead of
if (cnt <= chromnum.size()) {
Upvotes: 0
Reputation: 4669
Remove the equal sign,
if (cnt < chromnum.size()) { //this line
if (chromnum.get(cnt).equals(chrompos)) {
Hope to help you:)
Upvotes: 0
Reputation: 4123
Since, as per the code it looks like, that in if loop you are trying a
chromnum.get(cnt) and cnt is initialized to 0.
So the most probable issue is that the code never entered the while-loop, juts put a SOP after the while loop and check for the arraylist size.
Upvotes: 0
Reputation: 45090
You can't access the index i
if the size of the ArrayList
is also i
. The maximum accessible index is i-1
. Hence, the IndexOutOfBoundsException
which says you're trying to access the 207493
th index, even when the size of your List
is also 207493
.
Have a check to restrict cnt
below the size of your list
.
Upvotes: 0
Reputation:
Index i does not exist. You must always iterate to list.size() - 1
.
Upvotes: 0
Reputation: 4202
If you are incrementing cnt, make sure that it's always less that chromnum.size().
It should be-
if (cnt < chromnum.size())
Upvotes: 1