给初始化数组赋值错误



所以我似乎无法弄清楚为什么我无法在我的collectWords方法中将新值分配给String[] fileWords。如果我在for循环之外执行。

说fileWords[1] = "Hello",这工作。然而,相同类型的赋值在我的for循环中不会。这最终确保我运行到一个空指针异常(代码可能有奇怪的小语法错误,因为我仍然在测试一些方法)。

为了澄清这是一个家庭作业,我们必须使用标准数组,因为这是指令(内存分配相关实践)。

基本上代码所做的是从一个txt文件中抓取单词,然后必须计算长度为n的单词的数量并将其添加到数组中。单词的长度应该与索引号相关。我个人感觉会有很多数组空值,但这就是我们被指示要做的。

代码:

import edu.duke.*;
import org.apache.commons.csv.*;
import java.util.*;
import java.io.*;
public class WordLengths{
public static void main(String args[]){
WordLengths wordLengths = new WordLengths();
wordLengths.testCountWordLengths();
}

private Integer countWordsInFile(FileResource fr){
int nWords = 0;
for(String s : fr.words())
{
nWords++;
}
System.out.println("Number of words in file: "+ nWords);
return nWords;
}
private String[] collectWords(FileResource fr)
{
int nWords = countWordsInFile(fr);
String[] fileWords = new String[nWords+1];
int i = 0;
for(String s : fr.words())
{
fileWords[i] = s;
i++;
System.out.println("fileWords: "+i+ " word: " + fileWords[i]);
}
return fileWords;
}
private void testCollectWords(){
FileResource fr = new FileResource();
String[] wordCollection = collectWords(fr);
System.out.println("Word collection: "+wordCollection);
}
}
.txt文件输入示例:
THE TRAGEDY OF JULIUS CAESAR
by William Shakespeare

Dramatis Personae
JULIUS CAESAR, Roman statesman and general
OCTAVIUS, Triumvir after Caesar's death, later Augustus Caesar,
first emperor of Rome
MARK ANTONY, general and friend of Caesar, a Triumvir after his death
LEPIDUS, third member of the Triumvirate
MARCUS BRUTUS, leader of the conspiracy against Caesar
CASSIUS, instigator of the conspiracy
CASCA,          conspirator against Caesar
TREBONIUS,           "          "     "
CAIUS LIGARIUS,      "          "     "
DECIUS BRUTUS,       "          "     "
METELLUS CIMBER,     "          "     "
CINNA,               "          "     "
CALPURNIA, wife of Caesar
PORTIA, wife of Brutus
CICERO,     senator
POPILIUS,      "
POPILIUS LENA, "
FLAVIUS, tribune
MARULLUS, tribune
CATO,     supportor of Brutus
LUCILIUS,     "     "    "
TITINIUS,     "     "    "
MESSALA,      "     "    "
VOLUMNIUS,    "     "    "
ARTEMIDORUS, a teacher of rhetoric
CINNA, a poet
VARRO,     servant to Brutus
CLITUS,       "    "     "

澄清一下,我目前的算法不支持"旁边的单词。等等(包括诸如isn't等词)。

任何建议都很好。此外,我可以为索引号分配任何可供选择的值,这些值最终没有长度为n的单词。代码将解释为什么会有0作为值(必须仍然添加这部分,但不能让它现在分配任何值)。

谢谢。

您的问题在方法collectWords。你使用了错误的索引来打印这个单词。以下是collectWords方法的代码:

private String[] collectWords(FileResource fr) {
int nWords = countWordsInFile(fr);
String[] fileWords = new String[nWords + 1];
int i = 0;
for (String s : fr.words()) {
fileWords[i] = s;
i++;
System.out.println("fileWords: " + i + " word: " + fileWords[i]);
}
return fileWords;
}

for循环的第一次迭代中,i的值为0(零)。将一个单词存储在索引为0的数组fileWords中。然后增加索引,所以i的值现在是1(1)。fileWords中索引1处的元素[仍然]为null。修改最少的方法是修改以下行:

System.out.println("fileWords: " + i + " word: " + fileWords[i]);

:

System.out.println("fileWords: " + i + " word: " + fileWords[i - 1]);

同样,不能简单地打印数组的内容。同样,打印数组内容的最简单方法(在我看来)是通过java.util.Arrays类中的toString方法。因此,testCollectWords方法的最后一行应该是:

System.out.println("Word collection: " + Arrays.toString(wordCollection));

这是你的代码和我的修改。

public class WordLengths {
private Integer countWordsInFile(FileResource fr) {
int nWords = 0;
for (String s : fr.words()) {
nWords++;
}
System.out.println("Number of words in file: " + nWords);
return nWords;
}
private String[] collectWords(FileResource fr) {
int nWords = countWordsInFile(fr);
String[] fileWords = new String[nWords + 1];
int i = 0;
for (String s : fr.words()) {
fileWords[i] = s;
i++;
System.out.println("fileWords: " + i + " word: " + fileWords[i - 1]);
}
return fileWords;
}
private void testCollectWords() {
FileResource fr = new FileResource();
String[] wordCollection = collectWords(fr);
System.out.println("Word collection: " + java.util.Arrays.toString(wordCollection));
}
public static void main(String[] args) {
WordLengths wordLengths = new WordLengths();
wordLengths.testCollectWords();
}
}

相关内容

  • 没有找到相关文章

最新更新