Black Horse programmer-collections

Source: Internet
Author: User

Package COM. yang. ex17; import Java. util. arraylist; import Java. util. arrays; import Java. util. list;/* Arrays: the tool class used to operate arrays * contains static methods * aslist: convert the array to the list set *****/public class arraydemo {public static void main (string [] ARGs) {// todo auto-generated method stub string [] arr = {"ABC", "DDD", "KKKK"}; // converts an array to a list set: /** you can use the collection idea to operate arrays * Note: changing an array into a set does not allow the addition or deletion of a set because the array length * is fixed. * Contains * Get * indexof * If added or deleted, an unsupported operation exception occurs. ** // ** if all elements in the array are of the basic data type, this array will be used as an element in the set. * if all elements in the array are objects, the array will become a set, the elements in the array are directly converted into elements in the Set *****/list <string> List = arrays. aslist (ARR); system. out. println (list. contains ("cc"); system. out. println (list);} public static Boolean mycontains (string [] arr, string key) {for (INT x = 0; x <arr. length; X ++) {If (ARR [X]. equals (key) {return true;} return True ;}} package COM. yang. ex17; import Java. util. arraylist; import Java. util. collection; import Java. util. collections; import Java. util. comparator; import Java. util. iterator; import Java. util. list; import Java. util. stack; import Java. util. treeset; import COM. yang. exspe01.treeset;/* collections: class used to operate the set *****/public class demo {public static void main (string ARGs []) {// sortdemo (); // maxdemo (); // B Inarysearchdemo (); // filldemo (); // replaceall (); // orderdemo (); shuffledemo ();} public static void sortdemo () {list <string> List = new arraylist <string> (); list. add ("ABCD"); list. add ("AAAAA"); list. add ("dddd"); list. add ("ppppp"); list. add ("Rrrr"); list. add ("llll"); system. out. println (list); collections. sort (list); system. out. println (list);} class srtlencomparator implements comparator <string> {public int Co Mpare (string S1, string S2) {If (s1.length ()> s2.length () {return 1;} If (s1.length () <s2.length () {return-1 ;} return 0 ;}} public static void binarysearchdemo () {list <string> List = new arraylist <string> (); list. add ("ABCD"); list. add ("AAAAA"); list. add ("dddd"); list. add ("ppppp"); list. add ("qrrrr"); list. add ("fdsfsllll"); collections. sort (list); system. out. println (list); int Index = collections. binarysear CH (list, "qrrrr"); system. out. println (INDEX);} public static void maxdemo () {list <string> List = new arraylist <string> (); list. add ("ABCD"); list. add ("AAAAA"); list. add ("dddd"); list. add ("ppppp"); list. add ("qrrrr"); list. add ("fdsfsllll"); collections. sort (list); system. out. println (list); string max = collections. max (list); system. out. println (max); collections. max (list); system. out. println (list);} public static voi D filldemo () {list <string> List = new arraylist <string> (); list. add ("ABCD"); list. add ("AAAAA"); list. add ("dddd"); list. add ("ppppp"); list. add ("qrrrr"); list. add ("fdsfsllll"); system. out. println (list); collections. fill (list, "PP"); // replace all elements in the set with system. out. println (list);} public static void replaceall () {list <string> List = new arraylist <string> (); list. add ("ABCD"); list. add ("AAAAA"); list. add ("dddd"); list. add ("Ppppp"); list. add ("qrrrr"); list. add ("fdsfsllll"); system. out. println (list); collections. replaceall (list, "AAAAA", "KK"); system. out. println (list); collections. reverse (list); system. out. println (list);} public static void orderdemo () {treeset <string> treeset = new treeset <string> (collections. reverseorder (); // reverse the treeset. add ("ABCD"); treeset. add ("AAAAA"); treeset. add ("dddd"); treeset. add ("ppppp"); treeset. add ("Qrrrr"); treeset. add ("fdsfsllll"); iterator <string> iterator = treeset. iterator (); While (iterator. hasnext () {system. out. println (iterator. next () ;}} class strcomparator implements comparator <string >{ public int compare (string S1, string S2) {// int num = s1.compareto (S1 ); // If (Num> 0) {// return-1; //} // If (Num <0) {// return 1; return s2.compareto (S1 );}} public static void shuffledemo () {// sort Lis randomly T <string> List = new arraylist <string> (); list. add ("ABCD"); list. add ("AAAAA"); list. add ("dddd"); list. add ("ppppp"); list. add ("qrrrr"); list. add ("fdsfsllll"); system. out. println (list); collections. shuffle (list); system. out. println (list);} // public static <t extends comparable <? Extends T> void sort (list <t> List) {//} // Arrays: used to operate the array}

See http://edu.csdn.net/heima for details

Black Horse programmer-collections

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.