当我尝试在一个单独的课程中打印myWords时,我得到一个空白的陈述。我很确定代码是正确的但是我无法打印出我想要的单词。请帮忙!
public String[] multiLetter (int n, char included)
//public String[] multiLetter(int n, char included)
//returns an array of words with at least m occurrences
//of the letter included
{
for (int i = 0 ; i < words.size() ; i++)
{
int repeatCounter = 0;
for (int j = 0 ; j < words.get(i).length(); j ++)
{
if (included == words.get(i).charAt(j))
{
repeatCounter ++;
}
}//closes inner for loop
if (repeatCounter >= n)
{
myWords.add(words.get(i));
}
}
String [] array = new String [myWords.size()];
return myWords.toArray(array);
}
答案 0 :(得分:1)
首先:使用您编码的方式,您必须收到一个String列表作为参数。
第二:你必须创建myWords
变量。
我认为你的代码应该是那样的:
public String[] multiLetter (int n, char included, List<String> words)
//public String[] multiLetter(int n, char included)
//returns an array of words with at least m occurrences
//of the letter included
{
List<String> myWords = new ArrayList<String>();
for (int i = 0 ; i < words.size() ; i++)
{
int repeatCounter = 0;
for (int j = 0 ; j < words.get(i).length(); j ++)
{
if (included == words.get(i).charAt(j))
{
repeatCounter++;
}
}//closes inner for loop
if (repeatCounter >= n)
{
myWords.add(words.get(i));
}
}
return (String[])myWords.toArray();
}