Converting a Java base data type array to a collection

Source: Internet
Author: User

Use of the Aslist () method of the Arrays tool class
* A: Case Presentation
* Use of the Aslist () method of the Arrays tool class
* Collection ToArray (t[] a) generic version of the set-to-go array

Array-to-collection: Use of the Aslist () method of the Arrays tool class

public static void Demo2 () {
//int[] arr = {11,22,33,44,55};
//list<int[]> List = arrays.aslist (arr); An array of basic data types is converted to a collection, and the entire array is converted as an object, since the collection stores all references to objects. The base data type can only be used as a reference to an object
//system.out.println (list);
Integer[] arr = {11,22,33,44,55};//Convert an array to a collection, the array must be a reference data type so the correct way to convert is integer
list<integer> list = Arrays.aslist (arr);
SYSTEM.OUT.PRINTLN (list);
}public static void Demo1 () {
String[] arr = {"A", "B", "C"};
list<string> list = Arrays.aslist (arr);cannot add delete after converting array to collection
//list.add ("D");//cannot be added
SYSTEM.OUT.PRINTLN (list);
}

================================================================

Aggregate to array: Collection in ToArray (t[] a) generic version of the set-to-go array

The set-to-array, plus the generic
arraylist<string> list = new arraylist<> ();
List.add ("a");
List.add ("B");
List.add ("C");
List.add ("D");

string[] arr = List.toarray (new string[10]); //When the array is converted to an array, the length of the converted array is equal to the size of the collection if it is less than or equal to the size of the collection
If the length of the array is greater than size, the allocated array length is the same as the length you specify
for (String String:arr) {
System.out.println (string);
}
}

Converting a Java base data type array to a collection

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.