在这里,要将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