009-JavaSE基础巩固练习:方法的练习

方法的练习

一、数组遍历

  • 需求

    • 设计一个方法用于数组遍历,要求遍历的结果是在一行上的。
    • 例如:[11, 22, 33, 44, 55]
  • 实现

    package com.app.demo11_method_practice;
    
    import java.util.Arrays;
    
    /**
        方法的练习
            遍历数组:
                需求:设计一个方法用于数组遍历,要求遍历的结果是在一行上的。
                例如:[11, 22, 33, 44, 55]
     */
    public class MethodDemo1 {
        public static void main(String[] args) {
            // 1、定义数组,存入一批数据
            int[] arr1 = {11, 22, 33, 44, 55};
            int[] arr2 = {5, 2, 3, 1, 8};
    
            // 2、调用数组遍历的方法,传入要遍历的数组
            traverseArray(arr1);
            traverseArray(arr2);
        }
    
    
        /**
         * 实现方法:数组遍历
         * @param arr      要遍历的数组
         */
        private static void traverseArray(int[] arr) {
            // 要遍历的数组如果为null,则返回null
            if (arr == null) {
                System.out.println("要遍历的数组不能为null!!");
                return;
            }
    
            // 不为null,开始遍历
            System.out.print("[ "); // 在头部拼接一个 "[ "
            for (int i = 0; i < arr.length; i++) {
                // 使用三元运算符判断:
                // 如果遍历到的元素是最后一位(数组长度-1),则拼接一个 " ]"
                // 如果遍历到的元素不是最后一位,则拼接一个 ", "
                System.out.print(i == arr.length -1 ? arr[i] + " ]" : arr[i] + ", ");
            }
            System.out.println();
        }
    }
    
    [ 11, 22, 33, 44, 55 ]
    [ 5, 2, 3, 1, 8 ]
    
    Process finished with exit code 0
    
    


二、数组最大值

  • 需求

    • 设计一个方法求数组的最大值,并将最大值返回。
  • 实现

    package com.app.demo11_method_practice;
    
    /**
     * 方法的练习
     * 求数组最大值:
     * 需求:设计一个方法求数组的最大值,并将最大值返回
     */
    public class MethodDemo2 {
        public static void main(String[] args) {
            // 1、定义数组,存入一批数据
            double[] scores = {90.5, 65, 100, 59.5, 23};
            double[] faces = {1000, 2000, 500, -190, 8000};
    
            // 2、调用求数组最大值的方法,传入数组,并接收返回结果
            double scoreMax = arrayMaxValue(scores);
            double faceMax = arrayMaxValue(faces);
    
            System.out.println("scores数组中的最高分是:" + scoreMax);
            System.out.println("faces数组中的最高颜值是:" + faceMax);
        }
    
    
        /**
         * 实现方法:求数组最大值
         *
         * @param arr 传入的数组
         * @return 返回数组中的最大值
         */
        private static double arrayMaxValue(double[] arr) {
            // a.传入的数组如果为null,返回-1
            if (arr == null) {
                return -1;
            }
    
            // b.定义最大值变量,记录数组的首元素,用于与数组中其他的元素作比较
            double max = arr[0];
    
            // c.遍历数组,依次得到每一个元素
            for (int i = 0; i < arr.length; i++) {
                // 如果当前元素 大于 首元素,则替换
                if (arr[i] > max) {
                    max = arr[i];
                }
            }
    
            // d.循环结束,说明已经比完了,此时max变量里的值就是数组最大值
            return max;
        }
    }
    
    scores数组中的最高分是:100.0
    faces数组中的最高颜值是:8000.0
    
    Process finished with exit code 0
    
    


三、判断是否存在

  • 需求

    • 定义一个方法判断数组中的某一个数是否存在,将结果返回给调用处。
  • 实现

    package com.app.demo11_method_practice;
    
    /**
     *  方法的练习
     *      判断是否存在:
     *          需求:定义一个方法判断数组中的某一个数是否存在,将结果返回给调用处。
     */
    public class MethodDemo3 {
        public static void main(String[] args) {
            // 1、定义数组,存入一批数据
            int[] score = {100, 88, 44, 66, 2, 1};
    
            // 2、调用判断是否存在的方法,传入数组、要判断的数,并接收返回结果
            String result = numberExists(score, 88);    // 判断88是否存在于数组中
            System.out.println(result);
    
            System.out.println(numberExists(score, 666));   // 判断666是否存在于数组中
    
            System.out.println(numberExists(null, -100));
            System.out.println(numberExists(score, 0));
        }
    
    
        /**
         * 实现方法:判断某个数是否存在于数组中
         * @param arr       传入的数组
         * @param number    要判断的数
         * @return          返回判断结果
         */
        private static String numberExists(int[] arr, int number) {
            // a.如果传入的数组为null或传入的要判断的数为负数、0,则提示
            if (arr == null || number <= 0) {
                return "sorry!您的数组参数为null 或 要判断的参数不为正整数!";
            }
    
            // b.遍历数组,依次得到每一个元素
            for (int i = 0; i < arr.length; i++) {
                // 如果遍历到的元素 与 要判断的数 相等,说明存在
                if (arr[i] == number) {
                    return number + "存在于数组中~";
                }
            }
    
            // c.循环结束,说明判断完了,仍然没有相等的,说明该数不存在
            return number + "不存在于数组中~";
        }
    }
    
    88存在于数组中~
    666不存在于数组中~
    sorry!您的数组参数为null 或 要判断的参数不为正整数!
    sorry!您的数组参数为null 或 要判断的参数不为正整数!
    
    Process finished with exit code 0
    
    


四、复制数组

  • 需求

    • 定义一个方法copyOfRange(int[] arr, int from, int to)
  • 功能

    • 将数组arr中从索引from(包含from)开始
    • 到索引to结束(不包含to)的元素复制到新数组中,将新数组返回。
  • 实现

    package com.app.demo11_method_practice;
    
    import java.util.Arrays;
    
    /**
     * 方法的练习
     *      复制数组:
     *          需求:定义一个方法copyOfRange(int[] arr, int from, int to)
     *          功能:将数组arr中从索引 from(包含from)开始,到索引to结束(不包含to)的元素复制到新数组中,将新数组返回
     */
    public class MethodDemo4 {
        public static void main(String[] args) {
            // 1、定义数组,存入一批数据
            int[] numbers = {1, 3, 4, 1, 6};
            // 索引           0  1  2  3  4
    
            // 遍历输出原数组
            System.out.println("原数组:" + Arrays.toString(numbers));
    
            // 2、调用复制数组的方法,传入一个数组、开始索引(包含开始索引)参数、结束索引(不包含结束索引)参数。
            // 并接收返回结果
            int[] copyArr = copyOfRange(numbers, 1, 3); // 从索引1开始 到 索引3结束,不包含索引3位置的数据
    
            // 3、遍历新数组,依次得到每一个元素
            System.out.print("新数组:[");
            for (int i = 0; i < copyArr.length; i++) {
                System.out.print(i == copyArr.length-1 ? copyArr[i] + "]" : copyArr[i] + ", ");
            }
        }
    
    
        /**
         * 实现方法:拷贝从索引from开始到索引to结束的数组元素到一个新数组中
         * @param arr   要拷贝的数组
         * @param from      开始索引
         * @param to        结束索引
         * @return          返回新数组
         */
        private static int[] copyOfRange(int[] arr, int from, int to) {
            // a.如果传入的数组为null 或 from参数为负数 或 to参数为负数,则返回null
            if (arr == null || from < 0 || to < 0) {
                return null;
            }
    
            // b.定义动态数组,用于存储拷贝的数据
            // to - from:
            //      假如你要拷贝 1 到 3 索引的数据,
            //      那就是要 1 2 索引的数据,包含开始不包含结束
            //      那么就是要2个数据,那么新数组长度就是 3-1=2
            int[] newArr = new int[to - from];
    
            // c.伪造索引的思想
            int index = 0;
    
            // d.遍历数组,依次得到开始索引到结束索引的每一个元素
            // i = from: 从from索引开始遍历
            // i < to: 到to索引结束遍历
            for (int i = from; i < to; i++) {
                // 每得到一个数据,赋值给新数组
                // newArr[index]: 代表是新数组的0索引位置,也就是首位置
                // newArr[index] = arr[i]: 将数据拷贝到新数组的第一个位置
                newArr[index] = arr[i];
    
                // 拷贝完数据后,让新数组的索引位置往后移一位,以便于给拷贝下一个数据腾位置
                index++;
            }
    
            // e.循环结束,说明拷贝完成,返回新数组
            return newArr;
        }
    }
    
    原数组:[1, 3, 4, 1, 6]
    新数组:[3, 4]
    Process finished with exit code 0
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值