播放 Scala 更改自定义列表项值

Play Scala changing custom List item value

在我的 Play Framework Scala 项目中,我有一个 custom List。我需要的一些操作 change/replace 带有 another value 的自定义列表项值。Scala 没有Java 没有像下面这样的方法

List<Int> testList = new ArrayList<Int>();
testList.get(0)// get the testList 0 th value

我需要的

case class ImageGrid(
    image_id: Int,
    image_name: String,    
    image_desc: String   
    )

val ImageGridList:List[ImageGrid] = //based on some DB Query I set values

我怎样才能 change/Replace 0th1st ImageGridList's image_nameimage_desc

的位置

使用scala.collection.mutable.ArrayBuffer代替列表

val list = scala.collection.mutable.ArrayBuffer(1,2,3,4)

按索引位置设置值:

list(0)=5

您可以使用可变的ArrayBuffer,这里是示例代码;

scala> import scala.collection._
 import scala.collection._

 scala> case class ImageGrid(
   |     image_id: Int,
   |     image_name: String,    
   |     image_desc: String   
   |     )
defined class ImageGrid

scala> val imageGridList = mutable.Buffer[ImageGrid](ImageGrid(1,"img1","img2"))
imageGridList: scala.collection.mutable.Buffer[ImageGrid] = ArrayBuffer(ImageGrid(1,img1,img2))

scala> imageGridList(0) = ImageGrid(1,"img name changed","img2")

scala> imageGridList
res1: scala.collection.mutable.Buffer[ImageGrid] = ArrayBuffer(ImageGrid(1,img name changed,img2))

要将现有 List 转换为 ArrayBuffer,请使用 toBuffer