write a Java static method circulate which takes an ArrayList Integer> and creat
ID: 3880077 • Letter: W
Question
write a Java static method circulate which takes an ArrayList Integer> and creates a shifted copy of it. Circulate shifts all elements forward in the list to a new position based on the integer parameter index. If the elements shift off the end of the list, they wrap around to the beginning. Here is the header for this method: ArrayListInteger Circulate (ArrayListInteger» list, int index) You can assume that parameters 1ist is not null and index is non-negative. Here are some examples: A. If the passed list is: [0, 1, 2, 3, 4, 5] and index = 2, the returned list will be: [4, 5, 0, 1, 2, 3] B. If the passed list is: [0, 1, 2, 3, 4, 5] and index = 6, the returned list will be: [e, 1, 2, 3, 4, 5] If the passed list is: the returned list will be: [1, 2, 3, 4, 5, 0]? C. [0, 1, 2, 3, 4, 5] and index = 5,Explanation / Answer
Java Method:
public static ArrayList<Integer> Circulate(ArrayList<Integer> list, int index)
{
int temp; //to store last value
int n = list.size(); //get list size
//circulate index no. of times
for(int i=1;i<=index;i++)
{
temp = list.get(n-1); //get last element
//shift each element by one index
for(int j=n-1;j>0;j--)
{
list.set(j, list.get(j-1));
}
list.set(0, temp); //set first element to temp(last element)
}
return list; //return list
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.