Question
I need help filling out the milestones at the bottom of this code: Milestone 2: Write methods to time the ArrayList and LinkedList .get() methods
I need help filling out the milestones at the bottom of this code: Milestone 2: Write methods to time the ArrayList and LinkedList .get() methods and print the table. Milestone 3: Write methods to time the ArrayList .add(0, element) and LinkedList .addFirst methods and print the table. Milestone 4: Write methods to time the .contains method for each of the three data structures (two lists and HashSet) and print the table
CODE: package list.timing;
import java.util.List; import java.util.LinkedList; import java.util.ArrayList; import java.util.HashSet;
public class Main { public static final int iters = 100; public static final int[] sizes = {10, 50, 100, 500, 1000, 10000}; // --- // Example of how to time functions that change the size of the ADT public static float timeAdd(List public static float timeAdd(HashSet public static void fillToSize(List public static void fillToSize(HashSet public static void printTable(String title, List System.out.printf("%4s\t", "LL"); for(Float time: lltimes) { System.out.printf("%10.2f\t", time.floatValue()); } System.out.println(); System.out.printf("%4s\t", "HS"); for(Float time: hstimes) { System.out.printf("%10.2f\t", time.floatValue()); } System.out.println(); System.out.println(); } public static void printTable(String title, List System.out.printf("%4s\t", "LL"); for(Float time: lltimes) { System.out.printf("%10.2f\t", time.floatValue()); } System.out.println(); System.out.println(); } /** * @param args the command line arguments */ public static void main(String[] args) { LinkedList ArrayList // This code times the .add method, and adds the result to the appropriate list of times. for (int size: sizes) { fillToSize(ll, size); lltimes.add(timeAdd(ll)); fillToSize(al, size); altimes.add(timeAdd(al)); fillToSize(hs, size); hstimes.add(timeAdd(hs)); } // print the table for the .add method printTable("Add (.add)", lltimes, altimes, hstimes); //Make sure to clear all of the times arraylists so you don't have extra times being tacked on. lltimes.clear(); altimes.clear(); hstimes.clear(); for (int size: sizes) { fillToSize(ll, size); lltimes.add(timeSet(ll)); fillToSize(al, size); altimes.add(timeSet(al)); } printTable("Set index in middle (.set)", lltimes, altimes); // TODO: Milestone 2: Time ArrayList and LinkedList .get() method // You will need to write timeGet for linked list and array list // remember to clear your times arrays. //TODO: Milestone 3: Time .addFirst for LinkedList and .add(0, element) for ArrayList // You will need to write the appropriate timing methods as above //TODO: MIlestone 4: Time .contains() for LinkedList, ArrayList, and HashSet // You will need to write the appropriate timing methods as above } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started