Java Short Number Array Create toShortArray(boolean[] array)

Here you can find the source of toShortArray(boolean[] array)

Description

Coverts given booleans array to array of shorts.

License

Open Source License

Parameter

Parameter Description
array booleans array to convert.

Return

converted array of shorts.

Declaration

public static short[] toShortArray(boolean[] array) 

Method Source Code

//package com.java2s;
/*//  w  ww  .jav a2  s. c o m
 * The MIT License (MIT)
 *
 * Copyright (c) 2016. Diorite (by Bart?omiej Mazur (aka GotoFinal))
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

public class Main {
    /**
     * Coverts given booleans array to array of shorts.
     *
     * @param array
     *         booleans array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(boolean[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i] ? (short) 1 : (short) 0;
        }
        return result;
    }

    /**
     * Coverts given bytes array to array of shorts.
     *
     * @param array
     *         bytes array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(byte[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (short) array[i];
        }
        return result;
    }

    /**
     * Coverts given chars array to array of shorts.
     *
     * @param array
     *         chars array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(char[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (short) array[i];
        }
        return result;
    }

    /**
     * Coverts given ints array to array of shorts.
     *
     * @param array
     *         ints array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(int[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (short) array[i];
        }
        return result;
    }

    /**
     * Coverts given longs array to array of shorts.
     *
     * @param array
     *         longs array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(long[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (short) array[i];
        }
        return result;
    }

    /**
     * Coverts given floats array to array of shorts.
     *
     * @param array
     *         floats array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(float[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (short) array[i];
        }
        return result;
    }

    /**
     * Coverts given doubles array to array of shorts.
     *
     * @param array
     *         doubles array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(double[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (short) array[i];
        }
        return result;
    }

    /**
     * Coverts given shorts array to array of shorts.
     *
     * @param array
     *         shorts array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(Short[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i];
        }
        return result;
    }

    /**
     * Coverts given numbers array to array of bytes.
     *
     * @param array
     *         numbers array to convert.
     *
     * @return converted array of bytes.
     */
    public static short[] toShortArray(Number[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].shortValue();
        }
        return result;
    }

    /**
     * Coverts given booleans array to array of shorts.
     *
     * @param array
     *         booleans array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(Boolean[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i] ? (short) 1 : (short) 0;
        }
        return result;
    }

    /**
     * Coverts given bytes array to array of shorts.
     *
     * @param array
     *         bytes array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(Byte[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].shortValue();
        }
        return result;
    }

    /**
     * Coverts given chars array to array of shorts.
     *
     * @param array
     *         chars array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(Character[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            //noinspection UnnecessaryUnboxing
            result[i] = ((short) array[i].charValue());
        }
        return result;
    }

    /**
     * Coverts given ints array to array of shorts.
     *
     * @param array
     *         ints array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(Integer[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].shortValue();
        }
        return result;
    }

    /**
     * Coverts given longs array to array of shorts.
     *
     * @param array
     *         longs array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(Long[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].shortValue();
        }
        return result;
    }

    /**
     * Coverts given floats array to array of shorts.
     *
     * @param array
     *         floats array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(Float[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].shortValue();
        }
        return result;
    }

    /**
     * Coverts given doubles array to array of shorts.
     *
     * @param array
     *         doubles array to convert.
     *
     * @return converted array of shorts.
     */
    public static short[] toShortArray(Double[] array) {
        short[] result = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].shortValue();
        }
        return result;
    }
}

Related

  1. toShorts(byte[] array, int offset, int length)
  2. toShorts(byte[] bytes)
  3. toShorts(byte[] readBuffer, int o, int l)
  4. toShorts(byte[] value, int offset, int num)