What is the difference between array and buffer when using scala?

when I work on some task using scala, I wrote the code as follows:

object He { def main(args: Array[String]): Unit = { var myMatrix = Array.ofDim[String](3,3) // build a matrix for (i <- 0 to 1) { for ( j <- 0 to 1) { myMatrix(i)(j) = "faf"; } } var eventbuffer = for(i <- myMatrix) yield for(j <- i) yield j var eventArray = for(i <- eventbuffer) yield i.toArray var eventpool:Array[(String, Array[String])] = eventArray.toArray.map(son => (son(0), son)) } } 

I want to ask a question, what is the difference between eventbuffer and eventArray? Finally, what will be the eventpool? I'm really confused, thanks for helping me with this.

+5
source share
1 answer

In Scala, a Array is just a JVM array, and the various Buffer are actual classes.

An Array[String] ist the same as String[] in Java. You can think of ArrayBuffer as ArrayList in Java (they are very similar, but not equivalent) and ListBuffer as Java LinkedList (again, it seems, but not the same).

It should be noted, however, that in your example eventbuffer not a Buffer , but an array of arrays. This is actually a fairly accurate copy of myMatrix , so calling the toArray method is actually superfluous.

+8
source

Source: https://habr.com/ru/post/1264972/


All Articles