How to make a new List in Java

2021-6-3 anglehua

We create a Set as:

Set myset = new HashSet()

How do we create a List in Java?


List myList = new ArrayList();

or with generics (Java 7 or later)

List<MyType> myList = new ArrayList<>();

or with generics (Old java versions)

List<MyType> myList = new ArrayList<MyType>();


Additionally, if you want to create a list that has things in it (though it will be fixed size):

List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");


采集自互联网,如有侵权请联系本人

Powered by emlog 京ICP备15036472号-3 sitemap