开发者

Possible to return a String array

开发者 https://www.devze.com 2023-01-19 00:38 出处:网络
Is it possible to make a method that returns a 开发者_StackOverflow中文版String[] in java?Yes, but in Java the type is String[], not string[]. The case is important.

Is it possible to make a method that returns a 开发者_StackOverflow中文版String[] in java?


Yes, but in Java the type is String[], not 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


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" };
}
0

精彩评论

暂无评论...
验证码 换一张
取 消