如何在 Java 中将基本类型数组转换为包装器值数组列表?
在这里,为了将包装器值数组列表转换为基本类型数组,我们将 Integer 视为包装器,而 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP