0

I want to create a method along the lines of [return] methodName(int numberOfTimes, String[numberOfTimes] strings), meaning that, depending on the value of numberOfChoices, I can add that number of String values to the method.

I've wondered and, as I wrote it, it wouldn't work because it would fail at compile time, because numberOfChoices isn't defined, and, if it would compile, it'd still be tricky to work it out.

I think my best bet is going with String... strings and do a for loop like this:

void methodName(int numberOfTimes, String... strings) {
    for(int i = 0, i < numberOfTimes; i++) {
        // do something with strings[i]
    }
}

But I still wonder if what I originally wanted was possible.

EDIT: I'm dumb and am always thinking on "sharing" my methods outside of the workspace, that's why I want it to work on the most general way possible. Solution is actually removing numberOfChoices and just introducing as many String objects as needed in methodName. Something along the lines of methodname("One", "Two"). So, fixed code would be...

void methodName(String... choices) {
    for(int i = 0; i < choices.length; i++) {
        // do something with choices[i]
    }
}
Alxe
  • 381
  • 3
  • 12
  • 2
    Whats the rational of having the first parameter, apart from maybe validation? You could always just pass in as many arguments as you actually need. – Perception Mar 06 '13 at 22:06
  • 1
    `numberOfTimes` does seem redundant indeed... – assylias Mar 06 '13 at 22:07
  • You might want to look at this post: http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values – Chris.Stover Mar 06 '13 at 22:07
  • Because I was thinking on doing a method that prints a menu-like text interface with a set number of choices and paired Strings, then return the choice selected as an int for it to be passed in a switch case statement. EDIT: Actually, rethinking, it is indeed redundant, as I could just use the length of the array as limiter... – Alxe Mar 06 '13 at 22:09

4 Answers4

0

if the variable parameter is always of the same type (like strings) you could use an array or list as a parameter, the method won't care what the size of the array is, just that it's an array

void methodName(int numberOfTimes, String[] myStrings)
Jeff Hawthorne
  • 568
  • 2
  • 12
0

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of String arguments. You could then use loop without knowing the number like:

for (String stringParam: args) {
   // your logic on stringParam
}
zkarthik
  • 949
  • 1
  • 10
  • 18
0

In java arrays are dynamic so their is no need to specify index within method argument

[return] methodname(int numberOfTimes, String[] strings) it works

  • This is in fact the exact same as `methodname(int numberOfTimes, String ... strings)` - except that you have to put in an empty array or null if you do not want to pass any string to methodname. – ddmps Mar 06 '13 at 22:36
0

Java treats variable arguments as arrays, so you can iterate over strings like normal.

Example (returning all the strings concatenated with spaces):

public void wordsToSentence(String... words) {
    final StringBuilder builder = new StringBuilder();
    boolean notFirst = false;
    for (String s : words) {
        if (notFirst) builder.append(" ");
        else notFirst = true;
        builder.append(s);
    }
    return builder.toString();
}
jrtc27
  • 8,496
  • 3
  • 36
  • 68