How to convert Float array list to float array in Java?

Java 8Object Oriented ProgrammingProgramming

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

 Live Demo

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
raja
Published on 24-Apr-2019 10:56:16
Advertisements