Home > Software design >  ERROR: Index was out of range: if (formula2MatrixResult[i][j] < 0), 2D List<> in C#
ERROR: Index was out of range: if (formula2MatrixResult[i][j] < 0), 2D List<> in C#

Time:11-17

 // x1 - x2, x1 - x3, x1 - x4 ...
    private List<List<double>> Formula2(List<List<double>> formula1MatrixResult)
    {
        List<List<double>> formula2List = new List<List<double>>();
        int cwc = CriteriaWeights.Count; //in this case is 5
        for (int i = 0; i < cwc; i  ) 
        {
            List<double> formula2 = new List<double>();
            var ac = Alternatives.Count; //in this case is 5

            for (int j = 0; j < ac; j  ) 
            {
                for (int k = 0; k < Alternatives.Count; k  )
                {
                    if (k == j)
                        k  ;
                    else
                        formula2.Add(formula1MatrixResult[i][j] - formula1MatrixResult[i][k]);  
                } 
            }
            formula2List.Add(formula2);
        }
        return formula2List;

    }


    // if (x<0, 0) else (x * weight)
    private List<List<double>> Formula3(List<List<double>> formula2MatrixResult, List<double> criteriaWeights)
    {
        List<List<double>> formula3List = new List<List<double>>();
        int combinations = (Alternatives.Count * (Alternatives.Count - 1));
        List<double> sumList = new List<double>();

        for (int i = 0; i < CriteriaWeights.Count; i  ) //rows
        {
            List<double> sum1 = new List<double>();
            List<double> formula3 = new List<double>();


            for (int j = 0; j < combinations; j  ) //col
            {

                if (formula2MatrixResult[i][j] < 0)
                    formula3.Add(0);

                else
                    formula3.Add(formula2MatrixResult[i][j] * criteriaWeights[i]);

                sum1.Add(formula3[j]);
            }
            formula3List.Add(formula3);
            double result = sum1.Aggregate((sum, val) => sum   val);
            sumList.Add(result);
            //formula3.Clear();
        }
        return formula3List;
    }

I am getting an error saying the index is out of bound and I suspect it has to do with a mistake in Formula2 method. The formuala2Matrix(formula2 method) is supposed to be a 20 columns by 5 row matrix and I am not sure if thats what I created in Formula2 method. If yes, then I got get why I have an error in formula3 method. Thank you for ur help!

excel diagram

CodePudding user response:

in Formula2, the k has been added to k 2

  1. k in the for loop
  2. k in if (k == j)
for (int k = 0; k < Alternatives_Count; k  )
{
    if (k == j)
        k  ;
    else
        formula2.Add(formula1MatrixResult[i][j] - formula1MatrixResult[i][k]);
}

if you want to add only once, you can write like this:

for (int k = 0; k < Alternatives_Count; k  )
{
    if (k != j)
    {
        formula2.Add(formula1MatrixResult[i][j] - formula1MatrixResult[i][k]);
    }
}
  • Related