the transformation between list and array
List into array
Called the ToArray method of the list, there are two methods with the same name, where object[] ToArray () returns an array of type object, but it is inconvenient to use. The other is public <T> t[] ToArray (t[] a), which returns a generic array of t[].
Array into List
The static method Aslist of the arrays class is invoked, returning a list wrapper that wraps a normal Java array. The object returned is not ArrayList, but a view object that implements the serializable interface to serialize and implements the Randomaccess interface.
Example:
Package Com.xujin;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.List;
public class linkedlisttest{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}
the conversion between set and array
Similar to the above, set into array is called toarray generic method, and array conversion to set is called the Aslist method of the array.
Example:
This example is almost the same as the above, the only difference is
/************set to array********************************/
set<employee> staff = new hashset<employee> ();
And 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 linkedlisttest{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}