Thuta Learning
ရှာဖွေရန်
IntermediateProgrammingbeginner

Collections (Lists)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

List ဆိုတာ value တွေကို အစဉ်လိုက်သိမ်းတဲ့ collection ပါ။ Kotlin မှာ listOf() က read-only list ကိုဖန်တီးပြီး mutableListOf() က item ထပ်ထည့်၊ ဖျက်၊ ပြောင်းနိုင်တဲ့ list ကိုဖန်တီးပါတယ်။ App ထဲက product list, menu list, user list စတာတွေမှာ အလွန်အသုံးများပါတယ်။

kotlin
fun main() {
    val numbers = listOf(1, 2, 3)
    println(numbers[0])

    val fruits = mutableListOf("apple", "banana")
    fruits.add("cherry")
    println(fruits)
}
You should see
1 [apple, banana, cherry]

အနှစ်ချုပ်

List ကိုနားလည်ထားရင် data အစုတွေကို loop, filter, map နဲ့ လွယ်လွယ်ကူကူ process လုပ်နိုင်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • List ထဲမှာ item 3 ခုရှိရင် index က 0, 1, 2 သာရှိပါတယ်။ numbers[3] က error ဖြစ်နိုင်ပါတယ်။

Practical example — filter products

Practical example — filter products

kotlin
fun main() {
    val prices = listOf(12000, 5000, 25000, 8000)
    val affordable = prices.filter { it <= 10000 }

    println(affordable)
}

You'll know it worked when: [5000, 8000]

Collections (Lists) | Thuta Learning