让我们首先创建一个浮点数组列表-
ArrayList < Float > arrList = new ArrayList < Float > (); arrList.add(5.2 f); arrList.add(10.3 f); arrList.add(15.3 f); arrList.add(20.4 f);
现在,将float数组列表转换为float数组。首先,我们为浮点数组设置了相同的大小,即元素数相同。之后,我们分配了每个值-
final float[] arr = new float[arrList.size()]; int index = 0; for (final Float value: arrList) { arr[index++] = value; }
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayListarrList = new ArrayList(); arrList.add(5.2f); arrList.add(10.3f); arrList.add(15.3f); arrList.add(20.4f); arrList.add(25.2f); arrList.add(30.6f); arrList.add(45.3f); arrList.add(50.9f); final float[] arr = new float[arrList.size()]; int index = 0; for (final Float value: arrList) { arr[index++] = value; } System.out.println("Elements of float array..."); for (Float i: arr) { System.out.println(i); } } }
输出结果
Elements of float array... 5.2 10.3 15.3 20.4 25.2 30.6 45.3 50.9