私はCSクラスのプログラムを作成しようとしていて、問題があります。下のコードでは、私はinitializeArray(firstArray)
とinitializeArray(secondArray)
にエラーメッセージを表示し続けています。2D配列の処理/初期化(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;
}
}
}
異なる方法で綴られています。また、配列[i] [0] = 0;冗長です。 – HomeIsWhereThePcIs
あなたの冗長な声明が私を不思議に思った。そのコードはその声明なしでまったく同じことをするでしょうか? – Daniel
はい。あなたの頭の中で繰り返します。最初の反復ではi = 0なので、array [i] [0]は実際には配列[0] [0] = 0です。行配列[i] [j] = 0に来ると、iとjの両方が0なので、array [0] [0] = 0をもう一度実行します。 – HomeIsWhereThePcIs