• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable-Length arguement Lists and the use of:

 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is wrong... It is wrong because I am pulling in a variable number of parameters but pushing out a single parameter. How do I take the variable input and make a variable output?

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't have a variable return the way it works with variable parameters.

You could use a List or an Array though.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, String... Parameter is a String[] Parameter with a number of elements in the array equals to the number of passed arguments and thus when I pass the Parameter argument, it is passing an array to the script engine but it gives a warning that I don't like. Basically if the script language was expecting a string but got a String array, that would be bad. It is unfortunate that I cannot pass a variable number of arguments; really, really unfortunate.

Oficial request to Java developers, please provide some way to pass variable number of arguments to a method.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having problems following.

A method defined as void method(String... varArgs) can take an x-amount of Strings which are then shredded into an Array object. How, if not in some sort of Array, List or Collections would you iterate over arguments which length is not programatically defined ?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a method that takes a String... and you pass a String[], that shouldn't give any problems.

Now, if you have a method that takes an Object... and you pass a String[], that String[] can be treated in two ways:
- as a single element, because String[] is a subtype of Object
- as an Object[], because String[] is a subtype of Object[]

Now you're getting the warning because the compiler can't really determine which one you want; I believe it will treat the String[] as an Object. To prevent this error, cast the String[] as Object[]:
or

jake benn wrote:but it gives a warning that I don't like.

That's a good thing. You should prevent all warnings. After all, there is a reason for the warning. In this case, the warning informs you that the behaviour may not be what you expect.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually I have a method that takes a variable number of strings.

It looks like this:



So, if I am doing this:


the Object is the class and is defigned with an object at the bottom of the Ruby file. The functionName is the name of the function I am calling. In this case it is, "var_length_args". The problem comes when I have a variable number of arguements sitting in my Java method and I need to pass all of those through to the Ruby method. Passing Ruby the string array is a solution but one I have to implement on the Ruby side. I was hoping for a solution on the Java side. Whatever, I guess in the end it is sixes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic