0

I have code in scala :

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

 def addedge(i:Int,j:Int)
 {
     graph(i)+=j
 }

What does graph(i)+=j mean? Can anybody translate it in any other languages like c, c++ or java?

1 Answer 1

7

graph is an Array, just like in C or Java. graph(i) means "access the ith element of graph". Each element in graph is a Set of Ints. The += method on Set adds an item to the Set. So graph(i) += j adds the number j into the Set stored at index i in graph.

Trying things out in the REPL shows the behavior:

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))
Sign up to request clarification or add additional context in comments.

2 Comments

how about this one ?'for(i<-0 until n) { graph(i)=Set[Int](); }'
@user194611, ask a new question as a new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.