• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

put value in list at the beginning

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you took a look at the docs of the java.util.ArrayList here ?? take a look at the several add() methods.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give you a hint....


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

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thank you

add(0, Object element)

clearly:-)

thanks!
 
salvin francis
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic