Is there a way to declare an array with an unknown length? My problem is to return an int[] of odd integers from a range of numbers. My current output is adding 0s to fill up the remaining space of the array.
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] but I'd like it to be 3,5,7,9 It has to be an array and not an ArrayList. Is this possible? Or is there a complete different approach?