New features of JDK5: Automatic disassembly box, generics, enhanced for, static import, variable parameters, enumeration
Enhanced for: is one of the for loops.
Format:
For (element data type variable: Array or collection collection) {
Use the variable, which is the element
}
Benefit: simplifies the traversal of arrays and collections.
Disadvantage: The target of enhanced for cannot be null.
How to solve it? The target of the enhanced for is preceded by a non-null judgment and then in use.
1 Importjava.util.ArrayList;2 Importjava.util.Collection;3 Importjava.util.List;4 Public classFordemo {5 6 Public Static voidMain (string[] args) {7 //traversal of an int array8 int[] arr = {11,22,33,44,55,66,77};9 for(intI:arr) {Ten System.out.println (i); One } ASystem.out.println ("---------"); - - //traversal of a string array thestring[] str = {"A cat", "a Dog", "flower", "cursory"}; - for(String s:str) { - System.out.println (s); - } +System.out.println ("---------"); - + //traversal of the collection collection Acollection<integer> C =NewArraylist<integer>(); atC.add (2008); -C.add (2012); -C.add (2016); - for(Integer i:c) { - System.out.println (i); - } inSystem.out.println ("---------"); - to //traversal of the ArrayList collection +arraylist<string> s =NewArraylist<string>(); -S.add ("Java"); theS.add ("Java ee"); *S.add ("Javase"); $S.add ("Android");Panax Notoginseng for(String ss:s) { - SYSTEM.OUT.PRINTLN (ss); the } +System.out.println ("---------"); A the //But what happens when you iterate over an empty collection? +list<double> L =NULL;//a double in uppercase is a reference type - //NullPointerException $ //This d is what we get out of the list, and before we get it, it's definitely okay to make a judgment . $ //to be blunt, this is the function of the iterator. - //Workaround: Add a condition to the entry statement - if(L! =NULL){ the for(Double d:l) { - System.out.println (d);Wuyi } the } - Wu //enhanced for is actually used to replace iterators - //concurrentmodificationexception About for(String s1:s) { $ if("Android". Equals (S1)) { -S.add ("IOS"); - } - System.out.println (S1); A } + the } -}
ArrayList stores strings and iterates through them. Requires generics to be joined and enhanced for traversal.
3 types of traversal methods are currently in control:
A: iterators
B: General for
C: Enhanced for
1 Importjava.util.ArrayList;2 ImportJava.util.Iterator;3 Public classArrlistdemo {4 5 Public Static voidMain (string[] args) {6 //Create ArrayList Collection7arraylist<string> list =NewArraylist<string>();8 //adding elements to a collection9List.add ("Java");TenList.add ("Java ee"); OneList.add ("Javase"); AList.add ("Android"); - - //First Traversal method: iterator theIterator<string> it =list.iterator (); - while(It.hasnext ()) { -String s =It.next (); - System.out.println (s); + } -System.out.println ("-------"); + A //The second method of traversal: normal for at for(intx = 0; x < List.size (); X + +){ -String S1 =list.get (x); - System.out.println (S1); - } -System.out.println ("-------"); - in //The third method of traversal: Enhanced for - for(String s2:list) { to System.out.println (S2); + } - } the *}
Overview and use of Java 16-9 enhanced for