初めての質問ですのでルール違反等ありましたら申し訳ありません。
基本的に、オブジェクト (ワーム) を含むギザギザの配列 (りんご) があります。私が遭遇した主な問題は、この配列の要素をコンソール ウィンドウに書き込むことです。多かれ少なかれ、配列に適切にインデックスを付ける方法がわかりません。
object[][] Apples = new object[2][]; //This says that we will declare a jagged array of objects. Our Apples array will contain 2 elements, each of which is a single dimensional array
Apples[0] = new object[2]; //initializing the elements, saying element 0 of apples will be an object, in our case an array, with 2 element
Apples[1] = new object[2]; //initializing the elements, saying element 1 of apples will be an object, in our case an array, with 2 elements
Apples[0][0] = new Worm(22, "Matt"); //the first element in the array of worm in the first element of the array of apple
Apples[0][1] = new Worm(23, "Marty");//the second element in the array of worm in the first element of the array of apple
Apples[1][0] = new Worm(24, "Mike"); //the first element in the array of worm in the second element of the array of apple
Apples[1][1] = new Worm(25, "Pete"); //the second element in the array of worm in the second element of the array of apple
for (int i = 0; i < Apples.Length; i++) //this is the only thing i'm having trouble with
{
for (int j = 0; j < Apples[i].LongLength; j++)
{
System.Console.WriteLine("Element[{0}][{1}]: ", i, j);
System.Console.Write(Apples[i].ToString());
System.Console.Write("\n\r");
}
}
System.Console.WriteLine("\n\rPress any key to exit.");
System.Console.ReadKey();
私のワームクラスは次のとおりです。
public class Worm
{
public Worm(int WormAge, string WormName) //constructor that accepts two arguments
{
int age = WormAge;
string name = WormName;
}
}