Import Java. util. arrays; import Java. util. list; import Java. util. arraylist; public class stringarraytest {publicstatic void main (string [] ARGs) {string [] words = {"Ace", "boom", "crew", "dog ", "Eon"}; List <string> wordlist = arrays. aslist (words); For (string E: wordlist) {system. out. println (e );}}}
When arrays. aslist () is used and the add and remove methods are calledJava. Lang. unsupportedoperationexceptionException. This is because arrays. aslist () returns java. util. arrays $ arraylist instead of arraylist. Arrays $ arraylist and arraylist both inherit from javasactlist. Methods such as remove and add are the default throw in abstractlist.
Unsupportedoperationexception and no operation is performed. Arraylist override these methods are used to operate the list, but arrays $ arraylist does not have override remove (), add (), and so on, so throw unsupportedoperationexception.
The solution is to use iterator or convert it to arraylist and list arraylist = new arraylist (wordlist.