Collection class and Common API
The collection-class Set tool class defines several algorithms for class sets and mappings that are defined as static methods to view 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)
1List<string> nlist=NewArraylist<string>();2Nlist.add ("Zhangsan");3Nlist.add ("Lisi");4Nlist.add ("Wangwu");5Nlist.add ("Zhaoliu");6Nlist.add ("Tianqi");7System.out.println ("Before Operation");8 for(String s:nlist) {9System.out.print (s+ "");Ten } One A System.out.println (); -SYSTEM.OUT.PRINTLN ("After Exchange order"); -Collections.swap (nlist,1,2); the for(String s:nlist) { -System.out.print (s+ ""); - } - + System.out.println (); -System.out.println ("After natural sorting"); + Collections.sort (nList); A for(String s:nlist) { atSystem.out.print (s+ ""); - } - - System.out.println (); -SYSTEM.OUT.PRINTLN ("Binary search")); -System.out.println (Collections.binarysearch (nList, "Zhaoliu")); in -SYSTEM.OUT.PRINTLN ("Scramble Order")); to collections.shuffle (nList); + for(String s:nlist) { -System.out.print (s+ ""); the } * $ System.out.println ();Panax NotoginsengSystem.out.println ("Fill"); -Collections.fill (NList, "Jay"); the for(String s:nlist) { +System.out.print (s+ ""); A}
Output Result:
Before operation
Zhangsan Lisi Wangwu Zhaoliu Tianqi
After Exchange order
Zhangsan Wangwu Lisi Zhaoliu Tianqi
After natural sorting
Lisi Tianqi Wangwu Zhangsan Zhaoliu
Binary Method Search
4
Scramble Order
Zhaoliu Tianqi Wangwu Zhangsan Lisi
Fill
Jay Jay Jay Jay
Collection class and Common API