Collection class and common APIs, collectionapi
Collection class and common APIs
Collection-class set tool class, which defines several algorithms used for class set and ing. These algorithms are defined as static methods. For details, refer to the api documentation;
A) public static void sort (List <T> liet)
B) public static int binarySearch (List <T> list, T key)
C) public static void reverse (List <?> List)
D) public static void shuffle (List <?> List)
E) public static void swap (List <?> List, int I, int j)
F) public static <T> void fill (List <? Super T> list, T obj)
1 List <String> nList = new ArrayList <String> (); 2 nList. add ("zhangsan"); 3 nList. add ("lisi"); 4 nList. add ("wangwu"); 5 nList. add ("zhaoliu"); 6 nList. add ("tianqi"); 7 System. out. println ("before"); 8 for (String s: nList) {9 System. out. print (s + ""); 10} 11 12 System. out. println (); 13 System. out. println ("after switching order"); 14 Collections. swap (nList, 1, 2); 15 for (String s: nList) {16 System. out. print (s + ""); 17} 18 19 System. out. println (); 20 System. out. println ("after natural sorting"); 21 Collections. sort (nList); 22 for (String s: nList) {23 System. out. print (s + ""); 24} 25 26 System. out. println (); 27 System. out. println ("binary lookup"); 28 System. out. println (Collections. binarySearch (nList, "zhaoliu"); 29 30 System. out. println ("disordered order"); 31 Collections. shuffle (nList); 32 for (String s: nList) {33 System. out. print (s + ""); 34} 35 36 System. out. println (); 37 System. out. println ("fill"); 38 Collections. fill (nList, "jay"); 39 for (String s: nList) {40 System. out. print (s + ""); 41}
Output result:
Before operation
Zhangsan lisi wangwu zhaoliu tianqi
After switching order
Zhangsan wangwu lisi zhaoliu tianqi
After natural sorting
Lisi tianqi wangwu zhangsan zhaoliu
Binary Search
4
Disordered order
Zhaoliu tianqi wangwu zhangsan lisi
Fill
Jay