This week's giveaway is in the Java/Jakarta EE forum. We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line! See 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.
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 ]
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 ]
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.
posted 14 years ago
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 ]
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 ]
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).