如何在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
热门推荐
10 圣诞贺卡祝福语简短高级
11 团队虎年祝福语简短霸气
12 小孩生日祝福语简短励志
13 对好友的祝福语简短
14 升职祝福语简短霸气押韵
15 10岁高考祝福语简短
16 男生儿子生日祝福语简短
17 新婚祝福语英语简短精辟
18 朋友的孩子祝福语简短