java学习笔记6(方法)

方法

方法(method)是程序中最小的执行单元

  • 注意:
    • 方法必须先创建才可以使用,该过程成为方法定义。
    • 方法创建后并不是直接可以运行的,需要手动使用后,才执行,该过程成为方法调用。

方法的通用格式

public static 返回值类型 方法名(参数) {
方法体;
return 数据 ;
}

  • public static 修饰符
  • 返回值类型 方法操作完毕之后返回的数据的数据类型
    如果方法操作完毕,没有数据返回,这里写void,而且方法体中一般不写return
  • 方法名 调用方法时候使用的标识
  • 参数 由数据类型和变量名组成,多个参数之间用逗号隔开
  • 方法体 完成功能的代码块
  • return 如果方法操作完毕,有数据返回,用于把数据返回给调用者
    可以省略return,也可以单独的书写return,后面不加数据

方法重载

方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载

  • 多个方法在同一个类中
  • 多个方法具有相同的方法名
  • 多个方法的参数不相同:类型不同或者数量不同
  • public class MethodDemo {
    public static void fn(int a) {
    //方法体
    }
    public static int fn(double a) {
    //方法体
    }
    }

  • public class MethodDemo {
    public static float fn(int a) {
    //方法体
    }
    public static int fn(int a , int b) {
    //方法体
    }
    }

小练习

数组遍历

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

package com.itheima.test;

public class Test5 {
    public static void main(String[] args) {
        int[] arr = {11,22,33,44,55};
        printArr(arr);
    }
    public static void printArr(int[] arr){
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if(i == arr.length - 1){
                System.out.print(arr[i]);
            }else {
                System.out.print(arr[i] + ", ");
            }
        }
        System.out.println("]");
    }

}

数组最大值

需求:设计一个方法用于获取数组中元素的最大值,并将最大值返回

package com.itheima.test;

public class Test6 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,66,5,2,3};
        int max = getMax(arr);
        System.out.println(max);
    }
    public static int getMax(int[] arr){
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(arr[i] > max){
                max = arr[i];
            }
        }
        return max;
    }
}

数组中是否存在某数

需求:定义一个方法查看数组中是否有某一数字存在,将结果返回给调用处

package com.itheima.test;

public class Test7 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,66,52,100};
        boolean flag = contains(arr, 7);
        System.out.println(flag);
    }
    public static boolean contains(int[] arr, int number){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == number){
                return true;
            }
        }
        return false;
    }
}

复制数组

需求:定义一个方法copyOfRange(int[ ] arr, int from, int to),
将数组arr中从索引from(包含from)开始,
到索引to结束(不包含to)的元素复制到新数组中,
将新数组返回。

package com.itheima.test;

public class Test8 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] copyArr = copyOfRange(arr, 3, 7);
        for (int i = 0; i < copyArr.length; i++) {
            System.out.println(copyArr[i]);
        }
    }
    public static int[] copyOfRange(int[] arr, int from, int to) {
        int[] newArr = new int[to - from];
        for (int i = from; i < to; i++) {
            newArr[i-from] = arr[i];
        }
        return newArr;
    }
}

引用

https://www.bilibili.com/video/BV17F411T7Ao?spm_id_from=333.999.0.0

以上内容仅作为个人学习笔记,无商业行为。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值