This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 38045
    
  22
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: 917

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: 917

you are welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: put value in list at the beginning
 
Similar Threads
JSF - Renderized String of tags...
question about "new"
maxlength for customer id column
Question with selectOneMenu
XML One liners for the exam....