【问题标题】:Insert value into Mutable List using for loop (index wise) in scala在scala中使用for循环(索引方式)将值插入可变列表
【发布时间】:2020-01-28 17:27:24
【问题描述】:

我想使用 for 循环在可变列表索引中插入值,如下所示

var firstList=List("A","B","C","D")
var newList = new ListBuffer[String]()  
for(i<-0 to firstList.length-1){
    **newList(i)**=firstList(i)
}

我想做类似的任务.. 我知道如何使用“newList+=firstList(i)”这个方法插入值,但我希望用索引明智的方法来插入值

【问题讨论】:

  • “index wise”是什么意思?

标签: list scala indexing insert mutable


【解决方案1】:

在scala.collection.mutable.ArrayBuffer中使用def insert(n: Int, elems: A*)方法https://www.scala-lang.org/api/current/scala/collection/mutable/ArrayBuffer.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多