C#中的排序队列 [英] sorting queue in c#

查看:194
本文介绍了C#中的排序队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道队列的原理是先进先出,但是我想不使用ToArray()将数字放入数组中的队列,我想像double [] a = new double [100] ;并按降序排列,然后将第一个最小数字放在变量中怎么办呢?

Hi,I know that the principle of the queue is first in first out,but I want to put the number in queue in array not using ToArray() I want to use like double[] a = new double[100]; and ordering by descending and put the first smallest number in a variable how can do this??

Queue qu = new Queue();
        qu.Enqueue(4);
        qu.Enqueue(1);
        qu.Enqueue(5);
        qu.Enqueue(2);
        qu.Enqueue(3);		
var a = qu.ToArray().ToList();
a.Sort(); 
qu = new Queue(a);


在使用qu.ToArray()的代码中,我想使用像这样的数组


in this code using qu.ToArray() I want to use array like this

double[] a = new double[100]

推荐答案

重新排序队列不是正常操作-如果执行此操作,则可能是使用了错误的集合.

这就是为什么您不想使用ToArray的原因,我不确定-但您不能按照似乎的方式将集合强制转换为数组:这就是ToArray存在的原因.
您不必将任何内容转换为数组即可对其进行排序:
Reordering a queue is not a normal operation - if you are doing that, then you are probably using the wrong collection.

And quite why you don''t want to use ToArray I''m not sure - but you can''t just cast a collection to an array in the way you seem to think: that is why ToArray exists.

You don''t have to convert anything to an array in order to sort it though:
Queue<int> qu = new Queue<int>();
qu.Enqueue(4);
qu.Enqueue(1);
qu.Enqueue(5);
qu.Enqueue(2);
qu.Enqueue(3);
qu = new Queue<int>(qu.OrderBy(q => q));

会这样做.


这篇关于C#中的排序队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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