如何使用java修剪数组后查找数组的长度?

时间:2015-02-26 04:31:39

标签: java arrays trim

当我运行程序时,它打印出“修剪后,wordList长度:0”。不确定为什么?

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 < 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() >= 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 >= 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 < 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 < 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 < 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 < oldArr.length; i++) {
        upSizeHisto[i] = oldArr[i];
    }

    return upSizeHisto; // change all this to upsize the int[] array
}
} // END CLASS PROJECT#5

1 个答案:

答案 0 :(得分:0)

您的代码运行正常,唯一的问题是您永远不会增加wordCount或将word添加到wordList。只需在while循环中的某处添加此内容即可:

    if (wordCount >= wordList.length) wordList = upSizeArr(wordList);
    wordList[wordCount] = word;
    wordCount++;