Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 第二个参数替换第一个scala_Arrays_Scala_Function_Multidimensional Array - Fatal编程技术网

Arrays 第二个参数替换第一个scala

Arrays 第二个参数替换第一个scala,arrays,scala,function,multidimensional-array,Arrays,Scala,Function,Multidimensional Array,我试图在scala(^)中定义一个函数,它接受2个值并按如下方式打印它们 2 x 这是我到目前为止所拥有的 class $ (val text2D: Array[Array[Char]]) { def ^(that: $) = { " " ++ s"${this.text2D(0)(0)}" ++ "\n" ++ s"${that.text2D(0)(0)}" } def +(that: $) = this.text2D + "+" + that.text2

我试图在scala(^)中定义一个函数,它接受2个值并按如下方式打印它们

 2
x
这是我到目前为止所拥有的

class $ (val text2D: Array[Array[Char]])
{
  def ^(that: $) =
  {
    " " ++ s"${this.text2D(0)(0)}" ++
    "\n" ++ s"${that.text2D(0)(0)}"
  }

  def +(that: $) = this.text2D + "+" + that.text2D

  override def toString = s"${this.text2D(0)(0)}"
}

object $ {
  val array = Array.ofDim[Char](1,1)
  def apply(x: String): $ = {
    array (0)(0) = x.charAt(0)
    new $ (array)
  }
}

val x = $("x")
println(x)

val x2 = $("x") ^ $("2")
println(x2)
当我运行它时,我没有得到我期望的输出,而是得到了

 2
2

为什么它只接受第二个元素?任何帮助都将不胜感激。

对象
创建一个单例,因此您使用的(可变)数组将在调用
应用
之间共享。您需要在
apply
调用中分配该数组

def apply(x: String): $ = {
  val array = Array.ofDim[Char](1,1)
  array (0)(0) = x.charAt(0)
  new $ (array)
}
还有,有点不相关,但我相信你的论点是相反的。要获得所需的输出,您需要

" " ++ s"${that.text2D(0)(0)}" ++
"\n" ++ s"${this.text2D(0)(0)}"

我想你需要的是这样的东西:

class $(val text2D: Array[String]) {
  def ^(that: $): $ = {
    if (this.text2D.length == 0)
      that
    else if (that.text2D.length == 0)
      this
    else {
      val thisW = this.text2D(0).length
      val thatW = that.text2D(0).length

      // cross-pad arrays to have the same width
      val padThisRight = " " * thatW
      val padThatLeft = " " * thisW
      val thisPaddedW = this.text2D.map(_ + padThisRight)
      val thatPaddedW = that.text2D.map(padThatLeft + _)
      // first lines comes from that!
      new $(thatPaddedW ++ thisPaddedW)
    }
  }

  def +(that: $): $ = {
    if (this.text2D.length == 0)
      that
    else if (that.text2D.length == 0)
      this
    else {
      val thisH = this.text2D.length
      val thatH = that.text2D.length
      val thisW = this.text2D(0).length
      val thatW = that.text2D(0).length

      // pad arrays to have the same height
      val emptyThis = " " * thisW
      val emptyThat = " " * thatW
      val thisPaddedH = if (thisH >= thatH) this.text2D else Array.fill(thatH - thisH)(emptyThis) ++ this.text2D
      val thatPaddedH = if (thisH <= thatH) that.text2D else Array.fill(thisH - thatH)(emptyThat) ++ that.text2D

      new $(thisPaddedH.zip(thatPaddedH).map(p => p._1 + p._2))
    }
  }

  override def toString = text2D.mkString("\n")
}

object $ {
  def apply(x: String): $ = {
    new $(Array[String](x))
  }
}
产生以下输出

x2:
 2
x 
----------------------------
z:
 2    2
x  + y 
----------------------------
zz:
       3
 2    2 
x  + y  
----------------------------
这里的主要思想是,对
$
的操作会生成另一个
$
实例,而不是
字符串
(我使用
字符串
,而不是
数组[Char]
,因为它看起来容易得多,也没有明显的缺点)。这样,您就不必重新解析
String
并将其按新行拆分,也不必考虑如何处理字符串未对齐的情况。所以现在操作符
^
+
只是一个练习,将两个2d数组对齐,使其具有相同的宽度或高度,然后将它们连接起来

x2:
 2
x 
----------------------------
z:
 2    2
x  + y 
----------------------------
zz:
       3
 2    2 
x  + y  
----------------------------