索引超出范围异常错误 [英] Index out of range Exception error

查看:128
本文介绍了索引超出范围异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有3个列表字符串。



2列表字符串包含双精度值。



我想减去2个列表字符串对象并将它们绑成新的列表字符串



  for  int  c =  0 ; c <  yellowTimeStampRYcmnMsgIds.Count; c ++)
{
gatewaytimeRYcmnMsgIds [c] =(yellowTimeStampRYcmnMsgIds [c] - redTimeStampRYcmnMsgIds [c ]);

}





我在尝试这样的情况下从愤怒异常中获得索引



谢谢

John

解决方案

我有3个列表字符串 .2 list strings 包含双倍值。



如果你的列表是字符串列表其中每个字符串是双值的表示:你必须首先将字符串转换为双精度,然后进行减法,然后将结果转换为字符串,然后将其分配给新的字符串列表!



从字符串中减去一个字符串是没有意义的(并且不会首先编译)...在C#中,无论如何。



这是一个代码示例,它带有两个字符串列表,其中每个列表中的每个项目都被假定为是double的字符串表示形式,从另一个列表中减去一个列表中的项目,取t他导致double并转换为字符串,并将其存储在它返回的列表中。



注意此示例测试以确保for循环尝试比较两个列表使用两个列表中较短的列表...如果两个列表的长度不同。



它还检查每个列表中的每个项目以确保它们是双打的:

 private List <   string  >  subtractStringDouble(List <   string  >  list1,List <   string  >  list2)
{
List < string > list3 = new List & lt; string > ();

double d1,d2;

//使用两个列表中较短的一个
//你会注意到你可以
里面评估一个for循环声明
for(int i = 0; i < ((list1.Count < list2.Count) list1.Count list2.Count); i ++)

{ < span class =code-attribute>

< span class =code-attribute> if(double.TryParse(list1 [i], out d1))

{

if(double.TryParse(list2 [i], out d2))

< span class =code-attribute> {

list3.Add((d1 - d2).ToString(格式:F));

}

}

}



return list3;

< span class =code-attribute>}

这是一个测试:

 List <   string  >  test1 = new List <   string  >  {5.33,59.123,1.9}; 
列表< 字符串 > test2 = new List < string > {2.33,56.123, - 1.1};

列表< string > test3 = subtractStringDouble(test1,test2);

'list3中的三个项目中的每一个都应该是:3.00


也许,你 redTimeStampRYcmnMsgIds 列表中的元素少于 yellowTimeStampRYcmnMsgIds 列表中的元素,因此您应该检查是否可以从 redTimeStampRYcmnMsgIds 在特定位置:

   int  c =  0 ; c <  yellowTimeStampRYcmnMsgIds.Count; c ++)
{
if (redTimeStampRYcmnMsgIds.Count > = c) // 如果redTimeStampRYcmnMsgId,则使用Length而不是Count s是一个数组而不是List
{
gatewaytimeRYcmnMsgIds [c] =(yellowTimeStampRYcmnMsgIds [c] - redTimeStampRYcmnMsgIds [c]);
}
其他
{
// < span class =code-comment> redTimeStampRYcmnMsgIds列表中没有元素
}
}


确保 gatewaytimeRYcmnMsgIds redTimeStampRYcmnMsgIds yellowTimeStampRYcmnMsgIds

Hi,

I have 3 list strings.

2 list strings contains double values.

I want to subtract 2 list string objects and lace them into new list string

for (int c = 0; c < yellowTimeStampRYcmnMsgIds.Count; c++)
                {
                    gatewaytimeRYcmnMsgIds[c] = (yellowTimeStampRYcmnMsgIds[c] - redTimeStampRYcmnMsgIds[c]);
                   
                } 



I am getting index out of rage exceptionwhile trying like this

Thanks
John

解决方案

"I have 3 list strings. 2 list strings contains double values."

If your lists are lists of strings where each string is a representation of a double value: you must convert the strings to doubles first, then do the subtraction, and then convert the result to a string, and then assign that to your new List of strings !

"Subtracting" a string from a string makes no sense (and wouldn't compile in the first place) ... in C#, anyhow.

Here's a code example that takes two Lists of strings, where each item in each list is assumed to be a string representation of a double, subtracts the items in one list from the other list, takes the resulting double and converts into a string, and stores it in the list it returns.

Note this example tests to make sure that the for-loop attempts to compare the two lists using the shorter of the two lists ... if the two lists are not of the same length.

It also checks each item in each list to make sure they are doubles:

private List<string> subtractStringDouble(List<string> list1, List<string> list2)
{
    List<string> list3 = new List<string>();

    double d1, d2;

    // use the shorter of the two lists
    // you'll note that you can evaluate inside
    // a for-loop declaration
    for (int i = 0; i < ((list1.Count < list2.Count) ? list1.Count : list2.Count); i++)

    {

        if(double.TryParse(list1[i], out d1))

        {

            if(double.TryParse(list2[i], out d2))

            {

                list3.Add((d1 - d2).ToString(format:"F"));

            }

        }

    }



    return list3;

}

Here's a test:

List<string> test1 = new List<string>{ "5.33", "59.123", "1.9"};
List<string> test2 = new List<string> { "2.33", "56.123", "-1.1" };

List<string> test3 = subtractStringDouble(test1, test2);

Each of the three items in 'list3 should be: "3.00"


Probably, you have less elements in your redTimeStampRYcmnMsgIds list than in your yellowTimeStampRYcmnMsgIds list, so you should check whether you can take the element from redTimeStampRYcmnMsgIds at the specific position:

for (int c = 0; c < yellowTimeStampRYcmnMsgIds.Count; c++)
{
    if (redTimeStampRYcmnMsgIds.Count >= c) // use Length instead of Count if redTimeStampRYcmnMsgIds is an array and not a List
    {
      gatewaytimeRYcmnMsgIds[c] = (yellowTimeStampRYcmnMsgIds[c] - redTimeStampRYcmnMsgIds[c]);
    }
    else
    {
       // there are no elements in the redTimeStampRYcmnMsgIds list anymore
    }    
} 


Make sure gatewaytimeRYcmnMsgIds and redTimeStampRYcmnMsgIds are the same size of elements as yellowTimeStampRYcmnMsgIds.


这篇关于索引超出范围异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆