Array has tool class, aspect manipulation array
The collection also has a tool class: collections
Examples of common methods:
Packagedemo;Importjava.util.ArrayList;Importjava.util.Collections;Importjava.util.List; Public classCollectiondemo { Public Static voidMain (string[] args) {function1 (); Function2 (); Function3 (); } Public Static voidfunction1 () {//sort order: Must be a list collection, sorted in ascending order//string sorting, alphabetical orderlist<string> list =NewArraylist<string>(); List.add (A); List.add ("D"); List.add ("S"); List.add ("G"); Collections.sort (list); SYSTEM.OUT.PRINTLN (list); //[A, D, G, S] } Public Static voidfunction2 () {//binary Search, note must be a list collection//binary lookup is done by sortinglist<integer> list =NewArraylist<integer>(); List.add (11); List.add (3); List.add (16); List.add (9); List.add (15); Collections.sort (list); SYSTEM.OUT.PRINTLN (list); //[3, 9, one, and a.] intindex = Collections.binarysearch (list, 11); SYSTEM.OUT.PRINTLN (index);//2//the element with index 2 is one } Public Static voidFunction3 () {//random arrangement of setslist<integer> list =NewArraylist<integer>(); for(Integer i = 1; i<10;i++) {list.add (i); } System.out.println (list); //[1, 2, 3, 4, 5, 6, 7, 8, 9]Collections.reverse (list);//Flip A collectionSystem.out.println (list); //[9, 8, 7, 6, 5, 4, 3, 2, 1]Collections.shuffle (list);//Random arrangementSystem.out.println (list); //[7, 3, 4, 5, 8, 2, 6, 9, 1]//each run output is different }}
Java Learning Note 33 (Set Frame VII: Collections Tool Class)