------<ahref= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Dark Horse programmer--19,collections Tool class, Arrays tool class
/*
Collections is a class, note that there is a s behind here
Don't get mixed up with collection! Collection is just an interface.
The two should be distinguished.
Collections is a tool class.
All methods inside this class are static and can be called directly from the class name.
When do you use this tool class?
For example, there can be duplicate elements in a list collection, but what if you want to arrange those elements?
This is the time to use the tool class.
This tool class is a tool class that operates specifically for collections.
For example, this tool class has a well-defined static method sort
public static <t extend comparable<? Super t>> void Sort (list<t> List)
Call the time directly Collections.sort (LI);
Where Li is an example of list<teacher>, then this requires the object of the Teacher class to have a comparative
There is also a common method
public static <t>void sort (list<t>list, comparator<? Super t> C)
Call the time directly collections.sort (LI,COMPA);
Among them, Li is an example of list<teacher>,
And Compa is an instance of a subclass that implements the comparator interface,
This comparer operates on objects of subclasses of the teacher class or teacher class.
At this point, the elements are sorted according to the rules of the comparator.
*/
Import java.util.*;class je{public static void Main (string[] args) {method (FH ()); METHOD2 (FH ()); Method3 (FH ()); System.out.println ("helloworld!"); } public static list<string> fh () {list<string> biaoge=new arraylist<s Tring> (); Biaoge.add ("Bfuibag"); Biaoge.add ("Ibu"); Biaoge.add ("Ibu");//note there are repeating elements Biaoge.add ("Uitweru"); Biaoge.add ("FVGFIBERBRT"); Biaoge.add ("zz"); Biaoge.add ("Juykui"); Soc ("originally biaoge---" +biaoge);//First print the original order of Biaoge return biaoge; public static void Method (List<string> biaoge) {Soc ("method------below"); Collections.sort (Biaoge);//The SOC is arranged in the natural order of the Elements ("new arrangement biaoge---" +biaoge); Then print the Biaoge//string class using the Collections.sort () method The object string itself is comparable//used Collections.sort () to sort A duplicate element is encountered and will not be removed} public static void Method2 (List<string> biaoge) {Soc (" Below is the method2------"); Collections.sort (Biaoge,new Bijiao ());//The SOC is ranked by comparator ("New arrangement biaoge---" +biaoge); Even with comparator comparisons, duplicate elements are not rejected} public static void Method3 (List<string> biaoge) { Obtain the maximum SOC ("method3------Below"); String Max=collections.max (Biaoge); The maximum value is obtained according to the natural order of the elements, note that the maximum value is not taken by the angle mark! Soc ("max=" +max); Collections.sort (Biaoge); Max=collections.max (Biaoge); Soc ("max=" +max); Max=collections.max (Biaoge,new Bijiao ()); The maximum Soc ("max=" +max) is obtained by the comparator. } public static void Soc (Object obj) {System.out.println (obj); }}class Bijiao implements comparator<string>{public int compare (String s1,string s2) {if (S1.length () >s2.length ()) {return 1; } if (S1.length () ==s2.length ()) {return s1.compareto (S2); } return-1; }}/* The above code compiles the result of the run, originally biaoge---[Bfuibag,ibu, IBU, Uitweru, FVGFIBERBRT, ZZ, Juykui] The following is the method------new arrangement Biaoge---[Bfuibag, FVGFIBERBRT, IBU, IBU, Juykui, Uitweru, ZZ] originally Biaoge---[Bfuibag,ibu, IBU, Uitweru, FVGFIBERBRT, ZZ, Juykui] Below is the method2------new arrangement Biaoge---[ZZ, Ibu,ibu, Juykui, Bfuibag, Uitweru, FVGFIBERBRT] originally biaoge---[Bfuibag,ibu, IBU, Uitweru , FVGFIBERBRT, ZZ, Juykui] Below is method3------Max=zzmax=zzmax=fvgfiberbrthello world! */
—————— Split Line ——————
Calls to common methods in/*collections */import java.util.*;class je2{public static void Main (string[] args) { Filldemo (FH ()); Replacealldemo (FH ()); Reversedemo (FH ()); Reverseorderdemo (); } public static list<string> fh () {list<string> biaoge=new ARRAYLIST<STRING&G t; (); Biaoge.add ("Bfuibag"); Biaoge.add ("Ibu"); Biaoge.add ("Ibu");//note there are repeating elements Biaoge.add ("cfff"); Biaoge.add ("zz"); Biaoge.add ("RRR"); Biaoge.add ("Juykui"); return biaoge; } public static void Binarysearchdemo (List<string> biaoge) {Soc ("below is Binarysearchd Emo---"); Collections.sort (Biaoge);//Sort Soc (biaoge); int jb=Collections.binarysearch (Biaoge, "RRR"); Searching for "ddd" in Biaoge with binary lookup method, and returning the corresponding angular standard soc ("jb=" +JB); int cz= Collections.binarysearch (biaoge, "RKK"); Soc ("cz=" +cz);//This sentence is printed cz=-6/* with Collections.binarysearch () can not find the elements in the list, the return must be negative, the minus sign does not exist, after the The value is the appropriate insertion position if the element is not affected by the order of arrangement. The set collection has an inner-hidden arrangement, so the method is used for the list collection. If the element itself is not comparable, you can add a comparator, as follows: Int Cz=collections.binarysearch (Biaoge, "RKK", bicomparator); Where Bicomparator is the implementation of the Comparator<string> interface */} public static VO ID Filldemo (list<string> biaoge) {SOC ("Filldemo---" below); Soc ("Original Biaoge---" +biaoge); Collections.fill (Biaoge, "yyyyyy");//Replace All elements with "yyyyyy" Soc (Biaoge); } public static void Replacealldemo (List<string> biaoge){SOC ("Replacealldemo---below"); Soc ("Original Biaoge---" +biaoge); Collections.replaceall (Biaoge, "RRR", "KKK"); Replacing the RRR with KKK, it is very convenient for the SOC (Biaoge) to replace the elements with elements. public static void Reversedemo (List<string> biaoge) {SOC ("Reversedemo---" below); Soc ("Original Biaoge---" +biaoge); Collections.reverse (Biaoge);//Invert the elements of the list collection to arrange the SOC ("Biaoge---after reverse" +biaoge); public static void Reverseorderdemo () {SOC ("Below is Reverseorderdemo---"); Treeset<string> tg=new treeset<string> (); Tg.add ("Bfuibag"); Tg.add ("Ibu"); Tg.add ("Ibu");//note there are repeating elements Tg.add ("cfff"); Tg.add ("zz"); Tg.add ("RRR"); Tg.add ("Juykui"); Soc("Below is the TreeSet set TG without Collections.reverseorder ()"); Soc (TG); Treeset<string> ts=new treeset<string> (Collections.reverseorder ()); Ts.add ("Bfuibag"); Ts.add ("Ibu"); Ts.add ("Ibu");//note there are repeating elements Ts.add ("cfff"); Ts.add ("zz"); Ts.add ("RRR"); Ts.add ("Juykui"); Soc ("The following is a reverse comparator after TS"); SOC (TS); /* Collections.reverseorder () returns a reverse comparator, which is printed in reverse and one example: Treeset<s Tring> ts=new treeset<string> (Collections.reverseorder (GGG)); Which GGG is I casually named comparator, this comparator has its own rules, but Collections.reverseorder (GGG) GGG the rules flipped, then passed to the new treeset<string> The rule is to flip the rule after the GGG comparison rule. */} public static void Soc (Object obj) {System.out.pr Intln (obj); }}/* above code compile run result: The following is Filldemo---original biaoge---[Bfuibag,ibu, IBU, CFFF, ZZ, RRR, JUYKUI][YYYYYY, yyyyyy, yyyyyy, yyyyyy, y YYYYY,YYYYYY, yyyyyy] Below is replacealldemo---original biaoge---[Bfuibag,ibu, IBU, CFFF, ZZ, RRR, Juykui][bfuibag, IBU, IBU, CFFF, ZZ, KKK, Juykui] below is reversedemo---original biaoge---[Bfuibag,ibu, IBU, CFFF, ZZ, RRR, juykui]reverse after Biaoge---[Juykui, RRR, ZZ, CFFF, IBU, IBU, Bfuibag] Below is the reverseorderdemo---below is the Collections.reverseorder collection TreeSet, Tg[bfuibag, CFFF, without IBU (), Juykui, RRR, ZZ] The following is the addition of the reverse comparator after ts[zz, RRR, Juykui, IBU, CFFF, bfuibag]*/
———————— Split Line ————————
Some common methods inside the/*collections tool class */import Java.util.*;class je3{public static void Main (string[] args) { Swapdemo (FH ()); Shuffledemo (FH ()); } public static list<string> fh () {list<string> biaoge=new ARRAYLIST<STRING&G t; (); Biaoge.add ("Bfuibag"); Biaoge.add ("Ibu"); Biaoge.add ("Ibu");//note there are repeating elements Biaoge.add ("cfff"); Biaoge.add ("zz"); Biaoge.add ("RRR"); Biaoge.add ("Juykui"); return biaoge; public static void Swapdemo (List<string> biaoge) {SOC ("Swapdemo---" below); Soc (Biaoge); Collections.swap (biaoge,3,4); Transposition operation, replace the third and fourth position SOC ("Biaoge---after transposition" +biaoge); } public static void Shuffledemo (list<string> biaoge) {SOC ("Shuffledemo---" below); Soc (Biaoge); Collections.shuffle (Biaoge); The sequence of elements is disrupted by the SOC (Biaoge); } public static void Soc (Object obj) {System.out.println (obj); }}/* above code compile run result: Swapdemo---[Bfuibag, IBU, IBU, CFFF, ZZ, RRR, Juykui] after transposition biaoge---[Bfuibag,ibu, IBU, ZZ, CFFF, RRR, Juy Kui] Below is shuffledemo---[Bfuibag, IBU, IBU, CFFF, ZZ, RRR, Juykui][rrr, Juykui, IBU, CFFF, IBU, ZZ, bfuibag]*/
———————— Split Line ————————
/*
The arrays class is also a tool class for array manipulation.
All of the arrays classes are static classes.
*/
Import java.util.*;class je4{public static void Main (string[] args) {Tostringd Emo (); Aslistdemo (); public static void Aslistdemo () {SOC ("Below is Aslistdemo---"); String[] s={"SDF", "HJK", "Yuiio"}; List<string> k= arrays.aslist (s); K.add ("Kiuh"); /* Converts the array s to the list collection, but does not increment or subtract the array after it is converted to a set, otherwise the runtime exception is fixed because the array length is The The element in the array here is the string type, which is the object. When an object is inside an array, this method can be called directly into the list collection. */SOC ("K---" +k); Int[] s2={12,36,14,45}; List k2=arrays.aslist (S2); This sentence is equivalent to list<int[]> k2=arrays.aslist (S2); Soc ("K2---" +k2); /* The hash value of the array is printed here, if the element in the array is the base data type, The Arrays.aslist () method saves the entire array as an element into the list collection. */integer[] s3={5,14,78,5,4}; The array element is an object, note the distinction. List<integer> K3=arrays.aslist (S3); Soc ("K3---" +k3); public static void Tostringdemo () {SOC ("Below is Tostringdemo---"); Int[] x={12,85,4,74,32,46}; Soc (arrays.tostring (x)); Arrays.tostring (x) returns the information for array x} public static void Soc (Object obj) { System.out.println (obj); }}/* above code compile run result: Below is Tostringdemo---[12, 85, 4, 74, 32, 46] Below is aslistdemo---k---[SDF, HJK, YUIIO]K2---[[[email PROTECTED]]K3---[5, 14, 78, 5, 4] */
———————— Split Line ————————
/* collection to array: */import java.util.*;class je5{public static void Main (string[] args) {List<stri Ng> s=new arraylist<string> (); S.add ("sss01"); S.add ("Sss02"); S.add ("Sss03"); S.add ("Sss04"); Soc ("s---" +s); String[] z= S.toarray (new string[7]); /* ToArray () method is the method of the collection interface. If no new string[7] is run, the error occurs because the to array () method returns an array of object class objects. Adding new String[7] Creates and returns a String array of length 7, where the length is too long to fill with null, and if the length is too short, an array of the appropriate length is created. In order to conserve resources, you should just fill in the appropriate length. For example, in this question, it should be written as follows: string[] Z=s.toarray (New String[s.length ())); It is not possible to increase the deletion after the collection is transferred to the array. This is also to limit the manipulation of elements. */SOC ("Z---" +arrays.tostring (z)); } Public Static Void Soc (Object obj) {System.out.println (obj); }/* above code compile run result: s---[sss01, SSS02, SSS03, sss04]z---[Sss01, SSS02, Sss03, Sss04, Null,null, null]*/
—————— Split Line ——————
/*
Advanced for Loop
For (data type variable name: traversed collection or array)
{
Statement
}
The advanced for loop appears from the jdk1.5 version, and the underlying call to the iterator
The advanced for loop cannot operate diagonally, which is also a disadvantage.
*/
Import Java.util.*;class je6{publicstatic void Main (string[] args) {list<string> s=new ARR Aylist<string> (); S.add ("HJK"); S.add ("Yhdn"); S.add ("Vgbda"); S.add ("tr"); for (String a:s) {a= "233"; Soc (a); } Soc (s); /* This note: A is only pointing to the object being traversed, even if a is assigned, it still does not change the value of the traversed object. */for (String a:s) {SOC (a); } Soc (s); Map<string,string> m=new hashmap<string,string> (); M.put ("Yu01", "John Doe"); M.put ("Yu12", "Miss"); M.put ("Yu3", "Cannon"); M.put ("yu45", "Red"); set<map.entry<string,string>> RT = M.entryset (); For (map.entry<string,string> Me:m.entryset ()) {SOC ("Key---" +me.getkey () + "value-- -"+me.getvalue()); }} public static void Soc (Object obj) {System.out.println (obj); }}/* above code compile run result: 233233233233[HJK, YHDN, Vgbda, TR]HJKYHDNVGBDATR[HJK, YHDN, VGBDA, Tr]key---yu01 Value---John Doe key---YU12 value---miss key---yu45 value---red key---yu3 value---cannon */
—————— Split Line ——————
/* new feature in jdk1.5 version: variable parameter */import java.util.*;class je7{public static void Main (string[] args) { KK (12,45,75,62); Invisible put these numbers into the array//kk2 (455,58, "HjN");//This sentence compiles the error/* Why the second sentence of the KK2 (455,58, " HjN "); Compile time error? Because of the use of variable parameters, the parentheses have been automatically wrapped in the number of numbers, the subsequent "HJN" is not able to be loaded into the array of int type. */KK3 ("HJK", 56,84,24); This sentence compiles successfully, indicating that the mutable parameter is to be defined at the end of the parenthesis} public static void kk (int ... A)//Of course, not only can you write int, it can also be other types such as String, etc. or the type of your own definition {System.out.println (a.length); }/* public static void Kk2 (int ... a,string)//This sentence compilation error {System.out.println (a.length); } */public static void Kk3 (String a,int ... b) {System.out.println (b.length); } }
———————— Split Line ——————
/* Static import */import java.util.*;import static java.util.arrays.*;//import the Arrays class with all of the statics java.lang.system.*;// Import all static members of the system class, class je8{public static void Main (string[] args) {int[] x={12, 65,75,48}; Arrays.sort (x); Sort (x);//The array is sorted */* Because all static members in the arrays class were previously imported, so Arrays.sort (x); arrays can Omitted. */SOC (arrays.tostring (x)); /* SOC (toString (x));//This sentence is compiled without the introduction of the static members of the arrays class that were imported before, However, the ToString () method is also found in the object class, and all classes are directly or indirectly inheriting the object class, and for this kind of package there is a method of the same name. If you do not explicitly write a package or a method of which class, the compilation will be wrong. */System.out.println ("helloworld!"); Out.println ("Huhu"); } public static void Soc (Object obj) {System.out.println (MB (); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dark Horse programmer--19,collections Tool class, Arrays tool class, advanced for loop, variable parameter, static import