Java AtomicInteger count(byte[] array, byte value)

Here you can find the source of count(byte[] array, byte value)

Description

Returns the number of the elements in the specified array that equal the specified value.

License

Open Source License

Parameter

Parameter Description
array an array of bytes.
value a byte value.

Exception

Parameter Description
NullPointerException if array==null.

Return

the number of the elements in the specified array that equal the specified value.

Declaration

public static int count(byte[] array, byte value) 

Method Source Code

//package com.java2s;
/*//  w ww.  j a  va2 s  .  c o m
 * Copyright (C) 2014 Brian L. Browning
 *
 * This file is part of Beagle
 *
 * Beagle is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Beagle is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.concurrent.atomic.AtomicIntegerArray;

public class Main {
    /**
     * Returns the number of the elements in the specified array that
     * equal the specified value.
     * @param array an array of bytes.
     * @param value a byte value.
     * @return the number of the elements in the specified array that
     * equal the specified value.
     *
     * @throws NullPointerException if {@code array==null}.
     */
    public static int count(byte[] array, byte value) {
        int cnt = 0;
        for (byte i : array) {
            if (i == value) {
                ++cnt;
            }
        }
        return cnt;
    }

    /**
     * Returns the number of the elements in the specified array that
     * equal the specified value.
     * @param array an array of integers.
     * @param value an integer value.
     * @return the number of the elements in the specified array that
     * equal the specified value.
     *
     * @throws NullPointerException if {@code array==null}.
     */
    public static int count(int[] array, int value) {
        int cnt = 0;
        for (int i : array) {
            if (i == value) {
                ++cnt;
            }
        }
        return cnt;
    }

    /**
     * Returns the number of the elements in the specified array that
     * equal the specified value.
     * @param array an array of floats.
     * @param value a float value.
     * @return the number of the elements in the specified array that
     * equal the specified value.
     *
     * @throws IllegalArgumentException if {@code Float.isNaN(value)==true}
     * @throws NullPointerException if {@code array==null}
     */
    public static int count(float[] array, float value) {
        if (Float.isNaN(value)) {
            throw new IllegalArgumentException(String.valueOf(value));
        }
        int cnt = 0;
        for (float f : array) {
            if (f == value) {
                ++cnt;
            }
        }
        return cnt;
    }

    /**
     * Returns the number of the elements in the specified array that
     * equal the specified value.
     * @param array an array of integers.
     * @param value an integer value.
     * @return the number of the elements in the specified array that
     * equal the specified value.
     *
     * @throws NullPointerException if {@code array==null}
     */
    public static int count(AtomicIntegerArray array, int value) {
        int cnt = 0;
        for (int j = 0, n = array.length(); j < n; ++j) {
            if (array.get(j) == value) {
                ++cnt;
            }
        }
        return cnt;
    }
}

Related

  1. addNodeUse(String nodeInfo)
  2. bitwiseOrAndGet(final AtomicInteger ai, final int toOrValue)
  3. count()
  4. countDownToZero(AtomicInteger counter)
  5. countOccurrences(final Collection collection)
  6. create()
  7. createIdentifier(Class clazz)