Is it possible to make a method that returns a String[] in java?
-
15For questions as simple as this, you're probably best off just trying first, and then posting if you get stuck.Ben– Ben2010-10-05 19:53:33 +00:00Commented Oct 5, 2010 at 19:53
-
Welcome to stackoverflow..You might need to first refer to the most basic tutorials flooded out there on net and then try first before even expecting any help on SObeingmanish– beingmanish2018-05-30 16:58:50 +00:00Commented May 30, 2018 at 16:58
Add a comment
|
5 Answers
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
Comments
Yes:
String[] dummyMethod()
{
String[] s = new String[2];
s[0] = "hello";
s[1] = "world";
return s;
}
1 Comment
Grodriguez
The other obvious choice would have been "foo" + "bar", but then I see you managed to have a foo in your answer as well :)