How to convert Long array list to long array in Java?


Firstly, declare a Long array list and add some elements to it:

ArrayList < Long > arrList = new ArrayList < Long > ();
arrList.add(100000 L);
arrList.add(200000 L);
arrList.add(300000 L);
arrList.add(400000 L);
arrList.add(500000 L);

Now, set the same size for the newly created long array:

final long[] arr = new long[arrList.size()];
int index = 0;

Each and every element of the Long array list is assigned to the long array:

for (final Long value : arrList) {
   arr[index++] = value;
}

Example

import java.util.ArrayList;
public class Demo {
   public static void main(String[] args) {
      ArrayList<<Long> arrList = new ArrayList<Long>();
      arrList.add(100000L);
      arrList.add(200000L);
      arrList.add(300000L);
      arrList.add(400000L);
      arrList.add(500000L);
      arrList.add(600000L);
      arrList.add(700000L);
      arrList.add(800000L);
      arrList.add(900000L);
      arrList.add(1000000L);
      final long[] arr = new long[arrList.size()];
      int index = 0;
      for (final Long value : arrList) {
         arr[index++] = value;
      }
      System.out.println("Elements of Long array...");
      for (Long i : arr) {
         System.out.println(i);
      }
   }
}

Output

Elements of Long array...
100000
200000
300000
400000
500000
600000
700000
800000
900000
1000000

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements