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

public static ArrayList<Integer> doArrayListSearchGreatest(int numItems) { Syste

ID: 3923302 • Letter: P

Question

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

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

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

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

       // to list.

       long startTime = getTimestamp();

       // TODO Write code that gets the element at the last index from list.

       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<>();

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

       list.add(0);

   long startTime = getTimestamp();

   }

   // TODO Write code that gets the element at the last index from list.

   int lastNumber = list.get(list.size()-1);

   long endTime = getTimestamp();

   long totalTime = endTime - startTime;

   System.out.println(totalTime);

   return list;

   }