Enumeration
Packageenumeration;/*** Enumeration is the same as iterator usage, but iterator simplifies writing*/Importjava.util.Enumeration;ImportJava.util.Vector; Public classEnum { Public Static voidMain (string[] args) {Vector<String> V1 =NewVector<string>(); V1.add (A); V1.add ("BB"); V1.add ("CCC"); V1.add ("DDDD"); Enumeration<String> en1=v1.elements (); while(En1.hasmoreelements ()) {System.out.println (en1.nextelement ()); } }}
Arrays:
PackageThe container tool class. Collections_arrays;Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.HashMap;Importjava.util.List; Public classArraysdemo { Public Static voidMain (string[] args) {//demo_1 (); //demo_2 ();Demo_3 (); } Public Static voidDemo_3 () {HashMap<integer, string> map1 =NewHashmap<integer,string>(); Map1.put (1, "a"); Map1.put (2, "a"); Map1.put (3, "C"); Map1.put (4, "D"); for(Integer i:map1.keyset ()) {String value=Map1.get (i); System.out.println (i+":"+value); }//For (MAP1. Entry<integer,string> Me:map1.entrySet ()) {//Integer Key = Me.getkey ();//String value = Me.getvalue ();} } Public Static voiddemo_2 () {int[] arr = {1,5,34,76,456};//New Int[5]; //Binary Lookup Boolean can not be used in addition to others;System.out.println (Arrays.binarysearch (arr, 5)); int[] ar=arrays.copyof (arr, 3); for(inti = 0; i < ar.length; i++) {System.out.print (Ar[i]+","); } //array into stringSystem.out.println (arrays.tostring (arr)); //Focus: The array is converted to the list collection Reason: The collection method is more, for example contains indexOf .... Note: The array length is fixed, so adding and deleting methods is not available;//if the element in the array is an object, then directly to the list element, but the array is the basic type value, then the whole array as an element of the list;list<int[]> list=arrays.aslist (arr); SYSTEM.OUT.PRINTLN (list); } Public Static voiddemo_1 () {ArrayList<String> A1 =NewArraylist<string>(); A1.add ("ABC1"); A1.add ("ABC2"); A1.add ("ABC3"); A1.add ("ABC4"); /** For each: multiple iterations, not for element operations; * for (type variable: Single-column Collection | array) {* SYSTEM.OUT.PRINTLN (variable); *} * * for (String a:a1) {* SYSTEM.OUT.PRINTLN (a); * } * * */ //The method of collection The collection to an array is applicable to Set,listString[]arr=a1.toarray (NewString [A1.size ()]); for(String S1:arr) {System.out.print (S1+","); } }}
Collections:
PackageThe container tool class. Collections_arrays;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List;ImportJava.util.TreeSet;Importcomparator. comparator.comparatorbylength; Public classCollectionsdemo { Public Static voidMain (string[] args) {demo_2 (); } Public Static voidDemo () {List<String> L1 =NewArraylist<string>(); L1.add ("AAAAAA"); L1.add ("BBBBB"); L1.add ("CCCC"); L1.add ("DD"); //SortCollections.sort (L1); System.out.println (L1); //Collections.sort (L1, New Comparatorbylength ()); //System.out.println (L1);Mysort (L1,Newcomparatorbylength ()); System.out.println (L1); //Two-point search intIndex=collections.binarysearch (L1, "DD",Newcomparatorbylength ()); System.out.println ("Index=" +index); //Best ValueString max=Collections.max (L1); System.out.println ("Max=" +max); String min= Collections.min (L1,Newcomparatorbylength ()); System.out.println ("Min=" +min); //ReplaceCollections.replaceall (L1, "DD", "NBA"); //principle: L1.set (L1.indexof ("DD"), "NBA"); } Public Static voiddemo_1 () {//Reverse /*treeset<string> t1 = new treeset<string> (new comparator<string> () {public int com Pare (String s1,string s2) {int temp = S2.compareto (S1); return temp; } }); */ //treeset<string> t1 = new treeset<string> (Collections.reverseorder ());treeset<string> T1 =NewTreeset<string> (Collections.reverseorder (Newcomparatorbylength ())); T1.add ("AB"); T1.add ("ACC"); T1.add ("BBCDD"); T1.add ("CBCV"); SYSTEM.OUT.PRINTLN (t1); } Public Static voiddemo_2 () {List<String> L1 =NewArraylist<string>(); L1.add ("AAAAAA"); L1.add ("BBBBB"); L1.add ("CCCC"); L1.add ("DD"); //Replace all, initialize some elementSystem.out.println (L1); //Collections.fill (L1, "*");System.out.println (L1); //Random Sortcollections.shuffle (L1); System.out.println (L1); } Public Static voidDemo_3 () {//Focus: Change the Unsynchronized collection to synchronous /*collections.synchronizedcollection (c); Collections.synchronizedlist (list); Collections.synchronizedmap (m); Collections.synchronizedset (s); Principle: Class mycollections{public static list synlist (List list) {return new MyList (list) ; } private class mylist{private static Final Object lock = new Object (); Private list List; MyList (List list) {this.list=list; } public boolean Add (Object obj) {synchronized (lock) {List.add (); }} public Boolean remove (Object obj) {synchronized (lock) { List.remove (); } } } } */ }/**implement the Set sorting method yourself*/ Private Static<T>voidMysort (list<t> l1,comparator<?SuperT>comp) { for(intX=0;x<l1.size () -1;x++){ for(intY=x+1;y<l1.size (); y++){ if(Comp.compare (L1.get (x), L1.get (y)) >0){ //Collections.swap (L1, x, y);T temp =l1.get (x); L1.set (x, L1.get (y)); L1.set (y, temp);//temp cannot be replaced with L1.get (x) because it has previously modified the x position element to L1.get (y); } } } }}
java=== enumeration, Arrays,collections