List sort, find, shuffle, rotate

Source: Internet
Author: User
Tags comparable shuffle

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.