1 PackageP2;2 3 Importjava.util.ArrayList;4 Importjava.util.Arrays;5 Importjava.util.List;6 7 Public classToArray {8 9 Public Static voidMain (string[] args) {Ten One /* A * Set -to-go array - * The ToArray method in the collection interface is used. - * Set-to-array: You can restrict the operation methods of elements in the collection, and do not allow additions or deletions to them the * - * - * The Toarrays method needs to pass in an array of the specified type. - * How is the length defined? + * If the length is less than the size of the collection, then the method creates an array of the same type and the same size as the collection. - * If the length is greater than the size of the set, the method uses the specified array to store the elements in the collection, and the other location defaults to null + * So it is recommended that the last length be specified as the size of the collection. A */ at -list<string> list =NewArraylist<string>(); -List.add ("ABC1"); -List.add ("ABC2"); -List.add ("Abc31"); -List.add ("ABC4"); in -string[] arr = List.toarray (Newstring[list.size ()]); toSystem.out.println (arrays.tostring (arr));//results: [ABC1, ABC2, ABC31, ABC4] + - the } * $}
Java Learning Notes collection go to array---toArray ()