How to remove item from ArrayList in Kotlin

If you just want to filter one certain element of your array you can use .filterTo(destination, predicate). For your example it can look like this:

val arrayone = arrayListOf<String>("a", "b", "c")
val arraytwo = arrayListOf<String>()

arrayone.filterTo(arraytwo, { it != "a" })

println(arrayone) //[a, b, c]
println(arraytwo) //[b, c]

removeAt(0) removes the first element from the first list and returns it. arrayListOf then uses that removed item to create a new (the second) list.

arrayone then contains: b and c. arraytwo then contains a.

You may want to use drop instead, if you didn't want to touch the first list and if you only wanted to add the remaining items to the new list, e.g.:

var arrayone: ArrayList<String> = arrayListOf("a","b","c")

val arraytwo = arrayone.drop(1)

for (item in arraytwo) {
  println(item) // now prints all except the first one...
}
// arrayone contains: a, b, c
// arraytwo contains: b, c

Or use dropLast(1) if you want to drop only the last item. Or use dropWhile/dropLastWhile if you have a condition to apply and drop all until/upto that condition...

If you really want to remove items from the first and add only the removed ones to the second list, then your current approach is ok. If you however wanted to remove items at specific index and have a new list instead just containing the not-removed ones, you need to construct a copy of the first list first and then apply your removeAt on that list, e.g.:

val arraytwo = arrayone.toMutableList().apply { 
  removeAt(0)
}
// or without the apply:
arraytwo.removeAt(0)

Or you may use filterIndexed to solve that:

val arraytwo = arrayone.filterIndexed { index, _ ->
  index != 1 // you can also specify more interesting filters here...
} // filter, map, etc. all return you a new list. If that list must be mutable again, just add a .toMutableList() at the end

By using filterIndexed, filter, drop, etc. you ensure that the first list is kept untouched. If you didn't want to touch the first list in the first place, you may rather want to use listOf or toList, i.e. just a List as type instead, which does not expose mutable functions (check also Kotlin reference regarding Collections: List, Set, Map).

Maybe you are also interested in filter/filterNot and then soon in minus or similar functions to remove unwanted items without index.


removeAt returns the removed element:

abstract fun removeAt(index: Int): E (source)

Removes an element at the specified index from the list.

Return the element that has been removed.
kotlin.stdlib / kotlin.collections.MutableList.removeAt

You're making a new list with one element, the element you removed.

Try:

val arraytwo = ArrayList(arrayone) // Copy the list
arraytwo.removeAt(0)

You never clarified if you want to modify the original list. If you do, just do arrayone.removeAt(0). That's it.

You can also make use of apply:

val arraytwo = ArrayList(arrayone).apply { removeAt(0) }

If you only need to remove items at the start or end, you can use drop (to remove items at the start) or dropLast, but as far as I know there is no collection extension to drop an item in the middle of an iterable (and judging by your comment, you seem to need this.) This makes sense, since an iterable has no concept of size or index.


try this ,

var arrayone: ArrayList<String> = arrayListOf("a","b","c")
arrayone.removeAt(0)
val arraytwo:ArrayList<String> = arrayListOf(arrayone)
for (item in arraytwo) {
  println(item)
}

Tags:

Arrays

Kotlin