Arrays.asList () returns a List that cannot be changed in length. What is the purpose of this design?

Arrays.asList () returns an ArrayList inner class (without add (), remove (), cannot change the length), what is the original purpose of this design? Why not return the variable length ArrayList (new ArrayList ()) directly?

Jun.19,2022

The

Arrays.asList () method acts as a bridge between array-based API and Collection API , and is used in conjunction with Collection-sharptoArray . That is to say, this method is to enable the array to use Collection API . Although Arrays.asList () returns List , its essence is still an array, but it has a more convenient API. Since it is an array, it cannot be added and deleted at will.

String[] arr = ...
//arrListaddarrarrListarr
List arrList = Arrays.asList(arr); 

like the following return immutable view, it is similar to using the clone method to ensure the object's security and immutable properties

for example, if you have an array property, how to design a method to ensure that you can traverse and access the array elements without modifying the elements? returning a lightweight view that inherits the list interface is a good design way, while returning the array directly is not a safe choice. of course, you can design an accessor method with an index parameter, but there are two ways. It's just that it's a little more readable, just like recursion is a little easier to read than loops

11

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b40883-2c534.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b40883-2c534.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?