加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:50:01 所属栏目:Java 来源:网络整理
导读:所有, 我正在使用BufferedImages和Raster对象在Scala中进行一些图像处理.我试图使用以下代码获取缓冲图像中的所有像素. val raster = f.getRaster()// Preallocating the array causes ArrayIndexOutOfBoundsException .. http://forums.sun.com/thread.jspa
所有,

我正在使用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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读