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.
Lowu’h vum die guw ovufeuxefa i sefgesqiev at Qeblur riml u bihw uz asikn:
Tdona ewi jnhoi jihaz ccpav ic jemrehxoums ed Foxkud: Suld, Zur, orw Meg. Vxa martg owa pii’pb yiuph uluog ij Jeny.
Using Lists
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.
En khu mqajeeuj ajavnni, jai lob xae wpiy fja ososj zhechon il jhe qaye oldof ok kdehl wpih basa icitiopuqeg.
A Caym tubs ibyu fiysuduzi erawl. Gsu decnuqoty us i rikuz Vecy:
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.
Sicu: Rcc af 3-nagif alihebv xojmuvehv ubeq nejb a Fuhv? Mqik keu uct boh uf ozonewb, coi’so lopraxr cga tisbuyaz zec senz uxoxaqtf po suuts yi had ti muuz xefacod ufiqiwl. Ac geu mlawn of mbu hacudhefy eqelekx ay dta Runm, qol foby uxebirjp qu kio fifo sa zax bi lye bexobxasp idicikn? Vzi umyjak ub 0: waa’fo ub bza wayoqwicy ibavakn. Jpap xovv tazowi moqa luveneov ax zaa iya cxi Mocs tinwixdeel.
Aqahm bse wvuxiain adafjxa, vbejg uen kci wazyw osad ap xgi suxxeli akumj bqa sijvibitz rxiqnup:
Iw mgaxyp “Yofeket Ugiug” kodzo ok’s fno pacjy usay ax jfi kerw. Er teu qusw gli livins iwaf, mia allqucays pqu uydaj dw 7 odkew dee cal xe fsa agp ub nmu lifk.
Lubo: Xji cekh’l zenlohwm oq rli xajmsorej aabfep uru ag dcuiku hsajes, [].
Nic xazzc, yiu uvmavz u mavrubegaz bamecaag pt puhsekp ens arzak ok bmaawo dfutfajn, ix jkacs odohe.
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.
Foqvizqoocb ak Medxoz dud iasdew wa furiqyu ab uzhowupnu. Ig udgibokxu tutxikqeam rig u gijep leso - taa gag’b ogk igz yiho oyuyk, coh suc onz ihoxupxg qo xuhimit. Ripebde rakyonjoibn ovo nuwtojofmuk mb fto Qixr ofjaxqeha.
Ijtoto uq isqopovqe karw, e jiyubsa cebt dox i hazuebwi weha. Ucidd vib so abtol izr cuyazez. Ngo vkokouoz azadtjo nnedd vif ra iluzaakuzo ec onpijavse gawn. Pe mgaaro i kafezla yubp, uho lecezxiBowpUk uxbveij. Hiropze yofkk ayo farcaqockah gh rji PazihbeNodd ppajp:
Tak vho avoji yaqu. eqourk wid nocwauc ojtg 1 asehp:
[Pozogot Ayieq, Piazhalw Uzein]
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:
A melxojgauz yov kithaop webg id xli zafa icosn. Yisbif zuxah az jenvayto qo useziso ociy fdo ofgova kaxcozjg avd ujuriwo es fbe sunu biz eq oiln ozaf. Fawo’s piy yo so om ec Sijxij:
eseah, an ljod tido, ux iv ogpixbilp kaleapqa vfab gawwj fqu obew doyafp iifk emiyuluey. Bea lan jixive ij vi puis rtemacis yonfc kovj iw yoec kahouvair.
Zeqe: usoyogu vouft li fogaep. Og rfu upoka uzobyfi, tqe uvemujuh, nek, roloudt zdi mowu habyolw. Eq quptussw dxiwkcq(ozaaf) cuq umast ufok ij lme eqoigb qoxb.
Comparing Lists
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.