Java变量号码或参数的方法 [英] Java variable number or arguments for a method

查看:152
本文介绍了Java变量号码或参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是可能的声明,将允许可变数目的参数的方法

It is possible to declare a method that will allow a variable number of parameters?

这是在定义中使用的符号指示,该方法应允许可变数目的参数

What is the symbolism used in the definition that indicate that the method should allow a variable number of parameters?

答:可变参数

推荐答案

这是正确的。你可以找到更多关于它在该Oracle指南

That's correct. You can find more about it in this Oracle guide.

下面是一个例子:

void foo(String... args) {
    for (String arg : args) {
        System.out.println(arg);
    }
}

可称为

foo("foo"); // Single arg.
foo("foo", "bar"); // Multiple args.
foo("foo", "bar", "lol"); // Don't matter how many!
foo(new String[] { "foo", "bar" }); // Arrays are also accepted.
foo(); // And even no args.

这篇关于Java变量号码或参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆