gpt4 book ai didi

java - 链表toUppercase方法返回类型错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:26:50 25 4
gpt4 key购买 nike

我有一个名为 LString 的类(一个链表类),它与我的其他 Node 类一起工作。我写了一个 toUppercase() 方法,遍历字符序列并将小写转换为大写。

我的问题是返回类型,这是我在编写大量代码时似乎遇到的问题。我不确定如何返回所需的 LString 类型,因为即使我键入 return LString,它也会将其识别为变量并给出相同的不兼容类型错误。

这是显式错误:

    LString.java:134: error: incompatible types
return current;
^
required: LString
found: Node

如何在我的方法中返回这种必需类型的 LString?

我是 Java 的新手,在编写本文时掌握返回类型对我来说似乎很麻烦。让我知道我是否应该发布我的整个类(class)。如果我的问题有点不清楚,也请告诉我,我想与本论坛的用户简明扼要。

根据要求,这里是我的更多代码,用于指定我在两个类中所做的声明。

我的节点类:

      public class Node{
public char data;
public Node next;

//constructors from page 956
public Node()
{
this('\0',null); //'\0' is null char for java
}

public Node(char initialData, Node initialNext)
{
data = initialData;
next = initialNext;
}
}

还有我的 LString 类(我将只列出构造函数和 toUppercase 方法):

public class LString{

private Node front = null; //first val in list
private Node back; //last val in list
private int size = 0;
private int i;

public LString(){
//construct empty list
Node LString = new Node();
front = null;

}
public LString toUppercase(){
Node current = front;
while(current != null){
current.data = Character.toUpperCase(current.data);
current = current.next;
}
return front;
}
}

如果我需要提供更多信息,请告诉我。

最佳答案

要返回所需的 LString,只需执行以下操作:

return this;

因为 LString 是包含链表第一个节点的类,所有修改链表的方法都应该简单地返回它。另请注意,此行在构造函数中没有执行任何操作,您可以将其删除:

Node LString = new Node();

关于java - 链表toUppercase方法返回类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20133492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com