0

Hello there.
I have a working method here, called rotateRight() that takes an array and shifts all the elements to the right, except the last one that is placed at the start

I have debugged the method, and the method works fine. However the changes are not being reflected in main(). What should I change to fix that?

Thank you for your time.

B.

import java.util.*;

public class TestRotateRight {
    public static void main(String[] args) {
        int[] list = {3, 8, 19, 7};
        rotateRight(list);
        System.out.println(Arrays.toString(list));   // [7, 3, 8, 19]
        rotateRight(list);
        rotateRight(list);
        System.out.println(Arrays.toString(list));   // [8, 19, 7, 3]
        rotateRight(list);
        System.out.println(Arrays.toString(list));   // [3, 8, 19, 7]
        rotateRight(list);
        rotateRight(list);
        rotateRight(list);
        System.out.println(Arrays.toString(list));   // [8, 19, 7, 3]
        rotateRight(list);
        rotateRight(list);
        rotateRight(list);
        rotateRight(list);
        System.out.println(Arrays.toString(list));   // [8, 19, 7, 3]
    }

    public static void rotateRight(int[] array){
        int temp = array[array.length-1];
        int[] arrayTemp = new int[array.length];
        
        arrayTemp[0] = temp;
        
        for (int i = 0; i <= array.length - 2; i++) {
            arrayTemp[i+1] = array[i]; 
        }
        
        array = arrayTemp;
        
    }
    
}

Edited by Oppression: n/a

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by ztini
1

u needa return the sorted array from the rotateRight function

public static int[] rotateRight(int[] array){

and at the end of the function

return array;

and in the main

list = rotateRight(list);

hope this helps

Edited by kekkaishi: n/a

2

FYI - class names should not contain test unless they extend JUnit.TestCase. It is also considered good form to create a test class instead of the tests you have in the main method.

Test driven development will be very important to your career later; don't get into bad habits now. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.