1

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?

7
  • 3
    array.size()? In any case, did you actually remove anything? size() will return the current size. Commented Jan 27, 2012 at 18:10
  • How are you creating these lists? Are they backed by arrays? Commented Jan 27, 2012 at 18:11
  • 2
    I suspect you error does not come from the code you have pasted here. It must be something else. Commented Jan 27, 2012 at 18:12
  • 1
    The code you posted has no error in it (other than when size() returns 0). consider posting the code that you replaced with an ellipsis in the loops. Commented Jan 27, 2012 at 18:18
  • I suspect, given the methods you're calling from your array that you are actually using an ArrayList already. I don't remember remove or size being things you could use for an array. Is this correct? If not, your problem would probably stem from arrays not being re-sizable. You will need to figure out the new size needed for your array, create a new array and dump the information from the old array into the new array. As mentioned by previous posters, it would be easier to use an ArrayList or vector. Commented Jan 27, 2012 at 18:29

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

As an addition to the valid options provided by @z5h there, consider using the Iterator interface, which includes a remove() method that removes the item from the underlying collection without causing the kinds of problems that using Collection.remove() causes.

Using Iterator might help you walk through your collections in a more readable fashion, as well.

Comments

0

you may want to use an ArrayList instead of an plain array. It provides additional functions like remove(Object o)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.