doctorose.blogg.se

Kotlin mutable list
Kotlin mutable list












kotlin mutable list

The read-only collection types are covariant. numbers = mutableListOf("six", "seven") // compilation error Val numbers = mutableListOf("one", "two", "three", "four") Although, if you try to reassign a val collection, you'll get a compilation error. Note that altering a mutable collection doesn't require it to be a var: write operations modify the same mutable collection object, so the reference doesn't change. A pair of interfaces represent each collection type:Ī read-only interface that provides operations for accessing collection elements.Ī mutable interface that extends the corresponding read-only interface with write operations: adding, removing, and updating its elements. The Kotlin Standard Library provides implementations for basic collection types: sets, lists, and maps. The collection interfaces and related functions are located in the llections package. So, the Kotlin Standard Library offers generic interfaces, classes, and functions for creating, populating, and managing collections of any type. In other words, you add a String to a list of Strings the same way as you would do with Ints or a user-defined class. Kotlin lets you manipulate collections independently of the exact type of objects stored in them. Maps are useful for storing logical connections between objects, for example, an employee's ID and their position.

kotlin mutable list

Keys are unique, and each of them maps to exactly one value. Map (or dictionary) is a set of key-value pairs. For example, the numbers on lottery tickets form a set: they are unique, and their order is not important. Generally, the order of set elements has no significance. It reflects the mathematical abstraction of set: a group of objects without repetitions. An example of a list is a telephone number: it's a group of digits, their order is important, and they can repeat. Elements can occur more than once in a list. List is an ordered collection with access to elements by indices – integer numbers that reflect their position. The following collection types are relevant for Kotlin:

kotlin mutable list

For example, all the students in a department form a collection that can be used to calculate their average age. Objects in a collection are called elements or items. The Kotlin Standard Library provides a comprehensive set of tools for managing collections – groups of a variable number of items (possibly zero) that are significant to the problem being solved and are commonly operated on.Ĭollections are a common concept for most programming languages, so if you're familiar with, for example, Java or Python collections, you can skip this introduction and proceed to the detailed sections.Ī collection usually contains a number of objects (this number may also be zero) of the same type.














Kotlin mutable list