在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP