将对象数组转换为其原始类型数组 [英] Converting an array of objects to an array of their primitive types

查看:37
本文介绍了将对象数组转换为其原始类型数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有一组具有原始类型(例如字节、整数、字符等)的 Java 对象.有没有一种巧妙的方法可以将其转换为原始类型的数组?尤其是无需创建新数组并循环遍历内容即可完成此操作.

If you have an array of Java objects which have a primitive type (for example Byte, Integer, Char, etc). Is there a neat way I can convert it into an array of the primitive type? In particular can this be done without having to create a new array and loop through the contents.

例如,给定

Integer[] array

将其转换为最简洁的方法是什么

what is the neatest way to convert this into

int[] intArray

不幸的是,在 Hibernate 和一些我们无法控制的第三方库之间进行交互时,这是我们必须经常做的事情.这似乎是一个很常见的操作,所以如果没有捷径,我会感到惊讶.

Unfortunately, this is something we have to do quite frequently when interfacing between Hibernate and some third party libraries over which we have no control. It seems this would be a quite common operation so I would be surprised if there's no shortcut.

感谢您的帮助!

推荐答案

不幸的是,Java 平台中没有任何东西可以做到这一点.顺便说一句,您还需要显式处理 Integer[] 数组中的 null 元素(您将使用什么 int 来处理这些元素?).

Unfortunately, there's nothing in the Java platform that does this. Btw, you also need to explicitly handle null elements in the Integer[] array (what int are you going to use for those?).

这篇关于将对象数组转换为其原始类型数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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