The ToArray method of list in Java

Source: Internet
Author: User

To convert a list to an array of some type, take the string type as an example, in the following two ways:

Method 1, use the ToArray method without parameters string[] arr1=new string[list.size ()];list.toarray (arr);//At this point arr has the value in List//Method 2, Use the ToArray method with parameters string[] arr2= (string[]) List.toarray (new string[0]);//explanation of the meaning of the parameter: to store an array of elements in the list, if it is large enough Otherwise, a new array of the same run-time type is assigned for this purpose.

The following methods are incorrect:

String[] arr= (string[]) List.toarray ();//will appear java.lang.ClassCastException

Reference: List.toarray appears java.lang.ClassCastException

The ToArray method of list in Java

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.