Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList insertion and retrieval order

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

like image 725
divinedragon Avatar asked Jul 04 '12 15:07

divinedragon


People also ask

Is ArrayList an insertion order?

ArrayList maintains the insertion order i.e order of the object in which they are inserted. HashSet is an unordered collection and doesn't maintain any order. ArrayList allows duplicate values in its collection.

Is ArrayList LIFO or FIFO?

ArrayList is random access. You can insert and remove elements anywhere within the list. Yes, you can use this as a FIFO data structure, but it does not strictly enforce this behavior. If you want strict FIFO, then use Queue instead.

What is the order of ArrayList in Java?

An ArrayList can be sorted in two ways ascending and descending order. The collection class provides two methods for sorting ArrayList. sort() and reverseOrder() for ascending and descending order respectively.

What is retrieval operation in ArrayList?

To fetch an element from ArrayList, we can use the get(int index) method. This method takes an index as input and returns the element at that index . The index provided should be equal or greater than zero and should be less than ArrayList size. We can fetch the size of the ArrayList using the size() method.


1 Answers

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.

like image 194
Kalai Selvan Ravi Avatar answered Nov 08 '22 05:11

Kalai Selvan Ravi