1. Array to List
string[] City = {"Nanjing", "Shanghai", "Beijing"}; List<string> citylist = arrays.aslist (city);
Note: The list size of the array conversion is fixed, the Add, remove operation cannot be performed, or the following exception is thrown:
Citylist.add ("Xiamen"); Citylist.remove (2);
If you want to control the list size, you can only add the elements in the array to the list ...
2. List Goto Array
object[] Cityarray = Citylist.toarray (); for (Object X:cityarray) System.out.println (x);
Note: Since the list is converted to array, it is not possible to know what type of data is stored in the list, so an array is created with the object class. The results of the operation are as follows:
3. List Turn Set
string[] City = {"Nanjing", "Shanghai", "Beijing"}; List<string> citylist = arrays.aslist (city); Set listtoset = new HashSet (citylist);
4. Set Goto List
List List = new ArrayList (set);
5. Array Turn Set
Note: Convert array to list first, then set
string[] City = {"Nanjing", "Shanghai", "Beijing"}; Set set = New HashSet (arrays.aslist (city));
6. Set Goto Array
string[] City = {"Nanjing", "Shanghai", "Beijing"}; Set set = New HashSet (arrays.aslist (city)); object[] SetToArray = Set.toarray ();
Java: Converting arrays, lists, and collections to each other