Java Long Number Array Create toLongArray(String array)

Here you can find the source of toLongArray(String array)

Description

Cast a string to an array of longs

String should have the format "[1,2,3]".

License

Open Source License

Parameter

Parameter Description
array the string to be cast

Return

an array containing the parsed longs

Declaration

public static long[] toLongArray(String array) 

Method Source Code

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

public class Main {
    /**//from  w  w  w.  j  a  v a  2s  . com
     * Cast a string to an array of longs
     * <p>
     * String should have the format <code>"[1,2,3]"</code>.
     * 
     * @param array
     *            the string to be cast
     * @return an array containing the parsed longs
     */
    public static long[] toLongArray(String array) {
        // Empty array []
        if (array.length() == 2)
            return new long[0];

        String[] cast = array.substring(1, array.length() - 1).split(",");

        long[] result = new long[cast.length];

        for (int i = 0; i < cast.length; i++) {
            result[i] = Long.valueOf(cast[i]);
        }

        return result;
    }
}

Related

  1. toLongArray(final int[] in)
  2. toLongArray(final long[] longs)
  3. toLongArray(int... coordinates)
  4. toLongArray(Number[] array)
  5. toLongArray(Object[] arr)
  6. toLongArray(String str, String separator)
  7. toLongArray(String[] anArray)