初始化引用类型对象数组的简明方式(The concise way to initialize an array of reference type object)
初始化引用类型对象数组的简明方式(The concise way to initialize an array of reference type object)
我想知道是否有更好的方法来初始化一个引用类型对象的数组,像这样。
Queue<int>[] queues = new Queue<int>[10];
for (int i
初始化引用类型对象数组的简明方式(The concise way to initialize an array of reference type object)
我想知道是否有更好的方法来初始化一个引用类型对象的数组,像这样。
Queue<int>[] queues = new Queue<int>[10]; for (int i = 0; i < queues.Length; i++) queues[i] = new Queue<int>();我尝试了Enumerable.Repeat,但数组中的所有元素都引用同一个实例,
Queue<int>[] queues = Enumerable.Repeat(new Queue<int>(), 10).ToArray();我也试过Array.ForEach,但没有ref关键字它不起作用:
Queue<int>[] queues = Array.ForEach(queues, queue => queue = new Queue<int>());任何其他想法?
I wonder if there is better way to initialize an array of reference type object, like this.
Queue<int>[] queues = new Queue<int>[10]; for (int i = 0; i < queues.Length; i++) queues[i] = new Queue<int>();I tried Enumerable.Repeat, but all elements in the array refer to same instance,
Queue<int>[] queues = Enumerable.Repeat(new Queue<int>(), 10).ToArray();I also tried Array.ForEach, but it doesn't work without ref keyword:
Queue<int>[] queues = Array.ForEach(queues, queue => queue = new Queue<int>());any other idea?
最满意答案
你可以用这个:
Enumerable.Range(0,10).Select(_=>new Queue<int>()).ToArray()但IMO你的第一个例子也很好。
You could use this:
Enumerable.Range(0,10).Select(_=>new Queue<int>()).ToArray()But IMO your first example is perfectly fine too.
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2023-07-28 12:53:48
推荐阅读
留言与评论(共有 20 条评论) |
本站网友 姜黄的功效与作用 | 20分钟前 发表 |
like this. Queue<int>[] queues = new Queue<int>[10]; for (int i = 0; i < queues.Length; i++) queues[i] = new Queue<int>(); I tried Enumerable.Repeat | |
本站网友 沙市租房 | 6分钟前 发表 |
but all elements in the array refer to same instance | |
本站网友 当归生姜羊肉汤 | 10分钟前 发表 |
but it doesn't work without ref keyword | |
本站网友 attachments | 10分钟前 发表 |
Queue<int>[] queues = Enumerable.Repeat(new Queue<int>() | |
本站网友 准备好了吗 | 20分钟前 发表 |
but it doesn't work without ref keyword | |
本站网友 古交租房 | 1分钟前 发表 |
Queue<int>[] queues = new Queue<int>[10]; for (int i = 0; i < queues.Length; i++) queues[i] = new Queue<int>(); 我尝试了Enumerable.Repeat | |
本站网友 鹰潭市工商局 | 27分钟前 发表 |
但没有ref关键字它不起作用: Queue<int>[] queues = Array.ForEach(queues | |
本站网友 微软手机 | 7分钟前 发表 |
but all elements in the array refer to same instance | |
本站网友 藿香正气丸说明书 | 9分钟前 发表 |
Queue<int>[] queues = new Queue<int>[10]; for (int i = 0; i < queues.Length; i++) queues[i] = new Queue<int>(); 我尝试了Enumerable.Repeat | |
本站网友 异朽阁 | 8分钟前 发表 |
You could use this | |
本站网友 大事渲染 | 24分钟前 发表 |
but all elements in the array refer to same instance | |
本站网友 万点理财 | 1分钟前 发表 |
Queue<int>[] queues = Enumerable.Repeat(new Queue<int>() | |
本站网友 环切包皮 | 12分钟前 发表 |
but it doesn't work without ref keyword | |
本站网友 柠檬的功效与作用 | 15分钟前 发表 |
10).ToArray(); 我也试过Array.ForEach | |
本站网友 盐城美食 | 22分钟前 发表 |
10).ToArray(); 我也试过Array.ForEach | |
本站网友 超薄套套 | 25分钟前 发表 |
like this. Queue<int>[] queues = new Queue<int>[10]; for (int i = 0; i < queues.Length; i++) queues[i] = new Queue<int>(); I tried Enumerable.Repeat | |
本站网友 中医望闻问切 | 6分钟前 发表 |
Queue<int>[] queues = Enumerable.Repeat(new Queue<int>() | |
本站网友 恒悦轩 | 28分钟前 发表 |
像这样 | |
本站网友 孕妇禁忌食物 | 23分钟前 发表 |
10).Select(_=>new Queue<int>()).ToArray() But IMO your first example is perfectly fine too. |