Confusion with Vector

Hi,

Vector is a List. And List is an ordered collection (also known as a sequence) as documented in javadoc for List.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html

I used a vector and added the following values

"Orange"

"Mango"

"Apple"

and then printed all the element of this vector using a for loop.

I was assuming that as it is a list implementation, i wll get the sorted result

as Apple, Mango and then Orange.

But I got the items in the sequence as I had inserted into.

Am I meaning something wrong whatever I found in documentation for Vector?

Thanks

[653 byte] By [JL.Nayaka] at [2007-11-27 11:24:48]
# 1

> I was assuming that as it is a list implementation, i wll get the sorted result

"Ordered" does not mean "sorted".

> But I got the items in the sequence as I had inserted into.

That's the expected behavior. If you want the elements sorted, use a different implementation of List or use Collections.sort().

~

yawmarka at 2007-7-29 16:00:31 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks

JL.Nayaka at 2007-7-29 16:00:31 > top of Java-index,Java Essentials,Java Programming...
# 3

Check TreeSet

manuel.leiriaa at 2007-7-29 16:00:31 > top of Java-index,Java Essentials,Java Programming...
# 4

> Check TreeSet

If you do, make sure you want a Set and not a List. :o)

~

yawmarka at 2007-7-29 16:00:31 > top of Java-index,Java Essentials,Java Programming...
# 5

> > Check TreeSet

>

> If you do, make sure you want a Set and not a List.

> :o)

>

> ~

Plus even when dealing with lists, its better to work with ArrayList than Vectors, if synchronization is an issue there are concurrency classes as well

kilyasa at 2007-7-29 16:00:31 > top of Java-index,Java Essentials,Java Programming...
# 6

yes exactly . Vector is ordered according to insertion order.meaning elements will be fetched in the exact same order u inserted them.

Unlike ArrayList which is ordered according to indexes.

sjosh_theonea at 2007-7-29 16:00:31 > top of Java-index,Java Essentials,Java Programming...
# 7

> yes exactly . Vector is ordered according to

> insertion order.meaning elements will be fetched in

> the exact same order u inserted them.

>

> Unlike ArrayList which is ordered according to

> indexes.

I thought you were killing yourself. ArrayList orders elements the same way Vector does, by the way

georgemca at 2007-7-29 16:00:31 > top of Java-index,Java Essentials,Java Programming...
# 8

> yes exactly . Vector is ordered according to

> insertion order.meaning elements will be fetched in

> the exact same order u inserted them.

>

> Unlike ArrayList which is ordered according to

> indexes.

Wrong, exactly. Both Vector and ArrayList use insertion order. sjosh_theone, I recommend you concentrate on getting your file names straight* before giving out any more "advice".

*

http://forum.java.sun.com/thread.jspa?threadID=5197936

~

yawmarka at 2007-7-29 16:00:31 > top of Java-index,Java Essentials,Java Programming...