简体   繁体   English

将类的成员值添加到数组C#

[英]adding the values of members of a class to an array c#

I have create a class, islands, for which i have created 7 instances in the main class. 我创建了一个类孤岛,为此我在主类中创建了7个实例。 Each instance has an in assigned, which represents how many containers need to be picked up from each. 每个实例都有一个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;
    }
}

it is probably worth ignoring the main method and TEUlayer1/2 parts. 可能值得忽略主要方法和TEUlayer1 / 2部分。 these represent a stack of containers on a small ship, but this isn't relevant to the question. 这些代表了一艘船上的一堆集装箱,但这与问题无关。 My question is can I create a loop, or some other function, that goes through each instance of the island class ( is1 , is2 ...) and appends the value associated with it to a list or array etc? 我的问题是,我可以创建一个循环,或者一些其他的功能,即经过岛上类的每个实例( is1is2 ...),并附加与它相关联的列表或数组等价值? thanks! 谢谢!

To be able to iterate over your islands you need a collection of Islands. 为了能够遍历您的岛屿,您需要一个岛屿集合。

This works: 这有效:

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();
    }
}

Although having everything as static isn't ideal. 虽然将所有内容都设为静态并不理想。 Oh and your Island constructor was wrong. 哦,您的Island构造函数是错误的。

Output: 输出:

output when running the above 运行上面的输出

besides the problems with the islands class gilliduck mentioned, it seems. 看来,除了岛屿类吉利达克提到的问题外。 like you just want an array of islands. 就像您只想要一系列的岛屿。

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

then if you wamt to loop through 那如果你想遍历

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM