Kotlin ArrayListOf
Overview
In Kotlin, an ArrayList, specifically created using the arrayListOf function, is a dynamic array that can store a collection of elements. It's flexible and allows for adding, modifying, and removing items efficiently. You can declare and initialize a Kotlin arrayListOf with a specific data type, making it a versatile tool for managing lists of elements in your code.
Syntax
1.Creating an Empty ArrayList:
- Creating an ArrayList with Provided Elements:
Examples
1. Kotlin Program To Make New Empty ArrayList
Code:
Output:
2. Kotlin Program To Make New ArrayList with String Elements
Code:
Output :
3. Kotlin Program To Make New ArrayList With Elements Of Any Data Type
Code :
Output:
Functions of ArrayListOf
- add(element):
This function is used to add a single element to the end of an ArrayList.
Code:
Output:
- add(index, element):
This function is used to insert a single element at a specific index within an ArrayList.
Code:
Output:
- addAll(elementCollection):
This function is used to add all elements from another collection to the end of an ArrayList
Code :
Output:
- addAll(index, elementCollection):
This function is used to add all elements from another collection to a specific index within an ArrayList.
Code:
Output:
- clear():
The clear method is used to remove all the elements from an ArrayList, effectively making it an empty ArrayList.
Code:
Output:
- contains(element):
This function is used to check whether a specific element exists in an ArrayList. It returns true if the element is found in the ArrayList, and false otherwise.
Code:
Output:
-
containsAll(elementCollection):
This function is used to check if all elements in the specified collection are found in the ArrayList and returns true if so; otherwise, it returns false.Code:
Output :
-
get(index):
This function retrieves an element at a specific index in an ArrayList.Code:
Output:
-
indexOf(element) :
This function returns the index of the first occurrence of an element in an ArrayList, or -1 if the element is not found.Code:
Output:
- lastIndexOf(element):
This function returns the index of the last occurrence of an element in an ArrayList, or -1 if the element is not found.
Code:
Output:
- remove(element) :
This function removes an element from an ArrayList and returns true if successful, false if the element is not found.
Code:
Output:
- removeAll(elementCollection) :
This function removes elements from an ArrayList and returns true if any elements were removed, or false if none were found.
Code:
Output:
- removeAt(index):
removes an element by its position in an ArrayList, returning true if the element is successfully removed, or false if not found.
Code:
Output:
- set(index, element):
This function updates the element at a specific position in an ArrayList.
Code:
Output:
- toArray():
This function converts an ArrayList into an array of a specific type.
Code:
Output:
- toString():
This function is used to obtain the string representation of an ArrayList object.
Code:
Output:
-
isEmpty() :
This function is used to check if an ArrayList is empty.Code:
Output:
Conclusion
- ArrayList in Kotlin is a dynamic, flexible data structure that allows you to store and manage collections of elements efficiently.
- You can create an ArrayList using the arrayListOf() function, which can be initially empty or contain specific elements.
- Kotlin arrayListOf is versatile and supports various operations, including adding, inserting, removing, and updating elements, making them a powerful tool for list management.
- Key functions such as add, addAll, remove, set, and others provide convenient ways to manipulate the content of an ArrayList.
- You can also check for the presence of elements, find their indices, convert a Kotlin arrayListOf to an array, and check if it's empty, making Kotlin arrayListOf a comprehensive tool for handling lists in Kotlin.