Java Array Convert to toLongArray(String[] array)

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

Description

Converts a String array to a long array.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

New long array

Declaration

public static long[] toLongArray(String[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors://from  w  w  w.j  av a2s .  c  o m
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import java.util.Collection;

public class Main {
    /**
     * Converts a collection of {@code String} objects to a {@code long} array.
     *
     * @param collection Input collection
     * @return New {@code long} array
     * 
     */
    public static long[] toLongArray(Collection<String> collection) {
        int i = 0;
        int N = collection.size();
        long[] newArray = new long[N];
        for (String item : collection) {
            newArray[i] = Long.parseLong(item);
            i++;
        }

        return newArray;
    }

    /**
     * Converts a collection of {@code String} objects to a {@code long} array.
     *
     * @param collection Input collection
     * @param valueForNull Value for {@code null} positions
     * @return New {@code long} array
     * 
     */
    public static long[] toLongArray(Collection<String> collection, long valueForNull) {
        int i = 0;
        int N = collection.size();
        long[] newArray = new long[N];
        for (String item : collection) {
            newArray[i] = item == null ? valueForNull : Long.parseLong(item);
            i++;
        }

        return newArray;
    }

    /**
     * Converts a {@code String} array to a {@code long} array.
     *
     * @param array Input array
     * @return New {@code long} array
     * 
     */
    public static long[] toLongArray(String[] array) {
        long[] newArray = new long[array.length];
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                throw new RuntimeException("Null value in position " + i);
            }
            newArray[i] = Long.parseLong(array[i]);
        }
        return newArray;
    }

    /**
     * Converts a {@code String} array to a {@code long} array.
     *
     * @param array Input array
     * @param valueForNull Value for {@code null} positions
     * @return New {@code long} array
     * 
     */
    public static long[] toLongArray(String[] array, long valueForNull) {
        long[] newArray = new long[array.length];
        for (int i = 0; i < array.length; i++) {
            newArray[i] = array[i] == null ? valueForNull : Long.parseLong(array[i]);
        }
        return newArray;
    }
}

Related

  1. toFloatArray(final double[] doubles)
  2. toInt(Integer[] arr)
  3. toIntArray(double[] a)
  4. toIntArray(Integer[] data)
  5. toLongArray(int[] array)