Java Learning Note 33 (Set Frame VII: Collections Tool Class)

Source: Internet
Author: User

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)

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.