String... parameter in Java(Java中的字符串...参数)
问题描述
我必须为家庭作业实现一个 API,而我的老师使用我不熟悉的符号表示 API 中的一种方法(基于 javadoc).
I have to implement an API for a homework assignment, and my instructor has used a notation I am unfamiliar with for one of the methods in the API (javadoc based).
public void method(String... strs);
..."是什么意思?后来看起来我需要使用单个字符串实际参数以及多个字符串实际参数来调用相同的方法...
What do the '...' mean? It later looks like I'll need to call this same method using a single string actual parameter, as well as multiple string actual parameters...
Java 没有可选参数(据我所知),所以我在这里有点困惑......
Java doesn't have optional arguments (to my knowledge), so I am a little confused here...
推荐答案
叫做varargs;http://docs.oracle.com/javase/6/docs/technotes/指南/语言/varargs.html
这意味着您可以向方法传递任意数量的参数(甚至为零).
It means you can pass an arbitrary number of arguments to the method (even zero).
在该方法中,参数将自动放入指定类型的数组中,您可以使用该数组访问各个参数.
In the method, the arguments will automatically be put in an array of the specified type, that you use to access the individual arguments.
这篇关于Java中的字符串...参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java中的字符串...参数
基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
