aspose file tools*
The moose likes Java in General and the fly likes Variable-Length arguement Lists and the use of: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Variable-Length arguement Lists and the use of:" Watch "Variable-Length arguement Lists and the use of:" New topic
Author

Variable-Length arguement Lists and the use of:

Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Feb 23, 2009
Posts: 1183
You can't have a variable return the way it works with variable parameters.

You could use a List or an Array though.


JDBCSupport - An easy to use, light-weight JDBC framework -
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Oct 27, 2005
Posts: 19783
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable-Length arguement Lists and the use of: