循环遍历数组方法

循环遍历数组方法总结

1 while循环语句

    while(条件表达式){
        执行语句
    }

2 do…while循环语句

    do{
        执行语句
    }while(条件表达式);
while和do...while区别:
while是先判断条件是否成立再执行循环体
do...while是先执行一次循环再判断条件是否成立
do..while循环体中至少被执行一次

3 for循环语句

    for(初始化表达式 ;循环条件表达式 ; 循环后操作表达式){
        语句序列
    }

4 foreach循环语句

    for(元素变量x : 遍历对象obj){
        引用了x的Java语句;
    }

5 举例一:3中方法

public class Circle {
    public static void main(String[] args) {
        String[] arr = new String[]{"张三","李四","小红","小李","校长","狗儿","花儿","莲儿","荡儿","华儿","赢儿"};
        int index = 0;//索引变量
        System.out.println("数组元素第一种方法:");
        while(index<arr.length){                //while循环遍历数组
            System.out.print(arr[index++]+" ");
        }
        System.out.println();

        System.out.println("数组元素第二种方法:");
        for(String  x  : arr){                  //foreach循环遍历数组
            System.out.print(x+" ");
        }
        System.out.println();

        System.out.println("数组元素第三种方法:");
        for(int a = 0; a < arr.length; a++){    //for循环遍历数组
            System.out.print(arr[a]+" ");
        }
    }
}

6 举例二:九九乘法表

public class MultiplicationTable {
    public static void main(String[] args) {
        for(int i = 1; i <= 9; i++){
            for(int j = 1; j<= i; j++){
                System.out.print(j+"*"+i+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值