转换原始多头阵列成多头列表 [英] Convert an array of primitive longs into a List of Longs
问题描述
这可能是一个有点简单,headdesk这类问题的,但我第一次尝试令人惊讶的完全未能奏效。我想采取原始多头的数组,并把它变成一个列表,我试图这样做:
This may be a bit of an easy, headdesk sort of question, but my first attempt surprisingly completely failed to work. I wanted to take an array of primitive longs and turn it into a list, which I attempted to do like this:
long[] input = someAPI.getSomeLongs();
List<Long> inputAsList = Arrays.asList(input); //Total failure to even compile!
什么是做到这一点的正确方法?
What's the right way to do this?
推荐答案
我发现它方便使用Apache郎ArrayUtils做(<一个href=\"http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html#toObject(long[])\">JavaDoc)
I found it convenient to do using apache commons lang ArrayUtils (JavaDoc)
long[] input = someAPI.getSomeLongs();
Long[] inputBoxed = ArrayUtils.toObject(input);
List<Long> inputAsList = Arrays.asList(inputBoxed);
它也有反向的API
it also has the reverse API
long[] backToPrimitive = ArrayUtils.toPrimitive(objectArray);
编辑:更新,以提供完整的信号转换到列表中的意见和其他修复的建议
updated to provide a complete conversion to a list as suggested by comments and other fixes.
这篇关于转换原始多头阵列成多头列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!