codecamp

循环

各种循环操作

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class ForTest {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /* 1.普通数组中的使用 */
        int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };


        // 增强for循环
        for (int item : array) {
            System.out.println(item);
        }


        /* 2.二维数组中的使用 */
        int array2[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };


        // 增强for循环(foreach)
        for (int arr[] : array2) {
            for (int item : arr) {
                System.out.println(item);
            }
        }


        // 普通for循环
        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                System.out.println(array2[i][j]);
            }
        }


        /* 3.List中的使用 */
        List<String> list = new ArrayList<String>();
        list.add("我");
        list.add("爱");
        list.add("中");
        list.add("国");


        // 增强for循环
        for (String item : list) {
            System.out.println(item);
        }


        // 普通for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }


        // 迭代器遍历
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}
switch
CharSequence
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }