我是否错过了某些东西,或者做了可变参数分析Arrays.asList? [英] Am I missing something, or do varargs break Arrays.asList?

查看:110
本文介绍了我是否错过了某些东西,或者做了可变参数分析Arrays.asList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

不应该对Arrays.asList的这个调用返回一个 S'相反,它返回一个 List< long []>

  public静态< T>列表与LT; T> asList(T ... a)

方法签名与结果一致,可变参数引发整个数组放入列表中。它与 new ArrayList()相同。 list.add(myArray)是的,我知道它的用意是这样的: Arrays.asList(T t1,T t2,T t3)

我猜我在做什么,而不是可变参数形式,为什么我不能只有我的旧asList方法(至少我认为这是它是如何工作的)将会把内容放到一个列表中?任何其他干净这样做的方式?

解决方案

这是因为long []和Long []是不同的类型。

在第一种情况下T是long [],在第二个T中是Long。



如何解决这个问题?首先不要使用long []?

  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?

解决方案

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?

这篇关于我是否错过了某些东西,或者做了可变参数分析Arrays.asList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆