C#将值从数组复制到列表 [英] C# copy values from array to list

查看:237
本文介绍了C#将值从数组复制到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

for (int c = 0; c < date_old.Length; c++) 
{
    for (int d = 0; d < date_new.Length; d++)
    {
        newList[c] = data_old[c];

        if (date_old[c] == date_new[d])
        {
            newList[c] = data_new[d];
        }
    }
}

我想做的是以下事情:

我有四个数组:date_newdate_olddata_newdata_old和一个名为newList的列表. date_old和data_old的长度以及date_new和data_new的长度也相同. 我想遍历日期数组检查是否有相等的日期值.在执行此操作时,我想将每个值从data_old数组复制到newList.当一个值相等时,我想将此时的值从data_new位置复制到List中. 在第二个for循环之后,在这里得到一个OutOfBoundException.怎么了?

I have four arrays: date_new, date_old, data_new, data_old and a List called newList. date_old and data_old have the same length and date_new and data_new too. I want to loop through the date arrays check if there are equal date values. While I'm doing this, I want to copy every value from the data_old array to newList. When a value is equal, I want to copy the value at this point from the data_new position into the List. Here I get a OutOfBoundException after the second for loop. What is wrong?

推荐答案

当您尝试读/写索引大于array.Length -1的数组时,抛出此异常.

This exception is thrown when you try to read/write to an array with an index greater than array.Length -1.

仔细检查newList的大小.

这篇关于C#将值从数组复制到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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