Advertisement
Guest User

Test Program to check ArrayList vs LinkList Performance

a guest
Nov 18th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. // Test Program to check ArrayList vs LinkList Performance
  2.  
  3. package chapter24v10;
  4.  
  5. public class TestMyList {
  6.     /** Main method */
  7.     public static void main(String[] args) {
  8.         // Create a list for strings
  9.        
  10.         long start = System.nanoTime();
  11.         //MyList<String> list = new MyLinkedList<String>();
  12.         MyList<String> list = new MyArrayList<String>();
  13.  
  14.         // Add elements to the list
  15.         list.add("America"); // Add it to the list
  16.         System.out.println("(1) " + list);
  17.        
  18.         for (int i=0; i < 100000; i++) {
  19.             list.add(0, "USAUSAUSA");
  20.         }
  21.        
  22.         for (int i=0; i < 100000; i++) {
  23.             list.remove(0);
  24.         }
  25.  
  26.         list.add(0, "Canada"); // Add it to the beginning of the list
  27.         //System.out.println("(2) " + list);
  28.  
  29.         list.add("Russia"); // Add it to the end of the list
  30.         //System.out.println("(3) " + list);
  31.  
  32.         list.add("France"); // Add it to the end of the list
  33.         //System.out.println("(4) " + list);
  34.  
  35.         list.add(2, "Germany"); // Add it to the list at index 2
  36.         //System.out.println("(5) " + list);
  37.  
  38.         list.add(5, "Norway"); // Add it to the list at index 5
  39.         //System.out.println("(6) " + list);
  40.  
  41.         list.add(0, "Poland"); // Same as list.addFirst("Poland")
  42.         //System.out.println("(7) " + list);
  43.  
  44.         // Remove elements from the list
  45.         list.remove(0); // Same as list.remove("Australia") in this case
  46.         //System.out.println("(8) " + list);
  47.  
  48.         list.remove(2); // Remove the element at index 2
  49.         //System.out.println("(9) " + list);
  50.  
  51.         list.remove(list.size() - 1); // Remove the last element
  52.         //System.out.print("(10) " + list + "\n(11) ");
  53.  
  54.         //for (String s : list)
  55.         //  System.out.print(s.toUpperCase() + " ");
  56.  
  57.         long end = System.nanoTime();
  58.  
  59.         System.out.printf("%,d", end - start);
  60.         //list.clear();
  61.         //System.out.println("\nAfter clearing the list, the list size is "
  62.         //      + list.size());
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement