编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

为链接列表对象编写一个equals()方法:Writing an equals() method for linked list object

Jakob Egger Java 2022-5-11 10:27 4人围观

腾讯云服务器
为链接列表对象编写一个equals()方法的处理方法

我正在建立的名为 LString 的类中的 equals(Object other)方法遇到麻烦.该类包含一个名为 LString 的对象,该对象可从链接列表中构建字符串,以及其他一些方法.我正在研究两种方法, compareTo() equals().

I'm having trouble with an equals(Object other) method within a class I'm building called LString. The class contains an object called LString that builds strings out of linked lists, and a few other methods. I'm working on two methods, compareTo() and equals().

通过运行另一个名为 LStringTest 的文件来测试该类,该文件输出以下错误消息:

The class is tested by running another file, called LStringTest, which outputs this error message:

Running compareTo and equals tests (18 tests) Starting tests: .E.........E...E.E Time: 0.426 There were 4 failures: 1) t22aTestEquals[0](LStringTest$LStringCompareToTest) java.lang.Exception: test timed out after 100 milliseconds at LString.equals(LString.java:104) at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269) ... 9 more 2) t22aTestEquals[5](LStringTest$LStringCompareToTest) java.lang.Exception: test timed out after 100 milliseconds at LString.equals(LString.java:104) at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269) ... 9 more 3) t22aTestEquals[7](LStringTest$LStringCompareToTest) java.lang.Exception: test timed out after 100 milliseconds at LString.equals(LString.java:104) at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269) ... 9 more 4) t22aTestEquals[8](LStringTest$LStringCompareToTest) java.lang.Exception: test timed out after 100 milliseconds at LString.equals(LString.java:104) at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269) ... 9 more Test Failed! (4 of 18 tests failed.) Test failures: abandoning other phases. 

我相信我有 compareTo() equals()正确,而且我找不到错误的根源.

I believe I have compareTo() and equals() correct, and I can't find the source of the mistake.

这是我的代码:

import java.io.*; import java.util.*; public class LString { node front; int size; //Creating a node class private class node { char data; node next; public node (){ } public node (char newData){ this.data = newData; } public node (char newData, node newNext){ this.data = newData; this.next = newNext; } } //Constructors public LString(){ this.size = 0; this.front = null; } public LString(String original) { this.size = original.length(); if (original.length() > 0){ this.front = new node(original.charAt(0)); node curr = this.front; for (int i =1; i < original.length(); i++) { curr.next = new node(original.charAt(i)); curr = curr.next; } } } // Length method, returns the length of LString public int length() { return this.size; } // compareTo method, compares this LString to anotherLString, returns 0 if equal, // -1 if lexicogrpahically less, and 1 if lexicographically greater public int compareTo(LString anotherLString) { int len1 = length(); int len2 = anotherLString.length(); int lim = Math.min(len1, len2); node cn1 = front; node cn2 = anotherLString.front; int k = 0; while (k < lim) { char c1 = cn1.data; char c2 = cn2.data; if (c1 != c2) { return c1-c2; } k++; cn1 = cn1.next; cn2 = cn2.next; } return len1 - len2; } // a boolean equals method that returns true if LString and other are the same, false if not public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof LString) { LString otherLString = (LString)other; int n = length(); if (n == otherLString.length()) { node n1 = front; node n2 = otherLString.front; while (n1 != null) { if (n1.data != n2.data) { return false; } } return true; } } return false; } } 

感谢您提出任何建议,请尝试学习Java.

Thanks for any advice, trying to learn java.

问题解答

您的 equals 方法包含一个无限循环.您的while循环会检查是否 n1!= null ,但是在循环中的任何地方都不会更改 n1 的值:

Your equals method contains an infinite loop. Your while-loop checks to see if n1 != null, but nowhere in the loop do you change the value of n1:

 while (n1 != null) { if (n1.data != n2.data) { return false; } } 

在检查每个值之后,您需要将节点向下推进列表:

You need to advance the nodes down the list after you check each value:

while (n1 != null) { if (n1.data != n2.data) { return false; } n1 = n1.next; n2 = n2.next; } 

这篇关于为链接列表对象编写一个equals()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^