【问题标题】:CLOSED Append a word onto word listCLOSED 将一个单词添加到单词列表中
【发布时间】:2018-09-19 20:36:18
【问题描述】:

我在尝试将一个单词添加回单词列表时遇到了一些麻烦。

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

单词长度 7 56

我知道了,所以它可以正确计算字数,但输出没有将正确的字数和正确的字数放在一起。

应该是这样的 长度为 1 0 的单词

但我的节目 字长 1 97

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

我不确定如何解决这个问题。

我觉得应该是这样的:

wordList[wordCount-1] = word;

(-1 是这样我不会得到 Array out of bounds 错误)。

  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

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

【问题讨论】:

  • 请展示更多您的代码。
  • 发布您的代码。
  • 已更新。请,完整的代码运行良好。这只是我无法弄清楚的很小的一部分。

标签: java arrays append word


【解决方案1】:

所以你的初步检查是正确的。插入新单词时检查以确保 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 96 的单词。这应该是长度为 2 的单词。如果我从直方图中删除 -1,它会抛出 ArrayIndexOutOfBoundsException:
  • @LadyRen 如果你这样做 System.out.println(word);打印什么?或显示您正在使用的文件的示例并将其放在问题中
【解决方案2】:

在下面的代码中,您将数组索引打印为字长,但索引比字长小一(还记得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]

【讨论】:

    【解决方案3】:

    变化:

    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()]++;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 2013-05-19
      • 2012-05-09
      • 2011-07-27
      • 2020-03-21
      相关资源
      最近更新 更多