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

public static ArrayList<Integer> doArrayListRemoveFromEnd(int numItems) { System

ID: 3923299 • Letter: P

Question

public static ArrayList<Integer> doArrayListRemoveFromEnd(int numItems) {

       System.out.print("doArrayListRemoveFromEnd: ");

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

       // TODO Write code that adds integers 0 through (numitems - 1)

       // to list, inside a loop.

       long startTime = getTimestamp();

       // TODO Write code that removes the last element from list,

       // repeatedly until the list is empty.

       long endTime = getTimestamp();

       long totalTime = endTime - startTime;

       System.out.println(totalTime);

      

       return list;

   }

Explanation / Answer

Code that appends integers 0 through (numitems - 1) to the list. I have added a loop for adding values to the list

for(int i=0; i<numItems-1; i++){

       list.add(0);

   }

Then the entire program becomes

public static ArrayList<Integer> doArrayListAdd(int numItems) {

   System.out.print("doArrayListAdd: ");

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

   long startTime = getTimestamp();

   for(int i=0; i<numItems-1; i++){

       list.add(0);

   }

   long endTime = getTimestamp();

   long totalTime = endTime - startTime;

   System.out.println(totalTime);

   return list;

   }