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

Recommended Answers

All 2 Replies

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

Member Avatar for ztini

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. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.