Java – what is the purpose of using aslist?

I just want to know the benefits and purpose of the aslist () method in arrays

It returns a fixed size list supported by the specified array, so we can't add elements to the list. It's like an array (we can't add elements to it) Is there any way to convert a fixed size list to a non fixed size list?

When I try to add an element to a fixed size list, it throws an unsupported operationexception:

Double[] d = {3.0,4.0,5.0};
List<Double> myList = Arrays.asList(d);
myList.add(6.0); // here it will throw an exception

Solution

From Java documents:

"This method acts as a bridge between array based and collection based APIs"

For your problems;

Double[] d = {3.0,5.0};
List<Double> yourList = new ArrayList(Arrays.asList(d));
yourList.add(6.0);
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
分享
二维码
< <上一篇
下一篇>>