我正在尝试将 int 数组的内容复制到 double 类型的数组中。我必须先施放它们吗?
我成功地将一个int类型的数组复制到另一个int类型的数组中。
但是现在我想编写代码,将内容从数组 A
复制到数组 Y
(int 到 double)。
这是我的代码:
public class CopyingArraysEtc {
public void copyArrayAtoB() {
double[] x = {10.1,33,21,9},y = null;
int[] a = {23,31,11,9}, b = new int[4], c;
System.arraycopy(a, 0, b, 0, a.length);
for (int i = 0; i < b.length; i++)
{
System.out.println(b[i]);
}
}
public static void main(String[] args) {
//copy contents of Array A to array B
new CopyingArraysEtc().copyArrayAtoB();
}
}
最佳答案
值得一提的是,在当今时代,Java 8 提供了一种优雅的单行代码,无需使用第三方库即可完成此操作:
int[] ints = {23, 31, 11, 9};
double[] doubles = Arrays.stream(ints).asDoubleStream().toArray();
关于java - 将 int 数组的内容复制到 Java 中的 double 组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12729139/