Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

How can I Initialize an ArrayList through List.of()

1 Answer

0 votes
by (13.1k points)

If you are atleast using Java9:

import java.util.ArrayList;

import java.util.List;

ArrayList<String> friends =  new ArrayList<>(List.of(“Virat”,”Rohit”));

Before Java 9, you need to use Arrays.asList()

ArrayList<String> friends = new ArrayList<>(Arrays.asList(“Virat”,”Rohit”));

Want to learn Java? Check out the core Java certification from Intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 31, 2019 in Java by noah kapoor (5.3k points)
0 votes
1 answer
asked Mar 30, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 24, 2021 in Java by Jake (7k points)

Browse Categories

...