将整数数组列表转换为整数数组不是一项繁琐的任务。首先,创建一个整数数组列表,并向其中添加一些元素-
ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);
现在,将整数数组列表的每个值分配给整数数组。我们曾经size()
获取整数数组列表的大小,并将相同的大小放置到新创建的整数数组中-
final int[] arr = new int[arrList.size()]; int index = 0; for (final Integer value: arrList) { arr[index++] = value; }
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<Integer>arrList = new ArrayList<Integer>(); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500); arrList.add(600); arrList.add(700); arrList.add(800); arrList.add(900); arrList.add(1000); final int[] arr = new int[arrList.size()]; int index = 0; for (final Integer value : arrList) { arr[index++] = value; } System.out.println("Elements of int array..."); for (Integer i : arr) { System.out.println(i); } } }
输出结果
Elements of int array... 100 200 300 400 500 600 700 800 900 1000