显示索引C#

时间:2017-01-26 05:37:59

标签: c# list indexof

我的问题是我有M子问题。每个子问题包括x(双数组)和降低的成本,其对于每次迭代m具有不同的值。我想在所有子问题中显示具有最小降低成本的x。这是我的班级:

public class Subproblem
{
    public double[,] x { get; set; }
    public double ReducedCost { get; set; }
}

到目前为止,我已经可以获得最低的降低成本和指数。现在我想在该索引上显示x值(双数组)。我的代码是这样的:

var sub = new List<Subproblem>();
for (int m = 0; m < M; ++m)
{
Subproblem s = new Subproblem();
s.x = new double[DC1, DC1];      
s.ReducedCost = model.ObjVal;
    for (int i = 0; i < DC1; ++i)
    {
        for (int j = 0; j < DC1; ++j)
        {
            s.x[i, j] = x[i, j].X;
        }
    }
    sub.Add(s);
}
double minRC = sub.Min(a => a.ReducedCost);
int minRCIndex = sub.FindIndex((i) => i.ReducedCost == minRC);
Console.WriteLine(sub.x(minRCIndex));

最后一行(Console.WriteLine(sub.x(minRCIndex));)仍有红色下划线,我不知道如何写它

3 个答案:

答案 0 :(得分:2)

如果您只是想要获得最小的降低成本的子问题,那么您应该这样做:

Subproblem minimumReducedCostedSubproblem = sub[minRCIndex];

你可以像这样打印矩阵:

for (int i = 0; i < DC1; ++i)
{
    for (int j = 0; j < DC1; ++j)
    {
        Console.Write(minimumReducedCostedSubproblem.x[i, j] + "\t");
    }
    Console.Write("\n");
}

但你似乎有点困惑。您正在将子问题推入sub列表,其中包含相同的M次对象。因为model.ObjVal在第一个for循环中没有变化。那里也有一些奇怪的东西。

答案 1 :(得分:1)

应该是

var objWithMinReduceCost = sub[minRCIndex];
//Now you have the object of Subproblem derived from your logic.
//You can access x property of it have further logic to process it.
for (int i = 0; i < DC1; ++i)
{
    for (int j = 0; j < DC1; ++j)
    {
         Console.WriteLine(objWithMinReduceCost.x[i, j]);
    }
}

答案 2 :(得分:0)

如果您对获取double[,] result = sub.First(i => i.ReducedCost == minRC).x; 数组感兴趣,可以这样做:

ReducedCost

虽然正如Tolga所说,你所有的元素与你当前的代码都有相同的curl -X POST -v -u admin:Password 'http://mysonar/api/permissions/add_group?permission=admin&groupName=mynewgroup'