在 Scala 中初始化 2 维数组

发布于 2024-08-31 03:33:10 字数 1335 浏览 5 评论 0原文

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2024-09-07 03:33:10
for (r <- (1 to rows)) {

应该是:

for (r <- (0 to rows - 1)) {

...从 0 开始而不是 1 吗?

field (r) = spl.map (_.charAt (0))

是否应该使用运算符语法,如下所示:

field (r) = spl map (_.charAt (0))

... 不带“.” spl和map之间?


这是我的版本 - 我用数组[字符串]替换了扫描仪,因为我不太确定扫描仪的输入应该是什么。它在 Scala 2.7.5 上编译并运行:

class Field (val rows: Int, val cols: Int, lines: Array[String]) {
    var field = new Array [Array[Char]](rows)

    // These get replaced later on, but this is how to initialize a 2D array.
    for (i <- (0 to rows - 1)) {
        field(i) = new Array[Char](cols)
    }

    for (r <- (0 to rows - 1)) {
        val line = lines(r)
        val spl = line.split (" ")
        field(r) = spl map (_.charAt (0))
    }
}

var lines = Array[String] ("A A A A A", "B B B B B", "C C C C C", "D D D D D", "E E E E E")
var test  = new Field(5, 5, lines)
test.field
for (r <- (1 to rows)) {

Should this be:

for (r <- (0 to rows - 1)) {

... starting from 0 instead of 1?

field (r) = spl.map (_.charAt (0))

Should this use the operator syntax, like this:

field (r) = spl map (_.charAt (0))

... 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:

class Field (val rows: Int, val cols: Int, lines: Array[String]) {
    var field = new Array [Array[Char]](rows)

    // These get replaced later on, but this is how to initialize a 2D array.
    for (i <- (0 to rows - 1)) {
        field(i) = new Array[Char](cols)
    }

    for (r <- (0 to rows - 1)) {
        val line = lines(r)
        val spl = line.split (" ")
        field(r) = spl map (_.charAt (0))
    }
}

var lines = Array[String] ("A A A A A", "B B B B B", "C C C C C", "D D D D D", "E E E E E")
var test  = new Field(5, 5, lines)
test.field
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文