File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes order of elements in Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "order of elements in Vector" Watch "order of elements in Vector" New topic
Author

order of elements in Vector

anjali ray
Greenhorn

Joined: Mar 12, 2005
Posts: 28
I am adding elements in a vector. When I am getting the elements out of the vector they are not in the same order as I entered them. I want to use Vector as I need them synchronized.
How can I get the elements in the same order.
Thanks,
Anj
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You need them synchronized?

Usually if you just use the add method of Vector, it adds the element to the end of the Vector.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
Keith Lynn has told you that items added to a Vector are added to the end. So they are retained in the order of addition. To get them in order, use the standard methods
  • Iterator,
  • for-each loop
  • for loop.
  • If this is for an assignment, use an Iterator.

    As Keith Lynn has suggested, if you don't need synchronization, you might be better using an ArrayList.
    Raj Kumar Bindal
    Ranch Hand

    Joined: Apr 15, 2006
    Posts: 418
    I don't understand how are you getting the elements in a different order,as compared to the order in which you have inserted them.
    I have never got the sequence of elements in the different order.Can you give some example.
    Joel McNary
    Bartender

    Joined: Aug 20, 2001
    Posts: 1824

    And, if you need a synchronized list, you don't need to use Vector anyway:


    Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
    anjali ray
    Greenhorn

    Joined: Mar 12, 2005
    Posts: 28
    Thanks all. Synchronized ArrayList worked.
    Mr. C Lamont Gilbert
    Ranch Hand

    Joined: Oct 05, 2001
    Posts: 1170

    Are you sure you need the ArrayList to be synchronized? I have rarely found this useful or necessary.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: order of elements in Vector