如何在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 婆婆高寿祝福语大全简短