簡體   English   中英

如何在Java中從for循環返回多個字符串?

[英]How do I return multiple Strings from a for loop in Java?

我使用的是for循環,每次循環都需要返回String,但是使用“ return”會中斷循環,並且Eclipse會引發“ Unreachable Code”錯誤。 有什么建議么

如果返回ArrayList<String> ,不適合您的應用程序,這聽起來像是回調函數的工作。 定義一個回調接口:

public interface StringDelivery {
    public void processString(String aString);
}

然后,您可以在循環中回叫:

public void loopThroughStrings(StringDelivery callback) {
    for (. . .) {
        String nextString = . . .
        callback.processString(nextString);
    }
}

然后,您可以使用實現該接口的任何對象來調用它。

編輯:

如果要計算一堆字符串,但需要將它們作為單個字符串返回,則可以將它們放入數組中,然后使用Arrays.toString(Object[] array)將整個數組轉換為單個String:

int n = <number of strings>
String[] strings = new String[n];
for (int i = 0; i < n; ++i) {
    strings[i] = <i-th string>
}
return Arrays.toString(strings);

返回值的格式將為列表元素,並用“,”分隔並括在方括號“ []”中。

我覺得你的邏輯壞了

for (int counter = 0; counter < possibleAnswers.length; counter++){
    // This condition will be meet immediately because 0 is less then 25...
    if (counter < 25){
        return alpha[counter] + ": " + possibleAnswers[counter] + "\n";
    }
    // Meaning it is impossible for the program to ever reach this line...
    if (counter >= 26){
        return alpha[26] + a + ": " + possibleAnswers[counter] + "\n";
        a++;
    }
}

我認為您最好嘗試類似...

StringBuilder sb = new StringBuilder(25);
for (int counter = 0; counter < possibleAnswers.length; counter++){
    if (counter < 25){
        sb.append(alpha[counter] + ": " + possibleAnswers[counter] + "\n");
    }
    if (counter >= 26){
        sb.append(alpha[26] + a + ": " + possibleAnswers[counter] + "\n");
        a++;
    }
}
return sb.toString();

UPDATE工作示例

String possibleAnswers[] = new String[30];
String alpha[] = new String[30];

for (int index = 0; index < 30; index++) {
    possibleAnswers[index] = "Happy " + index;
    alpha[index] = Integer.toString(index);
}

int a = 0;

StringBuilder sb = new StringBuilder(25);
for (int counter = 0; counter < possibleAnswers.length; counter++) {
    if (counter < 25) {
        sb.append(alpha[counter]).append(": ").append(possibleAnswers[counter]).append("\n");
    }
    if (counter >= 26) {
        sb.append(alpha[26]).append(a).append(": ").append(possibleAnswers[counter]).append("\n");
        a++;
    }
}

System.out.println(sb);

哪個輸出

0: Happy 0
1: Happy 1
2: Happy 2
3: Happy 3
4: Happy 4
5: Happy 5
6: Happy 6
7: Happy 7
8: Happy 8
9: Happy 9
10: Happy 10
11: Happy 11
12: Happy 12
13: Happy 13
14: Happy 14
15: Happy 15
16: Happy 16
17: Happy 17
18: Happy 18
19: Happy 19
20: Happy 20
21: Happy 21
22: Happy 22
23: Happy 23
24: Happy 24
260: Happy 26
261: Happy 27
262: Happy 28
263: Happy 29

您將要使用ArrayList來存儲所有字符串。

for循環完成后,返回ArrayList。 就像是。

String returnString = "";

for loop ....{
   returnString = returnString + " " + someNewString;
 }


return returnString

為什么要復雜的事情..只是返回一個字符串數組

 public String[] function1()
 {
    String[] str1 new String(10);

    for(int i=0;i<10;i++)
    { 
          str1[i] ="string"+i;
    }
    return str1;
 }

您應該考慮從方法返回String數組。

僅糾正for循環不會返回任何內容。 它是返回值的功能。

最后,由於不清楚您要實現的目標,因此很難評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM