Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Lists


Lists are ordered collections. With a list, you can insert an element at a very specific location, as well as retrieve elements by the position in the collection. Kotlin provides a couple of pre-built methods for constructing immutable and mutable lists. Remember, immutability is achieved via interface. Here is how you would create lists in idiomatic Kotlin:

    val intList: List<Int> = listOf 
    println("Int  list[${intList.javaClass.canonicalName}]:${intList.joinToString(", ")}") 
 
    val emptyList: List<String> = emptyList<String>() 
    println("Empty  list[${emptyList.javaClass.canonicalName}]:${emptyList.joinToStrin g(",")}") 
 
    val nonNulls: List<String> = listOfNotNull<String>(null, "a", "b",  "c") 
    println("Non-Null string lists[${nonNulls.javaClass.canonicalName}]:${nonNulls.joinToString (",")}") 
 
    val doubleList: ArrayList<Double> = arrayListOf(84.88, 100.25,  999.99) 
...