2017-06-13 6 views
0

次のコードを書いて、各染色体が5x5の配列である6つの染色体の集団を生成しました。次に、別の方法で各染色体を印刷します。問題は毎回同じアレイを手に入れたことです!配列リストに長方形の配列を正しい方法で格納する方法

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); 
    } 
+0

印刷が間違っているか計算していますか?適切な結果を提供できますか? – NetMage

+0

それはリンクをちょうど提供するために提供されます – Rose

+0

「間違って」定義してください。 –

答えて

1

問題は、値型としてint [、]を扱っていることです。実際には参照型です。あなたはあなたのリスト

PopulationChromosomes.Add(auvChromosomes); 

に追加して、明確なそれが

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

あなたはあなただけのリストに入れてインスタンスをクリアしている場合。したがって、リストの配列はゼロでいっぱいになります。

明白でなくても、アレイの同じインスタンスを何度も何度も追加し続けるでしょう。すべての反復に対して新しい配列を作成する必要があります。

ソリューション:

あなたArray.Clear部分をスキップサイクル用と以内に新しいインスタンスを作成する場合は、あなたがそれを必要はありません、新しい配列インスタンスを作成

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

ありがとう(y) – Rose

0

@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); 
     } 
    } 
} 

より深く行く、あなたが本当に2次元配列をカプセル化するChromesomeクラスを持っている必要があります。

関連する問題