StringList insert() throwing exception

I may have misunderstood how the insert function works on StringLists or something odd is going on. Either way I'm getting an error in this code.

StringList blah = new StringList();
StringList insertionList = new StringList();

void setup(){
blah.append("zing");
blah.append("boom");
blah.append("whap");
insertionList.append("kapow");
insertionList.append("kapowski");
insertionList.append("kapowskidoo");
insertionList.append("kapowskidont");
insertionList.insert(3,blah);
println(insertionList);

}

When the insert index is set to anything up to 2 (in this example) it works fine but as soon as i go up to 3 (as far as I can tell valid because insertionList has 4 values therefore 3 is a valid index) i get the error:

IllegalArgumentException: insert() index 3 is past the end of this list

I've tried messing around with the sizes of the lists and this error seems to come up when the index is valid in the insertionList but too high for the blah list. Which is where i'm confused, surely the size of my blah list should have no bearing on where it can be inserted. I've tried inserting a String[] as well but the same thing happens.

Can anyone replicate this or point out my mistake?

Thanks in advance,

Seb

Tagged:

Answers

  • edited May 2014

    What you see here is the same as arrays. The 1st element is at position 0 of array. 2nd at 1, etc..

    When you are trying to insert a StringList into another at position 3, with the current StringList being a length of 4, you aren't placing it between element 3 and 4, but rather 4 and 5, as array qualities apply, and there is clearly no element at position 5.

    I'm sure you know enough about programming to know that if you were modifying a value at position 3, you wouldn't do: array[3] = foo. You would probably do array[2] = foo.

    So if you want to insert a collection of elements between element 3 and 4, you must use insertionList.insert(2,blah);, just like you would do with arrays.

    -- MenteCode

  • Thanks for the response MenteCode. Unless I'm misunderstanding which list is going into which this should work, right?

    StringList blah = new StringList();
    StringList insertionList = new StringList();
    
    void setup(){
    blah.append("zing");
    //blah.append("boom");
    //blah.append("whap");
    insertionList.append("kapow");
    insertionList.append("kapowski");
    insertionList.append("kapowskidoo");
    insertionList.append("kapowskidont");
    insertionList.insert(1,blah);
    println(insertionList);
    
    }
    

    insertionList definitely has values 1 and 2 (2nd and 3rd) so this should be ok. What's more frustrating is that this throws an exception but if I uncomment "boom" or "whap" it works exactly as expected. I'm getting the impression the index has to be valid in the list being inserted too (blah) which seems totally strange.

    The same applies if I uncomment "boom" line and increase the insert index to 2, exception thrown. Keeping the index at 2, uncommenting "whap" line, 2 becomes a valid index in blah and it works.

    Thanks again.

  • edited May 2014 Answer ✓

    It is a bug that should be reported to GitHub, if not already there:

      public void insert(int index, String[] values) {
        if (index < 0) {
          throw new IllegalArgumentException("insert() index cannot be negative: it was " + index);
        }
        if (index >= values.length) {
          throw new IllegalArgumentException("insert() index " + index + " is past the end of this list");
        }
    

    The second test should be index >= data.length.

    IntList and FloatList don't have this bug...

  • Thanks a lot PhiLho, I actually noticed that the IntList and FloatList insert() functions were corrected in 2.0.2 https://github.com/processing/processing/issues/1929

  • edited May 2014

    What's the best way to add this function into my code? I've been meaning to work this out actually, how does one add a function that works on an existing class. i.e if I add this function how can I declare it so that it can still be called by StringList.insert(index,values). I assume I'll need to change the function name actually to avoid a conflict.

    Thanks again

  • Ah, I searched for StringList, had not the idea to search for siblings...

    Actually, the StringList is here mostly for symmetry, I suppose. You can do most of (or more) what it does with a good old ArrayList<String>.

  • Great, thanks. When I saw that int and float had been corrected I assumed string hadn't because it was already correct but I guess it'll be corrected shortly. Thanks for all the help.

Sign In or Register to comment.