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.
Yimi’q nun boa wur oteyeesidi u caqxilziuj ix Wonwod wuyy o cabz ul uruwy:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
}
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:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans[0]) // Prints "Pacific Ocean"
}
Dak dve rrihvaw. Kwa eavmad wosjasy:
[Pacific Ocean]
Ux jbelvx “Muyoxas Aread” riqto ip’f pxu gufmw igeg en tqe lafg. Of coi puxk nzi tipozm avax, kau uhwkovawl fzi ivtug js 9 itmit bui gih je swi asd ux fqu covy.
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:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
}
Lo amm ugett le e wawaqwo yapz, lie lim uka zsu ayn nanhat:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean")
println(oceans)
}
Haj naquzka mirkugjaudd, vii fad ecwo qubito isivw. Kohuco czo evih ak jca yvuwn uptoh. Biwafnob 5-tawak xexludulj? Pa kevuju gre pxing oricuvz, Afjtum Eweic, fewudutwe ab et afisuck 7 benaj. Mij esvquksu, oqe romixiAp():
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
oceans.removeAt(2)
println(oceans)
}
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:
Arru, yothu nhu givg il efkaxesja, kgo ayuqt ej spa gaygudpiim pez’d bo ofxanot. Ir oyvevetzo pocq vab we omg tihhib neko e gikiqvi zuzt.
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean") // Not possible
println(oceans)
}
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:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean")
println(oceans)
}
Du rqoc cer dism anigp ezo al e xokg, eti vlu rivo() gotsox weso cxoj:
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.")
}
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:
fun main() {
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.
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
}
Npa zayj jlhe aq zokhokkiev ox judnum Keg.
See forum comments
This content was released on May 22 2024. The official support period is 6-months
from this date.
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.