让我们首先创建一个浮点数组列表-ArrayListarrList=newArrayList();
arrList.add(5.2f);
arrList.add(10.3f);
arrList.add(15.3f);
arrList.add(20.4f);
现在,将float数组列表转换为float数组。首先,我们为浮点数组设置了相同的大小,即元素数相同。之后,我们分配了每个值-finalfloat[]arr=newfloat[arrList.size()];
intindex=0;
for(finalFloatvalue:arrList){
arr[index++]=value;
}
示例importjava.util.ArrayList;
publicclassDemo{
publicstaticvoidmain(String[]args){
ArrayListarrList=newArrayList();
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);
finalfloat[]arr=newfloat[arrList.size()];
intindex=0;
for(finalFloatvalue:arrList){
arr[index++]=value;
}
System.out.println("Elementsoffloatarray...");
for(Floati:arr){
System.out.println(i);
}
}
}
输出结果Elementsoffloatarray...
5.2
10.3
15.3
20.4
25.2
30.6
45.3
50.9