The result of ArrayList sublist cannot be coerced to ArrayList, otherwise a classcastexception exception will be thrown, that is, Java.util.RandomAccessSubList cannot is cast to Java.util.ArrayList.
Cause: Sublist returns the inner class sublist of ArrayList, not ArrayList but a view of ArrayList, and all operations on the Sublist sub-list will eventually be reflected on the original list.
High attention to the addition or deletion of the original set in the sublist scene. Causes the concurrentmodificationexception exception to traverse, increment, and delete the child list. Using the collection-to-array method, you must use the collection's ToArray (t[] array), passing in an array of exactly the same type, and the size is list.size ().
Cause: With the ToArray parameter method, the ToArray method internally allocates the memory space and returns the new array address when the array space is not large enough, and the array element labeled [List.size ()] is set to null if the number of elements in the array is greater than is actually required. Other array elements retain their original values. It is therefore better to define the method into the parameter group size and the number of geometric elements.
For example:
list<string> list = new arraylist<string> (2); List.add ("Guan"); List.add ("Bao"); string[] Array = new string[list.size ()];
When you use the tool class Arrays.aslist () to convert an array to a collection, you cannot use it to modify a collection-related method, and its Add/remove/clear method throws a Unsupportedoperationexception exception.
Cause: The return object of Aslist is an inner class of Arrays, and does not implement the method of modifying the collection, Arrays.aslist is the adapter mode, but the interface is transformed, the data in the background is still an array.
1 string[] str = new string[]{"You", "WU"};
2 listlist = arrays.aslist (str);
First case: List.add ("Test"); run-time exception.
In the second case: str[0]= "Test", then List.get (0) will also be modified.
Conversion between collections and arrays considerations