已关闭将单词附加到单词列表

时间:2018-09-19 20:36:19

标签: java arrays append word

我在尝试将单词附加回单词列表时遇到麻烦。

程序会计算一个单词的长度,然后将其存储,以便输出显示如下内容:

长度为7 56的单词

我已经知道了,所以它可以正确地计算单词数,但是输出结果并不能正确显示单词长度正确的单词数。

应该是 长度为1 0的单词

但是我的节目 长度为1 97的单词

(这是长度为2的单词的正确计数)。

我不确定如何解决此问题。

我觉得应该是这样的:

wordList[wordCount-1] = word;

(-1是,所以我没有出现数组越界错误)。

  import java.io.*;
import java.util.*;

public class Project2
{
    static final int INITIAL_CAPACITY = 10;
    public static void main (String[] args) throws Exception
    {
        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
        if (args.length < 1 )
        {
            System.out.println("\nusage: C:\\> java Project2 <input filename>\n\n"); // i.e. C:\> java Project2 dictionary.txt
            System.exit(0);
        }
        int[] histogram = new int[0]; // histogram[i] == # of words of length n

        /* array of String to store the words from the dictionary. 
            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() )
        {
            String word = infile.readLine();
            // # # # # # DO NOT WRITE/MODIFY ANYTHING ABOVE THIS LINE # # # # #
            if (wordCount == wordList.length)
                wordList = upSizeArr(wordList);
            // test to see if list is full. If needed do an up size (just like Lab#3)

            wordList[wordCount++] = word;

            // now you may safely append word onto list and incr count
                int wordLength = word.length();
                if (word.length () > histogram.length)
                    histogram = upSizeHisto(histogram, wordLength);
            // look at the word length and see if the histogram length is AT LEAST
            // word length + 1. If not, you must upsize histogram to be EXACTLY word length + 1
            histogram[word.length()-1]++;

            // now you can increment the counter in the histogram for this word's length

            //  # # # # # DO NOT WRITE/MODIFY ANYTHING BELOW THIS LINE  # # # # #
        } //END WHILE INFILE READY
        infile.close();

        wordList = trimArr( wordList, wordCount );
        System.out.println( "After final trim: wordList length: " + wordList.length + " wordCount: " + wordCount );

        // PRINT WORD LENGTH FREQ HISTOGRAM
        for ( int i = 0; i < histogram.length ; i++ )
            System.out.format("words of length %2d  %d\n", i,histogram[i] );

    } // END main

    // YOU MUST CORRECTLY COPY THE STRING REFS FROM THE OLD ARR TO THE NEW ARR
    static String[] upSizeArr( String[] fullArr )
    {   
        String [] newArr = new String [fullArr.length*2];
        for (int count = 0; count < fullArr.length ; count++)
        {
            newArr[count] = fullArr[count];

        }


        return newArr; // just to make it complie you change as needed
    }
    static String[] trimArr( String[] oldArr, int count )
    {
        String[] newArr = new String[count];


        for ( count = 0; count < newArr.length ; count++)
        {
            newArr[count] = oldArr[count];

        }


        return newArr;  //return null; // just to make it complie you change as needed
    }

    // YOU MUST CORRECTLY COPY THE COUNTS FROM OLD HISTO TO NEW HISTO
    static int[] upSizeHisto( int[] oldArr, int newLength )
    {
        int [] newHisto= new int[newLength];


        if (oldArr.length > 1)
        {
        for (int count = 0; count < oldArr.length  ; count++)
        {
            newHisto[count] = oldArr[count];

        }
        }

        return newHisto; // just to make it complie you change as needed
    }
} // END CLASS PROJECT#2

问题:如何将单词附加回单词列表数组(单词列表来自文本文件)。不使用数组或哈希。

3 个答案:

答案 0 :(得分:0)

因此您初步检查是否正确。插入新单词时,请检查以确保wordList中的索引有效。然后通过使其容量加倍来修改wordList。出错的地方是从wordCount中减去1。这将第一次失败,因为wordCount为0,0-1-= -1,这是无效的索引。只需按原样使用wordCount并递增即可。您甚至可以在添加单词时使用帖子增量。

if (wordCount >= wordList.length)
    wordList = upSizeArr(wordList);
    // test to see if list is full. If needed do an up size (just like Lab#3)

wordList[wordCount++] = word;

答案 1 :(得分:0)

在下面的代码中,将数组索引打印为单词长度,但是索引比单词长度一个(记得histogram[word.length()-1]++吗?);

// PRINT WORD LENGTH FREQ HISTOGRAM
    for ( int i = 0; i < histogram.length ; i++ )
        System.out.format("words of length %2d  %d\n", i,histogram[i] );

该行应结束, i+1, histogram[i]

答案 2 :(得分:0)

更改:

if (word.length() > histogram.length)
  histogram = upSizeHisto(histogram, wordLength);

if (word.length() >= histogram.length)
  histogram = upSizeHisto(histogram, wordLength+1);

还有

histogram[word.length() - 1]++;

histogram[word.length()]++;