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?
System.out.printlnthe list, and you'll see you have less elements than you think0-9(when you just useArrayList.add( Object). Your check to make sure an index exists in theArrayListwould beif (cnt < chromnum.size()).