基本的に、私は独自の配列クラスを実装しようとしていますが、不明な数のプライベート インスタンス変数を生成するパブリック メソッドを作成する方法があるかどうか疑問に思っていましたか? 何かのようなもの:
public class myArray {
public myArray(int length) {
for(int i = 0; i < length; i++) {
int component+i;
}
}
int component(int indexOfComponent) {
return component+indexOfComponent;
}
}
その for ループ内のコード行が意味をなさないことはわかっています。しかし、私が話していることを説明しようと思っただけです。コンストラクターのパラメーターとして 3 を指定して myArray クラスからオブジェクトを作成すると、次のようになります。
public class myArray {
private int component1;
private int component2;
private int component3;
public myArray(int length) {
for(int i = 0; i < length; i++) {
int component+i;
}
int component(int indexOfComponent) {
return component+indexOfComponent;
}
}
}
そして、これらの変数は for ループ内にのみ存在することはわかっていますが、それが私がやろうとしていることを例示できる最良の方法です。独自の配列クラスを実装しようとしている場合でも、これは進むべき道ですか? また; これについては、別の質問に値すると思うことがもう 1 つありますが、それは for ループを使用して動的に変数に名前を付けるという問題全体です。