如何从Java中的for循环返回多个字符串?

时间:2012-09-28 03:33:53

标签: java string for-loop return break

我正在使用for循环,每次循环都需要返回一个String,但是使用“return”会打破循环,Eclipse会抛出“无法访问的代码”错误。任何建议

5 个答案:

答案 0 :(得分:2)

如果返回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)将整个数组转换为单个字符串:

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);

返回值将使用“,”分隔的列表元素进行格式化,并用方括号括起来:“[]”。

答案 1 :(得分:2)

我认为你的逻辑被打破了

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();

更新工作示例

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

答案 2 :(得分:0)

您将需要使用ArrayList存储所有字符串。

然后在for循环完成后返回ArrayList。像。的东西。

String returnString = "";

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


return returnString

答案 3 :(得分:0)

为什么复杂化问题..只返回一个字符串数组

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

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

答案 4 :(得分:0)

您应该考虑从该方法返回一个String数组。

只是纠正for循环不会返回任何内容。它是返回值的函数。

最后很难评论,因为它不清楚你想要达到的目标。