通用链表 [英] Generic LinkedList

查看:86
本文介绍了通用链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好专家
我有一个关于LinkedList的问题.
要连接两个LinkedList,有一个名为 concat 的方法,但它会返回新的Linkedlist.

为什么会这样?

据我所知,这是一个非常简单的任务,只需将第一个链表的最后一个节点分配给另一个链表的第一个节点即可.

我的问题是.Net Framework为什么不遵循这种方式?


在此先感谢

Hello Experts
I have a question about LinkedList.
To concatenate two LinkedList, There is method called concat but it returns new Linkedlist.

Why so?

As I know it is very simple task, just need to assign first linkedlist last node with another linkedlist first node.

My question is why .Net Framework not following this way?


Thanks in advance

推荐答案

这样,您可以避免丢失任何组合列表.例如,如果您具有通过和失败的列表,则可以轻松创建完整列表,而不会丢失最初的两个链接列表.这与您将执行的操作相同:
This way you avoid losing any of the combined lists. If you for example have a list of passed and failed you can easily create a complete list without losing the initial two linked lists. This is the same as if you would do:
String s = StringA + StringB; 


在这种情况下,StringA和/或StringB不会丢失,因为使用s来保存结果.这比以下方法更快,更短:


In this case the StringA and/or StringB isn''t lost because s is used to keep the result. This is way faster and shorter than:

String s = new String(StringA);
s.Concat(StringB);



由于使用了"="符号,因此很明显已经分配了数据.比调用将所有项目分配给对象的方法更容易发现.

祝你好运!

附言也许需要额外考虑(无法查看实际的.net代码,但我会以这种方式进行优化)可能是创建一个新的链表,并且将两者的结果都提供了机会,使所有链表的引用散布在内存中并将其放入顺序顺序.链表是一种不需要它就可以工作的列表,但是它当然可以极大地改善内存缓存.因此,这看起来像是在浪费时间,但是一次简单的复制会加快查找操作的速度,因此平均而言值得这样做.



Because of the use of the ''='' sign it is very clear that there is data assigned to it. It is easier to spot than calling a method that assigns all the items to the object.

Good luck!

Ps. Maybe an extra consideration (cannot see into the actual .net code but I would optimize this way) could be that creating a new linked list with the results of both gives the opportunity to get all the linked list references scattered through memory and put them into sequential order. A linked list is a kind of list that is crafted to work without the need for that but of course would it help improve memory caching enormously. So this might then look like a waste of time but this simple copying once would speed up the look up actions and therefore , on average, worth doing.


我不太确定. ..但我认为它与通用性有关.我的意思是串联两个链表可能看起来很合逻辑,但是出于实际(数组)和安全性(字符串)的原因,串联两个String或Arrays可能不合逻辑.它认为.Net Framework只是在试图使事情统一,特别是因为所有列表都具有相同的接口.换句话说,您希望所有列表的行为方式相同,以便您可以在不更改接口的情况下更改实现.

希望对您有所帮助
I''m not very sure... but I think it has something to with genericity. I mean concatenating two linked lists might seen very logical but concatenating two Strings or Arrays might not be logical for practical (arrays) and security (strings) reasons. It think that the .Net Framework is simply trying to make things uniform especially because all lists have the same interface. In other words, you want all lists to behave the same way so that you can change the implementation without changing the interface.

I hope this helps


我相信Concat()方法是System.Linq命名空间中的扩展方法.它可以与任何IEnumerable(T)集合一起使用,并返回IEnumerable(T).

不要问我为什么没有LinkedList类的AddRange()Concat()方法...
I believe the Concat() method is an extension method in the System.Linq namespace. It works with any IEnumerable(T) collection and it returns an IEnumerable(T).

Don''t ask me why there is no AddRange() or Concat() method of the LinkedList class...


这篇关于通用链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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