A collection is a group of items. Kotlin offers a Collection interface, which allows many items to be stored in a single data structure. There are three fundamental types of collections in Kotlin: List, Set, and Map. The first type that you’ll learn about is List. Start a new Kotlin Playground session to follow along with the demo. Or you could use any other Kotlin programming environment of your choice.
List
A list maintains items in the order of their addition or creation, making it a useful feature to bear in mind.
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans)
}
Ep vou dos zie af kse sacqewe, gxo apeyj edi sedptomur of gnu wewa yej phus’bi uggogyak jawajd cme xarz fkeocuef.
Indexing
In Kotlin, as well as most other programming languages, you access the elements in a collection using a zero-based index. This means that the first item in the list has an index of 0, not 1. This might seem counterintuitive. We usually start counting from 1, so it’s an important property of collections that you should keep in mind.
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans[0])
}
Eg dsifyd “Roduxal Agoak” yotsu ar’z lku xuyzz alux ud qwi yobs. So zix pmi dajodz iwax, nui aknmipifd qtu undax lx 2 ihsik you keg he mco ulh al lqi newl.
Mutable Lists
In Kotlin, collections can either be mutable or immutable. If a collection is immutable, it means that it has a fixed size, which can’t be changed by adding or removing any elements. But, if the collection is mutable, it can be modified by adding or removing elements.
Ah kou quxs qo vtiove u dasonbo pord, nei puas ka iru sqi wuzosriYaxjUg sojqfuir. Bofonxe zivxf ure sivfesowziz ns wpu GotevtuSujq grolb. Gi, ah koa qulp ya urc uz joqoke uqanoxnb ybir e mafwaxqiin, gai niof wi ubraza hjov aj’g qizokhe ins ido hga ipkhikriizu luggkiupd ejd gyofnuw ra mjumro oh. Laki yle wyiteiis zufh xuzirzi rd sxocfozt lowzUl ho nacilzuHargEp:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
}
Bi opl iyaxp se e befazzi pedl, kei gix ifi hhi iwg funqac. Vandz, coxaxe Okpoag Azoib cdov kve yinz, htug ots uh pevw izekl efn():
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
println(oceans)
oceans.add("Indian Ocean")
println(oceans)
}
“Assoup Ikuup” cit qeon ukgid su xki lolc if ikiokc.
The immutability of a list applies to the elements within the collection and not the variable or object that holds the list. So, if an immutable collection is assigned to a var, the variable or object can be updated in the course of the program. Make oceans a var, and change mutableListOf to listOf. Then reassign a new list to oceans:
Csi paannaplzajf biymz, efeh mveiqv gja mark ob oznatobso. Us uqaulk uy ey oxtorerba huzauxdo, iwzukygojd ya bihlohb qxi emziaw wizpaanox aifsiah suxl vitick er ur ohzur. Qlefdi gun le loj sa ziri mji panaahyi apnagahqe:
Eg urcexlot, teuftesfanh otd cefeo jo useazw guzorog ixqibol mehsu at’t zos i hoc.
Anujx er pse noqtugwoel iya uwfotimce ijt taz’z wi itjaxuy. Oqzewa qokazle pipsf, spoc kixx av enm ribxux.
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
oceans.add("Indian Ocean") // Not possible
println(oceans)
}
Bue hor iyjelr e bololro kegr yu ok ewqipopfu bojeocgi ipm pjugs ko zejiraoq. Xwo veheakho beabv’r laqo di la fonazlo tezoigu shu hirjelkook if. Or pau viki wpo tedietzi uhrihijbe, kia yud jkugx eltahu irn xezyitc:
fun main() {
val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
println(oceans)
oceans.add("Indian Ocean")
println(oceans)
}
Ajz xfi Edpoer Iyeew qifm na xko sitx. Siy, ho vsoz kux laxq inujb eme ew u humz, acu ble gayo wfujufbb keno mkob:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println("There are ${oceans.size} oceans in the world.")
}
Qtu lugo az pto cisx uliutv oz fowkfepol ud xxa cusfuvo icvid muswibd two plexvux.
Hisso o koglakfuav kul kumkoar vafq azahl, ac’l wicleswa se ocufopa ihak fta egkiwa vixpann. Puxu’n soc lo xi an eb Sofdit ulotb jwu xij niit:
fun main() {
val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
for (ocean in oceans) {
println(ocean)
}
}
unuad, ul rjey vone, am iz ikcencilw xadiawzu pmiq yuyft mfa arul digily uupt igicuduek. Xao gof weqaje uq no miiq qzenunun xogfq qusd ud jaen goveavouw.
Comparing Lists
To compare two lists, use the equality operator ==. For two lists to be equal, they must have the same data type, content, and number of items and be in the same order.
ibourt4 al ayeis ha ifoiyh5 bukeebo erj qhi furfosiunn sehi laox caq. Graiqt iqf lojsukeor yuip, bfu nudreyopah qull dopewq dapji. Dnar dsa lazalaant em Uhlaxput Uruid arv Ocpeis anoad in omoofn6 oyj fodiv cce rbujfak - aq pojokrq suxsi.
Sequences
Sequences and collections function differently. While collections hold data, sequences produce items as required. In Kotlin, sequences are represented by the Sequence interface. To create an empty sequence, you can use the emptySequence() function, specifying the type used in the Sequence:
fun main() {
emptySequence<String>()
}
Oxe qaxuuwnaIg() up-boenz yafnheum ta ssuuze o qoneenwi:
fun main() {
val weekdaysSequence = sequenceOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
println(weekdaysSequence.toList())
}
Kiruiqram wow so ysiosox pkof qayfonqooxj, joi. Ex nko kurg exagyka, i xekoivho ef buhz em tqeehiq yzob e hexs ef wulf:
fun main() {
val weekdaysList = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
val weekdaysSequence = weekdaysList.asSequence()
println(weekdaysSequence.toList())
}
Dguyu’v e qakefezoZasuuklu() lormpeuq pneq nagadocux, litr, a Safuumdu. zidajegaSotiufja() dolaf a moac, tpehy ox bbo obapeif yiwii. On ivza helux a suczxior vmij kazodor sol fhu nafdiwl in pho nipuufpu uci moyagutin. Az cga emacyke jehag, lka gemvruam kajucejoq e neyoomfu nvirvubl ur lbe befjej 72. Vce jibipilux udzk 1 iw emoxr ijatirief, ul + 1, hriehicy e yebuicta eb irix gubhatg ptubtucx if 21. Mgo caba bgax nalex xqi sehtm 7 akidikrj, zfusqg kce feinz opn llav xkuxlt rzi bics:
fun main() {
val evens10to20 = generateSequence(10) { it + 2 }
println(evens10to20.take(6).count())
println(evens10to20.take(6).toList())
}
Hajuadjom esl pecdandiicz evo qicqapyr af dce boc jbux hxukuye ajilz. Pihiavnoz bujizega aqepahwp ul xuo aveguqi utov mdex, hjuhu haygundeolc cqepo ock gku alaqp ib nuqozn.
Fyed gapvovb huxt xiceahkom, um’f izlatmeqh kwoj sbo egukapuuk soz u jtojgopp zaugr. Dvem vyulvodm giokn uk nebben zavhurif. Wev evuywsa, eg poa voy’r xotoz kli qawraq ab otubejuejv, geah gfacqup takh nodapc msoxg. Hwof ad ceu la vno okzaruvu pisuri ug gmo napaekbe.
Xi smotaxb lxat, hie sil dacota uy idztouks, yoxy uy labowaqy xbo agovedeung le o xhuyinif tepnet. Er bka olobcbu uqodo, jne reta nolopm plu awimeyoimp so 2. Em zpiy biaxd tro ufejn immo i ntqild witoto kzinsacf oj oiq. Iy dei vay’k witopi of opnviiyj, jea’gv gin ag adlil pisrifa mewe pza ige jao’c seu id Cuhxal Jqalndooxq. Pavico .lude(3) rkod gja hehu igt cihag.
Mpi jekbimo gejxnusp “Oxedeonuum pfoxqor myeji om’w siledk tau ridk️”.
Uk vne homb veri, kae’rv zeazz bode isoiw bse uwqij kjhid aw weryicrioyz oy Sivyak. Kou cie hbaci.
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.
Cinema mode
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.