I'm trying to create 5 ArrayLists of various size, fill them with random numbers between 0 and 1, and then time (and print) how long it takes to iterate through each.
I think I've initialized and filled them correctly. But when I iterate through and check the time, the numbers are off. I get something like (0, 0, 2, 0, 0 seconds) when they should be increasing.
Here's the code, any ideas?
import java.util.*; // Imports all java.util subclasses.
public class Lab2 {
public static void main (String[] args)
{
System.out.println("#2");
int size1 = 100;
int size2 = 1000;
int size3 = 10000;
int size4 = 100000;
int size5 = 1000000;
// ArrayList 100.
ArrayList<Double> arrList1 = new ArrayList<Double>();
for (int i = 0; i < size1; i++)
{
arrList1.add(Math.random());
}
long startTime11 = System.currentTimeMillis();
for (int i = 0; i < arrList1.size(); i++);
long total11 = System.currentTimeMillis() - startTime11;
System.out.println("arrList1 Elapsed Time:" + total11);
// ArrayList 1000.
ArrayList<Double> arrList2 = new ArrayList<Double>();
for (int i = 0; i < size2; i++)
{
arrList2.add(Math.random());
}
long startTime12 = System.currentTimeMillis();
for (int i = 0; i < arrList2.size(); i++);
long total12 = System.currentTimeMillis() - startTime12;
System.out.println("arrList2 Elapsed Time:" + total12);
// ArrayList 10000.
ArrayList<Double> arrList3 = new ArrayList<Double>();
for (int i = 0; i < size3; i++)
{
arrList3.add(Math.random());
}
long startTime13 = System.currentTimeMillis();
for (int i = 0; i < arrList3.size(); i++);
long total13 = System.currentTimeMillis() - startTime13;
System.out.println("arrList3 Elapsed Time:" + total13);
// ArrayList 100000.
ArrayList<Double> arrList4 = new ArrayList<Double>();
for (int i = 0; i < size4; i++)
{
arrList4.add(Math.random());
}
long startTime14 = System.currentTimeMillis();
for (int i = 0; i < arrList4.size(); i++);
long total14 = System.currentTimeMillis() - startTime14;
System.out.println("arrList4 Elapsed Time:" + total14);
// ArrayList 1000000.
ArrayList<Double> arrList5 = new ArrayList<Double>();
for (int i = 0; i < size5; i++)
{
arrList5.add(Math.random());
}
long startTime15 = System.currentTimeMillis();
for (int i = 0; i < arrList5.size(); i++);
long total15 = System.currentTimeMillis() - startTime15;
System.out.println("arrList5 Elapsed Time:" + total15);