Converts to primitive array. - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Converts to primitive array.

Demo Code

// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved.
import java.lang.reflect.Array;
import static jodd.util.StringPool.NULL;

public class Main{
    public static void main(String[] argv) throws Exception{
        byte[] array = new byte[]{34,35,36,37,37,37,67,68,69};
        System.out.println(java.util.Arrays.toString(values(array)));
    }//from   w w  w  . j a  v a 2  s.com
    /**
     * Converts to primitive array.
     */
    public static byte[] values(Byte[] array) {
        byte[] dest = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            Byte v = array[i];
            if (v != null) {
                dest[i] = v.byteValue();
            }
        }
        return dest;
    }
    /**
     * Converts to primitive array.
     */
    public static char[] values(Character[] array) {
        char[] dest = new char[array.length];
        for (int i = 0; i < array.length; i++) {
            Character v = array[i];
            if (v != null) {
                dest[i] = v.charValue();
            }
        }
        return dest;
    }
    /**
     * Converts to primitive array.
     */
    public static short[] values(Short[] array) {
        short[] dest = new short[array.length];
        for (int i = 0; i < array.length; i++) {
            Short v = array[i];
            if (v != null) {
                dest[i] = v.shortValue();
            }
        }
        return dest;
    }
    /**
     * Converts to primitive array.
     */
    public static int[] values(Integer[] array) {
        int[] dest = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            Integer v = array[i];
            if (v != null) {
                dest[i] = v.intValue();
            }
        }
        return dest;
    }
    /**
     * Converts to primitive array.
     */
    public static long[] values(Long[] array) {
        long[] dest = new long[array.length];
        for (int i = 0; i < array.length; i++) {
            Long v = array[i];
            if (v != null) {
                dest[i] = v.longValue();
            }
        }
        return dest;
    }
    /**
     * Converts to primitive array.
     */
    public static float[] values(Float[] array) {
        float[] dest = new float[array.length];
        for (int i = 0; i < array.length; i++) {
            Float v = array[i];
            if (v != null) {
                dest[i] = v.floatValue();
            }
        }
        return dest;
    }
    /**
     * Converts to primitive array.
     */
    public static double[] values(Double[] array) {
        double[] dest = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            Double v = array[i];
            if (v != null) {
                dest[i] = v.doubleValue();
            }
        }
        return dest;
    }
    /**
     * Converts to primitive array.
     */
    public static boolean[] values(Boolean[] array) {
        boolean[] dest = new boolean[array.length];
        for (int i = 0; i < array.length; i++) {
            Boolean v = array[i];
            if (v != null) {
                dest[i] = v.booleanValue();
            }
        }
        return dest;
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(Object[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(String[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(byte[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(char[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(short[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(int[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(long[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(float[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(double[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Converts an array to string. Elements are separated by comma.
     * Returned string contains no brackets.
     */
    public static String toString(boolean[] array) {
        if (array == null) {
            return NULL;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(array[i]);
        }
        return sb.toString();
    }
    /**
     * Appends an element to array.
     */
    public static <T> T[] append(T[] buffer, T newElement) {
        T[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>String</code> array.
     */
    public static String[] append(String buffer[], String newElement) {
        String[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>byte</code> array.
     */
    public static byte[] append(byte buffer[], byte newElement) {
        byte[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>char</code> array.
     */
    public static char[] append(char buffer[], char newElement) {
        char[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>short</code> array.
     */
    public static short[] append(short buffer[], short newElement) {
        short[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>int</code> array.
     */
    public static int[] append(int buffer[], int newElement) {
        int[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>long</code> array.
     */
    public static long[] append(long buffer[], long newElement) {
        long[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>float</code> array.
     */
    public static float[] append(float buffer[], float newElement) {
        float[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>double</code> array.
     */
    public static double[] append(double buffer[], double newElement) {
        double[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Appends an element to <code>boolean</code> array.
     */
    public static boolean[] append(boolean buffer[], boolean newElement) {
        boolean[] t = resize(buffer, buffer.length + 1);
        t[buffer.length] = newElement;
        return t;
    }
    /**
     * Resizes an array.
     */
    public static <T> T[] resize(T[] buffer, int newSize) {
        Class<T> componentType = (Class<T>) buffer.getClass()
                .getComponentType();
        T[] temp = (T[]) Array.newInstance(componentType, newSize);
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>String</code> array.
     */
    public static String[] resize(String buffer[], int newSize) {
        String temp[] = new String[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>byte</code> array.
     */
    public static byte[] resize(byte buffer[], int newSize) {
        byte temp[] = new byte[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>char</code> array.
     */
    public static char[] resize(char buffer[], int newSize) {
        char temp[] = new char[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>short</code> array.
     */
    public static short[] resize(short buffer[], int newSize) {
        short temp[] = new short[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>int</code> array.
     */
    public static int[] resize(int buffer[], int newSize) {
        int temp[] = new int[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>long</code> array.
     */
    public static long[] resize(long buffer[], int newSize) {
        long temp[] = new long[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>float</code> array.
     */
    public static float[] resize(float buffer[], int newSize) {
        float temp[] = new float[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>double</code> array.
     */
    public static double[] resize(double buffer[], int newSize) {
        double temp[] = new double[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
    /**
     * Resizes a <code>boolean</code> array.
     */
    public static boolean[] resize(boolean buffer[], int newSize) {
        boolean temp[] = new boolean[newSize];
        System.arraycopy(buffer, 0, temp, 0,
                buffer.length >= newSize ? newSize : buffer.length);
        return temp;
    }
}

Related Tutorials