我一直在爲我的CS類編寫一個程序,並遇到問題。在下面的代碼中,我不斷收到一條錯誤消息:initializeArray(firstArray)
和initializeArray(secondArray)
。處理/初始化二維數組(Java)
消息說the method initializeArray(int[][]) is undefined for type processArray
。任何人都可以向我解釋我在這裏做錯了什麼。我需要能夠在我的processArray方法中調用方法initializeArray。
public class ProcessArray {
// The constructor for the class: it initializes the attributes "rows" and "columns", and also completes the
// declarations for the two arrays using the dimensions received. It then calls a private method in the class
// to set the values in both arrays to 0.
public ProcessArray (int rows, int columns){
int[][] firstArray = new int[rows][columns];
int[][] secondArray = new int[rows][columns];
initializeArray(firstArray);
initializeArray(secondArray);
}
// This private utility method sets the values in both arrays to 0.
private void intializeArray(int[][] array){
int rows = array.length;
int columns = array[0].length;
for(int i = 0; i < rows; i++){
array[i][0] = 0;
for(int j = 0; j< columns; j++){
array[i][j] = 0;
}
}
}
你有方法的名稱拼寫錯誤拼寫。 array [i] [0] = 0;是多餘的。 – HomeIsWhereThePcIs
你多餘的陳述讓我好奇。如果沒有這種說法,代碼會做同樣的事情嗎? – Daniel
是的。經歷你的腦海中的迭代。在第一次迭代中,i = 0,所以array [i] [0]實際上是array [0] [0] = 0;那麼你來到行數組[i] [j] = 0,我和j都是0,所以你再次做數組[0] [0] = 0。 – HomeIsWhereThePcIs