如何在Scala中高效地将阵列复制到另一个阵列? [英] How to efficient copy an Array to another in Scala?

查看:50
本文介绍了如何在Scala中高效地将阵列复制到另一个阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用其他方式将Array复制到另一个Array?

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

我的想法是使用=运算符.例如:

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

val A = Array(...)
val B = A

但这可以吗?

第二种方法是使用for loop,例如:

Second way is to use for loop, for example:

val A = Array(...)
val B = new Array[](A.length)//here suppose the Type is same with A
for(i <- 0 until A.length)
    B(i) = A(i)

推荐答案

您可以使用.clone

scala> Array(1,2,3,4)
res0: Array[Int] = Array(1, 2, 3, 4)

scala> res0.clone
res1: Array[Int] = Array(1, 2, 3, 4)

这篇关于如何在Scala中高效地将阵列复制到另一个阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆