Google Android Study Jam 學習筆記(2)
學習範圍
單元 2 課程 3
Kotlin 語法
list
list
- 宣告
1
val numbers: List<Int> = listOf(1, 2, 3, 4, 5, 6)
- 其他
1 2 3 4 5 6 7 8 9
println("List: $numbers") // = println("List: " + numbers) println("Size: ${numbers.size}") println("First element: ${numbers[0]}") println("First: ${numbers.first()}") println("Last: ${numbers.last()}") println("Contains 4? ${numbers.contains(4)}") // true println("Contains 7? ${numbers.contains(7)}") // false println("Reversed list: ${colors.reversed()}") println("Sorted list: ${colors.sorted()}")
mutablelist
- 宣告
1
val entrees = mutableListOf<String>() // val entrees: MutableList<String> = mutableListOf()
- 其他
1 2 3 4 5 6 7
println("Add noodles: ${entrees.add("noodles")}") val moreItems = listOf("ravioli", "lasagna", "fettuccine") println("Add list: ${entrees.addAll(moreItems)}") // "Add list: true" println("Remove spaghetti: ${entrees.remove("spaghetti")}") println("Remove first element: ${entrees.removeAt(0)}") entrees.clear() println("Empty? ${entrees.isEmpty()}")
while
1 2 3 4 5 6 7 8
val guestsPerFamily = listOf(2, 4, 1, 3) var totalGuests = 0 var index = 0 while (index < guestsPerFamily.size) { totalGuests += guestsPerFamily[index] index++ } println("Total Guest Count: $totalGuests")
for
1 2 3
for (name in names) println(name) for (item in 5 downTo 1) print(item) for (item in 3..6 step 2) print(item)
vararg
將類型相同、數量可變的引數傳遞給函式或建構函式1 2
// 只能將一個參數標示為 vararg,且通常是清單中的最後一個參數。 class Vegetables(vararg val toppings: String) : Item("Vegetables", 5) {
Android Studio
RecycleView
MaterialCardView