这是我的代码: -
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Please enter number");
numbers[i] = input.nextInt();
}
System.out.println("Please enter your key");
int k =input.nextInt();
int Low=numbers[0];
int H=numbers[4];
Recursion(k,numbers ,Low , H);
}
如何将数组传递给方法? 我在最后一行收到错误
我试图让方法无效并在方法中做所有这些事情,但我得到错误?
答案 0 :(得分:0)
Java总是通过值将参数传递给方法。在Arrays / Objects的情况下,它也按值传递,但在这种情况下,它的值是一个引用。
以下是您可以用来将数组传递给方法并再次返回的代码片段。
import java.util.HashMap;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] numbers = new int[5];
Scanner input = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.println("Please enter number");
numbers[i] = input.nextInt();
}
System.out.println("Please enter your key");
int k = input.nextInt();
int Low = numbers[0];
int H = numbers[4];
int[] recursiveResult = recursion(k, numbers, Low, H);
}
private static int[] recursion(int k, int[] numbers, int low, int h) {
// TODO Auto-generated method stub
// Some Operation
return numbers;
}
}