ArrayList insertion and retrieval order

JavaCollections

Java Problem Overview


Suppose I insert 5 strings in an ArrayList. Will the order of insertion and retrieval from the ArrayList be the same?

Java Solutions


Solution 1 - Java

Yes. ArrayList is a sequential list. So, insertion and retrieval order is the same.

If you add elements during retrieval, the order will not remain the same.

Solution 2 - Java

Yes, ArrayList is an ordered collection and it maintains the insertion order.

Check the code below and run it:

public class ListExample {

    public static void main(String[] args) {
        List<String> myList = new ArrayList<String>();
        myList.add("one");
        myList.add("two");
        myList.add("three");
        myList.add("four");
        myList.add("five");
    
        System.out.println("Inserted in 'order': ");
        printList(myList);
        System.out.println("\n");
        System.out.println("Inserted out of 'order': ");

        // Clear the list
        myList.clear();
    
        myList.add("four");
        myList.add("five");
        myList.add("one");
        myList.add("two");
        myList.add("three");
    
        printList(myList);
    }

    private static void printList(List<String> myList) {
        for (String string : myList) {
            System.out.println(string);
        }
    }
}

Produces the following output:

Inserted in 'order': 
one
two
three
four
five


Inserted out of 'order': 
four
five
one
two
three

For detailed information, please refer to documentation: List (Java Platform SE7)

Solution 3 - Java

If you always add to the end, then each element will be added to the end and stay that way until you change it.

If you always insert at the start, then each element will appear in the reverse order you added them.

If you insert them in the middle, the order will be something else.

Solution 4 - Java

Yes, it will always be the same. From the documentation

> Appends the specified element to the end of this list. Parameters: e > element to be appended to this list Returns: true (as specified by > Collection.add(java.lang.Object))

ArrayList add() implementation

public boolean More ...add(E e) {
    ensureCapacity(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}

Solution 5 - Java

Yes it remains the same. but why not easily test it? Make an ArrayList, fill it and then retrieve the elements!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondivinedragonView Question on Stackoverflow
Solution 1 - JavaKalai Selvan RaviView Answer on Stackoverflow
Solution 2 - JavaaxcdntView Answer on Stackoverflow
Solution 3 - JavaPeter LawreyView Answer on Stackoverflow
Solution 4 - JavaEduardo DennisView Answer on Stackoverflow
Solution 5 - JavaMehsah YhookView Answer on Stackoverflow