Hi,
Is the Array List class ordered or unordered? Does ordered mean the same thing as sorted? Or, whatever order i add elements to the Array List object, i get it back in the same order -- is it what we mean by ordered? for ex., if i add in the sequence "5" "4" "3" "2" "1" i get back in the same order.
Also, we expect a ClassCastException to be thrown when the object which is added is of incompatible type. In this context, consider the following code and its output :
<code>
// creating an Array list
/* ArrayList inherits AbstractList which inherits AbstractCollection
and implements the List interface
1. Lists are ordered -- doubtful
2. Lists allow duplicates
3. Lists allow duplicate null also
*/
import java.util.*;
class MyClass
{
private int i;
public MyClass(int j)
{
i=j;
}
}
public class ArrayListEx
{
public static void main(
String[] args)
{
// Create the Array List
ArrayList al=new ArrayList();
al.add("First");
al.add("Second");
al.add("Third");
al.add(null);
al.add(null);
System.out.println("Contents of a1 : " + al);
MyClass m = new MyClass(2);
al.add(m);
System.out.println("Contents of a1 : " + al);
}
}
</code>
The output is
Contents of al:[First,Second,Third,null,null]
Contents of al:[First,Second,Third,null,null,MyClass@273d3c]
No ClassCastException was raised.
Any clarifications?g
------------------
Regards,
Shree