Java String Array to String arrayStringToArrayInt(final String[] as)

Here you can find the source of arrayStringToArrayInt(final String[] as)

Description

Convert an array of strings to an array of integers.

License

GNU General Public License

Parameter

Parameter Description
as Array of strings to convert

Return

An array of integers

Declaration

public static int[] arrayStringToArrayInt(final String[] as) 

Method Source Code

//package com.java2s;
/*/*from ww  w . j av  a2s. c om*/
 *                      Nividic development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the microarray platform
 * of the ?cole Normale Sup?rieure and the individual authors.
 * These should be listed in @author doc comments.
 *
 * For more information on the Nividic project and its aims,
 * or to join the Nividic mailing list, visit the home page
 * at:
 *
 *      http://www.transcriptome.ens.fr/nividic
 *
 */

public class Main {
    /**
     * Convert an array of strings to an array of integers.
     * @param as Array of strings to convert
     * @return An array of integers
     */
    public static int[] arrayStringToArrayInt(final String[] as) {

        if (as == null)
            return null;

        int[] result = new int[as.length];

        int j = 0;
        for (int i = 0; i < as.length; i++) {

            String s = as[i];
            if (s == null)
                continue;

            try {
                result[j++] = Integer.parseInt(s.trim());
            } catch (NumberFormatException e) {
                continue;
            }
        }

        int[] finalResult = new int[j];

        System.arraycopy(result, 0, finalResult, 0, j);

        return finalResult;
    }
}

Related

  1. array2string(String array[])
  2. array2string(String[] s)
  3. arrayAsString(String[] haystack)
  4. arrayAsString(String[] stringArray)
  5. ArrayCombine(String[] array, String delimiter)
  6. arrayStringtoString(final String[] arrayString)
  7. arrayStringToString(String[] args)