gpt4 book ai didi

java - 使用 System.arraycopy 将 double[] 转换为 Double[] 和 Viceversa,Java

转载 作者:行者123 更新时间:2023-11-30 08:48:00 24 4
gpt4 key购买 nike

我需要使用 double[] 数组,但有时我需要使用 Double[]

private Double[] double_Double(double[] ddata) {
Double[] Ddata = new Double[ddata.length];
for (int i = 0; i < ddata.length; i++) {
Ddata[i] = ddata[i];
}
return Ddata;
}
private double[] Double_double(Double[] Ddata) {
double[] ddata = new double[Ddata.length];
for (int i = 0; i < Ddata.length; i++) {
ddata[i] = Ddata[i];
}
return ddata;
}

我可以用 System.arraycopy 开发这个吗?

private Double[] double_Double(double[] ddata) {
Double[] Ddata = new Double[ddata.length];
System.arraycopy(ddata, 0, Ddata, 0, Ddata.length);
return Ddata;
}
private double[] Double_double(Double[] Ddata) {
double[] ddata = new double[Ddata.length];
System.arraycopy(Ddata, 0, ddata, 0, ddata.length);
return ddata;
}

How do I convert Double[] to double[]?没有回答我的问题!

测试此方法的问题是由于小数部分是“无穷大”......

最佳答案

不可以

arrayCopy是执行复制的 native 方法。这是 native 代码。

JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos, 
jobject dst, jint dst_pos, jint length))
JVMWrapper("JVM_ArrayCopy");
// Check if we have null pointers
if (src == NULL || dst == NULL) {
THROW(vmSymbols::java_lang_NullPointerException());
}
arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
assert(Universe::is_heap(s), "JVM_ArrayCopy: src not an oop");
assert(Universe::is_heap(d), "JVM_ArrayCopy: dst not an oop");
// Do copy
Klass::cast(s->klass())->copy_array(s, src_pos, d, dst_pos, length, thread);
JVM_END

so here you will get ArrayStoreException because primitive type double and Object Type DOUBLE unboxing and boxing doesn't happen automatically in arrayCopy.

关于java - 使用 System.arraycopy 将 double[] 转换为 Double[] 和 Viceversa,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163217/

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