public static ArrayList<Integer> doArrayListSearchMedian(int numItems) { System.
ID: 3923436 • Letter: P
Question
public static ArrayList<Integer> doArrayListSearchMedian(int numItems) {
System.out.print("doArrayListSearchMedian: ");
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 median value.
long endTime = getTimestamp();
long totalTime = endTime - startTime;
System.out.println(totalTime);
return list;
}
Explanation / Answer
Hi,
I have implemented the code in required areas. Highlighted the code changes below.
public static ArrayList<Integer> doArrayListSearchMedian(int numItems) {
System.out.print("doArrayListSearchMedian: ");
ArrayList<Integer> list = new ArrayList<Integer>();
// TODO Write code that adds integers 0 through (numitems - 1)
// to list.
for(int i=0; i<numItems; i++){
list.add(i);
}
long startTime = getTimestamp();
// TODO Write code that gets the element at the median value.
int index = list.size()/2;
int medianElement = list.get(index);
long endTime = getTimestamp();
long totalTime = endTime - startTime;
System.out.println(totalTime);
return list;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.