Question
In Java. I need help writing this small program, the intention here is to time the filling of an array and an arraylist and compare
In Java. I need help writing this small program, the intention here is to time the filling of an array and an arraylist and compare how much time it takes. The method fill should fill an array with 10,000,000 random values. It should then time how long it takes using the system timer. It should do the same for an arraylist with its own seperate time. The times should castes to string and then passed to a constructor. The constructor uses the DecimalFormat class to output the times in the following format "0.00". The output of the program should be the following:
Fill race, N = 10,000,000 array fill: ?? seconds arrayList fill: ?? seconds
I will now post the code I have so far:
import java.util.Random; import java.text.DecimalFormat; public class ArrayTest { int N=10000000; int [] theArray= new int[N]; Random rand = new Random(); public void fill(){ long startTimeArray1=System.currentTimeMillis(); for(int i=0;icurrentTimeMillis(); long fillTimeA1=(endTimeArray1-startTimeArray1)/1000; String fillTimeString=Long.toString(fillTimeA1); System.out.println(fillTimeString); } public static void main(String[] args) { ArrayTest test = new ArrayTest(); test.fill(); } }
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