遍历方法参数 [英] Iterating through method parameters
问题描述
这是一个简单的问题,也许是个琐碎的问题:在Java中,我如何遍历传递给我正在工作的方法的参数?
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?
我需要它来trim()
所有都是字符串的
I need it to trim()
all of them that are strings.
编辑
更准确地说,可以使用以下示例(用伪代码编写,以反映我希望其工作的方式):
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())
重点是getThisMethod().getParameters()
.我必须在那个地方写些什么?
The point is that getThisMethod().getParameters()
. What must I write in that place?
推荐答案
单个参数不可迭代;您将需要一个集合.
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.
如果您有太多令人讨厌的String参数,则可能需要重新考虑您的方法.要么将所有这些参数都封装到一个对象中(因为它们是相关的),要么该方法试图做太多事情.这说明我缺乏凝聚力.
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.
这篇关于遍历方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!