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}