Java Programming - Objects and Collections - Discussion

8. 

Which statement is true for the class java.util.ArrayList?

[A]. The elements in the collection are ordered.
[B]. The collection is guaranteed to be immutable.
[C]. The elements in the collection are guaranteed to be unique.
[D]. The elements in the collection are accessed using a unique key.

Answer: Option A

Explanation:

Yes, always the elements in the collection are ordered.


John said: (Jul 21, 2011)  
In what way are they ordered?
I run:

java.util.ArrayList<Integer> al = new java.util.ArrayList<Integer>();
al.add(4);
al.add(3);
al.add(5);

for (Integer i : al) {
System.out.println(i);
}

And I get
4
3
5
as output. Is that ordered?

David Sinclair said: (Dec 14, 2011)  
I think this question might have a wrong answer. I see no mention of ordering in the docs and as John says above testing it shows it not to be ordered.

Ruhul said: (May 27, 2012)  
In arrayList The colection are ordered in which order they are inserted.

Vinay Kumar said: (Sep 18, 2012)  
Yes whatever david is exactly right.

Arraylist elements will not be in an order.

So, answer is wrong.

Ravikiran said: (Jul 7, 2013)  
Yes, as @Rahul said the insertion order is preserved. So answer is correct.

Harish said: (Nov 9, 2013)  
ArrayList are meant for random access and they are not in ordered form.

Mustapha said: (Nov 16, 2013)  
ArrayList are not automatically sorted (unlike TreeSet).

ArrayList are ordered, it keep the same insertion order (unlike HashSet).

Marco S said: (Nov 21, 2013)  
Saying that an ArrayList is ordered is wrong, or at least deceiving. The *insertion order* is preserved if you append an object by calling add (E) , but that's quite different, and the definition of "ordered", used in the question, usually refers to the element values (natural ordering).

Zoltan B. said: (Feb 9, 2015)  
I would also say that the statement "the elements in the collection are ordered" is deceiving or at least not very clear. They certainly keep their position as long as only insertions are performed on the list (which is of course not guaranteed and the question doesn't say anything about how it will be used). But they most certainly will not be in their natural ordering.

B and C are definitely false. So answer D seems the most correct. All elements have a unique key (namely their index). Although if we consider multiple int's with the same values, than perhaps this statement is a bit problematic as well.

Daniil said: (Mar 1, 2015)  
The answer is D. ArrayList has a unique key access, and a key is index of array.

If test maker wants to get A for this question they need to clarify or change the test.

Radistao said: (Dec 4, 2015)  
Sorry, but this is a trap question related not to Java/programming knowledge, but definitions knowledge, like "ordered" != "sorted", but most of developers use "order" and "sort" as synonyms, so you confuse people. To be more consistent you should to fix A choice "The elements in the collection are ordered by insertion order".

In same time, D also looks correct! Because every element is accessed by unique key, which is an integer index of element!

Aakash Kumar said: (Mar 29, 2016)  
How can be an element in the order of ArrayList?

Prakash Vl said: (Jul 29, 2016)  
Wrong Answer!

ArrayList elements are indexed. :)

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.