如何在 C# 中以正确的方式将矩形数组存储在数组列表中

How to store a rectangular array in an array list in a correct way in C#

我编写了以下代码来生成 6 条染色体的群体,其中每条染色体是一个 5x5 的数组。然后我用另一种方法打印每条染色体。问题是我每次都得到相同的数组!

    static List<int[,]> PopulationChromosomes = new List<int[,]>();

    private void moveGenetic_Click(object sender, EventArgs e)
    {   

        FileStream fs = new FileStream("C:/temp/intialPopulation.txt", FileMode.Append, FileAccess.Write);

        writer = new StreamWriter("C:/temp/listOfChromosomesForAllRounds.txt", true);

        population = new int[6][,]; // jagged array    
        Random rnd = new Random(); // to gereate random number (either 0 or 1)
        auvChromosomes = new int[5, 5];

        for (int i = 0; i < population.Length; i++)
        {
            population[i] = new int[5, 5];   
        }

        using (StreamWriter sw = new StreamWriter(fs))   
        {   
            for (int i = 0; i < population.Length; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    for (int k = 0; k < 5; k++)
                    {
                        auvChromosomes[j, k] = rnd.Next(0, 2);
                        sw.Write("|" + auvChromosomes[j, k] + "|");
                    } // end-inner-for
                    sw.WriteLine();

                } // end-outer-inner-for
                PopulationChromosomes.Add(auvChromosomes);
                Array.Clear(auvChromosomes, 0, auvChromosomes.Length);
            } // end-outer-for

        } // end-using    

        Chromosomes(PopulationChromosomes, 1);
    }

您的问题是您将 int[] 视为值类型,而实际上它是引用类型。当您将它添加到您的列表时

PopulationChromosomes.Add(auvChromosomes);

然后清除它

Array.Clear(auvChromosomes, 0, auvChromosomes.Length);

您正在清除刚刚放入列表中的实例。所以列表中的数组将全为零。

即使没有清除,您也会一遍又一遍地添加相同的数组实例。您需要为每次迭代创建一个新数组。

解决方案:

在你的for循环中创建一个新的数组实例跳过Array.Clear部分,你不应该需要它如果您创建一个新实例

 for (int i = 0; i < population.Length; i++)
 {
     auvChromosomes = new int[5, 5];
     ...

@Michal S 的回答已经解决了主要问题,但我也想帮助您清理代码。

static List<int[,]> PopulationChromosomes = new List<int[,]>();

#region AUV Genetic Movement

private Random rnd = new Random(); // to generate random number (either 0 or 1)
private void moveGenetic_Click(object sender, EventArgs e)
{               
    using (StreamWriter sw = new StreamWriter(new FileStream("C:/Users/Welcome/Desktop/intialPopulation.txt", FileMode.Append, FileAccess.Write)))   
    {   
        var population = new int[6][,]; // define this in the scope where you use it
        for (int i = 0; i < population.Length; i++)
        {
            population[i] = new int[5, 5];
            for (int j = 0; j < 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    population[i][j, k] = rnd.Next(0, 2);
                    sw.Write("|{0}|", population[i][j, k]);
                } // end-inner-for
                sw.WriteLine();
            } // end-outer-inner-for
            PopulationChromosomes.Add(population[i]);
        } // end-outer-for
    } // end-using    

    Chromosomes(PopulationChromosomes, 1);
}

private const string lineSeparator = new string('-', 90);
public void Chromosomes(IEnumerable<int[,]> population, int round)
{  
    ////////////////  print  each chromosome matrix ////////////////
    using (writer = new StreamWriter("C:/Users/Welcome/Desktop/listOfChromosomesForAllRounds.txt", true))
    {
        writer.WriteLine("{0}\n********************************* List of Chrmomsomes (Round : {1})**********************************\n{2}", lineSeparator, round, lineSeparator);

        foreach (int[,] chromosme in population)
        {
            for (int h = 0; h < chromosme.GetLength(0); h++)
            {
                for (int k = 0; k < chromosme.GetLength(1); k++)
                {
                    writer.Write("|{0}|", chromosme[h, k]);
                }
                writer.WriteLine();
            }
            writer.WriteLine(lineSeparator);
        }
    }
}

再深入一点,你真的应该有一个Chromesome class来封装二维数组。