2
The method takes two numbers per parameter, and returns one array with the odd numbers between these two numbers.
public static int[] oddNumbers(int l, int r) {
int odd[] = new int[r];
for(int i=0; i<odd.length;i++){
if(l<=r && l%2!=0){
odd[i]=l;
} l++;
}
return odd;
}
However, it is returning the zeros in the array, and is to return only the odd.
At the moment, if put oddNumbers(2,5)
, the result will be: 0 3 0 5 0
How do I return only filled positions? 3 5
Yes, because an integer array is started with all fields like 0. The ones you do not fill continue with this value.
– user28595
Yes, yes. But is there any way to return only the filled fields?
– Daniel
Creating an array only with the amount of values you need to return or using Arraylist.
– user28595
You want to return one array that returns the odd between
l
andr
, that’s it?– Maniero
@Daniel filled in with that?
– Maniero
@exact bigown, an array that returns the odd between l and r.
– Daniel