1

I am trying to update an array with the resulting array after providing the original one as a parameter to a function. I am not sure how to achieve this. I haven't covered all the material yet and I need your help. So what I am trying to achieve:

Original array: [1, 2, 3, 4, 5, 6]
n = 3 where n is the number of times the elements of the array will be shifted to the right
Shifted array: [4, 5, 6, 1, 2, 3]

I've created a method that shifts by 1 position. I have another method to switch by N positions where I call the method switching by 1 position. It switches the array once, but then I cannot update my original array with the result from the shifting. So instead of getting:

Original array: [1, 2, 3, 4, 5, 6]
RIGHT SHIFT:    [6, 1, 2, 3, 4, 5]
RIGHT SHIFT:    [5, 6, 1, 2, 3, 4]
RIGHT SHIFT:    [4, 5, 6, 1, 2, 3]  --> **final result** <br>

I get:

Original array:   [1, 2, 3, 4, 5, 6]
RIGHT SHIFT:      [6, 1, 2, 3, 4, 5]
RIGHT SHIFT:      [6, 1, 2, 3, 4, 5]
RIGHT SHIFT:      [6, 1, 2, 3, 4, 5]

My code:

public class Shift {

public static int[] rotate(int[] seq){
    int[] origArr = seq;
    int[] shiftedArrR = new int[seq.length];

    for (int i = 0, j = i+1; j < origArr.length; i++, j++) {
        shiftedArrR[j] = origArr[i];
    }
    shiftedArrR[0] = origArr[origArr.length-1]; 

    System.out.print("RIGHT SHIFT:  ");
    System.out.println(java.util.Arrays.toString(shiftedArrR));
    return shiftedArrR;
}

public static void rotate(int[] seq, int times){

    System.out.println(java.util.Arrays.toString(seq));
    int[] temp = new int[seq.length];

    for (int i = 1; i <= times; i++) {
        temp = rotate(seq);
    }
}


// ------------ MAIN METHOD ----------------
public static void main(String[] args) {

    System.out.print("Original array:   ");
    rotate(new int[]{1,2,3,4,5,6}, 3);
    }

}

Apparently the result of rotate(seq) is not assigned to the seq which is provided to the rotate() method which is supposed to shift it more times. I know it is something very simple and I tried different ways to fix this, but with no luck.

5 Answers 5

2

You are using the unchanged array(seq) in each of the 3 iterations instead of the changed array (temp) here

for (int i = 1; i <= times; i++) { temp = rotate(seq); }

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I indeed knew I was not updating the array and was using it again and again, but wasn't sure where my (apparently stupid) mistake was :)
2

You are calling the rotate(int[] seq) method the correct number of times, but each time you pass in the original unmodified 'seq' array, rather than the output of the previous rotation.

Also, when you create the temp array with int[] temp = new int[seq.length];, you never actually do anything with the new array you have created - it is never read, and is just replaced with another new array (shiftedArrR) returned by the rotate method..

One way around this is just to ditch the temp array, and replace the line inside the loop with seq = rotate(seq);.

Another way is to modify the original rotate method so that it actually overwrites the content of the array which you pass in, rather than returning a new array.

1 Comment

Actually the original idea was for the first rotate method which gets only the sequence, to be a VOID method, I modified it to int[] and returned the array, thought that might be a solution, but still didn't work. Any idea how it would look like if the first one was a VOID?
1
public static void rotate(int[] seq, int times){

    System.out.println(java.util.Arrays.toString(seq));
    for (int i = 1; i <= times; i++) {
        seq = rotate(seq);
    }
}

replace your rotate method with this code. you always rotate the same array with rotate(seq). since you save it in temp you have the problem of calling the method with seq instead of temp. therefore change content of for loop to seq = rotate(seq);. this saves the output from your second rotate method in the array which you the pass again into the method

1 Comment

Thank you! I knew it was something really really simple but I was blind about it. :)
1

Mainly you are using the unchanged array in each iteration instead of the changed array.

Also your rotate(int[] seq) function seem quite confusing. So I update it a little bit.[In a most simple manner]. Also no need to create the extra temp variable to store the array.

 public class RightShift {

    public static void rotate(int[] seq){

        int temp = seq[seq.length - 1];

        for(int i = seq.length -1 ; i>0; --i)
            seq[i] = seq[i-1];

        seq[0] = temp;

        System.out.println("RIGHT SHIFT:  ");
        System.out.println(java.util.Arrays.toString(seq));
    }   

    public static void rotate(int[] seq, int times){

        System.out.println(java.util.Arrays.toString(seq));

        for (int i = 1; i <= times; i++) {
             rotate(seq);
        }
    }


    // ------------ MAIN METHOD ----------------
   public static void main(String[] args) {

        System.out.print("Original array:   ");
   //    rotate(new int[]{1,2,3,4,5,6});
        rotate(new int[]{1,2,3,4,5,6}, 3);
        }
}

1 Comment

Thanks! I will check out this version, thanks for simplifying it. Usually when I have to figure out how to program something (same with maths) I get a very complicated thing in the end while it is just one line of code for example.. Need to update my way of "thinking" somehow.
0

Like most of the guys mentioned, the array did not mutate. Anyway, a simpler approach would be to write just one method for rotation

import java.util.Arrays;

public class Shift {
    public static int[] rotate(int[] seq, int times){
        int temp = 0;
        int round = 0;
        while (round <times) {
            for(int i=0;i<seq.length;i++) {
                if(i == seq.length-1) {
                    seq[seq.length-1] = temp;
                    break;
                }
                if(i == 0) 
                    temp = seq[i];
                seq[i]=seq[i+1];
            }
            round++;
        }
        System.out.println("Rotated array: " +Arrays.toString(seq));
        return seq;
    }

    // ------------ MAIN METHOD ----------------
    public static void main(String[] args) {
        int[] original = new int[]{1,2,3,4,5,6};
        System.out.println("Original array:   " + Arrays.toString(original));
        rotate(new int[]{1,2,3,4,5,6}, 3);
        }
}

1 Comment

That's awesome! I was wondering if I can have a method but provide only one of the parameters after the first update, i.e. the number of shifts. But wasn't sure how to achieve this with just one method and if it is possible at all. I only know you can to do this with overloading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.