将包装器的 ArrayList 转换为相应的基元数组 [英] Cast ArrayList of wrappers to corresponding array of primitives

查看:27
本文介绍了将包装器的 ArrayList 转换为相应的基元数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有包装类 ShortArrayList.
添加一些值后,我想获取原始数组,但函数toArray(Object[] array) 似乎没有办法,因为它需要一个Array与包装类.

是否有另一种不使用 for 之类的方法?

解决方案

Apache Commons/Lang 有一个类 ArrayUtils 定义这些方法.

  • 所有名为 toObject() 的方法都从原始数组转换为包装器数组.
  • 所有调用的toPrimitive()从包装对象数组转换为原始数组

我认为,你需要 ArrayUtils 的 toPrimitive()

public static short[] toPrimitive(Short[] array)

<块引用>

将对象 Shorts 数组转换为基元.

I use an ArrayList with the wrapper class Short.
After adding some values I want to get the primitive array, but it seems that there is no way with the function toArray(Object[] array), because it need an Array with the wrapper class.

Is there another way without using a for or anything like that?

解决方案

Apache Commons / Lang has a class ArrayUtils that defines these methods.

  • All methods called toObject() convert from primitive array to wrapper array.
  • All called toPrimitive() convert from wrapper object array to primitive array

I think, you need ArrayUtils's toPrimitive()

public static short[] toPrimitive(Short[] array)

Converts an array of object Shorts to primitives.

这篇关于将包装器的 ArrayList 转换为相应的基元数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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