Is there a way to declare an array with an unknown length? My task is to return int [] of odd integers from a range of numbers. My current output adds 0s to fill the remaining array space.
public class Practice {
static int[] oddNumbers(int minimum, int maximum) {
int[] arr = new int[10];
int x = 0;
int count = 0;
for(int i = minimum; i <= maximum; i++){
if(i % 2 != 0){
arr[x] = i;
++x;
}
}
return arr;
}
public static void main(String[] args) {
int min = 3, max = 9;
System.out.println(Arrays.toString(oddNumbers(min, max)));
}
}
My current output is [3,5,7,9,0,0,0,0,0,0,0,0], but I would like it to be 3,5,7,9 It should be an array, not an ArrayList. Is it possible? Or is there a completely different approach?
source
share