Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java static method circulate which takes an ArrayList <Integer> and crea

ID: 3825928 • Letter: W

Question

Write a Java static method circulate which takes an ArrayList <Integer> and creates a shiftedcopy of it. Circulate shifts all elements forward in the list to a new position based on the integerparameter index. If the elements shift off the end of the list, they wrap around to the beginning.
Here is the header for this method:
ArrayList<Integer> Circulate (ArrayList<Integer> list, int index)
You can assume that parameters list 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: [0, 1, 2, 3, 4, 5]

C. If the passed list is: [0, 1, 2, 3, 4, 5] and index = 5,the returned list will be: [1,  2, 3, 4, 5, 0]?

Explanation / Answer

Hi, Please find my implementation.

import java.util.ArrayList;

public class ShiftElements {

   public static ArrayList<Integer> Circulate(ArrayList<Integer> list, int index){

       ArrayList<Integer> shiftedcopy = new ArrayList<>();

       int start = list.size()-index;

       for(int i=start; i<list.size(); i++)

           shiftedcopy.add(list.get(i));

       for(int i=0; i<start; i++)

           shiftedcopy.add(list.get(i));

       return shiftedcopy;

   }

   public static void main(String[] args) {

       ArrayList<Integer> list1 = new ArrayList<>();

       list1.add(0);

       list1.add(1);

       list1.add(2);

       list1.add(3);

       list1.add(4);

       list1.add(5);

       System.out.println(Circulate(list1, 2));

       System.out.println(Circulate(list1, 6));

       System.out.println(Circulate(list1, 5));

   }

}

/*

Sample run:

[4, 5, 0, 1, 2, 3]

[0, 1, 2, 3, 4, 5]

[1, 2, 3, 4, 5, 0]

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote