- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert Float array list to float array in Java?
Let us first create a float array list −
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);
Now, convert the float array list to float array. At first, we have set the same size to float array i.e. the same number of elements. After that, we have assigned each value −
final float[] arr = new float[arrList.size()]; int index = 0; for (final Float value: arrList) { arr[index++] = value; }
Example
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); } } }
Output
Elements of float array... 5.2 10.3 15.3 20.4 25.2 30.6 45.3 50.9
Advertisements