Conversion between collections and arrays considerations

Source: Internet
Author: User

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

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.