2013-05-15 41 views
0

我正在学习如何最近使用LinkedList,它一切正常,但如果我将它用作直接方法(不使用方法),它有很多错误。通过方法应用LinkedList

我想要做的是读取文件文本并将其保存到LinkedList中。

这是我到目前为止有:

public static void main(String[] args) { 

    Node<String> workflowHead = null; 
    Node<String> workflowTail = null; 

    try { 
     int i = 0; 
     Scanner in = new Scanner(new FileInputStream("workflow.txt")); 
     while (in.hasNextLine()) { 
      if (i == 0) { 
       workflowHead = new Node<String>(in.nextLine()); 
       workflowTail = workflowHead; 
      } 
      else { 
       workflowTail.next = new Node<String>(in.nextLine()); 
       workflowTail = workflowTail.next; 
      } 
      i++; 
     } 
     in.close(); 
    } catch (FileNotFoundException e) { 
     System.out.println(e.getMessage()); 
    } 
} 

以上就是我说的“直接方法”,而不使用方法。

现在告诉我,如何通过使用方法实现所有这一切?

上面的代码工作正常,但我需要将其转换为使用方法的代码。

我试过这样,但它悲惨的失败了:

public static void main(String[] args) { 

    Node<String> workflowHead = null; 
    Node<String> workflowTail = null; 

    workflowHead.read(workflowHead, workflowTail); 
} //End of main 

public class Method { 

public void read(Object head, Object tail) { 
    try { 
     int i = 0; 
     Scanner in = new Scanner(new FileInputStream("workflow.txt")); 
     while (in.hasNextLine()) { 
      if (i == 0) { 
       head = new Node<String>(in.nextLine()); 
       tail = head; 
      } 
      else { 
       tail.next = new Node<String>(in.nextLine()); 
       tail = tail.next; 
      } 
      i++; 
     } 
     in.close(); 
    } catch (FileNotFoundException e) { 
     System.out.println(e.getMessage()); 
    } 
} 

我做了什么错?

回答

0

当您尝试使用head = ...tail = ...(它不会在您当前的代码中产生任何编译时错误或错误,但是一旦您开始扩展它来测试它时)就会出现一个问题。这些将做什么只是给本地变量headtail一个新的价值。它不会更改workflowHeadworkflowTail

在更技术层面上,当您传入Object参数时,参数和原始对象都将指向相同的数据。所以修改该对象将改变两者。但是,当您重新分配参数=时,参数现在将指向一个不同的对象,而原始仍将指向原始对象。

你可能想要的是类变量而不是方法参数。

此外,workflowHead.read(...)不会工作,因为readMethod类的成员(可能不是理想的命名),但wordflowHeadNode型。

传递文件名而不是在方法中硬编码它可能是一个更好的主意。 “更好”的方式是从命令行传入,但出于测试目的,这并不理想。

我可能会做这样的事情:

public static void main(String[] args) { 
    MyLinkedList<String> linkedList = new MyLinkedList<String>(); 
    linkedList.read("workflow.txt"); 
} 

static class MyLinkedList<T> 
{ 
    private Node<T> head = null, tail = null; 
    public void read(String filename) 
    { 
    // ... - you can freely reassign head and tail here 
    } 

    // I'm assuming this class is defined elsewhere, 
    // but defining it here would likely make more sense 
    private class Node<T> 
    { 
    // ... 
    } 
} 

这可能是更好地把你的整个read方法进入MyLinkedList构造。

如果上面是超出你目前了解,这可能是更容易理解:

public static void main(String[] args) { 
    read("workflow.txt"); 
} 

static Node<T> head = null, tail = null; // these are class variables 
static void read(String filename) 
{ 
    // ... 
    Scanner in = new Scanner(new FileInputStream(filename)); 
    // ... - you can freely reassign head and tail here 
} 
+0

我做的第一个,但我只有一个问题,这行代码; list.read(“工作流程。TXT“); 误差表示; 找不到符号 符号:方法读(字符串) 位置:类型链表的变量列表。如果是完全一样的写的代码

+0

@ E-zardYusof,包括类和方法定义,那么它应该可以工作,你可以将代码粘贴到https://ideone.com(编译+运行代码)或http://pastebin.com/的地方,如果你愿意,我可以看看。 – Dukeling