I've got a problem to solve as follows:
Create a method that returns an array that contains only the positive values of another
int[] a
I've kinda solved it by writing this method:
public static int[] soloPositivi(int[] a) {
int[] pos = new int[a.length];
int j=0;
for (int i=0; i<a.length; i++){
if (a[i]>0) {
pos[j] = a[i];
j++;
}
}
return pos;
}
of course when I test it using for example:
int[] a = new int[] {2,-3,0,7,11};
I get [2,7,11,0,0]
, because I set:
int[] pos = new int[a.length];
The question is, how do I get the pos array to have a modular length so that I can get [2,7,11]
, without having to use lists? I have to solve this problem by only using Arrays methods.