Java Array Shorten toShortArray(Object[] array)

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

Description

to Short Array

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static short[] toShortArray(Object[] array) {
        if (array == null)
            return null;
        short[] ret = new short[array.length];
        for (int i = 0; i < array.length; i++)
            ret[i] = parseShort(array[i]);
        return ret;
    }// w w w.  j a  va 2 s . co  m

    public static short parseShort(Object o) {
        if (o == null)
            return 0;
        if (o instanceof Number)
            return ((Number) o).shortValue();
        return parseShort(o.toString());
    }

    public static short parseShort(String str) {
        try {
            if (str == null)
                return 0;
            str = str.trim();
            if (str.startsWith("+"))
                str = str.substring(1);
            int o = str.indexOf('.');
            if (o >= 0)
                str = str.substring(0, o);
            return Short.parseShort(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    public static String toString(short[] arr) {
        StringBuffer sb = new StringBuffer("[");
        for (int i = 0; i < arr.length; i++) {
            if (i > 0)
                sb.append(", ");
            sb.append(arr[i]);
        }
        sb.append("]");
        return sb.toString();
    }
}

Related

  1. toShortArray(byte[] data)
  2. toShortArray(double[][] array)
  3. toShortArray(final byte[] array)
  4. toShortArray(long value, int length)
  5. toShortArray(Number[] array)
  6. toShortArray(String str, String separator)
  7. toShortArray(String[] anArray)