数组
1. 数组类型的返回值
数组依然可以作为返回值类型,与之前的使用方式一致
package com.edu.test1; import java.util.Scanner; /** * 数组类型的返回值 * * 编写方法提示用户输入5门成绩 然后返回 * */ public class Test1 { public static double[] inputScore() { Scanner input = new Scanner(System.in); double [] scores = new double[5]; for(int i = 0;i < scores.length;i++) { System.out.println("请输入第" + (i + 1) + "门成绩"); scores[i] = input.nextDouble(); } return scores; } public static void main(String[] args) { double [] scores = inputScore(); for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); } } }
2. 可变长参数
可变长参数
可以传入0个或者多个同类型的参数
写法:数据类型... 形参名
要求:形参列表中只能有一个可变长参数,并且必须在形参列表的末尾
package com.edu.test2; /** * 可变长参数 * 可以传入0个或者多个同类型的参数 * 写法:数据类型... 形参名 * 要求:形参列表中只能有一个可变长参数,并且必须在形参列表的末尾 * */ public class Test1 { public static void m1(int... nums) { for(int i =0;i < nums.length;i++) { System.out.println(nums[i]); } } public static void m2(int[] nums) { for(int i =0;i < nums.length;i++) { System.out.println(nums[i]); } } public static void main(String[] args) {