使用scala中的替代项重载方法值ofDim

问题描述 投票:2回答:2
def accessRecord(filePath: String, recordSize: Long, offset: Long): Int = {
     val fs = FileSystem.get(SparkHadoopUtil.get.conf)
     val stream = fs.open(new Path(filePath))
     val bytes = Array.ofDim[Byte](recordSize)
     stream.readFully(offset, bytes)
     stream.close()
     bytes.length
     }

在上面的代码中,recordSize变量作为参数传递,该参数从下面的函数之一接收值,数据类型为Long。所以它给出了错误

error: overloaded method value ofDim with alternatives:
  (n1: Int,n2: Int,n3: Int,n4: Int,n5: Int)(implicit evidence$7: scala.reflect.ClassTag[Byte])Array[Array[Array[Array[Array[Byte]]]]] <and>
  (n1: Int,n2: Int,n3: Int,n4: Int)(implicit evidence$6: scala.reflect.ClassTag[Byte])Array[Array[Array[Array[Byte]]]] <and>
  (n1: Int,n2: Int,n3: Int)(implicit evidence$5: scala.reflect.ClassTag[Byte])Array[Array[Array[Byte]]] <and>
  (n1: Int,n2: Int)(implicit evidence$4: scala.reflect.ClassTag[Byte])Array[Array[Byte]] <and>
  (n1: Int)(implicit evidence$3: scala.reflect.ClassTag[Byte])Array[Byte]
 cannot be applied to (Long)
           val bytes = Array.ofDim[Byte](recordSize)
                                  ^ 

有人可以告诉我为什么我会遇到这个错误?提前致谢!

java arrays scala
2个回答
1
投票

数组的最大长度由数组的固定整数长度控制。这就是为什么ofDim采用Int论证,但是你在Long传递recordSize(这是错误的来源和含义)。要解决它,你可以像Long.toInt一样打电话

val bytes = Array.ofDim[Byte](recordSize.toInt)

或者将recordSize改为Int


1
投票

因为Array.ofDim[A](size: Int)不期待Long

通常,JVM上的数组总是被32位整数索引,并且它可能不会改变,因为现在不可能在不破坏所有内容的情况下用.length替换Long的返回类型。

© www.soinside.com 2019 - 2024. All rights reserved.