Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting a new item in arryalist.

 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an arryalist of 10 (say) items in it.
I want to push "x" item (not known what it's index might be) to be the second item in the list.
e.g
Arraylist a =[a,b,c,d,e,f,X,h,i,j];
I want to change the items order such that A becomes:
Arraylist a=[a,X,b,c,d,e,f,h,i,j]
Can someone sugest me way to do it?
Thanks!!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList::remove( int )
ArrayList::add( int , object )
[ August 15, 2002: Message edited by: Dirk Schreckmann ]
 
Peter Kristensson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

that should do it.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I ask why you want to do that. I ask because maybe ArrayList is not what you really want to use. There isn't enough information in the question to know that.
 
Gemini Moses
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter and Dirk...
Peter actually it is not pure java application hence i needed this thing to be done in arryalist itself.
Thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic