2014-04-20 117 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();    
}