微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Scala – 当一个arg必须为null时,如何显式选择要使用哪个重载方法?

所有,

我正在使用BufferedImages和Raster对象在Scala中进行一些图像处理.我试图使用以下代码获取缓冲图像中的所有像素.

val raster = f.getRaster()

// Preallocating the array causes Arrayindexoutofboundsexception .. http://forums.sun.com/thread.jspa?threadID=5297789
// RGB channels;
val pixelBuffer = new Array[Int](width*height*3)
val pixels = raster.getPixels(0,width,height,pixelBuffer)

现在,当我读取相对较大的文件时,这很好用.当我读入20×20 PNG文件时,我得到一个Arrayindexoutofboundsexception

java.lang.Arrayindexoutofboundsexception: 1200
at sun.awt.image.ByteInterleavedRaster.getPixels(ByteInterleavedRaster.java:1050)

我已经阅读了online,解决这个问题的方法是不预先分配pixelBuffer,而是传入一个空值并使用Raster.getPixels方法返回的值.

这是我的问题.当我做天真的方法,并将Nil作为最后一个参数传递:

val pixels = raster.getPixels(0,Nil)

我收到了错误

error: overloaded method value getPixels with alternatives (Int,Int,Array[Double])Array[Double] <and> (Int,Array[Float])Array[Float] <and> (Int,Array[Int])Array[Int] cannot be applied to (Int,Nil.type)
val pixels = raster.getPixels(0,Nil)

显然编译器无法确定我试图调用的两种方法中的哪一种;这是模棱两可的.如果我使用Java,我会抛出null以使我的意图明确.我无法弄清楚如何在Scala中获得相同的效果.我尝试过的事情:

val pixelBuffer:Array[Int] = Nil // Cannot instantiate an Array to Nil for some reason
 Nil.asInstanceOf(Array[Int]) // asInstanceOf is not a member of Nil

任何想法如何明确地告诉编译器我希望使用Int数组作为最后一个参数而不是Float数组的方法

编辑:
正如一个答案所指出的那样,我让Nil与null混淆了. Nil是一个空列表.请参阅以下blog post

另外,我应该指出数组越界异常是我的错(因为这些事情经常是这样).问题是我假设光栅有3个通道,但我的图像有4个通道,因为我已经创建了它.我改为预先分配数组如下:

val numChannels = raster.getNumBands() 

val pixelBuffer = new Array[Int](width*height*numChannels)
val pixels = raster.getPixels(minX,minY,pixelBuffer)

谢谢您的帮助

解决方法

(假设您需要在需要传递null时如何解决重载的问题):

正如您在Java中所做的那样,通过归因于您希望调用的重载对应的类型(在Java中,您将转换,但它相同的事情:要分配给null的静态类型的断言):

scala> object O { def m(i: Int,s: String): String = s * i; def m(i: Int,l: List[String]): String = l.mkString(":") * i }
defined module O

scala> O.m(23,null)
<console>:7: error: ambiguous reference to overloaded deFinition,both method m in object O of type (i: Int,l: List[String])String
and  method m in object O of type (i: Int,s: String)String
match argument types (Int,Null)
       O.m(23,null)
         ^

scala> O.m(23,null: String)
res4: String = nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull

scala> O.m(23,null: List[String])
java.lang.NullPointerException
        at O$.m(<console>:5)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at RequestResult$.<init>(<console>:9)
        at RequestResult$.<clinit>(<console>)
        at RequestResult$scala_repl_result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
        at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
        at scala.util.control.Exception$Catch.apply(Exception.scala:7...
scala>

原文地址:https://www.jb51.cc/java/121117.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐