With java 8, you can use a stream to filter out of your array what you don't want. In order to use your minimum and maximum variable inside of the filter method, you have to put them in a final variable. The w that you see in the filter method, is a local variable that represents each element value.
You also don't have to run through your array twice to find the minimum and the maximum. Just use if/else if to set the appropriate min/max.
import java.util.Arrays;
public class StackOverflow {
public static void main(String args[]) {
double[] weights = { 39.5, 34.8, 22.6, 38.7, 25.4, 30.1, 22.6, 41.8, 33.6, 26.2, 27.3 };
double minimum = Double.MAX_VALUE;
double maximum = Double.MIN_VALUE;
for (int i = 0; i < weights.length; i++) {
if (minimum > weights[i])
minimum = weights[i];
else if (maximum < weights[i])
maximum = weights[i];
}
System.out.println("Before: " + Arrays.toString(weights));
final double minimumFilter = minimum;
final double maximumFilter = maximum;
weights = Arrays.stream(weights)
.filter(w -> w != minimumFilter && w != maximumFilter)
.toArray();
System.out.println("After : " + Arrays.toString(weights));
}
}
Result:
Before: [39.5, 34.8, 22.6, 38.7, 25.4, 30.1, 22.6, 41.8, 33.6, 26.2, 27.3]
After : [39.5, 34.8, 38.7, 25.4, 30.1, 33.6, 26.2, 27.3]
// Two 22.6 minimum was removed and one 41.8 maximum was removed.