首先,声明一个长数组列表,并向其中添加一些元素:
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);
现在,为新创建的长数组设置相同的大小:
final long[] arr = new long[arrList.size()]; int index = 0;
Long数组列表中的每个元素都分配给long数组:
for (final Long value : arrList) { arr[index++] = value; }
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); } } }
输出结果
Elements of Long array... 100000 200000 300000 400000 500000 600000 700000 800000 900000 1000000