• Post Reply Bookmark Topic Watch Topic
  • New Topic

Quick question  RSS feed

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a simple question. I have an array but now want to add something to the first element of it without deleting the first element that already exists there (ie, the original first element moves up to the next element...). The only way I can think of doing this is to move all the elements up one place and then add the new element - but this doesn't seem to be a very good idea.
Alternatively, I could use vectors instead to maintain these elements but when I add the new element will I have to do the same as I would for the array or would placing the new element in the first position of the vector automatically move everything else up one place?
Please help. I need some advice.
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty much what you have to do if you use an array. The method System.arraycopy will do what you need as rapidly as possible. If using a List implementation (e.g. Vector, ArrayList, or LinkedList), the method add(int index, Object element) will do what you want
Note that, since Vector and ArrayList simply encapsulate an array, they use System.arraycopy behind the scenes.
If you're adding a lot of elements to the start or middle of a list, your best bet (for performance considerations) is to use a LinkedList.
[ March 17, 2004: Message edited by: Jeff Langr ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you use a linked list instead?
look here
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using an actual array would require you to shift all the elements over one spot before you insert your own at the front. You would do this by creating a new array, and using System.arraycopy to copy over all of them leaving the empty space.
You could use something from the java.util package like ArrayList which allows you to simply specify ArrayList.add(0, element) to insert it into the first position without further bother to you.
[ March 17, 2004: Message edited by: Nathaniel Stoddard ]
 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I use System.arraycopy do I need to insert the first element into the new array first and then invoke this method? Please could you explain.
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to make sure the array is large enough first. Then do the system array copy to copy elements from position 0 through n-1 to position 1 in the array. Finally set the new value at position 0.
The second best thing to do is to take a look at the code in java.util.ArrayList, in the method add(int,Object).
The best thing to do is dispense with the array and use a LinkedList or ArrayList. That's the point of objects--they can abstract away the ugliness of things like having to do a System.arraycopy.
Jeff
[ March 17, 2004: Message edited by: Jeff Langr ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the OP,
Seems every question you post at JavaRanch, you also post at Sun Forum.
As a result, many of the people trying to help you with solutions have simply had their time wasted because the solution was provided earlier at
either JavaRanch or Sun Forum.
Sun Forum link
(edited to add link)
[ March 17, 2004: Message edited by: Michael Dunn ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While we certainly discourage posting the same question in multiple JavaRanch Saloon forums, I think that posting the same question in different forums run by different organizations, ain't such a bad thing.
While it may seem a bit frustrating seeing efforts of help duplicated, don't forget that when someone is helped out here, not just the original poster might learn a thing or two. Many Ranchers have a good chance to read the thread. Many of these Ranchers might not frequent other forums sites for whatever reason(s).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!