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 ...
随机推荐
- 关于Git安装和操作中可能碰到的问题
markdown PDF 大致的安装流程和操作方法可以参照学长给的 Git和GitHub的简单教程 但是在具体实践过程中可能会碰到一些问题 下载 SSH key 先有远程库,要克隆一个本地库 先有本地 ...
- pandas 基本操作
1. 一维数据结构Series a. 概念:Series 是pandas 的一维数据结构,有重要的两个属性 index 和values b. 初始化: 可以通过 python 的 Lis ...
- Django——photo
要点: models 图片类型文件要使用models.ImageField(upload='文件夹名') 普通文件使用FileField 时间类型使用DatetimeField(auto_now_ad ...
- 编程菜鸟的日记-初学尝试编程-易传媒笔试题(C++实现)
题目:已知存在两个非递减的有序链表List1和List2,现在需要你将两个链表合并成一个有序的非递增序列链表List3,请用C++编码实现.(所有链表均为单链表结构) 思路:此处链表是否都有表头并没有 ...
- vue要点记录(待更新)
Vue实例 每个 Vue 实例都会代理其 data 对象里所有的属性:vm.a===data.a //true 注意只有这些被代理的属性是响应的. 如果在实例创建之后添加新的属性到实例上,它不会触发视 ...
- html-webpack-plugin插件使用chunks属性时报错
报错如下图: 原因是之前template属性使用的html模板内添加了script标签,将其删掉再使用chunks属性自动添加即可. (注释掉也不可以,必须删除!)
- centos7.4 python3.6 Anaconda3 的下安装tensorflow
参考这个博客 https://blog.csdn.net/learn_tech/article/details/79861683 在anaconda环境下,安装tensorflow 2018年04月0 ...
- dubbo学习汇总
1. dubbo官网 http://dubbo.io dubbo 作为一个阿里不用的框架,提供了非常多的资料.在分布式框架设计这方面. 2. 其他地方:http://shiyanjun.cn/arch ...
- 修改Arduino IDE默认字体
文件->首选项 点击直接编辑下面那个文件 修改editor.font这个条目就可以不用那么毁眼了..
- 安装配置Xdebug模块详解
1.XDebug安装配置 (1)下载XDebug下载地址:http://www.xdebug.org/必须下载跟机器上安装的php匹配的版本才行.具体下载方法如下:将phpinfo网页的源代码拷贝到h ...