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