简体   繁体   English

在C#中复制2D数组

[英]Copy 2D Array in C#

I have a code that stored the data to a temporary array. 我有一个将数据存储到临时数组的代码。

string filename = openFileDialog1.FileName;
string[] line = File.ReadAllLines(filename);
using (var reader2 = File.OpenText(@filename))
{
    for (int i = 0; i < line.Length; i++)
    {
        string lines = reader2.ReadLine();
        var data = lines.Split(',');
        double[,] arrayTemp = new double[line.Length, 2];
        arrayTemp[i, 0] = double.Parse(data[0]);
        arrayTemp[i, 1] = double.Parse(data[1]);
    }
    Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); //error the name "arrayTemp" does not exist in the current context.
} 

Since 2d array is not resizable and I want my global array to be flexible,so I use the Array.Copy method to copy the temp array to the Global class array. 由于2d数组不可调整大小,并且我希望全局数组灵活,因此我使用Array.Copy方法将临时数组复制到Global类数组。 However I got an error as commented in my code above. 但是我在上面的代码中注释了一个错误。

My question is how to copy the tempArray to the global class array. 我的问题是如何将tempArray复制到全局类数组。 Any idea how to fix this? 任何想法如何解决这个问题?

Problem : You have declared your array variable arrayTemp inside the for loop and it is not available outside of it. 问题:您已在for循环内声明了数组变量arrayTemp ,但在其外部不可用。

Solution : You need to move your array variable arrayTemp declaration outside the loop. 解决方案:您需要将数组变量arrayTemp声明arrayTemp循环。

Try This: 尝试这个:

 string filename = openFileDialog1.FileName;
 string[] line = File.ReadAllLines(filename);
 double arrayTemp=new double[line.Length,2];//declare outside forloop so it available after forloop.
 GlobalDataClass.dDataArray=new double[line.Length,2]; //add this line
 using (var reader2 = File.OpenText(@filename))
 {
   for (int i = 0; i < line.Length; i++)
   {
     string lines = reader2.ReadLine();
     var data = lines.Split(',');         
     GlobalDataClass.dDataArray[i, 0] = double.Parse(data[0]);
     GlobalDataClass.dDataArray[i, 1] = double.Parse(data[1]);
   }
    Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); 
 }

Your tempArray is out of the scope of you Array.Copy because it is inside of your for loop, you need to move it into the scope of your method call so the method can access it. tempArray超出的范围 ,你Array.Copy ,因为它是你的for循环,你需要将它移动到你的方法调用的范围,所以,方法可以访问它里面。 (Similar to how GlobalDataClass.dDataArray is declared elsewhere) (类似于在其他地方声明GlobalDataClass.dDataArray方式)

//Array is now declared in an accessible scop
double[,] arrayTemp;
string filename = openFileDialog1.FileName;
string[] line = File.ReadAllLines(filename);
using (var reader2 = File.OpenText(@filename))
{
    for (int i = 0; i < line.Length; i++)
    {
        string lines = reader2.ReadLine();
        arrayTemp = new double[line.Length, 2];
        var data = lines.Split(',');
        GlobalDataClass.dDataArray[i, 0] = double.Parse(data[0]);
        GlobalDataClass.dDataArray[i, 1] = double.Parse(data[1]);
    }
    Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); //error the name "arrayTemp" does not exist in the current context.
} 

Statements "deeper" into the { } hierarchy are only accessible to statements within them, and other child scopes in them. {}层次结构中“更深”的语句只能由其中的语句以及其中的其他子作用域访问。 Parent scopes outside of the original brackets cannot access the variables. 原始括号之外的父范围无法访问变量。

EDIT: 编辑:

it seems that you are trying to copy the contents of arrayTemp to GlobalDataClass.dDataArray , but you are assigning values to GlobalDataClass.dDataArray , and trying to copy empty arrayTemp to GlobalDataClass.dDataArray . 似乎您正在尝试将arrayTemp的内容复制到GlobalDataClass.dDataArray ,但是您正在将值分配给GlobalDataClass.dDataArray ,并且试图将空的arrayTemp复制到GlobalDataClass.dDataArray

So first declare the tempArray outside the for-loop and then populate it instead of GlobalDataClass.dDataArray inside the for-loop accordingly: 因此,首先在for循环外声明tempArray ,然后相应地在for循环内填充tempArray而不是GlobalDataClass.dDataArray

string filename = openFileDialog1.FileName;
string[] line = File.ReadAllLines(filename);

var arrayTemp = new double[line.Length, 2];

using (var reader2 = File.OpenText(@filename))
{
    for (int i = 0; i < line.Length; i++)
    {
        string lines = reader2.ReadLine();
        var data = lines.Split(',');
        arrayTemp[i, 0] = double.Parse(data[0]);
        arrayTemp[i, 1] = double.Parse(data[1]);
    }
    Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); // now the error should go away.
}

EDIT 2: 编辑2:

you don't need to read the file 2nd time in the using() clause. 您无需在using()子句中第二次读取文件。

Now please try the following: 现在,请尝试以下操作:

string filename = openFileDialog1.FileName;
string[] line = File.ReadAllLines(filename);

var arrayTemp = new double[line.Length, 2];

for (int i = 0; i < line.Length; i++)
{
    var data = line[i].Split(',');
    arrayTemp[i, 0] = double.Parse(data[0]);
    arrayTemp[i, 1] = double.Parse(data[1]);
}
Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); // now the error should go away.

看起来您的数组超出范围,因为它在for循环内声明,并且您试图在for循环外访问它

I have answered on this link 我已经在此链接上回答了

To copy an array to another one, just use the "Clone()" function like the following: 要将数组复制到另一个数组,只需使用“ Clone()”函数,如下所示:

This is your array list 这是您的阵列清单

object newArray = new object [row, column];

When you are creating another Array just use this code: 当创建另一个数组时,只需使用以下代码:

object[,] clonedArray = (object[,]) newArray.Clone();

Simple! 简单! Have fun! 玩得开心!

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

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