Java Array Convert to toLongArray(int[] array)

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

Description

to Long Array

License

Apache License

Declaration

public static Long[] toLongArray(int[] array) 

Method Source Code

//package com.java2s;
/*/*www.jav a2  s . c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static long[] toLongArray(Collection<Long> collection) {
        long[] newArray = new long[collection.size()];

        if (collection instanceof List) {
            List<Long> list = (List<Long>) collection;

            for (int i = 0; i < list.size(); i++) {
                Long value = list.get(i);

                newArray[i] = value.longValue();
            }
        } else {
            int i = 0;

            Iterator<Long> iterator = collection.iterator();

            while (iterator.hasNext()) {
                Long value = iterator.next();

                newArray[i++] = value.longValue();
            }
        }

        return newArray;
    }

    public static Long[] toLongArray(int[] array) {
        Long[] newArray = new Long[array.length];

        for (int i = 0; i < array.length; i++) {
            newArray[i] = (long) array[i];
        }

        return newArray;
    }

    public static Long[] toLongArray(long[] array) {
        Long[] newArray = new Long[array.length];

        for (int i = 0; i < array.length; i++) {
            newArray[i] = array[i];
        }

        return newArray;
    }

    public static Long[] toLongArray(Object[] array) {
        Long[] newArray = new Long[array.length];

        for (int i = 0; i < array.length; i++) {
            newArray[i] = (Long) array[i];
        }

        return newArray;
    }
}

Related

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