EN
Java - copy List to another list
0 points
In this article, we would like to show you how to copy a List to another list in Java.
Quick solution:
xxxxxxxxxx
1
List<String> copiedArrayList = new ArrayList<>(arrayList);
In these examples, we will show how to copy ArrayList of Strings to another ArrayList.
xxxxxxxxxx
1
import java.util.ArrayList;
2
import java.util.List;
3
4
public class Example {
5
6
public static void main(String[] args) {
7
List<String> originalArrayList = new ArrayList<>();
8
originalArrayList.add("x");
9
originalArrayList.add("y");
10
11
List<String> copiedArrayList = new ArrayList<>(originalArrayList);
12
13
System.out.println(copiedArrayList);
14
}
15
}
output:
xxxxxxxxxx
1
[x, y]
xxxxxxxxxx
1
import java.util.ArrayList;
2
import java.util.List;
3
4
public class Example {
5
6
public static void main(String[] args) {
7
List<String> originalArrayList = new ArrayList<>();
8
originalArrayList.add("x");
9
originalArrayList.add("y");
10
11
List<String> copiedArrayList = new ArrayList<>();
12
copiedArrayList.addAll(originalArrayList);
13
14
System.out.println(copiedArrayList);
15
}
16
}
output:
xxxxxxxxxx
1
[x, y]