如何在Java中将Wrapper值数组列表转换为原始数组?
在这里,要将Wrapper值数组列表转换为原始数组,我们考虑将Integer作为Wrapper,而将Double作为原始。
首先,声明一个Integer数组列表并向其中添加元素-
ArrayList < Integer > arrList = new ArrayList < Integer > ();arrList.add(5);
arrList.add(10);
arrList.add(15);
arrList.add(20);
arrList.add(25);
arrList.add(30);
arrList.add(45);
arrList.add(50);
现在,将上述Integer数组列表转换为基本数组。首先,我们为double数组设置相同的大小,然后分配每个值
final double[] arr = new double[arrList.size()];int index = 0;
for (final Integer value: arrList) {
arr[index++] = value;
}
以下是将Integer(包装器)数组列表转换为double数组(原始)的示例-
示例
import java.util.ArrayList;public class Demo {
public static void main(String[] args) {
ArrayList<Integer>arrList = new ArrayList<Integer>();
arrList.add(5);
arrList.add(10);
arrList.add(15);
arrList.add(20);
arrList.add(25);
arrList.add(30);
arrList.add(45);
arrList.add(50);
final double[] arr = new double[arrList.size()];
int index = 0;
for (final Integer value: arrList) {
arr[index++] = value;
}
System.out.println("Elements of double array...");
for (Double i: arr) {
System.out.println(i);
}
}
}
输出
Elements of double array...5.0
10.0
15.0
20.0
25.0
30.0
45.0
50.0
以上是 如何在Java中将Wrapper值数组列表转换为原始数组? 的全部内容, 来源链接: utcz.com/z/327263.html