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

val arraytwo:ArrayList<String> = arrayListOf(arrayone.removeAt(0))

for (item in arraytwo) {
  println(item)
}

I just want to remove item from the first array and make a new array. But this just prints one item at index 0

12
  • 1
    use arrayone.subList(1) instead of arrayListOf(arrayone.removeAt(0)) Commented Sep 27, 2018 at 9:15
  • 3
    removeAt returns the removed element. You're making a new list with one element, the element you removed. Commented Sep 27, 2018 at 9:16
  • What about if i want to remove the item at index 1 ? Commented Sep 27, 2018 at 9:16
  • 1
    does swift also remove that particular item from the first list? Commented Sep 27, 2018 at 9:41
  • 1
    and what does the first list then contain? Commented Sep 27, 2018 at 9:46

7 Answers 7

46

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.

Sign up to request clarification or add additional context in comments.

8 Comments

But drop returns all but first n elements. So it is not usefull for example if i want to remove just third element
well... it depends... it wasn't that clear what you wanted to accomplish ;-) if you just want to omit elements, drop is ok... if you want to remove items... It basically depends on whether the first list is allowed to be changed or not...
İs not ArrayList mutable?
@Selcuk That's the entire point... you're removing from the original list, not from your copy. If you don't want to copy the list, just do arrayone.removeAt(0) without another variable.
Ok got it. I need a copy to make a new one. And remove item from the copy. But i think it’s kind of weird. To make two process for such a simple situation. Thanks
|
8

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.

Comments

7

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]

Comments

7

For Normal ArrayList:

val arrList = arrayListOf("account", "bec", "csw", "account")
arrList.removeAll { it == "account" }

For Custom ArrayList:

arrList.removeAll { it.key == "account" }

Comments

3

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)
}

Comments

1
from import java.utils.ArrayList

ArrayList<YourCustomObject>.removeIf{yourCustomObject ->
     yourCustomObject.Id == IdToRemove
}

1 Comment

Your answer only contains code. I recommend that you don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
-1
  val x = arrayListOf(1, 2, 3, 5)
  x.removeFirst()
  println(x)

  val y = x.filterIndexed { index, value -> index != 0 }
  println(y)

Output:

[2, 3, 5]
[3, 5]

GL

Source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.