如何从只有索引的 Scala 列表中删除项目? [英] How to remove an item from a list in Scala having only its index?
本文介绍了如何从只有索引的 Scala 列表中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表如下:
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)
我不能使用ListBuffer
,不得不维护现有的结构.我该怎么办?
I can not use a ListBuffer
, are obliged to maintain the existing structure.
How can I do?
谢谢大家
推荐答案
简单使用
val trunced = internalIdList.take(index) ++ internalIdList.drop(index + 1)
如果索引大于列表的大小,这也将起作用(它将返回相同的列表).
This will also work if index is larger than the size of the list (It will return the same list).
这篇关于如何从只有索引的 Scala 列表中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文