Scala - 将数组填充到特定大小 [英] Scala - Pad an array to a certain size

查看:55
本文介绍了Scala - 将数组填充到特定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法:

def loadRom(filename: String) : Array[Int] = {
  val is = new FileInputStream(filename)
  Stream.continually(is.read).takeWhile(-1 !=).toArray
}

它返回一个 0x8000 数组(我总是可以保证).我想将它加载到 0x200000 大小的数组中.有没有一种优雅的方法来做到这一点?把它填充到 0x200000 怎么样?

It returns an array of 0x8000 (I can always guarantee that). I want to load it into an array of 0x200000 size. Is there an elegant way to do that? What about padding it out to 0x200000?

哎呀.我只是注意到 padTo 方法做了我想要的.关闭?

推荐答案

为了完整起见,有 SeqLike.padTo(数组隐式扩展 SeqLike).假设您想用零字节填充,只需将 .padTo(0x200000, 0) 附加到结果表达式即可.

For completeness's sake, there's SeqLike.padTo (arrays implicitly extend SeqLike). Assuming you want to pad with zero bytes, just append .padTo(0x200000, 0) to the result expression and you're good to go.

这篇关于Scala - 将数组填充到特定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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