【问题标题】:adding the values of members of a class to an array c#将类成员的值添加到数组c#
【发布时间】: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 部分。这些代表一艘小船上的一堆集装箱,但这与问题无关。 我的问题是我可以创建一个循环或其他一些函数,它遍历岛类的每个实例(is1is2 ...)并将与之关联的值附加到列表或数组等? 谢谢!

【问题讨论】:

  • 您对 Island 的类定义毫无意义。它是倒退的。你是说实例化一个 Island 并传入一个 int(s8s 代表什么?你应该有更清晰的变量名)。然后,您使用 S8s4collection 的默认值(因为它是 int,它不能为空,因此为 0)覆盖该值。如果您的目标只是将该数字传递给属性,请将其翻转。虽然你在它的名字中使用了单词集合,所以我想它应该是一个新实例化的列表或数组,而不仅仅是一个单一的 int。
  • 长话短说,您这里有一些基本问题,如果没有进一步的说明,我们甚至无法开始帮助您。如果这是您的代码中的错字,请清除它,但现在我投票关闭,因为不清楚。
  • S8s4collection 首先不是集合或数组。也许您应该从那里开始,然后使用更新编辑您的问题。

标签: c# arrays list class append


【解决方案1】:

为了能够遍历您的岛屿,您需要一系列岛屿。

这行得通:

public 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 List<Island> Islands = new List<Island> { new Island(2), new Island(3), new Island(1), new Island(4), new Island(2), new Island(2), new Island(1) };

    static void Main(string[] args)
    {
        Program.PrintLayer(Program.TeuLayer1);
        Program.PrintLayer(Program.TeuLayer2);
        Program.PrintIsland(Program.Islands);

        Console.ReadKey();
    }

    public class Island
    {
        public int S8s4collection { get; private set; }

        public Island(int s8s)
        {
            S8s4collection = s8s;
        }
    }

    private static void PrintIsland(IEnumerable<Island> islands)
    {
        var index = 0;
        foreach (var island in islands)
        {
            Console.WriteLine("Island {0} has a 'S8s4collection' of: {1}", index, island.S8s4collection);
            index++;
        }

        Console.WriteLine();
    }

    private static void PrintLayer(int[,] layer)
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                Console.Write(layer[i, j] + " ");
            }

            Console.WriteLine();
        }

        Console.WriteLine();
    }
}

虽然将所有内容都设为静态并不理想。哦,你的 Island 构造函数错了。

输出:

output when running the above

【讨论】:

  • 是的,对此表示歉意。那是个错误。本来是要评论的。由于它在答案部分,我现在已将其更改为实际答案
【解决方案2】:

似乎除了提到的岛级吉利德鸭的问题之外。就像你只想要一系列岛屿一样。

public static Island[] is = new Island[7];
public static Island is[1] = new Island(2);
...

如果你想循环播放

for(int i=0;i<is.Length;i++) {
  is[i].DoSomething () ;
} 

【讨论】:

    猜你喜欢
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多