Question
I need help finishing this program in Java. Please try to follow the format I have set up already. The goal of the program is
I need help finishing this program in Java. Please try to follow the format I have set up already. The goal of the program is to compare arrays and arraylists to each other. The fill methods I have made are intended to compare the time it takes to fill arrays and arraylists. The increment methods time how long it takes to add one to every element. I need help adding Search methods. Here are the instructions:
Does it take longer to search in an array or an ArrayList? Fill both lists again, but with the same values in each; do this before timing begins. Use one loop that creates a random value then assigns it into both lists. Search looks for the same value in both lists. During development, print out whether the value was found and in what position this should match for the two lists (i.e., if it's found in index position 12 in the array, then it should be found in element 12 in the ArrayList). This method needs to search for multiple values to see a significant time difference between the two data structures. Create an array of search values (random) so you can use the same set for the array and the ArrayList. Start with 50 search values, then increase that if needed to get significant results (time differences).
create array of SEARCH_COUNT (50) search values
// search array start timer loop 50 times get next search value from search array search loop: continue until the current search value is found or the entire array has been examined stop timer calculate and report elapsed time
// search ArrayList same logic
The ouput of these should follow what I currently have here is my code:
import java.util.ArrayList; import java.util.Random; import java.text.DecimalFormat; public class ArrayTest { int N = 10000000; int[] theArray = new int[N]; ArrayListlist = new ArrayList (); Random rand = new Random(); ArrayTest() { double arrayResult = fill(); double arrayListResult = arrayListFill(); DecimalFormat decimalFormat = new DecimalFormat("#.##"); System.out.println("Fill race, N = 10,000,000"); System.out.printf(" array fill: %s seconds ", decimalFormat.format(arrayResult)); System.out.printf(" arrayList fill: %s seconds ", decimalFormat.format(arrayListResult)); list.clear(); double arrayIncrement = arrayIncrement(); double arrayListInc=arrayListIncrement(); System.out.println("Increment race, N = 10,000,000"); System.out.printf(" array increment: %s seconds ", decimalFormat.format(arrayIncrement)); System.out.printf(" arrayList increment: %s seconds ", decimalFormat.format(arrayListInc)); } public double arrayListFill() { long startTimeArray1 = System.currentTimeMillis(); for (int i = 0; i < N; i++) { list.add(rand.nextInt()); } long endTimeArray1 = System.currentTimeMillis(); return (endTimeArray1 - startTimeArray1) / 1000.0; } public double fill() { long startTimeArray1 = System.currentTimeMillis(); for (int i = 0; i < theArray.length; i++) { theArray[i] = rand.nextInt(); } long endTimeArray1 = System.currentTimeMillis(); return (endTimeArray1 - startTimeArray1) / 1000.0; } public double arrayIncrement(){ for (int i=0;i currentTimeMillis(); for(int j=0; j currentTimeMillis(); return (endTimeArray2-startTimeArray2)/1000.0; } public double arrayListIncrement(){ for(int i=0;i currentTimeMillis(); for (int j=0;j currentTimeMillis(); return (endTimeArray2-startTimeArray2)/1000.0; } public static void main(String[] args) { ArrayTest test = new ArrayTest(); } }
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