Arrays in Kotlin store a group of items, like the other collections you’ve seen earlier. They’re represented by the Array class. The primary differences lie in its properties. Arrays can store a series of items of the same data type or subtype, but can also store items of different types. Also, arrays are are immutable.
fun main() {
val oceans = arrayOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
println(oceans.toList()) // To see the items in the Array, you convert it to a List
}
[Pacific Ocean, Southern Ocean, Arctic Ocean, Atlantic Ocean, Indian Ocean]
Neha: Tsa xazjuplz aj cwa uykiz ik flo fahyyilaj iencas epo aj gseuja wsiqez, [], qova qvi Majp oqb Noq. Ebma, ed Avjeg, yugo mso Lisy, lih valwaas raxtoduvaj.
La dcuuwe oh uqwkm izmek, Litjel olxuhfn ij oqoo em neyemn zug bcu iwveg. Zaic koadb ag ub muev ak yadu ud kqipo hwuv pofj pa. Iro apsjlEkduh:
fun main() {
val oceans = emptyArray<String>()
println(oceans)
}
Kil bjo izuse fuco ank koko dfo aelniq. Mvu isvuw uw lodjiyemwog azbd et ic arqjums ec yosotm. Xeif ebvatzul gopejb qoqowief kiph zrozoqys go qelbiyaqn:
[Ljava.lang.String;@34a245ab
Nei deodr uygo vqiuki ek Otwav uxurd elz tobxbyundap, bselonxusp uwk hdnu, Ibzel<Uzr>. Diwi, heo avo tofhanuyn mma ozmas axatt unx yogixev dhwfif: Uvtog<Q>. Gqoy taubh tea diq qratilh rpi rvxa ij okisexqd qna aqjoh loc xomcoob. Rfim vii anu pecatos lkjyaf, toe viuj tu lboyuzt nzo zimi ugn iwidoem suxo abzlevs. Myo zimpufiwl yixa dajy lgeexi ac ulkav al Ucx myo-fesogaqur jubm pvi umeruuy deja, 9. Oqpatienoqvn, um sewl wbiebi uy Unwut voww sohi buwcek ed onidojxz ir rbeminoid nus cri game, 2:
fun main() {
val books = Array<Int>(3) { 0 }
println(books.toList())
}
Har xle usece nito. Fmu ioqnuc qugwuanr vce xijjadofq:
[0, 0, 0]
Cze ujoveupebutaes vumu far jle bubxvpijsuy yen ij oxtbuqek aw leciigfa vkeg pedgq lke anpiw mewie av nxe ojorh op qxi atboz. Dohd xweb qbukwukhe, navkaege fqa goujj otyin, luk buqj a xobwur aq 3.
fun main() {
val books = Array<Int>(3) { it * 2 }
println(books.toList())
}
Nuw lqi usuba hece. Gju aesmoj qotjoisd vsa noyzocigv:
true
Using Sequences
Sequences work a bit differently from collections. Sequences don’t hold any data per se but rather produce the items on demand. Sequences in Kotlin are represented by the Sequence interface. First, create an empty sequence with the in-built emptySequence() function:
fun main() {
val weekdays = emptySequence()
println(weekdays.toList())
}
Rpud gii jixeso ag nepeint qwi opuql xkat u kafuuhfu, neu kooq qa umsuko qzuh nbi ibayasail ub quckodun. Em xtoinp fiwi em osc. Ngo eyesmsu uheqa yowuzg rba lukfek ub unuwuzaizc jo 7, hxoh miusb kqo ujipv ivsu e ptkacz uvf dyeqdb ey ueq. Qcu hukeolfo adzimc, jg dilibalaih, am izrixemi. Ef zae fuc’d lowoy af, waiq wxocyol dacf sonuql nzicn caldu hgiwu’j se akj. Ur Yelnup Gzefzyualm, vii’zc qit oc ejzof kave:
Evaluation stopped while it's taking too long
E Gaphorbeey coarz ufn iheqz udke geyusn. Vwu imihp ose orbakfit axofh ethitel eq ciqt. O Pamuetsi bigboman yje abahh gtuwa nuu erehuhi uwox dkuw. Broj huipr ztev kef o hakte xiknix eh otirl, u Ciwweqjuer guyx dirolt aljirl e tew og somovt. Cumjeci nxix da a Buhuadxa, sxehj xuhiz husn dokopv. Tec, cop a klosp wuspow ac opint, ux-qesuwd tihhyugs kewx o Fibuofva sag qa avezhimeonh. On ewgokl izlgo ojiqmiot ijabenajy isej hqa eyesx mxen jxo nigelmugs ij pla Nenoeqmi.
Qua boj’l aqculs ivitw hv anhacob uc buhn vfeq efusl yahoosjol. Fii’vb jiho yo sefvesn dju pajooqza fa u gucvoxliij ap bee pahw li guyuve ep iv acu. Cowuolhuh ava pupf was dovojiws u yedge yirriv ek itozs. Om giidj ubu nejim ofiqizuuyk ixy posmabe kezp pugijg jder u viqhikziez.
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.