JAVA - LString Java,链接字符串

标签 java string list builder

在我的程序中,我有一个 LString.java,它不应该有输入或输出,但如何测试编译是通过 LStringTest.java 进行的。该程序也通过 junit.jar 运行。

我的程序在前半部分表现良好,但随后开始给我错误消息,我不太确定它告诉我要做什么以及如何修复它。我想我的问题是如何解决这些错误,以便我的程序可以在没有任何错误的情况下运行其余的测试。预先感谢您!

下面是到目前为止我的代码的输出以及它给我的错误:

----jGRASP exec: java LStringTest
Running constructor, length, toString tests (10 tests)
Starting tests: ..........
Time: 0.010
OK! (10 tests passed.)

Running compareTo and equals tests (18 tests)
Starting tests: ..................
Time: 0.022
OK! (18 tests passed.)

Running charAt and setCharAt tests (18 tests)
Starting tests: ..................
Time: 0.032
OK! (18 tests passed.)

Running substring tests (63 tests)
Starting tests: ...............................................................
Time: 0.076
OK! (63 tests passed.)

Running replace tests (31 tests)
Starting tests: E...EEE....EEEEEEE...EEEEEEE...
Time: 0.046

There were 18 failures:
1) test61aReplaceEmptyString(LStringTest$LStringReplaceTestSpecial)
java.lang.NullPointerException
        at LString.replace(LString.java:171)
        at LStringTest$LStringReplaceTestSpecial.test61aReplaceEmptyString(LStringTest.java:553)
        ... 10 more
2) test63aReplaceOneChar(LStringTest$LStringReplaceTestSpecial)
java.lang.NullPointerException
        at LString.replace(LString.java:190)
        at LStringTest$LStringReplaceTestSpecial.test63aReplaceOneChar(LStringTest.java:576)
        ... 10 more
3) test63bReplaceOneChar(LStringTest$LStringReplaceTestSpecial)
java.lang.AssertionError: replace returned different LString expected same:<a> was not:<axyzzy>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTestSpecial.test63bReplaceOneChar(LStringTest.java:587)
        ... 10 more
4) test63cReplaceOneChar(LStringTest$LStringReplaceTestSpecial)
org.junit.ComparisonFailure: Replace of One Character LString is wrong expected:<[]xyzzy> but was:<[a]xyzzy>
        at org.junit.Assert.assertEquals(Assert.java:115)
        at LStringTest$LStringReplaceTestSpecial.test63cReplaceOneChar(LStringTest.java:593)
        ... 10 more
5) test71aReplacePrepend[0](LStringTest$LStringReplaceTest)
java.lang.NullPointerException
        at LString.replace(LString.java:190)
        at LStringTest$LStringReplaceTest.test71aReplacePrepend(LStringTest.java:660)
        ... 10 more
6) test71bReplaceAppend[0](LStringTest$LStringReplaceTest)
java.lang.AssertionError: replace returned different LString expected same:<abcd> was not:<abcdxyzzy>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTest.test71bReplaceAppend(LStringTest.java:673)
        ... 10 more
7) test71cReplaceInMiddle[0](LStringTest$LStringReplaceTest)
java.lang.AssertionError: replace returned different LString expected same:<abcd> was not:<abxyzzycd>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTest.test71cReplaceInMiddle(LStringTest.java:683)
        ... 10 more
8) test71dReplaceAtStart[0](LStringTest$LStringReplaceTest)
java.lang.NullPointerException
        at LString.replace(LString.java:190)
        at LStringTest$LStringReplaceTest.test71dReplaceAtStart(LStringTest.java:690)
        ... 10 more
9) test71eReplaceAtEnd[0](LStringTest$LStringReplaceTest)
java.lang.AssertionError: replace returned different LString expected same:<abcd> was not:<abxyzzy>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTest.test71eReplaceAtEnd(LStringTest.java:703)
        ... 10 more
10) test71fReplaceInMiddle[0](LStringTest$LStringReplaceTest)
java.lang.AssertionError: replace returned different LString expected same:<abcd> was not:<abxyzzycd>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTest.test71fReplaceInMiddle(LStringTest.java:713)
        ... 10 more
11) test71gReplaceAll[0](LStringTest$LStringReplaceTest)
org.junit.ComparisonFailure: replace(0, length) is wrong expected:<[]xyzzy> but was:<[a]xyzzy>
        at org.junit.Assert.assertEquals(Assert.java:115)
        at LStringTest$LStringReplaceTest.test71gReplaceAll(LStringTest.java:721)
        ... 10 more
12) test71aReplacePrepend[1](LStringTest$LStringReplaceTest)
java.lang.NullPointerException
        at LString.replace(LString.java:190)
        at LStringTest$LStringReplaceTest.test71aReplacePrepend(LStringTest.java:660)
        ... 10 more
13) test71bReplaceAppend[1](LStringTest$LStringReplaceTest)
java.lang.AssertionError: replace returned different LString expected same:<Yet another even longer string.> was not:<Yet another even longer string.xyzzy>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTest.test71bReplaceAppend(LStringTest.java:673)
        ... 10 more
14) test71cReplaceInMiddle[1](LStringTest$LStringReplaceTest)
java.lang.AssertionError: replace returned different LString expected same:<Yet another even longer string.> was not:<Yet another evexyzzyn longer string.>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTest.test71cReplaceInMiddle(LStringTest.java:683)
        ... 10 more
15) test71dReplaceAtStart[1](LStringTest$LStringReplaceTest)
java.lang.NullPointerException
        at LString.replace(LString.java:190)
        at LStringTest$LStringReplaceTest.test71dReplaceAtStart(LStringTest.java:690)
        ... 10 more
16) test71eReplaceAtEnd[1](LStringTest$LStringReplaceTest)
java.lang.AssertionError: replace returned different LString expected same:<Yet another even longer string.> was not:<Yet another evexyzzy>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTest.test71eReplaceAtEnd(LStringTest.java:703)
        ... 10 more
17) test71fReplaceInMiddle[1](LStringTest$LStringReplaceTest)
java.lang.AssertionError: replace returned different LString expected same:<Yet another even longer string.> was not:<Yet anotxyzzy string.>
        at org.junit.Assert.fail(Assert.java:88)
        at org.junit.Assert.failNotSame(Assert.java:737)
        at org.junit.Assert.assertSame(Assert.java:680)
        at LStringTest$LStringReplaceTest.test71fReplaceInMiddle(LStringTest.java:713)
        ... 10 more
18) test71gReplaceAll[1](LStringTest$LStringReplaceTest)
org.junit.ComparisonFailure: replace(0, length) is wrong expected:<[]xyzzy> but was:<[Y]xyzzy>
        at org.junit.Assert.assertEquals(Assert.java:115)
        at LStringTest$LStringReplaceTest.test71gReplaceAll(LStringTest.java:721)
        ... 10 more

Test Failed! (18 of 31 tests failed.)

Test failures: abandoning other phases.

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.
<小时/>

这是我的代码,LString.java:

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;
                    }
                    n1 = n1.next;
                    n2 = n2.next;
                }
                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();
        }

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

        throw new IllegalStateException();
    }

    public void setCharAt(int index, char ch) {
      if (index < 0 || index >= this.length()) {
         throw new IndexOutOfBoundsException();
      }

      else {
         node currNode = front;
         for (int i = 0; i <this.length(); i++, currNode = currNode.next) {
            if (i == index) {
            currNode.data = ch;
            }
         }
      }
   }

    public LString substring(int start, int end) {
      if (start < 0 || end > this.length() || start > end) {
         throw new IndexOutOfBoundsException();
      }

      LString substring = new LString();
      if (start == end) {
         return substring;
      }
      node node = this.front;
      for (int i = 0; i < start; i++) {
         node = node.next;
      }

      node copy = new node(node.data);
      substring.front = copy;
      for (int i = start+1; i < end; i++) {
         node = node.next;
         copy = copy.next = new node(node.data);
      }

      substring.size = end - start;
      return substring;      
    }

    public LString replace(int start,   int end, LString lStr) {
        if (start < 0 || end > length() || start > end) {
         throw new IndexOutOfBoundsException();
    }

    LString result = new LString();
    node node = this.front;
    node copy = new node(node.data);
       result.front = copy;
    for (int i = 1; i < start; i++) {
        node = node.next;
        copy = copy.next = new node(node.data);
    }

    node replace = lStr.front;
    for (int i = 0; i < lStr.length(); i++) {
        copy = copy.next = new node(replace.data);
        replace = replace.next;
    }

    for (int i = start; i < end; i++) {
        node = node.next;
    }

    for (int i = end; i < length(); i++) {
        node = node.next;
        copy = copy.next = new node(node.data);
    }

    result.size = length() + lStr.length() - (end - start);
    return result;
}   

    public void append(char data) {

        this.size++;

        if  (front == null) {
             front = new node(data);
             return;
        }

        node curr = front;
        while (curr.next != null) {
             curr = curr.next; 
        }

        curr.next = new node(data);

    }

    public void prepend (char data) {

        front = new node(data, front);
        size++;
    }


    public void delete(int index) {

        if (index == 0) {
             front = front.next;
        } else {
             node curr = front;
             for (int i = 0; i < index - 1; i++){
                curr = curr.next;
             }

             curr.next = curr.next.next;
          }

        size--;
    }

    public String toString() {
        StringBuilder result = new StringBuilder();

        node curr = front;
        while (curr != null) {

            result.append(curr.data);
            curr = curr.next;
        }

        return result.toString();
    }

    public void add(int index, char data) {
         if (index == 0) {
              front = new node(data, front);
         }  else {
              node curr = front;
              for (int i = 0; i < index - 1;  i++){
                    curr = curr.next;
              }

              curr.next = new node(data, curr.next);
            }
         }

    public void print() {
    System.out.println(this.toString());

    }
} 
<小时/>

@ScaryWombat 的错误 1:

这是来自 LStringTest.java 的代码:

@Test public void test61aReplaceEmptyString() {
         LString testLString = new LString();
         LString newLString = testLString.replace(0, 0, replaceLString);
         assertEquals("Replace of Empty LString is wrong",
               replaceString, newLString.toString());
         assertSame("replace returned different LString", testLString, newLString);
      }

这是 LString.java 中出现错误的代码片段。从第 171 行开始...

LString result = new LString();
    node node = this.front;
    node copy = new node(node.data);
       result.front = copy;

最佳答案

好吧,让我们稍微分解一下

test61aReplaceEmptyString 中调用时

 LString testLString = new LString();

它调用构造函数

// Constructors
 public LString(){
      this.size = 0;
      this.front = null;
 }

看到this.frontnull

现在,当您调用 testLString.replace(0, 0, ReplaceLString);

你会看到replace中有代码

node node = this.front;

记住 this.frontnull 所以现在 node 也是 null

顺便说一句,类名和对象名相同,这非常令人困惑。

无论如何,由于 nodenull 那么

 new node(node.data);

将抛出 NPE

要修复它,您需要测试现有的 LString 是否为空(即节点为空)

public LString replace(int start,   int end, LString lStr) {
    if (start < 0 || end > length() || start > end) {
     throw new IndexOutOfBoundsException();
    }

    if (this.front == null) {
       return lStr; // maybe correct logic for you?
    }

关于JAVA - LString Java,链接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37473206/

相关文章:

java - 主机名中是否支持下划线?

java - 从实例内部 inflatedView 不起作用

java - Jackson 将 XML 中的平面数组反序列化为 Pojo 列表

java - 如何使用JPA聚合字符串?

python - 在 python 中使用 .append 定义列表

python - 基于python中的变量类型处理数据子集

java - VideoView setVideoUri 否 Content Provider

python - 将文本列表格式化为列

java - 字符串资源的安全

python - 嵌套列表,检查一个列表是否与其他列表有共同元素,如果有则加入