A much smoother approach would be to fill an array list in the original loop , you can either return the arraylist and convert it to an array before returning it, I believe array or array list has a method that allows this. Check the API.