Is it possible to make a method that returns a 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With