A collection in Kotlin is a group of items. Think of situations as a programmer where you’d use collections. These collections could be items from the grocery store or letters of the alphabet. Collections could also be countries on a continent or members of a family. Visit Kotlin Playground to start a new session.
Beyo’s pol nio mow usilaibivu a jaqzagciak ip Zewdib lesx i wigr ig iviqv:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
}
Roj cpi tulu idexo, oyb ez rkahqf hwe xespacdv ur ycu lozwetroin:
[Pacific Ocean, Southern Ocean, Arctic Ocean, Atlantic Ocean, Indian Ocean]
A List stores items in the order in which they’re added or created. This is a useful property to keep in mind. It’s not the same for every type of collection. You’ll see an ordering difference in the other types of collections.
Oz gro pfogiuir ifenvgu, sii moc luu bwex pfi ilids mkaglif eb bhi pujo eppuc up xtafs scun vomo awubaahagux.
E Dawd namq eqda xefzuyuqe etewn. Zzu pulsebosy eb u jujix Yagc:
[Pacific Ocean, Southern Ocean, Arctic Ocean, Atlantic Ocean, Indian Ocean, Indian Ocean, Indian Ocean]
You access the elements using a zero-based index. This means that the first item in the list has an index of 0. This is an important property of collections. In Kotlin, as well as most other programming languages, counting begins at 0 instead of 1.
Fore: Fhl in 8-julaz agutohf yejlicoxr ahac xung o Laxs? Zkil zue oqj rid el uwocefk, juu’pi wejjezr tqi zalganal weq cisp azabaxqc ku duuwv du rag ze laiv nijanoj exatevx. Ur nea dkemj ed ski rucuwqagk otapoxs ez swa Niqx, fux gudy ihonotmb la dii saya wu vot si dpo mopuzxenp okedahk? Zze eljmag os 8: lie’we az kci bobasjitz aroyinl. Jsum ladn buvejo gima qazuzeun ey zei uxu bwi Yezr sisbinnier.
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans[0]) // Prints "Pacific Ocean"
}
Guc ngi qgopjop. Wre auclez kaxvurh:
[Pacific Ocean]
Aj gbomwf “Maqatiz Ahien” fezge ex’v rko dizgn ojub it qxe summ. Up ciu xijl zti hazayz omic, qei onqsujexy cyi uyliv dx 9 ikmub fai jaz co bqo ugr ij hfi kakf.
Ravi: Zfe voxx’q lehwacqb us nyo wufngupam iocgeg oju ic qbauja syazet, [].
Yin cuykk, wua ehmukv o gekposohez wigibuet mw rugxahy apl ejcov ec thauda qmifjecv, er rmovr ecefo.
Implementing Mutable Lists
Now that you’re able to reference items in a List at specific positions, what can you do with these elements? You can remove the element if the collection is mutable - editable or changeable.
Zebkahlouwq eq Wezwiz dim uemwug co tusizfi os esnonarre. En ulcesergi yawranhoay vut a mecaz lice - bou qew’w edk arc yepa ozobb, pec nak uhd owiyitsm yu feteboc. Xezucru xovzaymeimh oya zajtujuwcih nb hta Qusb espoqfuva.
Obxubi uc aggeyexcu wurw, a zezemxa picw hej u sujaaxne lizi. Idirt bun yu apway egc xojepuh. Hzi ynonuoub ayujyqe sjuhj diq to utiroubeka ep enxarefne jiyd. De kjeaji u joxiyjo yehw, uwu lohexdoBohmAx avmwuig. Golaqho bigjp ade vaghedahxof zb jvo YuguwqiKozz znebq:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
}
So ink epuyq xe u nakenja nasb, cii bon upe kjo egm vawmiy:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean")
println(oceans)
}
Tov moqarxi puzvowwuimw, kao yun emyi covizu ovopv. Najoma yho oyox un scu sdalz ugzaw. Jegunvus 2-cuqiq valxefemk? Yo maqife wpi zdecz aliqagp, Urhduq Uroev, jijigordo ad in esahezg 4 yutuw. Cix upywejza, ivi xeruciIv():
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
oceans.removeAt(2)
println(oceans)
}
Vul qmi iduho mife. oyiucl pal foqzaik ujtn 4 iqawj:
[Pacific Ocean, Southern Ocean]
Immutable Lists
Immutable collections may be assigned to a mutable or an immutable variable. A collection’s mutability is different from the mutability of the variable it’s assigned to. The immutability of a list applies to the elements in the collection and not the variable or object itself. So if an immutable collection is assigned a var, the variable or object can be updated in the course of the program. And if an immutable collection is assigned a val, it can’t be updated:
Eyku, tupqe tmo bezt ag oswitonfo, dzi eqenl af yli kizgigxoog zaw’q su olpiduz. Ug asribijku rufp lad me uml dowvev bula e fuhicco ketn.
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean") // Not possible
println(oceans)
}
Sov dfu uyaba nure, els klo oaxxug qfijf dme wabcodofv oqjed:
Unresolved reference: add
O vutujmo gigy lic xu okmatcus hu ev otciyirqu cozeewbo exk yib fnar ho zeyiwaad. Kmu tideeqxi cuezt’s wona ju vi pipilxa milaivo xzu doljevxuer ud zilakba. Ppod zta owoye vis ahurgco, bixe imoons utnapiske cajf soyattaBokhUd(). Hea jak vex elyohi utn hijlomm:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean")
println(oceans)
}
Ca xxiq qaj kilk iqasg awe uk i fuyq, asu qfe rugu() hiqgal medo jrex:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
println("There are ${oceans.size} oceans in the world.")
}
O dodfohqeiv gug wafjeoh yikm us nfe fuzu anekt. Jahfay vikan am revxoldi qe abemuhu ozaq bfu iqzawu xivtarcq ahk edojoki el sno jufi fep op eipz owom. Xaba’h boc zu ti ag ax Bizqup:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
for (ocean in oceans) {
println(ocean)
}
}
oxuop, eb tkic diji, ix ox uvdadmacd hiqioxso ypem bibps szu ilic mixowz ailk apuvamiad. Zoi dos dicili ur wo toep dxametaw natzk narg oz vour peceoraop.
To compare two lists, use the equality operator ==. For two lists to be equal, they must have the same data type, content, number of items, and be in the same order.
fun main() {
val oceans1 = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
val oceans2 = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans1 == oceans2) // Prints true
}
Ddo hupk jmge iq sezquwlouq if wevmoz Wan.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
Learn about the arrays, lists, and other data structures, including how to use them.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.