Home > OS >  How would I populate a multi-dimensional array using values from multiple lists in C#
How would I populate a multi-dimensional array using values from multiple lists in C#

Time:03-22

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.

CodePudding user response:

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

CodePudding user response:

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. (ie BusCompany would be a class not a variable)

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  ;

                    }
                }
             }
        }
    }
}

CodePudding user response:

What I think you're looking for is a 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. you can access their values with 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.)

Consider ditching all of those for loops, as that's a lot of iteration to go through. 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.

  • Related