Variable number of arguments (Varargs)
A parameter of a function (normally the last one) may be marked with vararg modifier:
fun <T> asList(vararg ts: T): List<T> {
val result = ArrayList<T>()
for (t in ts) // ts is an Array
result.add(t)
return result
}
allowing a variable number of arguments to be passed to the function:
val list = asList(1, 2, 3)
Inside a function a vararg-parameter of type T is visible as an array of T, i.e. the ts variable in the example above has type Array<out T>.
Only one parameter may be marked as vararg. If a vararg parameter is not the last one in the list, values for the following parameters can be passed using the named argument syntax, or, if the parameter has a function type, by passing a lambda outside parentheses.
When we call a vararg-function, we can pass arguments one-by-one, e.g. asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *):
val a = arrayOf(1, 2, 3)
val list = asList(-1, 0, *a, 4)
1.一般是函数中的最后一个参数
2.在一个函数中只可以声明一个参数为vararg
3.如果可变参数不是函数中的最后一个参数,则后面的参数通过命名参数语法来传递参数值
4.如果参数类型是函数, 可以在括号之外传递一个
5.传递一个已有的数组则通过*号
@Test fun testPair() {
val list = listOf("hello", true, "world")
val array = list.toTypedArray()
printVararg(*array, lastPrams = "...list...")
}
fun printVararg(vararg params: Any, lastPrams: String) {
for (param in params) {
println("param : $param, lastPrams: $lastPrams")
}
}
Variable number of arguments (Varargs)的更多相关文章
- Lua 变长参数(variable number of arguments)
lua变长参数 function add ( ... ) for i, v in ipairs{...} do print(i, ' ', v) end end add(1, 2, 'sdf') lu ...
- [翻译] Macros with a Variable Number of Arguments - GCC
可变参数宏(Variadic Macro) 在1999年的ISO C标准中,可以声明一个像函数一样接受可变参数的宏.定义这种宏的语法与函数的定义相似.这是一个例子: #define debug(for ...
- How to create functions that can accept variable number of parameters such as Format
http://www.chami.com/tips/delphi/112696D.html Sometimes it's necessary to pass undefined number of [ ...
- wrong number of arguments,java方法反射时数组参数的坑
java方法中只有一个参数是数组,反射的时候我们不能想当然的传歌数组进去,传数组进去的时候表示多个参数. 两个数组不是一个意思啊. 我们应该把数组转为objet,这样才表示一个参数. import j ...
- Error Code: 1318. Incorrect number of arguments for PROCEDURE company.new_procedure; expected 2, got
1.错误叙述性说明 20:27:34 call new_procedure(20150112) Error Code: 1318. Incorrect number of arguments for ...
- Error Code: 1318. Incorrect number of arguments for PROCEDURE student.new_procedure; expected 0, got
1.错误描述 13:58:20 call new_procedure('2000','zhangsan') Error Code: 1318. Incorrect number of argument ...
- [Jedis] ERR wrong number of arguments for 'mget'
看别人写的代码是件比较痛苦的事情,更加痛苦的是别人的代码出错还要负责调试好. 关于如何迅速定位问题和调试代码,我的一点感受是:逐行认真查看错误信息,在这些信息中找自己熟悉的内容(包括文件名.方法名等) ...
- hbase ERROR: wrong number of arguments (3 for 4)
hbase(main):036:0> get 'ddl', 'example', 'info:age'COLUMN ...
- 反射报错java.lang.IllegalArgumentException: wrong number of arguments
class Person{ private String name ; private String sex ; public Person(){ System.out.println("c ...
随机推荐
- BZOJ.3532.[SDOI2014]LIS(最小割ISAP 退流)
BZOJ 洛谷 \(LIS\)..经典模型? 令\(f_i\)表示以\(i\)结尾的\(LIS\)长度. 如果\(f_i=1\),连边\((S,i,INF)\):如果\(f_i=\max\limits ...
- LOJ.6235.区间素数个数(Min_25筛)
题目链接 \(Description\) 给定\(n\),求\(1\sim n\)中的素数个数. \(2\leq n\leq10^{11}\). \(Solution\) Min_25筛.只需要求出\ ...
- Centos6 安装RabbitMq3.7.7
安装包准备官网地址: Erlang安装包下载:https://www.erlang-solutions.com/resources/download.html RabbitMq安装包下载: ...
- JavaScript基础笔记(十)表单脚本
表单脚本 一.表单基础知识 JavaScript中表单对应的是HTMLFormElement类型,该类型继承自HTMLElement类型. 通过document.forms可以获得所有表单元素,通过数 ...
- [SDOI2017]树点涂色
Description: Bob有一棵\(n\)个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不 ...
- pymysql 使用twisted异步插入数据库:基于crawlspider爬取内容保存到本地mysql数据库
本文的前提是实现了整站内容的抓取,然后把抓取的内容保存到数据库. 可以参考另一篇已经实现整站抓取的文章:Scrapy 使用CrawlSpider整站抓取文章内容实现 本文也是基于这篇文章代码基础上实现 ...
- 关于EL表达式的学习总结
一.EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数 ...
- 阿里云服务器安装postgresql
https://blog.csdn.net/wlwlwlwl015/article/details/52399739 安装oracle http://www.cnblogs.com/vesaa/p/9 ...
- Vue(二十一)使用express模拟接口数据
1.下载express ... 2.使用vue-cli下载好项目文件 ... 3.找到文件 build - webpack.dev.conf.js 'use strict' const utils = ...
- 常见问题2:html+css效果综合整理
1.---文本框提示文字颜色--placeholder属性 <input type="text" placeholder="请输入提示文字" /> ...