Use the Arrays. asList function with caution -- Exception in thread & quot; main & qu

Source: Internet
Author: User
Tags addall


List<Method>  declaredMethods = Arrays.asList(entityClass.getDeclaredMethods());




Exception in thread "main" java.lang.UnsupportedOperationException

Why is this happening? Because we think that JDK will return an ArrayList or sorted list, the remove (int index) method is called directly. What is actually returned by asList? AsList returns a subclass of the custom AbstractList In the Arrays class, which does not implement the remove Method.

Solution: change the definition:

List <Method> declaredMethods = new ArrayList <> ();

DeclaredMethods. addAll (Arrays. asList (entityClass. getDeclaredMethods ()));


List <Method> declaredMethods = new ArrayList <> (Arrays. asList (entityClass. getDeclaredMethods ()));

Summary: The Arrays class does not implement the remove method because it is a subclass of the custom AbstractList class.

Therefore, to completely convert the array and Variable Parameter objects to the container type, you must use the AddAll ()

Or use the constructor to achieve the goal.

This article is from "welcone !" Blog. For more information, contact the author!

Related Article

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: 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.