List of lists can be sorted and found using the collections class
Sort, public static sort (list<e> List1) does not implement comparable nodes are not available
Find, int binarysearch (list<e> list2,t key,compareto<t>)
Are the first to put the linked list object, and the second one to put the node.
node to implement interface comparable interface CompareTo method as a comparison standard, the following parameters can be written null
(for example, Sring has implemented the CompareTo method, which is sorted in dictionary order)
Test code
PackageCGFG;Importjava.util.Collections;ImportJava.util.Iterator;Importjava.util.LinkedList; Public classtest{ Public Static voidMain (String args[]) {LinkedList<Student> list1=NewLinkedlist<student>(); List1.add (NewStudent ("Huang", 4)); List1.add (NewStudent ("Yu", 2)); List1.add (NewStudent ("Cheng", 5)); Iterator<Student> iterator1=List1.iterator (); for(; Iterator1.hasnext ();) {Student Student=Iterator1.next (); System.out.println (Student.getheight ()); } collections.sort (List1); Iterator1=List1.iterator (); for(; Iterator1.hasnext ();) {Student Student=Iterator1.next (); System.out.println (Student.getheight ()); } Student Test=NewStudent ("DD", 5); intA=collections.binarysearch (List1, test,NULL); Student b=List1.get (a); System.out.println (B.getname ()); }} classStudentImplementscomparable{PrivateString name; Private intheight; Student (String A,intb) {Name=A; Height=b; } intgetheight () {returnheight; } String GetName () {returnname; } Public intCompareTo (Object b) {Student B2=(Student) b; returnheight-B2.height; }}
View Code
Shuffle
public static void Shuffle (List<e> list1)
Rotating
public static void rotate (list<e> list1,int distance)//positive turn right, negative left
Switch
public static void reverse (list<e> list1)
Test code
PackageCGFG;Importjava.util.Collections;ImportJava.util.Iterator;Importjava.util.LinkedList; Public classtest{ Public Static voidMain (String args[]) {LinkedList<Student> list1=NewLinkedlist<student>(); List1.add (NewStudent ("Huang", 4)); List1.add (NewStudent ("Yu", 2)); List1.add (NewStudent ("Cheng", 5)); Iterator<Student> iterator1=List1.iterator (); for(; Iterator1.hasnext ();) {Student Student=Iterator1.next (); System.out.print (Student.getheight ()); } System.out.println (""); Collections.shuffle (list1);//upsetiterator1=List1.iterator (); for(; Iterator1.hasnext ();) {Student Student=Iterator1.next (); System.out.print (Student.getheight ()); } System.out.println (""); Collections.rotate (List1,2);//Right two timesiterator1=List1.iterator (); for(; Iterator1.hasnext ();) {Student Student=Iterator1.next (); System.out.print (Student.getheight ()); } System.out.println (""); Collections.reverse (list1);// Turniterator1=List1.iterator (); for(; Iterator1.hasnext ();) {Student Student=Iterator1.next (); System.out.print (Student.getheight ()); } }} classStudentImplementscomparable{PrivateString name; Private intheight; Student (String A,intb) {Name=A; Height=C; } intgetheight () {returnheight; } String GetName () {returnname; } Public intCompareTo (Object b) {Student B2=(Student) b; returnheight-B2.height; }}
View Code
List sort, find, shuffle, rotate