Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways of populating a list from an array

Tags:

java

What is the difference between these two styles of initialization :

List<String> list = new ArrayList<String>(Arrays.asList(arr));

and

List<String> list = Arrays.asList(arr);

One thing that I can make out is that in the latter case, we are not using the ArrayList class. But then, which class's object (list) are we creating here ?

like image 460
OneMoreError Avatar asked Dec 05 '25 10:12

OneMoreError


1 Answers

The first creates a mutable List, the second is of fixed size. ArrayList is not the only implementation of List. Arrays.asList returns its own implementation which is of fixed size, i.e. individual elements can be updated but elements cannot be added or removed.

like image 66
Reimeus Avatar answered Dec 10 '25 05:12

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!