![]() ![]() We can add elements to end of an ArrayList using add function call. Saturn Adding element to end of an ArrayList Planets.addAll(listOf("Jupiter", "Saturn")) In the following example, we are adding more planets to our list of planets using addAll method. We can use method addAll to add a list of elements to array list. Pluto Adding elements to an existing ArrayList In the following example, we are iterating through array list of planets and printing each planet. We can use for loop to iterate through an arraylist in Kotlin. Var planets = arrayListOf("Mercury", "Venus", "Earth", "Pluto") In the following example, we are creating an arraylist with list of planets and then printing the arraylist. We created String theory of empty cosmos below. We can create an array list using arrayListOf builtin function. In this tutorial we will go through features of ArrayList in Kotlin. In the following example, we will see how we can declare an array and a List in Kotlin and how we can manipulate the values of the same.ArrayList provides implementation for MutableList interface in Kotlin. As the size is dynamic in nature, hence good memory management. ![]() Use it for better accessibility in the code. Use it for better performance, as array is optimized for different primitive data types such as IntArray, DoubleArray. ![]() MutableList do have 'add' and 'remove' functions in order to increase or decrease the size of the MutableList. In order to create a mutable list, MutableList interface needs to be used.Īrray is of fixed size. List or MutableList interfaces are used to implement a List in KotlinĪrray is mutable, i.e., the values can be changed. ![]() In this article, we will take an example to demonstrate the difference between these two types of collections. By definition, both these collections allocate sequential memory location. List and array are two popular collections supported by Kotlin. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |