为链接的字符列表编写compareTo()方法

时间:2015-03-03 03:08:42

标签: java linked-list compareto

我在为构建字符串的链接列表对象编写compareTo()charAt()方法时遇到问题。这个名为LString的类包含一个构造函数和一些其他方法。它运行另一个文件,测试其作为链接列表字符串生成器的能力,我收到此错误消息:

Running constructor, length, toString tests (10 tests)
Starting tests: ..........
Time: 0.000
OK! (10 tests passed.)

Running compareTo and equals tests (18 tests)
Starting tests: EEEEEEEEEEEE......
Time: 0.015

There were 12 failures:
1) t21aTestCompareTo[0](LStringTest$LStringCompareToTest)
java.lang.AssertionError: compareTo of "abc" and "abd" wrong expected:<-1> but was:<0>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at org.junit.Assert.assertEquals(Assert.java:555)
        at LStringTest$LStringCompareToTest.t21aTestCompareTo(LStringTest.java:259)
        ... 9 more
2) t22aTestEquals[0](LStringTest$LStringCompareToTest)
java.lang.AssertionError: equals of "abc" and "abd" wrong expected:<false> but was:<true>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
3) t21aTestCompareTo[1](LStringTest$LStringCompareToTest)
java.lang.AssertionError: compareTo of "a" and "" wrong expected:<1> but was:<0>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at org.junit.Assert.assertEquals(Assert.java:555)
        at LStringTest$LStringCompareToTest.t21aTestCompareTo(LStringTest.java:261)
        ... 9 more
4) t22aTestEquals[1](LStringTest$LStringCompareToTest)
java.lang.AssertionError: equals of "" and "a" wrong expected:<false> but was:<true>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
5) t21aTestCompareTo[2](LStringTest$LStringCompareToTest)
java.lang.AssertionError: compareTo of "a" and "ab" wrong expected:<-1> but was:<0>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at org.junit.Assert.assertEquals(Assert.java:555)
        at LStringTest$LStringCompareToTest.t21aTestCompareTo(LStringTest.java:259)
        ... 9 more
6) t22aTestEquals[2](LStringTest$LStringCompareToTest)
java.lang.AssertionError: equals of "a" and "ab" wrong expected:<false> but was:<true>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
7) t21aTestCompareTo[3](LStringTest$LStringCompareToTest)
java.lang.AssertionError: compareTo of "abc" and "abcd" wrong expected:<-1> but was:<0>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at org.junit.Assert.assertEquals(Assert.java:555)
        at LStringTest$LStringCompareToTest.t21aTestCompareTo(LStringTest.java:259)
        ... 9 more
8) t22aTestEquals[3](LStringTest$LStringCompareToTest)
java.lang.AssertionError: equals of "abc" and "abcd" wrong expected:<false> but was:<true>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
9) t21aTestCompareTo[4](LStringTest$LStringCompareToTest)
java.lang.AssertionError: compareTo of "B" and "a" wrong expected:<-1> but was:<0>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at org.junit.Assert.assertEquals(Assert.java:555)

        at LStringTest$LStringCompareToTest.t21aTestCompareTo(LStringTest.java:259)
        ... 9 more
10) t22aTestEquals[4](LStringTest$LStringCompareToTest)
java.lang.AssertionError: equals of "B" and "a" wrong expected:<false> but was:<true>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
11) t21aTestCompareTo[5](LStringTest$LStringCompareToTest)
java.lang.AssertionError: compareTo of "BB" and "Ba" wrong expected:<-1> but was:<0>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at org.junit.Assert.assertEquals(Assert.java:555)
        at LStringTest$LStringCompareToTest.t21aTestCompareTo(LStringTest.java:259)
        ... 9 more
12) t22aTestEquals[5](LStringTest$LStringCompareToTest)
java.lang.AssertionError: equals of "BB" and "Ba" wrong expected:<false> but was:<true>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotEquals(Assert.java:743)
        at org.junit.Assert.assertEquals(Assert.java:118)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more

Test Failed! (12 of 18 tests failed.)

Test failures: abandoning other phases.

LString类用于模仿Java的StringStringBuilder,但是使用链接列表而不是数组。我对如何使用this关键字感到有些困惑。在下面的compareTo()方法中,我想通过对自己说“如果此索引处的LStrings字符等于同一索引处的参数的LString字符,返回0”,使用this

我正在引用此页面,但不确定如何有效地编写它:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29

如果LStrings具有完全相同的字符,我希望compareTo()返回0;如果此LString按字典顺序小于anotherLString,则希望返回小于零的值;如果字典值大于字典值,则希望值大于零。

public class LString    {

     node   front;
     int size;

     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;
          }


     }

     public LString(){
          this.size = 0;
          this.front =  null;
     }
     public LString(String original) {
        this.size = original.length();
        if (original != ""){

          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;
           }
        }



     }


     public int length()    {
        return this.size;
    }
    public int compareTo(LString anotherLString)    {
      for (int i = 0; i < this.length(); i++) {
         if (this.charAt(i) == anotherLString.charAt(i)) {
            return 0;
         }
         //other logic for returning 1 or -1
      }
      return this.length()-anotherLString.length();
    }
    public boolean  equals(Object other) {
        if  (other == null  ||  !(other instanceof LString)) {
            return false;
        }
        else {
            LString otherLString = (LString)other;
            return true;
        }
    }
    public char charAt(int index)   {
        //return this.charAt(index)?
    }
}

此代码中未完成相关方法。任何建议都值得赞赏,试图学习Java。

1 个答案:

答案 0 :(得分:0)

   public int compareTo(LString anotherLString)    {
         for (int i = 0; i < this.length() && i<anotherLString.length(); i++) {
            if (this.charAt(i) == anotherLString.charAt(i)) {
               return 0;
            }
            else if(((int)this.charAt(i))<((int)anotherLString.charAt(i)))
               return -1;
            else
               return 1;
         }
         return this.length()-anotherLString.length();
       }

我认为此解决方案应该可以满足您的要求。我已将char类型转换为int,然后将它们与返回-1 / 1进行比较。此外,我还在for循环中添加了长度限制。

注意:此处使用charAt(index)不是有效的解决方案,因为每次获取字符时都会遍历链接列表。相反,您应该直接遍历链接列表以进行比较。