Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Shouldn't this call to Arrays.asList return a list of Longs? Instead it is returning a List<long[]>

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

The method signature is consistent with the results, the varargs throws the entire array into the list. It's the same as new ArrayList(); list.add(myArray) And yes, I know it's meant to be used like this: Arrays.asList(T t1, T t2, T t3)

I guess what I'm getting at, is instead of the varargs form, why can't I just have my old asList method (at least I think this is how it used to work) that would take the contents and put them individually into a list? Any other clean way of doing this?

like image 732
oreoshake Avatar asked Dec 28 '25 14:12

oreoshake


1 Answers

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?

like image 99
Pyrolistical Avatar answered Dec 31 '25 18:12

Pyrolistical



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!