Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么此数组中的每个项都会被最后一项的值覆盖?_C#_Arrays_Forms - Fatal编程技术网

C# 为什么此数组中的每个项都会被最后一项的值覆盖?

C# 为什么此数组中的每个项都会被最后一项的值覆盖?,c#,arrays,forms,C#,Arrays,Forms,我正在做一个C#Windows窗体程序,我正在做一个遗传算法基础的练习。我试图做的事情没有达到预期效果,如下所示: 我的代码初始化了两个交错数组和一个普通数组。锯齿状数组各有20项,普通数组有8项。正常数组存储个体的基因组,锯齿状群体数组存储这些个体数组的数组 当我调用generate_population()时,它应该用正确的长度初始化锯齿数组中的每个数组,然后使用for循环返回create_indiv()填充所述数组(函数返回一个8项单个数组),然后通过访问填充数组的i索引并打印该值,将数组

我正在做一个C#Windows窗体程序,我正在做一个遗传算法基础的练习。我试图做的事情没有达到预期效果,如下所示:

我的代码初始化了两个交错数组和一个普通数组。锯齿状数组各有20项,普通数组有8项。正常数组存储个体的基因组,锯齿状
群体
数组存储这些
个体
数组的数组

当我调用
generate_population()
时,它应该用正确的长度初始化锯齿数组中的每个数组,然后使用for循环返回
create_indiv()
填充所述数组(函数返回一个8项
单个
数组),然后通过访问填充数组的i索引并打印该值,将数组打印到列表框中

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

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

例如:

generate_population()
中生成时,
population[][]
显示由以下数组填充:

  • 12345678
  • 87654321
  • 13579063
如果在调用
generate\u population()
后,尝试在另一个函数中迭代并打印
population[][]
的所有值,我会得到以下结果:

  • 13579063
  • 13579063
  • 13579063
有人发现问题了吗?这段代码并不代表完整的程序,只是我认为我隔离了问题的部分。我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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
整数计数=0;
int count2=0;
int[]个人=新的int[8];
int[][]人口=新int[20][];
int[][]workingpop=新int[20][];
随机种子=新随机();
公共表格1()
{
初始化组件();
}
公共int[]创建_indv()
{

对于(int i=0;i将此行放在方法create_indv()中:

您始终覆盖“个人”

population[i]

指向同一个“个体”

这只是普通“对象是引用,在多个位置使用同一对象意味着一个对象中的更改会影响一个对象引用的所有位置”的变体。请参阅标记的副本,以了解已解决此场景的许多现有堆栈溢出问题的一个示例。
population[i]