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: 3829310 • 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

ArrayListCircleTest.java

import java.util.ArrayList;


public class ArrayListCircleTest {

   public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<Integer>();
       list.add(0);
       list.add(1);
       list.add(2);
       list.add(3);
       list.add(4);
       list.add(5);
       System.out.println(Circulate(list, 2));
       System.out.println(Circulate(list, 6));
       System.out.println(Circulate(list, 5));
      
   }
   public static ArrayList<Integer> Circulate (ArrayList<Integer> list, int index) {
       if(list.size() - index <= 0){
           return list;
       }
       else{
           ArrayList<Integer> newList = new ArrayList<Integer>();
           for(int i=list.size() -index; i<list.size(); i++){
               newList.add(list.get(i));
           }
           for(int i=0; i<list.size()-index; i++){
               newList.add(list.get(i));
           }
           return newList;
       }
   }

}

Output:

[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