索引超出了数组界限 用户代码未处理 System.IndexOutOfRangeException
.net中 stringsplit=";"; string[]tolist=T...
.net中 string split = ";"; string[] tolist = To.Text.Trim().Split(split.ToCharArray()); for (int i = 0; i <= tolist.Length; i++) { mailmasg.To.Add(tolist[i].Trim()); } //添加收件人地址 string[] cclist = CC.Text.Trim().Split(split.ToCharArray()); for (int i = 0; i <= cclist.Length; i++) { mailmasg.CC.Add(cclist[i].Trim()); }测试报错说索引超出数组界限 求教 !!!!!
展开
2个回答
展开全部
for (int i = 0; i <= cclist.Length; i++)
你这里出错了
cclist.Length是数组的元素数量
但是我们用的时候cclist[i],i表示的是数组的下标
如果cclist里有5个数据的话,那么这5个数据的下标分别对应0 1 2 3 4
如果用for(int i=0;i<=5;i++)取用,那么读取的下标对应的是0 1 2 3 4 5
这个5下标已经超出数组的范围了
所以楼主这里要改为
for (int i = 0; i < tolist.Length; i++)
或者是
for (int i = 0; i <= tolist.Length-1; i++)
你这里出错了
cclist.Length是数组的元素数量
但是我们用的时候cclist[i],i表示的是数组的下标
如果cclist里有5个数据的话,那么这5个数据的下标分别对应0 1 2 3 4
如果用for(int i=0;i<=5;i++)取用,那么读取的下标对应的是0 1 2 3 4 5
这个5下标已经超出数组的范围了
所以楼主这里要改为
for (int i = 0; i < tolist.Length; i++)
或者是
for (int i = 0; i <= tolist.Length-1; i++)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string[] defines an array, usually array's index starts from 0 and ends with the array's length-1.
For example, an array list1 of length 3, there are 3 elements in the array, they are list1[0], list1[1], and list1[2]. If you visit list1[3], since it is the *FOURTH* element of the array, you will get the error: index out of bounds.
In your code, you should use the following code to loop through an array:
for (int i = 0; i < cclist.Length; i++) {
}
For example, an array list1 of length 3, there are 3 elements in the array, they are list1[0], list1[1], and list1[2]. If you visit list1[3], since it is the *FOURTH* element of the array, you will get the error: index out of bounds.
In your code, you should use the following code to loop through an array:
for (int i = 0; i < cclist.Length; i++) {
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询