Java 可能返回一个字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3867151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible to return a String array
提问by Luron
Is it possible to make a method that returns a String[]
in java?
是否可以String[]
在java中创建一个返回a的方法?
采纳答案by Mark Byers
Yes, but in Java the type is String[]
, not string[]
. The case is important.
是的,但在 Java 中,类型是String[]
,而不是string[]
。案情很重要。
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
回答by Mark Peters
Yes.
是的。
/** Returns a String array of length 5 */
public String[] createStringArray() {
return new String[5];
}
回答by Grodriguez
Yes:
是的:
String[] dummyMethod()
{
String[] s = new String[2];
s[0] = "hello";
s[1] = "world";
return s;
}
回答by John Gardner
yes.
是的。
public String[] returnStringArray()
{
return new String[] { "a", "b", "c" };
}
Do you have a more specific need?
您有更具体的需求吗?
回答by superfell
Sure
当然
public String [] getSomeStrings() {
return new String [] { "Hello", "World" };
}