简体   繁体   English

为链表创建自己的equals方法

[英]Creating my own equals method for a linked list

For my java class we need to write equals methods comparing two instances of the LinkedSet class that uses doubly linked lists using nodes. 对于我的Java类,我们需要编写equals方法,比较使用节点使用双链表的LinkedSet类的两个实例。 Thus far I have only really learned how to compare arrays so I am not too familiar with this concept and am really struggling with this assignment. 到目前为止,我只真正了解了如何比较数组,因此我对这个概念不太熟悉,并且在此分配方面确实很挣扎。

https://gist.github.com/rpcarney4/c50dd44d2728c266b26b621bebb12444 (here is a link to the code i currently have, the methods in question are on lines 170 and 185) https://gist.github.com/rpcarney4/c50dd44d2728c266b26b621bebb12444 (这是我目前拥有的代码的链接,有问题的方法在第170和185行)

Any help is greatly appreciated!! 任何帮助是极大的赞赏!!

edit: also what is the difference between the methods? 编辑:方法之间也有什么区别? in the parameters there is Set s and then LinkedSet s. 在参数中有Set,然后是LinkedSet。 Why do we need to have two different methods for this? 为什么我们需要两种不同的方法呢?

In your code Set is an interface while LinkedSet is a class implementing it. 在您的代码中Set是一个接口,而LinkedSet是实现它的类。

Now since you already have implemented the intersection for this class, it is very easy to check equality using that between two LinkedSet instances. 现在,由于您已经实现了此类的交集,因此使用两个LinkedSet实例之间的相等性检查非常容易。 This is because your LinkedSet instance won't allow duplicate values to be added in the LinkedSet. 这是因为您的LinkedSet实例不允许在LinkedSet中添加重复的值。 So, for testing equality between two LinkedSets, take their intersection and the check if the size of the intersection equals both the linkedsets size. 因此,要测试两个LinkedSet之间的相等性,请采用它们的交集,并检查交集的大小是否等于linkedsets的大小。 If this condition is satisfied, both the LinkedSets are equal. 如果满足此条件,则两个LinkedSet相等。

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

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