This discussion is archived
9 Replies Latest reply: Nov 11, 2010 5:21 AM by jtahlborn RSS

Add elements to arrylist from the end of the list

804485 Newbie
Currently Being Moderated
Is it possible to create a new arraylist with, say, 1000 int cap, and start to add elements from the end?
I.e.:

myList.add(999, "xyz");
myList.add(998, "bla");
myList.add(997, "foo");
etc...

When i try to do this, i get out of bounds exception because the arraylist size is still 0...

thanks
  • 1. Re: Add elements to arrylist from the end of the list
    PhHein Guru Moderator
    Currently Being Moderated
    No, you'd have to fill the list with dummy values first
  • 2. Re: Add elements to arrylist from the end of the list
    JoachimSauer Journeyer
    Currently Being Moderated
    The size of a List is defined as the number of elements it holds.

    A newly created ArrayList will always be empty.

    If you want to place an element at a specific position n, then you have to ensure that it already holds at least that many elements.

    So if you want to place an element at position 999, then the ArrayList must contain at least 999 elements (which may be null).

    So you could add null 1000 times to your List and then set the "real" values at the specified position, but I wouldn't do that.

    Chances are that you need a Map or something similar.

    Could you explain why you'd want to do that, using a real example?

    Edited by: Joachim Sauer on Nov 10, 2010 4:01 PM
  • 3. Re: Add elements to arrylist from the end of the list
    804485 Newbie
    Currently Being Moderated
    Thanks.

    The reason for doing it this way is that i'm traversing through another arraylist backwards while doing some calculations on that and adding the results to the new arraylist. The result is ofc that the list is in reverse order. I was hoping there was a simple solution to this other than calling reverse(), but i guess that will have to do.

    The reason im using arraylist is that i need to maintain order and be able to traverse backwards.

    thanks
  • 4. Re: Add elements to arrylist from the end of the list
    JoachimSauer Journeyer
    Currently Being Moderated
    user4199244 wrote:
    Thanks.

    The reason for doing it this way is that i'm traversing through another arraylist backwards while doing some calculations on that and adding the results to the new arraylist. The result is ofc that the list is in reverse order. I was hoping there was a simple solution to this other than calling reverse(), but i guess that will have to do.
    What's wrong with using reverse? It's a perfectly reasonable method call.

    if you really want to avoid using reverse() you can use a LinkedList instead of an ArrayList and always insert at the first position, which will result in a List in the same order as the original one. Note that this might have some other effects as LinkedList has some different performance characteristics than ArrayList (that's the reason we've chosen it here: inserts at position 0 behave much nicer on a LinkedList than on an ArrayList, but in other situations it might be undesireable).

    And if you already know the size of the result, you could also create an array of the desired size and just fill it from the end (arrays have a fixed size and will be "pre-filled" with null values).

    Edited by: Joachim Sauer on Nov 10, 2010 4:49 PM
  • 5. Re: Add elements to arrylist from the end of the list
    804485 Newbie
    Currently Being Moderated
    thanks. I wanted to avoid reverse() if it was possible simply to avoid overhead. When running though these calculations/iterations a couple of hundred million times, every ms counts :)
  • 6. Re: Add elements to arrylist from the end of the list
    DrClap Expert
    Currently Being Moderated
    user4199244 wrote:
    thanks. I wanted to avoid reverse() if it was possible simply to avoid overhead. When running though these calculations/iterations a couple of hundred million times, every ms counts :)
    And was your implementation of reverse() actually faster than the version built into the API? Or did you just assume that, even though you weren't familiar with the class, that whatever you threw together would naturally be faster than that?
  • 7. Re: Add elements to arrylist from the end of the list
    804485 Newbie
    Currently Being Moderated
    of course not, i just assumed that if it was possible to add an element to the list in the same manner you can to an array, that i wouldn't need to reverse the entire list, which, without having done any research, i intuitively assume to be somewhat intensive. I may of course be wrong, but it just feels like i have to do twice the work when reversing rather than just putting the element in the correct index in the first place. But by all means, please feel free to enlighten me.
  • 8. Re: Add elements to arrylist from the end of the list
    JoachimSauer Journeyer
    Currently Being Moderated
    user4199244 wrote:
    of course not, i just assumed that if it was possible to add an element to the list in the same manner you can to an array
    That's a bad assumption. A List is not an array and it should not be treated like one. Instead, re-read the documentation of List, to clarify this aspect.
    , that i wouldn't need to reverse the entire list, which, without having done any research, i intuitively assume to be somewhat intensive.
    "somewhat intensive" is a highly subjective term. And intuition is a pretty bad thing to apply to performance optimizatons, because it's terribly easy to get it wrong.
    I may of course be wrong, but it just feels like i have to do twice the work when reversing rather than just putting the element in the correct index in the first place. But by all means, please feel free to enlighten me.
    Of course it may be more work, but it might also be irrelevant. If the actual work you do is in any way significant, then reversing the array is very unlikely to take a significant percentage of the total runtime.

    Also: if you have really got millions of elements, then the correct choice of data structures can have much larger effects than any kind of micro-optimizations that you're talking about here.
  • 9. Re: Add elements to arrylist from the end of the list
    jtahlborn Expert
    Currently Being Moderated
    You could always use an array directly, then return the result using "Arrays.asList()".

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points