我正在编写一个程序,我想知道是否有可能让一个方法将整个数组作为参数在该方法中使用。
示例:
import java.util.Scanner;
public class PascalsTriangle {
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the row of Pascal's triangle that you would like to see: ");
int N = scan.nextInt();
int [] prevHolder = new int [1];
process(prevHolder[], N);
}
public static int process(int [] prevHolder, int N){
int [] holder = new int [N];
for (int i = 0; i < N; i++){
holder[i] = prevHolder[i]+prevHolder[i-1];
}
if (N == 1)
return N;
else{
return process(N - 1, holder[]);
}
}
}
是的,我知道代码不完整。
如果有可能,我可以举个例子。
提前致谢, 杰克
答案 0 :(得分:2)
void printAllStrings(String[] strings) {
for (String string : strings) {
System.out.println(string);
}
}
修改强>
变化
process(prevHolder[], N);
到
process(prevHolder, N);
因为编译器已经知道变量prevHolder
的类型为int[]
。
答案 1 :(得分:0)
public void exampleMethod() {
// create array of strings
String[] strings = new String[1];
strings[0] = "example";
// pass array to another method
processArray(strings);
}
public void processArray(String[] arrayOfStrings) {
for(String str : arrayOfStrings) {
// process your array here
}
}