convert Long[] to long[] (primitive) java

后端 未结 5 714
感情败类
感情败类 2021-02-18 13:13

How do i convert Long[] to long[]? For example i know how to convert if it is not array as below.

long ll = (Long)Object[1].longValue()

But how

5条回答
  •  迷失自我
    2021-02-18 13:19

    There are no standard API method for doing that (how would null-elements be handled?) so you would need to create such a method yourself.

    Something like this (will throw NullPointerException on any object beeing null):

    public static long[] toPrimitives(Long... objects) {
    
        long[] primitives = new long[objects.length];
        for (int i = 0; i < objects.length; i++)
             primitives[i] = objects[i];
    
        return primitives;
    }
    

提交回复
热议问题