Question: Write a function in the Java programming language that will rotate the values in an array of integers. There are two parameters to the function:

Write a function in the Java programming language that will rotate the values in an array of integers. There are two parameters to the function: the array to rotate and the magnitude/direction to rotate. If directional number is negative, then you are rotating the contents of the array left; if it is positive, then to the right. The second parameter also indicates how far to rotate. You will not modify the parameter array, but will instead return a new array with the elements rotated.
For example:
rotateArray([1,2,3,4,5],-1) should return a new array [2,3,4,5,1], because the second parameter is -1, it rotates to the left by one position.
rotateArray([-12,55],-3) should return a new array [55,-12]. Again, this is a left rotate three times.
rotateArray([1,1,2,3,5,8,11],2) should return a new array [8,11,1,1,2,3,5]. The second number is positive indicating a right rotation by two positions.
rotateArray([],1) should return a new array []. There are no contents to rotate, so an empty array is returned.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!