我无法写一个compareTo()
和charAt()
方法是建立一个字符串链表对象。类,称为LString
包含一个构造函数和其他一些方法。它运行与测试其能力,作为一个链表字符串生成另一个文件,我收到此错误信息:LString类,用链表做琴弦,JAVA
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的String
和StringBuilder
,但链表而不是阵列。我对如何使用this
关键字有点困惑。在下面的compareTo()
方法中,我想象使用this
通过对自己说:“如果此索引处的此LStrings字符等于同一索引处的参数的LString字符,则返回0.”
我引用此页面,但不能确定如何将其有效地写:http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
我想compareTo()
返回0,如果LStrings有相同的字符,值小于零,如果这LString排在最后少而不是另一个字符串,如果按字典顺序更大,则其值大于零。
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。
只是FYI你comapreTo方法将抛出一个空指针异常,如果'anotherString'比哪种方法被称为 – Vihar 2015-03-03 05:07:33