As we have seen the implementations of a List are :
Let us see the ArrayList implementation,
import java.util.ArrayList; import java.util.List; public class MyApplication { public static void main(String[] args) { Listx = new ArrayList<>(); x.add("Mohan"); x.add("Kriti"); x.add("Salim"); List y = new ArrayList<>(x); for (String data : y) { System.out.println(data); } } }
So, in the above code we have created a List,
Listx = new ArrayList<>();
And initialised three names to the variable x,
x.add("Mohan"); x.add("Kriti"); x.add("Salim");
Below is how the values are positioned in the List,
Then we have created a new List and passed the List x as parameter.
Listy = new ArrayList<>(x);
And the new List y gets all the values of the List x.
And we get the below output,