索引超出范围异常错误

编程入门 行业动态 更新时间:2024-10-26 10:37:36
本文介绍了索引超出范围异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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 ++) { if(double.TryParse(list1 [i], out d1)) { if(double.TryParse(list2 [i], out d2)) { list3.Add((d1 - d2).ToString(格式:F)); } } } return list3; }

这是一个测试:

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]); } 其他 { // 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.

更多推荐

索引超出范围异常错误

本文发布于:2023-10-18 10:36:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   异常   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!