segmentfault 对 mackdown 语法的反对不是很好,有些图片都显示不进去,大家能够去我的掘金查看这篇文章。

一、Kotlin 必备根底

<font face= 黑体>在 给 Android 开发者的 Kotlin 教程(一)中咱们次要就是简略的讲了一下 Kotlin 与 Java 的比拟。这一节咱们来讲一下 Kotlin 的必备根底,别离是 Kotlin 中的<font color= red>根本数据类型数组</font>以及<font color= red>汇合</font>这三大知识点。

二、意识 Kotlin 根本类型

<font face= 黑体>Kotlin 的根本数值类型包含 Byte、Short、Int、Long、Float、Double 等。不同于 Java 的是,字符不属于数值类型,是一个独立的数据类型

对于整数,存在四种具备不同大小和值范畴的类型
类型位宽最小值最大值
Byte8-128127
Short16-3276832767
Int32-2^312^31 - 1
Long64-2^632^63 - 1
对于浮点数,Kotlin 提供了 Float 和 Double 类型

| 类型 | 位宽 |

Float32
Double64

2.1、根本类型代码演练

<font face= 黑体>Kotlin 定义变量的关键字只有 val 和 var 两种。然而在定义 Int 类型变量的时候咱们并不需要<font color= red> val Int2: Int = 2 </font>这样写,间接<font color= red> val Int2 = 2</font> 就能够了。那是因为 kotlin 能为咱们主动推导变量的类型而不须要咱们指定。

<font face= 黑体>利用上面的 printType() 办法咱们就能够查看每个变量具体的类型了。

fun main() {    println("---main---")    baseType()}// Kotlin 根本类型代码演示fun baseType() {    val num1 = -1.67 // double    val num2 = 2    // Int    val num3 = 2f   // Float    val int1 = 3    // Int    println("num1:$num1 num2:$num2 num3:$num3 int1:$int1")    println(abs(num1))    println(num1.toInt()) // 转换成Int    printType(num1)    printType(num2)    printType(num3)    printType(int1)}fun printType(param: Any) {    println("$param is ${param::class.simpleName} type")}

<font face= 黑体>代码运行后果如下所示:

三、走进 Kotlin 的数组

<font face= 黑体>数组在 Kotlin 中应用 Array 类来示意,它定义了 get 与 set 办法以及 size 属性,以及一些其余有用的成员办法,具体源码如下所示:

public class Array<T> {    public inline constructor(size: Int, init: (Int) -> T)    public operator fun get(index: Int): T    public operator fun set(index: Int, value: T): Unit    public val size: Int    public operator fun iterator(): Iterator<T>}

3.1、Kotlin 数组的创立技巧

3.1.1、应用 arrayOf() 办法创立数组

<font face= 黑体>咱们能够应用库办法 arrayOf() 来创立一个数组并传递元素值给它,例如 arrayOf(1, 2, 3) 创立了 array [1, 2, 3]。

3.1.2 、应用 arrayOfNulls() 办法创立数组

<font face= 黑体>也能够应用库办法 arrayOfNulls() 创立一个指定大小的、所有元素都为空的数组。

3.1.3 、创立有初始值的数组

// 创立一个 Array<String> 初始化为 ["0", "1", "4", "9", "16"]val asc = Array(5) { i -> (i * i).toString() }asc.forEach { println(it) }

<font face= 黑体>Kotlin 不让咱们把 Array 赋值给 Array,以避免可能的运行时失败。

3.1.4 、原生类型数组

<font face= 黑体>Kotlin 也有无装箱开销的专门的类来示意原生类型数组: IntArray、ByteArray、 ShortArray 等等。这些类与 Array 并没有继承关系,然而它们有同样的办法属性集。它们也都有相应的工厂办法:

//通过intArrayOf、floatArrayOf、doubleArrayOf等创立数组val x: IntArray = intArrayOf(1, 2, 3)println("x[1] + x[2] = ${x[1] + x[2]}")
// 大小为 5、值为 [0, 0, 0, 0, 0] 的整型数组val arr = IntArray(5)// 例如:用常量初始化数组中的值// 大小为 5、值为 [42, 42, 42, 42, 42] 的整型数组val arr = IntArray(5) { 42 }// 例如:应用 lambda 表达式初始化数组中的值// 大小为 5、值为 [0, 1, 2, 3, 4] 的整型数组(值初始化为其索引值)var arr = IntArray(5) { it * 1 }

3.2、Kotlin 数组的遍历技巧

3.2.1、数组遍历

for (item in array) {    println(item)}

3.2.2、带索引遍历数组

for (i in array.indices) {    println(i.toString() + "->" + array[i])}

3.2.3、遍历元素(带索引)

for ((index, item) in array.withIndex()) {    println("$index->$item")}

3.2.4、forEach 遍历数组

array.forEach { println(it) }

3.2.5、forEach 增强版

array.forEachIndexed { index, item ->    println("$index:$item")}

3.3、数组代码演练

fun main() {    println("---main---")    arrayType()}/** * 数组 */fun arrayType() {    // arrayOf    val array: Array<Int> = arrayOf(1, 2, 3)    // ArrayOfNulls    val array1 = arrayOfNulls<Int>(3)    array1[0] = 4    array1[1] = 5    array1[2] = 6    // 通过Array的构造函数    val array2 = Array(5) { i -> (i * i).toString() }    // 原生类型数组    val x = intArrayOf(1, 2, 3)    println("x[0] + x[1] = ${x[0] + x[1]}")    // 大小为5、值为 [0, 0, 0, 0, 0] 的整型数组    val array3: IntArray = IntArray(5)    // 例如:用常量初始化数组中的值    // 大小为5、值为 [42, 42, 42, 42, 42] 的整型数组    val array4 = IntArray(5) { 42 }    // 例如:应用 lambda 表达式初始化数组中的值    // 大小为5、值为 [0, 1, 2, 3, 4] 的整型数组 (值初始化为其索引值)    val array5 = IntArray(5) { it * 1 }    println(array5[4])    /***遍历数组的5种形式***/    // 数组遍历    for (item in array) {        println(item)    }    // 带索引遍历数组    for (i in array.indices) {        println("$i -> ${array[i]}")    }    // 带索引遍历数组2    for ((index, item) in array.withIndex()) {        println("$index -> $item")    }    //forEach 遍历数组    array.forEach { println(it) }    //forEach 增强版    array.forEachIndexed { index, item ->        println("$index -> $item")    }}

四、走进 Kotlin 的汇合

<font face= 黑体>Kotlin 规范库提供了一整套用于治理汇合的工具,汇合是可变数量(可能为零)的一组条目,各种汇合对于解决问题都具备重要意义,并且常常用到。

  • <font face= 黑体>List 是一个有序汇合,可通过索引拜访元素。元素能够在 list 中呈现屡次。List 列表的程序很重要并且元素能够反复。
  • <font face= 黑体>Set 是惟一元素的汇合。一般来说 set 中元素的程序并不重要。
  • <font face= 黑体>Map 是一组键值对。键是惟一的,每个键都刚好映射到一个值,值能够反复。

4.1、汇合的可变性与不可变性

<font face= 黑体>在 Kotlin 中存在两种意义上的汇合,一种是能够批改的一种是不可批改的。

4.1.1、不可变汇合

val stringList = listOf("one", "two", "one")println(stringList)val stringSet = setOf("one", "two", "three")println(stringSet)

4.1.2、可变汇合

val numbers = mutableListOf(1, 2, 3, 4)numbers.add(5)numbers.removeAt(1)numbers[0] = 0println(numbers)
不难发现,每个不可变汇合都有对应的可变汇合,也就是以 mutable 为前缀的汇合。

4.2、汇合排序

<font face= 黑体>在 Kotlin 中提供了弱小对的汇合排序的 API,让咱们一起来学习一下:

val numbers = mutableListOf(1, 2, 3, 4)//随机排列元素numbers.shuffle()println(numbers)numbers.sort()//排序,从小打到numbers.sortDescending()//从大到小println(numbers)//定义一个Person类,有name 和 age 两属性data class Language(var name: String, var score: Int)val languageList: MutableList<Language> = mutableListOf()languageList.add(Language("Java", 80))languageList.add(Language("Kotlin", 90))languageList.add(Language("Dart", 99))languageList.add(Language("C", 80))//应用sortBy进行排序,适宜单条件排序languageList.sortBy { it.score }println(languageList)//应用sortWith进行排序,适宜多条件排序languageList.sortWith(compareBy(        //it变量是lambda中的隐式参数        { it.score }, { it.name }))println(languageList)

4.3、汇合中的 Set 与 Map

/**set**/val hello = mutableSetOf("H", "e", "l", "l", "o")//主动过滤反复元素hello.remove("o")//汇合的加减操作hello += setOf("w", "o", "r", "l", "d")println(hello)/**Map<K, V> 不是 Collection 接口的继承者;然而它也是 Kotlin 的一种汇合类型**/val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)println("All keys: ${numbersMap.keys}")println("All values: ${numbersMap.values}")if ("key2" in numbersMap) println("Value by key \"key2\": ${numbersMap["key2"]}")if (1 in numbersMap.values) println("1 is in the map")if (numbersMap.containsValue(1)) println(" 1 is in the map")

五、汇合问题

问题1、两个具备雷同键值对,但程序不同的 Map 相等吗?为什么?

问题2、两个具备雷同元素,但程序不同的 list 相等吗?为什么?

 /** * Q1、两个具备雷同键值对,但程序不同的Map相等吗?为什么? * */val numberMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 4, "key5" to 5)val anotherMap = mapOf("key2" to 2, "key1" to 1, "key3" to 3, "key4" to 4, "key5" to 5)println("anotherMap == numberMap:${anotherMap == numberMap}") /** * Q2、两个具备雷同元素,但程序不同的list相等吗?为什么? * */val stringList1 = listOf<String>("one", "two", "three")val stringList2 = listOf<String>("three", "two", "one")println("stringList1 == stringList2:${stringList1 == stringList2}")

<font face= 黑体>运行后果如下所示:

<font face= 黑体>能够看到<font color= red>无论键值对的程序如何,蕴含雷同键值对的两个 Map 是相等的</font>,然而<font color= red>两个具备雷同元素,但程序不同的 list 是不相等的</font>。具体起因的话咱们能够去查看源码:

Map 中的比拟源码
 public boolean equals(Object o) {    if (o == this)        return true;    if (!(o instanceof Map))        return false;    Map<?,?> m = (Map<?,?>) o;    if (m.size() != size())        return false;        // 比拟源码    try {        Iterator<Entry<K,V>> i = entrySet().iterator();        while (i.hasNext()) {            Entry<K,V> e = i.next();            K key = e.getKey();            V value = e.getValue();            if (value == null) {                // 要害代码                if (!(m.get(key)==null && m.containsKey(key)))                    return false;            } else {                if (!value.equals(m.get(key)))                    return false;            }        }    } catch (ClassCastException unused) {        return false;    } catch (NullPointerException unused) {        return false;    }    return true;}

<font face= 黑体>能够看到 Map 的 equals() 办法会去拿出一个 Map 中的一个 key 值,而后查看另外一个 Map 中是否有同样的 key 值,而后再去比拟这个两个 key 值所对应的值是否相等就能够了,所以跟程序是无关的。

List 中的比拟源码
public boolean equals(Object o) {   if (o == this)        return true;    if (!(o instanceof List))        return false;    ListIterator<E> e1 = listIterator();    ListIterator<?> e2 = ((List<?>) o).listIterator();    while (e1.hasNext() && e2.hasNext()) {        E o1 = e1.next();        Object o2 = e2.next();        // 要害代码        if (!(o1==null ? o2==null : o1.equals(o2)))            return false;    }    return !(e1.hasNext() || e2.hasNext());}

<font face= 黑体>能够看到 List 的 equals() 办法会去比拟每一个索引上对应的值是否相等,所以程序不同的 list 是不相等的。

六、残缺代码

import kotlin.math.absfun main() {    println("---main---")    //baseType()    arrayType()    //collectionType()    //collectionSort()}fun baseType() {    val num1 = -1.67 // double    val num2 = 2    // Int    val num3 = 2f   // Float    val int1 = 3    // Int    println("num1:$num1 num2:$num2 num3:$num3 int1:$int1")    println(abs(num1))    println(num1.toInt()) // 转换成Int    printType(num1)    printType(num2)    printType(num3)    printType(int1)}fun printType(param: Any) {    println("$param is ${param::class.simpleName} type")}/** * 数组 */fun arrayType() {    // arrayOf    val array: Array<Int> = arrayOf(1, 2, 3)    // ArrayOfNulls    val array1 = arrayOfNulls<Int>(3)    array1[0] = 4    array1[1] = 5    array1[2] = 6    // 通过Array的构造函数    val array2 = Array(5) { i -> (i * i).toString() }    // 原生类型数组    val x = intArrayOf(1, 2, 3)    println("x[0] + x[1] = ${x[0] + x[1]}")    // 大小为5、值为 [0, 0, 0, 0, 0] 的整型数组    val array3: IntArray = IntArray(5)    // 例如:用常量初始化数组中的值    // 大小为5、值为 [42, 42, 42, 42, 42] 的整型数组    val array4 = IntArray(5) { 42 }    // 例如:应用 lambda 表达式初始化数组中的值    // 大小为5、值为 [0, 1, 2, 3, 4] 的整型数组 (值初始化为其索引值)    val array5 = IntArray(5) { it * 1 }    println(array5[4])    /***遍历数组的5种形式***/    // 数组遍历    for (item in array) {        println(item)    }    // 带索引遍历数组    for (i in array.indices) {        println("$i -> ${array[i]}")    }    // 带索引遍历数组2    for ((index, item) in array.withIndex()) {        println("$index -> $item")    }    //forEach 遍历数组    array.forEach { println(it) }    //forEach 增强版    array.forEachIndexed { index, item ->        println("$index -> $item")    }}/** * 汇合 */fun collectionType() {    //不可变汇合    val stringList = listOf<String>("one", "two", "one")    println(stringList)    val stringSet = setOf<String>("one", "two", "one")    println(stringSet)    // 可变汇合    val numbers = mutableListOf<Int>(1, 2, 3, 4)    numbers.add(5)    numbers.removeAt(1)    numbers[0] = 0    println(numbers)    // 主动过滤反复元素    val hello = mutableSetOf("H", "e", "l", "l", "o")    hello.remove("o")    println(hello)    // 汇合的加减操作    hello += setOf("w", "o", "r", "l", "d")    println(hello)    /** Map<k, v> 不是 Collection 接口的继承者:然而它也是 Kotlin 的一种汇合类型 **/    val numberMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 4, "key5" to 5)    println("All keys:${numberMap.keys}")    println("All valus:${numberMap.values}")    // 判断 key 是否在map外面    if ("key2" in numberMap) println("Value by key2: ${numberMap["key2"]}")    // 判断 value 是否在map外面    if (1 in numberMap.values) println("1 is in the map")    if (numberMap.containsValue(1)) println("1 is in the map")    /**     * Q1、两个具备雷同键值对,单程序不同的Map相等吗?为什么?     *     * 无论键值对的程序如何,蕴含雷同键值对的两个 Map 是相等的     */    val anotherMap = mapOf("key2" to 2, "key1" to 1, "key3" to 3, "key4" to 4, "key5" to 5)    println("anotherMap == numberMap:${anotherMap == numberMap}")    /**     * Q2、两个具备雷同元素,但单程序不同的list相等吗?为什么?     *      *  不相等  源码调试     */    val stringList1 = listOf<String>("one", "two", "three")    val stringList2 = listOf<String>("one", "two", "three")    println("stringList1 == stringList2:${stringList1 == stringList2}")    stringList.equals(stringList2)}/** * 汇合排序 */fun collectionSort() {    val number3 = mutableListOf(1, 2, 3, 4)    // 随机排序    number3.shuffle()    println(number3)    // 从小到大    number3.sort()    println(number3)    // 从大到小    number3.sortDescending()    println(number3)    // 条件排序    data class Language(var name: String, var score: Int)    val languageList = mutableListOf<Language>()    languageList.add(Language("Java", 80))    languageList.add(Language("Kotlin", 90))    languageList.add(Language("Dart", 99))    languageList.add(Language("C", 80))    // 应用sortBy进行排序,适宜单条件排序  分数从小到大排序    languageList.sortBy {        it.score    }    println(languageList)    // 应用sortWith进行排序,适宜多条件排序    languageList.sortWith(compareBy({ it.score }, { it.name }))    println(languageList)}