Java 如何显示void方法内容
当我编译程序时,我犯了3个错误,我尝试了一些其他的方法,但仍然没有成功。程序必须显示LinkedList传入的内容,然后查找传入字符串的索引并打印出来,然后反向打印LinkedList的内容。执行所有这些操作的方法都是LinkedList类中的void方法。 这是LinkedList:Java 如何显示void方法内容,java,Java,当我编译程序时,我犯了3个错误,我尝试了一些其他的方法,但仍然没有成功。程序必须显示LinkedList传入的内容,然后查找传入字符串的索引并打印出来,然后反向打印LinkedList的内容。执行所有这些操作的方法都是LinkedList类中的void方法。 这是LinkedList: public class LinkedList { private Node front; private int count; public LinkedList() {
public class LinkedList {
private Node front;
private int count;
public LinkedList() {
front = null;
count = 0;
}
public void addToFront(String d) {
Node n = new Node (d, front);
front = n;
count++;
}
public int size() {
return count;
}
public boolean isEmpty() {
return (count == 0);
}
public void clear() {
front = null;
count = 0;
}
public String getFrontData() {
if (front == null)
return "Empty List";
else
return front.getData();
}
public Node getFront() {
return front;
}
//EX_1
public void set(int index, String d) {
if (index < 0 || index > size())
System.out.println("Cant add. Index Out of Bounds.");
else if (index == 0)
addToFront(d);
else {
Node curr = front;
for (int i = 0; i < index-1; i++)
curr = curr.getNext();
Node n = new Node(d, curr.getNext());
curr.setNext(n);
count++;
}
}
// EX_2
public void listAll(String d) {
Node curr = front;
boolean found = false;
int index = -1;
while (curr != null && ! found) {
index ++;
if (curr.getData().equals(d))
found = true;
curr = curr.getNext();
}
if (!found)
System.out.print("did not find Anything");
else
System.out.print(index);
}
// EX_3
public void printReverse() {
if (front == null)
System.out.print("The List is Empty");
else {
for (int i = size()-1; i >= 0; i--)
System.out.print(i + " --> ");
}
}
public void enumerate() {
Node curr = front;
while (curr!= null) {
System.out.print(curr);
curr = curr.getNext();
}
System.out.println();
}
}// Class
这是编译器错误:
----jGRASP exec: javac -g LinkedListDemo.java
LinkedListDemo.java:8: set(int,java.lang.String) in LinkedList cannot be applied to ()
System.out.println("Strings in the Linked List: " + str.set());
^
LinkedListDemo.java:20: listAll(java.lang.String) in LinkedList cannot be applied to ()
System.out.println("Number of T's in the List: " + str.listAll());
^
LinkedListDemo.java:27: 'void' type not allowed here
System.out.print("Components of the list in Order: " + str.printReverse());
^
3 errors
----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
1) 今后,请尽量具体一点。始终剪切/粘贴准确的错误消息,并尽可能指向相关的代码行
2) 即使您克服了编译错误,System.out.println(curr)
也可能不是您想要的
建议:
3) 查看此链接:
“希望这有助于
=========================================================
增编:
错误#1:
原因:
您定义了自己的方法LinkedList.set(),如下所示:public void set(int index,String d){…}
。所以如果你想使用它,你需要给它两个参数:一个整数索引和一个字符串变量。您使用零参数调用它:str.set()
。你需要正确地调用它。。。或者打别的电话
错误#2:
原因:
这里有两个问题:
1) System.out.println()中的+
需要一个字符串,您给它一个返回“void”的函数。如果“listAll()”返回一个“String”,则该部分将正常工作
2) 第二个问题是“LinkedList.listAll()”需要一个参数:字符串d
。你没有传递任何论点。这与上面的错误#`是同一个问题
错误#3:
原因:
同样,System.out.println()中的+
需要一个字符串,您给它一个返回“void”的函数。一种解决方案是将“printRev()”更改为返回字符串。更好的解决方案可能是让“printRev()调用System.out.println()本身
再一次-'希望这有帮助 你的问题是什么?“我有3个错误…”什么错误?你能发布编译器的输出吗?我用主程序和错误编辑了帖子请不要单独发布链接。你应该发布链接内容的相关部分,以便做出适当的回答(如果可能的话)。这不是答案。这是一个有点广泛的评论,但它应该是一个评论。非常感谢@FoggyDay,这是一个很大的帮助
----jGRASP exec: javac -g LinkedListDemo.java
LinkedListDemo.java:8: set(int,java.lang.String) in LinkedList cannot be applied to ()
System.out.println("Strings in the Linked List: " + str.set());
^
LinkedListDemo.java:20: listAll(java.lang.String) in LinkedList cannot be applied to ()
System.out.println("Number of T's in the List: " + str.listAll());
^
LinkedListDemo.java:27: 'void' type not allowed here
System.out.print("Components of the list in Order: " + str.printReverse());
^
3 errors
----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
----jGRASP exec: javac -g LinkedListDemo.java
LinkedListDemo.java:8: set(int,java.lang.String) in LinkedList cannot be applied to ()
System.out.println("Strings in the Linked List: " + str.set());
LinkedListDemo.java:20: listAll(java.lang.String) in LinkedList cannot be applied to ()
System.out.println("Number of T's in the List: " + str.listAll());
LinkedListDemo.java:27: 'void' type not allowed here
System.out.print("Components of the list in Order: " + str.printRev