Java collections and arrays of mutual transfer

Source: Internet
Author: User

Method One: Use the Arrays.aslist () method
String[] STRs = {"One", "one", "three"};
list<string> strlist = array.aslist (STRs);
Attention:
1) This method returns an array-based list view and does not create a list object, so the list cannot be added and deleted.
Making modifications to the list Yes, it will also be modified to the array.
2) The array is converted to a read-only list, using the Collections.unmodifiablelist () method to convert the array to list.
3) Return to the list of deletions, using new ArrayList (Array.aslist (Array)).

Method Two: Using the Collections.addall () method
String[] STRs = {"One", "one", "three"};
list<string> list = new ArrayList ();
Collections.addall (List,strs);
Attention:
This method is equivalent to an add operation that adds elements from the array STRs to the collection list without overwriting the original elements in the collection list.

Method Three: Using the Spring Framework's collectionutils.arraytolist () method
String[] STRs = {"One", "one", "three"};
list<string> list = Collectionutils.arraytolist (STRs);

Convert ArrayList to an array
Using the ToArray () method
arraylist<string> ArrayList = new arraylist<string> ();
Arraylist.add ("one");
Arraylist.add ("both");
String STRs = Arraylist.toarray (new string[0]);//Set conversion array

For more information, please refer to:
Https://www.cnblogs.com/GarfieldEr007/p/7082945.html

Java collections and arrays of mutual transfer

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.