So I'm trying to make a new Array of different length in a separate method by passing in an array from the main method but I'm having trouble. Essentially what I'm trying to do is make it so my starting array values of
int [] myInches = {89,12,33,7,72,42,76,49,69,85,61,23};
get transferred to my createLowerArray method, and by comparing it to user input maxParam, creates a new array and returns it.
public static int [] createLowerArray(int maxParam, int [] myInchesParam) {
int [] betterInches = {0,0,0,0,0,0,0,0,0,0};
for (int i = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam)
betterInches[i] = myInchesParam[i];
}
return betterInches;
}
So let's say the user inputs "40", it would see if the corresponding elements in myInches/myInchesParam were smaller or not, and if they were, would replace the array I created in that method with it's corresponding value. So since 12, 33, 7, and 23 are the only elements less than 40, it should make an array of length 3 with position 0 being 12, [1] = 12, [2] = 7, and [3] = 23. I know you can't make an array bigger than it already is due to memory issues but It's possible to make one smaller from what it already is right? If that's not possible either than I'd like to know how to get this result as at the moment the array it returns is of the same length as the original with incorrect element positions which is not what I wish to do. Thank you in advance for any help.