简体   繁体   English

将项目插入队列数组,C#

[英]Insert item to an array of queue, C#

I need to insert a simple integer to the queue. 我需要在队列中插入一个简单的整数。

For some reason, it always collapsed on null reference. 由于某些原因,它总是在null引用时折叠。 Do I need to init the queue? 我需要初始化队列吗? How to do this? 这个怎么做?

This my code. 这是我的代码。

Queue<int>[] OdafimColors = new Queue<int>[10];
OdafimColors[i].Enqueue(-1);

It is very simple, I can't realize why it doesn't work. 这很简单,我不明白为什么它不起作用。

You need to initialize each element in your array: 您需要初始化数组中的每个元素:

for(int i = 0; i<OdafimColors.Lenght; i++)
      OdafimColors[i] = new Queue<int>();

Your definition doesn't allocate space for Queues. 您的定义没有为队列分配空间。 It allocates memory for the array and the elements are null until you initialize them. 它为数组分配内存,并且在您初始化元素之前,这些元素为null。

I don't think you want it to be an array. 我不认为您希望它成为一个数组。 That makes it an array of queues. 这使它成为队列数组。

You probably want: 您可能想要:

Queue<int> OdafimColors = new Queue<int>();
OdafimColors.Enqueue(-1);

http://msdn.microsoft.com/en-us/library/7977ey2c(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/7977ey2c(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM