I have a list as follows:
val internalIdList: List[Int] = List() internalIdList = List(11, 12, 13, 14, 15)
From this list would remove the third element in order to obtain:
internalIdList = List(11, 12, 14, 15)
I can not use a ListBuffer
, are obliged to maintain the existing structure. How can I do?
Thanks to all
There are two options to remove an element by its index in list. Using del statement, and using pop() method. The del statement needs index of the element to remove. The pop() method of built-in list class requires index as argument.
You can use the splice() method to remove the item from an array at specific index in JavaScript. The syntax for removing array elements can be given with splice(startIndex, deleteCount) .
Scala List indexOf() method with example. The indexOf() method is utilized to check the index of the element from the stated list present in the method as argument. Return Type: It returns the index of the element present in the argument.
There is a .patch
method on Seq
, so in order to remove the third element you could simply do this:
List(11, 12, 13, 14, 15).patch(2, Nil, 1)
Which says: Starting at index 2, please remove 1 element, and replace it with Nil.
Knowing this method in depth enables you to do so much more than that. You can swap out any sublist of a list with arbitrary other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With