做公司网站有什么猫腻广告推广网站
方法的练习
一、数组遍历
-
需求:
- 设计一个方法用于数组遍历,要求遍历的结果是在一行上的。
- 例如:[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,则返回nullif (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,返回-1if (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.0Process 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参数为负数,则返回nullif (arr == null || from < 0 || to < 0) {return null;}// b.定义动态数组,用于存储拷贝的数据// to - from:// 假如你要拷贝 1 到 3 索引的数据,// 那就是要 1 2 索引的数据,包含开始不包含结束// 那么就是要2个数据,那么新数组长度就是 3-1=2int[] 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