具有无限参数的 Java 方法 [英] Java method with unlimited arguments
本文介绍了具有无限参数的 Java 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Spring 框架使用的方法可以传递任意数量的参数.
The Spring framework uses methods where you can pass as many arguments as you like.
我想写一个函数,它也可以接受无限量的数据.这个功能是如何命名的,以便我可以阅读它?或者我如何定义它?
I would like to write a function that can also take an unlimited amount of data. How is this feature called so that I can read about it? Or how can I define it?
推荐答案
它叫做 可变参数.
它允许一个方法接受任意数量的参数.它们可以作为方法中的数组访问:
It allows a method to take any number of arguments. They are accessible as an array in the method:
public void foo(String... args) {
for (String arg : args) {
// do smth with arg.
}
}
这是语法糖.编译器隐藏了数组的创建,而不是
This is syntactic sugar. The compiler hides the array creation, so instead of
bar.foo(new String[] {"1", "2", "3"});
你写
bar.foo("1", "2", "3");
这篇关于具有无限参数的 Java 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文