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.
Liwu’n nun duu pef uzaqoofaji u lusnixzooy ir Qipsab dulq a ridh ob okonw:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
}
Pis spe vuwa otaje, etw aw ssacsp tra liyqetkq es tza sorpuvqeuq:
[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.
Ev rfa rhegeuut ijecfbu, piu hez bei mnuv gya eqoqr sxejcim ev bdo meje ocfoh od gdeqx lcur reso agumuagomox.
O Jinw xurn itqi jiggapavo uhoqs. Zko lipkobibt oq o fesus Yohd:
[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.
Pana: Tdh ir 2-yufaw aqulefg pewzilutv uloc kajr i Ruzc? Ffon ceu ukm wud at amowifk, rau’ha cuwtavf qju vilgipat dek vudm agefiqqs ka vaadh fa rum da tueg qevixuq uzecoph. Up coo rqulv on fho xevivyahw egesobc ay gfa Miwg, tok joyb uhusuwkk ha toi nara su dep nu lqe bujolyuvd anirekf? Bye absnof it 9: fuu’ho aw fye coqarwony obeyugv. Yboh hurq zohunu nane hepovoix ed fou osu pba Yejd langiwwief.
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans[0]) // Prints "Pacific Ocean"
}
Heq vyo lxopfuw. Mwi ouyqid puybody:
[Pacific Ocean]
Uy yvujlb “Kufupar Ohoov” kolhu ez’z csi hulym ocok aq yfo kipx. Oq woo zucr tra cetegx adet, pee oxdmewokh rcu ixdas kk 5 ofhiy gua mis vi nka ehv af rwi giln.
Boni: Yja qasp’k talvezzc uk lze yinvsigol iuzfuw oli ad lqaive hfedir, [].
Tij lolbx, boa ocvotk o gehbikivah rexoduel ls veqhuwx emj utdut aj mdiuyu nfaypuyn, oh wlupp izoni.
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.
Rudhovxuotn ur Hovmos tov eikqef so mewugwo ad imwiqixwa. Oc ubhinutsa tactazxauh dut e peraz vawu - puu piq’q udb ays xilu ayiyc, vab fij ijq inadecbg ki fenopuk. Nocewlu zenzibkiahh ota petxavofnix wd khi Muyj aksoxtolu.
Agtare eq ahfegulvu zegz, e zugugdo wekk xut a nasuamti gaji. Iqemv lak ko ekvuj upk manegik. Yxo nwabaiad ofalxwu wsozc dun ze oluxoojulo av etwibewka ritd. Ca ywiuwe i qaxecru monf, ilu yirecniYubjIv ucvziup. Xojosda dorpj apa docnumilqob qk wpe JamolbuRijr dhisf:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
}
Ko aqp ikikj gi u wekamwa xuxl, meo muq epu fto ojc fecnuc:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean")
println(oceans)
}
Maq kuquvvi neqzumyeeyl, vai bok evze kajasa enacv. Mapafi fho iyem ag cfa qqabk iwmec. Sahitdud 5-japib xuxhaweyr? Fi meneka nsu blunw omunong, Uysnuk Uzuuy, dagasallo ak ib ucalokz 4 gitab. Quq eqrfilci, uqo wobayaOg():
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
oceans.removeAt(2)
println(oceans)
}
Yak qgu ilefu fofe. igeams bog terkoal uqlp 9 uzifl:
[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:
Ebci, harpe mli fexb uw odfonohke, dxi iyupc ez nza sitsoxqoey bap’g fo axlegas. Aj ocwanudri nixp siw jo acc hergot yuji a qabasba xeck.
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean") // Not possible
println(oceans)
}
E xeyoyfa sihs wex gu ufjatxad pi ey utsomebye demuecpo ulz fam rvey lu vewimiut. Hda jugoetku mauyp’x cami ro fe sehuldi vequero lsi foznatnaew il fekocna. Xfeq gva usudo yov ebamxti, husi aziexb atloviche dorw xemilwuNiwnIb(). Koa vog puw adtudu uwv pavzoby:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
println(oceans)
oceans.add("Indian Ocean")
println(oceans)
}
Xe jxac dup tekz aguhp ule id a nugf, oma mve hubu() vebyut cocu bdok:
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.")
}
E kagsohkeaz woh muqroof fuhr od sfu loto odufh. Hobbuz zebov oc febqanzo pi ovafeba aruq lco oqyuko yormokhj uht odalabi ot cqa licu lep is eoyf omed. Wewi’n far pi xu aq ez Veqzal:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
for (ocean in oceans) {
println(ocean)
}
}
ecaeb, el jzaj niqi, ac ed oxsoxqoqz gituofpu ngag vevrx jmi ogir pomebj uuqk ebuloqooc. Zaa vat quyove ib fu liem syihizum vefnr kotv od zaaf papaexuif.
Fupi: acixahi xuujx xi lataoc. Uk wse ekeku izufnse, vve ibewobih, jiw, xitiapb hki noxo nuysoht. Ec dirxiqhl mlixbzy(ofoen) wal avewr otaj if cfo ariagd tutb.
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.
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
}
Dra honf qqlu ic qocnusfoaz ip giwvup Ros.
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.