【发布时间】:2018-10-04 02:53:49
【问题描述】:
我创建了一个类,islands,为此我在主类中创建了 7 个实例。每个实例都有一个 in 分配,表示需要从每个实例中提取多少个容器。
class Program
{
public static int[,] TEUlayer1 = new int[4, 4] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };
public static int[,] TEUlayer2 = new int[4, 4] { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 0 } };
public static Island is1 = new Island(2);
public static Island is2 = new Island(3);
public static Island is3 = new Island(1);
public static Island is4 = new Island(4);
public static Island is5 = new Island(2);
public static Island is6 = new Island(2);
public static Island is7 = new Island(1);
static void Main(string[] args)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write(TEUlayer1[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write(TEUlayer2[i, j] + " ");
}
Console.WriteLine();
}
}
}
class Island
{
public int S8s4collection { get; set; }
public Island(int s8s)
{
s8s = S8s4collection;
}
}
可能值得忽略 main 方法和 TEUlayer1/2 部分。这些代表一艘小船上的一堆集装箱,但这与问题无关。
我的问题是我可以创建一个循环或其他一些函数,它遍历岛类的每个实例(is1,is2 ...)并将与之关联的值附加到列表或数组等?
谢谢!
【问题讨论】:
-
您对 Island 的类定义毫无意义。它是倒退的。你是说实例化一个 Island 并传入一个 int(s8s 代表什么?你应该有更清晰的变量名)。然后,您使用 S8s4collection 的默认值(因为它是 int,它不能为空,因此为 0)覆盖该值。如果您的目标只是将该数字传递给属性,请将其翻转。虽然你在它的名字中使用了单词集合,所以我想它应该是一个新实例化的列表或数组,而不仅仅是一个单一的 int。
-
长话短说,您这里有一些基本问题,如果没有进一步的说明,我们甚至无法开始帮助您。如果这是您的代码中的错字,请清除它,但现在我投票关闭,因为不清楚。
-
S8s4collection 首先不是集合或数组。也许您应该从那里开始,然后使用更新编辑您的问题。
标签: c# arrays list class append