Homework #1 Assignment

Modify the program demonstrated in lecture to perform the following timings:

1) Time required to add 50000 elements to the front of an ArrayList
2) Time required to get each element (in order, starting with the first) from the ArrayList
3) Time required to remove all 50000 elements from the ArrayList, starting from the last element and working towards the first

4) Time required to add 50000 elements to the front of a LinkedList
5) Time required to get each element from the LinkedList
6) Time required to remove 50000 elements from the end of a LinkedList, starting from the last element and working towards the first
 

Note that you'll have to change the code (and the println statement too) for each operation. When getting or removing elements from the list, you'll of course have to populate the list first. In those cases, be sure not to measure the time taken to populate the list.

For each measurement, copy/paste both the code for your program and the results from the console into a single file (called homework1.txt).


package hw1;

import java.util.*;	// contains the Java Collections Framework

/**
 * Review of usage of Java Collections classes used in se1021
 * @author hornick
 *
 */
public class JCFTimingApp {

	public static void main(String[] args) {
		Collection<Integer> al = new ArrayList<Integer>();	
		
		double start = System.nanoTime(); // clock snapshot before
		for(int i=0; i<50000; i++ )
			al.add( i );
		double stop = System.nanoTime();  // clock snapshot after
		
		double delta = (stop-start)/1e6; // convert nanoseconds to milliseconds
		System.out.println("Add to front of ArrayList took " + delta + "ms");

	}
}