Java如何将原始数组转换为对象数组?
要将原始数组转换为对象类型数组,我们可以使用ApacheCommonsLang库。commonslang提供了一个进行ArrayUtils此转换的类。要转换为其他方式,只需使用toPrimitive()方法。
package org.nhooo.example.commons.lang;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayPrimitiveObjectConversionDemo {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
boolean[] booleans = {true, false, false, true};
float[] decimals = {10.1f, 3.14f, 2.17f};
Integer[] numbersObjects = ArrayUtils.toObject(numbers);
Boolean[] booleansObjects = ArrayUtils.toObject(booleans);
Float[] decimalsObjects = ArrayUtils.toObject(decimals);
numbers = ArrayUtils.toPrimitive(numbersObjects);
booleans = ArrayUtils.toPrimitive(booleansObjects);
decimals = ArrayUtils.toPrimitive(decimalsObjects);
}
}Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=org/apache/commons/commons-lang3/3.9/commons-lang3-3.9.jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短