6 Replies Latest reply on Feb 26, 2008 4:46 PM by 807601

    ArrayList question


      I have an ArrayList instance and I do myArrayList.add(something) several times.

      After that I do something like for (Object obj : myArrayList) { do-something-with-the-object }

      My question is: will the for return my objects in the exact same order as I added them? I tried and it seems like the answer to this question is yes, but can I be sure that it will always behave like that?

        • 1. Re: ArrayList question
          yes, using ArrayList.get(i) on an index number will retrieve the object exactly as added. That is, provided you have not sorted the list, or performed an add(int index, E element) operation.
          • 2. Re: ArrayList question
            The for each loop processes values based on what is returned by the implementation of Iterable. AbstractList implements Iterable for ArrayList, and it returns values in the order they were added. So the short answer is yes, for ArrayList, but the long answer is it depends on how Iterable is implemented for the collection you're looping on.
            • 3. Re: ArrayList question

              So every collection whose Iterable is implemented by AbstractList will return values in the same order as added?

              Thank you both for your answers.
              • 4. Re: ArrayList question
                Implementations of List have a defined order.

                Collections that don't implement List don't have a defined order.

                TreeSet for example implements Set, which extends Collection but does not implement List. It will usually not return the objects in the order that they where added.

                ArrayList and LinkedList both implement List, which requires that the order of objects is defined and constant. You can explicitely change that order, by removing the objects and re-inserting them at other positions.

                There are also other interfaces that define other requirements on the order. For example there's SortedSet, which requires that the iterator returns the objects sorted according to their natural ordering or some provided Comparator.
                • 5. Re: ArrayList question
                  never mind guys,
                  it is stated here that
                  "The iterators returned by this class's iterator and listIterator methods are fail-fast: if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future."

                  so... it's pretty obvious now :-)
                  • 6. Re: ArrayList question
                    so... it's pretty obvious now :-)
                    I don't see how the paragraph you quoted has anything to do with your question.

                    It only says that if you add objects to the List, while you're iterating over it, it will throw some Exception. That doesn't tell me anything about the order of objects being returned by that iterator.