What Is The dAll() Method In Kotlin?
Maybe your like
The kotlin.collections package is part of Kotlin’s standard library. It contains all collection types, including Map, List, Set, etc.
The package provides the ArrayList class, which is a mutable list that uses a dynamic resizable array as the backing storage.
The addAll() method
The ArrayList class contains the addAll() method, which adds all elements of the specified input collection to the end of the array list.
Syntax
fun addAll(elements: Collection<E>): BooleanArguments
This method takes a collection of elements as input.
Return value
This method returns the boolean value true if the list is modified during the addAll() operation. Otherwise, it returns false.
Things to note
-
ArrayList follows the sequence of insertion of elements.
-
It is allowed to contain duplicate elements.
-
The elements are appended in the order they appear in the specified collection.
The illustration below shows the function of the addAll() method.
Adding elements of a collection to an array list using the addAll() methodCode
The ArrayList class is present in the kotlin.collection package and is imported by default in every Kotlin program.
fun main(args : Array<String>) { val countryList = ArrayList<String>() countryList.add("India") countryList.add("US") println("Printing ArrayList elements--") println(countryList) val islandList = ArrayList<String>() islandList.add("Maldives") islandList.add("Mauritius") countryList.addAll(islandList) println("Printing ArrayList elements after calling addAll()--") println(countryList) }RunExplanation
-
First, we create an empty ArrayList to store the strings.
-
Next, we add a few country names to the ArrayList object using the add() method, such as: "India"and "US".
-
Next, we create another array list of strings and add few island names to the new ArrayList object using the add() method, such as: "Maldives"and "Mauritius".
-
Next, we call the addAll() method by passing the island names array list as the input parameter. It then adds all the elements of the island names array list to the country name array list.
-
The Arraylist elements are displayed before and after calling the addAll() method by using the print() function of the kotlin.io package. The list contains four names after calling the addAll() method.
Relevant Answers
Explore Courses
Free Resources
License: Creative Commons-Attribution-ShareAlike 4.0 (CC-BY-SA 4.0)Tag » Add Element List Kotlin
-
Collection Write Operations | Kotlin
-
Ajouter Des éléments à Une Liste Dans Kotlin - Techie Delight
-
How To Add An Item To A List In Kotlin? - Stack Overflow
-
How To Add Element To List In Kotlin? - Tutorial Kart
-
How To Add Element To List At Specific Index In Kotlin? - Tutorial Kart
-
6 Ways To Add Items To A List In Kotlin - CodeVsColor
-
How To Add An Item To A List In Kotlin? - Tutorialspoint
-
Working With Lists In Kotlin - Baeldung
-
Kotlin Lists - Working With Lists In Kotlin - ZetCode
-
Add Element In Kotlin List Code Example - Code Grepper
-
Kotlin List & Mutable List Tutorial With Examples - BezKoder
-
Add Items To A List In Kotlin | Delft Stack
-
Shooting Yourself In The Foot While Adding An Element To A Kotlin List
-
Kotlin List : ListOf() - GeeksforGeeks
-
Kotlin Lists (listOf & MutableListOf): Explained With 16 Examples
-
Data Structures & Algorithms In Kotlin, Chapter 3: Linked List
-
Kotlin ArrayList - Javatpoint
-
How Mutable List Works In Kotlin With Examples? - EduCBA
-
How To Add New Element In Array In Kotlin ? - Tutorialwing