title: How to find the length of an array after it is trimmed, using java?
body: <p>When I run the program, it prints out "After trim, wordList length: 0". Not sure why?</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class Project5 {
  static final int INITIAL_CAPACITY = 10;
  
  public static void main(String[] args) throws Exception {
      if (args.length &lt; 1)
          die("You must type the dictionary filename on cmd line.\n");
  
      // Here we have declared an int array, called 'histogram' with initial
      // capacity of 0
      // it is a freq counter to word lengths in the file
  
      int[] histogram = new int[0];
  
      // Here we have declared an array of String to read the dictionary file
      // into. We use BufferedReader (not Scanner).
      // With each word read in, examine it's length and update word length
      // frequency histogram accordingly
  
      String[] wordList = new String[INITIAL_CAPACITY];
      int wordCount = 0;
      BufferedReader infile = new BufferedReader(new FileReader(args[0]));
      while (infile.ready()) // i.e. while there are more lines of text in the
                              // file
      {
          String word = infile.readLine();
  
          // YOUR CODE HERE TO CHECK TO SEE IF WORDLIST IS FULL
          // IF SO YOU MUST DO AN UPSIZE JUST LIKE LAB#6
          if (word.length() &gt;= histogram.length)
              histogram = upSizeHisto(histogram, word.length() + 1);
  
          // YOUR CODE HERE to add this word to your list
          histogram[word.length()]++;
  
          // YOUR CODE HERE TO LOOK AT THE LENGTH OF THE WORD AND UPDATE
          // HISTOGRAM
          // example if word.length() is 5 then histogram[5] gets increment
          // BUT IF WORD LENGTH IS &gt;= HISTORGRAM LENGTH
          // THEN YOU NEED TO FIRST CALL upSizeHisto TO UPSIZE THE HISTOGRAM
          // TO BE OF EXACTLY LENGTH word.length()+1
          // SIMILAR TO HOW YOU HAD TO UPSIZE WORDLIST
  
      } // END WHILE INFILE READY
      infile.close();
  
      wordList = trimArr(wordList, wordCount);
      System.out.println("After trim, wordList length: " + wordList.length);
  
      // PRINT WORD LENGTH FREQ HISTOGRAM
      for (int i = 0; i &lt; histogram.length; i++)
          System.out.println("words of length " + i + ": " + histogram[i]);
  
  } // END main
  
  private static void die(String msg) {
      System.out.println(msg);
      System.exit(0);
  }
  
  private static String[] upSizeArr(String[] oldArr) {
  
      int i = 0;
      String[] newArr = new String[oldArr.length * 2];
      for (i = 0; i &lt; oldArr.length; i++) {
          newArr[i] = oldArr[i];
      }
  
      return newArr; // replace with code from Lab6
  }
  
  private static String[] trimArr(String[] oldArr, int count) {
      int i = 0;
      String[] trimArr = new String[count];
      for (i = 0; i &lt; trimArr.length; i++) {
          trimArr[i] = oldArr[i];
      }
  
      return trimArr; // replace with code from Lab6
  }
  
  private static int[] upSizeHisto(int[] oldArr, int newLength) {
      int i = 0;
      int upSizeHisto[] = new int[newLength];
      for (i = 0; i &lt; oldArr.length; i++) {
          upSizeHisto[i] = oldArr[i];
      }
  
      return upSizeHisto; // change all this to upsize the int[] array
  }
  } // END CLASS PROJECT#5
  </code></pre>