Package test;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.HashSet;
Import java.util.List;
Import Java.util.Set;
public class Test2 {public static void main (string[] args) {List List = new ArrayList ();
List.add ("a");
List.add ("B");
List.add ("C");
List.add ("D");
List.add (1)//will produce java.lang.ArrayStoreException exceptions///When the data types in the list are consistent, the list can be converted to an array of object[] array = List.toarray ();
System.out.println ("The length of the object array converted from list is:" + array.length); You need to force type conversion when converting to an array of other types, and to use the ToArray method with parameters, the argument is an object array,//the contents of the list are placed in the parameter array, and when the length of the argument array is less than the number of elements in the list,
Automatically expands the length of the array to fit the length of the list string[] Array1 = (string[]) List.toarray (new string[0));
System.out.println ("The length of the string array converted from List is:" + array1.length);
Allocates a string array of length equal to the length of the list string[] Array2 = (string[]) List.toarray (new String[list.size ());
System.out.println ("The length of the string array converted from List is:" + array2.length);
List.clear (); Converts the array to list for (int i = 0; i < Array.Length i++) {List.add (array[i]);
System.out.println ("The number of elements that convert the array to list is:" + list.size ());
List.clear ();
Direct use of Arrays aslist method list = arrays.aslist (array);
System.out.println ("The number of elements that convert the array to list is:" + list.size ());
Set set = new HashSet ();
Set.add ("a");
Set.add ("B");
Converts set to array = Set.toarray ();
Array1 = (string[]) Set.toarray (new string[0));
Array2 = (string[]) Set.toarray (new String[set.size ()));
System.out.println ("The length of the object array converted from set is:" + array.length);
System.out.println ("The length of the string array converted from set is:" + array2.length);
Array to set///To convert array to list, then use list to construct set set = new HashSet (arrays.aslist (array));
System.out.println ("The number of elements that convert the array to set is:" + list.size ());
Empty the set, and then convert the array to list all add set.clear ();
Set.addall (Arrays.aslist (array1));
System.out.println ("The number of elements that convert the array to set is:" + list.size ());
}
}