Kotlin ArrayListOf
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
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
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
1. Kotlin Program To Make New Empty ArrayList
Code:
Output:
2. Kotlin Program To Make New ArrayList with String Elements
Code:
Output :
Turn Learning into Career Growth
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.