1、while循環
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.PRint("value of x : " + x ); x++; System.out.print(""); } }}2、do whilepublic class Test { public static void main(String args[]){ int x = 10; do{ System.out.print("value of x : " + x ); x++; System.out.print(""); }while( x < 20 ); }}3、forpublic class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print(""); } }}public class Test { public static void main(String args[]){ int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ System.out.print( x ); System.out.print(","); } System.out.print(""); String [] names ={"James", "Larry", "Tom", "Lacy"}; for( String name : names ) { System.out.print( name ); System.out.print(","); } }}遍歷List集合的三種方法
List<String> list = new ArrayList<String>();list.add("aaa");list.add("bbb");list.add("ccc");方法一:超級for循環遍歷for(String attribute : list) { System.out.println(attribute);}方法二:對于ArrayList來說速度比較快, 用for循環, 以size為條件遍歷:for(int i = 0 ; i < list.size() ; i++) { system.out.println(list.get(i));}方法三:集合類的通用遍歷方式, 從很早的版本就有, 用迭代器迭代Iterator it = list.iterator();while(it.hasNext()) { System.ou.println(it.next);}
新聞熱點
疑難解答