2021 09-07 Java 01

## 数组遍历
package come.itheima;
/*数组遍历
* 需求:
*    设计一个方法用于数组遍历
* */
public class Day090701 {
    public static void main(String[] args) {
        int[] arr = {11,22,33,44,55,66};
        printArray(arr);
    }
    //定义一个方法,用于数组遍历通用格式对数组进行遍历
    /*
    两个明确
       返回值类型void
       参数:int[] arr
            */
    public static void printArray(int[] arr){
        System.out.print("[");
        for(int x=0;x<arr.length;x++){
            if(x==arr.length-1){
                //arr.length-1是最后一个元素
            System.out.print(arr[x]);
            }else{
                System.out.print(arr[x]+",  ");
            }
        }
        System.out.println("]");
    }
}

package come.itheima;
/*
数组最大值
需求:
    设计一个方法用于获取数组中元素的最大值,调用方法并输出结果
思路:
    定义一个数组,用静态初始化完成数组元素初始化
    定义一个方法,用来获取数组中的最大值
    调用获取最大值方法,用变量接收返回效果
    把结果输出在控制台
 */
public class Day090702 {
    public static void main(String[] args) {
        //定义一个数组,用静初始化完成数组元素初始化
        int[] arr ={12,22,31,122,3};
        //用获取最大值方法,用变量接收返回效果
       int numeber = getMax(arr);
        //把结果输出在控制台
        System.out.println(numeber);
    }
    //定义一个方法用来获取数组中的最大值
    /*
    两个明确
    返回值类型 int
    参数 int[] arr
         */
    public static int getMax(int[] arr){
        int max = arr[0];
        for(int x=1;x<arr.length;x++){
            if(arr[x]>max){
                max = arr[x];
            }
        }
        return max;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值