将一个列表上的AddLast()添加到列表数组C#中的所有列表中 [英] AddLast() on one list adding to all lists in an array of lists C#

查看:40
本文介绍了将一个列表上的AddLast()添加到列表数组C#中的所有列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有以下代码:

LinkedList<int>[] listOfNumbers = new LinkedList<int>[6];
int[] someNumbers = new int[6];

for(int index = 0; index < 6; index++)
{
  listOfNumbers[index] = new LinkedList<int>();
}

someNumnbers[0] = 0;
someNumnbers[1] = 1;
someNumnbers[2] = 2;
someNumnbers[3] = 3;
someNumnbers[4] = 4;
someNumnbers[5] = 5;

for(int index = 0; index < 6; index++)
{
  listOfNumbers[index].AddLast(someNumbers[index]);
}

我希望在最后一次循环的第一遍之后,Visual Studio中的对象监视工具会提供以下报告:

I expect the following report from the object watch tool in Visual Studio after the first pass of the last loop:

listOfNumbers[0] has 1 element with value 0
listOfNumbers[1] has no elements
listOfNumbers[2] has no elements
listOfNumbers[3] has no elements
listOfNumbers[4] has no elements
listOfNumbers[5] has no elements

但是相反,我很好奇地发现了这一点:

But instead I'm curiously finding this:

listOfNumbers[0] has 1 element with value 0
listOfNumbers[1] has 1 element with value 0
listOfNumbers[2] has 1 element with value 0
listOfNumbers[3] has 1 element with value 0
listOfNumbers[4] has 1 element with value 0
listOfNumbers[5] has 1 element with value 0

当我运行最后一个循环使其完成时,我得到以下信息:

When I run the last loop to its completion, I get the following:

listOfNumbers[0] has 6 element with values 0,1,2,3,4,5
listOfNumbers[1] has 6 element with values 0,1,2,3,4,5
listOfNumbers[2] has 6 element with values 0,1,2,3,4,5
listOfNumbers[3] has 6 element with values 0,1,2,3,4,5
listOfNumbers[4] has 6 element with values 0,1,2,3,4,5
listOfNumbers[5] has 6 element with values 0,1,2,3,4,5

与我期望得到的:

listOfNumbers[0] has 1 element with value 0
listOfNumbers[1] has 1 element with value 1
listOfNumbers[2] has 1 element with value 2
listOfNumbers[3] has 1 element with value 3
listOfNumbers[4] has 1 element with value 4
listOfNumbers[5] has 1 element with value 5

我的第一个猜测是我在语法上写了一些不正确的东西,但是我似乎无法弄清楚是什么.显然,似乎在每次通过时都将AddLast()应用于列表数组中的每个列表,但是我不知道为什么.任何帮助将不胜感激.

My first guess is I've written something incorrect in the syntax, but I can't seem to figure out what. It obviously seems to be applying the AddLast() to every list in the list array on every pass, but I have no idea why. Any help would be appreciated.

推荐答案

如果您在每个索引中都具有相同的链表实例,并且您实际上正在初始化链表数组(如您已显示的那样)的话,就会发生这种情况.会是一个问题.相同的实例意味着您将链接列表实例化到一个变量中,然后在每个数组索引中将其赋值,这实际上意味着,由于它们在内部都是相同的对象,无论您在哪个对象中调用addlast方法,它将反映在数组中的每个项目中.

This would happen if you have the same instance of the linked list in each index, if u are actually initialising the linkedlist array as u have shown it it shouldn't be a problem. The same instance means that u instantiated a linked list once into a variable and then assigned that in each of the array index, this would essentially mean that since they are all the same object internally no matter which object u call the addlast method in, it would reflect in each of those items in the array.

这篇关于将一个列表上的AddLast()添加到列表数组C#中的所有列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆