Java, Shifting Elements in an Array

Java, Shifting Elements in an Array

Assuming your array is {10,20,30,40,50,60,70,80,90,100}

What your loop does is:

Iteration 1: array[1] = array[0]; {10,10,30,40,50,60,70,80,90,100}

Iteration 2: array[2] = array[1]; {10,10,10,40,50,60,70,80,90,100}

What you should be doing is

Object temp = pool[position];

for (int i = (position - 1); i >= 0; i--) {
array[i+1] = array[i];
}

array[0] = temp;

Shifting elements of an array to the right

There are several issues in this code:

  1. It returns null when places == 0 -- without shift, the original array needs to be returned
  2. In the given loop implementation the major part of the array may be skipped and instead of replacing the first places elements with 0, actually a few elements in the beginning of the array are set to 0.

Also it is better to change the signature of the method to set places before the vararg array.

So to address these issues, the following solution is offered:

public static int[] shiftWithDrop(int places, int ... array) {
if(array == null || places <= 0) {
return array;
}
for (int i = array.length; i-- > 0;) {
array[i] = i < places ? 0 : array[i - places];
}

return array;
}

Tests:

System.out.println(Arrays.toString(shiftWithDrop(1, 1, 2, 3, 4, 5)));
System.out.println(Arrays.toString(shiftWithDrop(2, new int[]{1, 2, 3, 4, 5})));
System.out.println(Arrays.toString(shiftWithDrop(3, 1, 2, 3, 4, 5)));
System.out.println(Arrays.toString(shiftWithDrop(7, 1, 2, 3, 4, 5)));

Output:

[0, 1, 2, 3, 4]
[0, 0, 1, 2, 3]
[0, 0, 0, 1, 2]
[0, 0, 0, 0, 0]

How to shift elements to left after removing array element?

Here is another variant:

    int[] arr = new int[] { 1, 2, 30, 4, 5, 30, 6 };

int k = 30;
int j = 0;

for (int i = 0; i < arr.length; i++) {
if (arr[i] != k) {
arr[j++] = arr[i];
}
}
while (j < arr.length) {
arr[j++] = 0;
}

How to shift an entire array one position to the right?

First, the loop variable is supposed to be going up. so change i-- to i++. Then, if I correctly understand what you're trying to do, you need a[i] = a[i-1] in the loop body. instead of a[i] = a[i] (which does nothing). Also, you don't need aux.

It's not clear what, if anything, you want to happen to a[0] when everything moves to the right. That should take place after the loop exits. (If you want the last element to move to the first element position, then I retract my comment about tmp; you'll need to store that last element somewhere so it's available after the array element gets overwritten. But it only needs to be stashed once, before the loop starts.)

One other comment: n must never be more than a.length or the loop will throw an exception. If n is always equal to a.length, then you can dispense with the argument and just use a.length in the method.

Java Shifting Elements in an Array with a For-loop

They key is to watch how values changes across the course of the loop. Since this isn't language-specific, let's look at it in JavaScript so we can run it here on-site (I've also changed values to v to make the messages shorter so they don't wrap):

var msg;var v = [1, 2, 3, 4, 5];var temp = v[v.length - 1];for (var i = v.length - 2; i >= 0; i--){  msg = "Replacing v[" + i + " + 1] with v[" + i + "]: " + JSON.stringify(v);  v[i + 1] = v[i];  msg += " => " + JSON.stringify(v);  console.log(msg);}msg = "Replacing v[0]     with temp: " + JSON.stringify(v);v[0] = temp;msg += " => " + JSON.stringify(v);console.log(msg);

How to shift all elements in an array to the left?

This is circular left shift by 1 element .You can consider k value as 1 here.

 public class ArrayCircularShift {

public static void main(String[] args) {
int origArray[] = {3,2,1,4,5,6};

// right shift by k
int k = 3;
int size = origArray.length;
k = k % size;
int rightShiftedArray[] = new int[size];

//right shift
for(int i =0 ;i < size ; i++)
{
rightShiftedArray[(k + i ) % size ] = origArray[i];
}

System.out.println("right shifted array");
for(int ele : rightShiftedArray)
System.out.println(ele);

//left shift by k
int[] leftShiftedArray = new int[size];

for(int i=0; i < size ; i++)
{
int pos = i - k;
if(pos >= 0)
leftShiftedArray[pos] = origArray[i];
else
leftShiftedArray[pos + size] = origArray[i];
}

System.out.println("left shifted array");
for(int ele : leftShiftedArray)
System.out.println(ele);
}
}

Shifting all elements of an array back one in Java

You could try with something like this:

int[] originalArray = ...
int[] shiftedArray = new int[originalArray.length];

System.arraycopy(originalArray, 0, shiftedArray, 1, originalArray.length - 1);
shiftedArray[0] = originalArray[originalArray.length - 1];

EDIT
As suggested, only one array is actually needed

int[] originalArray = ...
int lastItem = originalArray[originalArray.length - 1];

System.arraycopy(originalArray, 0, originalArray, 1, originalArray.length - 1);
originalArray [0] = lastItem;


Related Topics



Leave a reply



Submit