# 在 Java 中将 ArrayList 转换为 Int 数组

Joel Swapnil Singh 2023年10月12日

## 在 Java 中将 Arraylist 转换为 Int 数组的方法

### 使用 `for` 循环将 Arraylist 转换为 Int 数组

``````import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void solve(List<Integer> nums) {
int[] arr = new int[nums.size()];
for (int i = 0; i < nums.size(); i++) {
arr[i] = nums.get(i);
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
System.out.println("ArrayList elements");
for (int i = 0; i < nums.size(); i++) {
System.out.println(nums.get(i));
}
System.out.println("Array elements");
solve(nums); // ArrayList to Array Conversion
}
}
``````

``````ArrayList elements
10
20
30
40
50
Array elements
10
20
30
40
50
``````

### 使用 `Object[] toArray()` 函数将 Arraylist 转换为 Int 数组

``````import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void solve(List<Integer> nums) {
Object[] obj_arr = nums.toArray();
int[] arr = new int[nums.size()];
for (int i = 0; i < obj_arr.length; i++) {
arr[i] = (int) obj_arr[i];
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
System.out.println("ArrayList elements");
for (int i = 0; i < nums.size(); i++) {
System.out.println(nums.get(i));
}
System.out.println("Array elements");
solve(nums); // ArrayList to Array Conversion
}
}
``````

``````ArrayList elements
10
20
30
40
50
Array elements
10
20
30
40
50
``````

### 使用 `T[] toArray` 函数将 Arraylist 转换为 Int 数组

``````import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void solve(List<Integer> nums) {
Integer[] int_arr = new Integer[nums.size()];
int_arr = nums.toArray(int_arr);
int[] arr = new int[nums.size()];
for (int i = 0; i < int_arr.length; i++) {
arr[i] = int_arr[i];
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
System.out.println("ArrayList elements");
for (int i = 0; i < nums.size(); i++) {
System.out.println(nums.get(i));
}
System.out.println("Array elements");
solve(nums); // ArrayList to Array Conversion
}
}
``````

``````ArrayList elements
10
20
30
40
50
Array elements
10
20
30
40
50
``````

### 使用 `mapToInt()` 函数将 Arraylist 转换为 Int 数组

``````import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void solve(List<Integer> nums) {
int[] arr = nums.stream().mapToInt(i -> i).toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
List<Integer> nums = new ArrayList<Integer>();
System.out.println("ArrayList elements");
for (int i = 0; i < nums.size(); i++) {
System.out.println(nums.get(i));
}
System.out.println("Array elements");
solve(nums); // ArrayList to Array Conversion
}
}
``````

``````ArrayList elements
10
20
30
40
50
Array elements
10
20
30
40
50
``````