title: Timing collections class sort and timing making an arraylist
body: <p>This is code which I have written to time how long it takes to perform a selection sort:</p>
  
  <pre><code>static public String [ ] selectionSort(String [ ] wordlist)
  {
      for (int i = 1; i &lt; wordlist.length; i++) 
      {
          int s = i-1;
  
          for (int j = i; j &lt; wordlist.length; j++) 
          {
              if (wordlist[j].compareTo(wordlist[s]) &lt; 0) 
              {
                  s = j;
              }
          }
  
       String temp = wordlist[i-1];
       wordlist[i-1] = wordlist[s];
       wordlist[s] = temp;
      }
  
      return wordlist;
  }
  
  static public String [ ] timedSelectionSort(String [ ] wordlist)
  {
      long startTime = 0;
      long stopTime = 0;
      long elapsedMillis = 0;
      long elapsedSec = 0;
      long elapsedMin = 0;     
  
      startTime = System.currentTimeMillis();
  
      wordlist = selectionSort(wordlist);
  
      stopTime = System.currentTimeMillis();
      elapsedMillis = stopTime-startTime;
      elapsedSec = elapsedMillis/1000;
      elapsedMin = elapsedMillis/60000;
      System.out.printf("File reading took %d minutes and %s secs and %d milliseconds to execute\n",
                          elapsedMin,elapsedSec%60, elapsedMillis%1000);  
      System.out.println("Read " + wordlist.length + " strings.");
  
      return wordlist;
  }
  </code></pre>
  
  <p>I have to also time a how long it takes to make an arraylist object containing all of the strings in an array (my attempt is below)</p>
  
  <pre><code>static public ArrayList&lt;String&gt; makeArrayListClassObject(String [ ] wordlist)
  {
      long startTime = 0;
      long stopTime = 0;
      long elapsedMillis = 0;
      long elapsedSec = 0;
      long elapsedMin = 0;     
  
      startTime = System.currentTimeMillis();
  
      ArrayList&lt;String&gt; stringList = new ArrayList&lt;String&gt;(Arrays.asList(wordlist));
  
      stopTime = System.currentTimeMillis();
      elapsedMillis = stopTime-startTime;
      elapsedSec = elapsedMillis/1000;
      elapsedMin = elapsedMillis/60000;
      System.out.printf("File reading took %d minutes and %s secs and %d milliseconds to execute\n",
                          elapsedMin,elapsedSec%60, elapsedMillis%1000);  
      System.out.println("Read " + wordlist.length + " strings.");
  
      return stringList;
  }
  </code></pre>
  
  <p>as well as time how long it takes to sort the arraylist object using the Collections.sort( ) method. I have made the method (see below) but I don't know how to time it</p>
  
  <pre><code>static public void collectionsClassSort(ArrayList&lt;String&gt; arraylist)
  {
      Collections.sort(arraylist);
  }
  </code></pre>
  
  <p>I guess my questions are:
  1. Is there a way to use the same method to time these two methods the way I timed the first method?
  2. If not, how can I time these two methods?</p>
  
  <p><strong>EDIT</strong>: the timed collections sort now works, but the timed building of the arraylist still does not. meaning that the timer says that the method took 0 minutes, 0 seconds, and 0 milliseconds to execute</p>
  
  <p>This is the updated code for that:</p>
  
  <pre><code>static public ArrayList&lt;String&gt; makeArrayListClassObject(String [ ] wordlist)
  {
      long startTime = 0;
      long stopTime = 0;
      long elapsedMillis = 0;
      long elapsedSec = 0;
      long elapsedMin = 0;     
  
      startTime = System.currentTimeMillis();
  
      ArrayList&lt;String&gt; stringList = new ArrayList&lt;String&gt;(Arrays.asList(wordlist));
  
      stopTime = System.currentTimeMillis();
      elapsedMillis = stopTime-startTime;
      elapsedSec = elapsedMillis/1000;
      elapsedMin = elapsedMillis/60000;
      System.out.printf("File reading took %d minutes and %s secs and %d milliseconds to execute\n",
                          elapsedMin,elapsedSec%60, elapsedMillis%1000);  
  
      return stringList;                  
  }
  </code></pre>