Array.isDefinedAt在斯卡拉n维数组 [英] Array.isDefinedAt for n-dimensional arrays in scala

查看:177
本文介绍了Array.isDefinedAt在斯卡拉n维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种优雅的方式来恩preSS

Is there an elegant way to express

val a = Array.fill(2,10) {1}
def do_to_elt(i:Int,j:Int) {
    if (a.isDefinedAt(i) && a(i).isDefinedAt(j)) f(a(i)(j))
}

在Scala呢?

推荐答案

我建议您不要使用数组的数组为二维数组,主要有三个原因。首先,它允许矛盾:不是所有的列(或行,随你挑)必须是相同的大小。第二,它是低效的 - 你必须遵循两个指针,而不是一个。三,很少库函数存在的工作透明和有效的数组作为二维数组的数组。

I recommend that you not use arrays of arrays for 2D arrays, for three main reasons. First, it allows inconsistency: not all columns (or rows, take your pick) need to be the same size. Second, it is inefficient--you have to follow two pointers instead of one. Third, very few library functions exist that work transparently and usefully on arrays of arrays as 2D arrays.

由于这些事情,您应该使用支持二维数组,如 scalala 图书馆,或者你应该写你自己的。如果你选择后者,除其他外,这个问题奇迹般地消失了。

Given these things, you should either use a library that supports 2D arrays, like scalala, or you should write your own. If you do the latter, among other things, this problem magically goes away.

因此​​,在优雅的术语:没有,没有办法。 inelegance的但除此之外,你开始在路径中包含的地段的;你可能会做最好的快速步骤关闭它。

So in terms of elegance: no, there isn't a way. But beyond that, the path you're starting on contains lots of inelegance; you would probably do best to step off of it quickly.

这篇关于Array.isDefinedAt在斯卡拉n维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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