Kotlin 基础教程之数组容器
作者:jinkui 时间:2021-09-09 17:22:40
Kotlin 基础教程之数组容器
Arrays
Kotlin 标准库提供了arrayOf()创建数组, **ArrayOf创建特定类型数组
val array = arrayOf(1, 2, 3)
val countries = arrayOf("UK", "Germany", "Italy")
val numbers = intArrayOf(10, 20, 30)
val array1 = Array(10, { k -> k * k })
val longArray = emptyArray<Long>()
val studentArray = Array<Student>(2)
studentArray[0] = Student("james")
和Java不一样,Kotlin 的数组是容器类, 提供了 ByteArray, CharArray, ShortArray, IntArray, LongArray, BooleanArray, FloatArray, and DoubleArray。
Lists
List是有序容器,Kotlin 标准库通过listOf()创建list
val intList: List<Int> = listOf(20, 5, 10)
val emptyList: List<String> = emptyList<String>()
val nonNulls: List<String> = listOfNotNull<String>(null, "a", "b", "c")
val doubleList: ArrayList<Double> = arrayListOf(84.88, 100.25, 999.99)
其中,intList, emptyList, nonNulls是只读的实例,要修改这些list,需要进行类型转换
(intList as AbstractList<Int>).set(0, 30)
(nonNulls as java.util.ArrayList).addAll(arrayOf("x", "y"))
Maps
Map是<key, value>容器, Kotlin提供mapOf创建map
val map = mapOf("a" to 1, "b" to 2, "c" to 3)
val value = map.get(b)
val states: MutableMap<String, String>= mutableMapOf("AL" to "Alabama",
"AK" to "Alaska", "AZ" to "Arizona")
val customers: java.util.HashMap<Int, Customer> = hashMapOf(1 to
Customer("Dina", "Kreps", 1), 2 to Customer("Andy", "Smith", 2))
val linkedHashMap: java.util.LinkedHashMap<String, String> =
linkedMapOf("red" to "#FF0000","azure" to "#F0FFFF","white" to "#FFFFFF")
val sortedMap: java.util.SortedMap<Int, String> = sortedMapOf(4 to "d",
1 to "a", 3 to "c", 2 to "b")
Sets
Set是没有重复项的容器, Kotlin提供setOf创建Set
val intSet: Set<Int> = setOf(1, 21, 21, 2, 6, 3, 2) //1,21,2,6,3
val hashSet: java.util.HashSet<Book> = hashSetOf(
Book("Jules Verne", "Around the World in 80 Days Paperback", 2014, "978-1503215153"),
Book("Jules Verne", "Around the World in 80 Days Paperback", 2014, "978-1503215153"))
val sortedIntegers: java.util.TreeSet<Int> = sortedSetOf(11, 0, 9, 11, 9, 8)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://www.jianshu.com/p/a952c11975db
标签:Kotlin,数组容器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringMVC实现多文件上传
2023-10-12 16:49:15
![](https://img.aspxhome.com/file/2023/9/67999_0s.jpg)
Android WorkManager使用以及源码分析
2022-02-04 01:10:26
![](https://img.aspxhome.com/file/2023/4/89554_0s.jpg)
java中String的一些方法深入解析
2023-11-25 21:48:56
![](https://img.aspxhome.com/file/2023/2/60332_0s.png)
解决Java中由于数据太大自动转换成科学计数法的问题
2022-01-24 17:31:45
C#创建Excel多级分组的方法
2022-03-21 15:20:34
![](https://img.aspxhome.com/file/2023/5/81045_0s.jpg)
Spring声明式事务和@Aspect的拦截顺序问题的解决
2023-07-18 12:10:46
SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)
2022-11-05 23:12:26
SpringBoot整合liquibase及liquibase生成初始化脚本的方式
2023-07-29 11:53:18
![](https://img.aspxhome.com/file/2023/9/91089_0s.png)
使用Spring Expression Language (SpEL)全面解析表达式
2021-11-19 08:07:10
![](https://img.aspxhome.com/file/2023/8/101998_0s.png)
C#串口通信实现方法
2023-04-15 06:31:11
Java女装商城系统的实现流程
2023-09-23 11:53:51
![](https://img.aspxhome.com/file/2023/5/81255_0s.png)
C#直线的最小二乘法线性回归运算实例
2022-05-03 11:19:33
![](https://img.aspxhome.com/file/2023/8/104348_0s.png)
java面试题之try中含return语句时代码的执行顺序详解
2023-11-24 07:34:16
![](https://img.aspxhome.com/file/2023/3/59913_0s.png)
Android编程入门之HelloWorld项目目录结构分析
2022-07-23 23:34:40
![](https://img.aspxhome.com/file/2023/1/114361_0s.png)
MyBatis-Plus代码生成器的使用详解
2022-09-04 23:14:40
![](https://img.aspxhome.com/file/2023/4/85824_0s.png)
Java中Steam流的用法详解
2021-12-16 14:18:50
java图搜索算法之DFS与BFS详解
2022-08-15 15:48:57
![](https://img.aspxhome.com/file/2023/6/63026_0s.png)
IDEA内存调试插件(好用)
2022-04-19 08:51:13
![](https://img.aspxhome.com/file/2023/3/108413_0s.jpg)
Java类的定义以及执行顺序学习教程
2023-01-14 10:44:29
基于SpringMVC的全局异常处理器介绍
2023-11-23 20:55:58