Java Array The method rotateLeft() takes in a reference a to an integer array an
ID: 3771210 • Letter: J
Question
Java Array
The method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array.
For example, if the input array is a = {1, 2, 3, 4, 5}, then rotateLeft(a, 2) should return an array containing {3, 4, 5, 1, 2}.
This method should not make any changes to the input array.
public static int[] rotateLeft(int[] a, int n)
{
int[] temp = new int[a.length];
Complete this method
Explanation / Answer
import java.lang.reflect.Array;
public class ArrayReverse {
public static void main(String[] args){
int[] b={1, 2, 3, 4, 5,6};
rotateLeft(b,3);
}
public static int[] rotateLeft(int[] a,int n){
int[] b=new int[10];
int[] temp=new int[10];
int counter=0;
int inCounter=0;
for(int j=0;j< n;j++){
temp[j]=a[j];
}
for (int i = (n); i < a.length; i++) {
b[counter++] = a[i];
}
for(int i=0;i<n;i++){
b[counter++]=temp[i];
}
for(int i=0;i<a.length;i++){
System.out.println(" "+b[i]);
}
return b;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.