配列をパブリック(クラス内の他のメソッドにアクセス可能)にする必要がありますが、配列を作成するには入力値「T」が必要です。ユーザー入力を必要とする「グローバル」変数をインスタンス化するにはどうすればよいですか?
私のコードは次のとおりです。
public class PercolationStats {
**private double myarray[];**
public PercolationStats(int N, int T) {
**double myarray = new double[T];**
for (i=0;i<T;i++) {
Percolation percExperiment as new Percolation(N);
//do more stuff, make calls to percExperiment.publicmethods
myarray[i] = percExperiment.returnvalue;
}
}
public static void main(String[] args) {
int N = StdIn.readInt();
int T = StdIn.readInt();
PercolationStats percstats = new PercolationStats(N, T);
//do more stuff, including finding mean and stddev of myarray[]
StdOut.println(output);
}
擬似コードの別の例:
class PercolationStats {
Constructor(N, T) {
new Percolation(N) //x"T" times
}
Main {
new PercolationStats(N, T) //call constructor
}
}
class Percolation {
Constructor(N) {
**new WQF(N)** //another class that creates an array with size dependent on N
}
Main {
**make calls to WQF.publicmethods**
}
}
2番目の例では、パラメーターNを受け入れるために、PercolationのコンストラクターでクラスWQFの新しいインスタンスを作成する必要があるように思われます。ただし、WQFはPercolationのMainメソッドにアクセスできません。ヘルプ!