How to detect outliers in an ArrayList

后端 未结 8 1036
旧时难觅i
旧时难觅i 2021-01-14 02:54

I\'m trying to think of some code that will allow me to search through my ArrayList and detect any values outside the common range of \"good values.\"

Example: 100 1

8条回答
  •  忘掉有多难
    2021-01-14 03:56

    It is just a very simple implementation which fetches the information which numbers are not in the range:

    List notInRangeNumbers = new ArrayList();
    for (Integer number : numbers) {
        if (!isInRange(number)) {
            // call with a predefined factor value, here example value = 5
            notInRangeNumbers.add(number, 5);
        }
    }
    

    Additionally inside the isInRange method you have to define what do you mean by 'good values'. Below you will find an examplary implementation.

    private boolean isInRange(Integer number, int aroundFactor) {
       //TODO the implementation of the 'in range condition'
       // here the example implementation
       return number <= 100 + aroundFactor && number >= 100 - aroundFactor;
    }
    

提交回复
热议问题