The toarray method in the collection interface in Java

Source: Internet
Author: User
Import Java. lang. reflect. array; import Java. util. *; public class collection {public static void main (string ARGs []) {arraylist list = new arraylist (); list. add (1); list. add (2); list. add (3); // convert the window to an Array Using toarray // integer integerarray [] = (integer []) list. toarray (); // This method is incorrect. toarray can only be converted to an array of objects. // object objectarray [] = List. toarray ();/** The following describes another toarray method usage. t [] toarray (t a []) is converted to an array of the corresponding type, there must be a premise for this conversion, that is, the data stored in the arraylist is of this type, so that it can be converted to a type array, you cannot change it to another type. ***/arraylist <integer> newlist = new arraylist <integer> (); newlist. add (1); newlist. add (2); newlist. add (3); newlist. add (4); // write the statement correctly. Convert the type stored in the container to the corresponding array integer integerarray [] = newlist. toarray (new integer [4]); // The following method is incorrect // long longarray [] = newlist. toarray (New long [4]); // This contains type conversion, so error}/** remember one .. toarray can only be converted into arrays, but cannot be converted into arrays. **/21:19:38

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.