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?
Answer: Option
Explanation:
Yes, always the elements in the collection are ordered.
Discussion:
13 comments Page 1 of 2.
Radistao said:
10 years ago
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!
In same time, D also looks correct! Because every element is accessed by unique key, which is an integer index of element!
(5)
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.
If test maker wants to get A for this question they need to clarify or change the test.
(1)
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?
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.
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).
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).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers