【问题标题】:How to find the length of an array after it is trimmed, using java?使用java如何在修剪后找到数组的长度?
【发布时间】:2015-02-26 04:31:38
【问题描述】:

当我运行程序时,它会打印出“修剪后,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

【问题讨论】:

  • 我认为你甚至什么都没做,wordList 没有在 while 循环中使用,并且 count 也没有增加。
  • wordCount 被初始化为 0,但它从未被修改过,因此您的 trimArr() 调用会缩减为 0。
  • 它打印 "wordList length: 0" 因为wordList.length 是 0...

标签: java arrays trim


【解决方案1】:

您的代码运行良好,唯一的问题是您永远不会增加wordCount 或将word 添加到wordList。只需将其添加到 while 循环中的某个位置即可:

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

【讨论】:

  • @alfasin 是的,但这似乎并不重要,因为在那之后他再也没有使用过它:)
  • 对,这就是为什么一个完整的解决方案将是实际......使用它;)
猜你喜欢
  • 2021-11-04
  • 2011-05-05
  • 1970-01-01
  • 2020-07-27
  • 2021-10-26
  • 1970-01-01
  • 2010-12-25
  • 2010-12-27
相关资源
最近更新 更多