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

Creat a java class called ListComparison to measure the amount of time required

ID: 3771861 • Letter: C

Question

Creat a java class called ListComparison to measure the amount of time required to perform various operations on an ArrayBasedList, and on a LinkedListBasedList. (I don't think you will need to look at the codes for ArrayBasedList and LinkedBasedList to answer this question but if you want to, please comment)

In the comparisons your lists should store String objects. It does not matter what particular string values are used. Empty strings should be fine.

Many of the operations that you are asked to measure ask that you use random indexes. You can use the following method to generate a random index for an object in a ListInterface:

1. Create an empty list, then add one million String objects using the add(T) method.

2. Create an empty list, then add one million String objects using add(T, index) method. Each object should be added using a random index.

3. In a million element list, remove 10,000 elements using random indexes.

4. In a million element list, replace 10,000 elements using random indexes.

You can measure the number of millisecond required to execute a piece of code using the Data class from the java.util library. When a Date object is constructed it stores the current time. Create a Date object before and after the execution of the code to measure the elapsed time:

Date start = new Date();

// code that you are timing

Date end = new Date();

long elapsedTime = end.getTime() - start.getTime();

Explanation / Answer

import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; public class ListComparison { public static void main(String[] args) { ArrayList arrayList1 = new ArrayList(); ArrayList arrayList2 = new ArrayList(); LinkedList linkedList1 = new LinkedList(); LinkedList linkedList2 = new LinkedList(); Date start = new Date(); for(int i=1; i