Java – is there anything better than calling arrays Aslist is a better alternative to list initialization?
•
Java
Is there a better alternative to using arrays Aslist as a list batch initialization program? Worryingly, this one is verbose, involving an unrelated class and method
List<Double> myList = new ArrayList<Double>(Arrays.asList(3.01d,4.02d,5.03d));
Edit: this problem involves batch initialization. There are usually more than three values shown in the example
Solution
If you know you don't need to add anything to the list in the future, you can do so
List<Double> myList = Arrays.asList(3.01d,5.03d);
I'm pretty sure from arrays The list returned by aslist can be modified, but only you can change the elements there - you can't add new elements
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码