1. Introduction

In Kotlin, an ArrayList is a commonly used data structure to store a collection of elements. Occasionally, we may find the need to remove the first element from an ArrayList.

In this tutorial, we’ll explore various methods to achieve this task with code examples.

2. Using removeAt()

The removeAt() function allows us to remove an element at a specific index in an ArrayList. To remove the first element, we simply pass 0 as the index:

@Test
fun `Remove first element using removeAt`() {
    val arrayList = arrayListOf("Apple", "Banana", "Cherry")

    arrayList.removeAt(0)

    assertEquals(listOf("Banana", "Cherry"), arrayList)
}

In this test, we use the removeAt() function to remove the first element from the ArrayList.

3. Using remove()

Another way to remove the first occurrence of a specific element from an ArrayList is by using the remove() function. This requires us to know which element is the first one in the list.

To remove the first element, we need to specify its value as the argument:

@Test
fun `Remove first element using remove`() {
    val arrayList = arrayListOf("Apple", "Banana", "Cherry")

    arrayList.remove("Apple")

    assertEquals(listOf("Banana", "Cherry"), arrayList)
}

Knowing that Apple is the first element in the list, we can call remove() passing it as an argument.

3.1. Using remove() With first()

As mentioned, remove() takes an element to remove. We can use first() to discover the first element of a list. We can then remove() it:

@Test
fun `Remove first element using remove and first`() {
    val arrayList = arrayListOf("Apple", "Banana", "Cherry")

    arrayList.remove(arrayList.first())

    assertEquals(listOf("Banana", "Cherry"), arrayList)
}

4. Creating a New List Using subList()

We can create a new ArrayList containing all elements except the first one by using the subList() method:

@Test
fun `Remove first element by creating a sublist`() {
    val arrayList = arrayListOf("Apple", "Banana", "Cherry")

    val updatedList = arrayList.subList(1, arrayList.size)

    assertEquals(listOf("Banana", "Cherry"), updatedList)
}

By splitting the ArrayList starting at 1, we’re able to exclude the first item in the original List from our new List. A key thing to note is that updatedList and arrayList are linked, so changes to one will affect the other. A way to avoid this link is by using slice().

5. Using slice()

The slice() function is useful for extracting a subset of elements from an ArrayList. To remove the first element, we can use slice() from index 1 through the size of the List:

@Test
fun `Remove first element by using slice`() {
    val arrayList = arrayListOf("Apple", "Banana", "Cherry")
    val updatedList = arrayList.slice(1 until arrayList.size)

    assertEquals(listOf("Banana", "Cherry"), updatedList)
}

This function is very similar to subList(). The key difference is that slice() creates a new list.

6. Creating a New List Using drop()

The drop() function removes the first n elements from an ArrayList. We can use it to remove the first element of our ArrayList by dropping a single element:

@Test
fun `Remove first element by using drop`() {
    val arrayList = arrayListOf("Apple", "Banana", "Cherry")

    val updatedList = arrayList.drop(1)

    assertEquals(listOf("Banana", "Cherry"), updatedList)
}

The drop() function removes n elements from the start of the List. Dropping a single item effectively removes the first item from a list.

7. Using filterIndexed()

The filterIndexed() function can be used to create a new ArrayList with elements that satisfy a certain condition. To remove the first element, we can use the index parameter to exclude it:

@Test
fun `Remove first element by using filterIndexed`() {
    val arrayList = arrayListOf("Apple", "Banana", "Cherry")

    val updatedList = arrayList.filterIndexed { index, _ -> index != 0 }

    assertEquals(listOf("Banana", "Cherry"), updatedList)
}

8. Conclusion

In this article, we’ve explored various methods to remove the first element from an ArrayList in Kotlin and demonstrated them in JUnit tests.

As always, the code used in this article is available over on GitHub.

Comments are closed on this article!