I have 2 lists, and depending on a parameter I remove an element from 1 of the 2 lists. I then loop through the lists for additional processing. However even though the list has lost 1 element, the list.size() is still the same causing a java.lang.IndexOutOfBoundsException. Is there anything I can do to fix this?
System.out.println(high_list_price.size());
if(first_hit.equals("low")){
high_list_date.remove(high_list_date.size()-1);
high_list_price.remove(0);
high_list_percent.remove(0);
}
if(first_hit.equals("high")){
low_list_date.remove(low_list_date.size()-1);
low_list_price.remove(0);
low_list_percent.remove(0);
}
System.out.println(high_list_price.size());
for(int tt = 0;tt < low_list_date.size();tt++){
System.out.println(low_list_date.get(tt)+"|"+low_list_price.get(tt));
}
for(int ii = 0; ii < high_list_date.size();ii++){
System.out.print(high_list_date.get(ii)+"|");
System.out.println(+high_list_price.get(ii));
}
high_list_price.size() = 51 both before and after the .remove, yet high_list_date.size() goes from 51 to 50, why is that?
If you iterate through ArrayLists backwards, you can delete the current element without worrying about what comes after.
Another option is to iterate through, make a list of things to delete, then delete those elements form the original list.