简体   繁体   English

我将如何使用 C# 中多个列表的值填充多维数组

[英]How would I populate a multi-dimensional array using values from multiple lists in C#

This is my Code so far:到目前为止,这是我的代码:

 public static void FillArray(int[,,,] array)
 {

      List<string> BusCompany = new List<string>();


       BusCompany.Add("Sample Company 1");
       BusCompany.Add("Sample Company 2");
        BusCompany.Add("Sample Company 3");
      BusCompany.Add("Sample Company 4");
      BusCompany.Add("Sample Company 5");

      List<string> Route = new List<string>();
      BusCompany.Add("Sample Route 1");
      BusCompany.Add("Sample Route 2");

      List<string> Bus = new List<string>();
      BusCompany.Add("Sample Bus 1");
      BusCompany.Add("Sample Bus 2");

      List<string> Seat = new List<string>();
      BusCompany.Add("Seat 1");
      BusCompany.Add("Seat 2");
      BusCompany.Add("Seat 3");
      BusCompany.Add("Seat 4");
      BusCompany.Add("Seat 5");
      BusCompany.Add("Seat 6");
      BusCompany.Add("Seat 7");
    BusCompany.Add("Seat 8");
    BusCompany.Add("Seat 9");
    BusCompany.Add("Seat 10");

    List<string> Available = new List<string>();
    BusCompany.Add("Yes");

    string[,,,,] reservationTable;

    for (int BC = 0; BC < BusCompany.Count; BC++)
    {
        string BusCompanyValue = BusCompany[BC];
        for (int BR = 0; BR < Route.Count; BR++)
        {
            string BusRouteValue = Route[BR];
            for (int BB = 0; BB < Bus.Count; BB++)
            {
                string BusValue = Bus[BB];
                for (int BS = 0; BS < Seat.Count; BS++)
                {
                    string SeatValue = Seat[BS];
                    reservationTable[BC, BR, BB, BS,];
                }
            }
        }
    }
}

What I want it to do is get the values from the each list, and populate the array in a way where each one becomes a property of the one before.我想要它做的是从每个列表中获取值,并以一种方式填充数组,使每个列表都成为前一个列表的属性。 Sorry don't know how else to explain it.对不起不知道怎么解释。 For example one full thing would be例如,一件完整的事情是

 ["Sample Company 1", "Sample Route 1", "Sample Bus 1", "Seat 1", "Yes"]

while another would be而另一个会是

 ["Sample Company 1", "Sample Route 1", "Sample Bus 1", "Seat 2", "Yes"]

and so on until all the different variations have been added.依此类推,直到添加了所有不同的变体。 The "Yes" at the end is always the same value.最后的“是”总是相同的值。

My main attempt has just been the code I showed above, so far I've gotten no result.我的主要尝试只是上面显示的代码,到目前为止我还没有得到任何结果。

What I think you need is somethin glike this我认为你需要的是像这样的东西

class Company{
   string Name;
   List<Route> Routes;
   List<Bus> Buses;

}

class Route{
    string Name;
}

class Bus{
    List<Seat> Seats;
}

class Seat{
    int Number;
    bool Booked;
}

Here is what you asked for, but I think its totally not what you want这是您要的,但我认为这完全不是您想要的

I chnaged the names of the variables to be in line with normal c# naming standards.我更改了变量的名称以符合正常的 c# 命名标准。 (ie BusCompany would be a class not a variable) (即 BusCompany 将是 class 而不是变量)

public static void FillArray() {
    List<string> company = new List<string>();
    company.Add("Sample Company 1");
    company.Add("Sample Company 2");
    company.Add("Sample Company 3");
    company.Add("Sample Company 4");
    company.Add("Sample Company 5");

    List<string> route = new List<string>();
    route.Add("Sample Route 1");
    route.Add("Sample Route 2");

    List<string> bus = new List<string>();
    bus.Add("Sample Bus 1");
    bus.Add("Sample Bus 2");

    List<string> seat = new List<string>();
    seat.Add("Seat 1");
    seat.Add("Seat 2");
    seat.Add("Seat 3");
    seat.Add("Seat 4");
    seat.Add("Seat 5");
    seat.Add("Seat 6");
    seat.Add("Seat 7");
    seat.Add("Seat 8");
    seat.Add("Seat 9");
    seat.Add("Seat 10");

    List<string> available = new List<string>();
    available.Add("Yes");

    int totalRows = company.Count * bus.Count * seat.Count * available.Count * route.Count;

    string[,] table = new string[ totalRows,5];


    int row = 0;
    for (int bc = 0; bc < company.Count; bc++) {
        for (int br = 0; br < route.Count; br++) {
            for (int bb = 0; bb < bus.Count; bb++) {
                for (int bs = 0; bs < seat.Count; bs++) {
                    for (int av = 0; av < available.Count; av++) {
                        table[row, 0] = company[bc];
                        table[row, 1] = route[br];
                        table[row, 2] = bus[bb];
                        table[row, 3] = seat[bs];
                        table[row, 4] = available[av];
                        row++;

                    }
                }
             }
        }
    }
}

What I think you're looking for is a linked list .我认为您正在寻找的是linked list

Traveling from node to node is efficient in time and space.从一个节点到另一个节点的旅行在时间和空间上都是高效的。 You can access the next nodes by using node.Next , and even reassign them, and their next.next.您可以使用node.Next访问下一个节点,甚至重新分配它们以及它们的 next.next。 you can access their values with node.Value .您可以使用node.Value访问它们的值。 These help if you don't have to access them via index.如果您不必通过索引访问它们,这些会有所帮助。 If traversing from one to the next, this is your guy.如果从一个遍历到另一个,这就是你的人。 (Also, seats should really be a property of the Bus class.) (此外,座位实际上应该是巴士 class 的财产。)

Consider ditching all of those for loops, as that's a lot of iteration to go through.考虑放弃所有这些 for 循环,因为这是对 go 的大量迭代。 I would start by using a "pointer" node to traverse all of the lists at the same time, which cuts your runtime down significantly.我将从使用“指针”节点同时遍历所有列表开始,这会显着减少运行时间。

You can store the linked lists in an array for access to each one.您可以将链表存储在一个数组中,以便访问每个链表。

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

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