Arrays. aslist () appears java. Lang. unsupportedoperationexception

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.