Core Java (25) List and array conversion, set and array Conversion

Source: Internet
Author: User
Mutual conversion between list and array

List to array

The toarray method of list is called. There are two methods with the same name. object [] toarray () returns an array of the object type, but it is inconvenient to use. Public <t> T [] toarray (T [] A) returns a Generic Array T [].

Convert array to list

The static aslist method of the arrays class is called, And a list package encapsulated with common Java arrays is returned. The returned object is not an arraylist, but a view object. The serializable interface can be serialized and the randomaccess interface can be implemented.

Example:

Package COM. xujin; import Java. util. arraylist; import Java. util. arrays; import Java. util. list; public class implements listtest {public static void main (string... arg) {employee Jim = new employee ("Jim"); employee Bob = new employee ("Bob"); employee gin = new employee ("gin "); /*********** list to array ************************ * *******/list <employee> Staff = new arraylist <employee> (); staff. add (Jim); staff. add (Bob); staff. add (GIN); system. out. println (staff); // [name: Jim, name: Bob, name: Gin] employee [] e = new employee [staff. size ()]; staff. toarray (E); For (employee EM: E) system. out. println (EM);/** name: Jim * Name: Bob * Name: gin *** // ************* array to list ******************** * ***********/list <employee> arraytolist = arrays. aslist (E); system. out. println (arraytolist. get (2); // name: Gin} class employee {public employee (string name) {This. name = Name;} Public String getname () {return this. name;} Public String tostring () {return "name:" + name;} private string name = ""; // instance domain initialization}

Mutual conversion between set and array

Similar to the above, converting set to array is the generic method of toarray called, while converting array to set is the aslist method of the called array.

Example:

This example is almost the same as above. The only difference is that

/*********** Set to array ************************ ********/
Set <employee> Staff = new hashset <employee> ();

The results are the same.

Package COM. xujin; import Java. util. arrays; import Java. util. hashset; import Java. util. list; import Java. util. set; public class implements listtest {public static void main (string... arg) {employee Jim = new employee ("Jim"); employee Bob = new employee ("Bob"); employee gin = new employee ("gin "); /*********** set to array ************************ * ******/set <employee> Staff = new hashset <employee> (); staff. add (Jim); staff. add (Bob); staff. add (GIN); system. out. println (staff); // [name: Jim, name: Bob, name: Gin] employee [] e = new employee [staff. size ()]; staff. toarray (E); For (employee EM: E) system. out. println (EM);/** name: Jim * Name: Bob * Name: gin *** // ************* array to set ******************** * ***********/list <employee> arraytolist = arrays. aslist (E); system. out. println (arraytolist. get (2); // name: Gin} class employee {public employee (string name) {This. name = Name;} Public String getname () {return this. name;} Public String tostring () {return "name:" + name;} private string name = ""; // instance domain initialization}

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.