在 Scala 中初始化 2 维数组
(Scala 2.7.7:)我不习惯二维数组。数组是可变的,但是我如何指定一个 2d 数组 - 假设大小为 3x4。维度(2D)是固定的,但每个维度的大小应可初始化。我尝试过:
class Field (val rows: Int, val cols: Int, sc: java.util.Scanner) {
var field = new Array [Char](rows)(cols)
for (r <- (1 to rows)) {
val line = sc.nextLine ()
val spl = line.split (" ")
field (r) = spl.map (_.charAt (0))
}
def put (row: Int, col: Int, c: Char) =
todo ()
}
我收到此错误: :11: 错误:值更新不是 Char 的成员 field (r) = spl.map (_.charAt (0))
如果是 Java,则会有更多代码,但我知道如何做到这一点,所以我展示了我的意思:
public class Field
{
private char[][] field;
public Field (int rows, int cols, java.util.Scanner sc)
{
field = new char [rows][cols];
for (int r = 0; r < rows; ++r)
{
String line = sc.nextLine ();
String[] spl = line.split (" ");
for (int c = 0; c < cols; ++c)
field [r][c] = spl[c].charAt (0);
}
}
public static void main (String args[])
{
new Field (3, 4, new java.util.Scanner ("fraese.fld"));
}
}
fraese.fld 会例如,看起来像这样:
M M M
M . M
我得到了一些步骤,
val field = new Array [Array [Char]](rows)
但是我将如何实现“put”?或者有没有更好的方法来实现二维数组。是的,我可以使用一维数组并使用,
put (y, x, c) = field (y * width + x) = c
但我更喜欢看起来更像二维的表示法。
(Scala 2.7.7:) I don't get used to 2d-Arrays. Arrays are mutable, but how do I specify a 2d-Array which is - let's say of size 3x4. The dimension (2D) is fixed, but the size per dimension shall be initializable. I tried this:
class Field (val rows: Int, val cols: Int, sc: java.util.Scanner) {
var field = new Array [Char](rows)(cols)
for (r <- (1 to rows)) {
val line = sc.nextLine ()
val spl = line.split (" ")
field (r) = spl.map (_.charAt (0))
}
def put (row: Int, col: Int, c: Char) =
todo ()
}
I get this error:
:11: error: value update is not a member of Char
field (r) = spl.map (_.charAt (0))
If it would be Java, it would be much more code, but I would know how to do it, so I show what I mean:
public class Field
{
private char[][] field;
public Field (int rows, int cols, java.util.Scanner sc)
{
field = new char [rows][cols];
for (int r = 0; r < rows; ++r)
{
String line = sc.nextLine ();
String[] spl = line.split (" ");
for (int c = 0; c < cols; ++c)
field [r][c] = spl[c].charAt (0);
}
}
public static void main (String args[])
{
new Field (3, 4, new java.util.Scanner ("fraese.fld"));
}
}
and fraese.fld would look, for example, like that:
M M M
M . M
I get some steps wide with
val field = new Array [Array [Char]](rows)
but how would I then implement 'put'? Or is there a better way to implement the 2D-Array. Yes, I could use a one-dim-Array, and work with
put (y, x, c) = field (y * width + x) = c
but I would prefer a notation which looks more 2d-ish.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是:
...从 0 开始而不是 1 吗?
是否应该使用运算符语法,如下所示:
... 不带“.” spl和map之间?
这是我的版本 - 我用数组[字符串]替换了扫描仪,因为我不太确定扫描仪的输入应该是什么。它在 Scala 2.7.5 上编译并运行:
Should this be:
... starting from 0 instead of 1?
Should this use the operator syntax, like this:
... without the '.' between spl and map?
This is my version - I replaced the Scanner with an Array[String] since I'm not really sure what the input for the scanner is supposed to be. It compiles and runs on Scala 2.7.5: