System.ArgumentOutOfRangeException

时间:2018-04-15 23:38:40

标签: c#

我在设计一个关于搜索多城市航班的计划时遇到了问题。无论出于何种原因,我的代码都给出了一个错误,我无法弄清楚这个bug的生命。如果你能伸出援助之手,那么下周,可能是一个月,你将成为我最喜欢的人。非常感谢你!

MultiFactory.cs的代码:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;

namespace Reservation_Library
{
    public class MultiFactory
    {
        StreamWriter sw;
        XmlSerializer serial;
        List<MultiCityFlight> finalMatches;
        List<FlightRequest> flightList;
        List<Flight> entireList;
        List<Flight> multiCityList;
        const string FLIGHT_REQUEST = @"..\..\flightRequest.xml";
        const string MULTI_FILE = @"..\..\multiFlights.xml";
        const string FLIGHT_FILE = @"..\..\flights.xml";
        const string MATCH_FILE = @"..\..\match.xml";

        DateTime date;
        Random random = new Random();

        public DateTime randomDate()  //Generates a random date for the flight within the provided timeframe
        {
            int month = random.Next(6, 8);
            int day = random.Next(1, 31);
            int Day = random.Next(1, day);

            if (month == 7 && day > 1)
            {
                month = random.Next(6, 7);
                day = random.Next(1, 31);
            }

            date = new DateTime(2017, month, Day);

            return date;
        }



       /*public void SerializeMultiFlight(List<MultiCityFlight> multiFlights) //Simple serializer for writing full list to xml
        {
            serial = new XmlSerializer(finalMatches.GetType());
            sw = new StreamWriter(MULTI_FILE);
            serial.Serialize(sw, multiFlights);
            sw.Close();
        }*/

        public List<MultiCityFlight> findConnections(int choice) //Method for comparing the entire list of flights against the selected flight for matches. All matching flights added to a matchingFlights list
        {
            FlightFactory flight = new FlightFactory();
            entireList = new List<Flight>();
            List<Flight> firstConnections = new List<Flight>(); //Flights on same date as request, must have matching departure point
            List<Flight> secondConnections = new List<Flight>();
            List<FlightRequest> flightList = generateFlightRequests();
            finalMatches = new List<MultiCityFlight>();

            entireList = flight.generateFlightList();
            foreach(Flight f in entireList)
            {
                Console.Write(f.ToString());
            }

            FlightRequest selected = flightList[choice];

            for (int i = 0; i < entireList.Count; i++)
            {

                if (entireList[i].DateAndTime == selected.date &&
                    entireList[i].OrigAirport == selected.origAirport)
                {
                    firstConnections.Add(entireList[i]);
                }
                Console.WriteLine(firstConnections[0]);


                if (entireList[i].DateAndTime == selected.date && entireList[i].DestAirport == selected.destAirport)
                {
                    secondConnections.Add(entireList[i]);
                }


                /*using (var e1 = firstConnections.GetEnumerator())
                using (var e2 = secondConnections.GetEnumerator())
                {
                    while (e1.MoveNext() && e2.MoveNext())
                    {


                        Flight f1 = e1.Current;
                        Console.WriteLine(f1.ToString());
                        Flight f2 = e2.Current;
                        Console.WriteLine(f2.ToString());

                        TimeSpan t = f2.DateAndTime - f1.DateAndTime;
                        int diff = (int)t.Hours;

                        if (diff > 1 && f1.DestAirport == f2.OrigAirport)
                        {
                            finalMatches.Add(new MultiCityFlight(f1, f2));
                        }
                    }
                }*/
            }

            return finalMatches;
        }

        public List<FlightRequest> DeserializeFlightList() //Simple method for deserializing from xml to a list
        {
            flightList = new List<FlightRequest>();
            StreamReader sr = new StreamReader(FLIGHT_REQUEST);
            serial = new XmlSerializer(flightList.GetType());
            flightList = (List<FlightRequest>)serial.Deserialize(sr);
            sr.Close();
            return flightList;

        }

        public List<FlightRequest> generateFlightRequests() //Generating 20 random flights to search for matching flights
        {
            flightList = new List<FlightRequest>();

            FlightRequest f = new FlightRequest("MIA", "SEA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("MIA", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("MIA", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "SEA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "SEA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("MIA", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "LAX", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "SEA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("MIA", "DEN", randomDate());
            flightList.Add(f);
            f = new FlightRequest("SEA", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("DEN", "MIA", randomDate());
            flightList.Add(f);
            f = new FlightRequest("LAX", "SEA", randomDate());
            flightList.Add(f);

            return flightList;
        }
    }
}

这是发生错误的行:

Console.WriteLine(firstConnections[0]);

如果您需要其他课程或方法的代码,请询问。非常感谢!

1 个答案:

答案 0 :(得分:0)

显然你得到这个错误,因为List是空的。

只需调试一些代码:

Console.WriteLine(entireList[i].DateAndTime + " == "+selected.date+" | "+entireList[i].OrigAirport+" == "+selected.origAirport);

你应该看到,这不是真的。所以没有添加任何东西,所以你超出了范围:D