2014-04-20 89 views
1

当我尝试在单独的类中打印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);    
} 
+2

你永远不会打印任何东西。 –

+1

您遗漏了足够的相关信息,我们无法帮助您。 – csmckelvey

+0

我们需要看到您尝试打印阵列的类 –

回答

1

首先:使用您编码的方式,您必须接收字符串列表作为参数。 第二:你必须创建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();    
}