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


To convert integer array list to float array, let us first create an integer array list −

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);

Now, convert integer array list to float array. We have first set the size to the float array. With that, each and every value of the integer array is assigned to the float array −

final float[] arr = new float[arrList.size()];
int index = 0;
for (final Integer value: arrList) {
   arr[index++] = value;
}

Example

 Live Demo

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);
      }
   }
}

Output

Elements of float array...
25.0
50.0
100.0
200.0
300.0
400.0
500.0

Updated on: 30-Jul-2019

945 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements