- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在为构建字符串的链表对象编写 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 的 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
如果 LString 具有完全相同的字符,我希望 compareTo()
返回 0;如果此 LString 按字典顺序小于另一个 LString,则返回小于零的值;如果按字典顺序大于另一个 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。
最佳答案
我稍微更正了您的代码。
如果定义了equals()
,则应该定义hashCode()
。将 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();
}
}
关于java - LString类,使用链表来制作字符串,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28824474/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!