Theory
दो Lists, दो Rules
FestConnect Mobile के पास अलग needs वाले दो collections हैं। EVENTS की list, एक बार load होने पर, एक session के दौरान NAHIN बदलनी चाहिए: यह fixed है। REGISTRATIONS की list लोगों के sign up करने पर grow होती है: इसे बदलना ज़रूरी है।
Kotlin इसे एक deliberate distinction से handle करता है जो val बनाम var को echo करता है: कुछ collections READ-ONLY होते हैं, दूसरे MUTABLE, और आप CHOOSE करते हैं इस based पर कि data बदलना चाहिए या नहीं। यह lesson Kotlin के arrays और, ज़्यादा importantly, इसके lists cover करता है, जहाँ read-only versus mutable choice एक छोटा पर meaningful design decision है जो language आपसे लेने को कहती है।
Theory
Arrays: Fixed Size
एक array की एक FIXED size होती है, creation पर set होती है:
val nums = arrayOf(1, 2, 3) (या एक primitive int array के लिए intArrayOf(1, 2, 3))
Index से access (0-based): nums[0] 1 है; आप एक element CHANGE कर सकते हैं (nums[0] = 5) पर add या remove NAHIN कर सकते (length fixed है)। .size count देता है।
Arrays तब useful हैं जब size known और fixed हो, पर ज़्यादातर app data के लिए (grow होने वाली lists), आप LISTS की तरफ़ पहुँचेंगे, exactly जैसे आपने flexible collections के लिए Java में arrays पर ArrayList prefer किया।
Theory
Lists: Read-Only बनाम Mutable
Kotlin lists दो TYPES में आती हैं, और choosing key idea है:
- listOf(...) एक READ-ONLY (immutable) list बनाती है: आप इसे READ कर सकते हैं पर elements add, remove, या change NAHIN कर सकते
- mutableListOf(...) एक MUTABLE list बनाती है:
.add(),.remove(),.removeAt(), औरlist[i] = xसब काम करते हैं
यह val बनाम var को mirror करता है: default से READ-ONLY prefer कीजिए (safer, clearer intent), mutable सिर्फ़ तब इस्तेमाल कीजिए जब data genuinely बदलता है।
val events = listOf("Garba", "Coding") grow नहीं हो सकती; val regs = mutableListOf<String>() हो सकती है। दोनों 0-based list[i] access और .size इस्तेमाल करती हैं। (Maps और Sets same read-only/mutable pattern follow करते हैं: mapOf/mutableMapOf।)
Practical
Read-Only Events, Mutable Registrations
fun main() {
// READ-ONLY list: cannot be modified
val events = listOf("Garba Night", "Coding Contest")
println(events[0]) // Garba Night (0-based access)
println(events.size) // 2
// events.add("Webinar") // COMPILE ERROR: listOf is read-only
// MUTABLE list: can add/remove/change
val regs = mutableListOf("Riya")
regs.add("Aman") // ok
regs.add("Zoya")
regs.removeAt(0) // removes "Riya"
println(regs) // [Aman, Zoya]
println(regs.size) // 2
}
Quiz
आप listOf("Garba", "Coding") से एक list create करते हैं और इस पर .add() call करने की कोशिश करते हैं। क्या होता है?
- यह successfully item add करता है
- एक COMPILE ERROR: listOf एक READ-ONLY list बनाता है; items add या remove करने के लिए mutableListOf इस्तेमाल कीजिए
- सिर्फ़ तब एक runtime crash जब app चलता है
- यह silently add ignore करता है
Show the answer
एक COMPILE ERROR: listOf एक READ-ONLY list बनाता है; items add या remove करने के लिए mutableListOf इस्तेमाल कीजिए
listOf एक READ-ONLY (immutable) list बनाता है, जिसमें कोई add/remove operations नहीं हैं, तो .add() call करना एक COMPILE ERROR है, program चलने से पहले पकड़ा जाता है। एक list बनाने के लिए जिसे आप modify कर सकें, mutableListOf इस्तेमाल कीजिए, जो add, remove, removeAt, और index assignment provide करती है। यह read-only-बनाम-mutable distinction val बनाम var को mirror करता है और compiler को आपका intent enforce करने देता है: अगर एक list नहीं बदलनी चाहिए, listOf इसे guarantee करता है। Option A ग़लत है (read-only lists add reject करती हैं)। Option C error को runtime पर misplace करता है; Kotlin इसे COMPILE time पर पकड़ता है (read-only type में add बस नहीं है)। Option D silent behaviour invent करता है। Fixed data के लिए listOf चुनिए, changing data के लिए mutableListOf।
Think first
Read-Only Collections को क्यों Prefer करें?
आप बस हर जगह mutableListOf इस्तेमाल कर सकते थे और कौन सा पिक करें इसकी चिंता कभी न करते। Kotlin default से read-only listOf को क्यों encourage करता है? फिर tap कीजिए।
Show the answer
उन्हीं reasons से जिनसे var पर val prefer किया जाता है: SAFETY और CLARITY। एक read-only list GUARANTEE करती है कि data नहीं बदलेगा, तो आप (और compiler, और आपका code पढ़ने वाले दूसरे developers) इस पर rely कर सकते हैं: कोई accidental modification नहीं, कोई surprise नहीं जब आपके किसी दूसरे function को pass की गई list बदली हुई वापस आती है। यह INTENT explicit बनाती है: listOf कहती है 'यह fixed है', mutableListOf कहती है 'यह बदलेगी'। यह bugs की एक पूरी class prevent करती है जहाँ shared mutable data unexpectedly modify होता है, और code को reason करना आसान बनाती है। mutableListOf सिर्फ़ वहाँ इस्तेमाल कीजिए जहाँ data GENUINELY बदलनी चाहिए (growing registrations list जैसी); बाकी हर जगह read-only default रखिए। Discipline, default से immutable, necessity से mutable, safer modern code का एक hallmark है, और Kotlin इसे collections में build करता है।
Watch out
Array और List Traps
एक listOf में Add करने की कोशिश: यह read-only है; mutableListOf इस्तेमाल कीजिए।
एक Array Resize करने की कोशिश: arrays fixed-size हैं; grow करने के लिए एक list इस्तेमाल कीजिए।
Default से Mutable: data बदलनी ज़रूरी न होने तक listOf (read-only) prefer कीजिए।
Index Out of Bounds: i >= size वाला list[i] throws करता है; last index size - 1 है।
val list फिर भी Mutation Allow करती है अगर list Mutable है: val मतलब VARIABLE reassign नहीं हो सकता; एक val को assigned mutableListOf अभी भी modify हो सकती है (इसकी CONTENTS बदलती हैं, reference नहीं)।
Theory
Collections Ready; अब इन्हें Iterate कीजिए
FestConnect Mobile अपने events (read-only) और registrations (mutable) hold कर सकता है। हर item के साथ कुछ DO करने के लिए, इन्हें print करना, count करना, filter करना, आपको LOOPS चाहिए। Final Kotlin-basics lesson for और while loops cover करता है, और control keywords break, continue और return। फिर Unit 2 इस data और logic को proper CLASSES में बदलता है, Kotlin में FestConnect Mobile की object-oriented structure build करते हुए।
Summary
Key takeaways
- Arrays की एक FIXED size होती है: arrayOf(1,2,3); 0-based index से access (arr[0]); elements changeable, length नहीं।
- Lists दो types में आती हैं: listOf(...) READ-ONLY है (immutable); mutableListOf(...) MUTABLE है (add/remove/set)।
- यह read-only बनाम mutable split val बनाम var को mirror करता है; default से read-only prefer कीजिए।
- Read-only lists reading allow करती हैं (list[i], .size) पर modification नहीं (listOf पर add एक compile error है)।
- Mutable lists .add(), .remove(), .removeAt(), और list[i] = x support करती हैं।
- एक mutableListOf hold करने वाला val फिर भी अपनी CONTENTS बदल सकता है (reference fixed है, list नहीं)।
- Memory hook: arrays fixed-size हैं; listOf read-only है, mutableListOf बदल सकती है (val/var की तरह)।