Java List to Array toLongList(String[] array)

Here you can find the source of toLongList(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 List<Long> toLongList(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:/*  w  ww  .  j  a  v  a  2 s .  com*/
 *     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.ArrayList;

import java.util.List;

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

    /**
     * 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 List<Long> toLongList(String[] array, long valueForNull) {
        List<Long> out = new ArrayList<Long>(array.length);
        for (String array1 : array)
            out.add(array1 == null ? valueForNull : Long.parseLong(array1));

        return out;
    }
}

Related

  1. toArray(List l)
  2. toArray(List list)
  3. toArray(List list)
  4. toArray(List list)
  5. toLongList(String str, String splitStr)
  6. toObjectArray(List> lists)
  7. toObjectArray(List list)
  8. toObjectArray(List list)
  9. toObjectArrayNative(List L)

  10. HOME | Copyright © www.java2s.com 2016