为什么这个数组中的每个项都被最后一个项的值覆盖?(复制)

时间:2022-11-23 11:31:49

This question already has an answer here:

这个问题已经有了答案:

I am working on a C# Windows forms program, and I am doing an exercise in the basics of a genetic algorithm. What I am trying to do that isn't working as intended is as follows:

我正在编写一个c# Windows窗体程序,我正在做一个基本的遗传算法的练习。我想做的不是按预期的方式工作:

My code initializes two jagged arrays, and a normal array. The jagged arrays each have 20 items, and the normal array has 8. The normal array stores a genome for an individual, and the jagged population arrays store an array of these individual arrays.

我的代码初始化两个参差不齐的数组和一个普通数组。参差不齐的数组各有20个条目,而普通数组有8个。普通数组为个体存储了一个基因组,而参差不齐的种群数组存储了这些单个数组的数组。

When I call generate_population(), it's supposed to initialize each array within the jagged arrays with the correct length, then it populates said array with the return of create_indiv(), using for loops (the function returns an 8 item individual array), then it prints the arrays to a listbox by accessing the i index of the population array and printing that value.

当我调用generate_population(),它应该在交错数组初始化每个数组使用正确的长度,然后填充数组表示的回归create_indiv(),使用for循环(函数返回一个8项个人数组),然后打印数组访问我的列表框索引数组的人口和打印该值。

This all works fine, it generates a set of unique individuals successfully and it prints those individuals by accessing and printing a specific index in the population array.

这一切都很好,它成功地生成了一组独特的个体,并通过访问和打印种群数组中的特定索引来打印这些个体。

Problem is, if I run the generate_population() function once, then try to use the population array in another function, it shows that every single index value contains the same individual — the last one generated in the array.

问题是,如果我运行generate_population()函数一次,然后尝试在另一个函数中使用population数组,它会显示每个索引值都包含相同的个体——在数组中生成的最后一个。

Example:

例子:

Upon generation within generate_population(), population[][] shows itself as being populated by these arrays:

在generate_population()的生成中,人口[][]显示自己被这些数组填充:

  • 12345678
  • 12345678
  • 87654321
  • 87654321
  • 13579063
  • 13579063

If I try to iterate through, and print all values of population[][] in another function, after I call generate_population(), I get this:

如果我尝试遍历,并在另一个函数中打印所有人口的值[],在调用generate_population()之后,我得到:

  • 13579063
  • 13579063
  • 13579063
  • 13579063
  • 13579063
  • 13579063

Does anyone spot the problem? This code does not represent the full program, only the parts I think I isolated the problem to. I am 90% sure that the problem occurs in generate_population(), but I could be wrong about that. I can post the full program if necessary.

有人发现问题了吗?这段代码并不代表完整的程序,只有部分我认为我把问题隔离了。我90%肯定这个问题发生在generate_population()中,但我可能错了。我可以在必要的时候发布完整的程序。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int count = 0;
        int count2 = 0;
        int[] individual = new int[8];
        int[][] population = new int[20][];
        int[][] workingpop = new int[20][];

        Random seed = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        public int[] create_indv()
        {
            for (int i = 0; i <= 7; i++)
            {
                individual[i] = seed.Next(0, 10);
            }
            return individual;

        }

        public string genomestr(int[] indiv)
        {
            StringBuilder builder = new StringBuilder();
            foreach (int value in indiv)
            {
                builder.Append(value);
            }
            string fin = builder.ToString();
            return fin;
        }

        public int[][] generate_population(int number)
        {
            for (int i = 0;i < number; i++)
            {
                population[i] = new int[8];
                workingpop[i] = new int[8];
            }
            for (int i = 0; i < number; i++)
            {
                population[i] = create_indv();
                workingpop[i] = population[i];


                int l = i + 1;
                this.popchart.Items.Add("Creature #" + l + ": " + genomestr(population[i]));
            }
            return population;
        }

        private void pop_Click(object sender, EventArgs e)
        {
            popchart.Items.Clear();
            generate_population(20);
        }
    }
}

1 个解决方案

#1


0  

Put this line inside method create_indv():

将此行放入方法create_indv():

int[] individual = new int[8];

You overwrite "individual" all the time.

你总是覆盖“个人”。

population[i]

point to the same "individual"

指向同一个“个人”

#1


0  

Put this line inside method create_indv():

将此行放入方法create_indv():

int[] individual = new int[8];

You overwrite "individual" all the time.

你总是覆盖“个人”。

population[i]

point to the same "individual"

指向同一个“个人”