ArrayList in Kotlin

Learn via video courses
Topics Covered

Overview

An ArrayList in Kotlin is a dynamic, resizable collection that can store elements of a single data type. An ArrayList in Kotlin is used to handle lists of data and allows for flexible management of data, including adding, removing, and accessing elements.

Constructor of Kotlin ArrayList

ConstructorDescription
ArrayList()It's used to create an empty ArrayList
ArrayList(capacity: Int)It's used to create an ArrayList with a predetermined capacity.
ArrayList(elements: Collection)It's used to create an ArrayList filled from the elements of the collection.

Functions of Kotlin ArrayList

FunctionDescription
open fun add(element: E): BooleanIt's used to insert the designated element into the collection.
open fun add(index: Int, element: E)It's used to insert an element at a specific index.
open fun addAll(elements: Collection): BooleanIt's used to incorporate all elements from the designated collection into the existing collection.
open fun addAll(index: Int, elements: Collection): BooleanIt's used to insert all elements from the specified collection into the current list at the designated index.
open fun clear()It's used to clear or delete all elements from the collection.
open fun get(index: Int): EIt's used to return the element at the specified index in the list.
open fun indexOf(element: E): IntIt's used to provide the index of the first occurrence of a specified element in the list. If the specified element is not present in the list, it returns -1.
open fun lastIndexOf(element: E): IntIt's used to provide the index of the last occurrence of a specified element in the list. If the specified element is not present in the list, it returns -1.
open fun remove(element: E): BooleanIt's used to eliminate one occurrence of the particular element from the current collection if it exists.
open fun removeAt(index: Int): EIt's used to delete the element at the specified index from the list.
open fun removeRange(startIndex: Int, endIndex: Int)It removes a range of elements beginning at startIndex and ending before endIndex, where the element at endIndex is not included.
open fun set(index: Int, element: E): EIt's used to substitute the element at the designated position within the current list with the specified element.
open fun toArray(): Array<Any?>It's used to generate a new Array of type Array<Any?> containing the elements from this collection.
open fun toString(): StringIt's used to provide a string representation of the object.
fun trimToSize()It does nothing in this ArrayList implementation.

Some of the Important Methods:

1. add(index, element: E): Boolean

This function is used to insert a specified element into the ArrayList. The function takes two parameters: index and element. The element parameter is mandatory and contains the element you want to add to the ArrayList. The index parameter is optional. By default, if you do not provide an index, the element is added to the ArrayList at the position immediately following the last element.

Code:

Output:


2. addAll(index: Int, elements: Collection): Boolean

This function serves to include all the elements from a specified collection into the current list, and it allows you to specify the index at which these elements should be inserted. The first parameter, index, is optional. If you don't provide an index, the elements are added to the list at the end.

Code:

Output:


3. get(index: Int): E

The function is used to access and obtain the value of an element at a particular position within the list.

Code:

Output:


4. set(index: Int, element: E)

The function serves to substitute the element at the specified position within the current list with the element provided as an argument.

Code:

Output:


5. indexOf(element: E): Int

The function is used to provide the index of the first occurrence of a specified element within the list. If the element is not found in the list, it returns -1, indicating that the element is not present.

Code:

Output:


6. remove(element: E): Boolean

This function is used to eliminate the initial occurrence of a particular element from the current collection, provided that it exists.

Code:

Output:


7. removeAt(index: Int)

This function is used to eliminate an element at a specific index from the list.

Code:

Output:


8. clear()

This function is used to erase or eliminate all elements from the list, effectively resetting the list to an empty state.

Code:

Output:

In the Kotlin programming language, an ArrayList is a flexible list structure supported by an array, similar to Java's ArrayList. This data structure allows for on-the-fly resizing, offering an array of methods to insert, delete, and modify elements within the list.

To create an ArrayList in Kotlin, two primary methods exist: you may use the arrayListOf() function or directly use the ArrayList constructor. For instance:

Code:

Output:

Advantages and Disadvantages of Kotlin ArrayList

Here are some advantages and disadvantages of using an ArrayList in Kotlin:

Advantages:

  • Dynamic resizing:
    An ArrayList in Kotlin dynamically adjusts its size, enabling hassle-free addition and removal of elements, a feature not present in traditional arrays.

  • Flexible storage:
    An ArrayList in Kotlin can accommodate elements of any data type, making them suitable for managing a wide range of heterogeneous data collections.

  • Easy manipulation:
    With an extensive set of methods, an ArrayList in Kotlin simplifies the process of adding, removing, and manipulating elements within the list, ensuring a convenient and streamlined experience.

Disadvantages:

  • Performance Overhead:
    An ArrayList in Kotlin introduces a slight performance overhead compared to arrays, which can impact speed in specific scenarios.

  • Increased Memory Consumption:
    An ArrayList in Kotlin may consume more memory than arrays as it stores each element as an object, incurring additional memory usage.

  • Iteration Efficiency:
    While iterating through an ArrayList in Kotlin, accessing elements by index can be slower than arrays because each element necessitates a function call rather than direct memory access.

Conclusion

  • An arrayList in Kotlin offers dynamic resizing capabilities, a feature not present in traditional arrays.
  • An ArrayList in Kotlin can be used to store elements of a specific data type, and they maintain type safety.
  • With a rich set of methods, an ArrayList in Kotlin simplifies the process of adding, removing, and manipulating elements within the list, enhancing code efficiency and readability.
  • An arrayList in Kotlin may utilize more memory than arrays due to the object-based element storage, which could impact the memory footprint of your application.
  • When iterating over an ArrayList in Kotlin, accessing elements by index can be less efficient than arrays, as each access involves a function call. Careful consideration is needed for performance-critical scenarios.