Pixie
Pixie

Reputation: 311

Update array with resulting array from calling a function

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.

Upvotes: 1

Views: 115

Answers (5)

CAW
CAW

Reputation: 389

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.

Upvotes: 2

mwrdy
mwrdy

Reputation: 1

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);
        }
}

Upvotes: 0

Someone
Someone

Reputation: 444

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);
        }
}

Upvotes: 1

Curious
Curious

Reputation: 473

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); }

Upvotes: 2

XtremeBaumer
XtremeBaumer

Reputation: 6435

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

Upvotes: 1

Related Questions