Dark Horse Programmer-----Collection Framework Class (IV) advanced for loop, variable parameter of method and static import
1.1 Advanced For Loop (example 1)
Format:
For (data type variable name: Traversed collection (Collection) or array)
{
}
Iterates over a collection.
Only collection elements can be obtained. However, the collection cannot be manipulated.
In addition to iterating, iterators can also act on elements in the Remove collection.
If the listiterator is used, the collection can be added and checked during the traversal.
What is the difference between 1.1.2 traditional for and advanced for?
The advanced for has one limitation. The target must have been traversed.
1 Example 1:2 ImportJava.util.*;3 4 classForeachdemo5 {6 Public Static voidMain (string[] args)7 {8 9 TenArraylist<string> Al =NewArraylist<string>(); One AAl.add ("ABC1"); -Al.add ("ABC2"); -Al.add ("ABC3"); the - - for(String s:al) - { + //s = "KK"; - System.out.println (s); + } A at System.out.println (AL); - /* - iterator<string> it = Al.iterator (); - - While (It.hasnext ()) - { in System.out.println (It.next ()); - } to */ + - int[] arr = {3,5,1}; the * for(intx=0; x<arr.length; X + +) $ {Panax Notoginseng System.out.println (arr[x]); - } the for(intI:arr) + { ASystem.out.println ("I:" +i); the } + - $Hashmap<integer,string> HM =NewHashmap<integer,string>(); $ -Hm.put (1, "a"); -Hm.put (2, "B"); theHm.put (3, "C"); - WuyiSet<integer> KeySet =Hm.keyset (); the for(Integer i:keyset) - { WuSystem.out.println (i+ "::" +Hm.get (i)); - } About $ //set<map.entry<integer,string>> entryset = Hm.entryset (); - //For (map.entry<integer,string> me:entryset) - - for(map.entry<integer,string>Me:hm.entrySet ()) A { +System.out.println (Me.getkey () + "------" +Me.getvalue ()); the } - $ } the}
PS: It is recommended that you still want to use the legacy for when iterating through the array. Because traditional for can define a foot tag.
Variable parameters of the 2.1 method, new features appearing in the JDK1.5 version (example 2)
Note: Variable parameters must be defined at the end of the parameter list when used.
1 Example 22 classParammethoddemo3 {4 Public Static voidMain (string[] args)5 {6 //Show (3,4);7 /*8 //Although multiple methods are defined less. 9 but each time you define an array. As the actual parameter. Ten One int[] arr = {3,4}; A Show (arr); - - int[] arr1 = {2,3,4,5}; the Show (arr1); - */ - - /* + variable parameters. - is actually the abbreviated form of the previous array parameter. + You do not have to create an array object manually every time. A as long as the element you want to manipulate is passed as a parameter. at These parameters are implicitly encapsulated as arrays. - - */ -Show ("Haha", 2,3,4,5,6); - //Show (2,3,4,5,6,4,2,35,9, "heh"); - //Show (); in - } to Public Static voidShow (String str,int... arr) + { - System.out.println (arr.length); the } * /* $ Public static void Show (int[] arr)Panax Notoginseng { - the } + */ A /* the Public static void show (int a,int b) + { - System.out.println (A + "," +b); $ } $ Public static void show (int a,int b,int c) - {} - */ the}
3.1 Staticimport Static Import (example 3)
When the name of the class is the same, you need to specify a specific package name.
When the name of the method is the same, specify the owning object or class
Example 3:ImportJava.util.*;Import Staticjava.util.arrays.*;//all static members in the arrays class are imported. Import Staticjava.util.collections.*;Import Staticjava.lang.system.*;//all static members in the system class are imported. classStaticimport//extends Object{ Public Static voidMain (string[] args) {out.println ("Haha"); int[] arr = {3,1,5}; Sort (arr); intindex = BinarySearch (arr,1); Out.println (arrays.tostring (arr)); System.out.println ("Index=" +index); ArrayList Al=NewArrayList (); Al.add (1); Al.add (3); Al.add (2); Out.println (AL); Sort (AL); Out.println (AL); }}
Dark Horse Programmer-----Collection Framework Class (IV) advanced for loop, variable parameter of method and static import