gpt4 book ai didi

java - 装箱/拆箱多维原始数组的最有效方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:20:08 26 4
gpt4 key购买 nike

这是在不使用外部库的情况下在 Java 中装箱/拆箱多维原始数组的最有效方法吗?

private Float[][] toFloatArray(float[][] values)
{
Float[][] objArray = new Float[values.length][values[0].length];

for (int i = 0; i < values.length; i++)
{
for (int j = 0; j < values[0].length; j++)
{
objArray[i][j] = values[i][j];
}
}

return objArray;
}

使用外部库最有效的是什么?

最佳答案

这里没有魔法:当您使用 apache-commons 的库时,它就是这样做的:

public static short[] toPrimitive(Short[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return EMPTY_SHORT_ARRAY;
}
final short[] result = new short[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i].shortValue();
}
return result;
}

source

关于java - 装箱/拆箱多维原始数组的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5553690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com