Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

order of elements in Vector

 
anjali ray
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50270
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 418
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1840
    Eclipse IDE Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And, if you need a synchronized list, you don't need to use Vector anyway:
     
    anjali ray
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks all. Synchronized ArrayList worked.
     
    Mr. C Lamont Gilbert
    Ranch Hand
    Posts: 1170
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you sure you need the ArrayList to be synchronized? I have rarely found this useful or necessary.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic