简体   繁体   English

C#添加到列表不起作用

[英]C# add to list not working

I am new to C# and I am just trying to create a simple program to perform some basic calculations on VS2010. 我是C#的新手,我只是想创建一个简单的程序来在VS2010上执行一些基本计算。 The problem I'm having is that I don't seem to be able to add the calculated value to a list. 我遇到的问题是我似乎无法将计算出的值添加到列表中。

List<double> distance = new List<double>();
List<double> speed = new List<double>();

// distance and speed are populated using Add()

List<double> slope = new List<double>();
double rise, run;
for (int i= 0; i< distance.Count(); i++)
{
    rise = 0.0;
    run = 0.0;
    if (i> 0)
    {
        rise = speed[i] - speed[i- 1];
        run = distance[i] - distance[i- 1];

        if (rise == 0 || run == 0)
        {
            slope.Add(0.0);
        }
        else slope.Add(rise/run);

    }
    else slope.Add(0.0);
    Console.WriteLine("{0}, {1}, {2}, {3}, {4}", i, rise, run, rise/run, slope);
}

The resulting output is 结果输出是

...
10, -21.7, -0.089, -243.82, System.Collection.Generic.List`1[System.Double]
11, 1.49, -0.088, -16.93, System.Collection.Generic.List`1[System.Double]
12, -4.51, -0.514, 87.74, System.Collection.Generic.List`1[System.Double]

All other uses of the Add() function have been fine, however, these are all adding elements or averages of another list of the same type. Add()函数的所有其他用法都很好,但是,这些都是相加元素或同一类型另一个列表的平均值。

Why does the Add() not like the calculation using two double values? 为什么Add()不喜欢使用两个双精度值的计算? What should I be doing here? 我在这里应该做什么?

您可以使用string.Join来创建以逗号分隔的列表

Console.WriteLine("{0}, {1}, {2}, {3}, {4}", i, rise, run, rise/run, string.Join(", ", slope));
Console.WriteLine("{0}, {1}, {2}, {3}, {4}", i, rise, run, rise/run, slope);

All the values you print there are regular numbers—except for slope . 您所打印的所有值都是常规数字,但slope除外。 It is a list that contains numbers out of each iteration. 它是一个包含每次迭代中的数字的列表。 Because it is a more complex type, its ToString() method just gives its exact type. 因为它是更复杂的类型,所以它的ToString()方法仅给出其确切类型。

You have two choices here. 您在这里有两个选择。 Either you only print the value you just added to the list, slope[i] , or you print the whole list: 您可以只打印刚刚添加到列表中的值slope[i] ,或者打印整个列表:

// print the value
Console.WriteLine("{0}, {1}, {2}, {3}, {4}", i, rise, run, rise/run, slope[i]);

// print the whole list
Console.WriteLine("{0}, {1}, {2}, {3}, {4}", i, rise, run, rise/run, string.Join(", ", slope));

Because that's not the way to show a list in the console. 因为这不是在控制台中显示列表的方法。

Try this: 尝试这个:

for(int i=0;i<slope.Count;i++)
{ 
    Console.WriteLine(slope[i]);
}

Iterate to list as follows, 反复列出以下内容,

foreach(double d in slope)
 Console.WriteLine(d); //or Console.Write(d+" ");

You have to do like this because, list not a valid (here valid means the way you wanted to see the output) argument for Console.WriteLine . 您必须这样做,因为,对于Console.WriteLine ,列表不是有效的(这里的有效表示您希望查看输出的方式)参数。 c# will do .toString() to the list show the output, hence you see System.Collection.Generic.List1[System.Double] . C#将.toString()到列表以显示输出,因此您看到System.Collection.Generic.List1[System.Double] You have to iterate over it. 您必须对其进行迭代。

The reason you are seeing the text is because your format operation is taking the ToString() method. 您看到文本的原因是因为您的格式操作采用了ToString()方法。

Your rise/run division appears to have been assigned the type System.Double rather than the primitive type double, hence ToString() is giving a different result. 您的上升/运行除法似乎已被分配为System.Double类型,而不是原始类型double,因此ToString()给出了不同的结果。

There's nothing 'wrong', but you may wish to explicitly use the primitive double type for convenience in this situation. 没有什么“错误”,但是在这种情况下,为方便起见,您可能希望显式使用原始double类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM