我正在尝试使用以下代码制作一个简单的前十五名:
var top = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
val numbers = Array(4,5,8,1,33,23,45,6,11,10,87,46,43,66,55,98,78,71,19,20)
def getTop() : Unit = {
for ( i <- 0 to (numbers.length - 1)) {
set(0, numbers(i), top)
}
top.map(x => println(x))
}
def set(index: Int, number: Int, top: Array[Int]) : Unit = {
if (index <= top.length-1) {
if(top(index) < number) {
top(index) = number
}
else {
set(index+1, number, top)
}
}
}
该函数应将数组中的前十五个元素复制numbers
到 array top
,但目前我只得到:
98,78,71,20,0,0,0,0,0,0,0,0,0,0,0
我究竟做错了什么?