如何在Java中将整数数组列表转换为整数数组?
将整数数组列表转换为整数数组不是一项繁琐的任务。首先,创建一个整数数组列表,并向其中添加一些元素-
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
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短