数组在scala中设置

use*_*611 0 arrays scala set

我在scala中有代码:

 val graph = new Array [Set[Int]] (n)

 def addedge(i:Int,j:Int)
 {
     graph(i)+=j
 }
Run Code Online (Sandbox Code Playgroud)

什么graph(i)+=j意思?任何人都可以用c,c ++或java等其他语言翻译吗?

dhg*_*dhg 7

graphArray的,就像在C或Java. graph(i)意思是"访问ith元素graph".中的每个元素graph是一个SetInt第 添加项目的+=方法.所以将数字添加到存储在索引处.SetSetgraph(i) += jjSetigraph

在REPL中尝试一下就会显示出这种行为:

scala> val graph = Array(Set(1,2), Set(2,3), Set(1))
graph: Array[scala.collection.immutable.Set[Int]] = Array(Set(1, 2), Set(2, 3), Set(1))

scala> graph(1) += 4

scala> graph
res0: Array[scala.collection.immutable.Set[Int]] = Array(Set(1, 2), Set(2, 3, 4), Set(1))
Run Code Online (Sandbox Code Playgroud)