List<List<double>> A = new List<List<double>>()
{
new List<double>() { 1, 0, 0 },
new List<double>() { 1, -1, 0 }
};
List<List<double>> Temm = new List<List<double>>();
for (int i = 0; i < A.Count; i++)
{
for (int j = 0; j < A[i].Count; j++)
{
if (A[i][j] != 0)
{
Temm[i][j] = A[i][j];
}
else
{
Temm[i][j] = Temm[i][j - 1]; }
}
}
}
There is something wrong with this double for loop, I think Temm[i][j]
maybe illegal, so what's the correct way ? I also wanted to know how to declare the given size of the two dimensional List
List<List<double>> Temm = new List<List<double>>
say
Z * Y
?
You have 2 problems.
When you try to access [i][j] - to one that doesn't yet exist - which is everytime it will fail. Instead use the Insert
method:
List<List<double>> A = new List<List<double>>()
{
new List<double>() { 1, 0, 0 },
new List<double>() { 1, -1, 0 }
};
List<List<double>> Temm = new List<List<double>>(A.Count);
for (int i = 0; i < A.Count; i++)
{
Temm.Insert(i,new List<double>());
for (int j = 0; j < A[i].Count; j++)
{
if (A[i][j] != 0) { Temm[i].Insert(j,A[i][j]); }
else { Temm[i].Insert(j,Temm[i][j - 1]); }
}
}
Because each time you insert to the end of the list I would prefer to use the Add
:
List<List<double>> B = new List<List<double>>();
for (int i = 0; i < A.Count; i++)
{
List<double> innerResult = new List<double>();
for (int j = 0; j < A[i].Count; j++)
{
if (A[i][j] != 0)
{
innerResult.Add(A[i][j]);
}
else
{
innerResult.Add(innerResult[j - 1]);
}
}
B.Add(innerResult);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With