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

  private void activateRecords(long[] stuff) {
    ...
    api.activateRecords(Arrays.asList(specIdsToActivate));
  }

Should this call to Arrays.asList return a list of Longs? Instead, it returnsList<long[]>

public static <T> List<T> asList(T... a)

The signature of the method is consistent with the results, varargs throws the entire array into a list. This is the same as new ArrayList(); list.add(myArray)And yes, I know that it needs to be used as follows:Arrays.asList(T t1, T t2, T t3)

I assume that I get, instead of the varargs form, why can't I just use my old asList method (at least I think it worked that way) to take the contents and put them individually in a list? Any other clean way to do this?

+3
source share
2 answers

, long [] Long [] .

T [], T Long.

? long [] ?

+7

. :

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

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

( , )

.

Long l = 1l;

Long[] ls = new long[]{1l}
+5

Source: https://habr.com/ru/post/1699496/


All Articles