Harley Whales
Harley Whales

Reputation: 23

How do I shift all values in an array to the right and insert a new first value?

Here is the code I have so far

for (int i = 0; i < (arr.length - 1); i++) {
    arr[i+1] = arr[i];
}

arr[0] = newNum;

The issue I am having is that this code copies the initial first value, arr[0], to every other slot in the array.

For example, [2, 7, 5, 3], and inserting the new Number 6, would output [6, 2, 2, 2].

Any help would be greatly appreciated.

Upvotes: 2

Views: 2908

Answers (4)

Mr.Manticor
Mr.Manticor

Reputation: 1

Yes, of course it's funny to answer after 5 years as the question was asked. But still)) If you do it through reading from left to right, then I did this:

public static int[] ArrayShift (int[] mass)
    {   int previous= mass[0], next, last;
        last = mass[mass.length-1];
        for (int i=0; i < mass.length-1; i++)
        {
            if (i==0)
            {
                previous = mass[i+1];
                mass[i+1] = mass[i];
            }
            else
            {
                next = mass[i+1];
                mass[i+1] = previous;
                previous = next;
            }
        }
        mass[0] = last;
        return mass;
    }

The only thing is that I inserted the very last element of the array into the first element.

Upvotes: -1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476567

The problem is that your loop works left to right. By shifting to the right, and then shifting the next to the right, you will thus spread the first value accross the array.

Say the list is [1,4,2,5]. Then the first iteration will result in:

   [1,4,2,5]
-> [1,1,2,5]

Next i = 1, and thus:

   [1,1,2,5]
-> [1,1,1,5]

and finally if i = 2, then:

   [1,1,1,5]
-> [1,1,1,1]

You can solve the issue by iterating right to left:

for (int i = arr.length - 2; i >= 0; i--){ //array works right to left
    arr[i+1] = arr[i];
}   
arr[0] = newNum;

Now the shifting will happen as follows, for our example, i starts with i = 2, so after the first iteration:

   [1,4,2,5]
-> [1,4,2,2]

Now we decrement i and copy a[1] to a[2]:

   [1,4,2,2] 
-> [1,4,4,2]

and finally i = 0 and we thus copy a[0] to a[1]:

   [1,4,4,2]
-> [1,1,4,2]

So now the content is shifted to the right, and we can set a[0] to the new value.

Upvotes: 4

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

Don't reinvent the wheel. Java comes with a static method for copying arrays, that will do nicely here. It's System.arraycopy and for your case, you'd do this.

System.arraycopy(arr, 0, arr, 1, arr.length - 1);
arr[0] = newValue;

The arguments to System.arraycopy are

  • the array you're copying from
  • the position of the first element you want to copy
  • the array you're copying to - and it's OK for this to be the same array as you're copying from
  • the destination position of the first element that you want to copy
  • the number of elements you want to copy.

Upvotes: 3

luk2302
luk2302

Reputation: 57114

You have to start from the back and go to the beginning:

for (int i = arr.length - 1; i >= 1; i--) {
    arr[i] = arr[i - 1];
}

arr[0] = newNum;

Upvotes: 3

Related Questions