/*** 記錄一些好用的語句and方法*/
class KotlinFunActivity : BaseActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_kotlin_fun)initView()}private fun initView() {/***_____________________________________數(shù)組_______________________________________________***///1.數(shù)組最簡單創(chuàng)建方式,kotlin可以自動識別數(shù)據(jù)的類型
// val mArrays= arrayOf(1,2,3)//2.也可以指定類型創(chuàng)建數(shù)組,kotlin提供了8種byte,short,int,long,char,double,boolean如下簡單寫兩個
// val intArray = intArrayOf(1, 2, 3)
// val longArray = longArrayOf(1L, 2L, 3L)//3.指定長度創(chuàng)建null的數(shù)組必須指定類型;類似java的 Integer[] ints = new Integer[3];
// val mArrays = arrayOfNulls<Int>(3)
// mArrays[0] = 1//4.數(shù)組轉List,as作用是強轉
// var list = mArrays.toList() as ArrayList<Int>//5.數(shù)組的遍歷
// for (a in mArrays) {
// XLogUtils.d("數(shù)組的遍歷->$a")
// }/***_____________________________________List_______________________________________________***///1.創(chuàng)建不可變的list,可以包含空值.
// val people = listOf(PersonBean("湯姆", 23), PersonBean("杰瑞", 34))//2.創(chuàng)建不可變list,不包含空值.
// val people = listOfNotNull(PersonBean("湯姆", 23), PersonBean("杰瑞", 34))//3.創(chuàng)建可變list,可以含空值,跟listOf類似,在kotlin1.1中實際返回的是ArrayList.
// val people = mutableListOf(PersonBean("湯姆", 23), PersonBean("杰瑞", 34))//4.創(chuàng)建可變list,可以含空值,跟mutableListOf類似.
// val people = arrayListOf(PersonBean("湯姆", 23), PersonBean("杰瑞", 34))//5.相當于java中 List<T> list=new ArrayList<>();.
// var people = ArrayList<PersonBean>()
// people.add(PersonBean("湯姆", 23))
// people.add(PersonBean("杰瑞", 34))//6.查詢最大值&最小值,maxBy返回值類型是List<T>.
// XLogUtils.d("最大值1->" + people.maxBy { it.age }!!.name)
// XLogUtils.i("最小值1->" + people.minBy { it.age }!!.name)//7.遍歷List
// people.forEach { p: PersonBean ->
// XLogUtils.e("遍歷people->${p.name}")
// }//8.遍歷componentN的方式
// for ((name, age) in people) {
// XLogUtils.i(name + age)
// }//9.查找list中所有元素;all返回的是boolean類型
// XLogUtils.d("${people.all { it.age >= 22 }}")//10.查找list中所有元素,只要其中一個元素滿足返回true
// XLogUtils.d("${people.any { it.age >= 33 }}")//11.判斷元素在不在list中;in在 !in不在
// val person: PersonBean = people[0]
// XLogUtils.d("在不在集合中-> ${if (person in people) "在" else "不在"}")//12.刪除元素后的list
// val chars = ('a'..'z').toList()
// XLogUtils.d(chars.drop(23)) // 刪除前23個[x, y, z]
// XLogUtils.d(chars.dropLast(23)) // 刪除后23個[a, b, c]
// XLogUtils.d(chars.dropWhile { it < 'x' }) // 刪除小于'x'的[x, y, z]
// XLogUtils.d(chars.dropLastWhile { it > 'c' }) // 刪除大于'c'的[a, b, c]//13.filter過濾;返回結果是list
// XLogUtils.d(people.filter { it.age > 33 }[0].name)//14.列表倒序
// people.reverse()/***_____________________________________Map_______________________________________________***///1.mapOf返回不可變的map,有序
// val courseMap = mapOf(1 to "數(shù)學", 2 to "語文", 3 to "物理", 4 to "化學")//2.可變的map集合,
// val courseMap= mutableMapOf(1 to "數(shù)學", 2 to "語文", 3 to "物理", 4 to "化學")//有序
// val courseMap= linkedMapOf(1 to "數(shù)學", 2 to "語文", 3 to "物理", 4 to "化學")//有序
// val courseMap = hashMapOf(1 to "數(shù)學", 2 to "語文", 3 to "物理", 4 to "化學")//無序的val courseMap = sortedMapOf(1 to "數(shù)學", 2 to "語文", 3 to "物理", 4 to "化學")//無序的//3.map集合取最大值&最小值val maxMap: Map.Entry<Int, String> = courseMap.maxBy { it.key }!!XLogUtils.d("最大值2->${courseMap[maxMap.key]} ")XLogUtils.i("最小值2->" + courseMap.minBy { it.key })//4.map支持增刪查改,方法與java差不多,就不一一說了//遍歷map第一種方式courseMap.forEach {XLogUtils.d("遍歷數(shù)組第一種方式->${it.key} 值:${it.value}")}//遍歷map第二種方式for (map in courseMap) {XLogUtils.i("遍歷數(shù)組第二種方式->${map.key} 值:${map.value}")}//遍歷map第三種方式for ((key, value) in courseMap) {XLogUtils.e("遍歷數(shù)組第三種方式->$key 值:$value")}}
}
//5.創(chuàng)建空集合
//val map = emptyMap<String, String>()
PersonBean
class PersonBean(var name: String, var age: Int) {/*** componentN的用法*/operator fun component1(): String {return this.name}/*** componentN的用法*/operator fun component2(): Int {return this.age}
}