桌面支持--outlook高级搜索功能
2015-08-19 16:48
465 查看
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects): "Arbitrary Number of Arguments
You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array)."
You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array)."
Arbitrary Number of Arguments
You can use a construct called varargs to pass an arbitrary number of values to a method. You use varargs when you don't know how many of a particular type of argument will be passed to the method. It's a shortcut to creating an array manually (the previous method could have used varargs rather than an array). To use varargs, you follow the type of the last parameter by an ellipsis (three dots, ...), then a space, and the parameter name. The method can then be called with any number of that parameter, including none.public Polygon polygonFrom(Point... corners) { int numberOfSides = corners.length; double squareOfSide1, lengthOfSide1; squareOfSide1 = (corners[1].x - corners[0].x)*(corners[1].x - corners[0].x) + (corners[1].y - corners[0].y)*(corners[1].y - corners[0].y) ; lengthOfSide1 = Math.sqrt(squareOfSide1); // more method body code follows that creates // and returns a polygon connecting the Points }You can see that, inside the method,
cornersis treated like an array. The method can be called either with an array or with a sequence of arguments. The code in the method body will treat the parameter as an array in either case. You will most commonly see varargs with the printing methods; for example, this
printfmethod:
public PrintStream printf(String format, Object... args)allows you to print an arbitrary number of objects. It can be called like this:
System.out.printf("%s: %d, %s%n", name, idnum, address);or like this
System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);or with yet a different number of arguments.
相关文章推荐
- linux 安装JDK和Tomcat
- 快学Scala习题解答—第二章 控制结构和函数
- 看 Linux kernel 應具備的首要觀念是?
- 测试某个数是否为素数
- 获取程序列表 iHasApp
- NMAP 端口扫描工具下载 + 安装
- 字典树模板
- C++指针探秘
- C语言
- 使用m2eclipse搭建Web项目
- Ubuntu 添加sudo用户
- hdoj1242(bfs+priority_queue)
- 桌面支持--outlook2010邮件召回
- css 超链接 汉字不自动换行 white-space:nowrap;
- Linux系统日常管理
- mac 上wget安装
- aauto学习系列之<13>库
- Xcode 管理provisioning profile
- /proc/dev/net
- javascript函数对象模式--纠结的用法