ArrayList 转数组
时间: 2024-02-20 14:54:44 浏览: 76
以下是两种ArrayList转换为数组的方法:
1. 使用toArray()方法
```java
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
int size = list.size();
String[] array = (String[]) list.toArray(new String[size]);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
```
2. 使用Arrays.asList()方法
```java
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
String[] array = new String[3];
array[0] = "a";
array[1] = "b";
array[2] = "c";
List<String> list = Arrays.asList(array);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i)); }
}
}
```
阅读全文
相关推荐


















