简体   繁体   English

长 [] 到长 [] 反之亦然?

[英]Long[] to long[] and vice versa?

Does anybody know an efficient, quick and clean way to convert Long[] to long[] and vice versa in Java outside the obvious for-loop with (un)box&copy?有没有人知道在带有(un)box&copy 的明显 for 循环之外的 Java 中将Long[]转换为long[]和反之亦然的有效、快速和干净的方法?

Note: This is not about unboxing Long or boxing long .注意:这与取消装箱Long或装箱long无关 I am explicitly talking about arrays!我明确地谈论数组!

"There is no better way than loop and copy" would also be an answer ;) “没有比循环和复制更好的方法”也是一个答案;)

EDIT: I am aware of the implications of doing the above and that it is better to avoid it.编辑:我知道执行上述操作的含义,最好避免这样做。 Just assume, I cannot avoid it and want to do it as smartly as possible without using 3rd party stuff.假设,我无法避免它,并且希望在不使用 3rd 方的东西的情况下尽可能聪明地做到这一点。

如果您使用apache commons lang ,您可以执行以下操作:

long[] primitive = ArrayUtils.toPrimitive(new Long[]{1L, 2L, 3L, 4L});

There is no better way than loop and copy没有比循环和复制更好的方法了

The best solution is to avoid converting in the first place.最好的解决方案是首先避免转换。 If you are worries about efficiency avoid using Long[] as it is many times larger than long[] If you need to use a collection you can try TLongArrayList which wraps a long[].如果您担心效率,请避免使用Long[] ,因为它比long[]大很多倍。如果您需要使用集合,您可以尝试包装 long[] 的 TLongArrayList。

When Java boxes/unboxes, it actually DOES something, even if it doesn't show it to the user.当 Java 装箱/拆箱时,它实际上做了一些事情,即使它没有向用户显示它。

Long[] and long[] are very different objects. Long[]long[]是非常不同的对象。 So the answer is, no, you have to loop and copy.所以答案是,不,你必须循环和复制。 But a better question is, why do you need this?但更好的问题是,你为什么需要这个? You can use them interchangeably in almost any situation... You must decide at implementation what's more important to you, object creation, or memory usage.您几乎可以在任何情况下交替使用它们……您必须在实现时决定什么对您更重要,对象创建或内存使用。 Hopefully you use Long.valueOf to create your Long s (or autoboxing) to use Java's caching for Long values.希望您使用Long.valueOf创建您的Long s(或自动装箱)以使用 Java 对Long值的缓存。

This is the source of apache common lang 's ArrayUtils.toPrimitive .这是apache common langArrayUtils.toPrimitive的来源。 It uses a loop.它使用循环。

public static long[] toPrimitive(Long[] array) {
  if (array == null) {
    return null;
  } else if (array.length == 0) {
    return EMPTY_LONG_ARRAY;
  }
  final long[] result = new long[array.length];
  for (int i = 0; i < array.length; i++) {
    result[i] = array[i].longValue();
  }
  return result;
}

Java 本身不提供将对象数组转换为预置数组的任何方法。

ArrayUtil Reference Hope this may help. ArrayUtil 参考希望这可能会有所帮助。

long[] longs = new long[]{1L};
Long[] longObjects = ArrayUtils.toObject(longs);
List<Long> longList = java.util.Arrays.asList(longObjects);

没有比循环和复制更好的方法了,包裹在一个可以反复使用的辅助函数中

You can do it with ArrayUtils from apache as detailed here:您可以使用 Apache 中的 ArrayUtils 来执行此操作,如下所述:

Convert an array of primitive longs into a List of Longs 将原始 long 数组转换为 Long 列表

I can see no one answered for vice versa ie long[] to Long[], here is the simple technique, none other than looping !我可以看到没有人回答反之亦然,即 long[] 到 Long[],这是简单的技术,除了循环!

 int index=0;
 long prim[]={2,100,12,1};
 Long obj[]=new Long[prim.length];
 for(long x:prim){
     res[index++]=x;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM