Let's take a brief look at maps and sets. The key feature of a Set is that it doesn't allow duplicates. The List has already been described, and it is an ordered list of elements much like an array.Ī Map is a set of key value pairs, sometimes called a dictionary, hash map or associative array in other languages.Ī Set is an unordered list which allows you to store values in it as if it was a simple container. The List a good archetypal collection class, but what else do we have?Īs in Java there are three types of collection: If you want a List to be immutable make sure it isn't cast to MutableList. This results in being printed and immutable List a has been changed. They are all worth knowing about and very straightforward in their use.Īs already mentioned List isn't completely immutable.įor example, if you explicitly cast to MutableList then you can change elements: var a=listOf("x","y","z") Notable are the sorting methods, the binary search method, and a set of methods that let you find and count elements. You can check the size of a collection using the size property and the lastindex property.Īs well as these basic methods and properties there are a number of extensions that take us well beyond what is available in Java. If you do, the result is a run-time exception. It is important that you don't try to make use of an element that doesn't exist. Unlike other languages there is no support for "slicing notation". The sublist(start,end) method gives you the ability to extract a sub-list. To shrink the collection you can use remove and clear which remove one instance of an element and all of the elements respectively. You can also use the addAll method to add a list of elements in one go. The add method adds to the end of the collection by default, but you can insert an element at a specific position.įor example: var a:MutableList = mutableListOf("x","y","z") To add an element beyond what are already there you have to use the add method: var a:MutableList = mutableListOf("x","y","z") If you are familiar with more permissive languages such as JavaScript you might think that this allows you to reference elements that don't exist. The big difference is that you can make the MutableList bigger. You can create a MutableList in the same way as a List: var a:MutableList = mutableListOf("x","y","z")
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |