当前位置: 首页 > news >正文

复习知识点十之方法的重载

目录

方法的重载 

  练习1:

练习1: 数组遍历

 练习2: 数组的最大值

练习3:

练习4: 复制数组

基本数据类型和引用数据类型


方法的重载 

 Java虚拟机会通过参数的不同来区分同名的方法

 

  练习1:

public class Test4 {public static void main(String[] args) {//调用方法
//        compare(10,30);// compare((byte) 10,(byte) 20);short b1 = 10;short b2 = 20;compare(b1,b2);}//会把相同功能的方法名起成一样的名字//好处1 : 定义方法的时候可以不用那么多的单词了//好处2 : 调用方法是时候也不需要那么麻烦了public static void compare(byte b1, byte b2) {System.out.println("byte");System.out.println(b1 == b2);}public static void compare(short s1, short s2) {System.out.println("short");System.out.println(s1 == s2);}public static void compare(long n1, long n2) {System.out.println("long");System.out.println(n1 == n2);}public static void compare(int i1, int i2) {System.out.println("int");System.out.println(i1 == i2);}
}

练习1: 数组遍历

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

分析:

定义方法用于数组的遍历
1. 我要干嘛? 遍历数组
2. 我做这件事需要什么才能完成? 数组
3. 方法的调用处是否需要继续使用结果? 不需要返回值
public class Test5 {public static void main(String[] args) {//1. 定义数组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("]");}
}

 

扩展: 

 练习2: 数组的最大值

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

分析:

    1. 我要干嘛?    求最大值
    2. 我干这件事情,需要什么才能完成?  数组
     3. 是否需要返回值?  是

package com.itheima.test;public class Test6 {public static void main(String[] args) {//1. 定义数组int[] arr  ={1,2,9 ,4,5};//2. 调用方法求最大值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;}
}

练习3:

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

分析:

1. 我要做什么?            判断一个数是否存在

2. 我干这件事情,需要什么才能完成?  数组
 3. 调用处是否需要继续使用结果?  返回  true false

public class Test6 {public static void main(String[] args) {//1. 定义数组int[] arr  ={16,2,9 ,4,522,7,3};//2. 判断数字在数组中是否存在了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;}
}

练习4: 复制数组

 分析在代码的注释里面

 

public class Test8 {public static void main(String[] args) {//1. 定义数组int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};//2. 调用方法拷贝数据int[] copyArr = copyOfRange(arr, 3, 7);//3.遍历copyArrfor (int i = 0; i < copyArr.length; i++) {System.out.print(copyArr[i] + " ");}}//将数组arr中从索引 from (包含 from) 开始,到索引 to 结束(不包含 to) 的元素复制到新的数组中public static int[] copyOfRange(int[] arr, int from, int to) {//1. 定义数组int[] newArr = new int[to - from];//2. 把原始的数组arr中的from 到 to 上对应的元素,直接拷贝到newArr中//伪造变量int index = 0;for (int i = from; i < to; i++) {//格式:数组名[索引] = 数据值newArr[index] = arr[i];index++;}//3. 把新数组返回return newArr;}
}

 

 

基本数据类型和引用数据类型

 

http://www.lryc.cn/news/23496.html

相关文章:

  • 火爆全网的ChatGPT 和AI 可以为项目经理做什么?
  • 前端面试题 —— HTML
  • 同为(TOWE)电源线让家用电器随心放置
  • 2023上半年数学建模竞赛汇总(报名时间、比赛时间、难易程度、含金量、竞赛官网)
  • RK3568平台开发系列讲解(驱动基础篇)SMP(Symmetrical Multi-Processing)
  • HIVE --- zeppelin安装
  • 数据分析中的变量解释
  • django-博客(一)
  • Shell高级——Linux中的文件描述符
  • 洗地机哪个品牌最好用?家用洗地机十大名牌
  • java多线程(十)线程休眠
  • Leetcode20. 有效的括号
  • Android 项目必备(四十三)-->Android 开发者的 new 电脑
  • 如何水平和垂直居中元素
  • Rust泛型Generics
  • 六、并发集合
  • PHY调试经验
  • 从Java培训班出来好找工作吗?
  • 第51天|LeetCode503.下一个更大元素 II、LeetCode42. 接雨水
  • [12]云计算概念、技术与架构Thomas Erl-第5章 云使能技术
  • 超实用的公众号用户运营方案分享,纯干货
  • Git ---- 国内代码托管中心-码云
  • 【学习笔记】NOIP爆零赛8
  • 【Linux驱动】驱动设计硬件基础----串口、I2C、SPI、以太网接口、PCIE
  • 同为(TOWE)防雷产品助力福建移动南平分公司防雷改造
  • Win10安装mediapipe的步骤
  • 项目调研丨以太坊再质押项目EigenLayer白皮书四大看点(内附完整版中文白皮书)
  • 51-Jenkins-Periodic Backup插件实现Jenkins备份
  • C++之入门之引用,内联函数
  • linux kprobe使用