简体   繁体   中英

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. However I got an error as commented in my code above.

My question is how to copy the tempArray to the global class array. 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.

Solution : You need to move your array variable arrayTemp declaration outside the loop.

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. (Similar to how GlobalDataClass.dDataArray is declared elsewhere)

//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 .

So first declare the tempArray outside the for-loop and then populate it instead of GlobalDataClass.dDataArray inside the for-loop accordingly:

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:

you don't need to read the file 2nd time in the using() clause.

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:

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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