我在编译程序时遇到了3个错误,我尝试了一些其他方法,但仍然没有成功。程序必须显示LinkedList中传入的内容,然后查找传入的String的索引并将其打印出来,然后反向打印LinkedList的内容。执行所有这些操作的方法都是LinkedList类中的void方法。这是链接列表:
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
这是主程序:
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList str = new LinkedList();
str.set(0,"M");
System.out.println("Strings in the Linked List: " + str.set());
str.enumerate();
str.addToFront("M");
str.addToFront("T");
str.addToFront("N");
str.addToFront("T");
str.addToFront("L");
str.addToFront("H");
str.listAll("T");
System.out.println("Number of T's in the List: " + str.listAll());
str.enumerate();
str.addToFront("T");
str.addToFront("N");
str.addToFront("T");
System.out.print("Components of the list in Reverse: " + str.printReverse());
str.enumerate();
}// Main
}// 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( 查看此链接:
使用toString 打印链接列表
希望能有所帮助。。。
=============================================
附录:
错误#1:
----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());
病因:
您定义了自己的方法LinkedList.set((,如下所示:public void set(int index, String d) {...}
。因此,如果你想使用它,你需要给它两个参数:一个整数索引和一个字符串变量。您使用零参数调用它:str.set()
。你需要正确地称呼它。。。或者打电话给其他人。
错误#2:
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());
病因:
这里有两个问题:
1( System.out.println((中的+
需要一个字符串,您给它一个返回"void"的函数。如果"listAll(("返回一个"String",那么该部分就可以工作了。
2( 第二个问题是"LinkedList.listAll(("需要一个参数:String d
。你没有传递任何论点。这与上面的错误#`是相同的问题。
错误#3:
LinkedListDemo.java:27: 'void' type not allowed here
System.out.print("Components of the list in Order: " + str.printRev
病因:
同样,System.out.println((中的+
需要一个字符串,您给它一个返回"void"的函数。一种解决方案是更改"printRev(("以返回字符串。更好的解决方案可能是让"printRev(("调用System.out.println((本身。
再次——"希望有帮助!"!