mercredi 4 mars 2015

Why does my array add zeros on the end?


def filter(data : Array[Int]) : Array[Int] = {


var list: List[Int] = List();
var index = 0;

for(index <- data){
if(index % 2 == 0){
list.add(index);
}
}
var myArray = new Array[Int](list.length);
index = 0;
for(index <- list){
myArray(index) = index;
}

return myArray;


}


The array is meant to take data and filter all even numbers and return it, when i return it, it has lots of zeros added into the array. Please help? Thank you


Aucun commentaire:

Enregistrer un commentaire