EN
Java - convert array to ArrayList
0 points
In this article, we would like to show you how to convert an array to ArrayList in Java.
Quick solution:
xxxxxxxxxx
1
String[] array = {"a", "b", "c"};
2
List<String> arrayList = new ArrayList<>(Arrays.asList(array));
In these examples, we will show how to convert an array of Strings to the ArrayList.
xxxxxxxxxx
1
import java.util.ArrayList;
2
import java.util.Arrays;
3
import java.util.List;
4
5
public class Example {
6
7
public static void main(String[] args) {
8
String[] array = {"a", "b", "c", "d"};
9
10
List<String> arrayList = new ArrayList<>(Arrays.asList(array));
11
System.out.println(arrayList);
12
}
13
}
output:
xxxxxxxxxx
1
[a, b, c, d]
xxxxxxxxxx
1
import java.util.Collections;
2
import java.util.ArrayList;
3
import java.util.List;
4
5
public class Example {
6
7
public static void main(String[] args) {
8
String[] array = {"a", "b", "c", "d"};
9
10
List<String> arrayList = new ArrayList<>();
11
Collections.addAll(arrayList, array);
12
System.out.println(arrayList);
13
}
14
}
output:
xxxxxxxxxx
1
[a, b, c, d]