Scala 的多维数组再一次
var channelsNumber = track.getNumberOfChannels()
var framesNumber = lastFrame - firstFrame
var frames = Array.ofDim[Int](channelsNumber)(framesNumber)
System.out.println(frames.length);
System.out.println(frames.length);
我尝试定义二维整数数组。我收到此错误:
[error] .../test.scala:58: type mismatch;
[error] found : Int
[error] required: scala.reflect.ClassManifest[Int]
[error] var frames = Array.ofDim[Int](channelsNumber)(framesNumber)
[error] ^
[error] one error found
什么是“scala.reflect.ClassManifest[Int]”?为什么channelsNumber可以通过而framesNumber(也是一个整数)却不能?
var channelsNumber = track.getNumberOfChannels()
var framesNumber = lastFrame - firstFrame
var frames = Array.ofDim[Int](channelsNumber)(framesNumber)
System.out.println(frames.length);
System.out.println(frames.length);
I try to define two dimensional array of integers. And I get this error:
[error] .../test.scala:58: type mismatch;
[error] found : Int
[error] required: scala.reflect.ClassManifest[Int]
[error] var frames = Array.ofDim[Int](channelsNumber)(framesNumber)
[error] ^
[error] one error found
What is "scala.reflect.ClassManifest[Int]"? Why channelsNumber passes and framesNumber, which is also an integer doesn't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在,您正在调用您不想调用的方法
ofDim [T] (n1: Int)(implicit arg0: ClassManifest[T])
。将调用更改为Array.ofDim[Int](channelsNumber,framesNumber)
和方法ofDim [T] (n1: Int, n2: Int)(implicit arg0: ClassManifest[T])
将被调用。您希望隐式参数组保持隐式。并且 - 类清单是一种如何在泛型类中保留类型信息的方法。
Now, you are calling method
ofDim [T] (n1: Int)(implicit arg0: ClassManifest[T])
which you don't want to. Change the call toArray.ofDim[Int](channelsNumber,framesNumber)
and the methodofDim [T] (n1: Int, n2: Int)(implicit arg0: ClassManifest[T])
will be called. You want to leave the implicit parameter group implicit.And - class manifest is a way how to preserve type information in generic classes.
首先是你的错误:ofDim 获取单个参数列表中的所有维度。您需要
第二个,
ClassManifest
。由于类型擦除,并且与 JVM 中的事实相比,数组非常类似于泛型,但不是泛型(除其他外,没有类型擦除),泛型方法ofDim
需要传递元素的类型。这就是 ClassManifest,它接近于在 java 中传递 Class(如果您有一个必须返回数组的泛型方法,则必须在 java 中执行相同的操作 - 或者在 Collection.toArray 中传递正确类型的空数组) )这是一个隐式参数,即有另一个参数列表包含此参数,但 scala 编译器会尝试自动填充它,而无需您将其写入代码中。但如果你给出第二个参数列表,则意味着你打算自己传递ClassManifest
。First your error: ofDim takes all the dimension in a single parameter list. You need
Second,
ClassManifest
. Due to type erasure, and the fact than in the JVM, arrays are very much like generics, but are not generic (among other things, no type erasure), the generic methodofDim
needs to be passed the type of the elements. That is the ClassManifest, which is close to passing a Class in java (you have to do the same in java -or pass an empty array of the proper type, in Collection.toArray- if you have a generic method that must return an array) This comes as as animplicit
arguments, that is there is another parameter list with this argument, but the scala compiler will try to fill it automatically, without you having to write it in the code. But if you give a second parameter list, it means you intend to pass theClassManifest
yourself.