• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add elements to the left side of an array?  RSS feed

 
Dan Rana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let's say you have an array like this [1 2 3] and you wanted to add 4 to the beginning of that array making it [4 1 2 3]. is there a way of doing this using System.arraycopy maybe?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, if your array really is [1, 2, 3], then you can't get [1, 2, 3, 4] into it. You'll have to create a new array. Arrays' sizes are fixed at creation time.

Now, if it's, say, [1, 2, 3, 0] or [1, 2, 3, -1] or, using the Integer wrapper class rather than int, [1, 2, 3, null], then you can use System.arraycopy to copy the lower 3 elements into the upper 3 spots, and then put 4 in at index 0.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dan !!!

Can you post what you have tried?
 
Dan Rana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the array is [1 2 3] and i'm trying to get it to become [4 1 2 3]. a method named add will add 4. (


 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the data sturcture of array of size n, if one wants to insert an element into it, one has to move every elements one positon after the insertion point, except for appending.

so we say insert operation is not efficient for array which is O(n), if compared with LinkedList, which is O(1).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Rana wrote:is there a way of doing this using System.arraycopy maybe?

Yes; you would have to allocate a new array, and use System.arraycopy to copy the content of the old array to the new array, at the right position, and then you put the new value in the first element of the new array.

Carefully read the documentation of System.arraycopy to learn how you could copy the old array to the new one in the right position.

After doing arraycopy, you don't need to copy the values yourself (what you're doing in lines 22-23 of the code that you posted above). System.arraycopy already did that for you...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Rana wrote:the array is [1 2 3] and i'm trying to get it to become [4 1 2 3]. a method named add will add 4.

Just to let you know, an operation like that is not generally called an "add"; it's called a "push" (or in some classes 'addFirst()'), so if you do decide to implement it that way, make sure you document it WELL.

Another alternative is to allow a bit of spare room at both ends of your array, and keep track of the current 'start' and 'end'. It's a bit trickier but, assuming you do have some room, the add then becomes simply:
arr[--start] = num;

Obviously, you'd need to change your get() and size() methods a bit, but the implementations are very straightforward.

Winston
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That question looks like this thread, if you have a million years spare to read it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!