我在尝试将一个单词重新添加到单词列表中时遇到了一些问题。
该程序计算单词长度,然后将其存储,以便输出显示如下所示:
长度为7的单词56
我得到了它,所以它计算正确的字数,但是输出没有把正确的字数和正确的字长放在一起。
所以它应该是长度为10的单词
但我的显示的单词长度是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
问:如何将单词添加回单词列表数组(单词列表来自文本文件)。而不使用数组或散列。
发布于 2018-09-20 04:57:40
所以你的初步检查是正确的。插入新词时,请检查以确保wordList中的索引有效。然后通过将其容量加倍来修改wordList。错误之处在于从wordCount中减去1。这将是第一次失败,因为wordCount是0,0 -1 =-1,这是一个无效的索引。只需按原样使用wordCount并递增。你甚至可以在添加单词时使用post增量。
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;
发布于 2018-09-20 07:28:45
更改:
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()]++;
https://stackoverflow.com/questions/52413826
复制相似问题