+ 3

How to initialize a LinkedList object with elements when creating it.

There is some way to initialize a LinkedList object at the time of creating it other than this one.. LinkedList lst; List l = Arrays.asList(1,5,2,3,9); lst = new LinkedList(l); https://code.sololearn.com/cr799j68SGQ3/?ref=app

10th Mar 2018, 2:03 AM
Javier I. Rivera R.
Javier I. Rivera R. - avatar
1 Answer
+ 1
In Java, u can initialize a LinkedList object with elements directly by using double brace initialization. Here is an example: LinkedList<Integer> lst = new LinkedList<Integer>() {{ add(1); add(5); add(2); add(3); add(9); }};
13th Apr 2024, 5:26 PM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar