- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在为我正在构建的名为 LString
的类编写 substring()
方法时遇到一些问题。此类创建一个名为 LString
的链表对象,用于构建字符串。它模仿 Java 中的 String
和 StringBuilder
对象。
substring(int start, int end)
根据 提供的索引,从给定的
到this
LString 创建一个新的 LString
>开始结束
。它返回类型LString
。
以下是错误消息,未进行任何编辑以使 end
包含在内:
Running substring tests (63 tests)
Starting tests: ......E........................................................
Time: 0.031
There was 1 failure:
1) test43cSubStringOneChar(LStringTest$LStringSubStringTestSpecial)
java.lang.AssertionError: Substring of One Character LString is not equals LString expected: LString<a> but was: LString<a>
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$LStringSubStringTestSpecial.test43cSubStringOneChar(LStringTest.java:401)
... 10 more
Test Failed! (1 of 63 tests failed.)
Test failures: abandoning other phases.
这会产生一个更简单的错误消息,只有 1 次失败。
以下是与该错误消息对应的代码:
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);
}
return substring;
}
public LString replace(int start, int end, LString lStr) {
return null;
}
public String toString(){
StringBuilder result = new StringBuilder();
node curr = front;
while (curr != null){
result.append(curr.data);
curr = curr.next;
}
return result.toString();
}
}
一些注意事项:我确实编写了一个 toString()
方法,在这段代码中还有一个 replace()
方法,我将在这之后编写。
以下是使 end
包含在内的错误和代码:
错误:
Running substring tests (63 tests)
Starting tests: E....EEE....E.EEEEEE.....E.EEEEEEE....E.EEEEEEE....E.EEEEEEE...
Time: 0.028
There were 35 failures:
1) test41aSubStringEmpty(LStringTest$LStringSubStringTestSpecial)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTestSpecial.test41aSubStringEmpty(LStringTest.java:368)
... 10 more
2) test43bSubStringOneChar(LStringTest$LStringSubStringTestSpecial)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTestSpecial.test43bSubStringOneChar(LStringTest.java:395)
... 10 more
3) test43cSubStringOneChar(LStringTest$LStringSubStringTestSpecial)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTestSpecial.test43cSubStringOneChar(LStringTest.java:401)
... 10 more
4) test43dSubStringOneCharIsNew(LStringTest$LStringSubStringTestSpecial)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTestSpecial.test43dSubStringOneCharIsNew(LStringTest.java:407)
... 10 more
5) test51bEmptySubstringAtEnd[0](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51bEmptySubstringAtEnd(LStringTest.java:468)
... 10 more
6) test51dSubstringAtStart[0](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(0, mid) is not correct expected:<a[]> but was:<a[b]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51dSubstringAtStart(LStringTest.java:478)
... 10 more
7) test51eSubstringAtEnd[0](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51eSubstringAtEnd(LStringTest.java:483)
... 10 more
8) test51fSubstringInMiddle[0](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(left, right) is not correct expected:<[]> but was:<[b]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51fSubstringInMiddle(LStringTest.java:488)
... 10 more
9) test51gSubstringAll[0](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51gSubstringAll(LStringTest.java:493)
... 10 more
10) test51hSubstringAtStartIsNew[0](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(0, mid) is not new LString expected:<a[]> but was:<a[b]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51hSubstringAtStartIsNew(LStringTest.java:500)
... 10 more
11) test51jSubstringAtEndIsNew[0](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51jSubstringAtEndIsNew(LStringTest.java:505)
... 10 more
12) test51bEmptySubstringAtEnd[1](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51bEmptySubstringAtEnd(LStringTest.java:468)
... 10 more
13) test51dSubstringAtStart[1](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(0, mid) is not correct expected:<a[]> but was:<a[b]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51dSubstringAtStart(LStringTest.java:478)
... 10 more
14) test51eSubstringAtEnd[1](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51eSubstringAtEnd(LStringTest.java:483)
... 10 more
15) test51fSubstringInMiddle[1](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(left, right) is not correct expected:<b[]> but was:<b[c]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51fSubstringInMiddle(LStringTest.java:488)
... 10 more
16) test51gSubstringAll[1](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51gSubstringAll(LStringTest.java:493)
... 10 more
17) test51hSubstringAtStartIsNew[1](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(0, mid) is not new LString expected:<a[]> but was:<a[b]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51hSubstringAtStartIsNew(LStringTest.java:500)
... 10 more
18) test51jSubstringAtEndIsNew[1](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51jSubstringAtEndIsNew(LStringTest.java:505)
... 10 more
19) test51kSubstringInMiddleIsNew[1](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(left, right) is not new LString expected:<b[]> but was:<b[c]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51kSubstringInMiddleIsNew(LStringTest.java:515)
... 10 more
20) test51bEmptySubstringAtEnd[2](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51bEmptySubstringAtEnd(LStringTest.java:468)
... 10 more
21) test51dSubstringAtStart[2](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(0, mid) is not correct expected:<A long []> but was:<A long [s]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51dSubstringAtStart(LStringTest.java:478)
... 10 more
22) test51eSubstringAtEnd[2](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51eSubstringAtEnd(LStringTest.java:483)
... 10 more
23) test51fSubstringInMiddle[2](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(left, right) is not correct expected:<ng str[]> but was:<ng str[i]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51fSubstringInMiddle(LStringTest.java:488)
... 10 more
24) test51gSubstringAll[2](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51gSubstringAll(LStringTest.java:493)
... 10 more
25) test51hSubstringAtStartIsNew[2](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(0, mid) is not new LString expected:<A long []> but was:<A long [s]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51hSubstringAtStartIsNew(LStringTest.java:500)
... 10 more
26) test51jSubstringAtEndIsNew[2](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51jSubstringAtEndIsNew(LStringTest.java:505)
... 10 more
27) test51kSubstringInMiddleIsNew[2](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(left, right) is not new LString expected:<ng str[]> but was:<ng str[i]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51kSubstringInMiddleIsNew(LStringTest.java:515)
... 10 more
28) test51bEmptySubstringAtEnd[3](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51bEmptySubstringAtEnd(LStringTest.java:468)
... 10 more
29) test51dSubstringAtStart[3](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(0, mid) is not correct expected:<This is an even[]> but was:<This is an even[ ]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51dSubstringAtStart(LStringTest.java:478)
... 10 more
30) test51eSubstringAtEnd[3](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51eSubstringAtEnd(LStringTest.java:483)
... 10 more
31) test51fSubstringInMiddle[3](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(left, right) is not correct expected:<an even longer[]> but was:<an even longer[ ]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51fSubstringInMiddle(LStringTest.java:488)
... 10 more
32) test51gSubstringAll[3](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51gSubstringAll(LStringTest.java:493)
... 10 more
33) test51hSubstringAtStartIsNew[3](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(0, mid) is not new LString expected:<This is an even[]> but was:<This is an even[ ]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51hSubstringAtStartIsNew(LStringTest.java:500)
... 10 more
34) test51jSubstringAtEndIsNew[3](LStringTest$LStringSubStringTest)
java.lang.IndexOutOfBoundsException
at LString.substring(LString.java:142)
at LStringTest$LStringSubStringTest.test51jSubstringAtEndIsNew(LStringTest.java:505)
... 10 more
35) test51kSubstringInMiddleIsNew[3](LStringTest$LStringSubStringTest)
org.junit.ComparisonFailure: substring(left, right) is not new LString expected:<an even longer[]> but was:<an even longer[ ]>
at org.junit.Assert.assertEquals(Assert.java:115)
at LStringTest$LStringSubStringTest.test51kSubstringInMiddleIsNew(LStringTest.java:515)
... 10 more
Test Failed! (35 of 63 tests failed.)
Test failures: abandoning other phases.
以下是代码中的相关差异,仅substring()
不同:
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; i < end; i++) {
node = node.next;
copy = copy.next = new node(node.data);
}
return substring;
}
LStringTest
中有关错误的部分:
@Test public void test43aSubStringOneChar() {
LString testLString = new LString("a");
assertEquals("Substring of One Character LString is not Empty",
nullLString, testLString.substring(0, 0));
}
@Test public void test43bSubStringOneChar() {
LString testLString = new LString("a");
assertEquals("Substring of One Character LString is not Empty",
nullLString, testLString.substring(1, 1));
}
@Test public void test43cSubStringOneChar() {
LString testLString = new LString("a");
assertEquals("Substring of One Character LString is not equals LString",
testLString, testLString.substring(0, 1));
}
最佳答案
其他答案中的代码无法编译,即使编译了,也无法正常运行。
假设您使用标准(对于 Java)从零开始的索引,并且 end
索引是独占的,此代码将进行编译和测试。
public LString substring(int start, int end) {
if (start < 0 || end > length() || start > end) {
throw new IndexOutOfBoundsException();
}
LString result = new LString();
if (start == end) {
return result;
}
node node = this.front;
for (int i = 0; i < start; i++) {
node = node.next;
}
node copy = new node(node.data);
result.front = copy;
for (int i = start + 1; i < end; i++) {
node = node.next;
copy = copy.next = new node(node.data);
}
result.size = end - start;
return result;
}
如果您希望 end
具有包容性,尽管这违反了 Java 约定,您需要更改三件事:
end >= length()
而不是 end > length()
,if (start == end) { return substring; }
需要完全删除,并且int i = start
,而不是 int i = start + 1
。顺便说一句,如果类中有 toString()
方法,则可以更轻松地查看 LString
实例发生的情况。
@Override
public String toString() {
if (this.front == null) return "";
StringBuilder sb = new StringBuilder(length());
node node = this.front;
do sb.append(node.data);
while ((node = node.next) != null);
return sb.toString();
}
关于java - 链表对象的 substring() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865738/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!