• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

order of elements in Vector

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80055
409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 418
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Bartender
    Posts: 1844
    Eclipse IDE Ruby Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks all. Synchronized ArrayList worked.
     
    Ranch Hand
    Posts: 1170
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Are you sure you need the ArrayList to be synchronized? I have rarely found this useful or necessary.
     
    Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
    New web page for Paul's Rocket Mass Heaters movies
    https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
    reply
      Bookmark Topic Watch Topic
    • New Topic