LString将是一个链表类,它模仿标准的JavaString和StringBuilder类
我无法为构建字符串的链表对象编写charAt()和seCharAt方法。这个名为LString的类包含一个构造函数和一些其他方法。它与另一个文件一起运行,该文件测试其作为链表字符串生成器的能力,我收到以下错误消息: 运行构造函数、长度、toString测试(10个测试) 启动测试:。。。。。。。。。。 时间:0.032 好啊(10项测试通过。) 运行compareTo和equals测试(18个测试) 启动测试:。。。。。。。。。。。。。。。。。。 时间:0.041 好啊(18项测试通过。) 运行charAt和setCharAt测试(18个测试) 开始测试:…E…..E…..E。。 时间:0.065 有3次失败: 1) T33B测试索引自动边界ScharatLength 0 java.lang.Exception:意外异常,应为,但为 ... 还有两个 这就是我的代码的样子LString将是一个链表类,它模仿标准的JavaString和StringBuilder类,java,Java,我无法为构建字符串的链表对象编写charAt()和seCharAt方法。这个名为LString的类包含一个构造函数和一些其他方法。它与另一个文件一起运行,该文件测试其作为链表字符串生成器的能力,我收到以下错误消息: 运行构造函数、长度、toString测试(10个测试) 启动测试:。。。。。。。。。。 时间:0.032 好啊(10项测试通过。) 运行compareTo和equals测试(18个测试) 启动测试:。。。。。。。。。。。。。。。。。。 时间:0.041 好啊(18项测试通过。) 运行
public class LString {
public class node {
char data;
node next;
public node(char data) {
this.data = data;
this.next = null;
}
public node(char data, node next) {
this.data = data;
this.next = next;
}
}
node front;
public LString() {
this.front = null;
}
public LString(String original){
if (original.length() == 0){
this.front = null;
}else{
front = new node(original.charAt(0));
node current = front;
for(int i = 1; i < original.length(); i ++){
current.next = new node(original.charAt(i));
current = current.next;
}
}
}
// finds length of LString
public int length(){
int length = 0;
node list = front;
if (list == null){
return 0;
}else{
while (list != null){
list = list.next;
length += 1;
}
return length;
}
}
// creats a string with the same contents as LString
public String toString(){
String word = "";
for (int c = 0; c < this.length(); c++){
word = word + charAt(c);
}
return word;
}
//compares two LStrings and finds the one that is lexicographically greater
public int compareTo(LString anotherLString) {
int shortString;
if (this.length() < anotherLString.length()){
shortString = this.length();
}else{
shortString = anotherLString.length();
}
int difference = 0;
if(this.length() == 0 && anotherLString.length() > 0){
return -1;
}
if(anotherLString.length() == 0 && this.length() > 0){
return 1;
}
if(this.length() < anotherLString.length()){
return -1;
}
if(anotherLString.length() < this.length()){
return 1;
}
for (int j = 0; j < shortString; j++){
if (this.charAt(j) > anotherLString.charAt(j)){
difference += 1;
}else if (this.charAt(j) == anotherLString.charAt(j)){
difference += 0;
}else{
difference -= 1;
}
}
return difference;
}
//returns true if LString and other have the same characters
@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof LString)){
return false;
}else {
LString otherLString = (LString)other;
if (this.length() != otherLString.length()){
return false;
}else{
for (int j = 0; j < otherLString.length(); j++){
if (this.charAt(j) != otherLString.charAt(j)){
return false;
}
}
}
}
return true;
}
//returns the character at index
public char charAt(int index){
if (index < 0 || index > this.length() - 1){
throw new IndexOutOfBoundsException("Bad index");
}else{
node curr = front;
for (int i = 0; i < index; i++){
curr = curr.next;
}
return curr.data;
}
}
//set character at given index to ch
public void setCharAt(int index, char ch){
node curr = front;
if (index < 0 || index > this.length() - 1){
throw new IndexOutOfBoundsException("Bad index");
}else{
for (int i = 0; i < index; i++){
curr = curr.next;
}
curr.data = ch;
}
}
// returns a substring from start to end
public LString substring(int start, int end){
String newString = "";
node curr = front;
if (start < 0 || end > this.length() || end < start ){
throw new IndexOutOfBoundsException("Bad index");
}
int i = 0;
while (i <= start){
if (i == start){
for (int j = start; j < end; j++){
newString += curr.data;
curr = curr.next;
}
return (new LString(newString));
}
i++;
curr = curr.next;
}
return null;
}
//replaces substring with lStr
public LString replace(int start, int end, LString lStr){
if (start < 0 || end > this.length() || end < start){
throw new IndexOutOfBoundsException("Bad index");
}
String newString = this.substring(0, start).toString() + lStr.toString() +
this.substring(end, this.length()).toString();
LString newLString = new LString(newString);
this.front = newLString.front;
return this;
}
}
公共类LString{
公共类节点{
字符数据;
节点下一步;
公共节点(字符数据){
这个数据=数据;
this.next=null;
}
公共节点(字符数据,节点下一步){
这个数据=数据;
this.next=next;
}
}
节点锋;
公共电话号码(){
this.front=null;
}
公共LString(原始字符串){
if(原始.length()==0){
this.front=null;
}否则{
front=新节点(original.charAt(0));
节点电流=前端;
对于(int i=1;i0){
返回-1;
}
if(另一个字符串.length()==0&&this.length()>0){
返回1;
}
if(this.length()另一个字符(j)){
差值+=1;
}else如果(这个字符(j)=另一个字符(j)){
差值+=0;
}否则{
差值-=1;
}
}
收益差;
}
//如果LString和其他具有相同字符,则返回true
@凌驾
公共布尔等于(对象其他){
if(other==null | |!(LString的其他实例)){
返回false;
}否则{
LString otherLString=(LString)other;
if(this.length()!=otherLString.length()){
返回false;
}否则{
对于(int j=0;jthis.length()-1){
抛出新索引AutofBoundsException(“坏索引”);
}否则{
节点电流=前端;
对于(int i=0;ithis.length()-1){
抛出新索引AutofBoundsException(“坏索引”);
}否则{
对于(int i=0;i此.length()| |结束<开始){
抛出新索引AutofBoundsException(“坏索引”);
}
int i=0;
while(i this.length()| | end
有什么建议吗?非常感谢你 你能在不发布这么多代码的情况下缩小问题的范围吗?它给了我一个java.lang.NullPointerException错误,而不是charAt和setCharAt上的IndexOutOfBounds(你可以查看这些代码)。我不知道我做错了什么