user3541767
user3541767

Reputation: 11

two dimension arrays C# how to do a for loop? or search through the array?

So I'm having trouble with arrays, i'm new to c#. This is in the class program;

    enum Stations { FortitudeValley, Central, SouthBank, Toowong, Taringa};
    const int No_OF_TRAINS = 55;
    const int No_OF_STATIONS = 5;
    static int[,] timetable = new int[No_OF_STATIONS, No_OF_TRAINS];

I know how to get the user's input for the departing/arriving stations and their desired time to be at their arrival station. I need to match the user's desired arrival time to a time in the array. My question is how do I do this? How do I do a 'for loop' that goes through each station and how do i look in the array for a value that equals the user's desired arrival time? I'm assuming that the first index is the train stations and the second is the times.

Upvotes: 0

Views: 165

Answers (1)

Ehsan
Ehsan

Reputation: 32681

You are using a wrong data structure here. You should take a look at Dictionary. And then use it like this (perhaps)

Dictionary<string, List<DateTime>>

where string will be the station name, and List will contain your departing times.

Furthermore, you can create your own class and store all the attributes of your station in that class. Then you can use that in dictionary or via direct LINQ queries.

Edit:

You need to use nested for loops to loop through the multi-dimensional arrays. Take a look here

Upvotes: 2

Related Questions