如何在 Java 中将整数数组列表转换为浮点数组?
java 8object oriented programmingprogramming
要将整数数组列表转换为浮点数组,我们首先创建一个整数数组列表 −
ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(25); arrList.add(50); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);
现在,将整数数组列表转换为浮点数组。我们首先将大小设置为浮点数组。这样,整数数组的每个值都分配给浮点数组 −
final float[] arr = new float[arrList.size()]; int index = 0; for (final Integer value: arrList) { arr[index++] = value; }
示例
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<Integer>arrList = new ArrayList<Integer>(); arrList.add(25); arrList.add(50); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500); final float[] arr = new float[arrList.size()]; int index = 0; for (final Integer value: arrList) { arr[index++] = value; } System.out.println("Elements of float array..."); for (Float i: arr) { System.out.println(i); } } }
输出
Elements of float array... 25.0 50.0 100.0 200.0 300.0 400.0 500.0