2017年8月21日

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")
        }
    }

 

posted @ 2017-08-21 20:56 屁屁侠 阅读(556) 评论(0) 推荐(0) 编辑

2017年7月12日

Android SDK 在线更新镜像服务器资源

摘要: 大连东软信息学院镜像服务器地址:- http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:- IPv4: http://ubuntu.buct.edu.cn/ 端口:80- IPv4: http://ubuntu.buct.cn/ 端口:80- IPv6: 阅读全文

posted @ 2017-07-12 16:25 屁屁侠 阅读(651) 评论(0) 推荐(0) 编辑

2017年7月7日

JSON.parse()和JSON.stringify()

摘要: parse用于从一个字符串中解析出json对象,如var str = '{"name":"huangxiaojian","age":"23"}' 结果:JSON.parse(str) Object age: "23"name: "huangxiaojian"__proto__: Object 注意: 阅读全文

posted @ 2017-07-07 23:11 屁屁侠 阅读(252) 评论(0) 推荐(0) 编辑

2017年7月3日

npm WARN React-native@0.35.0 requires a peer of react@~15.3.1 but none was installed.

摘要: 解决方案: 方法一: 方法二:在package.json中可以添加依赖 阅读全文

posted @ 2017-07-03 20:26 屁屁侠 阅读(5885) 评论(0) 推荐(0) 编辑

查看安装的react-native和react版本

摘要: 转:http://blog.csdn.net/miss_ok/article/details/52777115 npm info React-native(目前是0.34.1) 知道最新版本后,通过以下命令来安装:npm install --save react-native@0.34.1 同样,n 阅读全文

posted @ 2017-07-03 20:25 屁屁侠 阅读(7394) 评论(0) 推荐(0) 编辑

Super expression must either be null or a function, not undefined

摘要: 按照之前买的用JavaScript开发移动应用的例子来编写的,然后报了这个错。我的头部声明是这样的 经过查询后是由于'React'和'Component'从'react native'分离到了'react'模块。所以这里我们只引入'react native'的模块是不够的,改成这样: 阅读全文

posted @ 2017-07-03 20:16 屁屁侠 阅读(11493) 评论(0) 推荐(0) 编辑

2017年6月23日

python os.path模块

摘要: from:http://www.cnblogs.com/dkblog/archive/2011/03/25/1995537.html 阅读全文

posted @ 2017-06-23 11:24 屁屁侠 阅读(311) 评论(0) 推荐(0) 编辑

python class和class(object)用法区别

摘要: from:https://my.oschina.net/shyl/blog/692930 阅读全文

posted @ 2017-06-23 11:20 屁屁侠 阅读(24523) 评论(0) 推荐(0) 编辑

2016年11月1日

c++中的##和#的区别

摘要: ##是一个连接符号,用于把参数连在一起 #是“字符串化”的意思。出现在宏定义中的#是把跟在后面的参数转换成一个字符串#define paster( n ) printf( "token " #n" = %d\n ", token##n ) 所以paster(9);就是相当于 printf("toke 阅读全文

posted @ 2016-11-01 15:04 屁屁侠 阅读(16980) 评论(0) 推荐(1) 编辑

2016年10月26日

c++中的&

摘要: 变量的前面表示取变量地址赋值给指针, 如:int a = 0; int *pa = &a;类型后面表示引用,引用即变量的替身。 int a = 0; int &ref = a;操作ref就跟操作a是一样的还有一种的与预算 如 int a = 0; a &= 0;// 按位与操作 阅读全文

posted @ 2016-10-26 17:47 屁屁侠 阅读(276) 评论(0) 推荐(0) 编辑

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示