2

I am adding elements to a list in a loop but inside the loop it shows that elements are added but outside the loop its not showing the list.

here is code:

def createSubgraphList(subgraphRdd: RDD[String]) : List[SubGraph] = {
val lstSubGraph = ListBuffer[SubGraph]()
var subGraph : SubGraph =null
val subGraphlist : SubGraph = new SubGraph()

subgraphRdd.foreach(x => {
  if(x.startsWith("f")){
    val freq= x.split(" ")
    subGraph.setIntFrequency(freq(1).toInt)
    lstSubGraph.append(subGraph)
    println("List size in loop: "+lstSubGraph.size)
  }
  if(x.startsWith("Subgraph")){
    subGraph = new SubGraph();
    subGraph.create()
  }
  if (x.startsWith("v")){
    subGraph.addVertex(x)
  }
  if (x.startsWith("e")){
    subGraph.addEdge(x)
  }
})
println("List size out of the loop: "+lstSubGraph.size)

lstSubGraph.toList
}

Output :
        List size in loop: 1
        List size in loop: 2
        List size out of the loop: 0
Aamir Ali
  • 21
  • 3

0 Answers0