dcsimg
How can I create a method which takes one or more ...
28 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 
Results  1 to 10 of 28  « Prev 1 2 3 Next » 

Posted By:   king_hw
Posted On:   Monday, January 22, 2001 12:10 AM

How can I create a method which takes one or more optional parameters?

Re: How can I create a method which takes one or more...

Posted By:   Jesper_Andersen  
Posted On:   Monday, March 12, 2001 12:33 PM

There isn't really replacement to va_args in c and C++. Such typeless interfaces violate the underlying philosophies of Java. So you'll never be able to write a method like


printf(String format, ...)

If there's a need for a variable list of arguments you'll can take advantage of the singly-rooted object heirarchy and pack a collection of objects as pass them into the method, where you'll unpack them again. This isn't as pretty, but it will give you the flexibiliy you want. But note that the object purists will tell you that the kind of functionality you want is a sign of a sick design, and you may want to spend some time re-thinking your API to see if maybe something can be refactored to solve the problem more elegantly.

Re: How can I create a method which takes one or more...

Posted By:   Jim_Harte  
Posted On:   Monday, March 12, 2001 09:44 AM

All methods must have a static number of arguments, however, what the argument is can change how it is interpreted. If you have a list of possible pieces of data which may be sent into the method, then have an argument which will be a collection. Since you will likely not know which pieces of data are going to be sent and in what order, you will want this collection to be a keyed collection where the key will identify what data the associated element contains.

Re: How can I create a method which takes one or more...

Posted By:   Anonymous  
Posted On:   Sunday, March 11, 2001 07:46 PM

java itself does not support optional parameter, but you can use overloading to achieve the same result, example, in C++:
void method(int a,int b=10){
//dosomething;
}
in java:
void method(int a){
method(a,10);
}
void method(int a,int b){
do something;
}
hope this may help you

Re: How can I create a method which takes one or more...

Posted By:   Lukas_Bradley  
Posted On:   Thursday, March 8, 2001 08:21 AM

I can think of two solutions.



First, use method overloading. Java allows you to use the same method name with different parameters. For instance, all of the following methods can be used in the same class:




int indexOf(int ch, int fromIndex)
int indexOf(String str)
int indexOf(String str, int fromIndex)


These three methods (from the String class) have the same name and return type, but use different, 'optional' parameters.



The second option is to pass an enumerate of general data types to the method, and have the method decide what needs to be done.




public String myMethod (String str, Vector v)
or
public String myMethod (String str, Object [] objs)


Within that Vector or Object array, you could store hundreds of optional arguments.



Parsing these, in no particular order, won't be fun or pretty. Also, this really isn't good coding style, because an evil programmer could abuse this method, and possibly make things nasty for you. However, it is a hack that will work.

Re: How can I create a method which takes one or more...

Posted By:   Tim_Rohaly  
Posted On:   Monday, March 5, 2001 11:04 AM


Unlike C and C++, Java has no special syntax to indicate
that a method may accept a variable number of arguments.
You can, however, acheive the same functionality
by simply overloading your methods. For example,
java.lang.String has 11 constructor methods,
each of which accept a different number and/or type of
arguments.



While overloading is good when you know what combinations
of arguments may be used, if you wish to accept an
unbounded number of arguments you should pass these
in as an array or some other data structure.

Re: How can I create a method which takes one or more...

Posted By:   Tom_Kerigan  
Posted On:   Saturday, March 3, 2001 08:32 AM

The solution is simple, but a little hackish. Since Java doesn't have the facilities to handle optional parameters (with default values), you have to simulate this behaviour by creating methods with signatures WITHOUT the optional parameter(s), having them wrap the call to the original method. These methods will essentially call the actual method, on your behalf, with the optional parameters taken care of. Here's an example:



Suppose you have the method:



public void myMethod(int parm1, int parm2, int parm3, int parm4) {

...

}



and you want parm4 to be an optional parameter. Creating the following method with the wrappered call to the original myMethod, simulates the intended behaviour:



public void myMethod(int parm1, int parm2, int parm3) {



int parm4 = someDefaultParm4Value;

myMethod(parm1, parm2, parm3, someDefaultParm4Value);




}

Re: How can I create a method which takes one or more...

Posted By:   Kevin_Riff  
Posted On:   Friday, March 2, 2001 02:21 PM

You can use the technique known as overloading. Declare two or more methods with the same name but different parameters. When you call the method, the compiler will look at the parameters you're sending and decide which overloaded method to call.




public void myMethod(int a, int b, int c) {
// Do something...
}


public void myMethod(int a, int b) {
// Call myMethod with a default value
myMethod(a, b, 0);
}


public void myMethod(int a) {
// Call myMethod with default values
myMethod(a, -1, 0);
}

Re: How can I create a method which takes one or more...

Posted By:   Sandip_Chitale  
Posted On:   Thursday, March 1, 2001 10:19 AM

The non-stringly-typed way is to do the
following -


public static void method(Object[] params);


You have to convert event the primitive
types like int to Integer objects.


This is how java.lang.reflect package deals
with variable arguments. In fact, You can find many example of that in java.text package also.


You can always use overloaded methods, but I guess you are looking for a compact single
method like C/C++ printf.


They are working on a JSR to make it easy -


http://java.sun.com/aboutJava/communityprocess/jsr/jsr_065_cncsarray.html

Re: How can I create a method which takes one or more...

Posted By:   Vadim_Gurov  
Posted On:   Wednesday, February 28, 2001 04:06 AM

pass to method array of Objects or Vector

Re: How can I create a method which takes one or more...

Posted By:   Peter_Kofler  
Posted On:   Tuesday, February 27, 2001 12:46 AM

All optional parameters are given as Objects
in an array:



public void methodWithOptParams(Object mandatoryParam1, ... , Object mandatoryParamN, Object[] optionalParams) {

// check for optional params

if (optionalParams!=null && optionalParams.length==1) {

do something

}

...

}
Results  1 to 10 of 28  « Prev 1 2 3 Next » 
About | Sitemap | Contact