user1051043
user1051043

Reputation: 45

Shifting first indexes

I have an array size of 3, and I am trying to shift the first index to the last place, while shifting the others to the left. for example:

{1,2,3} to {2,3,1}

here is my code

 int[] nums = {1,2,3}; 
 int[] numsShifted = new int[3];

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

The issue I am having is the last index of the array, i get the incorrect value. Thanks.

Upvotes: 0

Views: 93

Answers (7)

StepTNT
StepTNT

Reputation: 3967

If you want to use the code you wrote, just fix it as I did. Otherwise, the other answers are quite good!

    int[] nums = {1, 2, 3};
    int[] numsShifted = new int[3];

    for (int i = 0; i < nums.length; i++) { //Do one more iteration
        if (i + 1 > 2) { //If this is the last element, we take the first one of nums
            numsShifted[i] = nums[0];
        } else { //Otherwise we do as you did in your code
            numsShifted[i] = nums[i + 1];
            numsShifted[i + 1] = nums[i];
        }            
    }
    System.out.println(Arrays.toString(numsShifted));

EDIT : Remove tempNum as you don't need it

Upvotes: 0

JRL
JRL

Reputation: 78033

Here are some other options, without loops:

public static int[] queueFirst(int[] in) {
   int len = in.length;   
   if (len <= 1) return in;
   int[] ret  = Arrays.copyOf(Arrays.copyOfRange(in, 1, len), len);
   ret[len - 1] = in[0];
   return ret;
}

or for references:

public static <T> T[] queueFirst(T[] in) {
   if (in.length <= 1) return in;
   ArrayList<T> n = new ArrayList<T>(Arrays.asList(in));
   n.add(n.remove(0));
   return n.toArray(in);
}

Upvotes: 0

Ravi Jain
Ravi Jain

Reputation: 1482

int[] a = {1,2,3};
int[] b = new int[3];

for(int j = 0 , i = 2 ; j < a.length ; j++)
{
     b[i++] = a[j];
     if(i >= 3)
     i = 0;
}

Upvotes: 0

Tudor
Tudor

Reputation: 62469

Just do a simple shift and then copy the first number on the last position:

 int[] nums = {1,2,3}; 
 int[] numsShifted = new int[3];

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

Edit: You don't actually need to safe the first item, since you are not overwriting the original array.

Upvotes: 2

evanwong
evanwong

Reputation: 5134

int[] nums = {1,2,3}; 
int[] numsShifted = new int[3];

    for(int i = 0; i < nums.length - 1; i++)
    {
        int tempNum = nums[i]; //tempNum=1, tempNum=2
        numsRotated[i] = nums[i + 1]; //numsRotated[0]=2, numsRotated[1]=3
        numsRotated[i+1] = tempNum; //numsRotated[1]=1, numsRotated[2]=2  <- this is incorrect and the loop ends
    }

at the end, you have 2,3,2. You will need to fix your loop.

Upvotes: 0

twain249
twain249

Reputation: 5706

you have to save the nums[0] value;

int saved = nums[0];

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

Upvotes: 0

Nitin Chhajer
Nitin Chhajer

Reputation: 2339

Well you need to store the first element outside the loop and then begin shifting. After the end of the loop simply put the first element stored as the last element of the array.

Upvotes: 0

Related Questions