Java Programming - Objects and Collections - Discussion

Discussion Forum : Objects and Collections - Pointing out the correct statements (Q.No. 8)
8.
Which statement is true for the class java.util.ArrayList?
The elements in the collection are ordered.
The collection is guaranteed to be immutable.
The elements in the collection are guaranteed to be unique.
The elements in the collection are accessed using a unique key.
Answer: Option
Explanation:

Yes, always the elements in the collection are ordered.

Discussion:
13 comments Page 1 of 2.

John said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
In arrayList The colection are ordered in which order they are inserted.

Vinay kumar said:   1 decade ago
Yes whatever david is exactly right.

Arraylist elements will not be in an order.

So, answer is wrong.

Ravikiran said:   1 decade ago
Yes, as @Rahul said the insertion order is preserved. So answer is correct.

Harish said:   1 decade ago
ArrayList are meant for random access and they are not in ordered form.

Mustapha said:   1 decade ago
ArrayList are not automatically sorted (unlike TreeSet).

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

Marco S said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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.
(1)


Post your comments here:

Your comments will be displayed after verification.