You are being forwarded to the lastest updates ot his page!
Or you can Click Here if it doesn't work or you don't wish to wait.

List interface

anrup kris
I am confused by the List interface.It is supposed to represent an ordered collection.But it looks like the collection is ordered
only when it is created.If I use add()method on the list,the new element is appended to the end of the list.In that case,isn't the inherent ordering of the collection lost?
I am sure I am missing something important here.Can anybody help?

thanks
kris


Frank Carver
List is "ordered" in the sense that things in the list stay in the order in which you put them. So if I create a list and add three elements:

code:

List list = new ArrayList();
list.add("Hello");
list.add("There");
list.add("World");

then the three elements will remain in that order:

code:

Iterator it = list.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}

will print out

Hello
There
World

This is in contrast to an unordered collection, such as a Hashtable, where there is no guarantee about the order of the elements when you retrieve them.


Frank Carver
I also fee that this is probably more appropriate to the Java in General (Intermediate) forum, so I've moved it.