It's a simple and maybe trivial question: in Java how can I iterate through the parameters passed to the method where I'm working?
I need it to trim()
all of them that are strings.
EDIT
To be more precise an example of use can be this (written in a pseudo-code reflecting how I wish it to work):
public void methodName(String arg1, int arg2, int arg3, String arg4, double arg5)
for(Object obj : getThisMethod().getParameters() )
System.out.println(obj.getName() + " = " + obj.toString())
The point is that getThisMethod().getParameters()
. What must I write in that place?
If your function uses Varargs this is pretty straightforward:
private void yourFunction(String... parameters) {
for (String parameter : parameters) {
// Do something with parameter
}
}
Individual parameters aren't iterable; you'd need a collection for that.
You'll just have to get a shovel if they're individual Strings.
If you have so many String parameters that this is oppressive, perhaps your method needs to be re-thought. Either all those parameters should be encapsulated into an object, because they're related, or that method is trying to do too much. It speaks to a lack of cohesion to me.
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