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

前端 未结 2 1351
眼角桃花
眼角桃花 2021-01-20 18:50
  private void activateRecords(long[] stuff) {
    ...
    api.activateRecords(Arrays.asList(specIdsToActivate));
  }

Shouldn\'t this call to Array

2条回答
  •  心在旅途
    2021-01-20 19:12

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

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

    or

    private List array(final long[] lngs) {
        List list = new ArrayList();
        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}
    

提交回复
热议问题