list - Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2 -
still have problem java :/ here problem list :
if(listkey.size() == 1){ k = listkey.get(0); }else if(listkey.size() > 1){ for(int q : listkey){ if(listkey.get(q) == listkey.size()){ k = listkey.get(0); k1 = listkey.get(1); } else if(listkey.get(q) == listkey.size() -1 ){ k = listkey.get(q+1); k1 = listkey.get(0); } else if(listkey.get(q) == key && listkey.get(q) != listkey.size()){ k = listkey.get(q+1); k1 = listkey.get(q+2); } } }else if(listkey.size() == 0){ system.out.println("empty"); }
it gives me error:
exception in thread "main" java.lang.indexoutofboundsexception: index: 3, size: 2 @ java.util.arraylist.rangecheck(arraylist.java:635) @ java.util.arraylist.get(arraylist.java:411) @ com.server.peernode.findreplicas(peernode.java:222) @ com.server.peernode.printmenu(peernode.java:154) @ com.server.peernode.run(peernode.java:99) @ com.server.main.main(main.java:64)
i think i'm using correctly list , how iterate through it! i'm not exceeding size of list when try int it. in advance help
here idea: got key = 5 case 1: array {4}... want put 4 k case 2: array {4,6}.. want put k=6 , k1=4 case 3: array {4,6,7}.. want put k=6 , k1=7
i know mess code... , erros. hugh fixed , works... it's now:
if(listkey.size() == 1){ keyreplica1 = listkey.get(0); }else if(listkey.size() > 1){ listkey.add(key); collections.sort(listkey); (int q=0; q<listkey.size(); q++){ if(listkey.get(q) == key && listkey.get(q) == listkey.size()){ keyreplica1 = listkey.get(0); keyreplica2 = listkey.get(1); } else if(listkey.get(q) == key && listkey.get(q) == listkey.size() -1 ){ keyreplica1 = listkey.get(q+1); keyreplica2 = listkey.get(0); } else if(listkey.get(q) == key && listkey.get(q) != listkey.size()){ keyreplica1 = listkey.get(q+1); keyreplica2 = listkey.get(q+2); } } }else if(listkey.size() == 0){ }
is listkey list of integer? think you're mixing keys , values here - if list has more 1 entry, loop on values v, try v-th value in list. example, if listkey {3,4}, attempt find 3rd value in list, exceed bounds.
i think using following loop instead give behaviour expect:
for (int q=0; q<listkeys.size(); q++){
Comments
Post a Comment