Collections.copy Error: Source does not fit in dest

Source: Internet
Author: User

Today in the use of collections. Copy method time, error

Source does not fit in dest

The code in your own method is as follows:

list<option> prostatelist = Selectoptionsutil.getoptionsbyid ("prostate");        list<option> resultlist = new arraylist<option> (20);//Generate a newer list to prevent the update operation from affecting the cache variable        collections.copy ( Resultlist, prostatelist);//list copy

First go to see the source of Collections.copy:

  int srcsize = Src.size ();  if (Srcsize > Dest.size ())            throw new Indexoutofboundsexception ("Source does not fit in dest");

Found to be comparison size.

And, to view the source code of ArrayList, found that the use did not change size.

Public ArrayList (int initialcapacity) {        super ();        if (initialcapacity < 0)            throw new IllegalArgumentException ("Illegal capacity:" +                                               initialcapacity);        This.elementdata = new object[initialcapacity];    }

In a moment I was spartan.

Borrow the almighty Google search, found: http://stackoverflow.com/questions/6147650/java-lang-indexoutofboundsexception-source-does-not-fit-in-dest

The great God has encountered this problem.

See for yourself and find that you can do this directly using the following code.

    list<option> prostatelist = Selectoptionsutil.getoptionsbyid ("prostate");        list<option> resultlist = new arraylist<option> (prostatelist);//Generate a newer list to prevent the update operation from affecting the cache variable
You can use the construction method directly, without using the Copy method.

    /**     * Constructs a list containing     the elements of the specified * collection, in the order they is returned by T He collection ' s     * iterator.     *     * @param c the collection whose elements is to being placed into this list     * @throws NullPointerException if the s Pecified collection is null     *    /public ArrayList (collection<? extends e> c) {        elementdata = C.toarray () ;        size = Elementdata.length;        C.toarray might (incorrectly) not return object[] (see 6260652)        if (Elementdata.getclass ()! = Object[].class) 
   elementdata = arrays.copyof (elementdata, size, object[].class);    }



Collections.copy Error: Source does not fit in dest

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.