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.
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.
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.
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)
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).
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?
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)
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.
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).
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.
Ruhul said:
1 decade ago
In arrayList The colection are ordered in which order they are inserted.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers