简体   繁体   English

可以返回String数组

[英]Possible to return a String array

是否有可能在java中创建一个返回String[]的方法?

Yes, but in Java the type is String[] , not string[] . 是的,但在Java中,类型是String[] ,而不是string[] The case is important. 这个案子很重要。

For example a method could look something like this: 例如,方法可能如下所示:

public String[] foo() {
    // ...
}

Here is a complete example: 这是一个完整的例子:

public class Program
{
    public static void main(String[] args) {
        Program program = new Program();
        String[] greeting = program.getGreeting();
        for (String word: greeting) {
            System.out.println(word);
        }
    }

    public String[] getGreeting() {
        return new String[] { "hello", "world" };
    }
}

Result: 结果:

hello
world

ideone ideone

Yes. 是。

/** Returns a String array of length 5 */
public String[] createStringArray() {
    return new String[5];
}

Yes: 是:

String[] dummyMethod()
{
    String[] s = new String[2];
    s[0] = "hello";
    s[1] = "world";
    return s;
}

yes. 是。

public String[] returnStringArray()
{
    return new String[] { "a", "b", "c" };
}

Do you have a more specific need? 你有更具体的需求吗?

Sure 当然

public String [] getSomeStrings() {
    return new String [] { "Hello", "World" };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM