Code Description:
1 PackageArray;2 3 Importjava.util.ArrayList;4 Importjava.util.Arrays;5 Importjava.util.List;6 7 /**8 * Instructions for using the array:9 * Content:Ten * 1, array instantiation; One * 2, array and ArrayList conversion; A * 3. Instantiate ArrayList via array - * Difference: - *--array ([]): The most efficient, but its capacity is fixed and can not be changed dynamically; the *--arraylist: Capacity can be dynamically increased, but sacrificing efficiency; - * https://www.cnblogs.com/wangbin2188/p/6524200.html - */ - Public classArraydemo { + Public Static voidMain (string[] args) { - //Instantiation 1 +string[] Array =NewString[2]; AArray[0] = "AAA"; atARRAY[1] = "BBB"; - - //Instantiation 2 -String[] Array2 = {"A", "B", "C"}; - for(String s:array2) { - System.out.println (s); in } - to //data element Fill value +Arrays.fill (Array2, "D"); - for(String s:array2) { the System.out.println (s); * } $ //The arrays method contains: sort (), BinarySearch (), Equals (), fill (), aslist ()Panax Notoginseng - //instantiate ArrayList with array theList List =NewArrayList (Arrays.aslist ("A", "B")); + System.out.println (list); A theList List2 =arrays.aslist (array); + System.out.println (list2); - $ $ //ArrayList convert to array - Arraydemo.listtoarray (); - the //array conversion to ArrayList - arraydemo.arraytolist ();Wuyi the } - Wu /** - * ArrayList converted to array About */ $ Public Static voidListtoarray () { -list<string> list =NewArraylist<string>(); -List.add ("Wang Lihu"); -List.add ("Zhang San"); AList.add ("John Doe"); + intSize =list.size (); thestring[] Array = (string[]) List.toarray (Newstring[size]); - for(inti = 0; i < Array.Length; i++) { $ System.out.println (Array[i]); the } the } the the /** - * Array converted to ArrayList in */ the Public Static voidarraytolist () { thestring[] Array =NewString[3]; AboutArray[0] = "Wang Lihu"; theArray[1] = "Zhang San"; theARRAY[2] = "John Doe"; thelist<string> list =arrays.aslist (array); + for(inti = 0; I < list.size (); i++) { - System.out.println (List.get (i)); the }Bayi } the}
Operation Result:
Abcddd[a, B][AAA, BBB] Wang Lihu Zhang Sanli Tiger Dick and Harry Process finished with exit code 0
An array (array) Description of Java