如何显示void方法内容

时间:2014-03-16 01:36:01

标签: java

编译程序时遇到3个错误,我尝试了其他几种方法,但仍然无法实现。程序必须显示LinkedList的传入内容,然后查找传入的String索引并将其打印出来,然后反向打印出LinkedList的内容。执行所有这些操作的方法是LinkedList类中的void方法。 这是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

这是主程序:

    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 个答案:

答案 0 :(得分:0)

1)将来,请尝试更具体。始终剪切/粘贴确切的错误消息,并指出相关的代码行(如果可能)。

2)即使您遇到编译错误,System.out.println(curr)可能也不是您想要的。

建议:

3)看看这个链接:

Printing out a linked list using toString

&#39;希望有所帮助...

=============================================== ==========

附录:

错误#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()中的+期待字符串,你给它一个返回&#34; void&#34的函数;。如果&#34; listAll()&#34;返回&#34;字符串&#34;,该部分可以工作。

2)第二个问题是&#34; LinkedList.listAll()&#34;期待一个论点:String d。你没有传递任何论据。这与ERROR#`上面的问题相同。

错误#3:

 LinkedListDemo.java:27: 'void' type not allowed here
      System.out.print("Components of the list in Order: " + str.printRev

原因:

同样,System.out.println()中的+期待字符串,你给它一个返回&#34; void&#34的函数;。一种解决方案是改变&#34; printRev()&#34;返回一个字符串。更好的解决方案可能是&#34; printRev()&#34;调用System.out.println()本身。

再次 - &#39;希望有所帮助!