首页 > arrays > 将相同的数据传递给Scala中的两个或多个方法

将相同的数据传递给Scala中的两个或多个方法 (Passing same data to two or more methods in Scala)

2019-03-09 arraysscala

问题

我试图在main方法中调用两个方法并将相同的数据传递给它们。这里的第二个参数是一个具有n行和2列的二维数组。第一种方法成功执行,但当调用转到具有相同数据的第二种方法时,数组变为空(所有企业设置为零)。我想将相同的数据(数组)传递给两个方法。注意,第一个参数(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))
  }
}

解决方法

问题出在这里:

var arr = data

arr现在只是对元素的第二个引用data,因此对arr(like arr(i)(1) = 0)的任何修改也都是对它们的修改data

如果您想在arr不改变的情况下进行修改,则data需要进行深层复制。像这样的东西:

var arr = data.map(_.map(identity))

这只是可变变量和可变数据结构坏,坏的众多原因之一。好的Scala代码永远不会(好吧,几乎从不)使用它们。varArray

问题

I am trying to call two methods in main method and passing same data to both. Here second parameter is a two dimensional array with n rows and 2 columns. First method executes successfully but When call goes to second method with same data, array becomes null (all enteries set's to zero). I want to pass same data(array) to both methods. Note that value of first parameter (n) remains same. How can I pass same data to both methods? Code is given below.

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))
  }
}

解决方法

The problem is here:

var arr = data

arr is now a just a 2nd reference to the elements in data, so any modifications to arr (like arr(i)(1) = 0) are also modifications to data.

If you want to modify arr without changing data you'll need to make a deep copy. Something like this:

var arr = data.map(_.map(identity))

This is just one of the many, many, reasons why mutable variables (vars) and mutable data structures (Arrays) are bad, bad, bad. Good Scala code never (well, almost never) uses them.

相似信息