checks every double[] in the passed list of arrays for occurrences of the passed value, removes them and returns the newly created list of double arrays - Android java.lang

Android examples for java.lang:array element remove

Description

checks every double[] in the passed list of arrays for occurrences of the passed value, removes them and returns the newly created list of double arrays

Demo Code


//package com.java2s;
import java.util.ArrayList;

public class Main {
    /**/*  ww  w.ja va2 s  .co  m*/
     * checks every double[] in the passed list of arrays for occurrences of the passed value,
     * removes them and returns the newly created list of double arrays
     * @param _arr the list of arrays to remove values from
     * @param _val the values to remove from the arrays in the list
     * @return a list containing the arrays without the passed value
     */
    public static ArrayList<double[]> removeValues(
            ArrayList<double[]> _arr, double _val) {
        ArrayList<double[]> res = new ArrayList<double[]>();

        for (double[] arr : _arr) {
            res.add(removeValues(arr, _val));
        }
        return res;
    }

    /**
     * checks the passed double array for occurrences of the passed value and removes them
     * @param _arr the array to remove values from
     * @param _value the value to remove
     * @return an array without the passed value
     */
    private static double[] removeValues(double[] _arr, double _value) {
        double[] res = new double[_arr.length
                - countOccurrence(_arr, _value)];
        int resIdx = 0;
        for (double d : _arr) {
            if (d != _value) {
                res[resIdx] = d;
                resIdx++;
            }
        }
        return res;
    }

    /**
     * counts the number of occurrences of the passed value in the passed array
     * @param _arr the array to count the occurrences
     * @param _value the value to count
     * @return the number of occurrences of the passed value in the passed array
     */
    private static int countOccurrence(double[] _arr, double _value) {
        int res = 0;
        for (double d : _arr) {
            if (d == _value) {
                res++;
            }
        }
        return res;
    }
}

Related Tutorials