如何在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 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短