将相同数据传递到Scala中的两个或多个方法

时间:2019-03-09 03:57:37

标签: arrays scala

我试图在main方法中调用两个方法,并将相同的数据传递给这两个方法。在这里,第二个参数是具有n行2列的二维数组。第一个方法成功执行,但是当调用具有相同数据的第二个方法时,数组将为null(所有enter均设为零)。我想将相同的数据(数组)传递给两个方法。请注意,第一个参数(n)的值保持不变。如何将相同的数据传递给两种方法? 代码如下。

object Demo {
  def main(args: Array[String]): Unit = {
    println("Enter number of process:");
    val n = scala.io.StdIn.readInt();
    var array: Array[Array[Double]] = Array.ofDim(n, 2)
    var bt: Double = 0
    var at: Double = 0
    for (i <- 0 to n - 1) {
      println("Enter BT for process: " + i);
      bt = scala.io.StdIn.readDouble();
      println("Enter AT for process: " + i);
      at = scala.io.StdIn.readDouble();
      array(i)(0) = at
      array(i)(1) = bt
    }
    One(n, array)
    Two(n, array)
  }
  def One(n: Int, data: Array[Array[Double]]): Unit = {
    var q = 0.0
    var arr = data
    arr = arr.sortBy(x => x(1))
    var arr_copy = arr
    var wt = new Array[Double](n)
    var a = new Array[Double](n)
    var tat = new Array[Double](n)
    var new_tat = new Array[Double](n)
    var new_wt = new Array[Double](n)
    var D_Sum = 0.0
    var sum = 0.0
    for (i <- 0 to n - 1) {
      a(i) = arr(i)(1)
    }
    for (i <- 0 to n - 1) {
      wt(i) = 0
    }
    var tracker = 0.0
    var li = 0
    var updated_TAT = 0.0
    do {
      D_Sum = 0.0
      arr = arr.filterNot(x => x(1) <= 0)
      for (i <- 0 to arr.size - 1) {
        D_Sum = D_Sum + arr(i)(1)
      }
      q = math.round(math.sqrt(arr.length * D_Sum * 1.49))
      for (i <- 0 to arr.size - 1) {
        if (tracker >= arr(i)(0)) {
          if (arr(i)(1) > q) { //if BT > quantum time
            arr(i)(1) -= q
            tracker = tracker + q
            for (j <- 0 to arr.size - 1) {
              if ((j != i) && (arr(j)(1) != 0))
                wt(j) += q
            }
          }
          else {
            for (j <- 0 to arr.size - 1) {
              if ((j != i) && (arr(j)(1) != 0)) {
                wt(j) += arr(i)(1)
              }
            }
            tracker = tracker + arr(i)(1)
            updated_TAT = tracker
            new_tat(li) = updated_TAT - arr_copy(i)(0)
            new_wt(li) = tracker - arr_copy(i)(1) - arr_copy(i)(0)
            li = li + 1
            arr(i)(1) = 0
          }
        }
      }
      sum = 0.0
      for (i <- 0 to arr.length - 1)
        sum = sum + arr(i)(1)
    } while (sum != 0)
    var avg_wt = 0.0
    var avg_tat = 0.0
    for (j <- 0 to n - 1)
      avg_wt += wt(j)
    for (j <- 0 to n - 1)
      avg_tat += new_tat(j)
    println("average waiting time= " + (avg_wt / n) + " Average turn around time= " + (avg_tat / n))
  }
  def Two(n: Int, data: Array[Array[Double]]): Unit = {
    var arr = data
    arr = arr.sortBy(x => x(1))
    var arr_copy = arr
    var q = 0.0
    var wt = new Array[Double](n)
    var a = new Array[Double](n)
    var tat = new Array[Double](n)
    var new_tat = new Array[Double](n)
    var new_wt = new Array[Double](n)
    var sum = 0.0
    for (i <- 0 to n - 1) {
      a(i) = arr(i)(1)
    }
    for (i <- 0 to n - 1) {
      wt(i) = 0
    }
    var tracker = 0.0
    var li = 0
    var updated_TAT = 0.0
    do {
      arr = arr.filterNot(x => x(1) <= 0)
      q = arr(0)(1)
      for (i <- 0 to arr.size - 1) {
        if (tracker >= arr(i)(0)) {
          if (arr(i)(1) > q) { //if BT > quantum time
            arr(i)(1) -= q
            tracker = tracker + q
            for (j <- 0 to arr.size - 1) {
              if ((j != i) && (arr(j)(1) != 0))
                wt(j) += q
            }
          }
          else {
            for (j <- 0 to arr.size - 1) {
              if ((j != i) && (arr(j)(1) != 0)) {
                wt(j) += arr(i)(1)
              }
            }
            tracker = tracker + arr(i)(1)
            updated_TAT = tracker
            new_tat(li) = updated_TAT - arr_copy(i)(0)
            new_wt(li) = tracker - arr_copy(i)(1) - arr_copy(i)(0)
            li = li + 1
            arr(i)(1) = 0
          }
        }
      }
      sum = 0.0
      for (i <- 0 to arr.length - 1)
        sum = sum + arr(i)(1)
    } while (sum != 0)
    var avg_wt = 0.0
    var avg_tat = 0.0
    for (j <- 0 to n - 1)
      avg_wt += wt(j)
    for (j <- 0 to n - 1)
      avg_tat += new_tat(j)
    println("average waiting time= " + (avg_wt / n) + " Average turn around time= " + (avg_tat / n))
  }
}

1 个答案:

答案 0 :(得分:1)

问题在这里:

var arr = data

arr现在只是对data中元素的第二次引用,因此对arr的任何修改(如arr(i)(1) = 0)也都是对{{1}的修改}。

如果您想修改data而又不更改arr,则需要进行深拷贝。像这样:

data

这只是许多很多之一,可变变量(var arr = data.map(_.map(identity)) s和可变数据结构(var s)不好的原因,。好的Scala代码从不(嗯,几乎从不)使用它们。