Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
Subtract the elements of two lists from each other with unknown element lengths in C#

I have two lists in my program that the length of the lists is not equal. I want to subtract the members of these two lists element by element and save them in one list.

            List<double> arrivals = new List<double>();
            List<double> departure = new List<double>();
            List<double> delays = new List<double>();
            double departure_p = 0.8;
            double arrival_p = 0.45;
            int slot = 0;
            while (slot < 1000)
            {
                Random a = new Random();
                double arrival_t = a.NextDouble();
                Random d = new Random();
                double departure_t = d.NextDouble();

                if (departure_t <= departure_p)
                {
                    departure.Add(departure_t);
                }
                if (arrival_t <= arrival_p)
                {
                    arrivals.Add(arrival_t);
                }
                slot++;
           }

When I use this method I encounter the exception system.IndexOutOfRangeException.


for (int i = 0; i <= arrivals.Count; i++)
            {
                delays.Add(departure[i] - arrivals[i]);
            }
            foreach (var item in delays)
                Console.WriteLine(item);

How can I do this?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you write i <arrivals.Count in the for loop, the problem will be solved. You can also use the Zip method in Linq.

var delay = departure.Zip(arrivals, (depart, arri) => depart - arri);
            
            foreach (var item in delay)
            {
                Console.WriteLine(item);
            }
over 4 years ago · Santiago Trujillo Relatório

0

Well, IndexOutOfRangeException means that index i exceeds Count of either departure or arrivals so departure[i] - arrivals[i] becomes illegal. You can amend the for loop as

for (int i = 0; i < Math.Min(arrivals.Count, depart.Count); i++)
  delays.Add(departure[i] - arrivals[i]); 

foreach (var item in delays)
  Console.WriteLine(item);

with i < Math.Min(arrivals.Count, depart.Count) conditon we now guarantee, that i will never be beyond both departure and arrivals

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda