This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes put value in list at the beginning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "put value in list at the beginning" Watch "put value in list at the beginning" New topic
Author

put value in list at the beginning

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have a ArrayList, when I put a value, the value is put at the end of the list.
How can I put it at the beginning of the list?
Do I have to use a comparator or is there a easier better way?

Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
have you took a look at the docs of the java.util.ArrayList here ?? take a look at the several add() methods.


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43905
    
  32
You wouldn't usually use a Comparator with a List. A List "remembers" the order you insert the elements, and if you follow omar al kababji's hint, you can select a different insertion ordering.
A Comparator is used with some Sets so as to order the elements in the order of the elements' "value". Look at Comparator and this Set interface.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Let me give you a hint....


You want to Add an element to the 0th position of an ArrayList



My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Okay thank you

add(0, Object element)

clearly:-)

thanks!
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

you are welcome
 
Have you checked out Aspose?
 
subject: put value in list at the beginning
 
jQuery in Action, 3rd edition