当前位置: 首页 > 面试题库 >

LString类,使用链接列表制作字符串,java

白泽语
2023-03-14
问题内容

我在为构建字符串的链表对象编写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: EEEEEEEE.EEE.E....
Time: 0.016

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.IndexOutOfBoundsException: bad index
        at LString.charAt(LString.java:91)
        at LString.compareTo(LString.java:64)
        at LStringTest$LStringCompareToTest.t21aTestCompareTo(LStringTest.java:259)
        ... 9 more
4) t22aTestEquals[1](LStringTest$LStringCompareToTest)
java.lang.NullPointerException
        at LString.equals(LString.java:79)
        at LStringTest$LStringCompareToTest.t22aTestEquals(LStringTest.java:269)
        ... 9 more
5) t21aTestCompareTo[2](LStringTest$LStringCompareToTest)
java.lang.IndexOutOfBoundsException: bad index
        at LString.charAt(LString.java:91)
        at LString.compareTo(LString.java:64)
        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.IndexOutOfBoundsException: bad index
        at LString.charAt(LString.java:91)
        at LString.compareTo(LString.java:64)
        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) 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
10) 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
11) 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
12) t22aTestEquals[6](LStringTest$LStringCompareToTest)
java.lang.NullPointerException
        at LString.equals(LString.java:79)
        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()下面的方法中,我想this通过对自己说:“如果此索引处的此LStrings字符等于相同索引处的参数的LString字符,则返回0。”

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

compareTo()如果LString具有完全相同的字符,我想返回0;如果此LString在字典上小于anotherLString,则返回小于零的值;如果在字典上大于,则返回大于零的值。

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  !=  ""){

              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 total = 0;
      for (int i = 0; i < anotherLString.length(); i++) {
         total += this.charAt(i) - anotherLString.charAt(i);
      }
        return total;

        //}
        //return this.length()-anotherLString.length();
    }

   // a boolean equals method that returns true if LString and other are the same, false if not
    public boolean  equals(Object other)    {
        if  (other == null  ||  !(other instanceof LString)) {
            return false;
        }
        else {
            LString otherLString    = (LString)other;
         if (this.front.data == otherLString.front.data) {

               return true;
         }
        }
      return true;
    }

   // charAt returns the character of LString at the argument index
    public char charAt(int index)   {

        if ((index < 0) || (index >= this.length())) {
         throw new IndexOutOfBoundsException("bad index");
      }
      return this.front.data;
    }

该代码未完成所讨论的方法。感谢您尝试学习Java的任何建议。


问题答案:

我已对您的代码进行了更正。

您应该定义hashCode()是否定义equals()。将LString实现为CharSequence也很有用。

public class LString implements Comparable<LString>
{
    Node front;
    int size;

    //Creating a node class
    private static 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 );
        // char v1 = front.data;
        //   char v2 = anotherLString.front.data;
        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 anotherLString = ( LString ) other;
            int n = length();
            if ( n == anotherLString.length() )
            {
                Node n1 = front;
                Node n2 = anotherLString.front;
                while ( n1 != null )
                {
                    if ( n1.data != n2.data )
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        return false;
    }

    // charAt returns the character of LString at the argument index
    public char charAt( int index )
    {
        if ( ( index < 0 ) || ( index >= this.length() ) )
        {
            throw new IndexOutOfBoundsException( "bad index" );
        }

        Node curNode = front;
        for ( int i = 0; i < this.length(); i++, curNode = curNode.next )
        {
            if ( i == index )
            {
                return curNode.data;
            }
        }

        throw new IllegalStateException();
    }
}


 类似资料:
  • 在“基本类型”一章中,介绍了字符串,以及使用is_binary/1函数检查它: iex> string = "hello" "hello" iex> is_binary string true 本章将学习理解:二进制串(binaries)是个啥,它怎么和字符串(strings)扯上关系的; 以及用单引号包裹的值'like this'是啥意思。 UTF-8和Unicode 字符串是UTF-8编码的二

  • 我有一个值为-f.e.:。 我想替换这个字符: ,,,,,,,与那些: <代码>>、<代码>>、<代码>l、<代码>o、<代码>s、<代码>z、<代码>c、<代码>n。 这里我的意思是,如果解析器将找到f.e.:char(在第一个列表中是第二个)应该替换为在第二个列表中处于相同位置/位置的char,在本例中是:。 char应替换为char。 char应替换为char。 在我的例子中,要替换的字符列

  • 我想对返回 List 的方法使用 Optional 假设函数是 因此,该函数如下所示: 现在我想做一些事情,如果列表存在,所以像这样: 我是Optional的新手,已经浏览了关于Optional的文章,看起来这应该可以,但是我在IDE中遇到了语法错误: 方法ifPresent不适用于参数(无效)。 我想从那些可能更熟悉java 8中lamdas的开发人员那里获得一些帮助。

  • 我要做的是打印输入的反向数字。输入=“1 3 4 2”输出=“2 4 3 1” 我想知道为什么我的代码显示此错误: 错误:< code > string . join(ar)type错误:序列项0:预期的str实例,找到int 如果我将map参数从int改为str,它不会显示任何结果。

  • 问题内容: 我用这个: 但是Python 3出现了错误(或者…也许我只是忘了包含一些东西): 谢谢。 问题答案: 在Python3中,文字字符串默认为unicode。 假设这是一个对象,只需使用 的Python2等效于Python3,因此您还可以编写: 若你宁可。

  • 问题内容: 我有以下字符串: 我需要将其转换为: “查看http://www.google.com ” 原始字符串可以包含多个URL字符串。 如何在php中做到这一点? 谢谢 问题答案: 您可以使用以下内容: PHP版本 <5.3(ereg_replace),否则(preg_replace)