2011-10-17 90 views
0

我想讓我的類返回一個值,但我希望該值可以像循環一樣進行更改。我認爲我的代碼會比我更好地解釋它。Java返回一個循環

public static String readChat(String value) throws Exception 
{ 
    FormatS FormatS = new FormatS(); 

    BufferedReader mainChat = new BufferedReader(new FileReader("./chat.txt")); 
    String str; 
    while ((str = mainChat.readLine()) != null) 
    { 
     String msg = FormatS.FormatS(str, value); 
    } 
    mainChat.close(); 
return (msg); 

不幸的是,這將只返回在循環,因爲循環中的最後一個不返回,但只有最後的消息,我怎樣才能使它在循環返回的每一個值作爲一個單獨的回報? (如果可能的話從這裏不影響其他類)

+1

我相信你必須在調用方法的循環中調用這個方法。其他選項是使用分隔符連接字符串並返回整個字符串。 – Sid

回答

4

你不能。 Java方法只返回一個值,它不能返回多次。 退貨聲明之後,您就不在該方法中!

您可以使用集合(例如列表)來存儲所有消息,然後返回列表。

http://download.oracle.com/javase/6/docs/api/java/util/Collection.html

檢查此處查找有關集合的更多信息。如果你是Java新手,並計劃堅持使用它,那麼掌握它是一個非常關鍵的方面。

你應該做的是實例化一個新的集合,例如:

List<String> messageList = new ArrayList<String>(); 

,然後你的循環裏面,你會做...

messageList.add(msg); 

而對於大結局,當然...

return messageList; 

在「外面」的方法(來電),你可以在列表interate,並使用了我滿足您的需求。

1

如果你想讓這個方法在你每次調用它時返回一個不同的東西,你需要存儲該方法的狀態 - 這是特別不幸的因爲它是一種靜態方法。

感覺我覺得最好是返回一個Iterable<String>--一個可以迭代的單個對象,例如,增強的for循環。

這樣做的一個簡單方法是讀取整個文件,將其加載到List<String>然後返回。 Guava爲此提供了一個非常簡單的方法:

// Use the appropriate charset, of course - but the default platform encoding 
// is rarely a good idea. 
List<String> lines = Files.readLines(new File("chat.txt", Charsets.UTF_8)); 

List<String> formattedLines = Lists.newArrayList(); 
for (String line : lines) { 
    formattedLines.add(FormatS.FormatS(line, value)); 
} 
return formattedLines; 

FormatS.FormatS是一個相當奇怪的方法名,順便......)

有番石榴這樣的替代方法,例如通過LineProcessor - 但如果你對Java相對陌生,這可能是一個更簡單的起點。

2

製作一個數組,將每個值作爲一個單獨的項放入數組中,然後返回數組。或者將新值添加到循環中的msg上:

msg += FormatS.FormatS(str, value); 

在循環外啓動msg。

0

每次調用將返回下一行,或NULL如果沒有更多的行(如果這就是你所需要的):

static BufferedReader mainChat = new BufferedReader(new FileReader("./chat.txt")); 
static boolean finished = false; 

public static String readChat(String value) throws Exception 
{ 
    FormatS FormatS = new FormatS(); 

    if(finished) { 
     return null; 
    } 

    String str; 
    if ((str = mainChat.readLine()) != null) 
    { 
     return FormatS.FormatS(str, value); 
    } else { 
     finished = true; 
     mainChat.close(); 
     return null; 
    } 
} 

請注意,你必須做出一個單獨的(再)如果你想初始化函數多次使用它。

0

您必須從調用方法重複調用此方法。更好的方法是將內容存儲在字符串數組中,並返回數組而不必多次調用此方法。

private String[] strBuff; 
while ((str = mainChat.readLine()) != null) 
{ 
    strBuff[i] = FormatS.FormatS(str, value); 
} 
return strBuff; 
0

有兩種乾淨的方法可以使方法將多個返回值傳遞給調用者。

第一種也是最簡單的方法是返回一個包含所有返回值的對象。它可能是一個集合,就像一個List,或者它可能是你自己的價值類的一個對象。

List<Person> matches = findMatches(peopleDB,criteria); 
MatchReport report = findMatchesAsReport(peopleDB); 

...

public List<Person> findMatches(PersonSource source,Criteria criteria) { 
     List<Person> list = new ArrayList<Person>(); 
     while(source.hasNext()) { 
      Person person = source.next(); 
      if(person.matches(criteria)) { 
       list.add(person); 
      } 
     } 
     return list; 
} 

第二,更復雜的方法是定義一個處理,你的方法可以用手項目,因爲它遇到他們。

public interface PersonHandler { 
      public void onPerson(Person p); 
    } 

然後定義你的方法,這樣的處理程序將被傳遞給它:

public void findMatches(PeopleSource source, Criteria criteria, PersonHandler handler) { 
      while(source.hasNext()) { 
      Person person = source.next(); 
      if(person.matches(criteria)) { 
       handler.onPerson(person); 
      } 
      } 
    } 

調用者可以再定義一個PersonH​​andler比滿足自己的需求:

private static class PrintToWriterPersonHandler implements PersonHandler { 
     private PrintWriter writer; 
     public WriteToStreamPersonHandler(PrintWriter writer) { 
      this.writer = writer; 
     } 
     public void onPerson(Person p) { 
      writer.println(person); 
     } 
    } 

...

findMatches(source,criteria,new PrintToWriterPersonHandler(System.out)); 

這是退出參與其中,對於初學者來說很複雜。但值得記住。這意味着您可以處理產生大量響應的方法,而無需等到方法結束,並且不會結束內存中的巨大列表。這也意味着你可以處理來自可能無限期運行的方法的輸出!