How to efficient copy an Array to another in Scala?

前端 未结 4 1474
轻奢々
轻奢々 2021-01-12 03:28

How I can use another way to copy a Array to another Array?

My thought is to use the = operator. For example:

         


        
4条回答
  •  情话喂你
    2021-01-12 03:45

    Consider Array.copy in this example where dest is a mutable Array,

    val a = (1 to 5).toArray
    val dest = new Array[Int](a.size)
    

    and so

    dest
    Array[Int] = Array(0, 0, 0, 0, 0)
    

    Then for

    Array.copy(a, 0, dest, 0, a.size)
    

    we have that

    dest
    Array[Int] = Array(1, 2, 3, 4, 5)
    

    From Scala Array API note Scala Array.copy is equivalent to Java System.arraycopy, with support for polymorphic arrays.

提交回复
热议问题