List arrays and collections convert to each other

Source: Internet
Author: User

1. The list's ToArray () method is used to convert the collection to an array, but in fact the method is defined in the collection, so all the collections have this function,

There are two methods: Object "" ToArray () and t<t> [] ToArray (t[] a) The second method is more commonly used, and we can pass in an array of the specified type.

The element type of the data should be the same as the element type of the collection, and the return value is the transformed array, which holds all the elements in the collection.

eg

List<string> List = new ArrayList<String> ();

List. Add("a");

List. Add("B");

List. Add("C");

String[] strarr = List. ToArray(new String[] {});

System. Out. println(Arrays. ToString(Strarr)); //[A, B, c]

2, List converts an array to

                List string[] Strarr = { "a" "B" "C" }

List<String> List = Arrays. Aslist(Strarr);

System. Out. println(list); //[A, B, c]

List arrays and collections convert to each other

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.