Am I missing something, or do varargs break Arrays.asList?

[亡魂溺海] 提交于 2019-12-01 21:44:31

That's because long[] and Long[] are different types.

In the first case T is long[], in the second T is Long.

How to fix this? Don't use long[] in the first place?

Autoboxing cannot be done on arrays. You are allowed to do:

private List<Long> array(final long[] lngs) {
    List<Long> list = new ArrayList<Long>();
    for (long l : lngs) {
        list.add(l);
    }
    return list;
}

or

private List<Long> array(final long[] lngs) {
    List<Long> list = new ArrayList<Long>();
    for (Long l : lngs) {
        list.add(l);
    }
    return list;
}

(notice that the iterable types are different)

e.g.

Long l = 1l;

but not

Long[] ls = new long[]{1l}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!