def random[T](array: Array[(T, Double)]): T = {
var total: Double = 0
array.foreach(x => total += x._2)
if (total > 1)
throw new IllegalArgumentException("The total ratio shouldn't greater than 1.")
val ratio = rand.nextDouble()
var min: Double = 0
var max: Double = 0
var theOne:T = null // error here !!!
array.foreach {
x =>
max += x._2
if (ratio > min && ratio <= max)
theOne = x._1
min += x._2
}
theOne
}
我怎样才能避免这个问题?