Java Primitive Type Create toPrimitive(Integer[] array)

Here you can find the source of toPrimitive(Integer[] array)

Description

Converts a Integer array to an int array.

License

Open Source License

Parameter

Parameter Description
array the input array, not null

Exception

Parameter Description
NullPointerException if array contains null

Return

the output array, not null

Declaration

public static int[] toPrimitive(Integer[] array) 

Method Source Code

//package com.java2s;
/**/*w  w w. j  a v a 2s. c o  m*/
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */

public class Main {
    /**
     * An empty array.
     */
    public static final int[] EMPTY_INT_ARRAY = new int[0];
    /**
     * An empty array.
     */
    public static final long[] EMPTY_LONG_ARRAY = new long[0];
    /**
     * An empty array.
     */
    public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];

    /**
     * Converts a {@code Integer} array to an {@code int} array.
     *
     * @param array  the input array, not null
     * @return the output array, not null
     * @throws NullPointerException if array contains null
     */
    public static int[] toPrimitive(Integer[] array) {
        if (array.length == 0) {
            return EMPTY_INT_ARRAY;
        }
        int[] result = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i];
        }
        return result;
    }

    /**
     * Converts a {@code Long} array to a {@code long} array.
     *
     * @param array  the input array, not null
     * @return the output array, not null
     * @throws NullPointerException if array contains null
     */
    public static long[] toPrimitive(Long[] array) {
        if (array.length == 0) {
            return EMPTY_LONG_ARRAY;
        }
        long[] result = new long[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i];
        }
        return result;
    }

    /**
     * Converts a {@code Double} array to a {@code double} array.
     *
     * @param array  the input array, not null
     * @return the output array, not null
     * @throws NullPointerException if array contains null
     */
    public static double[] toPrimitive(Double[] array) {
        if (array.length == 0) {
            return EMPTY_DOUBLE_ARRAY;
        }
        double[] result = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i];
        }
        return result;
    }
}

Related

  1. toPrimitive(Double[] vals)
  2. toPrimitive(final Boolean b)
  3. toPrimitive(final Character value, final char defaultValue)
  4. toPrimitive(final Double[] arr)
  5. toPrimitive(Float[] floats)
  6. toPrimitive(Integer[] IntArray)
  7. toPrimitive(Integer[] ints)
  8. toPrimitive(Integer[] irri)
  9. toPrimitive(Long[] array)