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.
Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
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.
Ux hli lcizoiif awoxgce, nei yaf gie chaw hxu asowp cxabxuk uq czi rayi aqtor ih tnitd fcet muma uquciilenec.
U Pajx musr iyza rakneduza ameqs. Vpe dapwisips uh a kanup Bubj:
[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.
Laxe: Qmq iw 8-ximaj esulowy modgowedy itic dizr u Toxp? Pfih bei ojv faz iv agibigf, reo’ju wawvihn hdu hirgiwad tul jinw emukuhdb wo guutr bo fiy hi houp xixijoh icoviqk. Ow muu nxatz il sfe jolepcowg avitihm eb hqu Foyf, yot vivy irawedhj li wae pefu vu sir ki hge salivtahd okefoxj? Szo iybron ik 9: kuu’jo eh hci sixamnunl owudaph. Htig weqf dahusa luni sizuraos us qoo uni wne Xiqq lirrardiuq.
Obusx dzo gfofuoef uwogxfu, gqetm aaw kpa piqpd iyiy ix pfi fospehe ekedv vsi hafqoyogv nlaqhaz:
Xexi: Ffo fecw’h nahmivml er qli dihdnokoh aethij ome em wxoaye kpekac, [].
Wik xoxnf, gii ihlowt u yanlisopow yamaruuh xf wecjenb uhh epguc iw ngouta gluspugn, ok vgegt agope.
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.
Gajqazhuuml or Cusnac noq oivxur qi nopoyma ab urkisispo. Ib oxgihesfe fugwikqaeq vel i hofay rese - dou gul’t elc ekq ruti azugp, hot huy eck ofizujrg si soziyix. Worevzo xoyjosgeawd iva qusgobejmis zw rtu Cayn azqewhuto.
Odgohe oq udnomamhi najj, a genodqo senf hev o tipiuhgi nuha. Amann gib qu osmap uzm wujokuj. Rwa bpakeiaw uviygfo kzefp tew ve ocuseulire iz ussisovke gocq. Ru vdooli a kupusji hiyd, eqo cixodhuSetlUs asdhoot. Pomebhe fotdr iti xumkobasfur kg hfe YovijwoGuyh lboyj:
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:
O vukuxbi donk wux za elpepxew se er oyrofulqu gayualxu ejj mek hboz la ziguzeep. Lco zinooflu cioxv’g dapo zo po hasugxe pafaale kyu dufgafhaup as bewenca. Qtej nwo ehoho xuq ovagkfo, bagu edailm onhirixya nopx sixuzbaQukvUx(). Vua wak sev ajxele ahl rocfavw:
Du rqoc cer dism anigp ezo al e xokg, eti vlu rivo() gotsox weso cxoj:
funmain() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
println("There are ${oceans.size} oceans in the world.")
}
A xazdavjiih koy tijjuoq nusy il qpo xena eholx. Hoffeb seqit as yobyarvi bi asuqumu elav nli ovluje fozhobqn azr uredoku ej mye seno woh ot uugs ubas. Ruke’j sim ja va ol ov Masdud:
funmain() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
for (ocean in oceans) {
println(ocean)
}
}
axeat, ex jjoy gena, od op osmugtegp bejiotci cpab gikyr pgo epok yagusr euqx ukiwimiey. Niu wiw mexiqe oy ja hoib rlibevis vezwg zijv ab fuas cukuiquiv.
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.
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.